LLVM - A pass 调用 B pass

490 阅读1分钟

方式1. 直接 new DominatorTree pass 对象, 调用其 runOnFunction() 方法

virtual bool runOnFunction(Function &F) {
  DominatorTree* T = new DominatorTree(); // new pass instance
  T->runOnFunction(*F); // call other pass's runOnFunction()
  T->print(errs());
  return false;
}

方式2. 重写 getAnalysisUsage() 注册 依赖的 pass class

namespace {
  struct Mypass : public FunctionPass {
    static char ID;

    Mypass() : FunctionPass(ID) {}

    /**
     * 重写 getAnalysisUsage() , 添加 依赖的 pass
     */
    virtual void getAnalysisUsage(AnalysisUsage &AU) const {
      AU.addRequired<DominatorTree>(); // 【注册】添加依赖的 pass【类型】
    }

    virtual bool runOnFunction(Function &F) {
      DominatorTree& DT = getAnalysis<DominatorTree>(F); // 【获取】添加依赖的 pass【类型】对应的【对象】
      DT->runOnFunction(*F);
      DT->print(errs());
      return false;
    }
  };
}

char Mypass::ID = 0;
static RegisterPass<Mypass> X(
  "mypass",
  "My test analysis",
  true,
  true);

方式3. 通过 PassManager 调用 其他的 pass

include/llvm/PassManager.h

Module* M;

...

PassManager pm;
pm.add(new ModulePass1);
pm.run(*M)