1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007
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"
39 #include "tree-gimple.h"
40 #include "tree-inline.h"
44 #include "tree-dump.h"
45 #include "tree-pass.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 /* Structure to map a variable VAR to the set of blocks that contain
59 definitions for VAR. */
65 /* Blocks that contain definitions of VAR. Bit I will be set if the
66 Ith block contains a definition of VAR. */
69 /* Blocks that contain a PHI node for VAR. */
72 /* Blocks where VAR is live-on-entry. Similar semantics as
78 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
79 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
80 basic blocks where VAR is defined (assigned a new value). It also
81 contains a bitmap of all the blocks where VAR is live-on-entry
82 (i.e., there is a use of VAR in block B without a preceding
83 definition in B). The live-on-entry information is used when
84 computing PHI pruning heuristics. */
85 static htab_t def_blocks
;
87 /* Stack of trees used to restore the global currdefs to its original
88 state after completing rewriting of a block and its dominator
89 children. Its elements have the following properties:
91 - An SSA_NAME (N) indicates that the current definition of the
92 underlying variable should be set to the given SSA_NAME. If the
93 symbol associated with the SSA_NAME is not a GIMPLE register, the
94 next slot in the stack must be a _DECL node (SYM). In this case,
95 the name N in the previous slot is the current reaching
98 - A _DECL node indicates that the underlying variable has no
101 - A NULL node at the top entry is used to mark the last slot
102 associated with the current block. */
103 static VEC(tree
,heap
) *block_defs_stack
;
105 /* Set of existing SSA names being replaced by update_ssa. */
106 static sbitmap old_ssa_names
;
108 /* Set of new SSA names being added by update_ssa. Note that both
109 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
110 the operations done on them are presence tests. */
111 static sbitmap new_ssa_names
;
113 /* Symbols whose SSA form needs to be updated or created for the first
115 static bitmap syms_to_rename
;
117 /* Subset of SYMS_TO_RENAME. Contains all the GIMPLE register symbols
118 that have been marked for renaming. */
119 static bitmap regs_to_rename
;
121 /* Subset of SYMS_TO_RENAME. Contains all the memory symbols
122 that have been marked for renaming. */
123 static bitmap mem_syms_to_rename
;
125 /* Set of SSA names that have been marked to be released after they
126 were registered in the replacement table. They will be finally
127 released after we finish updating the SSA web. */
128 static bitmap names_to_release
;
130 /* For each block, the PHI nodes that need to be rewritten are stored into
132 typedef VEC(tree
, heap
) *tree_vec
;
133 DEF_VEC_P (tree_vec
);
134 DEF_VEC_ALLOC_P (tree_vec
, heap
);
136 static VEC(tree_vec
, heap
) *phis_to_rewrite
;
138 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
139 static bitmap blocks_with_phis_to_rewrite
;
141 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
142 to grow as the callers to register_new_name_mapping will typically
143 create new names on the fly. FIXME. Currently set to 1/3 to avoid
144 frequent reallocations but still need to find a reasonable growth
146 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
148 /* Tuple used to represent replacement mappings. */
155 /* NEW -> OLD_SET replacement table. If we are replacing several
156 existing SSA names O_1, O_2, ..., O_j with a new name N_i,
157 then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }. */
158 static htab_t repl_tbl
;
160 /* true if register_new_name_mapping needs to initialize the data
161 structures needed by update_ssa. */
162 static bool need_to_initialize_update_ssa_p
= true;
164 /* true if update_ssa needs to update virtual operands. */
165 static bool need_to_update_vops_p
= false;
167 /* Statistics kept by update_ssa to use in the virtual mapping
168 heuristic. If the number of virtual mappings is beyond certain
169 threshold, the updater will switch from using the mappings into
170 renaming the virtual symbols from scratch. In some cases, the
171 large number of name mappings for virtual names causes significant
172 slowdowns in the PHI insertion code. */
173 struct update_ssa_stats_d
175 unsigned num_virtual_mappings
;
176 unsigned num_total_mappings
;
177 bitmap virtual_symbols
;
178 unsigned num_virtual_symbols
;
180 static struct update_ssa_stats_d update_ssa_stats
;
182 /* Global data to attach to the main dominator walk structure. */
183 struct mark_def_sites_global_data
185 /* This bitmap contains the variables which are set before they
186 are used in a basic block. */
189 /* Bitmap of names to rename. */
190 sbitmap names_to_rename
;
192 /* Set of blocks that mark_def_sites deems interesting for the
193 renamer to process. */
194 sbitmap interesting_blocks
;
198 /* Information stored for SSA names. */
201 /* The current reaching definition replacing this SSA name. */
204 /* This field indicates whether or not the variable may need PHI nodes.
205 See the enum's definition for more detailed information about the
207 ENUM_BITFIELD (need_phi_state
) need_phi_state
: 2;
209 /* Age of this record (so that info_for_ssa_name table can be cleared
210 quicky); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
211 are assumed to be null. */
215 /* The information associated with names. */
216 typedef struct ssa_name_info
*ssa_name_info_p
;
217 DEF_VEC_P (ssa_name_info_p
);
218 DEF_VEC_ALLOC_P (ssa_name_info_p
, heap
);
220 static VEC(ssa_name_info_p
, heap
) *info_for_ssa_name
;
221 static unsigned current_info_for_ssa_name_age
;
223 /* The set of blocks affected by update_ssa. */
224 static bitmap blocks_to_update
;
226 /* The main entry point to the SSA renamer (rewrite_blocks) may be
227 called several times to do different, but related, tasks.
228 Initially, we need it to rename the whole program into SSA form.
229 At other times, we may need it to only rename into SSA newly
230 exposed symbols. Finally, we can also call it to incrementally fix
231 an already built SSA web. */
233 /* Convert the whole function into SSA form. */
236 /* Incrementally update the SSA web by replacing existing SSA
237 names with new ones. See update_ssa for details. */
242 /* Use TREE_VISITED to keep track of which statements we want to
243 rename. When renaming a subset of the variables, not all
244 statements will be processed. This is decided in mark_def_sites. */
245 #define REWRITE_THIS_STMT(T) TREE_VISITED (T)
247 /* Use the unsigned flag to keep track of which statements we want to
248 visit when marking new definition sites. This is slightly
249 different than REWRITE_THIS_STMT: it's used by update_ssa to
250 distinguish statements that need to have both uses and defs
251 processed from those that only need to have their defs processed.
252 Statements that define new SSA names only need to have their defs
253 registered, but they don't need to have their uses renamed. */
254 #define REGISTER_DEFS_IN_THIS_STMT(T) (T)->base.unsigned_flag
257 /* Prototypes for debugging functions. */
258 extern void dump_tree_ssa (FILE *);
259 extern void debug_tree_ssa (void);
260 extern void debug_def_blocks (void);
261 extern void dump_tree_ssa_stats (FILE *);
262 extern void debug_tree_ssa_stats (void);
263 extern void dump_update_ssa (FILE *);
264 extern void debug_update_ssa (void);
265 extern void dump_names_replaced_by (FILE *, tree
);
266 extern void debug_names_replaced_by (tree
);
267 extern void dump_def_blocks (FILE *);
268 extern void debug_def_blocks (void);
269 extern void dump_defs_stack (FILE *, int);
270 extern void debug_defs_stack (int);
271 extern void dump_currdefs (FILE *);
272 extern void debug_currdefs (void);
274 /* Get the information associated with NAME. */
276 static inline ssa_name_info_p
277 get_ssa_name_ann (tree name
)
279 unsigned ver
= SSA_NAME_VERSION (name
);
280 unsigned len
= VEC_length (ssa_name_info_p
, info_for_ssa_name
);
281 struct ssa_name_info
*info
;
285 unsigned new_len
= num_ssa_names
;
287 VEC_reserve (ssa_name_info_p
, heap
, info_for_ssa_name
, new_len
);
288 while (len
++ < new_len
)
290 struct ssa_name_info
*info
= XCNEW (struct ssa_name_info
);
291 info
->age
= current_info_for_ssa_name_age
;
292 VEC_quick_push (ssa_name_info_p
, info_for_ssa_name
, info
);
296 info
= VEC_index (ssa_name_info_p
, info_for_ssa_name
, ver
);
297 if (info
->age
< current_info_for_ssa_name_age
)
299 info
->need_phi_state
= 0;
300 info
->current_def
= NULL_TREE
;
301 info
->age
= current_info_for_ssa_name_age
;
308 /* Clears info for SSA names. */
311 clear_ssa_name_info (void)
313 current_info_for_ssa_name_age
++;
317 /* Get phi_state field for VAR. */
319 static inline enum need_phi_state
320 get_phi_state (tree var
)
322 if (TREE_CODE (var
) == SSA_NAME
)
323 return get_ssa_name_ann (var
)->need_phi_state
;
325 return var_ann (var
)->need_phi_state
;
329 /* Sets phi_state field for VAR to STATE. */
332 set_phi_state (tree var
, enum need_phi_state state
)
334 if (TREE_CODE (var
) == SSA_NAME
)
335 get_ssa_name_ann (var
)->need_phi_state
= state
;
337 var_ann (var
)->need_phi_state
= state
;
341 /* Return the current definition for VAR. */
344 get_current_def (tree var
)
346 if (TREE_CODE (var
) == SSA_NAME
)
347 return get_ssa_name_ann (var
)->current_def
;
349 return var_ann (var
)->current_def
;
353 /* Sets current definition of VAR to DEF. */
356 set_current_def (tree var
, tree def
)
358 if (TREE_CODE (var
) == SSA_NAME
)
359 get_ssa_name_ann (var
)->current_def
= def
;
361 var_ann (var
)->current_def
= def
;
365 /* Compute global livein information given the set of blockx where
366 an object is locally live at the start of the block (LIVEIN)
367 and the set of blocks where the object is defined (DEF_BLOCKS).
369 Note: This routine augments the existing local livein information
370 to include global livein (i.e., it modifies the underlying bitmap
374 compute_global_livein (bitmap livein
, bitmap def_blocks
)
376 basic_block bb
, *worklist
, *tos
;
381 = (basic_block
*) xmalloc (sizeof (basic_block
) * (last_basic_block
+ 1));
383 EXECUTE_IF_SET_IN_BITMAP (livein
, 0, i
, bi
)
384 *tos
++ = BASIC_BLOCK (i
);
386 /* Iterate until the worklist is empty. */
387 while (tos
!= worklist
)
392 /* Pull a block off the worklist. */
395 /* For each predecessor block. */
396 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
398 basic_block pred
= e
->src
;
399 int pred_index
= pred
->index
;
401 /* None of this is necessary for the entry block. */
402 if (pred
!= ENTRY_BLOCK_PTR
403 && ! bitmap_bit_p (livein
, pred_index
)
404 && ! bitmap_bit_p (def_blocks
, pred_index
))
407 bitmap_set_bit (livein
, pred_index
);
416 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
417 all statements in basic block BB. */
420 initialize_flags_in_bb (basic_block bb
)
423 block_stmt_iterator bsi
;
425 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
427 REWRITE_THIS_STMT (phi
) = 0;
428 REGISTER_DEFS_IN_THIS_STMT (phi
) = 0;
431 for (bsi
= bsi_start (bb
); !bsi_end_p (bsi
); bsi_next (&bsi
))
433 stmt
= bsi_stmt (bsi
);
434 /* We are going to use the operand cache API, such as
435 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
436 cache for each statement should be up-to-date. */
437 gcc_assert (!stmt_modified_p (stmt
));
438 REWRITE_THIS_STMT (stmt
) = 0;
439 REGISTER_DEFS_IN_THIS_STMT (stmt
) = 0;
443 /* Mark block BB as interesting for update_ssa. */
446 mark_block_for_update (basic_block bb
)
448 gcc_assert (blocks_to_update
!= NULL
);
449 if (bitmap_bit_p (blocks_to_update
, bb
->index
))
451 bitmap_set_bit (blocks_to_update
, bb
->index
);
452 initialize_flags_in_bb (bb
);
455 /* Return the set of blocks where variable VAR is defined and the blocks
456 where VAR is live on entry (livein). If no entry is found in
457 DEF_BLOCKS, a new one is created and returned. */
459 static inline struct def_blocks_d
*
460 get_def_blocks_for (tree var
)
462 struct def_blocks_d db
, *db_p
;
466 slot
= htab_find_slot (def_blocks
, (void *) &db
, INSERT
);
469 db_p
= XNEW (struct def_blocks_d
);
471 db_p
->def_blocks
= BITMAP_ALLOC (NULL
);
472 db_p
->phi_blocks
= BITMAP_ALLOC (NULL
);
473 db_p
->livein_blocks
= BITMAP_ALLOC (NULL
);
474 *slot
= (void *) db_p
;
477 db_p
= (struct def_blocks_d
*) *slot
;
483 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
484 VAR is defined by a PHI node. */
487 set_def_block (tree var
, basic_block bb
, bool phi_p
)
489 struct def_blocks_d
*db_p
;
490 enum need_phi_state state
;
492 state
= get_phi_state (var
);
493 db_p
= get_def_blocks_for (var
);
495 /* Set the bit corresponding to the block where VAR is defined. */
496 bitmap_set_bit (db_p
->def_blocks
, bb
->index
);
498 bitmap_set_bit (db_p
->phi_blocks
, bb
->index
);
500 /* Keep track of whether or not we may need to insert PHI nodes.
502 If we are in the UNKNOWN state, then this is the first definition
503 of VAR. Additionally, we have not seen any uses of VAR yet, so
504 we do not need a PHI node for this variable at this time (i.e.,
505 transition to NEED_PHI_STATE_NO).
507 If we are in any other state, then we either have multiple definitions
508 of this variable occurring in different blocks or we saw a use of the
509 variable which was not dominated by the block containing the
510 definition(s). In this case we may need a PHI node, so enter
511 state NEED_PHI_STATE_MAYBE. */
512 if (state
== NEED_PHI_STATE_UNKNOWN
)
513 set_phi_state (var
, NEED_PHI_STATE_NO
);
515 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
519 /* Mark block BB as having VAR live at the entry to BB. */
522 set_livein_block (tree var
, basic_block bb
)
524 struct def_blocks_d
*db_p
;
525 enum need_phi_state state
= get_phi_state (var
);
527 db_p
= get_def_blocks_for (var
);
529 /* Set the bit corresponding to the block where VAR is live in. */
530 bitmap_set_bit (db_p
->livein_blocks
, bb
->index
);
532 /* Keep track of whether or not we may need to insert PHI nodes.
534 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
535 by the single block containing the definition(s) of this variable. If
536 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
537 NEED_PHI_STATE_MAYBE. */
538 if (state
== NEED_PHI_STATE_NO
)
540 int def_block_index
= bitmap_first_set_bit (db_p
->def_blocks
);
542 if (def_block_index
== -1
543 || ! dominated_by_p (CDI_DOMINATORS
, bb
,
544 BASIC_BLOCK (def_block_index
)))
545 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
548 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
552 /* Return true if symbol SYM is marked for renaming. */
555 symbol_marked_for_renaming (tree sym
)
557 return bitmap_bit_p (syms_to_rename
, DECL_UID (sym
));
561 /* Return true if NAME is in OLD_SSA_NAMES. */
564 is_old_name (tree name
)
566 unsigned ver
= SSA_NAME_VERSION (name
);
567 return ver
< new_ssa_names
->n_bits
&& TEST_BIT (old_ssa_names
, ver
);
571 /* Return true if NAME is in NEW_SSA_NAMES. */
574 is_new_name (tree name
)
576 unsigned ver
= SSA_NAME_VERSION (name
);
577 return ver
< new_ssa_names
->n_bits
&& TEST_BIT (new_ssa_names
, ver
);
581 /* Hashing and equality functions for REPL_TBL. */
584 repl_map_hash (const void *p
)
586 return htab_hash_pointer ((const void *)((const struct repl_map_d
*)p
)->name
);
590 repl_map_eq (const void *p1
, const void *p2
)
592 return ((const struct repl_map_d
*)p1
)->name
593 == ((const struct repl_map_d
*)p2
)->name
;
597 repl_map_free (void *p
)
599 BITMAP_FREE (((struct repl_map_d
*)p
)->set
);
604 /* Return the names replaced by NEW (i.e., REPL_TBL[NEW].SET). */
607 names_replaced_by (tree
new)
613 slot
= htab_find_slot (repl_tbl
, (void *) &m
, NO_INSERT
);
615 /* If N was not registered in the replacement table, return NULL. */
616 if (slot
== NULL
|| *slot
== NULL
)
619 return ((struct repl_map_d
*) *slot
)->set
;
623 /* Add OLD to REPL_TBL[NEW].SET. */
626 add_to_repl_tbl (tree
new, tree old
)
628 struct repl_map_d m
, *mp
;
632 slot
= htab_find_slot (repl_tbl
, (void *) &m
, INSERT
);
635 mp
= XNEW (struct repl_map_d
);
637 mp
->set
= BITMAP_ALLOC (NULL
);
641 mp
= (struct repl_map_d
*) *slot
;
643 bitmap_set_bit (mp
->set
, SSA_NAME_VERSION (old
));
647 /* Add a new mapping NEW -> OLD REPL_TBL. Every entry N_i in REPL_TBL
648 represents the set of names O_1 ... O_j replaced by N_i. This is
649 used by update_ssa and its helpers to introduce new SSA names in an
650 already formed SSA web. */
653 add_new_name_mapping (tree
new, tree old
)
655 timevar_push (TV_TREE_SSA_INCREMENTAL
);
657 /* OLD and NEW must be different SSA names for the same symbol. */
658 gcc_assert (new != old
&& SSA_NAME_VAR (new) == SSA_NAME_VAR (old
));
660 /* If this mapping is for virtual names, we will need to update
661 virtual operands. If this is a mapping for .MEM, then we gather
662 the symbols associated with each name. */
663 if (!is_gimple_reg (new))
667 need_to_update_vops_p
= true;
669 update_ssa_stats
.num_virtual_mappings
++;
670 update_ssa_stats
.num_virtual_symbols
++;
672 /* Keep counts of virtual mappings and symbols to use in the
673 virtual mapping heuristic. If we have large numbers of
674 virtual mappings for a relatively low number of symbols, it
675 will make more sense to rename the symbols from scratch.
676 Otherwise, the insertion of PHI nodes for each of the old
677 names in these mappings will be very slow. */
678 sym
= SSA_NAME_VAR (new);
679 bitmap_set_bit (update_ssa_stats
.virtual_symbols
, DECL_UID (sym
));
682 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
683 caller may have created new names since the set was created. */
684 if (new_ssa_names
->n_bits
<= num_ssa_names
- 1)
686 unsigned int new_sz
= num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
;
687 new_ssa_names
= sbitmap_resize (new_ssa_names
, new_sz
, 0);
688 old_ssa_names
= sbitmap_resize (old_ssa_names
, new_sz
, 0);
691 /* Update the REPL_TBL table. */
692 add_to_repl_tbl (new, old
);
694 /* If OLD had already been registered as a new name, then all the
695 names that OLD replaces should also be replaced by NEW. */
696 if (is_new_name (old
))
697 bitmap_ior_into (names_replaced_by (new), names_replaced_by (old
));
699 /* Register NEW and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
701 SET_BIT (new_ssa_names
, SSA_NAME_VERSION (new));
702 SET_BIT (old_ssa_names
, SSA_NAME_VERSION (old
));
704 /* Update mapping counter to use in the virtual mapping heuristic. */
705 update_ssa_stats
.num_total_mappings
++;
707 timevar_pop (TV_TREE_SSA_INCREMENTAL
);
711 /* Call back for walk_dominator_tree used to collect definition sites
712 for every variable in the function. For every statement S in block
715 1- Variables defined by S in the DEFS of S are marked in the bitmap
716 WALK_DATA->GLOBAL_DATA->KILLS.
718 2- If S uses a variable VAR and there is no preceding kill of VAR,
719 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
721 This information is used to determine which variables are live
722 across block boundaries to reduce the number of PHI nodes
726 mark_def_sites (struct dom_walk_data
*walk_data
, basic_block bb
,
727 block_stmt_iterator bsi
)
729 struct mark_def_sites_global_data
*gd
;
735 stmt
= bsi_stmt (bsi
);
736 update_stmt_if_modified (stmt
);
738 gd
= (struct mark_def_sites_global_data
*) walk_data
->global_data
;
741 gcc_assert (blocks_to_update
== NULL
);
742 REGISTER_DEFS_IN_THIS_STMT (stmt
) = 0;
743 REWRITE_THIS_STMT (stmt
) = 0;
745 /* If a variable is used before being set, then the variable is live
746 across a block boundary, so mark it live-on-entry to BB. */
747 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
749 tree sym
= USE_FROM_PTR (use_p
);
750 gcc_assert (DECL_P (sym
));
751 if (!bitmap_bit_p (kills
, DECL_UID (sym
)))
752 set_livein_block (sym
, bb
);
753 REWRITE_THIS_STMT (stmt
) = 1;
756 /* Now process the defs. Mark BB as the definition block and add
757 each def to the set of killed symbols. */
758 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
760 gcc_assert (DECL_P (def
));
761 set_def_block (def
, bb
, false);
762 bitmap_set_bit (kills
, DECL_UID (def
));
763 REGISTER_DEFS_IN_THIS_STMT (stmt
) = 1;
766 /* If we found the statement interesting then also mark the block BB
768 if (REWRITE_THIS_STMT (stmt
) || REGISTER_DEFS_IN_THIS_STMT (stmt
))
769 SET_BIT (gd
->interesting_blocks
, bb
->index
);
772 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
773 in the dfs numbering of the dominance tree. */
777 /* Basic block whose index this entry corresponds to. */
780 /* The dfs number of this node. */
784 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
788 cmp_dfsnum (const void *a
, const void *b
)
790 const struct dom_dfsnum
*da
= a
;
791 const struct dom_dfsnum
*db
= b
;
793 return (int) da
->dfs_num
- (int) db
->dfs_num
;
796 /* Among the intervals starting at the N points specified in DEFS, find
797 the one that contains S, and return its bb_index. */
800 find_dfsnum_interval (struct dom_dfsnum
*defs
, unsigned n
, unsigned s
)
802 unsigned f
= 0, t
= n
, m
;
807 if (defs
[m
].dfs_num
<= s
)
813 return defs
[f
].bb_index
;
816 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
817 KILLS is a bitmap of blocks where the value is defined before any use. */
820 prune_unused_phi_nodes (bitmap phis
, bitmap kills
, bitmap uses
)
822 VEC(int, heap
) *worklist
;
824 unsigned i
, b
, p
, u
, top
;
826 basic_block def_bb
, use_bb
;
830 struct dom_dfsnum
*defs
;
831 unsigned n_defs
, adef
;
833 if (bitmap_empty_p (uses
))
839 /* The phi must dominate a use, or an argument of a live phi. Also, we
840 do not create any phi nodes in def blocks, unless they are also livein. */
841 to_remove
= BITMAP_ALLOC (NULL
);
842 bitmap_and_compl (to_remove
, kills
, uses
);
843 bitmap_and_compl_into (phis
, to_remove
);
844 if (bitmap_empty_p (phis
))
846 BITMAP_FREE (to_remove
);
850 /* We want to remove the unnecessary phi nodes, but we do not want to compute
851 liveness information, as that may be linear in the size of CFG, and if
852 there are lot of different variables to rewrite, this may lead to quadratic
855 Instead, we basically emulate standard dce. We put all uses to worklist,
856 then for each of them find the nearest def that dominates them. If this
857 def is a phi node, we mark it live, and if it was not live before, we
858 add the predecessors of its basic block to the worklist.
860 To quickly locate the nearest def that dominates use, we use dfs numbering
861 of the dominance tree (that is already available in order to speed up
862 queries). For each def, we have the interval given by the dfs number on
863 entry to and on exit from the corresponding subtree in the dominance tree.
864 The nearest dominator for a given use is the smallest of these intervals
865 that contains entry and exit dfs numbers for the basic block with the use.
866 If we store the bounds for all the uses to an array and sort it, we can
867 locate the nearest dominating def in logarithmic time by binary search.*/
868 bitmap_ior (to_remove
, kills
, phis
);
869 n_defs
= bitmap_count_bits (to_remove
);
870 defs
= XNEWVEC (struct dom_dfsnum
, 2 * n_defs
+ 1);
871 defs
[0].bb_index
= 1;
874 EXECUTE_IF_SET_IN_BITMAP (to_remove
, 0, i
, bi
)
876 def_bb
= BASIC_BLOCK (i
);
877 defs
[adef
].bb_index
= i
;
878 defs
[adef
].dfs_num
= bb_dom_dfs_in (CDI_DOMINATORS
, def_bb
);
879 defs
[adef
+ 1].bb_index
= i
;
880 defs
[adef
+ 1].dfs_num
= bb_dom_dfs_out (CDI_DOMINATORS
, def_bb
);
883 BITMAP_FREE (to_remove
);
884 gcc_assert (adef
== 2 * n_defs
+ 1);
885 qsort (defs
, adef
, sizeof (struct dom_dfsnum
), cmp_dfsnum
);
886 gcc_assert (defs
[0].bb_index
== 1);
888 /* Now each DEFS entry contains the number of the basic block to that the
889 dfs number corresponds. Change them to the number of basic block that
890 corresponds to the interval following the dfs number. Also, for the
891 dfs_out numbers, increase the dfs number by one (so that it corresponds
892 to the start of the following interval, not to the end of the current
893 one). We use WORKLIST as a stack. */
894 worklist
= VEC_alloc (int, heap
, n_defs
+ 1);
895 VEC_quick_push (int, worklist
, 1);
898 for (i
= 1; i
< adef
; i
++)
900 b
= defs
[i
].bb_index
;
903 /* This is a closing element. Interval corresponding to the top
904 of the stack after removing it follows. */
905 VEC_pop (int, worklist
);
906 top
= VEC_index (int, worklist
, VEC_length (int, worklist
) - 1);
907 defs
[n_defs
].bb_index
= top
;
908 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
+ 1;
912 /* Opening element. Nothing to do, just push it to the stack and move
913 it to the correct position. */
914 defs
[n_defs
].bb_index
= defs
[i
].bb_index
;
915 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
;
916 VEC_quick_push (int, worklist
, b
);
920 /* If this interval starts at the same point as the previous one, cancel
922 if (defs
[n_defs
].dfs_num
== defs
[n_defs
- 1].dfs_num
)
923 defs
[n_defs
- 1].bb_index
= defs
[n_defs
].bb_index
;
927 VEC_pop (int, worklist
);
928 gcc_assert (VEC_empty (int, worklist
));
930 /* Now process the uses. */
931 live_phis
= BITMAP_ALLOC (NULL
);
932 EXECUTE_IF_SET_IN_BITMAP (uses
, 0, i
, bi
)
934 VEC_safe_push (int, heap
, worklist
, i
);
937 while (!VEC_empty (int, worklist
))
939 b
= VEC_pop (int, worklist
);
940 if (b
== ENTRY_BLOCK
)
943 /* If there is a phi node in USE_BB, it is made live. Otherwise,
944 find the def that dominates the immediate dominator of USE_BB
945 (the kill in USE_BB does not dominate the use). */
946 if (bitmap_bit_p (phis
, b
))
950 use_bb
= get_immediate_dominator (CDI_DOMINATORS
, BASIC_BLOCK (b
));
951 p
= find_dfsnum_interval (defs
, n_defs
,
952 bb_dom_dfs_in (CDI_DOMINATORS
, use_bb
));
953 if (!bitmap_bit_p (phis
, p
))
957 /* If the phi node is already live, there is nothing to do. */
958 if (bitmap_bit_p (live_phis
, p
))
961 /* Mark the phi as live, and add the new uses to the worklist. */
962 bitmap_set_bit (live_phis
, p
);
963 def_bb
= BASIC_BLOCK (p
);
964 FOR_EACH_EDGE (e
, ei
, def_bb
->preds
)
967 if (bitmap_bit_p (uses
, u
))
970 /* In case there is a kill directly in the use block, do not record
971 the use (this is also necessary for correctness, as we assume that
972 uses dominated by a def directly in their block have been filtered
974 if (bitmap_bit_p (kills
, u
))
977 bitmap_set_bit (uses
, u
);
978 VEC_safe_push (int, heap
, worklist
, u
);
982 VEC_free (int, heap
, worklist
);
983 bitmap_copy (phis
, live_phis
);
984 BITMAP_FREE (live_phis
);
988 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
989 return a bitmap with all the blocks in the iterated dominance
990 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
991 frontier information as returned by compute_dominance_frontiers.
993 The resulting set of blocks are the potential sites where PHI nodes
994 are needed. The caller is responsible for freeing the memory
995 allocated for the return value. */
998 compute_idf (bitmap def_blocks
, bitmap
*dfs
)
1001 unsigned bb_index
, i
;
1002 VEC(int,heap
) *work_stack
;
1003 bitmap phi_insertion_points
;
1005 work_stack
= VEC_alloc (int, heap
, n_basic_blocks
);
1006 phi_insertion_points
= BITMAP_ALLOC (NULL
);
1008 /* Seed the work list with all the blocks in DEF_BLOCKS. We use
1009 VEC_quick_push here for speed. This is safe because we know that
1010 the number of definition blocks is no greater than the number of
1011 basic blocks, which is the initial capacity of WORK_STACK. */
1012 EXECUTE_IF_SET_IN_BITMAP (def_blocks
, 0, bb_index
, bi
)
1013 VEC_quick_push (int, work_stack
, bb_index
);
1015 /* Pop a block off the worklist, add every block that appears in
1016 the original block's DF that we have not already processed to
1017 the worklist. Iterate until the worklist is empty. Blocks
1018 which are added to the worklist are potential sites for
1020 while (VEC_length (int, work_stack
) > 0)
1022 bb_index
= VEC_pop (int, work_stack
);
1024 /* Since the registration of NEW -> OLD name mappings is done
1025 separately from the call to update_ssa, when updating the SSA
1026 form, the basic blocks where new and/or old names are defined
1027 may have disappeared by CFG cleanup calls. In this case,
1028 we may pull a non-existing block from the work stack. */
1029 gcc_assert (bb_index
< (unsigned) last_basic_block
);
1031 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs
[bb_index
], phi_insertion_points
,
1034 /* Use a safe push because if there is a definition of VAR
1035 in every basic block, then WORK_STACK may eventually have
1036 more than N_BASIC_BLOCK entries. */
1037 VEC_safe_push (int, heap
, work_stack
, i
);
1038 bitmap_set_bit (phi_insertion_points
, i
);
1042 VEC_free (int, heap
, work_stack
);
1044 return phi_insertion_points
;
1048 /* Return the set of blocks where variable VAR is defined and the blocks
1049 where VAR is live on entry (livein). Return NULL, if no entry is
1050 found in DEF_BLOCKS. */
1052 static inline struct def_blocks_d
*
1053 find_def_blocks_for (tree var
)
1055 struct def_blocks_d dm
;
1057 return (struct def_blocks_d
*) htab_find (def_blocks
, &dm
);
1061 /* Retrieve or create a default definition for symbol SYM. */
1064 get_default_def_for (tree sym
)
1066 tree ddef
= gimple_default_def (cfun
, sym
);
1068 if (ddef
== NULL_TREE
)
1070 ddef
= make_ssa_name (sym
, build_empty_stmt ());
1071 set_default_def (sym
, ddef
);
1078 /* Marks phi node PHI in basic block BB for rewrite. */
1081 mark_phi_for_rewrite (basic_block bb
, tree phi
)
1084 unsigned i
, idx
= bb
->index
;
1086 if (REWRITE_THIS_STMT (phi
))
1089 REWRITE_THIS_STMT (phi
) = 1;
1091 if (!blocks_with_phis_to_rewrite
)
1094 bitmap_set_bit (blocks_with_phis_to_rewrite
, idx
);
1095 VEC_reserve (tree_vec
, heap
, phis_to_rewrite
, last_basic_block
+ 1);
1096 for (i
= VEC_length (tree_vec
, phis_to_rewrite
); i
<= idx
; i
++)
1097 VEC_quick_push (tree_vec
, phis_to_rewrite
, NULL
);
1099 phis
= VEC_index (tree_vec
, phis_to_rewrite
, idx
);
1101 phis
= VEC_alloc (tree
, heap
, 10);
1103 VEC_safe_push (tree
, heap
, phis
, phi
);
1104 VEC_replace (tree_vec
, phis_to_rewrite
, idx
, phis
);
1108 /* Insert PHI nodes for variable VAR using the iterated dominance
1109 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1110 function assumes that the caller is incrementally updating the
1111 existing SSA form, in which case VAR may be an SSA name instead of
1114 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1115 PHI node for VAR. On exit, only the nodes that received a PHI node
1116 for VAR will be present in PHI_INSERTION_POINTS. */
1119 insert_phi_nodes_for (tree var
, bitmap phi_insertion_points
, bool update_p
)
1126 struct def_blocks_d
*def_map
;
1128 def_map
= find_def_blocks_for (var
);
1129 gcc_assert (def_map
);
1131 /* Remove the blocks where we already have PHI nodes for VAR. */
1132 bitmap_and_compl_into (phi_insertion_points
, def_map
->phi_blocks
);
1134 /* Remove obviously useless phi nodes. */
1135 prune_unused_phi_nodes (phi_insertion_points
, def_map
->def_blocks
,
1136 def_map
->livein_blocks
);
1138 /* And insert the PHI nodes. */
1139 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points
, 0, bb_index
, bi
)
1141 bb
= BASIC_BLOCK (bb_index
);
1143 mark_block_for_update (bb
);
1147 if (TREE_CODE (var
) == SSA_NAME
)
1149 /* If we are rewriting SSA names, create the LHS of the PHI
1150 node by duplicating VAR. This is useful in the case of
1151 pointers, to also duplicate pointer attributes (alias
1152 information, in particular). */
1156 gcc_assert (update_p
);
1157 phi
= create_phi_node (var
, bb
);
1159 new_lhs
= duplicate_ssa_name (var
, phi
);
1160 SET_PHI_RESULT (phi
, new_lhs
);
1161 add_new_name_mapping (new_lhs
, var
);
1163 /* Add VAR to every argument slot of PHI. We need VAR in
1164 every argument so that rewrite_update_phi_arguments knows
1165 which name is this PHI node replacing. If VAR is a
1166 symbol marked for renaming, this is not necessary, the
1167 renamer will use the symbol on the LHS to get its
1168 reaching definition. */
1169 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1170 add_phi_arg (phi
, var
, e
);
1174 tree sym
= DECL_P (var
) ? var
: SSA_NAME_VAR (var
);
1175 phi
= create_phi_node (sym
, bb
);
1178 /* Mark this PHI node as interesting for update_ssa. */
1179 REGISTER_DEFS_IN_THIS_STMT (phi
) = 1;
1180 mark_phi_for_rewrite (bb
, phi
);
1185 /* Insert PHI nodes at the dominance frontier of blocks with variable
1186 definitions. DFS contains the dominance frontier information for
1190 insert_phi_nodes (bitmap
*dfs
)
1192 referenced_var_iterator rvi
;
1195 timevar_push (TV_TREE_INSERT_PHI_NODES
);
1197 FOR_EACH_REFERENCED_VAR (var
, rvi
)
1199 struct def_blocks_d
*def_map
;
1202 def_map
= find_def_blocks_for (var
);
1203 if (def_map
== NULL
)
1206 if (get_phi_state (var
) != NEED_PHI_STATE_NO
)
1208 idf
= compute_idf (def_map
->def_blocks
, dfs
);
1209 insert_phi_nodes_for (var
, idf
, false);
1214 timevar_pop (TV_TREE_INSERT_PHI_NODES
);
1218 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1219 register DEF (an SSA_NAME) to be a new definition for SYM. */
1222 register_new_def (tree def
, tree sym
)
1226 /* If this variable is set in a single basic block and all uses are
1227 dominated by the set(s) in that single basic block, then there is
1228 no reason to record anything for this variable in the block local
1229 definition stacks. Doing so just wastes time and memory.
1231 This is the same test to prune the set of variables which may
1232 need PHI nodes. So we just use that information since it's already
1233 computed and available for us to use. */
1234 if (get_phi_state (sym
) == NEED_PHI_STATE_NO
)
1236 set_current_def (sym
, def
);
1240 currdef
= get_current_def (sym
);
1242 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1243 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1244 in the stack so that we know which symbol is being defined by
1245 this SSA name when we unwind the stack. */
1246 if (currdef
&& !is_gimple_reg (sym
))
1247 VEC_safe_push (tree
, heap
, block_defs_stack
, sym
);
1249 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1250 stack is later used by the dominator tree callbacks to restore
1251 the reaching definitions for all the variables defined in the
1252 block after a recursive visit to all its immediately dominated
1253 blocks. If there is no current reaching definition, then just
1254 record the underlying _DECL node. */
1255 VEC_safe_push (tree
, heap
, block_defs_stack
, currdef
? currdef
: sym
);
1257 /* Set the current reaching definition for SYM to be DEF. */
1258 set_current_def (sym
, def
);
1262 /* Perform a depth-first traversal of the dominator tree looking for
1263 variables to rename. BB is the block where to start searching.
1264 Renaming is a five step process:
1266 1- Every definition made by PHI nodes at the start of the blocks is
1267 registered as the current definition for the corresponding variable.
1269 2- Every statement in BB is rewritten. USE and VUSE operands are
1270 rewritten with their corresponding reaching definition. DEF and
1271 VDEF targets are registered as new definitions.
1273 3- All the PHI nodes in successor blocks of BB are visited. The
1274 argument corresponding to BB is replaced with its current reaching
1277 4- Recursively rewrite every dominator child block of BB.
1279 5- Restore (in reverse order) the current reaching definition for every
1280 new definition introduced in this block. This is done so that when
1281 we return from the recursive call, all the current reaching
1282 definitions are restored to the names that were valid in the
1283 dominator parent of BB. */
1285 /* SSA Rewriting Step 1. Initialization, create a block local stack
1286 of reaching definitions for new SSA names produced in this block
1287 (BLOCK_DEFS). Register new definitions for every PHI node in the
1291 rewrite_initialize_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1296 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1297 fprintf (dump_file
, "\n\nRenaming block #%d\n\n", bb
->index
);
1299 /* Mark the unwind point for this block. */
1300 VEC_safe_push (tree
, heap
, block_defs_stack
, NULL_TREE
);
1302 /* Step 1. Register new definitions for every PHI node in the block.
1303 Conceptually, all the PHI nodes are executed in parallel and each PHI
1304 node introduces a new version for the associated variable. */
1305 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
1307 tree result
= PHI_RESULT (phi
);
1308 gcc_assert (is_gimple_reg (result
));
1309 register_new_def (result
, SSA_NAME_VAR (result
));
1314 /* Return the current definition for variable VAR. If none is found,
1315 create a new SSA name to act as the zeroth definition for VAR. */
1318 get_reaching_def (tree var
)
1322 /* Lookup the current reaching definition for VAR. */
1323 currdef
= get_current_def (var
);
1325 /* If there is no reaching definition for VAR, create and register a
1326 default definition for it (if needed). */
1327 if (currdef
== NULL_TREE
)
1329 tree sym
= DECL_P (var
) ? var
: SSA_NAME_VAR (var
);
1330 currdef
= get_default_def_for (sym
);
1331 set_current_def (var
, currdef
);
1334 /* Return the current reaching definition for VAR, or the default
1335 definition, if we had to create one. */
1340 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1341 the block with its immediate reaching definitions. Update the current
1342 definition of a variable when a new real or virtual definition is found. */
1345 rewrite_stmt (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1346 basic_block bb ATTRIBUTE_UNUSED
, block_stmt_iterator si
)
1349 use_operand_p use_p
;
1350 def_operand_p def_p
;
1353 stmt
= bsi_stmt (si
);
1355 /* If mark_def_sites decided that we don't need to rewrite this
1356 statement, ignore it. */
1357 gcc_assert (blocks_to_update
== NULL
);
1358 if (!REWRITE_THIS_STMT (stmt
) && !REGISTER_DEFS_IN_THIS_STMT (stmt
))
1361 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1363 fprintf (dump_file
, "Renaming statement ");
1364 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
1365 fprintf (dump_file
, "\n");
1368 /* Step 1. Rewrite USES in the statement. */
1369 if (REWRITE_THIS_STMT (stmt
))
1370 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
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 operands. */
1378 if (REGISTER_DEFS_IN_THIS_STMT (stmt
))
1379 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
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
), var
);
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. */
1395 rewrite_add_phi_arguments (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1401 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1405 for (phi
= phi_nodes (e
->dest
); phi
; phi
= PHI_CHAIN (phi
))
1408 currdef
= get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi
)));
1409 add_phi_arg (phi
, currdef
, e
);
1415 /* Called after visiting all the statements in basic block BB and all
1416 of its dominator children. Restore CURRDEFS to its original value. */
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
)
1431 if (TREE_CODE (tmp
) == SSA_NAME
)
1433 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1434 current definition of its underlying variable. Note that
1435 if the SSA_NAME is not for a GIMPLE register, the symbol
1436 being defined is stored in the next slot in the stack.
1437 This mechanism is needed because an SSA name for a
1438 non-register symbol may be the definition for more than
1439 one symbol (e.g., SFTs, aliased variables, etc). */
1441 var
= SSA_NAME_VAR (saved_def
);
1442 if (!is_gimple_reg (var
))
1443 var
= VEC_pop (tree
, block_defs_stack
);
1447 /* If we recorded anything else, it must have been a _DECL
1448 node and its current reaching definition must have been
1454 set_current_def (var
, saved_def
);
1459 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1462 dump_decl_set (FILE *file
, bitmap set
)
1469 fprintf (file
, "{ ");
1471 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
1473 print_generic_expr (file
, referenced_var (i
), 0);
1474 fprintf (file
, " ");
1477 fprintf (file
, "}\n");
1480 fprintf (file
, "NIL\n");
1484 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1487 debug_decl_set (bitmap set
)
1489 dump_decl_set (stderr
, set
);
1493 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1494 stack up to a maximum of N levels. If N is -1, the whole stack is
1495 dumped. New levels are created when the dominator tree traversal
1496 used for renaming enters a new sub-tree. */
1499 dump_defs_stack (FILE *file
, int n
)
1503 fprintf (file
, "\n\nRenaming stack");
1505 fprintf (file
, " (up to %d levels)", n
);
1506 fprintf (file
, "\n\n");
1509 fprintf (file
, "Level %d (current level)\n", i
);
1510 for (j
= (int) VEC_length (tree
, block_defs_stack
) - 1; j
>= 0; j
--)
1514 name
= VEC_index (tree
, block_defs_stack
, j
);
1515 if (name
== NULL_TREE
)
1520 fprintf (file
, "\nLevel %d\n", i
);
1531 var
= SSA_NAME_VAR (name
);
1532 if (!is_gimple_reg (var
))
1535 var
= VEC_index (tree
, block_defs_stack
, j
);
1539 fprintf (file
, " Previous CURRDEF (");
1540 print_generic_expr (file
, var
, 0);
1541 fprintf (file
, ") = ");
1543 print_generic_expr (file
, name
, 0);
1545 fprintf (file
, "<NIL>");
1546 fprintf (file
, "\n");
1551 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1552 stack up to a maximum of N levels. If N is -1, the whole stack is
1553 dumped. New levels are created when the dominator tree traversal
1554 used for renaming enters a new sub-tree. */
1557 debug_defs_stack (int n
)
1559 dump_defs_stack (stderr
, n
);
1563 /* Dump the current reaching definition of every symbol to FILE. */
1566 dump_currdefs (FILE *file
)
1568 referenced_var_iterator i
;
1571 fprintf (file
, "\n\nCurrent reaching definitions\n\n");
1572 FOR_EACH_REFERENCED_VAR (var
, i
)
1573 if (syms_to_rename
== NULL
|| bitmap_bit_p (syms_to_rename
, DECL_UID (var
)))
1575 fprintf (file
, "CURRDEF (");
1576 print_generic_expr (file
, var
, 0);
1577 fprintf (file
, ") = ");
1578 if (get_current_def (var
))
1579 print_generic_expr (file
, get_current_def (var
), 0);
1581 fprintf (file
, "<NIL>");
1582 fprintf (file
, "\n");
1587 /* Dump the current reaching definition of every symbol to stderr. */
1590 debug_currdefs (void)
1592 dump_currdefs (stderr
);
1596 /* Dump SSA information to FILE. */
1599 dump_tree_ssa (FILE *file
)
1601 const char *funcname
1602 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
1604 fprintf (file
, "SSA renaming information for %s\n\n", funcname
);
1606 dump_def_blocks (file
);
1607 dump_defs_stack (file
, -1);
1608 dump_currdefs (file
);
1609 dump_tree_ssa_stats (file
);
1613 /* Dump SSA information to stderr. */
1616 debug_tree_ssa (void)
1618 dump_tree_ssa (stderr
);
1622 /* Dump statistics for the hash table HTAB. */
1625 htab_statistics (FILE *file
, htab_t htab
)
1627 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
1628 (long) htab_size (htab
),
1629 (long) htab_elements (htab
),
1630 htab_collisions (htab
));
1634 /* Dump SSA statistics on FILE. */
1637 dump_tree_ssa_stats (FILE *file
)
1639 if (def_blocks
|| repl_tbl
)
1640 fprintf (file
, "\nHash table statistics:\n");
1644 fprintf (file
, " def_blocks: ");
1645 htab_statistics (file
, def_blocks
);
1650 fprintf (file
, " repl_tbl: ");
1651 htab_statistics (file
, repl_tbl
);
1654 if (def_blocks
|| repl_tbl
)
1655 fprintf (file
, "\n");
1659 /* Dump SSA statistics on stderr. */
1662 debug_tree_ssa_stats (void)
1664 dump_tree_ssa_stats (stderr
);
1668 /* Hashing and equality functions for DEF_BLOCKS. */
1671 def_blocks_hash (const void *p
)
1673 return htab_hash_pointer
1674 ((const void *)((const struct def_blocks_d
*)p
)->var
);
1678 def_blocks_eq (const void *p1
, const void *p2
)
1680 return ((const struct def_blocks_d
*)p1
)->var
1681 == ((const struct def_blocks_d
*)p2
)->var
;
1685 /* Free memory allocated by one entry in DEF_BLOCKS. */
1688 def_blocks_free (void *p
)
1690 struct def_blocks_d
*entry
= (struct def_blocks_d
*) p
;
1691 BITMAP_FREE (entry
->def_blocks
);
1692 BITMAP_FREE (entry
->phi_blocks
);
1693 BITMAP_FREE (entry
->livein_blocks
);
1698 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1701 debug_def_blocks_r (void **slot
, void *data
)
1703 FILE *file
= (FILE *) data
;
1704 struct def_blocks_d
*db_p
= (struct def_blocks_d
*) *slot
;
1706 fprintf (file
, "VAR: ");
1707 print_generic_expr (file
, db_p
->var
, dump_flags
);
1708 bitmap_print (file
, db_p
->def_blocks
, ", DEF_BLOCKS: { ", "}");
1709 bitmap_print (file
, db_p
->livein_blocks
, ", LIVEIN_BLOCKS: { ", "}");
1710 bitmap_print (file
, db_p
->phi_blocks
, ", PHI_BLOCKS: { ", "}\n");
1716 /* Dump the DEF_BLOCKS hash table on FILE. */
1719 dump_def_blocks (FILE *file
)
1721 fprintf (file
, "\n\nDefinition and live-in blocks:\n\n");
1723 htab_traverse (def_blocks
, debug_def_blocks_r
, file
);
1727 /* Dump the DEF_BLOCKS hash table on stderr. */
1730 debug_def_blocks (void)
1732 dump_def_blocks (stderr
);
1736 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1739 register_new_update_single (tree new_name
, tree old_name
)
1741 tree currdef
= get_current_def (old_name
);
1743 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1744 This stack is later used by the dominator tree callbacks to
1745 restore the reaching definitions for all the variables
1746 defined in the block after a recursive visit to all its
1747 immediately dominated blocks. */
1748 VEC_reserve (tree
, heap
, block_defs_stack
, 2);
1749 VEC_quick_push (tree
, block_defs_stack
, currdef
);
1750 VEC_quick_push (tree
, block_defs_stack
, old_name
);
1752 /* Set the current reaching definition for OLD_NAME to be
1754 set_current_def (old_name
, new_name
);
1758 /* Register NEW_NAME to be the new reaching definition for all the
1759 names in OLD_NAMES. Used by the incremental SSA update routines to
1760 replace old SSA names with new ones. */
1763 register_new_update_set (tree new_name
, bitmap old_names
)
1768 EXECUTE_IF_SET_IN_BITMAP (old_names
, 0, i
, bi
)
1769 register_new_update_single (new_name
, ssa_name (i
));
1773 /* Initialization of block data structures for the incremental SSA
1774 update pass. Create a block local stack of reaching definitions
1775 for new SSA names produced in this block (BLOCK_DEFS). Register
1776 new definitions for every PHI node in the block. */
1779 rewrite_update_init_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1785 bool is_abnormal_phi
;
1787 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1788 fprintf (dump_file
, "\n\nRegistering new PHI nodes in block #%d\n\n",
1791 /* Mark the unwind point for this block. */
1792 VEC_safe_push (tree
, heap
, block_defs_stack
, NULL_TREE
);
1794 if (!bitmap_bit_p (blocks_to_update
, bb
->index
))
1797 /* Mark the LHS if any of the arguments flows through an abnormal
1799 is_abnormal_phi
= false;
1800 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1801 if (e
->flags
& EDGE_ABNORMAL
)
1803 is_abnormal_phi
= true;
1807 /* If any of the PHI nodes is a replacement for a name in
1808 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
1809 register it as a new definition for its corresponding name. Also
1810 register definitions for names whose underlying symbols are
1811 marked for renaming. */
1812 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
1816 if (!REGISTER_DEFS_IN_THIS_STMT (phi
))
1819 lhs
= PHI_RESULT (phi
);
1820 lhs_sym
= SSA_NAME_VAR (lhs
);
1822 if (symbol_marked_for_renaming (lhs_sym
))
1823 register_new_update_single (lhs
, lhs_sym
);
1827 /* If LHS is a new name, register a new definition for all
1828 the names replaced by LHS. */
1829 if (is_new_name (lhs
))
1830 register_new_update_set (lhs
, names_replaced_by (lhs
));
1832 /* If LHS is an OLD name, register it as a new definition
1834 if (is_old_name (lhs
))
1835 register_new_update_single (lhs
, lhs
);
1838 if (is_abnormal_phi
)
1839 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
) = 1;
1844 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
1845 the current reaching definition of every name re-written in BB to
1846 the original reaching definition before visiting BB. This
1847 unwinding must be done in the opposite order to what is done in
1848 register_new_update_set. */
1851 rewrite_update_fini_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1852 basic_block bb ATTRIBUTE_UNUSED
)
1854 while (VEC_length (tree
, block_defs_stack
) > 0)
1856 tree var
= VEC_pop (tree
, block_defs_stack
);
1859 /* NULL indicates the unwind stop point for this block (see
1860 rewrite_update_init_block). */
1864 saved_def
= VEC_pop (tree
, block_defs_stack
);
1865 set_current_def (var
, saved_def
);
1870 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1871 it is a symbol marked for renaming, replace it with USE_P's current
1872 reaching definition. */
1875 maybe_replace_use (use_operand_p use_p
)
1877 tree rdef
= NULL_TREE
;
1878 tree use
= USE_FROM_PTR (use_p
);
1879 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1881 if (symbol_marked_for_renaming (sym
))
1882 rdef
= get_reaching_def (sym
);
1883 else if (is_old_name (use
))
1884 rdef
= get_reaching_def (use
);
1886 if (rdef
&& rdef
!= use
)
1887 SET_USE (use_p
, rdef
);
1891 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1892 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1893 register it as the current definition for the names replaced by
1897 maybe_register_def (def_operand_p def_p
, tree stmt
)
1899 tree def
= DEF_FROM_PTR (def_p
);
1900 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
1902 /* If DEF is a naked symbol that needs renaming, create a new
1904 if (symbol_marked_for_renaming (sym
))
1908 def
= make_ssa_name (def
, stmt
);
1909 SET_DEF (def_p
, def
);
1912 register_new_update_single (def
, sym
);
1916 /* If DEF is a new name, register it as a new definition
1917 for all the names replaced by DEF. */
1918 if (is_new_name (def
))
1919 register_new_update_set (def
, names_replaced_by (def
));
1921 /* If DEF is an old name, register DEF as a new
1922 definition for itself. */
1923 if (is_old_name (def
))
1924 register_new_update_single (def
, def
);
1929 /* Update every variable used in the statement pointed-to by SI. The
1930 statement is assumed to be in SSA form already. Names in
1931 OLD_SSA_NAMES used by SI will be updated to their current reaching
1932 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1933 will be registered as a new definition for their corresponding name
1934 in OLD_SSA_NAMES. */
1937 rewrite_update_stmt (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1938 basic_block bb ATTRIBUTE_UNUSED
,
1939 block_stmt_iterator si
)
1943 use_operand_p use_p
;
1944 def_operand_p def_p
;
1947 stmt
= bsi_stmt (si
);
1948 ann
= stmt_ann (stmt
);
1950 gcc_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
1952 /* Only update marked statements. */
1953 if (!REWRITE_THIS_STMT (stmt
) && !REGISTER_DEFS_IN_THIS_STMT (stmt
))
1956 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1958 fprintf (dump_file
, "Updating SSA information for statement ");
1959 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
1960 fprintf (dump_file
, "\n");
1963 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1964 symbol is marked for renaming. */
1965 if (REWRITE_THIS_STMT (stmt
))
1967 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1968 maybe_replace_use (use_p
);
1970 if (need_to_update_vops_p
)
1971 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_VIRTUAL_USES
)
1972 maybe_replace_use (use_p
);
1975 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1976 Also register definitions for names whose underlying symbol is
1977 marked for renaming. */
1978 if (REGISTER_DEFS_IN_THIS_STMT (stmt
))
1980 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
1981 maybe_register_def (def_p
, stmt
);
1983 if (need_to_update_vops_p
)
1984 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_VIRTUAL_DEFS
)
1985 maybe_register_def (def_p
, stmt
);
1990 /* Visit all the successor blocks of BB looking for PHI nodes. For
1991 every PHI node found, check if any of its arguments is in
1992 OLD_SSA_NAMES. If so, and if the argument has a current reaching
1993 definition, replace it. */
1996 rewrite_update_phi_arguments (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
2003 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2008 if (!bitmap_bit_p (blocks_with_phis_to_rewrite
, e
->dest
->index
))
2011 phis
= VEC_index (tree_vec
, phis_to_rewrite
, e
->dest
->index
);
2012 for (i
= 0; VEC_iterate (tree
, phis
, i
, phi
); i
++)
2015 use_operand_p arg_p
;
2017 gcc_assert (REWRITE_THIS_STMT (phi
));
2019 arg_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
2020 arg
= USE_FROM_PTR (arg_p
);
2022 if (arg
&& !DECL_P (arg
) && TREE_CODE (arg
) != SSA_NAME
)
2025 lhs_sym
= SSA_NAME_VAR (PHI_RESULT (phi
));
2027 if (arg
== NULL_TREE
)
2029 /* When updating a PHI node for a recently introduced
2030 symbol we may find NULL arguments. That's why we
2031 take the symbol from the LHS of the PHI node. */
2032 SET_USE (arg_p
, get_reaching_def (lhs_sym
));
2036 tree sym
= DECL_P (arg
) ? arg
: SSA_NAME_VAR (arg
);
2038 if (symbol_marked_for_renaming (sym
))
2039 SET_USE (arg_p
, get_reaching_def (sym
));
2040 else if (is_old_name (arg
))
2041 SET_USE (arg_p
, get_reaching_def (arg
));
2044 if (e
->flags
& EDGE_ABNORMAL
)
2045 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p
)) = 1;
2051 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2054 ENTRY indicates the block where to start. Every block dominated by
2055 ENTRY will be rewritten.
2057 WHAT indicates what actions will be taken by the renamer (see enum
2060 BLOCKS are the set of interesting blocks for the dominator walker
2061 to process. If this set is NULL, then all the nodes dominated
2062 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2063 are not present in BLOCKS are ignored. */
2066 rewrite_blocks (basic_block entry
, enum rewrite_mode what
, sbitmap blocks
)
2068 struct dom_walk_data walk_data
;
2070 /* Rewrite all the basic blocks in the program. */
2071 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS
);
2073 /* Setup callbacks for the generic dominator tree walker. */
2074 memset (&walk_data
, 0, sizeof (walk_data
));
2076 walk_data
.dom_direction
= CDI_DOMINATORS
;
2077 walk_data
.interesting_blocks
= blocks
;
2079 if (what
== REWRITE_ALL
)
2080 walk_data
.before_dom_children_before_stmts
= rewrite_initialize_block
;
2082 walk_data
.before_dom_children_before_stmts
= rewrite_update_init_block
;
2084 if (what
== REWRITE_ALL
)
2085 walk_data
.before_dom_children_walk_stmts
= rewrite_stmt
;
2086 else if (what
== REWRITE_UPDATE
)
2087 walk_data
.before_dom_children_walk_stmts
= rewrite_update_stmt
;
2091 if (what
== REWRITE_ALL
)
2092 walk_data
.before_dom_children_after_stmts
= rewrite_add_phi_arguments
;
2093 else if (what
== REWRITE_UPDATE
)
2094 walk_data
.before_dom_children_after_stmts
= rewrite_update_phi_arguments
;
2098 if (what
== REWRITE_ALL
)
2099 walk_data
.after_dom_children_after_stmts
= rewrite_finalize_block
;
2100 else if (what
== REWRITE_UPDATE
)
2101 walk_data
.after_dom_children_after_stmts
= rewrite_update_fini_block
;
2105 block_defs_stack
= VEC_alloc (tree
, heap
, 10);
2107 /* Initialize the dominator walker. */
2108 init_walk_dominator_tree (&walk_data
);
2110 /* Recursively walk the dominator tree rewriting each statement in
2111 each basic block. */
2112 walk_dominator_tree (&walk_data
, entry
);
2114 /* Finalize the dominator walker. */
2115 fini_walk_dominator_tree (&walk_data
);
2117 /* Debugging dumps. */
2118 if (dump_file
&& (dump_flags
& TDF_STATS
))
2120 dump_dfa_stats (dump_file
);
2122 dump_tree_ssa_stats (dump_file
);
2125 VEC_free (tree
, heap
, block_defs_stack
);
2127 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS
);
2131 /* Block initialization routine for mark_def_sites. Clear the
2132 KILLS bitmap at the start of each block. */
2135 mark_def_sites_initialize_block (struct dom_walk_data
*walk_data
,
2136 basic_block bb ATTRIBUTE_UNUSED
)
2138 struct mark_def_sites_global_data
*gd
;
2139 gd
= (struct mark_def_sites_global_data
*) walk_data
->global_data
;
2140 bitmap_clear (gd
->kills
);
2144 /* Mark the definition site blocks for each variable, so that we know
2145 where the variable is actually live.
2147 INTERESTING_BLOCKS will be filled in with all the blocks that
2148 should be processed by the renamer. It is assumed to be
2149 initialized and zeroed by the caller. */
2152 mark_def_site_blocks (sbitmap interesting_blocks
)
2154 struct dom_walk_data walk_data
;
2155 struct mark_def_sites_global_data mark_def_sites_global_data
;
2157 /* Setup callbacks for the generic dominator tree walker to find and
2158 mark definition sites. */
2159 walk_data
.walk_stmts_backward
= false;
2160 walk_data
.dom_direction
= CDI_DOMINATORS
;
2161 walk_data
.initialize_block_local_data
= NULL
;
2162 walk_data
.before_dom_children_before_stmts
= mark_def_sites_initialize_block
;
2163 walk_data
.before_dom_children_walk_stmts
= mark_def_sites
;
2164 walk_data
.before_dom_children_after_stmts
= NULL
;
2165 walk_data
.after_dom_children_before_stmts
= NULL
;
2166 walk_data
.after_dom_children_walk_stmts
= NULL
;
2167 walk_data
.after_dom_children_after_stmts
= NULL
;
2168 walk_data
.interesting_blocks
= NULL
;
2170 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2171 large enough to accommodate all the variables referenced in the
2172 function, not just the ones we are renaming. */
2173 mark_def_sites_global_data
.kills
= BITMAP_ALLOC (NULL
);
2175 /* Create the set of interesting blocks that will be filled by
2177 mark_def_sites_global_data
.interesting_blocks
= interesting_blocks
;
2178 walk_data
.global_data
= &mark_def_sites_global_data
;
2180 /* We do not have any local data. */
2181 walk_data
.block_local_data_size
= 0;
2183 /* Initialize the dominator walker. */
2184 init_walk_dominator_tree (&walk_data
);
2186 /* Recursively walk the dominator tree. */
2187 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
2189 /* Finalize the dominator walker. */
2190 fini_walk_dominator_tree (&walk_data
);
2192 /* We no longer need this bitmap, clear and free it. */
2193 BITMAP_FREE (mark_def_sites_global_data
.kills
);
2197 /* Initialize internal data needed during renaming. */
2200 init_ssa_renamer (void)
2203 referenced_var_iterator rvi
;
2205 cfun
->gimple_df
->in_ssa_p
= false;
2207 /* Allocate memory for the DEF_BLOCKS hash table. */
2208 gcc_assert (def_blocks
== NULL
);
2209 def_blocks
= htab_create (num_referenced_vars
, def_blocks_hash
,
2210 def_blocks_eq
, def_blocks_free
);
2212 FOR_EACH_REFERENCED_VAR(var
, rvi
)
2213 set_current_def (var
, NULL_TREE
);
2217 /* Deallocate internal data structures used by the renamer. */
2220 fini_ssa_renamer (void)
2224 htab_delete (def_blocks
);
2228 cfun
->gimple_df
->in_ssa_p
= true;
2232 /* Main entry point into the SSA builder. The renaming process
2233 proceeds in four main phases:
2235 1- Compute dominance frontier and immediate dominators, needed to
2236 insert PHI nodes and rename the function in dominator tree
2239 2- Find and mark all the blocks that define variables
2240 (mark_def_site_blocks).
2242 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2244 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2246 Steps 3 and 4 are done using the dominator tree walker
2247 (walk_dominator_tree). */
2250 rewrite_into_ssa (void)
2254 sbitmap interesting_blocks
;
2256 timevar_push (TV_TREE_SSA_OTHER
);
2258 /* Initialize operand data structures. */
2259 init_ssa_operands ();
2261 /* Initialize internal data needed by the renamer. */
2262 init_ssa_renamer ();
2264 /* Initialize the set of interesting blocks. The callback
2265 mark_def_sites will add to this set those blocks that the renamer
2267 interesting_blocks
= sbitmap_alloc (last_basic_block
);
2268 sbitmap_zero (interesting_blocks
);
2270 /* Initialize dominance frontier. */
2271 dfs
= XNEWVEC (bitmap
, last_basic_block
);
2273 dfs
[bb
->index
] = BITMAP_ALLOC (NULL
);
2275 /* 1- Compute dominance frontiers. */
2276 calculate_dominance_info (CDI_DOMINATORS
);
2277 compute_dominance_frontiers (dfs
);
2279 /* 2- Find and mark definition sites. */
2280 mark_def_site_blocks (interesting_blocks
);
2282 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2283 insert_phi_nodes (dfs
);
2285 /* 4- Rename all the blocks. */
2286 rewrite_blocks (ENTRY_BLOCK_PTR
, REWRITE_ALL
, interesting_blocks
);
2288 /* Free allocated memory. */
2290 BITMAP_FREE (dfs
[bb
->index
]);
2292 sbitmap_free (interesting_blocks
);
2294 fini_ssa_renamer ();
2296 timevar_pop (TV_TREE_SSA_OTHER
);
2301 struct tree_opt_pass pass_build_ssa
=
2305 rewrite_into_ssa
, /* execute */
2308 0, /* static_pass_number */
2310 PROP_cfg
| PROP_referenced_vars
, /* properties_required */
2311 PROP_ssa
, /* properties_provided */
2312 0, /* properties_destroyed */
2313 0, /* todo_flags_start */
2316 | TODO_remove_unused_locals
, /* todo_flags_finish */
2321 /* Mark the definition of VAR at STMT and BB as interesting for the
2322 renamer. BLOCKS is the set of blocks that need updating. */
2325 mark_def_interesting (tree var
, tree stmt
, basic_block bb
, bool insert_phi_p
)
2327 gcc_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2328 REGISTER_DEFS_IN_THIS_STMT (stmt
) = 1;
2332 bool is_phi_p
= TREE_CODE (stmt
) == PHI_NODE
;
2334 set_def_block (var
, bb
, is_phi_p
);
2336 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2337 site for both itself and all the old names replaced by it. */
2338 if (TREE_CODE (var
) == SSA_NAME
&& is_new_name (var
))
2342 bitmap set
= names_replaced_by (var
);
2344 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
2345 set_def_block (ssa_name (i
), bb
, is_phi_p
);
2351 /* Mark the use of VAR at STMT and BB as interesting for the
2352 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2356 mark_use_interesting (tree var
, tree stmt
, basic_block bb
, bool insert_phi_p
)
2358 basic_block def_bb
= bb_for_stmt (stmt
);
2360 mark_block_for_update (def_bb
);
2361 mark_block_for_update (bb
);
2363 if (TREE_CODE (stmt
) == PHI_NODE
)
2364 mark_phi_for_rewrite (def_bb
, stmt
);
2366 REWRITE_THIS_STMT (stmt
) = 1;
2368 /* If VAR has not been defined in BB, then it is live-on-entry
2369 to BB. Note that we cannot just use the block holding VAR's
2370 definition because if VAR is one of the names in OLD_SSA_NAMES,
2371 it will have several definitions (itself and all the names that
2375 struct def_blocks_d
*db_p
= get_def_blocks_for (var
);
2376 if (!bitmap_bit_p (db_p
->def_blocks
, bb
->index
))
2377 set_livein_block (var
, bb
);
2382 /* Do a dominator walk starting at BB processing statements that
2383 reference symbols in SYMS_TO_RENAME. This is very similar to
2384 mark_def_sites, but the scan handles statements whose operands may
2385 already be SSA names.
2387 If INSERT_PHI_P is true, mark those uses as live in the
2388 corresponding block. This is later used by the PHI placement
2389 algorithm to make PHI pruning decisions.
2391 FIXME. Most of this would be unnecessary if we could associate a
2392 symbol to all the SSA names that reference it. But that
2393 sounds like it would be expensive to maintain. Still, it
2394 would be interesting to see if it makes better sense to do
2398 prepare_block_for_update (basic_block bb
, bool insert_phi_p
)
2401 block_stmt_iterator si
;
2406 mark_block_for_update (bb
);
2408 /* Process PHI nodes marking interesting those that define or use
2409 the symbols that we are interested in. */
2410 for (phi
= phi_nodes (bb
); phi
; phi
= PHI_CHAIN (phi
))
2412 tree lhs_sym
, lhs
= PHI_RESULT (phi
);
2414 lhs_sym
= DECL_P (lhs
) ? lhs
: SSA_NAME_VAR (lhs
);
2416 if (!symbol_marked_for_renaming (lhs_sym
))
2418 mark_def_interesting (lhs_sym
, phi
, bb
, insert_phi_p
);
2420 /* Mark the uses in phi nodes as interesting. It would be more correct
2421 to process the arguments of the phi nodes of the successor edges of
2422 BB at the end of prepare_block_for_update, however, that turns out
2423 to be significantly more expensive. Doing it here is conservatively
2424 correct -- it may only cause us to believe a value to be live in a
2425 block that also contains its definition, and thus insert a few more
2426 phi nodes for it. */
2427 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2429 mark_use_interesting (lhs_sym
, phi
, e
->src
, insert_phi_p
);
2433 /* Process the statements. */
2434 for (si
= bsi_start (bb
); !bsi_end_p (si
); bsi_next (&si
))
2438 use_operand_p use_p
;
2439 def_operand_p def_p
;
2441 stmt
= bsi_stmt (si
);
2443 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, i
, SSA_OP_ALL_USES
)
2445 tree use
= USE_FROM_PTR (use_p
);
2446 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
2447 if (symbol_marked_for_renaming (sym
))
2448 mark_use_interesting (sym
, stmt
, bb
, insert_phi_p
);
2451 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, i
, SSA_OP_ALL_DEFS
)
2453 tree def
= DEF_FROM_PTR (def_p
);
2454 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
2455 if (symbol_marked_for_renaming (sym
))
2456 mark_def_interesting (sym
, stmt
, bb
, insert_phi_p
);
2460 /* Now visit all the blocks dominated by BB. */
2461 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
2463 son
= next_dom_son (CDI_DOMINATORS
, son
))
2464 prepare_block_for_update (son
, insert_phi_p
);
2468 /* Helper for prepare_names_to_update. Mark all the use sites for
2469 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2470 prepare_names_to_update. */
2473 prepare_use_sites_for (tree name
, bool insert_phi_p
)
2475 use_operand_p use_p
;
2476 imm_use_iterator iter
;
2478 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
2480 tree stmt
= USE_STMT (use_p
);
2481 basic_block bb
= bb_for_stmt (stmt
);
2483 if (TREE_CODE (stmt
) == PHI_NODE
)
2485 int ix
= PHI_ARG_INDEX_FROM_USE (use_p
);
2486 edge e
= PHI_ARG_EDGE (stmt
, ix
);
2487 mark_use_interesting (name
, stmt
, e
->src
, insert_phi_p
);
2491 /* For regular statements, mark this as an interesting use
2493 mark_use_interesting (name
, stmt
, bb
, insert_phi_p
);
2499 /* Helper for prepare_names_to_update. Mark the definition site for
2500 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2501 prepare_names_to_update. */
2504 prepare_def_site_for (tree name
, bool insert_phi_p
)
2509 gcc_assert (names_to_release
== NULL
2510 || !bitmap_bit_p (names_to_release
, SSA_NAME_VERSION (name
)));
2512 stmt
= SSA_NAME_DEF_STMT (name
);
2513 bb
= bb_for_stmt (stmt
);
2516 gcc_assert (bb
->index
< last_basic_block
);
2517 mark_block_for_update (bb
);
2518 mark_def_interesting (name
, stmt
, bb
, insert_phi_p
);
2523 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2524 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2525 PHI nodes for newly created names. */
2528 prepare_names_to_update (bool insert_phi_p
)
2532 sbitmap_iterator sbi
;
2534 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2535 remove it from NEW_SSA_NAMES so that we don't try to visit its
2536 defining basic block (which most likely doesn't exist). Notice
2537 that we cannot do the same with names in OLD_SSA_NAMES because we
2538 want to replace existing instances. */
2539 if (names_to_release
)
2540 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2541 RESET_BIT (new_ssa_names
, i
);
2543 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2544 names may be considered to be live-in on blocks that contain
2545 definitions for their replacements. */
2546 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
2547 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2549 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2550 OLD_SSA_NAMES, but we have to ignore its definition site. */
2551 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
2553 if (names_to_release
== NULL
|| !bitmap_bit_p (names_to_release
, i
))
2554 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2555 prepare_use_sites_for (ssa_name (i
), insert_phi_p
);
2560 /* Dump all the names replaced by NAME to FILE. */
2563 dump_names_replaced_by (FILE *file
, tree name
)
2569 print_generic_expr (file
, name
, 0);
2570 fprintf (file
, " -> { ");
2572 old_set
= names_replaced_by (name
);
2573 EXECUTE_IF_SET_IN_BITMAP (old_set
, 0, i
, bi
)
2575 print_generic_expr (file
, ssa_name (i
), 0);
2576 fprintf (file
, " ");
2579 fprintf (file
, "}\n");
2583 /* Dump all the names replaced by NAME to stderr. */
2586 debug_names_replaced_by (tree name
)
2588 dump_names_replaced_by (stderr
, name
);
2592 /* Dump SSA update information to FILE. */
2595 dump_update_ssa (FILE *file
)
2600 if (!need_ssa_update_p ())
2603 if (new_ssa_names
&& sbitmap_first_set_bit (new_ssa_names
) >= 0)
2605 sbitmap_iterator sbi
;
2607 fprintf (file
, "\nSSA replacement table\n");
2608 fprintf (file
, "N_i -> { O_1 ... O_j } means that N_i replaces "
2609 "O_1, ..., O_j\n\n");
2611 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
2612 dump_names_replaced_by (file
, ssa_name (i
));
2614 fprintf (file
, "\n");
2615 fprintf (file
, "Number of virtual NEW -> OLD mappings: %7u\n",
2616 update_ssa_stats
.num_virtual_mappings
);
2617 fprintf (file
, "Number of real NEW -> OLD mappings: %7u\n",
2618 update_ssa_stats
.num_total_mappings
2619 - update_ssa_stats
.num_virtual_mappings
);
2620 fprintf (file
, "Number of total NEW -> OLD mappings: %7u\n",
2621 update_ssa_stats
.num_total_mappings
);
2623 fprintf (file
, "\nNumber of virtual symbols: %u\n",
2624 update_ssa_stats
.num_virtual_symbols
);
2627 if (syms_to_rename
&& !bitmap_empty_p (syms_to_rename
))
2629 fprintf (file
, "\n\nSymbols to be put in SSA form\n\n");
2630 dump_decl_set (file
, syms_to_rename
);
2633 if (names_to_release
&& !bitmap_empty_p (names_to_release
))
2635 fprintf (file
, "\n\nSSA names to release after updating the SSA web\n\n");
2636 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2638 print_generic_expr (file
, ssa_name (i
), 0);
2639 fprintf (file
, " ");
2643 fprintf (file
, "\n\n");
2647 /* Dump SSA update information to stderr. */
2650 debug_update_ssa (void)
2652 dump_update_ssa (stderr
);
2656 /* Initialize data structures used for incremental SSA updates. */
2659 init_update_ssa (void)
2661 /* Reserve more space than the current number of names. The calls to
2662 add_new_name_mapping are typically done after creating new SSA
2663 names, so we'll need to reallocate these arrays. */
2664 old_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2665 sbitmap_zero (old_ssa_names
);
2667 new_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2668 sbitmap_zero (new_ssa_names
);
2670 repl_tbl
= htab_create (20, repl_map_hash
, repl_map_eq
, repl_map_free
);
2671 need_to_initialize_update_ssa_p
= false;
2672 need_to_update_vops_p
= false;
2673 syms_to_rename
= BITMAP_ALLOC (NULL
);
2674 regs_to_rename
= BITMAP_ALLOC (NULL
);
2675 mem_syms_to_rename
= BITMAP_ALLOC (NULL
);
2676 names_to_release
= NULL
;
2677 memset (&update_ssa_stats
, 0, sizeof (update_ssa_stats
));
2678 update_ssa_stats
.virtual_symbols
= BITMAP_ALLOC (NULL
);
2682 /* Deallocate data structures used for incremental SSA updates. */
2685 delete_update_ssa (void)
2690 sbitmap_free (old_ssa_names
);
2691 old_ssa_names
= NULL
;
2693 sbitmap_free (new_ssa_names
);
2694 new_ssa_names
= NULL
;
2696 htab_delete (repl_tbl
);
2699 need_to_initialize_update_ssa_p
= true;
2700 need_to_update_vops_p
= false;
2701 BITMAP_FREE (syms_to_rename
);
2702 BITMAP_FREE (regs_to_rename
);
2703 BITMAP_FREE (mem_syms_to_rename
);
2704 BITMAP_FREE (update_ssa_stats
.virtual_symbols
);
2706 if (names_to_release
)
2708 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2709 release_ssa_name (ssa_name (i
));
2710 BITMAP_FREE (names_to_release
);
2713 clear_ssa_name_info ();
2715 fini_ssa_renamer ();
2717 if (blocks_with_phis_to_rewrite
)
2718 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite
, 0, i
, bi
)
2720 tree_vec phis
= VEC_index (tree_vec
, phis_to_rewrite
, i
);
2722 VEC_free (tree
, heap
, phis
);
2723 VEC_replace (tree_vec
, phis_to_rewrite
, i
, NULL
);
2726 BITMAP_FREE (blocks_with_phis_to_rewrite
);
2727 BITMAP_FREE (blocks_to_update
);
2731 /* Create a new name for OLD_NAME in statement STMT and replace the
2732 operand pointed to by DEF_P with the newly created name. Return
2733 the new name and register the replacement mapping <NEW, OLD> in
2734 update_ssa's tables. */
2737 create_new_def_for (tree old_name
, tree stmt
, def_operand_p def
)
2739 tree new_name
= duplicate_ssa_name (old_name
, stmt
);
2741 SET_DEF (def
, new_name
);
2743 if (TREE_CODE (stmt
) == PHI_NODE
)
2747 basic_block bb
= bb_for_stmt (stmt
);
2749 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2750 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2751 if (e
->flags
& EDGE_ABNORMAL
)
2753 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name
) = 1;
2758 register_new_name_mapping (new_name
, old_name
);
2760 /* For the benefit of passes that will be updating the SSA form on
2761 their own, set the current reaching definition of OLD_NAME to be
2763 set_current_def (old_name
, new_name
);
2769 /* Register name NEW to be a replacement for name OLD. This function
2770 must be called for every replacement that should be performed by
2774 register_new_name_mapping (tree
new, tree old
)
2776 if (need_to_initialize_update_ssa_p
)
2779 add_new_name_mapping (new, old
);
2783 /* Register symbol SYM to be renamed by update_ssa. */
2786 mark_sym_for_renaming (tree sym
)
2788 if (need_to_initialize_update_ssa_p
)
2791 /* FIXME. Why do we need this? */
2794 if (var_can_have_subvars (sym
) && (svars
= get_subvars_for_var (sym
)))
2799 for (i
= 0; VEC_iterate (tree
, svars
, i
, subvar
); ++i
)
2800 mark_sym_for_renaming (subvar
);
2804 bitmap_set_bit (syms_to_rename
, DECL_UID (sym
));
2806 if (!is_gimple_reg (sym
))
2808 need_to_update_vops_p
= true;
2809 if (memory_partition (sym
))
2810 bitmap_set_bit (syms_to_rename
, DECL_UID (memory_partition (sym
)));
2815 /* Register all the symbols in SET to be renamed by update_ssa. */
2818 mark_set_for_renaming (bitmap set
)
2823 if (set
== NULL
|| bitmap_empty_p (set
))
2826 if (need_to_initialize_update_ssa_p
)
2829 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
2830 mark_sym_for_renaming (referenced_var (i
));
2834 /* Return true if there is any work to be done by update_ssa. */
2837 need_ssa_update_p (void)
2839 return syms_to_rename
|| old_ssa_names
|| new_ssa_names
;
2842 /* Return true if SSA name mappings have been registered for SSA updating. */
2845 name_mappings_registered_p (void)
2847 return repl_tbl
&& htab_elements (repl_tbl
) > 0;
2850 /* Return true if name N has been registered in the replacement table. */
2853 name_registered_for_update_p (tree n
)
2855 if (!need_ssa_update_p ())
2858 return is_new_name (n
)
2860 || symbol_marked_for_renaming (SSA_NAME_VAR (n
));
2864 /* Return the set of all the SSA names marked to be replaced. */
2867 ssa_names_to_replace (void)
2871 sbitmap_iterator sbi
;
2873 ret
= BITMAP_ALLOC (NULL
);
2874 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
2875 bitmap_set_bit (ret
, i
);
2881 /* Mark NAME to be released after update_ssa has finished. */
2884 release_ssa_name_after_update_ssa (tree name
)
2886 gcc_assert (!need_to_initialize_update_ssa_p
);
2888 if (names_to_release
== NULL
)
2889 names_to_release
= BITMAP_ALLOC (NULL
);
2891 bitmap_set_bit (names_to_release
, SSA_NAME_VERSION (name
));
2895 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2896 frontier information. BLOCKS is the set of blocks to be updated.
2898 This is slightly different than the regular PHI insertion
2899 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
2900 real names (i.e., GIMPLE registers) are inserted:
2902 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2903 nodes inside the region affected by the block that defines VAR
2904 and the blocks that define all its replacements. All these
2905 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2907 First, we compute the entry point to the region (ENTRY). This is
2908 given by the nearest common dominator to all the definition
2909 blocks. When computing the iterated dominance frontier (IDF), any
2910 block not strictly dominated by ENTRY is ignored.
2912 We then call the standard PHI insertion algorithm with the pruned
2915 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
2916 names is not pruned. PHI nodes are inserted at every IDF block. */
2919 insert_updated_phi_nodes_for (tree var
, bitmap
*dfs
, bitmap blocks
,
2920 unsigned update_flags
)
2923 struct def_blocks_d
*db
;
2924 bitmap idf
, pruned_idf
;
2928 #if defined ENABLE_CHECKING
2929 if (TREE_CODE (var
) == SSA_NAME
)
2930 gcc_assert (is_old_name (var
));
2932 gcc_assert (symbol_marked_for_renaming (var
));
2935 /* Get all the definition sites for VAR. */
2936 db
= find_def_blocks_for (var
);
2938 /* No need to do anything if there were no definitions to VAR. */
2939 if (db
== NULL
|| bitmap_empty_p (db
->def_blocks
))
2942 /* Compute the initial iterated dominance frontier. */
2943 idf
= compute_idf (db
->def_blocks
, dfs
);
2944 pruned_idf
= BITMAP_ALLOC (NULL
);
2946 if (TREE_CODE (var
) == SSA_NAME
)
2948 if (update_flags
== TODO_update_ssa
)
2950 /* If doing regular SSA updates for GIMPLE registers, we are
2951 only interested in IDF blocks dominated by the nearest
2952 common dominator of all the definition blocks. */
2953 entry
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
2955 if (entry
!= ENTRY_BLOCK_PTR
)
2956 EXECUTE_IF_SET_IN_BITMAP (idf
, 0, i
, bi
)
2957 if (BASIC_BLOCK (i
) != entry
2958 && dominated_by_p (CDI_DOMINATORS
, BASIC_BLOCK (i
), entry
))
2959 bitmap_set_bit (pruned_idf
, i
);
2963 /* Otherwise, do not prune the IDF for VAR. */
2964 gcc_assert (update_flags
== TODO_update_ssa_full_phi
);
2965 bitmap_copy (pruned_idf
, idf
);
2970 /* Otherwise, VAR is a symbol that needs to be put into SSA form
2971 for the first time, so we need to compute the full IDF for
2973 bitmap_copy (pruned_idf
, idf
);
2976 if (!bitmap_empty_p (pruned_idf
))
2978 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
2979 are included in the region to be updated. The feeding blocks
2980 are important to guarantee that the PHI arguments are renamed
2983 /* FIXME, this is not needed if we are updating symbols. We are
2984 already starting at the ENTRY block anyway. */
2985 bitmap_ior_into (blocks
, pruned_idf
);
2986 EXECUTE_IF_SET_IN_BITMAP (pruned_idf
, 0, i
, bi
)
2990 basic_block bb
= BASIC_BLOCK (i
);
2992 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2993 if (e
->src
->index
>= 0)
2994 bitmap_set_bit (blocks
, e
->src
->index
);
2997 insert_phi_nodes_for (var
, pruned_idf
, true);
3000 BITMAP_FREE (pruned_idf
);
3005 /* Heuristic to determine whether SSA name mappings for virtual names
3006 should be discarded and their symbols rewritten from scratch. When
3007 there is a large number of mappings for virtual names, the
3008 insertion of PHI nodes for the old names in the mappings takes
3009 considerable more time than if we inserted PHI nodes for the
3012 Currently the heuristic takes these stats into account:
3014 - Number of mappings for virtual SSA names.
3015 - Number of distinct virtual symbols involved in those mappings.
3017 If the number of virtual mappings is much larger than the number of
3018 virtual symbols, then it will be faster to compute PHI insertion
3019 spots for the symbols. Even if this involves traversing the whole
3020 CFG, which is what happens when symbols are renamed from scratch. */
3023 switch_virtuals_to_full_rewrite_p (void)
3025 if (update_ssa_stats
.num_virtual_mappings
< (unsigned) MIN_VIRTUAL_MAPPINGS
)
3028 if (update_ssa_stats
.num_virtual_mappings
3029 > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
3030 * update_ssa_stats
.num_virtual_symbols
)
3037 /* Remove every virtual mapping and mark all the affected virtual
3038 symbols for renaming. */
3041 switch_virtuals_to_full_rewrite (void)
3044 sbitmap_iterator sbi
;
3048 fprintf (dump_file
, "\nEnabled virtual name mapping heuristic.\n");
3049 fprintf (dump_file
, "\tNumber of virtual mappings: %7u\n",
3050 update_ssa_stats
.num_virtual_mappings
);
3051 fprintf (dump_file
, "\tNumber of unique virtual symbols: %7u\n",
3052 update_ssa_stats
.num_virtual_symbols
);
3053 fprintf (dump_file
, "Updating FUD-chains from top of CFG will be "
3054 "faster than processing\nthe name mappings.\n\n");
3057 /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
3058 Note that it is not really necessary to remove the mappings from
3059 REPL_TBL, that would only waste time. */
3060 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
3061 if (!is_gimple_reg (ssa_name (i
)))
3062 RESET_BIT (new_ssa_names
, i
);
3064 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
3065 if (!is_gimple_reg (ssa_name (i
)))
3066 RESET_BIT (old_ssa_names
, i
);
3068 mark_set_for_renaming (update_ssa_stats
.virtual_symbols
);
3072 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3073 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3075 1- The names in OLD_SSA_NAMES dominated by the definitions of
3076 NEW_SSA_NAMES are all re-written to be reached by the
3077 appropriate definition from NEW_SSA_NAMES.
3079 2- If needed, new PHI nodes are added to the iterated dominance
3080 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3082 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3083 calling register_new_name_mapping for every pair of names that the
3084 caller wants to replace.
3086 The caller identifies the new names that have been inserted and the
3087 names that need to be replaced by calling register_new_name_mapping
3088 for every pair <NEW, OLD>. Note that the function assumes that the
3089 new names have already been inserted in the IL.
3091 For instance, given the following code:
3094 2 x_1 = PHI (0, x_5)
3105 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3108 2 x_1 = PHI (0, x_5)
3121 We want to replace all the uses of x_1 with the new definitions of
3122 x_10 and x_11. Note that the only uses that should be replaced are
3123 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3124 *not* be replaced (this is why we cannot just mark symbol 'x' for
3127 Additionally, we may need to insert a PHI node at line 11 because
3128 that is a merge point for x_10 and x_11. So the use of x_1 at line
3129 11 will be replaced with the new PHI node. The insertion of PHI
3130 nodes is optional. They are not strictly necessary to preserve the
3131 SSA form, and depending on what the caller inserted, they may not
3132 even be useful for the optimizers. UPDATE_FLAGS controls various
3133 aspects of how update_ssa operates, see the documentation for
3134 TODO_update_ssa*. */
3137 update_ssa (unsigned update_flags
)
3139 basic_block bb
, start_bb
;
3144 sbitmap_iterator sbi
;
3146 if (!need_ssa_update_p ())
3149 timevar_push (TV_TREE_SSA_INCREMENTAL
);
3151 blocks_with_phis_to_rewrite
= BITMAP_ALLOC (NULL
);
3152 if (!phis_to_rewrite
)
3153 phis_to_rewrite
= VEC_alloc (tree_vec
, heap
, last_basic_block
);
3154 blocks_to_update
= BITMAP_ALLOC (NULL
);
3156 /* Ensure that the dominance information is up-to-date. */
3157 calculate_dominance_info (CDI_DOMINATORS
);
3159 /* Only one update flag should be set. */
3160 gcc_assert (update_flags
== TODO_update_ssa
3161 || update_flags
== TODO_update_ssa_no_phi
3162 || update_flags
== TODO_update_ssa_full_phi
3163 || update_flags
== TODO_update_ssa_only_virtuals
);
3165 /* If we only need to update virtuals, remove all the mappings for
3166 real names before proceeding. The caller is responsible for
3167 having dealt with the name mappings before calling update_ssa. */
3168 if (update_flags
== TODO_update_ssa_only_virtuals
)
3170 sbitmap_zero (old_ssa_names
);
3171 sbitmap_zero (new_ssa_names
);
3172 htab_empty (repl_tbl
);
3175 insert_phi_p
= (update_flags
!= TODO_update_ssa_no_phi
);
3179 /* If the caller requested PHI nodes to be added, initialize
3180 live-in information data structures (DEF_BLOCKS). */
3182 /* For each SSA name N, the DEF_BLOCKS table describes where the
3183 name is defined, which blocks have PHI nodes for N, and which
3184 blocks have uses of N (i.e., N is live-on-entry in those
3186 def_blocks
= htab_create (num_ssa_names
, def_blocks_hash
,
3187 def_blocks_eq
, def_blocks_free
);
3194 /* Heuristic to avoid massive slow downs when the replacement
3195 mappings include lots of virtual names. */
3196 if (insert_phi_p
&& switch_virtuals_to_full_rewrite_p ())
3197 switch_virtuals_to_full_rewrite ();
3199 /* If there are symbols to rename, identify those symbols that are
3200 GIMPLE registers into the set REGS_TO_RENAME and those that are
3201 memory symbols into the set MEM_SYMS_TO_RENAME. */
3202 if (!bitmap_empty_p (syms_to_rename
))
3207 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename
, 0, i
, bi
)
3209 tree sym
= referenced_var (i
);
3210 if (is_gimple_reg (sym
))
3211 bitmap_set_bit (regs_to_rename
, i
);
3214 /* Memory partitioning information may have been
3215 computed after the symbol was marked for renaming,
3216 if SYM is inside a partition also mark the partition
3218 tree mpt
= memory_partition (sym
);
3220 bitmap_set_bit (syms_to_rename
, DECL_UID (mpt
));
3224 /* Memory symbols are those not in REGS_TO_RENAME. */
3225 bitmap_and_compl (mem_syms_to_rename
, syms_to_rename
, regs_to_rename
);
3228 /* If there are names defined in the replacement table, prepare
3229 definition and use sites for all the names in NEW_SSA_NAMES and
3231 if (sbitmap_first_set_bit (new_ssa_names
) >= 0)
3233 prepare_names_to_update (insert_phi_p
);
3235 /* If all the names in NEW_SSA_NAMES had been marked for
3236 removal, and there are no symbols to rename, then there's
3237 nothing else to do. */
3238 if (sbitmap_first_set_bit (new_ssa_names
) < 0
3239 && bitmap_empty_p (syms_to_rename
))
3243 /* Next, determine the block at which to start the renaming process. */
3244 if (!bitmap_empty_p (syms_to_rename
))
3246 /* If we have to rename some symbols from scratch, we need to
3247 start the process at the root of the CFG. FIXME, it should
3248 be possible to determine the nearest block that had a
3249 definition for each of the symbols that are marked for
3250 updating. For now this seems more work than it's worth. */
3251 start_bb
= ENTRY_BLOCK_PTR
;
3253 /* Traverse the CFG looking for existing definitions and uses of
3254 symbols in SYMS_TO_RENAME. Mark interesting blocks and
3255 statements and set local live-in information for the PHI
3256 placement heuristics. */
3257 prepare_block_for_update (start_bb
, insert_phi_p
);
3261 /* Otherwise, the entry block to the region is the nearest
3262 common dominator for the blocks in BLOCKS. */
3263 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3267 /* If requested, insert PHI nodes at the iterated dominance frontier
3268 of every block, creating new definitions for names in OLD_SSA_NAMES
3269 and for symbols in SYMS_TO_RENAME. */
3274 /* If the caller requested PHI nodes to be added, compute
3275 dominance frontiers. */
3276 dfs
= XNEWVEC (bitmap
, last_basic_block
);
3278 dfs
[bb
->index
] = BITMAP_ALLOC (NULL
);
3279 compute_dominance_frontiers (dfs
);
3281 if (sbitmap_first_set_bit (old_ssa_names
) >= 0)
3283 sbitmap_iterator sbi
;
3285 /* insert_update_phi_nodes_for will call add_new_name_mapping
3286 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3287 will grow while we are traversing it (but it will not
3288 gain any new members). Copy OLD_SSA_NAMES to a temporary
3290 sbitmap tmp
= sbitmap_alloc (old_ssa_names
->n_bits
);
3291 sbitmap_copy (tmp
, old_ssa_names
);
3292 EXECUTE_IF_SET_IN_SBITMAP (tmp
, 0, i
, sbi
)
3293 insert_updated_phi_nodes_for (ssa_name (i
), dfs
, blocks_to_update
,
3298 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename
, 0, i
, bi
)
3299 insert_updated_phi_nodes_for (referenced_var (i
), dfs
, blocks_to_update
,
3303 BITMAP_FREE (dfs
[bb
->index
]);
3306 /* Insertion of PHI nodes may have added blocks to the region.
3307 We need to re-compute START_BB to include the newly added
3309 if (start_bb
!= ENTRY_BLOCK_PTR
)
3310 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3314 /* Reset the current definition for name and symbol before renaming
3316 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
3317 set_current_def (ssa_name (i
), NULL_TREE
);
3319 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename
, 0, i
, bi
)
3320 set_current_def (referenced_var (i
), NULL_TREE
);
3322 /* Now start the renaming process at START_BB. */
3323 tmp
= sbitmap_alloc (last_basic_block
);
3325 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3328 rewrite_blocks (start_bb
, REWRITE_UPDATE
, tmp
);
3332 /* Debugging dumps. */
3338 dump_update_ssa (dump_file
);
3340 fprintf (dump_file
, "Incremental SSA update started at block: %d\n\n",
3344 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3346 fprintf (dump_file
, "Number of blocks in CFG: %d\n", last_basic_block
);
3347 fprintf (dump_file
, "Number of blocks to update: %d (%3.0f%%)\n\n",
3348 c
, PERCENT (c
, last_basic_block
));
3350 if (dump_flags
& TDF_DETAILS
)
3352 fprintf (dump_file
, "Affected blocks: ");
3353 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3354 fprintf (dump_file
, "%u ", i
);
3355 fprintf (dump_file
, "\n");
3358 fprintf (dump_file
, "\n\n");
3361 /* Free allocated memory. */
3363 delete_update_ssa ();
3365 timevar_pop (TV_TREE_SSA_INCREMENTAL
);