Questions about trip count

Hi,

I would propose to first check if the trip count is calculated correctly. I would do this with opt by calling:

opt -mem2reg -loopsimplify -indvars -scalar-evolution -analyze prog.ll

At the end of the output the tripcount of every loop should be written.

To get the trip count in your pass you should ask ScalarEvolution.

ScalarEvolution *SE;
Loop *L;
SE->getBackedgeTakenCount(L);

If you need help trying this please ask back.

Cheers
Tobi

Thanks Tobias. Your answer helped me a lot. Now I can get the trip count of the loop like this:

#define DEBUG_TYPE “hello”
#include “llvm/Pass.h”
#include “llvm/Function.h”
#include “llvm/Support/raw_ostream.h”
#include “llvm/ADT/Statistic.h”
#include “llvm/Analysis/LoopInfo.h”
#include “llvm/Analysis/ScalarEvolution.h”

using namespace llvm;

STATISTIC(HelloCounter, “Counts number of functions greeted”);

namespace {

struct Hello: public FunctionPass {
static char ID; // Pass identification, replacement for typeid
Hello() : FunctionPass(&ID) {}

void getLoopInfo(const Function& F) const {

const LoopInfo *LI = &getAnalysis ();
ScalarEvolution *SE = &getAnalysis ();
Function::const_iterator BB = F.begin(), E = F.end();

for (; BB != E; ++BB) {
const Loop* L = LI->getLoopFor(&*BB);
if (L != NULL) {
errs() << "Trip count: " << *SE->getBackedgeTakenCount(L);
L->dump();
}
}
}

virtual bool runOnFunction(Function &F) {

HelloCounter++;
errs() << "Hello: ";
errs().write_escaped(F.getName()) << ‘\n’;
getLoopInfo(F);
return false;
}

// We don’t modify the program, so we preserve all analyses
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired ();
AU.addRequired ();
}
};
}

char Hello::ID = 0;
static RegisterPass Y(“hello”,
“Hello World Pass (with getAnalysisUsage implemented)”);

But when I use that same function within a class I get some compiler errors. I’m trying to use this function like this:

#define DEBUG_TYPE “hello”
#include “llvm/Pass.h”
#include “llvm/Function.h”
#include “llvm/Support/raw_ostream.h”
#include “llvm/ADT/Statistic.h”
#include “llvm/Analysis/LoopInfo.h”
#include “llvm/Analysis/ScalarEvolution.h”

using namespace llvm;

STATISTIC(HelloCounter, “Counts number of functions greeted”);

namespace {

struct Hello: public FunctionPass {
static char ID; // Pass identification, replacement for typeid
Hello() : FunctionPass(&ID) {}

class testLoopInfo {

void getLoopInfo(const Function& F) const {

const LoopInfo *LI = &getAnalysis ();
ScalarEvolution *SE = &getAnalysis ();
Function::const_iterator BB = F.begin(), E = F.end();
for (; BB != E; ++BB) {

const Loop* L = LI->getLoopFor(&*BB);
if (L != NULL) {
errs() << "Trip count: " << *SE->getBackedgeTakenCount(L);
L->dump();
}
}
}

};

virtual bool runOnFunction(Function &F) {

HelloCounter++;
errs() << "Hello: ";
errs().write_escaped(F.getName()) << ‘\n’;
//getLoopInfo(F);
return false;
}

// We don’t modify the program, so we preserve all analyses
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.setPreservesAll();
AU.addRequired ();
AU.addRequired ();
}
};
}

char Hello::ID = 0;
static RegisterPass Y(“hello”,
“Hello World Pass (with getAnalysisUsage implemented)”);

I get the following errors:

**** Build of configuration Debug for project HelloLLVM ****

make all
llvm[0]: Compiling Hello.cpp for Release build (PIC)
Hello.cpp: In member function ‘void::Hello::testLoopInfo::getLoopInfo(const llvm::Function&) const’:
Hello.cpp:37: error: no matching function for call to ‘llvm::Pass::getAnalysis()’
/home/douglas/Programas/llvm-2.7/include/llvm/PassAnalysisSupport.h:209: note: candidates are: AnalysisType& llvm::Pass::getAnalysis() const [with AnalysisType = llvm::LoopInfo]
Hello.cpp:38: error: no matching function for call to ‘llvm::Pass::getAnalysis()’
/home/douglas/Programas/llvm-2.7/include/llvm/PassAnalysisSupport.h:209: note: candidates are: AnalysisType& llvm::Pass::getAnalysis() const [with AnalysisType = llvm::ScalarEvolution]
Hello.cpp: In member function ‘virtual bool::Hello::runOnFunction(llvm::Function&)’:
Hello.cpp:57: error: ‘getLoopInfo’ was not declared in this scope
make: ** [/home/douglas/Programas/llvm-2.7/lib/Transforms/Hello/Release/Hello.o] Erro 1

Anyone know how can I fix it?

Thanks a lot,

Douglas

Umm, pass in a "Pass* P", then use "P->getAnalysis<LoopInfo> ()"? C++
isn't Java :slight_smile:

-Eli