blob: 87fc1f76cc9344dcbbc9e2d59f4d99b4a17b0fc7 [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"
Chris Lattnerd9dad2c2003-07-01 16:27:32 +000011#include "llvm/Constants.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000012#include "llvm/Assembly/Writer.h"
Chris Lattnerdadd49b2002-07-31 17:15:40 +000013#include "Support/CommandLine.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000014#include "Support/GraphWriter.h"
Chris Lattner49a1ed02002-11-18 21:42:45 +000015#include "Support/Statistic.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000016#include <fstream>
17#include <sstream>
18
Chris Lattner55c10582002-10-03 20:38:41 +000019// OnlyPrintMain - The DataStructure printer exposes this option to allow
20// printing of only the graph for "main".
21//
Chris Lattner49a1ed02002-11-18 21:42:45 +000022namespace {
23 cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
24 Statistic<> MaxGraphSize ("dsnode", "Maximum graph size");
25 Statistic<> NumFoldedNodes ("dsnode", "Number of folded nodes (in final graph)");
26}
Chris Lattner55c10582002-10-03 20:38:41 +000027
28
Chris Lattnerc68c31b2002-07-10 22:38:08 +000029void DSNode::dump() const { print(std::cerr, 0); }
30
Chris Lattnerb3416bc2003-02-01 04:01:21 +000031static std::string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000032 std::stringstream OS;
Chris Lattner5a540632003-06-30 03:15:25 +000033 Module *M = 0;
34 // Get the module from ONE of the functions in the graph it is available.
35 if (G && !G->getReturnNodes().empty())
36 M = G->getReturnNodes().begin()->first->getParent();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000037
Chris Lattner08db7192002-11-06 06:20:27 +000038 if (N->isNodeCompletelyFolded())
Chris Lattner63899fa2003-07-02 04:39:27 +000039 OS << "COLLAPSED";
Chris Lattner08db7192002-11-06 06:20:27 +000040 else {
Chris Lattner49a1ed02002-11-18 21:42:45 +000041 WriteTypeSymbolic(OS, N->getType(), M);
42 if (N->isArray())
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000043 OS << " array";
Chris Lattner08db7192002-11-06 06:20:27 +000044 }
Chris Lattnerbd92b732003-06-19 21:15:11 +000045 if (unsigned NodeType = N->getNodeFlags()) {
Chris Lattner08db7192002-11-06 06:20:27 +000046 OS << ": ";
Chris Lattnerbd92b732003-06-19 21:15:11 +000047 if (NodeType & DSNode::AllocaNode ) OS << "S";
48 if (NodeType & DSNode::HeapNode ) OS << "H";
49 if (NodeType & DSNode::GlobalNode ) OS << "G";
50 if (NodeType & DSNode::UnknownNode) OS << "U";
51 if (NodeType & DSNode::Incomplete ) OS << "I";
52 if (NodeType & DSNode::Modified ) OS << "M";
53 if (NodeType & DSNode::Read ) OS << "R";
Chris Lattnerbd92b732003-06-19 21:15:11 +000054#ifndef NDEBUG
55 if (NodeType & DSNode::DEAD ) OS << "<dead>";
56#endif
Chris Lattner76d5b482002-07-11 20:33:32 +000057 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000058 }
59
Chris Lattnerfccd06f2002-10-01 22:33:50 +000060 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
61 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
62 OS << "\n";
63 }
64
Chris Lattnerc68c31b2002-07-10 22:38:08 +000065 return OS.str();
66}
67
Chris Lattnerf6c52db2002-10-13 19:31:57 +000068template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000069struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
70 static std::string getGraphName(const DSGraph *G) {
Chris Lattner5a540632003-06-30 03:15:25 +000071 switch (G->getReturnNodes().size()) {
Chris Lattner6681e982003-06-30 05:57:39 +000072 case 0: return G->getFunctionNames();
73 case 1: return "Function " + G->getFunctionNames();
74 default: return "Functions: " + G->getFunctionNames();
Chris Lattner5a540632003-06-30 03:15:25 +000075 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +000076 }
77
Chris Lattnere17a4e82002-10-17 01:02:46 +000078 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattner352a6fa2003-02-13 20:14:40 +000079 return "\tsize=\"10,7.5\";\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000080 "\trotate=\"90\";\n";
81 }
82
Chris Lattnere17a4e82002-10-17 01:02:46 +000083 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000084 return getCaption(Node, Graph);
85 }
86
Chris Lattnere17a4e82002-10-17 01:02:46 +000087 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000088 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000089 }
90
Chris Lattnereb265cd2002-10-16 02:04:36 +000091 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
92 /// and the return node.
93 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000094 static void addCustomGraphFeatures(const DSGraph *G,
95 GraphWriter<const DSGraph*> &GW) {
Chris Lattner5a540632003-06-30 03:15:25 +000096 Module *CurMod = 0;
97 if (!G->getReturnNodes().empty())
98 CurMod = G->getReturnNodes().begin()->first->getParent();
Chris Lattner714752f2003-02-04 00:03:18 +000099
Chris Lattner92673292002-11-02 00:13:20 +0000100 // Add scalar nodes to the graph...
Chris Lattner5a540632003-06-30 03:15:25 +0000101 const DSGraph::ScalarMapTy &VM = G->getScalarMap();
102 for (DSGraph::ScalarMapTy::const_iterator I = VM.begin(); I != VM.end();++I)
Chris Lattnerd9dad2c2003-07-01 16:27:32 +0000103 if (!isa<GlobalValue>(I->first) && !isa<ConstantPointerRef>(I->first)) {
Chris Lattner92673292002-11-02 00:13:20 +0000104 std::stringstream OS;
Chris Lattner714752f2003-02-04 00:03:18 +0000105 WriteAsOperand(OS, I->first, false, true, CurMod);
Chris Lattner49a1ed02002-11-18 21:42:45 +0000106 GW.emitSimpleNode(I->first, "", OS.str());
Chris Lattner92673292002-11-02 00:13:20 +0000107
108 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000109 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000110 if (EdgeDest == 0) EdgeDest = -1;
111 GW.emitEdge(I->first, -1, I->second.getNode(),
112 EdgeDest, "arrowtail=tee,color=gray63");
113 }
114
115
Chris Lattnereb265cd2002-10-16 02:04:36 +0000116 // Output the returned value pointer...
Chris Lattner5a540632003-06-30 03:15:25 +0000117 const DSGraph::ReturnNodesTy &RetNodes = G->getReturnNodes();
118 for (DSGraph::ReturnNodesTy::const_iterator I = RetNodes.begin(),
119 E = RetNodes.end(); I != E; ++I)
120 if (I->second.getNode()) {
121 std::string Label;
122 if (RetNodes.size() == 1)
123 Label = "returning";
124 else
125 Label = I->first->getName() + " ret node";
126 // Output the return node...
127 GW.emitSimpleNode((void*)1, "plaintext=circle", Label);
Chris Lattnereb265cd2002-10-16 02:04:36 +0000128
Chris Lattner5a540632003-06-30 03:15:25 +0000129 // Add edge from return node to real destination
130 int RetEdgeDest = I->second.getOffset() >> DS::PointerShift;;
131 if (RetEdgeDest == 0) RetEdgeDest = -1;
132 GW.emitEdge((void*)1, -1, I->second.getNode(),
133 RetEdgeDest, "arrowtail=tee,color=gray63");
134 }
Chris Lattner962ee452002-10-16 20:16:16 +0000135
136 // Output all of the call nodes...
Chris Lattner4f7815f2002-11-10 06:53:59 +0000137 const std::vector<DSCallSite> &FCs =
138 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
139 : G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000140 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000141 const DSCallSite &Call = FCs[i];
Chris Lattner923fc052003-02-05 21:59:58 +0000142 std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
143 EdgeSourceCaptions[0] = "r";
144 if (Call.isDirectCall())
145 EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
Chris Lattnerf1c28382003-02-14 20:25:47 +0000146 else
147 EdgeSourceCaptions[1] = "f";
Chris Lattner923fc052003-02-05 21:59:58 +0000148
149 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
150 &EdgeSourceCaptions);
Chris Lattner962ee452002-10-16 20:16:16 +0000151
Chris Lattner482b6512002-10-21 13:47:57 +0000152 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000153 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000154 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000155 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000156 }
Chris Lattner923fc052003-02-05 21:59:58 +0000157
158 // Print out the callee...
159 if (Call.isIndirectCall()) {
160 DSNode *N = Call.getCalleeNode();
161 assert(N && "Null call site callee node!");
Chris Lattner352a6fa2003-02-13 20:14:40 +0000162 GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000163 }
Chris Lattner923fc052003-02-05 21:59:58 +0000164
Chris Lattner0969c502002-10-21 02:08:03 +0000165 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
166 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000167 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000168 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000169 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner962ee452002-10-16 20:16:16 +0000170 }
171 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000172 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000173};
174
Chris Lattnere17a4e82002-10-17 01:02:46 +0000175void DSNode::print(std::ostream &O, const DSGraph *G) const {
176 GraphWriter<const DSGraph *> W(O, G);
177 W.writeNode(this);
178}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000179
Chris Lattnere17a4e82002-10-17 01:02:46 +0000180void DSGraph::print(std::ostream &O) const {
181 WriteGraph(O, this, "DataStructures");
182}
183
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000184void DSGraph::writeGraphToFile(std::ostream &O,
185 const std::string &GraphName) const {
186 std::string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000187 O << "Writing '" << Filename << "'...";
188 std::ofstream F(Filename.c_str());
189
190 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000191 print(F);
Chris Lattner60525942002-11-11 00:01:02 +0000192 unsigned NumCalls = shouldPrintAuxCalls() ?
193 getAuxFunctionCalls().size() : getFunctionCalls().size();
194 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000195 } else {
196 O << " error opening file for writing!\n";
197 }
198}
199
Chris Lattnere79eaa92003-02-10 18:17:07 +0000200/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
201/// then cleanup. For use from the debugger.
202///
203void DSGraph::viewGraph() const {
204 std::ofstream F("/tmp/tempgraph.dot");
205 if (!F.good()) {
206 std::cerr << "Error opening '/tmp/tempgraph.dot' for temporary graph!\n";
207 return;
208 }
209 print(F);
Chris Lattner2cec1d32003-02-11 19:27:27 +0000210 F.close();
Chris Lattnere79eaa92003-02-10 18:17:07 +0000211 if (system("dot -Tps /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
212 std::cerr << "Error running dot: 'dot' not in path?\n";
213 system("gv /tmp/tempgraph.ps");
214 system("rm /tmp/tempgraph.dot /tmp/tempgraph.ps");
215}
216
217
Chris Lattner0d9bab82002-07-18 00:12:30 +0000218template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000219static void printCollection(const Collection &C, std::ostream &O,
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000220 const Module *M, const std::string &Prefix) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000221 if (M == 0) {
222 O << "Null Module pointer, cannot continue!\n";
223 return;
224 }
225
Chris Lattner14212332002-11-07 02:18:46 +0000226 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000227 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner4f7815f2002-11-10 06:53:59 +0000228 if (C.hasGraph(*I)) {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000229 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner14212332002-11-07 02:18:46 +0000230 TotalNumNodes += Gr.getGraphSize();
Chris Lattner4f7815f2002-11-10 06:53:59 +0000231 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
232 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
233
234 TotalCallNodes += NumCalls;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000235 if (I->getName() == "main" || !OnlyPrintMain)
236 Gr.writeGraphToFile(O, Prefix+I->getName());
237 else {
238 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
Chris Lattner4f7815f2002-11-10 06:53:59 +0000239 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
Chris Lattner95a80ad2002-11-07 01:54:44 +0000240 }
Chris Lattner49a1ed02002-11-18 21:42:45 +0000241
242 if (MaxGraphSize < Gr.getNodes().size())
243 MaxGraphSize = Gr.getNodes().size();
244 for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
245 if (Gr.getNodes()[i]->isNodeCompletelyFolded())
246 ++NumFoldedNodes;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000247 }
Chris Lattner14212332002-11-07 02:18:46 +0000248
Chris Lattneraa0b4682002-11-09 21:12:07 +0000249 DSGraph &GG = C.getGlobalsGraph();
250 TotalNumNodes += GG.getGraphSize();
251 TotalCallNodes += GG.getFunctionCalls().size();
Chris Lattnerf76e7542002-11-09 21:40:58 +0000252 if (!OnlyPrintMain) {
Chris Lattneraa0b4682002-11-09 21:12:07 +0000253 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
254 } else {
255 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
256 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
257 }
258
Chris Lattner14212332002-11-07 02:18:46 +0000259 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000260 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000261}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000262
263
264// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000265void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000266 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000267}
268
Chris Lattner97f51a32002-07-27 01:12:15 +0000269void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000270 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000271}
272
273void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000274 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000275}