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