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