Search code examples
c++compiler-constructionllvmstatic-analysis

LLVM Error When Using a Pass from Another Pass


Here is my LLVM Pass:

#include <llvm/IR/Function.h>
#include <llvm/Pass.h>
#include <llvm/Support/raw_ostream.h>
#include <llvm/Analysis/MemoryDependenceAnalysis.h>

using namespace llvm;

namespace
{
    struct MemDepend : public FunctionPass
    {
        static char ID;
        MemDepend() : FunctionPass(ID){}

        virtual bool runOnFunction(Function &F)
        {
            MemoryDependenceAnalysis *MDA = &getAnalysis<MemoryDependenceAnalysis>();
            return false;
        }

        virtual void getAnalysisUsage(AnalysisUsage &AU) const
        {
              AU.setPreservesAll();
        }
    };
}

char MemDepend::ID = 0;
static RegisterPass<MemDepend> X("memdep", "Memory Pass", false, false);

When I try to compile it, I receive the following error:

In file included from /usr/local/include/llvm/Pass.h:388:0,
from /home/ahmad/Codes/LLVM/MyPass/myPass.cpp:3: /usr/local/include/llvm/PassAnalysisSupport.h: In instantiation of ‘AnalysisType& llvm::Pass::getAnalysis() const [with AnalysisType = llvm::MemoryDependenceAnalysis]’: /home/ahmad/Codes/LLVM/MyPass/myPass.cpp:18:79: required from here /usr/local/include/llvm/PassAnalysisSupport.h:223:37: error: no matching function for call to ‘llvm::Pass::getAnalysisID(void* (*)()) const’ return getAnalysisID(&AnalysisType::ID); ^

How can I compile it without error?


Solution

  • I believe the canonical way is

    MemoryDependenceResults &MDA =
        getAnalysis<MemoryDependenceWrapperPass>().getMemDep();
    

    And likewise,

    AU.addRequired<MemoryDependenceWrapperPass>();
    

    You can find examples of the above in the LLVM codebase.