SelectionDAGPrinter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This implements the SelectionDAG::viewGraph method.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "ScheduleDAGSDNodes.h"
  13. #include "llvm/ADT/DenseSet.h"
  14. #include "llvm/ADT/StringExtras.h"
  15. #include "llvm/CodeGen/MachineFunction.h"
  16. #include "llvm/CodeGen/SelectionDAG.h"
  17. #include "llvm/Support/Debug.h"
  18. #include "llvm/Support/GraphWriter.h"
  19. #include "llvm/Support/raw_ostream.h"
  20. using namespace llvm;
  21. #define DEBUG_TYPE "dag-printer"
  22. namespace llvm {
  23. template<>
  24. struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
  25. explicit DOTGraphTraits(bool isSimple=false) :
  26. DefaultDOTGraphTraits(isSimple) {}
  27. static bool hasEdgeDestLabels() {
  28. return true;
  29. }
  30. static unsigned numEdgeDestLabels(const void *Node) {
  31. return ((const SDNode *) Node)->getNumValues();
  32. }
  33. static std::string getEdgeDestLabel(const void *Node, unsigned i) {
  34. return ((const SDNode *) Node)->getValueType(i).getEVTString();
  35. }
  36. template<typename EdgeIter>
  37. static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
  38. return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
  39. }
  40. /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
  41. /// should actually target another edge source, not a node. If this method
  42. /// is implemented, getEdgeTarget should be implemented.
  43. template<typename EdgeIter>
  44. static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
  45. return true;
  46. }
  47. /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
  48. /// called to determine which outgoing edge of Node is the target of this
  49. /// edge.
  50. template<typename EdgeIter>
  51. static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
  52. SDNode *TargetNode = *I;
  53. SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
  54. std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
  55. return NI;
  56. }
  57. static std::string getGraphName(const SelectionDAG *G) {
  58. return std::string(G->getMachineFunction().getName());
  59. }
  60. static bool renderGraphFromBottomUp() {
  61. return true;
  62. }
  63. static std::string getNodeIdentifierLabel(const SDNode *Node,
  64. const SelectionDAG *Graph) {
  65. std::string R;
  66. raw_string_ostream OS(R);
  67. #ifndef NDEBUG
  68. OS << 't' << Node->PersistentId;
  69. #else
  70. OS << static_cast<const void *>(Node);
  71. #endif
  72. return R;
  73. }
  74. /// If you want to override the dot attributes printed for a particular
  75. /// edge, override this method.
  76. template<typename EdgeIter>
  77. static std::string getEdgeAttributes(const void *Node, EdgeIter EI,
  78. const SelectionDAG *Graph) {
  79. SDValue Op = EI.getNode()->getOperand(EI.getOperand());
  80. EVT VT = Op.getValueType();
  81. if (VT == MVT::Glue)
  82. return "color=red,style=bold";
  83. else if (VT == MVT::Other)
  84. return "color=blue,style=dashed";
  85. return "";
  86. }
  87. static std::string getSimpleNodeLabel(const SDNode *Node,
  88. const SelectionDAG *G) {
  89. std::string Result = Node->getOperationName(G);
  90. {
  91. raw_string_ostream OS(Result);
  92. Node->print_details(OS, G);
  93. }
  94. return Result;
  95. }
  96. std::string getNodeLabel(const SDNode *Node, const SelectionDAG *Graph);
  97. static std::string getNodeAttributes(const SDNode *N,
  98. const SelectionDAG *Graph) {
  99. #ifndef NDEBUG
  100. const std::string &Attrs = Graph->getGraphAttrs(N);
  101. if (!Attrs.empty()) {
  102. if (Attrs.find("shape=") == std::string::npos)
  103. return std::string("shape=Mrecord,") + Attrs;
  104. else
  105. return Attrs;
  106. }
  107. #endif
  108. return "shape=Mrecord";
  109. }
  110. static void addCustomGraphFeatures(SelectionDAG *G,
  111. GraphWriter<SelectionDAG*> &GW) {
  112. GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
  113. if (G->getRoot().getNode())
  114. GW.emitEdge(nullptr, -1, G->getRoot().getNode(), G->getRoot().getResNo(),
  115. "color=blue,style=dashed");
  116. }
  117. };
  118. }
  119. std::string DOTGraphTraits<SelectionDAG*>::getNodeLabel(const SDNode *Node,
  120. const SelectionDAG *G) {
  121. return DOTGraphTraits<SelectionDAG*>::getSimpleNodeLabel(Node, G);
  122. }
  123. /// viewGraph - Pop up a ghostview window with the reachable parts of the DAG
  124. /// rendered using 'dot'.
  125. ///
  126. void SelectionDAG::viewGraph(const std::string &Title) {
  127. // This code is only for debugging!
  128. #ifndef NDEBUG
  129. ViewGraph(this, "dag." + getMachineFunction().getName(),
  130. false, Title);
  131. #else
  132. errs() << "SelectionDAG::viewGraph is only available in debug builds on "
  133. << "systems with Graphviz or gv!\n";
  134. #endif // NDEBUG
  135. }
  136. // This overload is defined out-of-line here instead of just using a
  137. // default parameter because this is easiest for gdb to call.
  138. void SelectionDAG::viewGraph() {
  139. viewGraph("");
  140. }
  141. /// Just dump dot graph to a user-provided path and title.
  142. /// This doesn't open the dot viewer program and
  143. /// helps visualization when outside debugging session.
  144. /// FileName expects absolute path. If provided
  145. /// without any path separators then the file
  146. /// will be created in the current directory.
  147. /// Error will be emitted if the path is insane.
  148. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  149. LLVM_DUMP_METHOD void SelectionDAG::dumpDotGraph(const Twine &FileName,
  150. const Twine &Title) {
  151. dumpDotGraphToFile(this, FileName, Title);
  152. }
  153. #endif
  154. /// clearGraphAttrs - Clear all previously defined node graph attributes.
  155. /// Intended to be used from a debugging tool (eg. gdb).
  156. void SelectionDAG::clearGraphAttrs() {
  157. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  158. NodeGraphAttrs.clear();
  159. #else
  160. errs() << "SelectionDAG::clearGraphAttrs is only available in builds with "
  161. << "ABI breaking checks enabled on systems with Graphviz or gv!\n";
  162. #endif
  163. }
  164. /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
  165. ///
  166. void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
  167. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  168. NodeGraphAttrs[N] = Attrs;
  169. #else
  170. errs() << "SelectionDAG::setGraphAttrs is only available in builds with "
  171. << "ABI breaking checks enabled on systems with Graphviz or gv!\n";
  172. #endif
  173. }
  174. /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
  175. /// Used from getNodeAttributes.
  176. std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
  177. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  178. std::map<const SDNode *, std::string>::const_iterator I =
  179. NodeGraphAttrs.find(N);
  180. if (I != NodeGraphAttrs.end())
  181. return I->second;
  182. else
  183. return "";
  184. #else
  185. errs() << "SelectionDAG::getGraphAttrs is only available in builds with "
  186. << "ABI breaking checks enabled on systems with Graphviz or gv!\n";
  187. return std::string();
  188. #endif
  189. }
  190. /// setGraphColor - Convenience for setting node color attribute.
  191. ///
  192. void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
  193. #if LLVM_ENABLE_ABI_BREAKING_CHECKS
  194. NodeGraphAttrs[N] = std::string("color=") + Color;
  195. #else
  196. errs() << "SelectionDAG::setGraphColor is only available in builds with "
  197. << "ABI breaking checks enabled on systems with Graphviz or gv!\n";
  198. #endif
  199. }
  200. /// setSubgraphColorHelper - Implement setSubgraphColor. Return
  201. /// whether we truncated the search.
  202. ///
  203. bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
  204. int level, bool &printed) {
  205. bool hit_limit = false;
  206. #ifndef NDEBUG
  207. if (level >= 20) {
  208. if (!printed) {
  209. printed = true;
  210. LLVM_DEBUG(dbgs() << "setSubgraphColor hit max level\n");
  211. }
  212. return true;
  213. }
  214. unsigned oldSize = visited.size();
  215. visited.insert(N);
  216. if (visited.size() != oldSize) {
  217. setGraphColor(N, Color);
  218. for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
  219. i != iend;
  220. ++i) {
  221. hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
  222. }
  223. }
  224. #else
  225. errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
  226. << " on systems with Graphviz or gv!\n";
  227. #endif
  228. return hit_limit;
  229. }
  230. /// setSubgraphColor - Convenience for setting subgraph color attribute.
  231. ///
  232. void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
  233. #ifndef NDEBUG
  234. DenseSet<SDNode *> visited;
  235. bool printed = false;
  236. if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
  237. // Visually mark that we hit the limit
  238. if (strcmp(Color, "red") == 0) {
  239. setSubgraphColorHelper(N, "blue", visited, 0, printed);
  240. } else if (strcmp(Color, "yellow") == 0) {
  241. setSubgraphColorHelper(N, "green", visited, 0, printed);
  242. }
  243. }
  244. #else
  245. errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
  246. << " on systems with Graphviz or gv!\n";
  247. #endif
  248. }
  249. std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
  250. std::string s;
  251. raw_string_ostream O(s);
  252. O << "SU(" << SU->NodeNum << "): ";
  253. if (SU->getNode()) {
  254. SmallVector<SDNode *, 4> GluedNodes;
  255. for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
  256. GluedNodes.push_back(N);
  257. while (!GluedNodes.empty()) {
  258. O << DOTGraphTraits<SelectionDAG*>
  259. ::getSimpleNodeLabel(GluedNodes.back(), DAG);
  260. GluedNodes.pop_back();
  261. if (!GluedNodes.empty())
  262. O << "\n ";
  263. }
  264. } else {
  265. O << "CROSS RC COPY";
  266. }
  267. return O.str();
  268. }
  269. void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
  270. if (DAG) {
  271. // Draw a special "GraphRoot" node to indicate the root of the graph.
  272. GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
  273. const SDNode *N = DAG->getRoot().getNode();
  274. if (N && N->getNodeId() != -1)
  275. GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
  276. "color=blue,style=dashed");
  277. }
  278. }