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 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source 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 | // |
| 10 | // This file defines a '-print-cfg' analysis pass, which emits the |
| 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 | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 34 | /// CFGOnly flag - This is used to control whether or not the CFG graph printer |
| 35 | /// prints out the contents of basic blocks or not. This is acceptable because |
| 36 | /// this code is only really used for debugging purposes. |
| 37 | /// |
| 38 | static bool CFGOnly = false; |
| 39 | |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 40 | namespace llvm { |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 41 | template<> |
| 42 | struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits { |
| 43 | static std::string getGraphName(const Function *F) { |
| 44 | return "CFG for '" + F->getName() + "' function"; |
| 45 | } |
| 46 | |
| 47 | static std::string getNodeLabel(const BasicBlock *Node, |
| 48 | const Function *Graph) { |
Chris Lattner | d073ea0 | 2003-10-22 16:30:58 +0000 | [diff] [blame] | 49 | if (CFGOnly && !Node->getName().empty()) |
| 50 | return Node->getName() + ":"; |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 51 | |
| 52 | std::ostringstream Out; |
Chris Lattner | a75b3ef | 2003-10-22 16:22:42 +0000 | [diff] [blame] | 53 | if (CFGOnly) { |
Chris Lattner | 3749c9c | 2006-12-06 06:16:21 +0000 | [diff] [blame] | 54 | WriteAsOperand(Out, Node, false); |
Chris Lattner | a75b3ef | 2003-10-22 16:22:42 +0000 | [diff] [blame] | 55 | return Out.str(); |
| 56 | } |
| 57 | |
Chris Lattner | d073ea0 | 2003-10-22 16:30:58 +0000 | [diff] [blame] | 58 | if (Node->getName().empty()) { |
Chris Lattner | 3749c9c | 2006-12-06 06:16:21 +0000 | [diff] [blame] | 59 | WriteAsOperand(Out, Node, false); |
Chris Lattner | d073ea0 | 2003-10-22 16:30:58 +0000 | [diff] [blame] | 60 | Out << ":"; |
| 61 | } |
| 62 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 63 | Out << *Node; |
| 64 | std::string OutStr = Out.str(); |
| 65 | if (OutStr[0] == '\n') OutStr.erase(OutStr.begin()); |
| 66 | |
| 67 | // Process string output to make it nicer... |
| 68 | for (unsigned i = 0; i != OutStr.length(); ++i) |
| 69 | if (OutStr[i] == '\n') { // Left justify |
| 70 | OutStr[i] = '\\'; |
| 71 | OutStr.insert(OutStr.begin()+i+1, 'l'); |
| 72 | } else if (OutStr[i] == ';') { // Delete comments! |
| 73 | unsigned Idx = OutStr.find('\n', i+1); // Find end of line |
| 74 | OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx); |
| 75 | --i; |
| 76 | } |
| 77 | |
| 78 | return OutStr; |
| 79 | } |
| 80 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 81 | static std::string getEdgeSourceLabel(const BasicBlock *Node, |
| 82 | succ_const_iterator I) { |
| 83 | // Label source of conditional branches with "T" or "F" |
| 84 | if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator())) |
| 85 | if (BI->isConditional()) |
| 86 | return (I == succ_begin(Node)) ? "T" : "F"; |
| 87 | return ""; |
| 88 | } |
| 89 | }; |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 90 | } |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 91 | |
| 92 | namespace { |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 93 | struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame^] | 94 | static char ID; // Pass identification, replacement for typeid |
Devang Patel | 794fd75 | 2007-05-01 21:15:47 +0000 | [diff] [blame] | 95 | CFGPrinter() : FunctionPass((intptr_t)&ID) {} |
| 96 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 97 | virtual bool runOnFunction(Function &F) { |
| 98 | std::string Filename = "cfg." + F.getName() + ".dot"; |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 99 | cerr << "Writing '" << Filename << "'..."; |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 100 | std::ofstream File(Filename.c_str()); |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 101 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 102 | if (File.good()) |
| 103 | WriteGraph(File, (const Function*)&F); |
| 104 | else |
Bill Wendling | e815619 | 2006-12-07 01:30:32 +0000 | [diff] [blame] | 105 | cerr << " error opening file for writing!"; |
| 106 | cerr << "\n"; |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 107 | return false; |
| 108 | } |
| 109 | |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 110 | void print(std::ostream &OS, const Module* = 0) const {} |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 112 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 113 | AU.setPreservesAll(); |
| 114 | } |
| 115 | }; |
| 116 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 117 | char CFGPrinter::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 118 | RegisterPass<CFGPrinter> P1("print-cfg", |
| 119 | "Print CFG of function to 'dot' file"); |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 120 | |
Reid Spencer | d7d83db | 2007-02-05 23:42:17 +0000 | [diff] [blame] | 121 | struct VISIBILITY_HIDDEN CFGOnlyPrinter : public CFGPrinter { |
Nick Lewycky | ecd94c8 | 2007-05-06 13:37:16 +0000 | [diff] [blame^] | 122 | static char ID; // Pass identification, replacement for typeid |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 123 | virtual bool runOnFunction(Function &F) { |
| 124 | bool OldCFGOnly = CFGOnly; |
| 125 | CFGOnly = true; |
| 126 | CFGPrinter::runOnFunction(F); |
| 127 | CFGOnly = OldCFGOnly; |
| 128 | return false; |
| 129 | } |
Reid Spencer | ce9653c | 2004-12-07 04:03:45 +0000 | [diff] [blame] | 130 | void print(std::ostream &OS, const Module* = 0) const {} |
Misha Brukman | 2b37d7c | 2005-04-21 21:13:18 +0000 | [diff] [blame] | 131 | |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 132 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 133 | AU.setPreservesAll(); |
| 134 | } |
| 135 | }; |
| 136 | |
Devang Patel | 1997473 | 2007-05-03 01:11:54 +0000 | [diff] [blame] | 137 | char CFGOnlyPrinter::ID = 0; |
Chris Lattner | 5d8925c | 2006-08-27 22:30:17 +0000 | [diff] [blame] | 138 | RegisterPass<CFGOnlyPrinter> |
Chris Lattner | 1ca2a58 | 2003-12-11 21:48:18 +0000 | [diff] [blame] | 139 | P2("print-cfg-only", |
| 140 | "Print CFG of function to 'dot' file (with no function bodies)"); |
| 141 | } |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 142 | |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 143 | /// viewCFG - This function is meant for use from the debugger. You can just |
| 144 | /// say 'call F->viewCFG()' and a ghostview window should pop up from the |
| 145 | /// program, displaying the CFG of the current function. This depends on there |
| 146 | /// being a 'dot' and 'gv' program in your path. |
| 147 | /// |
| 148 | void Function::viewCFG() const { |
Reid Spencer | 9d5b532 | 2006-06-27 16:49:46 +0000 | [diff] [blame] | 149 | ViewGraph(this, "cfg" + getName()); |
Chris Lattner | 002362c | 2003-10-22 16:03:49 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | /// viewCFGOnly - This function is meant for use from the debugger. It works |
| 153 | /// just like viewCFG, but it does not include the contents of basic blocks |
| 154 | /// into the nodes, just the label. If you are only interested in the CFG t |
| 155 | /// his can make the graph smaller. |
| 156 | /// |
| 157 | void Function::viewCFGOnly() const { |
| 158 | CFGOnly = true; |
| 159 | viewCFG(); |
| 160 | CFGOnly = false; |
| 161 | } |
Brian Gaeke | c6e2d8a | 2004-04-26 16:27:08 +0000 | [diff] [blame] | 162 | |
| 163 | FunctionPass *llvm::createCFGPrinterPass () { |
| 164 | return new CFGPrinter(); |
| 165 | } |
| 166 | |
| 167 | FunctionPass *llvm::createCFGOnlyPrinterPass () { |
| 168 | return new CFGOnlyPrinter(); |
| 169 | } |
| 170 | |