blob: 7f83a98b4328ebaba69fb00cd1cb563a7476f007 [file] [log] [blame]
Chris Lattnera93d11b2003-10-22 16:03:49 +00001//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
Misha Brukman01808ca2005-04-21 21:13:18 +00002//
Chris Lattnera93d11b2003-10-22 16:03:49 +00003// 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 Brukman01808ca2005-04-21 21:13:18 +00007//
Chris Lattnera93d11b2003-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 Lattnera93d11b2003-10-22 16:03:49 +000020#include "llvm/Function.h"
Misha Brukman63b38bd2004-07-29 17:30:56 +000021#include "llvm/Instructions.h"
Misha Brukmana70ae902004-04-29 04:04:47 +000022#include "llvm/Pass.h"
Brian Gaeke104341f2004-04-26 16:27:08 +000023#include "llvm/Analysis/CFGPrinter.h"
Misha Brukmana70ae902004-04-29 04:04:47 +000024#include "llvm/Assembly/Writer.h"
Chris Lattnera93d11b2003-10-22 16:03:49 +000025#include "llvm/Support/CFG.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000026#include "llvm/Support/GraphWriter.h"
Chris Lattner590642e2005-08-03 17:55:05 +000027#include "llvm/Config/config.h"
Chris Lattnera93d11b2003-10-22 16:03:49 +000028#include <sstream>
29#include <fstream>
Chris Lattner62aff842003-12-11 21:48:18 +000030using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000031
Chris Lattnera93d11b2003-10-22 16:03:49 +000032/// CFGOnly flag - This is used to control whether or not the CFG graph printer
33/// prints out the contents of basic blocks or not. This is acceptable because
34/// this code is only really used for debugging purposes.
35///
36static bool CFGOnly = false;
37
Chris Lattner62aff842003-12-11 21:48:18 +000038namespace llvm {
Chris Lattnera93d11b2003-10-22 16:03:49 +000039template<>
40struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
41 static std::string getGraphName(const Function *F) {
42 return "CFG for '" + F->getName() + "' function";
43 }
44
45 static std::string getNodeLabel(const BasicBlock *Node,
46 const Function *Graph) {
Chris Lattneraa81dce2003-10-22 16:30:58 +000047 if (CFGOnly && !Node->getName().empty())
48 return Node->getName() + ":";
Chris Lattnera93d11b2003-10-22 16:03:49 +000049
50 std::ostringstream Out;
Chris Lattner9d63afd2003-10-22 16:22:42 +000051 if (CFGOnly) {
52 WriteAsOperand(Out, Node, false, true);
53 return Out.str();
54 }
55
Chris Lattneraa81dce2003-10-22 16:30:58 +000056 if (Node->getName().empty()) {
57 WriteAsOperand(Out, Node, false, true);
58 Out << ":";
59 }
60
Chris Lattnera93d11b2003-10-22 16:03:49 +000061 Out << *Node;
62 std::string OutStr = Out.str();
63 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
64
65 // Process string output to make it nicer...
66 for (unsigned i = 0; i != OutStr.length(); ++i)
67 if (OutStr[i] == '\n') { // Left justify
68 OutStr[i] = '\\';
69 OutStr.insert(OutStr.begin()+i+1, 'l');
70 } else if (OutStr[i] == ';') { // Delete comments!
71 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
72 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
73 --i;
74 }
75
76 return OutStr;
77 }
78
Chris Lattnera93d11b2003-10-22 16:03:49 +000079 static std::string getEdgeSourceLabel(const BasicBlock *Node,
80 succ_const_iterator I) {
81 // Label source of conditional branches with "T" or "F"
82 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
83 if (BI->isConditional())
84 return (I == succ_begin(Node)) ? "T" : "F";
85 return "";
86 }
87};
Chris Lattner62aff842003-12-11 21:48:18 +000088}
Chris Lattnera93d11b2003-10-22 16:03:49 +000089
90namespace {
91 struct CFGPrinter : public FunctionPass {
92 virtual bool runOnFunction(Function &F) {
93 std::string Filename = "cfg." + F.getName() + ".dot";
94 std::cerr << "Writing '" << Filename << "'...";
95 std::ofstream File(Filename.c_str());
Misha Brukman01808ca2005-04-21 21:13:18 +000096
Chris Lattnera93d11b2003-10-22 16:03:49 +000097 if (File.good())
98 WriteGraph(File, (const Function*)&F);
99 else
100 std::cerr << " error opening file for writing!";
101 std::cerr << "\n";
102 return false;
103 }
104
Reid Spencer90839362004-12-07 04:03:45 +0000105 void print(std::ostream &OS, const Module* = 0) const {}
Misha Brukman01808ca2005-04-21 21:13:18 +0000106
Chris Lattnera93d11b2003-10-22 16:03:49 +0000107 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
108 AU.setPreservesAll();
109 }
110 };
111
112 RegisterAnalysis<CFGPrinter> P1("print-cfg",
113 "Print CFG of function to 'dot' file");
Chris Lattner62aff842003-12-11 21:48:18 +0000114
115 struct CFGOnlyPrinter : public CFGPrinter {
116 virtual bool runOnFunction(Function &F) {
117 bool OldCFGOnly = CFGOnly;
118 CFGOnly = true;
119 CFGPrinter::runOnFunction(F);
120 CFGOnly = OldCFGOnly;
121 return false;
122 }
Reid Spencer90839362004-12-07 04:03:45 +0000123 void print(std::ostream &OS, const Module* = 0) const {}
Misha Brukman01808ca2005-04-21 21:13:18 +0000124
Chris Lattner62aff842003-12-11 21:48:18 +0000125 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
126 AU.setPreservesAll();
127 }
128 };
129
130 RegisterAnalysis<CFGOnlyPrinter>
131 P2("print-cfg-only",
132 "Print CFG of function to 'dot' file (with no function bodies)");
133}
Chris Lattnera93d11b2003-10-22 16:03:49 +0000134
Chris Lattnera93d11b2003-10-22 16:03:49 +0000135/// viewCFG - This function is meant for use from the debugger. You can just
136/// say 'call F->viewCFG()' and a ghostview window should pop up from the
137/// program, displaying the CFG of the current function. This depends on there
138/// being a 'dot' and 'gv' program in your path.
139///
140void Function::viewCFG() const {
Chris Lattner590642e2005-08-03 17:55:05 +0000141#ifndef NDEBUG
Chris Lattnera93d11b2003-10-22 16:03:49 +0000142 std::string Filename = "/tmp/cfg." + getName() + ".dot";
143 std::cerr << "Writing '" << Filename << "'... ";
144 std::ofstream F(Filename.c_str());
Misha Brukman01808ca2005-04-21 21:13:18 +0000145
Chris Lattnera93d11b2003-10-22 16:03:49 +0000146 if (!F.good()) {
147 std::cerr << " error opening file for writing!\n";
148 return;
149 }
150
151 WriteGraph(F, this);
152 F.close();
153 std::cerr << "\n";
154
Chris Lattner590642e2005-08-03 17:55:05 +0000155#ifdef HAVE_GRAPHVIZ
156 std::cerr << "Running 'Graphviz' program... " << std::flush;
157 if (system((LLVM_PATH_GRAPHVIZ " " + Filename).c_str())) {
158 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
159 } else {
160 system(("rm " + Filename).c_str());
161 return;
162 }
163#endif
164
165#ifdef HAVE_GV
Chris Lattnera93d11b2003-10-22 16:03:49 +0000166 std::cerr << "Running 'dot' program... " << std::flush;
Brian Gaekee330adf2004-05-05 06:10:06 +0000167 if (system(("dot -Tps -Nfontname=Courier -Gsize=7.5,10 " + Filename
168 + " > /tmp/cfg.tempgraph.ps").c_str())) {
Chris Lattnera93d11b2003-10-22 16:03:49 +0000169 std::cerr << "Error running dot: 'dot' not in path?\n";
170 } else {
171 std::cerr << "\n";
172 system("gv /tmp/cfg.tempgraph.ps");
173 }
174 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
Chris Lattner590642e2005-08-03 17:55:05 +0000175 return;
176#endif
177#endif
178 std::cerr << "Function::viewCFG is only available in debug builds on "
179 << "systems with Graphviz or gv!\n";
180
181 system(("rm " + Filename).c_str());
Chris Lattnera93d11b2003-10-22 16:03:49 +0000182}
183
184/// viewCFGOnly - This function is meant for use from the debugger. It works
185/// just like viewCFG, but it does not include the contents of basic blocks
186/// into the nodes, just the label. If you are only interested in the CFG t
187/// his can make the graph smaller.
188///
189void Function::viewCFGOnly() const {
190 CFGOnly = true;
191 viewCFG();
192 CFGOnly = false;
193}
Brian Gaeke104341f2004-04-26 16:27:08 +0000194
195FunctionPass *llvm::createCFGPrinterPass () {
196 return new CFGPrinter();
197}
198
199FunctionPass *llvm::createCFGOnlyPrinterPass () {
200 return new CFGOnlyPrinter();
201}
202