Print Mod/ref info
[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].first, M);
32     if (N->getTypeEntries()[i].second)
33       OS << "@" << N->getTypeEntries()[i].second;
34     OS << "\n";
35   }
36
37   if (N->NodeType & DSNode::ScalarNode) OS << "S";
38   if (N->NodeType & DSNode::AllocaNode) OS << "A";
39   if (N->NodeType & DSNode::NewNode   ) OS << "N";
40   if (N->NodeType & DSNode::GlobalNode) OS << "G";
41   if (N->NodeType & DSNode::Incomplete) OS << "I";
42   if (N->NodeType & DSNode::Modified  ) OS << "M";
43   if (N->NodeType & DSNode::Read      ) OS << "R";
44
45   for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
46     WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
47     OS << "\n";
48   }
49
50   if ((N->NodeType & DSNode::ScalarNode) && G) {
51     const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
52     for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
53            E = VM.end(); I != E; ++I)
54       if (I->second.getNode() == N) {
55         WriteAsOperand(OS, I->first, 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   static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
86     assert(Node == I.getNode() && "Iterator not for this node!");
87     return Node->getMergeMapLabel(I.getOffset());
88   }
89
90   /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
91   /// and the return node.
92   ///
93   static void addCustomGraphFeatures(const DSGraph *G,
94                                      GraphWriter<const DSGraph*> &GW) {
95     // Output the returned value pointer...
96     if (G->getRetNode().getNode() != 0) {
97       // Output the return node...
98       GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
99
100       // Add edge from return node to real destination
101       int RetEdgeDest = G->getRetNode().getOffset();
102       if (RetEdgeDest == 0) RetEdgeDest = -1;
103       GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
104                   RetEdgeDest, "arrowtail=tee,color=gray63");
105     }
106
107     // Output all of the call nodes...
108     const std::vector<std::vector<DSNodeHandle> > &FCs = G->getFunctionCalls();
109     for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
110       const std::vector<DSNodeHandle> &Call = FCs[i];
111       GW.emitSimpleNode(&Call, "shape=record", "call", Call.size());
112
113       for (unsigned j = 0, e = Call.size(); j != e; ++j)
114         if (Call[j].getNode()) {
115           int EdgeDest = Call[j].getOffset();
116           if (EdgeDest == 0) EdgeDest = -1;
117           GW.emitEdge(&Call, j, Call[j].getNode(), EdgeDest, "color=gray63");
118         }
119     }
120   }
121 };
122
123 void DSNode::print(std::ostream &O, const DSGraph *G) const {
124   GraphWriter<const DSGraph *> W(O, G);
125   W.writeNode(this);
126 }
127
128 void DSGraph::print(std::ostream &O) const {
129   WriteGraph(O, this, "DataStructures");
130 }
131
132 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
133   string Filename = GraphName + ".dot";
134   O << "Writing '" << Filename << "'...";
135   std::ofstream F(Filename.c_str());
136   
137   if (F.good()) {
138     print(F);
139     O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
140   } else {
141     O << "  error opening file for writing!\n";
142   }
143 }
144
145 template <typename Collection>
146 static void printCollection(const Collection &C, std::ostream &O,
147                             const Module *M, const string &Prefix) {
148   if (M == 0) {
149     O << "Null Module pointer, cannot continue!\n";
150     return;
151   }
152
153   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
154     if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
155       C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
156 }
157
158
159 // print - Print out the analysis results...
160 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
161   printCollection(*this, O, M, "ds.");
162 }
163
164 void BUDataStructures::print(std::ostream &O, const Module *M) const {
165   printCollection(*this, O, M, "bu.");
166 #if 0
167   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
168     if (!I->isExternal()) {
169       (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
170       break;
171     }
172 #endif
173 }
174
175 void TDDataStructures::print(std::ostream &O, const Module *M) const {
176   printCollection(*this, O, M, "td.");
177 #if 0
178   for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
179     if (!I->isExternal()) {
180       (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
181       break;
182     }
183 #endif
184 }