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