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)29 PreservedAnalyses PrintModulePass::run(Module &M) {
30 OS << Banner;
31 M.print(OS, nullptr, ShouldPreserveUseListOrder);
32 return PreservedAnalyses::all();
33 }
34
PrintFunctionPass()35 PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {}
PrintFunctionPass(raw_ostream & OS,const std::string & Banner)36 PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner)
37 : OS(OS), Banner(Banner) {}
38
run(Function & F)39 PreservedAnalyses PrintFunctionPass::run(Function &F) {
40 OS << Banner << static_cast<Value &>(F);
41 return PreservedAnalyses::all();
42 }
43
44 namespace {
45
46 class PrintModulePassWrapper : public ModulePass {
47 PrintModulePass P;
48
49 public:
50 static char ID;
PrintModulePassWrapper()51 PrintModulePassWrapper() : ModulePass(ID) {}
PrintModulePassWrapper(raw_ostream & OS,const std::string & Banner,bool ShouldPreserveUseListOrder)52 PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner,
53 bool ShouldPreserveUseListOrder)
54 : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {}
55
runOnModule(Module & M)56 bool runOnModule(Module &M) override {
57 P.run(M);
58 return false;
59 }
60
getAnalysisUsage(AnalysisUsage & AU) const61 void getAnalysisUsage(AnalysisUsage &AU) const override {
62 AU.setPreservesAll();
63 }
64 };
65
66 class PrintFunctionPassWrapper : public FunctionPass {
67 PrintFunctionPass P;
68
69 public:
70 static char ID;
PrintFunctionPassWrapper()71 PrintFunctionPassWrapper() : FunctionPass(ID) {}
PrintFunctionPassWrapper(raw_ostream & OS,const std::string & Banner)72 PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner)
73 : FunctionPass(ID), P(OS, Banner) {}
74
75 // This pass just prints a banner followed by the function as it's processed.
runOnFunction(Function & F)76 bool runOnFunction(Function &F) override {
77 P.run(F);
78 return false;
79 }
80
getAnalysisUsage(AnalysisUsage & AU) const81 void getAnalysisUsage(AnalysisUsage &AU) const override {
82 AU.setPreservesAll();
83 }
84 };
85
86 class PrintBasicBlockPass : public BasicBlockPass {
87 raw_ostream &Out;
88 std::string Banner;
89
90 public:
91 static char ID;
PrintBasicBlockPass()92 PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {}
PrintBasicBlockPass(raw_ostream & Out,const std::string & Banner)93 PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner)
94 : BasicBlockPass(ID), Out(Out), Banner(Banner) {}
95
runOnBasicBlock(BasicBlock & BB)96 bool runOnBasicBlock(BasicBlock &BB) override {
97 Out << Banner << BB;
98 return false;
99 }
100
getAnalysisUsage(AnalysisUsage & AU) const101 void getAnalysisUsage(AnalysisUsage &AU) const override {
102 AU.setPreservesAll();
103 }
104 };
105
106 }
107
108 char PrintModulePassWrapper::ID = 0;
109 INITIALIZE_PASS(PrintModulePassWrapper, "print-module",
110 "Print module to stderr", false, false)
111 char PrintFunctionPassWrapper::ID = 0;
112 INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function",
113 "Print function to stderr", false, false)
114 char PrintBasicBlockPass::ID = 0;
115 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
116 false)
117
createPrintModulePass(llvm::raw_ostream & OS,const std::string & Banner,bool ShouldPreserveUseListOrder)118 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
119 const std::string &Banner,
120 bool ShouldPreserveUseListOrder) {
121 return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder);
122 }
123
createPrintFunctionPass(llvm::raw_ostream & OS,const std::string & Banner)124 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
125 const std::string &Banner) {
126 return new PrintFunctionPassWrapper(OS, Banner);
127 }
128
createPrintBasicBlockPass(llvm::raw_ostream & OS,const std::string & Banner)129 BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS,
130 const std::string &Banner) {
131 return new PrintBasicBlockPass(OS, Banner);
132 }
133