blob: b78861c13470dc13a994079870ad531033649642 [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 Lattnerfccd06f2002-10-01 22:33:50 +0000106 if (getSize() != 0) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000107 O << "|{";
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000108 for (unsigned i = 0; i < getSize(); ++i) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000109 if (i) O << "|";
Chris Lattner9cfb3582002-10-02 05:17:55 +0000110 O << "<g" << i << ">" << (int)MergeMap[i];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000111 }
112 O << "}";
113 }
114 O << "}\"];\n";
115
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000116 for (unsigned i = 0; i != getSize(); ++i)
117 if (const DSNodeHandle *DSN = getLink(i))
118 writeEdge(O, this, ":g", i, *DSN);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000119}
120
121void DSGraph::print(std::ostream &O) const {
122 O << "digraph DataStructures {\n"
123 << "\tnode [shape=Mrecord];\n"
124 << "\tedge [arrowtail=\"dot\"];\n"
125 << "\tsize=\"10,7.5\";\n"
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000126 << "\trotate=\"90\";\n";
127
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000128 if (Func != 0)
129 O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000130
131 // Output all of the nodes...
132 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000133 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000134
135 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000136
137 // Output the returned value pointer...
138 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000139 O << "\tNode0x1" << "[ plaintext=circle, label =\""
140 << escapeLabel("returning") << "\"];\n";
141 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000142 }
143
144 // Output all of the call nodes...
145 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
146 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
147 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
148 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
149 if (j) O << "|";
150 O << "<g" << j << ">";
151 }
152 O << "}}\"];\n";
153
154 for (unsigned j = 0, e = Call.size(); j != e; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000155 if (Call[j].getNode())
Chris Lattner76d5b482002-07-11 20:33:32 +0000156 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000157 }
158
159
160 O << "}\n";
161}
162
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000163
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000164void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
165 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000166 O << "Writing '" << Filename << "'...";
167 std::ofstream F(Filename.c_str());
168
169 if (F.good()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000170 print(F);
171 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000172 } else {
173 O << " error opening file for writing!\n";
174 }
175}
176
Chris Lattner0d9bab82002-07-18 00:12:30 +0000177template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000178static void printCollection(const Collection &C, std::ostream &O,
179 const Module *M, const string &Prefix) {
180 if (M == 0) {
181 O << "Null Module pointer, cannot continue!\n";
182 return;
183 }
184
185 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000186 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000187 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000188}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000189
190
191// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000192void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000193 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000194}
195
Chris Lattner97f51a32002-07-27 01:12:15 +0000196void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000197 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000198#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000199 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
200 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000201 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000202 break;
203 }
Chris Lattner55c10582002-10-03 20:38:41 +0000204#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000205}
206
Chris Lattner55c10582002-10-03 20:38:41 +0000207#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000208void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000209 printCollection(*this, O, M, "td.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000210
211 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
212 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000213 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000214 break;
215 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000216}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000217#endif