DomPrinter.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. //===- DomPrinter.cpp - DOT printer for the dominance trees ------------===//
  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 file defines '-dot-dom' and '-dot-postdom' analysis passes, which emit
  10. // a dom.<fnname>.dot or postdom.<fnname>.dot file for each function in the
  11. // program, with a graph of the dominance/postdominance tree of that
  12. // function.
  13. //
  14. // There are also passes available to directly call dotty ('-view-dom' or
  15. // '-view-postdom'). By appending '-only' like '-dot-dom-only' only the
  16. // names of the bbs are printed, but the content is hidden.
  17. //
  18. //===----------------------------------------------------------------------===//
  19. #include "llvm/Analysis/DomPrinter.h"
  20. #include "llvm/Analysis/DOTGraphTraitsPass.h"
  21. #include "llvm/Analysis/PostDominators.h"
  22. #include "llvm/InitializePasses.h"
  23. using namespace llvm;
  24. void DominatorTree::viewGraph(const Twine &Name, const Twine &Title) {
  25. #ifndef NDEBUG
  26. ViewGraph(this, Name, false, Title);
  27. #else
  28. errs() << "DomTree dump not available, build with DEBUG\n";
  29. #endif // NDEBUG
  30. }
  31. void DominatorTree::viewGraph() {
  32. #ifndef NDEBUG
  33. this->viewGraph("domtree", "Dominator Tree for function");
  34. #else
  35. errs() << "DomTree dump not available, build with DEBUG\n";
  36. #endif // NDEBUG
  37. }
  38. namespace {
  39. struct LegacyDominatorTreeWrapperPassAnalysisGraphTraits {
  40. static DominatorTree *getGraph(DominatorTreeWrapperPass *DTWP) {
  41. return &DTWP->getDomTree();
  42. }
  43. };
  44. struct DomViewerWrapperPass
  45. : public DOTGraphTraitsViewerWrapperPass<
  46. DominatorTreeWrapperPass, false, DominatorTree *,
  47. LegacyDominatorTreeWrapperPassAnalysisGraphTraits> {
  48. static char ID;
  49. DomViewerWrapperPass()
  50. : DOTGraphTraitsViewerWrapperPass<
  51. DominatorTreeWrapperPass, false, DominatorTree *,
  52. LegacyDominatorTreeWrapperPassAnalysisGraphTraits>("dom", ID) {
  53. initializeDomViewerWrapperPassPass(*PassRegistry::getPassRegistry());
  54. }
  55. };
  56. struct DomOnlyViewerWrapperPass
  57. : public DOTGraphTraitsViewerWrapperPass<
  58. DominatorTreeWrapperPass, true, DominatorTree *,
  59. LegacyDominatorTreeWrapperPassAnalysisGraphTraits> {
  60. static char ID;
  61. DomOnlyViewerWrapperPass()
  62. : DOTGraphTraitsViewerWrapperPass<
  63. DominatorTreeWrapperPass, true, DominatorTree *,
  64. LegacyDominatorTreeWrapperPassAnalysisGraphTraits>("domonly", ID) {
  65. initializeDomOnlyViewerWrapperPassPass(*PassRegistry::getPassRegistry());
  66. }
  67. };
  68. struct LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits {
  69. static PostDominatorTree *getGraph(PostDominatorTreeWrapperPass *PDTWP) {
  70. return &PDTWP->getPostDomTree();
  71. }
  72. };
  73. struct PostDomViewerWrapperPass
  74. : public DOTGraphTraitsViewerWrapperPass<
  75. PostDominatorTreeWrapperPass, false, PostDominatorTree *,
  76. LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits> {
  77. static char ID;
  78. PostDomViewerWrapperPass()
  79. : DOTGraphTraitsViewerWrapperPass<
  80. PostDominatorTreeWrapperPass, false, PostDominatorTree *,
  81. LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits>("postdom",
  82. ID) {
  83. initializePostDomViewerWrapperPassPass(*PassRegistry::getPassRegistry());
  84. }
  85. };
  86. struct PostDomOnlyViewerWrapperPass
  87. : public DOTGraphTraitsViewerWrapperPass<
  88. PostDominatorTreeWrapperPass, true, PostDominatorTree *,
  89. LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits> {
  90. static char ID;
  91. PostDomOnlyViewerWrapperPass()
  92. : DOTGraphTraitsViewerWrapperPass<
  93. PostDominatorTreeWrapperPass, true, PostDominatorTree *,
  94. LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits>(
  95. "postdomonly", ID) {
  96. initializePostDomOnlyViewerWrapperPassPass(
  97. *PassRegistry::getPassRegistry());
  98. }
  99. };
  100. } // end anonymous namespace
  101. char DomViewerWrapperPass::ID = 0;
  102. INITIALIZE_PASS(DomViewerWrapperPass, "view-dom",
  103. "View dominance tree of function", false, false)
  104. char DomOnlyViewerWrapperPass::ID = 0;
  105. INITIALIZE_PASS(DomOnlyViewerWrapperPass, "view-dom-only",
  106. "View dominance tree of function (with no function bodies)",
  107. false, false)
  108. char PostDomViewerWrapperPass::ID = 0;
  109. INITIALIZE_PASS(PostDomViewerWrapperPass, "view-postdom",
  110. "View postdominance tree of function", false, false)
  111. char PostDomOnlyViewerWrapperPass::ID = 0;
  112. INITIALIZE_PASS(PostDomOnlyViewerWrapperPass, "view-postdom-only",
  113. "View postdominance tree of function "
  114. "(with no function bodies)",
  115. false, false)
  116. namespace {
  117. struct DomPrinterWrapperPass
  118. : public DOTGraphTraitsPrinterWrapperPass<
  119. DominatorTreeWrapperPass, false, DominatorTree *,
  120. LegacyDominatorTreeWrapperPassAnalysisGraphTraits> {
  121. static char ID;
  122. DomPrinterWrapperPass()
  123. : DOTGraphTraitsPrinterWrapperPass<
  124. DominatorTreeWrapperPass, false, DominatorTree *,
  125. LegacyDominatorTreeWrapperPassAnalysisGraphTraits>("dom", ID) {
  126. initializeDomPrinterWrapperPassPass(*PassRegistry::getPassRegistry());
  127. }
  128. };
  129. struct DomOnlyPrinterWrapperPass
  130. : public DOTGraphTraitsPrinterWrapperPass<
  131. DominatorTreeWrapperPass, true, DominatorTree *,
  132. LegacyDominatorTreeWrapperPassAnalysisGraphTraits> {
  133. static char ID;
  134. DomOnlyPrinterWrapperPass()
  135. : DOTGraphTraitsPrinterWrapperPass<
  136. DominatorTreeWrapperPass, true, DominatorTree *,
  137. LegacyDominatorTreeWrapperPassAnalysisGraphTraits>("domonly", ID) {
  138. initializeDomOnlyPrinterWrapperPassPass(*PassRegistry::getPassRegistry());
  139. }
  140. };
  141. struct PostDomPrinterWrapperPass
  142. : public DOTGraphTraitsPrinterWrapperPass<
  143. PostDominatorTreeWrapperPass, false, PostDominatorTree *,
  144. LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits> {
  145. static char ID;
  146. PostDomPrinterWrapperPass()
  147. : DOTGraphTraitsPrinterWrapperPass<
  148. PostDominatorTreeWrapperPass, false, PostDominatorTree *,
  149. LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits>("postdom",
  150. ID) {
  151. initializePostDomPrinterWrapperPassPass(*PassRegistry::getPassRegistry());
  152. }
  153. };
  154. struct PostDomOnlyPrinterWrapperPass
  155. : public DOTGraphTraitsPrinterWrapperPass<
  156. PostDominatorTreeWrapperPass, true, PostDominatorTree *,
  157. LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits> {
  158. static char ID;
  159. PostDomOnlyPrinterWrapperPass()
  160. : DOTGraphTraitsPrinterWrapperPass<
  161. PostDominatorTreeWrapperPass, true, PostDominatorTree *,
  162. LegacyPostDominatorTreeWrapperPassAnalysisGraphTraits>(
  163. "postdomonly", ID) {
  164. initializePostDomOnlyPrinterWrapperPassPass(
  165. *PassRegistry::getPassRegistry());
  166. }
  167. };
  168. } // end anonymous namespace
  169. char DomPrinterWrapperPass::ID = 0;
  170. INITIALIZE_PASS(DomPrinterWrapperPass, "dot-dom",
  171. "Print dominance tree of function to 'dot' file", false, false)
  172. char DomOnlyPrinterWrapperPass::ID = 0;
  173. INITIALIZE_PASS(DomOnlyPrinterWrapperPass, "dot-dom-only",
  174. "Print dominance tree of function to 'dot' file "
  175. "(with no function bodies)",
  176. false, false)
  177. char PostDomPrinterWrapperPass::ID = 0;
  178. INITIALIZE_PASS(PostDomPrinterWrapperPass, "dot-postdom",
  179. "Print postdominance tree of function to 'dot' file", false,
  180. false)
  181. char PostDomOnlyPrinterWrapperPass::ID = 0;
  182. INITIALIZE_PASS(PostDomOnlyPrinterWrapperPass, "dot-postdom-only",
  183. "Print postdominance tree of function to 'dot' file "
  184. "(with no function bodies)",
  185. false, false)
  186. // Create methods available outside of this file, to use them
  187. // "include/llvm/LinkAllPasses.h". Otherwise the pass would be deleted by
  188. // the link time optimization.
  189. FunctionPass *llvm::createDomPrinterWrapperPassPass() {
  190. return new DomPrinterWrapperPass();
  191. }
  192. FunctionPass *llvm::createDomOnlyPrinterWrapperPassPass() {
  193. return new DomOnlyPrinterWrapperPass();
  194. }
  195. FunctionPass *llvm::createDomViewerWrapperPassPass() {
  196. return new DomViewerWrapperPass();
  197. }
  198. FunctionPass *llvm::createDomOnlyViewerWrapperPassPass() {
  199. return new DomOnlyViewerWrapperPass();
  200. }
  201. FunctionPass *llvm::createPostDomPrinterWrapperPassPass() {
  202. return new PostDomPrinterWrapperPass();
  203. }
  204. FunctionPass *llvm::createPostDomOnlyPrinterWrapperPassPass() {
  205. return new PostDomOnlyPrinterWrapperPass();
  206. }
  207. FunctionPass *llvm::createPostDomViewerWrapperPassPass() {
  208. return new PostDomViewerWrapperPass();
  209. }
  210. FunctionPass *llvm::createPostDomOnlyViewerWrapperPassPass() {
  211. return new PostDomOnlyViewerWrapperPass();
  212. }