blob: 6fed4005d19350ab8c942257a838258f74f9627c [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 Lattner1ca2a582003-12-11 21:48:18 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner1ca2a582003-12-11 21:48:18 +000030namespace llvm {
Chris Lattner002362c2003-10-22 16:03:49 +000031template<>
32struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
33 static std::string getGraphName(const Function *F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +000034 return "CFG for '" + F->getNameStr() + "' function";
Chris Lattner002362c2003-10-22 16:03:49 +000035 }
36
37 static std::string getNodeLabel(const BasicBlock *Node,
Owen Anderson8cbc94a2009-06-24 17:37:09 +000038 const Function *Graph,
39 bool ShortNames) {
40 if (ShortNames && !Node->getName().empty())
Daniel Dunbarf6ccee52009-07-24 08:24:36 +000041 return Node->getNameStr() + ":";
Chris Lattner002362c2003-10-22 16:03:49 +000042
Chris Lattnerbdff5482009-08-23 04:37:46 +000043 std::string Str;
44 raw_string_ostream OS(Str);
45
Owen Anderson8cbc94a2009-06-24 17:37:09 +000046 if (ShortNames) {
Chris Lattnerbdff5482009-08-23 04:37:46 +000047 WriteAsOperand(OS, Node, false);
48 return OS.str();
Chris Lattnera75b3ef2003-10-22 16:22:42 +000049 }
50
Chris Lattnerd073ea02003-10-22 16:30:58 +000051 if (Node->getName().empty()) {
Chris Lattnerbdff5482009-08-23 04:37:46 +000052 WriteAsOperand(OS, Node, false);
53 OS << ":";
Chris Lattnerd073ea02003-10-22 16:30:58 +000054 }
Chris Lattnerbdff5482009-08-23 04:37:46 +000055
56 OS << *Node;
57 std::string OutStr = OS.str();
Chris Lattner002362c2003-10-22 16:03:49 +000058 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
59
60 // Process string output to make it nicer...
61 for (unsigned i = 0; i != OutStr.length(); ++i)
62 if (OutStr[i] == '\n') { // Left justify
63 OutStr[i] = '\\';
64 OutStr.insert(OutStr.begin()+i+1, 'l');
65 } else if (OutStr[i] == ';') { // Delete comments!
66 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
67 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
68 --i;
69 }
70
71 return OutStr;
72 }
73
Chris Lattner002362c2003-10-22 16:03:49 +000074 static std::string getEdgeSourceLabel(const BasicBlock *Node,
75 succ_const_iterator I) {
76 // Label source of conditional branches with "T" or "F"
77 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
78 if (BI->isConditional())
79 return (I == succ_begin(Node)) ? "T" : "F";
80 return "";
81 }
82};
Chris Lattner1ca2a582003-12-11 21:48:18 +000083}
Chris Lattner002362c2003-10-22 16:03:49 +000084
85namespace {
Dan Gohmana196b992007-05-14 14:25:08 +000086 struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
87 static char ID; // Pass identifcation, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +000088 CFGViewer() : FunctionPass(&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +000089
Dan Gohmana196b992007-05-14 14:25:08 +000090 virtual bool runOnFunction(Function &F) {
91 F.viewCFG();
92 return false;
93 }
94
Chris Lattner45cfe542009-08-23 06:03:38 +000095 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmana196b992007-05-14 14:25:08 +000096
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
98 AU.setPreservesAll();
99 }
100 };
Dan Gohman844731a2008-05-13 00:00:25 +0000101}
Dan Gohmana196b992007-05-14 14:25:08 +0000102
Dan Gohman844731a2008-05-13 00:00:25 +0000103char CFGViewer::ID = 0;
104static RegisterPass<CFGViewer>
105V0("view-cfg", "View CFG of function", false, true);
Dan Gohmana196b992007-05-14 14:25:08 +0000106
Dan Gohman844731a2008-05-13 00:00:25 +0000107namespace {
Dan Gohmana196b992007-05-14 14:25:08 +0000108 struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
109 static char ID; // Pass identifcation, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000110 CFGOnlyViewer() : FunctionPass(&ID) {}
Devang Patel1cee94f2008-03-18 00:39:19 +0000111
Dan Gohmana196b992007-05-14 14:25:08 +0000112 virtual bool runOnFunction(Function &F) {
Duncan Sandsad389362009-09-19 11:25:44 +0000113 F.viewCFGOnly();
Dan Gohmana196b992007-05-14 14:25:08 +0000114 return false;
115 }
116
Chris Lattner45cfe542009-08-23 06:03:38 +0000117 void print(raw_ostream &OS, const Module* = 0) const {}
Dan Gohmana196b992007-05-14 14:25:08 +0000118
119 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
120 AU.setPreservesAll();
121 }
122 };
Dan Gohman844731a2008-05-13 00:00:25 +0000123}
Dan Gohmana196b992007-05-14 14:25:08 +0000124
Dan Gohman844731a2008-05-13 00:00:25 +0000125char CFGOnlyViewer::ID = 0;
126static RegisterPass<CFGOnlyViewer>
127V1("view-cfg-only",
128 "View CFG of function (with no function bodies)", false, true);
Dan Gohmana196b992007-05-14 14:25:08 +0000129
Dan Gohman844731a2008-05-13 00:00:25 +0000130namespace {
Reid Spencerd7d83db2007-02-05 23:42:17 +0000131 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000132 static char ID; // Pass identification, replacement for typeid
Dan Gohmanae73dc12008-09-04 17:05:41 +0000133 CFGPrinter() : FunctionPass(&ID) {}
134 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
Devang Patel1cee94f2008-03-18 00:39:19 +0000135
Chris Lattner002362c2003-10-22 16:03:49 +0000136 virtual bool runOnFunction(Function &F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000137 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Chris Lattner103289e2009-08-23 07:19:13 +0000138 errs() << "Writing '" << Filename << "'...";
139
140 std::string ErrorInfo;
Dan Gohmanbaa26392009-08-25 15:34:52 +0000141 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000142
Chris Lattner103289e2009-08-23 07:19:13 +0000143 if (ErrorInfo.empty())
Chris Lattner002362c2003-10-22 16:03:49 +0000144 WriteGraph(File, (const Function*)&F);
145 else
Chris Lattner103289e2009-08-23 07:19:13 +0000146 errs() << " error opening file for writing!";
147 errs() << "\n";
Chris Lattner002362c2003-10-22 16:03:49 +0000148 return false;
149 }
150
Chris Lattner45cfe542009-08-23 06:03:38 +0000151 void print(raw_ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000152
Chris Lattner002362c2003-10-22 16:03:49 +0000153 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
154 AU.setPreservesAll();
155 }
156 };
Dan Gohman844731a2008-05-13 00:00:25 +0000157}
Chris Lattner002362c2003-10-22 16:03:49 +0000158
Dan Gohman844731a2008-05-13 00:00:25 +0000159char CFGPrinter::ID = 0;
160static RegisterPass<CFGPrinter>
Duncan Sands3ee8fc92008-09-23 12:47:39 +0000161P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
Chris Lattner1ca2a582003-12-11 21:48:18 +0000162
Dan Gohman844731a2008-05-13 00:00:25 +0000163namespace {
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000164 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
Nick Lewyckyecd94c82007-05-06 13:37:16 +0000165 static char ID; // Pass identification, replacement for typeid
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000166 CFGOnlyPrinter() : FunctionPass(&ID) {}
167 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
Chris Lattner1ca2a582003-12-11 21:48:18 +0000168 virtual bool runOnFunction(Function &F) {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000169 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Chris Lattner103289e2009-08-23 07:19:13 +0000170 errs() << "Writing '" << Filename << "'...";
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000171
Chris Lattner103289e2009-08-23 07:19:13 +0000172 std::string ErrorInfo;
Dan Gohmanbaa26392009-08-25 15:34:52 +0000173 raw_fd_ostream File(Filename.c_str(), ErrorInfo);
Chris Lattner103289e2009-08-23 07:19:13 +0000174
175 if (ErrorInfo.empty())
Owen Anderson8cbc94a2009-06-24 17:37:09 +0000176 WriteGraph(File, (const Function*)&F, true);
177 else
Chris Lattner103289e2009-08-23 07:19:13 +0000178 errs() << " error opening file for writing!";
179 errs() << "\n";
Chris Lattner1ca2a582003-12-11 21:48:18 +0000180 return false;
181 }
Chris Lattner45cfe542009-08-23 06:03:38 +0000182 void print(raw_ostream &OS, const Module* = 0) const {}
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000183
Chris Lattner1ca2a582003-12-11 21:48:18 +0000184 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
185 AU.setPreservesAll();
186 }
187 };
Chris Lattner1ca2a582003-12-11 21:48:18 +0000188}
Chris Lattner002362c2003-10-22 16:03:49 +0000189
Dan Gohman844731a2008-05-13 00:00:25 +0000190char CFGOnlyPrinter::ID = 0;
191static RegisterPass<CFGOnlyPrinter>
Duncan Sands3ee8fc92008-09-23 12:47:39 +0000192P2("dot-cfg-only",
Dan Gohman844731a2008-05-13 00:00:25 +0000193 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
194
Chris Lattner002362c2003-10-22 16:03:49 +0000195/// viewCFG - This function is meant for use from the debugger. You can just
196/// say 'call F->viewCFG()' and a ghostview window should pop up from the
197/// program, displaying the CFG of the current function. This depends on there
198/// being a 'dot' and 'gv' program in your path.
199///
200void Function::viewCFG() const {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000201 ViewGraph(this, "cfg" + getNameStr());
Chris Lattner002362c2003-10-22 16:03:49 +0000202}
203
204/// viewCFGOnly - This function is meant for use from the debugger. It works
205/// just like viewCFG, but it does not include the contents of basic blocks
206/// into the nodes, just the label. If you are only interested in the CFG t
207/// his can make the graph smaller.
208///
209void Function::viewCFGOnly() const {
Daniel Dunbarf6ccee52009-07-24 08:24:36 +0000210 ViewGraph(this, "cfg" + getNameStr(), true);
Chris Lattner002362c2003-10-22 16:03:49 +0000211}
Brian Gaekec6e2d8a2004-04-26 16:27:08 +0000212
213FunctionPass *llvm::createCFGPrinterPass () {
214 return new CFGPrinter();
215}
216
217FunctionPass *llvm::createCFGOnlyPrinterPass () {
218 return new CFGOnlyPrinter();
219}
220