blob: fd6006c2a5dbd5c474c81de285222955fb9fc886 [file] [log] [blame]
Chris Lattnerc68c31b2002-07-10 22:38:08 +00001//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
2//
3// This file implements the 'dot' graph printer.
4//
5//===----------------------------------------------------------------------===//
6
7#include "llvm/Analysis/DataStructure.h"
Chris Lattnerc5f21de2002-10-02 22:14:38 +00008#include "llvm/Analysis/DSGraph.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +00009#include "llvm/Analysis/DSGraphTraits.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000010#include "llvm/Module.h"
11#include "llvm/Assembly/Writer.h"
Chris Lattnerdadd49b2002-07-31 17:15:40 +000012#include "Support/CommandLine.h"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000013#include "Support/GraphWriter.h"
Chris Lattnerc68c31b2002-07-10 22:38:08 +000014#include <fstream>
15#include <sstream>
Chris Lattner0d9bab82002-07-18 00:12:30 +000016using std::string;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000017
Chris Lattner55c10582002-10-03 20:38:41 +000018// OnlyPrintMain - The DataStructure printer exposes this option to allow
19// printing of only the graph for "main".
20//
21static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
22
23
Chris Lattnerc68c31b2002-07-10 22:38:08 +000024void DSNode::dump() const { print(std::cerr, 0); }
25
Chris Lattnerfccd06f2002-10-01 22:33:50 +000026static string getCaption(const DSNode *N, const DSGraph *G) {
Chris Lattnerc68c31b2002-07-10 22:38:08 +000027 std::stringstream OS;
Chris Lattnerfccd06f2002-10-01 22:33:50 +000028 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
Chris Lattnerc68c31b2002-07-10 22:38:08 +000029
Chris Lattnerfccd06f2002-10-01 22:33:50 +000030 for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
Chris Lattnera3f85862002-10-18 18:22:46 +000031 WriteTypeSymbolic(OS, N->getTypeEntries()[i].Ty, M);
32 if (N->getTypeEntries()[i].Offset)
33 OS << "@" << N->getTypeEntries()[i].Offset;
Chris Lattneraf4800b2002-10-20 20:29:10 +000034 if (N->getTypeEntries()[i].isArray)
Chris Lattnerd1f8d0a2002-10-20 20:39:17 +000035 OS << " array";
Chris Lattner76d5b482002-07-11 20:33:32 +000036 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000037 }
38
Chris Lattnerfccd06f2002-10-01 22:33:50 +000039 if (N->NodeType & DSNode::AllocaNode) OS << "A";
40 if (N->NodeType & DSNode::NewNode ) OS << "N";
41 if (N->NodeType & DSNode::GlobalNode) OS << "G";
42 if (N->NodeType & DSNode::Incomplete) OS << "I";
Chris Lattnerdc062d32002-10-17 22:13:28 +000043 if (N->NodeType & DSNode::Modified ) OS << "M";
44 if (N->NodeType & DSNode::Read ) OS << "R";
Chris Lattnerfccd06f2002-10-01 22:33:50 +000045
46 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
47 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
48 OS << "\n";
49 }
50
Chris Lattnerc68c31b2002-07-10 22:38:08 +000051 return OS.str();
52}
53
Chris Lattnerf6c52db2002-10-13 19:31:57 +000054template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000055struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
56 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000057 if (G->hasFunction())
58 return "Function " + G->getFunction().getName();
59 else
60 return "Non-function graph";
61 }
62
Chris Lattnere17a4e82002-10-17 01:02:46 +000063 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000064 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000065 "\tsize=\"10,7.5\";\n"
66 "\trotate=\"90\";\n";
67 }
68
Chris Lattnere17a4e82002-10-17 01:02:46 +000069 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000070 return getCaption(Node, Graph);
71 }
72
Chris Lattnere17a4e82002-10-17 01:02:46 +000073 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000074 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000075 }
76
Chris Lattnere17a4e82002-10-17 01:02:46 +000077 static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000078 assert(Node == I.getNode() && "Iterator not for this node!");
Chris Lattnerff5feed2002-10-16 01:43:11 +000079 return Node->getMergeMapLabel(I.getOffset());
Chris Lattnerf29e3072002-10-16 01:18:27 +000080 }
Chris Lattnereb265cd2002-10-16 02:04:36 +000081
82 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
83 /// and the return node.
84 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000085 static void addCustomGraphFeatures(const DSGraph *G,
86 GraphWriter<const DSGraph*> &GW) {
Chris Lattner92673292002-11-02 00:13:20 +000087 // Add scalar nodes to the graph...
88 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
89 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
90 I != VM.end(); ++I)
91 if (!isa<GlobalValue>(I->first)) {
92 std::stringstream OS;
93 WriteAsOperand(OS, I->first, false, true, G->getFunction().getParent());
94 GW.emitSimpleNode(I->first, "plaintext=circle", OS.str());
95
96 // Add edge from return node to real destination
97 int EdgeDest = I->second.getOffset();
98 if (EdgeDest == 0) EdgeDest = -1;
99 GW.emitEdge(I->first, -1, I->second.getNode(),
100 EdgeDest, "arrowtail=tee,color=gray63");
101 }
102
103
Chris Lattnereb265cd2002-10-16 02:04:36 +0000104 // Output the returned value pointer...
105 if (G->getRetNode().getNode() != 0) {
106 // Output the return node...
107 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
108
109 // Add edge from return node to real destination
110 int RetEdgeDest = G->getRetNode().getOffset();
111 if (RetEdgeDest == 0) RetEdgeDest = -1;
112 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
113 RetEdgeDest, "arrowtail=tee,color=gray63");
114 }
Chris Lattner962ee452002-10-16 20:16:16 +0000115
116 // Output all of the call nodes...
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000117 const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000118 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000119 const DSCallSite &Call = FCs[i];
Chris Lattner0969c502002-10-21 02:08:03 +0000120 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
Chris Lattner962ee452002-10-16 20:16:16 +0000121
Chris Lattner482b6512002-10-21 13:47:57 +0000122 if (DSNode *N = Call.getRetVal().getNode()) {
123 int EdgeDest = Call.getRetVal().getOffset();
124 if (EdgeDest == 0) EdgeDest = -1;
125 GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
126 }
127 if (DSNode *N = Call.getCallee().getNode()) {
128 int EdgeDest = Call.getCallee().getOffset();
129 if (EdgeDest == 0) EdgeDest = -1;
130 GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
131 }
Chris Lattner0969c502002-10-21 02:08:03 +0000132 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
133 if (DSNode *N = Call.getPtrArg(j).getNode()) {
134 int EdgeDest = Call.getPtrArg(j).getOffset();
Chris Lattner962ee452002-10-16 20:16:16 +0000135 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner0969c502002-10-21 02:08:03 +0000136 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
Chris Lattner962ee452002-10-16 20:16:16 +0000137 }
138 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000139 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000140};
141
Chris Lattnere17a4e82002-10-17 01:02:46 +0000142void DSNode::print(std::ostream &O, const DSGraph *G) const {
143 GraphWriter<const DSGraph *> W(O, G);
144 W.writeNode(this);
145}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000146
Chris Lattnere17a4e82002-10-17 01:02:46 +0000147void DSGraph::print(std::ostream &O) const {
148 WriteGraph(O, this, "DataStructures");
149}
150
151void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000152 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000153 O << "Writing '" << Filename << "'...";
154 std::ofstream F(Filename.c_str());
155
156 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000157 print(F);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000158 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000159 } else {
160 O << " error opening file for writing!\n";
161 }
162}
163
Chris Lattner0d9bab82002-07-18 00:12:30 +0000164template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000165static void printCollection(const Collection &C, std::ostream &O,
166 const Module *M, const string &Prefix) {
167 if (M == 0) {
168 O << "Null Module pointer, cannot continue!\n";
169 return;
170 }
171
172 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000173 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000174 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000175}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000176
177
178// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000179void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000180 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000181}
182
Chris Lattner97f51a32002-07-27 01:12:15 +0000183void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000184 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000185#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000186 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
187 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000188 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000189 break;
190 }
Chris Lattner55c10582002-10-03 20:38:41 +0000191#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000192}
193
194void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000195 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000196#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000197 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
198 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000199 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000200 break;
201 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000202#endif
Chris Lattnere25ab832002-10-17 04:24:30 +0000203}