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