SelectionDAGPrinter.cpp 9.7 KB

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