blob: 528652b5fd88d315713670d5ad0d0d06dddc2523 [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"
8#include "llvm/Module.h"
9#include "llvm/Assembly/Writer.h"
Chris Lattnerdadd49b2002-07-31 17:15:40 +000010#include "Support/CommandLine.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000011#include <fstream>
12#include <sstream>
Chris Lattner0d9bab82002-07-18 00:12:30 +000013using std::string;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000014
15void DSNode::dump() const { print(std::cerr, 0); }
16
Chris Lattnerfccd06f2002-10-01 22:33:50 +000017static string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000018 std::stringstream OS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000019 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000020
Chris Lattnerfccd06f2002-10-01 22:33:50 +000021 for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
22 WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
23 if (N->getTypeEntries()[i].second)
24 OS << "@" << N->getTypeEntries()[i].second;
Chris Lattner76d5b482002-07-11 20:33:32 +000025 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000026 }
27
Chris Lattnerfccd06f2002-10-01 22:33:50 +000028 if (N->NodeType & DSNode::ScalarNode) OS << "S";
29 if (N->NodeType & DSNode::AllocaNode) OS << "A";
30 if (N->NodeType & DSNode::NewNode ) OS << "N";
31 if (N->NodeType & DSNode::GlobalNode) OS << "G";
32 if (N->NodeType & DSNode::Incomplete) OS << "I";
33
34 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
35 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
36 OS << "\n";
37 }
38
39 if ((N->NodeType & DSNode::ScalarNode) && G) {
Chris Lattner76d5b482002-07-11 20:33:32 +000040 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
41 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
42 E = VM.end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000043 if (I->second.getNode() == N) {
Chris Lattner76d5b482002-07-11 20:33:32 +000044 WriteAsOperand(OS, I->first, false, true, M);
Chris Lattnerfccd06f2002-10-01 22:33:50 +000045 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000046 }
47 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000048 return OS.str();
49}
50
Chris Lattner0d9bab82002-07-18 00:12:30 +000051static string getValueName(Value *V, Function &F) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000052 std::stringstream OS;
53 WriteAsOperand(OS, V, true, true, F.getParent());
54 return OS.str();
55}
56
57
58
Chris Lattner0d9bab82002-07-18 00:12:30 +000059static void replaceIn(string &S, char From, const string &To) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000060 for (unsigned i = 0; i < S.size(); )
61 if (S[i] == From) {
62 S.replace(S.begin()+i, S.begin()+i+1,
63 To.begin(), To.end());
64 i += To.size();
65 } else {
66 ++i;
67 }
68}
69
Anand Shukla6c5ed412002-07-16 00:03:10 +000070static std::string escapeLabel(const std::string &In) {
71 std::string Label(In);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000072 replaceIn(Label, '\\', "\\\\"); // Escape caption...
Chris Lattner76d5b482002-07-11 20:33:32 +000073 replaceIn(Label, '\n', "\\n");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000074 replaceIn(Label, ' ', "\\ ");
75 replaceIn(Label, '{', "\\{");
76 replaceIn(Label, '}', "\\}");
77 return Label;
78}
79
80static void writeEdge(std::ostream &O, const void *SrcNode,
81 const char *SrcNodePortName, int SrcNodeIdx,
Chris Lattnerfccd06f2002-10-01 22:33:50 +000082 const DSNodeHandle &VS,
83 const std::string &EdgeAttr = "") {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000084 O << "\tNode" << SrcNode << SrcNodePortName;
85 if (SrcNodeIdx != -1) O << SrcNodeIdx;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000086 O << " -> Node" << (void*)VS.getNode();
87 if (VS.getOffset()) O << ":g" << VS.getOffset();
Chris Lattnerc68c31b2002-07-10 22:38:08 +000088
89 if (!EdgeAttr.empty())
90 O << "[" << EdgeAttr << "]";
91 O << ";\n";
92}
93
Chris Lattner76d5b482002-07-11 20:33:32 +000094void DSNode::print(std::ostream &O, const DSGraph *G) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +000095 std::string Caption = escapeLabel(getCaption(this, G));
Chris Lattnerc68c31b2002-07-10 22:38:08 +000096
97 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
98
Chris Lattnerfccd06f2002-10-01 22:33:50 +000099 if (getSize() != 0) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000100 O << "|{";
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000101 for (unsigned i = 0; i < getSize(); ++i) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000102 if (i) O << "|";
Chris Lattner9cfb3582002-10-02 05:17:55 +0000103 O << "<g" << i << ">" << (int)MergeMap[i];
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000104 }
105 O << "}";
106 }
107 O << "}\"];\n";
108
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000109 for (unsigned i = 0; i != getSize(); ++i)
110 if (const DSNodeHandle *DSN = getLink(i))
111 writeEdge(O, this, ":g", i, *DSN);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000112}
113
114void DSGraph::print(std::ostream &O) const {
115 O << "digraph DataStructures {\n"
116 << "\tnode [shape=Mrecord];\n"
117 << "\tedge [arrowtail=\"dot\"];\n"
118 << "\tsize=\"10,7.5\";\n"
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000119 << "\trotate=\"90\";\n";
120
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000121 if (Func != 0)
122 O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000123
124 // Output all of the nodes...
125 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000126 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000127
128 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000129
130 // Output the returned value pointer...
131 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000132 O << "\tNode0x1" << "[ plaintext=circle, label =\""
133 << escapeLabel("returning") << "\"];\n";
134 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000135 }
136
137 // Output all of the call nodes...
138 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
139 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
140 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
141 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
142 if (j) O << "|";
143 O << "<g" << j << ">";
144 }
145 O << "}}\"];\n";
146
147 for (unsigned j = 0, e = Call.size(); j != e; ++j)
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000148 if (Call[j].getNode())
Chris Lattner76d5b482002-07-11 20:33:32 +0000149 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000150 }
151
152
153 O << "}\n";
154}
155
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000156
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000157void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
158 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000159 O << "Writing '" << Filename << "'...";
160 std::ofstream F(Filename.c_str());
161
162 if (F.good()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000163 print(F);
164 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000165 } else {
166 O << " error opening file for writing!\n";
167 }
168}
169
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000170static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
171
Chris Lattner0d9bab82002-07-18 00:12:30 +0000172template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000173static void printCollection(const Collection &C, std::ostream &O,
174 const Module *M, const string &Prefix) {
175 if (M == 0) {
176 O << "Null Module pointer, cannot continue!\n";
177 return;
178 }
179
180 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000181 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000182 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000183}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000184
185
186// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000187void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000188 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000189}
190
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000191#if 0
Chris Lattner97f51a32002-07-27 01:12:15 +0000192void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000193 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000194
195 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
196 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000197 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000198 break;
199 }
200}
201
202void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000203 printCollection(*this, O, M, "td.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000204
205 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
206 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000207 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000208 break;
209 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000210}
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000211#endif