Home | History | Annotate | Download | only in opt
      1 //===- PassPrinters.cpp - Utilities to print analysis info for 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 /// \file
     11 /// \brief Utilities to print analysis info for various kinds of passes.
     12 ///
     13 //===----------------------------------------------------------------------===//
     14 #include "PassPrinters.h"
     15 #include "llvm/Analysis/CallGraphSCCPass.h"
     16 #include "llvm/Analysis/LoopPass.h"
     17 #include "llvm/Analysis/RegionPass.h"
     18 #include "llvm/IR/Function.h"
     19 #include "llvm/Pass.h"
     20 #include <string>
     21 
     22 using namespace llvm;
     23 
     24 namespace {
     25 
     26 struct FunctionPassPrinter : public FunctionPass {
     27   const PassInfo *PassToPrint;
     28   raw_ostream &Out;
     29   static char ID;
     30   std::string PassName;
     31   bool QuietPass;
     32 
     33   FunctionPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
     34       : FunctionPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
     35     std::string PassToPrintName = PassToPrint->getPassName();
     36     PassName = "FunctionPass Printer: " + PassToPrintName;
     37   }
     38 
     39   bool runOnFunction(Function &F) override {
     40     if (!QuietPass)
     41       Out << "Printing analysis '" << PassToPrint->getPassName()
     42           << "' for function '" << F.getName() << "':\n";
     43 
     44     // Get and print pass...
     45     getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, F.getParent());
     46     return false;
     47   }
     48 
     49   const char *getPassName() const override { return PassName.c_str(); }
     50 
     51   void getAnalysisUsage(AnalysisUsage &AU) const override {
     52     AU.addRequiredID(PassToPrint->getTypeInfo());
     53     AU.setPreservesAll();
     54   }
     55 };
     56 
     57 char FunctionPassPrinter::ID = 0;
     58 
     59 struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
     60   static char ID;
     61   const PassInfo *PassToPrint;
     62   raw_ostream &Out;
     63   std::string PassName;
     64   bool QuietPass;
     65 
     66   CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
     67       : CallGraphSCCPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
     68     std::string PassToPrintName = PassToPrint->getPassName();
     69     PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
     70   }
     71 
     72   bool runOnSCC(CallGraphSCC &SCC) override {
     73     if (!QuietPass)
     74       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
     75 
     76     // Get and print pass...
     77     for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
     78       Function *F = (*I)->getFunction();
     79       if (F)
     80         getAnalysisID<Pass>(PassToPrint->getTypeInfo())
     81             .print(Out, F->getParent());
     82     }
     83     return false;
     84   }
     85 
     86   const char *getPassName() const override { return PassName.c_str(); }
     87 
     88   void getAnalysisUsage(AnalysisUsage &AU) const override {
     89     AU.addRequiredID(PassToPrint->getTypeInfo());
     90     AU.setPreservesAll();
     91   }
     92 };
     93 
     94 char CallGraphSCCPassPrinter::ID = 0;
     95 
     96 struct ModulePassPrinter : public ModulePass {
     97   static char ID;
     98   const PassInfo *PassToPrint;
     99   raw_ostream &Out;
    100   std::string PassName;
    101   bool QuietPass;
    102 
    103   ModulePassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
    104       : ModulePass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
    105     std::string PassToPrintName = PassToPrint->getPassName();
    106     PassName = "ModulePass Printer: " + PassToPrintName;
    107   }
    108 
    109   bool runOnModule(Module &M) override {
    110     if (!QuietPass)
    111       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
    112 
    113     // Get and print pass...
    114     getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
    115     return false;
    116   }
    117 
    118   const char *getPassName() const override { return PassName.c_str(); }
    119 
    120   void getAnalysisUsage(AnalysisUsage &AU) const override {
    121     AU.addRequiredID(PassToPrint->getTypeInfo());
    122     AU.setPreservesAll();
    123   }
    124 };
    125 
    126 char ModulePassPrinter::ID = 0;
    127 
    128 struct LoopPassPrinter : public LoopPass {
    129   static char ID;
    130   const PassInfo *PassToPrint;
    131   raw_ostream &Out;
    132   std::string PassName;
    133   bool QuietPass;
    134 
    135   LoopPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
    136       : LoopPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
    137     std::string PassToPrintName = PassToPrint->getPassName();
    138     PassName = "LoopPass Printer: " + PassToPrintName;
    139   }
    140 
    141   bool runOnLoop(Loop *L, LPPassManager &LPM) override {
    142     if (!QuietPass)
    143       Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
    144 
    145     // Get and print pass...
    146     getAnalysisID<Pass>(PassToPrint->getTypeInfo())
    147         .print(Out, L->getHeader()->getParent()->getParent());
    148     return false;
    149   }
    150 
    151   const char *getPassName() const override { return PassName.c_str(); }
    152 
    153   void getAnalysisUsage(AnalysisUsage &AU) const override {
    154     AU.addRequiredID(PassToPrint->getTypeInfo());
    155     AU.setPreservesAll();
    156   }
    157 };
    158 
    159 char LoopPassPrinter::ID = 0;
    160 
    161 struct RegionPassPrinter : public RegionPass {
    162   static char ID;
    163   const PassInfo *PassToPrint;
    164   raw_ostream &Out;
    165   std::string PassName;
    166   bool QuietPass;
    167 
    168   RegionPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
    169       : RegionPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
    170     std::string PassToPrintName = PassToPrint->getPassName();
    171     PassName = "RegionPass Printer: " + PassToPrintName;
    172   }
    173 
    174   bool runOnRegion(Region *R, RGPassManager &RGM) override {
    175     if (!QuietPass) {
    176       Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
    177           << "region: '" << R->getNameStr() << "' in function '"
    178           << R->getEntry()->getParent()->getName() << "':\n";
    179     }
    180     // Get and print pass...
    181     getAnalysisID<Pass>(PassToPrint->getTypeInfo())
    182         .print(Out, R->getEntry()->getParent()->getParent());
    183     return false;
    184   }
    185 
    186   const char *getPassName() const override { return PassName.c_str(); }
    187 
    188   void getAnalysisUsage(AnalysisUsage &AU) const override {
    189     AU.addRequiredID(PassToPrint->getTypeInfo());
    190     AU.setPreservesAll();
    191   }
    192 };
    193 
    194 char RegionPassPrinter::ID = 0;
    195 
    196 struct BasicBlockPassPrinter : public BasicBlockPass {
    197   const PassInfo *PassToPrint;
    198   raw_ostream &Out;
    199   static char ID;
    200   std::string PassName;
    201   bool QuietPass;
    202 
    203   BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
    204       : BasicBlockPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
    205     std::string PassToPrintName = PassToPrint->getPassName();
    206     PassName = "BasicBlockPass Printer: " + PassToPrintName;
    207   }
    208 
    209   bool runOnBasicBlock(BasicBlock &BB) override {
    210     if (!QuietPass)
    211       Out << "Printing Analysis info for BasicBlock '" << BB.getName()
    212           << "': Pass " << PassToPrint->getPassName() << ":\n";
    213 
    214     // Get and print pass...
    215     getAnalysisID<Pass>(PassToPrint->getTypeInfo())
    216         .print(Out, BB.getParent()->getParent());
    217     return false;
    218   }
    219 
    220   const char *getPassName() const override { return PassName.c_str(); }
    221 
    222   void getAnalysisUsage(AnalysisUsage &AU) const override {
    223     AU.addRequiredID(PassToPrint->getTypeInfo());
    224     AU.setPreservesAll();
    225   }
    226 };
    227 
    228 char BasicBlockPassPrinter::ID = 0;
    229 }
    230 
    231 FunctionPass *llvm::createFunctionPassPrinter(const PassInfo *PI,
    232                                               raw_ostream &OS, bool Quiet) {
    233   return new FunctionPassPrinter(PI, OS, Quiet);
    234 }
    235 
    236 CallGraphSCCPass *llvm::createCallGraphPassPrinter(const PassInfo *PI,
    237                                                    raw_ostream &OS,
    238                                                    bool Quiet) {
    239   return new CallGraphSCCPassPrinter(PI, OS, Quiet);
    240 }
    241 
    242 ModulePass *llvm::createModulePassPrinter(const PassInfo *PI, raw_ostream &OS,
    243                                           bool Quiet) {
    244   return new ModulePassPrinter(PI, OS, Quiet);
    245 }
    246 
    247 LoopPass *llvm::createLoopPassPrinter(const PassInfo *PI, raw_ostream &OS,
    248                                       bool Quiet) {
    249   return new LoopPassPrinter(PI, OS, Quiet);
    250 }
    251 
    252 RegionPass *llvm::createRegionPassPrinter(const PassInfo *PI, raw_ostream &OS,
    253                                           bool Quiet) {
    254   return new RegionPassPrinter(PI, OS, Quiet);
    255 }
    256 
    257 BasicBlockPass *llvm::createBasicBlockPassPrinter(const PassInfo *PI,
    258                                                   raw_ostream &OS, bool Quiet) {
    259   return new BasicBlockPassPrinter(PI, OS, Quiet);
    260 }
    261