blob: 6775efe398e1b1eb3536f039000d43e25b205fa1 [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
14#include "llvm/Analysis/DataStructure.h"
Chris Lattnerc5f21de2002-10-02 22:14:38 +000015#include "llvm/Analysis/DSGraph.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000016#include "llvm/Analysis/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"
Chris Lattnerdadd49b2002-07-31 17:15:40 +000020#include "Support/CommandLine.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000021#include "Support/GraphWriter.h"
Chris Lattner49a1ed02002-11-18 21:42:45 +000022#include "Support/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
43 if (G) G = N->getParentGraph();
44
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 Lattnerc68c31b2002-07-10 22:38:08 +000048
Chris Lattner08db7192002-11-06 06:20:27 +000049 if (N->isNodeCompletelyFolded())
Chris Lattner63899fa2003-07-02 04:39:27 +000050 OS << "COLLAPSED";
Chris Lattner08db7192002-11-06 06:20:27 +000051 else {
Chris Lattner49a1ed02002-11-18 21:42:45 +000052 WriteTypeSymbolic(OS, N->getType(), M);
53 if (N->isArray())
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000054 OS << " array";
Chris Lattner08db7192002-11-06 06:20:27 +000055 }
Chris Lattnerbd92b732003-06-19 21:15:11 +000056 if (unsigned NodeType = N->getNodeFlags()) {
Chris Lattner08db7192002-11-06 06:20:27 +000057 OS << ": ";
Chris Lattnerbd92b732003-06-19 21:15:11 +000058 if (NodeType & DSNode::AllocaNode ) OS << "S";
59 if (NodeType & DSNode::HeapNode ) OS << "H";
60 if (NodeType & DSNode::GlobalNode ) OS << "G";
61 if (NodeType & DSNode::UnknownNode) OS << "U";
62 if (NodeType & DSNode::Incomplete ) OS << "I";
63 if (NodeType & DSNode::Modified ) OS << "M";
64 if (NodeType & DSNode::Read ) OS << "R";
Chris Lattnerbd92b732003-06-19 21:15:11 +000065#ifndef NDEBUG
66 if (NodeType & DSNode::DEAD ) OS << "<dead>";
67#endif
Chris Lattner76d5b482002-07-11 20:33:32 +000068 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000069 }
70
Chris Lattnerfccd06f2002-10-01 22:33:50 +000071 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
72 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
73 OS << "\n";
74 }
75
Chris Lattnerc68c31b2002-07-10 22:38:08 +000076 return OS.str();
77}
78
Chris Lattner9a927292003-11-12 23:11:14 +000079namespace llvm {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000080template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000081struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
82 static std::string getGraphName(const DSGraph *G) {
Chris Lattner5a540632003-06-30 03:15:25 +000083 switch (G->getReturnNodes().size()) {
Chris Lattner6681e982003-06-30 05:57:39 +000084 case 0: return G->getFunctionNames();
85 case 1: return "Function " + G->getFunctionNames();
86 default: return "Functions: " + G->getFunctionNames();
Chris Lattner5a540632003-06-30 03:15:25 +000087 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +000088 }
89
Chris Lattnere17a4e82002-10-17 01:02:46 +000090 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattner352a6fa2003-02-13 20:14:40 +000091 return "\tsize=\"10,7.5\";\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000092 "\trotate=\"90\";\n";
93 }
94
Chris Lattnere17a4e82002-10-17 01:02:46 +000095 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000096 return getCaption(Node, Graph);
97 }
98
Chris Lattnere17a4e82002-10-17 01:02:46 +000099 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +0000100 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000101 }
102
Chris Lattnereb265cd2002-10-16 02:04:36 +0000103 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
104 /// and the return node.
105 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +0000106 static void addCustomGraphFeatures(const DSGraph *G,
107 GraphWriter<const DSGraph*> &GW) {
Chris Lattner5a540632003-06-30 03:15:25 +0000108 Module *CurMod = 0;
109 if (!G->getReturnNodes().empty())
110 CurMod = G->getReturnNodes().begin()->first->getParent();
Chris Lattner714752f2003-02-04 00:03:18 +0000111
Chris Lattner92673292002-11-02 00:13:20 +0000112 // Add scalar nodes to the graph...
Chris Lattner5a540632003-06-30 03:15:25 +0000113 const DSGraph::ScalarMapTy &VM = G->getScalarMap();
114 for (DSGraph::ScalarMapTy::const_iterator I = VM.begin(); I != VM.end();++I)
Chris Lattnerd9dad2c2003-07-01 16:27:32 +0000115 if (!isa<GlobalValue>(I->first) && !isa<ConstantPointerRef>(I->first)) {
Chris Lattner92673292002-11-02 00:13:20 +0000116 std::stringstream OS;
Chris Lattner714752f2003-02-04 00:03:18 +0000117 WriteAsOperand(OS, I->first, false, true, CurMod);
Chris Lattner49a1ed02002-11-18 21:42:45 +0000118 GW.emitSimpleNode(I->first, "", OS.str());
Chris Lattner92673292002-11-02 00:13:20 +0000119
120 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000121 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000122 if (EdgeDest == 0) EdgeDest = -1;
123 GW.emitEdge(I->first, -1, I->second.getNode(),
124 EdgeDest, "arrowtail=tee,color=gray63");
125 }
126
127
Chris Lattnereb265cd2002-10-16 02:04:36 +0000128 // Output the returned value pointer...
Chris Lattner5a540632003-06-30 03:15:25 +0000129 const DSGraph::ReturnNodesTy &RetNodes = G->getReturnNodes();
130 for (DSGraph::ReturnNodesTy::const_iterator I = RetNodes.begin(),
131 E = RetNodes.end(); I != E; ++I)
132 if (I->second.getNode()) {
133 std::string Label;
134 if (RetNodes.size() == 1)
135 Label = "returning";
136 else
137 Label = I->first->getName() + " ret node";
138 // Output the return node...
Chris Lattnerd8ea8a52003-11-12 04:58:19 +0000139 GW.emitSimpleNode((void*)I->first, "plaintext=circle", Label);
Chris Lattnereb265cd2002-10-16 02:04:36 +0000140
Chris Lattner5a540632003-06-30 03:15:25 +0000141 // Add edge from return node to real destination
142 int RetEdgeDest = I->second.getOffset() >> DS::PointerShift;;
143 if (RetEdgeDest == 0) RetEdgeDest = -1;
Chris Lattnerd8ea8a52003-11-12 04:58:19 +0000144 GW.emitEdge((void*)I->first, -1, I->second.getNode(),
Chris Lattner5a540632003-06-30 03:15:25 +0000145 RetEdgeDest, "arrowtail=tee,color=gray63");
146 }
Chris Lattner962ee452002-10-16 20:16:16 +0000147
148 // Output all of the call nodes...
Chris Lattner4f7815f2002-11-10 06:53:59 +0000149 const std::vector<DSCallSite> &FCs =
150 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
151 : G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000152 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000153 const DSCallSite &Call = FCs[i];
Chris Lattner923fc052003-02-05 21:59:58 +0000154 std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
155 EdgeSourceCaptions[0] = "r";
156 if (Call.isDirectCall())
157 EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
Chris Lattnerf1c28382003-02-14 20:25:47 +0000158 else
159 EdgeSourceCaptions[1] = "f";
Chris Lattner923fc052003-02-05 21:59:58 +0000160
161 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
162 &EdgeSourceCaptions);
Chris Lattner962ee452002-10-16 20:16:16 +0000163
Chris Lattner482b6512002-10-21 13:47:57 +0000164 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000165 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000166 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000167 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000168 }
Chris Lattner923fc052003-02-05 21:59:58 +0000169
170 // Print out the callee...
171 if (Call.isIndirectCall()) {
172 DSNode *N = Call.getCalleeNode();
173 assert(N && "Null call site callee node!");
Chris Lattner352a6fa2003-02-13 20:14:40 +0000174 GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000175 }
Chris Lattner923fc052003-02-05 21:59:58 +0000176
Chris Lattner0969c502002-10-21 02:08:03 +0000177 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
178 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000179 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000180 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000181 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner962ee452002-10-16 20:16:16 +0000182 }
183 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000184 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000185};
Chris Lattner9a927292003-11-12 23:11:14 +0000186} // end namespace llvm
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000187
Chris Lattnere17a4e82002-10-17 01:02:46 +0000188void DSNode::print(std::ostream &O, const DSGraph *G) const {
189 GraphWriter<const DSGraph *> W(O, G);
190 W.writeNode(this);
191}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000192
Chris Lattnere17a4e82002-10-17 01:02:46 +0000193void DSGraph::print(std::ostream &O) const {
194 WriteGraph(O, this, "DataStructures");
195}
196
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000197void DSGraph::writeGraphToFile(std::ostream &O,
198 const std::string &GraphName) const {
199 std::string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000200 O << "Writing '" << Filename << "'...";
201 std::ofstream F(Filename.c_str());
202
203 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000204 print(F);
Chris Lattner60525942002-11-11 00:01:02 +0000205 unsigned NumCalls = shouldPrintAuxCalls() ?
206 getAuxFunctionCalls().size() : getFunctionCalls().size();
207 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000208 } else {
209 O << " error opening file for writing!\n";
210 }
211}
212
Chris Lattnere79eaa92003-02-10 18:17:07 +0000213/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
214/// then cleanup. For use from the debugger.
215///
216void DSGraph::viewGraph() const {
217 std::ofstream F("/tmp/tempgraph.dot");
218 if (!F.good()) {
219 std::cerr << "Error opening '/tmp/tempgraph.dot' for temporary graph!\n";
220 return;
221 }
222 print(F);
Chris Lattner2cec1d32003-02-11 19:27:27 +0000223 F.close();
Chris Lattnere79eaa92003-02-10 18:17:07 +0000224 if (system("dot -Tps /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
225 std::cerr << "Error running dot: 'dot' not in path?\n";
226 system("gv /tmp/tempgraph.ps");
227 system("rm /tmp/tempgraph.dot /tmp/tempgraph.ps");
228}
229
230
Chris Lattner0d9bab82002-07-18 00:12:30 +0000231template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000232static void printCollection(const Collection &C, std::ostream &O,
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000233 const Module *M, const std::string &Prefix) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000234 if (M == 0) {
235 O << "Null Module pointer, cannot continue!\n";
236 return;
237 }
238
Chris Lattner14212332002-11-07 02:18:46 +0000239 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000240 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner4f7815f2002-11-10 06:53:59 +0000241 if (C.hasGraph(*I)) {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000242 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner14212332002-11-07 02:18:46 +0000243 TotalNumNodes += Gr.getGraphSize();
Chris Lattner4f7815f2002-11-10 06:53:59 +0000244 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
245 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
246
247 TotalCallNodes += NumCalls;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000248 if (I->getName() == "main" || !OnlyPrintMain)
249 Gr.writeGraphToFile(O, Prefix+I->getName());
250 else {
251 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
Chris Lattner4f7815f2002-11-10 06:53:59 +0000252 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
Chris Lattner95a80ad2002-11-07 01:54:44 +0000253 }
Chris Lattner49a1ed02002-11-18 21:42:45 +0000254
Chris Lattner51711152004-02-21 22:27:31 +0000255 unsigned GraphSize = Gr.getGraphSize();
Chris Lattnere92e7642004-02-07 23:58:05 +0000256 if (MaxGraphSize < GraphSize) MaxGraphSize = GraphSize;
257
258 for (DSGraph::node_iterator NI = Gr.node_begin(), E = Gr.node_end();
259 NI != E; ++NI)
260 if ((*NI)->isNodeCompletelyFolded())
Chris Lattner49a1ed02002-11-18 21:42:45 +0000261 ++NumFoldedNodes;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000262 }
Chris Lattner14212332002-11-07 02:18:46 +0000263
Chris Lattneraa0b4682002-11-09 21:12:07 +0000264 DSGraph &GG = C.getGlobalsGraph();
265 TotalNumNodes += GG.getGraphSize();
266 TotalCallNodes += GG.getFunctionCalls().size();
Chris Lattnerf76e7542002-11-09 21:40:58 +0000267 if (!OnlyPrintMain) {
Chris Lattneraa0b4682002-11-09 21:12:07 +0000268 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
269 } else {
270 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
271 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
272 }
273
Chris Lattner14212332002-11-07 02:18:46 +0000274 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000275 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000276}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000277
278
279// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000280void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000281 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000282 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000283}
284
Chris Lattner97f51a32002-07-27 01:12:15 +0000285void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000286 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000287 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000288}
289
290void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000291 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000292 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000293}
Brian Gaeked0fde302003-11-11 22:41:34 +0000294
Chris Lattner79390d42003-11-13 05:05:41 +0000295void CompleteBUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000296 if (DontPrintAnything) return;
Chris Lattner79390d42003-11-13 05:05:41 +0000297 printCollection(*this, O, M, "cbu.");
298}
299
300