1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
36 #include "diagnostic.h"
38 #include "tree-flow.h"
40 #include "tree-inline.h"
44 #include "tree-dump.h"
45 #include "tree-pass.h"
53 /* This file builds the SSA form for a function as described in:
54 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
55 Computing Static Single Assignment Form and the Control Dependence
56 Graph. ACM Transactions on Programming Languages and Systems,
57 13(4):451-490, October 1991. */
59 /* Structure to map a variable VAR to the set of blocks that contain
60 definitions for VAR. */
66 /* Blocks that contain definitions of VAR. Bit I will be set if the
67 Ith block contains a definition of VAR. */
70 /* Blocks that contain a PHI node for VAR. */
73 /* Blocks where VAR is live-on-entry. Similar semantics as
79 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
80 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
81 basic blocks where VAR is defined (assigned a new value). It also
82 contains a bitmap of all the blocks where VAR is live-on-entry
83 (i.e., there is a use of VAR in block B without a preceding
84 definition in B). The live-on-entry information is used when
85 computing PHI pruning heuristics. */
86 static htab_t def_blocks
;
88 /* Stack of trees used to restore the global currdefs to its original
89 state after completing rewriting of a block and its dominator
90 children. Its elements have the following properties:
92 - An SSA_NAME (N) indicates that the current definition of the
93 underlying variable should be set to the given SSA_NAME. If the
94 symbol associated with the SSA_NAME is not a GIMPLE register, the
95 next slot in the stack must be a _DECL node (SYM). In this case,
96 the name N in the previous slot is the current reaching
99 - A _DECL node indicates that the underlying variable has no
102 - A NULL node at the top entry is used to mark the last slot
103 associated with the current block. */
104 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
;
116 /* Symbols whose SSA form needs to be updated or created for the first
118 static bitmap syms_to_rename
;
120 /* Subset of SYMS_TO_RENAME. Contains all the GIMPLE register symbols
121 that have been marked for renaming. */
122 static bitmap regs_to_rename
;
124 /* Subset of SYMS_TO_RENAME. Contains all the memory symbols
125 that have been marked for renaming. */
126 static bitmap mem_syms_to_rename
;
128 /* Set of SSA names that have been marked to be released after they
129 were registered in the replacement table. They will be finally
130 released after we finish updating the SSA web. */
131 static bitmap names_to_release
;
133 /* For each block, the PHI nodes that need to be rewritten are stored into
135 typedef VEC(gimple
, heap
) *gimple_vec
;
136 DEF_VEC_P (gimple_vec
);
137 DEF_VEC_ALLOC_P (gimple_vec
, heap
);
139 static VEC(gimple_vec
, heap
) *phis_to_rewrite
;
141 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
142 static bitmap blocks_with_phis_to_rewrite
;
144 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
145 to grow as the callers to register_new_name_mapping will typically
146 create new names on the fly. FIXME. Currently set to 1/3 to avoid
147 frequent reallocations but still need to find a reasonable growth
149 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
151 /* Tuple used to represent replacement mappings. */
159 /* NEW -> OLD_SET replacement table. If we are replacing several
160 existing SSA names O_1, O_2, ..., O_j with a new name N_i,
161 then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }. */
162 static htab_t repl_tbl
;
164 /* true if register_new_name_mapping needs to initialize the data
165 structures needed by update_ssa. */
166 static bool need_to_initialize_update_ssa_p
= true;
168 /* true if update_ssa needs to update virtual operands. */
169 static bool need_to_update_vops_p
= false;
171 /* Statistics kept by update_ssa to use in the virtual mapping
172 heuristic. If the number of virtual mappings is beyond certain
173 threshold, the updater will switch from using the mappings into
174 renaming the virtual symbols from scratch. In some cases, the
175 large number of name mappings for virtual names causes significant
176 slowdowns in the PHI insertion code. */
177 struct update_ssa_stats_d
179 unsigned num_virtual_mappings
;
180 unsigned num_total_mappings
;
181 bitmap virtual_symbols
;
182 unsigned num_virtual_symbols
;
184 static struct update_ssa_stats_d update_ssa_stats
;
186 /* Global data to attach to the main dominator walk structure. */
187 struct mark_def_sites_global_data
189 /* This bitmap contains the variables which are set before they
190 are used in a basic block. */
193 /* Bitmap of names to rename. */
194 sbitmap names_to_rename
;
196 /* Set of blocks that mark_def_sites deems interesting for the
197 renamer to process. */
198 sbitmap interesting_blocks
;
202 /* Information stored for SSA names. */
205 /* The current reaching definition replacing this SSA name. */
208 /* This field indicates whether or not the variable may need PHI nodes.
209 See the enum's definition for more detailed information about the
211 ENUM_BITFIELD (need_phi_state
) need_phi_state
: 2;
213 /* Age of this record (so that info_for_ssa_name table can be cleared
214 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
215 are assumed to be null. */
219 /* The information associated with names. */
220 typedef struct ssa_name_info
*ssa_name_info_p
;
221 DEF_VEC_P (ssa_name_info_p
);
222 DEF_VEC_ALLOC_P (ssa_name_info_p
, heap
);
224 static VEC(ssa_name_info_p
, heap
) *info_for_ssa_name
;
225 static unsigned current_info_for_ssa_name_age
;
227 /* The set of blocks affected by update_ssa. */
228 static bitmap blocks_to_update
;
230 /* The main entry point to the SSA renamer (rewrite_blocks) may be
231 called several times to do different, but related, tasks.
232 Initially, we need it to rename the whole program into SSA form.
233 At other times, we may need it to only rename into SSA newly
234 exposed symbols. Finally, we can also call it to incrementally fix
235 an already built SSA web. */
237 /* Convert the whole function into SSA form. */
240 /* Incrementally update the SSA web by replacing existing SSA
241 names with new ones. See update_ssa for details. */
248 /* Prototypes for debugging functions. */
249 extern void dump_tree_ssa (FILE *);
250 extern void debug_tree_ssa (void);
251 extern void debug_def_blocks (void);
252 extern void dump_tree_ssa_stats (FILE *);
253 extern void debug_tree_ssa_stats (void);
254 extern void dump_update_ssa (FILE *);
255 extern void debug_update_ssa (void);
256 extern void dump_names_replaced_by (FILE *, tree
);
257 extern void debug_names_replaced_by (tree
);
258 extern void dump_def_blocks (FILE *);
259 extern void debug_def_blocks (void);
260 extern void dump_defs_stack (FILE *, int);
261 extern void debug_defs_stack (int);
262 extern void dump_currdefs (FILE *);
263 extern void debug_currdefs (void);
265 /* Return true if STMT needs to be rewritten. When renaming a subset
266 of the variables, not all statements will be processed. This is
267 decided in mark_def_sites. */
270 rewrite_uses_p (gimple stmt
)
272 return gimple_visited_p (stmt
);
276 /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
279 set_rewrite_uses (gimple stmt
, bool rewrite_p
)
281 gimple_set_visited (stmt
, rewrite_p
);
285 /* Return true if the DEFs created by statement STMT should be
286 registered when marking new definition sites. This is slightly
287 different than rewrite_uses_p: it's used by update_ssa to
288 distinguish statements that need to have both uses and defs
289 processed from those that only need to have their defs processed.
290 Statements that define new SSA names only need to have their defs
291 registered, but they don't need to have their uses renamed. */
294 register_defs_p (gimple stmt
)
296 return gimple_plf (stmt
, GF_PLF_1
) != 0;
300 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
303 set_register_defs (gimple stmt
, bool register_defs_p
)
305 gimple_set_plf (stmt
, GF_PLF_1
, register_defs_p
);
309 /* Get the information associated with NAME. */
311 static inline ssa_name_info_p
312 get_ssa_name_ann (tree name
)
314 unsigned ver
= SSA_NAME_VERSION (name
);
315 unsigned len
= VEC_length (ssa_name_info_p
, info_for_ssa_name
);
316 struct ssa_name_info
*info
;
320 unsigned new_len
= num_ssa_names
;
322 VEC_reserve (ssa_name_info_p
, heap
, info_for_ssa_name
, new_len
);
323 while (len
++ < new_len
)
325 struct ssa_name_info
*info
= XCNEW (struct ssa_name_info
);
326 info
->age
= current_info_for_ssa_name_age
;
327 VEC_quick_push (ssa_name_info_p
, info_for_ssa_name
, info
);
331 info
= VEC_index (ssa_name_info_p
, info_for_ssa_name
, ver
);
332 if (info
->age
< current_info_for_ssa_name_age
)
334 info
->need_phi_state
= 0;
335 info
->current_def
= NULL_TREE
;
336 info
->age
= current_info_for_ssa_name_age
;
343 /* Clears info for SSA names. */
346 clear_ssa_name_info (void)
348 current_info_for_ssa_name_age
++;
352 /* Get phi_state field for VAR. */
354 static inline enum need_phi_state
355 get_phi_state (tree var
)
357 if (TREE_CODE (var
) == SSA_NAME
)
358 return get_ssa_name_ann (var
)->need_phi_state
;
360 return var_ann (var
)->need_phi_state
;
364 /* Sets phi_state field for VAR to STATE. */
367 set_phi_state (tree var
, enum need_phi_state state
)
369 if (TREE_CODE (var
) == SSA_NAME
)
370 get_ssa_name_ann (var
)->need_phi_state
= state
;
372 var_ann (var
)->need_phi_state
= state
;
376 /* Return the current definition for VAR. */
379 get_current_def (tree var
)
381 if (TREE_CODE (var
) == SSA_NAME
)
382 return get_ssa_name_ann (var
)->current_def
;
384 return var_ann (var
)->current_def
;
388 /* Sets current definition of VAR to DEF. */
391 set_current_def (tree var
, tree def
)
393 if (TREE_CODE (var
) == SSA_NAME
)
394 get_ssa_name_ann (var
)->current_def
= def
;
396 var_ann (var
)->current_def
= def
;
400 /* Compute global livein information given the set of blocks where
401 an object is locally live at the start of the block (LIVEIN)
402 and the set of blocks where the object is defined (DEF_BLOCKS).
404 Note: This routine augments the existing local livein information
405 to include global livein (i.e., it modifies the underlying bitmap
409 compute_global_livein (bitmap livein ATTRIBUTE_UNUSED
, bitmap def_blocks ATTRIBUTE_UNUSED
)
411 basic_block bb
, *worklist
, *tos
;
416 = (basic_block
*) xmalloc (sizeof (basic_block
) * (last_basic_block
+ 1));
418 EXECUTE_IF_SET_IN_BITMAP (livein
, 0, i
, bi
)
419 *tos
++ = BASIC_BLOCK (i
);
421 /* Iterate until the worklist is empty. */
422 while (tos
!= worklist
)
427 /* Pull a block off the worklist. */
430 /* For each predecessor block. */
431 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
433 basic_block pred
= e
->src
;
434 int pred_index
= pred
->index
;
436 /* None of this is necessary for the entry block. */
437 if (pred
!= ENTRY_BLOCK_PTR
438 && ! bitmap_bit_p (livein
, pred_index
)
439 && ! bitmap_bit_p (def_blocks
, pred_index
))
442 bitmap_set_bit (livein
, pred_index
);
451 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
452 all statements in basic block BB. */
455 initialize_flags_in_bb (basic_block bb
)
458 gimple_stmt_iterator gsi
;
460 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
462 gimple phi
= gsi_stmt (gsi
);
463 set_rewrite_uses (phi
, false);
464 set_register_defs (phi
, false);
467 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
469 stmt
= gsi_stmt (gsi
);
471 /* We are going to use the operand cache API, such as
472 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
473 cache for each statement should be up-to-date. */
474 gcc_assert (!gimple_modified_p (stmt
));
475 set_rewrite_uses (stmt
, false);
476 set_register_defs (stmt
, false);
480 /* Mark block BB as interesting for update_ssa. */
483 mark_block_for_update (basic_block bb
)
485 gcc_assert (blocks_to_update
!= NULL
);
486 if (bitmap_bit_p (blocks_to_update
, bb
->index
))
488 bitmap_set_bit (blocks_to_update
, bb
->index
);
489 initialize_flags_in_bb (bb
);
492 /* Return the set of blocks where variable VAR is defined and the blocks
493 where VAR is live on entry (livein). If no entry is found in
494 DEF_BLOCKS, a new one is created and returned. */
496 static inline struct def_blocks_d
*
497 get_def_blocks_for (tree var
)
499 struct def_blocks_d db
, *db_p
;
503 slot
= htab_find_slot (def_blocks
, (void *) &db
, INSERT
);
506 db_p
= XNEW (struct def_blocks_d
);
508 db_p
->def_blocks
= BITMAP_ALLOC (NULL
);
509 db_p
->phi_blocks
= BITMAP_ALLOC (NULL
);
510 db_p
->livein_blocks
= BITMAP_ALLOC (NULL
);
511 *slot
= (void *) db_p
;
514 db_p
= (struct def_blocks_d
*) *slot
;
520 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
521 VAR is defined by a PHI node. */
524 set_def_block (tree var
, basic_block bb
, bool phi_p
)
526 struct def_blocks_d
*db_p
;
527 enum need_phi_state state
;
529 state
= get_phi_state (var
);
530 db_p
= get_def_blocks_for (var
);
532 /* Set the bit corresponding to the block where VAR is defined. */
533 bitmap_set_bit (db_p
->def_blocks
, bb
->index
);
535 bitmap_set_bit (db_p
->phi_blocks
, bb
->index
);
537 /* Keep track of whether or not we may need to insert PHI nodes.
539 If we are in the UNKNOWN state, then this is the first definition
540 of VAR. Additionally, we have not seen any uses of VAR yet, so
541 we do not need a PHI node for this variable at this time (i.e.,
542 transition to NEED_PHI_STATE_NO).
544 If we are in any other state, then we either have multiple definitions
545 of this variable occurring in different blocks or we saw a use of the
546 variable which was not dominated by the block containing the
547 definition(s). In this case we may need a PHI node, so enter
548 state NEED_PHI_STATE_MAYBE. */
549 if (state
== NEED_PHI_STATE_UNKNOWN
)
550 set_phi_state (var
, NEED_PHI_STATE_NO
);
552 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
556 /* Mark block BB as having VAR live at the entry to BB. */
559 set_livein_block (tree var
, basic_block bb
)
561 struct def_blocks_d
*db_p
;
562 enum need_phi_state state
= get_phi_state (var
);
564 db_p
= get_def_blocks_for (var
);
566 /* Set the bit corresponding to the block where VAR is live in. */
567 bitmap_set_bit (db_p
->livein_blocks
, bb
->index
);
569 /* Keep track of whether or not we may need to insert PHI nodes.
571 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
572 by the single block containing the definition(s) of this variable. If
573 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
574 NEED_PHI_STATE_MAYBE. */
575 if (state
== NEED_PHI_STATE_NO
)
577 int def_block_index
= bitmap_first_set_bit (db_p
->def_blocks
);
579 if (def_block_index
== -1
580 || ! dominated_by_p (CDI_DOMINATORS
, bb
,
581 BASIC_BLOCK (def_block_index
)))
582 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
585 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
589 /* Return true if symbol SYM is marked for renaming. */
592 symbol_marked_for_renaming (tree sym
)
594 return bitmap_bit_p (syms_to_rename
, DECL_UID (sym
));
598 /* Return true if NAME is in OLD_SSA_NAMES. */
601 is_old_name (tree name
)
603 unsigned ver
= SSA_NAME_VERSION (name
);
604 return ver
< new_ssa_names
->n_bits
&& TEST_BIT (old_ssa_names
, ver
);
608 /* Return true if NAME is in NEW_SSA_NAMES. */
611 is_new_name (tree name
)
613 unsigned ver
= SSA_NAME_VERSION (name
);
614 return ver
< new_ssa_names
->n_bits
&& TEST_BIT (new_ssa_names
, ver
);
618 /* Hashing and equality functions for REPL_TBL. */
621 repl_map_hash (const void *p
)
623 return htab_hash_pointer ((const void *)((const struct repl_map_d
*)p
)->name
);
627 repl_map_eq (const void *p1
, const void *p2
)
629 return ((const struct repl_map_d
*)p1
)->name
630 == ((const struct repl_map_d
*)p2
)->name
;
634 repl_map_free (void *p
)
636 BITMAP_FREE (((struct repl_map_d
*)p
)->set
);
641 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
644 names_replaced_by (tree new_tree
)
650 slot
= htab_find_slot (repl_tbl
, (void *) &m
, NO_INSERT
);
652 /* If N was not registered in the replacement table, return NULL. */
653 if (slot
== NULL
|| *slot
== NULL
)
656 return ((struct repl_map_d
*) *slot
)->set
;
660 /* Add OLD to REPL_TBL[NEW_TREE].SET. */
663 add_to_repl_tbl (tree new_tree
, tree old
)
665 struct repl_map_d m
, *mp
;
669 slot
= htab_find_slot (repl_tbl
, (void *) &m
, INSERT
);
672 mp
= XNEW (struct repl_map_d
);
674 mp
->set
= BITMAP_ALLOC (NULL
);
678 mp
= (struct repl_map_d
*) *slot
;
680 bitmap_set_bit (mp
->set
, SSA_NAME_VERSION (old
));
684 /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
685 represents the set of names O_1 ... O_j replaced by N_i. This is
686 used by update_ssa and its helpers to introduce new SSA names in an
687 already formed SSA web. */
690 add_new_name_mapping (tree new_tree
, tree old
)
692 timevar_push (TV_TREE_SSA_INCREMENTAL
);
694 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
695 gcc_assert (new_tree
!= old
&& SSA_NAME_VAR (new_tree
) == SSA_NAME_VAR (old
));
697 /* If this mapping is for virtual names, we will need to update
698 virtual operands. If this is a mapping for .MEM, then we gather
699 the symbols associated with each name. */
700 if (!is_gimple_reg (new_tree
))
704 need_to_update_vops_p
= true;
706 update_ssa_stats
.num_virtual_mappings
++;
707 update_ssa_stats
.num_virtual_symbols
++;
709 /* Keep counts of virtual mappings and symbols to use in the
710 virtual mapping heuristic. If we have large numbers of
711 virtual mappings for a relatively low number of symbols, it
712 will make more sense to rename the symbols from scratch.
713 Otherwise, the insertion of PHI nodes for each of the old
714 names in these mappings will be very slow. */
715 sym
= SSA_NAME_VAR (new_tree
);
716 bitmap_set_bit (update_ssa_stats
.virtual_symbols
, DECL_UID (sym
));
719 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
720 caller may have created new names since the set was created. */
721 if (new_ssa_names
->n_bits
<= num_ssa_names
- 1)
723 unsigned int new_sz
= num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
;
724 new_ssa_names
= sbitmap_resize (new_ssa_names
, new_sz
, 0);
725 old_ssa_names
= sbitmap_resize (old_ssa_names
, new_sz
, 0);
728 /* Update the REPL_TBL table. */
729 add_to_repl_tbl (new_tree
, old
);
731 /* If OLD had already been registered as a new name, then all the
732 names that OLD replaces should also be replaced by NEW_TREE. */
733 if (is_new_name (old
))
734 bitmap_ior_into (names_replaced_by (new_tree
), names_replaced_by (old
));
736 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
738 SET_BIT (new_ssa_names
, SSA_NAME_VERSION (new_tree
));
739 SET_BIT (old_ssa_names
, SSA_NAME_VERSION (old
));
741 /* Update mapping counter to use in the virtual mapping heuristic. */
742 update_ssa_stats
.num_total_mappings
++;
744 timevar_pop (TV_TREE_SSA_INCREMENTAL
);
748 /* Call back for walk_dominator_tree used to collect definition sites
749 for every variable in the function. For every statement S in block
752 1- Variables defined by S in the DEFS of S are marked in the bitmap
753 WALK_DATA->GLOBAL_DATA->KILLS.
755 2- If S uses a variable VAR and there is no preceding kill of VAR,
756 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
758 This information is used to determine which variables are live
759 across block boundaries to reduce the number of PHI nodes
763 mark_def_sites (struct dom_walk_data
*walk_data
, basic_block bb
,
764 gimple_stmt_iterator gsi
)
766 struct mark_def_sites_global_data
*gd
;
773 /* Since this is the first time that we rewrite the program into SSA
774 form, force an operand scan on every statement. */
775 stmt
= gsi_stmt (gsi
);
778 gd
= (struct mark_def_sites_global_data
*) walk_data
->global_data
;
781 gcc_assert (blocks_to_update
== NULL
);
782 set_register_defs (stmt
, false);
783 set_rewrite_uses (stmt
, false);
785 /* If a variable is used before being set, then the variable is live
786 across a block boundary, so mark it live-on-entry to BB. */
787 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
789 tree sym
= USE_FROM_PTR (use_p
);
790 gcc_assert (DECL_P (sym
));
791 if (!bitmap_bit_p (kills
, DECL_UID (sym
)))
792 set_livein_block (sym
, bb
);
793 set_rewrite_uses (stmt
, true);
796 /* Now process the defs. Mark BB as the definition block and add
797 each def to the set of killed symbols. */
798 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
800 gcc_assert (DECL_P (def
));
801 set_def_block (def
, bb
, false);
802 bitmap_set_bit (kills
, DECL_UID (def
));
803 set_register_defs (stmt
, true);
806 /* If we found the statement interesting then also mark the block BB
808 if (rewrite_uses_p (stmt
) || register_defs_p (stmt
))
809 SET_BIT (gd
->interesting_blocks
, bb
->index
);
812 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
813 in the dfs numbering of the dominance tree. */
817 /* Basic block whose index this entry corresponds to. */
820 /* The dfs number of this node. */
824 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
828 cmp_dfsnum (const void *a
, const void *b
)
830 const struct dom_dfsnum
*const da
= (const struct dom_dfsnum
*) a
;
831 const struct dom_dfsnum
*const db
= (const struct dom_dfsnum
*) b
;
833 return (int) da
->dfs_num
- (int) db
->dfs_num
;
836 /* Among the intervals starting at the N points specified in DEFS, find
837 the one that contains S, and return its bb_index. */
840 find_dfsnum_interval (struct dom_dfsnum
*defs
, unsigned n
, unsigned s
)
842 unsigned f
= 0, t
= n
, m
;
847 if (defs
[m
].dfs_num
<= s
)
853 return defs
[f
].bb_index
;
856 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
857 KILLS is a bitmap of blocks where the value is defined before any use. */
860 prune_unused_phi_nodes (bitmap phis
, bitmap kills
, bitmap uses
)
862 VEC(int, heap
) *worklist
;
864 unsigned i
, b
, p
, u
, top
;
866 basic_block def_bb
, use_bb
;
870 struct dom_dfsnum
*defs
;
871 unsigned n_defs
, adef
;
873 if (bitmap_empty_p (uses
))
879 /* The phi must dominate a use, or an argument of a live phi. Also, we
880 do not create any phi nodes in def blocks, unless they are also livein. */
881 to_remove
= BITMAP_ALLOC (NULL
);
882 bitmap_and_compl (to_remove
, kills
, uses
);
883 bitmap_and_compl_into (phis
, to_remove
);
884 if (bitmap_empty_p (phis
))
886 BITMAP_FREE (to_remove
);
890 /* We want to remove the unnecessary phi nodes, but we do not want to compute
891 liveness information, as that may be linear in the size of CFG, and if
892 there are lot of different variables to rewrite, this may lead to quadratic
895 Instead, we basically emulate standard dce. We put all uses to worklist,
896 then for each of them find the nearest def that dominates them. If this
897 def is a phi node, we mark it live, and if it was not live before, we
898 add the predecessors of its basic block to the worklist.
900 To quickly locate the nearest def that dominates use, we use dfs numbering
901 of the dominance tree (that is already available in order to speed up
902 queries). For each def, we have the interval given by the dfs number on
903 entry to and on exit from the corresponding subtree in the dominance tree.
904 The nearest dominator for a given use is the smallest of these intervals
905 that contains entry and exit dfs numbers for the basic block with the use.
906 If we store the bounds for all the uses to an array and sort it, we can
907 locate the nearest dominating def in logarithmic time by binary search.*/
908 bitmap_ior (to_remove
, kills
, phis
);
909 n_defs
= bitmap_count_bits (to_remove
);
910 defs
= XNEWVEC (struct dom_dfsnum
, 2 * n_defs
+ 1);
911 defs
[0].bb_index
= 1;
914 EXECUTE_IF_SET_IN_BITMAP (to_remove
, 0, i
, bi
)
916 def_bb
= BASIC_BLOCK (i
);
917 defs
[adef
].bb_index
= i
;
918 defs
[adef
].dfs_num
= bb_dom_dfs_in (CDI_DOMINATORS
, def_bb
);
919 defs
[adef
+ 1].bb_index
= i
;
920 defs
[adef
+ 1].dfs_num
= bb_dom_dfs_out (CDI_DOMINATORS
, def_bb
);
923 BITMAP_FREE (to_remove
);
924 gcc_assert (adef
== 2 * n_defs
+ 1);
925 qsort (defs
, adef
, sizeof (struct dom_dfsnum
), cmp_dfsnum
);
926 gcc_assert (defs
[0].bb_index
== 1);
928 /* Now each DEFS entry contains the number of the basic block to that the
929 dfs number corresponds. Change them to the number of basic block that
930 corresponds to the interval following the dfs number. Also, for the
931 dfs_out numbers, increase the dfs number by one (so that it corresponds
932 to the start of the following interval, not to the end of the current
933 one). We use WORKLIST as a stack. */
934 worklist
= VEC_alloc (int, heap
, n_defs
+ 1);
935 VEC_quick_push (int, worklist
, 1);
938 for (i
= 1; i
< adef
; i
++)
940 b
= defs
[i
].bb_index
;
943 /* This is a closing element. Interval corresponding to the top
944 of the stack after removing it follows. */
945 VEC_pop (int, worklist
);
946 top
= VEC_index (int, worklist
, VEC_length (int, worklist
) - 1);
947 defs
[n_defs
].bb_index
= top
;
948 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
+ 1;
952 /* Opening element. Nothing to do, just push it to the stack and move
953 it to the correct position. */
954 defs
[n_defs
].bb_index
= defs
[i
].bb_index
;
955 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
;
956 VEC_quick_push (int, worklist
, b
);
960 /* If this interval starts at the same point as the previous one, cancel
962 if (defs
[n_defs
].dfs_num
== defs
[n_defs
- 1].dfs_num
)
963 defs
[n_defs
- 1].bb_index
= defs
[n_defs
].bb_index
;
967 VEC_pop (int, worklist
);
968 gcc_assert (VEC_empty (int, worklist
));
970 /* Now process the uses. */
971 live_phis
= BITMAP_ALLOC (NULL
);
972 EXECUTE_IF_SET_IN_BITMAP (uses
, 0, i
, bi
)
974 VEC_safe_push (int, heap
, worklist
, i
);
977 while (!VEC_empty (int, worklist
))
979 b
= VEC_pop (int, worklist
);
980 if (b
== ENTRY_BLOCK
)
983 /* If there is a phi node in USE_BB, it is made live. Otherwise,
984 find the def that dominates the immediate dominator of USE_BB
985 (the kill in USE_BB does not dominate the use). */
986 if (bitmap_bit_p (phis
, b
))
990 use_bb
= get_immediate_dominator (CDI_DOMINATORS
, BASIC_BLOCK (b
));
991 p
= find_dfsnum_interval (defs
, n_defs
,
992 bb_dom_dfs_in (CDI_DOMINATORS
, use_bb
));
993 if (!bitmap_bit_p (phis
, p
))
997 /* If the phi node is already live, there is nothing to do. */
998 if (bitmap_bit_p (live_phis
, p
))
1001 /* Mark the phi as live, and add the new uses to the worklist. */
1002 bitmap_set_bit (live_phis
, p
);
1003 def_bb
= BASIC_BLOCK (p
);
1004 FOR_EACH_EDGE (e
, ei
, def_bb
->preds
)
1007 if (bitmap_bit_p (uses
, u
))
1010 /* In case there is a kill directly in the use block, do not record
1011 the use (this is also necessary for correctness, as we assume that
1012 uses dominated by a def directly in their block have been filtered
1014 if (bitmap_bit_p (kills
, u
))
1017 bitmap_set_bit (uses
, u
);
1018 VEC_safe_push (int, heap
, worklist
, u
);
1022 VEC_free (int, heap
, worklist
);
1023 bitmap_copy (phis
, live_phis
);
1024 BITMAP_FREE (live_phis
);
1028 /* Return the set of blocks where variable VAR is defined and the blocks
1029 where VAR is live on entry (livein). Return NULL, if no entry is
1030 found in DEF_BLOCKS. */
1032 static inline struct def_blocks_d
*
1033 find_def_blocks_for (tree var
)
1035 struct def_blocks_d dm
;
1037 return (struct def_blocks_d
*) htab_find (def_blocks
, &dm
);
1041 /* Retrieve or create a default definition for symbol SYM. */
1044 get_default_def_for (tree sym
)
1046 tree ddef
= gimple_default_def (cfun
, sym
);
1048 if (ddef
== NULL_TREE
)
1050 ddef
= make_ssa_name (sym
, gimple_build_nop ());
1051 set_default_def (sym
, ddef
);
1058 /* Marks phi node PHI in basic block BB for rewrite. */
1061 mark_phi_for_rewrite (basic_block bb
, gimple phi
)
1064 unsigned i
, idx
= bb
->index
;
1066 if (rewrite_uses_p (phi
))
1069 set_rewrite_uses (phi
, true);
1071 if (!blocks_with_phis_to_rewrite
)
1074 bitmap_set_bit (blocks_with_phis_to_rewrite
, idx
);
1075 VEC_reserve (gimple_vec
, heap
, phis_to_rewrite
, last_basic_block
+ 1);
1076 for (i
= VEC_length (gimple_vec
, phis_to_rewrite
); i
<= idx
; i
++)
1077 VEC_quick_push (gimple_vec
, phis_to_rewrite
, NULL
);
1079 phis
= VEC_index (gimple_vec
, phis_to_rewrite
, idx
);
1081 phis
= VEC_alloc (gimple
, heap
, 10);
1083 VEC_safe_push (gimple
, heap
, phis
, phi
);
1084 VEC_replace (gimple_vec
, phis_to_rewrite
, idx
, phis
);
1088 /* Insert PHI nodes for variable VAR using the iterated dominance
1089 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1090 function assumes that the caller is incrementally updating the
1091 existing SSA form, in which case VAR may be an SSA name instead of
1094 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1095 PHI node for VAR. On exit, only the nodes that received a PHI node
1096 for VAR will be present in PHI_INSERTION_POINTS. */
1099 insert_phi_nodes_for (tree var
, bitmap phi_insertion_points
, bool update_p
)
1106 struct def_blocks_d
*def_map
;
1108 def_map
= find_def_blocks_for (var
);
1109 gcc_assert (def_map
);
1111 /* Remove the blocks where we already have PHI nodes for VAR. */
1112 bitmap_and_compl_into (phi_insertion_points
, def_map
->phi_blocks
);
1114 /* Remove obviously useless phi nodes. */
1115 prune_unused_phi_nodes (phi_insertion_points
, def_map
->def_blocks
,
1116 def_map
->livein_blocks
);
1118 /* And insert the PHI nodes. */
1119 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points
, 0, bb_index
, bi
)
1121 bb
= BASIC_BLOCK (bb_index
);
1123 mark_block_for_update (bb
);
1127 if (TREE_CODE (var
) == SSA_NAME
)
1129 /* If we are rewriting SSA names, create the LHS of the PHI
1130 node by duplicating VAR. This is useful in the case of
1131 pointers, to also duplicate pointer attributes (alias
1132 information, in particular). */
1136 gcc_assert (update_p
);
1137 phi
= create_phi_node (var
, bb
);
1139 new_lhs
= duplicate_ssa_name (var
, phi
);
1140 gimple_phi_set_result (phi
, new_lhs
);
1141 add_new_name_mapping (new_lhs
, var
);
1143 /* Add VAR to every argument slot of PHI. We need VAR in
1144 every argument so that rewrite_update_phi_arguments knows
1145 which name is this PHI node replacing. If VAR is a
1146 symbol marked for renaming, this is not necessary, the
1147 renamer will use the symbol on the LHS to get its
1148 reaching definition. */
1149 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1150 add_phi_arg (phi
, var
, e
);
1154 gcc_assert (DECL_P (var
));
1155 phi
= create_phi_node (var
, bb
);
1158 /* Mark this PHI node as interesting for update_ssa. */
1159 set_register_defs (phi
, true);
1160 mark_phi_for_rewrite (bb
, phi
);
1165 /* Insert PHI nodes at the dominance frontier of blocks with variable
1166 definitions. DFS contains the dominance frontier information for
1170 insert_phi_nodes (bitmap
*dfs
)
1172 referenced_var_iterator rvi
;
1175 timevar_push (TV_TREE_INSERT_PHI_NODES
);
1177 FOR_EACH_REFERENCED_VAR (var
, rvi
)
1179 struct def_blocks_d
*def_map
;
1182 def_map
= find_def_blocks_for (var
);
1183 if (def_map
== NULL
)
1186 if (get_phi_state (var
) != NEED_PHI_STATE_NO
)
1188 idf
= compute_idf (def_map
->def_blocks
, dfs
);
1189 insert_phi_nodes_for (var
, idf
, false);
1194 timevar_pop (TV_TREE_INSERT_PHI_NODES
);
1198 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1199 register DEF (an SSA_NAME) to be a new definition for SYM. */
1202 register_new_def (tree def
, tree sym
)
1206 /* If this variable is set in a single basic block and all uses are
1207 dominated by the set(s) in that single basic block, then there is
1208 no reason to record anything for this variable in the block local
1209 definition stacks. Doing so just wastes time and memory.
1211 This is the same test to prune the set of variables which may
1212 need PHI nodes. So we just use that information since it's already
1213 computed and available for us to use. */
1214 if (get_phi_state (sym
) == NEED_PHI_STATE_NO
)
1216 set_current_def (sym
, def
);
1220 currdef
= get_current_def (sym
);
1222 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1223 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1224 in the stack so that we know which symbol is being defined by
1225 this SSA name when we unwind the stack. */
1226 if (currdef
&& !is_gimple_reg (sym
))
1227 VEC_safe_push (tree
, heap
, block_defs_stack
, sym
);
1229 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1230 stack is later used by the dominator tree callbacks to restore
1231 the reaching definitions for all the variables defined in the
1232 block after a recursive visit to all its immediately dominated
1233 blocks. If there is no current reaching definition, then just
1234 record the underlying _DECL node. */
1235 VEC_safe_push (tree
, heap
, block_defs_stack
, currdef
? currdef
: sym
);
1237 /* Set the current reaching definition for SYM to be DEF. */
1238 set_current_def (sym
, def
);
1242 /* Perform a depth-first traversal of the dominator tree looking for
1243 variables to rename. BB is the block where to start searching.
1244 Renaming is a five step process:
1246 1- Every definition made by PHI nodes at the start of the blocks is
1247 registered as the current definition for the corresponding variable.
1249 2- Every statement in BB is rewritten. USE and VUSE operands are
1250 rewritten with their corresponding reaching definition. DEF and
1251 VDEF targets are registered as new definitions.
1253 3- All the PHI nodes in successor blocks of BB are visited. The
1254 argument corresponding to BB is replaced with its current reaching
1257 4- Recursively rewrite every dominator child block of BB.
1259 5- Restore (in reverse order) the current reaching definition for every
1260 new definition introduced in this block. This is done so that when
1261 we return from the recursive call, all the current reaching
1262 definitions are restored to the names that were valid in the
1263 dominator parent of BB. */
1265 /* SSA Rewriting Step 1. Initialization, create a block local stack
1266 of reaching definitions for new SSA names produced in this block
1267 (BLOCK_DEFS). Register new definitions for every PHI node in the
1271 rewrite_initialize_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1275 gimple_stmt_iterator gsi
;
1277 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1278 fprintf (dump_file
, "\n\nRenaming block #%d\n\n", bb
->index
);
1280 /* Mark the unwind point for this block. */
1281 VEC_safe_push (tree
, heap
, block_defs_stack
, NULL_TREE
);
1283 /* Step 1. Register new definitions for every PHI node in the block.
1284 Conceptually, all the PHI nodes are executed in parallel and each PHI
1285 node introduces a new version for the associated variable. */
1286 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1290 phi
= gsi_stmt (gsi
);
1291 result
= gimple_phi_result (phi
);
1292 gcc_assert (is_gimple_reg (result
));
1293 register_new_def (result
, SSA_NAME_VAR (result
));
1298 /* Return the current definition for variable VAR. If none is found,
1299 create a new SSA name to act as the zeroth definition for VAR. */
1302 get_reaching_def (tree var
)
1306 /* Lookup the current reaching definition for VAR. */
1307 currdef
= get_current_def (var
);
1309 /* If there is no reaching definition for VAR, create and register a
1310 default definition for it (if needed). */
1311 if (currdef
== NULL_TREE
)
1313 tree sym
= DECL_P (var
) ? var
: SSA_NAME_VAR (var
);
1314 currdef
= get_default_def_for (sym
);
1315 set_current_def (var
, currdef
);
1318 /* Return the current reaching definition for VAR, or the default
1319 definition, if we had to create one. */
1324 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1325 the block with its immediate reaching definitions. Update the current
1326 definition of a variable when a new real or virtual definition is found. */
1329 rewrite_stmt (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1330 basic_block bb ATTRIBUTE_UNUSED
, gimple_stmt_iterator si
)
1333 use_operand_p use_p
;
1334 def_operand_p def_p
;
1337 stmt
= gsi_stmt (si
);
1339 /* If mark_def_sites decided that we don't need to rewrite this
1340 statement, ignore it. */
1341 gcc_assert (blocks_to_update
== NULL
);
1342 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
1345 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1347 fprintf (dump_file
, "Renaming statement ");
1348 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1349 fprintf (dump_file
, "\n");
1352 /* Step 1. Rewrite USES in the statement. */
1353 if (rewrite_uses_p (stmt
))
1354 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1356 tree var
= USE_FROM_PTR (use_p
);
1357 gcc_assert (DECL_P (var
));
1358 SET_USE (use_p
, get_reaching_def (var
));
1361 /* Step 2. Register the statement's DEF operands. */
1362 if (register_defs_p (stmt
))
1363 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
1365 tree var
= DEF_FROM_PTR (def_p
);
1366 gcc_assert (DECL_P (var
));
1367 SET_DEF (def_p
, make_ssa_name (var
, stmt
));
1368 register_new_def (DEF_FROM_PTR (def_p
), var
);
1373 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1374 PHI nodes. For every PHI node found, add a new argument containing the
1375 current reaching definition for the variable and the edge through which
1376 that definition is reaching the PHI node. */
1379 rewrite_add_phi_arguments (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1385 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1388 gimple_stmt_iterator gsi
;
1390 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
);
1394 phi
= gsi_stmt (gsi
);
1395 currdef
= get_reaching_def (SSA_NAME_VAR (gimple_phi_result (phi
)));
1396 add_phi_arg (phi
, currdef
, e
);
1402 /* Called after visiting all the statements in basic block BB and all
1403 of its dominator children. Restore CURRDEFS to its original value. */
1406 rewrite_finalize_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1407 basic_block bb ATTRIBUTE_UNUSED
)
1409 /* Restore CURRDEFS to its original state. */
1410 while (VEC_length (tree
, block_defs_stack
) > 0)
1412 tree tmp
= VEC_pop (tree
, block_defs_stack
);
1413 tree saved_def
, var
;
1415 if (tmp
== NULL_TREE
)
1418 if (TREE_CODE (tmp
) == SSA_NAME
)
1420 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1421 current definition of its underlying variable. Note that
1422 if the SSA_NAME is not for a GIMPLE register, the symbol
1423 being defined is stored in the next slot in the stack.
1424 This mechanism is needed because an SSA name for a
1425 non-register symbol may be the definition for more than
1426 one symbol (e.g., SFTs, aliased variables, etc). */
1428 var
= SSA_NAME_VAR (saved_def
);
1429 if (!is_gimple_reg (var
))
1430 var
= VEC_pop (tree
, block_defs_stack
);
1434 /* If we recorded anything else, it must have been a _DECL
1435 node and its current reaching definition must have been
1441 set_current_def (var
, saved_def
);
1446 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1449 dump_decl_set (FILE *file
, bitmap set
)
1456 fprintf (file
, "{ ");
1458 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
1460 print_generic_expr (file
, referenced_var (i
), 0);
1461 fprintf (file
, " ");
1464 fprintf (file
, "}\n");
1467 fprintf (file
, "NIL\n");
1471 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1474 debug_decl_set (bitmap set
)
1476 dump_decl_set (stderr
, set
);
1480 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1481 stack up to a maximum of N levels. If N is -1, the whole stack is
1482 dumped. New levels are created when the dominator tree traversal
1483 used for renaming enters a new sub-tree. */
1486 dump_defs_stack (FILE *file
, int n
)
1490 fprintf (file
, "\n\nRenaming stack");
1492 fprintf (file
, " (up to %d levels)", n
);
1493 fprintf (file
, "\n\n");
1496 fprintf (file
, "Level %d (current level)\n", i
);
1497 for (j
= (int) VEC_length (tree
, block_defs_stack
) - 1; j
>= 0; j
--)
1501 name
= VEC_index (tree
, block_defs_stack
, j
);
1502 if (name
== NULL_TREE
)
1507 fprintf (file
, "\nLevel %d\n", i
);
1518 var
= SSA_NAME_VAR (name
);
1519 if (!is_gimple_reg (var
))
1522 var
= VEC_index (tree
, block_defs_stack
, j
);
1526 fprintf (file
, " Previous CURRDEF (");
1527 print_generic_expr (file
, var
, 0);
1528 fprintf (file
, ") = ");
1530 print_generic_expr (file
, name
, 0);
1532 fprintf (file
, "<NIL>");
1533 fprintf (file
, "\n");
1538 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1539 stack up to a maximum of N levels. If N is -1, the whole stack is
1540 dumped. New levels are created when the dominator tree traversal
1541 used for renaming enters a new sub-tree. */
1544 debug_defs_stack (int n
)
1546 dump_defs_stack (stderr
, n
);
1550 /* Dump the current reaching definition of every symbol to FILE. */
1553 dump_currdefs (FILE *file
)
1555 referenced_var_iterator i
;
1558 fprintf (file
, "\n\nCurrent reaching definitions\n\n");
1559 FOR_EACH_REFERENCED_VAR (var
, i
)
1560 if (syms_to_rename
== NULL
|| bitmap_bit_p (syms_to_rename
, DECL_UID (var
)))
1562 fprintf (file
, "CURRDEF (");
1563 print_generic_expr (file
, var
, 0);
1564 fprintf (file
, ") = ");
1565 if (get_current_def (var
))
1566 print_generic_expr (file
, get_current_def (var
), 0);
1568 fprintf (file
, "<NIL>");
1569 fprintf (file
, "\n");
1574 /* Dump the current reaching definition of every symbol to stderr. */
1577 debug_currdefs (void)
1579 dump_currdefs (stderr
);
1583 /* Dump SSA information to FILE. */
1586 dump_tree_ssa (FILE *file
)
1588 const char *funcname
1589 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
1591 fprintf (file
, "SSA renaming information for %s\n\n", funcname
);
1593 dump_def_blocks (file
);
1594 dump_defs_stack (file
, -1);
1595 dump_currdefs (file
);
1596 dump_tree_ssa_stats (file
);
1600 /* Dump SSA information to stderr. */
1603 debug_tree_ssa (void)
1605 dump_tree_ssa (stderr
);
1609 /* Dump statistics for the hash table HTAB. */
1612 htab_statistics (FILE *file
, htab_t htab
)
1614 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
1615 (long) htab_size (htab
),
1616 (long) htab_elements (htab
),
1617 htab_collisions (htab
));
1621 /* Dump SSA statistics on FILE. */
1624 dump_tree_ssa_stats (FILE *file
)
1626 if (def_blocks
|| repl_tbl
)
1627 fprintf (file
, "\nHash table statistics:\n");
1631 fprintf (file
, " def_blocks: ");
1632 htab_statistics (file
, def_blocks
);
1637 fprintf (file
, " repl_tbl: ");
1638 htab_statistics (file
, repl_tbl
);
1641 if (def_blocks
|| repl_tbl
)
1642 fprintf (file
, "\n");
1646 /* Dump SSA statistics on stderr. */
1649 debug_tree_ssa_stats (void)
1651 dump_tree_ssa_stats (stderr
);
1655 /* Hashing and equality functions for DEF_BLOCKS. */
1658 def_blocks_hash (const void *p
)
1660 return htab_hash_pointer
1661 ((const void *)((const struct def_blocks_d
*)p
)->var
);
1665 def_blocks_eq (const void *p1
, const void *p2
)
1667 return ((const struct def_blocks_d
*)p1
)->var
1668 == ((const struct def_blocks_d
*)p2
)->var
;
1672 /* Free memory allocated by one entry in DEF_BLOCKS. */
1675 def_blocks_free (void *p
)
1677 struct def_blocks_d
*entry
= (struct def_blocks_d
*) p
;
1678 BITMAP_FREE (entry
->def_blocks
);
1679 BITMAP_FREE (entry
->phi_blocks
);
1680 BITMAP_FREE (entry
->livein_blocks
);
1685 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1688 debug_def_blocks_r (void **slot
, void *data
)
1690 FILE *file
= (FILE *) data
;
1691 struct def_blocks_d
*db_p
= (struct def_blocks_d
*) *slot
;
1693 fprintf (file
, "VAR: ");
1694 print_generic_expr (file
, db_p
->var
, dump_flags
);
1695 bitmap_print (file
, db_p
->def_blocks
, ", DEF_BLOCKS: { ", "}");
1696 bitmap_print (file
, db_p
->livein_blocks
, ", LIVEIN_BLOCKS: { ", "}");
1697 bitmap_print (file
, db_p
->phi_blocks
, ", PHI_BLOCKS: { ", "}\n");
1703 /* Dump the DEF_BLOCKS hash table on FILE. */
1706 dump_def_blocks (FILE *file
)
1708 fprintf (file
, "\n\nDefinition and live-in blocks:\n\n");
1710 htab_traverse (def_blocks
, debug_def_blocks_r
, file
);
1714 /* Dump the DEF_BLOCKS hash table on stderr. */
1717 debug_def_blocks (void)
1719 dump_def_blocks (stderr
);
1723 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1726 register_new_update_single (tree new_name
, tree old_name
)
1728 tree currdef
= get_current_def (old_name
);
1730 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1731 This stack is later used by the dominator tree callbacks to
1732 restore the reaching definitions for all the variables
1733 defined in the block after a recursive visit to all its
1734 immediately dominated blocks. */
1735 VEC_reserve (tree
, heap
, block_defs_stack
, 2);
1736 VEC_quick_push (tree
, block_defs_stack
, currdef
);
1737 VEC_quick_push (tree
, block_defs_stack
, old_name
);
1739 /* Set the current reaching definition for OLD_NAME to be
1741 set_current_def (old_name
, new_name
);
1745 /* Register NEW_NAME to be the new reaching definition for all the
1746 names in OLD_NAMES. Used by the incremental SSA update routines to
1747 replace old SSA names with new ones. */
1750 register_new_update_set (tree new_name
, bitmap old_names
)
1755 EXECUTE_IF_SET_IN_BITMAP (old_names
, 0, i
, bi
)
1756 register_new_update_single (new_name
, ssa_name (i
));
1760 /* Initialization of block data structures for the incremental SSA
1761 update pass. Create a block local stack of reaching definitions
1762 for new SSA names produced in this block (BLOCK_DEFS). Register
1763 new definitions for every PHI node in the block. */
1766 rewrite_update_init_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1771 bool is_abnormal_phi
;
1772 gimple_stmt_iterator gsi
;
1774 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1775 fprintf (dump_file
, "\n\nRegistering new PHI nodes in block #%d\n\n",
1778 /* Mark the unwind point for this block. */
1779 VEC_safe_push (tree
, heap
, block_defs_stack
, NULL_TREE
);
1781 if (!bitmap_bit_p (blocks_to_update
, bb
->index
))
1784 /* Mark the LHS if any of the arguments flows through an abnormal
1786 is_abnormal_phi
= false;
1787 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1788 if (e
->flags
& EDGE_ABNORMAL
)
1790 is_abnormal_phi
= true;
1794 /* If any of the PHI nodes is a replacement for a name in
1795 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
1796 register it as a new definition for its corresponding name. Also
1797 register definitions for names whose underlying symbols are
1798 marked for renaming. */
1799 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1802 gimple phi
= gsi_stmt (gsi
);
1804 if (!register_defs_p (phi
))
1807 lhs
= gimple_phi_result (phi
);
1808 lhs_sym
= SSA_NAME_VAR (lhs
);
1810 if (symbol_marked_for_renaming (lhs_sym
))
1811 register_new_update_single (lhs
, lhs_sym
);
1815 /* If LHS is a new name, register a new definition for all
1816 the names replaced by LHS. */
1817 if (is_new_name (lhs
))
1818 register_new_update_set (lhs
, names_replaced_by (lhs
));
1820 /* If LHS is an OLD name, register it as a new definition
1822 if (is_old_name (lhs
))
1823 register_new_update_single (lhs
, lhs
);
1826 if (is_abnormal_phi
)
1827 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
) = 1;
1832 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
1833 the current reaching definition of every name re-written in BB to
1834 the original reaching definition before visiting BB. This
1835 unwinding must be done in the opposite order to what is done in
1836 register_new_update_set. */
1839 rewrite_update_fini_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1840 basic_block bb ATTRIBUTE_UNUSED
)
1842 while (VEC_length (tree
, block_defs_stack
) > 0)
1844 tree var
= VEC_pop (tree
, block_defs_stack
);
1847 /* NULL indicates the unwind stop point for this block (see
1848 rewrite_update_init_block). */
1852 saved_def
= VEC_pop (tree
, block_defs_stack
);
1853 set_current_def (var
, saved_def
);
1858 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1859 it is a symbol marked for renaming, replace it with USE_P's current
1860 reaching definition. */
1863 maybe_replace_use (use_operand_p use_p
)
1865 tree rdef
= NULL_TREE
;
1866 tree use
= USE_FROM_PTR (use_p
);
1867 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1869 if (symbol_marked_for_renaming (sym
))
1870 rdef
= get_reaching_def (sym
);
1871 else if (is_old_name (use
))
1872 rdef
= get_reaching_def (use
);
1874 if (rdef
&& rdef
!= use
)
1875 SET_USE (use_p
, rdef
);
1879 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1880 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1881 register it as the current definition for the names replaced by
1885 maybe_register_def (def_operand_p def_p
, gimple stmt
)
1887 tree def
= DEF_FROM_PTR (def_p
);
1888 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
1890 /* If DEF is a naked symbol that needs renaming, create a new
1892 if (symbol_marked_for_renaming (sym
))
1896 def
= make_ssa_name (def
, stmt
);
1897 SET_DEF (def_p
, def
);
1900 register_new_update_single (def
, sym
);
1904 /* If DEF is a new name, register it as a new definition
1905 for all the names replaced by DEF. */
1906 if (is_new_name (def
))
1907 register_new_update_set (def
, names_replaced_by (def
));
1909 /* If DEF is an old name, register DEF as a new
1910 definition for itself. */
1911 if (is_old_name (def
))
1912 register_new_update_single (def
, def
);
1917 /* Update every variable used in the statement pointed-to by SI. The
1918 statement is assumed to be in SSA form already. Names in
1919 OLD_SSA_NAMES used by SI will be updated to their current reaching
1920 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1921 will be registered as a new definition for their corresponding name
1922 in OLD_SSA_NAMES. */
1925 rewrite_update_stmt (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1926 basic_block bb ATTRIBUTE_UNUSED
,
1927 gimple_stmt_iterator si
)
1930 use_operand_p use_p
;
1931 def_operand_p def_p
;
1934 stmt
= gsi_stmt (si
);
1936 gcc_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
1938 /* Only update marked statements. */
1939 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
1942 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1944 fprintf (dump_file
, "Updating SSA information for statement ");
1945 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1946 fprintf (dump_file
, "\n");
1949 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1950 symbol is marked for renaming. */
1951 if (rewrite_uses_p (stmt
))
1953 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1954 maybe_replace_use (use_p
);
1956 if (need_to_update_vops_p
)
1957 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_VIRTUAL_USES
)
1958 maybe_replace_use (use_p
);
1961 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1962 Also register definitions for names whose underlying symbol is
1963 marked for renaming. */
1964 if (register_defs_p (stmt
))
1966 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
1967 maybe_register_def (def_p
, stmt
);
1969 if (need_to_update_vops_p
)
1970 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_VIRTUAL_DEFS
)
1971 maybe_register_def (def_p
, stmt
);
1976 /* Visit all the successor blocks of BB looking for PHI nodes. For
1977 every PHI node found, check if any of its arguments is in
1978 OLD_SSA_NAMES. If so, and if the argument has a current reaching
1979 definition, replace it. */
1982 rewrite_update_phi_arguments (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1989 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1994 if (!bitmap_bit_p (blocks_with_phis_to_rewrite
, e
->dest
->index
))
1997 phis
= VEC_index (gimple_vec
, phis_to_rewrite
, e
->dest
->index
);
1998 for (i
= 0; VEC_iterate (gimple
, phis
, i
, phi
); i
++)
2001 use_operand_p arg_p
;
2003 gcc_assert (rewrite_uses_p (phi
));
2005 arg_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
2006 arg
= USE_FROM_PTR (arg_p
);
2008 if (arg
&& !DECL_P (arg
) && TREE_CODE (arg
) != SSA_NAME
)
2011 lhs_sym
= SSA_NAME_VAR (gimple_phi_result (phi
));
2013 if (arg
== NULL_TREE
)
2015 /* When updating a PHI node for a recently introduced
2016 symbol we may find NULL arguments. That's why we
2017 take the symbol from the LHS of the PHI node. */
2018 SET_USE (arg_p
, get_reaching_def (lhs_sym
));
2022 tree sym
= DECL_P (arg
) ? arg
: SSA_NAME_VAR (arg
);
2024 if (symbol_marked_for_renaming (sym
))
2025 SET_USE (arg_p
, get_reaching_def (sym
));
2026 else if (is_old_name (arg
))
2027 SET_USE (arg_p
, get_reaching_def (arg
));
2030 if (e
->flags
& EDGE_ABNORMAL
)
2031 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p
)) = 1;
2037 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2040 ENTRY indicates the block where to start. Every block dominated by
2041 ENTRY will be rewritten.
2043 WHAT indicates what actions will be taken by the renamer (see enum
2046 BLOCKS are the set of interesting blocks for the dominator walker
2047 to process. If this set is NULL, then all the nodes dominated
2048 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2049 are not present in BLOCKS are ignored. */
2052 rewrite_blocks (basic_block entry
, enum rewrite_mode what
, sbitmap blocks
)
2054 struct dom_walk_data walk_data
;
2056 /* Rewrite all the basic blocks in the program. */
2057 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS
);
2059 /* Setup callbacks for the generic dominator tree walker. */
2060 memset (&walk_data
, 0, sizeof (walk_data
));
2062 walk_data
.dom_direction
= CDI_DOMINATORS
;
2063 walk_data
.interesting_blocks
= blocks
;
2065 if (what
== REWRITE_ALL
)
2066 walk_data
.before_dom_children_before_stmts
= rewrite_initialize_block
;
2068 walk_data
.before_dom_children_before_stmts
= rewrite_update_init_block
;
2070 if (what
== REWRITE_ALL
)
2071 walk_data
.before_dom_children_walk_stmts
= rewrite_stmt
;
2072 else if (what
== REWRITE_UPDATE
)
2073 walk_data
.before_dom_children_walk_stmts
= rewrite_update_stmt
;
2077 if (what
== REWRITE_ALL
)
2078 walk_data
.before_dom_children_after_stmts
= rewrite_add_phi_arguments
;
2079 else if (what
== REWRITE_UPDATE
)
2080 walk_data
.before_dom_children_after_stmts
= rewrite_update_phi_arguments
;
2084 if (what
== REWRITE_ALL
)
2085 walk_data
.after_dom_children_after_stmts
= rewrite_finalize_block
;
2086 else if (what
== REWRITE_UPDATE
)
2087 walk_data
.after_dom_children_after_stmts
= rewrite_update_fini_block
;
2091 block_defs_stack
= VEC_alloc (tree
, heap
, 10);
2093 /* Initialize the dominator walker. */
2094 init_walk_dominator_tree (&walk_data
);
2096 /* Recursively walk the dominator tree rewriting each statement in
2097 each basic block. */
2098 walk_dominator_tree (&walk_data
, entry
);
2100 /* Finalize the dominator walker. */
2101 fini_walk_dominator_tree (&walk_data
);
2103 /* Debugging dumps. */
2104 if (dump_file
&& (dump_flags
& TDF_STATS
))
2106 dump_dfa_stats (dump_file
);
2108 dump_tree_ssa_stats (dump_file
);
2111 VEC_free (tree
, heap
, block_defs_stack
);
2113 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS
);
2117 /* Block initialization routine for mark_def_sites. Clear the
2118 KILLS bitmap at the start of each block. */
2121 mark_def_sites_initialize_block (struct dom_walk_data
*walk_data
,
2122 basic_block bb ATTRIBUTE_UNUSED
)
2124 struct mark_def_sites_global_data
*gd
;
2125 gd
= (struct mark_def_sites_global_data
*) walk_data
->global_data
;
2126 bitmap_clear (gd
->kills
);
2130 /* Mark the definition site blocks for each variable, so that we know
2131 where the variable is actually live.
2133 INTERESTING_BLOCKS will be filled in with all the blocks that
2134 should be processed by the renamer. It is assumed to be
2135 initialized and zeroed by the caller. */
2138 mark_def_site_blocks (sbitmap interesting_blocks
)
2140 struct dom_walk_data walk_data
;
2141 struct mark_def_sites_global_data mark_def_sites_global_data
;
2143 /* Setup callbacks for the generic dominator tree walker to find and
2144 mark definition sites. */
2145 walk_data
.walk_stmts_backward
= false;
2146 walk_data
.dom_direction
= CDI_DOMINATORS
;
2147 walk_data
.initialize_block_local_data
= NULL
;
2148 walk_data
.before_dom_children_before_stmts
= mark_def_sites_initialize_block
;
2149 walk_data
.before_dom_children_walk_stmts
= mark_def_sites
;
2150 walk_data
.before_dom_children_after_stmts
= NULL
;
2151 walk_data
.after_dom_children_before_stmts
= NULL
;
2152 walk_data
.after_dom_children_walk_stmts
= NULL
;
2153 walk_data
.after_dom_children_after_stmts
= NULL
;
2154 walk_data
.interesting_blocks
= NULL
;
2156 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2157 large enough to accommodate all the variables referenced in the
2158 function, not just the ones we are renaming. */
2159 mark_def_sites_global_data
.kills
= BITMAP_ALLOC (NULL
);
2161 /* Create the set of interesting blocks that will be filled by
2163 mark_def_sites_global_data
.interesting_blocks
= interesting_blocks
;
2164 walk_data
.global_data
= &mark_def_sites_global_data
;
2166 /* We do not have any local data. */
2167 walk_data
.block_local_data_size
= 0;
2169 /* Initialize the dominator walker. */
2170 init_walk_dominator_tree (&walk_data
);
2172 /* Recursively walk the dominator tree. */
2173 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
2175 /* Finalize the dominator walker. */
2176 fini_walk_dominator_tree (&walk_data
);
2178 /* We no longer need this bitmap, clear and free it. */
2179 BITMAP_FREE (mark_def_sites_global_data
.kills
);
2183 /* Initialize internal data needed during renaming. */
2186 init_ssa_renamer (void)
2189 referenced_var_iterator rvi
;
2191 cfun
->gimple_df
->in_ssa_p
= false;
2193 /* Allocate memory for the DEF_BLOCKS hash table. */
2194 gcc_assert (def_blocks
== NULL
);
2195 def_blocks
= htab_create (num_referenced_vars
, def_blocks_hash
,
2196 def_blocks_eq
, def_blocks_free
);
2198 FOR_EACH_REFERENCED_VAR(var
, rvi
)
2199 set_current_def (var
, NULL_TREE
);
2203 /* Deallocate internal data structures used by the renamer. */
2206 fini_ssa_renamer (void)
2210 htab_delete (def_blocks
);
2214 cfun
->gimple_df
->in_ssa_p
= true;
2217 /* Main entry point into the SSA builder. The renaming process
2218 proceeds in four main phases:
2220 1- Compute dominance frontier and immediate dominators, needed to
2221 insert PHI nodes and rename the function in dominator tree
2224 2- Find and mark all the blocks that define variables
2225 (mark_def_site_blocks).
2227 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2229 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2231 Steps 3 and 4 are done using the dominator tree walker
2232 (walk_dominator_tree). */
2235 rewrite_into_ssa (void)
2239 sbitmap interesting_blocks
;
2241 timevar_push (TV_TREE_SSA_OTHER
);
2243 /* Initialize operand data structures. */
2244 init_ssa_operands ();
2246 /* Initialize internal data needed by the renamer. */
2247 init_ssa_renamer ();
2249 /* Initialize the set of interesting blocks. The callback
2250 mark_def_sites will add to this set those blocks that the renamer
2252 interesting_blocks
= sbitmap_alloc (last_basic_block
);
2253 sbitmap_zero (interesting_blocks
);
2255 /* Initialize dominance frontier. */
2256 dfs
= XNEWVEC (bitmap
, last_basic_block
);
2258 dfs
[bb
->index
] = BITMAP_ALLOC (NULL
);
2260 /* 1- Compute dominance frontiers. */
2261 calculate_dominance_info (CDI_DOMINATORS
);
2262 compute_dominance_frontiers (dfs
);
2264 /* 2- Find and mark definition sites. */
2265 mark_def_site_blocks (interesting_blocks
);
2267 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2268 insert_phi_nodes (dfs
);
2270 /* 4- Rename all the blocks. */
2271 rewrite_blocks (ENTRY_BLOCK_PTR
, REWRITE_ALL
, interesting_blocks
);
2273 /* Free allocated memory. */
2275 BITMAP_FREE (dfs
[bb
->index
]);
2277 sbitmap_free (interesting_blocks
);
2279 fini_ssa_renamer ();
2281 timevar_pop (TV_TREE_SSA_OTHER
);
2286 struct gimple_opt_pass pass_build_ssa
=
2292 rewrite_into_ssa
, /* execute */
2295 0, /* static_pass_number */
2297 PROP_cfg
| PROP_referenced_vars
, /* properties_required */
2298 PROP_ssa
, /* properties_provided */
2299 0, /* properties_destroyed */
2300 0, /* todo_flags_start */
2303 | TODO_remove_unused_locals
/* todo_flags_finish */
2308 /* Mark the definition of VAR at STMT and BB as interesting for the
2309 renamer. BLOCKS is the set of blocks that need updating. */
2312 mark_def_interesting (tree var
, gimple stmt
, basic_block bb
, bool insert_phi_p
)
2314 gcc_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2315 set_register_defs (stmt
, true);
2319 bool is_phi_p
= gimple_code (stmt
) == GIMPLE_PHI
;
2321 set_def_block (var
, bb
, is_phi_p
);
2323 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2324 site for both itself and all the old names replaced by it. */
2325 if (TREE_CODE (var
) == SSA_NAME
&& is_new_name (var
))
2329 bitmap set
= names_replaced_by (var
);
2331 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
2332 set_def_block (ssa_name (i
), bb
, is_phi_p
);
2338 /* Mark the use of VAR at STMT and BB as interesting for the
2339 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2343 mark_use_interesting (tree var
, gimple stmt
, basic_block bb
, bool insert_phi_p
)
2345 basic_block def_bb
= gimple_bb (stmt
);
2347 mark_block_for_update (def_bb
);
2348 mark_block_for_update (bb
);
2350 if (gimple_code (stmt
) == GIMPLE_PHI
)
2351 mark_phi_for_rewrite (def_bb
, stmt
);
2353 set_rewrite_uses (stmt
, true);
2355 /* If VAR has not been defined in BB, then it is live-on-entry
2356 to BB. Note that we cannot just use the block holding VAR's
2357 definition because if VAR is one of the names in OLD_SSA_NAMES,
2358 it will have several definitions (itself and all the names that
2362 struct def_blocks_d
*db_p
= get_def_blocks_for (var
);
2363 if (!bitmap_bit_p (db_p
->def_blocks
, bb
->index
))
2364 set_livein_block (var
, bb
);
2369 /* Do a dominator walk starting at BB processing statements that
2370 reference symbols in SYMS_TO_RENAME. This is very similar to
2371 mark_def_sites, but the scan handles statements whose operands may
2372 already be SSA names.
2374 If INSERT_PHI_P is true, mark those uses as live in the
2375 corresponding block. This is later used by the PHI placement
2376 algorithm to make PHI pruning decisions.
2378 FIXME. Most of this would be unnecessary if we could associate a
2379 symbol to all the SSA names that reference it. But that
2380 sounds like it would be expensive to maintain. Still, it
2381 would be interesting to see if it makes better sense to do
2385 prepare_block_for_update (basic_block bb
, bool insert_phi_p
)
2388 gimple_stmt_iterator si
;
2392 mark_block_for_update (bb
);
2394 /* Process PHI nodes marking interesting those that define or use
2395 the symbols that we are interested in. */
2396 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
2398 gimple phi
= gsi_stmt (si
);
2399 tree lhs_sym
, lhs
= gimple_phi_result (phi
);
2401 lhs_sym
= DECL_P (lhs
) ? lhs
: SSA_NAME_VAR (lhs
);
2403 if (!symbol_marked_for_renaming (lhs_sym
))
2406 mark_def_interesting (lhs_sym
, phi
, bb
, insert_phi_p
);
2408 /* Mark the uses in phi nodes as interesting. It would be more correct
2409 to process the arguments of the phi nodes of the successor edges of
2410 BB at the end of prepare_block_for_update, however, that turns out
2411 to be significantly more expensive. Doing it here is conservatively
2412 correct -- it may only cause us to believe a value to be live in a
2413 block that also contains its definition, and thus insert a few more
2414 phi nodes for it. */
2415 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2416 mark_use_interesting (lhs_sym
, phi
, e
->src
, insert_phi_p
);
2419 /* Process the statements. */
2420 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
2424 use_operand_p use_p
;
2425 def_operand_p def_p
;
2427 stmt
= gsi_stmt (si
);
2429 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, i
, SSA_OP_ALL_USES
)
2431 tree use
= USE_FROM_PTR (use_p
);
2432 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
2433 if (symbol_marked_for_renaming (sym
))
2434 mark_use_interesting (sym
, stmt
, bb
, insert_phi_p
);
2437 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, i
, SSA_OP_ALL_DEFS
)
2439 tree def
= DEF_FROM_PTR (def_p
);
2440 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
2441 if (symbol_marked_for_renaming (sym
))
2442 mark_def_interesting (sym
, stmt
, bb
, insert_phi_p
);
2446 /* Now visit all the blocks dominated by BB. */
2447 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
2449 son
= next_dom_son (CDI_DOMINATORS
, son
))
2450 prepare_block_for_update (son
, insert_phi_p
);
2454 /* Helper for prepare_names_to_update. Mark all the use sites for
2455 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2456 prepare_names_to_update. */
2459 prepare_use_sites_for (tree name
, bool insert_phi_p
)
2461 use_operand_p use_p
;
2462 imm_use_iterator iter
;
2464 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
2466 gimple stmt
= USE_STMT (use_p
);
2467 basic_block bb
= gimple_bb (stmt
);
2469 if (gimple_code (stmt
) == GIMPLE_PHI
)
2471 int ix
= PHI_ARG_INDEX_FROM_USE (use_p
);
2472 edge e
= gimple_phi_arg_edge (stmt
, ix
);
2473 mark_use_interesting (name
, stmt
, e
->src
, insert_phi_p
);
2477 /* For regular statements, mark this as an interesting use
2479 mark_use_interesting (name
, stmt
, bb
, insert_phi_p
);
2485 /* Helper for prepare_names_to_update. Mark the definition site for
2486 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2487 prepare_names_to_update. */
2490 prepare_def_site_for (tree name
, bool insert_phi_p
)
2495 gcc_assert (names_to_release
== NULL
2496 || !bitmap_bit_p (names_to_release
, SSA_NAME_VERSION (name
)));
2498 stmt
= SSA_NAME_DEF_STMT (name
);
2499 bb
= gimple_bb (stmt
);
2502 gcc_assert (bb
->index
< last_basic_block
);
2503 mark_block_for_update (bb
);
2504 mark_def_interesting (name
, stmt
, bb
, insert_phi_p
);
2509 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2510 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2511 PHI nodes for newly created names. */
2514 prepare_names_to_update (bool insert_phi_p
)
2518 sbitmap_iterator sbi
;
2520 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2521 remove it from NEW_SSA_NAMES so that we don't try to visit its
2522 defining basic block (which most likely doesn't exist). Notice
2523 that we cannot do the same with names in OLD_SSA_NAMES because we
2524 want to replace existing instances. */
2525 if (names_to_release
)
2526 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2527 RESET_BIT (new_ssa_names
, i
);
2529 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2530 names may be considered to be live-in on blocks that contain
2531 definitions for their replacements. */
2532 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
2533 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2535 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2536 OLD_SSA_NAMES, but we have to ignore its definition site. */
2537 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
2539 if (names_to_release
== NULL
|| !bitmap_bit_p (names_to_release
, i
))
2540 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2541 prepare_use_sites_for (ssa_name (i
), insert_phi_p
);
2546 /* Dump all the names replaced by NAME to FILE. */
2549 dump_names_replaced_by (FILE *file
, tree name
)
2555 print_generic_expr (file
, name
, 0);
2556 fprintf (file
, " -> { ");
2558 old_set
= names_replaced_by (name
);
2559 EXECUTE_IF_SET_IN_BITMAP (old_set
, 0, i
, bi
)
2561 print_generic_expr (file
, ssa_name (i
), 0);
2562 fprintf (file
, " ");
2565 fprintf (file
, "}\n");
2569 /* Dump all the names replaced by NAME to stderr. */
2572 debug_names_replaced_by (tree name
)
2574 dump_names_replaced_by (stderr
, name
);
2578 /* Dump SSA update information to FILE. */
2581 dump_update_ssa (FILE *file
)
2586 if (!need_ssa_update_p ())
2589 if (new_ssa_names
&& sbitmap_first_set_bit (new_ssa_names
) >= 0)
2591 sbitmap_iterator sbi
;
2593 fprintf (file
, "\nSSA replacement table\n");
2594 fprintf (file
, "N_i -> { O_1 ... O_j } means that N_i replaces "
2595 "O_1, ..., O_j\n\n");
2597 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
2598 dump_names_replaced_by (file
, ssa_name (i
));
2600 fprintf (file
, "\n");
2601 fprintf (file
, "Number of virtual NEW -> OLD mappings: %7u\n",
2602 update_ssa_stats
.num_virtual_mappings
);
2603 fprintf (file
, "Number of real NEW -> OLD mappings: %7u\n",
2604 update_ssa_stats
.num_total_mappings
2605 - update_ssa_stats
.num_virtual_mappings
);
2606 fprintf (file
, "Number of total NEW -> OLD mappings: %7u\n",
2607 update_ssa_stats
.num_total_mappings
);
2609 fprintf (file
, "\nNumber of virtual symbols: %u\n",
2610 update_ssa_stats
.num_virtual_symbols
);
2613 if (syms_to_rename
&& !bitmap_empty_p (syms_to_rename
))
2615 fprintf (file
, "\n\nSymbols to be put in SSA form\n\n");
2616 dump_decl_set (file
, syms_to_rename
);
2619 if (names_to_release
&& !bitmap_empty_p (names_to_release
))
2621 fprintf (file
, "\n\nSSA names to release after updating the SSA web\n\n");
2622 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2624 print_generic_expr (file
, ssa_name (i
), 0);
2625 fprintf (file
, " ");
2629 fprintf (file
, "\n\n");
2633 /* Dump SSA update information to stderr. */
2636 debug_update_ssa (void)
2638 dump_update_ssa (stderr
);
2642 /* Initialize data structures used for incremental SSA updates. */
2645 init_update_ssa (void)
2647 /* Reserve more space than the current number of names. The calls to
2648 add_new_name_mapping are typically done after creating new SSA
2649 names, so we'll need to reallocate these arrays. */
2650 old_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2651 sbitmap_zero (old_ssa_names
);
2653 new_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2654 sbitmap_zero (new_ssa_names
);
2656 repl_tbl
= htab_create (20, repl_map_hash
, repl_map_eq
, repl_map_free
);
2657 need_to_initialize_update_ssa_p
= false;
2658 need_to_update_vops_p
= false;
2659 syms_to_rename
= BITMAP_ALLOC (NULL
);
2660 regs_to_rename
= BITMAP_ALLOC (NULL
);
2661 mem_syms_to_rename
= BITMAP_ALLOC (NULL
);
2662 names_to_release
= NULL
;
2663 memset (&update_ssa_stats
, 0, sizeof (update_ssa_stats
));
2664 update_ssa_stats
.virtual_symbols
= BITMAP_ALLOC (NULL
);
2668 /* Deallocate data structures used for incremental SSA updates. */
2671 delete_update_ssa (void)
2676 sbitmap_free (old_ssa_names
);
2677 old_ssa_names
= NULL
;
2679 sbitmap_free (new_ssa_names
);
2680 new_ssa_names
= NULL
;
2682 htab_delete (repl_tbl
);
2685 need_to_initialize_update_ssa_p
= true;
2686 need_to_update_vops_p
= false;
2687 BITMAP_FREE (syms_to_rename
);
2688 BITMAP_FREE (regs_to_rename
);
2689 BITMAP_FREE (mem_syms_to_rename
);
2690 BITMAP_FREE (update_ssa_stats
.virtual_symbols
);
2692 if (names_to_release
)
2694 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2695 release_ssa_name (ssa_name (i
));
2696 BITMAP_FREE (names_to_release
);
2699 clear_ssa_name_info ();
2701 fini_ssa_renamer ();
2703 if (blocks_with_phis_to_rewrite
)
2704 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite
, 0, i
, bi
)
2706 gimple_vec phis
= VEC_index (gimple_vec
, phis_to_rewrite
, i
);
2708 VEC_free (gimple
, heap
, phis
);
2709 VEC_replace (gimple_vec
, phis_to_rewrite
, i
, NULL
);
2712 BITMAP_FREE (blocks_with_phis_to_rewrite
);
2713 BITMAP_FREE (blocks_to_update
);
2717 /* Create a new name for OLD_NAME in statement STMT and replace the
2718 operand pointed to by DEF_P with the newly created name. Return
2719 the new name and register the replacement mapping <NEW, OLD> in
2720 update_ssa's tables. */
2723 create_new_def_for (tree old_name
, gimple stmt
, def_operand_p def
)
2725 tree new_name
= duplicate_ssa_name (old_name
, stmt
);
2727 SET_DEF (def
, new_name
);
2729 if (gimple_code (stmt
) == GIMPLE_PHI
)
2733 basic_block bb
= gimple_bb (stmt
);
2735 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2736 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2737 if (e
->flags
& EDGE_ABNORMAL
)
2739 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name
) = 1;
2744 register_new_name_mapping (new_name
, old_name
);
2746 /* For the benefit of passes that will be updating the SSA form on
2747 their own, set the current reaching definition of OLD_NAME to be
2749 set_current_def (old_name
, new_name
);
2755 /* Register name NEW to be a replacement for name OLD. This function
2756 must be called for every replacement that should be performed by
2760 register_new_name_mapping (tree new_Tree ATTRIBUTE_UNUSED
, tree old ATTRIBUTE_UNUSED
)
2762 if (need_to_initialize_update_ssa_p
)
2765 add_new_name_mapping (new_Tree
, old
);
2769 /* Register symbol SYM to be renamed by update_ssa. */
2772 mark_sym_for_renaming (tree sym
)
2774 if (need_to_initialize_update_ssa_p
)
2777 bitmap_set_bit (syms_to_rename
, DECL_UID (sym
));
2779 if (!is_gimple_reg (sym
))
2781 need_to_update_vops_p
= true;
2782 if (memory_partition (sym
))
2783 bitmap_set_bit (syms_to_rename
, DECL_UID (memory_partition (sym
)));
2788 /* Register all the symbols in SET to be renamed by update_ssa. */
2791 mark_set_for_renaming (bitmap set
)
2796 if (set
== NULL
|| bitmap_empty_p (set
))
2799 if (need_to_initialize_update_ssa_p
)
2802 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
2803 mark_sym_for_renaming (referenced_var (i
));
2807 /* Return true if there is any work to be done by update_ssa. */
2810 need_ssa_update_p (void)
2812 return syms_to_rename
|| old_ssa_names
|| new_ssa_names
;
2815 /* Return true if SSA name mappings have been registered for SSA updating. */
2818 name_mappings_registered_p (void)
2820 return repl_tbl
&& htab_elements (repl_tbl
) > 0;
2823 /* Return true if name N has been registered in the replacement table. */
2826 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED
)
2828 if (!need_ssa_update_p ())
2831 return is_new_name (n
)
2833 || symbol_marked_for_renaming (SSA_NAME_VAR (n
));
2837 /* Return the set of all the SSA names marked to be replaced. */
2840 ssa_names_to_replace (void)
2844 sbitmap_iterator sbi
;
2846 ret
= BITMAP_ALLOC (NULL
);
2847 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
2848 bitmap_set_bit (ret
, i
);
2854 /* Mark NAME to be released after update_ssa has finished. */
2857 release_ssa_name_after_update_ssa (tree name
)
2859 gcc_assert (!need_to_initialize_update_ssa_p
);
2861 if (names_to_release
== NULL
)
2862 names_to_release
= BITMAP_ALLOC (NULL
);
2864 bitmap_set_bit (names_to_release
, SSA_NAME_VERSION (name
));
2868 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2869 frontier information. BLOCKS is the set of blocks to be updated.
2871 This is slightly different than the regular PHI insertion
2872 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
2873 real names (i.e., GIMPLE registers) are inserted:
2875 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2876 nodes inside the region affected by the block that defines VAR
2877 and the blocks that define all its replacements. All these
2878 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2880 First, we compute the entry point to the region (ENTRY). This is
2881 given by the nearest common dominator to all the definition
2882 blocks. When computing the iterated dominance frontier (IDF), any
2883 block not strictly dominated by ENTRY is ignored.
2885 We then call the standard PHI insertion algorithm with the pruned
2888 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
2889 names is not pruned. PHI nodes are inserted at every IDF block. */
2892 insert_updated_phi_nodes_for (tree var
, bitmap
*dfs
, bitmap blocks
,
2893 unsigned update_flags
)
2896 struct def_blocks_d
*db
;
2897 bitmap idf
, pruned_idf
;
2901 #if defined ENABLE_CHECKING
2902 if (TREE_CODE (var
) == SSA_NAME
)
2903 gcc_assert (is_old_name (var
));
2905 gcc_assert (symbol_marked_for_renaming (var
));
2908 /* Get all the definition sites for VAR. */
2909 db
= find_def_blocks_for (var
);
2911 /* No need to do anything if there were no definitions to VAR. */
2912 if (db
== NULL
|| bitmap_empty_p (db
->def_blocks
))
2915 /* Compute the initial iterated dominance frontier. */
2916 idf
= compute_idf (db
->def_blocks
, dfs
);
2917 pruned_idf
= BITMAP_ALLOC (NULL
);
2919 if (TREE_CODE (var
) == SSA_NAME
)
2921 if (update_flags
== TODO_update_ssa
)
2923 /* If doing regular SSA updates for GIMPLE registers, we are
2924 only interested in IDF blocks dominated by the nearest
2925 common dominator of all the definition blocks. */
2926 entry
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
2928 if (entry
!= ENTRY_BLOCK_PTR
)
2929 EXECUTE_IF_SET_IN_BITMAP (idf
, 0, i
, bi
)
2930 if (BASIC_BLOCK (i
) != entry
2931 && dominated_by_p (CDI_DOMINATORS
, BASIC_BLOCK (i
), entry
))
2932 bitmap_set_bit (pruned_idf
, i
);
2936 /* Otherwise, do not prune the IDF for VAR. */
2937 gcc_assert (update_flags
== TODO_update_ssa_full_phi
);
2938 bitmap_copy (pruned_idf
, idf
);
2943 /* Otherwise, VAR is a symbol that needs to be put into SSA form
2944 for the first time, so we need to compute the full IDF for
2946 bitmap_copy (pruned_idf
, idf
);
2949 if (!bitmap_empty_p (pruned_idf
))
2951 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
2952 are included in the region to be updated. The feeding blocks
2953 are important to guarantee that the PHI arguments are renamed
2956 /* FIXME, this is not needed if we are updating symbols. We are
2957 already starting at the ENTRY block anyway. */
2958 bitmap_ior_into (blocks
, pruned_idf
);
2959 EXECUTE_IF_SET_IN_BITMAP (pruned_idf
, 0, i
, bi
)
2963 basic_block bb
= BASIC_BLOCK (i
);
2965 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2966 if (e
->src
->index
>= 0)
2967 bitmap_set_bit (blocks
, e
->src
->index
);
2970 insert_phi_nodes_for (var
, pruned_idf
, true);
2973 BITMAP_FREE (pruned_idf
);
2978 /* Heuristic to determine whether SSA name mappings for virtual names
2979 should be discarded and their symbols rewritten from scratch. When
2980 there is a large number of mappings for virtual names, the
2981 insertion of PHI nodes for the old names in the mappings takes
2982 considerable more time than if we inserted PHI nodes for the
2985 Currently the heuristic takes these stats into account:
2987 - Number of mappings for virtual SSA names.
2988 - Number of distinct virtual symbols involved in those mappings.
2990 If the number of virtual mappings is much larger than the number of
2991 virtual symbols, then it will be faster to compute PHI insertion
2992 spots for the symbols. Even if this involves traversing the whole
2993 CFG, which is what happens when symbols are renamed from scratch. */
2996 switch_virtuals_to_full_rewrite_p (void)
2998 if (update_ssa_stats
.num_virtual_mappings
< (unsigned) MIN_VIRTUAL_MAPPINGS
)
3001 if (update_ssa_stats
.num_virtual_mappings
3002 > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
3003 * update_ssa_stats
.num_virtual_symbols
)
3010 /* Remove every virtual mapping and mark all the affected virtual
3011 symbols for renaming. */
3014 switch_virtuals_to_full_rewrite (void)
3017 sbitmap_iterator sbi
;
3021 fprintf (dump_file
, "\nEnabled virtual name mapping heuristic.\n");
3022 fprintf (dump_file
, "\tNumber of virtual mappings: %7u\n",
3023 update_ssa_stats
.num_virtual_mappings
);
3024 fprintf (dump_file
, "\tNumber of unique virtual symbols: %7u\n",
3025 update_ssa_stats
.num_virtual_symbols
);
3026 fprintf (dump_file
, "Updating FUD-chains from top of CFG will be "
3027 "faster than processing\nthe name mappings.\n\n");
3030 /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
3031 Note that it is not really necessary to remove the mappings from
3032 REPL_TBL, that would only waste time. */
3033 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
3034 if (!is_gimple_reg (ssa_name (i
)))
3035 RESET_BIT (new_ssa_names
, i
);
3037 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
3038 if (!is_gimple_reg (ssa_name (i
)))
3039 RESET_BIT (old_ssa_names
, i
);
3041 mark_set_for_renaming (update_ssa_stats
.virtual_symbols
);
3045 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3046 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3048 1- The names in OLD_SSA_NAMES dominated by the definitions of
3049 NEW_SSA_NAMES are all re-written to be reached by the
3050 appropriate definition from NEW_SSA_NAMES.
3052 2- If needed, new PHI nodes are added to the iterated dominance
3053 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3055 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3056 calling register_new_name_mapping for every pair of names that the
3057 caller wants to replace.
3059 The caller identifies the new names that have been inserted and the
3060 names that need to be replaced by calling register_new_name_mapping
3061 for every pair <NEW, OLD>. Note that the function assumes that the
3062 new names have already been inserted in the IL.
3064 For instance, given the following code:
3067 2 x_1 = PHI (0, x_5)
3078 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3081 2 x_1 = PHI (0, x_5)
3094 We want to replace all the uses of x_1 with the new definitions of
3095 x_10 and x_11. Note that the only uses that should be replaced are
3096 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3097 *not* be replaced (this is why we cannot just mark symbol 'x' for
3100 Additionally, we may need to insert a PHI node at line 11 because
3101 that is a merge point for x_10 and x_11. So the use of x_1 at line
3102 11 will be replaced with the new PHI node. The insertion of PHI
3103 nodes is optional. They are not strictly necessary to preserve the
3104 SSA form, and depending on what the caller inserted, they may not
3105 even be useful for the optimizers. UPDATE_FLAGS controls various
3106 aspects of how update_ssa operates, see the documentation for
3107 TODO_update_ssa*. */
3110 update_ssa (unsigned update_flags
)
3112 basic_block bb
, start_bb
;
3117 sbitmap_iterator sbi
;
3119 if (!need_ssa_update_p ())
3122 timevar_push (TV_TREE_SSA_INCREMENTAL
);
3124 blocks_with_phis_to_rewrite
= BITMAP_ALLOC (NULL
);
3125 if (!phis_to_rewrite
)
3126 phis_to_rewrite
= VEC_alloc (gimple_vec
, heap
, last_basic_block
);
3127 blocks_to_update
= BITMAP_ALLOC (NULL
);
3129 /* Ensure that the dominance information is up-to-date. */
3130 calculate_dominance_info (CDI_DOMINATORS
);
3132 /* Only one update flag should be set. */
3133 gcc_assert (update_flags
== TODO_update_ssa
3134 || update_flags
== TODO_update_ssa_no_phi
3135 || update_flags
== TODO_update_ssa_full_phi
3136 || update_flags
== TODO_update_ssa_only_virtuals
);
3138 /* If we only need to update virtuals, remove all the mappings for
3139 real names before proceeding. The caller is responsible for
3140 having dealt with the name mappings before calling update_ssa. */
3141 if (update_flags
== TODO_update_ssa_only_virtuals
)
3143 sbitmap_zero (old_ssa_names
);
3144 sbitmap_zero (new_ssa_names
);
3145 htab_empty (repl_tbl
);
3148 insert_phi_p
= (update_flags
!= TODO_update_ssa_no_phi
);
3152 /* If the caller requested PHI nodes to be added, initialize
3153 live-in information data structures (DEF_BLOCKS). */
3155 /* For each SSA name N, the DEF_BLOCKS table describes where the
3156 name is defined, which blocks have PHI nodes for N, and which
3157 blocks have uses of N (i.e., N is live-on-entry in those
3159 def_blocks
= htab_create (num_ssa_names
, def_blocks_hash
,
3160 def_blocks_eq
, def_blocks_free
);
3167 /* Heuristic to avoid massive slow downs when the replacement
3168 mappings include lots of virtual names. */
3169 if (insert_phi_p
&& switch_virtuals_to_full_rewrite_p ())
3170 switch_virtuals_to_full_rewrite ();
3172 /* If there are symbols to rename, identify those symbols that are
3173 GIMPLE registers into the set REGS_TO_RENAME and those that are
3174 memory symbols into the set MEM_SYMS_TO_RENAME. */
3175 if (!bitmap_empty_p (syms_to_rename
))
3180 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename
, 0, i
, bi
)
3182 tree sym
= referenced_var (i
);
3183 if (is_gimple_reg (sym
))
3184 bitmap_set_bit (regs_to_rename
, i
);
3187 /* Memory partitioning information may have been
3188 computed after the symbol was marked for renaming,
3189 if SYM is inside a partition also mark the partition
3191 tree mpt
= memory_partition (sym
);
3193 bitmap_set_bit (syms_to_rename
, DECL_UID (mpt
));
3197 /* Memory symbols are those not in REGS_TO_RENAME. */
3198 bitmap_and_compl (mem_syms_to_rename
, syms_to_rename
, regs_to_rename
);
3201 /* If there are names defined in the replacement table, prepare
3202 definition and use sites for all the names in NEW_SSA_NAMES and
3204 if (sbitmap_first_set_bit (new_ssa_names
) >= 0)
3206 prepare_names_to_update (insert_phi_p
);
3208 /* If all the names in NEW_SSA_NAMES had been marked for
3209 removal, and there are no symbols to rename, then there's
3210 nothing else to do. */
3211 if (sbitmap_first_set_bit (new_ssa_names
) < 0
3212 && bitmap_empty_p (syms_to_rename
))
3216 /* Next, determine the block at which to start the renaming process. */
3217 if (!bitmap_empty_p (syms_to_rename
))
3219 /* If we have to rename some symbols from scratch, we need to
3220 start the process at the root of the CFG. FIXME, it should
3221 be possible to determine the nearest block that had a
3222 definition for each of the symbols that are marked for
3223 updating. For now this seems more work than it's worth. */
3224 start_bb
= ENTRY_BLOCK_PTR
;
3226 /* Traverse the CFG looking for existing definitions and uses of
3227 symbols in SYMS_TO_RENAME. Mark interesting blocks and
3228 statements and set local live-in information for the PHI
3229 placement heuristics. */
3230 prepare_block_for_update (start_bb
, insert_phi_p
);
3234 /* Otherwise, the entry block to the region is the nearest
3235 common dominator for the blocks in BLOCKS. */
3236 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3240 /* If requested, insert PHI nodes at the iterated dominance frontier
3241 of every block, creating new definitions for names in OLD_SSA_NAMES
3242 and for symbols in SYMS_TO_RENAME. */
3247 /* If the caller requested PHI nodes to be added, compute
3248 dominance frontiers. */
3249 dfs
= XNEWVEC (bitmap
, last_basic_block
);
3251 dfs
[bb
->index
] = BITMAP_ALLOC (NULL
);
3252 compute_dominance_frontiers (dfs
);
3254 if (sbitmap_first_set_bit (old_ssa_names
) >= 0)
3256 sbitmap_iterator sbi
;
3258 /* insert_update_phi_nodes_for will call add_new_name_mapping
3259 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3260 will grow while we are traversing it (but it will not
3261 gain any new members). Copy OLD_SSA_NAMES to a temporary
3263 sbitmap tmp
= sbitmap_alloc (old_ssa_names
->n_bits
);
3264 sbitmap_copy (tmp
, old_ssa_names
);
3265 EXECUTE_IF_SET_IN_SBITMAP (tmp
, 0, i
, sbi
)
3266 insert_updated_phi_nodes_for (ssa_name (i
), dfs
, blocks_to_update
,
3271 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename
, 0, i
, bi
)
3272 insert_updated_phi_nodes_for (referenced_var (i
), dfs
, blocks_to_update
,
3276 BITMAP_FREE (dfs
[bb
->index
]);
3279 /* Insertion of PHI nodes may have added blocks to the region.
3280 We need to re-compute START_BB to include the newly added
3282 if (start_bb
!= ENTRY_BLOCK_PTR
)
3283 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3287 /* Reset the current definition for name and symbol before renaming
3289 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
3290 set_current_def (ssa_name (i
), NULL_TREE
);
3292 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename
, 0, i
, bi
)
3293 set_current_def (referenced_var (i
), NULL_TREE
);
3295 /* Now start the renaming process at START_BB. */
3296 tmp
= sbitmap_alloc (last_basic_block
);
3298 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3301 rewrite_blocks (start_bb
, REWRITE_UPDATE
, tmp
);
3305 /* Debugging dumps. */
3311 dump_update_ssa (dump_file
);
3313 fprintf (dump_file
, "Incremental SSA update started at block: %d\n\n",
3317 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3319 fprintf (dump_file
, "Number of blocks in CFG: %d\n", last_basic_block
);
3320 fprintf (dump_file
, "Number of blocks to update: %d (%3.0f%%)\n\n",
3321 c
, PERCENT (c
, last_basic_block
));
3323 if (dump_flags
& TDF_DETAILS
)
3325 fprintf (dump_file
, "Affected blocks: ");
3326 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3327 fprintf (dump_file
, "%u ", i
);
3328 fprintf (dump_file
, "\n");
3331 fprintf (dump_file
, "\n\n");
3334 /* Free allocated memory. */
3336 delete_update_ssa ();
3338 timevar_pop (TV_TREE_SSA_INCREMENTAL
);