[llvm-dev] How can I use llvm::LoopInfo in the runOnModule method?

J Skye via llvm-dev llvm-dev at lists.llvm.org
Mon Apr 1 23:47:39 PDT 2019


Hi all,


I tried to have a LoopInfo object in a function pass, and add addRequired
in getAnalysisUsage, and then use getAnalysis in runOnFunction(). It worked
OK.
Now I want to have a module pass to traverse the functions, and similarly,
I want to have to loop information of the functions.
When I did the above in runOnModule, and build the module pass, the
following error popped out, and the compile did not succeed:

[ 50%] Building CXX object src/CMakeFiles/SkeletonPass.dir/Skeleton.cpp.o
/home/**/Desktop/skeleton/src/Skeleton.cpp: In member function
‘virtual bool llvm::SkeletonPass::runOnModule(llvm::Module&)’:
/home/**/Desktop/skeleton/src/Skeleton.cpp:47:43: error: no matching
function for call to
‘llvm::SkeletonPass::getAnalysis(llvm::Function*&)’
     LoopInfo &LI = getAnalysis<LoopInfo>(F);

...
home/**/Work/llvm-6.0.1.src/include/llvm/PassAnalysisSupport.h: In
instantiation of ‘llvm::AnalysisUsage&
llvm::AnalysisUsage::addRequired() [with PassClass = llvm::LoopInfo]’:
/home/**/Desktop/skeleton/src/Skeleton.cpp:24:38:   required from here
/home/**/Work/llvm-6.0.1.src/include/llvm/PassAnalysisSupport.h:67:39:
error: ‘ID’ is not a member of ‘llvm::LoopInfo’
     return addRequiredID(PassClass::ID);
                                       ^

Here is my source code:

#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Module.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/Dominators.h"
#include "llvm/Analysis/LoopInfo.h"

namespace llvm
{
    class SkeletonPass : public ModulePass
    {
    public:
        static char ID;
        SkeletonPass() : ModulePass(ID) {}
        bool runOnModule(Module &M) override;
        virtual StringRef getPassName() const override
        {
            return "Skeleton";
        }
        virtual void getAnalysisUsage(AnalysisUsage &AU) const override {
            errs() << "get analysis usage method.\n";
            AU.addRequired<LoopInfo>();
        }
    };

    Pass *createSkeletonPass()
    {
        return new SkeletonPass();
    }

    char SkeletonPass::ID = 0;
    Pass *createSkeletonPass();
    void initializeSkeletonPass(PassRegistry &Registry);


} // namespace llvm

using namespace llvm;
using namespace std;


bool SkeletonPass::runOnModule(llvm::Module &M){
    errs() << "=============start=============\n";
    auto F = M.getFunction("main");
    LoopInfo &LI = getAnalysis<LoopInfo>(F);

    LI.print(errs());
    errs() << "==========================\n";
}

static RegisterPass<SkeletonPass> X("run", "Hello world pass", false, false);

The version of LLVM is 6.0.1, can anyone tell me the correct way to handle
this in a module pass? Thanks a lot!

Best,
Liu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190402/e5bdc3b5/attachment.html>


More information about the llvm-dev mailing list