1 //===--- IRPrintingPasses.cpp - Module and Function printing 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 //===----------------------------------------------------------------------===//
10 // PrintModulePass and PrintFunctionPass implementations.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/IR/IRPrintingPasses.h"
15 #include "llvm/IR/Function.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/IR/PassManager.h"
18 #include "llvm/Pass.h"
19 #include "llvm/Support/Debug.h"
20 #include "llvm/Support/raw_ostream.h"
23 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
24 PrintModulePass::PrintModulePass(raw_ostream
&OS
, const std::string
&Banner
,
25 bool ShouldPreserveUseListOrder
)
26 : OS(OS
), Banner(Banner
),
27 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder
) {}
29 PreservedAnalyses
PrintModulePass::run(Module
&M
, ModuleAnalysisManager
&) {
31 if (llvm::isFunctionInPrintList("*"))
32 M
.print(OS
, nullptr, ShouldPreserveUseListOrder
);
34 for(const auto &F
: M
.functions())
35 if (llvm::isFunctionInPrintList(F
.getName()))
38 return PreservedAnalyses::all();
41 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
42 PrintFunctionPass::PrintFunctionPass(raw_ostream
&OS
, const std::string
&Banner
)
43 : OS(OS
), Banner(Banner
) {}
45 PreservedAnalyses
PrintFunctionPass::run(Function
&F
,
46 FunctionAnalysisManager
&) {
47 if (isFunctionInPrintList(F
.getName())) {
48 if (forcePrintModuleIR())
49 OS
<< Banner
<< " (function: " << F
.getName() << ")\n" << *F
.getParent();
51 OS
<< Banner
<< static_cast<Value
&>(F
);
53 return PreservedAnalyses::all();
58 class PrintModulePassWrapper
: public ModulePass
{
63 PrintModulePassWrapper() : ModulePass(ID
) {}
64 PrintModulePassWrapper(raw_ostream
&OS
, const std::string
&Banner
,
65 bool ShouldPreserveUseListOrder
)
66 : ModulePass(ID
), P(OS
, Banner
, ShouldPreserveUseListOrder
) {}
68 bool runOnModule(Module
&M
) override
{
69 ModuleAnalysisManager DummyMAM
;
74 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
78 StringRef
getPassName() const override
{ return "Print Module IR"; }
81 class PrintFunctionPassWrapper
: public FunctionPass
{
86 PrintFunctionPassWrapper() : FunctionPass(ID
) {}
87 PrintFunctionPassWrapper(raw_ostream
&OS
, const std::string
&Banner
)
88 : FunctionPass(ID
), P(OS
, Banner
) {}
90 // This pass just prints a banner followed by the function as it's processed.
91 bool runOnFunction(Function
&F
) override
{
92 FunctionAnalysisManager DummyFAM
;
97 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
101 StringRef
getPassName() const override
{ return "Print Function IR"; }
104 class PrintBasicBlockPass
: public BasicBlockPass
{
110 PrintBasicBlockPass() : BasicBlockPass(ID
), Out(dbgs()) {}
111 PrintBasicBlockPass(raw_ostream
&Out
, const std::string
&Banner
)
112 : BasicBlockPass(ID
), Out(Out
), Banner(Banner
) {}
114 bool runOnBasicBlock(BasicBlock
&BB
) override
{
119 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
120 AU
.setPreservesAll();
123 StringRef
getPassName() const override
{ return "Print BasicBlock IR"; }
128 char PrintModulePassWrapper::ID
= 0;
129 INITIALIZE_PASS(PrintModulePassWrapper
, "print-module",
130 "Print module to stderr", false, true)
131 char PrintFunctionPassWrapper::ID
= 0;
132 INITIALIZE_PASS(PrintFunctionPassWrapper
, "print-function",
133 "Print function to stderr", false, true)
134 char PrintBasicBlockPass::ID
= 0;
135 INITIALIZE_PASS(PrintBasicBlockPass
, "print-bb", "Print BB to stderr", false,
138 ModulePass
*llvm::createPrintModulePass(llvm::raw_ostream
&OS
,
139 const std::string
&Banner
,
140 bool ShouldPreserveUseListOrder
) {
141 return new PrintModulePassWrapper(OS
, Banner
, ShouldPreserveUseListOrder
);
144 FunctionPass
*llvm::createPrintFunctionPass(llvm::raw_ostream
&OS
,
145 const std::string
&Banner
) {
146 return new PrintFunctionPassWrapper(OS
, Banner
);
149 BasicBlockPass
*llvm::createPrintBasicBlockPass(llvm::raw_ostream
&OS
,
150 const std::string
&Banner
) {
151 return new PrintBasicBlockPass(OS
, Banner
);
154 bool llvm::isIRPrintingPass(Pass
*P
) {
155 const char *PID
= (const char*)P
->getPassID();
157 return (PID
== &PrintModulePassWrapper::ID
)
158 || (PID
== &PrintFunctionPassWrapper::ID
)
159 || (PID
== &PrintBasicBlockPass::ID
);