blob: 82aa0055027f8e6392989085cbfc452297c4deb6 [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
16void DSNode::dump() const { print(std::cerr, 0); }
17
Chris Lattnerfccd06f2002-10-01 22:33:50 +000018static string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000019 std::stringstream OS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000020 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000021
Chris Lattnerfccd06f2002-10-01 22:33:50 +000022 for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
23 WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
24 if (N->getTypeEntries()[i].second)
25 OS << "@" << N->getTypeEntries()[i].second;
Chris Lattner76d5b482002-07-11 20:33:32 +000026 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000027 }
28
Chris Lattnerfccd06f2002-10-01 22:33:50 +000029 if (N->NodeType & DSNode::ScalarNode) OS << "S";
30 if (N->NodeType & DSNode::AllocaNode) OS << "A";
31 if (N->NodeType & DSNode::NewNode ) OS << "N";
32 if (N->NodeType & DSNode::GlobalNode) OS << "G";
33 if (N->NodeType & DSNode::Incomplete) OS << "I";
34
35 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
36 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
37 OS << "\n";
38 }
39
40 if ((N->NodeType & DSNode::ScalarNode) && G) {
Chris Lattner76d5b482002-07-11 20:33:32 +000041 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
42 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
43 E = VM.end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000044 if (I->second.getNode() == N) {
Chris Lattner76d5b482002-07-11 20:33:32 +000045 WriteAsOperand(OS, I->first, false, true, M);
Chris Lattnerfccd06f2002-10-01 22:33:50 +000046 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000047 }
48 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000049 return OS.str();
50}
51
Chris Lattner0d9bab82002-07-18 00:12:30 +000052static string getValueName(Value *V, Function &F) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000053 std::stringstream OS;
54 WriteAsOperand(OS, V, true, true, F.getParent());
55 return OS.str();
56}
57
58
59
Chris Lattner0d9bab82002-07-18 00:12:30 +000060static void replaceIn(string &S, char From, const string &To) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000061 for (unsigned i = 0; i < S.size(); )
62 if (S[i] == From) {
63 S.replace(S.begin()+i, S.begin()+i+1,
64 To.begin(), To.end());
65 i += To.size();
66 } else {
67 ++i;
68 }
69}
70
Anand Shukla6c5ed412002-07-16 00:03:10 +000071static std::string escapeLabel(const std::string &In) {
72 std::string Label(In);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000073 replaceIn(Label, '\\', "\\\\"); // Escape caption...
Chris Lattner76d5b482002-07-11 20:33:32 +000074 replaceIn(Label, '\n', "\\n");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000075 replaceIn(Label, ' ', "\\ ");
76 replaceIn(Label, '{', "\\{");
77 replaceIn(Label, '}', "\\}");
78 return Label;
79}
80
81static void writeEdge(std::ostream &O, const void *SrcNode,
82 const char *SrcNodePortName, int SrcNodeIdx,
Chris Lattnerfccd06f2002-10-01 22:33:50 +000083 const DSNodeHandle &VS,
84 const std::string &EdgeAttr = "") {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000085 O << "\tNode" << SrcNode << SrcNodePortName;
86 if (SrcNodeIdx != -1) O << SrcNodeIdx;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000087 O << " -> Node" << (void*)VS.getNode();
88 if (VS.getOffset()) O << ":g" << VS.getOffset();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000089
90 if (!EdgeAttr.empty())
91 O << "[" << EdgeAttr << "]";
92 O << ";\n";
93}
94
Chris Lattner76d5b482002-07-11 20:33:32 +000095void DSNode::print(std::ostream &O, const DSGraph *G) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000096 std::string Caption = escapeLabel(getCaption(this, G));
Chris Lattnerc68c31b2002-07-10 22:38:08 +000097
98 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
99
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000100 if (getSize() != 0) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000101 O << "|{";
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000102 for (unsigned i = 0; i < getSize(); ++i) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000103 if (i) O << "|";
Chris Lattner9cfb3582002-10-02 05:17:55 +0000104 O << "<g" << i << ">" << (int)MergeMap[i];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000105 }
106 O << "}";
107 }
108 O << "}\"];\n";
109
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000110 for (unsigned i = 0; i != getSize(); ++i)
111 if (const DSNodeHandle *DSN = getLink(i))
112 writeEdge(O, this, ":g", i, *DSN);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000113}
114
115void DSGraph::print(std::ostream &O) const {
116 O << "digraph DataStructures {\n"
117 << "\tnode [shape=Mrecord];\n"
118 << "\tedge [arrowtail=\"dot\"];\n"
119 << "\tsize=\"10,7.5\";\n"
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000120 << "\trotate=\"90\";\n";
121
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000122 if (Func != 0)
123 O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000124
125 // Output all of the nodes...
126 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000127 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000128
129 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000130
131 // Output the returned value pointer...
132 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000133 O << "\tNode0x1" << "[ plaintext=circle, label =\""
134 << escapeLabel("returning") << "\"];\n";
135 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000136 }
137
138 // Output all of the call nodes...
139 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
140 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
141 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
142 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
143 if (j) O << "|";
144 O << "<g" << j << ">";
145 }
146 O << "}}\"];\n";
147
148 for (unsigned j = 0, e = Call.size(); j != e; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000149 if (Call[j].getNode())
Chris Lattner76d5b482002-07-11 20:33:32 +0000150 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000151 }
152
153
154 O << "}\n";
155}
156
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000157
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000158void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
159 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000160 O << "Writing '" << Filename << "'...";
161 std::ofstream F(Filename.c_str());
162
163 if (F.good()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000164 print(F);
165 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000166 } else {
167 O << " error opening file for writing!\n";
168 }
169}
170
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000171static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
172
Chris Lattner0d9bab82002-07-18 00:12:30 +0000173template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000174static void printCollection(const Collection &C, std::ostream &O,
175 const Module *M, const string &Prefix) {
176 if (M == 0) {
177 O << "Null Module pointer, cannot continue!\n";
178 return;
179 }
180
181 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000182 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000183 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000184}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000185
186
187// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000188void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000189 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000190}
191
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000192#if 0
Chris Lattner97f51a32002-07-27 01:12:15 +0000193void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000194 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000195
196 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
197 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000198 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000199 break;
200 }
201}
202
203void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000204 printCollection(*this, O, M, "td.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000205
206 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
207 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000208 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000209 break;
210 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000211}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000212#endif