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