[Alignment][NFC] Migrate Instructions to Align
[llvm-core.git] / tools / opt / PassPrinters.cpp
blob70da6a43f8d997366473b12c3b6ba64f4d6b2fcb
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 //===----------------------------------------------------------------------===//
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>
27 using namespace llvm;
29 namespace {
31 struct FunctionPassPrinter : public FunctionPass {
32 const PassInfo *PassToPrint;
33 raw_ostream &Out;
34 static char ID;
35 std::string PassName;
36 bool QuietPass;
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;
44 bool runOnFunction(Function &F) override {
45 if (!QuietPass)
46 Out << "Printing analysis '" << PassToPrint->getPassName()
47 << "' for function '" << F.getName() << "':\n";
49 // Get and print pass...
50 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, F.getParent());
51 return false;
54 StringRef getPassName() const override { return PassName; }
56 void getAnalysisUsage(AnalysisUsage &AU) const override {
57 AU.addRequiredID(PassToPrint->getTypeInfo());
58 AU.setPreservesAll();
62 char FunctionPassPrinter::ID = 0;
64 struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
65 static char ID;
66 const PassInfo *PassToPrint;
67 raw_ostream &Out;
68 std::string PassName;
69 bool QuietPass;
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;
77 bool runOnSCC(CallGraphSCC &SCC) override {
78 if (!QuietPass)
79 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
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());
88 return false;
91 StringRef getPassName() const override { return PassName; }
93 void getAnalysisUsage(AnalysisUsage &AU) const override {
94 AU.addRequiredID(PassToPrint->getTypeInfo());
95 AU.setPreservesAll();
99 char CallGraphSCCPassPrinter::ID = 0;
101 struct ModulePassPrinter : public ModulePass {
102 static char ID;
103 const PassInfo *PassToPrint;
104 raw_ostream &Out;
105 std::string PassName;
106 bool QuietPass;
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;
114 bool runOnModule(Module &M) override {
115 if (!QuietPass)
116 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
118 // Get and print pass...
119 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
120 return false;
123 StringRef getPassName() const override { return PassName; }
125 void getAnalysisUsage(AnalysisUsage &AU) const override {
126 AU.addRequiredID(PassToPrint->getTypeInfo());
127 AU.setPreservesAll();
131 char ModulePassPrinter::ID = 0;
133 struct LoopPassPrinter : public LoopPass {
134 static char ID;
135 const PassInfo *PassToPrint;
136 raw_ostream &Out;
137 std::string PassName;
138 bool QuietPass;
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;
146 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
147 if (!QuietPass)
148 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
150 // Get and print pass...
151 getAnalysisID<Pass>(PassToPrint->getTypeInfo())
152 .print(Out, L->getHeader()->getParent()->getParent());
153 return false;
156 StringRef getPassName() const override { return PassName; }
158 void getAnalysisUsage(AnalysisUsage &AU) const override {
159 AU.addRequiredID(PassToPrint->getTypeInfo());
160 AU.setPreservesAll();
164 char LoopPassPrinter::ID = 0;
166 struct RegionPassPrinter : public RegionPass {
167 static char ID;
168 const PassInfo *PassToPrint;
169 raw_ostream &Out;
170 std::string PassName;
171 bool QuietPass;
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;
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";
185 // Get and print pass...
186 getAnalysisID<Pass>(PassToPrint->getTypeInfo())
187 .print(Out, R->getEntry()->getParent()->getParent());
188 return false;
191 StringRef getPassName() const override { return PassName; }
193 void getAnalysisUsage(AnalysisUsage &AU) const override {
194 AU.addRequiredID(PassToPrint->getTypeInfo());
195 AU.setPreservesAll();
199 char RegionPassPrinter::ID = 0;
201 struct BasicBlockPassPrinter : public BasicBlockPass {
202 const PassInfo *PassToPrint;
203 raw_ostream &Out;
204 static char ID;
205 std::string PassName;
206 bool QuietPass;
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;
214 bool runOnBasicBlock(BasicBlock &BB) override {
215 if (!QuietPass)
216 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
217 << "': Pass " << PassToPrint->getPassName() << ":\n";
219 // Get and print pass...
220 getAnalysisID<Pass>(PassToPrint->getTypeInfo())
221 .print(Out, BB.getParent()->getParent());
222 return false;
225 StringRef getPassName() const override { return PassName; }
227 void getAnalysisUsage(AnalysisUsage &AU) const override {
228 AU.addRequiredID(PassToPrint->getTypeInfo());
229 AU.setPreservesAll();
233 char BasicBlockPassPrinter::ID = 0;
235 } // end anonymous namespace
237 FunctionPass *llvm::createFunctionPassPrinter(const PassInfo *PI,
238 raw_ostream &OS, bool Quiet) {
239 return new FunctionPassPrinter(PI, OS, Quiet);
242 CallGraphSCCPass *llvm::createCallGraphPassPrinter(const PassInfo *PI,
243 raw_ostream &OS,
244 bool Quiet) {
245 return new CallGraphSCCPassPrinter(PI, OS, Quiet);
248 ModulePass *llvm::createModulePassPrinter(const PassInfo *PI, raw_ostream &OS,
249 bool Quiet) {
250 return new ModulePassPrinter(PI, OS, Quiet);
253 LoopPass *llvm::createLoopPassPrinter(const PassInfo *PI, raw_ostream &OS,
254 bool Quiet) {
255 return new LoopPassPrinter(PI, OS, Quiet);
258 RegionPass *llvm::createRegionPassPrinter(const PassInfo *PI, raw_ostream &OS,
259 bool Quiet) {
260 return new RegionPassPrinter(PI, OS, Quiet);
263 BasicBlockPass *llvm::createBasicBlockPassPrinter(const PassInfo *PI,
264 raw_ostream &OS, bool Quiet) {
265 return new BasicBlockPassPrinter(PI, OS, Quiet);