1 //===- PassPrinters.cpp - Utilities to print analysis info for passes -----===//
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
7 //===----------------------------------------------------------------------===//
10 /// Utilities to print analysis info for various kinds of passes.
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"
31 struct FunctionPassPrinter
: public FunctionPass
{
32 const PassInfo
*PassToPrint
;
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
{
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());
54 StringRef
getPassName() const override
{ return PassName
; }
56 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
57 AU
.addRequiredID(PassToPrint
->getTypeInfo());
62 char FunctionPassPrinter::ID
= 0;
64 struct CallGraphSCCPassPrinter
: public CallGraphSCCPass
{
66 const PassInfo
*PassToPrint
;
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
{
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();
85 getAnalysisID
<Pass
>(PassToPrint
->getTypeInfo())
86 .print(Out
, F
->getParent());
91 StringRef
getPassName() const override
{ return PassName
; }
93 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
94 AU
.addRequiredID(PassToPrint
->getTypeInfo());
99 char CallGraphSCCPassPrinter::ID
= 0;
101 struct ModulePassPrinter
: public ModulePass
{
103 const PassInfo
*PassToPrint
;
105 std::string PassName
;
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
{
116 Out
<< "Printing analysis '" << PassToPrint
->getPassName() << "':\n";
118 // Get and print pass...
119 getAnalysisID
<Pass
>(PassToPrint
->getTypeInfo()).print(Out
, &M
);
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
{
135 const PassInfo
*PassToPrint
;
137 std::string PassName
;
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
{
148 Out
<< "Printing analysis '" << PassToPrint
->getPassName() << "':\n";
150 // Get and print pass...
151 getAnalysisID
<Pass
>(PassToPrint
->getTypeInfo())
152 .print(Out
, L
->getHeader()->getParent()->getParent());
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
{
168 const PassInfo
*PassToPrint
;
170 std::string PassName
;
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
{
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());
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
;
205 std::string PassName
;
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
{
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());
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
,
245 return new CallGraphSCCPassPrinter(PI
, OS
, Quiet
);
248 ModulePass
*llvm::createModulePassPrinter(const PassInfo
*PI
, raw_ostream
&OS
,
250 return new ModulePassPrinter(PI
, OS
, Quiet
);
253 LoopPass
*llvm::createLoopPassPrinter(const PassInfo
*PI
, raw_ostream
&OS
,
255 return new LoopPassPrinter(PI
, OS
, Quiet
);
258 RegionPass
*llvm::createRegionPassPrinter(const PassInfo
*PI
, raw_ostream
&OS
,
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
);