blob: e683c358e2f6daad014bcc09a2df172eca7bb905 [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 Lattnerfccd06f2002-10-01 22:33:50 +000030 for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
Chris Lattnera3f85862002-10-18 18:22:46 +000031 WriteTypeSymbolic(OS, N->getTypeEntries()[i].Ty, M);
32 if (N->getTypeEntries()[i].Offset)
33 OS << "@" << N->getTypeEntries()[i].Offset;
Chris Lattneraf4800b2002-10-20 20:29:10 +000034 if (N->getTypeEntries()[i].isArray)
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000035 OS << " array";
Chris Lattner76d5b482002-07-11 20:33:32 +000036 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000037 }
38
Chris Lattneref69ccf2002-11-03 19:46:15 +000039 if (N->NodeType & DSNode::AllocaNode ) OS << "S";
Chris Lattnerd18f3422002-11-03 21:24:04 +000040 if (N->NodeType & DSNode::HeapNode ) OS << "H";
Chris Lattner5af344d2002-11-02 00:36:03 +000041 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 Lattnerfccd06f2002-10-01 22:33:50 +000046
47 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
48 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
49 OS << "\n";
50 }
51
Chris Lattnerc68c31b2002-07-10 22:38:08 +000052 return OS.str();
53}
54
Chris Lattnerf6c52db2002-10-13 19:31:57 +000055template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000056struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
57 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000058 if (G->hasFunction())
59 return "Function " + G->getFunction().getName();
60 else
61 return "Non-function graph";
62 }
63
Chris Lattnere17a4e82002-10-17 01:02:46 +000064 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000065 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000066 "\tsize=\"10,7.5\";\n"
67 "\trotate=\"90\";\n";
68 }
69
Chris Lattnere17a4e82002-10-17 01:02:46 +000070 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000071 return getCaption(Node, Graph);
72 }
73
Chris Lattnere17a4e82002-10-17 01:02:46 +000074 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000075 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000076 }
77
Chris Lattnere17a4e82002-10-17 01:02:46 +000078 static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000079 assert(Node == I.getNode() && "Iterator not for this node!");
Chris Lattnerff5feed2002-10-16 01:43:11 +000080 return Node->getMergeMapLabel(I.getOffset());
Chris Lattnerf29e3072002-10-16 01:18:27 +000081 }
Chris Lattnereb265cd2002-10-16 02:04:36 +000082
83 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
84 /// and the return node.
85 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000086 static void addCustomGraphFeatures(const DSGraph *G,
87 GraphWriter<const DSGraph*> &GW) {
Chris Lattner92673292002-11-02 00:13:20 +000088 // Add scalar nodes to the graph...
Chris Lattnerc875f022002-11-03 21:27:48 +000089 const std::map<Value*, DSNodeHandle> &VM = G->getScalarMap();
Chris Lattner92673292002-11-02 00:13:20 +000090 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
91 I != VM.end(); ++I)
92 if (!isa<GlobalValue>(I->first)) {
93 std::stringstream OS;
94 WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
95 GW.emitSimpleNode(I->first, "plaintext=circle", OS.str());
96
97 // Add edge from return node to real destination
98 int EdgeDest = I->second.getOffset();
99 if (EdgeDest == 0) EdgeDest = -1;
100 GW.emitEdge(I->first, -1, I->second.getNode(),
101 EdgeDest, "arrowtail=tee,color=gray63");
102 }
103
104
Chris Lattnereb265cd2002-10-16 02:04:36 +0000105 // Output the returned value pointer...
106 if (G->getRetNode().getNode() != 0) {
107 // Output the return node...
108 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
109
110 // Add edge from return node to real destination
111 int RetEdgeDest = G->getRetNode().getOffset();
112 if (RetEdgeDest == 0) RetEdgeDest = -1;
113 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
114 RetEdgeDest, "arrowtail=tee,color=gray63");
115 }
Chris Lattner962ee452002-10-16 20:16:16 +0000116
117 // Output all of the call nodes...
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000118 const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000119 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000120 const DSCallSite &Call = FCs[i];
Chris Lattner0969c502002-10-21 02:08:03 +0000121 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
Chris Lattner962ee452002-10-16 20:16:16 +0000122
Chris Lattner482b6512002-10-21 13:47:57 +0000123 if (DSNode *N = Call.getRetVal().getNode()) {
124 int EdgeDest = Call.getRetVal().getOffset();
125 if (EdgeDest == 0) EdgeDest = -1;
126 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
127 }
128 if (DSNode *N = Call.getCallee().getNode()) {
129 int EdgeDest = Call.getCallee().getOffset();
130 if (EdgeDest == 0) EdgeDest = -1;
131 GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
132 }
Chris Lattner0969c502002-10-21 02:08:03 +0000133 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
134 if (DSNode *N = Call.getPtrArg(j).getNode()) {
135 int EdgeDest = Call.getPtrArg(j).getOffset();
Chris Lattner962ee452002-10-16 20:16:16 +0000136 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner0969c502002-10-21 02:08:03 +0000137 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
Chris Lattner962ee452002-10-16 20:16:16 +0000138 }
139 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000140 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000141};
142
Chris Lattnere17a4e82002-10-17 01:02:46 +0000143void DSNode::print(std::ostream &O, const DSGraph *G) const {
144 GraphWriter<const DSGraph *> W(O, G);
145 W.writeNode(this);
146}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000147
Chris Lattnere17a4e82002-10-17 01:02:46 +0000148void DSGraph::print(std::ostream &O) const {
149 WriteGraph(O, this, "DataStructures");
150}
151
152void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000153 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000154 O << "Writing '" << Filename << "'...";
155 std::ofstream F(Filename.c_str());
156
157 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000158 print(F);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000159 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000160 } else {
161 O << " error opening file for writing!\n";
162 }
163}
164
Chris Lattner0d9bab82002-07-18 00:12:30 +0000165template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000166static void printCollection(const Collection &C, std::ostream &O,
167 const Module *M, const string &Prefix) {
168 if (M == 0) {
169 O << "Null Module pointer, cannot continue!\n";
170 return;
171 }
172
173 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000174 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000175 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000176}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000177
178
179// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000180void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000181 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000182}
183
Chris Lattner97f51a32002-07-27 01:12:15 +0000184void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000185 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000186#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000187 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
188 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000189 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000190 break;
191 }
Chris Lattner55c10582002-10-03 20:38:41 +0000192#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000193}
194
195void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000196 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000197#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000198 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
199 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000200 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000201 break;
202 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000203#endif
Chris Lattnere25ab832002-10-17 04:24:30 +0000204}