blob: d8856b83903df8d2a69ab63a561a030adb231023 [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>
Chris Lattner0d9bab82002-07-18 00:12:30 +000017using std::string;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000018
Chris Lattner55c10582002-10-03 20:38:41 +000019// OnlyPrintMain - The DataStructure printer exposes this option to allow
20// printing of only the graph for "main".
21//
Chris Lattner49a1ed02002-11-18 21:42:45 +000022namespace {
23 cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
24 Statistic<> MaxGraphSize ("dsnode", "Maximum graph size");
25 Statistic<> NumFoldedNodes ("dsnode", "Number of folded nodes (in final graph)");
26}
Chris Lattner55c10582002-10-03 20:38:41 +000027
28
Chris Lattnerc68c31b2002-07-10 22:38:08 +000029void DSNode::dump() const { print(std::cerr, 0); }
30
Chris Lattnerfccd06f2002-10-01 22:33:50 +000031static string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000032 std::stringstream OS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000033 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000034
Chris Lattner08db7192002-11-06 06:20:27 +000035 if (N->isNodeCompletelyFolded())
36 OS << "FOLDED";
37 else {
Chris Lattner49a1ed02002-11-18 21:42:45 +000038 WriteTypeSymbolic(OS, N->getType(), M);
39 if (N->isArray())
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000040 OS << " array";
Chris Lattner08db7192002-11-06 06:20:27 +000041 }
42 if (N->NodeType) {
43 OS << ": ";
44 if (N->NodeType & DSNode::AllocaNode ) OS << "S";
45 if (N->NodeType & DSNode::HeapNode ) OS << "H";
46 if (N->NodeType & DSNode::GlobalNode ) OS << "G";
47 if (N->NodeType & DSNode::UnknownNode) OS << "U";
48 if (N->NodeType & DSNode::Incomplete ) OS << "I";
49 if (N->NodeType & DSNode::Modified ) OS << "M";
50 if (N->NodeType & DSNode::Read ) OS << "R";
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
68 return "Non-function graph";
69 }
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 Lattner92673292002-11-02 00:13:20 +000090 // Add scalar nodes to the graph...
Chris Lattnerc875f022002-11-03 21:27:48 +000091 const std::map<Value*, DSNodeHandle> &VM = G->getScalarMap();
Chris Lattner92673292002-11-02 00:13:20 +000092 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
93 I != VM.end(); ++I)
94 if (!isa<GlobalValue>(I->first)) {
95 std::stringstream OS;
96 WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
Chris Lattner49a1ed02002-11-18 21:42:45 +000097 GW.emitSimpleNode(I->first, "", OS.str());
Chris Lattner92673292002-11-02 00:13:20 +000098
99 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000100 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000101 if (EdgeDest == 0) EdgeDest = -1;
102 GW.emitEdge(I->first, -1, I->second.getNode(),
103 EdgeDest, "arrowtail=tee,color=gray63");
104 }
105
106
Chris Lattnereb265cd2002-10-16 02:04:36 +0000107 // Output the returned value pointer...
108 if (G->getRetNode().getNode() != 0) {
109 // Output the return node...
110 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
111
112 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000113 int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
Chris Lattnereb265cd2002-10-16 02:04:36 +0000114 if (RetEdgeDest == 0) RetEdgeDest = -1;
115 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
116 RetEdgeDest, "arrowtail=tee,color=gray63");
117 }
Chris Lattner962ee452002-10-16 20:16:16 +0000118
119 // Output all of the call nodes...
Chris Lattner4f7815f2002-11-10 06:53:59 +0000120 const std::vector<DSCallSite> &FCs =
121 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
122 : G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000123 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000124 const DSCallSite &Call = FCs[i];
Chris Lattner0969c502002-10-21 02:08:03 +0000125 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
Chris Lattner962ee452002-10-16 20:16:16 +0000126
Chris Lattner482b6512002-10-21 13:47:57 +0000127 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000128 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000129 if (EdgeDest == 0) EdgeDest = -1;
130 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
131 }
132 if (DSNode *N = Call.getCallee().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000133 int EdgeDest = Call.getCallee().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000134 if (EdgeDest == 0) EdgeDest = -1;
135 GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
136 }
Chris Lattner0969c502002-10-21 02:08:03 +0000137 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
138 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000139 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000140 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner0969c502002-10-21 02:08:03 +0000141 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
Chris Lattner962ee452002-10-16 20:16:16 +0000142 }
143 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000144 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000145};
146
Chris Lattnere17a4e82002-10-17 01:02:46 +0000147void DSNode::print(std::ostream &O, const DSGraph *G) const {
148 GraphWriter<const DSGraph *> W(O, G);
149 W.writeNode(this);
150}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000151
Chris Lattnere17a4e82002-10-17 01:02:46 +0000152void DSGraph::print(std::ostream &O) const {
153 WriteGraph(O, this, "DataStructures");
154}
155
156void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000157 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,
173 const Module *M, const string &Prefix) {
174 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}