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/Module.h"
10 #include "llvm/Assembly/Writer.h"
11 #include "Support/CommandLine.h"
16 void DSNode::dump() const { print(std::cerr, 0); }
18 static string getCaption(const DSNode *N, const DSGraph *G) {
20 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
22 for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
23 WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
24 if (N->getTypeEntries()[i].second)
25 OS << "@" << N->getTypeEntries()[i].second;
29 if (N->NodeType & DSNode::ScalarNode) OS << "S";
30 if (N->NodeType & DSNode::AllocaNode) OS << "A";
31 if (N->NodeType & DSNode::NewNode ) OS << "N";
32 if (N->NodeType & DSNode::GlobalNode) OS << "G";
33 if (N->NodeType & DSNode::Incomplete) OS << "I";
35 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
36 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
40 if ((N->NodeType & DSNode::ScalarNode) && G) {
41 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
42 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
43 E = VM.end(); I != E; ++I)
44 if (I->second.getNode() == N) {
45 WriteAsOperand(OS, I->first, false, true, M);
52 static string getValueName(Value *V, Function &F) {
54 WriteAsOperand(OS, V, true, true, F.getParent());
60 static void replaceIn(string &S, char From, const string &To) {
61 for (unsigned i = 0; i < S.size(); )
63 S.replace(S.begin()+i, S.begin()+i+1,
64 To.begin(), To.end());
71 static std::string escapeLabel(const std::string &In) {
72 std::string Label(In);
73 replaceIn(Label, '\\', "\\\\"); // Escape caption...
74 replaceIn(Label, '\n', "\\n");
75 replaceIn(Label, ' ', "\\ ");
76 replaceIn(Label, '{', "\\{");
77 replaceIn(Label, '}', "\\}");
81 static void writeEdge(std::ostream &O, const void *SrcNode,
82 const char *SrcNodePortName, int SrcNodeIdx,
83 const DSNodeHandle &VS,
84 const std::string &EdgeAttr = "") {
85 O << "\tNode" << SrcNode << SrcNodePortName;
86 if (SrcNodeIdx != -1) O << SrcNodeIdx;
87 O << " -> Node" << (void*)VS.getNode();
88 if (VS.getOffset()) O << ":g" << VS.getOffset();
90 if (!EdgeAttr.empty())
91 O << "[" << EdgeAttr << "]";
95 void DSNode::print(std::ostream &O, const DSGraph *G) const {
96 std::string Caption = escapeLabel(getCaption(this, G));
98 O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
100 if (getSize() != 0) {
102 for (unsigned i = 0; i < getSize(); ++i) {
104 O << "<g" << i << ">" << (int)MergeMap[i];
110 for (unsigned i = 0; i != getSize(); ++i)
111 if (const DSNodeHandle *DSN = getLink(i))
112 writeEdge(O, this, ":g", i, *DSN);
115 void DSGraph::print(std::ostream &O) const {
116 O << "digraph DataStructures {\n"
117 << "\tnode [shape=Mrecord];\n"
118 << "\tedge [arrowtail=\"dot\"];\n"
119 << "\tsize=\"10,7.5\";\n"
120 << "\trotate=\"90\";\n";
123 O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
125 // Output all of the nodes...
126 for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
127 Nodes[i]->print(O, this);
131 // Output the returned value pointer...
133 O << "\tNode0x1" << "[ plaintext=circle, label =\""
134 << escapeLabel("returning") << "\"];\n";
135 writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
138 // Output all of the call nodes...
139 for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
140 const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
141 O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
142 for (unsigned j = 0, e = Call.size(); j != e; ++j) {
144 O << "<g" << j << ">";
148 for (unsigned j = 0, e = Call.size(); j != e; ++j)
149 if (Call[j].getNode())
150 writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
158 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
159 string Filename = GraphName + ".dot";
160 O << "Writing '" << Filename << "'...";
161 std::ofstream F(Filename.c_str());
165 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
167 O << " error opening file for writing!\n";
171 static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
173 template <typename Collection>
174 static void printCollection(const Collection &C, std::ostream &O,
175 const Module *M, const string &Prefix) {
177 O << "Null Module pointer, cannot continue!\n";
181 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
182 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
183 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
187 // print - Print out the analysis results...
188 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
189 printCollection(*this, O, M, "ds.");
193 void BUDataStructures::print(std::ostream &O, const Module *M) const {
194 printCollection(*this, O, M, "bu.");
196 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
197 if (!I->isExternal()) {
198 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
203 void TDDataStructures::print(std::ostream &O, const Module *M) const {
204 printCollection(*this, O, M, "td.");
206 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
207 if (!I->isExternal()) {
208 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");