blob: f6ecfc596f55000eb069133e29701c03c6c7ace2 [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//
Duncan Sands3ee8fc92008-09-23 12:47:39 +000010// This file defines a '-dot-cfg' analysis pass, which emits the
Chris Lattner002362c2003-10-22 16:03:49 +000011// 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"
Chris Lattner1ca2a582003-12-11 21:48:18 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
Chris Lattner1ca2a582003-12-11 21:48:18 +000031namespace llvm {
Chris Lattner002362c2003-10-22 16:03:49 +000032template<>
33struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
34 static std::string getGraphName(const Function *F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +000035 return "CFG for '" + F->getNameStr() + "' function";
Chris Lattner002362c2003-10-22 16:03:49 +000036 }
37
38 static std::string getNodeLabel(const BasicBlock *Node,
Owen Anderson8cbc94a2009-06-24 17:37:09 +000039 const Function *Graph,
40 bool ShortNames) {
41 if (ShortNames && !Node->getName().empty())
Daniel Dunbarf6ccee52009-07-24 08:24:36 +000042 return Node->getNameStr() + ":";
Chris Lattner002362c2003-10-22 16:03:49 +000043
Chris Lattnerbdff5482009-08-23 04:37:46 +000044 std::string Str;
45 raw_string_ostream OS(Str);
46
Owen Anderson8cbc94a2009-06-24 17:37:09 +000047 if (ShortNames) {
Chris Lattnerbdff5482009-08-23 04:37:46 +000048 WriteAsOperand(OS, Node, false);
49 return OS.str();
Chris Lattnera75b3ef2003-10-22 16:22:42 +000050 }
51
Chris Lattnerd073ea02003-10-22 16:30:58 +000052 if (Node->getName().empty()) {
Chris Lattnerbdff5482009-08-23 04:37:46 +000053 WriteAsOperand(OS, Node, false);
54 OS << ":";
Chris Lattnerd073ea02003-10-22 16:30:58 +000055 }
Chris Lattnerbdff5482009-08-23 04:37:46 +000056
57 OS << *Node;
58 std::string OutStr = OS.str();
Chris Lattner002362c2003-10-22 16:03:49 +000059 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 Lattner002362c2003-10-22 16:03:49 +000075 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 Lattner1ca2a582003-12-11 21:48:18 +000084}
Chris Lattner002362c2003-10-22 16:03:49 +000085
86namespace {
Dan Gohmana196b992007-05-14 14:25:08 +000087 struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
88 static char ID; // Pass identifcation, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000089 CFGViewer() : FunctionPass(&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +000090
Dan Gohmana196b992007-05-14 14:25:08 +000091 virtual bool runOnFunction(Function &F) {
92 F.viewCFG();
93 return false;
94 }
95
Chris Lattner45cfe542009-08-23 06:03:38 +000096 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmana196b992007-05-14 14:25:08 +000097
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
99 AU.setPreservesAll();
100 }
101 };
Dan Gohman844731a2008-05-13 00:00:25 +0000102}
Dan Gohmana196b992007-05-14 14:25:08 +0000103
Dan Gohman844731a2008-05-13 00:00:25 +0000104char CFGViewer::ID = 0;
105static RegisterPass<CFGViewer>
106V0("view-cfg", "View CFG of function", false, true);
Dan Gohmana196b992007-05-14 14:25:08 +0000107
Dan Gohman844731a2008-05-13 00:00:25 +0000108namespace {
Dan Gohmana196b992007-05-14 14:25:08 +0000109 struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
110 static char ID; // Pass identifcation, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000111 CFGOnlyViewer() : FunctionPass(&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +0000112
Dan Gohmana196b992007-05-14 14:25:08 +0000113 virtual bool runOnFunction(Function &F) {
Dan Gohmana196b992007-05-14 14:25:08 +0000114 F.viewCFG();
Dan Gohmana196b992007-05-14 14:25:08 +0000115 return false;
116 }
117
Chris Lattner45cfe542009-08-23 06:03:38 +0000118 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmana196b992007-05-14 14:25:08 +0000119
120 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
121 AU.setPreservesAll();
122 }
123 };
Dan Gohman844731a2008-05-13 00:00:25 +0000124}
Dan Gohmana196b992007-05-14 14:25:08 +0000125
Dan Gohman844731a2008-05-13 00:00:25 +0000126char CFGOnlyViewer::ID = 0;
127static RegisterPass<CFGOnlyViewer>
128V1("view-cfg-only",
129 "View CFG of function (with no function bodies)", false, true);
Dan Gohmana196b992007-05-14 14:25:08 +0000130
Dan Gohman844731a2008-05-13 00:00:25 +0000131namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000132 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000133 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000134 CFGPrinter() : FunctionPass(&ID) {}
135 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
Devang Patel1cee94f2008-03-18 00:39:19 +0000136
Chris Lattner002362c2003-10-22 16:03:49 +0000137 virtual bool runOnFunction(Function &F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000138 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Bill Wendlinge8156192006-12-07 01:30:32 +0000139 cerr << "Writing '" << Filename << "'...";
Chris Lattner002362c2003-10-22 16:03:49 +0000140 std::ofstream File(Filename.c_str());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000141
Chris Lattner002362c2003-10-22 16:03:49 +0000142 if (File.good())
143 WriteGraph(File, (const Function*)&F);
144 else
Bill Wendlinge8156192006-12-07 01:30:32 +0000145 cerr << " error opening file for writing!";
146 cerr << "\n";
Chris Lattner002362c2003-10-22 16:03:49 +0000147 return false;
148 }
149
Chris Lattner45cfe542009-08-23 06:03:38 +0000150 void print(raw_ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000151
Chris Lattner002362c2003-10-22 16:03:49 +0000152 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
153 AU.setPreservesAll();
154 }
155 };
Dan Gohman844731a2008-05-13 00:00:25 +0000156}
Chris Lattner002362c2003-10-22 16:03:49 +0000157
Dan Gohman844731a2008-05-13 00:00:25 +0000158char CFGPrinter::ID = 0;
159static RegisterPass<CFGPrinter>
Duncan Sands3ee8fc92008-09-23 12:47:39 +0000160P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
Chris Lattner1ca2a582003-12-11 21:48:18 +0000161
Dan Gohman844731a2008-05-13 00:00:25 +0000162namespace {
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000163 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000164 static char ID; // Pass identification, replacement for typeid
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000165 CFGOnlyPrinter() : FunctionPass(&ID) {}
166 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
Chris Lattner1ca2a582003-12-11 21:48:18 +0000167 virtual bool runOnFunction(Function &F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000168 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000169 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 Lattner1ca2a582003-12-11 21:48:18 +0000177 return false;
178 }
Chris Lattner45cfe542009-08-23 06:03:38 +0000179 void print(raw_ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000180
Chris Lattner1ca2a582003-12-11 21:48:18 +0000181 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
182 AU.setPreservesAll();
183 }
184 };
Chris Lattner1ca2a582003-12-11 21:48:18 +0000185}
Chris Lattner002362c2003-10-22 16:03:49 +0000186
Dan Gohman844731a2008-05-13 00:00:25 +0000187char CFGOnlyPrinter::ID = 0;
188static RegisterPass<CFGOnlyPrinter>
Duncan Sands3ee8fc92008-09-23 12:47:39 +0000189P2("dot-cfg-only",
Dan Gohman844731a2008-05-13 00:00:25 +0000190 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
191
Chris Lattner002362c2003-10-22 16:03:49 +0000192/// 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///
197void Function::viewCFG() const {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000198 ViewGraph(this, "cfg" + getNameStr());
Chris Lattner002362c2003-10-22 16:03:49 +0000199}
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///
206void Function::viewCFGOnly() const {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000207 ViewGraph(this, "cfg" + getNameStr(), true);
Chris Lattner002362c2003-10-22 16:03:49 +0000208}
Brian Gaekec6e2d8a2004-04-26 16:27:08 +0000209
210FunctionPass *llvm::createCFGPrinterPass () {
211 return new CFGPrinter();
212}
213
214FunctionPass *llvm::createCFGOnlyPrinterPass () {
215 return new CFGOnlyPrinter();
216}
217