baff12bac028ed0494289d94656c677853570071
[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 "Support/Statistic.h"
15 #include <fstream>
16 #include <sstream>
17
18 // OnlyPrintMain - The DataStructure printer exposes this option to allow
19 // printing of only the graph for "main".
20 //
21 namespace {
22   cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
23   Statistic<> MaxGraphSize   ("dsnode", "Maximum graph size");
24   Statistic<> NumFoldedNodes ("dsnode", "Number of folded nodes (in final graph)");
25 }
26
27
28 void DSNode::dump() const { print(std::cerr, 0); }
29
30 static std::string getCaption(const DSNode *N, const DSGraph *G) {
31   std::stringstream OS;
32   Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
33
34   if (N->isNodeCompletelyFolded())
35     OS << "FOLDED";
36   else {
37     WriteTypeSymbolic(OS, N->getType(), M);
38     if (N->isArray())
39       OS << " array";
40   }
41   if (N->NodeType) {
42     OS << ": ";
43     if (N->NodeType & DSNode::AllocaNode ) OS << "S";
44     if (N->NodeType & DSNode::HeapNode   ) OS << "H";
45     if (N->NodeType & DSNode::GlobalNode ) OS << "G";
46     if (N->NodeType & DSNode::UnknownNode) OS << "U";
47     if (N->NodeType & DSNode::Incomplete ) OS << "I";
48     if (N->NodeType & DSNode::Modified   ) OS << "M";
49     if (N->NodeType & DSNode::Read       ) OS << "R";
50     OS << "\n";
51   }
52
53   for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
54     WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
55     OS << "\n";
56   }
57
58   return OS.str();
59 }
60
61 template<>
62 struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
63   static std::string getGraphName(const DSGraph *G) {
64     if (G->hasFunction())
65       return "Function " + G->getFunction().getName();
66     else
67       return "Non-function graph";
68   }
69
70   static const char *getGraphProperties(const DSGraph *G) {
71     return "\tedge [arrowtail=\"dot\"];\n"
72            "\tsize=\"10,7.5\";\n"
73            "\trotate=\"90\";\n";
74   }
75
76   static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
77     return getCaption(Node, Graph);
78   }
79
80   static std::string getNodeAttributes(const DSNode *N) {
81     return "shape=Mrecord";//fontname=Courier";
82   }
83   
84   /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
85   /// and the return node.
86   ///
87   static void addCustomGraphFeatures(const DSGraph *G,
88                                      GraphWriter<const DSGraph*> &GW) {
89     // Add scalar nodes to the graph...
90     const hash_map<Value*, DSNodeHandle> &VM = G->getScalarMap();
91     for (hash_map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
92          I != VM.end(); ++I)
93       if (!isa<GlobalValue>(I->first)) {
94         std::stringstream OS;
95         WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
96         GW.emitSimpleNode(I->first, "", OS.str());
97         
98         // Add edge from return node to real destination
99         int EdgeDest = I->second.getOffset() >> DS::PointerShift;
100         if (EdgeDest == 0) EdgeDest = -1;
101         GW.emitEdge(I->first, -1, I->second.getNode(),
102                     EdgeDest, "arrowtail=tee,color=gray63");
103       }
104
105
106     // Output the returned value pointer...
107     if (G->getRetNode().getNode() != 0) {
108       // Output the return node...
109       GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
110
111       // Add edge from return node to real destination
112       int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
113       if (RetEdgeDest == 0) RetEdgeDest = -1;
114       GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
115                   RetEdgeDest, "arrowtail=tee,color=gray63");
116     }
117
118     // Output all of the call nodes...
119     const std::vector<DSCallSite> &FCs =
120       G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
121       : G->getFunctionCalls();
122     for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
123       const DSCallSite &Call = FCs[i];
124       GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
125
126       if (DSNode *N = Call.getRetVal().getNode()) {
127         int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
128         if (EdgeDest == 0) EdgeDest = -1;
129         GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
130       }
131       if (DSNode *N = Call.getCallee().getNode()) {
132         int EdgeDest = Call.getCallee().getOffset() >> DS::PointerShift;
133         if (EdgeDest == 0) EdgeDest = -1;
134         GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
135       }
136       for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
137         if (DSNode *N = Call.getPtrArg(j).getNode()) {
138           int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
139           if (EdgeDest == 0) EdgeDest = -1;
140           GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
141         }
142     }
143   }
144 };
145
146 void DSNode::print(std::ostream &O, const DSGraph *G) const {
147   GraphWriter<const DSGraph *> W(O, G);
148   W.writeNode(this);
149 }
150
151 void DSGraph::print(std::ostream &O) const {
152   WriteGraph(O, this, "DataStructures");
153 }
154
155 void DSGraph::writeGraphToFile(std::ostream &O,
156                                const std::string &GraphName) const {
157   std::string Filename = GraphName + ".dot";
158   O << "Writing '" << Filename << "'...";
159   std::ofstream F(Filename.c_str());
160   
161   if (F.good()) {
162     print(F);
163     unsigned NumCalls = shouldPrintAuxCalls() ?
164       getAuxFunctionCalls().size() : getFunctionCalls().size();
165     O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
166   } else {
167     O << "  error opening file for writing!\n";
168   }
169 }
170
171 template <typename Collection>
172 static void printCollection(const Collection &C, std::ostream &O,
173                             const Module *M, const std::string &Prefix) {
174   if (M == 0) {
175     O << "Null Module pointer, cannot continue!\n";
176     return;
177   }
178
179   unsigned TotalNumNodes = 0, TotalCallNodes = 0;
180   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
181     if (C.hasGraph(*I)) {
182       DSGraph &Gr = C.getDSGraph((Function&)*I);
183       TotalNumNodes += Gr.getGraphSize();
184       unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
185         Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
186
187       TotalCallNodes += NumCalls;
188       if (I->getName() == "main" || !OnlyPrintMain)
189         Gr.writeGraphToFile(O, Prefix+I->getName());
190       else {
191         O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
192           << Gr.getGraphSize() << "+" << NumCalls << "]\n";
193       }
194
195       if (MaxGraphSize < Gr.getNodes().size())
196         MaxGraphSize = Gr.getNodes().size();
197       for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
198         if (Gr.getNodes()[i]->isNodeCompletelyFolded())
199           ++NumFoldedNodes;
200     }
201
202   DSGraph &GG = C.getGlobalsGraph();
203   TotalNumNodes  += GG.getGraphSize();
204   TotalCallNodes += GG.getFunctionCalls().size();
205   if (!OnlyPrintMain) {
206     GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
207   } else {
208     O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
209       << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
210   }
211
212   O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes 
213     << "] nodes total" << std::endl;
214 }
215
216
217 // print - Print out the analysis results...
218 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
219   printCollection(*this, O, M, "ds.");
220 }
221
222 void BUDataStructures::print(std::ostream &O, const Module *M) const {
223   printCollection(*this, O, M, "bu.");
224 }
225
226 void TDDataStructures::print(std::ostream &O, const Module *M) const {
227   printCollection(*this, O, M, "td.");
228 }