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