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"
14 #include "Support/Statistic.h"
19 // OnlyPrintMain - The DataStructure printer exposes this option to allow
20 // printing of only the graph for "main".
23 cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
24 Statistic<> MaxGraphSize ("dsnode", "Maximum graph size");
25 Statistic<> NumFoldedNodes ("dsnode", "Number of folded nodes (in final graph)");
29 void DSNode::dump() const { print(std::cerr, 0); }
31 static string getCaption(const DSNode *N, const DSGraph *G) {
33 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
35 if (N->isNodeCompletelyFolded())
38 WriteTypeSymbolic(OS, N->getType(), M);
44 if (N->NodeType & DSNode::AllocaNode ) OS << "S";
45 if (N->NodeType & DSNode::HeapNode ) OS << "H";
46 if (N->NodeType & DSNode::GlobalNode ) OS << "G";
47 if (N->NodeType & DSNode::UnknownNode) OS << "U";
48 if (N->NodeType & DSNode::Incomplete ) OS << "I";
49 if (N->NodeType & DSNode::Modified ) OS << "M";
50 if (N->NodeType & DSNode::Read ) OS << "R";
54 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
55 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
63 struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
64 static std::string getGraphName(const DSGraph *G) {
66 return "Function " + G->getFunction().getName();
68 return "Non-function graph";
71 static const char *getGraphProperties(const DSGraph *G) {
72 return "\tedge [arrowtail=\"dot\"];\n"
73 "\tsize=\"10,7.5\";\n"
77 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
78 return getCaption(Node, Graph);
81 static std::string getNodeAttributes(const DSNode *N) {
82 return "shape=Mrecord";//fontname=Courier";
85 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
86 /// and the return node.
88 static void addCustomGraphFeatures(const DSGraph *G,
89 GraphWriter<const DSGraph*> &GW) {
90 // Add scalar nodes to the graph...
91 const std::map<Value*, DSNodeHandle> &VM = G->getScalarMap();
92 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
94 if (!isa<GlobalValue>(I->first)) {
96 WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
97 GW.emitSimpleNode(I->first, "", OS.str());
99 // Add edge from return node to real destination
100 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
101 if (EdgeDest == 0) EdgeDest = -1;
102 GW.emitEdge(I->first, -1, I->second.getNode(),
103 EdgeDest, "arrowtail=tee,color=gray63");
107 // Output the returned value pointer...
108 if (G->getRetNode().getNode() != 0) {
109 // Output the return node...
110 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
112 // Add edge from return node to real destination
113 int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
114 if (RetEdgeDest == 0) RetEdgeDest = -1;
115 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
116 RetEdgeDest, "arrowtail=tee,color=gray63");
119 // Output all of the call nodes...
120 const std::vector<DSCallSite> &FCs =
121 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
122 : G->getFunctionCalls();
123 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
124 const DSCallSite &Call = FCs[i];
125 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
127 if (DSNode *N = Call.getRetVal().getNode()) {
128 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
129 if (EdgeDest == 0) EdgeDest = -1;
130 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
132 if (DSNode *N = Call.getCallee().getNode()) {
133 int EdgeDest = Call.getCallee().getOffset() >> DS::PointerShift;
134 if (EdgeDest == 0) EdgeDest = -1;
135 GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
137 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
138 if (DSNode *N = Call.getPtrArg(j).getNode()) {
139 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
140 if (EdgeDest == 0) EdgeDest = -1;
141 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
147 void DSNode::print(std::ostream &O, const DSGraph *G) const {
148 GraphWriter<const DSGraph *> W(O, G);
152 void DSGraph::print(std::ostream &O) const {
153 WriteGraph(O, this, "DataStructures");
156 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
157 string Filename = GraphName + ".dot";
158 O << "Writing '" << Filename << "'...";
159 std::ofstream F(Filename.c_str());
163 unsigned NumCalls = shouldPrintAuxCalls() ?
164 getAuxFunctionCalls().size() : getFunctionCalls().size();
165 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
167 O << " error opening file for writing!\n";
171 template <typename Collection>
172 static void printCollection(const Collection &C, std::ostream &O,
173 const Module *M, const string &Prefix) {
175 O << "Null Module pointer, cannot continue!\n";
179 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
180 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
181 if (C.hasGraph(*I)) {
182 DSGraph &Gr = C.getDSGraph((Function&)*I);
183 TotalNumNodes += Gr.getGraphSize();
184 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
185 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
187 TotalCallNodes += NumCalls;
188 if (I->getName() == "main" || !OnlyPrintMain)
189 Gr.writeGraphToFile(O, Prefix+I->getName());
191 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
192 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
195 if (MaxGraphSize < Gr.getNodes().size())
196 MaxGraphSize = Gr.getNodes().size();
197 for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
198 if (Gr.getNodes()[i]->isNodeCompletelyFolded())
202 DSGraph &GG = C.getGlobalsGraph();
203 TotalNumNodes += GG.getGraphSize();
204 TotalCallNodes += GG.getFunctionCalls().size();
205 if (!OnlyPrintMain) {
206 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
208 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
209 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
212 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
213 << "] nodes total" << std::endl;
217 // print - Print out the analysis results...
218 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
219 printCollection(*this, O, M, "ds.");
222 void BUDataStructures::print(std::ostream &O, const Module *M) const {
223 printCollection(*this, O, M, "bu.");
226 void TDDataStructures::print(std::ostream &O, const Module *M) const {
227 printCollection(*this, O, M, "td.");