blob: 962a0935e09e2e523f5d7dc1b8fde370271f34d7 [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 Lattnerc68c31b2002-07-10 22:38:08 +000014#include <fstream>
15#include <sstream>
Chris Lattner0d9bab82002-07-18 00:12:30 +000016using std::string;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017
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//
21static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
22
23
Chris Lattnerc68c31b2002-07-10 22:38:08 +000024void DSNode::dump() const { print(std::cerr, 0); }
25
Chris Lattnerfccd06f2002-10-01 22:33:50 +000026static string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000027 std::stringstream OS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000028 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000029
Chris Lattner08db7192002-11-06 06:20:27 +000030 if (N->isNodeCompletelyFolded())
31 OS << "FOLDED";
32 else {
33 WriteTypeSymbolic(OS, N->getType().Ty, M);
34 if (N->getType().isArray)
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000035 OS << " array";
Chris Lattner08db7192002-11-06 06:20:27 +000036 }
37 if (N->NodeType) {
38 OS << ": ";
39 if (N->NodeType & DSNode::AllocaNode ) OS << "S";
40 if (N->NodeType & DSNode::HeapNode ) OS << "H";
41 if (N->NodeType & DSNode::GlobalNode ) OS << "G";
42 if (N->NodeType & DSNode::UnknownNode) OS << "U";
43 if (N->NodeType & DSNode::Incomplete ) OS << "I";
44 if (N->NodeType & DSNode::Modified ) OS << "M";
45 if (N->NodeType & DSNode::Read ) OS << "R";
Chris Lattner76d5b482002-07-11 20:33:32 +000046 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000047 }
48
Chris Lattnerfccd06f2002-10-01 22:33:50 +000049 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
50 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
51 OS << "\n";
52 }
53
Chris Lattnerc68c31b2002-07-10 22:38:08 +000054 return OS.str();
55}
56
Chris Lattnerf6c52db2002-10-13 19:31:57 +000057template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000058struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
59 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000060 if (G->hasFunction())
61 return "Function " + G->getFunction().getName();
62 else
63 return "Non-function graph";
64 }
65
Chris Lattnere17a4e82002-10-17 01:02:46 +000066 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000067 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000068 "\tsize=\"10,7.5\";\n"
69 "\trotate=\"90\";\n";
70 }
71
Chris Lattnere17a4e82002-10-17 01:02:46 +000072 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000073 return getCaption(Node, Graph);
74 }
75
Chris Lattnere17a4e82002-10-17 01:02:46 +000076 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000077 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000078 }
79
Chris Lattnereb265cd2002-10-16 02:04:36 +000080 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
81 /// and the return node.
82 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000083 static void addCustomGraphFeatures(const DSGraph *G,
84 GraphWriter<const DSGraph*> &GW) {
Chris Lattner92673292002-11-02 00:13:20 +000085 // Add scalar nodes to the graph...
Chris Lattnerc875f022002-11-03 21:27:48 +000086 const std::map<Value*, DSNodeHandle> &VM = G->getScalarMap();
Chris Lattner92673292002-11-02 00:13:20 +000087 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
88 I != VM.end(); ++I)
89 if (!isa<GlobalValue>(I->first)) {
90 std::stringstream OS;
91 WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
92 GW.emitSimpleNode(I->first, "plaintext=circle", OS.str());
93
94 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +000095 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +000096 if (EdgeDest == 0) EdgeDest = -1;
97 GW.emitEdge(I->first, -1, I->second.getNode(),
98 EdgeDest, "arrowtail=tee,color=gray63");
99 }
100
101
Chris Lattnereb265cd2002-10-16 02:04:36 +0000102 // Output the returned value pointer...
103 if (G->getRetNode().getNode() != 0) {
104 // Output the return node...
105 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
106
107 // Add edge from return node to real destination
Chris Lattner08db7192002-11-06 06:20:27 +0000108 int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
Chris Lattnereb265cd2002-10-16 02:04:36 +0000109 if (RetEdgeDest == 0) RetEdgeDest = -1;
110 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
111 RetEdgeDest, "arrowtail=tee,color=gray63");
112 }
Chris Lattner962ee452002-10-16 20:16:16 +0000113
114 // Output all of the call nodes...
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000115 const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000116 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000117 const DSCallSite &Call = FCs[i];
Chris Lattner0969c502002-10-21 02:08:03 +0000118 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
Chris Lattner962ee452002-10-16 20:16:16 +0000119
Chris Lattner482b6512002-10-21 13:47:57 +0000120 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000121 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000122 if (EdgeDest == 0) EdgeDest = -1;
123 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
124 }
125 if (DSNode *N = Call.getCallee().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000126 int EdgeDest = Call.getCallee().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000127 if (EdgeDest == 0) EdgeDest = -1;
128 GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
129 }
Chris Lattner0969c502002-10-21 02:08:03 +0000130 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
131 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000132 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000133 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner0969c502002-10-21 02:08:03 +0000134 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
Chris Lattner962ee452002-10-16 20:16:16 +0000135 }
136 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000137 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000138};
139
Chris Lattnere17a4e82002-10-17 01:02:46 +0000140void DSNode::print(std::ostream &O, const DSGraph *G) const {
141 GraphWriter<const DSGraph *> W(O, G);
142 W.writeNode(this);
143}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000144
Chris Lattnere17a4e82002-10-17 01:02:46 +0000145void DSGraph::print(std::ostream &O) const {
146 WriteGraph(O, this, "DataStructures");
147}
148
149void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000150 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000151 O << "Writing '" << Filename << "'...";
152 std::ofstream F(Filename.c_str());
153
154 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000155 print(F);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000156 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000157 } else {
158 O << " error opening file for writing!\n";
159 }
160}
161
Chris Lattner0d9bab82002-07-18 00:12:30 +0000162template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000163static void printCollection(const Collection &C, std::ostream &O,
164 const Module *M, const string &Prefix) {
165 if (M == 0) {
166 O << "Null Module pointer, cannot continue!\n";
167 return;
168 }
169
Chris Lattner14212332002-11-07 02:18:46 +0000170 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000171 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner95a80ad2002-11-07 01:54:44 +0000172 if (!I->isExternal()) {
173 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner14212332002-11-07 02:18:46 +0000174 TotalNumNodes += Gr.getGraphSize();
175 TotalCallNodes += Gr.getFunctionCalls().size();
Chris Lattner95a80ad2002-11-07 01:54:44 +0000176 if (I->getName() == "main" || !OnlyPrintMain)
177 Gr.writeGraphToFile(O, Prefix+I->getName());
178 else {
179 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
180 << Gr.getGraphSize() << "+" << Gr.getFunctionCalls().size() << "]\n";
181 }
182 }
Chris Lattner14212332002-11-07 02:18:46 +0000183
184 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000185 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000186}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000187
188
189// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000190void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000191 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000192}
193
Chris Lattner97f51a32002-07-27 01:12:15 +0000194void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000195 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000196#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000197 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
198 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000199 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000200 break;
201 }
Chris Lattner55c10582002-10-03 20:38:41 +0000202#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000203}
204
205void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000206 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000207#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000208 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
209 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000210 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000211 break;
212 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000213#endif
Chris Lattnere25ab832002-10-17 04:24:30 +0000214}