LLVM 源码分析(四)FunctionPass
FunctionPass 主要增加doInitaliztion() ;doFinalization();runOnFunction();
1.
bool FunctionPass::doInitialization(Module &) { // By default, don't do anything. return false;}bool FunctionPass::doFinalization(Module &) { // By default, don't do anything. return false;}// Implement doInitialization and doFinalizationbool BBPassManager::doInitialization(Module &M) { bool Changed = false; for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) Changed |= getContainedPass(Index)->doInitialization(M); return Changed;}bool BBPassManager::doFinalization(Module &M) { bool Changed = false; for (unsigned Index = 0; Index < getNumContainedPasses(); ++Index) Changed |= getContainedPass(Index)->doFinalization(M); return Changed;} 和runOnModule 类似都是随着FunctionPass 的执行而执行
#include "llvm/Pass.h"#include "llvm/Function.h"#include "llvm/Support/raw_ostream.h"namespace { // Hello - The first implementation, without getAnalysisUsage. struct Hello : public FunctionPass { static char ID; // Pass identification, replacement for typeid Hello() : FunctionPass(ID) {} virtual bool runOnFunction(Function &F) { ++HelloCounter; errs() << "Hello: "; errs().write_escaped(F.getName()) << '\n'; return false; } bool doInitialization(Module &F){ // By default, don't do anything. errs()<<"Module test"; return false; } };}char Hello::ID = 0;static RegisterPass<Hello> X("hello", "Hello World Pass");