1 /* Generic dominator tree walker
2 Copyright (C) 2003, 2004, 2005, 2007 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 "basic-block.h"
27 #include "tree-flow.h"
31 /* This file implements a generic walker for dominator trees.
33 To understand the dominator walker one must first have a grasp of dominators,
34 immediate dominators and the dominator tree.
37 A block B1 is said to dominate B2 if every path from the entry to B2 must
38 pass through B1. Given the dominance relationship, we can proceed to
39 compute immediate dominators. Note it is not important whether or not
40 our definition allows a block to dominate itself.
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.
48 If we then construct a tree where each node is a basic block and there
49 is an edge from each block's immediate dominator to the block itself, then
50 we have a dominator tree.
53 [ Note this walker can also walk the post-dominator tree, which is
54 defined in a similar manner. i.e., block B1 is said to post-dominate
55 block B2 if all paths from B2 to the exit block must pass through
58 For example, given the CFG
75 We have a dominator tree which looks like
95 The dominator tree is the basis for a number of analysis, transformation
96 and optimization algorithms that operate on a semi-global basis.
98 The dominator walker is a generic routine which visits blocks in the CFG
99 via a depth first search of the dominator tree. In the example above
100 the dominator walker might visit blocks in the following order
101 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12.
103 The dominator walker has a number of callbacks to perform actions
104 during the walk of the dominator tree. There are two callbacks
105 which walk statements, one before visiting the dominator children,
106 one after visiting the dominator children. There is a callback
107 before and after each statement walk callback. In addition, the
108 dominator walker manages allocation/deallocation of data structures
109 which are local to each block visited.
111 The dominator walker is meant to provide a generic means to build a pass
112 which can analyze or transform/optimize a function based on walking
113 the dominator tree. One simply fills in the dominator walker data
114 structure with the appropriate callbacks and calls the walker.
116 We currently use the dominator walker to prune the set of variables
117 which might need PHI nodes (which can greatly improve compile-time
118 performance in some cases).
120 We also use the dominator walker to rewrite the function into SSA form
121 which reduces code duplication since the rewriting phase is inherently
122 a walk of the dominator tree.
124 And (of course), we use the dominator walker to drive a our dominator
125 optimizer, which is a semi-global optimizer.
129 Walking statements is based on the block statement iterator abstraction,
130 which is currently an abstraction over walking tree statements. Thus
131 the dominator walker is currently only useful for trees. */
133 /* Recursively walk the dominator tree.
135 WALK_DATA contains a set of callbacks to perform pass-specific
136 actions during the dominator walk as well as a stack of block local
137 data maintained during the dominator walk.
139 BB is the basic block we are currently visiting. */
142 walk_dominator_tree (struct dom_walk_data
*walk_data
, basic_block bb
)
146 block_stmt_iterator bsi
;
148 basic_block
*worklist
= XNEWVEC (basic_block
, n_basic_blocks
* 2);
153 /* Don't worry about unreachable blocks. */
154 if (EDGE_COUNT (bb
->preds
) > 0 || bb
== ENTRY_BLOCK_PTR
)
156 /* If block BB is not interesting to the caller, then none of the
157 callbacks that walk the statements in BB are going to be
159 is_interesting
= walk_data
->interesting_blocks
== NULL
160 || TEST_BIT (walk_data
->interesting_blocks
,
163 /* Callback to initialize the local data structure. */
164 if (walk_data
->initialize_block_local_data
)
168 /* First get some local data, reusing any local data pointer we may
170 if (VEC_length (void_p
, walk_data
->free_block_data
) > 0)
172 bd
= VEC_pop (void_p
, walk_data
->free_block_data
);
177 bd
= xcalloc (1, walk_data
->block_local_data_size
);
181 /* Push the local data into the local data stack. */
182 VEC_safe_push (void_p
, heap
, walk_data
->block_data_stack
, bd
);
184 /* Call the initializer. */
185 walk_data
->initialize_block_local_data (walk_data
, bb
,
190 /* Callback for operations to execute before we have walked the
191 dominator children, but before we walk statements. */
192 if (walk_data
->before_dom_children_before_stmts
)
193 (*walk_data
->before_dom_children_before_stmts
) (walk_data
, bb
);
195 /* Statement walk before walking dominator children. */
196 if (is_interesting
&& walk_data
->before_dom_children_walk_stmts
)
198 if (walk_data
->walk_stmts_backward
)
199 for (bsi
= bsi_last (bb
); !bsi_end_p (bsi
); bsi_prev (&bsi
))
200 (*walk_data
->before_dom_children_walk_stmts
) (walk_data
, bb
,
203 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
204 (*walk_data
->before_dom_children_walk_stmts
) (walk_data
, bb
,
208 /* Callback for operations to execute before we have walked the
209 dominator children, and after we walk statements. */
210 if (walk_data
->before_dom_children_after_stmts
)
211 (*walk_data
->before_dom_children_after_stmts
) (walk_data
, bb
);
213 /* Mark the current BB to be popped out of the recursion stack
214 once childs are processed. */
216 worklist
[sp
++] = NULL
;
218 for (dest
= first_dom_son (walk_data
->dom_direction
, bb
);
219 dest
; dest
= next_dom_son (walk_data
->dom_direction
, dest
))
220 worklist
[sp
++] = dest
;
222 /* NULL is used to signalize pop operation in recursion stack. */
223 while (sp
> 0 && !worklist
[sp
- 1])
227 is_interesting
= walk_data
->interesting_blocks
== NULL
228 || TEST_BIT (walk_data
->interesting_blocks
,
230 /* Callback for operations to execute after we have walked the
231 dominator children, but before we walk statements. */
232 if (walk_data
->after_dom_children_before_stmts
)
233 (*walk_data
->after_dom_children_before_stmts
) (walk_data
, bb
);
235 /* Statement walk after walking dominator children. */
236 if (is_interesting
&& walk_data
->after_dom_children_walk_stmts
)
238 if (walk_data
->walk_stmts_backward
)
239 for (bsi
= bsi_last (bb
); !bsi_end_p (bsi
); bsi_prev (&bsi
))
240 (*walk_data
->after_dom_children_walk_stmts
) (walk_data
, bb
,
243 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
244 (*walk_data
->after_dom_children_walk_stmts
) (walk_data
, bb
,
248 /* Callback for operations to execute after we have walked the
249 dominator children and after we have walked statements. */
250 if (walk_data
->after_dom_children_after_stmts
)
251 (*walk_data
->after_dom_children_after_stmts
) (walk_data
, bb
);
253 if (walk_data
->initialize_block_local_data
)
255 /* And finally pop the record off the block local data stack. */
256 bd
= VEC_pop (void_p
, walk_data
->block_data_stack
);
257 /* And save the block data so that we can re-use it. */
258 VEC_safe_push (void_p
, heap
, walk_data
->free_block_data
, bd
);
270 init_walk_dominator_tree (struct dom_walk_data
*walk_data
)
272 walk_data
->free_block_data
= NULL
;
273 walk_data
->block_data_stack
= NULL
;
277 fini_walk_dominator_tree (struct dom_walk_data
*walk_data
)
279 if (walk_data
->initialize_block_local_data
)
281 while (VEC_length (void_p
, walk_data
->free_block_data
) > 0)
282 free (VEC_pop (void_p
, walk_data
->free_block_data
));
285 VEC_free (void_p
, heap
, walk_data
->free_block_data
);
286 VEC_free (void_p
, heap
, walk_data
->block_data_stack
);