4a58c69afd3dfe6ccb6d4ac100a72257c26d0ec0
[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/Module.h"
9 #include "llvm/Assembly/Writer.h"
10 #include <fstream>
11 #include <sstream>
12 using std::string;
13
14 void DSNode::dump() const { print(std::cerr, 0); }
15
16 string DSNode::getCaption(const DSGraph *G) const {
17   std::stringstream OS;
18   Module *M = G ? G->getFunction().getParent() : 0;
19   WriteTypeSymbolic(OS, getType(), M);
20
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";
28   if (NodeType & Incomplete) OS << "I";
29
30   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
45   return OS.str();
46 }
47
48 static string getValueName(Value *V, Function &F) {
49   std::stringstream OS;
50   WriteAsOperand(OS, V, true, true, F.getParent());
51   return OS.str();
52 }
53
54
55
56 static void replaceIn(string &S, char From, const string &To) {
57   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
67 static std::string escapeLabel(const std::string &In) {
68   std::string Label(In);
69   replaceIn(Label, '\\', "\\\\");  // Escape caption...
70   replaceIn(Label, '\n', "\\n");
71   replaceIn(Label, ' ', "\\ ");
72   replaceIn(Label, '{', "\\{");
73   replaceIn(Label, '}', "\\}");
74   return Label;
75 }
76
77 static void writeEdge(std::ostream &O, const void *SrcNode,
78                       const char *SrcNodePortName, int SrcNodeIdx,
79                       const DSNode *VS, const std::string &EdgeAttr = "") {
80   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
89 void DSNode::print(std::ostream &O, const DSGraph *G) const {
90   std::string Caption = escapeLabel(getCaption(G));
91
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
109 void 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"
114     << "\trotate=\"90\";\n"
115     << "\tlabel=\"Function\\ " << Func.getName() << "\";\n\n";
116
117   // Output all of the nodes...
118   for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
119     Nodes[i]->print(O, this);
120
121   O << "\n";
122
123   // Output the returned value pointer...
124   if (RetNode != 0) {
125     O << "\tNode0x1" << "[ plaintext=circle, label =\""
126       << escapeLabel("returning") << "\"];\n";
127     writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
128   }    
129
130   // Output all of the call nodes...
131   for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
132     const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
133     O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
134     for (unsigned j = 0, e = Call.size(); j != e; ++j) {
135       if (j) O << "|";
136       O << "<g" << j << ">";
137     }
138     O << "}}\"];\n";
139
140     for (unsigned j = 0, e = Call.size(); j != e; ++j)
141       if (Call[j])
142         writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
143   }
144
145
146   O << "}\n";
147 }
148
149 template <typename Collection>
150 static void printCollection(const Collection &C, std::ostream &O, Module *M,
151                             const string &Prefix) {
152   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
153     if (!I->isExternal()) {
154       string Filename = Prefix + "." + I->getName() + ".dot";
155       O << "Writing '" << Filename << "'...";
156       std::ofstream F(Filename.c_str());
157       
158       if (F.good()) {
159         DSGraph &Graph = C.getDSGraph(*I);
160         Graph.print(F);
161         O << " [" << Graph.getGraphSize() << "+"
162           << Graph.getFunctionCalls().size() << "]\n";
163       } else {
164         O << "  error opening file for writing!\n";
165       }
166     }
167 }
168
169
170 // print - Print out the analysis results...
171 void LocalDataStructures::print(std::ostream &O, Module *M) const {
172   printCollection(*this, O, M, "ds");
173 }
174
175 void BUDataStructures::print(std::ostream &O, Module *M) const {
176   printCollection(*this, O, M, "bu");
177 }