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"
13 void DSNode::dump() const { print(std::cerr, 0); }
15 std::string DSNode::getCaption(const DSGraph *G) const {
17 Module *M = G ? G->getFunction().getParent() : 0;
18 WriteTypeSymbolic(OS, getType(), M);
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";
28 for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
30 WriteAsOperand(OS, Globals[i], false, true, M);
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) {
39 WriteAsOperand(OS, I->first, false, true, M);
46 static std::string getValueName(Value *V, Function &F) {
48 WriteAsOperand(OS, V, true, true, F.getParent());
54 static void replaceIn(std::string &S, char From, const std::string &To) {
55 for (unsigned i = 0; i < S.size(); )
57 S.replace(S.begin()+i, S.begin()+i+1,
58 To.begin(), To.end());
65 static std::string escapeLabel(const std::string &In) {
66 std::string Label(In);
67 replaceIn(Label, '\\', "\\\\"); // Escape caption...
68 replaceIn(Label, '\n', "\\n");
69 replaceIn(Label, ' ', "\\ ");
70 replaceIn(Label, '{', "\\{");
71 replaceIn(Label, '}', "\\}");
75 static void writeEdge(std::ostream &O, const void *SrcNode,
76 const char *SrcNodePortName, int SrcNodeIdx,
77 const DSNode *VS, const std::string &EdgeAttr = "") {
78 O << "\tNode" << SrcNode << SrcNodePortName;
79 if (SrcNodeIdx != -1) O << SrcNodeIdx;
80 O << " -> Node" << (void*)VS;
82 if (!EdgeAttr.empty())
83 O << "[" << EdgeAttr << "]";
87 void DSNode::print(std::ostream &O, const DSGraph *G) const {
88 std::string Caption = escapeLabel(getCaption(G));
90 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
94 for (unsigned i = 0; i < Links.size(); ++i) {
96 O << "<g" << i << ">";
102 for (unsigned i = 0; i < Links.size(); ++i)
104 writeEdge(O, this, ":g", i, Links[i]);
107 void 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";
115 // Output all of the nodes...
116 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
117 Nodes[i]->print(O, this);
121 // Output the returned value pointer...
123 O << "\tNode0x1" << "[ plaintext=circle, label =\""
124 << escapeLabel("returning") << "\"];\n";
125 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
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) {
134 O << "<g" << j << ">";
138 for (unsigned j = 0, e = Call.size(); j != e; ++j)
140 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
150 // print - Print out the analysis results...
151 void 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());
159 DSGraph &Graph = getDSGraph(*I);
161 O << " [" << Graph.getGraphSize() << "]\n";
163 O << " error opening file for writing!\n";