[PM] Simplify the interface exposed for IR printing passes.
[oota-llvm.git] / lib / IR / IRPrintingPasses.cpp
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/Pass.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/raw_ostream.h"
20 using namespace llvm;
21
22 namespace {
23
24 class PrintModulePass : public ModulePass {
25   std::string Banner;
26   raw_ostream *Out;
27   bool DeleteStream;
28
29 public:
30   static char ID;
31   PrintModulePass() : ModulePass(ID), Out(&dbgs()), DeleteStream(false) {}
32   PrintModulePass(const std::string &B, raw_ostream *o, bool DS)
33       : ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {}
34
35   ~PrintModulePass() {
36     if (DeleteStream)
37       delete Out;
38   }
39
40   bool runOnModule(Module &M) {
41     (*Out) << Banner << M;
42     return false;
43   }
44
45   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
46     AU.setPreservesAll();
47   }
48 };
49
50 class PrintFunctionPass : public FunctionPass {
51   std::string Banner;
52   raw_ostream *Out;
53   bool DeleteStream;
54
55 public:
56   static char ID;
57   PrintFunctionPass()
58       : FunctionPass(ID), Banner(""), Out(&dbgs()), DeleteStream(false) {}
59   PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS)
60       : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
61
62   ~PrintFunctionPass() {
63     if (DeleteStream)
64       delete Out;
65   }
66
67   // This pass just prints a banner followed by the function as it's processed.
68   bool runOnFunction(Function &F) {
69     (*Out) << Banner << static_cast<Value &>(F);
70     return false;
71   }
72
73   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
74     AU.setPreservesAll();
75   }
76 };
77
78 class PrintBasicBlockPass : public BasicBlockPass {
79   std::string Banner;
80   raw_ostream *Out;
81   bool DeleteStream;
82
83 public:
84   static char ID;
85   PrintBasicBlockPass()
86       : BasicBlockPass(ID), Out(&dbgs()), DeleteStream(false) {}
87   PrintBasicBlockPass(const std::string &B, raw_ostream *o, bool DS)
88       : BasicBlockPass(ID), Banner(B), Out(o), DeleteStream(DS) {}
89
90   ~PrintBasicBlockPass() {
91     if (DeleteStream)
92       delete Out;
93   }
94
95   bool runOnBasicBlock(BasicBlock &BB) {
96     (*Out) << Banner << BB;
97     return false;
98   }
99
100   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
101     AU.setPreservesAll();
102   }
103 };
104 }
105
106 char PrintModulePass::ID = 0;
107 INITIALIZE_PASS(PrintModulePass, "print-module", "Print module to stderr",
108                 false, false)
109 char PrintFunctionPass::ID = 0;
110 INITIALIZE_PASS(PrintFunctionPass, "print-function", "Print function to stderr",
111                 false, false)
112 char PrintBasicBlockPass::ID = 0;
113 INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false,
114                 false)
115
116 ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS,
117                                         const std::string &Banner) {
118   return new PrintModulePass(Banner, &OS, false);
119 }
120
121 FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS,
122                                             const std::string &Banner) {
123   return new PrintFunctionPass(Banner, &OS, false);
124 }
125
126 BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS,
127                                                 const std::string &Banner) {
128   return new PrintBasicBlockPass(Banner, &OS, false);
129 }