- Make DSCallSite not inherit from std::vector. Renamed methods slightly.
[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   for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
31     WriteTypeSymbolic(OS, N->getTypeEntries()[i].Ty, M);
32     if (N->getTypeEntries()[i].Offset)
33       OS << "@" << N->getTypeEntries()[i].Offset;
34     if (N->getTypeEntries()[i].isArray)
35       OS << " array";
36     OS << "\n";
37   }
38
39   if (N->NodeType & DSNode::ScalarNode) OS << "S";
40   if (N->NodeType & DSNode::AllocaNode) OS << "A";
41   if (N->NodeType & DSNode::NewNode   ) OS << "N";
42   if (N->NodeType & DSNode::GlobalNode) OS << "G";
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
47   for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
48     WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
49     OS << "\n";
50   }
51
52   if ((N->NodeType & DSNode::ScalarNode) && G) {
53     const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
54     for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
55            E = VM.end(); I != E; ++I)
56       if (I->second.getNode() == N) {
57         WriteAsOperand(OS, I->first, false, true, M);
58         OS << "\n";
59       }
60   }
61   return OS.str();
62 }
63
64 template<>
65 struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
66   static std::string getGraphName(const DSGraph *G) {
67     if (G->hasFunction())
68       return "Function " + G->getFunction().getName();
69     else
70       return "Non-function graph";
71   }
72
73   static const char *getGraphProperties(const DSGraph *G) {
74     return "\tedge [arrowtail=\"dot\"];\n"
75            "\tsize=\"10,7.5\";\n"
76            "\trotate=\"90\";\n";
77   }
78
79   static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
80     return getCaption(Node, Graph);
81   }
82
83   static std::string getNodeAttributes(const DSNode *N) {
84     return "shape=Mrecord";//fontname=Courier";
85   }
86   
87   static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
88     assert(Node == I.getNode() && "Iterator not for this node!");
89     return Node->getMergeMapLabel(I.getOffset());
90   }
91
92   /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
93   /// and the return node.
94   ///
95   static void addCustomGraphFeatures(const DSGraph *G,
96                                      GraphWriter<const DSGraph*> &GW) {
97     // Output the returned value pointer...
98     if (G->getRetNode().getNode() != 0) {
99       // Output the return node...
100       GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
101
102       // Add edge from return node to real destination
103       int RetEdgeDest = G->getRetNode().getOffset();
104       if (RetEdgeDest == 0) RetEdgeDest = -1;
105       GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
106                   RetEdgeDest, "arrowtail=tee,color=gray63");
107     }
108
109     // Output all of the call nodes...
110     const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
111     for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
112       const DSCallSite &Call = FCs[i];
113       GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
114
115       for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
116         if (DSNode *N = Call.getPtrArg(j).getNode()) {
117           int EdgeDest = Call.getPtrArg(j).getOffset();
118           if (EdgeDest == 0) EdgeDest = -1;
119           GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
120         }
121     }
122   }
123 };
124
125 void DSNode::print(std::ostream &O, const DSGraph *G) const {
126   GraphWriter<const DSGraph *> W(O, G);
127   W.writeNode(this);
128 }
129
130 void DSGraph::print(std::ostream &O) const {
131   WriteGraph(O, this, "DataStructures");
132 }
133
134 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
135   string Filename = GraphName + ".dot";
136   O << "Writing '" << Filename << "'...";
137   std::ofstream F(Filename.c_str());
138   
139   if (F.good()) {
140     print(F);
141     O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
142   } else {
143     O << "  error opening file for writing!\n";
144   }
145 }
146
147 template <typename Collection>
148 static void printCollection(const Collection &C, std::ostream &O,
149                             const Module *M, const string &Prefix) {
150   if (M == 0) {
151     O << "Null Module pointer, cannot continue!\n";
152     return;
153   }
154
155   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
156     if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
157       C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
158 }
159
160
161 // print - Print out the analysis results...
162 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
163   printCollection(*this, O, M, "ds.");
164 }
165
166 void BUDataStructures::print(std::ostream &O, const Module *M) const {
167   printCollection(*this, O, M, "bu.");
168 #if 0
169   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
170     if (!I->isExternal()) {
171       (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
172       break;
173     }
174 #endif
175 }
176
177 void TDDataStructures::print(std::ostream &O, const Module *M) const {
178   printCollection(*this, O, M, "td.");
179 #if 0
180   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
181     if (!I->isExternal()) {
182       (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
183       break;
184     }
185 #endif
186 }