15f8df19ef10c57dda2311be183ffc5e4230015c
[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
13 void DSNode::dump() const { print(std::cerr, 0); }
14
15 std::string DSNode::getCaption(const DSGraph *G) const {
16   std::stringstream OS;
17   Module *M = G ? G->getFunction().getParent() : 0;
18   WriteTypeSymbolic(OS, getType(), M);
19
20   OS << " ";
21   if (NodeType & ScalarNode) OS << "S";
22   if (NodeType & AllocaNode) OS << "A";
23   if (NodeType & NewNode   ) OS << "N";
24   if (NodeType & GlobalNode) OS << "G";
25   if (NodeType & SubElement) OS << "E";
26   if (NodeType & CastNode  ) OS << "C";
27
28   for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
29     OS << "\n";
30     WriteAsOperand(OS, Globals[i], false, true, M);
31   }
32
33   if ((NodeType & ScalarNode) && G) {
34     const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
35     for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
36            E = VM.end(); I != E; ++I)
37       if (I->second == this) {
38         OS << "\n";
39         WriteAsOperand(OS, I->first, false, true, M);
40       }
41   }
42
43   return OS.str();
44 }
45
46 static std::string getValueName(Value *V, Function &F) {
47   std::stringstream OS;
48   WriteAsOperand(OS, V, true, true, F.getParent());
49   return OS.str();
50 }
51
52
53
54 static void replaceIn(std::string &S, char From, const std::string &To) {
55   for (unsigned i = 0; i < S.size(); )
56     if (S[i] == From) {
57       S.replace(S.begin()+i, S.begin()+i+1,
58                 To.begin(), To.end());
59       i += To.size();
60     } else {
61       ++i;
62     }
63 }
64
65 static std::string escapeLabel(const std::string &In) {
66   std::string Label(In);
67   replaceIn(Label, '\\', "\\\\");  // Escape caption...
68   replaceIn(Label, '\n', "\\n");
69   replaceIn(Label, ' ', "\\ ");
70   replaceIn(Label, '{', "\\{");
71   replaceIn(Label, '}', "\\}");
72   return Label;
73 }
74
75 static void writeEdge(std::ostream &O, const void *SrcNode,
76                       const char *SrcNodePortName, int SrcNodeIdx,
77                       const DSNode *VS, const std::string &EdgeAttr = "") {
78   O << "\tNode" << SrcNode << SrcNodePortName;
79   if (SrcNodeIdx != -1) O << SrcNodeIdx;
80   O << " -> Node" << (void*)VS;
81
82   if (!EdgeAttr.empty())
83     O << "[" << EdgeAttr << "]";
84   O << ";\n";
85 }
86
87 void DSNode::print(std::ostream &O, const DSGraph *G) const {
88   std::string Caption = escapeLabel(getCaption(G));
89
90   O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
91
92   if (!Links.empty()) {
93     O << "|{";
94     for (unsigned i = 0; i < Links.size(); ++i) {
95       if (i) O << "|";
96       O << "<g" << i << ">";
97     }
98     O << "}";
99   }
100   O << "}\"];\n";
101
102   for (unsigned i = 0; i < Links.size(); ++i)
103     if (Links[i])
104       writeEdge(O, this, ":g", i, Links[i]);
105 }
106
107 void DSGraph::print(std::ostream &O) const {
108   O << "digraph DataStructures {\n"
109     << "\tnode [shape=Mrecord];\n"
110     << "\tedge [arrowtail=\"dot\"];\n"
111     << "\tsize=\"10,7.5\";\n"
112     << "\trotate=\"90\";\n"
113     << "\tlabel=\"Function\\ " << Func.getName() << "\";\n\n";
114
115   // Output all of the nodes...
116   for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
117     Nodes[i]->print(O, this);
118
119   O << "\n";
120
121   // Output the returned value pointer...
122   if (RetNode != 0) {
123     O << "\tNode0x1" << "[ plaintext=circle, label =\""
124       << escapeLabel("returning") << "\"];\n";
125     writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
126   }    
127
128   // Output all of the call nodes...
129   for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
130     const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
131     O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
132     for (unsigned j = 0, e = Call.size(); j != e; ++j) {
133       if (j) O << "|";
134       O << "<g" << j << ">";
135     }
136     O << "}}\"];\n";
137
138     for (unsigned j = 0, e = Call.size(); j != e; ++j)
139       if (Call[j])
140         writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
141   }
142
143
144   O << "}\n";
145 }
146
147
148
149
150 // print - Print out the analysis results...
151 void LocalDataStructures::print(std::ostream &O, Module *M) const {
152   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
153     if (!I->isExternal()) {
154       std::string Filename = "ds." + I->getName() + ".dot";
155       O << "Writing '" << Filename << "'...";
156       std::ofstream F(Filename.c_str());
157
158       if (F.good()) {
159         DSGraph &Graph = getDSGraph(*I);
160         Graph.print(F);
161         O << " [" << Graph.getGraphSize() << "]\n";
162       } else {
163         O << "  error opening file for writing!\n";
164       }
165     }
166 }