blob: 4d5fa81425e5eadf24e34dd0670155c69bb2573c [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
John Criswellb576c942003-10-20 19:43:21 +00002//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +00009//
10// This file implements the 'dot' graph printer.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner4dabb2c2004-07-07 06:32:21 +000014#include "llvm/Analysis/DataStructure/DataStructure.h"
15#include "llvm/Analysis/DataStructure/DSGraph.h"
16#include "llvm/Analysis/DataStructure/DSGraphTraits.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017#include "llvm/Module.h"
Chris Lattnerd9dad2c2003-07-01 16:27:32 +000018#include "llvm/Constants.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000019#include "llvm/Assembly/Writer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/GraphWriter.h"
22#include "llvm/ADT/Statistic.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000023#include <fstream>
24#include <sstream>
Chris Lattner9a927292003-11-12 23:11:14 +000025using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000026
Chris Lattner55c10582002-10-03 20:38:41 +000027// OnlyPrintMain - The DataStructure printer exposes this option to allow
28// printing of only the graph for "main".
29//
Chris Lattner49a1ed02002-11-18 21:42:45 +000030namespace {
31 cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
Chris Lattner4a857762004-01-22 13:42:43 +000032 cl::opt<bool> DontPrintAnything("dont-print-ds", cl::ReallyHidden);
Chris Lattnere92e7642004-02-07 23:58:05 +000033 Statistic<> MaxGraphSize ("dsa", "Maximum graph size");
34 Statistic<> NumFoldedNodes ("dsa", "Number of folded nodes (in final graph)");
Chris Lattner49a1ed02002-11-18 21:42:45 +000035}
Chris Lattner55c10582002-10-03 20:38:41 +000036
Chris Lattnerc68c31b2002-07-10 22:38:08 +000037void DSNode::dump() const { print(std::cerr, 0); }
38
Chris Lattnerb3416bc2003-02-01 04:01:21 +000039static std::string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000040 std::stringstream OS;
Chris Lattner5a540632003-06-30 03:15:25 +000041 Module *M = 0;
Chris Lattner153f2402004-02-25 23:06:30 +000042
Chris Lattner72529392004-03-02 21:39:43 +000043 if (!G) G = N->getParentGraph();
Chris Lattner153f2402004-02-25 23:06:30 +000044
Chris Lattner5a540632003-06-30 03:15:25 +000045 // Get the module from ONE of the functions in the graph it is available.
46 if (G && !G->getReturnNodes().empty())
47 M = G->getReturnNodes().begin()->first->getParent();
Chris Lattner72529392004-03-02 21:39:43 +000048 if (M == 0 && G) {
49 // If there is a global in the graph, we can use it to find the module.
50 const DSScalarMap &SM = G->getScalarMap();
51 if (SM.global_begin() != SM.global_end())
52 M = (*SM.global_begin())->getParent();
53 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000054
Chris Lattner08db7192002-11-06 06:20:27 +000055 if (N->isNodeCompletelyFolded())
Chris Lattner63899fa2003-07-02 04:39:27 +000056 OS << "COLLAPSED";
Chris Lattner08db7192002-11-06 06:20:27 +000057 else {
Chris Lattner49a1ed02002-11-18 21:42:45 +000058 WriteTypeSymbolic(OS, N->getType(), M);
59 if (N->isArray())
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000060 OS << " array";
Chris Lattner08db7192002-11-06 06:20:27 +000061 }
Chris Lattnerbd92b732003-06-19 21:15:11 +000062 if (unsigned NodeType = N->getNodeFlags()) {
Chris Lattner08db7192002-11-06 06:20:27 +000063 OS << ": ";
Chris Lattnerbd92b732003-06-19 21:15:11 +000064 if (NodeType & DSNode::AllocaNode ) OS << "S";
65 if (NodeType & DSNode::HeapNode ) OS << "H";
66 if (NodeType & DSNode::GlobalNode ) OS << "G";
67 if (NodeType & DSNode::UnknownNode) OS << "U";
68 if (NodeType & DSNode::Incomplete ) OS << "I";
69 if (NodeType & DSNode::Modified ) OS << "M";
70 if (NodeType & DSNode::Read ) OS << "R";
Chris Lattnerbd92b732003-06-19 21:15:11 +000071#ifndef NDEBUG
72 if (NodeType & DSNode::DEAD ) OS << "<dead>";
73#endif
Chris Lattner76d5b482002-07-11 20:33:32 +000074 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000075 }
76
Chris Lattnerfccd06f2002-10-01 22:33:50 +000077 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
78 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
79 OS << "\n";
80 }
81
Chris Lattnerc68c31b2002-07-10 22:38:08 +000082 return OS.str();
83}
84
Chris Lattner9a927292003-11-12 23:11:14 +000085namespace llvm {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000086template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000087struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
88 static std::string getGraphName(const DSGraph *G) {
Chris Lattner5a540632003-06-30 03:15:25 +000089 switch (G->getReturnNodes().size()) {
Chris Lattner6681e982003-06-30 05:57:39 +000090 case 0: return G->getFunctionNames();
91 case 1: return "Function " + G->getFunctionNames();
92 default: return "Functions: " + G->getFunctionNames();
Chris Lattner5a540632003-06-30 03:15:25 +000093 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +000094 }
95
Chris Lattnere17a4e82002-10-17 01:02:46 +000096 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000097 return getCaption(Node, Graph);
98 }
99
Chris Lattnere17a4e82002-10-17 01:02:46 +0000100 static std::string getNodeAttributes(const DSNode *N) {
Brian Gaeke61780852004-05-05 06:10:06 +0000101 return "shape=Mrecord";
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000102 }
Chris Lattner8e667cd2004-06-22 07:13:10 +0000103
104 static bool edgeTargetsEdgeSource(const void *Node,
105 DSNode::const_iterator I) {
106 unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
107 return (O >> DS::PointerShift) != 0;
108 }
109
110 static DSNode::const_iterator getEdgeTarget(const DSNode *Node,
111 DSNode::const_iterator I) {
112 unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
113 unsigned LinkNo = O >> DS::PointerShift;
114 const DSNode *N = *I;
115 DSNode::const_iterator R = N->begin();
116 for (; LinkNo; --LinkNo)
117 ++R;
118 return R;
119 }
120
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000121
Chris Lattnereb265cd2002-10-16 02:04:36 +0000122 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
123 /// and the return node.
124 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +0000125 static void addCustomGraphFeatures(const DSGraph *G,
126 GraphWriter<const DSGraph*> &GW) {
Chris Lattner5a540632003-06-30 03:15:25 +0000127 Module *CurMod = 0;
128 if (!G->getReturnNodes().empty())
129 CurMod = G->getReturnNodes().begin()->first->getParent();
Chris Lattner72529392004-03-02 21:39:43 +0000130 else {
131 // If there is a global in the graph, we can use it to find the module.
132 const DSScalarMap &SM = G->getScalarMap();
133 if (SM.global_begin() != SM.global_end())
134 CurMod = (*SM.global_begin())->getParent();
135 }
136
Chris Lattner714752f2003-02-04 00:03:18 +0000137
Chris Lattner92673292002-11-02 00:13:20 +0000138 // Add scalar nodes to the graph...
Chris Lattner5a540632003-06-30 03:15:25 +0000139 const DSGraph::ScalarMapTy &VM = G->getScalarMap();
140 for (DSGraph::ScalarMapTy::const_iterator I = VM.begin(); I != VM.end();++I)
Reid Spencere8404342004-07-18 00:18:30 +0000141 if (!isa<GlobalValue>(I->first)) {
Chris Lattner92673292002-11-02 00:13:20 +0000142 std::stringstream OS;
Chris Lattner714752f2003-02-04 00:03:18 +0000143 WriteAsOperand(OS, I->first, false, true, CurMod);
Chris Lattner49a1ed02002-11-18 21:42:45 +0000144 GW.emitSimpleNode(I->first, "", OS.str());
Chris Lattner92673292002-11-02 00:13:20 +0000145
146 // Add edge from return node to real destination
Chris Lattnerf1bd4b42004-10-30 07:21:19 +0000147 DSNode *DestNode = I->second.getNode();
Chris Lattner08db7192002-11-06 06:20:27 +0000148 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000149 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattnerf1bd4b42004-10-30 07:21:19 +0000150 GW.emitEdge(I->first, -1, DestNode,
Chris Lattner92673292002-11-02 00:13:20 +0000151 EdgeDest, "arrowtail=tee,color=gray63");
152 }
153
154
Chris Lattnereb265cd2002-10-16 02:04:36 +0000155 // Output the returned value pointer...
Chris Lattner5a540632003-06-30 03:15:25 +0000156 const DSGraph::ReturnNodesTy &RetNodes = G->getReturnNodes();
157 for (DSGraph::ReturnNodesTy::const_iterator I = RetNodes.begin(),
158 E = RetNodes.end(); I != E; ++I)
159 if (I->second.getNode()) {
160 std::string Label;
161 if (RetNodes.size() == 1)
162 Label = "returning";
163 else
164 Label = I->first->getName() + " ret node";
165 // Output the return node...
Chris Lattnerd8ea8a52003-11-12 04:58:19 +0000166 GW.emitSimpleNode((void*)I->first, "plaintext=circle", Label);
Chris Lattnereb265cd2002-10-16 02:04:36 +0000167
Chris Lattner5a540632003-06-30 03:15:25 +0000168 // Add edge from return node to real destination
Chris Lattnerf1bd4b42004-10-30 07:21:19 +0000169 DSNode *RetNode = I->second.getNode();
Chris Lattner5a540632003-06-30 03:15:25 +0000170 int RetEdgeDest = I->second.getOffset() >> DS::PointerShift;;
171 if (RetEdgeDest == 0) RetEdgeDest = -1;
Chris Lattnerf1bd4b42004-10-30 07:21:19 +0000172 GW.emitEdge((void*)I->first, -1, RetNode,
Chris Lattner5a540632003-06-30 03:15:25 +0000173 RetEdgeDest, "arrowtail=tee,color=gray63");
174 }
Chris Lattner962ee452002-10-16 20:16:16 +0000175
176 // Output all of the call nodes...
Chris Lattnera9548d92005-01-30 23:51:02 +0000177 const std::list<DSCallSite> &FCs =
Chris Lattner4f7815f2002-11-10 06:53:59 +0000178 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
179 : G->getFunctionCalls();
Chris Lattnera9548d92005-01-30 23:51:02 +0000180 for (std::list<DSCallSite>::const_iterator I = FCs.begin(), E = FCs.end();
181 I != E; ++I) {
182 const DSCallSite &Call = *I;
Chris Lattner923fc052003-02-05 21:59:58 +0000183 std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
184 EdgeSourceCaptions[0] = "r";
185 if (Call.isDirectCall())
186 EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
Chris Lattnerf1c28382003-02-14 20:25:47 +0000187 else
188 EdgeSourceCaptions[1] = "f";
Chris Lattner923fc052003-02-05 21:59:58 +0000189
190 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
191 &EdgeSourceCaptions);
Chris Lattner962ee452002-10-16 20:16:16 +0000192
Chris Lattner482b6512002-10-21 13:47:57 +0000193 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000194 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000195 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000196 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000197 }
Chris Lattner923fc052003-02-05 21:59:58 +0000198
199 // Print out the callee...
200 if (Call.isIndirectCall()) {
201 DSNode *N = Call.getCalleeNode();
202 assert(N && "Null call site callee node!");
Chris Lattner352a6fa2003-02-13 20:14:40 +0000203 GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000204 }
Chris Lattner923fc052003-02-05 21:59:58 +0000205
Chris Lattner0969c502002-10-21 02:08:03 +0000206 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
207 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000208 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000209 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000210 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner962ee452002-10-16 20:16:16 +0000211 }
212 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000213 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000214};
Chris Lattner9a927292003-11-12 23:11:14 +0000215} // end namespace llvm
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000216
Chris Lattnere17a4e82002-10-17 01:02:46 +0000217void DSNode::print(std::ostream &O, const DSGraph *G) const {
218 GraphWriter<const DSGraph *> W(O, G);
219 W.writeNode(this);
220}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000221
Chris Lattnere17a4e82002-10-17 01:02:46 +0000222void DSGraph::print(std::ostream &O) const {
223 WriteGraph(O, this, "DataStructures");
224}
225
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000226void DSGraph::writeGraphToFile(std::ostream &O,
227 const std::string &GraphName) const {
228 std::string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000229 O << "Writing '" << Filename << "'...";
230 std::ofstream F(Filename.c_str());
231
232 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000233 print(F);
Chris Lattner60525942002-11-11 00:01:02 +0000234 unsigned NumCalls = shouldPrintAuxCalls() ?
235 getAuxFunctionCalls().size() : getFunctionCalls().size();
236 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000237 } else {
238 O << " error opening file for writing!\n";
239 }
240}
241
Chris Lattnere79eaa92003-02-10 18:17:07 +0000242/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
243/// then cleanup. For use from the debugger.
244///
245void DSGraph::viewGraph() const {
246 std::ofstream F("/tmp/tempgraph.dot");
247 if (!F.good()) {
248 std::cerr << "Error opening '/tmp/tempgraph.dot' for temporary graph!\n";
249 return;
250 }
251 print(F);
Chris Lattner2cec1d32003-02-11 19:27:27 +0000252 F.close();
Brian Gaeke61780852004-05-05 06:10:06 +0000253 if (system("dot -Tps -Gsize=10,7.5 -Grotate=90 /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
Chris Lattnere79eaa92003-02-10 18:17:07 +0000254 std::cerr << "Error running dot: 'dot' not in path?\n";
255 system("gv /tmp/tempgraph.ps");
256 system("rm /tmp/tempgraph.dot /tmp/tempgraph.ps");
257}
258
259
Chris Lattner0d9bab82002-07-18 00:12:30 +0000260template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000261static void printCollection(const Collection &C, std::ostream &O,
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000262 const Module *M, const std::string &Prefix) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000263 if (M == 0) {
264 O << "Null Module pointer, cannot continue!\n";
265 return;
266 }
267
Chris Lattner14212332002-11-07 02:18:46 +0000268 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000269 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner4f7815f2002-11-10 06:53:59 +0000270 if (C.hasGraph(*I)) {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000271 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner14212332002-11-07 02:18:46 +0000272 TotalNumNodes += Gr.getGraphSize();
Chris Lattner4f7815f2002-11-10 06:53:59 +0000273 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
274 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
275
276 TotalCallNodes += NumCalls;
Chris Lattner1e759992005-02-01 19:10:48 +0000277 if (I->getName() == "main" || !OnlyPrintMain) {
278 Function *SCCFn = Gr.getReturnNodes().begin()->first;
279 if (&*I == SCCFn)
280 Gr.writeGraphToFile(O, Prefix+I->getName());
281 else
282 O << "Didn't write '" << Prefix+I->getName()
283 << ".dot' - Graph already emitted to '" << Prefix+SCCFn->getName()
284 << "\n";
285 } else {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000286 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
Chris Lattner4f7815f2002-11-10 06:53:59 +0000287 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
Chris Lattner95a80ad2002-11-07 01:54:44 +0000288 }
Chris Lattner49a1ed02002-11-18 21:42:45 +0000289
Chris Lattner51711152004-02-21 22:27:31 +0000290 unsigned GraphSize = Gr.getGraphSize();
Chris Lattnere92e7642004-02-07 23:58:05 +0000291 if (MaxGraphSize < GraphSize) MaxGraphSize = GraphSize;
292
293 for (DSGraph::node_iterator NI = Gr.node_begin(), E = Gr.node_end();
294 NI != E; ++NI)
295 if ((*NI)->isNodeCompletelyFolded())
Chris Lattner49a1ed02002-11-18 21:42:45 +0000296 ++NumFoldedNodes;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000297 }
Chris Lattner14212332002-11-07 02:18:46 +0000298
Chris Lattneraa0b4682002-11-09 21:12:07 +0000299 DSGraph &GG = C.getGlobalsGraph();
300 TotalNumNodes += GG.getGraphSize();
301 TotalCallNodes += GG.getFunctionCalls().size();
Chris Lattnerf76e7542002-11-09 21:40:58 +0000302 if (!OnlyPrintMain) {
Chris Lattneraa0b4682002-11-09 21:12:07 +0000303 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
304 } else {
305 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
306 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
307 }
308
Chris Lattner14212332002-11-07 02:18:46 +0000309 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000310 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000311}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000312
313
314// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000315void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000316 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000317 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000318}
319
Chris Lattner97f51a32002-07-27 01:12:15 +0000320void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000321 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000322 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000323}
324
325void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000326 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000327 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000328}
Brian Gaeked0fde302003-11-11 22:41:34 +0000329
Chris Lattner79390d42003-11-13 05:05:41 +0000330void CompleteBUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000331 if (DontPrintAnything) return;
Chris Lattner79390d42003-11-13 05:05:41 +0000332 printCollection(*this, O, M, "cbu.");
333}
334
335