getAnalysis*() called on an analysis that was not " "'required' by pass!

Hello,

I’d appreciate it if anyone can tell me what the error message means and point out what I am doing wrong.
Thank you.

I am trying to use the result of the DominatorTree analysis in my ModulePass class as explained in the section “Writing an LLVM Pass”.
http://llvm.org/docs/WritingAnLLVMPass.html#interaction
I called the method “addRequired()” in the method “getAnalysisUsage(AnalysisUsage&)”, but I get the following error message.

opt: /usr/local/llvm/llvm/include/llvm/PassAnalysisSupport.h:193: AnalysisType& llvm::Pass::getAnalysisID(const llvm::PassInfo*) const [with AnalysisType = llvm::DominatorTree]: Assertion `ResultPass && "getAnalysis*() called on an analysis that was not " “‘required’ by pass!”’ failed.

The following is the command I typed in:
opt -load /usr/local/llvm/install/lib/mymodulepass.so -mymodulepass <idct.bc > /dev/null

Here are the header and implementation files of my class with just the bare minimum that produces the error messages.
(MyModulePass.h)
#ifndef MYMODULEPASS_H
#define MYMODULEPASS_H
#include “llvm/Pass.h”

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

};

extern RegisterPass X_MyModulePass;
}

#endif

(MyModulePass.cpp)
#include
#include “MyModulePass.h”
#include “llvm/Module.h”
#include “llvm/Function.h”
#include “llvm/BasicBlock.h”
#include “llvm/Analysis/Dominators.h”
#include “llvm/Analysis/AliasAnalysis.h”

using namespace llvm;

// ID, register
char llvm::MyModulePass::ID = 0;
RegisterPassllvm::MyModulePass llvm::X_MyModulePass(“mymodulepass”, “My Module Pass”);

MyModulePass::MyModulePass() : ModulePass((intptr_t)&MyModulePass::ID) {
}

bool MyModulePass::runOnModule(Module &m) {
for (Module::const_iterator f = m.begin(); f != m.end(); ++f)
DominatorTree &dt = getAnalysis();

return false;
}

void MyModulePass::getAnalysisUsage(AnalysisUsage &info) const {
info.addRequired();
}

Hi,

bool MyModulePass::runOnModule(Module &m) {
        for (Module::const_iterator f = m.begin(); f != m.end(); ++f)
                DominatorTree &dt = getAnalysis<DominatorTree>();

        return false;
}

DominatorTree is a function-level analysis, while your pass is a
module-level one. It means you have to point the function you want the
analysis result for. Try using:

  getAnalysis<DominatorTree>(*f);

(It would probably require changing const_iterator to iterator)

Getting analysis result is valid only for function definitions, not
declarations. Hence, it is necessary to check f->isDeclaration() before
calling getAnalysis().

Wojtek