blob: 8ada5a3f74cddf0df8d9906a65a19db640bb5816 [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) {
38 return "CFG for '" + F->getName() + "' function";
39 }
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())
Dan Gohmanf17a25c2007-07-18 16:29:46 +000045 return Node->getName() + ":";
46
47 std::ostringstream Out;
Owen Andersonf4a15462009-06-24 17:37:09 +000048 if (ShortNames) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000049 WriteAsOperand(Out, Node, false);
50 return Out.str();
51 }
52
53 if (Node->getName().empty()) {
54 WriteAsOperand(Out, Node, false);
55 Out << ":";
56 }
57
58 Out << *Node;
59 std::string OutStr = Out.str();
60 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
61
62 // Process string output to make it nicer...
63 for (unsigned i = 0; i != OutStr.length(); ++i)
64 if (OutStr[i] == '\n') { // Left justify
65 OutStr[i] = '\\';
66 OutStr.insert(OutStr.begin()+i+1, 'l');
67 } else if (OutStr[i] == ';') { // Delete comments!
68 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
69 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
70 --i;
71 }
72
73 return OutStr;
74 }
75
76 static std::string getEdgeSourceLabel(const BasicBlock *Node,
77 succ_const_iterator I) {
78 // Label source of conditional branches with "T" or "F"
79 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
80 if (BI->isConditional())
81 return (I == succ_begin(Node)) ? "T" : "F";
82 return "";
83 }
84};
85}
86
87namespace {
88 struct VISIBILITY_HIDDEN CFGViewer : public FunctionPass {
89 static char ID; // Pass identifcation, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +000090 CFGViewer() : FunctionPass(&ID) {}
Devang Patel2b4fa682008-03-18 00:39:19 +000091
Dan Gohmanf17a25c2007-07-18 16:29:46 +000092 virtual bool runOnFunction(Function &F) {
93 F.viewCFG();
94 return false;
95 }
96
97 void print(std::ostream &OS, const Module* = 0) const {}
98
99 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
100 AU.setPreservesAll();
101 }
102 };
Dan Gohman089efff2008-05-13 00:00:25 +0000103}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000104
Dan Gohman089efff2008-05-13 00:00:25 +0000105char CFGViewer::ID = 0;
106static RegisterPass<CFGViewer>
107V0("view-cfg", "View CFG of function", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000108
Dan Gohman089efff2008-05-13 00:00:25 +0000109namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000110 struct VISIBILITY_HIDDEN CFGOnlyViewer : public FunctionPass {
111 static char ID; // Pass identifcation, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000112 CFGOnlyViewer() : FunctionPass(&ID) {}
Devang Patel2b4fa682008-03-18 00:39:19 +0000113
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000114 virtual bool runOnFunction(Function &F) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000115 F.viewCFG();
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000116 return false;
117 }
118
119 void print(std::ostream &OS, const Module* = 0) const {}
120
121 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
122 AU.setPreservesAll();
123 }
124 };
Dan Gohman089efff2008-05-13 00:00:25 +0000125}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000126
Dan Gohman089efff2008-05-13 00:00:25 +0000127char CFGOnlyViewer::ID = 0;
128static RegisterPass<CFGOnlyViewer>
129V1("view-cfg-only",
130 "View CFG of function (with no function bodies)", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000131
Dan Gohman089efff2008-05-13 00:00:25 +0000132namespace {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000133 struct VISIBILITY_HIDDEN CFGPrinter : public FunctionPass {
134 static char ID; // Pass identification, replacement for typeid
Dan Gohman26f8c272008-09-04 17:05:41 +0000135 CFGPrinter() : FunctionPass(&ID) {}
136 explicit CFGPrinter(void *pid) : FunctionPass(pid) {}
Devang Patel2b4fa682008-03-18 00:39:19 +0000137
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000138 virtual bool runOnFunction(Function &F) {
139 std::string Filename = "cfg." + F.getName() + ".dot";
140 cerr << "Writing '" << Filename << "'...";
141 std::ofstream File(Filename.c_str());
142
143 if (File.good())
144 WriteGraph(File, (const Function*)&F);
145 else
146 cerr << " error opening file for writing!";
147 cerr << "\n";
148 return false;
149 }
150
151 void print(std::ostream &OS, const Module* = 0) const {}
152
153 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
154 AU.setPreservesAll();
155 }
156 };
Dan Gohman089efff2008-05-13 00:00:25 +0000157}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000158
Dan Gohman089efff2008-05-13 00:00:25 +0000159char CFGPrinter::ID = 0;
160static RegisterPass<CFGPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +0000161P1("dot-cfg", "Print CFG of function to 'dot' file", false, true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000162
Dan Gohman089efff2008-05-13 00:00:25 +0000163namespace {
Owen Andersonf4a15462009-06-24 17:37:09 +0000164 struct VISIBILITY_HIDDEN CFGOnlyPrinter : public FunctionPass {
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000165 static char ID; // Pass identification, replacement for typeid
Owen Andersonf4a15462009-06-24 17:37:09 +0000166 CFGOnlyPrinter() : FunctionPass(&ID) {}
167 explicit CFGOnlyPrinter(void *pid) : FunctionPass(pid) {}
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000168 virtual bool runOnFunction(Function &F) {
Owen Andersonf4a15462009-06-24 17:37:09 +0000169 std::string Filename = "cfg." + F.getName() + ".dot";
170 cerr << "Writing '" << Filename << "'...";
171 std::ofstream File(Filename.c_str());
172
173 if (File.good())
174 WriteGraph(File, (const Function*)&F, true);
175 else
176 cerr << " error opening file for writing!";
177 cerr << "\n";
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000178 return false;
179 }
180 void print(std::ostream &OS, const Module* = 0) const {}
181
182 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
183 AU.setPreservesAll();
184 }
185 };
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000186}
187
Dan Gohman089efff2008-05-13 00:00:25 +0000188char CFGOnlyPrinter::ID = 0;
189static RegisterPass<CFGOnlyPrinter>
Duncan Sandsd10d6f72008-09-23 12:47:39 +0000190P2("dot-cfg-only",
Dan Gohman089efff2008-05-13 00:00:25 +0000191 "Print CFG of function to 'dot' file (with no function bodies)", false, true);
192
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000193/// viewCFG - This function is meant for use from the debugger. You can just
194/// say 'call F->viewCFG()' and a ghostview window should pop up from the
195/// program, displaying the CFG of the current function. This depends on there
196/// being a 'dot' and 'gv' program in your path.
197///
198void Function::viewCFG() const {
199 ViewGraph(this, "cfg" + getName());
200}
201
202/// viewCFGOnly - This function is meant for use from the debugger. It works
203/// just like viewCFG, but it does not include the contents of basic blocks
204/// into the nodes, just the label. If you are only interested in the CFG t
205/// his can make the graph smaller.
206///
207void Function::viewCFGOnly() const {
Owen Andersonf4a15462009-06-24 17:37:09 +0000208 ViewGraph(this, "cfg" + getName(), true);
Dan Gohmanf17a25c2007-07-18 16:29:46 +0000209}
210
211FunctionPass *llvm::createCFGPrinterPass () {
212 return new CFGPrinter();
213}
214
215FunctionPass *llvm::createCFGOnlyPrinterPass () {
216 return new CFGOnlyPrinter();
217}
218