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