* config/sh/lib1funcs-Os-4-200.asm: Guard entire file with
[official-gcc.git] / gcc / tree-into-ssa.c
blob38f338a058b20f4d2f9ff29256a5ba8e656b6a07
1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to
19 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
20 Boston, MA 02110-1301, USA. */
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"
34 #include "expr.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "bitmap.h"
38 #include "tree-flow.h"
39 #include "tree-gimple.h"
40 #include "tree-inline.h"
41 #include "varray.h"
42 #include "timevar.h"
43 #include "hashtab.h"
44 #include "tree-dump.h"
45 #include "tree-pass.h"
46 #include "cfgloop.h"
47 #include "domwalk.h"
48 #include "ggc.h"
49 #include "params.h"
50 #include "vecprim.h"
52 /* This file builds the SSA form for a function as described in:
53 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
54 Computing Static Single Assignment Form and the Control Dependence
55 Graph. ACM Transactions on Programming Languages and Systems,
56 13(4):451-490, October 1991. */
58 /* True if the code is in ssa form. */
59 bool in_ssa_p;
61 /* Structure to map a variable VAR to the set of blocks that contain
62 definitions for VAR. */
63 struct def_blocks_d
65 /* The variable. */
66 tree var;
68 /* Blocks that contain definitions of VAR. Bit I will be set if the
69 Ith block contains a definition of VAR. */
70 bitmap def_blocks;
72 /* Blocks that contain a PHI node for VAR. */
73 bitmap phi_blocks;
75 /* Blocks where VAR is live-on-entry. Similar semantics as
76 DEF_BLOCKS. */
77 bitmap livein_blocks;
81 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
82 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
83 basic blocks where VAR is defined (assigned a new value). It also
84 contains a bitmap of all the blocks where VAR is live-on-entry
85 (i.e., there is a use of VAR in block B without a preceding
86 definition in B). The live-on-entry information is used when
87 computing PHI pruning heuristics. */
88 static htab_t def_blocks;
90 /* Stack of trees used to restore the global currdefs to its original
91 state after completing rewriting of a block and its dominator
92 children. Its elements have the following properties:
94 - An SSA_NAME indicates that the current definition of the
95 underlying variable should be set to the given SSA_NAME.
97 - A _DECL node indicates that the underlying variable has no
98 current definition.
100 - A NULL node is used to mark the last node associated with the
101 current block.
103 - A NULL node at the top entry is used to mark the last node
104 associated with the current block. */
105 static VEC(tree,heap) *block_defs_stack;
107 /* Set of existing SSA names being replaced by update_ssa. */
108 static sbitmap old_ssa_names;
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;
115 /* Symbols whose SSA form needs to be updated or created for the first
116 time. */
117 static bitmap syms_to_rename;
119 /* Set of SSA names that have been marked to be released after they
120 were registered in the replacement table. They will be finally
121 released after we finish updating the SSA web. */
122 static bitmap names_to_release;
124 /* For each block, the phi nodes that need to be rewritten are stored into
125 these vectors. */
127 typedef VEC(tree, heap) *tree_vec;
128 DEF_VEC_P (tree_vec);
129 DEF_VEC_ALLOC_P (tree_vec, heap);
131 static VEC(tree_vec, heap) *phis_to_rewrite;
133 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
135 static bitmap blocks_with_phis_to_rewrite;
137 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
138 to grow as the callers to register_new_name_mapping will typically
139 create new names on the fly. FIXME. Currently set to 1/3 to avoid
140 frequent reallocations but still need to find a reasonable growth
141 strategy. */
142 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
144 /* Tuple used to represent replacement mappings. */
145 struct repl_map_d
147 tree name;
148 bitmap set;
151 /* NEW -> OLD_SET replacement table. If we are replacing several
152 existing SSA names O_1, O_2, ..., O_j with a new name N_i,
153 then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }. */
154 static htab_t repl_tbl;
156 /* true if register_new_name_mapping needs to initialize the data
157 structures needed by update_ssa. */
158 static bool need_to_initialize_update_ssa_p = true;
160 /* true if update_ssa needs to update virtual operands. */
161 static bool need_to_update_vops_p = false;
163 /* Statistics kept by update_ssa to use in the virtual mapping
164 heuristic. If the number of virtual mappings is beyond certain
165 threshold, the updater will switch from using the mappings into
166 renaming the virtual symbols from scratch. In some cases, the
167 large number of name mappings for virtual names causes significant
168 slowdowns in the PHI insertion code. */
169 struct update_ssa_stats_d
171 unsigned num_virtual_mappings;
172 unsigned num_total_mappings;
173 bitmap virtual_symbols;
174 unsigned num_virtual_symbols;
176 static struct update_ssa_stats_d update_ssa_stats;
178 /* Global data to attach to the main dominator walk structure. */
179 struct mark_def_sites_global_data
181 /* This bitmap contains the variables which are set before they
182 are used in a basic block. */
183 bitmap kills;
185 /* Bitmap of names to rename. */
186 sbitmap names_to_rename;
188 /* Set of blocks that mark_def_sites deems interesting for the
189 renamer to process. */
190 sbitmap interesting_blocks;
194 /* Information stored for SSA names. */
195 struct ssa_name_info
197 /* The actual definition of the ssa name. */
198 tree current_def;
200 /* This field indicates whether or not the variable may need PHI nodes.
201 See the enum's definition for more detailed information about the
202 states. */
203 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
205 /* Age of this record (so that info_for_ssa_name table can be cleared
206 quicky); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
207 are assumed to be null. */
208 unsigned age;
211 /* The information associated with names. */
212 typedef struct ssa_name_info *ssa_name_info_p;
213 DEF_VEC_P (ssa_name_info_p);
214 DEF_VEC_ALLOC_P (ssa_name_info_p, heap);
216 static VEC(ssa_name_info_p, heap) *info_for_ssa_name;
217 static unsigned current_info_for_ssa_name_age;
219 /* The set of blocks affected by update_ssa. */
221 static bitmap blocks_to_update;
223 /* The main entry point to the SSA renamer (rewrite_blocks) may be
224 called several times to do different, but related, tasks.
225 Initially, we need it to rename the whole program into SSA form.
226 At other times, we may need it to only rename into SSA newly
227 exposed symbols. Finally, we can also call it to incrementally fix
228 an already built SSA web. */
229 enum rewrite_mode {
230 /* Convert the whole function into SSA form. */
231 REWRITE_ALL,
233 /* Incrementally update the SSA web by replacing existing SSA
234 names with new ones. See update_ssa for details. */
235 REWRITE_UPDATE
239 /* Use TREE_VISITED to keep track of which statements we want to
240 rename. When renaming a subset of the variables, not all
241 statements will be processed. This is decided in mark_def_sites. */
242 #define REWRITE_THIS_STMT(T) TREE_VISITED (T)
244 /* Use the unsigned flag to keep track of which statements we want to
245 visit when marking new definition sites. This is slightly
246 different than REWRITE_THIS_STMT: it's used by update_ssa to
247 distinguish statements that need to have both uses and defs
248 processed from those that only need to have their defs processed.
249 Statements that define new SSA names only need to have their defs
250 registered, but they don't need to have their uses renamed. */
251 #define REGISTER_DEFS_IN_THIS_STMT(T) (T)->common.unsigned_flag
254 /* Prototypes for debugging functions. */
255 extern void dump_tree_ssa (FILE *);
256 extern void debug_tree_ssa (void);
257 extern void debug_def_blocks (void);
258 extern void dump_tree_ssa_stats (FILE *);
259 extern void debug_tree_ssa_stats (void);
260 void dump_update_ssa (FILE *);
261 void debug_update_ssa (void);
262 void dump_names_replaced_by (FILE *, tree);
263 void debug_names_replaced_by (tree);
265 /* Get the information associated with NAME. */
267 static inline struct ssa_name_info *
268 get_ssa_name_ann (tree name)
270 unsigned ver = SSA_NAME_VERSION (name);
271 unsigned len = VEC_length (ssa_name_info_p, info_for_ssa_name);
272 struct ssa_name_info *info;
274 if (ver >= len)
276 unsigned new_len = num_ssa_names;
278 VEC_reserve (ssa_name_info_p, heap, info_for_ssa_name, new_len);
279 while (len++ < new_len)
281 struct ssa_name_info *info = XCNEW (struct ssa_name_info);
282 info->age = current_info_for_ssa_name_age;
283 VEC_quick_push (ssa_name_info_p, info_for_ssa_name, info);
287 info = VEC_index (ssa_name_info_p, info_for_ssa_name, ver);
288 if (info->age < current_info_for_ssa_name_age)
290 info->need_phi_state = 0;
291 info->current_def = NULL_TREE;
292 info->age = current_info_for_ssa_name_age;
295 return info;
298 /* Clears info for ssa names. */
300 static void
301 clear_ssa_name_info (void)
303 current_info_for_ssa_name_age++;
306 /* Gets phi_state field for VAR. */
308 static inline enum need_phi_state
309 get_phi_state (tree var)
311 if (TREE_CODE (var) == SSA_NAME)
312 return get_ssa_name_ann (var)->need_phi_state;
313 else
314 return var_ann (var)->need_phi_state;
318 /* Sets phi_state field for VAR to STATE. */
320 static inline void
321 set_phi_state (tree var, enum need_phi_state state)
323 if (TREE_CODE (var) == SSA_NAME)
324 get_ssa_name_ann (var)->need_phi_state = state;
325 else
326 var_ann (var)->need_phi_state = state;
330 /* Return the current definition for VAR. */
332 tree
333 get_current_def (tree var)
335 if (TREE_CODE (var) == SSA_NAME)
336 return get_ssa_name_ann (var)->current_def;
337 else
338 return var_ann (var)->current_def;
342 /* Sets current definition of VAR to DEF. */
344 void
345 set_current_def (tree var, tree def)
347 if (TREE_CODE (var) == SSA_NAME)
348 get_ssa_name_ann (var)->current_def = def;
349 else
350 var_ann (var)->current_def = def;
354 /* Compute global livein information given the set of blockx where
355 an object is locally live at the start of the block (LIVEIN)
356 and the set of blocks where the object is defined (DEF_BLOCKS).
358 Note: This routine augments the existing local livein information
359 to include global livein (i.e., it modifies the underlying bitmap
360 for LIVEIN). */
362 void
363 compute_global_livein (bitmap livein, bitmap def_blocks)
365 basic_block bb, *worklist, *tos;
366 unsigned i;
367 bitmap_iterator bi;
369 tos = worklist
370 = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
372 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
374 *tos++ = BASIC_BLOCK (i);
377 /* Iterate until the worklist is empty. */
378 while (tos != worklist)
380 edge e;
381 edge_iterator ei;
383 /* Pull a block off the worklist. */
384 bb = *--tos;
386 /* For each predecessor block. */
387 FOR_EACH_EDGE (e, ei, bb->preds)
389 basic_block pred = e->src;
390 int pred_index = pred->index;
392 /* None of this is necessary for the entry block. */
393 if (pred != ENTRY_BLOCK_PTR
394 && ! bitmap_bit_p (livein, pred_index)
395 && ! bitmap_bit_p (def_blocks, pred_index))
397 *tos++ = pred;
398 bitmap_set_bit (livein, pred_index);
403 free (worklist);
407 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
408 all statements in basic block BB. */
410 static void
411 initialize_flags_in_bb (basic_block bb)
413 tree phi, stmt;
414 block_stmt_iterator bsi;
416 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
418 REWRITE_THIS_STMT (phi) = 0;
419 REGISTER_DEFS_IN_THIS_STMT (phi) = 0;
422 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
424 stmt = bsi_stmt (bsi);
425 /* We are going to use the operand cache API, such as
426 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
427 cache for each statement should be up-to-date. */
428 gcc_assert (!stmt_modified_p (stmt));
429 REWRITE_THIS_STMT (stmt) = 0;
430 REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
434 /* Mark block BB as interesting for update_ssa. */
436 static void
437 mark_block_for_update (basic_block bb)
439 gcc_assert (blocks_to_update != NULL);
440 if (bitmap_bit_p (blocks_to_update, bb->index))
441 return;
442 bitmap_set_bit (blocks_to_update, bb->index);
443 initialize_flags_in_bb (bb);
446 /* Return the set of blocks where variable VAR is defined and the blocks
447 where VAR is live on entry (livein). If no entry is found in
448 DEF_BLOCKS, a new one is created and returned. */
450 static inline struct def_blocks_d *
451 get_def_blocks_for (tree var)
453 struct def_blocks_d db, *db_p;
454 void **slot;
456 db.var = var;
457 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
458 if (*slot == NULL)
460 db_p = XNEW (struct def_blocks_d);
461 db_p->var = var;
462 db_p->def_blocks = BITMAP_ALLOC (NULL);
463 db_p->phi_blocks = BITMAP_ALLOC (NULL);
464 db_p->livein_blocks = BITMAP_ALLOC (NULL);
465 *slot = (void *) db_p;
467 else
468 db_p = (struct def_blocks_d *) *slot;
470 return db_p;
474 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
475 VAR is defined by a PHI node. */
477 static void
478 set_def_block (tree var, basic_block bb, bool phi_p)
480 struct def_blocks_d *db_p;
481 enum need_phi_state state;
483 state = get_phi_state (var);
484 db_p = get_def_blocks_for (var);
486 /* Set the bit corresponding to the block where VAR is defined. */
487 bitmap_set_bit (db_p->def_blocks, bb->index);
488 if (phi_p)
489 bitmap_set_bit (db_p->phi_blocks, bb->index);
491 /* Keep track of whether or not we may need to insert PHI nodes.
493 If we are in the UNKNOWN state, then this is the first definition
494 of VAR. Additionally, we have not seen any uses of VAR yet, so
495 we do not need a PHI node for this variable at this time (i.e.,
496 transition to NEED_PHI_STATE_NO).
498 If we are in any other state, then we either have multiple definitions
499 of this variable occurring in different blocks or we saw a use of the
500 variable which was not dominated by the block containing the
501 definition(s). In this case we may need a PHI node, so enter
502 state NEED_PHI_STATE_MAYBE. */
503 if (state == NEED_PHI_STATE_UNKNOWN)
504 set_phi_state (var, NEED_PHI_STATE_NO);
505 else
506 set_phi_state (var, NEED_PHI_STATE_MAYBE);
510 /* Mark block BB as having VAR live at the entry to BB. */
512 static void
513 set_livein_block (tree var, basic_block bb)
515 struct def_blocks_d *db_p;
516 enum need_phi_state state = get_phi_state (var);
518 db_p = get_def_blocks_for (var);
520 /* Set the bit corresponding to the block where VAR is live in. */
521 bitmap_set_bit (db_p->livein_blocks, bb->index);
523 /* Keep track of whether or not we may need to insert PHI nodes.
525 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
526 by the single block containing the definition(s) of this variable. If
527 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
528 NEED_PHI_STATE_MAYBE. */
529 if (state == NEED_PHI_STATE_NO)
531 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
533 if (def_block_index == -1
534 || ! dominated_by_p (CDI_DOMINATORS, bb,
535 BASIC_BLOCK (def_block_index)))
536 set_phi_state (var, NEED_PHI_STATE_MAYBE);
538 else
539 set_phi_state (var, NEED_PHI_STATE_MAYBE);
543 /* Return true if symbol SYM is marked for renaming. */
545 static inline bool
546 symbol_marked_for_renaming (tree sym)
548 gcc_assert (DECL_P (sym));
549 return bitmap_bit_p (syms_to_rename, DECL_UID (sym));
553 /* Return true if NAME is in OLD_SSA_NAMES. */
555 static inline bool
556 is_old_name (tree name)
558 unsigned ver = SSA_NAME_VERSION (name);
559 return ver < new_ssa_names->n_bits && TEST_BIT (old_ssa_names, ver);
563 /* Return true if NAME is in NEW_SSA_NAMES. */
565 static inline bool
566 is_new_name (tree name)
568 unsigned ver = SSA_NAME_VERSION (name);
569 return ver < new_ssa_names->n_bits && TEST_BIT (new_ssa_names, ver);
573 /* Hashing and equality functions for REPL_TBL. */
575 static hashval_t
576 repl_map_hash (const void *p)
578 return htab_hash_pointer ((const void *)((const struct repl_map_d *)p)->name);
581 static int
582 repl_map_eq (const void *p1, const void *p2)
584 return ((const struct repl_map_d *)p1)->name
585 == ((const struct repl_map_d *)p2)->name;
588 static void
589 repl_map_free (void *p)
591 BITMAP_FREE (((struct repl_map_d *)p)->set);
592 free (p);
596 /* Return the names replaced by NEW (i.e., REPL_TBL[NEW].SET). */
598 static inline bitmap
599 names_replaced_by (tree new)
601 struct repl_map_d m;
602 void **slot;
604 m.name = new;
605 slot = htab_find_slot (repl_tbl, (void *) &m, NO_INSERT);
607 /* If N was not registered in the replacement table, return NULL. */
608 if (slot == NULL || *slot == NULL)
609 return NULL;
611 return ((struct repl_map_d *) *slot)->set;
615 /* Add OLD to REPL_TBL[NEW].SET. */
617 static inline void
618 add_to_repl_tbl (tree new, tree old)
620 struct repl_map_d m, *mp;
621 void **slot;
623 m.name = new;
624 slot = htab_find_slot (repl_tbl, (void *) &m, INSERT);
625 if (*slot == NULL)
627 mp = XNEW (struct repl_map_d);
628 mp->name = new;
629 mp->set = BITMAP_ALLOC (NULL);
630 *slot = (void *) mp;
632 else
633 mp = (struct repl_map_d *) *slot;
635 bitmap_set_bit (mp->set, SSA_NAME_VERSION (old));
639 /* Add a new mapping NEW -> OLD REPL_TBL. Every entry N_i in REPL_TBL
640 represents the set of names O_1 ... O_j replaced by N_i. This is
641 used by update_ssa and its helpers to introduce new SSA names in an
642 already formed SSA web. */
644 static void
645 add_new_name_mapping (tree new, tree old)
647 timevar_push (TV_TREE_SSA_INCREMENTAL);
649 /* OLD and NEW must be different SSA names for the same symbol. */
650 gcc_assert (new != old && SSA_NAME_VAR (new) == SSA_NAME_VAR (old));
652 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
653 caller may have created new names since the set was created. */
654 if (new_ssa_names->n_bits <= num_ssa_names - 1)
656 unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
657 new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
658 old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
661 /* If this mapping is for virtual names, we will need to update
662 virtual operands. */
663 if (!is_gimple_reg (new))
665 tree sym;
666 size_t uid;
668 need_to_update_vops_p = true;
670 /* Keep counts of virtual mappings and symbols to use in the
671 virtual mapping heuristic. If we have large numbers of
672 virtual mappings for a relatively low number of symbols, it
673 will make more sense to rename the symbols from scratch.
674 Otherwise, the insertion of PHI nodes for each of the old
675 names in these mappings will be very slow. */
676 sym = SSA_NAME_VAR (new);
677 uid = DECL_UID (sym);
678 update_ssa_stats.num_virtual_mappings++;
679 if (!bitmap_bit_p (update_ssa_stats.virtual_symbols, uid))
681 bitmap_set_bit (update_ssa_stats.virtual_symbols, uid);
682 update_ssa_stats.num_virtual_symbols++;
686 /* Update the REPL_TBL table. */
687 add_to_repl_tbl (new, old);
689 /* If OLD had already been registered as a new name, then all the
690 names that OLD replaces should also be replaced by NEW. */
691 if (is_new_name (old))
692 bitmap_ior_into (names_replaced_by (new), names_replaced_by (old));
694 /* Register NEW and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
695 respectively. */
696 SET_BIT (new_ssa_names, SSA_NAME_VERSION (new));
697 SET_BIT (old_ssa_names, SSA_NAME_VERSION (old));
699 /* Update mapping counter to use in the virtual mapping heuristic. */
700 update_ssa_stats.num_total_mappings++;
702 timevar_pop (TV_TREE_SSA_INCREMENTAL);
706 /* Call back for walk_dominator_tree used to collect definition sites
707 for every variable in the function. For every statement S in block
710 1- Variables defined by S in the DEFS of S are marked in the bitmap
711 WALK_DATA->GLOBAL_DATA->KILLS.
713 2- If S uses a variable VAR and there is no preceding kill of VAR,
714 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
716 This information is used to determine which variables are live
717 across block boundaries to reduce the number of PHI nodes
718 we create. */
720 static void
721 mark_def_sites (struct dom_walk_data *walk_data,
722 basic_block bb,
723 block_stmt_iterator bsi)
725 struct mark_def_sites_global_data *gd =
726 (struct mark_def_sites_global_data *) walk_data->global_data;
727 bitmap kills = gd->kills;
728 tree stmt, def;
729 use_operand_p use_p;
730 def_operand_p def_p;
731 ssa_op_iter iter;
733 stmt = bsi_stmt (bsi);
734 update_stmt_if_modified (stmt);
736 gcc_assert (blocks_to_update == NULL);
737 REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
738 REWRITE_THIS_STMT (stmt) = 0;
740 /* If a variable is used before being set, then the variable is live
741 across a block boundary, so mark it live-on-entry to BB. */
742 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
743 SSA_OP_USE | SSA_OP_VUSE | SSA_OP_VMUSTKILL)
745 tree sym = USE_FROM_PTR (use_p);
746 gcc_assert (DECL_P (sym));
747 if (!bitmap_bit_p (kills, DECL_UID (sym)))
748 set_livein_block (sym, bb);
749 REWRITE_THIS_STMT (stmt) = 1;
752 /* Note that virtual definitions are irrelevant for computing KILLS
753 because a V_MAY_DEF does not constitute a killing definition of the
754 variable. However, the operand of a virtual definitions is a use
755 of the variable, so it may cause the variable to be considered
756 live-on-entry. */
757 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
759 tree sym = USE_FROM_PTR (use_p);
760 gcc_assert (DECL_P (sym));
761 set_livein_block (sym, bb);
762 set_def_block (sym, bb, false);
763 REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
764 REWRITE_THIS_STMT (stmt) = 1;
767 /* Now process the defs and must-defs made by this statement. */
768 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF | SSA_OP_VMUSTDEF)
770 gcc_assert (DECL_P (def));
771 set_def_block (def, bb, false);
772 bitmap_set_bit (kills, DECL_UID (def));
773 REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
776 /* If we found the statement interesting then also mark the block BB
777 as interesting. */
778 if (REWRITE_THIS_STMT (stmt) || REGISTER_DEFS_IN_THIS_STMT (stmt))
779 SET_BIT (gd->interesting_blocks, bb->index);
782 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
783 in the dfs numbering of the dominance tree. */
785 struct dom_dfsnum
787 /* Basic block whose index this entry corresponds to. */
788 unsigned bb_index;
790 /* The dfs number of this node. */
791 unsigned dfs_num;
794 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
795 for qsort. */
797 static int
798 cmp_dfsnum (const void *a, const void *b)
800 const struct dom_dfsnum *da = a;
801 const struct dom_dfsnum *db = b;
803 return (int) da->dfs_num - (int) db->dfs_num;
806 /* Among the intervals starting at the N points specified in DEFS, find
807 the one that contains S, and return its bb_index. */
809 static unsigned
810 find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
812 unsigned f = 0, t = n, m;
814 while (t > f + 1)
816 m = (f + t) / 2;
817 if (defs[m].dfs_num <= s)
818 f = m;
819 else
820 t = m;
823 return defs[f].bb_index;
826 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
827 KILLS is a bitmap of blocks where the value is defined before any use. */
829 static void
830 prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
832 VEC(int, heap) *worklist;
833 bitmap_iterator bi;
834 unsigned i, b, p, u, top;
835 bitmap live_phis;
836 basic_block def_bb, use_bb;
837 edge e;
838 edge_iterator ei;
839 bitmap to_remove;
840 struct dom_dfsnum *defs;
841 unsigned n_defs, adef;
843 if (bitmap_empty_p (uses))
845 bitmap_clear (phis);
846 return;
849 /* The phi must dominate a use, or an argument of a live phi. Also, we
850 do not create any phi nodes in def blocks, unless they are also livein. */
851 to_remove = BITMAP_ALLOC (NULL);
852 bitmap_and_compl (to_remove, kills, uses);
853 bitmap_and_compl_into (phis, to_remove);
854 if (bitmap_empty_p (phis))
856 BITMAP_FREE (to_remove);
857 return;
860 /* We want to remove the unnecessary phi nodes, but we do not want to compute
861 liveness information, as that may be linear in the size of CFG, and if
862 there are lot of different variables to rewrite, this may lead to quadratic
863 behavior.
865 Instead, we basically emulate standard dce. We put all uses to worklist,
866 then for each of them find the nearest def that dominates them. If this
867 def is a phi node, we mark it live, and if it was not live before, we
868 add the predecessors of its basic block to the worklist.
870 To quickly locate the nearest def that dominates use, we use dfs numbering
871 of the dominance tree (that is already available in order to speed up
872 queries). For each def, we have the interval given by the dfs number on
873 entry to and on exit from the corresponding subtree in the dominance tree.
874 The nearest dominator for a given use is the smallest of these intervals
875 that contains entry and exit dfs numbers for the basic block with the use.
876 If we store the bounds for all the uses to an array and sort it, we can
877 locate the nearest dominating def in logarithmic time by binary search.*/
878 bitmap_ior (to_remove, kills, phis);
879 n_defs = bitmap_count_bits (to_remove);
880 defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
881 defs[0].bb_index = 1;
882 defs[0].dfs_num = 0;
883 adef = 1;
884 EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
886 def_bb = BASIC_BLOCK (i);
887 defs[adef].bb_index = i;
888 defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
889 defs[adef + 1].bb_index = i;
890 defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
891 adef += 2;
893 BITMAP_FREE (to_remove);
894 gcc_assert (adef == 2 * n_defs + 1);
895 qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
896 gcc_assert (defs[0].bb_index == 1);
898 /* Now each DEFS entry contains the number of the basic block to that the
899 dfs number corresponds. Change them to the number of basic block that
900 corresponds to the interval following the dfs number. Also, for the
901 dfs_out numbers, increase the dfs number by one (so that it corresponds
902 to the start of the following interval, not to the end of the current
903 one). We use WORKLIST as a stack. */
904 worklist = VEC_alloc (int, heap, n_defs + 1);
905 VEC_quick_push (int, worklist, 1);
906 top = 1;
907 n_defs = 1;
908 for (i = 1; i < adef; i++)
910 b = defs[i].bb_index;
911 if (b == top)
913 /* This is a closing element. Interval corresponding to the top
914 of the stack after removing it follows. */
915 VEC_pop (int, worklist);
916 top = VEC_index (int, worklist, VEC_length (int, worklist) - 1);
917 defs[n_defs].bb_index = top;
918 defs[n_defs].dfs_num = defs[i].dfs_num + 1;
920 else
922 /* Opening element. Nothing to do, just push it to the stack and move
923 it to the correct position. */
924 defs[n_defs].bb_index = defs[i].bb_index;
925 defs[n_defs].dfs_num = defs[i].dfs_num;
926 VEC_quick_push (int, worklist, b);
927 top = b;
930 /* If this interval starts at the same point as the previous one, cancel
931 the previous one. */
932 if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
933 defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
934 else
935 n_defs++;
937 VEC_pop (int, worklist);
938 gcc_assert (VEC_empty (int, worklist));
940 /* Now process the uses. */
941 live_phis = BITMAP_ALLOC (NULL);
942 EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
944 VEC_safe_push (int, heap, worklist, i);
947 while (!VEC_empty (int, worklist))
949 b = VEC_pop (int, worklist);
950 if (b == ENTRY_BLOCK)
951 continue;
953 /* If there is a phi node in USE_BB, it is made live. Otherwise,
954 find the def that dominates the immediate dominator of USE_BB
955 (the kill in USE_BB does not dominate the use). */
956 if (bitmap_bit_p (phis, b))
957 p = b;
958 else
960 use_bb = get_immediate_dominator (CDI_DOMINATORS, BASIC_BLOCK (b));
961 p = find_dfsnum_interval (defs, n_defs,
962 bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
963 if (!bitmap_bit_p (phis, p))
964 continue;
967 /* If the phi node is already live, there is nothing to do. */
968 if (bitmap_bit_p (live_phis, p))
969 continue;
971 /* Mark the phi as live, and add the new uses to the worklist. */
972 bitmap_set_bit (live_phis, p);
973 def_bb = BASIC_BLOCK (p);
974 FOR_EACH_EDGE (e, ei, def_bb->preds)
976 u = e->src->index;
977 if (bitmap_bit_p (uses, u))
978 continue;
980 bitmap_set_bit (uses, u);
981 VEC_safe_push (int, heap, worklist, u);
985 VEC_free (int, heap, worklist);
986 bitmap_copy (phis, live_phis);
987 BITMAP_FREE (live_phis);
988 free (defs);
991 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
992 return a bitmap with all the blocks in the iterated dominance
993 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
994 frontier information as returned by compute_dominance_frontiers.
996 The resulting set of blocks are the potential sites where PHI nodes
997 are needed. The caller is responsible from freeing the memory
998 allocated for the return value. */
1000 static bitmap
1001 find_idf (bitmap def_blocks, bitmap *dfs)
1003 bitmap_iterator bi;
1004 unsigned bb_index;
1005 VEC(int,heap) *work_stack;
1006 bitmap phi_insertion_points;
1008 work_stack = VEC_alloc (int, heap, n_basic_blocks);
1009 phi_insertion_points = BITMAP_ALLOC (NULL);
1011 /* Seed the work list with all the blocks in DEF_BLOCKS. */
1012 EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1013 /* We use VEC_quick_push here for speed. This is safe because we
1014 know that the number of definition blocks is no greater than
1015 the number of basic blocks, which is the initial capacity of
1016 WORK_STACK. */
1017 VEC_quick_push (int, work_stack, bb_index);
1019 /* Pop a block off the worklist, add every block that appears in
1020 the original block's DF that we have not already processed to
1021 the worklist. Iterate until the worklist is empty. Blocks
1022 which are added to the worklist are potential sites for
1023 PHI nodes. */
1024 while (VEC_length (int, work_stack) > 0)
1026 bb_index = VEC_pop (int, work_stack);
1028 /* Since the registration of NEW -> OLD name mappings is done
1029 separately from the call to update_ssa, when updating the SSA
1030 form, the basic blocks where new and/or old names are defined
1031 may have disappeared by CFG cleanup calls. In this case,
1032 we may pull a non-existing block from the work stack. */
1033 gcc_assert (bb_index < (unsigned) last_basic_block);
1035 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index], phi_insertion_points,
1036 0, bb_index, bi)
1038 /* Use a safe push because if there is a definition of VAR
1039 in every basic block, then WORK_STACK may eventually have
1040 more than N_BASIC_BLOCK entries. */
1041 VEC_safe_push (int, heap, work_stack, bb_index);
1042 bitmap_set_bit (phi_insertion_points, bb_index);
1046 VEC_free (int, heap, work_stack);
1048 return phi_insertion_points;
1052 /* Return the set of blocks where variable VAR is defined and the blocks
1053 where VAR is live on entry (livein). Return NULL, if no entry is
1054 found in DEF_BLOCKS. */
1056 static inline struct def_blocks_d *
1057 find_def_blocks_for (tree var)
1059 struct def_blocks_d dm;
1060 dm.var = var;
1061 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1065 /* Retrieve or create a default definition for symbol SYM. */
1067 static inline tree
1068 get_default_def_for (tree sym)
1070 tree ddef = default_def (sym);
1072 if (ddef == NULL_TREE)
1074 ddef = make_ssa_name (sym, build_empty_stmt ());
1075 set_default_def (sym, ddef);
1078 return ddef;
1082 /* Marks phi node PHI in basic block BB for rewrite. */
1084 static void
1085 mark_phi_for_rewrite (basic_block bb, tree phi)
1087 tree_vec phis;
1088 unsigned i, idx = bb->index;
1090 if (REWRITE_THIS_STMT (phi))
1091 return;
1092 REWRITE_THIS_STMT (phi) = 1;
1094 if (!blocks_with_phis_to_rewrite)
1095 return;
1097 bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
1098 VEC_reserve (tree_vec, heap, phis_to_rewrite, last_basic_block + 1);
1099 for (i = VEC_length (tree_vec, phis_to_rewrite); i <= idx; i++)
1100 VEC_quick_push (tree_vec, phis_to_rewrite, NULL);
1102 phis = VEC_index (tree_vec, phis_to_rewrite, idx);
1103 if (!phis)
1104 phis = VEC_alloc (tree, heap, 10);
1106 VEC_safe_push (tree, heap, phis, phi);
1107 VEC_replace (tree_vec, phis_to_rewrite, idx, phis);
1110 /* Insert PHI nodes for variable VAR using the iterated dominance
1111 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1112 function assumes that the caller is incrementally updating the SSA
1113 form, in which case (1) VAR is assumed to be an SSA name, (2) a new
1114 SSA name is created for VAR's symbol, and, (3) all the arguments
1115 for the newly created PHI node are set to VAR.
1117 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1118 PHI node for VAR. On exit, only the nodes that received a PHI node
1119 for VAR will be present in PHI_INSERTION_POINTS. */
1121 static void
1122 insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
1124 unsigned bb_index;
1125 edge e;
1126 tree phi;
1127 basic_block bb;
1128 bitmap_iterator bi;
1129 struct def_blocks_d *def_map;
1131 def_map = find_def_blocks_for (var);
1132 gcc_assert (def_map);
1134 /* Remove the blocks where we already have PHI nodes for VAR. */
1135 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1137 /* Remove obviously useless phi nodes. */
1138 prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
1139 def_map->livein_blocks);
1141 /* And insert the PHI nodes. */
1142 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
1144 bb = BASIC_BLOCK (bb_index);
1145 if (update_p)
1146 mark_block_for_update (bb);
1148 if (update_p && TREE_CODE (var) == SSA_NAME)
1150 /* If we are rewriting SSA names, create the LHS of the PHI
1151 node by duplicating VAR. This is useful in the case of
1152 pointers, to also duplicate pointer attributes (alias
1153 information, in particular). */
1154 edge_iterator ei;
1155 tree new_lhs;
1157 phi = create_phi_node (var, bb);
1158 new_lhs = duplicate_ssa_name (var, phi);
1159 SET_PHI_RESULT (phi, new_lhs);
1160 add_new_name_mapping (new_lhs, var);
1162 /* Add VAR to every argument slot of PHI. We need VAR in
1163 every argument so that rewrite_update_phi_arguments knows
1164 which name is this PHI node replacing. If VAR is a
1165 symbol marked for renaming, this is not necessary, the
1166 renamer will use the symbol on the LHS to get its
1167 reaching definition. */
1168 FOR_EACH_EDGE (e, ei, bb->preds)
1169 add_phi_arg (phi, var, e);
1171 else
1173 tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1174 phi = create_phi_node (sym, bb);
1177 /* Mark this PHI node as interesting for update_ssa. */
1178 REGISTER_DEFS_IN_THIS_STMT (phi) = 1;
1179 mark_phi_for_rewrite (bb, phi);
1184 /* Insert PHI nodes at the dominance frontier of blocks with variable
1185 definitions. DFS contains the dominance frontier information for
1186 the flowgraph. PHI nodes will only be inserted at the dominance
1187 frontier of definition blocks for variables whose NEED_PHI_STATE
1188 annotation is marked as ``maybe'' or ``unknown'' (computed by
1189 mark_def_sites). */
1191 static void
1192 insert_phi_nodes (bitmap *dfs)
1194 referenced_var_iterator rvi;
1195 tree var;
1197 timevar_push (TV_TREE_INSERT_PHI_NODES);
1199 FOR_EACH_REFERENCED_VAR (var, rvi)
1201 struct def_blocks_d *def_map;
1202 bitmap idf;
1204 def_map = find_def_blocks_for (var);
1205 if (def_map == NULL)
1206 continue;
1208 if (get_phi_state (var) != NEED_PHI_STATE_NO)
1210 idf = find_idf (def_map->def_blocks, dfs);
1211 insert_phi_nodes_for (var, idf, false);
1212 BITMAP_FREE (idf);
1216 timevar_pop (TV_TREE_INSERT_PHI_NODES);
1220 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
1221 variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
1222 into the stack pointed to by BLOCK_DEFS_P. */
1224 void
1225 register_new_def (tree def, VEC(tree,heap) **block_defs_p)
1227 tree var = SSA_NAME_VAR (def);
1228 tree currdef;
1230 /* If this variable is set in a single basic block and all uses are
1231 dominated by the set(s) in that single basic block, then there is
1232 no reason to record anything for this variable in the block local
1233 definition stacks. Doing so just wastes time and memory.
1235 This is the same test to prune the set of variables which may
1236 need PHI nodes. So we just use that information since it's already
1237 computed and available for us to use. */
1238 if (get_phi_state (var) == NEED_PHI_STATE_NO)
1240 set_current_def (var, def);
1241 return;
1244 currdef = get_current_def (var);
1246 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
1247 later used by the dominator tree callbacks to restore the reaching
1248 definitions for all the variables defined in the block after a recursive
1249 visit to all its immediately dominated blocks. If there is no current
1250 reaching definition, then just record the underlying _DECL node. */
1251 VEC_safe_push (tree, heap, *block_defs_p, currdef ? currdef : var);
1253 /* Set the current reaching definition for VAR to be DEF. */
1254 set_current_def (var, def);
1258 /* Perform a depth-first traversal of the dominator tree looking for
1259 variables to rename. BB is the block where to start searching.
1260 Renaming is a five step process:
1262 1- Every definition made by PHI nodes at the start of the blocks is
1263 registered as the current definition for the corresponding variable.
1265 2- Every statement in BB is rewritten. USE and VUSE operands are
1266 rewritten with their corresponding reaching definition. DEF and
1267 VDEF targets are registered as new definitions.
1269 3- All the PHI nodes in successor blocks of BB are visited. The
1270 argument corresponding to BB is replaced with its current reaching
1271 definition.
1273 4- Recursively rewrite every dominator child block of BB.
1275 5- Restore (in reverse order) the current reaching definition for every
1276 new definition introduced in this block. This is done so that when
1277 we return from the recursive call, all the current reaching
1278 definitions are restored to the names that were valid in the
1279 dominator parent of BB. */
1281 /* SSA Rewriting Step 1. Initialization, create a block local stack
1282 of reaching definitions for new SSA names produced in this block
1283 (BLOCK_DEFS). Register new definitions for every PHI node in the
1284 block. */
1286 static void
1287 rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1288 basic_block bb)
1290 tree phi;
1292 if (dump_file && (dump_flags & TDF_DETAILS))
1293 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1295 /* Mark the unwind point for this block. */
1296 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1298 /* Step 1. Register new definitions for every PHI node in the block.
1299 Conceptually, all the PHI nodes are executed in parallel and each PHI
1300 node introduces a new version for the associated variable. */
1301 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1303 tree result = PHI_RESULT (phi);
1304 register_new_def (result, &block_defs_stack);
1309 /* Return the current definition for variable VAR. If none is found,
1310 create a new SSA name to act as the zeroth definition for VAR. If VAR
1311 is call clobbered and there exists a more recent definition of
1312 GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
1313 has been clobbered by a function call since its last assignment. */
1315 static tree
1316 get_reaching_def (tree var)
1318 tree currdef_var, avar;
1320 /* Lookup the current reaching definition for VAR. */
1321 currdef_var = get_current_def (var);
1323 /* If there is no reaching definition for VAR, create and register a
1324 default definition for it (if needed). */
1325 if (currdef_var == NULL_TREE)
1327 avar = DECL_P (var) ? var : SSA_NAME_VAR (var);
1328 currdef_var = get_default_def_for (avar);
1329 set_current_def (var, currdef_var);
1332 /* Return the current reaching definition for VAR, or the default
1333 definition, if we had to create one. */
1334 return currdef_var;
1338 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1339 the block with its immediate reaching definitions. Update the current
1340 definition of a variable when a new real or virtual definition is found. */
1342 static void
1343 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1344 basic_block bb ATTRIBUTE_UNUSED,
1345 block_stmt_iterator si)
1347 tree stmt;
1348 use_operand_p use_p;
1349 def_operand_p def_p;
1350 ssa_op_iter iter;
1352 stmt = bsi_stmt (si);
1354 /* If mark_def_sites decided that we don't need to rewrite this
1355 statement, ignore it. */
1356 gcc_assert (blocks_to_update == NULL);
1357 if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
1358 return;
1360 if (dump_file && (dump_flags & TDF_DETAILS))
1362 fprintf (dump_file, "Renaming statement ");
1363 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1364 fprintf (dump_file, "\n");
1367 /* Step 1. Rewrite USES and VUSES in the statement. */
1368 if (REWRITE_THIS_STMT (stmt))
1369 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1370 SSA_OP_ALL_USES|SSA_OP_ALL_KILLS)
1372 tree var = USE_FROM_PTR (use_p);
1373 gcc_assert (DECL_P (var));
1374 SET_USE (use_p, get_reaching_def (var));
1377 /* Step 2. Register the statement's DEF and VDEF operands. */
1378 if (REGISTER_DEFS_IN_THIS_STMT (stmt))
1379 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1381 tree var = DEF_FROM_PTR (def_p);
1382 gcc_assert (DECL_P (var));
1383 SET_DEF (def_p, make_ssa_name (var, stmt));
1384 register_new_def (DEF_FROM_PTR (def_p), &block_defs_stack);
1389 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1390 PHI nodes. For every PHI node found, add a new argument containing the
1391 current reaching definition for the variable and the edge through which
1392 that definition is reaching the PHI node. */
1394 static void
1395 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1396 basic_block bb)
1398 edge e;
1399 edge_iterator ei;
1401 FOR_EACH_EDGE (e, ei, bb->succs)
1403 tree phi;
1405 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
1407 tree currdef;
1408 currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
1409 add_phi_arg (phi, currdef, e);
1415 /* Called after visiting basic block BB. Restore CURRDEFS to its
1416 original value. */
1418 static void
1419 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1420 basic_block bb ATTRIBUTE_UNUSED)
1422 /* Restore CURRDEFS to its original state. */
1423 while (VEC_length (tree, block_defs_stack) > 0)
1425 tree tmp = VEC_pop (tree, block_defs_stack);
1426 tree saved_def, var;
1428 if (tmp == NULL_TREE)
1429 break;
1431 /* If we recorded an SSA_NAME, then make the SSA_NAME the current
1432 definition of its underlying variable. If we recorded anything
1433 else, it must have been an _DECL node and its current reaching
1434 definition must have been NULL. */
1435 if (TREE_CODE (tmp) == SSA_NAME)
1437 saved_def = tmp;
1438 var = SSA_NAME_VAR (saved_def);
1440 else
1442 saved_def = NULL;
1443 var = tmp;
1446 set_current_def (var, saved_def);
1451 /* Dump SSA information to FILE. */
1453 void
1454 dump_tree_ssa (FILE *file)
1456 basic_block bb;
1457 const char *funcname
1458 = lang_hooks.decl_printable_name (current_function_decl, 2);
1460 fprintf (file, "SSA information for %s\n\n", funcname);
1462 FOR_EACH_BB (bb)
1464 dump_bb (bb, file, 0);
1465 fputs (" ", file);
1466 print_generic_stmt (file, phi_nodes (bb), dump_flags);
1467 fputs ("\n\n", file);
1472 /* Dump SSA information to stderr. */
1474 void
1475 debug_tree_ssa (void)
1477 dump_tree_ssa (stderr);
1481 /* Dump statistics for the hash table HTAB. */
1483 static void
1484 htab_statistics (FILE *file, htab_t htab)
1486 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1487 (long) htab_size (htab),
1488 (long) htab_elements (htab),
1489 htab_collisions (htab));
1493 /* Dump SSA statistics on FILE. */
1495 void
1496 dump_tree_ssa_stats (FILE *file)
1498 fprintf (file, "\nHash table statistics:\n");
1500 fprintf (file, " def_blocks: ");
1501 htab_statistics (file, def_blocks);
1503 fprintf (file, "\n");
1507 /* Dump SSA statistics on stderr. */
1509 void
1510 debug_tree_ssa_stats (void)
1512 dump_tree_ssa_stats (stderr);
1516 /* Hashing and equality functions for DEF_BLOCKS. */
1518 static hashval_t
1519 def_blocks_hash (const void *p)
1521 return htab_hash_pointer
1522 ((const void *)((const struct def_blocks_d *)p)->var);
1525 static int
1526 def_blocks_eq (const void *p1, const void *p2)
1528 return ((const struct def_blocks_d *)p1)->var
1529 == ((const struct def_blocks_d *)p2)->var;
1533 /* Free memory allocated by one entry in DEF_BLOCKS. */
1535 static void
1536 def_blocks_free (void *p)
1538 struct def_blocks_d *entry = (struct def_blocks_d *) p;
1539 BITMAP_FREE (entry->def_blocks);
1540 BITMAP_FREE (entry->phi_blocks);
1541 BITMAP_FREE (entry->livein_blocks);
1542 free (entry);
1546 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1548 static int
1549 debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
1551 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1553 fprintf (stderr, "VAR: ");
1554 print_generic_expr (stderr, db_p->var, dump_flags);
1555 bitmap_print (stderr, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1556 bitmap_print (stderr, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}\n");
1558 return 1;
1562 /* Dump the DEF_BLOCKS hash table on stderr. */
1564 void
1565 debug_def_blocks (void)
1567 htab_traverse (def_blocks, debug_def_blocks_r, NULL);
1571 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1573 static inline void
1574 register_new_update_single (tree new_name, tree old_name)
1576 tree currdef = get_current_def (old_name);
1578 /* Push the current reaching definition into *BLOCK_DEFS_P.
1579 This stack is later used by the dominator tree callbacks to
1580 restore the reaching definitions for all the variables
1581 defined in the block after a recursive visit to all its
1582 immediately dominated blocks. */
1583 VEC_reserve (tree, heap, block_defs_stack, 2);
1584 VEC_quick_push (tree, block_defs_stack, currdef);
1585 VEC_quick_push (tree, block_defs_stack, old_name);
1587 /* Set the current reaching definition for OLD_NAME to be
1588 NEW_NAME. */
1589 set_current_def (old_name, new_name);
1593 /* Register NEW_NAME to be the new reaching definition for all the
1594 names in OLD_NAMES. Used by the incremental SSA update routines to
1595 replace old SSA names with new ones. */
1597 static inline void
1598 register_new_update_set (tree new_name, bitmap old_names)
1600 bitmap_iterator bi;
1601 unsigned i;
1603 EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1604 register_new_update_single (new_name, ssa_name (i));
1608 /* Initialization of block data structures for the incremental SSA
1609 update pass. Create a block local stack of reaching definitions
1610 for new SSA names produced in this block (BLOCK_DEFS). Register
1611 new definitions for every PHI node in the block. */
1613 static void
1614 rewrite_update_init_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1615 basic_block bb)
1617 edge e;
1618 edge_iterator ei;
1619 tree phi;
1620 bool is_abnormal_phi;
1622 if (dump_file && (dump_flags & TDF_DETAILS))
1623 fprintf (dump_file, "\n\nRegistering new PHI nodes in block #%d\n\n",
1624 bb->index);
1626 /* Mark the unwind point for this block. */
1627 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1629 if (!bitmap_bit_p (blocks_to_update, bb->index))
1630 return;
1632 /* Mark the LHS if any of the arguments flows through an abnormal
1633 edge. */
1634 is_abnormal_phi = false;
1635 FOR_EACH_EDGE (e, ei, bb->preds)
1636 if (e->flags & EDGE_ABNORMAL)
1638 is_abnormal_phi = true;
1639 break;
1642 /* If any of the PHI nodes is a replacement for a name in
1643 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
1644 register it as a new definition for its corresponding name. Also
1645 register definitions for names whose underlying symbols are
1646 marked for renaming. */
1648 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1650 tree lhs, lhs_sym;
1652 if (!REGISTER_DEFS_IN_THIS_STMT (phi))
1653 continue;
1655 lhs = PHI_RESULT (phi);
1656 lhs_sym = SSA_NAME_VAR (lhs);
1658 if (symbol_marked_for_renaming (lhs_sym))
1659 register_new_update_single (lhs, lhs_sym);
1660 else
1662 /* If LHS is a new name, register a new definition for all
1663 the names replaced by LHS. */
1664 if (is_new_name (lhs))
1665 register_new_update_set (lhs, names_replaced_by (lhs));
1667 /* If LHS is an OLD name, register it as a new definition
1668 for itself. */
1669 if (is_old_name (lhs))
1670 register_new_update_single (lhs, lhs);
1673 if (is_abnormal_phi)
1674 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
1679 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
1680 the current reaching definition of every name re-written in BB to
1681 the original reaching definition before visiting BB. This
1682 unwinding must be done in the opposite order to what is done in
1683 register_new_update_set. */
1685 static void
1686 rewrite_update_fini_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1687 basic_block bb ATTRIBUTE_UNUSED)
1689 while (VEC_length (tree, block_defs_stack) > 0)
1691 tree var = VEC_pop (tree, block_defs_stack);
1692 tree saved_def;
1694 /* NULL indicates the unwind stop point for this block (see
1695 rewrite_update_init_block). */
1696 if (var == NULL)
1697 return;
1699 saved_def = VEC_pop (tree, block_defs_stack);
1700 set_current_def (var, saved_def);
1705 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1706 it is a symbol marked for renaming, replace it with USE_P's current
1707 reaching definition. */
1709 static inline void
1710 maybe_replace_use (use_operand_p use_p)
1712 tree rdef = NULL_TREE;
1713 tree use = USE_FROM_PTR (use_p);
1714 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1716 if (symbol_marked_for_renaming (sym))
1717 rdef = get_reaching_def (sym);
1718 else if (is_old_name (use))
1719 rdef = get_reaching_def (use);
1721 if (rdef && rdef != use)
1722 SET_USE (use_p, rdef);
1726 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1727 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1728 register it as the current definition for the names replaced by
1729 DEF_P. */
1731 static inline void
1732 maybe_register_def (def_operand_p def_p, tree stmt)
1734 tree def = DEF_FROM_PTR (def_p);
1735 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
1737 /* If DEF is a naked symbol that needs renaming, create a
1738 new name for it. */
1739 if (symbol_marked_for_renaming (sym))
1741 if (DECL_P (def))
1743 def = make_ssa_name (def, stmt);
1744 SET_DEF (def_p, def);
1747 register_new_update_single (def, sym);
1749 else
1751 /* If DEF is a new name, register it as a new definition
1752 for all the names replaced by DEF. */
1753 if (is_new_name (def))
1754 register_new_update_set (def, names_replaced_by (def));
1756 /* If DEF is an old name, register DEF as a new
1757 definition for itself. */
1758 if (is_old_name (def))
1759 register_new_update_single (def, def);
1764 /* Update every variable used in the statement pointed-to by SI. The
1765 statement is assumed to be in SSA form already. Names in
1766 OLD_SSA_NAMES used by SI will be updated to their current reaching
1767 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1768 will be registered as a new definition for their corresponding name
1769 in OLD_SSA_NAMES. */
1771 static void
1772 rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1773 basic_block bb ATTRIBUTE_UNUSED,
1774 block_stmt_iterator si)
1776 stmt_ann_t ann;
1777 tree stmt;
1778 use_operand_p use_p;
1779 def_operand_p def_p;
1780 ssa_op_iter iter;
1782 stmt = bsi_stmt (si);
1783 ann = stmt_ann (stmt);
1785 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
1787 /* Only update marked statements. */
1788 if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
1789 return;
1791 if (dump_file && (dump_flags & TDF_DETAILS))
1793 fprintf (dump_file, "Updating SSA information for statement ");
1794 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1795 fprintf (dump_file, "\n");
1798 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1799 symbol is marked for renaming. */
1800 if (REWRITE_THIS_STMT (stmt))
1802 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1803 maybe_replace_use (use_p);
1805 if (need_to_update_vops_p)
1806 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter,
1807 SSA_OP_VIRTUAL_USES | SSA_OP_VIRTUAL_KILLS)
1808 maybe_replace_use (use_p);
1811 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1812 Also register definitions for names whose underlying symbol is
1813 marked for renaming. */
1814 if (REGISTER_DEFS_IN_THIS_STMT (stmt))
1816 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1817 maybe_register_def (def_p, stmt);
1819 if (need_to_update_vops_p)
1820 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_VIRTUAL_DEFS)
1821 maybe_register_def (def_p, stmt);
1826 /* Replace the operand pointed to by USE_P with USE's current reaching
1827 definition. */
1829 static inline void
1830 replace_use (use_operand_p use_p, tree use)
1832 tree rdef = get_reaching_def (use);
1833 if (rdef != use)
1834 SET_USE (use_p, rdef);
1838 /* Visit all the successor blocks of BB looking for PHI nodes. For
1839 every PHI node found, check if any of its arguments is in
1840 OLD_SSA_NAMES. If so, and if the argument has a current reaching
1841 definition, replace it. */
1843 static void
1844 rewrite_update_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1845 basic_block bb)
1847 edge e;
1848 edge_iterator ei;
1849 unsigned i;
1851 FOR_EACH_EDGE (e, ei, bb->succs)
1853 tree phi;
1854 tree_vec phis;
1856 if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
1857 continue;
1859 phis = VEC_index (tree_vec, phis_to_rewrite, e->dest->index);
1860 for (i = 0; VEC_iterate (tree, phis, i, phi); i++)
1862 tree arg;
1863 use_operand_p arg_p;
1865 gcc_assert (REWRITE_THIS_STMT (phi));
1867 arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
1868 arg = USE_FROM_PTR (arg_p);
1870 if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
1871 continue;
1873 if (arg == NULL_TREE)
1875 /* When updating a PHI node for a recently introduced
1876 symbol we may find NULL arguments. That's why we
1877 take the symbol from the LHS of the PHI node. */
1878 replace_use (arg_p, SSA_NAME_VAR (PHI_RESULT (phi)));
1880 else
1882 tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
1884 if (symbol_marked_for_renaming (sym))
1885 replace_use (arg_p, sym);
1886 else if (is_old_name (arg))
1887 replace_use (arg_p, arg);
1890 if (e->flags & EDGE_ABNORMAL)
1891 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
1897 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
1898 form.
1900 ENTRY indicates the block where to start. Every block dominated by
1901 ENTRY will be rewritten.
1903 WHAT indicates what actions will be taken by the renamer (see enum
1904 rewrite_mode).
1906 BLOCKS are the set of interesting blocks for the dominator walker
1907 to process. If this set is NULL, then all the nodes dominated
1908 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
1909 are not present in BLOCKS are ignored. */
1911 static void
1912 rewrite_blocks (basic_block entry, enum rewrite_mode what, sbitmap blocks)
1914 struct dom_walk_data walk_data;
1916 /* Rewrite all the basic blocks in the program. */
1917 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1919 /* Setup callbacks for the generic dominator tree walker. */
1920 memset (&walk_data, 0, sizeof (walk_data));
1922 walk_data.dom_direction = CDI_DOMINATORS;
1923 walk_data.interesting_blocks = blocks;
1925 if (what == REWRITE_UPDATE)
1926 walk_data.before_dom_children_before_stmts = rewrite_update_init_block;
1927 else
1928 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
1930 if (what == REWRITE_ALL)
1931 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
1932 else if (what == REWRITE_UPDATE)
1933 walk_data.before_dom_children_walk_stmts = rewrite_update_stmt;
1934 else
1935 gcc_unreachable ();
1937 if (what == REWRITE_ALL)
1938 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
1939 else if (what == REWRITE_UPDATE)
1940 walk_data.before_dom_children_after_stmts = rewrite_update_phi_arguments;
1941 else
1942 gcc_unreachable ();
1944 if (what == REWRITE_ALL)
1945 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
1946 else if (what == REWRITE_UPDATE)
1947 walk_data.after_dom_children_after_stmts = rewrite_update_fini_block;
1948 else
1949 gcc_unreachable ();
1951 block_defs_stack = VEC_alloc (tree, heap, 10);
1953 /* Initialize the dominator walker. */
1954 init_walk_dominator_tree (&walk_data);
1956 /* Recursively walk the dominator tree rewriting each statement in
1957 each basic block. */
1958 walk_dominator_tree (&walk_data, entry);
1960 /* Finalize the dominator walker. */
1961 fini_walk_dominator_tree (&walk_data);
1963 /* Debugging dumps. */
1964 if (dump_file && (dump_flags & TDF_STATS))
1966 dump_dfa_stats (dump_file);
1967 if (def_blocks)
1968 dump_tree_ssa_stats (dump_file);
1971 if (def_blocks)
1973 htab_delete (def_blocks);
1974 def_blocks = NULL;
1977 VEC_free (tree, heap, block_defs_stack);
1979 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1983 /* Block initialization routine for mark_def_sites. Clear the
1984 KILLS bitmap at the start of each block. */
1986 static void
1987 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
1988 basic_block bb ATTRIBUTE_UNUSED)
1990 struct mark_def_sites_global_data *gd =
1991 (struct mark_def_sites_global_data *) walk_data->global_data;
1992 bitmap kills = gd->kills;
1993 bitmap_clear (kills);
1997 /* Mark the definition site blocks for each variable, so that we know
1998 where the variable is actually live.
2000 INTERESTING_BLOCKS will be filled in with all the blocks that
2001 should be processed by the renamer. It is assumed to be
2002 initialized and zeroed by the caller. */
2004 static void
2005 mark_def_site_blocks (sbitmap interesting_blocks)
2007 struct dom_walk_data walk_data;
2008 struct mark_def_sites_global_data mark_def_sites_global_data;
2009 referenced_var_iterator rvi;
2010 tree var;
2012 /* Allocate memory for the DEF_BLOCKS hash table. */
2013 def_blocks = htab_create (num_referenced_vars,
2014 def_blocks_hash, def_blocks_eq, def_blocks_free);
2015 FOR_EACH_REFERENCED_VAR(var, rvi)
2016 set_current_def (var, NULL_TREE);
2018 /* Setup callbacks for the generic dominator tree walker to find and
2019 mark definition sites. */
2020 walk_data.walk_stmts_backward = false;
2021 walk_data.dom_direction = CDI_DOMINATORS;
2022 walk_data.initialize_block_local_data = NULL;
2023 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
2024 walk_data.before_dom_children_walk_stmts = mark_def_sites;
2025 walk_data.before_dom_children_after_stmts = NULL;
2026 walk_data.after_dom_children_before_stmts = NULL;
2027 walk_data.after_dom_children_walk_stmts = NULL;
2028 walk_data.after_dom_children_after_stmts = NULL;
2029 walk_data.interesting_blocks = NULL;
2031 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2032 large enough to accommodate all the variables referenced in the
2033 function, not just the ones we are renaming. */
2034 mark_def_sites_global_data.kills = BITMAP_ALLOC (NULL);
2036 /* Create the set of interesting blocks that will be filled by
2037 mark_def_sites. */
2038 mark_def_sites_global_data.interesting_blocks = interesting_blocks;
2039 walk_data.global_data = &mark_def_sites_global_data;
2041 /* We do not have any local data. */
2042 walk_data.block_local_data_size = 0;
2044 /* Initialize the dominator walker. */
2045 init_walk_dominator_tree (&walk_data);
2047 /* Recursively walk the dominator tree. */
2048 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
2050 /* Finalize the dominator walker. */
2051 fini_walk_dominator_tree (&walk_data);
2053 /* We no longer need this bitmap, clear and free it. */
2054 BITMAP_FREE (mark_def_sites_global_data.kills);
2058 /* Main entry point into the SSA builder. The renaming process
2059 proceeds in four main phases:
2061 1- Compute dominance frontier and immediate dominators, needed to
2062 insert PHI nodes and rename the function in dominator tree
2063 order.
2065 2- Find and mark all the blocks that define variables
2066 (mark_def_site_blocks).
2068 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2070 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2072 Steps 3 and 4 are done using the dominator tree walker
2073 (walk_dominator_tree). */
2075 static unsigned int
2076 rewrite_into_ssa (void)
2078 bitmap *dfs;
2079 basic_block bb;
2080 sbitmap interesting_blocks;
2082 timevar_push (TV_TREE_SSA_OTHER);
2084 /* Initialize operand data structures. */
2085 init_ssa_operands ();
2087 /* Initialize the set of interesting blocks. The callback
2088 mark_def_sites will add to this set those blocks that the renamer
2089 should process. */
2090 interesting_blocks = sbitmap_alloc (last_basic_block);
2091 sbitmap_zero (interesting_blocks);
2093 /* Initialize dominance frontier. */
2094 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap));
2095 FOR_EACH_BB (bb)
2096 dfs[bb->index] = BITMAP_ALLOC (NULL);
2098 /* 1- Compute dominance frontiers. */
2099 calculate_dominance_info (CDI_DOMINATORS);
2100 compute_dominance_frontiers (dfs);
2102 /* 2- Find and mark definition sites. */
2103 mark_def_site_blocks (interesting_blocks);
2105 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2106 insert_phi_nodes (dfs);
2108 /* 4- Rename all the blocks. */
2109 rewrite_blocks (ENTRY_BLOCK_PTR, REWRITE_ALL, interesting_blocks);
2111 /* Free allocated memory. */
2112 FOR_EACH_BB (bb)
2113 BITMAP_FREE (dfs[bb->index]);
2114 free (dfs);
2115 sbitmap_free (interesting_blocks);
2117 timevar_pop (TV_TREE_SSA_OTHER);
2118 in_ssa_p = true;
2119 return 0;
2123 struct tree_opt_pass pass_build_ssa =
2125 "ssa", /* name */
2126 NULL, /* gate */
2127 rewrite_into_ssa, /* execute */
2128 NULL, /* sub */
2129 NULL, /* next */
2130 0, /* static_pass_number */
2131 0, /* tv_id */
2132 PROP_cfg | PROP_referenced_vars, /* properties_required */
2133 PROP_ssa, /* properties_provided */
2134 0, /* properties_destroyed */
2135 0, /* todo_flags_start */
2136 TODO_dump_func
2137 | TODO_verify_ssa
2138 | TODO_remove_unused_locals, /* todo_flags_finish */
2139 0 /* letter */
2143 /* Mark the definition of VAR at STMT and BB as interesting for the
2144 renamer. BLOCKS is the set of blocks that need updating. */
2146 static void
2147 mark_def_interesting (tree var, tree stmt, basic_block bb, bool insert_phi_p)
2149 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
2150 REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
2152 if (insert_phi_p)
2154 bool is_phi_p = TREE_CODE (stmt) == PHI_NODE;
2156 set_def_block (var, bb, is_phi_p);
2158 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2159 site for both itself and all the old names replaced by it. */
2160 if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2162 bitmap_iterator bi;
2163 unsigned i;
2164 bitmap set = names_replaced_by (var);
2165 if (set)
2166 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2167 set_def_block (ssa_name (i), bb, is_phi_p);
2173 /* Mark the use of VAR at STMT and BB as interesting for the
2174 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2175 nodes. */
2177 static inline void
2178 mark_use_interesting (tree var, tree stmt, basic_block bb, bool insert_phi_p)
2180 basic_block def_bb = bb_for_stmt (stmt);
2182 mark_block_for_update (def_bb);
2183 mark_block_for_update (bb);
2185 if (TREE_CODE (stmt) == PHI_NODE)
2186 mark_phi_for_rewrite (def_bb, stmt);
2187 else
2188 REWRITE_THIS_STMT (stmt) = 1;
2190 /* If VAR has not been defined in BB, then it is live-on-entry
2191 to BB. Note that we cannot just use the block holding VAR's
2192 definition because if VAR is one of the names in OLD_SSA_NAMES,
2193 it will have several definitions (itself and all the names that
2194 replace it). */
2195 if (insert_phi_p)
2197 struct def_blocks_d *db_p = get_def_blocks_for (var);
2198 if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2199 set_livein_block (var, bb);
2204 /* Do a dominator walk starting at BB processing statements that
2205 reference symbols in SYMS_TO_RENAME. This is very similar to
2206 mark_def_sites, but the scan handles statements whose operands may
2207 already be SSA names.
2209 If INSERT_PHI_P is true, mark those uses as live in the
2210 corresponding block. This is later used by the PHI placement
2211 algorithm to make PHI pruning decisions. */
2213 static void
2214 prepare_block_for_update (basic_block bb, bool insert_phi_p)
2216 basic_block son;
2217 block_stmt_iterator si;
2218 tree phi;
2219 edge e;
2220 edge_iterator ei;
2222 mark_block_for_update (bb);
2224 /* Process PHI nodes marking interesting those that define or use
2225 the symbols that we are interested in. */
2226 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2228 tree lhs_sym, lhs = PHI_RESULT (phi);
2230 lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
2232 if (!symbol_marked_for_renaming (lhs_sym))
2233 continue;
2234 mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2236 /* Mark the uses in phi nodes as interesting. It would be more correct
2237 to process the arguments of the phi nodes of the successor edges of
2238 BB at the end of prepare_block_for_update, however, that turns out
2239 to be significantly more expensive. Doing it here is conservatively
2240 correct -- it may only cause us to believe a value to be live in a
2241 block that also contains its definition, and thus insert a few more
2242 phi nodes for it. */
2243 FOR_EACH_EDGE (e, ei, bb->preds)
2245 mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
2249 /* Process the statements. */
2250 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2252 tree stmt;
2253 ssa_op_iter i;
2254 use_operand_p use_p;
2255 def_operand_p def_p;
2257 stmt = bsi_stmt (si);
2259 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
2261 tree use = USE_FROM_PTR (use_p);
2262 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2263 if (symbol_marked_for_renaming (sym))
2264 mark_use_interesting (use, stmt, bb, insert_phi_p);
2267 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_DEF)
2269 tree def = DEF_FROM_PTR (def_p);
2270 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2272 if (symbol_marked_for_renaming (sym))
2273 mark_def_interesting (def, stmt, bb, insert_phi_p);
2276 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_VIRTUAL_DEFS)
2278 tree def = DEF_FROM_PTR (def_p);
2279 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2281 if (symbol_marked_for_renaming (sym))
2283 mark_use_interesting (sym, stmt, bb, insert_phi_p);
2284 mark_def_interesting (sym, stmt, bb, insert_phi_p);
2288 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_VUSE)
2290 tree use = USE_FROM_PTR (use_p);
2291 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2293 if (symbol_marked_for_renaming (sym))
2294 mark_use_interesting (sym, stmt, bb, insert_phi_p);
2298 /* Now visit all the blocks dominated by BB. */
2299 for (son = first_dom_son (CDI_DOMINATORS, bb);
2300 son;
2301 son = next_dom_son (CDI_DOMINATORS, son))
2302 prepare_block_for_update (son, insert_phi_p);
2306 /* Helper for prepare_names_to_update. Mark all the use sites for
2307 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2308 prepare_names_to_update. */
2310 static void
2311 prepare_use_sites_for (tree name, bool insert_phi_p)
2313 use_operand_p use_p;
2314 imm_use_iterator iter;
2316 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2318 tree stmt = USE_STMT (use_p);
2319 basic_block bb = bb_for_stmt (stmt);
2321 if (TREE_CODE (stmt) == PHI_NODE)
2323 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
2324 edge e = PHI_ARG_EDGE (stmt, ix);
2325 mark_use_interesting (name, stmt, e->src, insert_phi_p);
2327 else
2329 /* For regular statements, mark this as an interesting use
2330 for NAME. */
2331 mark_use_interesting (name, stmt, bb, insert_phi_p);
2337 /* Helper for prepare_names_to_update. Mark the definition site for
2338 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2339 prepare_names_to_update. */
2341 static void
2342 prepare_def_site_for (tree name, bool insert_phi_p)
2344 tree stmt;
2345 basic_block bb;
2347 gcc_assert (names_to_release == NULL
2348 || !bitmap_bit_p (names_to_release, SSA_NAME_VERSION (name)));
2350 stmt = SSA_NAME_DEF_STMT (name);
2351 bb = bb_for_stmt (stmt);
2352 if (bb)
2354 gcc_assert (bb->index < last_basic_block);
2355 mark_block_for_update (bb);
2356 mark_def_interesting (name, stmt, bb, insert_phi_p);
2361 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2362 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2363 PHI nodes for newly created names. */
2365 static void
2366 prepare_names_to_update (bool insert_phi_p)
2368 unsigned i = 0;
2369 bitmap_iterator bi;
2370 sbitmap_iterator sbi;
2372 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2373 remove it from NEW_SSA_NAMES so that we don't try to visit its
2374 defining basic block (which most likely doesn't exist). Notice
2375 that we cannot do the same with names in OLD_SSA_NAMES because we
2376 want to replace existing instances. */
2377 if (names_to_release)
2378 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2379 RESET_BIT (new_ssa_names, i);
2381 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2382 names may be considered to be live-in on blocks that contain
2383 definitions for their replacements. */
2384 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2385 prepare_def_site_for (ssa_name (i), insert_phi_p);
2387 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2388 OLD_SSA_NAMES, but we have to ignore its definition site. */
2389 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2391 if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2392 prepare_def_site_for (ssa_name (i), insert_phi_p);
2393 prepare_use_sites_for (ssa_name (i), insert_phi_p);
2398 /* Dump all the names replaced by NAME to FILE. */
2400 void
2401 dump_names_replaced_by (FILE *file, tree name)
2403 unsigned i;
2404 bitmap old_set;
2405 bitmap_iterator bi;
2407 print_generic_expr (file, name, 0);
2408 fprintf (file, " -> { ");
2410 old_set = names_replaced_by (name);
2411 EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2413 print_generic_expr (file, ssa_name (i), 0);
2414 fprintf (file, " ");
2417 fprintf (file, "}\n");
2421 /* Dump all the names replaced by NAME to stderr. */
2423 void
2424 debug_names_replaced_by (tree name)
2426 dump_names_replaced_by (stderr, name);
2430 /* Dump SSA update information to FILE. */
2432 void
2433 dump_update_ssa (FILE *file)
2435 unsigned i = 0;
2436 bitmap_iterator bi;
2438 if (!need_ssa_update_p ())
2439 return;
2441 if (new_ssa_names && sbitmap_first_set_bit (new_ssa_names) >= 0)
2443 sbitmap_iterator sbi;
2445 fprintf (file, "\nSSA replacement table\n");
2446 fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2447 "O_1, ..., O_j\n\n");
2449 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2450 dump_names_replaced_by (file, ssa_name (i));
2452 fprintf (file, "\n");
2453 fprintf (file, "Number of virtual NEW -> OLD mappings: %7u\n",
2454 update_ssa_stats.num_virtual_mappings);
2455 fprintf (file, "Number of real NEW -> OLD mappings: %7u\n",
2456 update_ssa_stats.num_total_mappings
2457 - update_ssa_stats.num_virtual_mappings);
2458 fprintf (file, "Number of total NEW -> OLD mappings: %7u\n",
2459 update_ssa_stats.num_total_mappings);
2461 fprintf (file, "\nNumber of virtual symbols: %u\n",
2462 update_ssa_stats.num_virtual_symbols);
2465 if (syms_to_rename && !bitmap_empty_p (syms_to_rename))
2467 fprintf (file, "\n\nSymbols to be put in SSA form\n\n");
2468 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
2470 print_generic_expr (file, referenced_var (i), 0);
2471 fprintf (file, " ");
2475 if (names_to_release && !bitmap_empty_p (names_to_release))
2477 fprintf (file, "\n\nSSA names to release after updating the SSA web\n\n");
2478 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2480 print_generic_expr (file, ssa_name (i), 0);
2481 fprintf (file, " ");
2485 fprintf (file, "\n\n");
2489 /* Dump SSA update information to stderr. */
2491 void
2492 debug_update_ssa (void)
2494 dump_update_ssa (stderr);
2498 /* Initialize data structures used for incremental SSA updates. */
2500 static void
2501 init_update_ssa (void)
2503 /* Reserve more space than the current number of names. The calls to
2504 add_new_name_mapping are typically done after creating new SSA
2505 names, so we'll need to reallocate these arrays. */
2506 old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2507 sbitmap_zero (old_ssa_names);
2509 new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2510 sbitmap_zero (new_ssa_names);
2512 repl_tbl = htab_create (20, repl_map_hash, repl_map_eq, repl_map_free);
2513 need_to_initialize_update_ssa_p = false;
2514 need_to_update_vops_p = false;
2515 syms_to_rename = BITMAP_ALLOC (NULL);
2516 names_to_release = NULL;
2517 memset (&update_ssa_stats, 0, sizeof (update_ssa_stats));
2518 update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL);
2522 /* Deallocate data structures used for incremental SSA updates. */
2524 void
2525 delete_update_ssa (void)
2527 unsigned i;
2528 bitmap_iterator bi;
2530 sbitmap_free (old_ssa_names);
2531 old_ssa_names = NULL;
2533 sbitmap_free (new_ssa_names);
2534 new_ssa_names = NULL;
2536 htab_delete (repl_tbl);
2537 repl_tbl = NULL;
2539 need_to_initialize_update_ssa_p = true;
2540 need_to_update_vops_p = false;
2541 BITMAP_FREE (syms_to_rename);
2542 BITMAP_FREE (update_ssa_stats.virtual_symbols);
2544 if (names_to_release)
2546 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2547 release_ssa_name (ssa_name (i));
2548 BITMAP_FREE (names_to_release);
2551 clear_ssa_name_info ();
2555 /* Create a new name for OLD_NAME in statement STMT and replace the
2556 operand pointed to by DEF_P with the newly created name. Return
2557 the new name and register the replacement mapping <NEW, OLD> in
2558 update_ssa's tables. */
2560 tree
2561 create_new_def_for (tree old_name, tree stmt, def_operand_p def)
2563 tree new_name = duplicate_ssa_name (old_name, stmt);
2565 SET_DEF (def, new_name);
2567 if (TREE_CODE (stmt) == PHI_NODE)
2569 edge e;
2570 edge_iterator ei;
2571 basic_block bb = bb_for_stmt (stmt);
2573 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2574 FOR_EACH_EDGE (e, ei, bb->preds)
2575 if (e->flags & EDGE_ABNORMAL)
2577 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
2578 break;
2582 register_new_name_mapping (new_name, old_name);
2584 /* For the benefit of passes that will be updating the SSA form on
2585 their own, set the current reaching definition of OLD_NAME to be
2586 NEW_NAME. */
2587 set_current_def (old_name, new_name);
2589 return new_name;
2593 /* Register name NEW to be a replacement for name OLD. This function
2594 must be called for every replacement that should be performed by
2595 update_ssa. */
2597 void
2598 register_new_name_mapping (tree new, tree old)
2600 if (need_to_initialize_update_ssa_p)
2601 init_update_ssa ();
2603 add_new_name_mapping (new, old);
2607 /* Register symbol SYM to be renamed by update_ssa. */
2609 void
2610 mark_sym_for_renaming (tree sym)
2612 if (need_to_initialize_update_ssa_p)
2613 init_update_ssa ();
2615 bitmap_set_bit (syms_to_rename, DECL_UID (sym));
2617 if (!is_gimple_reg (sym))
2618 need_to_update_vops_p = true;
2622 /* Register all the symbols in SET to be renamed by update_ssa. */
2624 void
2625 mark_set_for_renaming (bitmap set)
2627 bitmap_iterator bi;
2628 unsigned i;
2630 if (bitmap_empty_p (set))
2631 return;
2633 if (need_to_initialize_update_ssa_p)
2634 init_update_ssa ();
2636 bitmap_ior_into (syms_to_rename, set);
2638 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2639 if (!is_gimple_reg (referenced_var (i)))
2641 need_to_update_vops_p = true;
2642 break;
2647 /* Return true if there is any work to be done by update_ssa. */
2649 bool
2650 need_ssa_update_p (void)
2652 return syms_to_rename || old_ssa_names || new_ssa_names;
2656 /* Return true if name N has been registered in the replacement table. */
2658 bool
2659 name_registered_for_update_p (tree n)
2661 if (!need_ssa_update_p ())
2662 return false;
2664 return is_new_name (n)
2665 || is_old_name (n)
2666 || symbol_marked_for_renaming (SSA_NAME_VAR (n));
2670 /* Return the set of all the SSA names marked to be replaced. */
2672 bitmap
2673 ssa_names_to_replace (void)
2675 unsigned i = 0;
2676 bitmap ret;
2677 sbitmap_iterator sbi;
2679 ret = BITMAP_ALLOC (NULL);
2680 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2681 bitmap_set_bit (ret, i);
2683 return ret;
2687 /* Mark NAME to be released after update_ssa has finished. */
2689 void
2690 release_ssa_name_after_update_ssa (tree name)
2692 gcc_assert (!need_to_initialize_update_ssa_p);
2694 if (names_to_release == NULL)
2695 names_to_release = BITMAP_ALLOC (NULL);
2697 bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
2701 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2702 frontier information. BLOCKS is the set of blocks to be updated.
2704 This is slightly different than the regular PHI insertion
2705 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
2706 real names (i.e., GIMPLE registers) are inserted:
2708 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2709 nodes inside the region affected by the block that defines VAR
2710 and the blocks that define all its replacements. All these
2711 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2713 First, we compute the entry point to the region (ENTRY). This is
2714 given by the nearest common dominator to all the definition
2715 blocks. When computing the iterated dominance frontier (IDF), any
2716 block not strictly dominated by ENTRY is ignored.
2718 We then call the standard PHI insertion algorithm with the pruned
2719 IDF.
2721 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
2722 names is not pruned. PHI nodes are inserted at every IDF block. */
2724 static void
2725 insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks,
2726 unsigned update_flags)
2728 basic_block entry;
2729 struct def_blocks_d *db;
2730 bitmap idf, pruned_idf;
2731 bitmap_iterator bi;
2732 unsigned i;
2734 #if defined ENABLE_CHECKING
2735 if (TREE_CODE (var) == SSA_NAME)
2736 gcc_assert (is_old_name (var));
2737 else
2738 gcc_assert (symbol_marked_for_renaming (var));
2739 #endif
2741 /* Get all the definition sites for VAR. */
2742 db = find_def_blocks_for (var);
2744 /* No need to do anything if there were no definitions to VAR. */
2745 if (db == NULL || bitmap_empty_p (db->def_blocks))
2746 return;
2748 /* Compute the initial iterated dominance frontier. */
2749 idf = find_idf (db->def_blocks, dfs);
2750 pruned_idf = BITMAP_ALLOC (NULL);
2752 if (TREE_CODE (var) == SSA_NAME)
2754 if (update_flags == TODO_update_ssa)
2756 /* If doing regular SSA updates for GIMPLE registers, we are
2757 only interested in IDF blocks dominated by the nearest
2758 common dominator of all the definition blocks. */
2759 entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
2760 db->def_blocks);
2762 if (entry != ENTRY_BLOCK_PTR)
2763 EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
2764 if (BASIC_BLOCK (i) != entry
2765 && dominated_by_p (CDI_DOMINATORS, BASIC_BLOCK (i), entry))
2766 bitmap_set_bit (pruned_idf, i);
2768 else
2770 /* Otherwise, do not prune the IDF for VAR. */
2771 gcc_assert (update_flags == TODO_update_ssa_full_phi);
2772 bitmap_copy (pruned_idf, idf);
2775 else
2777 /* Otherwise, VAR is a symbol that needs to be put into SSA form
2778 for the first time, so we need to compute the full IDF for
2779 it. */
2780 bitmap_copy (pruned_idf, idf);
2783 if (!bitmap_empty_p (pruned_idf))
2785 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
2786 are included in the region to be updated. The feeding blocks
2787 are important to guarantee that the PHI arguments are renamed
2788 properly. */
2789 bitmap_ior_into (blocks, pruned_idf);
2790 EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
2792 edge e;
2793 edge_iterator ei;
2794 basic_block bb = BASIC_BLOCK (i);
2796 FOR_EACH_EDGE (e, ei, bb->preds)
2797 if (e->src->index >= 0)
2798 bitmap_set_bit (blocks, e->src->index);
2801 insert_phi_nodes_for (var, pruned_idf, true);
2804 BITMAP_FREE (pruned_idf);
2805 BITMAP_FREE (idf);
2809 /* Heuristic to determine whether SSA name mappings for virtual names
2810 should be discarded and their symbols rewritten from scratch. When
2811 there is a large number of mappings for virtual names, the
2812 insertion of PHI nodes for the old names in the mappings takes
2813 considerable more time than if we inserted PHI nodes for the
2814 symbols instead.
2816 Currently the heuristic takes these stats into account:
2818 - Number of mappings for virtual SSA names.
2819 - Number of distinct virtual symbols involved in those mappings.
2821 If the number of virtual mappings is much larger than the number of
2822 virtual symbols, then it will be faster to compute PHI insertion
2823 spots for the symbols. Even if this involves traversing the whole
2824 CFG, which is what happens when symbols are renamed from scratch. */
2826 static bool
2827 switch_virtuals_to_full_rewrite_p (void)
2829 if (update_ssa_stats.num_virtual_mappings < (unsigned) MIN_VIRTUAL_MAPPINGS)
2830 return false;
2832 if (update_ssa_stats.num_virtual_mappings
2833 > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
2834 * update_ssa_stats.num_virtual_symbols)
2835 return true;
2837 return false;
2841 /* Remove every virtual mapping and mark all the affected virtual
2842 symbols for renaming. */
2844 static void
2845 switch_virtuals_to_full_rewrite (void)
2847 unsigned i = 0;
2848 sbitmap_iterator sbi;
2850 if (dump_file)
2852 fprintf (dump_file, "\nEnabled virtual name mapping heuristic.\n");
2853 fprintf (dump_file, "\tNumber of virtual mappings: %7u\n",
2854 update_ssa_stats.num_virtual_mappings);
2855 fprintf (dump_file, "\tNumber of unique virtual symbols: %7u\n",
2856 update_ssa_stats.num_virtual_symbols);
2857 fprintf (dump_file, "Updating FUD-chains from top of CFG will be "
2858 "faster than processing\nthe name mappings.\n\n");
2861 /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
2862 Note that it is not really necessary to remove the mappings from
2863 REPL_TBL, that would only waste time. */
2864 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2865 if (!is_gimple_reg (ssa_name (i)))
2866 RESET_BIT (new_ssa_names, i);
2868 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2869 if (!is_gimple_reg (ssa_name (i)))
2870 RESET_BIT (old_ssa_names, i);
2872 bitmap_ior_into (syms_to_rename, update_ssa_stats.virtual_symbols);
2876 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
2877 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
2879 1- The names in OLD_SSA_NAMES dominated by the definitions of
2880 NEW_SSA_NAMES are all re-written to be reached by the
2881 appropriate definition from NEW_SSA_NAMES.
2883 2- If needed, new PHI nodes are added to the iterated dominance
2884 frontier of the blocks where each of NEW_SSA_NAMES are defined.
2886 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
2887 calling register_new_name_mapping for every pair of names that the
2888 caller wants to replace.
2890 The caller identifies the new names that have been inserted and the
2891 names that need to be replaced by calling register_new_name_mapping
2892 for every pair <NEW, OLD>. Note that the function assumes that the
2893 new names have already been inserted in the IL.
2895 For instance, given the following code:
2897 1 L0:
2898 2 x_1 = PHI (0, x_5)
2899 3 if (x_1 < 10)
2900 4 if (x_1 > 7)
2901 5 y_2 = 0
2902 6 else
2903 7 y_3 = x_1 + x_7
2904 8 endif
2905 9 x_5 = x_1 + 1
2906 10 goto L0;
2907 11 endif
2909 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
2911 1 L0:
2912 2 x_1 = PHI (0, x_5)
2913 3 if (x_1 < 10)
2914 4 x_10 = ...
2915 5 if (x_1 > 7)
2916 6 y_2 = 0
2917 7 else
2918 8 x_11 = ...
2919 9 y_3 = x_1 + x_7
2920 10 endif
2921 11 x_5 = x_1 + 1
2922 12 goto L0;
2923 13 endif
2925 We want to replace all the uses of x_1 with the new definitions of
2926 x_10 and x_11. Note that the only uses that should be replaced are
2927 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
2928 *not* be replaced (this is why we cannot just mark symbol 'x' for
2929 renaming).
2931 Additionally, we may need to insert a PHI node at line 11 because
2932 that is a merge point for x_10 and x_11. So the use of x_1 at line
2933 11 will be replaced with the new PHI node. The insertion of PHI
2934 nodes is optional. They are not strictly necessary to preserve the
2935 SSA form, and depending on what the caller inserted, they may not
2936 even be useful for the optimizers. UPDATE_FLAGS controls various
2937 aspects of how update_ssa operates, see the documentation for
2938 TODO_update_ssa*. */
2940 void
2941 update_ssa (unsigned update_flags)
2943 basic_block bb, start_bb;
2944 bitmap_iterator bi;
2945 unsigned i = 0;
2946 sbitmap tmp;
2947 bool insert_phi_p;
2948 sbitmap_iterator sbi;
2950 if (!need_ssa_update_p ())
2951 return;
2953 timevar_push (TV_TREE_SSA_INCREMENTAL);
2955 blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
2956 if (!phis_to_rewrite)
2957 phis_to_rewrite = VEC_alloc (tree_vec, heap, last_basic_block);
2958 blocks_to_update = BITMAP_ALLOC (NULL);
2960 /* Ensure that the dominance information is up-to-date. */
2961 calculate_dominance_info (CDI_DOMINATORS);
2963 /* Only one update flag should be set. */
2964 gcc_assert (update_flags == TODO_update_ssa
2965 || update_flags == TODO_update_ssa_no_phi
2966 || update_flags == TODO_update_ssa_full_phi
2967 || update_flags == TODO_update_ssa_only_virtuals);
2969 /* If we only need to update virtuals, remove all the mappings for
2970 real names before proceeding. The caller is responsible for
2971 having dealt with the name mappings before calling update_ssa. */
2972 if (update_flags == TODO_update_ssa_only_virtuals)
2974 sbitmap_zero (old_ssa_names);
2975 sbitmap_zero (new_ssa_names);
2976 htab_empty (repl_tbl);
2979 insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
2981 if (insert_phi_p)
2983 /* If the caller requested PHI nodes to be added, initialize
2984 live-in information data structures (DEF_BLOCKS). */
2986 /* For each SSA name N, the DEF_BLOCKS table describes where the
2987 name is defined, which blocks have PHI nodes for N, and which
2988 blocks have uses of N (i.e., N is live-on-entry in those
2989 blocks). */
2990 def_blocks = htab_create (num_ssa_names, def_blocks_hash,
2991 def_blocks_eq, def_blocks_free);
2993 else
2995 def_blocks = NULL;
2998 /* Heuristic to avoid massive slow downs when the replacement
2999 mappings include lots of virtual names. */
3000 if (insert_phi_p && switch_virtuals_to_full_rewrite_p ())
3001 switch_virtuals_to_full_rewrite ();
3003 /* If there are names defined in the replacement table, prepare
3004 definition and use sites for all the names in NEW_SSA_NAMES and
3005 OLD_SSA_NAMES. */
3006 if (sbitmap_first_set_bit (new_ssa_names) >= 0)
3008 prepare_names_to_update (insert_phi_p);
3010 /* If all the names in NEW_SSA_NAMES had been marked for
3011 removal, and there are no symbols to rename, then there's
3012 nothing else to do. */
3013 if (sbitmap_first_set_bit (new_ssa_names) < 0
3014 && bitmap_empty_p (syms_to_rename))
3015 goto done;
3018 /* Next, determine the block at which to start the renaming process. */
3019 if (!bitmap_empty_p (syms_to_rename))
3021 /* If we have to rename some symbols from scratch, we need to
3022 start the process at the root of the CFG. FIXME, it should
3023 be possible to determine the nearest block that had a
3024 definition for each of the symbols that are marked for
3025 updating. For now this seems more work than it's worth. */
3026 start_bb = ENTRY_BLOCK_PTR;
3028 /* Traverse the CFG looking for definitions and uses of symbols
3029 in SYMS_TO_RENAME. Mark interesting blocks and statements
3030 and set local live-in information for the PHI placement
3031 heuristics. */
3032 prepare_block_for_update (start_bb, insert_phi_p);
3034 else
3036 /* Otherwise, the entry block to the region is the nearest
3037 common dominator for the blocks in BLOCKS. */
3038 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3039 blocks_to_update);
3042 /* If requested, insert PHI nodes at the iterated dominance frontier
3043 of every block, creating new definitions for names in OLD_SSA_NAMES
3044 and for symbols in SYMS_TO_RENAME. */
3045 if (insert_phi_p)
3047 bitmap *dfs;
3049 /* If the caller requested PHI nodes to be added, compute
3050 dominance frontiers. */
3051 dfs = XNEWVEC (bitmap, last_basic_block);
3052 FOR_EACH_BB (bb)
3053 dfs[bb->index] = BITMAP_ALLOC (NULL);
3054 compute_dominance_frontiers (dfs);
3056 if (sbitmap_first_set_bit (old_ssa_names) >= 0)
3058 sbitmap_iterator sbi;
3060 /* insert_update_phi_nodes_for will call add_new_name_mapping
3061 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3062 will grow while we are traversing it (but it will not
3063 gain any new members). Copy OLD_SSA_NAMES to a temporary
3064 for traversal. */
3065 sbitmap tmp = sbitmap_alloc (old_ssa_names->n_bits);
3066 sbitmap_copy (tmp, old_ssa_names);
3067 EXECUTE_IF_SET_IN_SBITMAP (tmp, 0, i, sbi)
3068 insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
3069 update_flags);
3070 sbitmap_free (tmp);
3073 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
3074 insert_updated_phi_nodes_for (referenced_var (i), dfs,
3075 blocks_to_update, update_flags);
3077 FOR_EACH_BB (bb)
3078 BITMAP_FREE (dfs[bb->index]);
3079 free (dfs);
3081 /* Insertion of PHI nodes may have added blocks to the region.
3082 We need to re-compute START_BB to include the newly added
3083 blocks. */
3084 if (start_bb != ENTRY_BLOCK_PTR)
3085 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3086 blocks_to_update);
3089 /* Reset the current definition for name and symbol before renaming
3090 the sub-graph. */
3091 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3092 set_current_def (ssa_name (i), NULL_TREE);
3094 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
3095 set_current_def (referenced_var (i), NULL_TREE);
3097 /* Now start the renaming process at START_BB. */
3098 tmp = sbitmap_alloc (last_basic_block);
3099 sbitmap_zero (tmp);
3100 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3101 SET_BIT (tmp, i);
3103 rewrite_blocks (start_bb, REWRITE_UPDATE, tmp);
3105 sbitmap_free (tmp);
3107 /* Debugging dumps. */
3108 if (dump_file)
3110 int c;
3111 unsigned i;
3113 dump_update_ssa (dump_file);
3115 fprintf (dump_file, "Incremental SSA update started at block: %d\n\n",
3116 start_bb->index);
3118 c = 0;
3119 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3120 c++;
3121 fprintf (dump_file, "Number of blocks in CFG: %d\n", last_basic_block);
3122 fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n\n",
3123 c, PERCENT (c, last_basic_block));
3125 if (dump_flags & TDF_DETAILS)
3127 fprintf (dump_file, "Affected blocks: ");
3128 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3129 fprintf (dump_file, "%u ", i);
3130 fprintf (dump_file, "\n");
3133 fprintf (dump_file, "\n\n");
3136 /* Free allocated memory. */
3137 done:
3138 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
3140 tree_vec phis = VEC_index (tree_vec, phis_to_rewrite, i);
3142 VEC_free (tree, heap, phis);
3143 VEC_replace (tree_vec, phis_to_rewrite, i, NULL);
3145 BITMAP_FREE (blocks_with_phis_to_rewrite);
3146 BITMAP_FREE (blocks_to_update);
3147 delete_update_ssa ();
3149 timevar_pop (TV_TREE_SSA_INCREMENTAL);