2002-07-11 00:38:08 +02:00
|
|
|
//===- Printer.cpp - Code for printing data structure graphs nicely -------===//
|
2003-10-20 21:43:21 +02:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-07-11 00:38:08 +02:00
|
|
|
//
|
|
|
|
// 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"
|
2003-07-01 18:27:32 +02:00
|
|
|
#include "llvm/Constants.h"
|
2002-07-11 00:38:08 +02:00
|
|
|
#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-11-18 22:42:45 +01:00
|
|
|
#include "Support/Statistic.h"
|
2002-07-11 00:38:08 +02:00
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2003-11-13 00:11:14 +01:00
|
|
|
using namespace llvm;
|
2003-11-11 23:41:34 +01: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".
|
|
|
|
//
|
2002-11-18 22:42:45 +01: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 22:38:41 +02:00
|
|
|
|
2002-07-11 00:38:08 +02:00
|
|
|
void DSNode::dump() const { print(std::cerr, 0); }
|
|
|
|
|
2003-02-01 05:01:21 +01:00
|
|
|
static std::string getCaption(const DSNode *N, const DSGraph *G) {
|
2002-07-11 00:38:08 +02:00
|
|
|
std::stringstream OS;
|
2003-06-30 05:15:25 +02:00
|
|
|
Module *M = 0;
|
|
|
|
// Get the module from ONE of the functions in the graph it is available.
|
|
|
|
if (G && !G->getReturnNodes().empty())
|
|
|
|
M = G->getReturnNodes().begin()->first->getParent();
|
2002-10-02 00:33:50 +02:00
|
|
|
|
2002-11-06 07:20:27 +01:00
|
|
|
if (N->isNodeCompletelyFolded())
|
2003-07-02 06:39:27 +02:00
|
|
|
OS << "COLLAPSED";
|
2002-11-06 07:20:27 +01:00
|
|
|
else {
|
2002-11-18 22:42:45 +01:00
|
|
|
WriteTypeSymbolic(OS, N->getType(), M);
|
|
|
|
if (N->isArray())
|
2002-10-20 22:39:17 +02:00
|
|
|
OS << " array";
|
2002-11-06 07:20:27 +01:00
|
|
|
}
|
2003-06-19 23:15:11 +02:00
|
|
|
if (unsigned NodeType = N->getNodeFlags()) {
|
2002-11-06 07:20:27 +01:00
|
|
|
OS << ": ";
|
2003-06-19 23:15:11 +02: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";
|
|
|
|
#ifndef NDEBUG
|
|
|
|
if (NodeType & DSNode::DEAD ) OS << "<dead>";
|
|
|
|
#endif
|
2002-10-02 00:33:50 +02: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 22:33:32 +02:00
|
|
|
OS << "\n";
|
|
|
|
}
|
|
|
|
|
2002-07-11 00:38:08 +02:00
|
|
|
return OS.str();
|
|
|
|
}
|
|
|
|
|
2003-11-13 00:11:14 +01:00
|
|
|
namespace llvm {
|
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) {
|
2003-06-30 05:15:25 +02:00
|
|
|
switch (G->getReturnNodes().size()) {
|
2003-06-30 07:57:39 +02:00
|
|
|
case 0: return G->getFunctionNames();
|
|
|
|
case 1: return "Function " + G->getFunctionNames();
|
|
|
|
default: return "Functions: " + G->getFunctionNames();
|
2003-06-30 05:15:25 +02:00
|
|
|
}
|
2002-10-13 21:31:57 +02:00
|
|
|
}
|
|
|
|
|
2002-10-17 03:02:46 +02:00
|
|
|
static const char *getGraphProperties(const DSGraph *G) {
|
2003-02-13 21:14:40 +01:00
|
|
|
return "\tsize=\"10,7.5\";\n"
|
2002-10-13 21:31:57 +02:00
|
|
|
"\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-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) {
|
2003-06-30 05:15:25 +02:00
|
|
|
Module *CurMod = 0;
|
|
|
|
if (!G->getReturnNodes().empty())
|
|
|
|
CurMod = G->getReturnNodes().begin()->first->getParent();
|
2003-02-04 01:03:18 +01:00
|
|
|
|
2002-11-02 01:13:20 +01:00
|
|
|
// Add scalar nodes to the graph...
|
2003-06-30 05:15:25 +02:00
|
|
|
const DSGraph::ScalarMapTy &VM = G->getScalarMap();
|
|
|
|
for (DSGraph::ScalarMapTy::const_iterator I = VM.begin(); I != VM.end();++I)
|
2003-07-01 18:27:32 +02:00
|
|
|
if (!isa<GlobalValue>(I->first) && !isa<ConstantPointerRef>(I->first)) {
|
2002-11-02 01:13:20 +01:00
|
|
|
std::stringstream OS;
|
2003-02-04 01:03:18 +01:00
|
|
|
WriteAsOperand(OS, I->first, false, true, CurMod);
|
2002-11-18 22:42:45 +01:00
|
|
|
GW.emitSimpleNode(I->first, "", OS.str());
|
2002-11-02 01:13:20 +01:00
|
|
|
|
|
|
|
// Add edge from return node to real destination
|
2002-11-06 07:20:27 +01:00
|
|
|
int EdgeDest = I->second.getOffset() >> DS::PointerShift;
|
2002-11-02 01:13:20 +01:00
|
|
|
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...
|
2003-06-30 05:15:25 +02:00
|
|
|
const DSGraph::ReturnNodesTy &RetNodes = G->getReturnNodes();
|
|
|
|
for (DSGraph::ReturnNodesTy::const_iterator I = RetNodes.begin(),
|
|
|
|
E = RetNodes.end(); I != E; ++I)
|
|
|
|
if (I->second.getNode()) {
|
|
|
|
std::string Label;
|
|
|
|
if (RetNodes.size() == 1)
|
|
|
|
Label = "returning";
|
|
|
|
else
|
|
|
|
Label = I->first->getName() + " ret node";
|
|
|
|
// Output the return node...
|
2003-11-12 05:58:19 +01:00
|
|
|
GW.emitSimpleNode((void*)I->first, "plaintext=circle", Label);
|
2003-06-30 05:15:25 +02:00
|
|
|
|
|
|
|
// Add edge from return node to real destination
|
|
|
|
int RetEdgeDest = I->second.getOffset() >> DS::PointerShift;;
|
|
|
|
if (RetEdgeDest == 0) RetEdgeDest = -1;
|
2003-11-12 05:58:19 +01:00
|
|
|
GW.emitEdge((void*)I->first, -1, I->second.getNode(),
|
2003-06-30 05:15:25 +02:00
|
|
|
RetEdgeDest, "arrowtail=tee,color=gray63");
|
|
|
|
}
|
2002-10-16 22:16:16 +02:00
|
|
|
|
|
|
|
// Output all of the call nodes...
|
2002-11-10 07:53:59 +01:00
|
|
|
const std::vector<DSCallSite> &FCs =
|
|
|
|
G->shouldPrintAuxCalls() ? G->getAuxFunctionCalls()
|
|
|
|
: 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];
|
2003-02-05 22:59:58 +01:00
|
|
|
std::vector<std::string> EdgeSourceCaptions(Call.getNumPtrArgs()+2);
|
|
|
|
EdgeSourceCaptions[0] = "r";
|
|
|
|
if (Call.isDirectCall())
|
|
|
|
EdgeSourceCaptions[1] = Call.getCalleeFunc()->getName();
|
2003-02-14 21:25:47 +01:00
|
|
|
else
|
|
|
|
EdgeSourceCaptions[1] = "f";
|
2003-02-05 22:59:58 +01:00
|
|
|
|
|
|
|
GW.emitSimpleNode(&Call, "shape=record", "call", Call.getNumPtrArgs()+2,
|
|
|
|
&EdgeSourceCaptions);
|
2002-10-16 22:16:16 +02:00
|
|
|
|
2002-10-21 15:47:57 +02:00
|
|
|
if (DSNode *N = Call.getRetVal().getNode()) {
|
2002-11-06 07:20:27 +01:00
|
|
|
int EdgeDest = Call.getRetVal().getOffset() >> DS::PointerShift;
|
2002-10-21 15:47:57 +02:00
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
2003-02-13 21:14:40 +01:00
|
|
|
GW.emitEdge(&Call, 0, N, EdgeDest, "color=gray63,tailclip=false");
|
2002-10-21 15:47:57 +02:00
|
|
|
}
|
2003-02-05 22:59:58 +01:00
|
|
|
|
|
|
|
// Print out the callee...
|
|
|
|
if (Call.isIndirectCall()) {
|
|
|
|
DSNode *N = Call.getCalleeNode();
|
|
|
|
assert(N && "Null call site callee node!");
|
2003-02-13 21:14:40 +01:00
|
|
|
GW.emitEdge(&Call, 1, N, -1, "color=gray63,tailclip=false");
|
2002-10-21 15:47:57 +02:00
|
|
|
}
|
2003-02-05 22:59:58 +01:00
|
|
|
|
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()) {
|
2002-11-06 07:20:27 +01:00
|
|
|
int EdgeDest = Call.getPtrArg(j).getOffset() >> DS::PointerShift;
|
2002-10-16 22:16:16 +02:00
|
|
|
if (EdgeDest == 0) EdgeDest = -1;
|
2003-02-13 21:14:40 +01:00
|
|
|
GW.emitEdge(&Call, j+2, N, EdgeDest, "color=gray63,tailclip=false");
|
2002-10-16 22:16:16 +02:00
|
|
|
}
|
|
|
|
}
|
2002-10-16 04:04:36 +02:00
|
|
|
}
|
2002-10-13 21:31:57 +02:00
|
|
|
};
|
2003-11-13 00:11:14 +01:00
|
|
|
} // end namespace llvm
|
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
|
|
|
|
2003-02-01 05:01:21 +01:00
|
|
|
void DSGraph::writeGraphToFile(std::ostream &O,
|
|
|
|
const std::string &GraphName) const {
|
|
|
|
std::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-11-11 01:01:02 +01:00
|
|
|
unsigned NumCalls = shouldPrintAuxCalls() ?
|
|
|
|
getAuxFunctionCalls().size() : getFunctionCalls().size();
|
|
|
|
O << " [" << getGraphSize() << "+" << NumCalls << "]\n";
|
2002-07-31 00:07:26 +02:00
|
|
|
} else {
|
|
|
|
O << " error opening file for writing!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-02-10 19:17:07 +01: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 20:27:27 +01:00
|
|
|
F.close();
|
2003-02-10 19:17:07 +01: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 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,
|
2003-02-01 05:01:21 +01:00
|
|
|
const Module *M, const std::string &Prefix) {
|
2002-07-27 03:12:15 +02:00
|
|
|
if (M == 0) {
|
|
|
|
O << "Null Module pointer, cannot continue!\n";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-11-07 03:18:46 +01:00
|
|
|
unsigned TotalNumNodes = 0, TotalCallNodes = 0;
|
2002-07-27 03:12:15 +02:00
|
|
|
for (Module::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
|
2002-11-10 07:53:59 +01:00
|
|
|
if (C.hasGraph(*I)) {
|
2002-11-07 02:54:44 +01:00
|
|
|
DSGraph &Gr = C.getDSGraph((Function&)*I);
|
2002-11-07 03:18:46 +01:00
|
|
|
TotalNumNodes += Gr.getGraphSize();
|
2002-11-10 07:53:59 +01:00
|
|
|
unsigned NumCalls = Gr.shouldPrintAuxCalls() ?
|
|
|
|
Gr.getAuxFunctionCalls().size() : Gr.getFunctionCalls().size();
|
|
|
|
|
|
|
|
TotalCallNodes += NumCalls;
|
2002-11-07 02:54:44 +01:00
|
|
|
if (I->getName() == "main" || !OnlyPrintMain)
|
|
|
|
Gr.writeGraphToFile(O, Prefix+I->getName());
|
|
|
|
else {
|
|
|
|
O << "Skipped Writing '" << Prefix+I->getName() << ".dot'... ["
|
2002-11-10 07:53:59 +01:00
|
|
|
<< Gr.getGraphSize() << "+" << NumCalls << "]\n";
|
2002-11-07 02:54:44 +01:00
|
|
|
}
|
2002-11-18 22:42:45 +01: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 02:54:44 +01:00
|
|
|
}
|
2002-11-07 03:18:46 +01:00
|
|
|
|
2002-11-09 22:12:07 +01:00
|
|
|
DSGraph &GG = C.getGlobalsGraph();
|
|
|
|
TotalNumNodes += GG.getGraphSize();
|
|
|
|
TotalCallNodes += GG.getFunctionCalls().size();
|
2002-11-09 22:40:58 +01:00
|
|
|
if (!OnlyPrintMain) {
|
2002-11-09 22:12:07 +01:00
|
|
|
GG.writeGraphToFile(O, Prefix+"GlobalsGraph");
|
|
|
|
} else {
|
|
|
|
O << "Skipped Writing '" << Prefix << "GlobalsGraph.dot'... ["
|
|
|
|
<< GG.getGraphSize() << "+" << GG.getFunctionCalls().size() << "]\n";
|
|
|
|
}
|
|
|
|
|
2002-11-07 03:18:46 +01:00
|
|
|
O << "\nGraphs contain [" << TotalNumNodes << "+" << TotalCallNodes
|
2002-11-08 02:21:07 +01:00
|
|
|
<< "] nodes total" << std::endl;
|
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-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
|
|
|
}
|
2003-11-11 23:41:34 +01:00
|
|
|
|