[LLVMdev] getAnalysis<LoopInfo> from ModulePass

Mariusz Grad mariusz.grad at gmail.com
Wed Sep 15 06:20:46 PDT 2010


On Sep 15, 2010, at 2:49 PM, ether zhhb wrote:

Hi Ether,

Thank you for your help.

> you may try &getAnalysis<LoopInfo> (F);
> please have a look at the declaration of  getAnalysis for detail information.


Whenever I tried to do that I during compilation received following error:
error: no matching function for call to '<unnamed>::bb_info::getAnalysis(const llvm::Function&) const'

The problem was in module iterator, exactly here:
     Module::const_iterator f_it = M.begin(), f_ite = M.end(); 
It shouldn't be const iterator, this works:
     Module::iterator f_it = M.begin(), f_ite = M.end(); 

I figured it out after looking on other code which was presented on this mailing list (thanks for that).
Here is that other code (which works just fine).

Greetings, and thx for the help.

# ========================== #

#include "llvm/Constants.h"
#include "llvm/Support/Debug.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/IntrinsicInst.h"
#include "llvm/Module.h"
#include "llvm/Pass.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/Analysis/LoopPass.h"
#include "llvm/Support/Compiler.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/Streams.h"

#include "llvm/Support/raw_ostream.h" 

using namespace llvm;

namespace {
  class ModuleLoop : public ModulePass {
   public:
     static char ID; // Pass identification, replacement for typeid
     virtual bool runOnModule(Module &M);
     virtual void getAnalysisUsage(AnalysisUsage &AU) const;
     ModuleLoop() : ModulePass(&ID) {}
 };
}

char ModuleLoop::ID = 0;
static RegisterPass<ModuleLoop> X("moduleloop", "LoopPass within ModulePass");

// ***************************************************************************

void ModuleLoop::getAnalysisUsage(AnalysisUsage &AU) const {
 AU.setPreservesAll();
 AU.addRequired<LoopInfo>();
}


bool ModuleLoop::runOnModule(Module &M) {

 for (Module::iterator func_iter = M.begin(), func_iter_end = M.end();
     func_iter != func_iter_end; ++func_iter) {
   Function &F = *func_iter;

   if (!F.isDeclaration()) {
     LoopInfo &LI = getAnalysis<LoopInfo>(F);
     for (Function::const_iterator bb_it = func_iter->begin(), bb_ite = func_iter->end(); bb_it != bb_ite;  ++bb_it) 
       errs() << bb_it->getName() << " " <<  LI.getLoopDepth(bb_it) << "\n";
   }
 }
 return false;
}


-- 
Mariusz Grad







More information about the llvm-dev mailing list