Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1 | //===- 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 | |
| 13 | void DSNode::dump() const { print(std::cerr, 0); } |
| 14 | |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 15 | std::string DSNode::getCaption(const DSGraph *G) const { |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 16 | std::stringstream OS; |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 17 | Module *M = G ? G->getFunction().getParent() : 0; |
| 18 | WriteTypeSymbolic(OS, getType(), M); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 19 | |
| 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 Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 28 | 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 Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 43 | return OS.str(); |
| 44 | } |
| 45 | |
| 46 | static 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 | |
| 54 | static 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 Shukla | 6c5ed41 | 2002-07-16 00:03:10 +0000 | [diff] [blame^] | 65 | static std::string escapeLabel(const std::string &In) { |
| 66 | std::string Label(In); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 67 | replaceIn(Label, '\\', "\\\\"); // Escape caption... |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 68 | replaceIn(Label, '\n', "\\n"); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 69 | replaceIn(Label, ' ', "\\ "); |
| 70 | replaceIn(Label, '{', "\\{"); |
| 71 | replaceIn(Label, '}', "\\}"); |
| 72 | return Label; |
| 73 | } |
| 74 | |
| 75 | static void writeEdge(std::ostream &O, const void *SrcNode, |
| 76 | const char *SrcNodePortName, int SrcNodeIdx, |
Anand Shukla | 6c5ed41 | 2002-07-16 00:03:10 +0000 | [diff] [blame^] | 77 | const DSNode *VS, const std::string &EdgeAttr = "") { |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 78 | 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 Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 87 | void DSNode::print(std::ostream &O, const DSGraph *G) const { |
Anand Shukla | 6c5ed41 | 2002-07-16 00:03:10 +0000 | [diff] [blame^] | 88 | std::string Caption = escapeLabel(getCaption(G)); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 89 | |
| 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 | |
| 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"; |
| 114 | |
| 115 | // Output all of the nodes... |
| 116 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 117 | Nodes[i]->print(O, this); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 118 | |
| 119 | O << "\n"; |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 120 | |
| 121 | // Output the returned value pointer... |
| 122 | if (RetNode != 0) { |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 123 | O << "\tNode0x1" << "[ plaintext=circle, label =\"" |
| 124 | << escapeLabel("returning") << "\"];\n"; |
| 125 | writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63"); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 126 | } |
| 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 Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 140 | writeEdge(O, &Call, ":g", j, Call[j], "color=gray63"); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | |
| 144 | O << "}\n"; |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 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()); |
| 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 | } |