Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 1 | //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===// |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Duncan Sands | 9c40c28 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 10 | // This file defines a '-dot-cfg' analysis pass, which emits the |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 11 | // cfg.<fnname>.dot file for each function in the program, with a graph of the |
| 12 | // CFG for that function. |
| 13 | // |
| 14 | // The other main feature of this file is that it implements the |
| 15 | // Function::viewCFG method, which is useful for debugging passes which operate |
| 16 | // on the CFG. |
| 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CFGPrinter.h" |
Chris Lattner | da42426 | 2009-10-18 04:09:11 +0000 | [diff] [blame] | 21 | #include "llvm/Pass.h" |
Benjamin Kramer | d59664f | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/FileSystem.h" |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | 960707c | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 25 | namespace { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 26 | struct CFGViewerLegacyPass : public FunctionPass { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 27 | static char ID; // Pass identifcation, replacement for typeid |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 28 | CFGViewerLegacyPass() : FunctionPass(ID) { |
| 29 | initializeCFGViewerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 30 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 31 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 32 | bool runOnFunction(Function &F) override { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 33 | F.viewCFG(); |
| 34 | return false; |
| 35 | } |
| 36 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 37 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 38 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 39 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 40 | AU.setPreservesAll(); |
| 41 | } |
| 42 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 43 | } |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 44 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 45 | char CFGViewerLegacyPass::ID = 0; |
| 46 | INITIALIZE_PASS(CFGViewerLegacyPass, "view-cfg", "View CFG of function", false, true) |
| 47 | |
| 48 | PreservedAnalyses CFGViewerPass::run(Function &F, |
| 49 | FunctionAnalysisManager &AM) { |
| 50 | F.viewCFG(); |
| 51 | return PreservedAnalyses::all(); |
| 52 | } |
| 53 | |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 54 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 55 | namespace { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 56 | struct CFGOnlyViewerLegacyPass : public FunctionPass { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 57 | static char ID; // Pass identifcation, replacement for typeid |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 58 | CFGOnlyViewerLegacyPass() : FunctionPass(ID) { |
| 59 | initializeCFGOnlyViewerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 60 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 61 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 62 | bool runOnFunction(Function &F) override { |
Duncan Sands | 1636b7e | 2009-09-19 11:25:44 +0000 | [diff] [blame] | 63 | F.viewCFGOnly(); |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 67 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 68 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 69 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 70 | AU.setPreservesAll(); |
| 71 | } |
| 72 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 73 | } |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 74 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 75 | char CFGOnlyViewerLegacyPass::ID = 0; |
| 76 | INITIALIZE_PASS(CFGOnlyViewerLegacyPass, "view-cfg-only", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 77 | "View CFG of function (with no function bodies)", false, true) |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 78 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 79 | PreservedAnalyses CFGOnlyViewerPass::run(Function &F, |
| 80 | FunctionAnalysisManager &AM) { |
| 81 | F.viewCFGOnly(); |
| 82 | return PreservedAnalyses::all(); |
| 83 | } |
| 84 | |
| 85 | static void writeCFGToDotFile(Function &F) { |
| 86 | std::string Filename = ("cfg." + F.getName() + ".dot").str(); |
| 87 | errs() << "Writing '" << Filename << "'..."; |
| 88 | |
| 89 | std::error_code EC; |
| 90 | raw_fd_ostream File(Filename, EC, sys::fs::F_Text); |
| 91 | |
| 92 | if (!EC) |
| 93 | WriteGraph(File, (const Function*)&F); |
| 94 | else |
| 95 | errs() << " error opening file for writing!"; |
| 96 | errs() << "\n"; |
| 97 | } |
| 98 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 99 | namespace { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 100 | struct CFGPrinterLegacyPass : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 101 | static char ID; // Pass identification, replacement for typeid |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 102 | CFGPrinterLegacyPass() : FunctionPass(ID) { |
| 103 | initializeCFGPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 104 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 105 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 106 | bool runOnFunction(Function &F) override { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 107 | writeCFGToDotFile(F); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 108 | return false; |
| 109 | } |
| 110 | |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 111 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 112 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 113 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 114 | AU.setPreservesAll(); |
| 115 | } |
| 116 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 117 | } |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 118 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 119 | char CFGPrinterLegacyPass::ID = 0; |
| 120 | INITIALIZE_PASS(CFGPrinterLegacyPass, "dot-cfg", "Print CFG of function to 'dot' file", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 121 | false, true) |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 122 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 123 | PreservedAnalyses CFGPrinterPass::run(Function &F, |
| 124 | FunctionAnalysisManager &AM) { |
| 125 | writeCFGToDotFile(F); |
| 126 | return PreservedAnalyses::all(); |
| 127 | } |
| 128 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 129 | namespace { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 130 | struct CFGOnlyPrinterLegacyPass : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 131 | static char ID; // Pass identification, replacement for typeid |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 132 | CFGOnlyPrinterLegacyPass() : FunctionPass(ID) { |
| 133 | initializeCFGOnlyPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 134 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 135 | |
| 136 | bool runOnFunction(Function &F) override { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 137 | writeCFGToDotFile(F); |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 138 | return false; |
| 139 | } |
Craig Topper | 9f00886 | 2014-04-15 04:59:12 +0000 | [diff] [blame] | 140 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 141 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 142 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 143 | AU.setPreservesAll(); |
| 144 | } |
| 145 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 146 | } |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 147 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 148 | char CFGOnlyPrinterLegacyPass::ID = 0; |
| 149 | INITIALIZE_PASS(CFGOnlyPrinterLegacyPass, "dot-cfg-only", |
Owen Anderson | d31d82d | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 150 | "Print CFG of function to 'dot' file (with no function bodies)", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 151 | false, true) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 152 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 153 | PreservedAnalyses CFGOnlyPrinterPass::run(Function &F, |
| 154 | FunctionAnalysisManager &AM) { |
| 155 | writeCFGToDotFile(F); |
| 156 | return PreservedAnalyses::all(); |
| 157 | } |
| 158 | |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 159 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 160 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 161 | /// program, displaying the CFG of the current function. This depends on there |
| 162 | /// being a 'dot' and 'gv' program in your path. |
| 163 | /// |
| 164 | void Function::viewCFG() const { |
Benjamin Kramer | 4c93d15 | 2011-11-15 16:26:38 +0000 | [diff] [blame] | 165 | ViewGraph(this, "cfg" + getName()); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 169 | /// just like viewCFG, but it does not include the contents of basic blocks |
Eric Christopher | 650c8f2 | 2014-05-20 17:11:11 +0000 | [diff] [blame] | 170 | /// into the nodes, just the label. If you are only interested in the CFG |
| 171 | /// this can make the graph smaller. |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 172 | /// |
| 173 | void Function::viewCFGOnly() const { |
Benjamin Kramer | 4c93d15 | 2011-11-15 16:26:38 +0000 | [diff] [blame] | 174 | ViewGraph(this, "cfg" + getName(), true); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 175 | } |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 176 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 177 | FunctionPass *llvm::createCFGPrinterLegacyPassPass () { |
| 178 | return new CFGPrinterLegacyPass(); |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 181 | FunctionPass *llvm::createCFGOnlyPrinterLegacyPassPass () { |
| 182 | return new CFGOnlyPrinterLegacyPass(); |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 183 | } |
| 184 | |