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" |
Chris Lattner | dadd49b | 2002-07-31 17:15:40 +0000 | [diff] [blame^] | 10 | #include "Support/CommandLine.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 11 | #include <fstream> |
| 12 | #include <sstream> |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 13 | using std::string; |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 14 | |
| 15 | void DSNode::dump() const { print(std::cerr, 0); } |
| 16 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 17 | string DSNode::getCaption(const DSGraph *G) const { |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 18 | std::stringstream OS; |
Vikram S. Adve | dfd2f32 | 2002-07-30 22:07:26 +0000 | [diff] [blame] | 19 | Module *M = G && &G->getFunction()? G->getFunction().getParent() : 0; |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 20 | WriteTypeSymbolic(OS, getType(), M); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 21 | |
| 22 | OS << " "; |
| 23 | if (NodeType & ScalarNode) OS << "S"; |
| 24 | if (NodeType & AllocaNode) OS << "A"; |
| 25 | if (NodeType & NewNode ) OS << "N"; |
| 26 | if (NodeType & GlobalNode) OS << "G"; |
| 27 | if (NodeType & SubElement) OS << "E"; |
| 28 | if (NodeType & CastNode ) OS << "C"; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 29 | if (NodeType & Incomplete) OS << "I"; |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 30 | |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 31 | for (unsigned i = 0, e = Globals.size(); i != e; ++i) { |
| 32 | OS << "\n"; |
| 33 | WriteAsOperand(OS, Globals[i], false, true, M); |
| 34 | } |
| 35 | |
| 36 | if ((NodeType & ScalarNode) && G) { |
| 37 | const std::map<Value*, DSNodeHandle> &VM = G->getValueMap(); |
| 38 | for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(), |
| 39 | E = VM.end(); I != E; ++I) |
| 40 | if (I->second == this) { |
| 41 | OS << "\n"; |
| 42 | WriteAsOperand(OS, I->first, false, true, M); |
| 43 | } |
| 44 | } |
| 45 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 46 | return OS.str(); |
| 47 | } |
| 48 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 49 | static string getValueName(Value *V, Function &F) { |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 50 | std::stringstream OS; |
| 51 | WriteAsOperand(OS, V, true, true, F.getParent()); |
| 52 | return OS.str(); |
| 53 | } |
| 54 | |
| 55 | |
| 56 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 57 | static void replaceIn(string &S, char From, const string &To) { |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 58 | for (unsigned i = 0; i < S.size(); ) |
| 59 | if (S[i] == From) { |
| 60 | S.replace(S.begin()+i, S.begin()+i+1, |
| 61 | To.begin(), To.end()); |
| 62 | i += To.size(); |
| 63 | } else { |
| 64 | ++i; |
| 65 | } |
| 66 | } |
| 67 | |
Anand Shukla | 6c5ed41 | 2002-07-16 00:03:10 +0000 | [diff] [blame] | 68 | static std::string escapeLabel(const std::string &In) { |
| 69 | std::string Label(In); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 70 | replaceIn(Label, '\\', "\\\\"); // Escape caption... |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 71 | replaceIn(Label, '\n', "\\n"); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 72 | replaceIn(Label, ' ', "\\ "); |
| 73 | replaceIn(Label, '{', "\\{"); |
| 74 | replaceIn(Label, '}', "\\}"); |
| 75 | return Label; |
| 76 | } |
| 77 | |
| 78 | static void writeEdge(std::ostream &O, const void *SrcNode, |
| 79 | const char *SrcNodePortName, int SrcNodeIdx, |
Anand Shukla | 6c5ed41 | 2002-07-16 00:03:10 +0000 | [diff] [blame] | 80 | const DSNode *VS, const std::string &EdgeAttr = "") { |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 81 | O << "\tNode" << SrcNode << SrcNodePortName; |
| 82 | if (SrcNodeIdx != -1) O << SrcNodeIdx; |
| 83 | O << " -> Node" << (void*)VS; |
| 84 | |
| 85 | if (!EdgeAttr.empty()) |
| 86 | O << "[" << EdgeAttr << "]"; |
| 87 | O << ";\n"; |
| 88 | } |
| 89 | |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 90 | void DSNode::print(std::ostream &O, const DSGraph *G) const { |
Anand Shukla | 6c5ed41 | 2002-07-16 00:03:10 +0000 | [diff] [blame] | 91 | std::string Caption = escapeLabel(getCaption(G)); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 92 | |
| 93 | O << "\tNode" << (void*)this << " [ label =\"{" << Caption; |
| 94 | |
| 95 | if (!Links.empty()) { |
| 96 | O << "|{"; |
| 97 | for (unsigned i = 0; i < Links.size(); ++i) { |
| 98 | if (i) O << "|"; |
| 99 | O << "<g" << i << ">"; |
| 100 | } |
| 101 | O << "}"; |
| 102 | } |
| 103 | O << "}\"];\n"; |
| 104 | |
| 105 | for (unsigned i = 0; i < Links.size(); ++i) |
| 106 | if (Links[i]) |
| 107 | writeEdge(O, this, ":g", i, Links[i]); |
| 108 | } |
| 109 | |
| 110 | void DSGraph::print(std::ostream &O) const { |
| 111 | O << "digraph DataStructures {\n" |
| 112 | << "\tnode [shape=Mrecord];\n" |
| 113 | << "\tedge [arrowtail=\"dot\"];\n" |
| 114 | << "\tsize=\"10,7.5\";\n" |
Vikram S. Adve | dfd2f32 | 2002-07-30 22:07:26 +0000 | [diff] [blame] | 115 | << "\trotate=\"90\";\n"; |
| 116 | |
| 117 | if (&Func != 0) |
| 118 | O << "\tlabel=\"Function\\ " << Func.getName() << "\";\n\n"; |
| 119 | else |
| 120 | O << "\tlabel=\"Global Graph\";\n\n"; |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 121 | |
| 122 | // Output all of the nodes... |
| 123 | for (unsigned i = 0, e = Nodes.size(); i != e; ++i) |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 124 | Nodes[i]->print(O, this); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 125 | |
| 126 | O << "\n"; |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 127 | |
| 128 | // Output the returned value pointer... |
| 129 | if (RetNode != 0) { |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 130 | O << "\tNode0x1" << "[ plaintext=circle, label =\"" |
| 131 | << escapeLabel("returning") << "\"];\n"; |
| 132 | writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63"); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | // Output all of the call nodes... |
| 136 | for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) { |
| 137 | const std::vector<DSNodeHandle> &Call = FunctionCalls[i]; |
| 138 | O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{"; |
| 139 | for (unsigned j = 0, e = Call.size(); j != e; ++j) { |
| 140 | if (j) O << "|"; |
| 141 | O << "<g" << j << ">"; |
| 142 | } |
| 143 | O << "}}\"];\n"; |
| 144 | |
| 145 | for (unsigned j = 0, e = Call.size(); j != e; ++j) |
| 146 | if (Call[j]) |
Chris Lattner | 76d5b48 | 2002-07-11 20:33:32 +0000 | [diff] [blame] | 147 | writeEdge(O, &Call, ":g", j, Call[j], "color=gray63"); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | |
| 151 | O << "}\n"; |
| 152 | } |
| 153 | |
Vikram S. Adve | dfd2f32 | 2002-07-30 22:07:26 +0000 | [diff] [blame] | 154 | |
| 155 | static void printGraph(const DSGraph &Graph, std::ostream &O, |
| 156 | const string &GraphName, const string &Prefix) { |
| 157 | string Filename = Prefix + "." + GraphName + ".dot"; |
| 158 | O << "Writing '" << Filename << "'..."; |
| 159 | std::ofstream F(Filename.c_str()); |
| 160 | |
| 161 | if (F.good()) { |
| 162 | Graph.print(F); |
| 163 | O << " [" << Graph.getGraphSize() << "+" |
| 164 | << Graph.getFunctionCalls().size() << "]\n"; |
| 165 | } else { |
| 166 | O << " error opening file for writing!\n"; |
| 167 | } |
| 168 | } |
| 169 | |
Chris Lattner | dadd49b | 2002-07-31 17:15:40 +0000 | [diff] [blame^] | 170 | static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden); |
| 171 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 172 | template <typename Collection> |
Chris Lattner | 97f51a3 | 2002-07-27 01:12:15 +0000 | [diff] [blame] | 173 | static void printCollection(const Collection &C, std::ostream &O, |
| 174 | const Module *M, const string &Prefix) { |
| 175 | if (M == 0) { |
| 176 | O << "Null Module pointer, cannot continue!\n"; |
| 177 | return; |
| 178 | } |
| 179 | |
| 180 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
Chris Lattner | dadd49b | 2002-07-31 17:15:40 +0000 | [diff] [blame^] | 181 | if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain)) |
Vikram S. Adve | dfd2f32 | 2002-07-30 22:07:26 +0000 | [diff] [blame] | 182 | printGraph(C.getDSGraph((Function&)*I), O, I->getName(), Prefix); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 183 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 184 | |
| 185 | |
| 186 | // print - Print out the analysis results... |
Chris Lattner | 97f51a3 | 2002-07-27 01:12:15 +0000 | [diff] [blame] | 187 | void LocalDataStructures::print(std::ostream &O, const Module *M) const { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 188 | printCollection(*this, O, M, "ds"); |
| 189 | } |
| 190 | |
Chris Lattner | 97f51a3 | 2002-07-27 01:12:15 +0000 | [diff] [blame] | 191 | void BUDataStructures::print(std::ostream &O, const Module *M) const { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 192 | printCollection(*this, O, M, "bu"); |
Vikram S. Adve | dfd2f32 | 2002-07-30 22:07:26 +0000 | [diff] [blame] | 193 | |
| 194 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 195 | if (!I->isExternal()) { |
| 196 | printGraph(*getDSGraph(*I).GlobalsGraph, O, "program", "gg"); |
| 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | void TDDataStructures::print(std::ostream &O, const Module *M) const { |
| 202 | printCollection(*this, O, M, "td"); |
| 203 | |
| 204 | for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I) |
| 205 | if (!I->isExternal()) { |
| 206 | printGraph(*getDSGraph(*I).GlobalsGraph, O, "program", "gg"); |
| 207 | break; |
| 208 | } |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 209 | } |