/3rd_party/llvm/include/llvm/Support/GraphWriter.h

https://code.google.com/p/softart/ · C++ Header · 359 lines · 242 code · 73 blank · 44 comment · 64 complexity · a10aa1c54397d2a60c0079c3cd7fa85c MD5 · raw file

  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. #ifndef LLVM_SUPPORT_GRAPHWRITER_H
  23. #define LLVM_SUPPORT_GRAPHWRITER_H
  24. #include "llvm/ADT/GraphTraits.h"
  25. #include "llvm/Support/DOTGraphTraits.h"
  26. #include "llvm/Support/Path.h"
  27. #include "llvm/Support/raw_ostream.h"
  28. #include <cassert>
  29. #include <vector>
  30. namespace llvm {
  31. namespace DOT { // Private functions...
  32. std::string EscapeString(const std::string &Label);
  33. /// \brief Get a color string for this node number. Simply round-robin selects
  34. /// from a reasonable number of colors.
  35. StringRef getColorString(unsigned NodeNumber);
  36. }
  37. namespace GraphProgram {
  38. enum Name {
  39. DOT,
  40. FDP,
  41. NEATO,
  42. TWOPI,
  43. CIRCO
  44. };
  45. }
  46. void DisplayGraph(StringRef Filename, bool wait = true,
  47. GraphProgram::Name program = GraphProgram::DOT);
  48. template<typename GraphType>
  49. class GraphWriter {
  50. raw_ostream &O;
  51. const GraphType &G;
  52. typedef DOTGraphTraits<GraphType> DOTTraits;
  53. typedef GraphTraits<GraphType> GTraits;
  54. typedef typename GTraits::NodeType NodeType;
  55. typedef typename GTraits::nodes_iterator node_iterator;
  56. typedef typename GTraits::ChildIteratorType child_iterator;
  57. DOTTraits DTraits;
  58. // Writes the edge labels of the node to O and returns true if there are any
  59. // edge labels not equal to the empty string "".
  60. bool getEdgeSourceLabels(raw_ostream &O, NodeType *Node) {
  61. child_iterator EI = GTraits::child_begin(Node);
  62. child_iterator EE = GTraits::child_end(Node);
  63. bool hasEdgeSourceLabels = false;
  64. for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i) {
  65. std::string label = DTraits.getEdgeSourceLabel(Node, EI);
  66. if (label.empty())
  67. continue;
  68. hasEdgeSourceLabels = true;
  69. if (i)
  70. O << "|";
  71. O << "<s" << i << ">" << DOT::EscapeString(label);
  72. }
  73. if (EI != EE && hasEdgeSourceLabels)
  74. O << "|<s64>truncated...";
  75. return hasEdgeSourceLabels;
  76. }
  77. public:
  78. GraphWriter(raw_ostream &o, const GraphType &g, bool SN) : O(o), G(g) {
  79. DTraits = DOTTraits(SN);
  80. }
  81. void writeGraph(const std::string &Title = "") {
  82. // Output the header for the graph...
  83. writeHeader(Title);
  84. // Emit all of the nodes in the graph...
  85. writeNodes();
  86. // Output any customizations on the graph
  87. DOTGraphTraits<GraphType>::addCustomGraphFeatures(G, *this);
  88. // Output the end of the graph
  89. writeFooter();
  90. }
  91. void writeHeader(const std::string &Title) {
  92. std::string GraphName = DTraits.getGraphName(G);
  93. if (!Title.empty())
  94. O << "digraph \"" << DOT::EscapeString(Title) << "\" {\n";
  95. else if (!GraphName.empty())
  96. O << "digraph \"" << DOT::EscapeString(GraphName) << "\" {\n";
  97. else
  98. O << "digraph unnamed {\n";
  99. if (DTraits.renderGraphFromBottomUp())
  100. O << "\trankdir=\"BT\";\n";
  101. if (!Title.empty())
  102. O << "\tlabel=\"" << DOT::EscapeString(Title) << "\";\n";
  103. else if (!GraphName.empty())
  104. O << "\tlabel=\"" << DOT::EscapeString(GraphName) << "\";\n";
  105. O << DTraits.getGraphProperties(G);
  106. O << "\n";
  107. }
  108. void writeFooter() {
  109. // Finish off the graph
  110. O << "}\n";
  111. }
  112. void writeNodes() {
  113. // Loop over the graph, printing it out...
  114. for (node_iterator I = GTraits::nodes_begin(G), E = GTraits::nodes_end(G);
  115. I != E; ++I)
  116. if (!isNodeHidden(*I))
  117. writeNode(*I);
  118. }
  119. bool isNodeHidden(NodeType &Node) {
  120. return isNodeHidden(&Node);
  121. }
  122. bool isNodeHidden(NodeType *const *Node) {
  123. return isNodeHidden(*Node);
  124. }
  125. bool isNodeHidden(NodeType *Node) {
  126. return DTraits.isNodeHidden(Node);
  127. }
  128. void writeNode(NodeType& Node) {
  129. writeNode(&Node);
  130. }
  131. void writeNode(NodeType *const *Node) {
  132. writeNode(*Node);
  133. }
  134. void writeNode(NodeType *Node) {
  135. std::string NodeAttributes = DTraits.getNodeAttributes(Node, G);
  136. O << "\tNode" << static_cast<const void*>(Node) << " [shape=record,";
  137. if (!NodeAttributes.empty()) O << NodeAttributes << ",";
  138. O << "label=\"{";
  139. if (!DTraits.renderGraphFromBottomUp()) {
  140. O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
  141. // If we should include the address of the node in the label, do so now.
  142. if (DTraits.hasNodeAddressLabel(Node, G))
  143. O << "|" << static_cast<const void*>(Node);
  144. std::string NodeDesc = DTraits.getNodeDescription(Node, G);
  145. if (!NodeDesc.empty())
  146. O << "|" << DOT::EscapeString(NodeDesc);
  147. }
  148. std::string edgeSourceLabels;
  149. raw_string_ostream EdgeSourceLabels(edgeSourceLabels);
  150. bool hasEdgeSourceLabels = getEdgeSourceLabels(EdgeSourceLabels, Node);
  151. if (hasEdgeSourceLabels) {
  152. if (!DTraits.renderGraphFromBottomUp()) O << "|";
  153. O << "{" << EdgeSourceLabels.str() << "}";
  154. if (DTraits.renderGraphFromBottomUp()) O << "|";
  155. }
  156. if (DTraits.renderGraphFromBottomUp()) {
  157. O << DOT::EscapeString(DTraits.getNodeLabel(Node, G));
  158. // If we should include the address of the node in the label, do so now.
  159. if (DTraits.hasNodeAddressLabel(Node, G))
  160. O << "|" << static_cast<const void*>(Node);
  161. std::string NodeDesc = DTraits.getNodeDescription(Node, G);
  162. if (!NodeDesc.empty())
  163. O << "|" << DOT::EscapeString(NodeDesc);
  164. }
  165. if (DTraits.hasEdgeDestLabels()) {
  166. O << "|{";
  167. unsigned i = 0, e = DTraits.numEdgeDestLabels(Node);
  168. for (; i != e && i != 64; ++i) {
  169. if (i) O << "|";
  170. O << "<d" << i << ">"
  171. << DOT::EscapeString(DTraits.getEdgeDestLabel(Node, i));
  172. }
  173. if (i != e)
  174. O << "|<d64>truncated...";
  175. O << "}";
  176. }
  177. O << "}\"];\n"; // Finish printing the "node" line
  178. // Output all of the edges now
  179. child_iterator EI = GTraits::child_begin(Node);
  180. child_iterator EE = GTraits::child_end(Node);
  181. for (unsigned i = 0; EI != EE && i != 64; ++EI, ++i)
  182. if (!DTraits.isNodeHidden(*EI))
  183. writeEdge(Node, i, EI);
  184. for (; EI != EE; ++EI)
  185. if (!DTraits.isNodeHidden(*EI))
  186. writeEdge(Node, 64, EI);
  187. }
  188. void writeEdge(NodeType *Node, unsigned edgeidx, child_iterator EI) {
  189. if (NodeType *TargetNode = *EI) {
  190. int DestPort = -1;
  191. if (DTraits.edgeTargetsEdgeSource(Node, EI)) {
  192. child_iterator TargetIt = DTraits.getEdgeTarget(Node, EI);
  193. // Figure out which edge this targets...
  194. unsigned Offset =
  195. (unsigned)std::distance(GTraits::child_begin(TargetNode), TargetIt);
  196. DestPort = static_cast<int>(Offset);
  197. }
  198. if (DTraits.getEdgeSourceLabel(Node, EI).empty())
  199. edgeidx = -1;
  200. emitEdge(static_cast<const void*>(Node), edgeidx,
  201. static_cast<const void*>(TargetNode), DestPort,
  202. DTraits.getEdgeAttributes(Node, EI, G));
  203. }
  204. }
  205. /// emitSimpleNode - Outputs a simple (non-record) node
  206. void emitSimpleNode(const void *ID, const std::string &Attr,
  207. const std::string &Label, unsigned NumEdgeSources = 0,
  208. const std::vector<std::string> *EdgeSourceLabels = 0) {
  209. O << "\tNode" << ID << "[ ";
  210. if (!Attr.empty())
  211. O << Attr << ",";
  212. O << " label =\"";
  213. if (NumEdgeSources) O << "{";
  214. O << DOT::EscapeString(Label);
  215. if (NumEdgeSources) {
  216. O << "|{";
  217. for (unsigned i = 0; i != NumEdgeSources; ++i) {
  218. if (i) O << "|";
  219. O << "<s" << i << ">";
  220. if (EdgeSourceLabels) O << DOT::EscapeString((*EdgeSourceLabels)[i]);
  221. }
  222. O << "}}";
  223. }
  224. O << "\"];\n";
  225. }
  226. /// emitEdge - Output an edge from a simple node into the graph...
  227. void emitEdge(const void *SrcNodeID, int SrcNodePort,
  228. const void *DestNodeID, int DestNodePort,
  229. const std::string &Attrs) {
  230. if (SrcNodePort > 64) return; // Eminating from truncated part?
  231. if (DestNodePort > 64) DestNodePort = 64; // Targeting the truncated part?
  232. O << "\tNode" << SrcNodeID;
  233. if (SrcNodePort >= 0)
  234. O << ":s" << SrcNodePort;
  235. O << " -> Node" << DestNodeID;
  236. if (DestNodePort >= 0 && DTraits.hasEdgeDestLabels())
  237. O << ":d" << DestNodePort;
  238. if (!Attrs.empty())
  239. O << "[" << Attrs << "]";
  240. O << ";\n";
  241. }
  242. /// getOStream - Get the raw output stream into the graph file. Useful to
  243. /// write fancy things using addCustomGraphFeatures().
  244. raw_ostream &getOStream() {
  245. return O;
  246. }
  247. };
  248. template<typename GraphType>
  249. raw_ostream &WriteGraph(raw_ostream &O, const GraphType &G,
  250. bool ShortNames = false,
  251. const Twine &Title = "") {
  252. // Start the graph emission process...
  253. GraphWriter<GraphType> W(O, G, ShortNames);
  254. // Emit the graph.
  255. W.writeGraph(Title.str());
  256. return O;
  257. }
  258. std::string createGraphFilename(const Twine &Name, int &FD);
  259. template <typename GraphType>
  260. std::string WriteGraph(const GraphType &G, const Twine &Name,
  261. bool ShortNames = false, const Twine &Title = "") {
  262. int FD;
  263. std::string Filename = createGraphFilename(Name, FD);
  264. raw_fd_ostream O(FD, /*shouldClose=*/ true);
  265. if (FD == -1) {
  266. errs() << "error opening file '" << Filename << "' for writing!\n";
  267. return "";
  268. }
  269. llvm::WriteGraph(O, G, ShortNames, Title);
  270. errs() << " done. \n";
  271. return Filename;
  272. }
  273. /// ViewGraph - Emit a dot graph, run 'dot', run gv on the postscript file,
  274. /// then cleanup. For use from the debugger.
  275. ///
  276. template<typename GraphType>
  277. void ViewGraph(const GraphType &G, const Twine &Name,
  278. bool ShortNames = false, const Twine &Title = "",
  279. GraphProgram::Name Program = GraphProgram::DOT) {
  280. std::string Filename = llvm::WriteGraph(G, Name, ShortNames, Title);
  281. if (Filename.empty())
  282. return;
  283. DisplayGraph(Filename, true, Program);
  284. }
  285. } // End llvm namespace
  286. #endif