blob: ac93d4c1ca0afdb84520a57d5993be1ebbb64b17 [file] [log] [blame]
Chris Lattnera93d11b2003-10-22 16:03:49 +00001//===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
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
20#include "Support/GraphWriter.h"
21#include "llvm/Pass.h"
22#include "llvm/Function.h"
23#include "llvm/iTerminators.h"
Chris Lattner9d63afd2003-10-22 16:22:42 +000024#include "llvm/Assembly/Writer.h"
Chris Lattnera93d11b2003-10-22 16:03:49 +000025#include "llvm/Support/CFG.h"
26#include <sstream>
27#include <fstream>
28
29/// CFGOnly flag - This is used to control whether or not the CFG graph printer
30/// prints out the contents of basic blocks or not. This is acceptable because
31/// this code is only really used for debugging purposes.
32///
33static bool CFGOnly = false;
34
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,
42 const Function *Graph) {
Chris Lattneraa81dce2003-10-22 16:30:58 +000043 if (CFGOnly && !Node->getName().empty())
44 return Node->getName() + ":";
Chris Lattnera93d11b2003-10-22 16:03:49 +000045
46 std::ostringstream Out;
Chris Lattner9d63afd2003-10-22 16:22:42 +000047 if (CFGOnly) {
48 WriteAsOperand(Out, Node, false, true);
49 return Out.str();
50 }
51
Chris Lattneraa81dce2003-10-22 16:30:58 +000052 if (Node->getName().empty()) {
53 WriteAsOperand(Out, Node, false, true);
54 Out << ":";
55 }
56
Chris Lattnera93d11b2003-10-22 16:03:49 +000057 Out << *Node;
58 std::string OutStr = Out.str();
59 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
75 static std::string getNodeAttributes(const BasicBlock *N) {
76 return "fontname=Courier";
77 }
78
79 static std::string getEdgeSourceLabel(const BasicBlock *Node,
80 succ_const_iterator I) {
81 // Label source of conditional branches with "T" or "F"
82 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
83 if (BI->isConditional())
84 return (I == succ_begin(Node)) ? "T" : "F";
85 return "";
86 }
87};
88
89namespace {
90 struct CFGPrinter : public FunctionPass {
91 virtual bool runOnFunction(Function &F) {
92 std::string Filename = "cfg." + F.getName() + ".dot";
93 std::cerr << "Writing '" << Filename << "'...";
94 std::ofstream File(Filename.c_str());
95
96 if (File.good())
97 WriteGraph(File, (const Function*)&F);
98 else
99 std::cerr << " error opening file for writing!";
100 std::cerr << "\n";
101 return false;
102 }
103
104 void print(std::ostream &OS) const {}
105
106 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
107 AU.setPreservesAll();
108 }
109 };
110
111 RegisterAnalysis<CFGPrinter> P1("print-cfg",
112 "Print CFG of function to 'dot' file");
113};
114
115
116
117
118/// viewCFG - This function is meant for use from the debugger. You can just
119/// say 'call F->viewCFG()' and a ghostview window should pop up from the
120/// program, displaying the CFG of the current function. This depends on there
121/// being a 'dot' and 'gv' program in your path.
122///
123void Function::viewCFG() const {
124 std::string Filename = "/tmp/cfg." + getName() + ".dot";
125 std::cerr << "Writing '" << Filename << "'... ";
126 std::ofstream F(Filename.c_str());
127
128 if (!F.good()) {
129 std::cerr << " error opening file for writing!\n";
130 return;
131 }
132
133 WriteGraph(F, this);
134 F.close();
135 std::cerr << "\n";
136
137 std::cerr << "Running 'dot' program... " << std::flush;
138 if (system(("dot -Tps " + Filename + " > /tmp/cfg.tempgraph.ps").c_str())) {
139 std::cerr << "Error running dot: 'dot' not in path?\n";
140 } else {
141 std::cerr << "\n";
142 system("gv /tmp/cfg.tempgraph.ps");
143 }
144 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
145}
146
147/// viewCFGOnly - This function is meant for use from the debugger. It works
148/// just like viewCFG, but it does not include the contents of basic blocks
149/// into the nodes, just the label. If you are only interested in the CFG t
150/// his can make the graph smaller.
151///
152void Function::viewCFGOnly() const {
153 CFGOnly = true;
154 viewCFG();
155 CFGOnly = false;
156}