blob: 614cb1fd0e4fca270854657cfd79f5a42eb17d81 [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"
Chris Lattnerc5f21de2002-10-02 22:14:38 +00008#include "llvm/Analysis/DSGraph.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +00009#include "llvm/Analysis/DSGraphTraits.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000010#include "llvm/Module.h"
11#include "llvm/Assembly/Writer.h"
Chris Lattnerdadd49b2002-07-31 17:15:40 +000012#include "Support/CommandLine.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000013#include "Support/GraphWriter.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000014#include <fstream>
15#include <sstream>
Chris Lattner0d9bab82002-07-18 00:12:30 +000016using std::string;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017
Chris Lattner55c10582002-10-03 20:38:41 +000018// OnlyPrintMain - The DataStructure printer exposes this option to allow
19// printing of only the graph for "main".
20//
21static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
22
23
Chris Lattnerc68c31b2002-07-10 22:38:08 +000024void DSNode::dump() const { print(std::cerr, 0); }
25
Chris Lattnerfccd06f2002-10-01 22:33:50 +000026static string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000027 std::stringstream OS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000028 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000029
Chris Lattnerfccd06f2002-10-01 22:33:50 +000030 for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
31 WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
32 if (N->getTypeEntries()[i].second)
33 OS << "@" << N->getTypeEntries()[i].second;
Chris Lattner76d5b482002-07-11 20:33:32 +000034 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000035 }
36
Chris Lattnerfccd06f2002-10-01 22:33:50 +000037 if (N->NodeType & DSNode::ScalarNode) OS << "S";
38 if (N->NodeType & DSNode::AllocaNode) OS << "A";
39 if (N->NodeType & DSNode::NewNode ) OS << "N";
40 if (N->NodeType & DSNode::GlobalNode) OS << "G";
41 if (N->NodeType & DSNode::Incomplete) OS << "I";
42
43 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
44 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
45 OS << "\n";
46 }
47
48 if ((N->NodeType & DSNode::ScalarNode) && G) {
Chris Lattner76d5b482002-07-11 20:33:32 +000049 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
50 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
51 E = VM.end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000052 if (I->second.getNode() == N) {
Chris Lattner76d5b482002-07-11 20:33:32 +000053 WriteAsOperand(OS, I->first, false, true, M);
Chris Lattnerfccd06f2002-10-01 22:33:50 +000054 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000055 }
56 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000057 return OS.str();
58}
59
Chris Lattner0d9bab82002-07-18 00:12:30 +000060static string getValueName(Value *V, Function &F) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000061 std::stringstream OS;
62 WriteAsOperand(OS, V, true, true, F.getParent());
63 return OS.str();
64}
65
66
67
Chris Lattner0d9bab82002-07-18 00:12:30 +000068static void replaceIn(string &S, char From, const string &To) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000069 for (unsigned i = 0; i < S.size(); )
70 if (S[i] == From) {
71 S.replace(S.begin()+i, S.begin()+i+1,
72 To.begin(), To.end());
73 i += To.size();
74 } else {
75 ++i;
76 }
77}
78
Anand Shukla6c5ed412002-07-16 00:03:10 +000079static std::string escapeLabel(const std::string &In) {
80 std::string Label(In);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000081 replaceIn(Label, '\\', "\\\\"); // Escape caption...
Chris Lattner76d5b482002-07-11 20:33:32 +000082 replaceIn(Label, '\n', "\\n");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000083 replaceIn(Label, ' ', "\\ ");
84 replaceIn(Label, '{', "\\{");
85 replaceIn(Label, '}', "\\}");
86 return Label;
87}
88
89static void writeEdge(std::ostream &O, const void *SrcNode,
90 const char *SrcNodePortName, int SrcNodeIdx,
Chris Lattnerfccd06f2002-10-01 22:33:50 +000091 const DSNodeHandle &VS,
92 const std::string &EdgeAttr = "") {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000093 O << "\tNode" << SrcNode << SrcNodePortName;
94 if (SrcNodeIdx != -1) O << SrcNodeIdx;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000095 O << " -> Node" << (void*)VS.getNode();
96 if (VS.getOffset()) O << ":g" << VS.getOffset();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000097
98 if (!EdgeAttr.empty())
99 O << "[" << EdgeAttr << "]";
100 O << ";\n";
101}
102
Chris Lattner76d5b482002-07-11 20:33:32 +0000103void DSNode::print(std::ostream &O, const DSGraph *G) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000104 std::string Caption = escapeLabel(getCaption(this, G));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000105
106 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
107
Chris Lattner6727ec62002-10-03 21:55:13 +0000108 unsigned Size = getSize();
109 if (Size > 64) Size = 64; // Don't print out HUGE graph nodes!
110
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000111 if (getSize() != 0) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000112 O << "|{";
Chris Lattner6727ec62002-10-03 21:55:13 +0000113 for (unsigned i = 0; i < Size; ++i) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000114 if (i) O << "|";
Chris Lattner9cfb3582002-10-02 05:17:55 +0000115 O << "<g" << i << ">" << (int)MergeMap[i];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000116 }
Chris Lattner6727ec62002-10-03 21:55:13 +0000117 if (Size != getSize())
118 O << "|truncated...";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000119 O << "}";
120 }
121 O << "}\"];\n";
122
Chris Lattner6727ec62002-10-03 21:55:13 +0000123 for (unsigned i = 0; i != Size; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000124 if (const DSNodeHandle *DSN = getLink(i))
125 writeEdge(O, this, ":g", i, *DSN);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000126}
127
128void DSGraph::print(std::ostream &O) const {
129 O << "digraph DataStructures {\n"
130 << "\tnode [shape=Mrecord];\n"
131 << "\tedge [arrowtail=\"dot\"];\n"
132 << "\tsize=\"10,7.5\";\n"
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000133 << "\trotate=\"90\";\n";
134
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000135 if (Func != 0)
136 O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000137
138 // Output all of the nodes...
139 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000140 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000141
142 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000143
144 // Output the returned value pointer...
145 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000146 O << "\tNode0x1" << "[ plaintext=circle, label =\""
147 << escapeLabel("returning") << "\"];\n";
148 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnereb265cd2002-10-16 02:04:36 +0000149 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000150
151 // Output all of the call nodes...
152 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
153 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
154 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
155 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
156 if (j) O << "|";
157 O << "<g" << j << ">";
158 }
159 O << "}}\"];\n";
160
161 for (unsigned j = 0, e = Call.size(); j != e; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000162 if (Call[j].getNode())
Chris Lattner76d5b482002-07-11 20:33:32 +0000163 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000164 }
165
166
167 O << "}\n";
168}
169
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000170template<>
171struct DOTGraphTraits<DSGraph*> : public DefaultDOTGraphTraits {
172 static std::string getGraphName(DSGraph *G) {
173 if (G->hasFunction())
174 return "Function " + G->getFunction().getName();
175 else
176 return "Non-function graph";
177 }
178
179 static const char *getGraphProperties(DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +0000180 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000181 "\tsize=\"10,7.5\";\n"
182 "\trotate=\"90\";\n";
183 }
184
185 static std::string getNodeLabel(DSNode *Node, DSGraph *Graph) {
186 return getCaption(Node, Graph);
187 }
188
189 static std::string getNodeAttributes(DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +0000190 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000191 }
192
Chris Lattnerf29e3072002-10-16 01:18:27 +0000193 static int getEdgeSourceLabel(DSNode *Node, DSNode::iterator I) {
194 assert(Node == I.getNode() && "Iterator not for this node!");
Chris Lattnerff5feed2002-10-16 01:43:11 +0000195 return Node->getMergeMapLabel(I.getOffset());
Chris Lattnerf29e3072002-10-16 01:18:27 +0000196 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000197
198 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
199 /// and the return node.
200 ///
201 static void addCustomGraphFeatures(DSGraph *G, GraphWriter<DSGraph*> &GW) {
202 // Output the returned value pointer...
203 if (G->getRetNode().getNode() != 0) {
204 // Output the return node...
205 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
206
207 // Add edge from return node to real destination
208 int RetEdgeDest = G->getRetNode().getOffset();
209 if (RetEdgeDest == 0) RetEdgeDest = -1;
210 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
211 RetEdgeDest, "arrowtail=tee,color=gray63");
212 }
Chris Lattner962ee452002-10-16 20:16:16 +0000213
214 // Output all of the call nodes...
215 const std::vector<std::vector<DSNodeHandle> > &FCs = G->getFunctionCalls();
216 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
217 const std::vector<DSNodeHandle> &Call = FCs[i];
218 GW.emitSimpleNode(&Call, "shape=record", "call", Call.size());
219
220 for (unsigned j = 0, e = Call.size(); j != e; ++j)
221 if (Call[j].getNode()) {
222 int EdgeDest = Call[j].getOffset();
223 if (EdgeDest == 0) EdgeDest = -1;
224 GW.emitEdge(&Call, j, Call[j].getNode(), EdgeDest, "color=gray63");
225 }
226 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000227 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000228};
229
230
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000231void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
232 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000233 O << "Writing '" << Filename << "'...";
234 std::ofstream F(Filename.c_str());
235
236 if (F.good()) {
Chris Lattner641e1c22002-10-16 01:34:28 +0000237 WriteGraph(F, this, "DataStructures");
Chris Lattnerf29e3072002-10-16 01:18:27 +0000238 //print(F);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000239 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000240 } else {
241 O << " error opening file for writing!\n";
242 }
243}
244
Chris Lattner0d9bab82002-07-18 00:12:30 +0000245template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000246static void printCollection(const Collection &C, std::ostream &O,
247 const Module *M, const string &Prefix) {
248 if (M == 0) {
249 O << "Null Module pointer, cannot continue!\n";
250 return;
251 }
252
253 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000254 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000255 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000256}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000257
258
259// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000260void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000261 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000262}
263
Chris Lattner97f51a32002-07-27 01:12:15 +0000264void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000265 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000266#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000267 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
268 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000269 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000270 break;
271 }
Chris Lattner55c10582002-10-03 20:38:41 +0000272#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000273}
274
Chris Lattner55c10582002-10-03 20:38:41 +0000275#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000276void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000277 printCollection(*this, O, M, "td.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000278
279 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
280 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000281 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000282 break;
283 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000284}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000285#endif