Chandler Carruth | 12e9d2b | 2014-01-09 02:39:45 +0000 | [diff] [blame] | 1 | //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===// |
Daniel Dunbar | 54d5b9e | 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 | |
Chandler Carruth | b8ddc70 | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 14 | #include "llvm/IR/IRPrintingPasses.h" |
Chandler Carruth | 9fb823b | 2013-01-02 11:36:10 +0000 | [diff] [blame] | 15 | #include "llvm/IR/Function.h" |
| 16 | #include "llvm/IR/Module.h" |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 17 | #include "llvm/Pass.h" |
David Greene | 5f2433e | 2010-01-05 01:30:18 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 19 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | namespace { |
| 23 | |
Chandler Carruth | 3dd261d | 2014-01-12 11:16:01 +0000 | [diff] [blame^] | 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) |
Owen Anderson | a7aed18 | 2010-08-06 18:33:48 +0000 | [diff] [blame] | 60 | : FunctionPass(ID), Banner(B), Out(o), DeleteStream(DS) {} |
Chandler Carruth | 3dd261d | 2014-01-12 11:16:01 +0000 | [diff] [blame^] | 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 | }; |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | char PrintModulePass::ID = 0; |
Chandler Carruth | 3dd261d | 2014-01-12 11:16:01 +0000 | [diff] [blame^] | 107 | INITIALIZE_PASS(PrintModulePass, "print-module", "Print module to stderr", |
| 108 | false, false) |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 109 | char PrintFunctionPass::ID = 0; |
Chandler Carruth | 3dd261d | 2014-01-12 11:16:01 +0000 | [diff] [blame^] | 110 | INITIALIZE_PASS(PrintFunctionPass, "print-function", "Print function to stderr", |
| 111 | false, false) |
Sergei Larin | cd1201b | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 112 | char PrintBasicBlockPass::ID = 0; |
Chandler Carruth | 3dd261d | 2014-01-12 11:16:01 +0000 | [diff] [blame^] | 113 | INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false, |
| 114 | false) |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 115 | |
Chandler Carruth | 3dd261d | 2014-01-12 11:16:01 +0000 | [diff] [blame^] | 116 | ModulePass *llvm::createPrintModulePass(llvm::raw_ostream *OS, |
David Greene | 9b063df | 2010-04-02 23:17:14 +0000 | [diff] [blame] | 117 | bool DeleteStream, |
| 118 | const std::string &Banner) { |
| 119 | return new PrintModulePass(Banner, OS, DeleteStream); |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 122 | FunctionPass *llvm::createPrintFunctionPass(const std::string &Banner, |
Chandler Carruth | 3dd261d | 2014-01-12 11:16:01 +0000 | [diff] [blame^] | 123 | llvm::raw_ostream *OS, |
Daniel Dunbar | 81b5fa5 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 124 | bool DeleteStream) { |
Daniel Dunbar | 54d5b9e | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 125 | return new PrintFunctionPass(Banner, OS, DeleteStream); |
| 126 | } |
| 127 | |
Sergei Larin | cd1201b | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 128 | BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream *OS, |
Chandler Carruth | 3dd261d | 2014-01-12 11:16:01 +0000 | [diff] [blame^] | 129 | bool DeleteStream, |
| 130 | const std::string &Banner) { |
Sergei Larin | cd1201b | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 131 | return new PrintBasicBlockPass(Banner, OS, DeleteStream); |
| 132 | } |