blob: 09748ff839732b0e3e15caeea9488f5cc164203d [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 }
41 if (N->NodeType) {
42 OS << ": ";
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";
Chris Lattner76d5b482002-07-11 20:33:32 +000050 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000051 }
52
Chris Lattnerfccd06f2002-10-01 22:33:50 +000053 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
54 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
55 OS << "\n";
56 }
57
Chris Lattnerc68c31b2002-07-10 22:38:08 +000058 return OS.str();
59}
60
Chris Lattnerf6c52db2002-10-13 19:31:57 +000061template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000062struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
63 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000064 if (G->hasFunction())
65 return "Function " + G->getFunction().getName();
66 else
Chris Lattner32d10dd2003-02-02 16:42:01 +000067 return "Globals graph";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000068 }
69
Chris Lattnere17a4e82002-10-17 01:02:46 +000070 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000071 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000072 "\tsize=\"10,7.5\";\n"
73 "\trotate=\"90\";\n";
74 }
75
Chris Lattnere17a4e82002-10-17 01:02:46 +000076 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000077 return getCaption(Node, Graph);
78 }
79
Chris Lattnere17a4e82002-10-17 01:02:46 +000080 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000081 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000082 }
83
Chris Lattnereb265cd2002-10-16 02:04:36 +000084 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
85 /// and the return node.
86 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000087 static void addCustomGraphFeatures(const DSGraph *G,
88 GraphWriter<const DSGraph*> &GW) {
Chris Lattner714752f2003-02-04 00:03:18 +000089 Module *CurMod = G->hasFunction() ? G->getFunction().getParent() : 0;
90
Chris Lattner92673292002-11-02 00:13:20 +000091 // Add scalar nodes to the graph...
Chris Lattner41c04f72003-02-01 04:52:08 +000092 const hash_map<Value*, DSNodeHandle> &VM = G->getScalarMap();
93 for (hash_map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
Chris Lattner92673292002-11-02 00:13:20 +000094 I != VM.end(); ++I)
95 if (!isa<GlobalValue>(I->first)) {
96 std::stringstream OS;
Chris Lattner714752f2003-02-04 00:03:18 +000097 WriteAsOperand(OS, I->first, false, true, CurMod);
Chris Lattner49a1ed02002-11-18 21:42:45 +000098 GW.emitSimpleNode(I->first, "", OS.str());
Chris Lattner92673292002-11-02 00:13:20 +000099
100 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000101 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000102 if (EdgeDest == 0) EdgeDest = -1;
103 GW.emitEdge(I->first, -1, I->second.getNode(),
104 EdgeDest, "arrowtail=tee,color=gray63");
105 }
106
107
Chris Lattnereb265cd2002-10-16 02:04:36 +0000108 // Output the returned value pointer...
109 if (G->getRetNode().getNode() != 0) {
110 // Output the return node...
111 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
112
113 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000114 int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
Chris Lattnereb265cd2002-10-16 02:04:36 +0000115 if (RetEdgeDest == 0) RetEdgeDest = -1;
116 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
117 RetEdgeDest, "arrowtail=tee,color=gray63");
118 }
Chris Lattner962ee452002-10-16 20:16:16 +0000119
120 // Output all of the call nodes...
Chris Lattner4f7815f2002-11-10 06:53:59 +0000121 const std::vector<DSCallSite> &FCs =
122 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
123 : G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000124 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000125 const DSCallSite &Call = FCs[i];
Chris Lattner923fc052003-02-05 21:59:58 +0000126 std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
127 EdgeSourceCaptions[0] = "r";
128 if (Call.isDirectCall())
129 EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
130
131 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
132 &EdgeSourceCaptions);
Chris Lattner962ee452002-10-16 20:16:16 +0000133
Chris Lattner482b6512002-10-21 13:47:57 +0000134 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000135 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000136 if (EdgeDest == 0) EdgeDest = -1;
137 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
138 }
Chris Lattner923fc052003-02-05 21:59:58 +0000139
140 // Print out the callee...
141 if (Call.isIndirectCall()) {
142 DSNode *N = Call.getCalleeNode();
143 assert(N && "Null call site callee node!");
144 GW.emitEdge(&Call, 1, N, -1, "color=gray63");
Chris Lattner482b6512002-10-21 13:47:57 +0000145 }
Chris Lattner923fc052003-02-05 21:59:58 +0000146
Chris Lattner0969c502002-10-21 02:08:03 +0000147 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
148 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000149 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000150 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner0969c502002-10-21 02:08:03 +0000151 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
Chris Lattner962ee452002-10-16 20:16:16 +0000152 }
153 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000154 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000155};
156
Chris Lattnere17a4e82002-10-17 01:02:46 +0000157void DSNode::print(std::ostream &O, const DSGraph *G) const {
158 GraphWriter<const DSGraph *> W(O, G);
159 W.writeNode(this);
160}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000161
Chris Lattnere17a4e82002-10-17 01:02:46 +0000162void DSGraph::print(std::ostream &O) const {
163 WriteGraph(O, this, "DataStructures");
164}
165
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000166void DSGraph::writeGraphToFile(std::ostream &O,
167 const std::string &GraphName) const {
168 std::string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000169 O << "Writing '" << Filename << "'...";
170 std::ofstream F(Filename.c_str());
171
172 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000173 print(F);
Chris Lattner60525942002-11-11 00:01:02 +0000174 unsigned NumCalls = shouldPrintAuxCalls() ?
175 getAuxFunctionCalls().size() : getFunctionCalls().size();
176 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000177 } else {
178 O << " error opening file for writing!\n";
179 }
180}
181
Chris Lattnere79eaa92003-02-10 18:17:07 +0000182/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
183/// then cleanup. For use from the debugger.
184///
185void DSGraph::viewGraph() const {
186 std::ofstream F("/tmp/tempgraph.dot");
187 if (!F.good()) {
188 std::cerr << "Error opening '/tmp/tempgraph.dot' for temporary graph!\n";
189 return;
190 }
191 print(F);
192 if (system("dot -Tps /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
193 std::cerr << "Error running dot: 'dot' not in path?\n";
194 system("gv /tmp/tempgraph.ps");
195 system("rm /tmp/tempgraph.dot /tmp/tempgraph.ps");
196}
197
198
Chris Lattner0d9bab82002-07-18 00:12:30 +0000199template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000200static void printCollection(const Collection &C, std::ostream &O,
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000201 const Module *M, const std::string &Prefix) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000202 if (M == 0) {
203 O << "Null Module pointer, cannot continue!\n";
204 return;
205 }
206
Chris Lattner14212332002-11-07 02:18:46 +0000207 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000208 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner4f7815f2002-11-10 06:53:59 +0000209 if (C.hasGraph(*I)) {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000210 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner14212332002-11-07 02:18:46 +0000211 TotalNumNodes += Gr.getGraphSize();
Chris Lattner4f7815f2002-11-10 06:53:59 +0000212 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
213 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
214
215 TotalCallNodes += NumCalls;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000216 if (I->getName() == "main" || !OnlyPrintMain)
217 Gr.writeGraphToFile(O, Prefix+I->getName());
218 else {
219 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
Chris Lattner4f7815f2002-11-10 06:53:59 +0000220 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
Chris Lattner95a80ad2002-11-07 01:54:44 +0000221 }
Chris Lattner49a1ed02002-11-18 21:42:45 +0000222
223 if (MaxGraphSize < Gr.getNodes().size())
224 MaxGraphSize = Gr.getNodes().size();
225 for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
226 if (Gr.getNodes()[i]->isNodeCompletelyFolded())
227 ++NumFoldedNodes;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000228 }
Chris Lattner14212332002-11-07 02:18:46 +0000229
Chris Lattneraa0b4682002-11-09 21:12:07 +0000230 DSGraph &GG = C.getGlobalsGraph();
231 TotalNumNodes += GG.getGraphSize();
232 TotalCallNodes += GG.getFunctionCalls().size();
Chris Lattnerf76e7542002-11-09 21:40:58 +0000233 if (!OnlyPrintMain) {
Chris Lattneraa0b4682002-11-09 21:12:07 +0000234 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
235 } else {
236 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
237 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
238 }
239
Chris Lattner14212332002-11-07 02:18:46 +0000240 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000241 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000242}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000243
244
245// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000246void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000247 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000248}
249
Chris Lattner97f51a32002-07-27 01:12:15 +0000250void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000251 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000252}
253
254void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000255 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000256}