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