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 
FunctionPassPrinter__anonda01d5dc0111::FunctionPassPrinter33   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 
runOnFunction__anonda01d5dc0111::FunctionPassPrinter39   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 
getPassName__anonda01d5dc0111::FunctionPassPrinter49   const char *getPassName() const override { return PassName.c_str(); }
50 
getAnalysisUsage__anonda01d5dc0111::FunctionPassPrinter51   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 
CallGraphSCCPassPrinter__anonda01d5dc0111::CallGraphSCCPassPrinter66   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 
runOnSCC__anonda01d5dc0111::CallGraphSCCPassPrinter72   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 
getPassName__anonda01d5dc0111::CallGraphSCCPassPrinter86   const char *getPassName() const override { return PassName.c_str(); }
87 
getAnalysisUsage__anonda01d5dc0111::CallGraphSCCPassPrinter88   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 
ModulePassPrinter__anonda01d5dc0111::ModulePassPrinter103   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 
runOnModule__anonda01d5dc0111::ModulePassPrinter109   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 
getPassName__anonda01d5dc0111::ModulePassPrinter118   const char *getPassName() const override { return PassName.c_str(); }
119 
getAnalysisUsage__anonda01d5dc0111::ModulePassPrinter120   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 
LoopPassPrinter__anonda01d5dc0111::LoopPassPrinter135   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 
runOnLoop__anonda01d5dc0111::LoopPassPrinter141   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 
getPassName__anonda01d5dc0111::LoopPassPrinter151   const char *getPassName() const override { return PassName.c_str(); }
152 
getAnalysisUsage__anonda01d5dc0111::LoopPassPrinter153   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 
RegionPassPrinter__anonda01d5dc0111::RegionPassPrinter168   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 
runOnRegion__anonda01d5dc0111::RegionPassPrinter174   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 
getPassName__anonda01d5dc0111::RegionPassPrinter186   const char *getPassName() const override { return PassName.c_str(); }
187 
getAnalysisUsage__anonda01d5dc0111::RegionPassPrinter188   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 
BasicBlockPassPrinter__anonda01d5dc0111::BasicBlockPassPrinter203   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 
runOnBasicBlock__anonda01d5dc0111::BasicBlockPassPrinter209   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 
getPassName__anonda01d5dc0111::BasicBlockPassPrinter220   const char *getPassName() const override { return PassName.c_str(); }
221 
getAnalysisUsage__anonda01d5dc0111::BasicBlockPassPrinter222   void getAnalysisUsage(AnalysisUsage &AU) const override {
223     AU.addRequiredID(PassToPrint->getTypeInfo());
224     AU.setPreservesAll();
225   }
226 };
227 
228 char BasicBlockPassPrinter::ID = 0;
229 }
230 
createFunctionPassPrinter(const PassInfo * PI,raw_ostream & OS,bool Quiet)231 FunctionPass *llvm::createFunctionPassPrinter(const PassInfo *PI,
232                                               raw_ostream &OS, bool Quiet) {
233   return new FunctionPassPrinter(PI, OS, Quiet);
234 }
235 
createCallGraphPassPrinter(const PassInfo * PI,raw_ostream & OS,bool Quiet)236 CallGraphSCCPass *llvm::createCallGraphPassPrinter(const PassInfo *PI,
237                                                    raw_ostream &OS,
238                                                    bool Quiet) {
239   return new CallGraphSCCPassPrinter(PI, OS, Quiet);
240 }
241 
createModulePassPrinter(const PassInfo * PI,raw_ostream & OS,bool Quiet)242 ModulePass *llvm::createModulePassPrinter(const PassInfo *PI, raw_ostream &OS,
243                                           bool Quiet) {
244   return new ModulePassPrinter(PI, OS, Quiet);
245 }
246 
createLoopPassPrinter(const PassInfo * PI,raw_ostream & OS,bool Quiet)247 LoopPass *llvm::createLoopPassPrinter(const PassInfo *PI, raw_ostream &OS,
248                                       bool Quiet) {
249   return new LoopPassPrinter(PI, OS, Quiet);
250 }
251 
createRegionPassPrinter(const PassInfo * PI,raw_ostream & OS,bool Quiet)252 RegionPass *llvm::createRegionPassPrinter(const PassInfo *PI, raw_ostream &OS,
253                                           bool Quiet) {
254   return new RegionPassPrinter(PI, OS, Quiet);
255 }
256 
createBasicBlockPassPrinter(const PassInfo * PI,raw_ostream & OS,bool Quiet)257 BasicBlockPass *llvm::createBasicBlockPassPrinter(const PassInfo *PI,
258                                                   raw_ostream &OS, bool Quiet) {
259   return new BasicBlockPassPrinter(PI, OS, Quiet);
260 }
261