blob: 6a85ded331220d5cac69f42bbd13fe26a0cda2fe [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 Lattner2cec1d32003-02-11 19:27:27 +000050 if (N->NodeType & DSNode::DEAD ) OS << "<dead>";
Chris Lattner76d5b482002-07-11 20:33:32 +000051 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000052 }
53
Chris Lattnerfccd06f2002-10-01 22:33:50 +000054 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
55 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
56 OS << "\n";
57 }
58
Chris Lattnerc68c31b2002-07-10 22:38:08 +000059 return OS.str();
60}
61
Chris Lattnerf6c52db2002-10-13 19:31:57 +000062template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000063struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
64 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000065 if (G->hasFunction())
66 return "Function " + G->getFunction().getName();
67 else
Chris Lattner2cec1d32003-02-11 19:27:27 +000068 return "Global graph";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000069 }
70
Chris Lattnere17a4e82002-10-17 01:02:46 +000071 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000072 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000073 "\tsize=\"10,7.5\";\n"
74 "\trotate=\"90\";\n";
75 }
76
Chris Lattnere17a4e82002-10-17 01:02:46 +000077 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000078 return getCaption(Node, Graph);
79 }
80
Chris Lattnere17a4e82002-10-17 01:02:46 +000081 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000082 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000083 }
84
Chris Lattnereb265cd2002-10-16 02:04:36 +000085 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
86 /// and the return node.
87 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000088 static void addCustomGraphFeatures(const DSGraph *G,
89 GraphWriter<const DSGraph*> &GW) {
Chris Lattner714752f2003-02-04 00:03:18 +000090 Module *CurMod = G->hasFunction() ? G->getFunction().getParent() : 0;
91
Chris Lattner92673292002-11-02 00:13:20 +000092 // Add scalar nodes to the graph...
Chris Lattner41c04f72003-02-01 04:52:08 +000093 const hash_map<Value*, DSNodeHandle> &VM = G->getScalarMap();
94 for (hash_map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
Chris Lattner92673292002-11-02 00:13:20 +000095 I != VM.end(); ++I)
96 if (!isa<GlobalValue>(I->first)) {
97 std::stringstream OS;
Chris Lattner714752f2003-02-04 00:03:18 +000098 WriteAsOperand(OS, I->first, false, true, CurMod);
Chris Lattner49a1ed02002-11-18 21:42:45 +000099 GW.emitSimpleNode(I->first, "", OS.str());
Chris Lattner92673292002-11-02 00:13:20 +0000100
101 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000102 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000103 if (EdgeDest == 0) EdgeDest = -1;
104 GW.emitEdge(I->first, -1, I->second.getNode(),
105 EdgeDest, "arrowtail=tee,color=gray63");
106 }
107
108
Chris Lattnereb265cd2002-10-16 02:04:36 +0000109 // Output the returned value pointer...
110 if (G->getRetNode().getNode() != 0) {
111 // Output the return node...
112 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
113
114 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000115 int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
Chris Lattnereb265cd2002-10-16 02:04:36 +0000116 if (RetEdgeDest == 0) RetEdgeDest = -1;
117 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
118 RetEdgeDest, "arrowtail=tee,color=gray63");
119 }
Chris Lattner962ee452002-10-16 20:16:16 +0000120
121 // Output all of the call nodes...
Chris Lattner4f7815f2002-11-10 06:53:59 +0000122 const std::vector<DSCallSite> &FCs =
123 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
124 : G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000125 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000126 const DSCallSite &Call = FCs[i];
Chris Lattner923fc052003-02-05 21:59:58 +0000127 std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
128 EdgeSourceCaptions[0] = "r";
129 if (Call.isDirectCall())
130 EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
131
132 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
133 &EdgeSourceCaptions);
Chris Lattner962ee452002-10-16 20:16:16 +0000134
Chris Lattner482b6512002-10-21 13:47:57 +0000135 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000136 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000137 if (EdgeDest == 0) EdgeDest = -1;
138 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
139 }
Chris Lattner923fc052003-02-05 21:59:58 +0000140
141 // Print out the callee...
142 if (Call.isIndirectCall()) {
143 DSNode *N = Call.getCalleeNode();
144 assert(N && "Null call site callee node!");
145 GW.emitEdge(&Call, 1, N, -1, "color=gray63");
Chris Lattner482b6512002-10-21 13:47:57 +0000146 }
Chris Lattner923fc052003-02-05 21:59:58 +0000147
Chris Lattner0969c502002-10-21 02:08:03 +0000148 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
149 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000150 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000151 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner0969c502002-10-21 02:08:03 +0000152 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
Chris Lattner962ee452002-10-16 20:16:16 +0000153 }
154 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000155 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000156};
157
Chris Lattnere17a4e82002-10-17 01:02:46 +0000158void DSNode::print(std::ostream &O, const DSGraph *G) const {
159 GraphWriter<const DSGraph *> W(O, G);
160 W.writeNode(this);
161}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000162
Chris Lattnere17a4e82002-10-17 01:02:46 +0000163void DSGraph::print(std::ostream &O) const {
164 WriteGraph(O, this, "DataStructures");
165}
166
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000167void DSGraph::writeGraphToFile(std::ostream &O,
168 const std::string &GraphName) const {
169 std::string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000170 O << "Writing '" << Filename << "'...";
171 std::ofstream F(Filename.c_str());
172
173 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000174 print(F);
Chris Lattner60525942002-11-11 00:01:02 +0000175 unsigned NumCalls = shouldPrintAuxCalls() ?
176 getAuxFunctionCalls().size() : getFunctionCalls().size();
177 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000178 } else {
179 O << " error opening file for writing!\n";
180 }
181}
182
Chris Lattnere79eaa92003-02-10 18:17:07 +0000183/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
184/// then cleanup. For use from the debugger.
185///
186void DSGraph::viewGraph() const {
187 std::ofstream F("/tmp/tempgraph.dot");
188 if (!F.good()) {
189 std::cerr << "Error opening '/tmp/tempgraph.dot' for temporary graph!\n";
190 return;
191 }
192 print(F);
Chris Lattner2cec1d32003-02-11 19:27:27 +0000193 F.close();
Chris Lattnere79eaa92003-02-10 18:17:07 +0000194 if (system("dot -Tps /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
195 std::cerr << "Error running dot: 'dot' not in path?\n";
196 system("gv /tmp/tempgraph.ps");
197 system("rm /tmp/tempgraph.dot /tmp/tempgraph.ps");
198}
199
200
Chris Lattner0d9bab82002-07-18 00:12:30 +0000201template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000202static void printCollection(const Collection &C, std::ostream &O,
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000203 const Module *M, const std::string &Prefix) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000204 if (M == 0) {
205 O << "Null Module pointer, cannot continue!\n";
206 return;
207 }
208
Chris Lattner14212332002-11-07 02:18:46 +0000209 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000210 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner4f7815f2002-11-10 06:53:59 +0000211 if (C.hasGraph(*I)) {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000212 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner14212332002-11-07 02:18:46 +0000213 TotalNumNodes += Gr.getGraphSize();
Chris Lattner4f7815f2002-11-10 06:53:59 +0000214 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
215 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
216
217 TotalCallNodes += NumCalls;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000218 if (I->getName() == "main" || !OnlyPrintMain)
219 Gr.writeGraphToFile(O, Prefix+I->getName());
220 else {
221 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
Chris Lattner4f7815f2002-11-10 06:53:59 +0000222 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
Chris Lattner95a80ad2002-11-07 01:54:44 +0000223 }
Chris Lattner49a1ed02002-11-18 21:42:45 +0000224
225 if (MaxGraphSize < Gr.getNodes().size())
226 MaxGraphSize = Gr.getNodes().size();
227 for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
228 if (Gr.getNodes()[i]->isNodeCompletelyFolded())
229 ++NumFoldedNodes;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000230 }
Chris Lattner14212332002-11-07 02:18:46 +0000231
Chris Lattneraa0b4682002-11-09 21:12:07 +0000232 DSGraph &GG = C.getGlobalsGraph();
233 TotalNumNodes += GG.getGraphSize();
234 TotalCallNodes += GG.getFunctionCalls().size();
Chris Lattnerf76e7542002-11-09 21:40:58 +0000235 if (!OnlyPrintMain) {
Chris Lattneraa0b4682002-11-09 21:12:07 +0000236 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
237 } else {
238 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
239 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
240 }
241
Chris Lattner14212332002-11-07 02:18:46 +0000242 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000243 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000244}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000245
246
247// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000248void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000249 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000250}
251
Chris Lattner97f51a32002-07-27 01:12:15 +0000252void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000253 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000254}
255
256void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000257 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000258}