GraphWriter.h revision 65b660743ccce5393f70d3667ac4e2b0b76e5236
1//===-- llvm/Support/GraphWriter.h - Write graph to a .dot file -*- C++ -*-===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file defines a simple interface that can be used to print out generic 11// LLVM graphs to ".dot" files. "dot" is a tool that is part of the AT&T 12// graphviz package (http://www.research.att.com/sw/tools/graphviz/) which can 13// be used to turn the files output by this interface into a variety of 14// different graphics formats. 15// 16// Graphs do not need to implement any interface past what is already required 17// by the GraphTraits template, but they can choose to implement specializations 18// of the DOTGraphTraits template if they want to customize the graphs output in 19// any way. 20// 21//===----------------------------------------------------------------------===// 22 23#ifndef LLVM_SUPPORT_GRAPHWRITER_H 24#define LLVM_SUPPORT_GRAPHWRITER_H 25 26#include "llvm/Support/DOTGraphTraits.h" 27#include "llvm/Support/raw_ostream.h" 28#include "llvm/ADT/GraphTraits.h" 29#include "llvm/System/Path.h" 30#include <vector> 31#include <cassert> 32 33namespace llvm { 34 35namespace DOT { // Private functions... 36 std::string EscapeString(const std::string &Label); 37} 38 39namespace GraphProgram { 40 enum Name { 41 DOT, 42 FDP, 43 NEATO, 44 TWOPI, 45 CIRCO 46 }; 47} 48 49void DisplayGraph(const sys::Path& Filename, bool wait=true, GraphProgram::Name program = GraphProgram::DOT); 50 51template<typename GraphType> 52class GraphWriter { 53 raw_ostream &O; 54 const GraphType &G; 55 bool ShortNames; 56 57 typedef DOTGraphTraits<GraphType> DOTTraits; 58 typedef GraphTraits<GraphType> GTraits; 59 typedef typename GTraits::NodeType NodeType; 60 typedef typename GTraits::nodes_iterator node_iterator; 61 typedef typename GTraits::ChildIteratorType child_iterator; 62public: 63 GraphWriter(raw_ostream &o, const GraphType &g, bool SN) : 64 O(o), G(g), ShortNames(SN) {} 65 66 void writeHeader(const std::string &Name) { 67 std::string GraphName = DOTTraits::getGraphName(G); 68 69 if (!Name.empty()) 70 O << "digraph \"" << DOT::EscapeString(Name) << "\" {\n"; 71 else if (!GraphName.empty()) 72 O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n"; 73 else 74 O << "digraph unnamed {\n"; 75 76 if (DOTTraits::renderGraphFromBottomUp()) 77 O << "\trankdir=\"BT\";\n"; 78 79 if (!Name.empty()) 80 O << "\tlabel=\"" << DOT::EscapeString(Name) << "\";\n"; 81 else if (!GraphName.empty()) 82 O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n"; 83 O << DOTTraits::getGraphProperties(G); 84 O << "\n"; 85 } 86 87 void writeFooter() { 88 // Finish off the graph 89 O << "}\n"; 90 } 91 92 void writeNodes() { 93 // Loop over the graph, printing it out... 94 for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G); 95 I != E; ++I) 96 writeNode(*I); 97 } 98 99 void writeNode(NodeType& Node) { 100 writeNode(&Node); 101 } 102 103 void writeNode(NodeType *const *Node) { 104 writeNode(*Node); 105 } 106 107 void writeNode(NodeType *Node) { 108 std::string NodeAttributes = DOTTraits::getNodeAttributes(Node, G); 109 110 O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,"; 111 if (!NodeAttributes.empty()) O << NodeAttributes << ","; 112 O << "label=\"{"; 113 114 if (!DOTTraits::renderGraphFromBottomUp()) { 115 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames)); 116 117 // If we should include the address of the node in the label, do so now. 118 if (DOTTraits::hasNodeAddressLabel(Node, G)) 119 O << "|" << (void*)Node; 120 } 121 122 // Print out the fields of the current node... 123 child_iterator EI = GTraits::child_begin(Node); 124 child_iterator EE = GTraits::child_end(Node); 125 if (EI != EE) { 126 if (!DOTTraits::renderGraphFromBottomUp()) O << "|"; 127 O << "{"; 128 129 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) { 130 if (i) O << "|"; 131 O << "<s" << i << ">" << DOTTraits::getEdgeSourceLabel(Node, EI); 132 } 133 134 if (EI != EE) 135 O << "|<s64>truncated..."; 136 O << "}"; 137 if (DOTTraits::renderGraphFromBottomUp()) O << "|"; 138 } 139 140 if (DOTTraits::renderGraphFromBottomUp()) { 141 O << DOT::EscapeString(DOTTraits::getNodeLabel(Node, G, ShortNames)); 142 143 // If we should include the address of the node in the label, do so now. 144 if (DOTTraits::hasNodeAddressLabel(Node, G)) 145 O << "|" << (void*)Node; 146 } 147 148 if (DOTTraits::hasEdgeDestLabels()) { 149 O << "|{"; 150 151 unsigned i = 0, e = DOTTraits::numEdgeDestLabels(Node); 152 for (; i != e && i != 64; ++i) { 153 if (i) O << "|"; 154 O << "<d" << i << ">" << DOTTraits::getEdgeDestLabel(Node, i); 155 } 156 157 if (i != e) 158 O << "|<d64>truncated..."; 159 O << "}"; 160 } 161 162 O << "}\"];\n"; // Finish printing the "node" line 163 164 // Output all of the edges now 165 EI = GTraits::child_begin(Node); 166 for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) 167 writeEdge(Node, i, EI); 168 for (; EI != EE; ++EI) 169 writeEdge(Node, 64, EI); 170 } 171 172 void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) { 173 if (NodeType *TargetNode = *EI) { 174 int DestPort = -1; 175 if (DOTTraits::edgeTargetsEdgeSource(Node, EI)) { 176 child_iterator TargetIt = DOTTraits::getEdgeTarget(Node, EI); 177 178 // Figure out which edge this targets... 179 unsigned Offset = 180 (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt); 181 DestPort = static_cast<int>(Offset); 182 } 183 184 emitEdge(static_cast<const void*>(Node), edgeidx, 185 static_cast<const void*>(TargetNode), DestPort, 186 DOTTraits::getEdgeAttributes(Node, EI)); 187 } 188 } 189 190 /// emitSimpleNode - Outputs a simple (non-record) node 191 void emitSimpleNode(const void *ID, const std::string &Attr, 192 const std::string &Label, unsigned NumEdgeSources = 0, 193 const std::vector<std::string> *EdgeSourceLabels = 0) { 194 O << "\tNode" << ID << "[ "; 195 if (!Attr.empty()) 196 O << Attr << ","; 197 O << " label =\""; 198 if (NumEdgeSources) O << "{"; 199 O << DOT::EscapeString(Label); 200 if (NumEdgeSources) { 201 O << "|{"; 202 203 for (unsigned i = 0; i != NumEdgeSources; ++i) { 204 if (i) O << "|"; 205 O << "<s" << i << ">"; 206 if (EdgeSourceLabels) O << (*EdgeSourceLabels)[i]; 207 } 208 O << "}}"; 209 } 210 O << "\"];\n"; 211 } 212 213 /// emitEdge - Output an edge from a simple node into the graph... 214 void emitEdge(const void *SrcNodeID, int SrcNodePort, 215 const void *DestNodeID, int DestNodePort, 216 const std::string &Attrs) { 217 if (SrcNodePort > 64) return; // Eminating from truncated part? 218 if (DestNodePort > 64) DestNodePort = 64; // Targetting the truncated part? 219 220 O << "\tNode" << SrcNodeID; 221 if (SrcNodePort >= 0) 222 O << ":s" << SrcNodePort; 223 O << " -> Node" << DestNodeID; 224 if (DestNodePort >= 0) { 225 if (DOTTraits::hasEdgeDestLabels()) 226 O << ":d" << DestNodePort; 227 else 228 O << ":s" << DestNodePort; 229 } 230 231 if (!Attrs.empty()) 232 O << "[" << Attrs << "]"; 233 O << ";\n"; 234 } 235}; 236 237template<typename GraphType> 238raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G, 239 bool ShortNames = false, 240 const std::string &Name = "", 241 const std::string &Title = "") { 242 // Start the graph emission process... 243 GraphWriter<GraphType> W(O, G, ShortNames); 244 245 // Output the header for the graph... 246 W.writeHeader(Title); 247 248 // Emit all of the nodes in the graph... 249 W.writeNodes(); 250 251 // Output any customizations on the graph 252 DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, W); 253 254 // Output the end of the graph 255 W.writeFooter(); 256 return O; 257} 258 259template<typename GraphType> 260sys::Path WriteGraph(const GraphType &G, const std::string &Name, 261 bool ShortNames = false, const std::string &Title = "") { 262 std::string ErrMsg; 263 sys::Path Filename = sys::Path::GetTemporaryDirectory(&ErrMsg); 264 if (Filename.isEmpty()) { 265 errs() << "Error: " << ErrMsg << "\n"; 266 return Filename; 267 } 268 Filename.appendComponent(Name + ".dot"); 269 if (Filename.makeUnique(true,&ErrMsg)) { 270 errs() << "Error: " << ErrMsg << "\n"; 271 return sys::Path(); 272 } 273 274 errs() << "Writing '" << Filename.str() << "'... "; 275 276 std::string ErrorInfo; 277 raw_fd_ostream O(Filename.c_str(), ErrorInfo); 278 279 if (ErrorInfo.empty()) { 280 WriteGraph(O, G, ShortNames, Name, Title); 281 errs() << " done. \n"; 282 } else { 283 errs() << "error opening file '" << Filename.str() << "' for writing!\n"; 284 Filename.clear(); 285 } 286 287 return Filename; 288} 289 290/// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file, 291/// then cleanup. For use from the debugger. 292/// 293template<typename GraphType> 294void ViewGraph(const GraphType &G, const std::string &Name, 295 bool ShortNames = false, const std::string &Title = "", 296 GraphProgram::Name Program = GraphProgram::DOT) { 297 sys::Path Filename = WriteGraph(G, Name, ShortNames, Title); 298 299 if (Filename.isEmpty()) 300 return; 301 302 DisplayGraph(Filename, true, Program); 303} 304 305} // End llvm namespace 306 307#endif 308