1 //===- Printer.cpp - Code for printing data structure graphs nicely -------===//
3 // This file implements the 'dot' graph printer.
5 //===----------------------------------------------------------------------===//
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 "Support/Statistic.h"
18 // OnlyPrintMain - The DataStructure printer exposes this option to allow
19 // printing of only the graph for "main".
22 cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
23 Statistic<> MaxGraphSize ("dsnode", "Maximum graph size");
24 Statistic<> NumFoldedNodes ("dsnode", "Number of folded nodes (in final graph)");
28 void DSNode::dump() const { print(std::cerr, 0); }
30 static std::string getCaption(const DSNode *N, const DSGraph *G) {
32 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
34 if (N->isNodeCompletelyFolded())
37 WriteTypeSymbolic(OS, N->getType(), M);
43 if (N->NodeType & DSNode::AllocaNode ) OS << "S";
44 if (N->NodeType & DSNode::HeapNode ) OS << "H";
45 if (N->NodeType & DSNode::GlobalNode ) OS << "G";
46 if (N->NodeType & DSNode::UnknownNode) OS << "U";
47 if (N->NodeType & DSNode::Incomplete ) OS << "I";
48 if (N->NodeType & DSNode::Modified ) OS << "M";
49 if (N->NodeType & DSNode::Read ) OS << "R";
53 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
54 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
62 struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
63 static std::string getGraphName(const DSGraph *G) {
65 return "Function " + G->getFunction().getName();
67 return "Non-function graph";
70 static const char *getGraphProperties(const DSGraph *G) {
71 return "\tedge [arrowtail=\"dot\"];\n"
72 "\tsize=\"10,7.5\";\n"
76 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
77 return getCaption(Node, Graph);
80 static std::string getNodeAttributes(const DSNode *N) {
81 return "shape=Mrecord";//fontname=Courier";
84 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
85 /// and the return node.
87 static void addCustomGraphFeatures(const DSGraph *G,
88 GraphWriter<const DSGraph*> &GW) {
89 // Add scalar nodes to the graph...
90 const hash_map<Value*, DSNodeHandle> &VM = G->getScalarMap();
91 for (hash_map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
93 if (!isa<GlobalValue>(I->first)) {
95 WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
96 GW.emitSimpleNode(I->first, "", OS.str());
98 // Add edge from return node to real destination
99 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
100 if (EdgeDest == 0) EdgeDest = -1;
101 GW.emitEdge(I->first, -1, I->second.getNode(),
102 EdgeDest, "arrowtail=tee,color=gray63");
106 // Output the returned value pointer...
107 if (G->getRetNode().getNode() != 0) {
108 // Output the return node...
109 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
111 // Add edge from return node to real destination
112 int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
113 if (RetEdgeDest == 0) RetEdgeDest = -1;
114 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
115 RetEdgeDest, "arrowtail=tee,color=gray63");
118 // Output all of the call nodes...
119 const std::vector<DSCallSite> &FCs =
120 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
121 : G->getFunctionCalls();
122 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
123 const DSCallSite &Call = FCs[i];
124 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
126 if (DSNode *N = Call.getRetVal().getNode()) {
127 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
128 if (EdgeDest == 0) EdgeDest = -1;
129 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
131 if (DSNode *N = Call.getCallee().getNode()) {
132 int EdgeDest = Call.getCallee().getOffset() >> DS::PointerShift;
133 if (EdgeDest == 0) EdgeDest = -1;
134 GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
136 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
137 if (DSNode *N = Call.getPtrArg(j).getNode()) {
138 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
139 if (EdgeDest == 0) EdgeDest = -1;
140 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
146 void DSNode::print(std::ostream &O, const DSGraph *G) const {
147 GraphWriter<const DSGraph *> W(O, G);
151 void DSGraph::print(std::ostream &O) const {
152 WriteGraph(O, this, "DataStructures");
155 void DSGraph::writeGraphToFile(std::ostream &O,
156 const std::string &GraphName) const {
157 std::string Filename = GraphName + ".dot";
158 O << "Writing '" << Filename << "'...";
159 std::ofstream F(Filename.c_str());
163 unsigned NumCalls = shouldPrintAuxCalls() ?
164 getAuxFunctionCalls().size() : getFunctionCalls().size();
165 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
167 O << " error opening file for writing!\n";
171 template <typename Collection>
172 static void printCollection(const Collection &C, std::ostream &O,
173 const Module *M, const std::string &Prefix) {
175 O << "Null Module pointer, cannot continue!\n";
179 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
180 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
181 if (C.hasGraph(*I)) {
182 DSGraph &Gr = C.getDSGraph((Function&)*I);
183 TotalNumNodes += Gr.getGraphSize();
184 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
185 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
187 TotalCallNodes += NumCalls;
188 if (I->getName() == "main" || !OnlyPrintMain)
189 Gr.writeGraphToFile(O, Prefix+I->getName());
191 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
192 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
195 if (MaxGraphSize < Gr.getNodes().size())
196 MaxGraphSize = Gr.getNodes().size();
197 for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
198 if (Gr.getNodes()[i]->isNodeCompletelyFolded())
202 DSGraph &GG = C.getGlobalsGraph();
203 TotalNumNodes += GG.getGraphSize();
204 TotalCallNodes += GG.getFunctionCalls().size();
205 if (!OnlyPrintMain) {
206 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
208 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
209 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
212 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
213 << "] nodes total" << std::endl;
217 // print - Print out the analysis results...
218 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
219 printCollection(*this, O, M, "ds.");
222 void BUDataStructures::print(std::ostream &O, const Module *M) const {
223 printCollection(*this, O, M, "bu.");
226 void TDDataStructures::print(std::ostream &O, const Module *M) const {
227 printCollection(*this, O, M, "td.");