SelectionDAGPrinter.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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 std::string(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. /// Just dump dot graph to a user-provided path and title.
  146. /// This doesn't open the dot viewer program and
  147. /// helps visualization when outside debugging session.
  148. /// FileName expects absolute path. If provided
  149. /// without any path separators then the file
  150. /// will be created in the current directory.
  151. /// Error will be emitted if the path is insane.
  152. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  153. LLVM_DUMP_METHOD void SelectionDAG::dumpDotGraph(const Twine &FileName,
  154. const Twine &Title) {
  155. dumpDotGraphToFile(this, FileName, Title);
  156. }
  157. #endif
  158. /// clearGraphAttrs - Clear all previously defined node graph attributes.
  159. /// Intended to be used from a debugging tool (eg. gdb).
  160. void SelectionDAG::clearGraphAttrs() {
  161. #ifndef NDEBUG
  162. NodeGraphAttrs.clear();
  163. #else
  164. errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
  165. << " on systems with Graphviz or gv!\n";
  166. #endif
  167. }
  168. /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
  169. ///
  170. void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
  171. #ifndef NDEBUG
  172. NodeGraphAttrs[N] = Attrs;
  173. #else
  174. errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
  175. << " on systems with Graphviz or gv!\n";
  176. #endif
  177. }
  178. /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
  179. /// Used from getNodeAttributes.
  180. std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
  181. #ifndef NDEBUG
  182. std::map<const SDNode *, std::string>::const_iterator I =
  183. NodeGraphAttrs.find(N);
  184. if (I != NodeGraphAttrs.end())
  185. return I->second;
  186. else
  187. return "";
  188. #else
  189. errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
  190. << " on systems with Graphviz or gv!\n";
  191. return std::string();
  192. #endif
  193. }
  194. /// setGraphColor - Convenience for setting node color attribute.
  195. ///
  196. void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
  197. #ifndef NDEBUG
  198. NodeGraphAttrs[N] = std::string("color=") + Color;
  199. #else
  200. errs() << "SelectionDAG::setGraphColor is only available in debug builds"
  201. << " on systems with Graphviz or gv!\n";
  202. #endif
  203. }
  204. /// setSubgraphColorHelper - Implement setSubgraphColor. Return
  205. /// whether we truncated the search.
  206. ///
  207. bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
  208. int level, bool &printed) {
  209. bool hit_limit = false;
  210. #ifndef NDEBUG
  211. if (level >= 20) {
  212. if (!printed) {
  213. printed = true;
  214. LLVM_DEBUG(dbgs() << "setSubgraphColor hit max level\n");
  215. }
  216. return true;
  217. }
  218. unsigned oldSize = visited.size();
  219. visited.insert(N);
  220. if (visited.size() != oldSize) {
  221. setGraphColor(N, Color);
  222. for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
  223. i != iend;
  224. ++i) {
  225. hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
  226. }
  227. }
  228. #else
  229. errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
  230. << " on systems with Graphviz or gv!\n";
  231. #endif
  232. return hit_limit;
  233. }
  234. /// setSubgraphColor - Convenience for setting subgraph color attribute.
  235. ///
  236. void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
  237. #ifndef NDEBUG
  238. DenseSet<SDNode *> visited;
  239. bool printed = false;
  240. if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
  241. // Visually mark that we hit the limit
  242. if (strcmp(Color, "red") == 0) {
  243. setSubgraphColorHelper(N, "blue", visited, 0, printed);
  244. } else if (strcmp(Color, "yellow") == 0) {
  245. setSubgraphColorHelper(N, "green", visited, 0, printed);
  246. }
  247. }
  248. #else
  249. errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
  250. << " on systems with Graphviz or gv!\n";
  251. #endif
  252. }
  253. std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
  254. std::string s;
  255. raw_string_ostream O(s);
  256. O << "SU(" << SU->NodeNum << "): ";
  257. if (SU->getNode()) {
  258. SmallVector<SDNode *, 4> GluedNodes;
  259. for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
  260. GluedNodes.push_back(N);
  261. while (!GluedNodes.empty()) {
  262. O << DOTGraphTraits<SelectionDAG*>
  263. ::getSimpleNodeLabel(GluedNodes.back(), DAG);
  264. GluedNodes.pop_back();
  265. if (!GluedNodes.empty())
  266. O << "\n ";
  267. }
  268. } else {
  269. O << "CROSS RC COPY";
  270. }
  271. return O.str();
  272. }
  273. void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
  274. if (DAG) {
  275. // Draw a special "GraphRoot" node to indicate the root of the graph.
  276. GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
  277. const SDNode *N = DAG->getRoot().getNode();
  278. if (N && N->getNodeId() != -1)
  279. GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
  280. "color=blue,style=dashed");
  281. }
  282. }