Fix DealII type problems.
[official-gcc/Ramakrishna.git] / gcc / domwalk.c
blob07764eb80474226e2d52d64e678c93a2e44cb24d
1 /* Generic dominator tree walker
2 Copyright (C) 2003, 2004, 2005, 2007, 2008 Free Software Foundation,
3 Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "basic-block.h"
27 #include "domwalk.h"
28 #include "ggc.h"
30 /* This file implements a generic walker for dominator trees.
32 To understand the dominator walker one must first have a grasp of dominators,
33 immediate dominators and the dominator tree.
35 Dominators
36 A block B1 is said to dominate B2 if every path from the entry to B2 must
37 pass through B1. Given the dominance relationship, we can proceed to
38 compute immediate dominators. Note it is not important whether or not
39 our definition allows a block to dominate itself.
41 Immediate Dominators:
42 Every block in the CFG has no more than one immediate dominator. The
43 immediate dominator of block BB must dominate BB and must not dominate
44 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.
52 [ Note this walker can also walk the post-dominator tree, which is
53 defined in a similar manner. i.e., block B1 is said to post-dominate
54 block B2 if all paths from B2 to the exit block must pass through
55 B1. ]
57 For example, given the CFG
62 / \
63 3 4
64 / \
65 +---------->5 6
66 | / \ /
67 | +--->8 7
68 | | / |
69 | +--9 11
70 | / |
71 +--- 10 ---> 12
74 We have a dominator tree which looks like
79 / \
80 / \
81 3 4
82 / / \ \
83 | | | |
84 5 6 7 12
85 | |
86 8 11
94 The dominator tree is the basis for a number of analysis, transformation
95 and optimization algorithms that operate on a semi-global basis.
97 The dominator walker is a generic routine which visits blocks in the CFG
98 via a depth first search of the dominator tree. In the example above
99 the dominator walker might visit blocks in the following order
100 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12.
102 The dominator walker has a number of callbacks to perform actions
103 during the walk of the dominator tree. There are two callbacks
104 which walk statements, one before visiting the dominator children,
105 one after visiting the dominator children. There is a callback
106 before and after each statement walk callback. In addition, the
107 dominator walker manages allocation/deallocation of data structures
108 which are local to each block visited.
110 The dominator walker is meant to provide a generic means to build a pass
111 which can analyze or transform/optimize a function based on walking
112 the dominator tree. One simply fills in the dominator walker data
113 structure with the appropriate callbacks and calls the walker.
115 We currently use the dominator walker to prune the set of variables
116 which might need PHI nodes (which can greatly improve compile-time
117 performance in some cases).
119 We also use the dominator walker to rewrite the function into SSA form
120 which reduces code duplication since the rewriting phase is inherently
121 a walk of the dominator tree.
123 And (of course), we use the dominator walker to drive our dominator
124 optimizer, which is a semi-global optimizer.
126 TODO:
128 Walking statements is based on the block statement iterator abstraction,
129 which is currently an abstraction over walking tree statements. Thus
130 the dominator walker is currently only useful for trees. */
132 /* Recursively walk the dominator tree.
134 WALK_DATA contains a set of callbacks to perform pass-specific
135 actions during the dominator walk as well as a stack of block local
136 data maintained during the dominator walk.
138 BB is the basic block we are currently visiting. */
140 void
141 walk_dominator_tree (struct dom_walk_data *walk_data, basic_block bb)
143 void *bd = NULL;
144 basic_block dest;
145 basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks * 2);
146 int sp = 0;
148 while (true)
150 /* Don't worry about unreachable blocks. */
151 if (EDGE_COUNT (bb->preds) > 0
152 || bb == ENTRY_BLOCK_PTR
153 || bb == EXIT_BLOCK_PTR)
155 /* Callback to initialize the local data structure. */
156 if (walk_data->initialize_block_local_data)
158 bool recycled;
160 /* First get some local data, reusing any local data
161 pointer we may have saved. */
162 if (VEC_length (void_p, walk_data->free_block_data) > 0)
164 bd = VEC_pop (void_p, walk_data->free_block_data);
165 recycled = 1;
167 else
169 bd = xcalloc (1, walk_data->block_local_data_size);
170 recycled = 0;
173 /* Push the local data into the local data stack. */
174 VEC_safe_push (void_p, heap, walk_data->block_data_stack, bd);
176 /* Call the initializer. */
177 walk_data->initialize_block_local_data (walk_data, bb,
178 recycled);
182 /* Callback for operations to execute before we have walked the
183 dominator children, but before we walk statements. */
184 if (walk_data->before_dom_children)
185 (*walk_data->before_dom_children) (walk_data, bb);
187 /* Mark the current BB to be popped out of the recursion stack
188 once children are processed. */
189 worklist[sp++] = bb;
190 worklist[sp++] = NULL;
192 for (dest = first_dom_son (walk_data->dom_direction, bb);
193 dest; dest = next_dom_son (walk_data->dom_direction, dest))
194 worklist[sp++] = dest;
196 /* NULL is used to mark pop operations in the recursion stack. */
197 while (sp > 0 && !worklist[sp - 1])
199 --sp;
200 bb = worklist[--sp];
202 /* Callback for operations to execute after we have walked the
203 dominator children, but before we walk statements. */
204 if (walk_data->after_dom_children)
205 (*walk_data->after_dom_children) (walk_data, bb);
207 if (walk_data->initialize_block_local_data)
209 /* And finally pop the record off the block local data stack. */
210 bd = VEC_pop (void_p, walk_data->block_data_stack);
211 /* And save the block data so that we can re-use it. */
212 VEC_safe_push (void_p, heap, walk_data->free_block_data, bd);
215 if (sp)
216 bb = worklist[--sp];
217 else
218 break;
220 free (worklist);
223 void
224 init_walk_dominator_tree (struct dom_walk_data *walk_data)
226 walk_data->free_block_data = NULL;
227 walk_data->block_data_stack = NULL;
230 void
231 fini_walk_dominator_tree (struct dom_walk_data *walk_data)
233 if (walk_data->initialize_block_local_data)
235 while (VEC_length (void_p, walk_data->free_block_data) > 0)
236 free (VEC_pop (void_p, walk_data->free_block_data));
239 VEC_free (void_p, heap, walk_data->free_block_data);
240 VEC_free (void_p, heap, walk_data->block_data_stack);