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 | // |
Duncan Sands | 3ee8fc9 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 10 | // This file defines a '-dot-cfg' analysis pass, which emits the |
Chris Lattner | 002362c | 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 | c6e2d8a | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 20 | #include "llvm/Analysis/CFGPrinter.h" |
Chris Lattner | 88067b9 | 2009-10-18 04:09:11 +0000 | [diff] [blame] | 21 | |
| 22 | #include "llvm/Pass.h" |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 23 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 25 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 26 | struct CFGViewer : public FunctionPass { |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 27 | static char ID; // Pass identifcation, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 28 | CFGViewer() : FunctionPass(ID) { |
| 29 | initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry()); |
| 30 | } |
Devang Patel | 1cee94f | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 31 | |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 32 | virtual bool runOnFunction(Function &F) { |
| 33 | F.viewCFG(); |
| 34 | return false; |
| 35 | } |
| 36 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 37 | void print(raw_ostream &OS, const Module* = 0) const {} |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 38 | |
| 39 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 40 | AU.setPreservesAll(); |
| 41 | } |
| 42 | }; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 43 | } |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 44 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 45 | char CFGViewer::ID = 0; |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 46 | INITIALIZE_PASS(CFGViewer, "view-cfg", "View CFG of function", false, true) |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 47 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 48 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 49 | struct CFGOnlyViewer : public FunctionPass { |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 50 | static char ID; // Pass identifcation, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 51 | CFGOnlyViewer() : FunctionPass(ID) { |
| 52 | initializeCFGOnlyViewerPass(*PassRegistry::getPassRegistry()); |
| 53 | } |
Devang Patel | 1cee94f | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 54 | |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 55 | virtual bool runOnFunction(Function &F) { |
Duncan Sands | ad38936 | 2009-09-19 11:25:44 +0000 | [diff] [blame] | 56 | F.viewCFGOnly(); |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 57 | return false; |
| 58 | } |
| 59 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 60 | void print(raw_ostream &OS, const Module* = 0) const {} |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 61 | |
| 62 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 63 | AU.setPreservesAll(); |
| 64 | } |
| 65 | }; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 66 | } |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 67 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 68 | char CFGOnlyViewer::ID = 0; |
Owen Anderson | d8cc7be | 2010-07-21 23:07:00 +0000 | [diff] [blame] | 69 | INITIALIZE_PASS(CFGOnlyViewer, "view-cfg-only", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 70 | "View CFG of function (with no function bodies)", false, true) |
Dan Gohman | a196b99 | 2007-05-14 14:25:08 +0000 | [diff] [blame] | 71 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 72 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 73 | struct CFGPrinter : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 74 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 75 | CFGPrinter() : FunctionPass(ID) { |
| 76 | initializeCFGPrinterPass(*PassRegistry::getPassRegistry()); |
| 77 | } |
Devang Patel | 1cee94f | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 79 | virtual bool runOnFunction(Function &F) { |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 80 | std::string Filename = "cfg." + F.getName().str() + ".dot"; |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 81 | errs() << "Writing '" << Filename << "'..."; |
| 82 | |
| 83 | std::string ErrorInfo; |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 84 | raw_fd_ostream File(Filename.c_str(), ErrorInfo); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 85 | |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 86 | if (ErrorInfo.empty()) |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 87 | WriteGraph(File, (const Function*)&F); |
| 88 | else |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 89 | errs() << " error opening file for writing!"; |
| 90 | errs() << "\n"; |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 94 | void print(raw_ostream &OS, const Module* = 0) const {} |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 95 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 96 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 97 | AU.setPreservesAll(); |
| 98 | } |
| 99 | }; |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 100 | } |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 101 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 102 | char CFGPrinter::ID = 0; |
Owen Anderson | 02dd53e | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 103 | INITIALIZE_PASS(CFGPrinter, "dot-cfg", "Print CFG of function to 'dot' file", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 104 | false, true) |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 105 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 106 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 107 | struct CFGOnlyPrinter : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame] | 108 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 109 | CFGOnlyPrinter() : FunctionPass(ID) { |
| 110 | initializeCFGOnlyPrinterPass(*PassRegistry::getPassRegistry()); |
| 111 | } |
| 112 | |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 113 | virtual bool runOnFunction(Function &F) { |
Benjamin Kramer | a7b0cb7 | 2011-11-15 16:27:03 +0000 | [diff] [blame] | 114 | std::string Filename = "cfg." + F.getName().str() + ".dot"; |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 115 | errs() << "Writing '" << Filename << "'..."; |
Owen Anderson | 8cbc94a | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 117 | std::string ErrorInfo; |
Dan Gohman | baa2639 | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 118 | raw_fd_ostream File(Filename.c_str(), ErrorInfo); |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 119 | |
| 120 | if (ErrorInfo.empty()) |
Owen Anderson | 8cbc94a | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 121 | WriteGraph(File, (const Function*)&F, true); |
| 122 | else |
Chris Lattner | 103289e | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 123 | errs() << " error opening file for writing!"; |
| 124 | errs() << "\n"; |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 125 | return false; |
| 126 | } |
Chris Lattner | 45cfe54 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 127 | void print(raw_ostream &OS, const Module* = 0) const {} |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 128 | |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 129 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 130 | AU.setPreservesAll(); |
| 131 | } |
| 132 | }; |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 133 | } |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 134 | |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 135 | char CFGOnlyPrinter::ID = 0; |
Owen Anderson | 02dd53e | 2010-08-23 17:52:01 +0000 | [diff] [blame] | 136 | INITIALIZE_PASS(CFGOnlyPrinter, "dot-cfg-only", |
| 137 | "Print CFG of function to 'dot' file (with no function bodies)", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 138 | false, true) |
Dan Gohman | 844731a | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 139 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 140 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 141 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 142 | /// program, displaying the CFG of the current function. This depends on there |
| 143 | /// being a 'dot' and 'gv' program in your path. |
| 144 | /// |
| 145 | void Function::viewCFG() const { |
Benjamin Kramer | 25ad1cc | 2011-11-15 16:26:38 +0000 | [diff] [blame] | 146 | ViewGraph(this, "cfg" + getName()); |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 150 | /// just like viewCFG, but it does not include the contents of basic blocks |
| 151 | /// into the nodes, just the label. If you are only interested in the CFG t |
| 152 | /// his can make the graph smaller. |
| 153 | /// |
| 154 | void Function::viewCFGOnly() const { |
Benjamin Kramer | 25ad1cc | 2011-11-15 16:26:38 +0000 | [diff] [blame] | 155 | ViewGraph(this, "cfg" + getName(), true); |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 156 | } |
Brian Gaeke | c6e2d8a | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 157 | |
| 158 | FunctionPass *llvm::createCFGPrinterPass () { |
| 159 | return new CFGPrinter(); |
| 160 | } |
| 161 | |
| 162 | FunctionPass *llvm::createCFGOnlyPrinterPass () { |
| 163 | return new CFGOnlyPrinter(); |
| 164 | } |
| 165 | |