LLVM 源码分析 (二)ModulePass 类

上一篇写的是关于pass 类  这个类是一个抽象类  具体实现是通过例如modulePass FunctionPass实现的
本文将重点介绍module pass 
这个Pass 可以操作整个module  是以文件进行区分的。ModulePass 可以操作Module 下的大部分 基本是万能的但是不是最方便的。我们需要根据需要选择 FunctionPass  LoopPass 等
vs2008
LLVM 源码分析 (二)ModulePass 类_第1张图片
 
doxygen
LLVM 源码分析 (二)ModulePass 类_第2张图片

主要新出现的类成员

runOnModule - Virtual method overriden by subclasses to process the module being operated on. 

 /// runOnModule - Virtual method overriden by subclasses to process the module
/// being operated on.


virtual bool runOnModule(Module &M) = 0;   
bool runOnModule(Module &M) {
      (*Out) << Banner << M;
      return false;
    }


源码来自PrintSCC.cpp
// run - Print out SCCs in the call graph for the specified module.
bool CallGraphSCC::runOnModule(Module &M) {
  CallGraphNode* rootNode = getAnalysis<CallGraph>().getRoot();
  unsigned sccNum = 0;
  errs() << "SCCs for the program in PostOrder:";
  for (scc_iterator<CallGraphNode*> SCCI = scc_begin(rootNode),
         E = scc_end(rootNode); SCCI != E; ++SCCI) {
    const std::vector<CallGraphNode*> &nextSCC = *SCCI;
    errs() << "\nSCC #" << ++sccNum << " : ";
    for (std::vector<CallGraphNode*>::const_iterator I = nextSCC.begin(),
           E = nextSCC.end(); I != E; ++I)
      errs() << ((*I)->getFunction() ? (*I)->getFunction()->getName()
                                     : "external node") << ", ";
    if (nextSCC.size() == 1 && SCCI.hasLoop())
      errs() << " (Has self-loop).";
  }
  errs() << "\n";


  return true;
}



下面给出源码中使用MudulePass的一个例子
 
#include "llvm/ADT/SetVector.h"
#include "llvm/Pass.h"

namespace llvm {

class Type;
class Value;

class FindUsedTypes : public ModulePass {
  SetVector<Type *> UsedTypes;
public:
  static char ID; // Pass identification, replacement for typeid
  FindUsedTypes() : ModulePass(ID) {
    initializeFindUsedTypesPass(*PassRegistry::getPassRegistry());
  }

  /// getTypes - After the pass has been run, return the set containing all of
  /// the types used in the module.
  ///
  const SetVector<Type *> &getTypes() const { return UsedTypes; }

  /// Print the types found in the module.  If the optional Module parameter is
  /// passed in, then the types are printed symbolically if possible, using the
  /// symbol table from the module.
  ///
  void print(raw_ostream &o, const Module *M) const;

private:
  /// IncorporateType - Incorporate one type and all of its subtypes into the
  /// collection of used types.
  ///
  void IncorporateType(Type *Ty);

  /// IncorporateValue - Incorporate all of the types used by this value.
  ///
  void IncorporateValue(const Value *V);

public:
  /// run - This incorporates all types used by the specified module
  bool runOnModule(Module &M);

  /// getAnalysisUsage - We do not modify anything.
  virtual void getAnalysisUsage(AnalysisUsage &AU) const {
    AU.setPreservesAll();
  }
};

} // End llvm namespace



你可能感兴趣的:(LLVM 源码分析 (二)ModulePass 类)