blob: 03ce297c4a9dad198e1297b7266724743b85e93f [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>
Chris Lattner0d9bab82002-07-18 00:12:30 +000012using std::string;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000013
14void DSNode::dump() const { print(std::cerr, 0); }
15
Chris Lattner0d9bab82002-07-18 00:12:30 +000016string DSNode::getCaption(const DSGraph *G) const {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017 std::stringstream OS;
Chris Lattner76d5b482002-07-11 20:33:32 +000018 Module *M = G ? G->getFunction().getParent() : 0;
19 WriteTypeSymbolic(OS, getType(), M);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000020
21 OS << " ";
22 if (NodeType & ScalarNode) OS << "S";
23 if (NodeType & AllocaNode) OS << "A";
24 if (NodeType & NewNode ) OS << "N";
25 if (NodeType & GlobalNode) OS << "G";
26 if (NodeType & SubElement) OS << "E";
27 if (NodeType & CastNode ) OS << "C";
Chris Lattner0d9bab82002-07-18 00:12:30 +000028 if (NodeType & Incomplete) OS << "I";
Chris Lattnerc68c31b2002-07-10 22:38:08 +000029
Chris Lattner76d5b482002-07-11 20:33:32 +000030 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
31 OS << "\n";
32 WriteAsOperand(OS, Globals[i], false, true, M);
33 }
34
35 if ((NodeType & ScalarNode) && G) {
36 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
37 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
38 E = VM.end(); I != E; ++I)
39 if (I->second == this) {
40 OS << "\n";
41 WriteAsOperand(OS, I->first, false, true, M);
42 }
43 }
44
Chris Lattnerc68c31b2002-07-10 22:38:08 +000045 return OS.str();
46}
47
Chris Lattner0d9bab82002-07-18 00:12:30 +000048static string getValueName(Value *V, Function &F) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000049 std::stringstream OS;
50 WriteAsOperand(OS, V, true, true, F.getParent());
51 return OS.str();
52}
53
54
55
Chris Lattner0d9bab82002-07-18 00:12:30 +000056static void replaceIn(string &S, char From, const string &To) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000057 for (unsigned i = 0; i < S.size(); )
58 if (S[i] == From) {
59 S.replace(S.begin()+i, S.begin()+i+1,
60 To.begin(), To.end());
61 i += To.size();
62 } else {
63 ++i;
64 }
65}
66
Anand Shukla6c5ed412002-07-16 00:03:10 +000067static std::string escapeLabel(const std::string &In) {
68 std::string Label(In);
Chris Lattnerc68c31b2002-07-10 22:38:08 +000069 replaceIn(Label, '\\', "\\\\"); // Escape caption...
Chris Lattner76d5b482002-07-11 20:33:32 +000070 replaceIn(Label, '\n', "\\n");
Chris Lattnerc68c31b2002-07-10 22:38:08 +000071 replaceIn(Label, ' ', "\\ ");
72 replaceIn(Label, '{', "\\{");
73 replaceIn(Label, '}', "\\}");
74 return Label;
75}
76
77static void writeEdge(std::ostream &O, const void *SrcNode,
78 const char *SrcNodePortName, int SrcNodeIdx,
Anand Shukla6c5ed412002-07-16 00:03:10 +000079 const DSNode *VS, const std::string &EdgeAttr = "") {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000080 O << "\tNode" << SrcNode << SrcNodePortName;
81 if (SrcNodeIdx != -1) O << SrcNodeIdx;
82 O << " -> Node" << (void*)VS;
83
84 if (!EdgeAttr.empty())
85 O << "[" << EdgeAttr << "]";
86 O << ";\n";
87}
88
Chris Lattner76d5b482002-07-11 20:33:32 +000089void DSNode::print(std::ostream &O, const DSGraph *G) const {
Anand Shukla6c5ed412002-07-16 00:03:10 +000090 std::string Caption = escapeLabel(getCaption(G));
Chris Lattnerc68c31b2002-07-10 22:38:08 +000091
92 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
93
94 if (!Links.empty()) {
95 O << "|{";
96 for (unsigned i = 0; i < Links.size(); ++i) {
97 if (i) O << "|";
98 O << "<g" << i << ">";
99 }
100 O << "}";
101 }
102 O << "}\"];\n";
103
104 for (unsigned i = 0; i < Links.size(); ++i)
105 if (Links[i])
106 writeEdge(O, this, ":g", i, Links[i]);
107}
108
109void DSGraph::print(std::ostream &O) const {
110 O << "digraph DataStructures {\n"
111 << "\tnode [shape=Mrecord];\n"
112 << "\tedge [arrowtail=\"dot\"];\n"
113 << "\tsize=\"10,7.5\";\n"
114 << "\trotate=\"90\";\n"
115 << "\tlabel=\"Function\\ " << Func.getName() << "\";\n\n";
116
117 // Output all of the nodes...
118 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
Chris Lattner76d5b482002-07-11 20:33:32 +0000119 Nodes[i]->print(O, this);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000120
121 O << "\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000122
123 // Output the returned value pointer...
124 if (RetNode != 0) {
Chris Lattner76d5b482002-07-11 20:33:32 +0000125 O << "\tNode0x1" << "[ plaintext=circle, label =\""
126 << escapeLabel("returning") << "\"];\n";
127 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000128 }
129
130 // Output all of the call nodes...
131 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
132 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
133 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
134 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
135 if (j) O << "|";
136 O << "<g" << j << ">";
137 }
138 O << "}}\"];\n";
139
140 for (unsigned j = 0, e = Call.size(); j != e; ++j)
141 if (Call[j])
Chris Lattner76d5b482002-07-11 20:33:32 +0000142 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000143 }
144
145
146 O << "}\n";
147}
148
Chris Lattner0d9bab82002-07-18 00:12:30 +0000149template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000150static void printCollection(const Collection &C, std::ostream &O,
151 const Module *M, const string &Prefix) {
152 if (M == 0) {
153 O << "Null Module pointer, cannot continue!\n";
154 return;
155 }
156
157 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000158 if (!I->isExternal()) {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000159 string Filename = Prefix + "." + I->getName() + ".dot";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000160 O << "Writing '" << Filename << "'...";
161 std::ofstream F(Filename.c_str());
Chris Lattner0d9bab82002-07-18 00:12:30 +0000162
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000163 if (F.good()) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000164 DSGraph &Graph = C.getDSGraph((Function&)*I);
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000165 Graph.print(F);
Chris Lattner0d9bab82002-07-18 00:12:30 +0000166 O << " [" << Graph.getGraphSize() << "+"
167 << Graph.getFunctionCalls().size() << "]\n";
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000168 } else {
169 O << " error opening file for writing!\n";
170 }
171 }
172}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000173
174
175// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000176void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000177 printCollection(*this, O, M, "ds");
178}
179
Chris Lattner97f51a32002-07-27 01:12:15 +0000180void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner0d9bab82002-07-18 00:12:30 +0000181 printCollection(*this, O, M, "bu");
182}