blob: e9b5a179728bd615782b7afd044c67170b4e7d6b [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 Lattnerfccd06f2002-10-01 22:33:50 +000032 Module *M = G && &G->getFunction() ? 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
67 return "Non-function graph";
68 }
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 Lattner92673292002-11-02 00:13:20 +000089 // Add scalar nodes to the graph...
Chris Lattnerc875f022002-11-03 21:27:48 +000090 const std::map<Value*, DSNodeHandle> &VM = G->getScalarMap();
Chris Lattner92673292002-11-02 00:13:20 +000091 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
92 I != VM.end(); ++I)
93 if (!isa<GlobalValue>(I->first)) {
94 std::stringstream OS;
95 WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
Chris Lattner49a1ed02002-11-18 21:42:45 +000096 GW.emitSimpleNode(I->first, "", OS.str());
Chris Lattner92673292002-11-02 00:13:20 +000097
98 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +000099 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000100 if (EdgeDest == 0) EdgeDest = -1;
101 GW.emitEdge(I->first, -1, I->second.getNode(),
102 EdgeDest, "arrowtail=tee,color=gray63");
103 }
104
105
Chris Lattnereb265cd2002-10-16 02:04:36 +0000106 // Output the returned value pointer...
107 if (G->getRetNode().getNode() != 0) {
108 // Output the return node...
109 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
110
111 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000112 int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
Chris Lattnereb265cd2002-10-16 02:04:36 +0000113 if (RetEdgeDest == 0) RetEdgeDest = -1;
114 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
115 RetEdgeDest, "arrowtail=tee,color=gray63");
116 }
Chris Lattner962ee452002-10-16 20:16:16 +0000117
118 // Output all of the call nodes...
Chris Lattner4f7815f2002-11-10 06:53:59 +0000119 const std::vector<DSCallSite> &FCs =
120 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
121 : G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000122 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000123 const DSCallSite &Call = FCs[i];
Chris Lattner0969c502002-10-21 02:08:03 +0000124 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
Chris Lattner962ee452002-10-16 20:16:16 +0000125
Chris Lattner482b6512002-10-21 13:47:57 +0000126 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000127 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000128 if (EdgeDest == 0) EdgeDest = -1;
129 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
130 }
131 if (DSNode *N = Call.getCallee().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000132 int EdgeDest = Call.getCallee().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000133 if (EdgeDest == 0) EdgeDest = -1;
134 GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
135 }
Chris Lattner0969c502002-10-21 02:08:03 +0000136 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
137 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000138 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000139 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner0969c502002-10-21 02:08:03 +0000140 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
Chris Lattner962ee452002-10-16 20:16:16 +0000141 }
142 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000143 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000144};
145
Chris Lattnere17a4e82002-10-17 01:02:46 +0000146void DSNode::print(std::ostream &O, const DSGraph *G) const {
147 GraphWriter<const DSGraph *> W(O, G);
148 W.writeNode(this);
149}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000150
Chris Lattnere17a4e82002-10-17 01:02:46 +0000151void DSGraph::print(std::ostream &O) const {
152 WriteGraph(O, this, "DataStructures");
153}
154
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000155void DSGraph::writeGraphToFile(std::ostream &O,
156 const std::string &GraphName) const {
157 std::string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000158 O << "Writing '" << Filename << "'...";
159 std::ofstream F(Filename.c_str());
160
161 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000162 print(F);
Chris Lattner60525942002-11-11 00:01:02 +0000163 unsigned NumCalls = shouldPrintAuxCalls() ?
164 getAuxFunctionCalls().size() : getFunctionCalls().size();
165 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000166 } else {
167 O << " error opening file for writing!\n";
168 }
169}
170
Chris Lattner0d9bab82002-07-18 00:12:30 +0000171template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000172static void printCollection(const Collection &C, std::ostream &O,
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000173 const Module *M, const std::string &Prefix) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000174 if (M == 0) {
175 O << "Null Module pointer, cannot continue!\n";
176 return;
177 }
178
Chris Lattner14212332002-11-07 02:18:46 +0000179 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000180 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner4f7815f2002-11-10 06:53:59 +0000181 if (C.hasGraph(*I)) {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000182 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner14212332002-11-07 02:18:46 +0000183 TotalNumNodes += Gr.getGraphSize();
Chris Lattner4f7815f2002-11-10 06:53:59 +0000184 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
185 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
186
187 TotalCallNodes += NumCalls;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000188 if (I->getName() == "main" || !OnlyPrintMain)
189 Gr.writeGraphToFile(O, Prefix+I->getName());
190 else {
191 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
Chris Lattner4f7815f2002-11-10 06:53:59 +0000192 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
Chris Lattner95a80ad2002-11-07 01:54:44 +0000193 }
Chris Lattner49a1ed02002-11-18 21:42:45 +0000194
195 if (MaxGraphSize < Gr.getNodes().size())
196 MaxGraphSize = Gr.getNodes().size();
197 for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
198 if (Gr.getNodes()[i]->isNodeCompletelyFolded())
199 ++NumFoldedNodes;
Chris Lattner95a80ad2002-11-07 01:54:44 +0000200 }
Chris Lattner14212332002-11-07 02:18:46 +0000201
Chris Lattneraa0b4682002-11-09 21:12:07 +0000202 DSGraph &GG = C.getGlobalsGraph();
203 TotalNumNodes += GG.getGraphSize();
204 TotalCallNodes += GG.getFunctionCalls().size();
Chris Lattnerf76e7542002-11-09 21:40:58 +0000205 if (!OnlyPrintMain) {
Chris Lattneraa0b4682002-11-09 21:12:07 +0000206 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
207 } else {
208 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
209 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
210 }
211
Chris Lattner14212332002-11-07 02:18:46 +0000212 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000213 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000214}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000215
216
217// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000218void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000219 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000220}
221
Chris Lattner97f51a32002-07-27 01:12:15 +0000222void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000223 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000224}
225
226void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000227 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000228}