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;}PassManager.cpp
// 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;}2.runOnFunction - Virtual method overriden by subclasses to do the per-function processing of the pass.
和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");- 1楼ychw3658小时前
- 图片都有点不太正常 鼠标左键按住拖到就可以去另外的网页里面看了