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"
|
|
|
|
#include "llvm/Module.h"
|
|
|
|
#include "llvm/Assembly/Writer.h"
|
|
|
|
#include <fstream>
|
|
|
|
#include <sstream>
|
2002-07-18 00:12:30 +00:00
|
|
|
using std::string;
|
2002-07-10 22:38:08 +00:00
|
|
|
|
|
|
|
void DSNode::dump() const { print(std::cerr, 0); }
|
|
|
|
|
2002-07-18 00:12:30 +00:00
|
|
|
string DSNode::getCaption(const DSGraph *G) const {
|
2002-07-10 22:38:08 +00:00
|
|
|
std::stringstream OS;
|
2002-07-11 20:33:32 +00:00
|
|
|
Module *M = G ? G->getFunction().getParent() : 0;
|
|
|
|
WriteTypeSymbolic(OS, getType(), M);
|
2002-07-10 22:38:08 +00:00
|
|
|
|
|
|
|
OS << " ";
|
|
|
|
if (NodeType & ScalarNode) OS << "S";
|
|
|
|
if (NodeType & AllocaNode) OS << "A";
|
|
|
|
if (NodeType & NewNode ) OS << "N";
|
|
|
|
if (NodeType & GlobalNode) OS << "G";
|
|
|
|
if (NodeType & SubElement) OS << "E";
|
|
|
|
if (NodeType & CastNode ) OS << "C";
|
2002-07-18 00:12:30 +00:00
|
|
|
if (NodeType & Incomplete) OS << "I";
|
2002-07-10 22:38:08 +00:00
|
|
|
|
2002-07-11 20:33:32 +00:00
|
|
|
for (unsigned i = 0, e = Globals.size(); i != e; ++i) {
|
|
|
|
OS << "\n";
|
|
|
|
WriteAsOperand(OS, Globals[i], false, true, M);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((NodeType & 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 == this) {
|
|
|
|
OS << "\n";
|
|
|
|
WriteAsOperand(OS, I->first, false, true, M);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-16 00:03:10 +00:00
|
|
|
const DSNode *VS, const std::string &EdgeAttr = "") {
|
2002-07-10 22:38:08 +00:00
|
|
|
O << "\tNode" << SrcNode << SrcNodePortName;
|
|
|
|
if (SrcNodeIdx != -1) O << SrcNodeIdx;
|
|
|
|
O << " -> Node" << (void*)VS;
|
|
|
|
|
|
|
|
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-07-16 00:03:10 +00:00
|
|
|
std::string Caption = escapeLabel(getCaption(G));
|
2002-07-10 22:38:08 +00:00
|
|
|
|
|
|
|
O << "\tNode" << (void*)this << " [ label =\"{" << Caption;
|
|
|
|
|
|
|
|
if (!Links.empty()) {
|
|
|
|
O << "|{";
|
|
|
|
for (unsigned i = 0; i < Links.size(); ++i) {
|
|
|
|
if (i) O << "|";
|
|
|
|
O << "<g" << i << ">";
|
|
|
|
}
|
|
|
|
O << "}";
|
|
|
|
}
|
|
|
|
O << "}\"];\n";
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < Links.size(); ++i)
|
|
|
|
if (Links[i])
|
|
|
|
writeEdge(O, this, ":g", i, Links[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
|
|
|
<< "\tlabel=\"Function\\ " << Func.getName() << "\";\n\n";
|
|
|
|
|
|
|
|
// 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-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)
|
|
|
|
if (Call[j])
|
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-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-10 22:38:08 +00:00
|
|
|
if (!I->isExternal()) {
|
2002-07-18 00:12:30 +00:00
|
|
|
string Filename = Prefix + "." + I->getName() + ".dot";
|
2002-07-10 22:38:08 +00:00
|
|
|
O << "Writing '" << Filename << "'...";
|
|
|
|
std::ofstream F(Filename.c_str());
|
2002-07-18 00:12:30 +00:00
|
|
|
|
2002-07-10 22:38:08 +00:00
|
|
|
if (F.good()) {
|
2002-07-27 01:12:15 +00:00
|
|
|
DSGraph &Graph = C.getDSGraph((Function&)*I);
|
2002-07-10 22:38:08 +00:00
|
|
|
Graph.print(F);
|
2002-07-18 00:12:30 +00:00
|
|
|
O << " [" << Graph.getGraphSize() << "+"
|
|
|
|
<< Graph.getFunctionCalls().size() << "]\n";
|
2002-07-10 22:38:08 +00:00
|
|
|
} else {
|
|
|
|
O << " error opening file for writing!\n";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
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-07-18 00:12:30 +00:00
|
|
|
printCollection(*this, O, M, "ds");
|
|
|
|
}
|
|
|
|
|
2002-07-27 01:12:15 +00:00
|
|
|
void BUDataStructures::print(std::ostream &O, const Module *M) const {
|
2002-07-18 00:12:30 +00:00
|
|
|
printCollection(*this, O, M, "bu");
|
|
|
|
}
|