blob: 00418f8013f5e75943d18f1867603ca6fa1e4117 [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
2//
3// This file implements the 'dot' graph printer.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/Analysis/DataStructure.h"
8#include "llvm/Module.h"
9#include "llvm/Assembly/Writer.h"
10#include <fstream>
11#include <sstream>
Chris Lattner0d9bab82002-07-18 00:12:30 +000012using std::string;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000013
14void DSNode::dump() const { print(std::cerr, 0); }
15
Chris Lattner0d9bab82002-07-18 00:12:30 +000016string DSNode::getCaption(const DSGraph *G) const {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017 std::stringstream OS;
Vikram S. Advedfd2f322002-07-30 22:07:26 +000018 Module *M = G && &G->getFunction()? G->getFunction().getParent() : 0;
Chris Lattner76d5b482002-07-11 20:33:32 +000019 WriteTypeSymbolic(OS, getType(), M);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000020
21 OS << " ";
22 if (NodeType & ScalarNode) OS << "S";
23 if (NodeType & AllocaNode) OS << "A";
24 if (NodeType & NewNode ) OS << "N";
25 if (NodeType & GlobalNode) OS << "G";
26 if (NodeType & SubElement) OS << "E";
27 if (NodeType & CastNode ) OS << "C";
Chris Lattner0d9bab82002-07-18 00:12:30 +000028 if (NodeType & Incomplete) OS << "I";
Chris Lattnerc68c31b2002-07-10 22:38:08 +000029
Chris Lattner76d5b482002-07-11 20:33:32 +000030 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
31 OS << "\n";
32 WriteAsOperand(OS, Globals[i], false, true, M);
33 }
34
35 if ((NodeType & ScalarNode) && G) {
36 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
37 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
38 E = VM.end(); I != E; ++I)
39 if (I->second == this) {
40 OS << "\n";
41 WriteAsOperand(OS, I->first, false, true, M);
42 }
43 }
44
Chris Lattnerc68c31b2002-07-10 22:38:08 +000045 return OS.str();
46}
47
Chris Lattner0d9bab82002-07-18 00:12:30 +000048static string getValueName(Value *V, Function &F) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000049 std::stringstream OS;
50 WriteAsOperand(OS, V, true, true, F.getParent());
51 return OS.str();
52}
53
54
55
Chris Lattner0d9bab82002-07-18 00:12:30 +000056static void replaceIn(string &S, char From, const string &To) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000057 for (unsigned i = 0; i < S.size(); )
58 if (S[i] == From) {
59 S.replace(S.begin()+i, S.begin()+i+1,
60 To.begin(), To.end());
61 i += To.size();
62 } else {
63 ++i;
64 }
65}
66
Anand Shukla6c5ed412002-07-16 00:03:10 +000067static std::string escapeLabel(const std::string &In) {
68 std::string Label(In);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000069 replaceIn(Label, '\\', "\\\\"); // Escape caption...
Chris Lattner76d5b482002-07-11 20:33:32 +000070 replaceIn(Label, '\n', "\\n");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000071 replaceIn(Label, ' ', "\\ ");
72 replaceIn(Label, '{', "\\{");
73 replaceIn(Label, '}', "\\}");
74 return Label;
75}
76
77static void writeEdge(std::ostream &O, const void *SrcNode,
78 const char *SrcNodePortName, int SrcNodeIdx,
Anand Shukla6c5ed412002-07-16 00:03:10 +000079 const DSNode *VS, const std::string &EdgeAttr = "") {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000080 O << "\tNode" << SrcNode << SrcNodePortName;
81 if (SrcNodeIdx != -1) O << SrcNodeIdx;
82 O << " -> Node" << (void*)VS;
83
84 if (!EdgeAttr.empty())
85 O << "[" << EdgeAttr << "]";
86 O << ";\n";
87}
88
Chris Lattner76d5b482002-07-11 20:33:32 +000089void DSNode::print(std::ostream &O, const DSGraph *G) const {
Anand Shukla6c5ed412002-07-16 00:03:10 +000090 std::string Caption = escapeLabel(getCaption(G));
Chris Lattnerc68c31b2002-07-10 22:38:08 +000091
92 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
93
94 if (!Links.empty()) {
95 O << "|{";
96 for (unsigned i = 0; i < Links.size(); ++i) {
97 if (i) O << "|";
98 O << "<g" << i << ">";
99 }
100 O << "}";
101 }
102 O << "}\"];\n";
103
104 for (unsigned i = 0; i < Links.size(); ++i)
105 if (Links[i])
106 writeEdge(O, this, ":g", i, Links[i]);
107}
108
109void DSGraph::print(std::ostream &O) const {
110 O << "digraph DataStructures {\n"
111 << "\tnode [shape=Mrecord];\n"
112 << "\tedge [arrowtail=\"dot\"];\n"
113 << "\tsize=\"10,7.5\";\n"
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000114 << "\trotate=\"90\";\n";
115
116 if (&Func != 0)
117 O << "\tlabel=\"Function\\ " << Func.getName() << "\";\n\n";
118 else
119 O << "\tlabel=\"Global Graph\";\n\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000120
121 // Output all of the nodes...
122 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000123 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000124
125 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000126
127 // Output the returned value pointer...
128 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000129 O << "\tNode0x1" << "[ plaintext=circle, label =\""
130 << escapeLabel("returning") << "\"];\n";
131 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000132 }
133
134 // Output all of the call nodes...
135 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
136 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
137 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
138 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
139 if (j) O << "|";
140 O << "<g" << j << ">";
141 }
142 O << "}}\"];\n";
143
144 for (unsigned j = 0, e = Call.size(); j != e; ++j)
145 if (Call[j])
Chris Lattner76d5b482002-07-11 20:33:32 +0000146 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000147 }
148
149
150 O << "}\n";
151}
152
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000153
154static void printGraph(const DSGraph &Graph, std::ostream &O,
155 const string &GraphName, const string &Prefix) {
156 string Filename = Prefix + "." + GraphName + ".dot";
157 O << "Writing '" << Filename << "'...";
158 std::ofstream F(Filename.c_str());
159
160 if (F.good()) {
161 Graph.print(F);
162 O << " [" << Graph.getGraphSize() << "+"
163 << Graph.getFunctionCalls().size() << "]\n";
164 } else {
165 O << " error opening file for writing!\n";
166 }
167}
168
Chris Lattner0d9bab82002-07-18 00:12:30 +0000169template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000170static void printCollection(const Collection &C, std::ostream &O,
171 const Module *M, const string &Prefix) {
172 if (M == 0) {
173 O << "Null Module pointer, cannot continue!\n";
174 return;
175 }
176
177 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000178 if (!I->isExternal())
179 printGraph(C.getDSGraph((Function&)*I), O, I->getName(), Prefix);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000180}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000181
182
183// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000184void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000185 printCollection(*this, O, M, "ds");
186}
187
Chris Lattner97f51a32002-07-27 01:12:15 +0000188void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000189 printCollection(*this, O, M, "bu");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000190
191 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
192 if (!I->isExternal()) {
193 printGraph(*getDSGraph(*I).GlobalsGraph, O, "program", "gg");
194 break;
195 }
196}
197
198void TDDataStructures::print(std::ostream &O, const Module *M) const {
199 printCollection(*this, O, M, "td");
200
201 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
202 if (!I->isExternal()) {
203 printGraph(*getDSGraph(*I).GlobalsGraph, O, "program", "gg");
204 break;
205 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000206}