blob: 71a5071ae46c8ea04013c35ca74bb0342d601dff [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// The LLVM Compiler Infrastructure
4//
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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattnerc68c31b2002-07-10 22:38:08 +00009//
10// This file implements the 'dot' graph printer.
11//
12//===----------------------------------------------------------------------===//
13
Chris Lattner4dabb2c2004-07-07 06:32:21 +000014#include "llvm/Analysis/DataStructure/DataStructure.h"
15#include "llvm/Analysis/DataStructure/DSGraph.h"
16#include "llvm/Analysis/DataStructure/DSGraphTraits.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017#include "llvm/Module.h"
Chris Lattnerd9dad2c2003-07-01 16:27:32 +000018#include "llvm/Constants.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000019#include "llvm/Assembly/Writer.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000020#include "llvm/Support/CommandLine.h"
21#include "llvm/Support/GraphWriter.h"
Reid Spencer3e0c1542006-06-05 15:44:46 +000022#include "llvm/System/Path.h"
23#include "llvm/System/Program.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000024#include "llvm/ADT/Statistic.h"
Reid Spencer3e0c1542006-06-05 15:44:46 +000025#include "llvm/Config/config.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000026#include <fstream>
27#include <sstream>
Chris Lattner9a927292003-11-12 23:11:14 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
Chris Lattner55c10582002-10-03 20:38:41 +000030// OnlyPrintMain - The DataStructure printer exposes this option to allow
31// printing of only the graph for "main".
32//
Chris Lattner49a1ed02002-11-18 21:42:45 +000033namespace {
34 cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
Chris Lattner4a857762004-01-22 13:42:43 +000035 cl::opt<bool> DontPrintAnything("dont-print-ds", cl::ReallyHidden);
Chris Lattnere92e7642004-02-07 23:58:05 +000036 Statistic<> MaxGraphSize ("dsa", "Maximum graph size");
37 Statistic<> NumFoldedNodes ("dsa", "Number of folded nodes (in final graph)");
Chris Lattner49a1ed02002-11-18 21:42:45 +000038}
Chris Lattner55c10582002-10-03 20:38:41 +000039
Chris Lattnerc68c31b2002-07-10 22:38:08 +000040void DSNode::dump() const { print(std::cerr, 0); }
41
Chris Lattnerb3416bc2003-02-01 04:01:21 +000042static std::string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000043 std::stringstream OS;
Chris Lattner5a540632003-06-30 03:15:25 +000044 Module *M = 0;
Chris Lattner153f2402004-02-25 23:06:30 +000045
Chris Lattner72529392004-03-02 21:39:43 +000046 if (!G) G = N->getParentGraph();
Chris Lattner153f2402004-02-25 23:06:30 +000047
Chris Lattner5a540632003-06-30 03:15:25 +000048 // Get the module from ONE of the functions in the graph it is available.
Chris Lattnera5f47ea2005-03-15 16:55:04 +000049 if (G && G->retnodes_begin() != G->retnodes_end())
50 M = G->retnodes_begin()->first->getParent();
Chris Lattner72529392004-03-02 21:39:43 +000051 if (M == 0 && G) {
52 // If there is a global in the graph, we can use it to find the module.
53 const DSScalarMap &SM = G->getScalarMap();
54 if (SM.global_begin() != SM.global_end())
55 M = (*SM.global_begin())->getParent();
56 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000057
Chris Lattner08db7192002-11-06 06:20:27 +000058 if (N->isNodeCompletelyFolded())
Chris Lattner63899fa2003-07-02 04:39:27 +000059 OS << "COLLAPSED";
Chris Lattner08db7192002-11-06 06:20:27 +000060 else {
Chris Lattner49a1ed02002-11-18 21:42:45 +000061 WriteTypeSymbolic(OS, N->getType(), M);
62 if (N->isArray())
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000063 OS << " array";
Chris Lattner08db7192002-11-06 06:20:27 +000064 }
Chris Lattnerbd92b732003-06-19 21:15:11 +000065 if (unsigned NodeType = N->getNodeFlags()) {
Chris Lattner08db7192002-11-06 06:20:27 +000066 OS << ": ";
Chris Lattnerbd92b732003-06-19 21:15:11 +000067 if (NodeType & DSNode::AllocaNode ) OS << "S";
68 if (NodeType & DSNode::HeapNode ) OS << "H";
69 if (NodeType & DSNode::GlobalNode ) OS << "G";
70 if (NodeType & DSNode::UnknownNode) OS << "U";
71 if (NodeType & DSNode::Incomplete ) OS << "I";
72 if (NodeType & DSNode::Modified ) OS << "M";
73 if (NodeType & DSNode::Read ) OS << "R";
Chris Lattnerbd92b732003-06-19 21:15:11 +000074#ifndef NDEBUG
75 if (NodeType & DSNode::DEAD ) OS << "<dead>";
76#endif
Chris Lattner76d5b482002-07-11 20:33:32 +000077 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000078 }
79
Chris Lattnerf5c7ad82005-03-20 02:40:11 +000080 EquivalenceClasses<GlobalValue*> *GlobalECs = 0;
81 if (G) GlobalECs = &G->getGlobalECs();
Misha Brukman2b37d7c2005-04-21 21:13:18 +000082
Chris Lattnerf5c7ad82005-03-20 02:40:11 +000083 for (unsigned i = 0, e = N->getGlobalsList().size(); i != e; ++i) {
84 WriteAsOperand(OS, N->getGlobalsList()[i], false, true, M);
85
86 // Figure out how many globals are equivalent to this one.
87 if (GlobalECs) {
88 EquivalenceClasses<GlobalValue*>::iterator I =
89 GlobalECs->findValue(N->getGlobalsList()[i]);
90 if (I != GlobalECs->end()) {
Misha Brukman2b37d7c2005-04-21 21:13:18 +000091 unsigned NumMembers =
Chris Lattnerf5c7ad82005-03-20 02:40:11 +000092 std::distance(GlobalECs->member_begin(I), GlobalECs->member_end());
93 if (NumMembers != 1) OS << " + " << (NumMembers-1) << " EC";
94 }
95 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +000096 OS << "\n";
97 }
98
Chris Lattnerc68c31b2002-07-10 22:38:08 +000099 return OS.str();
100}
101
Chris Lattner9a927292003-11-12 23:11:14 +0000102namespace llvm {
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000103template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +0000104struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
105 static std::string getGraphName(const DSGraph *G) {
Chris Lattner5a540632003-06-30 03:15:25 +0000106 switch (G->getReturnNodes().size()) {
Chris Lattner6681e982003-06-30 05:57:39 +0000107 case 0: return G->getFunctionNames();
108 case 1: return "Function " + G->getFunctionNames();
109 default: return "Functions: " + G->getFunctionNames();
Chris Lattner5a540632003-06-30 03:15:25 +0000110 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000111 }
112
Chris Lattnere17a4e82002-10-17 01:02:46 +0000113 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000114 return getCaption(Node, Graph);
115 }
116
Chris Lattnere17a4e82002-10-17 01:02:46 +0000117 static std::string getNodeAttributes(const DSNode *N) {
Brian Gaeke61780852004-05-05 06:10:06 +0000118 return "shape=Mrecord";
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000119 }
Chris Lattner8e667cd2004-06-22 07:13:10 +0000120
121 static bool edgeTargetsEdgeSource(const void *Node,
122 DSNode::const_iterator I) {
123 unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
124 return (O >> DS::PointerShift) != 0;
125 }
126
127 static DSNode::const_iterator getEdgeTarget(const DSNode *Node,
128 DSNode::const_iterator I) {
129 unsigned O = I.getNode()->getLink(I.getOffset()).getOffset();
130 unsigned LinkNo = O >> DS::PointerShift;
131 const DSNode *N = *I;
132 DSNode::const_iterator R = N->begin();
133 for (; LinkNo; --LinkNo)
134 ++R;
135 return R;
136 }
137
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000138
Chris Lattnereb265cd2002-10-16 02:04:36 +0000139 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
140 /// and the return node.
141 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +0000142 static void addCustomGraphFeatures(const DSGraph *G,
143 GraphWriter<const DSGraph*> &GW) {
Chris Lattner5a540632003-06-30 03:15:25 +0000144 Module *CurMod = 0;
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000145 if (G->retnodes_begin() != G->retnodes_end())
146 CurMod = G->retnodes_begin()->first->getParent();
Chris Lattner72529392004-03-02 21:39:43 +0000147 else {
148 // If there is a global in the graph, we can use it to find the module.
149 const DSScalarMap &SM = G->getScalarMap();
150 if (SM.global_begin() != SM.global_end())
151 CurMod = (*SM.global_begin())->getParent();
152 }
153
Chris Lattner714752f2003-02-04 00:03:18 +0000154
Chris Lattner92673292002-11-02 00:13:20 +0000155 // Add scalar nodes to the graph...
Chris Lattner5a540632003-06-30 03:15:25 +0000156 const DSGraph::ScalarMapTy &VM = G->getScalarMap();
157 for (DSGraph::ScalarMapTy::const_iterator I = VM.begin(); I != VM.end();++I)
Reid Spencere8404342004-07-18 00:18:30 +0000158 if (!isa<GlobalValue>(I->first)) {
Chris Lattner92673292002-11-02 00:13:20 +0000159 std::stringstream OS;
Chris Lattner714752f2003-02-04 00:03:18 +0000160 WriteAsOperand(OS, I->first, false, true, CurMod);
Chris Lattner49a1ed02002-11-18 21:42:45 +0000161 GW.emitSimpleNode(I->first, "", OS.str());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000162
Chris Lattner92673292002-11-02 00:13:20 +0000163 // Add edge from return node to real destination
Chris Lattnerf1bd4b42004-10-30 07:21:19 +0000164 DSNode *DestNode = I->second.getNode();
Chris Lattner08db7192002-11-06 06:20:27 +0000165 int EdgeDest = I->second.getOffset() >> DS::PointerShift;
Chris Lattner92673292002-11-02 00:13:20 +0000166 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattnerf1bd4b42004-10-30 07:21:19 +0000167 GW.emitEdge(I->first, -1, DestNode,
Chris Lattner92673292002-11-02 00:13:20 +0000168 EdgeDest, "arrowtail=tee,color=gray63");
169 }
170
171
Chris Lattnereb265cd2002-10-16 02:04:36 +0000172 // Output the returned value pointer...
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000173 for (DSGraph::retnodes_iterator I = G->retnodes_begin(),
174 E = G->retnodes_end(); I != E; ++I)
Chris Lattner5a540632003-06-30 03:15:25 +0000175 if (I->second.getNode()) {
176 std::string Label;
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000177 if (G->getReturnNodes().size() == 1)
Chris Lattner5a540632003-06-30 03:15:25 +0000178 Label = "returning";
179 else
180 Label = I->first->getName() + " ret node";
181 // Output the return node...
Chris Lattnerd8ea8a52003-11-12 04:58:19 +0000182 GW.emitSimpleNode((void*)I->first, "plaintext=circle", Label);
Chris Lattnereb265cd2002-10-16 02:04:36 +0000183
Chris Lattner5a540632003-06-30 03:15:25 +0000184 // Add edge from return node to real destination
Chris Lattnerf1bd4b42004-10-30 07:21:19 +0000185 DSNode *RetNode = I->second.getNode();
Chris Lattner5a540632003-06-30 03:15:25 +0000186 int RetEdgeDest = I->second.getOffset() >> DS::PointerShift;;
187 if (RetEdgeDest == 0) RetEdgeDest = -1;
Chris Lattnerf1bd4b42004-10-30 07:21:19 +0000188 GW.emitEdge((void*)I->first, -1, RetNode,
Chris Lattner5a540632003-06-30 03:15:25 +0000189 RetEdgeDest, "arrowtail=tee,color=gray63");
190 }
Chris Lattner962ee452002-10-16 20:16:16 +0000191
192 // Output all of the call nodes...
Chris Lattnera9548d92005-01-30 23:51:02 +0000193 const std::list<DSCallSite> &FCs =
Chris Lattner4f7815f2002-11-10 06:53:59 +0000194 G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
195 : G->getFunctionCalls();
Chris Lattnera9548d92005-01-30 23:51:02 +0000196 for (std::list<DSCallSite>::const_iterator I = FCs.begin(), E = FCs.end();
197 I != E; ++I) {
198 const DSCallSite &Call = *I;
Chris Lattner923fc052003-02-05 21:59:58 +0000199 std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
200 EdgeSourceCaptions[0] = "r";
201 if (Call.isDirectCall())
202 EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
Chris Lattnerf1c28382003-02-14 20:25:47 +0000203 else
204 EdgeSourceCaptions[1] = "f";
Chris Lattner923fc052003-02-05 21:59:58 +0000205
206 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
207 &EdgeSourceCaptions);
Chris Lattner962ee452002-10-16 20:16:16 +0000208
Chris Lattner482b6512002-10-21 13:47:57 +0000209 if (DSNode *N = Call.getRetVal().getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000210 int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
Chris Lattner482b6512002-10-21 13:47:57 +0000211 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000212 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000213 }
Chris Lattner923fc052003-02-05 21:59:58 +0000214
215 // Print out the callee...
216 if (Call.isIndirectCall()) {
217 DSNode *N = Call.getCalleeNode();
218 assert(N && "Null call site callee node!");
Chris Lattner352a6fa2003-02-13 20:14:40 +0000219 GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
Chris Lattner482b6512002-10-21 13:47:57 +0000220 }
Chris Lattner923fc052003-02-05 21:59:58 +0000221
Chris Lattner0969c502002-10-21 02:08:03 +0000222 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
223 if (DSNode *N = Call.getPtrArg(j).getNode()) {
Chris Lattner08db7192002-11-06 06:20:27 +0000224 int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
Chris Lattner962ee452002-10-16 20:16:16 +0000225 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner352a6fa2003-02-13 20:14:40 +0000226 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
Chris Lattner962ee452002-10-16 20:16:16 +0000227 }
228 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000229 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000230};
Chris Lattner9a927292003-11-12 23:11:14 +0000231} // end namespace llvm
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000232
Chris Lattnere17a4e82002-10-17 01:02:46 +0000233void DSNode::print(std::ostream &O, const DSGraph *G) const {
234 GraphWriter<const DSGraph *> W(O, G);
235 W.writeNode(this);
236}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000237
Chris Lattnere17a4e82002-10-17 01:02:46 +0000238void DSGraph::print(std::ostream &O) const {
239 WriteGraph(O, this, "DataStructures");
240}
241
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000242void DSGraph::writeGraphToFile(std::ostream &O,
243 const std::string &GraphName) const {
244 std::string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000245 O << "Writing '" << Filename << "'...";
246 std::ofstream F(Filename.c_str());
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000247
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000248 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000249 print(F);
Chris Lattner60525942002-11-11 00:01:02 +0000250 unsigned NumCalls = shouldPrintAuxCalls() ?
251 getAuxFunctionCalls().size() : getFunctionCalls().size();
252 O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000253 } else {
254 O << " error opening file for writing!\n";
255 }
256}
257
Chris Lattnere79eaa92003-02-10 18:17:07 +0000258/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
259/// then cleanup. For use from the debugger.
260///
261void DSGraph::viewGraph() const {
Reid Spencer3e0c1542006-06-05 15:44:46 +0000262 char pathsuff[9];
263
264 sprintf(pathsuff, "%06u", unsigned(rand()));
265
266 sys::Path TempDir = sys::Path::GetTemporaryDirectory();
267 sys::Path Filename = TempDir;
268 Filename.appendComponent("ds.tempgraph." + std::string(pathsuff) + ".dot");
269 std::cerr << "Writing '" << Filename << "'... ";
270 std::ofstream F(Filename.c_str());
271
Chris Lattnere79eaa92003-02-10 18:17:07 +0000272 if (!F.good()) {
Reid Spencer3e0c1542006-06-05 15:44:46 +0000273 std::cerr << " error opening file for writing!\n";
Chris Lattnere79eaa92003-02-10 18:17:07 +0000274 return;
275 }
Reid Spencer3e0c1542006-06-05 15:44:46 +0000276
Chris Lattnere79eaa92003-02-10 18:17:07 +0000277 print(F);
Chris Lattner2cec1d32003-02-11 19:27:27 +0000278 F.close();
Reid Spencer3e0c1542006-06-05 15:44:46 +0000279 std::cerr << "\n";
280
281#if HAVE_GRAPHVIZ
282 sys::Path Graphviz(LLVM_PATH_GRAPHVIZ);
283 std::vector<const char*> args;
284 args.push_back(Graphviz.c_str());
285 args.push_back(Filename.c_str());
286 args.push_back(0);
287
288 std::cerr << "Running 'Graphviz' program... " << std::flush;
289 if (sys::Program::ExecuteAndWait(Graphviz, &args[0])) {
290 std::cerr << "Error viewing graph: 'Graphviz' not in path?\n";
291 } else {
292 Filename.eraseFromDisk();
293 return;
294 }
295#elif (HAVE_GV && HAVE_DOT)
296 sys::Path PSFilename = TempDir;
297 PSFilename.appendComponent(std::string("ds.tempgraph") + "." + pathsuff + ".ps");
298
299 sys::Path dot(LLVM_PATH_DOT);
300 std::vector<const char*> args;
301 args.push_back(dot.c_str());
302 args.push_back("-Tps");
303 args.push_back("-Nfontname=Courier");
304 args.push_back("-Gsize=7.5,10");
305 args.push_back(Filename.c_str());
306 args.push_back("-o");
307 args.push_back(PSFilename.c_str());
308 args.push_back(0);
309
310 std::cerr << "Running 'dot' program... " << std::flush;
311 if (sys::Program::ExecuteAndWait(dot, &args[0])) {
312 std::cerr << "Error viewing graph: 'dot' not in path?\n";
313 } else {
314 std::cerr << "\n";
315
316 sys::Path gv(LLVM_PATH_GV);
317 args.clear();
318 args.push_back(gv.c_str());
319 args.push_back(PSFilename.c_str());
320 args.push_back(0);
321
322 sys::Program::ExecuteAndWait(gv, &args[0]);
323 }
324 Filename.eraseFromDisk();
325 PSFilename.eraseFromDisk();
326 return;
327#elif HAVE_DOTTY
328 sys::Path dotty(LLVM_PATH_DOTTY);
329 std::vector<const char*> args;
330 args.push_back(Filename.c_str());
331 args.push_back(0);
332
333 std::cerr << "Running 'dotty' program... " << std::flush;
334 if (sys::Program::ExecuteAndWait(dotty, &args[0])) {
335 std::cerr << "Error viewing graph: 'dotty' not in path?\n";
336 } else {
337#ifndef __MINGW32__ // Dotty spawns another app and doesn't wait until it returns
338 Filename.eraseFromDisk();
339#endif
340 return;
341 }
342#endif
343
344 Filename.eraseFromDisk();
345 TempDir.eraseFromDisk(true);
Chris Lattnere79eaa92003-02-10 18:17:07 +0000346}
347
348
Chris Lattner0d9bab82002-07-18 00:12:30 +0000349template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000350static void printCollection(const Collection &C, std::ostream &O,
Chris Lattnerb3416bc2003-02-01 04:01:21 +0000351 const Module *M, const std::string &Prefix) {
Chris Lattner97f51a32002-07-27 01:12:15 +0000352 if (M == 0) {
353 O << "Null Module pointer, cannot continue!\n";
354 return;
355 }
356
Chris Lattner14212332002-11-07 02:18:46 +0000357 unsigned TotalNumNodes = 0, TotalCallNodes = 0;
Chris Lattner97f51a32002-07-27 01:12:15 +0000358 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner4f7815f2002-11-10 06:53:59 +0000359 if (C.hasGraph(*I)) {
Chris Lattner95a80ad2002-11-07 01:54:44 +0000360 DSGraph &Gr = C.getDSGraph((Function&)*I);
Chris Lattner4f7815f2002-11-10 06:53:59 +0000361 unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
362 Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
Chris Lattnercf908252005-03-25 20:54:45 +0000363 bool IsDuplicateGraph = false;
Chris Lattner4f7815f2002-11-10 06:53:59 +0000364
Chris Lattner1e759992005-02-01 19:10:48 +0000365 if (I->getName() == "main" || !OnlyPrintMain) {
Chris Lattnera5f47ea2005-03-15 16:55:04 +0000366 Function *SCCFn = Gr.retnodes_begin()->first;
Chris Lattner0423e032005-03-25 20:37:32 +0000367 if (&*I == SCCFn) {
Chris Lattner1e759992005-02-01 19:10:48 +0000368 Gr.writeGraphToFile(O, Prefix+I->getName());
Chris Lattner0423e032005-03-25 20:37:32 +0000369 } else {
Chris Lattnercf908252005-03-25 20:54:45 +0000370 IsDuplicateGraph = true; // Don't double count node/call nodes.
Chris Lattner1e759992005-02-01 19:10:48 +0000371 O << "Didn't write '" << Prefix+I->getName()
372 << ".dot' - Graph already emitted to '" << Prefix+SCCFn->getName()
373 << "\n";
Chris Lattner0423e032005-03-25 20:37:32 +0000374 }
Chris Lattner1e759992005-02-01 19:10:48 +0000375 } else {
Chris Lattnercf908252005-03-25 20:54:45 +0000376 Function *SCCFn = Gr.retnodes_begin()->first;
377 if (&*I == SCCFn) {
378 O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
379 << Gr.getGraphSize() << "+" << NumCalls << "]\n";
380 } else {
381 IsDuplicateGraph = true; // Don't double count node/call nodes.
382 }
Chris Lattner95a80ad2002-11-07 01:54:44 +0000383 }
Chris Lattner49a1ed02002-11-18 21:42:45 +0000384
Chris Lattnercf908252005-03-25 20:54:45 +0000385 if (!IsDuplicateGraph) {
386 unsigned GraphSize = Gr.getGraphSize();
387 if (MaxGraphSize < GraphSize) MaxGraphSize = GraphSize;
Chris Lattnere92e7642004-02-07 23:58:05 +0000388
Chris Lattnercf908252005-03-25 20:54:45 +0000389 TotalNumNodes += Gr.getGraphSize();
390 TotalCallNodes += NumCalls;
391 for (DSGraph::node_iterator NI = Gr.node_begin(), E = Gr.node_end();
392 NI != E; ++NI)
393 if (NI->isNodeCompletelyFolded())
394 ++NumFoldedNodes;
395 }
Chris Lattner95a80ad2002-11-07 01:54:44 +0000396 }
Chris Lattner14212332002-11-07 02:18:46 +0000397
Chris Lattneraa0b4682002-11-09 21:12:07 +0000398 DSGraph &GG = C.getGlobalsGraph();
399 TotalNumNodes += GG.getGraphSize();
400 TotalCallNodes += GG.getFunctionCalls().size();
Chris Lattnerf76e7542002-11-09 21:40:58 +0000401 if (!OnlyPrintMain) {
Chris Lattneraa0b4682002-11-09 21:12:07 +0000402 GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
403 } else {
404 O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
405 << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
406 }
407
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000408 O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
Chris Lattner33312f72002-11-08 01:21:07 +0000409 << "] nodes total" << std::endl;
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000410}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000411
412
413// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000414void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000415 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000416 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000417}
418
Chris Lattner97f51a32002-07-27 01:12:15 +0000419void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000420 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000421 printCollection(*this, O, M, "bu.");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000422}
423
424void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000425 if (DontPrintAnything) return;
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000426 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000427}
Brian Gaeked0fde302003-11-11 22:41:34 +0000428
Chris Lattner79390d42003-11-13 05:05:41 +0000429void CompleteBUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattner4a857762004-01-22 13:42:43 +0000430 if (DontPrintAnything) return;
Chris Lattner79390d42003-11-13 05:05:41 +0000431 printCollection(*this, O, M, "cbu.");
432}
433
434
Chris Lattneradfd5f12005-03-13 19:51:24 +0000435void EquivClassGraphs::print(std::ostream &O, const Module *M) const {
436 if (DontPrintAnything) return;
437 printCollection(*this, O, M, "eq.");
438}
439