1 //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // PrintModulePass and PrintFunctionPass implementations.
11 //
12 //===----------------------------------------------------------------------===//
13
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"
21 using namespace llvm;
22
PrintModulePass()23 PrintModulePass::PrintModulePass() : OS(dbgs()) {}
PrintModulePass(raw_ostream & OS,const std::string & Banner,bool ShouldPreserveUseListOrder)24 PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner,
25 bool ShouldPreserveUseListOrder)
26 : OS(OS), Banner(Banner),
27 ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {}
28
run(Module & M,AnalysisManager<Module> &)29 PreservedAnalyses PrintModulePass::run(Module &M, AnalysisManager<Module> &) {
30 OS << Banner;
31 if (llvm::isFunctionInPrintList("*"))
32 M.print(OS, nullptr, ShouldPreserveUseListOrder);
33 else {
34 for(const auto &F : M.functions())
35 if (llvm::isFunctionInPrintList(F.getName()))
36 F.print(OS);
37 }
38 return PreservedAnalyses::all();
39 }
40
PrintFunctionPass()41 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
PrintFunctionPass(raw_ostream & OS,const std::string & Banner)42 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
43 : OS(OS), Banner(Banner) {}
44
run(Function & F,AnalysisManager<Function> &)45 PreservedAnalyses PrintFunctionPass::run(Function &F,
46 AnalysisManager<Function> &) {
47 if (isFunctionInPrintList(F.getName()))
48 OS << Banner << static_cast<Value &>(F);
49 return PreservedAnalyses::all();
50 }
51
52 namespace {
53
54 class PrintModulePassWrapper : public ModulePass {
55 PrintModulePass P;
56
57 public:
58 static char ID;
PrintModulePassWrapper()59 PrintModulePassWrapper() : ModulePass(ID) {}
PrintModulePassWrapper(raw_ostream & OS,const std::string & Banner,bool ShouldPreserveUseListOrder)60 PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
61 bool ShouldPreserveUseListOrder)
62 : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
63
runOnModule(Module & M)64 bool runOnModule(Module &M) override {
65 ModuleAnalysisManager DummyMAM;
66 P.run(M, DummyMAM);
67 return false;
68 }
69
getAnalysisUsage(AnalysisUsage & AU) const70 void getAnalysisUsage(AnalysisUsage &AU) const override {
71 AU.setPreservesAll();
72 }
73 };
74
75 class PrintFunctionPassWrapper : public FunctionPass {
76 PrintFunctionPass P;
77
78 public:
79 static char ID;
PrintFunctionPassWrapper()80 PrintFunctionPassWrapper() : FunctionPass(ID) {}
PrintFunctionPassWrapper(raw_ostream & OS,const std::string & Banner)81 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
82 : FunctionPass(ID), P(OS, Banner) {}
83
84 // This pass just prints a banner followed by the function as it's processed.
runOnFunction(Function & F)85 bool runOnFunction(Function &F) override {
86 FunctionAnalysisManager DummyFAM;
87 P.run(F, DummyFAM);
88 return false;
89 }
90
getAnalysisUsage(AnalysisUsage & AU) const91 void getAnalysisUsage(AnalysisUsage &AU) const override {
92 AU.setPreservesAll();
93 }
94 };
95
96 class PrintBasicBlockPass : public BasicBlockPass {
97 raw_ostream &Out;
98 std::string Banner;
99
100 public:
101 static char ID;
PrintBasicBlockPass()102 PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
PrintBasicBlockPass(raw_ostream & Out,const std::string & Banner)103 PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
104 : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
105
runOnBasicBlock(BasicBlock & BB)106 bool runOnBasicBlock(BasicBlock &BB) override {
107 Out << Banner << BB;
108 return false;
109 }
110
getAnalysisUsage(AnalysisUsage & AU) const111 void getAnalysisUsage(AnalysisUsage &AU) const override {
112 AU.setPreservesAll();
113 }
114 };
115
116 }
117
118 char PrintModulePassWrapper::ID = 0;
119 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
120 "Print module to stderr", false, false)
121 char PrintFunctionPassWrapper::ID = 0;
122 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
123 "Print function to stderr", false, false)
124 char PrintBasicBlockPass::ID = 0;
125 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
126 false)
127
createPrintModulePass(llvm::raw_ostream & OS,const std::string & Banner,bool ShouldPreserveUseListOrder)128 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
129 const std::string &Banner,
130 bool ShouldPreserveUseListOrder) {
131 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
132 }
133
createPrintFunctionPass(llvm::raw_ostream & OS,const std::string & Banner)134 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
135 const std::string &Banner) {
136 return new PrintFunctionPassWrapper(OS, Banner);
137 }
138
createPrintBasicBlockPass(llvm::raw_ostream & OS,const std::string & Banner)139 BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS,
140 const std::string &Banner) {
141 return new PrintBasicBlockPass(OS, Banner);
142 }
143