blob: 1c377f6f1e7b4718015606dab58f783a8e80cff2 [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 Lattner76d5b482002-07-11 20:33:32 +000034 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000035 }
36
Chris Lattnerfccd06f2002-10-01 22:33:50 +000037 if (N->NodeType & DSNode::ScalarNode) OS << "S";
38 if (N->NodeType & DSNode::AllocaNode) OS << "A";
39 if (N->NodeType & DSNode::NewNode ) OS << "N";
40 if (N->NodeType & DSNode::GlobalNode) OS << "G";
41 if (N->NodeType & DSNode::Incomplete) OS << "I";
Chris Lattnerdc062d32002-10-17 22:13:28 +000042 if (N->NodeType & DSNode::Modified ) OS << "M";
43 if (N->NodeType & DSNode::Read ) OS << "R";
Chris Lattnerfccd06f2002-10-01 22:33:50 +000044
45 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
46 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
47 OS << "\n";
48 }
49
50 if ((N->NodeType & DSNode::ScalarNode) && G) {
Chris Lattner76d5b482002-07-11 20:33:32 +000051 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
52 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
53 E = VM.end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000054 if (I->second.getNode() == N) {
Chris Lattner76d5b482002-07-11 20:33:32 +000055 WriteAsOperand(OS, I->first, false, true, M);
Chris Lattnerfccd06f2002-10-01 22:33:50 +000056 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000057 }
58 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000059 return OS.str();
60}
61
Chris Lattnerf6c52db2002-10-13 19:31:57 +000062template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000063struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
64 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000065 if (G->hasFunction())
66 return "Function " + G->getFunction().getName();
67 else
68 return "Non-function graph";
69 }
70
Chris Lattnere17a4e82002-10-17 01:02:46 +000071 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000072 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000073 "\tsize=\"10,7.5\";\n"
74 "\trotate=\"90\";\n";
75 }
76
Chris Lattnere17a4e82002-10-17 01:02:46 +000077 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000078 return getCaption(Node, Graph);
79 }
80
Chris Lattnere17a4e82002-10-17 01:02:46 +000081 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000082 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000083 }
84
Chris Lattnere17a4e82002-10-17 01:02:46 +000085 static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000086 assert(Node == I.getNode() && "Iterator not for this node!");
Chris Lattnerff5feed2002-10-16 01:43:11 +000087 return Node->getMergeMapLabel(I.getOffset());
Chris Lattnerf29e3072002-10-16 01:18:27 +000088 }
Chris Lattnereb265cd2002-10-16 02:04:36 +000089
90 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
91 /// and the return node.
92 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000093 static void addCustomGraphFeatures(const DSGraph *G,
94 GraphWriter<const DSGraph*> &GW) {
Chris Lattnereb265cd2002-10-16 02:04:36 +000095 // Output the returned value pointer...
96 if (G->getRetNode().getNode() != 0) {
97 // Output the return node...
98 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
99
100 // Add edge from return node to real destination
101 int RetEdgeDest = G->getRetNode().getOffset();
102 if (RetEdgeDest == 0) RetEdgeDest = -1;
103 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
104 RetEdgeDest, "arrowtail=tee,color=gray63");
105 }
Chris Lattner962ee452002-10-16 20:16:16 +0000106
107 // Output all of the call nodes...
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000108 const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
Chris Lattner962ee452002-10-16 20:16:16 +0000109 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
Vikram S. Adve42fd1692002-10-20 18:07:37 +0000110 const DSCallSite &Call = FCs[i];
Chris Lattner962ee452002-10-16 20:16:16 +0000111 GW.emitSimpleNode(&Call, "shape=record", "call", Call.size());
112
113 for (unsigned j = 0, e = Call.size(); j != e; ++j)
114 if (Call[j].getNode()) {
115 int EdgeDest = Call[j].getOffset();
116 if (EdgeDest == 0) EdgeDest = -1;
117 GW.emitEdge(&Call, j, Call[j].getNode(), EdgeDest, "color=gray63");
118 }
119 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000120 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000121};
122
Chris Lattnere17a4e82002-10-17 01:02:46 +0000123void DSNode::print(std::ostream &O, const DSGraph *G) const {
124 GraphWriter<const DSGraph *> W(O, G);
125 W.writeNode(this);
126}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000127
Chris Lattnere17a4e82002-10-17 01:02:46 +0000128void DSGraph::print(std::ostream &O) const {
129 WriteGraph(O, this, "DataStructures");
130}
131
132void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000133 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000134 O << "Writing '" << Filename << "'...";
135 std::ofstream F(Filename.c_str());
136
137 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000138 print(F);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000139 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000140 } else {
141 O << " error opening file for writing!\n";
142 }
143}
144
Chris Lattner0d9bab82002-07-18 00:12:30 +0000145template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000146static void printCollection(const Collection &C, std::ostream &O,
147 const Module *M, const string &Prefix) {
148 if (M == 0) {
149 O << "Null Module pointer, cannot continue!\n";
150 return;
151 }
152
153 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000154 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000155 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000156}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000157
158
159// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000160void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000161 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000162}
163
Chris Lattner97f51a32002-07-27 01:12:15 +0000164void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000165 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000166#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000167 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
168 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000169 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000170 break;
171 }
Chris Lattner55c10582002-10-03 20:38:41 +0000172#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000173}
174
175void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000176 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000177#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000178 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
179 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000180 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000181 break;
182 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000183#endif
Chris Lattnere25ab832002-10-17 04:24:30 +0000184}