1 //===- Printer.cpp - Code for printing data structure graphs nicely -------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the 'dot' graph printer.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Analysis/DataStructure/DataStructure.h"
15 #include "llvm/Analysis/DataStructure/DSGraph.h"
16 #include "llvm/Analysis/DataStructure/DSGraphTraits.h"
17 #include "llvm/Module.h"
18 #include "llvm/Constants.h"
19 #include "llvm/Assembly/Writer.h"
20 #include "llvm/Support/CommandLine.h"
21 #include "llvm/Support/GraphWriter.h"
22 #include "llvm/ADT/Statistic.h"
23 #include "llvm/Config/config.h"
28 // OnlyPrintMain - The DataStructure printer exposes this option to allow
29 // printing of only the graph for "main".
32 cl::opt
<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden
);
33 cl::opt
<bool> DontPrintAnything("dont-print-ds", cl::ReallyHidden
);
34 Statistic
<> MaxGraphSize ("dsa", "Maximum graph size");
35 Statistic
<> NumFoldedNodes ("dsa", "Number of folded nodes (in final graph)");
38 void DSNode::dump() const { print(std::cerr
, 0); }
40 static std::string
getCaption(const DSNode
*N
, const DSGraph
*G
) {
44 if (!G
) G
= N
->getParentGraph();
46 // Get the module from ONE of the functions in the graph it is available.
47 if (G
&& G
->retnodes_begin() != G
->retnodes_end())
48 M
= G
->retnodes_begin()->first
->getParent();
50 // If there is a global in the graph, we can use it to find the module.
51 const DSScalarMap
&SM
= G
->getScalarMap();
52 if (SM
.global_begin() != SM
.global_end())
53 M
= (*SM
.global_begin())->getParent();
56 if (N
->isNodeCompletelyFolded())
59 WriteTypeSymbolic(OS
, N
->getType(), M
);
63 if (unsigned NodeType
= N
->getNodeFlags()) {
65 if (NodeType
& DSNode::AllocaNode
) OS
<< "S";
66 if (NodeType
& DSNode::HeapNode
) OS
<< "H";
67 if (NodeType
& DSNode::GlobalNode
) OS
<< "G";
68 if (NodeType
& DSNode::UnknownNode
) OS
<< "U";
69 if (NodeType
& DSNode::Incomplete
) OS
<< "I";
70 if (NodeType
& DSNode::Modified
) OS
<< "M";
71 if (NodeType
& DSNode::Read
) OS
<< "R";
73 if (NodeType
& DSNode::DEAD
) OS
<< "<dead>";
78 EquivalenceClasses
<GlobalValue
*> *GlobalECs
= 0;
79 if (G
) GlobalECs
= &G
->getGlobalECs();
81 for (unsigned i
= 0, e
= N
->getGlobalsList().size(); i
!= e
; ++i
) {
82 WriteAsOperand(OS
, N
->getGlobalsList()[i
], false, true, M
);
84 // Figure out how many globals are equivalent to this one.
86 EquivalenceClasses
<GlobalValue
*>::iterator I
=
87 GlobalECs
->findValue(N
->getGlobalsList()[i
]);
88 if (I
!= GlobalECs
->end()) {
90 std::distance(GlobalECs
->member_begin(I
), GlobalECs
->member_end());
91 if (NumMembers
!= 1) OS
<< " + " << (NumMembers
-1) << " EC";
102 struct DOTGraphTraits
<const DSGraph
*> : public DefaultDOTGraphTraits
{
103 static std::string
getGraphName(const DSGraph
*G
) {
104 switch (G
->getReturnNodes().size()) {
105 case 0: return G
->getFunctionNames();
106 case 1: return "Function " + G
->getFunctionNames();
107 default: return "Functions: " + G
->getFunctionNames();
111 static std::string
getNodeLabel(const DSNode
*Node
, const DSGraph
*Graph
) {
112 return getCaption(Node
, Graph
);
115 static std::string
getNodeAttributes(const DSNode
*N
) {
116 return "shape=Mrecord";
119 static bool edgeTargetsEdgeSource(const void *Node
,
120 DSNode::const_iterator I
) {
121 unsigned O
= I
.getNode()->getLink(I
.getOffset()).getOffset();
122 return (O
>> DS::PointerShift
) != 0;
125 static DSNode::const_iterator
getEdgeTarget(const DSNode
*Node
,
126 DSNode::const_iterator I
) {
127 unsigned O
= I
.getNode()->getLink(I
.getOffset()).getOffset();
128 unsigned LinkNo
= O
>> DS::PointerShift
;
129 const DSNode
*N
= *I
;
130 DSNode::const_iterator R
= N
->begin();
131 for (; LinkNo
; --LinkNo
)
137 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
138 /// and the return node.
140 static void addCustomGraphFeatures(const DSGraph
*G
,
141 GraphWriter
<const DSGraph
*> &GW
) {
143 if (G
->retnodes_begin() != G
->retnodes_end())
144 CurMod
= G
->retnodes_begin()->first
->getParent();
146 // If there is a global in the graph, we can use it to find the module.
147 const DSScalarMap
&SM
= G
->getScalarMap();
148 if (SM
.global_begin() != SM
.global_end())
149 CurMod
= (*SM
.global_begin())->getParent();
153 // Add scalar nodes to the graph...
154 const DSGraph::ScalarMapTy
&VM
= G
->getScalarMap();
155 for (DSGraph::ScalarMapTy::const_iterator I
= VM
.begin(); I
!= VM
.end();++I
)
156 if (!isa
<GlobalValue
>(I
->first
)) {
157 std::stringstream OS
;
158 WriteAsOperand(OS
, I
->first
, false, true, CurMod
);
159 GW
.emitSimpleNode(I
->first
, "", OS
.str());
161 // Add edge from return node to real destination
162 DSNode
*DestNode
= I
->second
.getNode();
163 int EdgeDest
= I
->second
.getOffset() >> DS::PointerShift
;
164 if (EdgeDest
== 0) EdgeDest
= -1;
165 GW
.emitEdge(I
->first
, -1, DestNode
,
166 EdgeDest
, "arrowtail=tee,color=gray63");
170 // Output the returned value pointer...
171 for (DSGraph::retnodes_iterator I
= G
->retnodes_begin(),
172 E
= G
->retnodes_end(); I
!= E
; ++I
)
173 if (I
->second
.getNode()) {
175 if (G
->getReturnNodes().size() == 1)
178 Label
= I
->first
->getName() + " ret node";
179 // Output the return node...
180 GW
.emitSimpleNode((void*)I
->first
, "plaintext=circle", Label
);
182 // Add edge from return node to real destination
183 DSNode
*RetNode
= I
->second
.getNode();
184 int RetEdgeDest
= I
->second
.getOffset() >> DS::PointerShift
;;
185 if (RetEdgeDest
== 0) RetEdgeDest
= -1;
186 GW
.emitEdge((void*)I
->first
, -1, RetNode
,
187 RetEdgeDest
, "arrowtail=tee,color=gray63");
190 // Output all of the call nodes...
191 const std::list
<DSCallSite
> &FCs
=
192 G
->shouldPrintAuxCalls() ? G
->getAuxFunctionCalls()
193 : G
->getFunctionCalls();
194 for (std::list
<DSCallSite
>::const_iterator I
= FCs
.begin(), E
= FCs
.end();
196 const DSCallSite
&Call
= *I
;
197 std::vector
<std::string
> EdgeSourceCaptions(Call
.getNumPtrArgs()+2);
198 EdgeSourceCaptions
[0] = "r";
199 if (Call
.isDirectCall())
200 EdgeSourceCaptions
[1] = Call
.getCalleeFunc()->getName();
202 EdgeSourceCaptions
[1] = "f";
204 GW
.emitSimpleNode(&Call
, "shape=record", "call", Call
.getNumPtrArgs()+2,
205 &EdgeSourceCaptions
);
207 if (DSNode
*N
= Call
.getRetVal().getNode()) {
208 int EdgeDest
= Call
.getRetVal().getOffset() >> DS::PointerShift
;
209 if (EdgeDest
== 0) EdgeDest
= -1;
210 GW
.emitEdge(&Call
, 0, N
, EdgeDest
, "color=gray63,tailclip=false");
213 // Print out the callee...
214 if (Call
.isIndirectCall()) {
215 DSNode
*N
= Call
.getCalleeNode();
216 assert(N
&& "Null call site callee node!");
217 GW
.emitEdge(&Call
, 1, N
, -1, "color=gray63,tailclip=false");
220 for (unsigned j
= 0, e
= Call
.getNumPtrArgs(); j
!= e
; ++j
)
221 if (DSNode
*N
= Call
.getPtrArg(j
).getNode()) {
222 int EdgeDest
= Call
.getPtrArg(j
).getOffset() >> DS::PointerShift
;
223 if (EdgeDest
== 0) EdgeDest
= -1;
224 GW
.emitEdge(&Call
, j
+2, N
, EdgeDest
, "color=gray63,tailclip=false");
229 } // end namespace llvm
231 void DSNode::print(std::ostream
&O
, const DSGraph
*G
) const {
232 GraphWriter
<const DSGraph
*> W(O
, G
);
236 void DSGraph::print(std::ostream
&O
) const {
237 WriteGraph(O
, this, "DataStructures");
240 void DSGraph::writeGraphToFile(std::ostream
&O
,
241 const std::string
&GraphName
) const {
242 std::string Filename
= GraphName
+ ".dot";
243 O
<< "Writing '" << Filename
<< "'...";
244 std::ofstream
F(Filename
.c_str());
248 unsigned NumCalls
= shouldPrintAuxCalls() ?
249 getAuxFunctionCalls().size() : getFunctionCalls().size();
250 O
<< " [" << getGraphSize() << "+" << NumCalls
<< "]\n";
252 O
<< " error opening file for writing!\n";
256 /// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
257 /// then cleanup. For use from the debugger.
259 void DSGraph::viewGraph() const {
260 ViewGraph(this, "ds.tempgraph", "DataStructures");
264 template <typename Collection
>
265 static void printCollection(const Collection
&C
, std::ostream
&O
,
266 const Module
*M
, const std::string
&Prefix
) {
268 O
<< "Null Module pointer, cannot continue!\n";
272 unsigned TotalNumNodes
= 0, TotalCallNodes
= 0;
273 for (Module::const_iterator I
= M
->begin(), E
= M
->end(); I
!= E
; ++I
)
274 if (C
.hasGraph(*I
)) {
275 DSGraph
&Gr
= C
.getDSGraph((Function
&)*I
);
276 unsigned NumCalls
= Gr
.shouldPrintAuxCalls() ?
277 Gr
.getAuxFunctionCalls().size() : Gr
.getFunctionCalls().size();
278 bool IsDuplicateGraph
= false;
280 if (I
->getName() == "main" || !OnlyPrintMain
) {
281 Function
*SCCFn
= Gr
.retnodes_begin()->first
;
283 Gr
.writeGraphToFile(O
, Prefix
+I
->getName());
285 IsDuplicateGraph
= true; // Don't double count node/call nodes.
286 O
<< "Didn't write '" << Prefix
+I
->getName()
287 << ".dot' - Graph already emitted to '" << Prefix
+SCCFn
->getName()
291 Function
*SCCFn
= Gr
.retnodes_begin()->first
;
293 O
<< "Skipped Writing '" << Prefix
+I
->getName() << ".dot'... ["
294 << Gr
.getGraphSize() << "+" << NumCalls
<< "]\n";
296 IsDuplicateGraph
= true; // Don't double count node/call nodes.
300 if (!IsDuplicateGraph
) {
301 unsigned GraphSize
= Gr
.getGraphSize();
302 if (MaxGraphSize
< GraphSize
) MaxGraphSize
= GraphSize
;
304 TotalNumNodes
+= Gr
.getGraphSize();
305 TotalCallNodes
+= NumCalls
;
306 for (DSGraph::node_iterator NI
= Gr
.node_begin(), E
= Gr
.node_end();
308 if (NI
->isNodeCompletelyFolded())
313 DSGraph
&GG
= C
.getGlobalsGraph();
314 TotalNumNodes
+= GG
.getGraphSize();
315 TotalCallNodes
+= GG
.getFunctionCalls().size();
316 if (!OnlyPrintMain
) {
317 GG
.writeGraphToFile(O
, Prefix
+"GlobalsGraph");
319 O
<< "Skipped Writing '" << Prefix
<< "GlobalsGraph.dot'... ["
320 << GG
.getGraphSize() << "+" << GG
.getFunctionCalls().size() << "]\n";
323 O
<< "\nGraphs contain [" << TotalNumNodes
<< "+" << TotalCallNodes
324 << "] nodes total" << std::endl
;
328 // print - Print out the analysis results...
329 void LocalDataStructures::print(std::ostream
&O
, const Module
*M
) const {
330 if (DontPrintAnything
) return;
331 printCollection(*this, O
, M
, "ds.");
334 void BUDataStructures::print(std::ostream
&O
, const Module
*M
) const {
335 if (DontPrintAnything
) return;
336 printCollection(*this, O
, M
, "bu.");
339 void TDDataStructures::print(std::ostream
&O
, const Module
*M
) const {
340 if (DontPrintAnything
) return;
341 printCollection(*this, O
, M
, "td.");
344 void CompleteBUDataStructures::print(std::ostream
&O
, const Module
*M
) const {
345 if (DontPrintAnything
) return;
346 printCollection(*this, O
, M
, "cbu.");
350 void EquivClassGraphs::print(std::ostream
&O
, const Module
*M
) const {
351 if (DontPrintAnything
) return;
352 printCollection(*this, O
, M
, "eq.");