2014-09-23 71 views
1

我想通過寫入函數通過從IR獲取循環信息。所以我遵循一些例子,寫下如下。我不太熟悉寫傳球和傳球經理。訪問函數中的loopinfo LLVM錯誤通過

#include <iostream> 
#include "llvm/LLVMContext.h" 
#include "llvm/Module.h" 
#include "llvm/Function.h" 
#include "llvm/BasicBlock.h" 
#include "llvm/Analysis/LoopInfo.h" 
#include "llvm/Support/IRReader.h" 
#include "llvm/Support/SourceMgr.h" 
#include "llvm/Support/raw_ostream.h" 
#include "llvm/Analysis/LoopInfo.h" 
#include "llvm/Analysis/LoopPass.h" 
#include "llvm/Pass.h" 
#include "llvm/PassManager.h" 

using namespace llvm; 

namespace { 
    class DetectLoop: public FunctionPass { 
    public: 
     DetectLoop() : FunctionPass(ID) {} 

     static char ID; 

     virtual void getAnalysisUsage(AnalysisUsage &AU) const { 
      AU.addRequired<LoopInfo>();//I'm not sure if it's correct here *****1***** 
     } 

     virtual bool runOnFunction(Function &F) { 
      if (!F.isDeclaration()) 
       LoopInfo &li = getAnalysis<LoopInfo>(F);//*****2***** 
      for (Function::iterator I = F.begin(); I != F.end(); I++) { 
       BasicBlock *BB = I; 
       for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE;) { 
        Instruction &I = *BI++; 
        //did noting inside 
       } 
      } 
      return false; 
     } 
    }; 
} 

char DetectLoop::ID = 0; 

int main(int argc, char** argv) 
{ 
    if (argc < 2) { 
     errs() << "Expected an argument - IR file name\n"; 
     exit(1); 
    } 

    SMDiagnostic Err; 
    std::cout<<argv[1]<<std::endl; 
    Module *Mod = ParseIRFile(argv[1], Err, getGlobalContext()); 
    Err.Print(argv[0], errs()); 


    if (Mod) { 
     PassManager PM; 
     PM.add(new DetectLoop()); 
     PM.add(new LoopInfo());//*****3***** 
     PM.run(*Mod); 
    } 
    else { 
     std::cout << "Mod is null" << std::endl; 
    } 

    return 0; 
} 

當我運行此程序,它只是給我看segmentation error(core dumped)

但是當我註釋掉addRequired錯誤味精我得到的是

IRparser: PassManager.cpp:1200: virtual llvm::Pass* llvm::PMDataManager::getOnTheFlyPass 
(llvm::Pass*, llvm::AnalysisID, llvm::Function&): Assertion `0 && "Unable to find on the fly pass"' failed. 
Stack dump: 
0. Running pass 'Function Pass Manager' on module '../../testcase/forloop1.ll'. 
1. Running pass 'Unnamed pass: implement Pass::getPassName()' on function '@main' 
Aborted (core dumped) 

我已經打上3個名額我不確定哪個是正確的。任何人都可以幫助我嗎?

回答

0

我以前有過這個問題。搜索了幾個答案之後,我找到了解決方案。 您應該更改下面兩個語句的位置:

PM.add(new DetectLoop()); 
    PM.add(new LoopInfo());//*****3***** 

由於LoopInfo通行證必須自己通過之前,臨時用戶。

2

如果你在一個模塊使用它:

LoopInfo &li = getAnalysis<LoopInfo>(F) 

如果你在一個函數中使用它:

LoopInfo &li = getAnalysis<LoopInfo>()