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 | |
| 20 | #include "llvm/Function.h" |
| 21 | #include "llvm/Instructions.h" |
| 22 | #include "llvm/Pass.h" |
| 23 | #include "llvm/Analysis/CFGPrinter.h" |
| 24 | #include "llvm/Assembly/Writer.h" |
| 25 | #include "llvm/Support/CFG.h" |
| 26 | #include "llvm/Support/Compiler.h" |
| 27 | #include "llvm/Support/GraphWriter.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 30 | namespace llvm { |
| 31 | template<> |
| 32 | struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits { |
| 33 | static std::string getGraphName(const Function *F) { |
Daniel Dunbar | 1e13b97 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 34 | return "CFG for '" + F->getNameStr() + "' function"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | static std::string getNodeLabel(const BasicBlock *Node, |
Owen Anderson | f4a1546 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 38 | const Function *Graph, |
| 39 | bool ShortNames) { |
| 40 | if (ShortNames && !Node->getName().empty()) |
Daniel Dunbar | 1e13b97 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 41 | return Node->getNameStr() + ":"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 8a6411c | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 43 | std::string Str; |
| 44 | raw_string_ostream OS(Str); |
| 45 | |
Owen Anderson | f4a1546 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 46 | if (ShortNames) { |
Chris Lattner | 8a6411c | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 47 | WriteAsOperand(OS, Node, false); |
| 48 | return OS.str(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | if (Node->getName().empty()) { |
Chris Lattner | 8a6411c | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 52 | WriteAsOperand(OS, Node, false); |
| 53 | OS << ":"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 54 | } |
Chris Lattner | 8a6411c | 2009-08-23 04:37:46 +0000 | [diff] [blame] | 55 | |
| 56 | OS << *Node; |
| 57 | std::string OutStr = OS.str(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 58 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 59 | |
| 60 | // Process string output to make it nicer... |
| 61 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 62 | if (OutStr[i] == '\n') { // Left justify |
| 63 | OutStr[i] = '\\'; |
| 64 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 65 | } else if (OutStr[i] == ';') { // Delete comments! |
| 66 | unsigned Idx = OutStr.find('\n', i+1); // Find end of line |
| 67 | OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx); |
| 68 | --i; |
| 69 | } |
| 70 | |
| 71 | return OutStr; |
| 72 | } |
| 73 | |
| 74 | static std::string getEdgeSourceLabel(const BasicBlock *Node, |
| 75 | succ_const_iterator I) { |
| 76 | // Label source of conditional branches with "T" or "F" |
| 77 | if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator())) |
| 78 | if (BI->isConditional()) |
| 79 | return (I == succ_begin(Node)) ? "T" : "F"; |
| 80 | return ""; |
| 81 | } |
| 82 | }; |
| 83 | } |
| 84 | |
| 85 | namespace { |
| 86 | struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass { |
| 87 | static char ID; // Pass identifcation, replacement for typeid |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 88 | CFGViewer() : FunctionPass(&ID) {} |
Devang Patel | 2b4fa68 | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 89 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 90 | virtual bool runOnFunction(Function &F) { |
| 91 | F.viewCFG(); |
| 92 | return false; |
| 93 | } |
| 94 | |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 95 | void print(raw_ostream &OS, const Module* = 0) const {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 96 | |
| 97 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 98 | AU.setPreservesAll(); |
| 99 | } |
| 100 | }; |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 101 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 102 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 103 | char CFGViewer::ID = 0; |
| 104 | static RegisterPass<CFGViewer> |
| 105 | V0("view-cfg", "View CFG of function", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 106 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 107 | namespace { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 108 | struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass { |
| 109 | static char ID; // Pass identifcation, replacement for typeid |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 110 | CFGOnlyViewer() : FunctionPass(&ID) {} |
Devang Patel | 2b4fa68 | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 111 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 112 | virtual bool runOnFunction(Function &F) { |
Duncan Sands | d899f59 | 2009-09-19 11:25:44 +0000 | [diff] [blame^] | 113 | F.viewCFGOnly(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 114 | return false; |
| 115 | } |
| 116 | |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 117 | void print(raw_ostream &OS, const Module* = 0) const {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 118 | |
| 119 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 120 | AU.setPreservesAll(); |
| 121 | } |
| 122 | }; |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 123 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 124 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 125 | char CFGOnlyViewer::ID = 0; |
| 126 | static RegisterPass<CFGOnlyViewer> |
| 127 | V1("view-cfg-only", |
| 128 | "View CFG of function (with no function bodies)", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 129 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 130 | namespace { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 131 | struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass { |
| 132 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 26f8c27 | 2008-09-04 17:05:41 +0000 | [diff] [blame] | 133 | CFGPrinter() : FunctionPass(&ID) {} |
| 134 | explicit CFGPrinter(void *pid) : FunctionPass(pid) {} |
Devang Patel | 2b4fa68 | 2008-03-18 00:39:19 +0000 | [diff] [blame] | 135 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 136 | virtual bool runOnFunction(Function &F) { |
Daniel Dunbar | 1e13b97 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 137 | std::string Filename = "cfg." + F.getNameStr() + ".dot"; |
Chris Lattner | bf9d76d | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 138 | errs() << "Writing '" << Filename << "'..."; |
| 139 | |
| 140 | std::string ErrorInfo; |
Dan Gohman | 176426d | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 141 | raw_fd_ostream File(Filename.c_str(), ErrorInfo); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 142 | |
Chris Lattner | bf9d76d | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 143 | if (ErrorInfo.empty()) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 144 | WriteGraph(File, (const Function*)&F); |
| 145 | else |
Chris Lattner | bf9d76d | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 146 | errs() << " error opening file for writing!"; |
| 147 | errs() << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 148 | return false; |
| 149 | } |
| 150 | |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 151 | void print(raw_ostream &OS, const Module* = 0) const {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 152 | |
| 153 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 154 | AU.setPreservesAll(); |
| 155 | } |
| 156 | }; |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 157 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 158 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 159 | char CFGPrinter::ID = 0; |
| 160 | static RegisterPass<CFGPrinter> |
Duncan Sands | d10d6f7 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 161 | P1("dot-cfg", "Print CFG of function to 'dot' file", false, true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 162 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 163 | namespace { |
Owen Anderson | f4a1546 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 164 | struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 165 | static char ID; // Pass identification, replacement for typeid |
Owen Anderson | f4a1546 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 166 | CFGOnlyPrinter() : FunctionPass(&ID) {} |
| 167 | explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 168 | virtual bool runOnFunction(Function &F) { |
Daniel Dunbar | 1e13b97 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 169 | std::string Filename = "cfg." + F.getNameStr() + ".dot"; |
Chris Lattner | bf9d76d | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 170 | errs() << "Writing '" << Filename << "'..."; |
Owen Anderson | f4a1546 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 171 | |
Chris Lattner | bf9d76d | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 172 | std::string ErrorInfo; |
Dan Gohman | 176426d | 2009-08-25 15:34:52 +0000 | [diff] [blame] | 173 | raw_fd_ostream File(Filename.c_str(), ErrorInfo); |
Chris Lattner | bf9d76d | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 174 | |
| 175 | if (ErrorInfo.empty()) |
Owen Anderson | f4a1546 | 2009-06-24 17:37:09 +0000 | [diff] [blame] | 176 | WriteGraph(File, (const Function*)&F, true); |
| 177 | else |
Chris Lattner | bf9d76d | 2009-08-23 07:19:13 +0000 | [diff] [blame] | 178 | errs() << " error opening file for writing!"; |
| 179 | errs() << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 180 | return false; |
| 181 | } |
Chris Lattner | 397f456 | 2009-08-23 06:03:38 +0000 | [diff] [blame] | 182 | void print(raw_ostream &OS, const Module* = 0) const {} |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 183 | |
| 184 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 185 | AU.setPreservesAll(); |
| 186 | } |
| 187 | }; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 188 | } |
| 189 | |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 190 | char CFGOnlyPrinter::ID = 0; |
| 191 | static RegisterPass<CFGOnlyPrinter> |
Duncan Sands | d10d6f7 | 2008-09-23 12:47:39 +0000 | [diff] [blame] | 192 | P2("dot-cfg-only", |
Dan Gohman | 089efff | 2008-05-13 00:00:25 +0000 | [diff] [blame] | 193 | "Print CFG of function to 'dot' file (with no function bodies)", false, true); |
| 194 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 195 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 196 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 197 | /// program, displaying the CFG of the current function. This depends on there |
| 198 | /// being a 'dot' and 'gv' program in your path. |
| 199 | /// |
| 200 | void Function::viewCFG() const { |
Daniel Dunbar | 1e13b97 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 201 | ViewGraph(this, "cfg" + getNameStr()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 205 | /// just like viewCFG, but it does not include the contents of basic blocks |
| 206 | /// into the nodes, just the label. If you are only interested in the CFG t |
| 207 | /// his can make the graph smaller. |
| 208 | /// |
| 209 | void Function::viewCFGOnly() const { |
Daniel Dunbar | 1e13b97 | 2009-07-24 08:24:36 +0000 | [diff] [blame] | 210 | ViewGraph(this, "cfg" + getNameStr(), true); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | FunctionPass *llvm::createCFGPrinterPass () { |
| 214 | return new CFGPrinter(); |
| 215 | } |
| 216 | |
| 217 | FunctionPass *llvm::createCFGOnlyPrinterPass () { |
| 218 | return new CFGOnlyPrinter(); |
| 219 | } |
| 220 | |