blob: 46d3930bd6764baccce2d3c612b121bca012c5a7 [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//
Chris Lattnerf3ebc3f2007-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 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 Spencerf75727a2007-02-05 23:42:17 +000026#include "llvm/Support/Compiler.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000027#include "llvm/Support/GraphWriter.h"
Chris Lattner590642e2005-08-03 17:55:05 +000028#include "llvm/Config/config.h"
Bill Wendling597d4512006-11-28 22:46:12 +000029#include <iosfwd>
Chris Lattnera93d11b2003-10-22 16:03:49 +000030#include <sstream>
31#include <fstream>
Chris Lattner62aff842003-12-11 21:48:18 +000032using namespace llvm;
Brian Gaeke960707c2003-11-11 22:41:34 +000033
Chris Lattnera93d11b2003-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 Lattner62aff842003-12-11 21:48:18 +000040namespace llvm {
Chris Lattnera93d11b2003-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 Lattneraa81dce2003-10-22 16:30:58 +000049 if (CFGOnly && !Node->getName().empty())
50 return Node->getName() + ":";
Chris Lattnera93d11b2003-10-22 16:03:49 +000051
52 std::ostringstream Out;
Chris Lattner9d63afd2003-10-22 16:22:42 +000053 if (CFGOnly) {
Chris Lattneredcc8c22006-12-06 06:16:21 +000054 WriteAsOperand(Out, Node, false);
Chris Lattner9d63afd2003-10-22 16:22:42 +000055 return Out.str();
56 }
57
Chris Lattneraa81dce2003-10-22 16:30:58 +000058 if (Node->getName().empty()) {
Chris Lattneredcc8c22006-12-06 06:16:21 +000059 WriteAsOperand(Out, Node, false);
Chris Lattneraa81dce2003-10-22 16:30:58 +000060 Out << ":";
61 }
62
Chris Lattnera93d11b2003-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 Lattnera93d11b2003-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 Lattner62aff842003-12-11 21:48:18 +000090}
Chris Lattnera93d11b2003-10-22 16:03:49 +000091
92namespace {
Dan Gohman90d97ac2007-05-14 14:25:08 +000093 struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
94 static char ID; // Pass identifcation, replacement for typeid
Dan Gohmana79db302008-09-04 17:05:41 +000095 CFGViewer() : FunctionPass(&ID) {}
Devang Patel864970e2008-03-18 00:39:19 +000096
Dan Gohman90d97ac2007-05-14 14:25:08 +000097 virtual bool runOnFunction(Function &F) {
98 F.viewCFG();
99 return false;
100 }
101
102 void print(std::ostream &OS, const Module* = 0) const {}
103
104 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
105 AU.setPreservesAll();
106 }
107 };
Dan Gohmand78c4002008-05-13 00:00:25 +0000108}
Dan Gohman90d97ac2007-05-14 14:25:08 +0000109
Dan Gohmand78c4002008-05-13 00:00:25 +0000110char CFGViewer::ID = 0;
111static RegisterPass<CFGViewer>
112V0("view-cfg", "View CFG of function", false, true);
Dan Gohman90d97ac2007-05-14 14:25:08 +0000113
Dan Gohmand78c4002008-05-13 00:00:25 +0000114namespace {
Dan Gohman90d97ac2007-05-14 14:25:08 +0000115 struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
116 static char ID; // Pass identifcation, replacement for typeid
Dan Gohmana79db302008-09-04 17:05:41 +0000117 CFGOnlyViewer() : FunctionPass(&ID) {}
Devang Patel864970e2008-03-18 00:39:19 +0000118
Dan Gohman90d97ac2007-05-14 14:25:08 +0000119 virtual bool runOnFunction(Function &F) {
120 CFGOnly = true;
121 F.viewCFG();
122 CFGOnly = false;
123 return false;
124 }
125
126 void print(std::ostream &OS, const Module* = 0) const {}
127
128 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
129 AU.setPreservesAll();
130 }
131 };
Dan Gohmand78c4002008-05-13 00:00:25 +0000132}
Dan Gohman90d97ac2007-05-14 14:25:08 +0000133
Dan Gohmand78c4002008-05-13 00:00:25 +0000134char CFGOnlyViewer::ID = 0;
135static RegisterPass<CFGOnlyViewer>
136V1("view-cfg-only",
137 "View CFG of function (with no function bodies)", false, true);
Dan Gohman90d97ac2007-05-14 14:25:08 +0000138
Dan Gohmand78c4002008-05-13 00:00:25 +0000139namespace {
Reid Spencerf75727a2007-02-05 23:42:17 +0000140 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000141 static char ID; // Pass identification, replacement for typeid
Dan Gohmana79db302008-09-04 17:05:41 +0000142 CFGPrinter() : FunctionPass(&ID) {}
143 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
Devang Patel864970e2008-03-18 00:39:19 +0000144
Chris Lattnera93d11b2003-10-22 16:03:49 +0000145 virtual bool runOnFunction(Function &F) {
146 std::string Filename = "cfg." + F.getName() + ".dot";
Bill Wendlingf3baad32006-12-07 01:30:32 +0000147 cerr << "Writing '" << Filename << "'...";
Chris Lattnera93d11b2003-10-22 16:03:49 +0000148 std::ofstream File(Filename.c_str());
Misha Brukman01808ca2005-04-21 21:13:18 +0000149
Chris Lattnera93d11b2003-10-22 16:03:49 +0000150 if (File.good())
151 WriteGraph(File, (const Function*)&F);
152 else
Bill Wendlingf3baad32006-12-07 01:30:32 +0000153 cerr << " error opening file for writing!";
154 cerr << "\n";
Chris Lattnera93d11b2003-10-22 16:03:49 +0000155 return false;
156 }
157
Reid Spencer90839362004-12-07 04:03:45 +0000158 void print(std::ostream &OS, const Module* = 0) const {}
Misha Brukman01808ca2005-04-21 21:13:18 +0000159
Chris Lattnera93d11b2003-10-22 16:03:49 +0000160 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
161 AU.setPreservesAll();
162 }
163 };
Dan Gohmand78c4002008-05-13 00:00:25 +0000164}
Chris Lattnera93d11b2003-10-22 16:03:49 +0000165
Dan Gohmand78c4002008-05-13 00:00:25 +0000166char CFGPrinter::ID = 0;
167static RegisterPass<CFGPrinter>
168P1("print-cfg", "Print CFG of function to 'dot' file", false, true);
Chris Lattner62aff842003-12-11 21:48:18 +0000169
Dan Gohmand78c4002008-05-13 00:00:25 +0000170namespace {
Reid Spencerf75727a2007-02-05 23:42:17 +0000171 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public CFGPrinter {
Nick Lewyckye7da2d62007-05-06 13:37:16 +0000172 static char ID; // Pass identification, replacement for typeid
Dan Gohmana79db302008-09-04 17:05:41 +0000173 CFGOnlyPrinter() : CFGPrinter(&ID) {}
Chris Lattner62aff842003-12-11 21:48:18 +0000174 virtual bool runOnFunction(Function &F) {
175 bool OldCFGOnly = CFGOnly;
176 CFGOnly = true;
177 CFGPrinter::runOnFunction(F);
178 CFGOnly = OldCFGOnly;
179 return false;
180 }
Reid Spencer90839362004-12-07 04:03:45 +0000181 void print(std::ostream &OS, const Module* = 0) const {}
Misha Brukman01808ca2005-04-21 21:13:18 +0000182
Chris Lattner62aff842003-12-11 21:48:18 +0000183 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
184 AU.setPreservesAll();
185 }
186 };
Chris Lattner62aff842003-12-11 21:48:18 +0000187}
Chris Lattnera93d11b2003-10-22 16:03:49 +0000188
Dan Gohmand78c4002008-05-13 00:00:25 +0000189char CFGOnlyPrinter::ID = 0;
190static RegisterPass<CFGOnlyPrinter>
191P2("print-cfg-only",
192 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
193
Chris Lattnera93d11b2003-10-22 16:03:49 +0000194/// viewCFG - This function is meant for use from the debugger. You can just
195/// say 'call F->viewCFG()' and a ghostview window should pop up from the
196/// program, displaying the CFG of the current function. This depends on there
197/// being a 'dot' and 'gv' program in your path.
198///
199void Function::viewCFG() const {
Reid Spenceree7eaa22006-06-27 16:49:46 +0000200 ViewGraph(this, "cfg" + getName());
Chris Lattnera93d11b2003-10-22 16:03:49 +0000201}
202
203/// viewCFGOnly - This function is meant for use from the debugger. It works
204/// just like viewCFG, but it does not include the contents of basic blocks
205/// into the nodes, just the label. If you are only interested in the CFG t
206/// his can make the graph smaller.
207///
208void Function::viewCFGOnly() const {
209 CFGOnly = true;
210 viewCFG();
211 CFGOnly = false;
212}
Brian Gaeke104341f2004-04-26 16:27:08 +0000213
214FunctionPass *llvm::createCFGPrinterPass () {
215 return new CFGPrinter();
216}
217
218FunctionPass *llvm::createCFGOnlyPrinterPass () {
219 return new CFGOnlyPrinter();
220}
221