blob: 7b61ff8ebfe872dd84a303b0c117da5c18c6ab76 [file] [log] [blame]
Chris Lattner002362c2003-10-22 16:03:49 +00001//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
Chris Lattner002362c2003-10-22 16:03:49 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner4ee451d2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
Chris Lattner002362c2003-10-22 16:03:49 +00008//===----------------------------------------------------------------------===//
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 Lattner002362c2003-10-22 16:03:49 +000020#include "llvm/Function.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000021#include "llvm/Instructions.h"
Misha Brukmanbf94a1e2004-04-29 04:04:47 +000022#include "llvm/Pass.h"
Brian Gaekec6e2d8a2004-04-26 16:27:08 +000023#include "llvm/Analysis/CFGPrinter.h"
Misha Brukmanbf94a1e2004-04-29 04:04:47 +000024#include "llvm/Assembly/Writer.h"
Chris Lattner002362c2003-10-22 16:03:49 +000025#include "llvm/Support/CFG.h"
Reid Spencerd7d83db2007-02-05 23:42:17 +000026#include "llvm/Support/Compiler.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/Support/GraphWriter.h"
Chris Lattnerb06f6772005-08-03 17:55:05 +000028#include "llvm/Config/config.h"
Bill Wendling6f81b512006-11-28 22:46:12 +000029#include <iosfwd>
Chris Lattner002362c2003-10-22 16:03:49 +000030#include <sstream>
31#include <fstream>
Chris Lattner1ca2a582003-12-11 21:48:18 +000032using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000033
Chris Lattner002362c2003-10-22 16:03:49 +000034/// 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///
38static bool CFGOnly = false;
39
Chris Lattner1ca2a582003-12-11 21:48:18 +000040namespace llvm {
Chris Lattner002362c2003-10-22 16:03:49 +000041template<>
42struct 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 Lattnerd073ea02003-10-22 16:30:58 +000049 if (CFGOnly && !Node->getName().empty())
50 return Node->getName() + ":";
Chris Lattner002362c2003-10-22 16:03:49 +000051
52 std::ostringstream Out;
Chris Lattnera75b3ef2003-10-22 16:22:42 +000053 if (CFGOnly) {
Chris Lattner3749c9c2006-12-06 06:16:21 +000054 WriteAsOperand(Out, Node, false);
Chris Lattnera75b3ef2003-10-22 16:22:42 +000055 return Out.str();
56 }
57
Chris Lattnerd073ea02003-10-22 16:30:58 +000058 if (Node->getName().empty()) {
Chris Lattner3749c9c2006-12-06 06:16:21 +000059 WriteAsOperand(Out, Node, false);
Chris Lattnerd073ea02003-10-22 16:30:58 +000060 Out << ":";
61 }
62
Chris Lattner002362c2003-10-22 16:03:49 +000063 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 Lattner002362c2003-10-22 16:03:49 +000081 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 Lattner1ca2a582003-12-11 21:48:18 +000090}
Chris Lattner002362c2003-10-22 16:03:49 +000091
92namespace {
Dan Gohmana196b992007-05-14 14:25:08 +000093 struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
94 static char ID; // Pass identifcation, replacement for typeid
95 CFGViewer() : FunctionPass((intptr_t)&ID) {}
96
Devang Patel1cee94f2008-03-18 00:39:19 +000097 /// isAnalysis - Return true if this pass is implementing an analysis pass.
98 virtual bool isAnalysis() const { return true; }
99
Dan Gohmana196b992007-05-14 14:25:08 +0000100 virtual bool runOnFunction(Function &F) {
101 F.viewCFG();
102 return false;
103 }
104
105 void print(std::ostream &OS, const Module* = 0) const {}
106
107 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108 AU.setPreservesAll();
109 }
110 };
111
112 char CFGViewer::ID = 0;
113 RegisterPass<CFGViewer> V0("view-cfg",
114 "View CFG of function");
115
116 struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
117 static char ID; // Pass identifcation, replacement for typeid
118 CFGOnlyViewer() : FunctionPass((intptr_t)&ID) {}
119
Devang Patel1cee94f2008-03-18 00:39:19 +0000120 /// isAnalysis - Return true if this pass is implementing an analysis pass.
121 virtual bool isAnalysis() const { return true; }
122
Dan Gohmana196b992007-05-14 14:25:08 +0000123 virtual bool runOnFunction(Function &F) {
124 CFGOnly = true;
125 F.viewCFG();
126 CFGOnly = false;
127 return false;
128 }
129
130 void print(std::ostream &OS, const Module* = 0) const {}
131
132 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
133 AU.setPreservesAll();
134 }
135 };
136
137 char CFGOnlyViewer::ID = 0;
138 RegisterPass<CFGOnlyViewer> V1("view-cfg-only",
139 "View CFG of function (with no function bodies)");
140
Reid Spencerd7d83db2007-02-05 23:42:17 +0000141 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000142 static char ID; // Pass identification, replacement for typeid
Devang Patel794fd752007-05-01 21:15:47 +0000143 CFGPrinter() : FunctionPass((intptr_t)&ID) {}
Dan Gohmana6900c72007-07-02 14:53:37 +0000144 explicit CFGPrinter(intptr_t pid) : FunctionPass(pid) {}
Devang Patel794fd752007-05-01 21:15:47 +0000145
Devang Patel1cee94f2008-03-18 00:39:19 +0000146 /// isAnalysis - Return true if this pass is implementing an analysis pass.
147 virtual bool isAnalysis() const { return true; }
148
Chris Lattner002362c2003-10-22 16:03:49 +0000149 virtual bool runOnFunction(Function &F) {
150 std::string Filename = "cfg." + F.getName() + ".dot";
Bill Wendlinge8156192006-12-07 01:30:32 +0000151 cerr << "Writing '" << Filename << "'...";
Chris Lattner002362c2003-10-22 16:03:49 +0000152 std::ofstream File(Filename.c_str());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000153
Chris Lattner002362c2003-10-22 16:03:49 +0000154 if (File.good())
155 WriteGraph(File, (const Function*)&F);
156 else
Bill Wendlinge8156192006-12-07 01:30:32 +0000157 cerr << " error opening file for writing!";
158 cerr << "\n";
Chris Lattner002362c2003-10-22 16:03:49 +0000159 return false;
160 }
161
Reid Spencerce9653c2004-12-07 04:03:45 +0000162 void print(std::ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000163
Chris Lattner002362c2003-10-22 16:03:49 +0000164 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
165 AU.setPreservesAll();
166 }
167 };
168
Devang Patel19974732007-05-03 01:11:54 +0000169 char CFGPrinter::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +0000170 RegisterPass<CFGPrinter> P1("print-cfg",
171 "Print CFG of function to 'dot' file");
Chris Lattner1ca2a582003-12-11 21:48:18 +0000172
Reid Spencerd7d83db2007-02-05 23:42:17 +0000173 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public CFGPrinter {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000174 static char ID; // Pass identification, replacement for typeid
Devang Patela301a162007-06-05 20:24:36 +0000175 CFGOnlyPrinter() : CFGPrinter((intptr_t)&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +0000176
177 /// isAnalysis - Return true if this pass is implementing an analysis pass.
178 virtual bool isAnalysis() const { return true; }
179
Chris Lattner1ca2a582003-12-11 21:48:18 +0000180 virtual bool runOnFunction(Function &F) {
181 bool OldCFGOnly = CFGOnly;
182 CFGOnly = true;
183 CFGPrinter::runOnFunction(F);
184 CFGOnly = OldCFGOnly;
185 return false;
186 }
Reid Spencerce9653c2004-12-07 04:03:45 +0000187 void print(std::ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000188
Chris Lattner1ca2a582003-12-11 21:48:18 +0000189 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
190 AU.setPreservesAll();
191 }
192 };
193
Devang Patel19974732007-05-03 01:11:54 +0000194 char CFGOnlyPrinter::ID = 0;
Chris Lattner5d8925c2006-08-27 22:30:17 +0000195 RegisterPass<CFGOnlyPrinter>
Chris Lattner1ca2a582003-12-11 21:48:18 +0000196 P2("print-cfg-only",
197 "Print CFG of function to 'dot' file (with no function bodies)");
198}
Chris Lattner002362c2003-10-22 16:03:49 +0000199
Chris Lattner002362c2003-10-22 16:03:49 +0000200/// viewCFG - This function is meant for use from the debugger. You can just
201/// say 'call F->viewCFG()' and a ghostview window should pop up from the
202/// program, displaying the CFG of the current function. This depends on there
203/// being a 'dot' and 'gv' program in your path.
204///
205void Function::viewCFG() const {
Reid Spencer9d5b5322006-06-27 16:49:46 +0000206 ViewGraph(this, "cfg" + getName());
Chris Lattner002362c2003-10-22 16:03:49 +0000207}
208
209/// viewCFGOnly - This function is meant for use from the debugger. It works
210/// just like viewCFG, but it does not include the contents of basic blocks
211/// into the nodes, just the label. If you are only interested in the CFG t
212/// his can make the graph smaller.
213///
214void Function::viewCFGOnly() const {
215 CFGOnly = true;
216 viewCFG();
217 CFGOnly = false;
218}
Brian Gaekec6e2d8a2004-04-26 16:27:08 +0000219
220FunctionPass *llvm::createCFGPrinterPass () {
221 return new CFGPrinter();
222}
223
224FunctionPass *llvm::createCFGOnlyPrinterPass () {
225 return new CFGOnlyPrinter();
226}
227