LCOV - code coverage report
Current view: top level - lib/IR - IRPrintingPasses.cpp (source / functions) Hit Total Coverage
Test: llvm-toolchain.info Lines: 43 60 71.7 %
Date: 2017-09-14 15:23:50 Functions: 21 30 70.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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           0 : PrintModulePass::PrintModulePass() : OS(dbgs()) {}
      24       10128 : PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
      25       10128 :                                  bool ShouldPreserveUseListOrder)
      26             :     : OS(OS), Banner(Banner),
      27       20256 :       ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
      28             : 
      29       10127 : PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) {
      30       20254 :   OS << Banner;
      31       10127 :   if (llvm::isFunctionInPrintList("*"))
      32       10125 :     M.print(OS, nullptr, ShouldPreserveUseListOrder);
      33             :   else {
      34           6 :     for(const auto &F : M.functions())
      35           4 :       if (llvm::isFunctionInPrintList(F.getName()))
      36           2 :         F.print(OS);
      37             :   }
      38       10127 :   return PreservedAnalyses::all();
      39             : }
      40             : 
      41           0 : PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
      42         144 : PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
      43         288 :     : OS(OS), Banner(Banner) {}
      44             : 
      45         219 : PreservedAnalyses PrintFunctionPass::run(Function &F,
      46             :                                          FunctionAnalysisManager &) {
      47         219 :   if (isFunctionInPrintList(F.getName()))
      48         406 :     OS << Banner << static_cast<Value &>(F);
      49         219 :   return PreservedAnalyses::all();
      50             : }
      51             : 
      52             : namespace {
      53             : 
      54       28983 : class PrintModulePassWrapper : public ModulePass {
      55             :   PrintModulePass P;
      56             : 
      57             : public:
      58             :   static char ID;
      59           0 :   PrintModulePassWrapper() : ModulePass(ID) {}
      60             :   PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
      61             :                          bool ShouldPreserveUseListOrder)
      62       19326 :       : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
      63             : 
      64        9664 :   bool runOnModule(Module &M) override {
      65       19328 :     ModuleAnalysisManager DummyMAM;
      66        9664 :     P.run(M, DummyMAM);
      67        9664 :     return false;
      68             :   }
      69             : 
      70        9663 :   void getAnalysisUsage(AnalysisUsage &AU) const override {
      71        9663 :     AU.setPreservesAll();
      72        9663 :   }
      73             : 
      74          15 :   StringRef getPassName() const override { return "Print Module IR"; }
      75             : };
      76             : 
      77         568 : class PrintFunctionPassWrapper : public FunctionPass {
      78             :   PrintFunctionPass P;
      79             : 
      80             : public:
      81             :   static char ID;
      82           0 :   PrintFunctionPassWrapper() : FunctionPass(ID) {}
      83             :   PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
      84         284 :       : FunctionPass(ID), P(OS, Banner) {}
      85             : 
      86             :   // This pass just prints a banner followed by the function as it's processed.
      87         217 :   bool runOnFunction(Function &F) override {
      88         434 :     FunctionAnalysisManager DummyFAM;
      89         217 :     P.run(F, DummyFAM);
      90         217 :     return false;
      91             :   }
      92             : 
      93         142 :   void getAnalysisUsage(AnalysisUsage &AU) const override {
      94         142 :     AU.setPreservesAll();
      95         142 :   }
      96             : 
      97           0 :   StringRef getPassName() const override { return "Print Function IR"; }
      98             : };
      99             : 
     100           0 : class PrintBasicBlockPass : public BasicBlockPass {
     101             :   raw_ostream &Out;
     102             :   std::string Banner;
     103             : 
     104             : public:
     105             :   static char ID;
     106           0 :   PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
     107             :   PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
     108           0 :       : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
     109             : 
     110           0 :   bool runOnBasicBlock(BasicBlock &BB) override {
     111           0 :     Out << Banner << BB;
     112           0 :     return false;
     113             :   }
     114             : 
     115           0 :   void getAnalysisUsage(AnalysisUsage &AU) const override {
     116           0 :     AU.setPreservesAll();
     117           0 :   }
     118             : 
     119           0 :   StringRef getPassName() const override { return "Print BasicBlock IR"; }
     120             : };
     121             : 
     122             : }
     123             : 
     124             : char PrintModulePassWrapper::ID = 0;
     125      128825 : INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
     126             :                 "Print module to stderr", false, false)
     127             : char PrintFunctionPassWrapper::ID = 0;
     128      128825 : INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
     129             :                 "Print function to stderr", false, false)
     130             : char PrintBasicBlockPass::ID = 0;
     131      128825 : INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
     132             :                 false)
     133             : 
     134        9663 : ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
     135             :                                         const std::string &Banner,
     136             :                                         bool ShouldPreserveUseListOrder) {
     137       19326 :   return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
     138             : }
     139             : 
     140         142 : FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
     141             :                                             const std::string &Banner) {
     142         284 :   return new PrintFunctionPassWrapper(OS, Banner);
     143             : }
     144             : 
     145           0 : BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS,
     146             :                                                 const std::string &Banner) {
     147           0 :   return new PrintBasicBlockPass(OS, Banner);
     148             : }

Generated by: LCOV version 1.13