Home | History | Annotate | Download | only in IR
      1 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // PrintModulePass and PrintFunctionPass implementations.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #include "llvm/IR/IRPrintingPasses.h"
     15 #include "llvm/IR/Function.h"
     16 #include "llvm/IR/Module.h"
     17 #include "llvm/IR/PassManager.h"
     18 #include "llvm/Pass.h"
     19 #include "llvm/Support/Debug.h"
     20 #include "llvm/Support/raw_ostream.h"
     21 using namespace llvm;
     22 
     23 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
     24 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner)
     25     : OS(OS), Banner(Banner) {}
     26 
     27 PreservedAnalyses PrintModulePass::run(Module *M) {
     28   OS << Banner << *M;
     29   return PreservedAnalyses::all();
     30 }
     31 
     32 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
     33 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
     34     : OS(OS), Banner(Banner) {}
     35 
     36 PreservedAnalyses PrintFunctionPass::run(Function *F) {
     37   OS << Banner << static_cast<Value &>(*F);
     38   return PreservedAnalyses::all();
     39 }
     40 
     41 namespace {
     42 
     43 class PrintModulePassWrapper : public ModulePass {
     44   PrintModulePass P;
     45 
     46 public:
     47   static char ID;
     48   PrintModulePassWrapper() : ModulePass(ID) {}
     49   PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner)
     50       : ModulePass(ID), P(OS, Banner) {}
     51 
     52   bool runOnModule(Module &M) override {
     53     P.run(&M);
     54     return false;
     55   }
     56 
     57   void getAnalysisUsage(AnalysisUsage &AU) const override {
     58     AU.setPreservesAll();
     59   }
     60 };
     61 
     62 class PrintFunctionPassWrapper : public FunctionPass {
     63   PrintFunctionPass P;
     64 
     65 public:
     66   static char ID;
     67   PrintFunctionPassWrapper() : FunctionPass(ID) {}
     68   PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
     69       : FunctionPass(ID), P(OS, Banner) {}
     70 
     71   // This pass just prints a banner followed by the function as it's processed.
     72   bool runOnFunction(Function &F) override {
     73     P.run(&F);
     74     return false;
     75   }
     76 
     77   void getAnalysisUsage(AnalysisUsage &AU) const override {
     78     AU.setPreservesAll();
     79   }
     80 };
     81 
     82 class PrintBasicBlockPass : public BasicBlockPass {
     83   raw_ostream &Out;
     84   std::string Banner;
     85 
     86 public:
     87   static char ID;
     88   PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
     89   PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
     90       : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
     91 
     92   bool runOnBasicBlock(BasicBlock &BB) override {
     93     Out << Banner << BB;
     94     return false;
     95   }
     96 
     97   void getAnalysisUsage(AnalysisUsage &AU) const override {
     98     AU.setPreservesAll();
     99   }
    100 };
    101 
    102 }
    103 
    104 char PrintModulePassWrapper::ID = 0;
    105 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
    106                 "Print module to stderr", false, false)
    107 char PrintFunctionPassWrapper::ID = 0;
    108 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
    109                 "Print function to stderr", false, false)
    110 char PrintBasicBlockPass::ID = 0;
    111 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
    112                 false)
    113 
    114 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
    115                                         const std::string &Banner) {
    116   return new PrintModulePassWrapper(OS, Banner);
    117 }
    118 
    119 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
    120                                             const std::string &Banner) {
    121   return new PrintFunctionPassWrapper(OS, Banner);
    122 }
    123 
    124 BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS,
    125                                                 const std::string &Banner) {
    126   return new PrintBasicBlockPass(OS, Banner);
    127 }
    128