llvm.org GIT mirror llvm / 002362c
Implement the Function::viewCFG* methods, for use in a debugger. Also, the -print-cfg pass now lives here. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@9374 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 16 years ago
1 changed file(s) with 144 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===- CFGPrinter.cpp - DOT printer for the control flow graph ------------===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file was developed by the LLVM research group and is distributed under
5 // the University of Illinois Open Source License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines a '-print-cfg' analysis pass, which emits the
10 // cfg..dot file for each function in the program, with a graph of the
11 // CFG for that function.
12 //
13 // The other main feature of this file is that it implements the
14 // Function::viewCFG method, which is useful for debugging passes which operate
15 // on the CFG.
16 //
17 //===----------------------------------------------------------------------===//
18
19 #include "Support/GraphWriter.h"
20 #include "llvm/Pass.h"
21 #include "llvm/Function.h"
22 #include "llvm/iTerminators.h"
23 #include "llvm/Support/CFG.h"
24 #include
25 #include
26
27 /// CFGOnly flag - This is used to control whether or not the CFG graph printer
28 /// prints out the contents of basic blocks or not. This is acceptable because
29 /// this code is only really used for debugging purposes.
30 ///
31 static bool CFGOnly = false;
32
33 template<>
34 struct DOTGraphTraits : public DefaultDOTGraphTraits {
35 static std::string getGraphName(const Function *F) {
36 return "CFG for '" + F->getName() + "' function";
37 }
38
39 static std::string getNodeLabel(const BasicBlock *Node,
40 const Function *Graph) {
41 if (CFGOnly) return Node->getName() + ":";
42
43 std::ostringstream Out;
44 Out << *Node;
45 std::string OutStr = Out.str();
46 if (OutStr[0] == '\n') OutStr.erase(OutStr.begin());
47
48 // Process string output to make it nicer...
49 for (unsigned i = 0; i != OutStr.length(); ++i)
50 if (OutStr[i] == '\n') { // Left justify
51 OutStr[i] = '\\';
52 OutStr.insert(OutStr.begin()+i+1, 'l');
53 } else if (OutStr[i] == ';') { // Delete comments!
54 unsigned Idx = OutStr.find('\n', i+1); // Find end of line
55 OutStr.erase(OutStr.begin()+i, OutStr.begin()+Idx);
56 --i;
57 }
58
59 return OutStr;
60 }
61
62 static std::string getNodeAttributes(const BasicBlock *N) {
63 return "fontname=Courier";
64 }
65
66 static std::string getEdgeSourceLabel(const BasicBlock *Node,
67 succ_const_iterator I) {
68 // Label source of conditional branches with "T" or "F"
69 if (const BranchInst *BI = dyn_cast(Node->getTerminator()))
70 if (BI->isConditional())
71 return (I == succ_begin(Node)) ? "T" : "F";
72 return "";
73 }
74 };
75
76 namespace {
77 struct CFGPrinter : public FunctionPass {
78 virtual bool runOnFunction(Function &F) {
79 std::string Filename = "cfg." + F.getName() + ".dot";
80 std::cerr << "Writing '" << Filename << "'...";
81 std::ofstream File(Filename.c_str());
82
83 if (File.good())
84 WriteGraph(File, (const Function*)&F);
85 else
86 std::cerr << " error opening file for writing!";
87 std::cerr << "\n";
88 return false;
89 }
90
91 void print(std::ostream &OS) const {}
92
93 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
94 AU.setPreservesAll();
95 }
96 };
97
98 RegisterAnalysis P1("print-cfg",
99 "Print CFG of function to 'dot' file");
100 };
101
102
103
104
105 /// viewCFG - This function is meant for use from the debugger. You can just
106 /// say 'call F->viewCFG()' and a ghostview window should pop up from the
107 /// program, displaying the CFG of the current function. This depends on there
108 /// being a 'dot' and 'gv' program in your path.
109 ///
110 void Function::viewCFG() const {
111 std::string Filename = "/tmp/cfg." + getName() + ".dot";
112 std::cerr << "Writing '" << Filename << "'... ";
113 std::ofstream F(Filename.c_str());
114
115 if (!F.good()) {
116 std::cerr << " error opening file for writing!\n";
117 return;
118 }
119
120 WriteGraph(F, this);
121 F.close();
122 std::cerr << "\n";
123
124 std::cerr << "Running 'dot' program... " << std::flush;
125 if (system(("dot -Tps " + Filename + " > /tmp/cfg.tempgraph.ps").c_str())) {
126 std::cerr << "Error running dot: 'dot' not in path?\n";
127 } else {
128 std::cerr << "\n";
129 system("gv /tmp/cfg.tempgraph.ps");
130 }
131 system(("rm " + Filename + " /tmp/cfg.tempgraph.ps").c_str());
132 }
133
134 /// viewCFGOnly - This function is meant for use from the debugger. It works
135 /// just like viewCFG, but it does not include the contents of basic blocks
136 /// into the nodes, just the label. If you are only interested in the CFG t
137 /// his can make the graph smaller.
138 ///
139 void Function::viewCFGOnly() const {
140 CFGOnly = true;
141 viewCFG();
142 CFGOnly = false;
143 }