00001 /* Generic dominator tree walker 00002 Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc. 00003 Contributed by Diego Novillo <dnovillo@redhat.com> 00004 00005 This file is part of GCC. 00006 00007 GCC is free software; you can redistribute it and/or modify 00008 it under the terms of the GNU General Public License as published by 00009 the Free Software Foundation; either version 2, or (at your option) 00010 any later version. 00011 00012 GCC is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU General Public License for more details. 00016 00017 You should have received a copy of the GNU General Public License 00018 along with GCC; see the file COPYING. If not, write to 00019 the Free Software Foundation, 51 Franklin Street, Fifth Floor, 00020 Boston, MA 02110-1301, USA. */ 00021 00022 #include "config.h" 00023 #include "system.h" 00024 #include "coretypes.h" 00025 #include "tm.h" 00026 #include "tree.h" 00027 #include "basic-block.h" 00028 #include "tree-flow.h" 00029 #include "domwalk.h" 00030 #include "ggc.h" 00031 00032 /* This file implements a generic walker for dominator trees. 00033 00034 To understand the dominator walker one must first have a grasp of dominators, 00035 immediate dominators and the dominator tree. 00036 00037 Dominators 00038 A block B1 is said to dominate B2 if every path from the entry to B2 must 00039 pass through B1. Given the dominance relationship, we can proceed to 00040 compute immediate dominators. Note it is not important whether or not 00041 our definition allows a block to dominate itself. 00042 00043 Immediate Dominators: 00044 Every block in the CFG has no more than one immediate dominator. The 00045 immediate dominator of block BB must dominate BB and must not dominate 00046 any other dominator of BB and must not be BB itself. 00047 00048 Dominator tree: 00049 If we then construct a tree where each node is a basic block and there 00050 is an edge from each block's immediate dominator to the block itself, then 00051 we have a dominator tree. 00052 00053 00054 [ Note this walker can also walk the post-dominator tree, which is 00055 defined in a similar manner. i.e., block B1 is said to post-dominate 00056 block B2 if all paths from B2 to the exit block must pass through 00057 B1. ] 00058 00059 For example, given the CFG 00060 00061 1 00062 | 00063 2 00064 / \ 00065 3 4 00066 / \ 00067 +---------->5 6 00068 | / \ / 00069 | +--->8 7 00070 | | / | 00071 | +--9 11 00072 | / | 00073 +--- 10 ---> 12 00074 00075 00076 We have a dominator tree which looks like 00077 00078 1 00079 | 00080 2 00081 / \ 00082 / \ 00083 3 4 00084 / / \ \ 00085 | | | | 00086 5 6 7 12 00087 | | 00088 8 11 00089 | 00090 9 00091 | 00092 10 00093 00094 00095 00096 The dominator tree is the basis for a number of analysis, transformation 00097 and optimization algorithms that operate on a semi-global basis. 00098 00099 The dominator walker is a generic routine which visits blocks in the CFG 00100 via a depth first search of the dominator tree. In the example above 00101 the dominator walker might visit blocks in the following order 00102 1, 2, 3, 4, 5, 8, 9, 10, 6, 7, 11, 12. 00103 00104 The dominator walker has a number of callbacks to perform actions 00105 during the walk of the dominator tree. There are two callbacks 00106 which walk statements, one before visiting the dominator children, 00107 one after visiting the dominator children. There is a callback 00108 before and after each statement walk callback. In addition, the 00109 dominator walker manages allocation/deallocation of data structures 00110 which are local to each block visited. 00111 00112 The dominator walker is meant to provide a generic means to build a pass 00113 which can analyze or transform/optimize a function based on walking 00114 the dominator tree. One simply fills in the dominator walker data 00115 structure with the appropriate callbacks and calls the walker. 00116 00117 We currently use the dominator walker to prune the set of variables 00118 which might need PHI nodes (which can greatly improve compile-time 00119 performance in some cases). 00120 00121 We also use the dominator walker to rewrite the function into SSA form 00122 which reduces code duplication since the rewriting phase is inherently 00123 a walk of the dominator tree. 00124 00125 And (of course), we use the dominator walker to drive a our dominator 00126 optimizer, which is a semi-global optimizer. 00127 00128 TODO: 00129 00130 Walking statements is based on the block statement iterator abstraction, 00131 which is currently an abstraction over walking tree statements. Thus 00132 the dominator walker is currently only useful for trees. */ 00133 00134 /* Recursively walk the dominator tree. 00135 00136 WALK_DATA contains a set of callbacks to perform pass-specific 00137 actions during the dominator walk as well as a stack of block local 00138 data maintained during the dominator walk. 00139 00140 BB is the basic block we are currently visiting. */ 00141 00142 void 00143 walk_dominator_tree (struct dom_walk_data *walk_data, basic_block bb) 00144 { 00145 void *bd = NULL; 00146 basic_block dest; 00147 block_stmt_iterator bsi; 00148 bool is_interesting; 00149 basic_block *worklist = XNEWVEC (basic_block, n_basic_blocks * 2); 00150 int sp = 0; 00151 00152 while (true) 00153 { 00154 /* Don't worry about unreachable blocks. */ 00155 if (EDGE_COUNT (bb->preds) > 0 || bb == ENTRY_BLOCK_PTR) 00156 { 00157 /* If block BB is not interesting to the caller, then none of the 00158 callbacks that walk the statements in BB are going to be 00159 executed. */ 00160 is_interesting = walk_data->interesting_blocks == NULL 00161 || TEST_BIT (walk_data->interesting_blocks, 00162 bb->index); 00163 00164 /* Callback to initialize the local data structure. */ 00165 if (walk_data->initialize_block_local_data) 00166 { 00167 bool recycled; 00168 00169 /* First get some local data, reusing any local data pointer we may 00170 have saved. */ 00171 if (VEC_length (void_p, walk_data->free_block_data) > 0) 00172 { 00173 bd = VEC_pop (void_p, walk_data->free_block_data); 00174 recycled = 1; 00175 } 00176 else 00177 { 00178 bd = xcalloc (1, walk_data->block_local_data_size); 00179 recycled = 0; 00180 } 00181 00182 /* Push the local data into the local data stack. */ 00183 VEC_safe_push (void_p, heap, walk_data->block_data_stack, bd); 00184 00185 /* Call the initializer. */ 00186 walk_data->initialize_block_local_data (walk_data, bb, 00187 recycled); 00188 00189 } 00190 00191 /* Callback for operations to execute before we have walked the 00192 dominator children, but before we walk statements. */ 00193 if (walk_data->before_dom_children_before_stmts) 00194 (*walk_data->before_dom_children_before_stmts) (walk_data, bb); 00195 00196 /* Statement walk before walking dominator children. */ 00197 if (is_interesting && walk_data->before_dom_children_walk_stmts) 00198 { 00199 if (walk_data->walk_stmts_backward) 00200 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi)) 00201 (*walk_data->before_dom_children_walk_stmts) (walk_data, bb, 00202 bsi); 00203 else 00204 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) 00205 (*walk_data->before_dom_children_walk_stmts) (walk_data, bb, 00206 bsi); 00207 } 00208 00209 /* Callback for operations to execute before we have walked the 00210 dominator children, and after we walk statements. */ 00211 if (walk_data->before_dom_children_after_stmts) 00212 (*walk_data->before_dom_children_after_stmts) (walk_data, bb); 00213 00214 /* Mark the current BB to be popped out of the recursion stack 00215 once childs are processed. */ 00216 worklist[sp++] = bb; 00217 worklist[sp++] = NULL; 00218 00219 for (dest = first_dom_son (walk_data->dom_direction, bb); 00220 dest; dest = next_dom_son (walk_data->dom_direction, dest)) 00221 worklist[sp++] = dest; 00222 } 00223 /* NULL is used to signalize pop operation in recursion stack. */ 00224 while (sp > 0 && !worklist[sp - 1]) 00225 { 00226 --sp; 00227 bb = worklist[--sp]; 00228 is_interesting = walk_data->interesting_blocks == NULL 00229 || TEST_BIT (walk_data->interesting_blocks, 00230 bb->index); 00231 /* Callback for operations to execute after we have walked the 00232 dominator children, but before we walk statements. */ 00233 if (walk_data->after_dom_children_before_stmts) 00234 (*walk_data->after_dom_children_before_stmts) (walk_data, bb); 00235 00236 /* Statement walk after walking dominator children. */ 00237 if (is_interesting && walk_data->after_dom_children_walk_stmts) 00238 { 00239 if (walk_data->walk_stmts_backward) 00240 for (bsi = bsi_last (bb); !bsi_end_p (bsi); bsi_prev (&bsi)) 00241 (*walk_data->after_dom_children_walk_stmts) (walk_data, bb, 00242 bsi); 00243 else 00244 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi)) 00245 (*walk_data->after_dom_children_walk_stmts) (walk_data, bb, 00246 bsi); 00247 } 00248 00249 /* Callback for operations to execute after we have walked the 00250 dominator children and after we have walked statements. */ 00251 if (walk_data->after_dom_children_after_stmts) 00252 (*walk_data->after_dom_children_after_stmts) (walk_data, bb); 00253 00254 if (walk_data->initialize_block_local_data) 00255 { 00256 /* And finally pop the record off the block local data stack. */ 00257 bd = VEC_pop (void_p, walk_data->block_data_stack); 00258 /* And save the block data so that we can re-use it. */ 00259 VEC_safe_push (void_p, heap, walk_data->free_block_data, bd); 00260 } 00261 } 00262 if (sp) 00263 bb = worklist[--sp]; 00264 else 00265 break; 00266 } 00267 free (worklist); 00268 } 00269 00270 void 00271 init_walk_dominator_tree (struct dom_walk_data *walk_data) 00272 { 00273 walk_data->free_block_data = NULL; 00274 walk_data->block_data_stack = NULL; 00275 } 00276 00277 void 00278 fini_walk_dominator_tree (struct dom_walk_data *walk_data) 00279 { 00280 if (walk_data->initialize_block_local_data) 00281 { 00282 while (VEC_length (void_p, walk_data->free_block_data) > 0) 00283 free (VEC_pop (void_p, walk_data->free_block_data)); 00284 } 00285 00286 VEC_free (void_p, heap, walk_data->free_block_data); 00287 VEC_free (void_p, heap, walk_data->block_data_stack); 00288 }
1.5.6