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 | |
Xinliang David Li | 45a607e | 2018-07-25 17:22:12 +0000 | [diff] [blame] | 25 | static cl::opt<std::string> CFGFuncName( |
| 26 | "cfg-func-name", cl::Hidden, |
| 27 | cl::desc("The name of a function (or its substring)" |
| 28 | " whose CFG is viewed/printed.")); |
| 29 | |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 30 | namespace { |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 31 | struct CFGViewerLegacyPass : public FunctionPass { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 32 | static char ID; // Pass identifcation, replacement for typeid |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 33 | CFGViewerLegacyPass() : FunctionPass(ID) { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 34 | initializeCFGViewerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 35 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 36 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 37 | bool runOnFunction(Function &F) override { |
| 38 | F.viewCFG(); |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 39 | return false; |
| 40 | } |
| 41 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 42 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 43 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 44 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 45 | AU.setPreservesAll(); |
| 46 | } |
| 47 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 48 | } |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 49 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 50 | char CFGViewerLegacyPass::ID = 0; |
| 51 | INITIALIZE_PASS(CFGViewerLegacyPass, "view-cfg", "View CFG of function", false, true) |
| 52 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 53 | PreservedAnalyses CFGViewerPass::run(Function &F, |
| 54 | FunctionAnalysisManager &AM) { |
| 55 | F.viewCFG(); |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 56 | return PreservedAnalyses::all(); |
| 57 | } |
| 58 | |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 59 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 60 | namespace { |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 61 | struct CFGOnlyViewerLegacyPass : public FunctionPass { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 62 | static char ID; // Pass identifcation, replacement for typeid |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 63 | CFGOnlyViewerLegacyPass() : FunctionPass(ID) { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 64 | initializeCFGOnlyViewerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 65 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 66 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 67 | bool runOnFunction(Function &F) override { |
| 68 | F.viewCFGOnly(); |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 69 | return false; |
| 70 | } |
| 71 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 72 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 73 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 74 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 75 | AU.setPreservesAll(); |
| 76 | } |
| 77 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 78 | } |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 79 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 80 | char CFGOnlyViewerLegacyPass::ID = 0; |
| 81 | INITIALIZE_PASS(CFGOnlyViewerLegacyPass, "view-cfg-only", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 82 | "View CFG of function (with no function bodies)", false, true) |
Dan Gohman | 90d97ac | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 83 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 84 | PreservedAnalyses CFGOnlyViewerPass::run(Function &F, |
| 85 | FunctionAnalysisManager &AM) { |
| 86 | F.viewCFGOnly(); |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 87 | return PreservedAnalyses::all(); |
| 88 | } |
| 89 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 90 | static void writeCFGToDotFile(Function &F, bool CFGOnly = false) { |
Xinliang David Li | 45a607e | 2018-07-25 17:22:12 +0000 | [diff] [blame] | 91 | if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) |
| 92 | return; |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 93 | std::string Filename = ("cfg." + F.getName() + ".dot").str(); |
| 94 | errs() << "Writing '" << Filename << "'..."; |
| 95 | |
| 96 | std::error_code EC; |
| 97 | raw_fd_ostream File(Filename, EC, sys::fs::F_Text); |
| 98 | |
| 99 | if (!EC) |
| 100 | WriteGraph(File, (const Function*)&F, CFGOnly); |
| 101 | else |
| 102 | errs() << " error opening file for writing!"; |
| 103 | errs() << "\n"; |
| 104 | } |
| 105 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 106 | namespace { |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 107 | struct CFGPrinterLegacyPass : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 108 | static char ID; // Pass identification, replacement for typeid |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 109 | CFGPrinterLegacyPass() : FunctionPass(ID) { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 110 | initializeCFGPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 111 | } |
Devang Patel | 864970e | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 112 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 113 | bool runOnFunction(Function &F) override { |
| 114 | writeCFGToDotFile(F); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 115 | return false; |
| 116 | } |
| 117 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 118 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 119 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 120 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 121 | AU.setPreservesAll(); |
| 122 | } |
| 123 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 124 | } |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 125 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 126 | char CFGPrinterLegacyPass::ID = 0; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 127 | INITIALIZE_PASS(CFGPrinterLegacyPass, "dot-cfg", "Print CFG of function to 'dot' file", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 128 | false, true) |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 129 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 130 | PreservedAnalyses CFGPrinterPass::run(Function &F, |
| 131 | FunctionAnalysisManager &AM) { |
| 132 | writeCFGToDotFile(F); |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 133 | return PreservedAnalyses::all(); |
| 134 | } |
| 135 | |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 136 | namespace { |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 137 | struct CFGOnlyPrinterLegacyPass : public FunctionPass { |
Nick Lewycky | e7da2d6 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 138 | static char ID; // Pass identification, replacement for typeid |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 139 | CFGOnlyPrinterLegacyPass() : FunctionPass(ID) { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 140 | initializeCFGOnlyPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 6c18d1a | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 141 | } |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 142 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 143 | bool runOnFunction(Function &F) override { |
| 144 | writeCFGToDotFile(F, /*CFGOnly=*/true); |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 145 | return false; |
| 146 | } |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 147 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Misha Brukman | 01808ca | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 148 | |
Craig Topper | e9ba759 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 149 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 62aff84 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 150 | AU.setPreservesAll(); |
| 151 | } |
| 152 | }; |
Alexander Kornienko | f00654e | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 153 | } |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 154 | |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 155 | char CFGOnlyPrinterLegacyPass::ID = 0; |
| 156 | INITIALIZE_PASS(CFGOnlyPrinterLegacyPass, "dot-cfg-only", |
Owen Anderson | d31d82d | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 157 | "Print CFG of function to 'dot' file (with no function bodies)", |
Owen Anderson | df7a4f2 | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 158 | false, true) |
Dan Gohman | d78c400 | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 159 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 160 | PreservedAnalyses CFGOnlyPrinterPass::run(Function &F, |
| 161 | FunctionAnalysisManager &AM) { |
| 162 | writeCFGToDotFile(F, /*CFGOnly=*/true); |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 163 | return PreservedAnalyses::all(); |
| 164 | } |
| 165 | |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 166 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 167 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 168 | /// program, displaying the CFG of the current function. This depends on there |
| 169 | /// being a 'dot' and 'gv' program in your path. |
| 170 | /// |
| 171 | void Function::viewCFG() const { |
Xinliang David Li | 45a607e | 2018-07-25 17:22:12 +0000 | [diff] [blame] | 172 | if (!CFGFuncName.empty() && !getName().contains(CFGFuncName)) |
| 173 | return; |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 174 | ViewGraph(this, "cfg" + getName()); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 178 | /// 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] | 179 | /// into the nodes, just the label. If you are only interested in the CFG |
| 180 | /// this can make the graph smaller. |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 181 | /// |
| 182 | void Function::viewCFGOnly() const { |
Xinliang David Li | 45a607e | 2018-07-25 17:22:12 +0000 | [diff] [blame] | 183 | if (!CFGFuncName.empty() && !getName().contains(CFGFuncName)) |
| 184 | return; |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 185 | ViewGraph(this, "cfg" + getName(), true); |
Chris Lattner | a93d11b | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 186 | } |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 187 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 188 | FunctionPass *llvm::createCFGPrinterLegacyPassPass () { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 189 | return new CFGPrinterLegacyPass(); |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 190 | } |
| 191 | |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 192 | FunctionPass *llvm::createCFGOnlyPrinterLegacyPassPass () { |
Sriraman Tallam | 06a67ba | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 193 | return new CFGOnlyPrinterLegacyPass(); |
Brian Gaeke | 104341f | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 194 | } |
Sean Fertile | cd0d763 | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 195 | |