Start using the new function cloning header
[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 using std::string;
18
19 // OnlyPrintMain - The DataStructure printer exposes this option to allow
20 // printing of only the graph for "main".
21 //
22 namespace {
23   cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
24   Statistic<> MaxGraphSize   ("dsnode", "Maximum graph size");
25   Statistic<> NumFoldedNodes ("dsnode", "Number of folded nodes (in final graph)");
26 }
27
28
29 void DSNode::dump() const { print(std::cerr, 0); }
30
31 static string getCaption(const DSNode *N, const DSGraph *G) {
32   std::stringstream OS;
33   Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
34
35   if (N->isNodeCompletelyFolded())
36     OS << "FOLDED";
37   else {
38     WriteTypeSymbolic(OS, N->getType(), M);
39     if (N->isArray())
40       OS << " array";
41   }
42   if (N->NodeType) {
43     OS << ": ";
44     if (N->NodeType & DSNode::AllocaNode ) OS << "S";
45     if (N->NodeType & DSNode::HeapNode   ) OS << "H";
46     if (N->NodeType & DSNode::GlobalNode ) OS << "G";
47     if (N->NodeType & DSNode::UnknownNode) OS << "U";
48     if (N->NodeType & DSNode::Incomplete ) OS << "I";
49     if (N->NodeType & DSNode::Modified   ) OS << "M";
50     if (N->NodeType & DSNode::Read       ) OS << "R";
51     OS << "\n";
52   }
53
54   for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
55     WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
56     OS << "\n";
57   }
58
59   return OS.str();
60 }
61
62 template<>
63 struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
64   static std::string getGraphName(const DSGraph *G) {
65     if (G->hasFunction())
66       return "Function " + G->getFunction().getName();
67     else
68       return "Non-function graph";
69   }
70
71   static const char *getGraphProperties(const DSGraph *G) {
72     return "\tedge [arrowtail=\"dot\"];\n"
73            "\tsize=\"10,7.5\";\n"
74            "\trotate=\"90\";\n";
75   }
76
77   static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
78     return getCaption(Node, Graph);
79   }
80
81   static std::string getNodeAttributes(const DSNode *N) {
82     return "shape=Mrecord";//fontname=Courier";
83   }
84   
85   /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
86   /// and the return node.
87   ///
88   static void addCustomGraphFeatures(const DSGraph *G,
89                                      GraphWriter<const DSGraph*> &GW) {
90     // Add scalar nodes to the graph...
91     const std::map<Value*, DSNodeHandle> &VM = G->getScalarMap();
92     for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
93          I != VM.end(); ++I)
94       if (!isa<GlobalValue>(I->first)) {
95         std::stringstream OS;
96         WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
97         GW.emitSimpleNode(I->first, "", OS.str());
98         
99         // Add edge from return node to real destination
100         int EdgeDest = I->second.getOffset() >> DS::PointerShift;
101         if (EdgeDest == 0) EdgeDest = -1;
102         GW.emitEdge(I->first, -1, I->second.getNode(),
103                     EdgeDest, "arrowtail=tee,color=gray63");
104       }
105
106
107     // Output the returned value pointer...
108     if (G->getRetNode().getNode() != 0) {
109       // Output the return node...
110       GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
111
112       // Add edge from return node to real destination
113       int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
114       if (RetEdgeDest == 0) RetEdgeDest = -1;
115       GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
116                   RetEdgeDest, "arrowtail=tee,color=gray63");
117     }
118
119     // Output all of the call nodes...
120     const std::vector<DSCallSite> &FCs =
121       G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
122       : G->getFunctionCalls();
123     for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
124       const DSCallSite &Call = FCs[i];
125       GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
126
127       if (DSNode *N = Call.getRetVal().getNode()) {
128         int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
129         if (EdgeDest == 0) EdgeDest = -1;
130         GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
131       }
132       if (DSNode *N = Call.getCallee().getNode()) {
133         int EdgeDest = Call.getCallee().getOffset() >> DS::PointerShift;
134         if (EdgeDest == 0) EdgeDest = -1;
135         GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
136       }
137       for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
138         if (DSNode *N = Call.getPtrArg(j).getNode()) {
139           int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
140           if (EdgeDest == 0) EdgeDest = -1;
141           GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
142         }
143     }
144   }
145 };
146
147 void DSNode::print(std::ostream &O, const DSGraph *G) const {
148   GraphWriter<const DSGraph *> W(O, G);
149   W.writeNode(this);
150 }
151
152 void DSGraph::print(std::ostream &O) const {
153   WriteGraph(O, this, "DataStructures");
154 }
155
156 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
157   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 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 }