Why your code cannot be created
AU.addRequired<typename passclass>() needs the LLMV::Pass type, however what you are going through is LoopInfo , which is just an LLVM inner class for serving loop information. It does not have an ID field.
LoopInfoWrapperPass should be used instead
If you want to get information about the loop. Try changing it to AU.addRequired<LoopInfoWrapperPass> , as shown in LLVM. Write a new Pass document . LoopInfoWrapperPass used to generate LoopInfo .
LoopInfo should be obtained from the aisle
Your code also has a problem on how to get LoopInfo , you are trying to use new to create LoopInfo , what you get will be empty LoopInfo .
The code should work for your question
Below is a modified version of your code that can print the expected information.
#include "llvm/ADT/Statistic.h" #include "llvm/IR/Function.h" #include "llvm/Support/raw_ostream.h" #include "iostream" #include "llvm/Pass.h" #include "llvm/IR/InstIterator.h" #include <llvm/IR/Instructions.h> #include <llvm/Analysis/LoopInfo.h> using namespace llvm; namespace { struct CFG : public FunctionPass { static char ID; // Pass identification, replacement for typeid CFG() : FunctionPass(ID) {} void getAnalysisUsage(AnalysisUsage &AU) const override { AU.setPreservesCFG(); AU.addRequired<LoopInfoWrapperPass>(); } bool runOnFunction(Function &F) override { LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); errs().write_escaped(F.getName()); errs() << " : "; for( Function::iterator b = F.begin() , be = F.end(); b != be; ++b){ errs() << "\n\t BB : "; bool isLoop = LI.getLoopFor(b); if(isLoop){ errs() << "loop{"; } for(BasicBlock::iterator i = b->begin() , ie = b->end(); i!=ie; ++i){ if( isa<CallInst>(&(*i)) || isa<InvokeInst>(&(*i))){ errs() << cast<CallInst>(&(*i))->getCalledFunction()->getName() << "\t"; } } if(isLoop){ errs() << "}"; } } errs() << '\n'; return false; } }; } char CFG::ID = 0; static RegisterPass<CFG> X("CFG", "Gen CFG",true ,true);
For the following code in LLVM opt :
#include <stdio.h>
The output will be:
foo : BB : BB : loop{} BB : loop{} BB : loop{} BB : main : BB : llvm.memset.p0i8.i64 foo printf
source share