2014-06-11 54 views
2

如果我從LLVM的外部構建庫,那麼定義和正確「註冊」分析組以及作爲該組的一部分的傳遞的最小代碼是什麼?如果通過取決於先前分析的結果,它將會怎樣?在LLVM中動態加載分析傳遞和分析組的最小代碼

writing an LLVM pass上的文檔提供了關於在不同場景下應該做什麼的信息,但它分佈在很多部分,有些似乎與最新的LLVM的源代碼和註釋相矛盾。我正在尋找所需文件的完整源代碼,就像他們提供的基本Hello World Pass一樣。

+0

爲了使它更清楚,當我試圖按照寫作的LLVM通文檔中給出的說明,我得到一個錯誤「兩次有相同論點的通行證......試圖登記」 –

回答

1

我能弄明白這一點。這是您需要的代碼。

在標題:

class MyAnalysis { 
public: 
    static char ID; 
    MyAnalysis(); 
    ~MyAnalysis(); 
} 

在來源:

struct MyPass : public llvm::ImmutablePass, public MyAnalysis { 
    static char ID; 
     MyPass() : llvm::ImmutablePass(ID) { 
    } 

    /// getAdjustedAnalysisPointer - This method is used when a pass implements 
    /// an analysis interface through multiple inheritance. 
    virtual void *getAdjustedAnalysisPointer(AnalysisID PI) { 
     if (PI == &MyAnalysis::ID) 
     return (MyAnalysis*)this; 
     return this; 
    } 
}; 

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

    void getAnalysisUsage(AnalysisUsage &AU) const { 
    AU.addRequired<MyAnalysis>(); 
    } 
    virtual bool runOnFunction(Function &F) { 
    MyAnalysis& info =getAnalysis<MyAnalysis>(); 
     ... 
     //use the analysis here 
     ... 
    } 
} 

char MyAnalysis::ID = 0; 
static llvm::RegisterAnalysisGroup<MyAnalysis> P("My Super Awesome Analysis"); 

char MyPass::ID = 0; 
static llvm::RegisterPass<MyPass> X("my-pass", "My pass which provides MyAnalysis", true, true); 

//This is the line that is necessary, but for some reason is never mentioned in the 
//llvm source or documentation. This adds MyPass to the analysis group MyAnalysis. 
//Note that the "true" arguemnt to the macro makes MyPass the default implementation. 
//Omit it to register other passes. 
static llvm::RegisterAnalysisGroup<MyAnalysis,true> Y(X); 


char UsingPass::ID = 0; 
static RegisterPass<UsingPass> X("using-pass", "Some other pass that uses MyAnalysis", true, true);