blob: 50d893366b7f3fd2449a4df4e7ec318d0d58e944 [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- 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"
Chris Lattnerc5f21de2002-10-02 22:14:38 +00008#include "llvm/Analysis/DSGraph.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +00009#include "llvm/Analysis/DSGraphTraits.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000010#include "llvm/Module.h"
11#include "llvm/Assembly/Writer.h"
Chris Lattnerdadd49b2002-07-31 17:15:40 +000012#include "Support/CommandLine.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000013#include "Support/GraphWriter.h"
Chris Lattner49a1ed02002-11-18 21:42:45 +000014#include "Support/Statistic.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000015#include <fstream>
16#include <sstream>
17
Chris Lattner55c10582002-10-03 20:38:41 +000018// OnlyPrintMain - The DataStructure printer exposes this option to allow
19// printing of only the graph for "main".
20//
Chris Lattner49a1ed02002-11-18 21:42:45 +000021namespace {
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)");
25}
Chris Lattner55c10582002-10-03 20:38:41 +000026
27
Chris Lattnerc68c31b2002-07-10 22:38:08 +000028void DSNode::dump() const { print(std::cerr, 0); }
29
Chris Lattnerb3416bc2003-02-01 04:01:21 +000030static std::string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000031 std::stringstream OS;
Chris Lattner714752f2003-02-04 00:03:18 +000032 Module *M = G && G->hasFunction() ? G->getFunction().getParent() : 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000033
Chris Lattner08db7192002-11-06 06:20:27 +000034 if (N->isNodeCompletelyFolded())
35 OS << "FOLDED";
36 else {
Chris Lattner49a1ed02002-11-18 21:42:45 +000037 WriteTypeSymbolic(OS, N->getType(), M);
38 if (N->isArray())
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000039 OS << " array";
Chris Lattner08db7192002-11-06 06:20:27 +000040 }
Chris Lattnerbd92b732003-06-19 21:15:11 +000041 if (unsigned NodeType = N->getNodeFlags()) {
Chris Lattner08db7192002-11-06 06:20:27 +000042 OS << ": ";
Chris Lattnerbd92b732003-06-19 21:15:11 +000043 if (NodeType & DSNode::AllocaNode ) OS << "S";
44 if (NodeType & DSNode::HeapNode ) OS << "H";
45 if (NodeType & DSNode::GlobalNode ) OS << "G";
46 if (NodeType & DSNode::UnknownNode) OS << "U";
47 if (NodeType & DSNode::Incomplete ) OS << "I";
48 if (NodeType & DSNode::Modified ) OS << "M";
49 if (NodeType & DSNode::Read ) OS << "R";
Chris Lattnerbd92b732003-06-19 21:15:11 +000050#ifndef NDEBUG
51 if (NodeType & DSNode::DEAD ) OS << "<dead>";
52#endif
Chris Lattner76d5b482002-07-11 20:33:32 +000053 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000054 }
55
Chris Lattnerfccd06f2002-10-01 22:33:50 +000056 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
57 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
58 OS << "\n";
59 }
60
Chris Lattnerc68c31b2002-07-10 22:38:08 +000061 return OS.str();
62}
63
Chris Lattnerf6c52db2002-10-13 19:31:57 +000064template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000065struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
66 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000067 if (G->hasFunction())
68 return "Function " + G->getFunction().getName();
69 else
Chris Lattner2cec1d32003-02-11 19:27:27 +000070 return "Global graph";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000071 }
72
Chris Lattnere17a4e82002-10-17 01:02:46 +000073 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattner352a6fa2003-02-13 20:14:40 +000074 return "\tsize=\"10,7.5\";\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000075 "\trotate=\"90\";\n";
76 }
77
Chris Lattnere17a4e82002-10-17 01:02:46 +000078 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000079 return getCaption(Node, Graph);
80 }
81
Chris Lattnere17a4e82002-10-17 01:02:46 +000082 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000083 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000084 }
85
Chris Lattnereb265cd2002-10-16 02:04:36 +000086 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
87 /// and the return node.
88 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000089 static void addCustomGraphFeatures(const DSGraph *G,
90 GraphWriter<const DSGraph*> &GW) {
Chris Lattner714752f2003-02-04 00:03:18 +000091 Module *CurMod = G->hasFunction() ? G->getFunction().getParent() : 0;
92
Chris Lattner92673292002-11-02 00:13:20 +000093 // Add scalar nodes to the graph...
Chris Lattner41c04f72003-02-01 04:52:08 +000094 const hash_map<Value*, DSNodeHandle> &VM = G->getScalarMap();
95 for (hash_map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
Chris Lattner92673292002-11-02 00:13:20 +000096 I != VM.end(); ++I)
97 if (!isa<GlobalValue>(I->first)) {
98 std::stringstream OS;
Chris Lattner714752f2003-02-04 00:03:18 +000099 WriteAsOperand(OS, I->first, false, true, CurMod);
Chris Lattner49a1ed02002-11-18 21:42:45 +0000100 GW.emitSimpleNode(I->first, "", OS.str());
Chris Lattner92673292002-11-02 00:13:20 +0000101
102 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000103 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000104 if (EdgeDest == 0) EdgeDest = -1;
105 GW.emitEdge(I->first, -1, I->second.getNode(),
106 EdgeDest, "arrowtail=tee,color=gray63");
107 }
108
109
Chris Lattnereb265cd2002-10-16 02:04:36 +0000110 // Output the returned value pointer...
111 if (G->getRetNode().getNode() != 0) {
112 // Output the return node...
113 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
114
115 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000116 int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
Chris Lattnereb265cd2002-10-16 02:04:36 +0000117 if (RetEdgeDest == 0) RetEdgeDest = -1;
118 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
119 RetEdgeDest, "arrowtail=tee,color=gray63");
120 }
Chris Lattner962ee452002-10-16 20:16:16 +0000121
122 // Output all of the call nodes...
Chris Lattner4f7815f2002-11-10 06:53:59 +0000123 const std::vector<DSCallSite> &FCs =
124 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
125 : G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000126 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000127 const DSCallSite &Call = FCs[i];
Chris Lattner923fc052003-02-05 21:59:58 +0000128 std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
129 EdgeSourceCaptions[0] = "r";
130 if (Call.isDirectCall())
131 EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
Chris Lattnerf1c28382003-02-14 20:25:47 +0000132 else
133 EdgeSourceCaptions[1] = "f";
Chris Lattner923fc052003-02-05 21:59:58 +0000134
135 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
136 &EdgeSourceCaptions);
Chris Lattner962ee452002-10-16 20:16:16 +0000137
Chris Lattner482b6512002-10-21 13:47:57 +0000138 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000139 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000140 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000141 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000142 }
Chris Lattner923fc052003-02-05 21:59:58 +0000143
144 // Print out the callee...
145 if (Call.isIndirectCall()) {
146 DSNode *N = Call.getCalleeNode();
147 assert(N && "Null call site callee node!");
Chris Lattner352a6fa2003-02-13 20:14:40 +0000148 GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000149 }
Chris Lattner923fc052003-02-05 21:59:58 +0000150
Chris Lattner0969c502002-10-21 02:08:03 +0000151 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
152 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000153 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000154 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000155 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner962ee452002-10-16 20:16:16 +0000156 }
157 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000158 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000159};
160
Chris Lattnere17a4e82002-10-17 01:02:46 +0000161void DSNode::print(std::ostream &O, const DSGraph *G) const {
162 GraphWriter<const DSGraph *> W(O, G);
163 W.writeNode(this);
164}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000165
Chris Lattnere17a4e82002-10-17 01:02:46 +0000166void DSGraph::print(std::ostream &O) const {
167 WriteGraph(O, this, "DataStructures");
168}
169
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000170void DSGraph::writeGraphToFile(std::ostream &O,
171 const std::string &GraphName) const {
172 std::string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000173 O << "Writing '" << Filename << "'...";
174 std::ofstream F(Filename.c_str());
175
176 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000177 print(F);
Chris Lattner60525942002-11-11 00:01:02 +0000178 unsigned NumCalls = shouldPrintAuxCalls() ?
179 getAuxFunctionCalls().size() : getFunctionCalls().size();
180 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000181 } else {
182 O << " error opening file for writing!\n";
183 }
184}
185
Chris Lattnere79eaa92003-02-10 18:17:07 +0000186/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
187/// then cleanup. For use from the debugger.
188///
189void DSGraph::viewGraph() const {
190 std::ofstream F("/tmp/tempgraph.dot");
191 if (!F.good()) {
192 std::cerr << "Error opening '/tmp/tempgraph.dot' for temporary graph!\n";
193 return;
194 }
195 print(F);
Chris Lattner2cec1d32003-02-11 19:27:27 +0000196 F.close();
Chris Lattnere79eaa92003-02-10 18:17:07 +0000197 if (system("dot -Tps /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
198 std::cerr << "Error running dot: 'dot' not in path?\n";
199 system("gv /tmp/tempgraph.ps");
200 system("rm /tmp/tempgraph.dot /tmp/tempgraph.ps");
201}
202
203
Chris Lattner0d9bab82002-07-18 00:12:30 +0000204template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000205static void printCollection(const Collection &C, std::ostream &O,
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000206 const Module *M, const std::string &Prefix) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000207 if (M == 0) {
208 O << "Null Module pointer, cannot continue!\n";
209 return;
210 }
211
Chris Lattner14212332002-11-07 02:18:46 +0000212 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000213 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner4f7815f2002-11-10 06:53:59 +0000214 if (C.hasGraph(*I)) {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000215 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner14212332002-11-07 02:18:46 +0000216 TotalNumNodes += Gr.getGraphSize();
Chris Lattner4f7815f2002-11-10 06:53:59 +0000217 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
218 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
219
220 TotalCallNodes += NumCalls;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000221 if (I->getName() == "main" || !OnlyPrintMain)
222 Gr.writeGraphToFile(O, Prefix+I->getName());
223 else {
224 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
Chris Lattner4f7815f2002-11-10 06:53:59 +0000225 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
Chris Lattner95a80ad2002-11-07 01:54:44 +0000226 }
Chris Lattner49a1ed02002-11-18 21:42:45 +0000227
228 if (MaxGraphSize < Gr.getNodes().size())
229 MaxGraphSize = Gr.getNodes().size();
230 for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
231 if (Gr.getNodes()[i]->isNodeCompletelyFolded())
232 ++NumFoldedNodes;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000233 }
Chris Lattner14212332002-11-07 02:18:46 +0000234
Chris Lattneraa0b4682002-11-09 21:12:07 +0000235 DSGraph &GG = C.getGlobalsGraph();
236 TotalNumNodes += GG.getGraphSize();
237 TotalCallNodes += GG.getFunctionCalls().size();
Chris Lattnerf76e7542002-11-09 21:40:58 +0000238 if (!OnlyPrintMain) {
Chris Lattneraa0b4682002-11-09 21:12:07 +0000239 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
240 } else {
241 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
242 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
243 }
244
Chris Lattner14212332002-11-07 02:18:46 +0000245 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000246 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000247}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000248
249
250// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000251void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000252 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000253}
254
Chris Lattner97f51a32002-07-27 01:12:15 +0000255void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000256 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000257}
258
259void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000260 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000261}