Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 1 | //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===// |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 2 | // |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 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 | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 7 | // |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chandler Carruth | c179d7b | 2018-10-09 04:30:23 +0000 | [diff] [blame] | 10 | // This file defines a `-dot-cfg` analysis pass, which emits the |
| 11 | // `<prefix>.<fnname>.dot` file for each function in the program, with a graph |
| 12 | // of the CFG for that function. The default value for `<prefix>` is `cfg` but |
| 13 | // can be customized as needed. |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 14 | // |
| 15 | // The other main feature of this file is that it implements the |
| 16 | // Function::viewCFG method, which is useful for debugging passes which operate |
| 17 | // on the CFG. |
| 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
Brian Gaeke | c6e2d8a | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 21 | #include "llvm/Analysis/CFGPrinter.h" |
Chris Lattner | 88067b9 | 2009-10-18 04:09:11 +0000 | [diff] [blame] | 22 | #include "llvm/Pass.h" |
Benjamin Kramer | 7259f14 | 2014-04-29 23:26:49 +0000 | [diff] [blame] | 23 | #include "llvm/Support/FileSystem.h" |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 24 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 25 | |
Xinliang David Li | 8544cc8 | 2018-07-25 17:22:12 +0000 | [diff] [blame] | 26 | static cl::opt<std::string> CFGFuncName( |
| 27 | "cfg-func-name", cl::Hidden, |
| 28 | cl::desc("The name of a function (or its substring)" |
| 29 | " whose CFG is viewed/printed.")); |
| 30 | |
Chandler Carruth | c179d7b | 2018-10-09 04:30:23 +0000 | [diff] [blame] | 31 | static cl::opt<std::string> CFGDotFilenamePrefix( |
| 32 | "cfg-dot-filename-prefix", cl::Hidden, |
| 33 | cl::desc("The prefix used for the CFG dot file names.")); |
| 34 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 35 | namespace { |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 36 | struct CFGViewerLegacyPass : public FunctionPass { |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 37 | static char ID; // Pass identifcation, replacement for typeid |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 38 | CFGViewerLegacyPass() : FunctionPass(ID) { |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 39 | initializeCFGViewerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 40 | } |
Devang Patel | 1cee94f | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 41 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 42 | bool runOnFunction(Function &F) override { |
| 43 | F.viewCFG(); |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 44 | return false; |
| 45 | } |
| 46 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 47 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 48 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 49 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 50 | AU.setPreservesAll(); |
| 51 | } |
| 52 | }; |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 53 | } |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 54 | |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 55 | char CFGViewerLegacyPass::ID = 0; |
| 56 | INITIALIZE_PASS(CFGViewerLegacyPass, "view-cfg", "View CFG of function", false, true) |
| 57 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 58 | PreservedAnalyses CFGViewerPass::run(Function &F, |
| 59 | FunctionAnalysisManager &AM) { |
| 60 | F.viewCFG(); |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 61 | return PreservedAnalyses::all(); |
| 62 | } |
| 63 | |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 64 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 65 | namespace { |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 66 | struct CFGOnlyViewerLegacyPass : public FunctionPass { |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 67 | static char ID; // Pass identifcation, replacement for typeid |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 68 | CFGOnlyViewerLegacyPass() : FunctionPass(ID) { |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 69 | initializeCFGOnlyViewerLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 70 | } |
Devang Patel | 1cee94f | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 71 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 72 | bool runOnFunction(Function &F) override { |
| 73 | F.viewCFGOnly(); |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 77 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 78 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 79 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 80 | AU.setPreservesAll(); |
| 81 | } |
| 82 | }; |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 83 | } |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 84 | |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 85 | char CFGOnlyViewerLegacyPass::ID = 0; |
| 86 | INITIALIZE_PASS(CFGOnlyViewerLegacyPass, "view-cfg-only", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 87 | "View CFG of function (with no function bodies)", false, true) |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 88 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 89 | PreservedAnalyses CFGOnlyViewerPass::run(Function &F, |
| 90 | FunctionAnalysisManager &AM) { |
| 91 | F.viewCFGOnly(); |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 92 | return PreservedAnalyses::all(); |
| 93 | } |
| 94 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 95 | static void writeCFGToDotFile(Function &F, bool CFGOnly = false) { |
Xinliang David Li | 8544cc8 | 2018-07-25 17:22:12 +0000 | [diff] [blame] | 96 | if (!CFGFuncName.empty() && !F.getName().contains(CFGFuncName)) |
| 97 | return; |
Chandler Carruth | c179d7b | 2018-10-09 04:30:23 +0000 | [diff] [blame] | 98 | std::string Filename = |
| 99 | (CFGDotFilenamePrefix + "." + F.getName() + ".dot").str(); |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 100 | errs() << "Writing '" << Filename << "'..."; |
| 101 | |
| 102 | std::error_code EC; |
| 103 | raw_fd_ostream File(Filename, EC, sys::fs::F_Text); |
| 104 | |
| 105 | if (!EC) |
| 106 | WriteGraph(File, (const Function*)&F, CFGOnly); |
| 107 | else |
| 108 | errs() << " error opening file for writing!"; |
| 109 | errs() << "\n"; |
| 110 | } |
| 111 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 112 | namespace { |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 113 | struct CFGPrinterLegacyPass : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 114 | static char ID; // Pass identification, replacement for typeid |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 115 | CFGPrinterLegacyPass() : FunctionPass(ID) { |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 116 | initializeCFGPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 117 | } |
Devang Patel | 1cee94f | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 118 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 119 | bool runOnFunction(Function &F) override { |
| 120 | writeCFGToDotFile(F); |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 121 | return false; |
| 122 | } |
| 123 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 124 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 125 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 126 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 127 | AU.setPreservesAll(); |
| 128 | } |
| 129 | }; |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 130 | } |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 131 | |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 132 | char CFGPrinterLegacyPass::ID = 0; |
Fangrui Song | af7b183 | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 133 | INITIALIZE_PASS(CFGPrinterLegacyPass, "dot-cfg", "Print CFG of function to 'dot' file", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 134 | false, true) |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 135 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 136 | PreservedAnalyses CFGPrinterPass::run(Function &F, |
| 137 | FunctionAnalysisManager &AM) { |
| 138 | writeCFGToDotFile(F); |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 139 | return PreservedAnalyses::all(); |
| 140 | } |
| 141 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 142 | namespace { |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 143 | struct CFGOnlyPrinterLegacyPass : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 144 | static char ID; // Pass identification, replacement for typeid |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 145 | CFGOnlyPrinterLegacyPass() : FunctionPass(ID) { |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 146 | initializeCFGOnlyPrinterLegacyPassPass(*PassRegistry::getPassRegistry()); |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 147 | } |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 148 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 149 | bool runOnFunction(Function &F) override { |
| 150 | writeCFGToDotFile(F, /*CFGOnly=*/true); |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 151 | return false; |
| 152 | } |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 153 | void print(raw_ostream &OS, const Module* = nullptr) const override {} |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 154 | |
Craig Topper | c37e6c0 | 2014-03-05 07:30:04 +0000 | [diff] [blame] | 155 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 156 | AU.setPreservesAll(); |
| 157 | } |
| 158 | }; |
Alexander Kornienko | cd52a7a | 2015-06-23 09:49:53 +0000 | [diff] [blame] | 159 | } |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 160 | |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 161 | char CFGOnlyPrinterLegacyPass::ID = 0; |
| 162 | INITIALIZE_PASS(CFGOnlyPrinterLegacyPass, "dot-cfg-only", |
Owen Anderson | 02dd53e | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 163 | "Print CFG of function to 'dot' file (with no function bodies)", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 164 | false, true) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 165 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 166 | PreservedAnalyses CFGOnlyPrinterPass::run(Function &F, |
| 167 | FunctionAnalysisManager &AM) { |
| 168 | writeCFGToDotFile(F, /*CFGOnly=*/true); |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 169 | return PreservedAnalyses::all(); |
| 170 | } |
| 171 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 172 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 173 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 174 | /// program, displaying the CFG of the current function. This depends on there |
| 175 | /// being a 'dot' and 'gv' program in your path. |
| 176 | /// |
| 177 | void Function::viewCFG() const { |
Xinliang David Li | 8544cc8 | 2018-07-25 17:22:12 +0000 | [diff] [blame] | 178 | if (!CFGFuncName.empty() && !getName().contains(CFGFuncName)) |
| 179 | return; |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 180 | ViewGraph(this, "cfg" + getName()); |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 181 | } |
| 182 | |
| 183 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 184 | /// just like viewCFG, but it does not include the contents of basic blocks |
Eric Christopher | 68c7a1c | 2014-05-20 17:11:11 +0000 | [diff] [blame] | 185 | /// into the nodes, just the label. If you are only interested in the CFG |
| 186 | /// this can make the graph smaller. |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 187 | /// |
| 188 | void Function::viewCFGOnly() const { |
Xinliang David Li | 8544cc8 | 2018-07-25 17:22:12 +0000 | [diff] [blame] | 189 | if (!CFGFuncName.empty() && !getName().contains(CFGFuncName)) |
| 190 | return; |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 191 | ViewGraph(this, "cfg" + getName(), true); |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 192 | } |
Brian Gaeke | c6e2d8a | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 193 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 194 | FunctionPass *llvm::createCFGPrinterLegacyPassPass () { |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 195 | return new CFGPrinterLegacyPass(); |
Brian Gaeke | c6e2d8a | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 198 | FunctionPass *llvm::createCFGOnlyPrinterLegacyPassPass () { |
Sriraman Tallam | f31e663 | 2016-09-15 18:35:27 +0000 | [diff] [blame] | 199 | return new CFGOnlyPrinterLegacyPass(); |
Brian Gaeke | c6e2d8a | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 200 | } |
Sean Fertile | 551913f | 2018-06-29 17:48:58 +0000 | [diff] [blame] | 201 | |