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].Ty, M);
32 if (N->getTypeEntries()[i].Offset)
33 OS << "@" << N->getTypeEntries()[i].Offset;
34 if (N->getTypeEntries()[i].isArray)
39 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";
44 if (N->NodeType & DSNode::Modified ) OS << "M";
45 if (N->NodeType & DSNode::Read ) OS << "R";
47 for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
48 WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
52 if ((N->NodeType & DSNode::ScalarNode) && G) {
53 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)
56 if (I->second.getNode() == N) {
57 WriteAsOperand(OS, I->first, false, true, M);
65 struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
66 static std::string getGraphName(const DSGraph *G) {
68 return "Function " + G->getFunction().getName();
70 return "Non-function graph";
73 static const char *getGraphProperties(const DSGraph *G) {
74 return "\tedge [arrowtail=\"dot\"];\n"
75 "\tsize=\"10,7.5\";\n"
79 static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
80 return getCaption(Node, Graph);
83 static std::string getNodeAttributes(const DSNode *N) {
84 return "shape=Mrecord";//fontname=Courier";
87 static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
88 assert(Node == I.getNode() && "Iterator not for this node!");
89 return Node->getMergeMapLabel(I.getOffset());
92 /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
93 /// and the return node.
95 static void addCustomGraphFeatures(const DSGraph *G,
96 GraphWriter<const DSGraph*> &GW) {
97 // Output the returned value pointer...
98 if (G->getRetNode().getNode() != 0) {
99 // Output the return node...
100 GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
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");
109 // Output all of the call nodes...
110 const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
111 for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
112 const DSCallSite &Call = FCs[i];
113 GW.emitSimpleNode(&Call, "shape=record", "call", Call.size());
115 for (unsigned j = 0, e = Call.size(); j != e; ++j)
116 if (Call[j].getNode()) {
117 int EdgeDest = Call[j].getOffset();
118 if (EdgeDest == 0) EdgeDest = -1;
119 GW.emitEdge(&Call, j, Call[j].getNode(), EdgeDest, "color=gray63");
125 void DSNode::print(std::ostream &O, const DSGraph *G) const {
126 GraphWriter<const DSGraph *> W(O, G);
130 void DSGraph::print(std::ostream &O) const {
131 WriteGraph(O, this, "DataStructures");
134 void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
135 string Filename = GraphName + ".dot";
136 O << "Writing '" << Filename << "'...";
137 std::ofstream F(Filename.c_str());
141 O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
143 O << " error opening file for writing!\n";
147 template <typename Collection>
148 static void printCollection(const Collection &C, std::ostream &O,
149 const Module *M, const string &Prefix) {
151 O << "Null Module pointer, cannot continue!\n";
155 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
156 if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
157 C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
161 // print - Print out the analysis results...
162 void LocalDataStructures::print(std::ostream &O, const Module *M) const {
163 printCollection(*this, O, M, "ds.");
166 void BUDataStructures::print(std::ostream &O, const Module *M) const {
167 printCollection(*this, O, M, "bu.");
169 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
170 if (!I->isExternal()) {
171 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
177 void TDDataStructures::print(std::ostream &O, const Module *M) const {
178 printCollection(*this, O, M, "td.");
180 for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
181 if (!I->isExternal()) {
182 (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");