2002-07-10 22:38:08 +00:00
|
|
|
//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
|
|
|
|
//
|
|
|
|
// This file implements the 'dot' graph printer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/DataStructure.h"
|
2002-10-02 22:14:38 +00:00
|
|
|
#include "llvm/Analysis/DSGraph.h"
|
2002-10-13 19:31:57 +00:00
|
|
|
#include "llvm/Analysis/DSGraphTraits.h"
|
2002-07-10 22:38:08 +00:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
2002-07-31 17:15:40 +00:00
|
|
|
#include "Support/CommandLine.h"
|
2002-10-13 19:31:57 +00:00
|
|
|
#include "Support/GraphWriter.h"
|
2002-11-18 21:42:45 +00:00
|
|
|
#include "Support/Statistic.h"
|
2002-07-10 22:38:08 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
|
|
|
|
2002-10-03 20:38:41 +00:00
|
|
|
// OnlyPrintMain - The DataStructure printer exposes this option to allow
|
|
|
|
// printing of only the graph for "main".
|
|
|
|
//
|
2002-11-18 21:42:45 +00:00
|
|
|
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)");
|
|
|
|
}
|
2002-10-03 20:38:41 +00:00
|
|
|
|
|
|
|
|
2002-07-10 22:38:08 +00:00
|
|
|
void DSNode::dump() const { print(std::cerr, 0); }
|
|
|
|
|
2003-02-01 04:01:21 +00:00
|
|
|
static std::string getCaption(const DSNode *N, const DSGraph *G) {
|
2002-07-10 22:38:08 +00:00
|
|
|
std::stringstream OS;
|
2003-02-04 00:03:18 +00:00
|
|
|
Module *M = G && G->hasFunction() ? G->getFunction().getParent() : 0;
|
2002-10-01 22:33:50 +00:00
|
|
|
|
2002-11-06 06:20:27 +00:00
|
|
|
if (N->isNodeCompletelyFolded())
|
|
|
|
OS << "FOLDED";
|
|
|
|
else {
|
2002-11-18 21:42:45 +00:00
|
|
|
WriteTypeSymbolic(OS, N->getType(), M);
|
|
|
|
if (N->isArray())
|
2002-10-20 20:39:17 +00:00
|
|
|
OS << " array";
|
2002-11-06 06:20:27 +00:00
|
|
|
}
|
2003-06-19 21:15:11 +00:00
|
|
|
if (unsigned NodeType = N->getNodeFlags()) {
|
2002-11-06 06:20:27 +00:00
|
|
|
OS << ": ";
|
2003-06-19 21:15:11 +00:00
|
|
|
if (NodeType & DSNode::AllocaNode ) OS << "S";
|
|
|
|
if (NodeType & DSNode::HeapNode ) OS << "H";
|
|
|
|
if (NodeType & DSNode::GlobalNode ) OS << "G";
|
|
|
|
if (NodeType & DSNode::UnknownNode) OS << "U";
|
|
|
|
if (NodeType & DSNode::Incomplete ) OS << "I";
|
|
|
|
if (NodeType & DSNode::Modified ) OS << "M";
|
|
|
|
if (NodeType & DSNode::Read ) OS << "R";
|
|
|
|
if (NodeType & DSNode::MultiObject) OS << "m";
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (NodeType & DSNode::DEAD ) OS << "<dead>";
|
|
|
|
#endif
|
2002-10-01 22:33:50 +00:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
|
|
|
|
WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
|
2002-07-11 20:33:32 +00:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2002-07-10 22:38:08 +00:00
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2002-10-13 19:31:57 +00:00
|
|
|
template<>
|
2002-10-17 01:02:46 +00:00
|
|
|
struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(const DSGraph *G) {
|
2002-10-13 19:31:57 +00:00
|
|
|
if (G->hasFunction())
|
|
|
|
return "Function " + G->getFunction().getName();
|
|
|
|
else
|
2003-02-11 19:27:27 +00:00
|
|
|
return "Global graph";
|
2002-10-13 19:31:57 +00:00
|
|
|
}
|
|
|
|
|
2002-10-17 01:02:46 +00:00
|
|
|
static const char *getGraphProperties(const DSGraph *G) {
|
2003-02-13 20:14:40 +00:00
|
|
|
return "\tsize=\"10,7.5\";\n"
|
2002-10-13 19:31:57 +00:00
|
|
|
"\trotate=\"90\";\n";
|
|
|
|
}
|
|
|
|
|
2002-10-17 01:02:46 +00:00
|
|
|
static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
|
2002-10-13 19:31:57 +00:00
|
|
|
return getCaption(Node, Graph);
|
|
|
|
}
|
|
|
|
|
2002-10-17 01:02:46 +00:00
|
|
|
static std::string getNodeAttributes(const DSNode *N) {
|
2002-10-16 01:18:27 +00:00
|
|
|
return "shape=Mrecord";//fontname=Courier";
|
2002-10-13 19:31:57 +00:00
|
|
|
}
|
|
|
|
|
2002-10-16 02:04:36 +00:00
|
|
|
/// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
|
|
|
|
/// and the return node.
|
|
|
|
///
|
2002-10-17 01:02:46 +00:00
|
|
|
static void addCustomGraphFeatures(const DSGraph *G,
|
|
|
|
GraphWriter<const DSGraph*> &GW) {
|
2003-02-04 00:03:18 +00:00
|
|
|
Module *CurMod = G->hasFunction() ? G->getFunction().getParent() : 0;
|
|
|
|
|
2002-11-02 00:13:20 +00:00
|
|
|
// Add scalar nodes to the graph...
|
2003-02-01 04:52:08 +00:00
|
|
|
const hash_map<Value*, DSNodeHandle> &VM = G->getScalarMap();
|
|
|
|
for (hash_map<Value*, DSNodeHandle>::const_iterator I = VM.begin();
|
2002-11-02 00:13:20 +00:00
|
|
|
I != VM.end(); ++I)
|
|
|
|
if (!isa<GlobalValue>(I->first)) {
|
|
|
|
std::stringstream OS;
|
2003-02-04 00:03:18 +00:00
|
|
|
WriteAsOperand(OS, I->first, false, true, CurMod);
|
2002-11-18 21:42:45 +00:00
|
|
|
GW.emitSimpleNode(I->first, "", OS.str());
|
2002-11-02 00:13:20 +00:00
|
|
|
|
|
|
|
// Add edge from return node to real destination
|
2002-11-06 06:20:27 +00:00
|
|
|
int EdgeDest = I->second.getOffset() >> DS::PointerShift;
|
2002-11-02 00:13:20 +00:00
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
|
|
|
GW.emitEdge(I->first, -1, I->second.getNode(),
|
|
|
|
EdgeDest, "arrowtail=tee,color=gray63");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-16 02:04:36 +00:00
|
|
|
// 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
|
2002-11-06 06:20:27 +00:00
|
|
|
int RetEdgeDest = G->getRetNode().getOffset() >> DS::PointerShift;;
|
2002-10-16 02:04:36 +00:00
|
|
|
if (RetEdgeDest == 0) RetEdgeDest = -1;
|
|
|
|
GW.emitEdge((void*)1, -1, G->getRetNode().getNode(),
|
|
|
|
RetEdgeDest, "arrowtail=tee,color=gray63");
|
|
|
|
}
|
2002-10-16 20:16:16 +00:00
|
|
|
|
|
|
|
// Output all of the call nodes...
|
2002-11-10 06:53:59 +00:00
|
|
|
const std::vector<DSCallSite> &FCs =
|
|
|
|
G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
|
|
|
|
: G->getFunctionCalls();
|
2002-10-16 20:16:16 +00:00
|
|
|
for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
|
2002-10-20 18:07:37 +00:00
|
|
|
const DSCallSite &Call = FCs[i];
|
2003-02-05 21:59:58 +00:00
|
|
|
std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
|
|
|
|
EdgeSourceCaptions[0] = "r";
|
|
|
|
if (Call.isDirectCall())
|
|
|
|
EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
|
2003-02-14 20:25:47 +00:00
|
|
|
else
|
|
|
|
EdgeSourceCaptions[1] = "f";
|
2003-02-05 21:59:58 +00:00
|
|
|
|
|
|
|
GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
|
|
|
|
&EdgeSourceCaptions);
|
2002-10-16 20:16:16 +00:00
|
|
|
|
2002-10-21 13:47:57 +00:00
|
|
|
if (DSNode *N = Call.getRetVal().getNode()) {
|
2002-11-06 06:20:27 +00:00
|
|
|
int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
|
2002-10-21 13:47:57 +00:00
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
2003-02-13 20:14:40 +00:00
|
|
|
GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
|
2002-10-21 13:47:57 +00:00
|
|
|
}
|
2003-02-05 21:59:58 +00:00
|
|
|
|
|
|
|
// Print out the callee...
|
|
|
|
if (Call.isIndirectCall()) {
|
|
|
|
DSNode *N = Call.getCalleeNode();
|
|
|
|
assert(N && "Null call site callee node!");
|
2003-02-13 20:14:40 +00:00
|
|
|
GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
|
2002-10-21 13:47:57 +00:00
|
|
|
}
|
2003-02-05 21:59:58 +00:00
|
|
|
|
2002-10-21 02:08:03 +00:00
|
|
|
for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
|
|
|
|
if (DSNode *N = Call.getPtrArg(j).getNode()) {
|
2002-11-06 06:20:27 +00:00
|
|
|
int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
|
2002-10-16 20:16:16 +00:00
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
2003-02-13 20:14:40 +00:00
|
|
|
GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
|
2002-10-16 20:16:16 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-16 02:04:36 +00:00
|
|
|
}
|
2002-10-13 19:31:57 +00:00
|
|
|
};
|
|
|
|
|
2002-10-17 01:02:46 +00:00
|
|
|
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");
|
|
|
|
}
|
2002-10-13 19:31:57 +00:00
|
|
|
|
2003-02-01 04:01:21 +00:00
|
|
|
void DSGraph::writeGraphToFile(std::ostream &O,
|
|
|
|
const std::string &GraphName) const {
|
|
|
|
std::string Filename = GraphName + ".dot";
|
2002-07-30 22:07:26 +00:00
|
|
|
O << "Writing '" << Filename << "'...";
|
|
|
|
std::ofstream F(Filename.c_str());
|
|
|
|
|
|
|
|
if (F.good()) {
|
2002-10-17 01:02:46 +00:00
|
|
|
print(F);
|
2002-11-11 00:01:02 +00:00
|
|
|
unsigned NumCalls = shouldPrintAuxCalls() ?
|
|
|
|
getAuxFunctionCalls().size() : getFunctionCalls().size();
|
|
|
|
O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
|
2002-07-30 22:07:26 +00:00
|
|
|
} else {
|
|
|
|
O << " error opening file for writing!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-10 18:17:07 +00:00
|
|
|
/// 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);
|
2003-02-11 19:27:27 +00:00
|
|
|
F.close();
|
2003-02-10 18:17:07 +00:00
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
template <typename Collection>
|
2002-07-27 01:12:15 +00:00
|
|
|
static void printCollection(const Collection &C, std::ostream &O,
|
2003-02-01 04:01:21 +00:00
|
|
|
const Module *M, const std::string &Prefix) {
|
2002-07-27 01:12:15 +00:00
|
|
|
if (M == 0) {
|
|
|
|
O << "Null Module pointer, cannot continue!\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-11-07 02:18:46 +00:00
|
|
|
unsigned TotalNumNodes = 0, TotalCallNodes = 0;
|
2002-07-27 01:12:15 +00:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
2002-11-10 06:53:59 +00:00
|
|
|
if (C.hasGraph(*I)) {
|
2002-11-07 01:54:44 +00:00
|
|
|
DSGraph &Gr = C.getDSGraph((Function&)*I);
|
2002-11-07 02:18:46 +00:00
|
|
|
TotalNumNodes += Gr.getGraphSize();
|
2002-11-10 06:53:59 +00:00
|
|
|
unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
|
|
|
|
Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
|
|
|
|
|
|
|
|
TotalCallNodes += NumCalls;
|
2002-11-07 01:54:44 +00:00
|
|
|
if (I->getName() == "main" || !OnlyPrintMain)
|
|
|
|
Gr.writeGraphToFile(O, Prefix+I->getName());
|
|
|
|
else {
|
|
|
|
O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
|
2002-11-10 06:53:59 +00:00
|
|
|
<< Gr.getGraphSize() << "+" << NumCalls << "]\n";
|
2002-11-07 01:54:44 +00:00
|
|
|
}
|
2002-11-18 21:42:45 +00:00
|
|
|
|
|
|
|
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;
|
2002-11-07 01:54:44 +00:00
|
|
|
}
|
2002-11-07 02:18:46 +00:00
|
|
|
|
2002-11-09 21:12:07 +00:00
|
|
|
DSGraph &GG = C.getGlobalsGraph();
|
|
|
|
TotalNumNodes += GG.getGraphSize();
|
|
|
|
TotalCallNodes += GG.getFunctionCalls().size();
|
2002-11-09 21:40:58 +00:00
|
|
|
if (!OnlyPrintMain) {
|
2002-11-09 21:12:07 +00:00
|
|
|
GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
|
|
|
|
} else {
|
|
|
|
O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
|
|
|
|
<< GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
|
|
|
|
}
|
|
|
|
|
2002-11-07 02:18:46 +00:00
|
|
|
O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
|
2002-11-08 01:21:07 +00:00
|
|
|
<< "] nodes total" << std::endl;
|
2002-07-10 22:38:08 +00:00
|
|
|
}
|
2002-07-18 00:12:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
// print - Print out the analysis results...
|
2002-07-27 01:12:15 +00:00
|
|
|
void LocalDataStructures::print(std::ostream &O, const Module *M) const {
|
2002-10-01 22:33:50 +00:00
|
|
|
printCollection(*this, O, M, "ds.");
|
2002-07-18 00:12:30 +00:00
|
|
|
}
|
|
|
|
|
2002-07-27 01:12:15 +00:00
|
|
|
void BUDataStructures::print(std::ostream &O, const Module *M) const {
|
2002-10-01 22:33:50 +00:00
|
|
|
printCollection(*this, O, M, "bu.");
|
2002-07-30 22:07:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void TDDataStructures::print(std::ostream &O, const Module *M) const {
|
2002-10-01 22:33:50 +00:00
|
|
|
printCollection(*this, O, M, "td.");
|
2002-10-17 04:24:30 +00:00
|
|
|
}
|