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
;
37 FunctionPassPrinter(const PassInfo
*PI
, raw_ostream
&out
)
38 : FunctionPass(ID
), PassToPrint(PI
), Out(out
) {
39 std::string PassToPrintName
= std::string(PassToPrint
->getPassName());
40 PassName
= "FunctionPass Printer: " + PassToPrintName
;
43 bool runOnFunction(Function
&F
) override
{
44 Out
<< "Printing analysis '" << PassToPrint
->getPassName()
45 << "' for function '" << F
.getName() << "':\n";
47 // Get and print pass...
48 getAnalysisID
<Pass
>(PassToPrint
->getTypeInfo()).print(Out
, F
.getParent());
52 StringRef
getPassName() const override
{ return PassName
; }
54 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
55 AU
.addRequiredID(PassToPrint
->getTypeInfo());
60 char FunctionPassPrinter::ID
= 0;
62 struct CallGraphSCCPassPrinter
: public CallGraphSCCPass
{
64 const PassInfo
*PassToPrint
;
68 CallGraphSCCPassPrinter(const PassInfo
*PI
, raw_ostream
&out
)
69 : CallGraphSCCPass(ID
), PassToPrint(PI
), Out(out
) {
70 std::string PassToPrintName
= std::string(PassToPrint
->getPassName());
71 PassName
= "CallGraphSCCPass Printer: " + PassToPrintName
;
74 bool runOnSCC(CallGraphSCC
&SCC
) override
{
75 Out
<< "Printing analysis '" << PassToPrint
->getPassName() << "':\n";
77 // Get and print pass...
78 for (CallGraphSCC::iterator I
= SCC
.begin(), E
= SCC
.end(); I
!= E
; ++I
) {
79 Function
*F
= (*I
)->getFunction();
81 getAnalysisID
<Pass
>(PassToPrint
->getTypeInfo())
82 .print(Out
, F
->getParent());
87 StringRef
getPassName() const override
{ return PassName
; }
89 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
90 AU
.addRequiredID(PassToPrint
->getTypeInfo());
95 char CallGraphSCCPassPrinter::ID
= 0;
97 struct ModulePassPrinter
: public ModulePass
{
99 const PassInfo
*PassToPrint
;
101 std::string PassName
;
103 ModulePassPrinter(const PassInfo
*PI
, raw_ostream
&out
)
104 : ModulePass(ID
), PassToPrint(PI
), Out(out
) {
105 std::string PassToPrintName
= std::string(PassToPrint
->getPassName());
106 PassName
= "ModulePass Printer: " + PassToPrintName
;
109 bool runOnModule(Module
&M
) override
{
110 Out
<< "Printing analysis '" << PassToPrint
->getPassName() << "':\n";
112 // Get and print pass...
113 getAnalysisID
<Pass
>(PassToPrint
->getTypeInfo()).print(Out
, &M
);
117 StringRef
getPassName() const override
{ return PassName
; }
119 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
120 AU
.addRequiredID(PassToPrint
->getTypeInfo());
121 AU
.setPreservesAll();
125 char ModulePassPrinter::ID
= 0;
127 struct LoopPassPrinter
: public LoopPass
{
129 const PassInfo
*PassToPrint
;
131 std::string PassName
;
133 LoopPassPrinter(const PassInfo
*PI
, raw_ostream
&out
)
134 : LoopPass(ID
), PassToPrint(PI
), Out(out
) {
135 std::string PassToPrintName
= std::string(PassToPrint
->getPassName());
136 PassName
= "LoopPass Printer: " + PassToPrintName
;
139 bool runOnLoop(Loop
*L
, LPPassManager
&LPM
) override
{
140 Out
<< "Printing analysis '" << PassToPrint
->getPassName() << "':\n";
142 // Get and print pass...
143 getAnalysisID
<Pass
>(PassToPrint
->getTypeInfo())
144 .print(Out
, L
->getHeader()->getParent()->getParent());
148 StringRef
getPassName() const override
{ return PassName
; }
150 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
151 AU
.addRequiredID(PassToPrint
->getTypeInfo());
152 AU
.setPreservesAll();
156 char LoopPassPrinter::ID
= 0;
158 struct RegionPassPrinter
: public RegionPass
{
160 const PassInfo
*PassToPrint
;
162 std::string PassName
;
164 RegionPassPrinter(const PassInfo
*PI
, raw_ostream
&out
)
165 : RegionPass(ID
), PassToPrint(PI
), Out(out
) {
166 std::string PassToPrintName
= std::string(PassToPrint
->getPassName());
167 PassName
= "RegionPass Printer: " + PassToPrintName
;
170 bool runOnRegion(Region
*R
, RGPassManager
&RGM
) override
{
171 Out
<< "Printing analysis '" << PassToPrint
->getPassName() << "' for "
172 << "region: '" << R
->getNameStr() << "' in function '"
173 << R
->getEntry()->getParent()->getName() << "':\n";
174 // Get and print pass...
175 getAnalysisID
<Pass
>(PassToPrint
->getTypeInfo())
176 .print(Out
, R
->getEntry()->getParent()->getParent());
180 StringRef
getPassName() const override
{ return PassName
; }
182 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
183 AU
.addRequiredID(PassToPrint
->getTypeInfo());
184 AU
.setPreservesAll();
188 char RegionPassPrinter::ID
= 0;
190 } // end anonymous namespace
192 FunctionPass
*llvm::createFunctionPassPrinter(const PassInfo
*PI
,
194 return new FunctionPassPrinter(PI
, OS
);
197 CallGraphSCCPass
*llvm::createCallGraphPassPrinter(const PassInfo
*PI
,
199 return new CallGraphSCCPassPrinter(PI
, OS
);
202 ModulePass
*llvm::createModulePassPrinter(const PassInfo
*PI
, raw_ostream
&OS
) {
203 return new ModulePassPrinter(PI
, OS
);
206 LoopPass
*llvm::createLoopPassPrinter(const PassInfo
*PI
, raw_ostream
&OS
) {
207 return new LoopPassPrinter(PI
, OS
);
210 RegionPass
*llvm::createRegionPassPrinter(const PassInfo
*PI
, raw_ostream
&OS
) {
211 return new RegionPassPrinter(PI
, OS
);