LLVM中动态加载的Analysis Pass和Analysis Group的最小代码

时间:2014-06-11 23:35:13

标签: llvm

如果我从LLVM外部构建一个库,那么定义的最小代码是什么,并正确地注册"分析小组和该小组的一部分通行证?如果通过取决于先前分析的结果,它将如何看待?

writing an LLVM pass上的文档提供了有关在不同场景中应该做什么的信息,但它分布在许多部分,其中一些似乎与最新LLVM的源代码和注释相矛盾。我正在寻找所需文件的完整源代码,就像它们提供基本的Hello World Pass一样。

1 个答案:

答案 0 :(得分: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);