1 //===- PassPrinters.cpp - Utilities to print analysis info for passes -----===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Utilities to print analysis info for various kinds of passes.
13 //===----------------------------------------------------------------------===//
14 #include "PassPrinters.h"
15 #include "llvm/Analysis/CallGraphSCCPass.h"
16 #include "llvm/Analysis/LoopPass.h"
17 #include "llvm/Analysis/RegionPass.h"
18 #include "llvm/IR/Function.h"
19 #include "llvm/Pass.h"
26 struct FunctionPassPrinter : public FunctionPass {
27 const PassInfo *PassToPrint;
33 FunctionPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
34 : FunctionPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
35 std::string PassToPrintName = PassToPrint->getPassName();
36 PassName = "FunctionPass Printer: " + PassToPrintName;
39 bool runOnFunction(Function &F) override {
41 Out << "Printing analysis '" << PassToPrint->getPassName()
42 << "' for function '" << F.getName() << "':\n";
44 // Get and print pass...
45 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, F.getParent());
49 const char *getPassName() const override { return PassName.c_str(); }
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.addRequiredID(PassToPrint->getTypeInfo());
57 char FunctionPassPrinter::ID = 0;
59 struct CallGraphSCCPassPrinter : public CallGraphSCCPass {
61 const PassInfo *PassToPrint;
66 CallGraphSCCPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
67 : CallGraphSCCPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
68 std::string PassToPrintName = PassToPrint->getPassName();
69 PassName = "CallGraphSCCPass Printer: " + PassToPrintName;
72 bool runOnSCC(CallGraphSCC &SCC) override {
74 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
76 // Get and print pass...
77 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
78 Function *F = (*I)->getFunction();
80 getAnalysisID<Pass>(PassToPrint->getTypeInfo())
81 .print(Out, F->getParent());
86 const char *getPassName() const override { return PassName.c_str(); }
88 void getAnalysisUsage(AnalysisUsage &AU) const override {
89 AU.addRequiredID(PassToPrint->getTypeInfo());
94 char CallGraphSCCPassPrinter::ID = 0;
96 struct ModulePassPrinter : public ModulePass {
98 const PassInfo *PassToPrint;
100 std::string PassName;
103 ModulePassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
104 : ModulePass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
105 std::string PassToPrintName = PassToPrint->getPassName();
106 PassName = "ModulePass Printer: " + PassToPrintName;
109 bool runOnModule(Module &M) override {
111 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
113 // Get and print pass...
114 getAnalysisID<Pass>(PassToPrint->getTypeInfo()).print(Out, &M);
118 const char *getPassName() const override { return PassName.c_str(); }
120 void getAnalysisUsage(AnalysisUsage &AU) const override {
121 AU.addRequiredID(PassToPrint->getTypeInfo());
122 AU.setPreservesAll();
126 char ModulePassPrinter::ID = 0;
128 struct LoopPassPrinter : public LoopPass {
130 const PassInfo *PassToPrint;
132 std::string PassName;
135 LoopPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
136 : LoopPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
137 std::string PassToPrintName = PassToPrint->getPassName();
138 PassName = "LoopPass Printer: " + PassToPrintName;
141 bool runOnLoop(Loop *L, LPPassManager &LPM) override {
143 Out << "Printing analysis '" << PassToPrint->getPassName() << "':\n";
145 // Get and print pass...
146 getAnalysisID<Pass>(PassToPrint->getTypeInfo())
147 .print(Out, L->getHeader()->getParent()->getParent());
151 const char *getPassName() const override { return PassName.c_str(); }
153 void getAnalysisUsage(AnalysisUsage &AU) const override {
154 AU.addRequiredID(PassToPrint->getTypeInfo());
155 AU.setPreservesAll();
159 char LoopPassPrinter::ID = 0;
161 struct RegionPassPrinter : public RegionPass {
163 const PassInfo *PassToPrint;
165 std::string PassName;
168 RegionPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
169 : RegionPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
170 std::string PassToPrintName = PassToPrint->getPassName();
171 PassName = "RegionPass Printer: " + PassToPrintName;
174 bool runOnRegion(Region *R, RGPassManager &RGM) override {
176 Out << "Printing analysis '" << PassToPrint->getPassName() << "' for "
177 << "region: '" << R->getNameStr() << "' in function '"
178 << R->getEntry()->getParent()->getName() << "':\n";
180 // Get and print pass...
181 getAnalysisID<Pass>(PassToPrint->getTypeInfo())
182 .print(Out, R->getEntry()->getParent()->getParent());
186 const char *getPassName() const override { return PassName.c_str(); }
188 void getAnalysisUsage(AnalysisUsage &AU) const override {
189 AU.addRequiredID(PassToPrint->getTypeInfo());
190 AU.setPreservesAll();
194 char RegionPassPrinter::ID = 0;
196 struct BasicBlockPassPrinter : public BasicBlockPass {
197 const PassInfo *PassToPrint;
200 std::string PassName;
203 BasicBlockPassPrinter(const PassInfo *PI, raw_ostream &out, bool Quiet)
204 : BasicBlockPass(ID), PassToPrint(PI), Out(out), QuietPass(Quiet) {
205 std::string PassToPrintName = PassToPrint->getPassName();
206 PassName = "BasicBlockPass Printer: " + PassToPrintName;
209 bool runOnBasicBlock(BasicBlock &BB) override {
211 Out << "Printing Analysis info for BasicBlock '" << BB.getName()
212 << "': Pass " << PassToPrint->getPassName() << ":\n";
214 // Get and print pass...
215 getAnalysisID<Pass>(PassToPrint->getTypeInfo())
216 .print(Out, BB.getParent()->getParent());
220 const char *getPassName() const override { return PassName.c_str(); }
222 void getAnalysisUsage(AnalysisUsage &AU) const override {
223 AU.addRequiredID(PassToPrint->getTypeInfo());
224 AU.setPreservesAll();
228 char BasicBlockPassPrinter::ID = 0;
231 FunctionPass *llvm::createFunctionPassPrinter(const PassInfo *PI,
232 raw_ostream &OS, bool Quiet) {
233 return new FunctionPassPrinter(PI, OS, Quiet);
236 CallGraphSCCPass *llvm::createCallGraphPassPrinter(const PassInfo *PI,
239 return new CallGraphSCCPassPrinter(PI, OS, Quiet);
242 ModulePass *llvm::createModulePassPrinter(const PassInfo *PI, raw_ostream &OS,
244 return new ModulePassPrinter(PI, OS, Quiet);
247 LoopPass *llvm::createLoopPassPrinter(const PassInfo *PI, raw_ostream &OS,
249 return new LoopPassPrinter(PI, OS, Quiet);
252 RegionPass *llvm::createRegionPassPrinter(const PassInfo *PI, raw_ostream &OS,
254 return new RegionPassPrinter(PI, OS, Quiet);
257 BasicBlockPass *llvm::createBasicBlockPassPrinter(const PassInfo *PI,
258 raw_ostream &OS, bool Quiet) {
259 return new BasicBlockPassPrinter(PI, OS, Quiet);