Chandler Carruth | c2c50cd | 2013-01-02 09:10:48 +0000 | [diff] [blame] | 1 | //===--- IR/PrintModulePass.cpp - Module/Function Printer -----------------===// |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 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/Assembly/PrintModulePass.h" |
Chandler Carruth | 0b8c9a8 | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Function.h" |
| 16 | #include "llvm/IR/Module.h" |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
David Greene | 9a6c940 | 2010-01-05 01:30:18 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 24 | class PrintModulePass : public ModulePass { |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 25 | std::string Banner; |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 26 | raw_ostream *Out; // raw_ostream to print on |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 27 | bool DeleteStream; // Delete the ostream in our dtor? |
| 28 | public: |
| 29 | static char ID; |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 30 | PrintModulePass() : ModulePass(ID), Out(&dbgs()), |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 31 | DeleteStream(false) {} |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 32 | PrintModulePass(const std::string &B, raw_ostream *o, bool DS) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 33 | : ModulePass(ID), Banner(B), Out(o), DeleteStream(DS) {} |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 34 | |
| 35 | ~PrintModulePass() { |
| 36 | if (DeleteStream) delete Out; |
| 37 | } |
| 38 | |
| 39 | bool runOnModule(Module &M) { |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 40 | (*Out) << Banner << M; |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 41 | return false; |
| 42 | } |
| 43 | |
| 44 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 45 | AU.setPreservesAll(); |
| 46 | } |
| 47 | }; |
| 48 | |
| 49 | class PrintFunctionPass : public FunctionPass { |
| 50 | std::string Banner; // String to print before each function |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 51 | raw_ostream *Out; // raw_ostream to print on |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 52 | bool DeleteStream; // Delete the ostream in our dtor? |
| 53 | public: |
| 54 | static char ID; |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 55 | PrintFunctionPass() : FunctionPass(ID), Banner(""), Out(&dbgs()), |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 56 | DeleteStream(false) {} |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 57 | PrintFunctionPass(const std::string &B, raw_ostream *o, bool DS) |
Owen Anderson | 90c579d | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 58 | : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {} |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 59 | |
Dan Gohman | b85b31b | 2010-08-16 21:57:30 +0000 | [diff] [blame] | 60 | ~PrintFunctionPass() { |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 61 | if (DeleteStream) delete Out; |
| 62 | } |
| 63 | |
| 64 | // runOnFunction - This pass just prints a banner followed by the |
| 65 | // function as it's processed. |
| 66 | // |
| 67 | bool runOnFunction(Function &F) { |
| 68 | (*Out) << Banner << static_cast<Value&>(F); |
| 69 | return false; |
| 70 | } |
| 71 | |
| 72 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 73 | AU.setPreservesAll(); |
| 74 | } |
| 75 | }; |
Sergei Larin | 68b2faf | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 76 | |
| 77 | class PrintBasicBlockPass : public BasicBlockPass { |
| 78 | std::string Banner; |
| 79 | raw_ostream *Out; // raw_ostream to print on |
| 80 | bool DeleteStream; // Delete the ostream in our dtor? |
| 81 | public: |
| 82 | static char ID; |
| 83 | PrintBasicBlockPass() : BasicBlockPass(ID), Out(&dbgs()), |
| 84 | DeleteStream(false) {} |
| 85 | PrintBasicBlockPass(const std::string &B, raw_ostream *o, bool DS) |
| 86 | : BasicBlockPass(ID), Banner(B), Out(o), DeleteStream(DS) {} |
| 87 | |
| 88 | ~PrintBasicBlockPass() { |
| 89 | if (DeleteStream) delete Out; |
| 90 | } |
| 91 | |
| 92 | bool runOnBasicBlock(BasicBlock &BB) { |
| 93 | (*Out) << Banner << BB; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 98 | AU.setPreservesAll(); |
| 99 | } |
| 100 | }; |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | char PrintModulePass::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 104 | INITIALIZE_PASS(PrintModulePass, "print-module", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 105 | "Print module to stderr", false, false) |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 106 | char PrintFunctionPass::ID = 0; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 107 | INITIALIZE_PASS(PrintFunctionPass, "print-function", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 108 | "Print function to stderr", false, false) |
Sergei Larin | 68b2faf | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 109 | char PrintBasicBlockPass::ID = 0; |
| 110 | INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", |
| 111 | "Print BB to stderr", false, false) |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 112 | |
| 113 | /// createPrintModulePass - Create and return a pass that writes the |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 114 | /// module to the specified raw_ostream. |
| 115 | ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS, |
David Greene | 5c8aa95 | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 116 | bool DeleteStream, |
| 117 | const std::string &Banner) { |
| 118 | return new PrintModulePass(Banner, OS, DeleteStream); |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /// createPrintFunctionPass - Create and return a pass that prints |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 122 | /// functions to the specified raw_ostream as they are processed. |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 123 | FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner, |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 124 | llvm::raw_ostream *OS, |
| 125 | bool DeleteStream) { |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 126 | return new PrintFunctionPass(Banner, OS, DeleteStream); |
| 127 | } |
| 128 | |
Sergei Larin | 68b2faf | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 129 | /// createPrintBasicBlockPass - Create and return a pass that writes the |
| 130 | /// BB to the specified raw_ostream. |
| 131 | BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream *OS, |
| 132 | bool DeleteStream, |
| 133 | const std::string &Banner) { |
| 134 | return new PrintBasicBlockPass(Banner, OS, DeleteStream); |
| 135 | } |
| 136 | |