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