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