RegionPrinter.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. //===- RegionPrinter.cpp - Print regions tree pass ------------------------===//
  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. // Print out the region tree of a function using dotty/graphviz.
  9. //===----------------------------------------------------------------------===//
  10. #include "llvm/Analysis/RegionPrinter.h"
  11. #include "llvm/ADT/DepthFirstIterator.h"
  12. #include "llvm/ADT/PostOrderIterator.h"
  13. #include "llvm/ADT/Statistic.h"
  14. #include "llvm/Analysis/DOTGraphTraitsPass.h"
  15. #include "llvm/Analysis/Passes.h"
  16. #include "llvm/Analysis/RegionInfo.h"
  17. #include "llvm/Analysis/RegionIterator.h"
  18. #include "llvm/InitializePasses.h"
  19. #include "llvm/Support/CommandLine.h"
  20. #include "llvm/Support/Debug.h"
  21. #include "llvm/Support/raw_ostream.h"
  22. #ifndef NDEBUG
  23. #include "llvm/IR/LegacyPassManager.h"
  24. #endif
  25. using namespace llvm;
  26. //===----------------------------------------------------------------------===//
  27. /// onlySimpleRegion - Show only the simple regions in the RegionViewer.
  28. static cl::opt<bool>
  29. onlySimpleRegions("only-simple-regions",
  30. cl::desc("Show only simple regions in the graphviz viewer"),
  31. cl::Hidden,
  32. cl::init(false));
  33. namespace llvm {
  34. template<>
  35. struct DOTGraphTraits<RegionNode*> : public DefaultDOTGraphTraits {
  36. DOTGraphTraits (bool isSimple=false)
  37. : DefaultDOTGraphTraits(isSimple) {}
  38. std::string getNodeLabel(RegionNode *Node, RegionNode *Graph) {
  39. if (!Node->isSubRegion()) {
  40. BasicBlock *BB = Node->getNodeAs<BasicBlock>();
  41. if (isSimple())
  42. return DOTGraphTraits<DOTFuncInfo *>
  43. ::getSimpleNodeLabel(BB, nullptr);
  44. else
  45. return DOTGraphTraits<DOTFuncInfo *>
  46. ::getCompleteNodeLabel(BB, nullptr);
  47. }
  48. return "Not implemented";
  49. }
  50. };
  51. template <>
  52. struct DOTGraphTraits<RegionInfo *> : public DOTGraphTraits<RegionNode *> {
  53. DOTGraphTraits (bool isSimple = false)
  54. : DOTGraphTraits<RegionNode*>(isSimple) {}
  55. static std::string getGraphName(const RegionInfo *) { return "Region Graph"; }
  56. std::string getNodeLabel(RegionNode *Node, RegionInfo *G) {
  57. return DOTGraphTraits<RegionNode *>::getNodeLabel(
  58. Node, reinterpret_cast<RegionNode *>(G->getTopLevelRegion()));
  59. }
  60. std::string getEdgeAttributes(RegionNode *srcNode,
  61. GraphTraits<RegionInfo *>::ChildIteratorType CI,
  62. RegionInfo *G) {
  63. RegionNode *destNode = *CI;
  64. if (srcNode->isSubRegion() || destNode->isSubRegion())
  65. return "";
  66. // In case of a backedge, do not use it to define the layout of the nodes.
  67. BasicBlock *srcBB = srcNode->getNodeAs<BasicBlock>();
  68. BasicBlock *destBB = destNode->getNodeAs<BasicBlock>();
  69. Region *R = G->getRegionFor(destBB);
  70. while (R && R->getParent())
  71. if (R->getParent()->getEntry() == destBB)
  72. R = R->getParent();
  73. else
  74. break;
  75. if (R && R->getEntry() == destBB && R->contains(srcBB))
  76. return "constraint=false";
  77. return "";
  78. }
  79. // Print the cluster of the subregions. This groups the single basic blocks
  80. // and adds a different background color for each group.
  81. static void printRegionCluster(const Region &R, GraphWriter<RegionInfo *> &GW,
  82. unsigned depth = 0) {
  83. raw_ostream &O = GW.getOStream();
  84. O.indent(2 * depth) << "subgraph cluster_" << static_cast<const void*>(&R)
  85. << " {\n";
  86. O.indent(2 * (depth + 1)) << "label = \"\";\n";
  87. if (!onlySimpleRegions || R.isSimple()) {
  88. O.indent(2 * (depth + 1)) << "style = filled;\n";
  89. O.indent(2 * (depth + 1)) << "color = "
  90. << ((R.getDepth() * 2 % 12) + 1) << "\n";
  91. } else {
  92. O.indent(2 * (depth + 1)) << "style = solid;\n";
  93. O.indent(2 * (depth + 1)) << "color = "
  94. << ((R.getDepth() * 2 % 12) + 2) << "\n";
  95. }
  96. for (const auto &RI : R)
  97. printRegionCluster(*RI, GW, depth + 1);
  98. const RegionInfo &RI = *static_cast<const RegionInfo*>(R.getRegionInfo());
  99. for (auto *BB : R.blocks())
  100. if (RI.getRegionFor(BB) == &R)
  101. O.indent(2 * (depth + 1)) << "Node"
  102. << static_cast<const void*>(RI.getTopLevelRegion()->getBBNode(BB))
  103. << ";\n";
  104. O.indent(2 * depth) << "}\n";
  105. }
  106. static void addCustomGraphFeatures(const RegionInfo *G,
  107. GraphWriter<RegionInfo *> &GW) {
  108. raw_ostream &O = GW.getOStream();
  109. O << "\tcolorscheme = \"paired12\"\n";
  110. printRegionCluster(*G->getTopLevelRegion(), GW, 4);
  111. }
  112. };
  113. } //end namespace llvm
  114. namespace {
  115. struct RegionInfoPassGraphTraits {
  116. static RegionInfo *getGraph(RegionInfoPass *RIP) {
  117. return &RIP->getRegionInfo();
  118. }
  119. };
  120. struct RegionPrinter
  121. : public DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
  122. RegionInfoPassGraphTraits> {
  123. static char ID;
  124. RegionPrinter()
  125. : DOTGraphTraitsPrinter<RegionInfoPass, false, RegionInfo *,
  126. RegionInfoPassGraphTraits>("reg", ID) {
  127. initializeRegionPrinterPass(*PassRegistry::getPassRegistry());
  128. }
  129. };
  130. char RegionPrinter::ID = 0;
  131. struct RegionOnlyPrinter
  132. : public DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
  133. RegionInfoPassGraphTraits> {
  134. static char ID;
  135. RegionOnlyPrinter()
  136. : DOTGraphTraitsPrinter<RegionInfoPass, true, RegionInfo *,
  137. RegionInfoPassGraphTraits>("reg", ID) {
  138. initializeRegionOnlyPrinterPass(*PassRegistry::getPassRegistry());
  139. }
  140. };
  141. char RegionOnlyPrinter::ID = 0;
  142. struct RegionViewer
  143. : public DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
  144. RegionInfoPassGraphTraits> {
  145. static char ID;
  146. RegionViewer()
  147. : DOTGraphTraitsViewer<RegionInfoPass, false, RegionInfo *,
  148. RegionInfoPassGraphTraits>("reg", ID) {
  149. initializeRegionViewerPass(*PassRegistry::getPassRegistry());
  150. }
  151. };
  152. char RegionViewer::ID = 0;
  153. struct RegionOnlyViewer
  154. : public DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
  155. RegionInfoPassGraphTraits> {
  156. static char ID;
  157. RegionOnlyViewer()
  158. : DOTGraphTraitsViewer<RegionInfoPass, true, RegionInfo *,
  159. RegionInfoPassGraphTraits>("regonly", ID) {
  160. initializeRegionOnlyViewerPass(*PassRegistry::getPassRegistry());
  161. }
  162. };
  163. char RegionOnlyViewer::ID = 0;
  164. } //end anonymous namespace
  165. INITIALIZE_PASS(RegionPrinter, "dot-regions",
  166. "Print regions of function to 'dot' file", true, true)
  167. INITIALIZE_PASS(
  168. RegionOnlyPrinter, "dot-regions-only",
  169. "Print regions of function to 'dot' file (with no function bodies)", true,
  170. true)
  171. INITIALIZE_PASS(RegionViewer, "view-regions", "View regions of function",
  172. true, true)
  173. INITIALIZE_PASS(RegionOnlyViewer, "view-regions-only",
  174. "View regions of function (with no function bodies)",
  175. true, true)
  176. FunctionPass *llvm::createRegionPrinterPass() { return new RegionPrinter(); }
  177. FunctionPass *llvm::createRegionOnlyPrinterPass() {
  178. return new RegionOnlyPrinter();
  179. }
  180. FunctionPass* llvm::createRegionViewerPass() {
  181. return new RegionViewer();
  182. }
  183. FunctionPass* llvm::createRegionOnlyViewerPass() {
  184. return new RegionOnlyViewer();
  185. }
  186. #ifndef NDEBUG
  187. static void viewRegionInfo(RegionInfo *RI, bool ShortNames) {
  188. assert(RI && "Argument must be non-null");
  189. llvm::Function *F = RI->getTopLevelRegion()->getEntry()->getParent();
  190. std::string GraphName = DOTGraphTraits<RegionInfo *>::getGraphName(RI);
  191. llvm::ViewGraph(RI, "reg", ShortNames,
  192. Twine(GraphName) + " for '" + F->getName() + "' function");
  193. }
  194. static void invokeFunctionPass(const Function *F, FunctionPass *ViewerPass) {
  195. assert(F && "Argument must be non-null");
  196. assert(!F->isDeclaration() && "Function must have an implementation");
  197. // The viewer and analysis passes do not modify anything, so we can safely
  198. // remove the const qualifier
  199. auto NonConstF = const_cast<Function *>(F);
  200. llvm::legacy::FunctionPassManager FPM(NonConstF->getParent());
  201. FPM.add(ViewerPass);
  202. FPM.doInitialization();
  203. FPM.run(*NonConstF);
  204. FPM.doFinalization();
  205. }
  206. void llvm::viewRegion(RegionInfo *RI) { viewRegionInfo(RI, false); }
  207. void llvm::viewRegion(const Function *F) {
  208. invokeFunctionPass(F, createRegionViewerPass());
  209. }
  210. void llvm::viewRegionOnly(RegionInfo *RI) { viewRegionInfo(RI, true); }
  211. void llvm::viewRegionOnly(const Function *F) {
  212. invokeFunctionPass(F, createRegionOnlyViewerPass());
  213. }
  214. #endif