blob: 15f8df19ef10c57dda2311be183ffc5e4230015c [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"
10#include <fstream>
11#include <sstream>
12
13void DSNode::dump() const { print(std::cerr, 0); }
14
Chris Lattner76d5b482002-07-11 20:33:32 +000015std::string DSNode::getCaption(const DSGraph *G) const {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000016 std::stringstream OS;
Chris Lattner76d5b482002-07-11 20:33:32 +000017 Module *M = G ? G->getFunction().getParent() : 0;
18 WriteTypeSymbolic(OS, getType(), M);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000019
20 OS << " ";
21 if (NodeType & ScalarNode) OS << "S";
22 if (NodeType & AllocaNode) OS << "A";
23 if (NodeType & NewNode ) OS << "N";
24 if (NodeType & GlobalNode) OS << "G";
25 if (NodeType & SubElement) OS << "E";
26 if (NodeType & CastNode ) OS << "C";
27
Chris Lattner76d5b482002-07-11 20:33:32 +000028 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
29 OS << "\n";
30 WriteAsOperand(OS, Globals[i], false, true, M);
31 }
32
33 if ((NodeType & ScalarNode) && G) {
34 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
35 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
36 E = VM.end(); I != E; ++I)
37 if (I->second == this) {
38 OS << "\n";
39 WriteAsOperand(OS, I->first, false, true, M);
40 }
41 }
42
Chris Lattnerc68c31b2002-07-10 22:38:08 +000043 return OS.str();
44}
45
46static std::string getValueName(Value *V, Function &F) {
47 std::stringstream OS;
48 WriteAsOperand(OS, V, true, true, F.getParent());
49 return OS.str();
50}
51
52
53
54static void replaceIn(std::string &S, char From, const std::string &To) {
55 for (unsigned i = 0; i < S.size(); )
56 if (S[i] == From) {
57 S.replace(S.begin()+i, S.begin()+i+1,
58 To.begin(), To.end());
59 i += To.size();
60 } else {
61 ++i;
62 }
63}
64
Anand Shukla6c5ed412002-07-16 00:03:10 +000065static std::string escapeLabel(const std::string &In) {
66 std::string Label(In);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000067 replaceIn(Label, '\\', "\\\\"); // Escape caption...
Chris Lattner76d5b482002-07-11 20:33:32 +000068 replaceIn(Label, '\n', "\\n");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000069 replaceIn(Label, ' ', "\\ ");
70 replaceIn(Label, '{', "\\{");
71 replaceIn(Label, '}', "\\}");
72 return Label;
73}
74
75static void writeEdge(std::ostream &O, const void *SrcNode,
76 const char *SrcNodePortName, int SrcNodeIdx,
Anand Shukla6c5ed412002-07-16 00:03:10 +000077 const DSNode *VS, const std::string &EdgeAttr = "") {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000078 O << "\tNode" << SrcNode << SrcNodePortName;
79 if (SrcNodeIdx != -1) O << SrcNodeIdx;
80 O << " -> Node" << (void*)VS;
81
82 if (!EdgeAttr.empty())
83 O << "[" << EdgeAttr << "]";
84 O << ";\n";
85}
86
Chris Lattner76d5b482002-07-11 20:33:32 +000087void DSNode::print(std::ostream &O, const DSGraph *G) const {
Anand Shukla6c5ed412002-07-16 00:03:10 +000088 std::string Caption = escapeLabel(getCaption(G));
Chris Lattnerc68c31b2002-07-10 22:38:08 +000089
90 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
91
92 if (!Links.empty()) {
93 O << "|{";
94 for (unsigned i = 0; i < Links.size(); ++i) {
95 if (i) O << "|";
96 O << "<g" << i << ">";
97 }
98 O << "}";
99 }
100 O << "}\"];\n";
101
102 for (unsigned i = 0; i < Links.size(); ++i)
103 if (Links[i])
104 writeEdge(O, this, ":g", i, Links[i]);
105}
106
107void DSGraph::print(std::ostream &O) const {
108 O << "digraph DataStructures {\n"
109 << "\tnode [shape=Mrecord];\n"
110 << "\tedge [arrowtail=\"dot\"];\n"
111 << "\tsize=\"10,7.5\";\n"
112 << "\trotate=\"90\";\n"
113 << "\tlabel=\"Function\\ " << Func.getName() << "\";\n\n";
114
115 // Output all of the nodes...
116 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000117 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000118
119 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000120
121 // Output the returned value pointer...
122 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000123 O << "\tNode0x1" << "[ plaintext=circle, label =\""
124 << escapeLabel("returning") << "\"];\n";
125 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000126 }
127
128 // Output all of the call nodes...
129 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
130 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
131 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
132 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
133 if (j) O << "|";
134 O << "<g" << j << ">";
135 }
136 O << "}}\"];\n";
137
138 for (unsigned j = 0, e = Call.size(); j != e; ++j)
139 if (Call[j])
Chris Lattner76d5b482002-07-11 20:33:32 +0000140 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000141 }
142
143
144 O << "}\n";
145}
146
147
148
149
150// print - Print out the analysis results...
151void LocalDataStructures::print(std::ostream &O, Module *M) const {
152 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
153 if (!I->isExternal()) {
154 std::string Filename = "ds." + I->getName() + ".dot";
155 O << "Writing '" << Filename << "'...";
156 std::ofstream F(Filename.c_str());
157
158 if (F.good()) {
159 DSGraph &Graph = getDSGraph(*I);
160 Graph.print(F);
161 O << " [" << Graph.getGraphSize() << "]\n";
162 } else {
163 O << " error opening file for writing!\n";
164 }
165 }
166}