blob: 8b6e362cbc15ec115764b151e29a4f0d4811b637 [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::ScalarNode) OS << "S";
40 if (N->NodeType & DSNode::AllocaNode) OS << "A";
41 if (N->NodeType & DSNode::NewNode ) OS << "N";
42 if (N->NodeType & DSNode::GlobalNode) OS << "G";
43 if (N->NodeType & DSNode::Incomplete) OS << "I";
Chris Lattnerdc062d32002-10-17 22:13:28 +000044 if (N->NodeType & DSNode::Modified ) OS << "M";
45 if (N->NodeType & DSNode::Read ) OS << "R";
Chris Lattnerfccd06f2002-10-01 22:33:50 +000046
47 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
48 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
49 OS << "\n";
50 }
51
52 if ((N->NodeType & DSNode::ScalarNode) && G) {
Chris Lattner76d5b482002-07-11 20:33:32 +000053 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
54 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
55 E = VM.end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000056 if (I->second.getNode() == N) {
Chris Lattner76d5b482002-07-11 20:33:32 +000057 WriteAsOperand(OS, I->first, false, true, M);
Chris Lattnerfccd06f2002-10-01 22:33:50 +000058 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000059 }
60 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000061 return OS.str();
62}
63
Chris Lattnerf6c52db2002-10-13 19:31:57 +000064template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000065struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
66 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000067 if (G->hasFunction())
68 return "Function " + G->getFunction().getName();
69 else
70 return "Non-function graph";
71 }
72
Chris Lattnere17a4e82002-10-17 01:02:46 +000073 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000074 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000075 "\tsize=\"10,7.5\";\n"
76 "\trotate=\"90\";\n";
77 }
78
Chris Lattnere17a4e82002-10-17 01:02:46 +000079 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000080 return getCaption(Node, Graph);
81 }
82
Chris Lattnere17a4e82002-10-17 01:02:46 +000083 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000084 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000085 }
86
Chris Lattnere17a4e82002-10-17 01:02:46 +000087 static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000088 assert(Node == I.getNode() && "Iterator not for this node!");
Chris Lattnerff5feed2002-10-16 01:43:11 +000089 return Node->getMergeMapLabel(I.getOffset());
Chris Lattnerf29e3072002-10-16 01:18:27 +000090 }
Chris Lattnereb265cd2002-10-16 02:04:36 +000091
92 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
93 /// and the return node.
94 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000095 static void addCustomGraphFeatures(const DSGraph *G,
96 GraphWriter<const DSGraph*> &GW) {
Chris Lattnereb265cd2002-10-16 02:04:36 +000097 // Output the returned value pointer...
98 if (G->getRetNode().getNode() != 0) {
99 // Output the return node...
100 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
101
102 // Add edge from return node to real destination
103 int RetEdgeDest = G->getRetNode().getOffset();
104 if (RetEdgeDest == 0) RetEdgeDest = -1;
105 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
106 RetEdgeDest, "arrowtail=tee,color=gray63");
107 }
Chris Lattner962ee452002-10-16 20:16:16 +0000108
109 // Output all of the call nodes...
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000110 const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000111 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000112 const DSCallSite &Call = FCs[i];
Chris Lattner0969c502002-10-21 02:08:03 +0000113 GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
Chris Lattner962ee452002-10-16 20:16:16 +0000114
Chris Lattner0969c502002-10-21 02:08:03 +0000115 for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
116 if (DSNode *N = Call.getPtrArg(j).getNode()) {
117 int EdgeDest = Call.getPtrArg(j).getOffset();
Chris Lattner962ee452002-10-16 20:16:16 +0000118 if (EdgeDest == 0) EdgeDest = -1;
Chris Lattner0969c502002-10-21 02:08:03 +0000119 GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
Chris Lattner962ee452002-10-16 20:16:16 +0000120 }
121 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000122 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000123};
124
Chris Lattnere17a4e82002-10-17 01:02:46 +0000125void DSNode::print(std::ostream &O, const DSGraph *G) const {
126 GraphWriter<const DSGraph *> W(O, G);
127 W.writeNode(this);
128}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000129
Chris Lattnere17a4e82002-10-17 01:02:46 +0000130void DSGraph::print(std::ostream &O) const {
131 WriteGraph(O, this, "DataStructures");
132}
133
134void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000135 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000136 O << "Writing '" << Filename << "'...";
137 std::ofstream F(Filename.c_str());
138
139 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000140 print(F);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000141 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000142 } else {
143 O << " error opening file for writing!\n";
144 }
145}
146
Chris Lattner0d9bab82002-07-18 00:12:30 +0000147template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000148static void printCollection(const Collection &C, std::ostream &O,
149 const Module *M, const string &Prefix) {
150 if (M == 0) {
151 O << "Null Module pointer, cannot continue!\n";
152 return;
153 }
154
155 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000156 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000157 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000158}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000159
160
161// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000162void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000163 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000164}
165
Chris Lattner97f51a32002-07-27 01:12:15 +0000166void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000167 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000168#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000169 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
170 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000171 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000172 break;
173 }
Chris Lattner55c10582002-10-03 20:38:41 +0000174#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000175}
176
177void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000178 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000179#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000180 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
181 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000182 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000183 break;
184 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000185#endif
Chris Lattnere25ab832002-10-17 04:24:30 +0000186}