]>
Commit | Line | Data |
---|---|---|
6de9cd9a | 1 | /* Rewrite a program in Normal form into SSA. |
5624e564 | 2 | Copyright (C) 2001-2015 Free Software Foundation, Inc. |
6de9cd9a DN |
3 | Contributed by Diego Novillo <dnovillo@redhat.com> |
4 | ||
5 | This file is part of GCC. | |
6 | ||
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 | |
9dcd6f09 | 9 | the Free Software Foundation; either version 3, or (at your option) |
6de9cd9a DN |
10 | any later version. |
11 | ||
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. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9dcd6f09 NC |
18 | along with GCC; see the file COPYING3. If not see |
19 | <http://www.gnu.org/licenses/>. */ | |
6de9cd9a DN |
20 | |
21 | #include "config.h" | |
22 | #include "system.h" | |
23 | #include "coretypes.h" | |
24 | #include "tm.h" | |
40e23961 MC |
25 | #include "input.h" |
26 | #include "alias.h" | |
27 | #include "symtab.h" | |
6de9cd9a | 28 | #include "tree.h" |
40e23961 | 29 | #include "fold-const.h" |
6de9cd9a | 30 | #include "flags.h" |
6de9cd9a DN |
31 | #include "tm_p.h" |
32 | #include "langhooks.h" | |
60393bbc | 33 | #include "predict.h" |
83685514 | 34 | #include "hard-reg-set.h" |
6de9cd9a | 35 | #include "function.h" |
60393bbc AM |
36 | #include "dominance.h" |
37 | #include "cfg.h" | |
38 | #include "cfganal.h" | |
39 | #include "basic-block.h" | |
cf835838 | 40 | #include "gimple-pretty-print.h" |
2fb9a547 AM |
41 | #include "tree-ssa-alias.h" |
42 | #include "internal-fn.h" | |
43 | #include "gimple-expr.h" | |
44 | #include "is-a.h" | |
726a989a | 45 | #include "gimple.h" |
5be5c238 | 46 | #include "gimple-iterator.h" |
442b4905 AM |
47 | #include "gimple-ssa.h" |
48 | #include "tree-cfg.h" | |
49 | #include "tree-phinodes.h" | |
50 | #include "ssa-iterators.h" | |
d8a2d370 | 51 | #include "stringpool.h" |
442b4905 AM |
52 | #include "tree-ssanames.h" |
53 | #include "tree-into-ssa.h" | |
36566b39 | 54 | #include "rtl.h" |
36566b39 PK |
55 | #include "insn-config.h" |
56 | #include "expmed.h" | |
57 | #include "dojump.h" | |
58 | #include "explow.h" | |
59 | #include "calls.h" | |
60 | #include "emit-rtl.h" | |
61 | #include "varasm.h" | |
62 | #include "stmt.h" | |
d8a2d370 | 63 | #include "expr.h" |
442b4905 AM |
64 | #include "tree-dfa.h" |
65 | #include "tree-ssa.h" | |
6de9cd9a | 66 | #include "tree-inline.h" |
6de9cd9a DN |
67 | #include "tree-pass.h" |
68 | #include "cfgloop.h" | |
69 | #include "domwalk.h" | |
84d65814 | 70 | #include "params.h" |
d8d707b4 | 71 | #include "diagnostic-core.h" |
6de9cd9a | 72 | |
1fe37220 | 73 | #define PERCENT(x,y) ((float)(x) * 100.0 / (float)(y)) |
726a989a | 74 | |
6de9cd9a DN |
75 | /* This file builds the SSA form for a function as described in: |
76 | R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently | |
77 | Computing Static Single Assignment Form and the Control Dependence | |
78 | Graph. ACM Transactions on Programming Languages and Systems, | |
79 | 13(4):451-490, October 1991. */ | |
80 | ||
6de9cd9a DN |
81 | /* Structure to map a variable VAR to the set of blocks that contain |
82 | definitions for VAR. */ | |
83 | struct def_blocks_d | |
84 | { | |
6de9cd9a DN |
85 | /* Blocks that contain definitions of VAR. Bit I will be set if the |
86 | Ith block contains a definition of VAR. */ | |
87 | bitmap def_blocks; | |
88 | ||
7256233c | 89 | /* Blocks that contain a PHI node for VAR. */ |
5f240ec4 ZD |
90 | bitmap phi_blocks; |
91 | ||
6de9cd9a DN |
92 | /* Blocks where VAR is live-on-entry. Similar semantics as |
93 | DEF_BLOCKS. */ | |
94 | bitmap livein_blocks; | |
95 | }; | |
96 | ||
d9ed2fbd RG |
97 | typedef struct def_blocks_d *def_blocks_p; |
98 | ||
6de9cd9a | 99 | |
9fae925b | 100 | /* Stack of trees used to restore the global currdefs to its original |
0bca51f0 DN |
101 | state after completing rewriting of a block and its dominator |
102 | children. Its elements have the following properties: | |
9fae925b | 103 | |
38635499 DN |
104 | - An SSA_NAME (N) indicates that the current definition of the |
105 | underlying variable should be set to the given SSA_NAME. If the | |
106 | symbol associated with the SSA_NAME is not a GIMPLE register, the | |
107 | next slot in the stack must be a _DECL node (SYM). In this case, | |
108 | the name N in the previous slot is the current reaching | |
109 | definition for SYM. | |
9fae925b | 110 | |
0bca51f0 DN |
111 | - A _DECL node indicates that the underlying variable has no |
112 | current definition. | |
7256233c | 113 | |
38635499 | 114 | - A NULL node at the top entry is used to mark the last slot |
0bca51f0 | 115 | associated with the current block. */ |
9771b263 | 116 | static vec<tree> block_defs_stack; |
3a2e4b46 | 117 | |
726a989a | 118 | |
0bca51f0 DN |
119 | /* Set of existing SSA names being replaced by update_ssa. */ |
120 | static sbitmap old_ssa_names; | |
121 | ||
122 | /* Set of new SSA names being added by update_ssa. Note that both | |
123 | NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of | |
124 | the operations done on them are presence tests. */ | |
125 | static sbitmap new_ssa_names; | |
126 | ||
16876bdc | 127 | static sbitmap interesting_blocks; |
726a989a | 128 | |
0bca51f0 DN |
129 | /* Set of SSA names that have been marked to be released after they |
130 | were registered in the replacement table. They will be finally | |
131 | released after we finish updating the SSA web. */ | |
132 | static bitmap names_to_release; | |
133 | ||
9771b263 | 134 | /* vec of vec of PHIs to rewrite in a basic block. Element I corresponds |
580b2c2e SB |
135 | the to basic block with index I. Allocated once per compilation, *not* |
136 | released between different functions. */ | |
538dd0b7 | 137 | static vec< vec<gphi *> > phis_to_rewrite; |
2ce79879 ZD |
138 | |
139 | /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */ | |
2ce79879 ZD |
140 | static bitmap blocks_with_phis_to_rewrite; |
141 | ||
0bca51f0 | 142 | /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need |
45db3141 RG |
143 | to grow as the callers to create_new_def_for will create new names on |
144 | the fly. | |
145 | FIXME. Currently set to 1/3 to avoid frequent reallocations but still | |
146 | need to find a reasonable growth strategy. */ | |
0bca51f0 DN |
147 | #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3)) |
148 | ||
0bca51f0 | 149 | |
5006671f | 150 | /* The function the SSA updating data structures have been initialized for. |
45db3141 | 151 | NULL if they need to be initialized by create_new_def_for. */ |
5006671f | 152 | static struct function *update_ssa_initialized_fn = NULL; |
0bca51f0 | 153 | |
6de9cd9a DN |
154 | /* Global data to attach to the main dominator walk structure. */ |
155 | struct mark_def_sites_global_data | |
156 | { | |
0bca51f0 DN |
157 | /* This bitmap contains the variables which are set before they |
158 | are used in a basic block. */ | |
7d793e36 | 159 | bitmap kills; |
6de9cd9a DN |
160 | }; |
161 | ||
80560f95 AM |
162 | /* It is advantageous to avoid things like life analysis for variables which |
163 | do not need PHI nodes. This enum describes whether or not a particular | |
164 | variable may need a PHI node. */ | |
165 | ||
166 | enum need_phi_state { | |
167 | /* This is the default. If we are still in this state after finding | |
168 | all the definition and use sites, then we will assume the variable | |
169 | needs PHI nodes. This is probably an overly conservative assumption. */ | |
170 | NEED_PHI_STATE_UNKNOWN, | |
171 | ||
172 | /* This state indicates that we have seen one or more sets of the | |
173 | variable in a single basic block and that the sets dominate all | |
174 | uses seen so far. If after finding all definition and use sites | |
175 | we are still in this state, then the variable does not need any | |
176 | PHI nodes. */ | |
177 | NEED_PHI_STATE_NO, | |
178 | ||
179 | /* This state indicates that we have either seen multiple definitions of | |
180 | the variable in multiple blocks, or that we encountered a use in a | |
181 | block that was not dominated by the block containing the set(s) of | |
182 | this variable. This variable is assumed to need PHI nodes. */ | |
183 | NEED_PHI_STATE_MAYBE | |
184 | }; | |
185 | ||
8812aab1 RG |
186 | /* Information stored for both SSA names and decls. */ |
187 | struct common_info_d | |
b4e209fd | 188 | { |
b4e209fd RG |
189 | /* This field indicates whether or not the variable may need PHI nodes. |
190 | See the enum's definition for more detailed information about the | |
191 | states. */ | |
192 | ENUM_BITFIELD (need_phi_state) need_phi_state : 2; | |
193 | ||
8812aab1 | 194 | /* The current reaching definition replacing this var. */ |
b4e209fd RG |
195 | tree current_def; |
196 | ||
8812aab1 | 197 | /* Definitions for this var. */ |
b4e209fd RG |
198 | struct def_blocks_d def_blocks; |
199 | }; | |
200 | ||
8812aab1 RG |
201 | /* The information associated with decls and SSA names. */ |
202 | typedef struct common_info_d *common_info_p; | |
203 | ||
204 | /* Information stored for decls. */ | |
205 | struct var_info_d | |
206 | { | |
207 | /* The variable. */ | |
208 | tree var; | |
209 | ||
210 | /* Information stored for both SSA names and decls. */ | |
211 | struct common_info_d info; | |
212 | }; | |
213 | ||
b4e209fd RG |
214 | /* The information associated with decls. */ |
215 | typedef struct var_info_d *var_info_p; | |
216 | ||
b4e209fd | 217 | |
bf190e8d LC |
218 | /* VAR_INFOS hashtable helpers. */ |
219 | ||
220 | struct var_info_hasher : typed_free_remove <var_info_d> | |
221 | { | |
84baa4b9 TS |
222 | typedef var_info_d *value_type; |
223 | typedef var_info_d *compare_type; | |
84baa4b9 TS |
224 | static inline hashval_t hash (const value_type &); |
225 | static inline bool equal (const value_type &, const compare_type &); | |
bf190e8d LC |
226 | }; |
227 | ||
228 | inline hashval_t | |
84baa4b9 | 229 | var_info_hasher::hash (const value_type &p) |
bf190e8d LC |
230 | { |
231 | return DECL_UID (p->var); | |
232 | } | |
233 | ||
234 | inline bool | |
84baa4b9 | 235 | var_info_hasher::equal (const value_type &p1, const compare_type &p2) |
bf190e8d LC |
236 | { |
237 | return p1->var == p2->var; | |
238 | } | |
239 | ||
240 | ||
b4e209fd RG |
241 | /* Each entry in VAR_INFOS contains an element of type STRUCT |
242 | VAR_INFO_D. */ | |
c203e8a7 | 243 | static hash_table<var_info_hasher> *var_infos; |
b4e209fd RG |
244 | |
245 | ||
0bca51f0 | 246 | /* Information stored for SSA names. */ |
5f240ec4 ZD |
247 | struct ssa_name_info |
248 | { | |
891f2df6 RG |
249 | /* Age of this record (so that info_for_ssa_name table can be cleared |
250 | quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields | |
251 | are assumed to be null. */ | |
252 | unsigned age; | |
95dd3097 | 253 | |
891f2df6 RG |
254 | /* Replacement mappings, allocated from update_ssa_obstack. */ |
255 | bitmap repl_set; | |
b4e209fd | 256 | |
8812aab1 RG |
257 | /* Information stored for both SSA names and decls. */ |
258 | struct common_info_d info; | |
5f240ec4 | 259 | }; |
6de9cd9a | 260 | |
95dd3097 ZD |
261 | /* The information associated with names. */ |
262 | typedef struct ssa_name_info *ssa_name_info_p; | |
95dd3097 | 263 | |
9771b263 | 264 | static vec<ssa_name_info_p> info_for_ssa_name; |
95dd3097 ZD |
265 | static unsigned current_info_for_ssa_name_age; |
266 | ||
891f2df6 RG |
267 | static bitmap_obstack update_ssa_obstack; |
268 | ||
95dd3097 | 269 | /* The set of blocks affected by update_ssa. */ |
95dd3097 | 270 | static bitmap blocks_to_update; |
6de9cd9a | 271 | |
0bca51f0 DN |
272 | /* The main entry point to the SSA renamer (rewrite_blocks) may be |
273 | called several times to do different, but related, tasks. | |
274 | Initially, we need it to rename the whole program into SSA form. | |
275 | At other times, we may need it to only rename into SSA newly | |
276 | exposed symbols. Finally, we can also call it to incrementally fix | |
277 | an already built SSA web. */ | |
278 | enum rewrite_mode { | |
279 | /* Convert the whole function into SSA form. */ | |
280 | REWRITE_ALL, | |
281 | ||
282 | /* Incrementally update the SSA web by replacing existing SSA | |
283 | names with new ones. See update_ssa for details. */ | |
284 | REWRITE_UPDATE | |
285 | }; | |
286 | ||
13714310 RG |
287 | /* The set of symbols we ought to re-write into SSA form in update_ssa. */ |
288 | static bitmap symbols_to_rename_set; | |
9771b263 | 289 | static vec<tree> symbols_to_rename; |
13714310 RG |
290 | |
291 | /* Mark SYM for renaming. */ | |
292 | ||
293 | static void | |
294 | mark_for_renaming (tree sym) | |
295 | { | |
296 | if (!symbols_to_rename_set) | |
297 | symbols_to_rename_set = BITMAP_ALLOC (NULL); | |
298 | if (bitmap_set_bit (symbols_to_rename_set, DECL_UID (sym))) | |
9771b263 | 299 | symbols_to_rename.safe_push (sym); |
13714310 RG |
300 | } |
301 | ||
302 | /* Return true if SYM is marked for renaming. */ | |
303 | ||
304 | static bool | |
305 | marked_for_renaming (tree sym) | |
306 | { | |
70b5e7dc | 307 | if (!symbols_to_rename_set || sym == NULL_TREE) |
13714310 RG |
308 | return false; |
309 | return bitmap_bit_p (symbols_to_rename_set, DECL_UID (sym)); | |
310 | } | |
311 | ||
312 | ||
726a989a RB |
313 | /* Return true if STMT needs to be rewritten. When renaming a subset |
314 | of the variables, not all statements will be processed. This is | |
315 | decided in mark_def_sites. */ | |
316 | ||
317 | static inline bool | |
318 | rewrite_uses_p (gimple stmt) | |
319 | { | |
320 | return gimple_visited_p (stmt); | |
321 | } | |
322 | ||
323 | ||
324 | /* Set the rewrite marker on STMT to the value given by REWRITE_P. */ | |
325 | ||
326 | static inline void | |
327 | set_rewrite_uses (gimple stmt, bool rewrite_p) | |
328 | { | |
329 | gimple_set_visited (stmt, rewrite_p); | |
330 | } | |
331 | ||
332 | ||
333 | /* Return true if the DEFs created by statement STMT should be | |
334 | registered when marking new definition sites. This is slightly | |
335 | different than rewrite_uses_p: it's used by update_ssa to | |
336 | distinguish statements that need to have both uses and defs | |
337 | processed from those that only need to have their defs processed. | |
338 | Statements that define new SSA names only need to have their defs | |
339 | registered, but they don't need to have their uses renamed. */ | |
340 | ||
341 | static inline bool | |
342 | register_defs_p (gimple stmt) | |
343 | { | |
344 | return gimple_plf (stmt, GF_PLF_1) != 0; | |
345 | } | |
346 | ||
347 | ||
348 | /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */ | |
349 | ||
350 | static inline void | |
351 | set_register_defs (gimple stmt, bool register_defs_p) | |
352 | { | |
353 | gimple_set_plf (stmt, GF_PLF_1, register_defs_p); | |
354 | } | |
355 | ||
356 | ||
5f240ec4 ZD |
357 | /* Get the information associated with NAME. */ |
358 | ||
38635499 | 359 | static inline ssa_name_info_p |
5f240ec4 ZD |
360 | get_ssa_name_ann (tree name) |
361 | { | |
95dd3097 | 362 | unsigned ver = SSA_NAME_VERSION (name); |
9771b263 | 363 | unsigned len = info_for_ssa_name.length (); |
95dd3097 ZD |
364 | struct ssa_name_info *info; |
365 | ||
f5843d08 | 366 | /* Re-allocate the vector at most once per update/into-SSA. */ |
95dd3097 | 367 | if (ver >= len) |
9771b263 | 368 | info_for_ssa_name.safe_grow_cleared (num_ssa_names); |
f5843d08 RG |
369 | |
370 | /* But allocate infos lazily. */ | |
9771b263 | 371 | info = info_for_ssa_name[ver]; |
f5843d08 | 372 | if (!info) |
95dd3097 | 373 | { |
f5843d08 RG |
374 | info = XCNEW (struct ssa_name_info); |
375 | info->age = current_info_for_ssa_name_age; | |
376 | info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN; | |
9771b263 | 377 | info_for_ssa_name[ver] = info; |
95dd3097 ZD |
378 | } |
379 | ||
95dd3097 ZD |
380 | if (info->age < current_info_for_ssa_name_age) |
381 | { | |
95dd3097 | 382 | info->age = current_info_for_ssa_name_age; |
8812aab1 RG |
383 | info->repl_set = NULL; |
384 | info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN; | |
385 | info->info.current_def = NULL_TREE; | |
386 | info->info.def_blocks.def_blocks = NULL; | |
387 | info->info.def_blocks.phi_blocks = NULL; | |
388 | info->info.def_blocks.livein_blocks = NULL; | |
95dd3097 | 389 | } |
5f240ec4 | 390 | |
95dd3097 | 391 | return info; |
5f240ec4 ZD |
392 | } |
393 | ||
b4e209fd RG |
394 | /* Return and allocate the auxiliar information for DECL. */ |
395 | ||
396 | static inline var_info_p | |
397 | get_var_info (tree decl) | |
398 | { | |
399 | struct var_info_d vi; | |
bf190e8d | 400 | var_info_d **slot; |
b4e209fd | 401 | vi.var = decl; |
c203e8a7 | 402 | slot = var_infos->find_slot_with_hash (&vi, DECL_UID (decl), INSERT); |
b4e209fd RG |
403 | if (*slot == NULL) |
404 | { | |
405 | var_info_p v = XCNEW (struct var_info_d); | |
406 | v->var = decl; | |
bf190e8d | 407 | *slot = v; |
b4e209fd RG |
408 | return v; |
409 | } | |
bf190e8d | 410 | return *slot; |
b4e209fd RG |
411 | } |
412 | ||
38635499 DN |
413 | |
414 | /* Clears info for SSA names. */ | |
95dd3097 ZD |
415 | |
416 | static void | |
417 | clear_ssa_name_info (void) | |
418 | { | |
419 | current_info_for_ssa_name_age++; | |
891f2df6 RG |
420 | |
421 | /* If current_info_for_ssa_name_age wraps we use stale information. | |
422 | Asser that this does not happen. */ | |
423 | gcc_assert (current_info_for_ssa_name_age != 0); | |
95dd3097 | 424 | } |
7256233c | 425 | |
38635499 | 426 | |
8812aab1 | 427 | /* Get access to the auxiliar information stored per SSA name or decl. */ |
5f240ec4 | 428 | |
8812aab1 RG |
429 | static inline common_info_p |
430 | get_common_info (tree var) | |
5f240ec4 ZD |
431 | { |
432 | if (TREE_CODE (var) == SSA_NAME) | |
8812aab1 | 433 | return &get_ssa_name_ann (var)->info; |
5f240ec4 | 434 | else |
8812aab1 | 435 | return &get_var_info (var)->info; |
5f240ec4 ZD |
436 | } |
437 | ||
7256233c | 438 | |
5f240ec4 ZD |
439 | /* Return the current definition for VAR. */ |
440 | ||
84d65814 | 441 | tree |
5f240ec4 ZD |
442 | get_current_def (tree var) |
443 | { | |
8812aab1 | 444 | return get_common_info (var)->current_def; |
5f240ec4 ZD |
445 | } |
446 | ||
7256233c | 447 | |
5f240ec4 ZD |
448 | /* Sets current definition of VAR to DEF. */ |
449 | ||
84d65814 | 450 | void |
5f240ec4 ZD |
451 | set_current_def (tree var, tree def) |
452 | { | |
8812aab1 | 453 | get_common_info (var)->current_def = def; |
5f240ec4 ZD |
454 | } |
455 | ||
95dd3097 ZD |
456 | /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for |
457 | all statements in basic block BB. */ | |
458 | ||
459 | static void | |
460 | initialize_flags_in_bb (basic_block bb) | |
461 | { | |
726a989a RB |
462 | gimple stmt; |
463 | gimple_stmt_iterator gsi; | |
95dd3097 | 464 | |
726a989a | 465 | for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
95dd3097 | 466 | { |
726a989a RB |
467 | gimple phi = gsi_stmt (gsi); |
468 | set_rewrite_uses (phi, false); | |
469 | set_register_defs (phi, false); | |
95dd3097 ZD |
470 | } |
471 | ||
726a989a | 472 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
95dd3097 | 473 | { |
726a989a RB |
474 | stmt = gsi_stmt (gsi); |
475 | ||
95dd3097 ZD |
476 | /* We are going to use the operand cache API, such as |
477 | SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand | |
478 | cache for each statement should be up-to-date. */ | |
4ad14919 | 479 | gcc_checking_assert (!gimple_modified_p (stmt)); |
726a989a RB |
480 | set_rewrite_uses (stmt, false); |
481 | set_register_defs (stmt, false); | |
95dd3097 ZD |
482 | } |
483 | } | |
484 | ||
485 | /* Mark block BB as interesting for update_ssa. */ | |
486 | ||
487 | static void | |
488 | mark_block_for_update (basic_block bb) | |
489 | { | |
4ad14919 | 490 | gcc_checking_assert (blocks_to_update != NULL); |
f3cf730b | 491 | if (!bitmap_set_bit (blocks_to_update, bb->index)) |
95dd3097 | 492 | return; |
95dd3097 ZD |
493 | initialize_flags_in_bb (bb); |
494 | } | |
495 | ||
7256233c DN |
496 | /* Return the set of blocks where variable VAR is defined and the blocks |
497 | where VAR is live on entry (livein). If no entry is found in | |
498 | DEF_BLOCKS, a new one is created and returned. */ | |
6de9cd9a | 499 | |
7256233c | 500 | static inline struct def_blocks_d * |
8812aab1 | 501 | get_def_blocks_for (common_info_p info) |
6de9cd9a | 502 | { |
8812aab1 | 503 | struct def_blocks_d *db_p = &info->def_blocks; |
b4e209fd | 504 | if (!db_p->def_blocks) |
6de9cd9a | 505 | { |
b4e209fd RG |
506 | db_p->def_blocks = BITMAP_ALLOC (&update_ssa_obstack); |
507 | db_p->phi_blocks = BITMAP_ALLOC (&update_ssa_obstack); | |
508 | db_p->livein_blocks = BITMAP_ALLOC (&update_ssa_obstack); | |
a32b97a2 BB |
509 | } |
510 | ||
7256233c | 511 | return db_p; |
6de9cd9a DN |
512 | } |
513 | ||
7d5f9cc6 | 514 | |
5f240ec4 | 515 | /* Mark block BB as the definition site for variable VAR. PHI_P is true if |
0bca51f0 | 516 | VAR is defined by a PHI node. */ |
6de9cd9a DN |
517 | |
518 | static void | |
0bca51f0 | 519 | set_def_block (tree var, basic_block bb, bool phi_p) |
6de9cd9a DN |
520 | { |
521 | struct def_blocks_d *db_p; | |
8812aab1 | 522 | common_info_p info; |
34eb8991 | 523 | |
8812aab1 RG |
524 | info = get_common_info (var); |
525 | db_p = get_def_blocks_for (info); | |
6de9cd9a DN |
526 | |
527 | /* Set the bit corresponding to the block where VAR is defined. */ | |
528 | bitmap_set_bit (db_p->def_blocks, bb->index); | |
5f240ec4 ZD |
529 | if (phi_p) |
530 | bitmap_set_bit (db_p->phi_blocks, bb->index); | |
6de9cd9a | 531 | |
7256233c | 532 | /* Keep track of whether or not we may need to insert PHI nodes. |
6de9cd9a DN |
533 | |
534 | If we are in the UNKNOWN state, then this is the first definition | |
535 | of VAR. Additionally, we have not seen any uses of VAR yet, so | |
7256233c | 536 | we do not need a PHI node for this variable at this time (i.e., |
6de9cd9a DN |
537 | transition to NEED_PHI_STATE_NO). |
538 | ||
539 | If we are in any other state, then we either have multiple definitions | |
540 | of this variable occurring in different blocks or we saw a use of the | |
541 | variable which was not dominated by the block containing the | |
542 | definition(s). In this case we may need a PHI node, so enter | |
543 | state NEED_PHI_STATE_MAYBE. */ | |
8812aab1 RG |
544 | if (info->need_phi_state == NEED_PHI_STATE_UNKNOWN) |
545 | info->need_phi_state = NEED_PHI_STATE_NO; | |
6de9cd9a | 546 | else |
8812aab1 | 547 | info->need_phi_state = NEED_PHI_STATE_MAYBE; |
6de9cd9a DN |
548 | } |
549 | ||
550 | ||
551 | /* Mark block BB as having VAR live at the entry to BB. */ | |
552 | ||
553 | static void | |
554 | set_livein_block (tree var, basic_block bb) | |
555 | { | |
8812aab1 | 556 | common_info_p info; |
6de9cd9a | 557 | struct def_blocks_d *db_p; |
6de9cd9a | 558 | |
8812aab1 RG |
559 | info = get_common_info (var); |
560 | db_p = get_def_blocks_for (info); | |
6de9cd9a DN |
561 | |
562 | /* Set the bit corresponding to the block where VAR is live in. */ | |
563 | bitmap_set_bit (db_p->livein_blocks, bb->index); | |
564 | ||
7256233c | 565 | /* Keep track of whether or not we may need to insert PHI nodes. |
6de9cd9a DN |
566 | |
567 | If we reach here in NEED_PHI_STATE_NO, see if this use is dominated | |
568 | by the single block containing the definition(s) of this variable. If | |
569 | it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to | |
570 | NEED_PHI_STATE_MAYBE. */ | |
8812aab1 | 571 | if (info->need_phi_state == NEED_PHI_STATE_NO) |
6de9cd9a DN |
572 | { |
573 | int def_block_index = bitmap_first_set_bit (db_p->def_blocks); | |
574 | ||
575 | if (def_block_index == -1 | |
576 | || ! dominated_by_p (CDI_DOMINATORS, bb, | |
06e28de2 | 577 | BASIC_BLOCK_FOR_FN (cfun, def_block_index))) |
8812aab1 | 578 | info->need_phi_state = NEED_PHI_STATE_MAYBE; |
6de9cd9a DN |
579 | } |
580 | else | |
8812aab1 | 581 | info->need_phi_state = NEED_PHI_STATE_MAYBE; |
6de9cd9a DN |
582 | } |
583 | ||
584 | ||
0bca51f0 | 585 | /* Return true if NAME is in OLD_SSA_NAMES. */ |
6de9cd9a | 586 | |
0bca51f0 DN |
587 | static inline bool |
588 | is_old_name (tree name) | |
589 | { | |
84d65814 | 590 | unsigned ver = SSA_NAME_VERSION (name); |
c36eadbe | 591 | if (!old_ssa_names) |
5006671f | 592 | return false; |
c36eadbe | 593 | return (ver < SBITMAP_SIZE (old_ssa_names) |
d7c028c0 | 594 | && bitmap_bit_p (old_ssa_names, ver)); |
0bca51f0 DN |
595 | } |
596 | ||
597 | ||
598 | /* Return true if NAME is in NEW_SSA_NAMES. */ | |
34eb8991 | 599 | |
0bca51f0 DN |
600 | static inline bool |
601 | is_new_name (tree name) | |
602 | { | |
84d65814 | 603 | unsigned ver = SSA_NAME_VERSION (name); |
5006671f RG |
604 | if (!new_ssa_names) |
605 | return false; | |
5829cc0f | 606 | return (ver < SBITMAP_SIZE (new_ssa_names) |
d7c028c0 | 607 | && bitmap_bit_p (new_ssa_names, ver)); |
6de9cd9a DN |
608 | } |
609 | ||
7256233c | 610 | |
82d6e6fc | 611 | /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */ |
0bca51f0 DN |
612 | |
613 | static inline bitmap | |
82d6e6fc | 614 | names_replaced_by (tree new_tree) |
0bca51f0 | 615 | { |
891f2df6 | 616 | return get_ssa_name_ann (new_tree)->repl_set; |
0bca51f0 DN |
617 | } |
618 | ||
619 | ||
82d6e6fc | 620 | /* Add OLD to REPL_TBL[NEW_TREE].SET. */ |
0bca51f0 DN |
621 | |
622 | static inline void | |
82d6e6fc | 623 | add_to_repl_tbl (tree new_tree, tree old) |
0bca51f0 | 624 | { |
891f2df6 RG |
625 | bitmap *set = &get_ssa_name_ann (new_tree)->repl_set; |
626 | if (!*set) | |
627 | *set = BITMAP_ALLOC (&update_ssa_obstack); | |
628 | bitmap_set_bit (*set, SSA_NAME_VERSION (old)); | |
0bca51f0 DN |
629 | } |
630 | ||
631 | ||
82d6e6fc | 632 | /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL |
0bca51f0 DN |
633 | represents the set of names O_1 ... O_j replaced by N_i. This is |
634 | used by update_ssa and its helpers to introduce new SSA names in an | |
635 | already formed SSA web. */ | |
636 | ||
637 | static void | |
82d6e6fc | 638 | add_new_name_mapping (tree new_tree, tree old) |
0bca51f0 | 639 | { |
82d6e6fc | 640 | /* OLD and NEW_TREE must be different SSA names for the same symbol. */ |
4ad14919 RG |
641 | gcc_checking_assert (new_tree != old |
642 | && SSA_NAME_VAR (new_tree) == SSA_NAME_VAR (old)); | |
84d65814 | 643 | |
38635499 DN |
644 | /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our |
645 | caller may have created new names since the set was created. */ | |
5829cc0f | 646 | if (SBITMAP_SIZE (new_ssa_names) <= num_ssa_names - 1) |
38635499 DN |
647 | { |
648 | unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR; | |
649 | new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0); | |
650 | old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0); | |
0bca51f0 DN |
651 | } |
652 | ||
0bca51f0 | 653 | /* Update the REPL_TBL table. */ |
82d6e6fc | 654 | add_to_repl_tbl (new_tree, old); |
d00ad49b | 655 | |
0bca51f0 | 656 | /* If OLD had already been registered as a new name, then all the |
82d6e6fc | 657 | names that OLD replaces should also be replaced by NEW_TREE. */ |
0bca51f0 | 658 | if (is_new_name (old)) |
82d6e6fc | 659 | bitmap_ior_into (names_replaced_by (new_tree), names_replaced_by (old)); |
0bca51f0 | 660 | |
82d6e6fc | 661 | /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES, |
0bca51f0 | 662 | respectively. */ |
d7c028c0 LC |
663 | bitmap_set_bit (new_ssa_names, SSA_NAME_VERSION (new_tree)); |
664 | bitmap_set_bit (old_ssa_names, SSA_NAME_VERSION (old)); | |
d00ad49b | 665 | } |
6de9cd9a | 666 | |
6de9cd9a | 667 | |
7256233c DN |
668 | /* Call back for walk_dominator_tree used to collect definition sites |
669 | for every variable in the function. For every statement S in block | |
670 | BB: | |
6de9cd9a | 671 | |
f47c96aa | 672 | 1- Variables defined by S in the DEFS of S are marked in the bitmap |
ccf5c864 | 673 | KILLS. |
6de9cd9a | 674 | |
7256233c | 675 | 2- If S uses a variable VAR and there is no preceding kill of VAR, |
7b71de26 | 676 | then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR. |
6de9cd9a | 677 | |
7256233c DN |
678 | This information is used to determine which variables are live |
679 | across block boundaries to reduce the number of PHI nodes | |
680 | we create. */ | |
6de9cd9a | 681 | |
7256233c | 682 | static void |
ccf5c864 | 683 | mark_def_sites (basic_block bb, gimple stmt, bitmap kills) |
7256233c | 684 | { |
726a989a | 685 | tree def; |
7256233c | 686 | use_operand_p use_p; |
7256233c DN |
687 | ssa_op_iter iter; |
688 | ||
726a989a RB |
689 | /* Since this is the first time that we rewrite the program into SSA |
690 | form, force an operand scan on every statement. */ | |
726a989a | 691 | update_stmt (stmt); |
7256233c | 692 | |
4ad14919 | 693 | gcc_checking_assert (blocks_to_update == NULL); |
726a989a RB |
694 | set_register_defs (stmt, false); |
695 | set_rewrite_uses (stmt, false); | |
7256233c | 696 | |
b5b8b0ac | 697 | if (is_gimple_debug (stmt)) |
ddb555ed JJ |
698 | { |
699 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE) | |
700 | { | |
701 | tree sym = USE_FROM_PTR (use_p); | |
4ad14919 | 702 | gcc_checking_assert (DECL_P (sym)); |
ddb555ed JJ |
703 | set_rewrite_uses (stmt, true); |
704 | } | |
705 | if (rewrite_uses_p (stmt)) | |
d7c028c0 | 706 | bitmap_set_bit (interesting_blocks, bb->index); |
ddb555ed JJ |
707 | return; |
708 | } | |
b5b8b0ac | 709 | |
7256233c DN |
710 | /* If a variable is used before being set, then the variable is live |
711 | across a block boundary, so mark it live-on-entry to BB. */ | |
39c58b3a | 712 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES) |
7256233c | 713 | { |
0bca51f0 | 714 | tree sym = USE_FROM_PTR (use_p); |
4ad14919 | 715 | gcc_checking_assert (DECL_P (sym)); |
a3648cfc | 716 | if (!bitmap_bit_p (kills, DECL_UID (sym))) |
0bca51f0 | 717 | set_livein_block (sym, bb); |
726a989a | 718 | set_rewrite_uses (stmt, true); |
7256233c | 719 | } |
b8698a0f | 720 | |
38635499 DN |
721 | /* Now process the defs. Mark BB as the definition block and add |
722 | each def to the set of killed symbols. */ | |
39c58b3a | 723 | FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS) |
7256233c | 724 | { |
4ad14919 | 725 | gcc_checking_assert (DECL_P (def)); |
0bca51f0 | 726 | set_def_block (def, bb, false); |
a3648cfc | 727 | bitmap_set_bit (kills, DECL_UID (def)); |
726a989a | 728 | set_register_defs (stmt, true); |
7256233c | 729 | } |
0bca51f0 DN |
730 | |
731 | /* If we found the statement interesting then also mark the block BB | |
732 | as interesting. */ | |
726a989a | 733 | if (rewrite_uses_p (stmt) || register_defs_p (stmt)) |
d7c028c0 | 734 | bitmap_set_bit (interesting_blocks, bb->index); |
7256233c DN |
735 | } |
736 | ||
f074ff6c ZD |
737 | /* Structure used by prune_unused_phi_nodes to record bounds of the intervals |
738 | in the dfs numbering of the dominance tree. */ | |
739 | ||
740 | struct dom_dfsnum | |
741 | { | |
742 | /* Basic block whose index this entry corresponds to. */ | |
743 | unsigned bb_index; | |
744 | ||
745 | /* The dfs number of this node. */ | |
746 | unsigned dfs_num; | |
747 | }; | |
748 | ||
749 | /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback | |
750 | for qsort. */ | |
751 | ||
752 | static int | |
753 | cmp_dfsnum (const void *a, const void *b) | |
754 | { | |
3d9a9f94 KG |
755 | const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a; |
756 | const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b; | |
f074ff6c ZD |
757 | |
758 | return (int) da->dfs_num - (int) db->dfs_num; | |
759 | } | |
760 | ||
761 | /* Among the intervals starting at the N points specified in DEFS, find | |
762 | the one that contains S, and return its bb_index. */ | |
763 | ||
764 | static unsigned | |
765 | find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s) | |
766 | { | |
767 | unsigned f = 0, t = n, m; | |
768 | ||
769 | while (t > f + 1) | |
770 | { | |
771 | m = (f + t) / 2; | |
772 | if (defs[m].dfs_num <= s) | |
773 | f = m; | |
774 | else | |
775 | t = m; | |
776 | } | |
777 | ||
778 | return defs[f].bb_index; | |
779 | } | |
780 | ||
781 | /* Clean bits from PHIS for phi nodes whose value cannot be used in USES. | |
782 | KILLS is a bitmap of blocks where the value is defined before any use. */ | |
783 | ||
784 | static void | |
785 | prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses) | |
786 | { | |
f074ff6c ZD |
787 | bitmap_iterator bi; |
788 | unsigned i, b, p, u, top; | |
789 | bitmap live_phis; | |
790 | basic_block def_bb, use_bb; | |
791 | edge e; | |
792 | edge_iterator ei; | |
793 | bitmap to_remove; | |
794 | struct dom_dfsnum *defs; | |
795 | unsigned n_defs, adef; | |
796 | ||
797 | if (bitmap_empty_p (uses)) | |
798 | { | |
799 | bitmap_clear (phis); | |
800 | return; | |
801 | } | |
802 | ||
803 | /* The phi must dominate a use, or an argument of a live phi. Also, we | |
804 | do not create any phi nodes in def blocks, unless they are also livein. */ | |
805 | to_remove = BITMAP_ALLOC (NULL); | |
806 | bitmap_and_compl (to_remove, kills, uses); | |
807 | bitmap_and_compl_into (phis, to_remove); | |
808 | if (bitmap_empty_p (phis)) | |
809 | { | |
810 | BITMAP_FREE (to_remove); | |
811 | return; | |
812 | } | |
813 | ||
814 | /* We want to remove the unnecessary phi nodes, but we do not want to compute | |
815 | liveness information, as that may be linear in the size of CFG, and if | |
816 | there are lot of different variables to rewrite, this may lead to quadratic | |
817 | behavior. | |
818 | ||
819 | Instead, we basically emulate standard dce. We put all uses to worklist, | |
820 | then for each of them find the nearest def that dominates them. If this | |
821 | def is a phi node, we mark it live, and if it was not live before, we | |
822 | add the predecessors of its basic block to the worklist. | |
b8698a0f | 823 | |
f074ff6c ZD |
824 | To quickly locate the nearest def that dominates use, we use dfs numbering |
825 | of the dominance tree (that is already available in order to speed up | |
826 | queries). For each def, we have the interval given by the dfs number on | |
827 | entry to and on exit from the corresponding subtree in the dominance tree. | |
828 | The nearest dominator for a given use is the smallest of these intervals | |
829 | that contains entry and exit dfs numbers for the basic block with the use. | |
830 | If we store the bounds for all the uses to an array and sort it, we can | |
831 | locate the nearest dominating def in logarithmic time by binary search.*/ | |
832 | bitmap_ior (to_remove, kills, phis); | |
833 | n_defs = bitmap_count_bits (to_remove); | |
834 | defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1); | |
835 | defs[0].bb_index = 1; | |
836 | defs[0].dfs_num = 0; | |
837 | adef = 1; | |
838 | EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi) | |
839 | { | |
06e28de2 | 840 | def_bb = BASIC_BLOCK_FOR_FN (cfun, i); |
f074ff6c ZD |
841 | defs[adef].bb_index = i; |
842 | defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb); | |
843 | defs[adef + 1].bb_index = i; | |
844 | defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb); | |
845 | adef += 2; | |
846 | } | |
847 | BITMAP_FREE (to_remove); | |
848 | gcc_assert (adef == 2 * n_defs + 1); | |
849 | qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum); | |
850 | gcc_assert (defs[0].bb_index == 1); | |
851 | ||
852 | /* Now each DEFS entry contains the number of the basic block to that the | |
853 | dfs number corresponds. Change them to the number of basic block that | |
854 | corresponds to the interval following the dfs number. Also, for the | |
855 | dfs_out numbers, increase the dfs number by one (so that it corresponds | |
856 | to the start of the following interval, not to the end of the current | |
857 | one). We use WORKLIST as a stack. */ | |
ef062b13 | 858 | auto_vec<int> worklist (n_defs + 1); |
9771b263 | 859 | worklist.quick_push (1); |
f074ff6c ZD |
860 | top = 1; |
861 | n_defs = 1; | |
862 | for (i = 1; i < adef; i++) | |
863 | { | |
864 | b = defs[i].bb_index; | |
865 | if (b == top) | |
866 | { | |
867 | /* This is a closing element. Interval corresponding to the top | |
868 | of the stack after removing it follows. */ | |
9771b263 DN |
869 | worklist.pop (); |
870 | top = worklist[worklist.length () - 1]; | |
f074ff6c ZD |
871 | defs[n_defs].bb_index = top; |
872 | defs[n_defs].dfs_num = defs[i].dfs_num + 1; | |
873 | } | |
874 | else | |
875 | { | |
876 | /* Opening element. Nothing to do, just push it to the stack and move | |
877 | it to the correct position. */ | |
878 | defs[n_defs].bb_index = defs[i].bb_index; | |
879 | defs[n_defs].dfs_num = defs[i].dfs_num; | |
9771b263 | 880 | worklist.quick_push (b); |
f074ff6c ZD |
881 | top = b; |
882 | } | |
883 | ||
884 | /* If this interval starts at the same point as the previous one, cancel | |
885 | the previous one. */ | |
886 | if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num) | |
887 | defs[n_defs - 1].bb_index = defs[n_defs].bb_index; | |
888 | else | |
889 | n_defs++; | |
890 | } | |
9771b263 DN |
891 | worklist.pop (); |
892 | gcc_assert (worklist.is_empty ()); | |
f074ff6c ZD |
893 | |
894 | /* Now process the uses. */ | |
895 | live_phis = BITMAP_ALLOC (NULL); | |
896 | EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi) | |
897 | { | |
9771b263 | 898 | worklist.safe_push (i); |
f074ff6c ZD |
899 | } |
900 | ||
9771b263 | 901 | while (!worklist.is_empty ()) |
f074ff6c | 902 | { |
9771b263 | 903 | b = worklist.pop (); |
f074ff6c ZD |
904 | if (b == ENTRY_BLOCK) |
905 | continue; | |
906 | ||
907 | /* If there is a phi node in USE_BB, it is made live. Otherwise, | |
908 | find the def that dominates the immediate dominator of USE_BB | |
909 | (the kill in USE_BB does not dominate the use). */ | |
910 | if (bitmap_bit_p (phis, b)) | |
911 | p = b; | |
912 | else | |
913 | { | |
06e28de2 DM |
914 | use_bb = get_immediate_dominator (CDI_DOMINATORS, |
915 | BASIC_BLOCK_FOR_FN (cfun, b)); | |
f074ff6c ZD |
916 | p = find_dfsnum_interval (defs, n_defs, |
917 | bb_dom_dfs_in (CDI_DOMINATORS, use_bb)); | |
918 | if (!bitmap_bit_p (phis, p)) | |
919 | continue; | |
920 | } | |
921 | ||
922 | /* If the phi node is already live, there is nothing to do. */ | |
fcaa4ca4 | 923 | if (!bitmap_set_bit (live_phis, p)) |
f074ff6c ZD |
924 | continue; |
925 | ||
fcaa4ca4 | 926 | /* Add the new uses to the worklist. */ |
06e28de2 | 927 | def_bb = BASIC_BLOCK_FOR_FN (cfun, p); |
f074ff6c ZD |
928 | FOR_EACH_EDGE (e, ei, def_bb->preds) |
929 | { | |
930 | u = e->src->index; | |
931 | if (bitmap_bit_p (uses, u)) | |
932 | continue; | |
933 | ||
1e5787ef ZD |
934 | /* In case there is a kill directly in the use block, do not record |
935 | the use (this is also necessary for correctness, as we assume that | |
936 | uses dominated by a def directly in their block have been filtered | |
937 | out before). */ | |
938 | if (bitmap_bit_p (kills, u)) | |
939 | continue; | |
940 | ||
f074ff6c | 941 | bitmap_set_bit (uses, u); |
9771b263 | 942 | worklist.safe_push (u); |
f074ff6c ZD |
943 | } |
944 | } | |
945 | ||
f074ff6c ZD |
946 | bitmap_copy (phis, live_phis); |
947 | BITMAP_FREE (live_phis); | |
948 | free (defs); | |
949 | } | |
7256233c | 950 | |
7256233c DN |
951 | /* Return the set of blocks where variable VAR is defined and the blocks |
952 | where VAR is live on entry (livein). Return NULL, if no entry is | |
953 | found in DEF_BLOCKS. */ | |
954 | ||
955 | static inline struct def_blocks_d * | |
956 | find_def_blocks_for (tree var) | |
957 | { | |
8812aab1 | 958 | def_blocks_p p = &get_common_info (var)->def_blocks; |
b4e209fd RG |
959 | if (!p->def_blocks) |
960 | return NULL; | |
961 | return p; | |
7256233c DN |
962 | } |
963 | ||
964 | ||
2ce79879 ZD |
965 | /* Marks phi node PHI in basic block BB for rewrite. */ |
966 | ||
967 | static void | |
538dd0b7 | 968 | mark_phi_for_rewrite (basic_block bb, gphi *phi) |
2ce79879 | 969 | { |
538dd0b7 | 970 | vec<gphi *> phis; |
580b2c2e | 971 | unsigned n, idx = bb->index; |
2ce79879 | 972 | |
726a989a | 973 | if (rewrite_uses_p (phi)) |
2ce79879 | 974 | return; |
38635499 | 975 | |
726a989a | 976 | set_rewrite_uses (phi, true); |
2ce79879 ZD |
977 | |
978 | if (!blocks_with_phis_to_rewrite) | |
979 | return; | |
980 | ||
981 | bitmap_set_bit (blocks_with_phis_to_rewrite, idx); | |
580b2c2e | 982 | |
8b1c6fd7 | 983 | n = (unsigned) last_basic_block_for_fn (cfun) + 1; |
9771b263 DN |
984 | if (phis_to_rewrite.length () < n) |
985 | phis_to_rewrite.safe_grow_cleared (n); | |
2ce79879 | 986 | |
9771b263 DN |
987 | phis = phis_to_rewrite[idx]; |
988 | phis.reserve (10); | |
2ce79879 | 989 | |
9771b263 DN |
990 | phis.safe_push (phi); |
991 | phis_to_rewrite[idx] = phis; | |
2ce79879 ZD |
992 | } |
993 | ||
7256233c | 994 | /* Insert PHI nodes for variable VAR using the iterated dominance |
0bca51f0 | 995 | frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this |
38635499 DN |
996 | function assumes that the caller is incrementally updating the |
997 | existing SSA form, in which case VAR may be an SSA name instead of | |
998 | a symbol. | |
0bca51f0 DN |
999 | |
1000 | PHI_INSERTION_POINTS is updated to reflect nodes that already had a | |
1001 | PHI node for VAR. On exit, only the nodes that received a PHI node | |
1002 | for VAR will be present in PHI_INSERTION_POINTS. */ | |
7256233c DN |
1003 | |
1004 | static void | |
0bca51f0 | 1005 | insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p) |
7256233c DN |
1006 | { |
1007 | unsigned bb_index; | |
1008 | edge e; | |
538dd0b7 | 1009 | gphi *phi; |
7256233c DN |
1010 | basic_block bb; |
1011 | bitmap_iterator bi; | |
4ad14919 | 1012 | struct def_blocks_d *def_map = find_def_blocks_for (var); |
7256233c DN |
1013 | |
1014 | /* Remove the blocks where we already have PHI nodes for VAR. */ | |
1015 | bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks); | |
1016 | ||
f074ff6c ZD |
1017 | /* Remove obviously useless phi nodes. */ |
1018 | prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks, | |
1019 | def_map->livein_blocks); | |
7256233c DN |
1020 | |
1021 | /* And insert the PHI nodes. */ | |
f074ff6c | 1022 | EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi) |
7256233c | 1023 | { |
06e28de2 | 1024 | bb = BASIC_BLOCK_FOR_FN (cfun, bb_index); |
95dd3097 ZD |
1025 | if (update_p) |
1026 | mark_block_for_update (bb); | |
7256233c | 1027 | |
fa5556de RB |
1028 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1029 | { | |
1030 | fprintf (dump_file, "creating PHI node in block #%d for ", bb_index); | |
1031 | print_generic_expr (dump_file, var, TDF_SLIM); | |
1032 | fprintf (dump_file, "\n"); | |
1033 | } | |
726a989a | 1034 | phi = NULL; |
38635499 DN |
1035 | |
1036 | if (TREE_CODE (var) == SSA_NAME) | |
7256233c | 1037 | { |
84d65814 DN |
1038 | /* If we are rewriting SSA names, create the LHS of the PHI |
1039 | node by duplicating VAR. This is useful in the case of | |
1040 | pointers, to also duplicate pointer attributes (alias | |
1041 | information, in particular). */ | |
7256233c | 1042 | edge_iterator ei; |
84d65814 | 1043 | tree new_lhs; |
0bca51f0 | 1044 | |
4ad14919 | 1045 | gcc_checking_assert (update_p); |
dcc748dd RG |
1046 | new_lhs = duplicate_ssa_name (var, NULL); |
1047 | phi = create_phi_node (new_lhs, bb); | |
84d65814 | 1048 | add_new_name_mapping (new_lhs, var); |
0bca51f0 DN |
1049 | |
1050 | /* Add VAR to every argument slot of PHI. We need VAR in | |
1051 | every argument so that rewrite_update_phi_arguments knows | |
1052 | which name is this PHI node replacing. If VAR is a | |
1053 | symbol marked for renaming, this is not necessary, the | |
1054 | renamer will use the symbol on the LHS to get its | |
1055 | reaching definition. */ | |
7256233c | 1056 | FOR_EACH_EDGE (e, ei, bb->preds) |
9e227d60 | 1057 | add_phi_arg (phi, var, e, UNKNOWN_LOCATION); |
7256233c | 1058 | } |
84d65814 DN |
1059 | else |
1060 | { | |
b5b8b0ac | 1061 | tree tracked_var; |
3a56edc7 | 1062 | |
4ad14919 | 1063 | gcc_checking_assert (DECL_P (var)); |
e24c4814 | 1064 | phi = create_phi_node (var, bb); |
3a56edc7 AO |
1065 | |
1066 | tracked_var = target_for_debug_bind (var); | |
1067 | if (tracked_var) | |
b5b8b0ac AO |
1068 | { |
1069 | gimple note = gimple_build_debug_bind (tracked_var, | |
1070 | PHI_RESULT (phi), | |
1071 | phi); | |
1072 | gimple_stmt_iterator si = gsi_after_labels (bb); | |
1073 | gsi_insert_before (&si, note, GSI_SAME_STMT); | |
1074 | } | |
84d65814 | 1075 | } |
0bca51f0 DN |
1076 | |
1077 | /* Mark this PHI node as interesting for update_ssa. */ | |
726a989a | 1078 | set_register_defs (phi, true); |
2ce79879 | 1079 | mark_phi_for_rewrite (bb, phi); |
7256233c DN |
1080 | } |
1081 | } | |
1082 | ||
b4e209fd | 1083 | /* Sort var_infos after DECL_UID of their var. */ |
d9ed2fbd RG |
1084 | |
1085 | static int | |
b4e209fd | 1086 | insert_phi_nodes_compare_var_infos (const void *a, const void *b) |
d9ed2fbd | 1087 | { |
b4e209fd RG |
1088 | const struct var_info_d *defa = *(struct var_info_d * const *)a; |
1089 | const struct var_info_d *defb = *(struct var_info_d * const *)b; | |
d9ed2fbd RG |
1090 | if (DECL_UID (defa->var) < DECL_UID (defb->var)) |
1091 | return -1; | |
1092 | else | |
1093 | return 1; | |
1094 | } | |
7256233c | 1095 | |
7256233c DN |
1096 | /* Insert PHI nodes at the dominance frontier of blocks with variable |
1097 | definitions. DFS contains the dominance frontier information for | |
38635499 | 1098 | the flowgraph. */ |
7256233c DN |
1099 | |
1100 | static void | |
0fc555fb | 1101 | insert_phi_nodes (bitmap_head *dfs) |
7256233c | 1102 | { |
c203e8a7 | 1103 | hash_table<var_info_hasher>::iterator hi; |
d9ed2fbd | 1104 | unsigned i; |
b4e209fd | 1105 | var_info_p info; |
7256233c DN |
1106 | |
1107 | timevar_push (TV_TREE_INSERT_PHI_NODES); | |
b8698a0f | 1108 | |
c203e8a7 TS |
1109 | auto_vec<var_info_p> vars (var_infos->elements ()); |
1110 | FOR_EACH_HASH_TABLE_ELEMENT (*var_infos, info, var_info_p, hi) | |
8812aab1 | 1111 | if (info->info.need_phi_state != NEED_PHI_STATE_NO) |
9771b263 | 1112 | vars.quick_push (info); |
d9ed2fbd | 1113 | |
e68c7b43 RG |
1114 | /* Do two stages to avoid code generation differences for UID |
1115 | differences but no UID ordering differences. */ | |
9771b263 | 1116 | vars.qsort (insert_phi_nodes_compare_var_infos); |
e68c7b43 | 1117 | |
9771b263 | 1118 | FOR_EACH_VEC_ELT (vars, i, info) |
5f240ec4 | 1119 | { |
8812aab1 | 1120 | bitmap idf = compute_idf (info->info.def_blocks.def_blocks, dfs); |
b4e209fd | 1121 | insert_phi_nodes_for (info->var, idf, false); |
e68c7b43 RG |
1122 | BITMAP_FREE (idf); |
1123 | } | |
1124 | ||
6de9cd9a DN |
1125 | timevar_pop (TV_TREE_INSERT_PHI_NODES); |
1126 | } | |
1127 | ||
1128 | ||
38635499 DN |
1129 | /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and |
1130 | register DEF (an SSA_NAME) to be a new definition for SYM. */ | |
7256233c | 1131 | |
cfaab3a9 | 1132 | static void |
38635499 | 1133 | register_new_def (tree def, tree sym) |
7256233c | 1134 | { |
8812aab1 | 1135 | common_info_p info = get_common_info (sym); |
7256233c | 1136 | tree currdef; |
b8698a0f | 1137 | |
7256233c DN |
1138 | /* If this variable is set in a single basic block and all uses are |
1139 | dominated by the set(s) in that single basic block, then there is | |
1140 | no reason to record anything for this variable in the block local | |
1141 | definition stacks. Doing so just wastes time and memory. | |
1142 | ||
1143 | This is the same test to prune the set of variables which may | |
1144 | need PHI nodes. So we just use that information since it's already | |
1145 | computed and available for us to use. */ | |
8812aab1 | 1146 | if (info->need_phi_state == NEED_PHI_STATE_NO) |
7256233c | 1147 | { |
8812aab1 | 1148 | info->current_def = def; |
7256233c DN |
1149 | return; |
1150 | } | |
1151 | ||
8812aab1 | 1152 | currdef = info->current_def; |
7256233c | 1153 | |
38635499 DN |
1154 | /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose |
1155 | SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM | |
1156 | in the stack so that we know which symbol is being defined by | |
1157 | this SSA name when we unwind the stack. */ | |
1158 | if (currdef && !is_gimple_reg (sym)) | |
9771b263 | 1159 | block_defs_stack.safe_push (sym); |
7256233c | 1160 | |
38635499 DN |
1161 | /* Push the current reaching definition into BLOCK_DEFS_STACK. This |
1162 | stack is later used by the dominator tree callbacks to restore | |
1163 | the reaching definitions for all the variables defined in the | |
1164 | block after a recursive visit to all its immediately dominated | |
1165 | blocks. If there is no current reaching definition, then just | |
1166 | record the underlying _DECL node. */ | |
9771b263 | 1167 | block_defs_stack.safe_push (currdef ? currdef : sym); |
38635499 DN |
1168 | |
1169 | /* Set the current reaching definition for SYM to be DEF. */ | |
8812aab1 | 1170 | info->current_def = def; |
7256233c DN |
1171 | } |
1172 | ||
1173 | ||
6de9cd9a DN |
1174 | /* Perform a depth-first traversal of the dominator tree looking for |
1175 | variables to rename. BB is the block where to start searching. | |
1176 | Renaming is a five step process: | |
1177 | ||
1178 | 1- Every definition made by PHI nodes at the start of the blocks is | |
1179 | registered as the current definition for the corresponding variable. | |
1180 | ||
1181 | 2- Every statement in BB is rewritten. USE and VUSE operands are | |
1182 | rewritten with their corresponding reaching definition. DEF and | |
1183 | VDEF targets are registered as new definitions. | |
b8698a0f | 1184 | |
6de9cd9a DN |
1185 | 3- All the PHI nodes in successor blocks of BB are visited. The |
1186 | argument corresponding to BB is replaced with its current reaching | |
1187 | definition. | |
1188 | ||
1189 | 4- Recursively rewrite every dominator child block of BB. | |
1190 | ||
1191 | 5- Restore (in reverse order) the current reaching definition for every | |
1192 | new definition introduced in this block. This is done so that when | |
1193 | we return from the recursive call, all the current reaching | |
1194 | definitions are restored to the names that were valid in the | |
1195 | dominator parent of BB. */ | |
1196 | ||
7256233c | 1197 | /* Return the current definition for variable VAR. If none is found, |
38635499 | 1198 | create a new SSA name to act as the zeroth definition for VAR. */ |
5f240ec4 | 1199 | |
7256233c DN |
1200 | static tree |
1201 | get_reaching_def (tree var) | |
1202 | { | |
8812aab1 | 1203 | common_info_p info = get_common_info (var); |
38635499 | 1204 | tree currdef; |
b8698a0f | 1205 | |
7256233c | 1206 | /* Lookup the current reaching definition for VAR. */ |
8812aab1 | 1207 | currdef = info->current_def; |
5f240ec4 | 1208 | |
7256233c DN |
1209 | /* If there is no reaching definition for VAR, create and register a |
1210 | default definition for it (if needed). */ | |
38635499 | 1211 | if (currdef == NULL_TREE) |
7256233c | 1212 | { |
38635499 | 1213 | tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var); |
32244553 | 1214 | currdef = get_or_create_ssa_default_def (cfun, sym); |
7256233c DN |
1215 | } |
1216 | ||
1217 | /* Return the current reaching definition for VAR, or the default | |
1218 | definition, if we had to create one. */ | |
38635499 | 1219 | return currdef; |
7256233c DN |
1220 | } |
1221 | ||
1222 | ||
ddb555ed JJ |
1223 | /* Helper function for rewrite_stmt. Rewrite uses in a debug stmt. */ |
1224 | ||
1225 | static void | |
1226 | rewrite_debug_stmt_uses (gimple stmt) | |
1227 | { | |
1228 | use_operand_p use_p; | |
1229 | ssa_op_iter iter; | |
1230 | bool update = false; | |
1231 | ||
1232 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE) | |
1233 | { | |
5f564b8f | 1234 | tree var = USE_FROM_PTR (use_p), def; |
8812aab1 | 1235 | common_info_p info = get_common_info (var); |
4ad14919 | 1236 | gcc_checking_assert (DECL_P (var)); |
8812aab1 | 1237 | def = info->current_def; |
5f564b8f | 1238 | if (!def) |
ddb555ed | 1239 | { |
fefa31b5 DM |
1240 | if (TREE_CODE (var) == PARM_DECL |
1241 | && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun))) | |
ddb555ed JJ |
1242 | { |
1243 | gimple_stmt_iterator gsi | |
fefa31b5 DM |
1244 | = |
1245 | gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))); | |
ddb555ed JJ |
1246 | int lim; |
1247 | /* Search a few source bind stmts at the start of first bb to | |
1248 | see if a DEBUG_EXPR_DECL can't be reused. */ | |
1249 | for (lim = 32; | |
1250 | !gsi_end_p (gsi) && lim > 0; | |
1251 | gsi_next (&gsi), lim--) | |
1252 | { | |
1253 | gimple gstmt = gsi_stmt (gsi); | |
1254 | if (!gimple_debug_source_bind_p (gstmt)) | |
1255 | break; | |
1256 | if (gimple_debug_source_bind_get_value (gstmt) == var) | |
1257 | { | |
1258 | def = gimple_debug_source_bind_get_var (gstmt); | |
1259 | if (TREE_CODE (def) == DEBUG_EXPR_DECL) | |
1260 | break; | |
1261 | else | |
1262 | def = NULL_TREE; | |
1263 | } | |
1264 | } | |
1265 | /* If not, add a new source bind stmt. */ | |
1266 | if (def == NULL_TREE) | |
1267 | { | |
1268 | gimple def_temp; | |
1269 | def = make_node (DEBUG_EXPR_DECL); | |
1270 | def_temp = gimple_build_debug_source_bind (def, var, NULL); | |
1271 | DECL_ARTIFICIAL (def) = 1; | |
1272 | TREE_TYPE (def) = TREE_TYPE (var); | |
1273 | DECL_MODE (def) = DECL_MODE (var); | |
fefa31b5 DM |
1274 | gsi = |
1275 | gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun))); | |
ddb555ed JJ |
1276 | gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT); |
1277 | } | |
1278 | update = true; | |
1279 | } | |
1280 | } | |
1281 | else | |
15923c25 | 1282 | { |
8812aab1 | 1283 | /* Check if info->current_def can be trusted. */ |
5f564b8f MM |
1284 | basic_block bb = gimple_bb (stmt); |
1285 | basic_block def_bb | |
1286 | = SSA_NAME_IS_DEFAULT_DEF (def) | |
1287 | ? NULL : gimple_bb (SSA_NAME_DEF_STMT (def)); | |
1288 | ||
1289 | /* If definition is in current bb, it is fine. */ | |
1290 | if (bb == def_bb) | |
1291 | ; | |
1292 | /* If definition bb doesn't dominate the current bb, | |
1293 | it can't be used. */ | |
1294 | else if (def_bb && !dominated_by_p (CDI_DOMINATORS, bb, def_bb)) | |
1295 | def = NULL; | |
1296 | /* If there is just one definition and dominates the current | |
1297 | bb, it is fine. */ | |
8812aab1 | 1298 | else if (info->need_phi_state == NEED_PHI_STATE_NO) |
5f564b8f MM |
1299 | ; |
1300 | else | |
15923c25 | 1301 | { |
8812aab1 | 1302 | struct def_blocks_d *db_p = get_def_blocks_for (info); |
15923c25 | 1303 | |
5f564b8f MM |
1304 | /* If there are some non-debug uses in the current bb, |
1305 | it is fine. */ | |
1306 | if (bitmap_bit_p (db_p->livein_blocks, bb->index)) | |
15923c25 | 1307 | ; |
5f564b8f | 1308 | /* Otherwise give up for now. */ |
15923c25 | 1309 | else |
5f564b8f | 1310 | def = NULL; |
15923c25 JJ |
1311 | } |
1312 | } | |
ddb555ed JJ |
1313 | if (def == NULL) |
1314 | { | |
1315 | gimple_debug_bind_reset_value (stmt); | |
1316 | update_stmt (stmt); | |
1317 | return; | |
1318 | } | |
1319 | SET_USE (use_p, def); | |
1320 | } | |
1321 | if (update) | |
1322 | update_stmt (stmt); | |
1323 | } | |
1324 | ||
7256233c DN |
1325 | /* SSA Rewriting Step 2. Rewrite every variable used in each statement in |
1326 | the block with its immediate reaching definitions. Update the current | |
1327 | definition of a variable when a new real or virtual definition is found. */ | |
5f240ec4 ZD |
1328 | |
1329 | static void | |
5f33a4fc | 1330 | rewrite_stmt (gimple_stmt_iterator *si) |
5f240ec4 | 1331 | { |
7256233c DN |
1332 | use_operand_p use_p; |
1333 | def_operand_p def_p; | |
1334 | ssa_op_iter iter; | |
5f33a4fc | 1335 | gimple stmt = gsi_stmt (*si); |
7256233c | 1336 | |
7256233c DN |
1337 | /* If mark_def_sites decided that we don't need to rewrite this |
1338 | statement, ignore it. */ | |
95dd3097 | 1339 | gcc_assert (blocks_to_update == NULL); |
726a989a | 1340 | if (!rewrite_uses_p (stmt) && !register_defs_p (stmt)) |
7256233c | 1341 | return; |
5f240ec4 ZD |
1342 | |
1343 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
7256233c DN |
1344 | { |
1345 | fprintf (dump_file, "Renaming statement "); | |
726a989a | 1346 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
7256233c DN |
1347 | fprintf (dump_file, "\n"); |
1348 | } | |
5f240ec4 | 1349 | |
38635499 | 1350 | /* Step 1. Rewrite USES in the statement. */ |
726a989a | 1351 | if (rewrite_uses_p (stmt)) |
ddb555ed JJ |
1352 | { |
1353 | if (is_gimple_debug (stmt)) | |
1354 | rewrite_debug_stmt_uses (stmt); | |
1355 | else | |
39c58b3a | 1356 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES) |
ddb555ed JJ |
1357 | { |
1358 | tree var = USE_FROM_PTR (use_p); | |
4ad14919 | 1359 | gcc_checking_assert (DECL_P (var)); |
ddb555ed JJ |
1360 | SET_USE (use_p, get_reaching_def (var)); |
1361 | } | |
1362 | } | |
5f240ec4 | 1363 | |
38635499 | 1364 | /* Step 2. Register the statement's DEF operands. */ |
726a989a | 1365 | if (register_defs_p (stmt)) |
39c58b3a | 1366 | FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS) |
0bca51f0 DN |
1367 | { |
1368 | tree var = DEF_FROM_PTR (def_p); | |
5f33a4fc | 1369 | tree name; |
b5b8b0ac | 1370 | tree tracked_var; |
5f33a4fc | 1371 | |
4ad14919 | 1372 | gcc_checking_assert (DECL_P (var)); |
5f33a4fc RG |
1373 | |
1374 | if (gimple_clobber_p (stmt) | |
1375 | && is_gimple_reg (var)) | |
1376 | { | |
1377 | /* If we rewrite a DECL into SSA form then drop its | |
1378 | clobber stmts and replace uses with a new default def. */ | |
4ad14919 RG |
1379 | gcc_checking_assert (TREE_CODE (var) == VAR_DECL |
1380 | && !gimple_vdef (stmt)); | |
5f33a4fc RG |
1381 | gsi_replace (si, gimple_build_nop (), true); |
1382 | register_new_def (get_or_create_ssa_default_def (cfun, var), var); | |
1383 | break; | |
1384 | } | |
1385 | ||
1386 | name = make_ssa_name (var, stmt); | |
b5b8b0ac | 1387 | SET_DEF (def_p, name); |
38635499 | 1388 | register_new_def (DEF_FROM_PTR (def_p), var); |
b5b8b0ac AO |
1389 | |
1390 | tracked_var = target_for_debug_bind (var); | |
1391 | if (tracked_var) | |
1392 | { | |
1393 | gimple note = gimple_build_debug_bind (tracked_var, name, stmt); | |
5f33a4fc | 1394 | gsi_insert_after (si, note, GSI_SAME_STMT); |
b5b8b0ac | 1395 | } |
0bca51f0 | 1396 | } |
5f240ec4 | 1397 | } |
6de9cd9a | 1398 | |
7256233c | 1399 | |
6de9cd9a DN |
1400 | /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for |
1401 | PHI nodes. For every PHI node found, add a new argument containing the | |
1402 | current reaching definition for the variable and the edge through which | |
471854f8 | 1403 | that definition is reaching the PHI node. */ |
6de9cd9a DN |
1404 | |
1405 | static void | |
ccf5c864 | 1406 | rewrite_add_phi_arguments (basic_block bb) |
6de9cd9a DN |
1407 | { |
1408 | edge e; | |
628f6a4e | 1409 | edge_iterator ei; |
6de9cd9a | 1410 | |
628f6a4e | 1411 | FOR_EACH_EDGE (e, ei, bb->succs) |
6de9cd9a | 1412 | { |
538dd0b7 DM |
1413 | gphi *phi; |
1414 | gphi_iterator gsi; | |
6de9cd9a | 1415 | |
726a989a RB |
1416 | for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi); |
1417 | gsi_next (&gsi)) | |
6de9cd9a | 1418 | { |
ffc5b2bb RB |
1419 | tree currdef, res; |
1420 | location_t loc; | |
f5045c96 | 1421 | |
538dd0b7 | 1422 | phi = gsi.phi (); |
ffc5b2bb RB |
1423 | res = gimple_phi_result (phi); |
1424 | currdef = get_reaching_def (SSA_NAME_VAR (res)); | |
1425 | /* Virtual operand PHI args do not need a location. */ | |
1426 | if (virtual_operand_p (res)) | |
1427 | loc = UNKNOWN_LOCATION; | |
1428 | else | |
1429 | loc = gimple_location (SSA_NAME_DEF_STMT (currdef)); | |
1430 | add_phi_arg (phi, currdef, e, loc); | |
6de9cd9a DN |
1431 | } |
1432 | } | |
1433 | } | |
1434 | ||
4d9192b5 TS |
1435 | class rewrite_dom_walker : public dom_walker |
1436 | { | |
1437 | public: | |
1438 | rewrite_dom_walker (cdi_direction direction) : dom_walker (direction) {} | |
1439 | ||
1440 | virtual void before_dom_children (basic_block); | |
1441 | virtual void after_dom_children (basic_block); | |
1442 | }; | |
1443 | ||
ccf5c864 PB |
1444 | /* SSA Rewriting Step 1. Initialization, create a block local stack |
1445 | of reaching definitions for new SSA names produced in this block | |
1446 | (BLOCK_DEFS). Register new definitions for every PHI node in the | |
1447 | block. */ | |
1448 | ||
4d9192b5 TS |
1449 | void |
1450 | rewrite_dom_walker::before_dom_children (basic_block bb) | |
ccf5c864 | 1451 | { |
ccf5c864 PB |
1452 | if (dump_file && (dump_flags & TDF_DETAILS)) |
1453 | fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index); | |
1454 | ||
1455 | /* Mark the unwind point for this block. */ | |
9771b263 | 1456 | block_defs_stack.safe_push (NULL_TREE); |
ccf5c864 PB |
1457 | |
1458 | /* Step 1. Register new definitions for every PHI node in the block. | |
1459 | Conceptually, all the PHI nodes are executed in parallel and each PHI | |
1460 | node introduces a new version for the associated variable. */ | |
538dd0b7 DM |
1461 | for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
1462 | gsi_next (&gsi)) | |
ccf5c864 | 1463 | { |
39c58b3a | 1464 | tree result = gimple_phi_result (gsi_stmt (gsi)); |
ccf5c864 PB |
1465 | register_new_def (result, SSA_NAME_VAR (result)); |
1466 | } | |
1467 | ||
1468 | /* Step 2. Rewrite every variable used in each statement in the block | |
1469 | with its immediate reaching definitions. Update the current definition | |
1470 | of a variable when a new real or virtual definition is found. */ | |
d7c028c0 | 1471 | if (bitmap_bit_p (interesting_blocks, bb->index)) |
538dd0b7 DM |
1472 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); |
1473 | gsi_next (&gsi)) | |
5f33a4fc | 1474 | rewrite_stmt (&gsi); |
ccf5c864 PB |
1475 | |
1476 | /* Step 3. Visit all the successor blocks of BB looking for PHI nodes. | |
1477 | For every PHI node found, add a new argument containing the current | |
1478 | reaching definition for the variable and the edge through which that | |
1479 | definition is reaching the PHI node. */ | |
1480 | rewrite_add_phi_arguments (bb); | |
1481 | } | |
1482 | ||
1483 | ||
7256233c | 1484 | |
38635499 DN |
1485 | /* Called after visiting all the statements in basic block BB and all |
1486 | of its dominator children. Restore CURRDEFS to its original value. */ | |
6de9cd9a | 1487 | |
4d9192b5 TS |
1488 | void |
1489 | rewrite_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED) | |
6de9cd9a | 1490 | { |
9fae925b | 1491 | /* Restore CURRDEFS to its original state. */ |
9771b263 | 1492 | while (block_defs_stack.length () > 0) |
6de9cd9a | 1493 | { |
9771b263 | 1494 | tree tmp = block_defs_stack.pop (); |
6de9cd9a DN |
1495 | tree saved_def, var; |
1496 | ||
9fae925b JL |
1497 | if (tmp == NULL_TREE) |
1498 | break; | |
1499 | ||
6de9cd9a DN |
1500 | if (TREE_CODE (tmp) == SSA_NAME) |
1501 | { | |
38635499 DN |
1502 | /* If we recorded an SSA_NAME, then make the SSA_NAME the |
1503 | current definition of its underlying variable. Note that | |
1504 | if the SSA_NAME is not for a GIMPLE register, the symbol | |
1505 | being defined is stored in the next slot in the stack. | |
1506 | This mechanism is needed because an SSA name for a | |
1507 | non-register symbol may be the definition for more than | |
1508 | one symbol (e.g., SFTs, aliased variables, etc). */ | |
6de9cd9a DN |
1509 | saved_def = tmp; |
1510 | var = SSA_NAME_VAR (saved_def); | |
38635499 | 1511 | if (!is_gimple_reg (var)) |
9771b263 | 1512 | var = block_defs_stack.pop (); |
6de9cd9a DN |
1513 | } |
1514 | else | |
1515 | { | |
38635499 DN |
1516 | /* If we recorded anything else, it must have been a _DECL |
1517 | node and its current reaching definition must have been | |
1518 | NULL. */ | |
6de9cd9a DN |
1519 | saved_def = NULL; |
1520 | var = tmp; | |
1521 | } | |
b8698a0f | 1522 | |
8812aab1 | 1523 | get_common_info (var)->current_def = saved_def; |
6de9cd9a DN |
1524 | } |
1525 | } | |
1526 | ||
1527 | ||
38635499 DN |
1528 | /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */ |
1529 | ||
24e47c76 | 1530 | DEBUG_FUNCTION void |
38635499 DN |
1531 | debug_decl_set (bitmap set) |
1532 | { | |
1533 | dump_decl_set (stderr, set); | |
5006671f | 1534 | fprintf (stderr, "\n"); |
38635499 DN |
1535 | } |
1536 | ||
1537 | ||
1538 | /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the | |
1539 | stack up to a maximum of N levels. If N is -1, the whole stack is | |
1540 | dumped. New levels are created when the dominator tree traversal | |
1541 | used for renaming enters a new sub-tree. */ | |
1542 | ||
1543 | void | |
1544 | dump_defs_stack (FILE *file, int n) | |
1545 | { | |
1546 | int i, j; | |
1547 | ||
1548 | fprintf (file, "\n\nRenaming stack"); | |
1549 | if (n > 0) | |
1550 | fprintf (file, " (up to %d levels)", n); | |
1551 | fprintf (file, "\n\n"); | |
1552 | ||
1553 | i = 1; | |
1554 | fprintf (file, "Level %d (current level)\n", i); | |
9771b263 | 1555 | for (j = (int) block_defs_stack.length () - 1; j >= 0; j--) |
38635499 DN |
1556 | { |
1557 | tree name, var; | |
b8698a0f | 1558 | |
9771b263 | 1559 | name = block_defs_stack[j]; |
38635499 DN |
1560 | if (name == NULL_TREE) |
1561 | { | |
1562 | i++; | |
1563 | if (n > 0 && i > n) | |
1564 | break; | |
1565 | fprintf (file, "\nLevel %d\n", i); | |
1566 | continue; | |
1567 | } | |
1568 | ||
1569 | if (DECL_P (name)) | |
1570 | { | |
1571 | var = name; | |
1572 | name = NULL_TREE; | |
1573 | } | |
1574 | else | |
1575 | { | |
1576 | var = SSA_NAME_VAR (name); | |
1577 | if (!is_gimple_reg (var)) | |
1578 | { | |
1579 | j--; | |
9771b263 | 1580 | var = block_defs_stack[j]; |
38635499 DN |
1581 | } |
1582 | } | |
1583 | ||
1584 | fprintf (file, " Previous CURRDEF ("); | |
1585 | print_generic_expr (file, var, 0); | |
1586 | fprintf (file, ") = "); | |
1587 | if (name) | |
1588 | print_generic_expr (file, name, 0); | |
1589 | else | |
1590 | fprintf (file, "<NIL>"); | |
1591 | fprintf (file, "\n"); | |
1592 | } | |
1593 | } | |
1594 | ||
1595 | ||
1596 | /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the | |
1597 | stack up to a maximum of N levels. If N is -1, the whole stack is | |
1598 | dumped. New levels are created when the dominator tree traversal | |
1599 | used for renaming enters a new sub-tree. */ | |
1600 | ||
24e47c76 | 1601 | DEBUG_FUNCTION void |
38635499 DN |
1602 | debug_defs_stack (int n) |
1603 | { | |
1604 | dump_defs_stack (stderr, n); | |
1605 | } | |
1606 | ||
1607 | ||
1608 | /* Dump the current reaching definition of every symbol to FILE. */ | |
1609 | ||
1610 | void | |
1611 | dump_currdefs (FILE *file) | |
1612 | { | |
13714310 | 1613 | unsigned i; |
38635499 DN |
1614 | tree var; |
1615 | ||
9771b263 | 1616 | if (symbols_to_rename.is_empty ()) |
13714310 RG |
1617 | return; |
1618 | ||
38635499 | 1619 | fprintf (file, "\n\nCurrent reaching definitions\n\n"); |
9771b263 | 1620 | FOR_EACH_VEC_ELT (symbols_to_rename, i, var) |
13714310 | 1621 | { |
8812aab1 | 1622 | common_info_p info = get_common_info (var); |
13714310 RG |
1623 | fprintf (file, "CURRDEF ("); |
1624 | print_generic_expr (file, var, 0); | |
1625 | fprintf (file, ") = "); | |
8812aab1 RG |
1626 | if (info->current_def) |
1627 | print_generic_expr (file, info->current_def, 0); | |
13714310 RG |
1628 | else |
1629 | fprintf (file, "<NIL>"); | |
1630 | fprintf (file, "\n"); | |
1631 | } | |
38635499 DN |
1632 | } |
1633 | ||
1634 | ||
1635 | /* Dump the current reaching definition of every symbol to stderr. */ | |
1636 | ||
24e47c76 | 1637 | DEBUG_FUNCTION void |
38635499 DN |
1638 | debug_currdefs (void) |
1639 | { | |
1640 | dump_currdefs (stderr); | |
1641 | } | |
1642 | ||
1643 | ||
6de9cd9a DN |
1644 | /* Dump SSA information to FILE. */ |
1645 | ||
1646 | void | |
1647 | dump_tree_ssa (FILE *file) | |
1648 | { | |
6de9cd9a | 1649 | const char *funcname |
673fda6b | 1650 | = lang_hooks.decl_printable_name (current_function_decl, 2); |
6de9cd9a | 1651 | |
38635499 | 1652 | fprintf (file, "SSA renaming information for %s\n\n", funcname); |
6de9cd9a | 1653 | |
b4e209fd | 1654 | dump_var_infos (file); |
38635499 DN |
1655 | dump_defs_stack (file, -1); |
1656 | dump_currdefs (file); | |
1657 | dump_tree_ssa_stats (file); | |
6de9cd9a DN |
1658 | } |
1659 | ||
1660 | ||
1661 | /* Dump SSA information to stderr. */ | |
1662 | ||
24e47c76 | 1663 | DEBUG_FUNCTION void |
6de9cd9a DN |
1664 | debug_tree_ssa (void) |
1665 | { | |
1666 | dump_tree_ssa (stderr); | |
1667 | } | |
1668 | ||
1669 | ||
7256233c DN |
1670 | /* Dump statistics for the hash table HTAB. */ |
1671 | ||
1672 | static void | |
c203e8a7 | 1673 | htab_statistics (FILE *file, const hash_table<var_info_hasher> &htab) |
7256233c DN |
1674 | { |
1675 | fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n", | |
bf190e8d LC |
1676 | (long) htab.size (), |
1677 | (long) htab.elements (), | |
1678 | htab.collisions ()); | |
7256233c DN |
1679 | } |
1680 | ||
1681 | ||
6de9cd9a DN |
1682 | /* Dump SSA statistics on FILE. */ |
1683 | ||
1684 | void | |
1685 | dump_tree_ssa_stats (FILE *file) | |
1686 | { | |
c203e8a7 | 1687 | if (var_infos) |
38635499 | 1688 | { |
b4e209fd RG |
1689 | fprintf (file, "\nHash table statistics:\n"); |
1690 | fprintf (file, " var_infos: "); | |
c203e8a7 | 1691 | htab_statistics (file, *var_infos); |
b4e209fd | 1692 | fprintf (file, "\n"); |
38635499 | 1693 | } |
6de9cd9a DN |
1694 | } |
1695 | ||
1696 | ||
1697 | /* Dump SSA statistics on stderr. */ | |
1698 | ||
24e47c76 | 1699 | DEBUG_FUNCTION void |
6de9cd9a DN |
1700 | debug_tree_ssa_stats (void) |
1701 | { | |
1702 | dump_tree_ssa_stats (stderr); | |
1703 | } | |
1704 | ||
1705 | ||
b4e209fd | 1706 | /* Callback for htab_traverse to dump the VAR_INFOS hash table. */ |
6de9cd9a | 1707 | |
bf190e8d LC |
1708 | int |
1709 | debug_var_infos_r (var_info_d **slot, FILE *file) | |
7256233c | 1710 | { |
bf190e8d | 1711 | struct var_info_d *info = *slot; |
b8698a0f | 1712 | |
38635499 | 1713 | fprintf (file, "VAR: "); |
8812aab1 RG |
1714 | print_generic_expr (file, info->var, dump_flags); |
1715 | bitmap_print (file, info->info.def_blocks.def_blocks, | |
1716 | ", DEF_BLOCKS: { ", "}"); | |
1717 | bitmap_print (file, info->info.def_blocks.livein_blocks, | |
1718 | ", LIVEIN_BLOCKS: { ", "}"); | |
1719 | bitmap_print (file, info->info.def_blocks.phi_blocks, | |
1720 | ", PHI_BLOCKS: { ", "}\n"); | |
6de9cd9a | 1721 | |
7256233c DN |
1722 | return 1; |
1723 | } | |
6de9cd9a | 1724 | |
6de9cd9a | 1725 | |
b4e209fd | 1726 | /* Dump the VAR_INFOS hash table on FILE. */ |
38635499 DN |
1727 | |
1728 | void | |
b4e209fd | 1729 | dump_var_infos (FILE *file) |
38635499 DN |
1730 | { |
1731 | fprintf (file, "\n\nDefinition and live-in blocks:\n\n"); | |
c203e8a7 TS |
1732 | if (var_infos) |
1733 | var_infos->traverse <FILE *, debug_var_infos_r> (file); | |
38635499 DN |
1734 | } |
1735 | ||
1736 | ||
b4e209fd | 1737 | /* Dump the VAR_INFOS hash table on stderr. */ |
6de9cd9a | 1738 | |
24e47c76 | 1739 | DEBUG_FUNCTION void |
b4e209fd | 1740 | debug_var_infos (void) |
7256233c | 1741 | { |
b4e209fd | 1742 | dump_var_infos (stderr); |
7256233c | 1743 | } |
6de9cd9a | 1744 | |
5f240ec4 | 1745 | |
0bca51f0 | 1746 | /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */ |
6de9cd9a | 1747 | |
0bca51f0 DN |
1748 | static inline void |
1749 | register_new_update_single (tree new_name, tree old_name) | |
1750 | { | |
8812aab1 RG |
1751 | common_info_p info = get_common_info (old_name); |
1752 | tree currdef = info->current_def; | |
5f240ec4 | 1753 | |
38635499 | 1754 | /* Push the current reaching definition into BLOCK_DEFS_STACK. |
0bca51f0 DN |
1755 | This stack is later used by the dominator tree callbacks to |
1756 | restore the reaching definitions for all the variables | |
1757 | defined in the block after a recursive visit to all its | |
1758 | immediately dominated blocks. */ | |
9771b263 DN |
1759 | block_defs_stack.reserve (2); |
1760 | block_defs_stack.quick_push (currdef); | |
1761 | block_defs_stack.quick_push (old_name); | |
5f240ec4 | 1762 | |
0bca51f0 DN |
1763 | /* Set the current reaching definition for OLD_NAME to be |
1764 | NEW_NAME. */ | |
8812aab1 | 1765 | info->current_def = new_name; |
0bca51f0 | 1766 | } |
7256233c | 1767 | |
6de9cd9a | 1768 | |
0bca51f0 DN |
1769 | /* Register NEW_NAME to be the new reaching definition for all the |
1770 | names in OLD_NAMES. Used by the incremental SSA update routines to | |
1771 | replace old SSA names with new ones. */ | |
7256233c | 1772 | |
0bca51f0 DN |
1773 | static inline void |
1774 | register_new_update_set (tree new_name, bitmap old_names) | |
1775 | { | |
1776 | bitmap_iterator bi; | |
1777 | unsigned i; | |
7256233c | 1778 | |
0bca51f0 DN |
1779 | EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi) |
1780 | register_new_update_single (new_name, ssa_name (i)); | |
6de9cd9a DN |
1781 | } |
1782 | ||
7256233c | 1783 | |
0bca51f0 | 1784 | |
84d65814 DN |
1785 | /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or |
1786 | it is a symbol marked for renaming, replace it with USE_P's current | |
1787 | reaching definition. */ | |
1788 | ||
1789 | static inline void | |
1790 | maybe_replace_use (use_operand_p use_p) | |
1791 | { | |
1792 | tree rdef = NULL_TREE; | |
1793 | tree use = USE_FROM_PTR (use_p); | |
1794 | tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use); | |
1795 | ||
13714310 | 1796 | if (marked_for_renaming (sym)) |
84d65814 DN |
1797 | rdef = get_reaching_def (sym); |
1798 | else if (is_old_name (use)) | |
1799 | rdef = get_reaching_def (use); | |
1800 | ||
1801 | if (rdef && rdef != use) | |
1802 | SET_USE (use_p, rdef); | |
1803 | } | |
1804 | ||
1805 | ||
b5b8b0ac AO |
1806 | /* Same as maybe_replace_use, but without introducing default stmts, |
1807 | returning false to indicate a need to do so. */ | |
1808 | ||
1809 | static inline bool | |
1810 | maybe_replace_use_in_debug_stmt (use_operand_p use_p) | |
1811 | { | |
1812 | tree rdef = NULL_TREE; | |
1813 | tree use = USE_FROM_PTR (use_p); | |
1814 | tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use); | |
1815 | ||
13714310 | 1816 | if (marked_for_renaming (sym)) |
8812aab1 | 1817 | rdef = get_var_info (sym)->info.current_def; |
b5b8b0ac AO |
1818 | else if (is_old_name (use)) |
1819 | { | |
8812aab1 | 1820 | rdef = get_ssa_name_ann (use)->info.current_def; |
b5b8b0ac AO |
1821 | /* We can't assume that, if there's no current definition, the |
1822 | default one should be used. It could be the case that we've | |
1823 | rearranged blocks so that the earlier definition no longer | |
1824 | dominates the use. */ | |
1825 | if (!rdef && SSA_NAME_IS_DEFAULT_DEF (use)) | |
1826 | rdef = use; | |
1827 | } | |
1828 | else | |
1829 | rdef = use; | |
1830 | ||
1831 | if (rdef && rdef != use) | |
1832 | SET_USE (use_p, rdef); | |
1833 | ||
1834 | return rdef != NULL_TREE; | |
1835 | } | |
1836 | ||
1837 | ||
84d65814 DN |
1838 | /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES |
1839 | or OLD_SSA_NAMES, or if it is a symbol marked for renaming, | |
1840 | register it as the current definition for the names replaced by | |
956623c1 | 1841 | DEF_P. Returns whether the statement should be removed. */ |
84d65814 | 1842 | |
956623c1 | 1843 | static inline bool |
3a56edc7 AO |
1844 | maybe_register_def (def_operand_p def_p, gimple stmt, |
1845 | gimple_stmt_iterator gsi) | |
84d65814 DN |
1846 | { |
1847 | tree def = DEF_FROM_PTR (def_p); | |
1848 | tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def); | |
956623c1 | 1849 | bool to_delete = false; |
84d65814 | 1850 | |
38635499 DN |
1851 | /* If DEF is a naked symbol that needs renaming, create a new |
1852 | name for it. */ | |
13714310 | 1853 | if (marked_for_renaming (sym)) |
84d65814 DN |
1854 | { |
1855 | if (DECL_P (def)) | |
1856 | { | |
956623c1 MG |
1857 | if (gimple_clobber_p (stmt) && is_gimple_reg (sym)) |
1858 | { | |
1859 | gcc_checking_assert (TREE_CODE (sym) == VAR_DECL); | |
1860 | /* Replace clobber stmts with a default def. This new use of a | |
1861 | default definition may make it look like SSA_NAMEs have | |
1862 | conflicting lifetimes, so we need special code to let them | |
1863 | coalesce properly. */ | |
1864 | to_delete = true; | |
1865 | def = get_or_create_ssa_default_def (cfun, sym); | |
1866 | } | |
1867 | else | |
1868 | def = make_ssa_name (def, stmt); | |
84d65814 | 1869 | SET_DEF (def_p, def); |
3a56edc7 | 1870 | |
956623c1 | 1871 | tree tracked_var = target_for_debug_bind (sym); |
3a56edc7 AO |
1872 | if (tracked_var) |
1873 | { | |
1874 | gimple note = gimple_build_debug_bind (tracked_var, def, stmt); | |
c47987fa JJ |
1875 | /* If stmt ends the bb, insert the debug stmt on the single |
1876 | non-EH edge from the stmt. */ | |
1877 | if (gsi_one_before_end_p (gsi) && stmt_ends_bb_p (stmt)) | |
1878 | { | |
1879 | basic_block bb = gsi_bb (gsi); | |
1880 | edge_iterator ei; | |
1881 | edge e, ef = NULL; | |
1882 | FOR_EACH_EDGE (e, ei, bb->succs) | |
1883 | if (!(e->flags & EDGE_EH)) | |
1884 | { | |
4ad14919 | 1885 | gcc_checking_assert (!ef); |
c47987fa JJ |
1886 | ef = e; |
1887 | } | |
23d5ed5d AO |
1888 | /* If there are other predecessors to ef->dest, then |
1889 | there must be PHI nodes for the modified | |
1890 | variable, and therefore there will be debug bind | |
1891 | stmts after the PHI nodes. The debug bind notes | |
1892 | we'd insert would force the creation of a new | |
1893 | block (diverging codegen) and be redundant with | |
1894 | the post-PHI bind stmts, so don't add them. | |
1895 | ||
1896 | As for the exit edge, there wouldn't be redundant | |
1897 | bind stmts, but there wouldn't be a PC to bind | |
1898 | them to either, so avoid diverging the CFG. */ | |
1899 | if (ef && single_pred_p (ef->dest) | |
fefa31b5 | 1900 | && ef->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
23d5ed5d AO |
1901 | { |
1902 | /* If there were PHI nodes in the node, we'd | |
1903 | have to make sure the value we're binding | |
1904 | doesn't need rewriting. But there shouldn't | |
1905 | be PHI nodes in a single-predecessor block, | |
1906 | so we just add the note. */ | |
1907 | gsi_insert_on_edge_immediate (ef, note); | |
1908 | } | |
c47987fa JJ |
1909 | } |
1910 | else | |
1911 | gsi_insert_after (&gsi, note, GSI_SAME_STMT); | |
3a56edc7 | 1912 | } |
84d65814 DN |
1913 | } |
1914 | ||
1915 | register_new_update_single (def, sym); | |
1916 | } | |
1917 | else | |
1918 | { | |
1919 | /* If DEF is a new name, register it as a new definition | |
1920 | for all the names replaced by DEF. */ | |
1921 | if (is_new_name (def)) | |
1922 | register_new_update_set (def, names_replaced_by (def)); | |
1923 | ||
1924 | /* If DEF is an old name, register DEF as a new | |
1925 | definition for itself. */ | |
1926 | if (is_old_name (def)) | |
1927 | register_new_update_single (def, def); | |
1928 | } | |
956623c1 MG |
1929 | |
1930 | return to_delete; | |
84d65814 DN |
1931 | } |
1932 | ||
1933 | ||
0bca51f0 DN |
1934 | /* Update every variable used in the statement pointed-to by SI. The |
1935 | statement is assumed to be in SSA form already. Names in | |
1936 | OLD_SSA_NAMES used by SI will be updated to their current reaching | |
1937 | definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI | |
1938 | will be registered as a new definition for their corresponding name | |
956623c1 | 1939 | in OLD_SSA_NAMES. Returns whether STMT should be removed. */ |
0bca51f0 | 1940 | |
956623c1 | 1941 | static bool |
3a56edc7 | 1942 | rewrite_update_stmt (gimple stmt, gimple_stmt_iterator gsi) |
0bca51f0 | 1943 | { |
0bca51f0 DN |
1944 | use_operand_p use_p; |
1945 | def_operand_p def_p; | |
1946 | ssa_op_iter iter; | |
1947 | ||
0bca51f0 | 1948 | /* Only update marked statements. */ |
726a989a | 1949 | if (!rewrite_uses_p (stmt) && !register_defs_p (stmt)) |
956623c1 | 1950 | return false; |
0bca51f0 DN |
1951 | |
1952 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
1953 | { | |
1954 | fprintf (dump_file, "Updating SSA information for statement "); | |
726a989a | 1955 | print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM); |
0bca51f0 DN |
1956 | } |
1957 | ||
1958 | /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying | |
1959 | symbol is marked for renaming. */ | |
726a989a | 1960 | if (rewrite_uses_p (stmt)) |
b5b8b0ac AO |
1961 | { |
1962 | if (is_gimple_debug (stmt)) | |
1963 | { | |
1964 | bool failed = false; | |
1965 | ||
1966 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE) | |
1967 | if (!maybe_replace_use_in_debug_stmt (use_p)) | |
1968 | { | |
1969 | failed = true; | |
1970 | break; | |
1971 | } | |
1972 | ||
1973 | if (failed) | |
1974 | { | |
1975 | /* DOM sometimes threads jumps in such a way that a | |
1976 | debug stmt ends up referencing a SSA variable that no | |
1977 | longer dominates the debug stmt, but such that all | |
1978 | incoming definitions refer to the same definition in | |
1979 | an earlier dominator. We could try to recover that | |
1980 | definition somehow, but this will have to do for now. | |
1981 | ||
1982 | Introducing a default definition, which is what | |
1983 | maybe_replace_use() would do in such cases, may | |
1984 | modify code generation, for the otherwise-unused | |
1985 | default definition would never go away, modifying SSA | |
1986 | version numbers all over. */ | |
1987 | gimple_debug_bind_reset_value (stmt); | |
1988 | update_stmt (stmt); | |
1989 | } | |
1990 | } | |
1991 | else | |
1992 | { | |
1993 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES) | |
1994 | maybe_replace_use (use_p); | |
1995 | } | |
1996 | } | |
0bca51f0 DN |
1997 | |
1998 | /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES. | |
1999 | Also register definitions for names whose underlying symbol is | |
2000 | marked for renaming. */ | |
956623c1 | 2001 | bool to_delete = false; |
726a989a | 2002 | if (register_defs_p (stmt)) |
5006671f | 2003 | FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS) |
956623c1 MG |
2004 | to_delete |= maybe_register_def (def_p, stmt, gsi); |
2005 | ||
2006 | return to_delete; | |
0bca51f0 DN |
2007 | } |
2008 | ||
2009 | ||
2010 | /* Visit all the successor blocks of BB looking for PHI nodes. For | |
2011 | every PHI node found, check if any of its arguments is in | |
2012 | OLD_SSA_NAMES. If so, and if the argument has a current reaching | |
2013 | definition, replace it. */ | |
2014 | ||
2015 | static void | |
ccf5c864 | 2016 | rewrite_update_phi_arguments (basic_block bb) |
0bca51f0 DN |
2017 | { |
2018 | edge e; | |
2019 | edge_iterator ei; | |
2ce79879 | 2020 | unsigned i; |
0bca51f0 DN |
2021 | |
2022 | FOR_EACH_EDGE (e, ei, bb->succs) | |
2023 | { | |
538dd0b7 DM |
2024 | gphi *phi; |
2025 | vec<gphi *> phis; | |
0bca51f0 | 2026 | |
2ce79879 ZD |
2027 | if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index)) |
2028 | continue; | |
b8698a0f | 2029 | |
9771b263 DN |
2030 | phis = phis_to_rewrite[e->dest->index]; |
2031 | FOR_EACH_VEC_ELT (phis, i, phi) | |
0bca51f0 | 2032 | { |
f5045c96 | 2033 | tree arg, lhs_sym, reaching_def = NULL; |
0bca51f0 DN |
2034 | use_operand_p arg_p; |
2035 | ||
4ad14919 | 2036 | gcc_checking_assert (rewrite_uses_p (phi)); |
0bca51f0 DN |
2037 | |
2038 | arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e); | |
2039 | arg = USE_FROM_PTR (arg_p); | |
2040 | ||
2041 | if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME) | |
2042 | continue; | |
2043 | ||
726a989a | 2044 | lhs_sym = SSA_NAME_VAR (gimple_phi_result (phi)); |
38635499 | 2045 | |
0bca51f0 DN |
2046 | if (arg == NULL_TREE) |
2047 | { | |
2048 | /* When updating a PHI node for a recently introduced | |
2049 | symbol we may find NULL arguments. That's why we | |
2050 | take the symbol from the LHS of the PHI node. */ | |
f5045c96 AM |
2051 | reaching_def = get_reaching_def (lhs_sym); |
2052 | ||
0bca51f0 DN |
2053 | } |
2054 | else | |
2055 | { | |
2056 | tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg); | |
2057 | ||
13714310 | 2058 | if (marked_for_renaming (sym)) |
f5045c96 | 2059 | reaching_def = get_reaching_def (sym); |
0bca51f0 | 2060 | else if (is_old_name (arg)) |
f5045c96 AM |
2061 | reaching_def = get_reaching_def (arg); |
2062 | } | |
2063 | ||
2064 | /* Update the argument if there is a reaching def. */ | |
2065 | if (reaching_def) | |
2066 | { | |
f5045c96 AM |
2067 | source_location locus; |
2068 | int arg_i = PHI_ARG_INDEX_FROM_USE (arg_p); | |
2069 | ||
2070 | SET_USE (arg_p, reaching_def); | |
f5045c96 | 2071 | |
ffc5b2bb RB |
2072 | /* Virtual operands do not need a location. */ |
2073 | if (virtual_operand_p (reaching_def)) | |
2074 | locus = UNKNOWN_LOCATION; | |
f5045c96 | 2075 | else |
ffc5b2bb RB |
2076 | { |
2077 | gimple stmt = SSA_NAME_DEF_STMT (reaching_def); | |
538dd0b7 | 2078 | gphi *other_phi = dyn_cast <gphi *> (stmt); |
ffc5b2bb RB |
2079 | |
2080 | /* Single element PHI nodes behave like copies, so get the | |
2081 | location from the phi argument. */ | |
538dd0b7 DM |
2082 | if (other_phi |
2083 | && gimple_phi_num_args (other_phi) == 1) | |
2084 | locus = gimple_phi_arg_location (other_phi, 0); | |
ffc5b2bb RB |
2085 | else |
2086 | locus = gimple_location (stmt); | |
2087 | } | |
f5045c96 AM |
2088 | |
2089 | gimple_phi_arg_set_location (phi, arg_i, locus); | |
0bca51f0 DN |
2090 | } |
2091 | ||
f5045c96 | 2092 | |
0bca51f0 DN |
2093 | if (e->flags & EDGE_ABNORMAL) |
2094 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1; | |
2095 | } | |
2096 | } | |
2097 | } | |
2098 | ||
4d9192b5 TS |
2099 | class rewrite_update_dom_walker : public dom_walker |
2100 | { | |
2101 | public: | |
2102 | rewrite_update_dom_walker (cdi_direction direction) : dom_walker (direction) {} | |
2103 | ||
2104 | virtual void before_dom_children (basic_block); | |
2105 | virtual void after_dom_children (basic_block); | |
2106 | }; | |
0bca51f0 | 2107 | |
ccf5c864 PB |
2108 | /* Initialization of block data structures for the incremental SSA |
2109 | update pass. Create a block local stack of reaching definitions | |
2110 | for new SSA names produced in this block (BLOCK_DEFS). Register | |
2111 | new definitions for every PHI node in the block. */ | |
2112 | ||
4d9192b5 TS |
2113 | void |
2114 | rewrite_update_dom_walker::before_dom_children (basic_block bb) | |
ccf5c864 | 2115 | { |
ccf5c864 | 2116 | bool is_abnormal_phi; |
ccf5c864 PB |
2117 | |
2118 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
427f99e2 | 2119 | fprintf (dump_file, "Registering new PHI nodes in block #%d\n", |
ccf5c864 PB |
2120 | bb->index); |
2121 | ||
2122 | /* Mark the unwind point for this block. */ | |
9771b263 | 2123 | block_defs_stack.safe_push (NULL_TREE); |
ccf5c864 PB |
2124 | |
2125 | if (!bitmap_bit_p (blocks_to_update, bb->index)) | |
2126 | return; | |
2127 | ||
2128 | /* Mark the LHS if any of the arguments flows through an abnormal | |
2129 | edge. */ | |
31ff2426 | 2130 | is_abnormal_phi = bb_has_abnormal_pred (bb); |
ccf5c864 PB |
2131 | |
2132 | /* If any of the PHI nodes is a replacement for a name in | |
2133 | OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then | |
2134 | register it as a new definition for its corresponding name. Also | |
2135 | register definitions for names whose underlying symbols are | |
2136 | marked for renaming. */ | |
538dd0b7 DM |
2137 | for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi); |
2138 | gsi_next (&gsi)) | |
ccf5c864 PB |
2139 | { |
2140 | tree lhs, lhs_sym; | |
538dd0b7 | 2141 | gphi *phi = gsi.phi (); |
ccf5c864 PB |
2142 | |
2143 | if (!register_defs_p (phi)) | |
2144 | continue; | |
b8698a0f | 2145 | |
ccf5c864 PB |
2146 | lhs = gimple_phi_result (phi); |
2147 | lhs_sym = SSA_NAME_VAR (lhs); | |
2148 | ||
13714310 | 2149 | if (marked_for_renaming (lhs_sym)) |
ccf5c864 PB |
2150 | register_new_update_single (lhs, lhs_sym); |
2151 | else | |
2152 | { | |
2153 | ||
2154 | /* If LHS is a new name, register a new definition for all | |
2155 | the names replaced by LHS. */ | |
2156 | if (is_new_name (lhs)) | |
2157 | register_new_update_set (lhs, names_replaced_by (lhs)); | |
b8698a0f | 2158 | |
ccf5c864 PB |
2159 | /* If LHS is an OLD name, register it as a new definition |
2160 | for itself. */ | |
2161 | if (is_old_name (lhs)) | |
2162 | register_new_update_single (lhs, lhs); | |
2163 | } | |
2164 | ||
2165 | if (is_abnormal_phi) | |
2166 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1; | |
2167 | } | |
2168 | ||
2169 | /* Step 2. Rewrite every variable used in each statement in the block. */ | |
d7c028c0 | 2170 | if (bitmap_bit_p (interesting_blocks, bb->index)) |
3a56edc7 | 2171 | { |
4ad14919 | 2172 | gcc_checking_assert (bitmap_bit_p (blocks_to_update, bb->index)); |
538dd0b7 | 2173 | for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); ) |
956623c1 MG |
2174 | if (rewrite_update_stmt (gsi_stmt (gsi), gsi)) |
2175 | gsi_remove (&gsi, true); | |
2176 | else | |
2177 | gsi_next (&gsi); | |
3a56edc7 | 2178 | } |
ccf5c864 PB |
2179 | |
2180 | /* Step 3. Update PHI nodes. */ | |
2181 | rewrite_update_phi_arguments (bb); | |
2182 | } | |
2183 | ||
2184 | /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore | |
2185 | the current reaching definition of every name re-written in BB to | |
2186 | the original reaching definition before visiting BB. This | |
2187 | unwinding must be done in the opposite order to what is done in | |
2188 | register_new_update_set. */ | |
2189 | ||
4d9192b5 TS |
2190 | void |
2191 | rewrite_update_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED) | |
ccf5c864 | 2192 | { |
9771b263 | 2193 | while (block_defs_stack.length () > 0) |
ccf5c864 | 2194 | { |
9771b263 | 2195 | tree var = block_defs_stack.pop (); |
ccf5c864 | 2196 | tree saved_def; |
b8698a0f | 2197 | |
ccf5c864 PB |
2198 | /* NULL indicates the unwind stop point for this block (see |
2199 | rewrite_update_enter_block). */ | |
2200 | if (var == NULL) | |
2201 | return; | |
2202 | ||
9771b263 | 2203 | saved_def = block_defs_stack.pop (); |
8812aab1 | 2204 | get_common_info (var)->current_def = saved_def; |
ccf5c864 PB |
2205 | } |
2206 | } | |
2207 | ||
2208 | ||
0bca51f0 | 2209 | /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA |
b8698a0f | 2210 | form. |
0bca51f0 DN |
2211 | |
2212 | ENTRY indicates the block where to start. Every block dominated by | |
2213 | ENTRY will be rewritten. | |
2214 | ||
2215 | WHAT indicates what actions will be taken by the renamer (see enum | |
2216 | rewrite_mode). | |
2217 | ||
2218 | BLOCKS are the set of interesting blocks for the dominator walker | |
2219 | to process. If this set is NULL, then all the nodes dominated | |
2220 | by ENTRY are walked. Otherwise, blocks dominated by ENTRY that | |
2221 | are not present in BLOCKS are ignored. */ | |
2222 | ||
2223 | static void | |
ccf5c864 | 2224 | rewrite_blocks (basic_block entry, enum rewrite_mode what) |
0bca51f0 | 2225 | { |
0bca51f0 DN |
2226 | /* Rewrite all the basic blocks in the program. */ |
2227 | timevar_push (TV_TREE_SSA_REWRITE_BLOCKS); | |
2228 | ||
4d9192b5 | 2229 | block_defs_stack.create (10); |
0bca51f0 | 2230 | |
4d9192b5 TS |
2231 | /* Recursively walk the dominator tree rewriting each statement in |
2232 | each basic block. */ | |
0bca51f0 | 2233 | if (what == REWRITE_ALL) |
4d9192b5 | 2234 | rewrite_dom_walker (CDI_DOMINATORS).walk (entry); |
0bca51f0 | 2235 | else if (what == REWRITE_UPDATE) |
4d9192b5 | 2236 | rewrite_update_dom_walker (CDI_DOMINATORS).walk (entry); |
0bca51f0 DN |
2237 | else |
2238 | gcc_unreachable (); | |
2239 | ||
0bca51f0 DN |
2240 | /* Debugging dumps. */ |
2241 | if (dump_file && (dump_flags & TDF_STATS)) | |
2242 | { | |
2243 | dump_dfa_stats (dump_file); | |
c203e8a7 | 2244 | if (var_infos) |
0bca51f0 DN |
2245 | dump_tree_ssa_stats (dump_file); |
2246 | } | |
b8698a0f | 2247 | |
9771b263 | 2248 | block_defs_stack.release (); |
0bca51f0 DN |
2249 | |
2250 | timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS); | |
2251 | } | |
2252 | ||
4d9192b5 | 2253 | class mark_def_dom_walker : public dom_walker |
7256233c | 2254 | { |
4d9192b5 TS |
2255 | public: |
2256 | mark_def_dom_walker (cdi_direction direction); | |
2257 | ~mark_def_dom_walker (); | |
5f240ec4 | 2258 | |
4d9192b5 | 2259 | virtual void before_dom_children (basic_block); |
5f240ec4 | 2260 | |
4d9192b5 | 2261 | private: |
7256233c DN |
2262 | /* Notice that this bitmap is indexed using variable UIDs, so it must be |
2263 | large enough to accommodate all the variables referenced in the | |
2264 | function, not just the ones we are renaming. */ | |
65d3284b | 2265 | bitmap m_kills; |
4d9192b5 | 2266 | }; |
6de9cd9a | 2267 | |
4d9192b5 | 2268 | mark_def_dom_walker::mark_def_dom_walker (cdi_direction direction) |
65d3284b | 2269 | : dom_walker (direction), m_kills (BITMAP_ALLOC (NULL)) |
4d9192b5 TS |
2270 | { |
2271 | } | |
7256233c | 2272 | |
4d9192b5 TS |
2273 | mark_def_dom_walker::~mark_def_dom_walker () |
2274 | { | |
65d3284b | 2275 | BITMAP_FREE (m_kills); |
4d9192b5 | 2276 | } |
7256233c | 2277 | |
4d9192b5 TS |
2278 | /* Block processing routine for mark_def_sites. Clear the KILLS bitmap |
2279 | at the start of each block, and call mark_def_sites for each statement. */ | |
7256233c | 2280 | |
4d9192b5 TS |
2281 | void |
2282 | mark_def_dom_walker::before_dom_children (basic_block bb) | |
2283 | { | |
2284 | gimple_stmt_iterator gsi; | |
7256233c | 2285 | |
65d3284b | 2286 | bitmap_clear (m_kills); |
4d9192b5 | 2287 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) |
65d3284b | 2288 | mark_def_sites (bb, gsi_stmt (gsi), m_kills); |
6de9cd9a DN |
2289 | } |
2290 | ||
38635499 DN |
2291 | /* Initialize internal data needed during renaming. */ |
2292 | ||
2293 | static void | |
2294 | init_ssa_renamer (void) | |
2295 | { | |
38635499 DN |
2296 | cfun->gimple_df->in_ssa_p = false; |
2297 | ||
2298 | /* Allocate memory for the DEF_BLOCKS hash table. */ | |
c203e8a7 TS |
2299 | gcc_assert (!var_infos); |
2300 | var_infos = new hash_table<var_info_hasher> | |
2301 | (vec_safe_length (cfun->local_decls)); | |
38635499 | 2302 | |
b4e209fd | 2303 | bitmap_obstack_initialize (&update_ssa_obstack); |
38635499 DN |
2304 | } |
2305 | ||
2306 | ||
2307 | /* Deallocate internal data structures used by the renamer. */ | |
2308 | ||
2309 | static void | |
2310 | fini_ssa_renamer (void) | |
2311 | { | |
c203e8a7 TS |
2312 | delete var_infos; |
2313 | var_infos = NULL; | |
38635499 | 2314 | |
b4e209fd RG |
2315 | bitmap_obstack_release (&update_ssa_obstack); |
2316 | ||
13714310 RG |
2317 | cfun->gimple_df->ssa_renaming_needed = 0; |
2318 | cfun->gimple_df->rename_vops = 0; | |
38635499 DN |
2319 | cfun->gimple_df->in_ssa_p = true; |
2320 | } | |
2321 | ||
7256233c | 2322 | /* Main entry point into the SSA builder. The renaming process |
0bca51f0 | 2323 | proceeds in four main phases: |
6de9cd9a | 2324 | |
0bca51f0 DN |
2325 | 1- Compute dominance frontier and immediate dominators, needed to |
2326 | insert PHI nodes and rename the function in dominator tree | |
2327 | order. | |
6de9cd9a | 2328 | |
4d9192b5 | 2329 | 2- Find and mark all the blocks that define variables. |
6de9cd9a | 2330 | |
0bca51f0 | 2331 | 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes). |
6de9cd9a | 2332 | |
0bca51f0 | 2333 | 4- Rename all the blocks (rewrite_blocks) and statements in the program. |
6de9cd9a | 2334 | |
77bfa778 | 2335 | Steps 3 and 4 are done using the dominator tree walker |
0bca51f0 | 2336 | (walk_dominator_tree). */ |
7256233c | 2337 | |
be55bfe6 TS |
2338 | namespace { |
2339 | ||
2340 | const pass_data pass_data_build_ssa = | |
2341 | { | |
2342 | GIMPLE_PASS, /* type */ | |
2343 | "ssa", /* name */ | |
2344 | OPTGROUP_NONE, /* optinfo_flags */ | |
be55bfe6 TS |
2345 | TV_TREE_SSA_OTHER, /* tv_id */ |
2346 | PROP_cfg, /* properties_required */ | |
2347 | PROP_ssa, /* properties_provided */ | |
2348 | 0, /* properties_destroyed */ | |
2349 | 0, /* todo_flags_start */ | |
3bea341f | 2350 | TODO_remove_unused_locals, /* todo_flags_finish */ |
be55bfe6 TS |
2351 | }; |
2352 | ||
2353 | class pass_build_ssa : public gimple_opt_pass | |
2354 | { | |
2355 | public: | |
2356 | pass_build_ssa (gcc::context *ctxt) | |
2357 | : gimple_opt_pass (pass_data_build_ssa, ctxt) | |
2358 | {} | |
2359 | ||
2360 | /* opt_pass methods: */ | |
2361 | virtual bool gate (function *fun) | |
2362 | { | |
2363 | /* Do nothing for funcions that was produced already in SSA form. */ | |
2364 | return !(fun->curr_properties & PROP_ssa); | |
2365 | } | |
2366 | ||
2367 | virtual unsigned int execute (function *); | |
2368 | ||
2369 | }; // class pass_build_ssa | |
2370 | ||
2371 | unsigned int | |
2372 | pass_build_ssa::execute (function *fun) | |
6de9cd9a | 2373 | { |
0fc555fb | 2374 | bitmap_head *dfs; |
7256233c | 2375 | basic_block bb; |
70b5e7dc | 2376 | unsigned i; |
b8698a0f | 2377 | |
0bca51f0 | 2378 | /* Initialize operand data structures. */ |
be55bfe6 | 2379 | init_ssa_operands (fun); |
6de9cd9a | 2380 | |
38635499 DN |
2381 | /* Initialize internal data needed by the renamer. */ |
2382 | init_ssa_renamer (); | |
2383 | ||
0bca51f0 DN |
2384 | /* Initialize the set of interesting blocks. The callback |
2385 | mark_def_sites will add to this set those blocks that the renamer | |
2386 | should process. */ | |
be55bfe6 | 2387 | interesting_blocks = sbitmap_alloc (last_basic_block_for_fn (fun)); |
f61e445a | 2388 | bitmap_clear (interesting_blocks); |
6de9cd9a | 2389 | |
af5d3a18 | 2390 | /* Initialize dominance frontier. */ |
be55bfe6 TS |
2391 | dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (fun)); |
2392 | FOR_EACH_BB_FN (bb, fun) | |
0fc555fb | 2393 | bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack); |
6de9cd9a | 2394 | |
0bca51f0 DN |
2395 | /* 1- Compute dominance frontiers. */ |
2396 | calculate_dominance_info (CDI_DOMINATORS); | |
7256233c | 2397 | compute_dominance_frontiers (dfs); |
6de9cd9a | 2398 | |
0bca51f0 | 2399 | /* 2- Find and mark definition sites. */ |
be55bfe6 | 2400 | mark_def_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr); |
0bca51f0 DN |
2401 | |
2402 | /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */ | |
84d65814 | 2403 | insert_phi_nodes (dfs); |
6de9cd9a | 2404 | |
0bca51f0 | 2405 | /* 4- Rename all the blocks. */ |
be55bfe6 | 2406 | rewrite_blocks (ENTRY_BLOCK_PTR_FOR_FN (fun), REWRITE_ALL); |
6de9cd9a | 2407 | |
7256233c | 2408 | /* Free allocated memory. */ |
be55bfe6 | 2409 | FOR_EACH_BB_FN (bb, fun) |
0fc555fb | 2410 | bitmap_clear (&dfs[bb->index]); |
7256233c | 2411 | free (dfs); |
6de9cd9a | 2412 | |
b5dcb2b9 JL |
2413 | sbitmap_free (interesting_blocks); |
2414 | ||
38635499 DN |
2415 | fini_ssa_renamer (); |
2416 | ||
70b5e7dc RG |
2417 | /* Try to get rid of all gimplifier generated temporaries by making |
2418 | its SSA names anonymous. This way we can garbage collect them | |
2419 | all after removing unused locals which we do in our TODO. */ | |
2420 | for (i = 1; i < num_ssa_names; ++i) | |
2421 | { | |
2422 | tree decl, name = ssa_name (i); | |
2423 | if (!name | |
2424 | || SSA_NAME_IS_DEFAULT_DEF (name)) | |
2425 | continue; | |
2426 | decl = SSA_NAME_VAR (name); | |
2427 | if (decl | |
2428 | && TREE_CODE (decl) == VAR_DECL | |
2429 | && !VAR_DECL_IS_VIRTUAL_OPERAND (decl) | |
74fc8b8a RB |
2430 | && DECL_IGNORED_P (decl)) |
2431 | SET_SSA_NAME_VAR_OR_IDENTIFIER (name, DECL_NAME (decl)); | |
70b5e7dc RG |
2432 | } |
2433 | ||
c2924966 | 2434 | return 0; |
6de9cd9a DN |
2435 | } |
2436 | ||
27a4cd48 DM |
2437 | } // anon namespace |
2438 | ||
2439 | gimple_opt_pass * | |
2440 | make_pass_build_ssa (gcc::context *ctxt) | |
2441 | { | |
2442 | return new pass_build_ssa (ctxt); | |
2443 | } | |
2444 | ||
6de9cd9a | 2445 | |
0bca51f0 DN |
2446 | /* Mark the definition of VAR at STMT and BB as interesting for the |
2447 | renamer. BLOCKS is the set of blocks that need updating. */ | |
6de9cd9a | 2448 | |
0bca51f0 | 2449 | static void |
726a989a | 2450 | mark_def_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p) |
6de9cd9a | 2451 | { |
4ad14919 | 2452 | gcc_checking_assert (bitmap_bit_p (blocks_to_update, bb->index)); |
726a989a | 2453 | set_register_defs (stmt, true); |
0bca51f0 DN |
2454 | |
2455 | if (insert_phi_p) | |
2456 | { | |
726a989a | 2457 | bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI; |
0bca51f0 | 2458 | |
0bca51f0 DN |
2459 | set_def_block (var, bb, is_phi_p); |
2460 | ||
2461 | /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition | |
2462 | site for both itself and all the old names replaced by it. */ | |
2463 | if (TREE_CODE (var) == SSA_NAME && is_new_name (var)) | |
2464 | { | |
2465 | bitmap_iterator bi; | |
2466 | unsigned i; | |
2467 | bitmap set = names_replaced_by (var); | |
2468 | if (set) | |
2469 | EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi) | |
2470 | set_def_block (ssa_name (i), bb, is_phi_p); | |
2471 | } | |
2472 | } | |
2473 | } | |
2474 | ||
2475 | ||
2476 | /* Mark the use of VAR at STMT and BB as interesting for the | |
2477 | renamer. INSERT_PHI_P is true if we are going to insert new PHI | |
95dd3097 | 2478 | nodes. */ |
0bca51f0 DN |
2479 | |
2480 | static inline void | |
726a989a | 2481 | mark_use_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p) |
0bca51f0 | 2482 | { |
726a989a | 2483 | basic_block def_bb = gimple_bb (stmt); |
2ce79879 | 2484 | |
95dd3097 ZD |
2485 | mark_block_for_update (def_bb); |
2486 | mark_block_for_update (bb); | |
2487 | ||
726a989a | 2488 | if (gimple_code (stmt) == GIMPLE_PHI) |
538dd0b7 | 2489 | mark_phi_for_rewrite (def_bb, as_a <gphi *> (stmt)); |
2ce79879 | 2490 | else |
b5b8b0ac AO |
2491 | { |
2492 | set_rewrite_uses (stmt, true); | |
2493 | ||
2494 | if (is_gimple_debug (stmt)) | |
2495 | return; | |
2496 | } | |
0bca51f0 DN |
2497 | |
2498 | /* If VAR has not been defined in BB, then it is live-on-entry | |
2499 | to BB. Note that we cannot just use the block holding VAR's | |
2500 | definition because if VAR is one of the names in OLD_SSA_NAMES, | |
2501 | it will have several definitions (itself and all the names that | |
2502 | replace it). */ | |
2503 | if (insert_phi_p) | |
2504 | { | |
8812aab1 | 2505 | struct def_blocks_d *db_p = get_def_blocks_for (get_common_info (var)); |
0bca51f0 DN |
2506 | if (!bitmap_bit_p (db_p->def_blocks, bb->index)) |
2507 | set_livein_block (var, bb); | |
2508 | } | |
2509 | } | |
2510 | ||
2511 | ||
0bca51f0 | 2512 | /* Do a dominator walk starting at BB processing statements that |
13714310 | 2513 | reference symbols in SSA operands. This is very similar to |
84d65814 | 2514 | mark_def_sites, but the scan handles statements whose operands may |
95dd3097 | 2515 | already be SSA names. |
0bca51f0 | 2516 | |
84d65814 DN |
2517 | If INSERT_PHI_P is true, mark those uses as live in the |
2518 | corresponding block. This is later used by the PHI placement | |
38635499 DN |
2519 | algorithm to make PHI pruning decisions. |
2520 | ||
2521 | FIXME. Most of this would be unnecessary if we could associate a | |
2522 | symbol to all the SSA names that reference it. But that | |
2523 | sounds like it would be expensive to maintain. Still, it | |
2524 | would be interesting to see if it makes better sense to do | |
2525 | that. */ | |
0bca51f0 DN |
2526 | |
2527 | static void | |
95dd3097 | 2528 | prepare_block_for_update (basic_block bb, bool insert_phi_p) |
0bca51f0 DN |
2529 | { |
2530 | basic_block son; | |
f074ff6c ZD |
2531 | edge e; |
2532 | edge_iterator ei; | |
0bca51f0 | 2533 | |
95dd3097 ZD |
2534 | mark_block_for_update (bb); |
2535 | ||
0bca51f0 | 2536 | /* Process PHI nodes marking interesting those that define or use |
84d65814 | 2537 | the symbols that we are interested in. */ |
538dd0b7 DM |
2538 | for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si); |
2539 | gsi_next (&si)) | |
0bca51f0 | 2540 | { |
538dd0b7 | 2541 | gphi *phi = si.phi (); |
726a989a | 2542 | tree lhs_sym, lhs = gimple_phi_result (phi); |
0bca51f0 | 2543 | |
13714310 | 2544 | if (TREE_CODE (lhs) == SSA_NAME |
a471762f RG |
2545 | && (! virtual_operand_p (lhs) |
2546 | || ! cfun->gimple_df->rename_vops)) | |
f074ff6c | 2547 | continue; |
726a989a | 2548 | |
a471762f | 2549 | lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs); |
13714310 | 2550 | mark_for_renaming (lhs_sym); |
f074ff6c ZD |
2551 | mark_def_interesting (lhs_sym, phi, bb, insert_phi_p); |
2552 | ||
2553 | /* Mark the uses in phi nodes as interesting. It would be more correct | |
2554 | to process the arguments of the phi nodes of the successor edges of | |
2555 | BB at the end of prepare_block_for_update, however, that turns out | |
2556 | to be significantly more expensive. Doing it here is conservatively | |
2557 | correct -- it may only cause us to believe a value to be live in a | |
2558 | block that also contains its definition, and thus insert a few more | |
2559 | phi nodes for it. */ | |
2560 | FOR_EACH_EDGE (e, ei, bb->preds) | |
726a989a | 2561 | mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p); |
0bca51f0 DN |
2562 | } |
2563 | ||
2564 | /* Process the statements. */ | |
538dd0b7 DM |
2565 | for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si); |
2566 | gsi_next (&si)) | |
0bca51f0 | 2567 | { |
726a989a | 2568 | gimple stmt; |
0bca51f0 DN |
2569 | ssa_op_iter i; |
2570 | use_operand_p use_p; | |
2571 | def_operand_p def_p; | |
b8698a0f | 2572 | |
726a989a | 2573 | stmt = gsi_stmt (si); |
0bca51f0 | 2574 | |
13714310 RG |
2575 | if (cfun->gimple_df->rename_vops |
2576 | && gimple_vuse (stmt)) | |
0bca51f0 | 2577 | { |
13714310 | 2578 | tree use = gimple_vuse (stmt); |
0bca51f0 | 2579 | tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use); |
13714310 RG |
2580 | mark_for_renaming (sym); |
2581 | mark_use_interesting (sym, stmt, bb, insert_phi_p); | |
0bca51f0 DN |
2582 | } |
2583 | ||
13714310 | 2584 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE) |
0bca51f0 | 2585 | { |
13714310 RG |
2586 | tree use = USE_FROM_PTR (use_p); |
2587 | if (!DECL_P (use)) | |
2588 | continue; | |
2589 | mark_for_renaming (use); | |
2590 | mark_use_interesting (use, stmt, bb, insert_phi_p); | |
2591 | } | |
2592 | ||
2593 | if (cfun->gimple_df->rename_vops | |
2594 | && gimple_vdef (stmt)) | |
2595 | { | |
2596 | tree def = gimple_vdef (stmt); | |
0bca51f0 | 2597 | tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def); |
13714310 RG |
2598 | mark_for_renaming (sym); |
2599 | mark_def_interesting (sym, stmt, bb, insert_phi_p); | |
2600 | } | |
2601 | ||
2602 | FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_DEF) | |
2603 | { | |
2604 | tree def = DEF_FROM_PTR (def_p); | |
2605 | if (!DECL_P (def)) | |
2606 | continue; | |
2607 | mark_for_renaming (def); | |
2608 | mark_def_interesting (def, stmt, bb, insert_phi_p); | |
0bca51f0 DN |
2609 | } |
2610 | } | |
2611 | ||
2612 | /* Now visit all the blocks dominated by BB. */ | |
84d65814 | 2613 | for (son = first_dom_son (CDI_DOMINATORS, bb); |
38635499 DN |
2614 | son; |
2615 | son = next_dom_son (CDI_DOMINATORS, son)) | |
95dd3097 | 2616 | prepare_block_for_update (son, insert_phi_p); |
84d65814 DN |
2617 | } |
2618 | ||
2619 | ||
2620 | /* Helper for prepare_names_to_update. Mark all the use sites for | |
2621 | NAME as interesting. BLOCKS and INSERT_PHI_P are as in | |
2622 | prepare_names_to_update. */ | |
2623 | ||
2624 | static void | |
95dd3097 | 2625 | prepare_use_sites_for (tree name, bool insert_phi_p) |
84d65814 DN |
2626 | { |
2627 | use_operand_p use_p; | |
2628 | imm_use_iterator iter; | |
2629 | ||
2630 | FOR_EACH_IMM_USE_FAST (use_p, iter, name) | |
2631 | { | |
726a989a RB |
2632 | gimple stmt = USE_STMT (use_p); |
2633 | basic_block bb = gimple_bb (stmt); | |
84d65814 | 2634 | |
726a989a | 2635 | if (gimple_code (stmt) == GIMPLE_PHI) |
84d65814 | 2636 | { |
f074ff6c | 2637 | int ix = PHI_ARG_INDEX_FROM_USE (use_p); |
538dd0b7 | 2638 | edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), ix); |
f074ff6c | 2639 | mark_use_interesting (name, stmt, e->src, insert_phi_p); |
84d65814 DN |
2640 | } |
2641 | else | |
2642 | { | |
2643 | /* For regular statements, mark this as an interesting use | |
2644 | for NAME. */ | |
95dd3097 | 2645 | mark_use_interesting (name, stmt, bb, insert_phi_p); |
84d65814 DN |
2646 | } |
2647 | } | |
0bca51f0 DN |
2648 | } |
2649 | ||
2650 | ||
84d65814 DN |
2651 | /* Helper for prepare_names_to_update. Mark the definition site for |
2652 | NAME as interesting. BLOCKS and INSERT_PHI_P are as in | |
2653 | prepare_names_to_update. */ | |
0bca51f0 DN |
2654 | |
2655 | static void | |
95dd3097 | 2656 | prepare_def_site_for (tree name, bool insert_phi_p) |
0bca51f0 | 2657 | { |
726a989a | 2658 | gimple stmt; |
0bca51f0 DN |
2659 | basic_block bb; |
2660 | ||
4ad14919 RG |
2661 | gcc_checking_assert (names_to_release == NULL |
2662 | || !bitmap_bit_p (names_to_release, | |
2663 | SSA_NAME_VERSION (name))); | |
0bca51f0 DN |
2664 | |
2665 | stmt = SSA_NAME_DEF_STMT (name); | |
726a989a | 2666 | bb = gimple_bb (stmt); |
0bca51f0 DN |
2667 | if (bb) |
2668 | { | |
8b1c6fd7 | 2669 | gcc_checking_assert (bb->index < last_basic_block_for_fn (cfun)); |
95dd3097 ZD |
2670 | mark_block_for_update (bb); |
2671 | mark_def_interesting (name, stmt, bb, insert_phi_p); | |
0bca51f0 DN |
2672 | } |
2673 | } | |
2674 | ||
2675 | ||
84d65814 | 2676 | /* Mark definition and use sites of names in NEW_SSA_NAMES and |
95dd3097 ZD |
2677 | OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert |
2678 | PHI nodes for newly created names. */ | |
0bca51f0 DN |
2679 | |
2680 | static void | |
95dd3097 | 2681 | prepare_names_to_update (bool insert_phi_p) |
0bca51f0 | 2682 | { |
dfea6c85 | 2683 | unsigned i = 0; |
0bca51f0 | 2684 | bitmap_iterator bi; |
b6e7e9af | 2685 | sbitmap_iterator sbi; |
0bca51f0 DN |
2686 | |
2687 | /* If a name N from NEW_SSA_NAMES is also marked to be released, | |
2688 | remove it from NEW_SSA_NAMES so that we don't try to visit its | |
2689 | defining basic block (which most likely doesn't exist). Notice | |
2690 | that we cannot do the same with names in OLD_SSA_NAMES because we | |
2691 | want to replace existing instances. */ | |
2692 | if (names_to_release) | |
2693 | EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi) | |
d7c028c0 | 2694 | bitmap_clear_bit (new_ssa_names, i); |
0bca51f0 | 2695 | |
84d65814 DN |
2696 | /* First process names in NEW_SSA_NAMES. Otherwise, uses of old |
2697 | names may be considered to be live-in on blocks that contain | |
2698 | definitions for their replacements. */ | |
d4ac4ce2 | 2699 | EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi) |
95dd3097 | 2700 | prepare_def_site_for (ssa_name (i), insert_phi_p); |
84d65814 | 2701 | |
0bca51f0 DN |
2702 | /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from |
2703 | OLD_SSA_NAMES, but we have to ignore its definition site. */ | |
d4ac4ce2 | 2704 | EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi) |
84d65814 DN |
2705 | { |
2706 | if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i)) | |
95dd3097 ZD |
2707 | prepare_def_site_for (ssa_name (i), insert_phi_p); |
2708 | prepare_use_sites_for (ssa_name (i), insert_phi_p); | |
b6e7e9af | 2709 | } |
0bca51f0 DN |
2710 | } |
2711 | ||
2712 | ||
2713 | /* Dump all the names replaced by NAME to FILE. */ | |
2714 | ||
2715 | void | |
2716 | dump_names_replaced_by (FILE *file, tree name) | |
2717 | { | |
2718 | unsigned i; | |
2719 | bitmap old_set; | |
2720 | bitmap_iterator bi; | |
2721 | ||
2722 | print_generic_expr (file, name, 0); | |
2723 | fprintf (file, " -> { "); | |
2724 | ||
2725 | old_set = names_replaced_by (name); | |
2726 | EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi) | |
2727 | { | |
2728 | print_generic_expr (file, ssa_name (i), 0); | |
2729 | fprintf (file, " "); | |
2730 | } | |
2731 | ||
2732 | fprintf (file, "}\n"); | |
2733 | } | |
2734 | ||
2735 | ||
2736 | /* Dump all the names replaced by NAME to stderr. */ | |
2737 | ||
24e47c76 | 2738 | DEBUG_FUNCTION void |
0bca51f0 DN |
2739 | debug_names_replaced_by (tree name) |
2740 | { | |
2741 | dump_names_replaced_by (stderr, name); | |
2742 | } | |
2743 | ||
2744 | ||
84d65814 | 2745 | /* Dump SSA update information to FILE. */ |
0bca51f0 DN |
2746 | |
2747 | void | |
84d65814 | 2748 | dump_update_ssa (FILE *file) |
0bca51f0 | 2749 | { |
dfea6c85 | 2750 | unsigned i = 0; |
0bca51f0 DN |
2751 | bitmap_iterator bi; |
2752 | ||
5006671f | 2753 | if (!need_ssa_update_p (cfun)) |
0bca51f0 DN |
2754 | return; |
2755 | ||
f61e445a | 2756 | if (new_ssa_names && bitmap_first_set_bit (new_ssa_names) >= 0) |
0bca51f0 | 2757 | { |
b6e7e9af KH |
2758 | sbitmap_iterator sbi; |
2759 | ||
0bca51f0 DN |
2760 | fprintf (file, "\nSSA replacement table\n"); |
2761 | fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces " | |
2762 | "O_1, ..., O_j\n\n"); | |
2763 | ||
d4ac4ce2 | 2764 | EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi) |
b6e7e9af | 2765 | dump_names_replaced_by (file, ssa_name (i)); |
0bca51f0 DN |
2766 | } |
2767 | ||
13714310 | 2768 | if (symbols_to_rename_set && !bitmap_empty_p (symbols_to_rename_set)) |
0bca51f0 | 2769 | { |
427f99e2 | 2770 | fprintf (file, "\nSymbols to be put in SSA form\n"); |
13714310 | 2771 | dump_decl_set (file, symbols_to_rename_set); |
5006671f | 2772 | fprintf (file, "\n"); |
0bca51f0 DN |
2773 | } |
2774 | ||
0bca51f0 DN |
2775 | if (names_to_release && !bitmap_empty_p (names_to_release)) |
2776 | { | |
427f99e2 | 2777 | fprintf (file, "\nSSA names to release after updating the SSA web\n\n"); |
0bca51f0 DN |
2778 | EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi) |
2779 | { | |
2780 | print_generic_expr (file, ssa_name (i), 0); | |
2781 | fprintf (file, " "); | |
2782 | } | |
427f99e2 | 2783 | fprintf (file, "\n"); |
0bca51f0 | 2784 | } |
0bca51f0 DN |
2785 | } |
2786 | ||
2787 | ||
84d65814 | 2788 | /* Dump SSA update information to stderr. */ |
0bca51f0 | 2789 | |
24e47c76 | 2790 | DEBUG_FUNCTION void |
84d65814 | 2791 | debug_update_ssa (void) |
0bca51f0 | 2792 | { |
84d65814 | 2793 | dump_update_ssa (stderr); |
0bca51f0 DN |
2794 | } |
2795 | ||
2796 | ||
2797 | /* Initialize data structures used for incremental SSA updates. */ | |
2798 | ||
2799 | static void | |
5006671f | 2800 | init_update_ssa (struct function *fn) |
0bca51f0 | 2801 | { |
84d65814 | 2802 | /* Reserve more space than the current number of names. The calls to |
0bca51f0 DN |
2803 | add_new_name_mapping are typically done after creating new SSA |
2804 | names, so we'll need to reallocate these arrays. */ | |
2805 | old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR); | |
f61e445a | 2806 | bitmap_clear (old_ssa_names); |
0bca51f0 DN |
2807 | |
2808 | new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR); | |
f61e445a | 2809 | bitmap_clear (new_ssa_names); |
0bca51f0 | 2810 | |
891f2df6 RG |
2811 | bitmap_obstack_initialize (&update_ssa_obstack); |
2812 | ||
0bca51f0 | 2813 | names_to_release = NULL; |
5006671f | 2814 | update_ssa_initialized_fn = fn; |
0bca51f0 DN |
2815 | } |
2816 | ||
2817 | ||
2818 | /* Deallocate data structures used for incremental SSA updates. */ | |
2819 | ||
84d65814 | 2820 | void |
0bca51f0 DN |
2821 | delete_update_ssa (void) |
2822 | { | |
2823 | unsigned i; | |
2824 | bitmap_iterator bi; | |
2825 | ||
2826 | sbitmap_free (old_ssa_names); | |
2827 | old_ssa_names = NULL; | |
2828 | ||
2829 | sbitmap_free (new_ssa_names); | |
2830 | new_ssa_names = NULL; | |
2831 | ||
13714310 RG |
2832 | BITMAP_FREE (symbols_to_rename_set); |
2833 | symbols_to_rename_set = NULL; | |
9771b263 | 2834 | symbols_to_rename.release (); |
0bca51f0 DN |
2835 | |
2836 | if (names_to_release) | |
2837 | { | |
2838 | EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi) | |
2839 | release_ssa_name (ssa_name (i)); | |
2840 | BITMAP_FREE (names_to_release); | |
2841 | } | |
2842 | ||
95dd3097 | 2843 | clear_ssa_name_info (); |
38635499 DN |
2844 | |
2845 | fini_ssa_renamer (); | |
2846 | ||
2847 | if (blocks_with_phis_to_rewrite) | |
2848 | EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi) | |
2849 | { | |
538dd0b7 | 2850 | vec<gphi *> phis = phis_to_rewrite[i]; |
9771b263 DN |
2851 | phis.release (); |
2852 | phis_to_rewrite[i].create (0); | |
38635499 DN |
2853 | } |
2854 | ||
2855 | BITMAP_FREE (blocks_with_phis_to_rewrite); | |
2856 | BITMAP_FREE (blocks_to_update); | |
891f2df6 | 2857 | |
5006671f | 2858 | update_ssa_initialized_fn = NULL; |
0bca51f0 DN |
2859 | } |
2860 | ||
2861 | ||
2862 | /* Create a new name for OLD_NAME in statement STMT and replace the | |
45db3141 RG |
2863 | operand pointed to by DEF_P with the newly created name. If DEF_P |
2864 | is NULL then STMT should be a GIMPLE assignment. | |
2865 | Return the new name and register the replacement mapping <NEW, OLD> in | |
0bca51f0 DN |
2866 | update_ssa's tables. */ |
2867 | ||
2868 | tree | |
726a989a | 2869 | create_new_def_for (tree old_name, gimple stmt, def_operand_p def) |
0bca51f0 | 2870 | { |
45db3141 | 2871 | tree new_name; |
0bca51f0 | 2872 | |
45db3141 RG |
2873 | timevar_push (TV_TREE_SSA_INCREMENTAL); |
2874 | ||
2875 | if (!update_ssa_initialized_fn) | |
2876 | init_update_ssa (cfun); | |
2877 | ||
2878 | gcc_assert (update_ssa_initialized_fn == cfun); | |
2879 | ||
2880 | new_name = duplicate_ssa_name (old_name, stmt); | |
2881 | if (def) | |
2882 | SET_DEF (def, new_name); | |
2883 | else | |
2884 | gimple_assign_set_lhs (stmt, new_name); | |
0bca51f0 | 2885 | |
726a989a | 2886 | if (gimple_code (stmt) == GIMPLE_PHI) |
0bca51f0 | 2887 | { |
726a989a | 2888 | basic_block bb = gimple_bb (stmt); |
0bca51f0 DN |
2889 | |
2890 | /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */ | |
31ff2426 | 2891 | SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = bb_has_abnormal_pred (bb); |
0bca51f0 DN |
2892 | } |
2893 | ||
45db3141 | 2894 | add_new_name_mapping (new_name, old_name); |
0bca51f0 DN |
2895 | |
2896 | /* For the benefit of passes that will be updating the SSA form on | |
2897 | their own, set the current reaching definition of OLD_NAME to be | |
2898 | NEW_NAME. */ | |
8812aab1 | 2899 | get_ssa_name_ann (old_name)->info.current_def = new_name; |
0bca51f0 | 2900 | |
45db3141 | 2901 | timevar_pop (TV_TREE_SSA_INCREMENTAL); |
0bca51f0 | 2902 | |
45db3141 | 2903 | return new_name; |
0bca51f0 DN |
2904 | } |
2905 | ||
2906 | ||
525174a2 | 2907 | /* Mark virtual operands of FN for renaming by update_ssa. */ |
0bca51f0 DN |
2908 | |
2909 | void | |
525174a2 | 2910 | mark_virtual_operands_for_renaming (struct function *fn) |
0bca51f0 | 2911 | { |
525174a2 RG |
2912 | fn->gimple_df->ssa_renaming_needed = 1; |
2913 | fn->gimple_df->rename_vops = 1; | |
0bca51f0 DN |
2914 | } |
2915 | ||
3d9c733e AM |
2916 | /* Replace all uses of NAME by underlying variable and mark it |
2917 | for renaming. This assumes the defining statement of NAME is | |
2918 | going to be removed. */ | |
2919 | ||
2920 | void | |
2921 | mark_virtual_operand_for_renaming (tree name) | |
2922 | { | |
2923 | tree name_var = SSA_NAME_VAR (name); | |
2924 | bool used = false; | |
2925 | imm_use_iterator iter; | |
2926 | use_operand_p use_p; | |
2927 | gimple stmt; | |
2928 | ||
2929 | gcc_assert (VAR_DECL_IS_VIRTUAL_OPERAND (name_var)); | |
2930 | FOR_EACH_IMM_USE_STMT (stmt, iter, name) | |
2931 | { | |
2932 | FOR_EACH_IMM_USE_ON_STMT (use_p, iter) | |
2933 | SET_USE (use_p, name_var); | |
2934 | used = true; | |
2935 | } | |
2936 | if (used) | |
2937 | mark_virtual_operands_for_renaming (cfun); | |
2938 | } | |
2939 | ||
2940 | /* Replace all uses of the virtual PHI result by its underlying variable | |
2941 | and mark it for renaming. This assumes the PHI node is going to be | |
2942 | removed. */ | |
2943 | ||
2944 | void | |
538dd0b7 | 2945 | mark_virtual_phi_result_for_renaming (gphi *phi) |
3d9c733e AM |
2946 | { |
2947 | if (dump_file && (dump_flags & TDF_DETAILS)) | |
2948 | { | |
2949 | fprintf (dump_file, "Marking result for renaming : "); | |
2950 | print_gimple_stmt (dump_file, phi, 0, TDF_SLIM); | |
2951 | fprintf (dump_file, "\n"); | |
2952 | } | |
2953 | ||
2954 | mark_virtual_operand_for_renaming (gimple_phi_result (phi)); | |
2955 | } | |
0bca51f0 | 2956 | |
5006671f RG |
2957 | /* Return true if there is any work to be done by update_ssa |
2958 | for function FN. */ | |
0bca51f0 DN |
2959 | |
2960 | bool | |
5006671f | 2961 | need_ssa_update_p (struct function *fn) |
0bca51f0 | 2962 | { |
5006671f RG |
2963 | gcc_assert (fn != NULL); |
2964 | return (update_ssa_initialized_fn == fn | |
13714310 | 2965 | || (fn->gimple_df && fn->gimple_df->ssa_renaming_needed)); |
0bca51f0 DN |
2966 | } |
2967 | ||
0bca51f0 DN |
2968 | /* Return true if name N has been registered in the replacement table. */ |
2969 | ||
2970 | bool | |
726a989a | 2971 | name_registered_for_update_p (tree n ATTRIBUTE_UNUSED) |
0bca51f0 | 2972 | { |
5006671f | 2973 | if (!update_ssa_initialized_fn) |
0bca51f0 DN |
2974 | return false; |
2975 | ||
5006671f RG |
2976 | gcc_assert (update_ssa_initialized_fn == cfun); |
2977 | ||
2978 | return is_new_name (n) || is_old_name (n); | |
0bca51f0 DN |
2979 | } |
2980 | ||
2981 | ||
0bca51f0 DN |
2982 | /* Mark NAME to be released after update_ssa has finished. */ |
2983 | ||
2984 | void | |
2985 | release_ssa_name_after_update_ssa (tree name) | |
2986 | { | |
5006671f | 2987 | gcc_assert (cfun && update_ssa_initialized_fn == cfun); |
0bca51f0 DN |
2988 | |
2989 | if (names_to_release == NULL) | |
2990 | names_to_release = BITMAP_ALLOC (NULL); | |
2991 | ||
2992 | bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name)); | |
2993 | } | |
2994 | ||
2995 | ||
2996 | /* Insert new PHI nodes to replace VAR. DFS contains dominance | |
2997 | frontier information. BLOCKS is the set of blocks to be updated. | |
2998 | ||
2999 | This is slightly different than the regular PHI insertion | |
3000 | algorithm. The value of UPDATE_FLAGS controls how PHI nodes for | |
3001 | real names (i.e., GIMPLE registers) are inserted: | |
b8698a0f | 3002 | |
0bca51f0 DN |
3003 | - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI |
3004 | nodes inside the region affected by the block that defines VAR | |
3005 | and the blocks that define all its replacements. All these | |
84d65814 | 3006 | definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS. |
0bca51f0 DN |
3007 | |
3008 | First, we compute the entry point to the region (ENTRY). This is | |
3009 | given by the nearest common dominator to all the definition | |
3010 | blocks. When computing the iterated dominance frontier (IDF), any | |
3011 | block not strictly dominated by ENTRY is ignored. | |
3012 | ||
3013 | We then call the standard PHI insertion algorithm with the pruned | |
3014 | IDF. | |
3015 | ||
3016 | - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real | |
3017 | names is not pruned. PHI nodes are inserted at every IDF block. */ | |
3018 | ||
3019 | static void | |
0fc555fb | 3020 | insert_updated_phi_nodes_for (tree var, bitmap_head *dfs, bitmap blocks, |
0bca51f0 DN |
3021 | unsigned update_flags) |
3022 | { | |
3023 | basic_block entry; | |
3024 | struct def_blocks_d *db; | |
3025 | bitmap idf, pruned_idf; | |
3026 | bitmap_iterator bi; | |
3027 | unsigned i; | |
3028 | ||
0bca51f0 | 3029 | if (TREE_CODE (var) == SSA_NAME) |
77a74ed7 | 3030 | gcc_checking_assert (is_old_name (var)); |
0bca51f0 | 3031 | else |
13714310 | 3032 | gcc_checking_assert (marked_for_renaming (var)); |
0bca51f0 DN |
3033 | |
3034 | /* Get all the definition sites for VAR. */ | |
3035 | db = find_def_blocks_for (var); | |
3036 | ||
3037 | /* No need to do anything if there were no definitions to VAR. */ | |
3038 | if (db == NULL || bitmap_empty_p (db->def_blocks)) | |
3039 | return; | |
3040 | ||
3041 | /* Compute the initial iterated dominance frontier. */ | |
38635499 | 3042 | idf = compute_idf (db->def_blocks, dfs); |
0bca51f0 DN |
3043 | pruned_idf = BITMAP_ALLOC (NULL); |
3044 | ||
3045 | if (TREE_CODE (var) == SSA_NAME) | |
3046 | { | |
3047 | if (update_flags == TODO_update_ssa) | |
3048 | { | |
3049 | /* If doing regular SSA updates for GIMPLE registers, we are | |
3050 | only interested in IDF blocks dominated by the nearest | |
3051 | common dominator of all the definition blocks. */ | |
3052 | entry = nearest_common_dominator_for_set (CDI_DOMINATORS, | |
3053 | db->def_blocks); | |
fefa31b5 | 3054 | if (entry != ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
0bca51f0 | 3055 | EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi) |
06e28de2 DM |
3056 | if (BASIC_BLOCK_FOR_FN (cfun, i) != entry |
3057 | && dominated_by_p (CDI_DOMINATORS, | |
3058 | BASIC_BLOCK_FOR_FN (cfun, i), entry)) | |
0bca51f0 DN |
3059 | bitmap_set_bit (pruned_idf, i); |
3060 | } | |
3061 | else | |
3062 | { | |
3063 | /* Otherwise, do not prune the IDF for VAR. */ | |
4ad14919 | 3064 | gcc_checking_assert (update_flags == TODO_update_ssa_full_phi); |
0bca51f0 DN |
3065 | bitmap_copy (pruned_idf, idf); |
3066 | } | |
3067 | } | |
3068 | else | |
3069 | { | |
3070 | /* Otherwise, VAR is a symbol that needs to be put into SSA form | |
3071 | for the first time, so we need to compute the full IDF for | |
3072 | it. */ | |
3073 | bitmap_copy (pruned_idf, idf); | |
0bca51f0 DN |
3074 | } |
3075 | ||
3076 | if (!bitmap_empty_p (pruned_idf)) | |
3077 | { | |
3078 | /* Make sure that PRUNED_IDF blocks and all their feeding blocks | |
3079 | are included in the region to be updated. The feeding blocks | |
3080 | are important to guarantee that the PHI arguments are renamed | |
3081 | properly. */ | |
38635499 DN |
3082 | |
3083 | /* FIXME, this is not needed if we are updating symbols. We are | |
3084 | already starting at the ENTRY block anyway. */ | |
0bca51f0 DN |
3085 | bitmap_ior_into (blocks, pruned_idf); |
3086 | EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi) | |
3087 | { | |
3088 | edge e; | |
3089 | edge_iterator ei; | |
06e28de2 | 3090 | basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i); |
0bca51f0 DN |
3091 | |
3092 | FOR_EACH_EDGE (e, ei, bb->preds) | |
3093 | if (e->src->index >= 0) | |
3094 | bitmap_set_bit (blocks, e->src->index); | |
3095 | } | |
3096 | ||
3097 | insert_phi_nodes_for (var, pruned_idf, true); | |
3098 | } | |
3099 | ||
3100 | BITMAP_FREE (pruned_idf); | |
3101 | BITMAP_FREE (idf); | |
3102 | } | |
3103 | ||
fa5556de RB |
3104 | /* Sort symbols_to_rename after their DECL_UID. */ |
3105 | ||
3106 | static int | |
3107 | insert_updated_phi_nodes_compare_uids (const void *a, const void *b) | |
3108 | { | |
3109 | const_tree syma = *(const const_tree *)a; | |
3110 | const_tree symb = *(const const_tree *)b; | |
3111 | if (DECL_UID (syma) == DECL_UID (symb)) | |
3112 | return 0; | |
3113 | return DECL_UID (syma) < DECL_UID (symb) ? -1 : 1; | |
3114 | } | |
0bca51f0 DN |
3115 | |
3116 | /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of | |
3117 | existing SSA names (OLD_SSA_NAMES), update the SSA form so that: | |
3118 | ||
3119 | 1- The names in OLD_SSA_NAMES dominated by the definitions of | |
3120 | NEW_SSA_NAMES are all re-written to be reached by the | |
3121 | appropriate definition from NEW_SSA_NAMES. | |
3122 | ||
3123 | 2- If needed, new PHI nodes are added to the iterated dominance | |
3124 | frontier of the blocks where each of NEW_SSA_NAMES are defined. | |
3125 | ||
3126 | The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by | |
45db3141 | 3127 | calling create_new_def_for to create new defs for names that the |
0bca51f0 DN |
3128 | caller wants to replace. |
3129 | ||
45db3141 RG |
3130 | The caller cretaes the new names to be inserted and the names that need |
3131 | to be replaced by calling create_new_def_for for each old definition | |
3132 | to be replaced. Note that the function assumes that the | |
3133 | new defining statement has already been inserted in the IL. | |
0bca51f0 DN |
3134 | |
3135 | For instance, given the following code: | |
3136 | ||
3137 | 1 L0: | |
3138 | 2 x_1 = PHI (0, x_5) | |
3139 | 3 if (x_1 < 10) | |
3140 | 4 if (x_1 > 7) | |
3141 | 5 y_2 = 0 | |
3142 | 6 else | |
3143 | 7 y_3 = x_1 + x_7 | |
3144 | 8 endif | |
3145 | 9 x_5 = x_1 + 1 | |
3146 | 10 goto L0; | |
3147 | 11 endif | |
3148 | ||
3149 | Suppose that we insert new names x_10 and x_11 (lines 4 and 8). | |
3150 | ||
3151 | 1 L0: | |
3152 | 2 x_1 = PHI (0, x_5) | |
3153 | 3 if (x_1 < 10) | |
3154 | 4 x_10 = ... | |
3155 | 5 if (x_1 > 7) | |
3156 | 6 y_2 = 0 | |
3157 | 7 else | |
3158 | 8 x_11 = ... | |
3159 | 9 y_3 = x_1 + x_7 | |
3160 | 10 endif | |
3161 | 11 x_5 = x_1 + 1 | |
3162 | 12 goto L0; | |
3163 | 13 endif | |
3164 | ||
3165 | We want to replace all the uses of x_1 with the new definitions of | |
3166 | x_10 and x_11. Note that the only uses that should be replaced are | |
3167 | those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should | |
3168 | *not* be replaced (this is why we cannot just mark symbol 'x' for | |
3169 | renaming). | |
3170 | ||
3171 | Additionally, we may need to insert a PHI node at line 11 because | |
3172 | that is a merge point for x_10 and x_11. So the use of x_1 at line | |
3173 | 11 will be replaced with the new PHI node. The insertion of PHI | |
3174 | nodes is optional. They are not strictly necessary to preserve the | |
3175 | SSA form, and depending on what the caller inserted, they may not | |
3176 | even be useful for the optimizers. UPDATE_FLAGS controls various | |
3177 | aspects of how update_ssa operates, see the documentation for | |
3178 | TODO_update_ssa*. */ | |
3179 | ||
3180 | void | |
3181 | update_ssa (unsigned update_flags) | |
3182 | { | |
0bca51f0 DN |
3183 | basic_block bb, start_bb; |
3184 | bitmap_iterator bi; | |
dfea6c85 | 3185 | unsigned i = 0; |
0bca51f0 | 3186 | bool insert_phi_p; |
b6e7e9af | 3187 | sbitmap_iterator sbi; |
13714310 | 3188 | tree sym; |
0bca51f0 | 3189 | |
580b2c2e SB |
3190 | /* Only one update flag should be set. */ |
3191 | gcc_assert (update_flags == TODO_update_ssa | |
3192 | || update_flags == TODO_update_ssa_no_phi | |
3193 | || update_flags == TODO_update_ssa_full_phi | |
3194 | || update_flags == TODO_update_ssa_only_virtuals); | |
3195 | ||
5006671f | 3196 | if (!need_ssa_update_p (cfun)) |
0bca51f0 DN |
3197 | return; |
3198 | ||
90ad8d00 TV |
3199 | #ifdef ENABLE_CHECKING |
3200 | timevar_push (TV_TREE_STMT_VERIFY); | |
3201 | ||
3202 | bool err = false; | |
3203 | ||
3204 | FOR_EACH_BB_FN (bb, cfun) | |
3205 | { | |
3206 | gimple_stmt_iterator gsi; | |
3207 | for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi)) | |
3208 | { | |
3209 | gimple stmt = gsi_stmt (gsi); | |
3210 | ||
3211 | ssa_op_iter i; | |
3212 | use_operand_p use_p; | |
3213 | FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES) | |
3214 | { | |
3215 | tree use = USE_FROM_PTR (use_p); | |
3216 | if (TREE_CODE (use) != SSA_NAME) | |
3217 | continue; | |
3218 | ||
3219 | if (SSA_NAME_IN_FREE_LIST (use)) | |
3220 | { | |
3221 | error ("statement uses released SSA name:"); | |
3222 | debug_gimple_stmt (stmt); | |
3223 | fprintf (stderr, "The use of "); | |
3224 | print_generic_expr (stderr, use, 0); | |
3225 | fprintf (stderr," should have been replaced\n"); | |
3226 | err = true; | |
3227 | } | |
3228 | } | |
3229 | } | |
3230 | } | |
3231 | ||
3232 | if (err) | |
3233 | internal_error ("cannot update SSA form"); | |
3234 | ||
3235 | timevar_pop (TV_TREE_STMT_VERIFY); | |
3236 | #endif | |
3237 | ||
0bca51f0 DN |
3238 | timevar_push (TV_TREE_SSA_INCREMENTAL); |
3239 | ||
427f99e2 MJ |
3240 | if (dump_file && (dump_flags & TDF_DETAILS)) |
3241 | fprintf (dump_file, "\nUpdating SSA:\n"); | |
3242 | ||
5006671f RG |
3243 | if (!update_ssa_initialized_fn) |
3244 | init_update_ssa (cfun); | |
580b2c2e SB |
3245 | else if (update_flags == TODO_update_ssa_only_virtuals) |
3246 | { | |
3247 | /* If we only need to update virtuals, remove all the mappings for | |
3248 | real names before proceeding. The caller is responsible for | |
3249 | having dealt with the name mappings before calling update_ssa. */ | |
f61e445a LC |
3250 | bitmap_clear (old_ssa_names); |
3251 | bitmap_clear (new_ssa_names); | |
580b2c2e SB |
3252 | } |
3253 | ||
5006671f RG |
3254 | gcc_assert (update_ssa_initialized_fn == cfun); |
3255 | ||
2ce79879 | 3256 | blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL); |
9771b263 | 3257 | if (!phis_to_rewrite.exists ()) |
8b1c6fd7 | 3258 | phis_to_rewrite.create (last_basic_block_for_fn (cfun) + 1); |
95dd3097 | 3259 | blocks_to_update = BITMAP_ALLOC (NULL); |
2ce79879 | 3260 | |
0bca51f0 DN |
3261 | /* Ensure that the dominance information is up-to-date. */ |
3262 | calculate_dominance_info (CDI_DOMINATORS); | |
3263 | ||
84d65814 | 3264 | insert_phi_p = (update_flags != TODO_update_ssa_no_phi); |
0bca51f0 | 3265 | |
84d65814 DN |
3266 | /* If there are names defined in the replacement table, prepare |
3267 | definition and use sites for all the names in NEW_SSA_NAMES and | |
3268 | OLD_SSA_NAMES. */ | |
f61e445a | 3269 | if (bitmap_first_set_bit (new_ssa_names) >= 0) |
84d65814 | 3270 | { |
95dd3097 | 3271 | prepare_names_to_update (insert_phi_p); |
84d65814 DN |
3272 | |
3273 | /* If all the names in NEW_SSA_NAMES had been marked for | |
3274 | removal, and there are no symbols to rename, then there's | |
3275 | nothing else to do. */ | |
f61e445a | 3276 | if (bitmap_first_set_bit (new_ssa_names) < 0 |
13714310 | 3277 | && !cfun->gimple_df->ssa_renaming_needed) |
84d65814 DN |
3278 | goto done; |
3279 | } | |
3280 | ||
3281 | /* Next, determine the block at which to start the renaming process. */ | |
13714310 | 3282 | if (cfun->gimple_df->ssa_renaming_needed) |
84d65814 | 3283 | { |
b4e209fd RG |
3284 | /* If we rename bare symbols initialize the mapping to |
3285 | auxiliar info we need to keep track of. */ | |
c203e8a7 | 3286 | var_infos = new hash_table<var_info_hasher> (47); |
b4e209fd | 3287 | |
84d65814 DN |
3288 | /* If we have to rename some symbols from scratch, we need to |
3289 | start the process at the root of the CFG. FIXME, it should | |
3290 | be possible to determine the nearest block that had a | |
3291 | definition for each of the symbols that are marked for | |
3292 | updating. For now this seems more work than it's worth. */ | |
fefa31b5 | 3293 | start_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun); |
84d65814 | 3294 | |
38635499 | 3295 | /* Traverse the CFG looking for existing definitions and uses of |
13714310 | 3296 | symbols in SSA operands. Mark interesting blocks and |
38635499 DN |
3297 | statements and set local live-in information for the PHI |
3298 | placement heuristics. */ | |
95dd3097 | 3299 | prepare_block_for_update (start_bb, insert_phi_p); |
343c5d46 RG |
3300 | |
3301 | #ifdef ENABLE_CHECKING | |
3302 | for (i = 1; i < num_ssa_names; ++i) | |
3303 | { | |
3304 | tree name = ssa_name (i); | |
3305 | if (!name | |
3306 | || virtual_operand_p (name)) | |
3307 | continue; | |
3308 | ||
3309 | /* For all but virtual operands, which do not have SSA names | |
3310 | with overlapping life ranges, ensure that symbols marked | |
3311 | for renaming do not have existing SSA names associated with | |
3312 | them as we do not re-write them out-of-SSA before going | |
3313 | into SSA for the remaining symbol uses. */ | |
3314 | if (marked_for_renaming (SSA_NAME_VAR (name))) | |
3315 | { | |
3316 | fprintf (stderr, "Existing SSA name for symbol marked for " | |
3317 | "renaming: "); | |
3318 | print_generic_expr (stderr, name, TDF_SLIM); | |
3319 | fprintf (stderr, "\n"); | |
3320 | internal_error ("SSA corruption"); | |
3321 | } | |
3322 | } | |
3323 | #endif | |
0bca51f0 DN |
3324 | } |
3325 | else | |
84d65814 DN |
3326 | { |
3327 | /* Otherwise, the entry block to the region is the nearest | |
3328 | common dominator for the blocks in BLOCKS. */ | |
95dd3097 ZD |
3329 | start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS, |
3330 | blocks_to_update); | |
84d65814 | 3331 | } |
0bca51f0 DN |
3332 | |
3333 | /* If requested, insert PHI nodes at the iterated dominance frontier | |
84d65814 | 3334 | of every block, creating new definitions for names in OLD_SSA_NAMES |
13714310 | 3335 | and for symbols found. */ |
0bca51f0 DN |
3336 | if (insert_phi_p) |
3337 | { | |
0fc555fb | 3338 | bitmap_head *dfs; |
9caf90a8 KH |
3339 | |
3340 | /* If the caller requested PHI nodes to be added, compute | |
3341 | dominance frontiers. */ | |
8b1c6fd7 | 3342 | dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun)); |
11cd3bed | 3343 | FOR_EACH_BB_FN (bb, cfun) |
0fc555fb | 3344 | bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack); |
9caf90a8 KH |
3345 | compute_dominance_frontiers (dfs); |
3346 | ||
f61e445a | 3347 | if (bitmap_first_set_bit (old_ssa_names) >= 0) |
0bca51f0 | 3348 | { |
b6e7e9af KH |
3349 | sbitmap_iterator sbi; |
3350 | ||
84d65814 DN |
3351 | /* insert_update_phi_nodes_for will call add_new_name_mapping |
3352 | when inserting new PHI nodes, so the set OLD_SSA_NAMES | |
3353 | will grow while we are traversing it (but it will not | |
3354 | gain any new members). Copy OLD_SSA_NAMES to a temporary | |
3355 | for traversal. */ | |
5829cc0f | 3356 | sbitmap tmp = sbitmap_alloc (SBITMAP_SIZE (old_ssa_names)); |
f61e445a | 3357 | bitmap_copy (tmp, old_ssa_names); |
d4ac4ce2 | 3358 | EXECUTE_IF_SET_IN_BITMAP (tmp, 0, i, sbi) |
95dd3097 | 3359 | insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update, |
b6e7e9af | 3360 | update_flags); |
0bca51f0 DN |
3361 | sbitmap_free (tmp); |
3362 | } | |
3363 | ||
fa5556de | 3364 | symbols_to_rename.qsort (insert_updated_phi_nodes_compare_uids); |
9771b263 | 3365 | FOR_EACH_VEC_ELT (symbols_to_rename, i, sym) |
13714310 | 3366 | insert_updated_phi_nodes_for (sym, dfs, blocks_to_update, |
38635499 | 3367 | update_flags); |
0bca51f0 | 3368 | |
11cd3bed | 3369 | FOR_EACH_BB_FN (bb, cfun) |
0fc555fb | 3370 | bitmap_clear (&dfs[bb->index]); |
9caf90a8 KH |
3371 | free (dfs); |
3372 | ||
0bca51f0 DN |
3373 | /* Insertion of PHI nodes may have added blocks to the region. |
3374 | We need to re-compute START_BB to include the newly added | |
3375 | blocks. */ | |
fefa31b5 | 3376 | if (start_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun)) |
95dd3097 ZD |
3377 | start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS, |
3378 | blocks_to_update); | |
0bca51f0 DN |
3379 | } |
3380 | ||
3381 | /* Reset the current definition for name and symbol before renaming | |
3382 | the sub-graph. */ | |
d4ac4ce2 | 3383 | EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi) |
8812aab1 | 3384 | get_ssa_name_ann (ssa_name (i))->info.current_def = NULL_TREE; |
0bca51f0 | 3385 | |
9771b263 | 3386 | FOR_EACH_VEC_ELT (symbols_to_rename, i, sym) |
8812aab1 | 3387 | get_var_info (sym)->info.current_def = NULL_TREE; |
0bca51f0 DN |
3388 | |
3389 | /* Now start the renaming process at START_BB. */ | |
8b1c6fd7 | 3390 | interesting_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun)); |
f61e445a | 3391 | bitmap_clear (interesting_blocks); |
95dd3097 | 3392 | EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi) |
d7c028c0 | 3393 | bitmap_set_bit (interesting_blocks, i); |
0bca51f0 | 3394 | |
ccf5c864 | 3395 | rewrite_blocks (start_bb, REWRITE_UPDATE); |
0bca51f0 | 3396 | |
ccf5c864 | 3397 | sbitmap_free (interesting_blocks); |
0bca51f0 DN |
3398 | |
3399 | /* Debugging dumps. */ | |
3400 | if (dump_file) | |
3401 | { | |
3402 | int c; | |
3403 | unsigned i; | |
3404 | ||
84d65814 | 3405 | dump_update_ssa (dump_file); |
0bca51f0 | 3406 | |
427f99e2 | 3407 | fprintf (dump_file, "Incremental SSA update started at block: %d\n", |
0bca51f0 DN |
3408 | start_bb->index); |
3409 | ||
3410 | c = 0; | |
95dd3097 | 3411 | EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi) |
0bca51f0 | 3412 | c++; |
8b1c6fd7 DM |
3413 | fprintf (dump_file, "Number of blocks in CFG: %d\n", |
3414 | last_basic_block_for_fn (cfun)); | |
427f99e2 | 3415 | fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n", |
8b1c6fd7 | 3416 | c, PERCENT (c, last_basic_block_for_fn (cfun))); |
0bca51f0 DN |
3417 | |
3418 | if (dump_flags & TDF_DETAILS) | |
3419 | { | |
0eb09f31 | 3420 | fprintf (dump_file, "Affected blocks:"); |
95dd3097 | 3421 | EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi) |
0eb09f31 | 3422 | fprintf (dump_file, " %u", i); |
0bca51f0 DN |
3423 | fprintf (dump_file, "\n"); |
3424 | } | |
3425 | ||
3426 | fprintf (dump_file, "\n\n"); | |
3427 | } | |
3428 | ||
3429 | /* Free allocated memory. */ | |
84d65814 | 3430 | done: |
0bca51f0 DN |
3431 | delete_update_ssa (); |
3432 | ||
3433 | timevar_pop (TV_TREE_SSA_INCREMENTAL); | |
3434 | } |