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