blob: 51ebe632eb43bedacc5a536b2055a8acb2186c87 [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 Lattnerc68c31b2002-07-10 22:38:08 +00009#include "llvm/Module.h"
10#include "llvm/Assembly/Writer.h"
Chris Lattnerdadd49b2002-07-31 17:15:40 +000011#include "Support/CommandLine.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000012#include <fstream>
13#include <sstream>
Chris Lattner0d9bab82002-07-18 00:12:30 +000014using std::string;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000015
Chris Lattner55c10582002-10-03 20:38:41 +000016// OnlyPrintMain - The DataStructure printer exposes this option to allow
17// printing of only the graph for "main".
18//
19static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
20
21
Chris Lattnerc68c31b2002-07-10 22:38:08 +000022void DSNode::dump() const { print(std::cerr, 0); }
23
Chris Lattnerfccd06f2002-10-01 22:33:50 +000024static string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000025 std::stringstream OS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000026 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000027
Chris Lattnerfccd06f2002-10-01 22:33:50 +000028 for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
29 WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
30 if (N->getTypeEntries()[i].second)
31 OS << "@" << N->getTypeEntries()[i].second;
Chris Lattner76d5b482002-07-11 20:33:32 +000032 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000033 }
34
Chris Lattnerfccd06f2002-10-01 22:33:50 +000035 if (N->NodeType & DSNode::ScalarNode) OS << "S";
36 if (N->NodeType & DSNode::AllocaNode) OS << "A";
37 if (N->NodeType & DSNode::NewNode ) OS << "N";
38 if (N->NodeType & DSNode::GlobalNode) OS << "G";
39 if (N->NodeType & DSNode::Incomplete) OS << "I";
40
41 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
42 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
43 OS << "\n";
44 }
45
46 if ((N->NodeType & DSNode::ScalarNode) && G) {
Chris Lattner76d5b482002-07-11 20:33:32 +000047 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
48 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
49 E = VM.end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000050 if (I->second.getNode() == N) {
Chris Lattner76d5b482002-07-11 20:33:32 +000051 WriteAsOperand(OS, I->first, false, true, M);
Chris Lattnerfccd06f2002-10-01 22:33:50 +000052 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000053 }
54 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000055 return OS.str();
56}
57
Chris Lattner0d9bab82002-07-18 00:12:30 +000058static string getValueName(Value *V, Function &F) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000059 std::stringstream OS;
60 WriteAsOperand(OS, V, true, true, F.getParent());
61 return OS.str();
62}
63
64
65
Chris Lattner0d9bab82002-07-18 00:12:30 +000066static void replaceIn(string &S, char From, const string &To) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000067 for (unsigned i = 0; i < S.size(); )
68 if (S[i] == From) {
69 S.replace(S.begin()+i, S.begin()+i+1,
70 To.begin(), To.end());
71 i += To.size();
72 } else {
73 ++i;
74 }
75}
76
Anand Shukla6c5ed412002-07-16 00:03:10 +000077static std::string escapeLabel(const std::string &In) {
78 std::string Label(In);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000079 replaceIn(Label, '\\', "\\\\"); // Escape caption...
Chris Lattner76d5b482002-07-11 20:33:32 +000080 replaceIn(Label, '\n', "\\n");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000081 replaceIn(Label, ' ', "\\ ");
82 replaceIn(Label, '{', "\\{");
83 replaceIn(Label, '}', "\\}");
84 return Label;
85}
86
87static void writeEdge(std::ostream &O, const void *SrcNode,
88 const char *SrcNodePortName, int SrcNodeIdx,
Chris Lattnerfccd06f2002-10-01 22:33:50 +000089 const DSNodeHandle &VS,
90 const std::string &EdgeAttr = "") {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000091 O << "\tNode" << SrcNode << SrcNodePortName;
92 if (SrcNodeIdx != -1) O << SrcNodeIdx;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000093 O << " -> Node" << (void*)VS.getNode();
94 if (VS.getOffset()) O << ":g" << VS.getOffset();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000095
96 if (!EdgeAttr.empty())
97 O << "[" << EdgeAttr << "]";
98 O << ";\n";
99}
100
Chris Lattner76d5b482002-07-11 20:33:32 +0000101void DSNode::print(std::ostream &O, const DSGraph *G) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000102 std::string Caption = escapeLabel(getCaption(this, G));
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000103
104 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
105
Chris Lattner6727ec62002-10-03 21:55:13 +0000106 unsigned Size = getSize();
107 if (Size > 64) Size = 64; // Don't print out HUGE graph nodes!
108
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000109 if (getSize() != 0) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000110 O << "|{";
Chris Lattner6727ec62002-10-03 21:55:13 +0000111 for (unsigned i = 0; i < Size; ++i) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000112 if (i) O << "|";
Chris Lattner9cfb3582002-10-02 05:17:55 +0000113 O << "<g" << i << ">" << (int)MergeMap[i];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000114 }
Chris Lattner6727ec62002-10-03 21:55:13 +0000115 if (Size != getSize())
116 O << "|truncated...";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000117 O << "}";
118 }
119 O << "}\"];\n";
120
Chris Lattner6727ec62002-10-03 21:55:13 +0000121 for (unsigned i = 0; i != Size; ++i)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000122 if (const DSNodeHandle *DSN = getLink(i))
123 writeEdge(O, this, ":g", i, *DSN);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000124}
125
126void DSGraph::print(std::ostream &O) const {
127 O << "digraph DataStructures {\n"
128 << "\tnode [shape=Mrecord];\n"
129 << "\tedge [arrowtail=\"dot\"];\n"
130 << "\tsize=\"10,7.5\";\n"
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000131 << "\trotate=\"90\";\n";
132
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000133 if (Func != 0)
134 O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000135
136 // Output all of the nodes...
137 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000138 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000139
140 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000141
142 // Output the returned value pointer...
143 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000144 O << "\tNode0x1" << "[ plaintext=circle, label =\""
145 << escapeLabel("returning") << "\"];\n";
146 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000147 }
148
149 // Output all of the call nodes...
150 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
151 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
152 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
153 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
154 if (j) O << "|";
155 O << "<g" << j << ">";
156 }
157 O << "}}\"];\n";
158
159 for (unsigned j = 0, e = Call.size(); j != e; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000160 if (Call[j].getNode())
Chris Lattner76d5b482002-07-11 20:33:32 +0000161 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000162 }
163
164
165 O << "}\n";
166}
167
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000168
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000169void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
170 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000171 O << "Writing '" << Filename << "'...";
172 std::ofstream F(Filename.c_str());
173
174 if (F.good()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000175 print(F);
176 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000177 } else {
178 O << " error opening file for writing!\n";
179 }
180}
181
Chris Lattner0d9bab82002-07-18 00:12:30 +0000182template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000183static void printCollection(const Collection &C, std::ostream &O,
184 const Module *M, const string &Prefix) {
185 if (M == 0) {
186 O << "Null Module pointer, cannot continue!\n";
187 return;
188 }
189
190 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000191 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000192 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000193}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000194
195
196// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000197void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000198 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000199}
200
Chris Lattner97f51a32002-07-27 01:12:15 +0000201void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000202 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000203#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000204 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
205 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000206 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000207 break;
208 }
Chris Lattner55c10582002-10-03 20:38:41 +0000209#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000210}
211
Chris Lattner55c10582002-10-03 20:38:41 +0000212#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000213void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000214 printCollection(*this, O, M, "td.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000215
216 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
217 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000218 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000219 break;
220 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000221}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000222#endif