2014-09-23 4 views
1

함수 패스를 작성하여 IR에서 루프 정보를 가져 오려고합니다. 그래서 몇 가지 예를 따라 다음과 같이 썼습니다. 필자는 패스 및 패스 관리자를 잘 쓰는 것에 익숙하지 않습니다.function pass에서 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),

을 보여하지만 주석 때 은 내가 가진 오류 MSG가 나는 3 곳을 표시 한

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) 

했다 addRequired 어느 것이 옳은지 확실하지 않습니다. 누구든지 저를 도울 수 있습니까?

답변

0

나는 전에이 질문을했다. 몇 가지 답변을 검색 한 후 해결책을 찾았습니다. 당신은 아래의 두 문장의 위치를 ​​변경해야합니다 LoopInfo 패스는 자신의 패스 전에 registed해야하기 때문에

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

. 당신이 모듈에서 사용하는 경우

2

:

LoopInfo &li = getAnalysis<LoopInfo>() 
: 당신이 함수에서 사용하는 경우

LoopInfo &li = getAnalysis<LoopInfo>(F)