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/Analysis/DSGraph.h"
9 #include "llvm/Analysis/DSGraphTraits.h"
10 #include "llvm/Module.h"
11 #include "llvm/Assembly/Writer.h"
12 #include "Support/CommandLine.h"
13 #include "Support/GraphWriter.h"
18 // OnlyPrintMain - The DataStructure printer exposes this option to allow
19 // printing of only the graph for "main".
21 static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
24 void DSNode::dump() const { print(std::cerr, 0); }
26 static string getCaption(const DSNode *N, const DSGraph *G) {
28 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
30 for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
31 WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
32 if (N->getTypeEntries()[i].second)
33 OS << "@" << N->getTypeEntries()[i].second;
37 if (N->NodeType & DSNode::ScalarNode) OS << "S";
38 if (N->NodeType & DSNode::AllocaNode) OS << "A";
39 if (N->NodeType & DSNode::NewNode ) OS << "N";
40 if (N->NodeType & DSNode::GlobalNode) OS << "G";
41 if (N->NodeType & DSNode::Incomplete) OS << "I";
43 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
44 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
48 if ((N->NodeType & DSNode::ScalarNode) && G) {
49 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
50 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
51 E = VM.end(); I != E; ++I)
52 if (I->second.getNode() == N) {
53 WriteAsOperand(OS, I->first, false, true, M);
60 static string getValueName(Value *V, Function &F) {
62 WriteAsOperand(OS, V, true, true, F.getParent());
68 static void replaceIn(string &S, char From, const string &To) {
69 for (unsigned i = 0; i < S.size(); )
71 S.replace(S.begin()+i, S.begin()+i+1,
72 To.begin(), To.end());
79 static std::string escapeLabel(const std::string &In) {
80 std::string Label(In);
81 replaceIn(Label, '\\', "\\\\"); // Escape caption...
82 replaceIn(Label, '\n', "\\n");
83 replaceIn(Label, ' ', "\\ ");
84 replaceIn(Label, '{', "\\{");
85 replaceIn(Label, '}', "\\}");
89 static void writeEdge(std::ostream &O, const void *SrcNode,
90 const char *SrcNodePortName, int SrcNodeIdx,
91 const DSNodeHandle &VS,
92 const std::string &EdgeAttr = "") {
93 O << "\tNode" << SrcNode << SrcNodePortName;
94 if (SrcNodeIdx != -1) O << SrcNodeIdx;
95 O << " -> Node" << (void*)VS.getNode();
96 if (VS.getOffset()) O << ":g" << VS.getOffset();
98 if (!EdgeAttr.empty())
99 O << "[" << EdgeAttr << "]";
103 void DSNode::print(std::ostream &O, const DSGraph *G) const {
104 std::string Caption = escapeLabel(getCaption(this, G));
106 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
108 unsigned Size = getSize();
109 if (Size > 64) Size = 64; // Don't print out HUGE graph nodes!
111 if (getSize() != 0) {
113 for (unsigned i = 0; i < Size; ++i) {
115 O << "<g" << i << ">" << (int)MergeMap[i];
117 if (Size != getSize())
118 O << "|truncated...";
123 for (unsigned i = 0; i != Size; ++i)
124 if (const DSNodeHandle *DSN = getLink(i))
125 writeEdge(O, this, ":g", i, *DSN);
128 void DSGraph::print(std::ostream &O) const {
129 O << "digraph DataStructures {\n"
130 << "\tnode [shape=Mrecord];\n"
131 << "\tedge [arrowtail=\"dot\"];\n"
132 << "\tsize=\"10,7.5\";\n"
133 << "\trotate=\"90\";\n";
136 O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
138 // Output all of the nodes...
139 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
140 Nodes[i]->print(O, this);
144 // Output the returned value pointer...
146 O << "\tNode0x1" << "[ plaintext=circle, label =\""
147 << escapeLabel("returning") << "\"];\n";
148 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
151 // Output all of the call nodes...
152 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
153 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
154 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
155 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
157 O << "<g" << j << ">";
161 for (unsigned j = 0, e = Call.size(); j != e; ++j)
162 if (Call[j].getNode())
163 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
171 struct DOTGraphTraits<DSGraph*> : public DefaultDOTGraphTraits {
172 static std::string getGraphName(DSGraph *G) {
173 if (G->hasFunction())
174 return "Function " + G->getFunction().getName();
176 return "Non-function graph";
179 static const char *getGraphProperties(DSGraph *G) {
180 return "\tedge [arrowtail=\"dot\"];\n"
181 "\tsize=\"10,7.5\";\n"
182 "\trotate=\"90\";\n";
185 static std::string getNodeLabel(DSNode *Node, DSGraph *Graph) {
186 return getCaption(Node, Graph);
189 static std::string getNodeAttributes(DSNode *N) {
190 return "shape=Mrecord";//fontname=Courier";
193 static int getEdgeSourceLabel(DSNode *Node, DSNode::iterator I) {
194 assert(Node == I.getNode() && "Iterator not for this node!");
195 return Node->getMergeMapLabel(I.getLink());
201 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
202 string Filename = GraphName + ".dot";
203 O << "Writing '" << Filename << "'...";
204 std::ofstream F(Filename.c_str());
207 WriteGraph(F, this, "DataStructures");
209 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
211 O << " error opening file for writing!\n";
215 template <typename Collection>
216 static void printCollection(const Collection &C, std::ostream &O,
217 const Module *M, const string &Prefix) {
219 O << "Null Module pointer, cannot continue!\n";
223 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
224 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
225 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
229 // print - Print out the analysis results...
230 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
231 printCollection(*this, O, M, "ds.");
234 void BUDataStructures::print(std::ostream &O, const Module *M) const {
235 printCollection(*this, O, M, "bu.");
237 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
238 if (!I->isExternal()) {
239 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
246 void TDDataStructures::print(std::ostream &O, const Module *M) const {
247 printCollection(*this, O, M, "td.");
249 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
250 if (!I->isExternal()) {
251 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");