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-07-10 22:38:08 +00:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2002-07-18 00:12:30 +00:00
|
|
|
using std::string;
|
2002-07-10 22:38:08 +00:00
|
|
|
|
2002-10-03 20:38:41 +00:00
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
2002-07-10 22:38:08 +00:00
|
|
|
void DSNode::dump() const { print(std::cerr, 0); }
|
|
|
|
|
2002-10-01 22:33:50 +00:00
|
|
|
static string getCaption(const DSNode *N, const DSGraph *G) {
|
2002-07-10 22:38:08 +00:00
|
|
|
std::stringstream OS;
|
2002-10-01 22:33:50 +00:00
|
|
|
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;
|
|
|
|
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);
|
2002-07-11 20:33:32 +00:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2002-10-01 22:33:50 +00:00
|
|
|
if ((N->NodeType & DSNode::ScalarNode) && G) {
|
2002-07-11 20:33:32 +00:00
|
|
|
const std::map<Value*, DSNodeHandle> &VM = G->getValueMap();
|
|
|
|
for (std::map<Value*, DSNodeHandle>::const_iterator I = VM.begin(),
|
|
|
|
E = VM.end(); I != E; ++I)
|
2002-10-01 22:33:50 +00:00
|
|
|
if (I->second.getNode() == N) {
|
2002-07-11 20:33:32 +00:00
|
|
|
WriteAsOperand(OS, I->first, false, true, M);
|
2002-10-01 22:33:50 +00:00
|
|
|
OS << "\n";
|
2002-07-11 20:33:32 +00:00
|
|
|
}
|
|
|
|
}
|
2002-07-10 22:38:08 +00:00
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
static string getValueName(Value *V, Function &F) {
|
2002-07-10 22:38:08 +00:00
|
|
|
std::stringstream OS;
|
|
|
|
WriteAsOperand(OS, V, true, true, F.getParent());
|
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
static void replaceIn(string &S, char From, const string &To) {
|
2002-07-10 22:38:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-16 00:03:10 +00:00
|
|
|
static std::string escapeLabel(const std::string &In) {
|
|
|
|
std::string Label(In);
|
2002-07-10 22:38:08 +00:00
|
|
|
replaceIn(Label, '\\', "\\\\"); // Escape caption...
|
2002-07-11 20:33:32 +00:00
|
|
|
replaceIn(Label, '\n', "\\n");
|
2002-07-10 22:38:08 +00:00
|
|
|
replaceIn(Label, ' ', "\\ ");
|
|
|
|
replaceIn(Label, '{', "\\{");
|
|
|
|
replaceIn(Label, '}', "\\}");
|
|
|
|
return Label;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void writeEdge(std::ostream &O, const void *SrcNode,
|
|
|
|
const char *SrcNodePortName, int SrcNodeIdx,
|
2002-10-01 22:33:50 +00:00
|
|
|
const DSNodeHandle &VS,
|
|
|
|
const std::string &EdgeAttr = "") {
|
2002-07-10 22:38:08 +00:00
|
|
|
O << "\tNode" << SrcNode << SrcNodePortName;
|
|
|
|
if (SrcNodeIdx != -1) O << SrcNodeIdx;
|
2002-10-01 22:33:50 +00:00
|
|
|
O << " -> Node" << (void*)VS.getNode();
|
|
|
|
if (VS.getOffset()) O << ":g" << VS.getOffset();
|
2002-07-10 22:38:08 +00:00
|
|
|
|
|
|
|
if (!EdgeAttr.empty())
|
|
|
|
O << "[" << EdgeAttr << "]";
|
|
|
|
O << ";\n";
|
|
|
|
}
|
|
|
|
|
2002-07-11 20:33:32 +00:00
|
|
|
void DSNode::print(std::ostream &O, const DSGraph *G) const {
|
2002-10-01 22:33:50 +00:00
|
|
|
std::string Caption = escapeLabel(getCaption(this, G));
|
2002-07-10 22:38:08 +00:00
|
|
|
|
|
|
|
O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
|
|
|
|
|
2002-10-03 21:55:13 +00:00
|
|
|
unsigned Size = getSize();
|
|
|
|
if (Size > 64) Size = 64; // Don't print out HUGE graph nodes!
|
|
|
|
|
2002-10-01 22:33:50 +00:00
|
|
|
if (getSize() != 0) {
|
2002-07-10 22:38:08 +00:00
|
|
|
O << "|{";
|
2002-10-03 21:55:13 +00:00
|
|
|
for (unsigned i = 0; i < Size; ++i) {
|
2002-07-10 22:38:08 +00:00
|
|
|
if (i) O << "|";
|
2002-10-02 05:17:55 +00:00
|
|
|
O << "<g" << i << ">" << (int)MergeMap[i];
|
2002-07-10 22:38:08 +00:00
|
|
|
}
|
2002-10-03 21:55:13 +00:00
|
|
|
if (Size != getSize())
|
|
|
|
O << "|truncated...";
|
2002-07-10 22:38:08 +00:00
|
|
|
O << "}";
|
|
|
|
}
|
|
|
|
O << "}\"];\n";
|
|
|
|
|
2002-10-03 21:55:13 +00:00
|
|
|
for (unsigned i = 0; i != Size; ++i)
|
2002-10-01 22:33:50 +00:00
|
|
|
if (const DSNodeHandle *DSN = getLink(i))
|
|
|
|
writeEdge(O, this, ":g", i, *DSN);
|
2002-07-10 22:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void DSGraph::print(std::ostream &O) const {
|
|
|
|
O << "digraph DataStructures {\n"
|
|
|
|
<< "\tnode [shape=Mrecord];\n"
|
|
|
|
<< "\tedge [arrowtail=\"dot\"];\n"
|
|
|
|
<< "\tsize=\"10,7.5\";\n"
|
2002-07-30 22:07:26 +00:00
|
|
|
<< "\trotate=\"90\";\n";
|
|
|
|
|
2002-10-01 22:33:50 +00:00
|
|
|
if (Func != 0)
|
|
|
|
O << "\tlabel=\"Function\\ " << Func->getName() << "\";\n\n";
|
2002-07-10 22:38:08 +00:00
|
|
|
|
|
|
|
// Output all of the nodes...
|
|
|
|
for (unsigned i = 0, e = Nodes.size(); i != e; ++i)
|
2002-07-11 20:33:32 +00:00
|
|
|
Nodes[i]->print(O, this);
|
2002-07-10 22:38:08 +00:00
|
|
|
|
|
|
|
O << "\n";
|
|
|
|
|
|
|
|
// Output the returned value pointer...
|
|
|
|
if (RetNode != 0) {
|
2002-07-11 20:33:32 +00:00
|
|
|
O << "\tNode0x1" << "[ plaintext=circle, label =\""
|
|
|
|
<< escapeLabel("returning") << "\"];\n";
|
|
|
|
writeEdge(O, (void*)1, "", -1, RetNode, "arrowtail=tee,color=gray63");
|
2002-10-16 02:04:36 +00:00
|
|
|
}
|
2002-07-10 22:38:08 +00:00
|
|
|
|
|
|
|
// 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)
|
2002-10-01 22:33:50 +00:00
|
|
|
if (Call[j].getNode())
|
2002-07-11 20:33:32 +00:00
|
|
|
writeEdge(O, &Call, ":g", j, Call[j], "color=gray63");
|
2002-07-10 22:38:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
O << "}\n";
|
|
|
|
}
|
|
|
|
|
2002-10-13 19:31:57 +00:00
|
|
|
template<>
|
|
|
|
struct DOTGraphTraits<DSGraph*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(DSGraph *G) {
|
|
|
|
if (G->hasFunction())
|
|
|
|
return "Function " + G->getFunction().getName();
|
|
|
|
else
|
|
|
|
return "Non-function graph";
|
|
|
|
}
|
|
|
|
|
|
|
|
static const char *getGraphProperties(DSGraph *G) {
|
2002-10-16 01:18:27 +00:00
|
|
|
return "\tedge [arrowtail=\"dot\"];\n"
|
2002-10-13 19:31:57 +00:00
|
|
|
"\tsize=\"10,7.5\";\n"
|
|
|
|
"\trotate=\"90\";\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getNodeLabel(DSNode *Node, DSGraph *Graph) {
|
|
|
|
return getCaption(Node, Graph);
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::string getNodeAttributes(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 01:18:27 +00:00
|
|
|
static int getEdgeSourceLabel(DSNode *Node, DSNode::iterator I) {
|
|
|
|
assert(Node == I.getNode() && "Iterator not for this node!");
|
2002-10-16 01:43:11 +00:00
|
|
|
return Node->getMergeMapLabel(I.getOffset());
|
2002-10-16 01:18:27 +00:00
|
|
|
}
|
2002-10-16 02:04:36 +00:00
|
|
|
|
|
|
|
/// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
|
|
|
|
/// and the return node.
|
|
|
|
///
|
|
|
|
static void addCustomGraphFeatures(DSGraph *G, GraphWriter<DSGraph*> &GW) {
|
|
|
|
// 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();
|
|
|
|
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...
|
|
|
|
const std::vector<std::vector<DSNodeHandle> > &FCs = G->getFunctionCalls();
|
|
|
|
for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
|
|
|
|
const std::vector<DSNodeHandle> &Call = FCs[i];
|
|
|
|
GW.emitSimpleNode(&Call, "shape=record", "call", Call.size());
|
|
|
|
|
|
|
|
for (unsigned j = 0, e = Call.size(); j != e; ++j)
|
|
|
|
if (Call[j].getNode()) {
|
|
|
|
int EdgeDest = Call[j].getOffset();
|
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
|
|
|
GW.emitEdge(&Call, j, Call[j].getNode(), EdgeDest, "color=gray63");
|
|
|
|
}
|
|
|
|
}
|
2002-10-16 02:04:36 +00:00
|
|
|
}
|
2002-10-13 19:31:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2002-10-01 22:33:50 +00:00
|
|
|
void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) {
|
|
|
|
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-16 01:34:28 +00:00
|
|
|
WriteGraph(F, this, "DataStructures");
|
2002-10-16 01:18:27 +00:00
|
|
|
//print(F);
|
2002-10-01 22:33:50 +00:00
|
|
|
O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
|
2002-07-30 22:07:26 +00:00
|
|
|
} else {
|
|
|
|
O << " error opening file for writing!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
|
|
|
const Module *M, const string &Prefix) {
|
|
|
|
if (M == 0) {
|
|
|
|
O << "Null Module pointer, cannot continue!\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
2002-07-31 17:15:40 +00:00
|
|
|
if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
|
2002-10-01 22:33:50 +00:00
|
|
|
C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
|
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-10-03 20:38:41 +00:00
|
|
|
#if 0
|
2002-07-30 22:07:26 +00:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!I->isExternal()) {
|
2002-10-01 22:33:50 +00:00
|
|
|
(*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
|
2002-07-30 22:07:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-10-03 20:38:41 +00:00
|
|
|
#endif
|
2002-07-30 22:07:26 +00:00
|
|
|
}
|
|
|
|
|
2002-10-03 20:38:41 +00:00
|
|
|
#if 0
|
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-07-30 22:07:26 +00:00
|
|
|
|
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!I->isExternal()) {
|
2002-10-01 22:33:50 +00:00
|
|
|
(*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
|
2002-07-30 22:07:26 +00:00
|
|
|
break;
|
|
|
|
}
|
2002-07-18 00:12:30 +00:00
|
|
|
}
|
2002-10-01 22:33:50 +00:00
|
|
|
#endif
|