getAnalysis<LoopInfo> from ModulePass

Hi,

I wrote tiny ModulePass which iterates over functions and then uses getAnalysis<LoopInfo> in order to get informations about the loop.
It compiles smoothly, but whenever I try to run it I got error like this:
opt: .. PassAnalysisSupport.h:203: AnalysisType& llvm::Pass::getAnalysisID(const llvm::PassInfo*) const [with AnalysisType = llvm::LoopInfo]: Assertion `ResultPass && "getAnalysis*() called on an analysis that was not " "'required' by pass!"' failed.
What actually this assertion means?

Whenever I change the ModulePass to the FunctionPass the code bellow works just fine.

Here is the code:
# ============================== #

#define DEBUG_TYPE "bb_info"
#include "llvm/Pass.h"
#include "llvm/Function.h"
#include <llvm/Module.h>
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/LoopInfo.h"

using namespace llvm;

namespace {
  struct bb_info: public ModulePass {
    static char ID;

    bb_info() : ModulePass(&ID) {}

      void getLoopInfo(const Function& F) const {
        const LoopInfo *LI = &getAnalysis<LoopInfo> ();
        Function::const_iterator BB = F.begin(), E = F.end();
        for (; BB != E; ++BB)
          errs() << BB->getName() << " " << LI->getLoopDepth(&*BB) << "\n";
    };

    virtual bool runOnModule(Module &M) {
      Module::const_iterator f_it = M.begin(), f_ite = M.end();
      for ( ; f_it != f_ite; ++f_it )
      {
        errs() << f_it->getName() << '\n';
        if (!f_it->isDeclaration())
          getLoopInfo(*f_it);
      }
      return true;
    }

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

char bb_info::ID = 0;
static RegisterPass<bb_info> Y("bb_info", "testing ..");

hi,

Hi,

I wrote tiny ModulePass which iterates over functions and then uses getAnalysis<LoopInfo> in order to get informations about the loop.
It compiles smoothly, but whenever I try to run it I got error like this:
opt: .. PassAnalysisSupport.h:203: AnalysisType& llvm::Pass::getAnalysisID(const llvm::PassInfo*) const [with AnalysisType = llvm::LoopInfo]: Assertion `ResultPass && "getAnalysis*() called on an analysis that was not " "'required' by pass!"' failed.
What actually this assertion means?

Whenever I change the ModulePass to the FunctionPass the code bellow works just fine.

Here is the code:
# ============================== #

#define DEBUG_TYPE "bb_info"
#include "llvm/Pass.h"
#include "llvm/Function.h"
#include <llvm/Module.h>
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/Statistic.h"
#include "llvm/Analysis/LoopInfo.h"

using namespace llvm;

namespace {
struct bb_info: public ModulePass {
static char ID;

bb_info() : ModulePass(&ID) {}

 void getLoopInfo\(const Function&amp; F\) const \{
   const LoopInfo \*LI =  &amp;getAnalysis&lt;LoopInfo&gt; \(\);

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

best regards
ether

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;
}