blob: 2364d984f9279b24a3f954fcea27dbed7183d6a5 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
8//===----------------------------------------------------------------------===//
9//
Duncan Sandsd10d6f72008-09-23 12:47:39 +000010// This file defines a '-dot-cfg' analysis pass, which emits the
Dan Gohmanf17a25c2007-07-18 16:29:46 +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
20#include "llvm/Function.h"
21#include "llvm/Instructions.h"
22#include "llvm/Pass.h"
23#include "llvm/Analysis/CFGPrinter.h"
24#include "llvm/Assembly/Writer.h"
25#include "llvm/Support/CFG.h"
26#include "llvm/Support/Compiler.h"
27#include "llvm/Support/GraphWriter.h"
28#include "llvm/Config/config.h"
29#include <iosfwd>
30#include <sstream>
31#include <fstream>
32using namespace llvm;
33
Dan Gohmanf17a25c2007-07-18 16:29:46 +000034namespace llvm {
35template<>
36struct DOTGraphTraits<const Function*> : public DefaultDOTGraphTraits {
37 static std::string getGraphName(const Function *F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +000038 return "CFG for '" + F->getNameStr() + "' function";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000039 }
40
41 static std::string getNodeLabel(const BasicBlock *Node,
Owen Andersonf4a15462009-06-24 17:37:09 +000042 const Function *Graph,
43 bool ShortNames) {
44 if (ShortNames && !Node->getName().empty())
Daniel Dunbar1e13b972009-07-24 08:24:36 +000045 return Node->getNameStr() + ":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000046
Chris Lattner8a6411c2009-08-23 04:37:46 +000047 std::string Str;
48 raw_string_ostream OS(Str);
49
Owen Andersonf4a15462009-06-24 17:37:09 +000050 if (ShortNames) {
Chris Lattner8a6411c2009-08-23 04:37:46 +000051 WriteAsOperand(OS, Node, false);
52 return OS.str();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000053 }
54
55 if (Node->getName().empty()) {
Chris Lattner8a6411c2009-08-23 04:37:46 +000056 WriteAsOperand(OS, Node, false);
57 OS << ":";
Dan Gohmanf17a25c2007-07-18 16:29:46 +000058 }
Chris Lattner8a6411c2009-08-23 04:37:46 +000059
60 OS << *Node;
61 std::string OutStr = OS.str();
Dan Gohmanf17a25c2007-07-18 16:29:46 +000062 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
63
64 // Process string output to make it nicer...
65 for (unsigned i = 0; i != OutStr.length(); ++i)
66 if (OutStr[i] == '\n') { // Left justify
67 OutStr[i] = '\\';
68 OutStr.insert(OutStr.begin()+i+1, 'l');
69 } else if (OutStr[i] == ';') { // Delete comments!
70 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
71 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
72 --i;
73 }
74
75 return OutStr;
76 }
77
78 static std::string getEdgeSourceLabel(const BasicBlock *Node,
79 succ_const_iterator I) {
80 // Label source of conditional branches with "T" or "F"
81 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
82 if (BI->isConditional())
83 return (I == succ_begin(Node)) ? "T" : "F";
84 return "";
85 }
86};
87}
88
89namespace {
90 struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
91 static char ID; // Pass identifcation, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000092 CFGViewer() : FunctionPass(&ID) {}
Devang Patel2b4fa682008-03-18 00:39:19 +000093
Dan Gohmanf17a25c2007-07-18 16:29:46 +000094 virtual bool runOnFunction(Function &F) {
95 F.viewCFG();
96 return false;
97 }
98
99 void print(std::ostream &OS, const Module* = 0) const {}
100
101 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
102 AU.setPreservesAll();
103 }
104 };
Dan Gohman089efff2008-05-13 00:00:25 +0000105}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000106
Dan Gohman089efff2008-05-13 00:00:25 +0000107char CFGViewer::ID = 0;
108static RegisterPass<CFGViewer>
109V0("view-cfg", "View CFG of function", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110
Dan Gohman089efff2008-05-13 00:00:25 +0000111namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000112 struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
113 static char ID; // Pass identifcation, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000114 CFGOnlyViewer() : FunctionPass(&ID) {}
Devang Patel2b4fa682008-03-18 00:39:19 +0000115
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 virtual bool runOnFunction(Function &F) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000117 F.viewCFG();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000118 return false;
119 }
120
121 void print(std::ostream &OS, const Module* = 0) const {}
122
123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.setPreservesAll();
125 }
126 };
Dan Gohman089efff2008-05-13 00:00:25 +0000127}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000128
Dan Gohman089efff2008-05-13 00:00:25 +0000129char CFGOnlyViewer::ID = 0;
130static RegisterPass<CFGOnlyViewer>
131V1("view-cfg-only",
132 "View CFG of function (with no function bodies)", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133
Dan Gohman089efff2008-05-13 00:00:25 +0000134namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000135 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
136 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000137 CFGPrinter() : FunctionPass(&ID) {}
138 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
Devang Patel2b4fa682008-03-18 00:39:19 +0000139
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000140 virtual bool runOnFunction(Function &F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000141 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000142 cerr << "Writing '" << Filename << "'...";
143 std::ofstream File(Filename.c_str());
144
145 if (File.good())
146 WriteGraph(File, (const Function*)&F);
147 else
148 cerr << " error opening file for writing!";
149 cerr << "\n";
150 return false;
151 }
152
153 void print(std::ostream &OS, const Module* = 0) const {}
154
155 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
156 AU.setPreservesAll();
157 }
158 };
Dan Gohman089efff2008-05-13 00:00:25 +0000159}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000160
Dan Gohman089efff2008-05-13 00:00:25 +0000161char CFGPrinter::ID = 0;
162static RegisterPass<CFGPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +0000163P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000164
Dan Gohman089efff2008-05-13 00:00:25 +0000165namespace {
Owen Andersonf4a15462009-06-24 17:37:09 +0000166 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000167 static char ID; // Pass identification, replacement for typeid
Owen Andersonf4a15462009-06-24 17:37:09 +0000168 CFGOnlyPrinter() : FunctionPass(&ID) {}
169 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000170 virtual bool runOnFunction(Function &F) {
Daniel Dunbar1e13b972009-07-24 08:24:36 +0000171 std::string Filename = "cfg." + F.getNameStr() + ".dot";
Owen Andersonf4a15462009-06-24 17:37:09 +0000172 cerr << "Writing '" << Filename << "'...";
173 std::ofstream File(Filename.c_str());
174
175 if (File.good())
176 WriteGraph(File, (const Function*)&F, true);
177 else
178 cerr << " error opening file for writing!";
179 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000180 return false;
181 }
182 void print(std::ostream &OS, const Module* = 0) const {}
183
184 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
185 AU.setPreservesAll();
186 }
187 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000188}
189
Dan Gohman089efff2008-05-13 00:00:25 +0000190char CFGOnlyPrinter::ID = 0;
191static RegisterPass<CFGOnlyPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +0000192P2("dot-cfg-only",
Dan Gohman089efff2008-05-13 00:00:25 +0000193 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
194
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Dunbar1e13b972009-07-24 08:24:36 +0000201 ViewGraph(this, "cfg" + getNameStr());
Dan Gohmanf17a25c2007-07-18 16:29:46 +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 Dunbar1e13b972009-07-24 08:24:36 +0000210 ViewGraph(this, "cfg" + getNameStr(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000211}
212
213FunctionPass *llvm::createCFGPrinterPass () {
214 return new CFGPrinter();
215}
216
217FunctionPass *llvm::createCFGOnlyPrinterPass () {
218 return new CFGOnlyPrinter();
219}
220