Chandler Carruth | 41d9e92 | 2014-01-09 02:39:45 +0000 | [diff] [blame] | 1 | //===--- IRPrintingPasses.cpp - Module and Function printing passes -------===// |
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 | |
Chandler Carruth | 8a5351f | 2014-01-12 11:10:32 +0000 | [diff] [blame] | 14 | #include "llvm/IR/IRPrintingPasses.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" |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 17 | #include "llvm/IR/PassManager.h" |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 18 | #include "llvm/Pass.h" |
David Greene | 9a6c940 | 2010-01-05 01:30:18 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Debug.h" |
Daniel Dunbar | 3b0da26 | 2008-10-22 03:25:22 +0000 | [diff] [blame] | 20 | #include "llvm/Support/raw_ostream.h" |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 23 | PrintModulePass::PrintModulePass() : OS(dbgs()) {} |
Duncan P. N. Exon Smith | 2c7f24d | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 24 | PrintModulePass::PrintModulePass(raw_ostream &OS, const std::string &Banner, |
| 25 | bool ShouldPreserveUseListOrder) |
| 26 | : OS(OS), Banner(Banner), |
| 27 | ShouldPreserveUseListOrder(ShouldPreserveUseListOrder) {} |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 28 | |
Sean Silva | 2fb9a98 | 2016-08-09 00:28:38 +0000 | [diff] [blame] | 29 | PreservedAnalyses PrintModulePass::run(Module &M, ModuleAnalysisManager &) { |
Sven van Haastregt | 71e1958 | 2018-11-14 10:05:28 +0000 | [diff] [blame] | 30 | if (!Banner.empty()) |
| 31 | OS << Banner << "\n"; |
Weiming Zhao | b85410e | 2016-01-06 22:55:03 +0000 | [diff] [blame] | 32 | if (llvm::isFunctionInPrintList("*")) |
| 33 | M.print(OS, nullptr, ShouldPreserveUseListOrder); |
| 34 | else { |
| 35 | for(const auto &F : M.functions()) |
| 36 | if (llvm::isFunctionInPrintList(F.getName())) |
| 37 | F.print(OS); |
| 38 | } |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 39 | return PreservedAnalyses::all(); |
| 40 | } |
| 41 | |
| 42 | PrintFunctionPass::PrintFunctionPass() : OS(dbgs()) {} |
| 43 | PrintFunctionPass::PrintFunctionPass(raw_ostream &OS, const std::string &Banner) |
| 44 | : OS(OS), Banner(Banner) {} |
| 45 | |
Chandler Carruth | 04d0fe9 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 46 | PreservedAnalyses PrintFunctionPass::run(Function &F, |
Sean Silva | 20b343c | 2016-08-09 00:28:15 +0000 | [diff] [blame] | 47 | FunctionAnalysisManager &) { |
Benjamin Kramer | 79df7df | 2017-12-01 18:39:58 +0000 | [diff] [blame] | 48 | if (isFunctionInPrintList(F.getName())) { |
Fedor Sergeev | 7d160f7 | 2017-12-01 17:42:46 +0000 | [diff] [blame] | 49 | if (forcePrintModuleIR()) |
| 50 | OS << Banner << " (function: " << F.getName() << ")\n" << *F.getParent(); |
| 51 | else |
| 52 | OS << Banner << static_cast<Value &>(F); |
Benjamin Kramer | 79df7df | 2017-12-01 18:39:58 +0000 | [diff] [blame] | 53 | } |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 54 | return PreservedAnalyses::all(); |
| 55 | } |
| 56 | |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 57 | namespace { |
| 58 | |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 59 | class PrintModulePassWrapper : public ModulePass { |
| 60 | PrintModulePass P; |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 61 | |
| 62 | public: |
| 63 | static char ID; |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 64 | PrintModulePassWrapper() : ModulePass(ID) {} |
Duncan P. N. Exon Smith | 2c7f24d | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 65 | PrintModulePassWrapper(raw_ostream &OS, const std::string &Banner, |
| 66 | bool ShouldPreserveUseListOrder) |
| 67 | : ModulePass(ID), P(OS, Banner, ShouldPreserveUseListOrder) {} |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 68 | |
Craig Topper | 98f54c0 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 69 | bool runOnModule(Module &M) override { |
Chandler Carruth | 04d0fe9 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 70 | ModuleAnalysisManager DummyMAM; |
| 71 | P.run(M, DummyMAM); |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 72 | return false; |
| 73 | } |
| 74 | |
Craig Topper | 98f54c0 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 75 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 76 | AU.setPreservesAll(); |
| 77 | } |
Yaron Keren | ea56368 | 2017-03-10 07:09:20 +0000 | [diff] [blame] | 78 | |
Michael Kruse | f29303d | 2017-08-25 12:38:53 +0000 | [diff] [blame] | 79 | StringRef getPassName() const override { return "Print Module IR"; } |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 82 | class PrintFunctionPassWrapper : public FunctionPass { |
| 83 | PrintFunctionPass P; |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 84 | |
| 85 | public: |
| 86 | static char ID; |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 87 | PrintFunctionPassWrapper() : FunctionPass(ID) {} |
| 88 | PrintFunctionPassWrapper(raw_ostream &OS, const std::string &Banner) |
| 89 | : FunctionPass(ID), P(OS, Banner) {} |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 90 | |
| 91 | // This pass just prints a banner followed by the function as it's processed. |
Craig Topper | 98f54c0 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 92 | bool runOnFunction(Function &F) override { |
Chandler Carruth | 04d0fe9 | 2016-06-17 00:11:01 +0000 | [diff] [blame] | 93 | FunctionAnalysisManager DummyFAM; |
| 94 | P.run(F, DummyFAM); |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
Craig Topper | 98f54c0 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 98 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 99 | AU.setPreservesAll(); |
| 100 | } |
Yaron Keren | ea56368 | 2017-03-10 07:09:20 +0000 | [diff] [blame] | 101 | |
Michael Kruse | f29303d | 2017-08-25 12:38:53 +0000 | [diff] [blame] | 102 | StringRef getPassName() const override { return "Print Function IR"; } |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | class PrintBasicBlockPass : public BasicBlockPass { |
Chandler Carruth | 313e399 | 2014-01-12 11:40:03 +0000 | [diff] [blame] | 106 | raw_ostream &Out; |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 107 | std::string Banner; |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 108 | |
| 109 | public: |
| 110 | static char ID; |
Chandler Carruth | 313e399 | 2014-01-12 11:40:03 +0000 | [diff] [blame] | 111 | PrintBasicBlockPass() : BasicBlockPass(ID), Out(dbgs()) {} |
| 112 | PrintBasicBlockPass(raw_ostream &Out, const std::string &Banner) |
| 113 | : BasicBlockPass(ID), Out(Out), Banner(Banner) {} |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 114 | |
Craig Topper | 98f54c0 | 2014-03-05 06:35:38 +0000 | [diff] [blame] | 115 | bool runOnBasicBlock(BasicBlock &BB) override { |
Chandler Carruth | 313e399 | 2014-01-12 11:40:03 +0000 | [diff] [blame] | 116 | Out << Banner << BB; |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
Hans Wennborg | 68b0d1d | 2014-05-24 20:19:40 +0000 | [diff] [blame] | 120 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 121 | AU.setPreservesAll(); |
| 122 | } |
Yaron Keren | ea56368 | 2017-03-10 07:09:20 +0000 | [diff] [blame] | 123 | |
| 124 | StringRef getPassName() const override { return "Print BasicBlock IR"; } |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 125 | }; |
Chandler Carruth | 313e399 | 2014-01-12 11:40:03 +0000 | [diff] [blame] | 126 | |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 127 | } |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 128 | |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 129 | char PrintModulePassWrapper::ID = 0; |
| 130 | INITIALIZE_PASS(PrintModulePassWrapper, "print-module", |
Duncan P. N. Exon Smith | 15753b0 | 2018-07-11 23:30:25 +0000 | [diff] [blame] | 131 | "Print module to stderr", false, true) |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 132 | char PrintFunctionPassWrapper::ID = 0; |
| 133 | INITIALIZE_PASS(PrintFunctionPassWrapper, "print-function", |
Duncan P. N. Exon Smith | 15753b0 | 2018-07-11 23:30:25 +0000 | [diff] [blame] | 134 | "Print function to stderr", false, true) |
Sergei Larin | 68b2faf | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 135 | char PrintBasicBlockPass::ID = 0; |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 136 | INITIALIZE_PASS(PrintBasicBlockPass, "print-bb", "Print BB to stderr", false, |
Duncan P. N. Exon Smith | 15753b0 | 2018-07-11 23:30:25 +0000 | [diff] [blame] | 137 | true) |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 138 | |
Chandler Carruth | a5ced5e | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 139 | ModulePass *llvm::createPrintModulePass(llvm::raw_ostream &OS, |
Duncan P. N. Exon Smith | 2c7f24d | 2015-04-15 02:38:06 +0000 | [diff] [blame] | 140 | const std::string &Banner, |
| 141 | bool ShouldPreserveUseListOrder) { |
| 142 | return new PrintModulePassWrapper(OS, Banner, ShouldPreserveUseListOrder); |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 143 | } |
| 144 | |
Chandler Carruth | a5ced5e | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 145 | FunctionPass *llvm::createPrintFunctionPass(llvm::raw_ostream &OS, |
| 146 | const std::string &Banner) { |
Chandler Carruth | a595257 | 2014-01-12 12:15:39 +0000 | [diff] [blame] | 147 | return new PrintFunctionPassWrapper(OS, Banner); |
Daniel Dunbar | f4db3a5 | 2008-10-21 23:33:38 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Chandler Carruth | a5ced5e | 2014-01-12 11:30:46 +0000 | [diff] [blame] | 150 | BasicBlockPass *llvm::createPrintBasicBlockPass(llvm::raw_ostream &OS, |
Chandler Carruth | 2ad3b87 | 2014-01-12 11:16:01 +0000 | [diff] [blame] | 151 | const std::string &Banner) { |
Chandler Carruth | 313e399 | 2014-01-12 11:40:03 +0000 | [diff] [blame] | 152 | return new PrintBasicBlockPass(OS, Banner); |
Sergei Larin | 68b2faf | 2013-02-08 23:37:41 +0000 | [diff] [blame] | 153 | } |
Vedant Kumar | b12dd1f | 2018-05-15 00:29:27 +0000 | [diff] [blame] | 154 | |
| 155 | bool llvm::isIRPrintingPass(Pass *P) { |
| 156 | const char *PID = (const char*)P->getPassID(); |
| 157 | |
| 158 | return (PID == &PrintModulePassWrapper::ID) |
| 159 | || (PID == &PrintFunctionPassWrapper::ID) |
| 160 | || (PID == &PrintBasicBlockPass::ID); |
| 161 | } |