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