2002-07-11 00:38:08 +02:00
|
|
|
//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
|
|
|
|
//
|
|
|
|
// This file implements the 'dot' graph printer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Analysis/DataStructure.h"
|
2002-10-03 00:14:38 +02:00
|
|
|
#include "llvm/Analysis/DSGraph.h"
|
2002-10-13 21:31:57 +02:00
|
|
|
#include "llvm/Analysis/DSGraphTraits.h"
|
2002-07-11 00:38:08 +02:00
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
2002-07-31 19:15:40 +02:00
|
|
|
#include "Support/CommandLine.h"
|
2002-10-13 21:31:57 +02:00
|
|
|
#include "Support/GraphWriter.h"
|
2002-07-11 00:38:08 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2002-07-18 02:12:30 +02:00
|
|
|
using std::string;
|
2002-07-11 00:38:08 +02:00
|
|
|
|
2002-10-03 22:38:41 +02: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-11 00:38:08 +02:00
|
|
|
void DSNode::dump() const { print(std::cerr, 0); }
|
|
|
|
|
2002-10-02 00:33:50 +02:00
|
|
|
static string getCaption(const DSNode *N, const DSGraph *G) {
|
2002-07-11 00:38:08 +02:00
|
|
|
std::stringstream OS;
|
2002-10-02 00:33:50 +02:00
|
|
|
Module *M = G && &G->getFunction() ? G->getFunction().getParent() : 0;
|
|
|
|
|
|
|
|
for (unsigned i = 0, e = N->getTypeEntries().size(); i != e; ++i) {
|
2002-10-18 20:22:46 +02:00
|
|
|
WriteTypeSymbolic(OS, N->getTypeEntries()[i].Ty, M);
|
|
|
|
if (N->getTypeEntries()[i].Offset)
|
|
|
|
OS << "@" << N->getTypeEntries()[i].Offset;
|
2002-10-20 22:29:10 +02:00
|
|
|
if (N->getTypeEntries()[i].isArray)
|
2002-10-20 22:39:17 +02:00
|
|
|
OS << " array";
|
2002-10-02 00:33:50 +02:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2002-11-03 20:46:15 +01:00
|
|
|
if (N->NodeType & DSNode::AllocaNode ) OS << "S";
|
2002-11-03 22:24:04 +01:00
|
|
|
if (N->NodeType & DSNode::HeapNode ) OS << "H";
|
2002-11-02 01:36:03 +01:00
|
|
|
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";
|
2002-10-02 00:33:50 +02:00
|
|
|
|
|
|
|
for (unsigned i = 0, e = N->getGlobals().size(); i != e; ++i) {
|
|
|
|
WriteAsOperand(OS, N->getGlobals()[i], false, true, M);
|
2002-07-11 22:33:32 +02:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2002-07-11 00:38:08 +02:00
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2002-10-13 21:31:57 +02:00
|
|
|
template<>
|
2002-10-17 03:02:46 +02:00
|
|
|
struct DOTGraphTraits<const DSGraph*> : public DefaultDOTGraphTraits {
|
|
|
|
static std::string getGraphName(const DSGraph *G) {
|
2002-10-13 21:31:57 +02:00
|
|
|
if (G->hasFunction())
|
|
|
|
return "Function " + G->getFunction().getName();
|
|
|
|
else
|
|
|
|
return "Non-function graph";
|
|
|
|
}
|
|
|
|
|
2002-10-17 03:02:46 +02:00
|
|
|
static const char *getGraphProperties(const DSGraph *G) {
|
2002-10-16 03:18:27 +02:00
|
|
|
return "\tedge [arrowtail=\"dot\"];\n"
|
2002-10-13 21:31:57 +02:00
|
|
|
"\tsize=\"10,7.5\";\n"
|
|
|
|
"\trotate=\"90\";\n";
|
|
|
|
}
|
|
|
|
|
2002-10-17 03:02:46 +02:00
|
|
|
static std::string getNodeLabel(const DSNode *Node, const DSGraph *Graph) {
|
2002-10-13 21:31:57 +02:00
|
|
|
return getCaption(Node, Graph);
|
|
|
|
}
|
|
|
|
|
2002-10-17 03:02:46 +02:00
|
|
|
static std::string getNodeAttributes(const DSNode *N) {
|
2002-10-16 03:18:27 +02:00
|
|
|
return "shape=Mrecord";//fontname=Courier";
|
2002-10-13 21:31:57 +02:00
|
|
|
}
|
|
|
|
|
2002-10-17 03:02:46 +02:00
|
|
|
static int getEdgeSourceLabel(const DSNode *Node, DSNode::iterator I) {
|
2002-10-16 03:18:27 +02:00
|
|
|
assert(Node == I.getNode() && "Iterator not for this node!");
|
2002-10-16 03:43:11 +02:00
|
|
|
return Node->getMergeMapLabel(I.getOffset());
|
2002-10-16 03:18:27 +02:00
|
|
|
}
|
2002-10-16 04:04:36 +02:00
|
|
|
|
|
|
|
/// addCustomGraphFeatures - Use this graph writing hook to emit call nodes
|
|
|
|
/// and the return node.
|
|
|
|
///
|
2002-10-17 03:02:46 +02:00
|
|
|
static void addCustomGraphFeatures(const DSGraph *G,
|
|
|
|
GraphWriter<const DSGraph*> &GW) {
|
2002-11-02 01:13:20 +01:00
|
|
|
// Add scalar nodes to the graph...
|
2002-11-03 22:27:48 +01:00
|
|
|
const std::map<Value*, DSNodeHandle> &VM = G->getScalarMap();
|
2002-11-02 01:13:20 +01:00
|
|
|
for (std::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, G->getFunction().getParent());
|
|
|
|
GW.emitSimpleNode(I->first, "plaintext=circle", OS.str());
|
|
|
|
|
|
|
|
// Add edge from return node to real destination
|
|
|
|
int EdgeDest = I->second.getOffset();
|
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
|
|
|
GW.emitEdge(I->first, -1, I->second.getNode(),
|
|
|
|
EdgeDest, "arrowtail=tee,color=gray63");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-16 04:04:36 +02: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
|
|
|
|
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 22:16:16 +02:00
|
|
|
|
|
|
|
// Output all of the call nodes...
|
2002-10-20 20:07:37 +02:00
|
|
|
const std::vector<DSCallSite> &FCs = G->getFunctionCalls();
|
2002-10-16 22:16:16 +02:00
|
|
|
for (unsigned i = 0, e = FCs.size(); i != e; ++i) {
|
2002-10-20 20:07:37 +02:00
|
|
|
const DSCallSite &Call = FCs[i];
|
2002-10-21 04:08:03 +02:00
|
|
|
GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2);
|
2002-10-16 22:16:16 +02:00
|
|
|
|
2002-10-21 15:47:57 +02:00
|
|
|
if (DSNode *N = Call.getRetVal().getNode()) {
|
|
|
|
int EdgeDest = Call.getRetVal().getOffset();
|
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
|
|
|
GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63");
|
|
|
|
}
|
|
|
|
if (DSNode *N = Call.getCallee().getNode()) {
|
|
|
|
int EdgeDest = Call.getCallee().getOffset();
|
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
|
|
|
GW.emitEdge(&Call, 1, N, EdgeDest, "color=gray63");
|
|
|
|
}
|
2002-10-21 04:08:03 +02:00
|
|
|
for (unsigned j = 0, e = Call.getNumPtrArgs(); j != e; ++j)
|
|
|
|
if (DSNode *N = Call.getPtrArg(j).getNode()) {
|
|
|
|
int EdgeDest = Call.getPtrArg(j).getOffset();
|
2002-10-16 22:16:16 +02:00
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
2002-10-21 04:08:03 +02:00
|
|
|
GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63");
|
2002-10-16 22:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
2002-10-16 04:04:36 +02:00
|
|
|
}
|
2002-10-13 21:31:57 +02:00
|
|
|
};
|
|
|
|
|
2002-10-17 03:02:46 +02: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 21:31:57 +02:00
|
|
|
|
2002-10-17 03:02:46 +02:00
|
|
|
void DSGraph::writeGraphToFile(std::ostream &O, const string &GraphName) const {
|
2002-10-02 00:33:50 +02:00
|
|
|
string Filename = GraphName + ".dot";
|
2002-07-31 00:07:26 +02:00
|
|
|
O << "Writing '" << Filename << "'...";
|
|
|
|
std::ofstream F(Filename.c_str());
|
|
|
|
|
|
|
|
if (F.good()) {
|
2002-10-17 03:02:46 +02:00
|
|
|
print(F);
|
2002-10-02 00:33:50 +02:00
|
|
|
O << " [" << getGraphSize() << "+" << getFunctionCalls().size() << "]\n";
|
2002-07-31 00:07:26 +02:00
|
|
|
} else {
|
|
|
|
O << " error opening file for writing!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-07-18 02:12:30 +02:00
|
|
|
template <typename Collection>
|
2002-07-27 03:12:15 +02: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 19:15:40 +02:00
|
|
|
if (!I->isExternal() && (I->getName() == "main" || !OnlyPrintMain))
|
2002-10-02 00:33:50 +02:00
|
|
|
C.getDSGraph((Function&)*I).writeGraphToFile(O, Prefix+I->getName());
|
2002-07-11 00:38:08 +02:00
|
|
|
}
|
2002-07-18 02:12:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
// print - Print out the analysis results...
|
2002-07-27 03:12:15 +02:00
|
|
|
void LocalDataStructures::print(std::ostream &O, const Module *M) const {
|
2002-10-02 00:33:50 +02:00
|
|
|
printCollection(*this, O, M, "ds.");
|
2002-07-18 02:12:30 +02:00
|
|
|
}
|
|
|
|
|
2002-07-27 03:12:15 +02:00
|
|
|
void BUDataStructures::print(std::ostream &O, const Module *M) const {
|
2002-10-02 00:33:50 +02:00
|
|
|
printCollection(*this, O, M, "bu.");
|
2002-10-03 22:38:41 +02:00
|
|
|
#if 0
|
2002-07-31 00:07:26 +02:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!I->isExternal()) {
|
2002-10-02 00:33:50 +02:00
|
|
|
(*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
|
2002-07-31 00:07:26 +02:00
|
|
|
break;
|
|
|
|
}
|
2002-10-03 22:38:41 +02:00
|
|
|
#endif
|
2002-07-31 00:07:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void TDDataStructures::print(std::ostream &O, const Module *M) const {
|
2002-10-02 00:33:50 +02:00
|
|
|
printCollection(*this, O, M, "td.");
|
2002-10-17 06:24:30 +02:00
|
|
|
#if 0
|
2002-07-31 00:07:26 +02:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
|
|
|
if (!I->isExternal()) {
|
2002-10-02 00:33:50 +02:00
|
|
|
(*getDSGraph(*I).GlobalsGraph)->writeGraphToFile(O, "gg.program");
|
2002-07-31 00:07:26 +02:00
|
|
|
break;
|
|
|
|
}
|
2002-10-02 00:33:50 +02:00
|
|
|
#endif
|
2002-10-17 06:24:30 +02:00
|
|
|
}
|