Daily bump.
[official-gcc.git] / gcc / domwalk.c
blob8c1ddc69490764651fe7f65871b7d7d150706b10
1 /* Generic dominator tree walker
2 Copyright (C) 2003-2013 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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "basic-block.h"
26 #include "domwalk.h"
27 #include "sbitmap.h"
29 /* This file implements a generic walker for dominator trees.
31 To understand the dominator walker one must first have a grasp of dominators,
32 immediate dominators and the dominator tree.
34 Dominators
35 A block B1 is said to dominate B2 if every path from the entry to B2 must
36 pass through B1. Given the dominance relationship, we can proceed to
37 compute immediate dominators. Note it is not important whether or not
38 our definition allows a block to dominate itself.
40 Immediate Dominators:
41 Every block in the CFG has no more than one immediate dominator. The
42 immediate dominator of block BB must dominate BB and must not dominate
43 any other dominator of BB and must not be BB itself.
45 Dominator tree:
46 If we then construct a tree where each node is a basic block and there
47 is an edge from each block's immediate dominator to the block itself, then
48 we have a dominator tree.
51 [ Note this walker can also walk the post-dominator tree, which is
52 defined in a similar manner. i.e., block B1 is said to post-dominate
53 block B2 if all paths from B2 to the exit block must pass through
54 B1. ]
56 For example, given the CFG
61 / \
62 3 4
63 / \
64 +---------->5 6
65 | / \ /
66 | +--->8 7
67 | | / |
68 | +--9 11
69 | / |
70 +--- 10 ---> 12
73 We have a dominator tree which looks like
78 / \
79 / \
80 3 4
81 / / \ \
82 | | | |
83 5 6 7 12
84 | |
85 8 11
93 The dominator tree is the basis for a number of analysis, transformation
94 and optimization algorithms that operate on a semi-global basis.
96 The dominator walker is a generic routine which visits blocks in the CFG
97 via a depth first search of the dominator tree. In the example above
98 the dominator walker might visit blocks in the following order
99 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12.
101 The dominator walker has a number of callbacks to perform actions
102 during the walk of the dominator tree. There are two callbacks
103 which walk statements, one before visiting the dominator children,
104 one after visiting the dominator children. There is a callback
105 before and after each statement walk callback. In addition, the
106 dominator walker manages allocation/deallocation of data structures
107 which are local to each block visited.
109 The dominator walker is meant to provide a generic means to build a pass
110 which can analyze or transform/optimize a function based on walking
111 the dominator tree. One simply fills in the dominator walker data
112 structure with the appropriate callbacks and calls the walker.
114 We currently use the dominator walker to prune the set of variables
115 which might need PHI nodes (which can greatly improve compile-time
116 performance in some cases).
118 We also use the dominator walker to rewrite the function into SSA form
119 which reduces code duplication since the rewriting phase is inherently
120 a walk of the dominator tree.
122 And (of course), we use the dominator walker to drive our dominator
123 optimizer, which is a semi-global optimizer.
125 TODO:
127 Walking statements is based on the block statement iterator abstraction,
128 which is currently an abstraction over walking tree statements. Thus
129 the dominator walker is currently only useful for trees. */
131 static int *bb_postorder;
133 static int
134 cmp_bb_postorder (const void *a, const void *b)
136 basic_block bb1 = *(basic_block *)const_cast<void *>(a);
137 basic_block bb2 = *(basic_block *)const_cast<void *>(b);
138 if (bb1->index == bb2->index)
139 return 0;
140 /* Place higher completion number first (pop off lower number first). */
141 if (bb_postorder[bb1->index] > bb_postorder[bb2->index])
142 return -1;
143 return 1;
146 /* Recursively walk the dominator tree.
148 WALK_DATA contains a set of callbacks to perform pass-specific
149 actions during the dominator walk as well as a stack of block local
150 data maintained during the dominator walk.
152 BB is the basic block we are currently visiting. */
154 void
155 walk_dominator_tree (struct dom_walk_data *walk_data, basic_block bb)
157 void *bd = NULL;
158 basic_block dest;
159 basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks * 2);
160 int sp = 0;
161 int *postorder, postorder_num;
163 if (walk_data->dom_direction == CDI_DOMINATORS)
165 postorder = XNEWVEC (int, n_basic_blocks);
166 postorder_num = inverted_post_order_compute (postorder);
167 bb_postorder = XNEWVEC (int, last_basic_block);
168 for (int i = 0; i < postorder_num; ++i)
169 bb_postorder[postorder[i]] = i;
170 free (postorder);
173 while (true)
175 /* Don't worry about unreachable blocks. */
176 if (EDGE_COUNT (bb->preds) > 0
177 || bb == ENTRY_BLOCK_PTR
178 || bb == EXIT_BLOCK_PTR)
180 /* Callback to initialize the local data structure. */
181 if (walk_data->initialize_block_local_data)
183 bool recycled;
185 /* First get some local data, reusing any local data
186 pointer we may have saved. */
187 if (walk_data->free_block_data.length () > 0)
189 bd = walk_data->free_block_data.pop ();
190 recycled = 1;
192 else
194 bd = xcalloc (1, walk_data->block_local_data_size);
195 recycled = 0;
198 /* Push the local data into the local data stack. */
199 walk_data->block_data_stack.safe_push (bd);
201 /* Call the initializer. */
202 walk_data->initialize_block_local_data (walk_data, bb,
203 recycled);
207 /* Callback for operations to execute before we have walked the
208 dominator children, but before we walk statements. */
209 if (walk_data->before_dom_children)
210 (*walk_data->before_dom_children) (walk_data, bb);
212 /* Mark the current BB to be popped out of the recursion stack
213 once children are processed. */
214 worklist[sp++] = bb;
215 worklist[sp++] = NULL;
217 int saved_sp = sp;
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;
221 if (walk_data->dom_direction == CDI_DOMINATORS)
222 switch (sp - saved_sp)
224 case 0:
225 case 1:
226 break;
227 default:
228 qsort (&worklist[saved_sp], sp - saved_sp,
229 sizeof (basic_block), cmp_bb_postorder);
232 /* NULL is used to mark pop operations in the recursion stack. */
233 while (sp > 0 && !worklist[sp - 1])
235 --sp;
236 bb = worklist[--sp];
238 /* Callback for operations to execute after we have walked the
239 dominator children, but before we walk statements. */
240 if (walk_data->after_dom_children)
241 (*walk_data->after_dom_children) (walk_data, bb);
243 if (walk_data->initialize_block_local_data)
245 /* And finally pop the record off the block local data stack. */
246 bd = walk_data->block_data_stack.pop ();
247 /* And save the block data so that we can re-use it. */
248 walk_data->free_block_data.safe_push (bd);
251 if (sp)
252 bb = worklist[--sp];
253 else
254 break;
256 if (walk_data->dom_direction == CDI_DOMINATORS)
258 free (bb_postorder);
259 bb_postorder = NULL;
261 free (worklist);
264 void
265 init_walk_dominator_tree (struct dom_walk_data *walk_data)
267 walk_data->free_block_data.create (0);
268 walk_data->block_data_stack.create (0);
271 void
272 fini_walk_dominator_tree (struct dom_walk_data *walk_data)
274 if (walk_data->initialize_block_local_data)
276 while (walk_data->free_block_data.length () > 0)
277 free (walk_data->free_block_data.pop ());
280 walk_data->free_block_data.release ();
281 walk_data->block_data_stack.release ();