SelectionDAGPrinter.cpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. //===-- SelectionDAGPrinter.cpp - Implement SelectionDAG::viewGraph() -----===//
  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 implements the SelectionDAG::viewGraph method.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/CodeGen/SelectionDAG.h"
  14. #include "ScheduleDAGSDNodes.h"
  15. #include "llvm/ADT/DenseSet.h"
  16. #include "llvm/ADT/StringExtras.h"
  17. #include "llvm/CodeGen/MachineConstantPool.h"
  18. #include "llvm/CodeGen/MachineFunction.h"
  19. #include "llvm/CodeGen/MachineModuleInfo.h"
  20. #include "llvm/IR/Constants.h"
  21. #include "llvm/IR/DebugInfo.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/GraphWriter.h"
  24. #include "llvm/Support/raw_ostream.h"
  25. #include "llvm/Target/TargetMachine.h"
  26. #include "llvm/Target/TargetRegisterInfo.h"
  27. using namespace llvm;
  28. #define DEBUG_TYPE "dag-printer"
  29. namespace llvm {
  30. template<>
  31. struct DOTGraphTraits<SelectionDAG*> : public DefaultDOTGraphTraits {
  32. explicit DOTGraphTraits(bool isSimple=false) :
  33. DefaultDOTGraphTraits(isSimple) {}
  34. static bool hasEdgeDestLabels() {
  35. return true;
  36. }
  37. static unsigned numEdgeDestLabels(const void *Node) {
  38. return ((const SDNode *) Node)->getNumValues();
  39. }
  40. static std::string getEdgeDestLabel(const void *Node, unsigned i) {
  41. return ((const SDNode *) Node)->getValueType(i).getEVTString();
  42. }
  43. template<typename EdgeIter>
  44. static std::string getEdgeSourceLabel(const void *Node, EdgeIter I) {
  45. return itostr(I - SDNodeIterator::begin((const SDNode *) Node));
  46. }
  47. /// edgeTargetsEdgeSource - This method returns true if this outgoing edge
  48. /// should actually target another edge source, not a node. If this method
  49. /// is implemented, getEdgeTarget should be implemented.
  50. template<typename EdgeIter>
  51. static bool edgeTargetsEdgeSource(const void *Node, EdgeIter I) {
  52. return true;
  53. }
  54. /// getEdgeTarget - If edgeTargetsEdgeSource returns true, this method is
  55. /// called to determine which outgoing edge of Node is the target of this
  56. /// edge.
  57. template<typename EdgeIter>
  58. static EdgeIter getEdgeTarget(const void *Node, EdgeIter I) {
  59. SDNode *TargetNode = *I;
  60. SDNodeIterator NI = SDNodeIterator::begin(TargetNode);
  61. std::advance(NI, I.getNode()->getOperand(I.getOperand()).getResNo());
  62. return NI;
  63. }
  64. static std::string getGraphName(const SelectionDAG *G) {
  65. return G->getMachineFunction().getName();
  66. }
  67. static bool renderGraphFromBottomUp() {
  68. return true;
  69. }
  70. static bool hasNodeAddressLabel(const SDNode *Node,
  71. const SelectionDAG *Graph) {
  72. return true;
  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. /// clearGraphAttrs - Clear all previously defined node graph attributes.
  142. /// Intended to be used from a debugging tool (eg. gdb).
  143. void SelectionDAG::clearGraphAttrs() {
  144. #ifndef NDEBUG
  145. NodeGraphAttrs.clear();
  146. #else
  147. errs() << "SelectionDAG::clearGraphAttrs is only available in debug builds"
  148. << " on systems with Graphviz or gv!\n";
  149. #endif
  150. }
  151. /// setGraphAttrs - Set graph attributes for a node. (eg. "color=red".)
  152. ///
  153. void SelectionDAG::setGraphAttrs(const SDNode *N, const char *Attrs) {
  154. #ifndef NDEBUG
  155. NodeGraphAttrs[N] = Attrs;
  156. #else
  157. errs() << "SelectionDAG::setGraphAttrs is only available in debug builds"
  158. << " on systems with Graphviz or gv!\n";
  159. #endif
  160. }
  161. /// getGraphAttrs - Get graph attributes for a node. (eg. "color=red".)
  162. /// Used from getNodeAttributes.
  163. const std::string SelectionDAG::getGraphAttrs(const SDNode *N) const {
  164. #ifndef NDEBUG
  165. std::map<const SDNode *, std::string>::const_iterator I =
  166. NodeGraphAttrs.find(N);
  167. if (I != NodeGraphAttrs.end())
  168. return I->second;
  169. else
  170. return "";
  171. #else
  172. errs() << "SelectionDAG::getGraphAttrs is only available in debug builds"
  173. << " on systems with Graphviz or gv!\n";
  174. return std::string();
  175. #endif
  176. }
  177. /// setGraphColor - Convenience for setting node color attribute.
  178. ///
  179. void SelectionDAG::setGraphColor(const SDNode *N, const char *Color) {
  180. #ifndef NDEBUG
  181. NodeGraphAttrs[N] = std::string("color=") + Color;
  182. #else
  183. errs() << "SelectionDAG::setGraphColor is only available in debug builds"
  184. << " on systems with Graphviz or gv!\n";
  185. #endif
  186. }
  187. /// setSubgraphColorHelper - Implement setSubgraphColor. Return
  188. /// whether we truncated the search.
  189. ///
  190. bool SelectionDAG::setSubgraphColorHelper(SDNode *N, const char *Color, DenseSet<SDNode *> &visited,
  191. int level, bool &printed) {
  192. bool hit_limit = false;
  193. #ifndef NDEBUG
  194. if (level >= 20) {
  195. if (!printed) {
  196. printed = true;
  197. DEBUG(dbgs() << "setSubgraphColor hit max level\n");
  198. }
  199. return true;
  200. }
  201. unsigned oldSize = visited.size();
  202. visited.insert(N);
  203. if (visited.size() != oldSize) {
  204. setGraphColor(N, Color);
  205. for(SDNodeIterator i = SDNodeIterator::begin(N), iend = SDNodeIterator::end(N);
  206. i != iend;
  207. ++i) {
  208. hit_limit = setSubgraphColorHelper(*i, Color, visited, level+1, printed) || hit_limit;
  209. }
  210. }
  211. #else
  212. errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
  213. << " on systems with Graphviz or gv!\n";
  214. #endif
  215. return hit_limit;
  216. }
  217. /// setSubgraphColor - Convenience for setting subgraph color attribute.
  218. ///
  219. void SelectionDAG::setSubgraphColor(SDNode *N, const char *Color) {
  220. #ifndef NDEBUG
  221. DenseSet<SDNode *> visited;
  222. bool printed = false;
  223. if (setSubgraphColorHelper(N, Color, visited, 0, printed)) {
  224. // Visually mark that we hit the limit
  225. if (strcmp(Color, "red") == 0) {
  226. setSubgraphColorHelper(N, "blue", visited, 0, printed);
  227. } else if (strcmp(Color, "yellow") == 0) {
  228. setSubgraphColorHelper(N, "green", visited, 0, printed);
  229. }
  230. }
  231. #else
  232. errs() << "SelectionDAG::setSubgraphColor is only available in debug builds"
  233. << " on systems with Graphviz or gv!\n";
  234. #endif
  235. }
  236. std::string ScheduleDAGSDNodes::getGraphNodeLabel(const SUnit *SU) const {
  237. std::string s;
  238. raw_string_ostream O(s);
  239. O << "SU(" << SU->NodeNum << "): ";
  240. if (SU->getNode()) {
  241. SmallVector<SDNode *, 4> GluedNodes;
  242. for (SDNode *N = SU->getNode(); N; N = N->getGluedNode())
  243. GluedNodes.push_back(N);
  244. while (!GluedNodes.empty()) {
  245. O << DOTGraphTraits<SelectionDAG*>
  246. ::getSimpleNodeLabel(GluedNodes.back(), DAG);
  247. GluedNodes.pop_back();
  248. if (!GluedNodes.empty())
  249. O << "\n ";
  250. }
  251. } else {
  252. O << "CROSS RC COPY";
  253. }
  254. return O.str();
  255. }
  256. void ScheduleDAGSDNodes::getCustomGraphFeatures(GraphWriter<ScheduleDAG*> &GW) const {
  257. if (DAG) {
  258. // Draw a special "GraphRoot" node to indicate the root of the graph.
  259. GW.emitSimpleNode(nullptr, "plaintext=circle", "GraphRoot");
  260. const SDNode *N = DAG->getRoot().getNode();
  261. if (N && N->getNodeId() != -1)
  262. GW.emitEdge(nullptr, -1, &SUnits[N->getNodeId()], -1,
  263. "color=blue,style=dashed");
  264. }
  265. }