RegionPrinter.cpp 8.5 KB

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