1 /* Generic dominator tree walker
2 Copyright (C) 2003-2015 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
26 #include "hard-reg-set.h"
29 #include "dominance.h"
32 #include "basic-block.h"
36 /* This file implements a generic walker for dominator trees.
38 To understand the dominator walker one must first have a grasp of dominators,
39 immediate dominators and the dominator tree.
42 A block B1 is said to dominate B2 if every path from the entry to B2 must
43 pass through B1. Given the dominance relationship, we can proceed to
44 compute immediate dominators. Note it is not important whether or not
45 our definition allows a block to dominate itself.
48 Every block in the CFG has no more than one immediate dominator. The
49 immediate dominator of block BB must dominate BB and must not dominate
50 any other dominator of BB and must not be BB itself.
53 If we then construct a tree where each node is a basic block and there
54 is an edge from each block's immediate dominator to the block itself, then
55 we have a dominator tree.
58 [ Note this walker can also walk the post-dominator tree, which is
59 defined in a similar manner. i.e., block B1 is said to post-dominate
60 block B2 if all paths from B2 to the exit block must pass through
63 For example, given the CFG
80 We have a dominator tree which looks like
100 The dominator tree is the basis for a number of analysis, transformation
101 and optimization algorithms that operate on a semi-global basis.
103 The dominator walker is a generic routine which visits blocks in the CFG
104 via a depth first search of the dominator tree. In the example above
105 the dominator walker might visit blocks in the following order
106 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12.
108 The dominator walker has a number of callbacks to perform actions
109 during the walk of the dominator tree. There are two callbacks
110 which walk statements, one before visiting the dominator children,
111 one after visiting the dominator children. There is a callback
112 before and after each statement walk callback. In addition, the
113 dominator walker manages allocation/deallocation of data structures
114 which are local to each block visited.
116 The dominator walker is meant to provide a generic means to build a pass
117 which can analyze or transform/optimize a function based on walking
118 the dominator tree. One simply fills in the dominator walker data
119 structure with the appropriate callbacks and calls the walker.
121 We currently use the dominator walker to prune the set of variables
122 which might need PHI nodes (which can greatly improve compile-time
123 performance in some cases).
125 We also use the dominator walker to rewrite the function into SSA form
126 which reduces code duplication since the rewriting phase is inherently
127 a walk of the dominator tree.
129 And (of course), we use the dominator walker to drive our dominator
130 optimizer, which is a semi-global optimizer.
134 Walking statements is based on the block statement iterator abstraction,
135 which is currently an abstraction over walking tree statements. Thus
136 the dominator walker is currently only useful for trees. */
138 static int *bb_postorder
;
141 cmp_bb_postorder (const void *a
, const void *b
)
143 basic_block bb1
= *(basic_block
*)const_cast<void *>(a
);
144 basic_block bb2
= *(basic_block
*)const_cast<void *>(b
);
145 if (bb1
->index
== bb2
->index
)
147 /* Place higher completion number first (pop off lower number first). */
148 if (bb_postorder
[bb1
->index
] > bb_postorder
[bb2
->index
])
153 /* Recursively walk the dominator tree.
154 BB is the basic block we are currently visiting. */
157 dom_walker::walk (basic_block bb
)
160 basic_block
*worklist
= XNEWVEC (basic_block
,
161 n_basic_blocks_for_fn (cfun
) * 2);
163 int *postorder
, postorder_num
;
165 if (m_dom_direction
== CDI_DOMINATORS
)
167 postorder
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
168 postorder_num
= inverted_post_order_compute (postorder
);
169 bb_postorder
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
170 for (int i
= 0; i
< postorder_num
; ++i
)
171 bb_postorder
[postorder
[i
]] = i
;
177 /* Don't worry about unreachable blocks. */
178 if (EDGE_COUNT (bb
->preds
) > 0
179 || bb
== ENTRY_BLOCK_PTR_FOR_FN (cfun
)
180 || bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
182 /* Callback for subclasses to do custom things before we have walked
183 the dominator children, but before we walk statements. */
184 before_dom_children (bb
);
186 /* Mark the current BB to be popped out of the recursion stack
187 once children are processed. */
189 worklist
[sp
++] = NULL
;
192 for (dest
= first_dom_son (m_dom_direction
, bb
);
193 dest
; dest
= next_dom_son (m_dom_direction
, dest
))
194 worklist
[sp
++] = dest
;
195 if (m_dom_direction
== CDI_DOMINATORS
)
196 switch (sp
- saved_sp
)
202 qsort (&worklist
[saved_sp
], sp
- saved_sp
,
203 sizeof (basic_block
), cmp_bb_postorder
);
206 /* NULL is used to mark pop operations in the recursion stack. */
207 while (sp
> 0 && !worklist
[sp
- 1])
212 /* Callback allowing subclasses to do custom things after we have
213 walked dominator children, but before we walk statements. */
214 after_dom_children (bb
);
221 if (m_dom_direction
== CDI_DOMINATORS
)