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