blob: dade3f750a51ef43ba81d7d7c75f5e364e544006 [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) {
31 WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
32 if (N->getTypeEntries()[i].second)
33 OS << "@" << N->getTypeEntries()[i].second;
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";
42
43 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
44 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
45 OS << "\n";
46 }
47
48 if ((N->NodeType & DSNode::ScalarNode) && G) {
Chris Lattner76d5b482002-07-11 20:33:32 +000049 const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
50 for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
51 E = VM.end(); I != E; ++I)
Chris Lattnerfccd06f2002-10-01 22:33:50 +000052 if (I->second.getNode() == N) {
Chris Lattner76d5b482002-07-11 20:33:32 +000053 WriteAsOperand(OS, I->first, false, true, M);
Chris Lattnerfccd06f2002-10-01 22:33:50 +000054 OS << "\n";
Chris Lattner76d5b482002-07-11 20:33:32 +000055 }
56 }
Chris Lattnerc68c31b2002-07-10 22:38:08 +000057 return OS.str();
58}
59
Chris Lattnerf6c52db2002-10-13 19:31:57 +000060template<>
Chris Lattnere17a4e82002-10-17 01:02:46 +000061struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
62 static std::string getGraphName(const DSGraph *G) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000063 if (G->hasFunction())
64 return "Function " + G->getFunction().getName();
65 else
66 return "Non-function graph";
67 }
68
Chris Lattnere17a4e82002-10-17 01:02:46 +000069 static const char *getGraphProperties(const DSGraph *G) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000070 return "\tedge [arrowtail=\"dot\"];\n"
Chris Lattnerf6c52db2002-10-13 19:31:57 +000071 "\tsize=\"10,7.5\";\n"
72 "\trotate=\"90\";\n";
73 }
74
Chris Lattnere17a4e82002-10-17 01:02:46 +000075 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
Chris Lattnerf6c52db2002-10-13 19:31:57 +000076 return getCaption(Node, Graph);
77 }
78
Chris Lattnere17a4e82002-10-17 01:02:46 +000079 static std::string getNodeAttributes(const DSNode *N) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000080 return "shape=Mrecord";//fontname=Courier";
Chris Lattnerf6c52db2002-10-13 19:31:57 +000081 }
82
Chris Lattnere17a4e82002-10-17 01:02:46 +000083 static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
Chris Lattnerf29e3072002-10-16 01:18:27 +000084 assert(Node == I.getNode() && "Iterator not for this node!");
Chris Lattnerff5feed2002-10-16 01:43:11 +000085 return Node->getMergeMapLabel(I.getOffset());
Chris Lattnerf29e3072002-10-16 01:18:27 +000086 }
Chris Lattnereb265cd2002-10-16 02:04:36 +000087
88 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
89 /// and the return node.
90 ///
Chris Lattnere17a4e82002-10-17 01:02:46 +000091 static void addCustomGraphFeatures(const DSGraph *G,
92 GraphWriter<const DSGraph*> &GW) {
Chris Lattnereb265cd2002-10-16 02:04:36 +000093 // Output the returned value pointer...
94 if (G->getRetNode().getNode() != 0) {
95 // Output the return node...
96 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
97
98 // Add edge from return node to real destination
99 int RetEdgeDest = G->getRetNode().getOffset();
100 if (RetEdgeDest == 0) RetEdgeDest = -1;
101 GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
102 RetEdgeDest, "arrowtail=tee,color=gray63");
103 }
Chris Lattner962ee452002-10-16 20:16:16 +0000104
105 // Output all of the call nodes...
106 const std::vector<std::vector<DSNodeHandle> > &FCs = G->getFunctionCalls();
107 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
108 const std::vector<DSNodeHandle> &Call = FCs[i];
109 GW.emitSimpleNode(&Call, "shape=record", "call", Call.size());
110
111 for (unsigned j = 0, e = Call.size(); j != e; ++j)
112 if (Call[j].getNode()) {
113 int EdgeDest = Call[j].getOffset();
114 if (EdgeDest == 0) EdgeDest = -1;
115 GW.emitEdge(&Call, j, Call[j].getNode(), EdgeDest, "color=gray63");
116 }
117 }
Chris Lattnereb265cd2002-10-16 02:04:36 +0000118 }
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000119};
120
Chris Lattnere17a4e82002-10-17 01:02:46 +0000121void DSNode::print(std::ostream &O, const DSGraph *G) const {
122 GraphWriter<const DSGraph *> W(O, G);
123 W.writeNode(this);
124}
Chris Lattnerf6c52db2002-10-13 19:31:57 +0000125
Chris Lattnere17a4e82002-10-17 01:02:46 +0000126void DSGraph::print(std::ostream &O) const {
127 WriteGraph(O, this, "DataStructures");
128}
129
130void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000131 string Filename = GraphName + ".dot";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000132 O << "Writing '" << Filename << "'...";
133 std::ofstream F(Filename.c_str());
134
135 if (F.good()) {
Chris Lattnere17a4e82002-10-17 01:02:46 +0000136 print(F);
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000137 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000138 } else {
139 O << " error opening file for writing!\n";
140 }
141}
142
Chris Lattner0d9bab82002-07-18 00:12:30 +0000143template <typename Collection>
Chris Lattner97f51a32002-07-27 01:12:15 +0000144static void printCollection(const Collection &C, std::ostream &O,
145 const Module *M, const string &Prefix) {
146 if (M == 0) {
147 O << "Null Module pointer, cannot continue!\n";
148 return;
149 }
150
151 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattnerdadd49b2002-07-31 17:15:40 +0000152 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000153 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
Chris Lattnerc68c31b2002-07-10 22:38:08 +0000154}
Chris Lattner0d9bab82002-07-18 00:12:30 +0000155
156
157// print - Print out the analysis results...
Chris Lattner97f51a32002-07-27 01:12:15 +0000158void LocalDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000159 printCollection(*this, O, M, "ds.");
Chris Lattner0d9bab82002-07-18 00:12:30 +0000160}
161
Chris Lattner97f51a32002-07-27 01:12:15 +0000162void BUDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000163 printCollection(*this, O, M, "bu.");
Chris Lattner55c10582002-10-03 20:38:41 +0000164#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000165 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
166 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000167 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000168 break;
169 }
Chris Lattner55c10582002-10-03 20:38:41 +0000170#endif
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000171}
172
173void TDDataStructures::print(std::ostream &O, const Module *M) const {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000174 printCollection(*this, O, M, "td.");
Chris Lattnere25ab832002-10-17 04:24:30 +0000175#if 0
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000176 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
177 if (!I->isExternal()) {
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000178 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
Vikram S. Advedfd2f322002-07-30 22:07:26 +0000179 break;
180 }
Chris Lattnerfccd06f2002-10-01 22:33:50 +0000181#endif
Chris Lattnere25ab832002-10-17 04:24:30 +0000182}