1 //===- Printer.cpp - Code for printing data structure graphs nicely -------===//
3 // This file implements the 'dot' graph printer.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/DataStructure.h"
8 #include "llvm/Module.h"
9 #include "llvm/Assembly/Writer.h"
14 void DSNode::dump() const { print(std::cerr, 0); }
16 string DSNode::getCaption(const DSGraph *G) const {
18 Module *M = G ? G->getFunction().getParent() : 0;
19 WriteTypeSymbolic(OS, getType(), M);
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";
28 if (NodeType & Incomplete) OS << "I";
30 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
32 WriteAsOperand(OS, Globals[i], false, true, M);
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) {
41 WriteAsOperand(OS, I->first, false, true, M);
48 static string getValueName(Value *V, Function &F) {
50 WriteAsOperand(OS, V, true, true, F.getParent());
56 static void replaceIn(string &S, char From, const string &To) {
57 for (unsigned i = 0; i < S.size(); )
59 S.replace(S.begin()+i, S.begin()+i+1,
60 To.begin(), To.end());
67 static std::string escapeLabel(const std::string &In) {
68 std::string Label(In);
69 replaceIn(Label, '\\', "\\\\"); // Escape caption...
70 replaceIn(Label, '\n', "\\n");
71 replaceIn(Label, ' ', "\\ ");
72 replaceIn(Label, '{', "\\{");
73 replaceIn(Label, '}', "\\}");
77 static void writeEdge(std::ostream &O, const void *SrcNode,
78 const char *SrcNodePortName, int SrcNodeIdx,
79 const DSNode *VS, const std::string &EdgeAttr = "") {
80 O << "\tNode" << SrcNode << SrcNodePortName;
81 if (SrcNodeIdx != -1) O << SrcNodeIdx;
82 O << " -> Node" << (void*)VS;
84 if (!EdgeAttr.empty())
85 O << "[" << EdgeAttr << "]";
89 void DSNode::print(std::ostream &O, const DSGraph *G) const {
90 std::string Caption = escapeLabel(getCaption(G));
92 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
96 for (unsigned i = 0; i < Links.size(); ++i) {
98 O << "<g" << i << ">";
104 for (unsigned i = 0; i < Links.size(); ++i)
106 writeEdge(O, this, ":g", i, Links[i]);
109 void 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";
117 // Output all of the nodes...
118 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
119 Nodes[i]->print(O, this);
123 // Output the returned value pointer...
125 O << "\tNode0x1" << "[ plaintext=circle, label =\""
126 << escapeLabel("returning") << "\"];\n";
127 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
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) {
136 O << "<g" << j << ">";
140 for (unsigned j = 0, e = Call.size(); j != e; ++j)
142 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
149 template <typename Collection>
150 static void printCollection(const Collection &C, std::ostream &O, Module *M,
151 const string &Prefix) {
152 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
153 if (!I->isExternal()) {
154 string Filename = Prefix + "." + I->getName() + ".dot";
155 O << "Writing '" << Filename << "'...";
156 std::ofstream F(Filename.c_str());
159 DSGraph &Graph = C.getDSGraph(*I);
161 O << " [" << Graph.getGraphSize() << "+"
162 << Graph.getFunctionCalls().size() << "]\n";
164 O << " error opening file for writing!\n";
170 // print - Print out the analysis results...
171 void LocalDataStructures::print(std::ostream &O, Module *M) const {
172 printCollection(*this, O, M, "ds");
175 void BUDataStructures::print(std::ostream &O, Module *M) const {
176 printCollection(*this, O, M, "bu");