Hello;
I wrote this simple loop pass to collect the number of instructions in each loop of the program. The code is as follows-
#define DEBUG_TYPE “loopinst”
#include “llvm/Pass.h”
#include “llvm/Analysis/LoopPass.h”
#include “llvm/Support/raw_ostream.h”
#include “llvm/ADT/Statistic.h”
#include “llvm/Instructions.h”
#include “llvm/Analysis/LoopInfo.h”
using namespace llvm;
STATISTIC(LoopInstNum, “Counts number of instructions in a loop”);
namespace {
struct LoopInst : public LoopPass {
static char ID; // Pass identification, replacement for typeid
LoopInst() : LoopPass(ID) {}
virtual bool runOnLoop(Loop *L, LPPassManager &LPM) {
LoopInfo *LI = &getAnalysis();
for (Loop::block_iterator b = L->block_begin(), be = L->block_end();b != be; ++b)
{
for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end(); i != ie; ++i)
{
++LoopInstNum;
errs() << "Hello: ";
}
}
return false;
}
// We don’t modify the program, so we preserve all analyses
virtual void getAnalysisUsage(AnalysisUsage &AU) const {
AU.addRequired();
AU.addPreserved();
}
};
}
char LoopInst::ID = 0;
static RegisterPass X(“loop-inst”, “loop instruction Pass”);
I put it under llvm-src/lib/Transforms directory and ran a “make” from there to create the .so file. But when I run opt with the library, I get the following error -
opt -load=/home/arnie/llvm-development/llvm/Debug+Asserts/lib/LoopInst.so -loops -loop-inst a.s
opt: symbol lookup error: /home/arnie/llvm-development/llvm/Debug+Asserts/lib/LoopInst.so: undefined symbol: _ZNK4llvm8LoopBaseINS_10BasicBlockENS_4LoopEE11block_beginEv
Can anyone tell me what I am doing wrong?
Also what’s the difference between declaring a pass as a struct vs declaring it as a class. In the “writing a pass” tutorial the “Hello” pass has been declared as a struct but most (if not all) the LLVM passes are written as classes.
Thanks a lot;