2004-09-01 22:55:40 +00:00
|
|
|
//===-- llvm/Support/GraphWriter.h - Write graph to a .dot file -*- C++ -*-===//
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2005-04-21 20:48:15 +00:00
|
|
|
//
|
2003-10-20 19:46:57 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2002-10-07 18:37:10 +00:00
|
|
|
//
|
|
|
|
// This file defines a simple interface that can be used to print out generic
|
|
|
|
// LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T
|
|
|
|
// graphviz package (http://www.research.att.com/sw/tools/graphviz/) which can
|
|
|
|
// be used to turn the files output by this interface into a variety of
|
|
|
|
// different graphics formats.
|
|
|
|
//
|
|
|
|
// Graphs do not need to implement any interface past what is already required
|
|
|
|
// by the GraphTraits template, but they can choose to implement specializations
|
|
|
|
// of the DOTGraphTraits template if they want to customize the graphs output in
|
|
|
|
// any way.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#ifndef LLVM_SUPPORT_GRAPHWRITER_H
|
|
|
|
#define LLVM_SUPPORT_GRAPHWRITER_H
|
2002-10-07 18:37:10 +00:00
|
|
|
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/Support/DOTGraphTraits.h"
|
2009-08-23 07:19:13 +00:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2004-09-01 22:55:40 +00:00
|
|
|
#include "llvm/ADT/GraphTraits.h"
|
2006-06-27 16:49:46 +00:00
|
|
|
#include "llvm/System/Path.h"
|
2006-11-17 09:52:49 +00:00
|
|
|
#include <vector>
|
2009-07-08 21:53:41 +00:00
|
|
|
#include <cassert>
|
2002-10-07 18:37:10 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
|
|
|
|
2002-10-07 18:37:10 +00:00
|
|
|
namespace DOT { // Private functions...
|
2009-08-23 07:19:13 +00:00
|
|
|
std::string EscapeString(const std::string &Label);
|
2002-10-07 18:37:10 +00:00
|
|
|
}
|
|
|
|
|
2009-07-09 17:06:18 +00:00
|
|
|
namespace GraphProgram {
|
|
|
|
enum Name {
|
|
|
|
DOT,
|
|
|
|
FDP,
|
|
|
|
NEATO,
|
|
|
|
TWOPI,
|
|
|
|
CIRCO
|
|
|
|
};
|
|
|
|
}
|
2009-09-20 04:03:25 +00:00
|
|
|
|
2009-07-09 17:06:18 +00:00
|
|
|
void DisplayGraph(const sys::Path& Filename, bool wait=true, GraphProgram::Name program = GraphProgram::DOT);
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2002-10-07 18:37:10 +00:00
|
|
|
template<typename GraphType>
|
2002-10-16 01:34:18 +00:00
|
|
|
class GraphWriter {
|
2009-08-23 07:19:13 +00:00
|
|
|
raw_ostream &O;
|
2002-10-16 01:34:18 +00:00
|
|
|
const GraphType &G;
|
2009-06-24 17:37:09 +00:00
|
|
|
bool ShortNames;
|
2002-10-07 18:37:10 +00:00
|
|
|
|
2002-10-16 01:34:18 +00:00
|
|
|
typedef DOTGraphTraits<GraphType> DOTTraits;
|
|
|
|
typedef GraphTraits<GraphType> GTraits;
|
|
|
|
typedef typename GTraits::NodeType NodeType;
|
|
|
|
typedef typename GTraits::nodes_iterator node_iterator;
|
|
|
|
typedef typename GTraits::ChildIteratorType child_iterator;
|
|
|
|
public:
|
2009-08-23 07:19:13 +00:00
|
|
|
GraphWriter(raw_ostream &o, const GraphType &g, bool SN) :
|
2009-06-24 17:37:09 +00:00
|
|
|
O(o), G(g), ShortNames(SN) {}
|
2002-10-17 00:59:59 +00:00
|
|
|
|
|
|
|
void writeHeader(const std::string &Name) {
|
2008-07-08 23:33:46 +00:00
|
|
|
std::string GraphName = DOTTraits::getGraphName(G);
|
|
|
|
|
|
|
|
if (!Name.empty())
|
2008-07-21 19:57:57 +00:00
|
|
|
O << "digraph \"" << DOT::EscapeString(Name) << "\" {\n";
|
2008-07-08 23:33:46 +00:00
|
|
|
else if (!GraphName.empty())
|
2008-07-10 19:55:54 +00:00
|
|
|
O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
|
2008-07-08 23:33:46 +00:00
|
|
|
else
|
|
|
|
O << "digraph unnamed {\n";
|
2002-10-07 18:37:10 +00:00
|
|
|
|
2005-01-11 00:24:59 +00:00
|
|
|
if (DOTTraits::renderGraphFromBottomUp())
|
|
|
|
O << "\trankdir=\"BT\";\n";
|
|
|
|
|
2008-07-21 19:57:57 +00:00
|
|
|
if (!Name.empty())
|
|
|
|
O << "\tlabel=\"" << DOT::EscapeString(Name) << "\";\n";
|
|
|
|
else if (!GraphName.empty())
|
2002-10-16 01:34:18 +00:00
|
|
|
O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
|
|
|
|
O << DOTTraits::getGraphProperties(G);
|
|
|
|
O << "\n";
|
|
|
|
}
|
|
|
|
|
2002-10-17 00:59:59 +00:00
|
|
|
void writeFooter() {
|
2002-10-16 01:34:18 +00:00
|
|
|
// Finish off the graph
|
|
|
|
O << "}\n";
|
|
|
|
}
|
2002-10-07 18:37:10 +00:00
|
|
|
|
2002-10-16 01:34:18 +00:00
|
|
|
void writeNodes() {
|
|
|
|
// Loop over the graph, printing it out...
|
|
|
|
for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
|
|
|
|
I != E; ++I)
|
2007-09-19 21:26:49 +00:00
|
|
|
writeNode(*I);
|
|
|
|
}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2007-09-19 21:26:49 +00:00
|
|
|
void writeNode(NodeType& Node) {
|
|
|
|
writeNode(&Node);
|
2002-10-16 01:34:18 +00:00
|
|
|
}
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2005-01-10 23:05:07 +00:00
|
|
|
void writeNode(NodeType *const *Node) {
|
|
|
|
writeNode(*Node);
|
|
|
|
}
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2002-10-16 01:34:18 +00:00
|
|
|
void writeNode(NodeType *Node) {
|
2006-10-02 12:26:53 +00:00
|
|
|
std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G);
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2008-07-27 23:12:19 +00:00
|
|
|
O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
|
2002-10-07 18:37:10 +00:00
|
|
|
if (!NodeAttributes.empty()) O << NodeAttributes << ",";
|
2005-01-11 00:24:59 +00:00
|
|
|
O << "label=\"{";
|
|
|
|
|
2005-10-01 00:19:21 +00:00
|
|
|
if (!DOTTraits::renderGraphFromBottomUp()) {
|
2009-06-24 17:37:09 +00:00
|
|
|
O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2005-10-01 00:19:21 +00:00
|
|
|
// If we should include the address of the node in the label, do so now.
|
|
|
|
if (DOTTraits::hasNodeAddressLabel(Node, G))
|
|
|
|
O << "|" << (void*)Node;
|
|
|
|
}
|
|
|
|
|
2002-10-07 18:37:10 +00:00
|
|
|
// Print out the fields of the current node...
|
2002-10-16 01:34:18 +00:00
|
|
|
child_iterator EI = GTraits::child_begin(Node);
|
|
|
|
child_iterator EE = GTraits::child_end(Node);
|
2002-10-07 18:37:10 +00:00
|
|
|
if (EI != EE) {
|
2005-01-11 00:24:59 +00:00
|
|
|
if (!DOTTraits::renderGraphFromBottomUp()) O << "|";
|
|
|
|
O << "{";
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2002-10-07 18:37:10 +00:00
|
|
|
for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
|
|
|
|
if (i) O << "|";
|
2008-07-21 21:06:55 +00:00
|
|
|
O << "<s" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI);
|
2002-10-07 18:37:10 +00:00
|
|
|
}
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2002-10-07 18:37:10 +00:00
|
|
|
if (EI != EE)
|
2008-07-21 21:06:55 +00:00
|
|
|
O << "|<s64>truncated...";
|
2002-10-07 18:37:10 +00:00
|
|
|
O << "}";
|
2005-01-11 00:24:59 +00:00
|
|
|
if (DOTTraits::renderGraphFromBottomUp()) O << "|";
|
2002-10-07 18:37:10 +00:00
|
|
|
}
|
2005-10-01 00:19:21 +00:00
|
|
|
|
|
|
|
if (DOTTraits::renderGraphFromBottomUp()) {
|
2009-06-24 17:37:09 +00:00
|
|
|
O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames));
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2005-10-01 00:19:21 +00:00
|
|
|
// If we should include the address of the node in the label, do so now.
|
|
|
|
if (DOTTraits::hasNodeAddressLabel(Node, G))
|
|
|
|
O << "|" << (void*)Node;
|
|
|
|
}
|
|
|
|
|
2008-07-21 21:06:55 +00:00
|
|
|
if (DOTTraits::hasEdgeDestLabels()) {
|
|
|
|
O << "|{";
|
|
|
|
|
|
|
|
unsigned i = 0, e = DOTTraits::numEdgeDestLabels(Node);
|
|
|
|
for (; i != e && i != 64; ++i) {
|
|
|
|
if (i) O << "|";
|
|
|
|
O << "<d" << i << ">" << DOTTraits::getEdgeDestLabel(Node, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (i != e)
|
|
|
|
O << "|<d64>truncated...";
|
|
|
|
O << "}";
|
|
|
|
}
|
|
|
|
|
2005-09-30 19:33:41 +00:00
|
|
|
O << "}\"];\n"; // Finish printing the "node" line
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2002-10-07 18:37:10 +00:00
|
|
|
// Output all of the edges now
|
|
|
|
EI = GTraits::child_begin(Node);
|
2002-10-16 01:34:18 +00:00
|
|
|
for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
|
|
|
|
writeEdge(Node, i, EI);
|
2004-02-11 20:44:17 +00:00
|
|
|
for (; EI != EE; ++EI)
|
|
|
|
writeEdge(Node, 64, EI);
|
2002-10-16 01:34:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
|
|
|
|
if (NodeType *TargetNode = *EI) {
|
2002-10-16 02:03:18 +00:00
|
|
|
int DestPort = -1;
|
2002-10-16 01:34:18 +00:00
|
|
|
if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) {
|
|
|
|
child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI);
|
|
|
|
|
|
|
|
// Figure out which edge this targets...
|
2008-05-05 18:30:58 +00:00
|
|
|
unsigned Offset =
|
|
|
|
(unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
|
2003-11-16 20:21:15 +00:00
|
|
|
DestPort = static_cast<int>(Offset);
|
2002-10-07 18:37:10 +00:00
|
|
|
}
|
2002-10-16 02:03:18 +00:00
|
|
|
|
2008-07-27 23:12:19 +00:00
|
|
|
emitEdge(static_cast<const void*>(Node), edgeidx,
|
|
|
|
static_cast<const void*>(TargetNode), DestPort,
|
2002-10-16 02:03:18 +00:00
|
|
|
DOTTraits::getEdgeAttributes(Node, EI));
|
2002-10-07 18:37:10 +00:00
|
|
|
}
|
|
|
|
}
|
2002-10-16 02:03:18 +00:00
|
|
|
|
|
|
|
/// emitSimpleNode - Outputs a simple (non-record) node
|
2002-10-16 20:15:38 +00:00
|
|
|
void emitSimpleNode(const void *ID, const std::string &Attr,
|
2003-02-05 19:40:59 +00:00
|
|
|
const std::string &Label, unsigned NumEdgeSources = 0,
|
|
|
|
const std::vector<std::string> *EdgeSourceLabels = 0) {
|
2002-10-16 02:03:18 +00:00
|
|
|
O << "\tNode" << ID << "[ ";
|
|
|
|
if (!Attr.empty())
|
|
|
|
O << Attr << ",";
|
2002-10-18 14:55:44 +00:00
|
|
|
O << " label =\"";
|
|
|
|
if (NumEdgeSources) O << "{";
|
|
|
|
O << DOT::EscapeString(Label);
|
2002-10-16 20:15:38 +00:00
|
|
|
if (NumEdgeSources) {
|
|
|
|
O << "|{";
|
2005-04-21 20:48:15 +00:00
|
|
|
|
2002-10-16 20:15:38 +00:00
|
|
|
for (unsigned i = 0; i != NumEdgeSources; ++i) {
|
|
|
|
if (i) O << "|";
|
2009-07-23 15:24:38 +00:00
|
|
|
O << "<s" << i << ">";
|
2003-02-05 19:40:59 +00:00
|
|
|
if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i];
|
2002-10-16 20:15:38 +00:00
|
|
|
}
|
2002-10-18 14:55:44 +00:00
|
|
|
O << "}}";
|
2002-10-16 20:15:38 +00:00
|
|
|
}
|
2002-10-18 14:55:44 +00:00
|
|
|
O << "\"];\n";
|
2002-10-16 02:03:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// emitEdge - Output an edge from a simple node into the graph...
|
2002-10-16 20:15:38 +00:00
|
|
|
void emitEdge(const void *SrcNodeID, int SrcNodePort,
|
|
|
|
const void *DestNodeID, int DestNodePort,
|
|
|
|
const std::string &Attrs) {
|
2002-11-10 06:47:03 +00:00
|
|
|
if (SrcNodePort > 64) return; // Eminating from truncated part?
|
|
|
|
if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part?
|
|
|
|
|
2002-10-16 02:03:18 +00:00
|
|
|
O << "\tNode" << SrcNodeID;
|
|
|
|
if (SrcNodePort >= 0)
|
2008-07-21 21:06:55 +00:00
|
|
|
O << ":s" << SrcNodePort;
|
2008-07-27 23:12:19 +00:00
|
|
|
O << " -> Node" << DestNodeID;
|
2009-08-05 15:04:22 +00:00
|
|
|
if (DestNodePort >= 0) {
|
|
|
|
if (DOTTraits::hasEdgeDestLabels())
|
2009-09-20 04:03:25 +00:00
|
|
|
O << ":d" << DestNodePort;
|
2009-08-05 15:04:22 +00:00
|
|
|
else
|
2009-09-20 04:03:25 +00:00
|
|
|
O << ":s" << DestNodePort;
|
2009-08-05 15:04:22 +00:00
|
|
|
}
|
2002-10-16 02:03:18 +00:00
|
|
|
|
|
|
|
if (!Attrs.empty())
|
|
|
|
O << "[" << Attrs << "]";
|
|
|
|
O << ";\n";
|
|
|
|
}
|
2002-10-16 01:34:18 +00:00
|
|
|
};
|
2002-10-07 18:37:10 +00:00
|
|
|
|
2002-10-16 01:34:18 +00:00
|
|
|
template<typename GraphType>
|
2009-08-23 07:19:13 +00:00
|
|
|
raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
|
|
|
|
bool ShortNames = false,
|
|
|
|
const std::string &Name = "",
|
|
|
|
const std::string &Title = "") {
|
2002-10-16 01:34:18 +00:00
|
|
|
// Start the graph emission process...
|
2009-06-24 17:37:09 +00:00
|
|
|
GraphWriter<GraphType> W(O, G, ShortNames);
|
2002-10-17 00:59:59 +00:00
|
|
|
|
|
|
|
// Output the header for the graph...
|
2008-07-21 19:57:57 +00:00
|
|
|
W.writeHeader(Title);
|
2002-10-17 00:59:59 +00:00
|
|
|
|
|
|
|
// Emit all of the nodes in the graph...
|
|
|
|
W.writeNodes();
|
|
|
|
|
|
|
|
// Output any customizations on the graph
|
|
|
|
DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W);
|
|
|
|
|
|
|
|
// Output the end of the graph
|
|
|
|
W.writeFooter();
|
2002-10-07 18:37:10 +00:00
|
|
|
return O;
|
|
|
|
}
|
|
|
|
|
2006-06-27 16:49:46 +00:00
|
|
|
template<typename GraphType>
|
2009-08-23 22:45:37 +00:00
|
|
|
sys::Path WriteGraph(const GraphType &G, const std::string &Name,
|
|
|
|
bool ShortNames = false, const std::string &Title = "") {
|
2006-08-22 19:01:30 +00:00
|
|
|
std::string ErrMsg;
|
|
|
|
sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg);
|
|
|
|
if (Filename.isEmpty()) {
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << "Error: " << ErrMsg << "\n";
|
2006-08-22 19:01:30 +00:00
|
|
|
return Filename;
|
|
|
|
}
|
2006-06-27 16:49:46 +00:00
|
|
|
Filename.appendComponent(Name + ".dot");
|
2006-08-23 20:34:57 +00:00
|
|
|
if (Filename.makeUnique(true,&ErrMsg)) {
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << "Error: " << ErrMsg << "\n";
|
2006-08-23 20:34:57 +00:00
|
|
|
return sys::Path();
|
|
|
|
}
|
|
|
|
|
2009-08-23 22:45:37 +00:00
|
|
|
errs() << "Writing '" << Filename.str() << "'... ";
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-08-23 07:19:13 +00:00
|
|
|
std::string ErrorInfo;
|
2009-08-25 15:34:52 +00:00
|
|
|
raw_fd_ostream O(Filename.c_str(), ErrorInfo);
|
2006-06-27 16:49:46 +00:00
|
|
|
|
2009-08-23 07:19:13 +00:00
|
|
|
if (ErrorInfo.empty()) {
|
2009-06-24 17:37:09 +00:00
|
|
|
WriteGraph(O, G, ShortNames, Name, Title);
|
2009-08-23 07:19:13 +00:00
|
|
|
errs() << " done. \n";
|
2006-06-27 16:49:46 +00:00
|
|
|
} else {
|
2009-08-23 22:45:37 +00:00
|
|
|
errs() << "error opening file '" << Filename.str() << "' for writing!\n";
|
2006-06-27 16:49:46 +00:00
|
|
|
Filename.clear();
|
|
|
|
}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2006-06-27 16:49:46 +00:00
|
|
|
return Filename;
|
|
|
|
}
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2006-06-27 16:49:46 +00:00
|
|
|
/// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
|
|
|
|
/// then cleanup. For use from the debugger.
|
|
|
|
///
|
|
|
|
template<typename GraphType>
|
2009-08-23 22:45:37 +00:00
|
|
|
void ViewGraph(const GraphType &G, const std::string &Name,
|
|
|
|
bool ShortNames = false, const std::string &Title = "",
|
2009-07-09 17:06:18 +00:00
|
|
|
GraphProgram::Name Program = GraphProgram::DOT) {
|
2009-08-23 22:45:37 +00:00
|
|
|
sys::Path Filename = WriteGraph(G, Name, ShortNames, Title);
|
2006-06-27 16:49:46 +00:00
|
|
|
|
2009-08-23 22:45:37 +00:00
|
|
|
if (Filename.isEmpty())
|
2006-06-27 16:49:46 +00:00
|
|
|
return;
|
2009-02-20 22:51:36 +00:00
|
|
|
|
2009-07-09 17:06:18 +00:00
|
|
|
DisplayGraph(Filename, true, Program);
|
2006-06-27 16:49:46 +00:00
|
|
|
}
|
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|
|
|
|
|
2002-10-07 18:37:10 +00:00
|
|
|
#endif
|