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