DataStructure.h doesn't include DSGraph.h
[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/Module.h"
10 #include "llvm/Assembly/Writer.h"
11 #include "Support/CommandLine.h"
12 #include <fstream>
13 #include <sstream>
14 using std::string;
15
16 void DSNode::dump() const { print(std::cerr, 0); }
17
18 static string getCaption(const DSNode *N, const DSGraph *G) {
19   std::stringstream OS;
20   Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
21
22   for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
23     WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
24     if (N->getTypeEntries()[i].second)
25       OS << "@" << N->getTypeEntries()[i].second;
26     OS << "\n";
27   }
28
29   if (N->NodeType & DSNode::ScalarNode) OS << "S";
30   if (N->NodeType & DSNode::AllocaNode) OS << "A";
31   if (N->NodeType & DSNode::NewNode   ) OS << "N";
32   if (N->NodeType & DSNode::GlobalNode) OS << "G";
33   if (N->NodeType & DSNode::Incomplete) OS << "I";
34
35   for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
36     WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
37     OS << "\n";
38   }
39
40   if ((N->NodeType & DSNode::ScalarNode) && G) {
41     const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
42     for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
43            E = VM.end(); I != E; ++I)
44       if (I->second.getNode() == N) {
45         WriteAsOperand(OS, I->first, false, true, M);
46         OS << "\n";
47       }
48   }
49   return OS.str();
50 }
51
52 static string getValueName(Value *V, Function &F) {
53   std::stringstream OS;
54   WriteAsOperand(OS, V, true, true, F.getParent());
55   return OS.str();
56 }
57
58
59
60 static void replaceIn(string &S, char From, const string &To) {
61   for (unsigned i = 0; i < S.size(); )
62     if (S[i] == From) {
63       S.replace(S.begin()+i, S.begin()+i+1,
64                 To.begin(), To.end());
65       i += To.size();
66     } else {
67       ++i;
68     }
69 }
70
71 static std::string escapeLabel(const std::string &In) {
72   std::string Label(In);
73   replaceIn(Label, '\\', "\\\\");  // Escape caption...
74   replaceIn(Label, '\n', "\\n");
75   replaceIn(Label, ' ', "\\ ");
76   replaceIn(Label, '{', "\\{");
77   replaceIn(Label, '}', "\\}");
78   return Label;
79 }
80
81 static void writeEdge(std::ostream &O, const void *SrcNode,
82                       const char *SrcNodePortName, int SrcNodeIdx,
83                       const DSNodeHandle &VS,
84                       const std::string &EdgeAttr = "") {
85   O << "\tNode" << SrcNode << SrcNodePortName;
86   if (SrcNodeIdx != -1) O << SrcNodeIdx;
87   O << " -> Node" << (void*)VS.getNode();
88   if (VS.getOffset()) O << ":g" << VS.getOffset();
89
90   if (!EdgeAttr.empty())
91     O << "[" << EdgeAttr << "]";
92   O << ";\n";
93 }
94
95 void DSNode::print(std::ostream &O, const DSGraph *G) const {
96   std::string Caption = escapeLabel(getCaption(this, G));
97
98   O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
99
100   if (getSize() != 0) {
101     O << "|{";
102     for (unsigned i = 0; i < getSize(); ++i) {
103       if (i) O << "|";
104       O << "<g" << i << ">" << (int)MergeMap[i];
105     }
106     O << "}";
107   }
108   O << "}\"];\n";
109
110   for (unsigned i = 0; i != getSize(); ++i)
111     if (const DSNodeHandle *DSN = getLink(i))
112       writeEdge(O, this, ":g", i, *DSN);
113 }
114
115 void DSGraph::print(std::ostream &O) const {
116   O << "digraph DataStructures {\n"
117     << "\tnode [shape=Mrecord];\n"
118     << "\tedge [arrowtail=\"dot\"];\n"
119     << "\tsize=\"10,7.5\";\n"
120     << "\trotate=\"90\";\n";
121
122   if (Func != 0)
123     O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
124
125   // Output all of the nodes...
126   for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
127     Nodes[i]->print(O, this);
128
129   O << "\n";
130
131   // Output the returned value pointer...
132   if (RetNode != 0) {
133     O << "\tNode0x1" << "[ plaintext=circle, label =\""
134       << escapeLabel("returning") << "\"];\n";
135     writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
136   }    
137
138   // Output all of the call nodes...
139   for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
140     const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
141     O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
142     for (unsigned j = 0, e = Call.size(); j != e; ++j) {
143       if (j) O << "|";
144       O << "<g" << j << ">";
145     }
146     O << "}}\"];\n";
147
148     for (unsigned j = 0, e = Call.size(); j != e; ++j)
149       if (Call[j].getNode())
150         writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
151   }
152
153
154   O << "}\n";
155 }
156
157
158 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
159   string Filename = GraphName + ".dot";
160   O << "Writing '" << Filename << "'...";
161   std::ofstream F(Filename.c_str());
162   
163   if (F.good()) {
164     print(F);
165     O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
166   } else {
167     O << "  error opening file for writing!\n";
168   }
169 }
170
171 static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
172
173 template <typename Collection>
174 static void printCollection(const Collection &C, std::ostream &O,
175                             const Module *M, const string &Prefix) {
176   if (M == 0) {
177     O << "Null Module pointer, cannot continue!\n";
178     return;
179   }
180
181   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
182     if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
183       C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
184 }
185
186
187 // print - Print out the analysis results...
188 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
189   printCollection(*this, O, M, "ds.");
190 }
191
192 #if 0
193 void BUDataStructures::print(std::ostream &O, const Module *M) const {
194   printCollection(*this, O, M, "bu.");
195
196   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
197     if (!I->isExternal()) {
198       (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
199       break;
200     }
201 }
202
203 void TDDataStructures::print(std::ostream &O, const Module *M) const {
204   printCollection(*this, O, M, "td.");
205
206   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
207     if (!I->isExternal()) {
208       (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
209       break;
210     }
211 }
212 #endif