* Print the "return" node in the graphs
[oota-llvm.git] / lib / Analysis / DataStructure / Printer.cpp
1 //===- 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/Analysis/DSGraph.h"
9 #include "llvm/Analysis/DSGraphTraits.h"
10 #include "llvm/Module.h"
11 #include "llvm/Assembly/Writer.h"
12 #include "Support/CommandLine.h"
13 #include "Support/GraphWriter.h"
14 #include <fstream>
15 #include <sstream>
16 using std::string;
17
18 // OnlyPrintMain - The DataStructure printer exposes this option to allow
19 // printing of only the graph for "main".
20 //
21 static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
22
23
24 void DSNode::dump() const { print(std::cerr, 0); }
25
26 static string getCaption(const DSNode *N, const DSGraph *G) {
27   std::stringstream OS;
28   Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
29
30   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;
34     OS << "\n";
35   }
36
37   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) {
49     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)
52       if (I->second.getNode() == N) {
53         WriteAsOperand(OS, I->first, false, true, M);
54         OS << "\n";
55       }
56   }
57   return OS.str();
58 }
59
60 static string getValueName(Value *V, Function &F) {
61   std::stringstream OS;
62   WriteAsOperand(OS, V, true, true, F.getParent());
63   return OS.str();
64 }
65
66
67
68 static void replaceIn(string &S, char From, const string &To) {
69   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
79 static std::string escapeLabel(const std::string &In) {
80   std::string Label(In);
81   replaceIn(Label, '\\', "\\\\");  // Escape caption...
82   replaceIn(Label, '\n', "\\n");
83   replaceIn(Label, ' ', "\\ ");
84   replaceIn(Label, '{', "\\{");
85   replaceIn(Label, '}', "\\}");
86   return Label;
87 }
88
89 static void writeEdge(std::ostream &O, const void *SrcNode,
90                       const char *SrcNodePortName, int SrcNodeIdx,
91                       const DSNodeHandle &VS,
92                       const std::string &EdgeAttr = "") {
93   O << "\tNode" << SrcNode << SrcNodePortName;
94   if (SrcNodeIdx != -1) O << SrcNodeIdx;
95   O << " -> Node" << (void*)VS.getNode();
96   if (VS.getOffset()) O << ":g" << VS.getOffset();
97
98   if (!EdgeAttr.empty())
99     O << "[" << EdgeAttr << "]";
100   O << ";\n";
101 }
102
103 void DSNode::print(std::ostream &O, const DSGraph *G) const {
104   std::string Caption = escapeLabel(getCaption(this, G));
105
106   O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
107
108   unsigned Size = getSize();
109   if (Size > 64) Size = 64;   // Don't print out HUGE graph nodes!
110
111   if (getSize() != 0) {
112     O << "|{";
113     for (unsigned i = 0; i < Size; ++i) {
114       if (i) O << "|";
115       O << "<g" << i << ">" << (int)MergeMap[i];
116     }
117     if (Size != getSize())
118       O << "|truncated...";
119     O << "}";
120   }
121   O << "}\"];\n";
122
123   for (unsigned i = 0; i != Size; ++i)
124     if (const DSNodeHandle *DSN = getLink(i))
125       writeEdge(O, this, ":g", i, *DSN);
126 }
127
128 void 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"
133     << "\trotate=\"90\";\n";
134
135   if (Func != 0)
136     O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
137
138   // Output all of the nodes...
139   for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
140     Nodes[i]->print(O, this);
141
142   O << "\n";
143
144   // Output the returned value pointer...
145   if (RetNode != 0) {
146     O << "\tNode0x1" << "[ plaintext=circle, label =\""
147       << escapeLabel("returning") << "\"];\n";
148     writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
149   }
150
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)
162       if (Call[j].getNode())
163         writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
164   }
165
166
167   O << "}\n";
168 }
169
170 template<>
171 struct 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) {
180     return "\tedge [arrowtail=\"dot\"];\n"
181            "\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) {
190     return "shape=Mrecord";//fontname=Courier";
191   }
192   
193   static int getEdgeSourceLabel(DSNode *Node, DSNode::iterator I) {
194     assert(Node == I.getNode() && "Iterator not for this node!");
195     return Node->getMergeMapLabel(I.getOffset());
196   }
197
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     }
213   }
214 };
215
216
217
218 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
219   string Filename = GraphName + ".dot";
220   O << "Writing '" << Filename << "'...";
221   std::ofstream F(Filename.c_str());
222   
223   if (F.good()) {
224     WriteGraph(F, this, "DataStructures");
225     //print(F);
226     O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
227   } else {
228     O << "  error opening file for writing!\n";
229   }
230 }
231
232 template <typename Collection>
233 static void printCollection(const Collection &C, std::ostream &O,
234                             const Module *M, const string &Prefix) {
235   if (M == 0) {
236     O << "Null Module pointer, cannot continue!\n";
237     return;
238   }
239
240   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
241     if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
242       C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
243 }
244
245
246 // print - Print out the analysis results...
247 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
248   printCollection(*this, O, M, "ds.");
249 }
250
251 void BUDataStructures::print(std::ostream &O, const Module *M) const {
252   printCollection(*this, O, M, "bu.");
253 #if 0
254   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
255     if (!I->isExternal()) {
256       (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
257       break;
258     }
259 #endif
260 }
261
262 #if 0
263 void TDDataStructures::print(std::ostream &O, const Module *M) const {
264   printCollection(*this, O, M, "td.");
265
266   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
267     if (!I->isExternal()) {
268       (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
269       break;
270     }
271 }
272 #endif