blob: 4d6819d4fa4d67c16cc22a57fab7ab6307e882bd [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 Lattner9d63afd2003-10-22 16:22:42 +000043 if (CFGOnly && !Node->getName().empty()) return Node->getName() + ":";
Chris Lattnera93d11b2003-10-22 16:03:49 +000044
45 std::ostringstream Out;
Chris Lattner9d63afd2003-10-22 16:22:42 +000046 if (CFGOnly) {
47 WriteAsOperand(Out, Node, false, true);
48 return Out.str();
49 }
50
Chris Lattnera93d11b2003-10-22 16:03:49 +000051 Out << *Node;
52 std::string OutStr = Out.str();
53 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
54
55 // Process string output to make it nicer...
56 for (unsigned i = 0; i != OutStr.length(); ++i)
57 if (OutStr[i] == '\n') { // Left justify
58 OutStr[i] = '\\';
59 OutStr.insert(OutStr.begin()+i+1, 'l');
60 } else if (OutStr[i] == ';') { // Delete comments!
61 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
62 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
63 --i;
64 }
65
66 return OutStr;
67 }
68
69 static std::string getNodeAttributes(const BasicBlock *N) {
70 return "fontname=Courier";
71 }
72
73 static std::string getEdgeSourceLabel(const BasicBlock *Node,
74 succ_const_iterator I) {
75 // Label source of conditional branches with "T" or "F"
76 if (const BranchInst *BI = dyn_cast<BranchInst>(Node->getTerminator()))
77 if (BI->isConditional())
78 return (I == succ_begin(Node)) ? "T" : "F";
79 return "";
80 }
81};
82
83namespace {
84 struct CFGPrinter : public FunctionPass {
85 virtual bool runOnFunction(Function &F) {
86 std::string Filename = "cfg." + F.getName() + ".dot";
87 std::cerr << "Writing '" << Filename << "'...";
88 std::ofstream File(Filename.c_str());
89
90 if (File.good())
91 WriteGraph(File, (const Function*)&F);
92 else
93 std::cerr << " error opening file for writing!";
94 std::cerr << "\n";
95 return false;
96 }
97
98 void print(std::ostream &OS) const {}
99
100 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
101 AU.setPreservesAll();
102 }
103 };
104
105 RegisterAnalysis<CFGPrinter> P1("print-cfg",
106 "Print CFG of function to 'dot' file");
107};
108
109
110
111
112/// viewCFG - This function is meant for use from the debugger. You can just
113/// say 'call F->viewCFG()' and a ghostview window should pop up from the
114/// program, displaying the CFG of the current function. This depends on there
115/// being a 'dot' and 'gv' program in your path.
116///
117void Function::viewCFG() const {
118 std::string Filename = "/tmp/cfg." + getName() + ".dot";
119 std::cerr << "Writing '" << Filename << "'... ";
120 std::ofstream F(Filename.c_str());
121
122 if (!F.good()) {
123 std::cerr << " error opening file for writing!\n";
124 return;
125 }
126
127 WriteGraph(F, this);
128 F.close();
129 std::cerr << "\n";
130
131 std::cerr << "Running 'dot' program... " << std::flush;
132 if (system(("dot -Tps " + Filename + " > /tmp/cfg.tempgraph.ps").c_str())) {
133 std::cerr << "Error running dot: 'dot' not in path?\n";
134 } else {
135 std::cerr << "\n";
136 system("gv /tmp/cfg.tempgraph.ps");
137 }
138 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
139}
140
141/// viewCFGOnly - This function is meant for use from the debugger. It works
142/// just like viewCFG, but it does not include the contents of basic blocks
143/// into the nodes, just the label. If you are only interested in the CFG t
144/// his can make the graph smaller.
145///
146void Function::viewCFGOnly() const {
147 CFGOnly = true;
148 viewCFG();
149 CFGOnly = false;
150}