#include "llvm/Assembly/Writer.h"
#include "Support/CommandLine.h"
#include "Support/GraphWriter.h"
+#include "Support/Statistic.h"
#include <fstream>
#include <sstream>
-using std::string;
// OnlyPrintMain - The DataStructure printer exposes this option to allow
// printing of only the graph for "main".
//
-static cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
+namespace {
+ cl::opt<bool> OnlyPrintMain("only-print-main-ds", cl::ReallyHidden);
+ Statistic<> MaxGraphSize ("dsnode", "Maximum graph size");
+ Statistic<> NumFoldedNodes ("dsnode", "Number of folded nodes (in final graph)");
+}
void DSNode::dump() const { print(std::cerr, 0); }
-static string getCaption(const DSNode *N, const DSGraph *G) {
+static std::string getCaption(const DSNode *N, const DSGraph *G) {
std::stringstream OS;
- Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
-
- for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
- WriteTypeSymbolic(OS, N->getTypeEntries()[i].first, M);
- if (N->getTypeEntries()[i].second)
- OS << "@" << N->getTypeEntries()[i].second;
+ Module *M = G && G->hasFunction() ? G->getFunction().getParent() : 0;
+
+ if (N->isNodeCompletelyFolded())
+ OS << "FOLDED";
+ else {
+ WriteTypeSymbolic(OS, N->getType(), M);
+ if (N->isArray())
+ OS << " array";
+ }
+ if (N->NodeType) {
+ OS << ": ";
+ if (N->NodeType & DSNode::AllocaNode ) OS << "S";
+ if (N->NodeType & DSNode::HeapNode ) OS << "H";
+ if (N->NodeType & DSNode::GlobalNode ) OS << "G";
+ if (N->NodeType & DSNode::UnknownNode) OS << "U";
+ if (N->NodeType & DSNode::Incomplete ) OS << "I";
+ if (N->NodeType & DSNode::Modified ) OS << "M";
+ if (N->NodeType & DSNode::Read ) OS << "R";
+ if (N->NodeType & DSNode::DEAD ) OS << "<dead>";
OS << "\n";
}
- if (N->NodeType & DSNode::ScalarNode) OS << "S";
- if (N->NodeType & DSNode::AllocaNode) OS << "A";
- if (N->NodeType & DSNode::NewNode ) OS << "N";
- if (N->NodeType & DSNode::GlobalNode) OS << "G";
- if (N->NodeType & DSNode::Incomplete) OS << "I";
-
for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
OS << "\n";
}
- if ((N->NodeType & DSNode::ScalarNode) && G) {
- const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
- for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
- E = VM.end(); I != E; ++I)
- if (I->second.getNode() == N) {
- WriteAsOperand(OS, I->first, false, true, M);
- OS << "\n";
- }
- }
- return OS.str();
-}
-
-static string getValueName(Value *V, Function &F) {
- std::stringstream OS;
- WriteAsOperand(OS, V, true, true, F.getParent());
return OS.str();
}
-
-
-static void replaceIn(string &S, char From, const string &To) {
- for (unsigned i = 0; i < S.size(); )
- if (S[i] == From) {
- S.replace(S.begin()+i, S.begin()+i+1,
- To.begin(), To.end());
- i += To.size();
- } else {
- ++i;
- }
-}
-
-static std::string escapeLabel(const std::string &In) {
- std::string Label(In);
- replaceIn(Label, '\\', "\\\\"); // Escape caption...
- replaceIn(Label, '\n', "\\n");
- replaceIn(Label, ' ', "\\ ");
- replaceIn(Label, '{', "\\{");
- replaceIn(Label, '}', "\\}");
- return Label;
-}
-
-static void writeEdge(std::ostream &O, const void *SrcNode,
- const char *SrcNodePortName, int SrcNodeIdx,
- const DSNodeHandle &VS,
- const std::string &EdgeAttr = "") {
- O << "\tNode" << SrcNode << SrcNodePortName;
- if (SrcNodeIdx != -1) O << SrcNodeIdx;
- O << " -> Node" << (void*)VS.getNode();
- if (VS.getOffset()) O << ":g" << VS.getOffset();
-
- if (!EdgeAttr.empty())
- O << "[" << EdgeAttr << "]";
- O << ";\n";
-}
-
-void DSNode::print(std::ostream &O, const DSGraph *G) const {
- std::string Caption = escapeLabel(getCaption(this, G));
-
- O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
-
- unsigned Size = getSize();
- if (Size > 64) Size = 64; // Don't print out HUGE graph nodes!
-
- if (getSize() != 0) {
- O << "|{";
- for (unsigned i = 0; i < Size; ++i) {
- if (i) O << "|";
- O << "<g" << i << ">" << (int)MergeMap[i];
- }
- if (Size != getSize())
- O << "|truncated...";
- O << "}";
- }
- O << "}\"];\n";
-
- for (unsigned i = 0; i != Size; ++i)
- if (const DSNodeHandle *DSN = getLink(i))
- writeEdge(O, this, ":g", i, *DSN);
-}
-
-void DSGraph::print(std::ostream &O) const {
- O << "digraph DataStructures {\n"
- << "\tnode [shape=Mrecord];\n"
- << "\tedge [arrowtail=\"dot\"];\n"
- << "\tsize=\"10,7.5\";\n"
- << "\trotate=\"90\";\n";
-
- if (Func != 0)
- O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
-
- // Output all of the nodes...
- for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
- Nodes[i]->print(O, this);
-
- O << "\n";
-
- // Output the returned value pointer...
- if (RetNode != 0) {
- O << "\tNode0x1" << "[ plaintext=circle, label =\""
- << escapeLabel("returning") << "\"];\n";
- writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
- }
-
- // Output all of the call nodes...
- for (unsigned i = 0, e = FunctionCalls.size(); i != e; ++i) {
- const std::vector<DSNodeHandle> &Call = FunctionCalls[i];
- O << "\tNode" << (void*)&Call << " [shape=record,label=\"{call|{";
- for (unsigned j = 0, e = Call.size(); j != e; ++j) {
- if (j) O << "|";
- O << "<g" << j << ">";
- }
- O << "}}\"];\n";
-
- for (unsigned j = 0, e = Call.size(); j != e; ++j)
- if (Call[j].getNode())
- writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
- }
-
-
- O << "}\n";
-}
-
template<>
-struct DOTGraphTraits<DSGraph*> : public DefaultDOTGraphTraits {
- static std::string getGraphName(DSGraph *G) {
+struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
+ static std::string getGraphName(const DSGraph *G) {
if (G->hasFunction())
return "Function " + G->getFunction().getName();
else
- return "Non-function graph";
+ return "Global graph";
}
- static const char *getGraphProperties(DSGraph *G) {
- return "\tnode [shape=Mrecord];\n"
- "\tedge [arrowtail=\"dot\"];\n"
- "\tsize=\"10,7.5\";\n"
+ static const char *getGraphProperties(const DSGraph *G) {
+ return "\tsize=\"10,7.5\";\n"
"\trotate=\"90\";\n";
}
- static std::string getNodeLabel(DSNode *Node, DSGraph *Graph) {
+ static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
return getCaption(Node, Graph);
}
- static std::string getNodeAttributes(DSNode *N) {
- return "";//fontname=Courier";
+ static std::string getNodeAttributes(const DSNode *N) {
+ return "shape=Mrecord";//fontname=Courier";
}
- //static int getEdgeSourceLabel(DSNode *Node, node_iterator I) {
- // return MergeMap[i];
- // }
+ /// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
+ /// and the return node.
+ ///
+ static void addCustomGraphFeatures(const DSGraph *G,
+ GraphWriter<const DSGraph*> &GW) {
+ Module *CurMod = G->hasFunction() ? G->getFunction().getParent() : 0;
+
+ // Add scalar nodes to the graph...
+ const hash_map<Value*, DSNodeHandle> &VM = G->getScalarMap();
+ for (hash_map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
+ I != VM.end(); ++I)
+ if (!isa<GlobalValue>(I->first)) {
+ std::stringstream OS;
+ WriteAsOperand(OS, I->first, false, true, CurMod);
+ GW.emitSimpleNode(I->first, "", OS.str());
+
+ // Add edge from return node to real destination
+ int EdgeDest = I->second.getOffset() >> DS::PointerShift;
+ if (EdgeDest == 0) EdgeDest = -1;
+ GW.emitEdge(I->first, -1, I->second.getNode(),
+ EdgeDest, "arrowtail=tee,color=gray63");
+ }
+
+
+ // Output the returned value pointer...
+ if (G->getRetNode().getNode() != 0) {
+ // Output the return node...
+ GW.emitSimpleNode((void*)1, "plaintext=circle", "returning");
+
+ // Add edge from return node to real destination
+ int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
+ if (RetEdgeDest == 0) RetEdgeDest = -1;
+ GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
+ RetEdgeDest, "arrowtail=tee,color=gray63");
+ }
+
+ // Output all of the call nodes...
+ const std::vector<DSCallSite> &FCs =
+ G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
+ : G->getFunctionCalls();
+ for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
+ const DSCallSite &Call = FCs[i];
+ std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
+ EdgeSourceCaptions[0] = "r";
+ if (Call.isDirectCall())
+ EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
+ else
+ EdgeSourceCaptions[1] = "f";
+
+ GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
+ &EdgeSourceCaptions);
+
+ if (DSNode *N = Call.getRetVal().getNode()) {
+ int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
+ if (EdgeDest == 0) EdgeDest = -1;
+ GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
+ }
+
+ // Print out the callee...
+ if (Call.isIndirectCall()) {
+ DSNode *N = Call.getCalleeNode();
+ assert(N && "Null call site callee node!");
+ GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
+ }
+
+ for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
+ if (DSNode *N = Call.getPtrArg(j).getNode()) {
+ int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
+ if (EdgeDest == 0) EdgeDest = -1;
+ GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
+ }
+ }
+ }
};
+void DSNode::print(std::ostream &O, const DSGraph *G) const {
+ GraphWriter<const DSGraph *> W(O, G);
+ W.writeNode(this);
+}
+void DSGraph::print(std::ostream &O) const {
+ WriteGraph(O, this, "DataStructures");
+}
-void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
- string Filename = GraphName + ".dot";
+void DSGraph::writeGraphToFile(std::ostream &O,
+ const std::string &GraphName) const {
+ std::string Filename = GraphName + ".dot";
O << "Writing '" << Filename << "'...";
std::ofstream F(Filename.c_str());
if (F.good()) {
- WriteGraph(F, this);
print(F);
- O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
+ unsigned NumCalls = shouldPrintAuxCalls() ?
+ getAuxFunctionCalls().size() : getFunctionCalls().size();
+ O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
} else {
O << " error opening file for writing!\n";
}
}
+/// viewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
+/// then cleanup. For use from the debugger.
+///
+void DSGraph::viewGraph() const {
+ std::ofstream F("/tmp/tempgraph.dot");
+ if (!F.good()) {
+ std::cerr << "Error opening '/tmp/tempgraph.dot' for temporary graph!\n";
+ return;
+ }
+ print(F);
+ F.close();
+ if (system("dot -Tps /tmp/tempgraph.dot > /tmp/tempgraph.ps"))
+ std::cerr << "Error running dot: 'dot' not in path?\n";
+ system("gv /tmp/tempgraph.ps");
+ system("rm /tmp/tempgraph.dot /tmp/tempgraph.ps");
+}
+
+
template <typename Collection>
static void printCollection(const Collection &C, std::ostream &O,
- const Module *M, const string &Prefix) {
+ const Module *M, const std::string &Prefix) {
if (M == 0) {
O << "Null Module pointer, cannot continue!\n";
return;
}
+ unsigned TotalNumNodes = 0, TotalCallNodes = 0;
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
- if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
- C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
+ if (C.hasGraph(*I)) {
+ DSGraph &Gr = C.getDSGraph((Function&)*I);
+ TotalNumNodes += Gr.getGraphSize();
+ unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
+ Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
+
+ TotalCallNodes += NumCalls;
+ if (I->getName() == "main" || !OnlyPrintMain)
+ Gr.writeGraphToFile(O, Prefix+I->getName());
+ else {
+ O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
+ << Gr.getGraphSize() << "+" << NumCalls << "]\n";
+ }
+
+ if (MaxGraphSize < Gr.getNodes().size())
+ MaxGraphSize = Gr.getNodes().size();
+ for (unsigned i = 0, e = Gr.getNodes().size(); i != e; ++i)
+ if (Gr.getNodes()[i]->isNodeCompletelyFolded())
+ ++NumFoldedNodes;
+ }
+
+ DSGraph &GG = C.getGlobalsGraph();
+ TotalNumNodes += GG.getGraphSize();
+ TotalCallNodes += GG.getFunctionCalls().size();
+ if (!OnlyPrintMain) {
+ GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
+ } else {
+ O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
+ << GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
+ }
+
+ O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
+ << "] nodes total" << std::endl;
}
void BUDataStructures::print(std::ostream &O, const Module *M) const {
printCollection(*this, O, M, "bu.");
-#if 0
- for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
- if (!I->isExternal()) {
- (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
- break;
- }
-#endif
}
-#if 0
void TDDataStructures::print(std::ostream &O, const Module *M) const {
printCollection(*this, O, M, "td.");
-
- for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
- if (!I->isExternal()) {
- (*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
- break;
- }
}
-#endif