domwalk.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /* Generic dominator tree walker
  2. Copyright (C) 2003-2015 Free Software Foundation, Inc.
  3. Contributed by Diego Novillo <dnovillo@redhat.com>
  4. This file is part of GCC.
  5. GCC is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3, or (at your option)
  8. any later version.
  9. GCC is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GCC; see the file COPYING3. If not see
  15. <http://www.gnu.org/licenses/>. */
  16. #include "config.h"
  17. #include "system.h"
  18. #include "coretypes.h"
  19. #include "tm.h"
  20. #include "predict.h"
  21. #include "vec.h"
  22. #include "hashtab.h"
  23. #include "hash-set.h"
  24. #include "machmode.h"
  25. #include "hard-reg-set.h"
  26. #include "input.h"
  27. #include "function.h"
  28. #include "dominance.h"
  29. #include "cfg.h"
  30. #include "cfganal.h"
  31. #include "basic-block.h"
  32. #include "domwalk.h"
  33. #include "sbitmap.h"
  34. /* This file implements a generic walker for dominator trees.
  35. To understand the dominator walker one must first have a grasp of dominators,
  36. immediate dominators and the dominator tree.
  37. Dominators
  38. A block B1 is said to dominate B2 if every path from the entry to B2 must
  39. pass through B1. Given the dominance relationship, we can proceed to
  40. compute immediate dominators. Note it is not important whether or not
  41. our definition allows a block to dominate itself.
  42. Immediate Dominators:
  43. Every block in the CFG has no more than one immediate dominator. The
  44. immediate dominator of block BB must dominate BB and must not dominate
  45. any other dominator of BB and must not be BB itself.
  46. Dominator tree:
  47. If we then construct a tree where each node is a basic block and there
  48. is an edge from each block's immediate dominator to the block itself, then
  49. we have a dominator tree.
  50. [ Note this walker can also walk the post-dominator tree, which is
  51. defined in a similar manner. i.e., block B1 is said to post-dominate
  52. block B2 if all paths from B2 to the exit block must pass through
  53. B1. ]
  54. For example, given the CFG
  55. 1
  56. |
  57. 2
  58. / \
  59. 3 4
  60. / \
  61. +---------->5 6
  62. | / \ /
  63. | +--->8 7
  64. | | / |
  65. | +--9 11
  66. | / |
  67. +--- 10 ---> 12
  68. We have a dominator tree which looks like
  69. 1
  70. |
  71. 2
  72. / \
  73. / \
  74. 3 4
  75. / / \ \
  76. | | | |
  77. 5 6 7 12
  78. | |
  79. 8 11
  80. |
  81. 9
  82. |
  83. 10
  84. The dominator tree is the basis for a number of analysis, transformation
  85. and optimization algorithms that operate on a semi-global basis.
  86. The dominator walker is a generic routine which visits blocks in the CFG
  87. via a depth first search of the dominator tree. In the example above
  88. the dominator walker might visit blocks in the following order
  89. 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12.
  90. The dominator walker has a number of callbacks to perform actions
  91. during the walk of the dominator tree. There are two callbacks
  92. which walk statements, one before visiting the dominator children,
  93. one after visiting the dominator children. There is a callback
  94. before and after each statement walk callback. In addition, the
  95. dominator walker manages allocation/deallocation of data structures
  96. which are local to each block visited.
  97. The dominator walker is meant to provide a generic means to build a pass
  98. which can analyze or transform/optimize a function based on walking
  99. the dominator tree. One simply fills in the dominator walker data
  100. structure with the appropriate callbacks and calls the walker.
  101. We currently use the dominator walker to prune the set of variables
  102. which might need PHI nodes (which can greatly improve compile-time
  103. performance in some cases).
  104. We also use the dominator walker to rewrite the function into SSA form
  105. which reduces code duplication since the rewriting phase is inherently
  106. a walk of the dominator tree.
  107. And (of course), we use the dominator walker to drive our dominator
  108. optimizer, which is a semi-global optimizer.
  109. TODO:
  110. Walking statements is based on the block statement iterator abstraction,
  111. which is currently an abstraction over walking tree statements. Thus
  112. the dominator walker is currently only useful for trees. */
  113. static int *bb_postorder;
  114. static int
  115. cmp_bb_postorder (const void *a, const void *b)
  116. {
  117. basic_block bb1 = *(basic_block *)const_cast<void *>(a);
  118. basic_block bb2 = *(basic_block *)const_cast<void *>(b);
  119. if (bb1->index == bb2->index)
  120. return 0;
  121. /* Place higher completion number first (pop off lower number first). */
  122. if (bb_postorder[bb1->index] > bb_postorder[bb2->index])
  123. return -1;
  124. return 1;
  125. }
  126. /* Recursively walk the dominator tree.
  127. BB is the basic block we are currently visiting. */
  128. void
  129. dom_walker::walk (basic_block bb)
  130. {
  131. basic_block dest;
  132. basic_block *worklist = XNEWVEC (basic_block,
  133. n_basic_blocks_for_fn (cfun) * 2);
  134. int sp = 0;
  135. int *postorder, postorder_num;
  136. if (m_dom_direction == CDI_DOMINATORS)
  137. {
  138. postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
  139. postorder_num = inverted_post_order_compute (postorder);
  140. bb_postorder = XNEWVEC (int, last_basic_block_for_fn (cfun));
  141. for (int i = 0; i < postorder_num; ++i)
  142. bb_postorder[postorder[i]] = i;
  143. free (postorder);
  144. }
  145. while (true)
  146. {
  147. /* Don't worry about unreachable blocks. */
  148. if (EDGE_COUNT (bb->preds) > 0
  149. || bb == ENTRY_BLOCK_PTR_FOR_FN (cfun)
  150. || bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
  151. {
  152. /* Callback for subclasses to do custom things before we have walked
  153. the dominator children, but before we walk statements. */
  154. before_dom_children (bb);
  155. /* Mark the current BB to be popped out of the recursion stack
  156. once children are processed. */
  157. worklist[sp++] = bb;
  158. worklist[sp++] = NULL;
  159. int saved_sp = sp;
  160. for (dest = first_dom_son (m_dom_direction, bb);
  161. dest; dest = next_dom_son (m_dom_direction, dest))
  162. worklist[sp++] = dest;
  163. if (m_dom_direction == CDI_DOMINATORS)
  164. switch (sp - saved_sp)
  165. {
  166. case 0:
  167. case 1:
  168. break;
  169. default:
  170. qsort (&worklist[saved_sp], sp - saved_sp,
  171. sizeof (basic_block), cmp_bb_postorder);
  172. }
  173. }
  174. /* NULL is used to mark pop operations in the recursion stack. */
  175. while (sp > 0 && !worklist[sp - 1])
  176. {
  177. --sp;
  178. bb = worklist[--sp];
  179. /* Callback allowing subclasses to do custom things after we have
  180. walked dominator children, but before we walk statements. */
  181. after_dom_children (bb);
  182. }
  183. if (sp)
  184. bb = worklist[--sp];
  185. else
  186. break;
  187. }
  188. if (m_dom_direction == CDI_DOMINATORS)
  189. {
  190. free (bb_postorder);
  191. bb_postorder = NULL;
  192. }
  193. free (worklist);
  194. }