1 //===- Printer.cpp - Code for printing data structure graphs nicely -------===//
3 // This file implements the 'dot' graph printer.
5 //===----------------------------------------------------------------------===//
7 #include "llvm/Analysis/DataStructure.h"
8 #include "llvm/Analysis/DSGraph.h"
9 #include "llvm/Analysis/DSGraphTraits.h"
10 #include "llvm/Module.h"
11 #include "llvm/Assembly/Writer.h"
12 #include "Support/CommandLine.h"
13 #include "Support/GraphWriter.h"
18 // OnlyPrintMain - The DataStructure printer exposes this option to allow
19 // printing of only the graph for "main".
21 static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
24 void DSNode::dump() const { print(std::cerr, 0); }
26 static string getCaption(const DSNode *N, const DSGraph *G) {
28 Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
30 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;
37 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 if (N->NodeType & DSNode::Modified ) OS << "M";
43 if (N->NodeType & DSNode::Read ) OS << "R";
45 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
46 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
50 if ((N->NodeType & DSNode::ScalarNode) && G) {
51 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)
54 if (I->second.getNode() == N) {
55 WriteAsOperand(OS, I->first, false, true, M);
63 struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
64 static std::string getGraphName(const DSGraph *G) {
66 return "Function " + G->getFunction().getName();
68 return "Non-function graph";
71 static const char *getGraphProperties(const DSGraph *G) {
72 return "\tedge [arrowtail=\"dot\"];\n"
73 "\tsize=\"10,7.5\";\n"
77 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
78 return getCaption(Node, Graph);
81 static std::string getNodeAttributes(const DSNode *N) {
82 return "shape=Mrecord";//fontname=Courier";
85 static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
86 assert(Node == I.getNode() && "Iterator not for this node!");
87 return Node->getMergeMapLabel(I.getOffset());
90 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
91 /// and the return node.
93 static void addCustomGraphFeatures(const DSGraph *G,
94 GraphWriter<const DSGraph*> &GW) {
95 // Output the returned value pointer...
96 if (G->getRetNode().getNode() != 0) {
97 // Output the return node...
98 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
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");
107 // Output all of the call nodes...
108 const std::vector<std::vector<DSNodeHandle> > &FCs = G->getFunctionCalls();
109 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
110 const std::vector<DSNodeHandle> &Call = FCs[i];
111 GW.emitSimpleNode(&Call, "shape=record", "call", Call.size());
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");
123 void DSNode::print(std::ostream &O, const DSGraph *G) const {
124 GraphWriter<const DSGraph *> W(O, G);
128 void DSGraph::print(std::ostream &O) const {
129 WriteGraph(O, this, "DataStructures");
132 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
133 string Filename = GraphName + ".dot";
134 O << "Writing '" << Filename << "'...";
135 std::ofstream F(Filename.c_str());
139 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
141 O << " error opening file for writing!\n";
145 template <typename Collection>
146 static void printCollection(const Collection &C, std::ostream &O,
147 const Module *M, const string &Prefix) {
149 O << "Null Module pointer, cannot continue!\n";
153 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
154 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
155 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
159 // print - Print out the analysis results...
160 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
161 printCollection(*this, O, M, "ds.");
164 void BUDataStructures::print(std::ostream &O, const Module *M) const {
165 printCollection(*this, O, M, "bu.");
167 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
168 if (!I->isExternal()) {
169 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
175 void TDDataStructures::print(std::ostream &O, const Module *M) const {
176 printCollection(*this, O, M, "td.");
178 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
179 if (!I->isExternal()) {
180 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");