LLVM API Documentation

IRPrintingPasses.cpp
Go to the documentation of this file.
00001 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // PrintModulePass and PrintFunctionPass implementations.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/IR/IRPrintingPasses.h"
00015 #include "llvm/IR/Function.h"
00016 #include "llvm/IR/Module.h"
00017 #include "llvm/IR/PassManager.h"
00018 #include "llvm/Pass.h"
00019 #include "llvm/Support/Debug.h"
00020 #include "llvm/Support/raw_ostream.h"
00021 using namespace llvm;
00022 
00023 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
00024 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner)
00025     : OS(OS), Banner(Banner) {}
00026 
00027 PreservedAnalyses PrintModulePass::run(Module *M) {
00028   OS << Banner << *M;
00029   return PreservedAnalyses::all();
00030 }
00031 
00032 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
00033 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
00034     : OS(OS), Banner(Banner) {}
00035 
00036 PreservedAnalyses PrintFunctionPass::run(Function *F) {
00037   OS << Banner << static_cast<Value &>(*F);
00038   return PreservedAnalyses::all();
00039 }
00040 
00041 namespace {
00042 
00043 class PrintModulePassWrapper : public ModulePass {
00044   PrintModulePass P;
00045 
00046 public:
00047   static char ID;
00048   PrintModulePassWrapper() : ModulePass(ID) {}
00049   PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner)
00050       : ModulePass(ID), P(OS, Banner) {}
00051 
00052   bool runOnModule(Module &M) override {
00053     P.run(&M);
00054     return false;
00055   }
00056 
00057   void getAnalysisUsage(AnalysisUsage &AU) const override {
00058     AU.setPreservesAll();
00059   }
00060 };
00061 
00062 class PrintFunctionPassWrapper : public FunctionPass {
00063   PrintFunctionPass P;
00064 
00065 public:
00066   static char ID;
00067   PrintFunctionPassWrapper() : FunctionPass(ID) {}
00068   PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
00069       : FunctionPass(ID), P(OS, Banner) {}
00070 
00071   // This pass just prints a banner followed by the function as it's processed.
00072   bool runOnFunction(Function &F) override {
00073     P.run(&F);
00074     return false;
00075   }
00076 
00077   void getAnalysisUsage(AnalysisUsage &AU) const override {
00078     AU.setPreservesAll();
00079   }
00080 };
00081 
00082 class PrintBasicBlockPass : public BasicBlockPass {
00083   raw_ostream &Out;
00084   std::string Banner;
00085 
00086 public:
00087   static char ID;
00088   PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
00089   PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
00090       : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
00091 
00092   bool runOnBasicBlock(BasicBlock &BB) override {
00093     Out << Banner << BB;
00094     return false;
00095   }
00096 
00097   void getAnalysisUsage(AnalysisUsage &AU) const override {
00098     AU.setPreservesAll();
00099   }
00100 };
00101 
00102 }
00103 
00104 char PrintModulePassWrapper::ID = 0;
00105 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
00106                 "Print module to stderr", false, false)
00107 char PrintFunctionPassWrapper::ID = 0;
00108 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
00109                 "Print function to stderr", false, false)
00110 char PrintBasicBlockPass::ID = 0;
00111 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
00112                 false)
00113 
00114 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
00115                                         const std::string &Banner) {
00116   return new PrintModulePassWrapper(OS, Banner);
00117 }
00118 
00119 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
00120                                             const std::string &Banner) {
00121   return new PrintFunctionPassWrapper(OS, Banner);
00122 }
00123 
00124 BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS,
00125                                                 const std::string &Banner) {
00126   return new PrintBasicBlockPass(OS, Banner);
00127 }