blob: 986c28d0a8878bfd7def6121e61ca0ad2a286eca [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 Lattner0d9bab82002-07-18 00:12:30 +000017string DSNode::getCaption(const DSGraph *G) const {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000018 std::stringstream OS;
Vikram S. Advedfd2f322002-07-30 22:07:26 +000019 Module *M = G && &G->getFunction()? G->getFunction().getParent() : 0;
Chris Lattner76d5b482002-07-11 20:33:32 +000020 WriteTypeSymbolic(OS, getType(), M);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000021
22 OS << " ";
23 if (NodeType & ScalarNode) OS << "S";
24 if (NodeType & AllocaNode) OS << "A";
25 if (NodeType & NewNode ) OS << "N";
26 if (NodeType & GlobalNode) OS << "G";
27 if (NodeType & SubElement) OS << "E";
28 if (NodeType & CastNode ) OS << "C";
Chris Lattner0d9bab82002-07-18 00:12:30 +000029 if (NodeType & Incomplete) OS << "I";
Chris Lattnerc68c31b2002-07-10 22:38:08 +000030
Chris Lattner76d5b482002-07-11 20:33:32 +000031 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
32 OS << "\n";
33 WriteAsOperand(OS, Globals[i], false, true, M);
34 }
35
36 if ((NodeType & ScalarNode) && G) {
37 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
38 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
39 E = VM.end(); I != E; ++I)
40 if (I->second == this) {
41 OS << "\n";
42 WriteAsOperand(OS, I->first, false, true, M);
43 }
44 }
45
Chris Lattnerc68c31b2002-07-10 22:38:08 +000046 return OS.str();
47}
48
Chris Lattner0d9bab82002-07-18 00:12:30 +000049static string getValueName(Value *V, Function &F) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000050 std::stringstream OS;
51 WriteAsOperand(OS, V, true, true, F.getParent());
52 return OS.str();
53}
54
55
56
Chris Lattner0d9bab82002-07-18 00:12:30 +000057static void replaceIn(string &S, char From, const string &To) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000058 for (unsigned i = 0; i < S.size(); )
59 if (S[i] == From) {
60 S.replace(S.begin()+i, S.begin()+i+1,
61 To.begin(), To.end());
62 i += To.size();
63 } else {
64 ++i;
65 }
66}
67
Anand Shukla6c5ed412002-07-16 00:03:10 +000068static std::string escapeLabel(const std::string &In) {
69 std::string Label(In);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000070 replaceIn(Label, '\\', "\\\\"); // Escape caption...
Chris Lattner76d5b482002-07-11 20:33:32 +000071 replaceIn(Label, '\n', "\\n");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000072 replaceIn(Label, ' ', "\\ ");
73 replaceIn(Label, '{', "\\{");
74 replaceIn(Label, '}', "\\}");
75 return Label;
76}
77
78static void writeEdge(std::ostream &O, const void *SrcNode,
79 const char *SrcNodePortName, int SrcNodeIdx,
Anand Shukla6c5ed412002-07-16 00:03:10 +000080 const DSNode *VS, const std::string &EdgeAttr = "") {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000081 O << "\tNode" << SrcNode << SrcNodePortName;
82 if (SrcNodeIdx != -1) O << SrcNodeIdx;
83 O << " -> Node" << (void*)VS;
84
85 if (!EdgeAttr.empty())
86 O << "[" << EdgeAttr << "]";
87 O << ";\n";
88}
89
Chris Lattner76d5b482002-07-11 20:33:32 +000090void DSNode::print(std::ostream &O, const DSGraph *G) const {
Anand Shukla6c5ed412002-07-16 00:03:10 +000091 std::string Caption = escapeLabel(getCaption(G));
Chris Lattnerc68c31b2002-07-10 22:38:08 +000092
93 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
94
95 if (!Links.empty()) {
96 O << "|{";
97 for (unsigned i = 0; i < Links.size(); ++i) {
98 if (i) O << "|";
99 O << "<g" << i << ">";
100 }
101 O << "}";
102 }
103 O << "}\"];\n";
104
105 for (unsigned i = 0; i < Links.size(); ++i)
106 if (Links[i])
107 writeEdge(O, this, ":g", i, Links[i]);
108}
109
110void DSGraph::print(std::ostream &O) const {
111 O << "digraph DataStructures {\n"
112 << "\tnode [shape=Mrecord];\n"
113 << "\tedge [arrowtail=\"dot\"];\n"
114 << "\tsize=\"10,7.5\";\n"
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000115 << "\trotate=\"90\";\n";
116
117 if (&Func != 0)
118 O << "\tlabel=\"Function\\ " << Func.getName() << "\";\n\n";
119 else
120 O << "\tlabel=\"Global Graph\";\n\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000121
122 // Output all of the nodes...
123 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000124 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000125
126 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000127
128 // Output the returned value pointer...
129 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000130 O << "\tNode0x1" << "[ plaintext=circle, label =\""
131 << escapeLabel("returning") << "\"];\n";
132 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000133 }
134
135 // Output all of the call nodes...
136 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
137 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
138 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
139 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
140 if (j) O << "|";
141 O << "<g" << j << ">";
142 }
143 O << "}}\"];\n";
144
145 for (unsigned j = 0, e = Call.size(); j != e; ++j)
146 if (Call[j])
Chris Lattner76d5b482002-07-11 20:33:32 +0000147 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000148 }
149
150
151 O << "}\n";
152}
153
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000154
155static void printGraph(const DSGraph &Graph, std::ostream &O,
156 const string &GraphName, const string &Prefix) {
157 string Filename = Prefix + "." + GraphName + ".dot";
158 O << "Writing '" << Filename << "'...";
159 std::ofstream F(Filename.c_str());
160
161 if (F.good()) {
162 Graph.print(F);
163 O << " [" << Graph.getGraphSize() << "+"
164 << Graph.getFunctionCalls().size() << "]\n";
165 } 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))
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000182 printGraph(C.getDSGraph((Function&)*I), O, I->getName(), Prefix);
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 Lattner0d9bab82002-07-18 00:12:30 +0000188 printCollection(*this, O, M, "ds");
189}
190
Chris Lattner97f51a32002-07-27 01:12:15 +0000191void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000192 printCollection(*this, O, M, "bu");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000193
194 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
195 if (!I->isExternal()) {
196 printGraph(*getDSGraph(*I).GlobalsGraph, O, "program", "gg");
197 break;
198 }
199}
200
201void TDDataStructures::print(std::ostream &O, const Module *M) const {
202 printCollection(*this, O, M, "td");
203
204 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
205 if (!I->isExternal()) {
206 printGraph(*getDSGraph(*I).GlobalsGraph, O, "program", "gg");
207 break;
208 }
Chris Lattner0d9bab82002-07-18 00:12:30 +0000209}