1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010, 2011
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"
29 #include "langhooks.h"
30 #include "basic-block.h"
32 #include "gimple-pretty-print.h"
34 #include "tree-flow.h"
36 #include "tree-inline.h"
38 #include "tree-pass.h"
45 /* This file builds the SSA form for a function as described in:
46 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
47 Computing Static Single Assignment Form and the Control Dependence
48 Graph. ACM Transactions on Programming Languages and Systems,
49 13(4):451-490, October 1991. */
51 /* Structure to map a variable VAR to the set of blocks that contain
52 definitions for VAR. */
58 /* Blocks that contain definitions of VAR. Bit I will be set if the
59 Ith block contains a definition of VAR. */
62 /* Blocks that contain a PHI node for VAR. */
65 /* Blocks where VAR is live-on-entry. Similar semantics as
71 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
72 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
73 basic blocks where VAR is defined (assigned a new value). It also
74 contains a bitmap of all the blocks where VAR is live-on-entry
75 (i.e., there is a use of VAR in block B without a preceding
76 definition in B). The live-on-entry information is used when
77 computing PHI pruning heuristics. */
78 static htab_t def_blocks
;
80 /* Stack of trees used to restore the global currdefs to its original
81 state after completing rewriting of a block and its dominator
82 children. Its elements have the following properties:
84 - An SSA_NAME (N) indicates that the current definition of the
85 underlying variable should be set to the given SSA_NAME. If the
86 symbol associated with the SSA_NAME is not a GIMPLE register, the
87 next slot in the stack must be a _DECL node (SYM). In this case,
88 the name N in the previous slot is the current reaching
91 - A _DECL node indicates that the underlying variable has no
94 - A NULL node at the top entry is used to mark the last slot
95 associated with the current block. */
96 static VEC(tree
,heap
) *block_defs_stack
;
99 /* Set of existing SSA names being replaced by update_ssa. */
100 static sbitmap old_ssa_names
;
102 /* Set of new SSA names being added by update_ssa. Note that both
103 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
104 the operations done on them are presence tests. */
105 static sbitmap new_ssa_names
;
107 sbitmap interesting_blocks
;
109 /* Set of SSA names that have been marked to be released after they
110 were registered in the replacement table. They will be finally
111 released after we finish updating the SSA web. */
112 static bitmap names_to_release
;
114 static VEC(gimple_vec
, heap
) *phis_to_rewrite
;
116 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
117 static bitmap blocks_with_phis_to_rewrite
;
119 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
120 to grow as the callers to register_new_name_mapping will typically
121 create new names on the fly. FIXME. Currently set to 1/3 to avoid
122 frequent reallocations but still need to find a reasonable growth
124 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
126 /* Tuple used to represent replacement mappings. */
134 /* NEW -> OLD_SET replacement table. If we are replacing several
135 existing SSA names O_1, O_2, ..., O_j with a new name N_i,
136 then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }. */
137 static htab_t repl_tbl
;
139 /* The function the SSA updating data structures have been initialized for.
140 NULL if they need to be initialized by register_new_name_mapping. */
141 static struct function
*update_ssa_initialized_fn
= NULL
;
143 /* Statistics kept by update_ssa to use in the virtual mapping
144 heuristic. If the number of virtual mappings is beyond certain
145 threshold, the updater will switch from using the mappings into
146 renaming the virtual symbols from scratch. In some cases, the
147 large number of name mappings for virtual names causes significant
148 slowdowns in the PHI insertion code. */
149 struct update_ssa_stats_d
151 unsigned num_virtual_mappings
;
152 unsigned num_total_mappings
;
153 bitmap virtual_symbols
;
154 unsigned num_virtual_symbols
;
156 static struct update_ssa_stats_d update_ssa_stats
;
158 /* Global data to attach to the main dominator walk structure. */
159 struct mark_def_sites_global_data
161 /* This bitmap contains the variables which are set before they
162 are used in a basic block. */
167 /* Information stored for SSA names. */
170 /* The current reaching definition replacing this SSA name. */
173 /* This field indicates whether or not the variable may need PHI nodes.
174 See the enum's definition for more detailed information about the
176 ENUM_BITFIELD (need_phi_state
) need_phi_state
: 2;
178 /* Age of this record (so that info_for_ssa_name table can be cleared
179 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
180 are assumed to be null. */
184 /* The information associated with names. */
185 typedef struct ssa_name_info
*ssa_name_info_p
;
186 DEF_VEC_P (ssa_name_info_p
);
187 DEF_VEC_ALLOC_P (ssa_name_info_p
, heap
);
189 static VEC(ssa_name_info_p
, heap
) *info_for_ssa_name
;
190 static unsigned current_info_for_ssa_name_age
;
192 /* The set of blocks affected by update_ssa. */
193 static bitmap blocks_to_update
;
195 /* The main entry point to the SSA renamer (rewrite_blocks) may be
196 called several times to do different, but related, tasks.
197 Initially, we need it to rename the whole program into SSA form.
198 At other times, we may need it to only rename into SSA newly
199 exposed symbols. Finally, we can also call it to incrementally fix
200 an already built SSA web. */
202 /* Convert the whole function into SSA form. */
205 /* Incrementally update the SSA web by replacing existing SSA
206 names with new ones. See update_ssa for details. */
213 /* Prototypes for debugging functions. */
214 extern void dump_tree_ssa (FILE *);
215 extern void debug_tree_ssa (void);
216 extern void debug_def_blocks (void);
217 extern void dump_tree_ssa_stats (FILE *);
218 extern void debug_tree_ssa_stats (void);
219 extern void dump_update_ssa (FILE *);
220 extern void debug_update_ssa (void);
221 extern void dump_names_replaced_by (FILE *, tree
);
222 extern void debug_names_replaced_by (tree
);
223 extern void dump_def_blocks (FILE *);
224 extern void debug_def_blocks (void);
225 extern void dump_defs_stack (FILE *, int);
226 extern void debug_defs_stack (int);
227 extern void dump_currdefs (FILE *);
228 extern void debug_currdefs (void);
230 /* Return true if STMT needs to be rewritten. When renaming a subset
231 of the variables, not all statements will be processed. This is
232 decided in mark_def_sites. */
235 rewrite_uses_p (gimple stmt
)
237 return gimple_visited_p (stmt
);
241 /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
244 set_rewrite_uses (gimple stmt
, bool rewrite_p
)
246 gimple_set_visited (stmt
, rewrite_p
);
250 /* Return true if the DEFs created by statement STMT should be
251 registered when marking new definition sites. This is slightly
252 different than rewrite_uses_p: it's used by update_ssa to
253 distinguish statements that need to have both uses and defs
254 processed from those that only need to have their defs processed.
255 Statements that define new SSA names only need to have their defs
256 registered, but they don't need to have their uses renamed. */
259 register_defs_p (gimple stmt
)
261 return gimple_plf (stmt
, GF_PLF_1
) != 0;
265 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
268 set_register_defs (gimple stmt
, bool register_defs_p
)
270 gimple_set_plf (stmt
, GF_PLF_1
, register_defs_p
);
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
= NEED_PHI_STATE_UNKNOWN
;
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 blocks 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 ATTRIBUTE_UNUSED
, bitmap def_blocks ATTRIBUTE_UNUSED
)
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 gimple_stmt_iterator gsi
;
425 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
427 gimple phi
= gsi_stmt (gsi
);
428 set_rewrite_uses (phi
, false);
429 set_register_defs (phi
, false);
432 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
434 stmt
= gsi_stmt (gsi
);
436 /* We are going to use the operand cache API, such as
437 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
438 cache for each statement should be up-to-date. */
439 gcc_assert (!gimple_modified_p (stmt
));
440 set_rewrite_uses (stmt
, false);
441 set_register_defs (stmt
, false);
445 /* Mark block BB as interesting for update_ssa. */
448 mark_block_for_update (basic_block bb
)
450 gcc_assert (blocks_to_update
!= NULL
);
451 if (!bitmap_set_bit (blocks_to_update
, bb
->index
))
453 initialize_flags_in_bb (bb
);
456 /* Return the set of blocks where variable VAR is defined and the blocks
457 where VAR is live on entry (livein). If no entry is found in
458 DEF_BLOCKS, a new one is created and returned. */
460 static inline struct def_blocks_d
*
461 get_def_blocks_for (tree var
)
463 struct def_blocks_d db
, *db_p
;
467 slot
= htab_find_slot (def_blocks
, (void *) &db
, INSERT
);
470 db_p
= XNEW (struct def_blocks_d
);
472 db_p
->def_blocks
= BITMAP_ALLOC (NULL
);
473 db_p
->phi_blocks
= BITMAP_ALLOC (NULL
);
474 db_p
->livein_blocks
= BITMAP_ALLOC (NULL
);
475 *slot
= (void *) db_p
;
478 db_p
= (struct def_blocks_d
*) *slot
;
484 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
485 VAR is defined by a PHI node. */
488 set_def_block (tree var
, basic_block bb
, bool phi_p
)
490 struct def_blocks_d
*db_p
;
491 enum need_phi_state state
;
493 state
= get_phi_state (var
);
494 db_p
= get_def_blocks_for (var
);
496 /* Set the bit corresponding to the block where VAR is defined. */
497 bitmap_set_bit (db_p
->def_blocks
, bb
->index
);
499 bitmap_set_bit (db_p
->phi_blocks
, bb
->index
);
501 /* Keep track of whether or not we may need to insert PHI nodes.
503 If we are in the UNKNOWN state, then this is the first definition
504 of VAR. Additionally, we have not seen any uses of VAR yet, so
505 we do not need a PHI node for this variable at this time (i.e.,
506 transition to NEED_PHI_STATE_NO).
508 If we are in any other state, then we either have multiple definitions
509 of this variable occurring in different blocks or we saw a use of the
510 variable which was not dominated by the block containing the
511 definition(s). In this case we may need a PHI node, so enter
512 state NEED_PHI_STATE_MAYBE. */
513 if (state
== NEED_PHI_STATE_UNKNOWN
)
514 set_phi_state (var
, NEED_PHI_STATE_NO
);
516 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
520 /* Mark block BB as having VAR live at the entry to BB. */
523 set_livein_block (tree var
, basic_block bb
)
525 struct def_blocks_d
*db_p
;
526 enum need_phi_state state
= get_phi_state (var
);
528 db_p
= get_def_blocks_for (var
);
530 /* Set the bit corresponding to the block where VAR is live in. */
531 bitmap_set_bit (db_p
->livein_blocks
, bb
->index
);
533 /* Keep track of whether or not we may need to insert PHI nodes.
535 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
536 by the single block containing the definition(s) of this variable. If
537 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
538 NEED_PHI_STATE_MAYBE. */
539 if (state
== NEED_PHI_STATE_NO
)
541 int def_block_index
= bitmap_first_set_bit (db_p
->def_blocks
);
543 if (def_block_index
== -1
544 || ! dominated_by_p (CDI_DOMINATORS
, bb
,
545 BASIC_BLOCK (def_block_index
)))
546 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
549 set_phi_state (var
, NEED_PHI_STATE_MAYBE
);
553 /* Return true if symbol SYM is marked for renaming. */
556 symbol_marked_for_renaming (tree sym
)
558 return bitmap_bit_p (SYMS_TO_RENAME (cfun
), DECL_UID (sym
));
562 /* Return true if NAME is in OLD_SSA_NAMES. */
565 is_old_name (tree name
)
567 unsigned ver
= SSA_NAME_VERSION (name
);
570 return ver
< new_ssa_names
->n_bits
&& TEST_BIT (old_ssa_names
, ver
);
574 /* Return true if NAME is in NEW_SSA_NAMES. */
577 is_new_name (tree name
)
579 unsigned ver
= SSA_NAME_VERSION (name
);
582 return ver
< new_ssa_names
->n_bits
&& TEST_BIT (new_ssa_names
, ver
);
586 /* Hashing and equality functions for REPL_TBL. */
589 repl_map_hash (const void *p
)
591 return htab_hash_pointer ((const void *)((const struct repl_map_d
*)p
)->name
);
595 repl_map_eq (const void *p1
, const void *p2
)
597 return ((const struct repl_map_d
*)p1
)->name
598 == ((const struct repl_map_d
*)p2
)->name
;
602 repl_map_free (void *p
)
604 BITMAP_FREE (((struct repl_map_d
*)p
)->set
);
609 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
612 names_replaced_by (tree new_tree
)
618 slot
= htab_find_slot (repl_tbl
, (void *) &m
, NO_INSERT
);
620 /* If N was not registered in the replacement table, return NULL. */
621 if (slot
== NULL
|| *slot
== NULL
)
624 return ((struct repl_map_d
*) *slot
)->set
;
628 /* Add OLD to REPL_TBL[NEW_TREE].SET. */
631 add_to_repl_tbl (tree new_tree
, tree old
)
633 struct repl_map_d m
, *mp
;
637 slot
= htab_find_slot (repl_tbl
, (void *) &m
, INSERT
);
640 mp
= XNEW (struct repl_map_d
);
642 mp
->set
= BITMAP_ALLOC (NULL
);
646 mp
= (struct repl_map_d
*) *slot
;
648 bitmap_set_bit (mp
->set
, SSA_NAME_VERSION (old
));
652 /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
653 represents the set of names O_1 ... O_j replaced by N_i. This is
654 used by update_ssa and its helpers to introduce new SSA names in an
655 already formed SSA web. */
658 add_new_name_mapping (tree new_tree
, tree old
)
660 timevar_push (TV_TREE_SSA_INCREMENTAL
);
662 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
663 gcc_assert (new_tree
!= old
&& SSA_NAME_VAR (new_tree
) == SSA_NAME_VAR (old
));
665 /* If this mapping is for virtual names, we will need to update
666 virtual operands. If this is a mapping for .MEM, then we gather
667 the symbols associated with each name. */
668 if (!is_gimple_reg (new_tree
))
672 update_ssa_stats
.num_virtual_mappings
++;
673 update_ssa_stats
.num_virtual_symbols
++;
675 /* Keep counts of virtual mappings and symbols to use in the
676 virtual mapping heuristic. If we have large numbers of
677 virtual mappings for a relatively low number of symbols, it
678 will make more sense to rename the symbols from scratch.
679 Otherwise, the insertion of PHI nodes for each of the old
680 names in these mappings will be very slow. */
681 sym
= SSA_NAME_VAR (new_tree
);
682 bitmap_set_bit (update_ssa_stats
.virtual_symbols
, DECL_UID (sym
));
685 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
686 caller may have created new names since the set was created. */
687 if (new_ssa_names
->n_bits
<= num_ssa_names
- 1)
689 unsigned int new_sz
= num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
;
690 new_ssa_names
= sbitmap_resize (new_ssa_names
, new_sz
, 0);
691 old_ssa_names
= sbitmap_resize (old_ssa_names
, new_sz
, 0);
694 /* Update the REPL_TBL table. */
695 add_to_repl_tbl (new_tree
, old
);
697 /* If OLD had already been registered as a new name, then all the
698 names that OLD replaces should also be replaced by NEW_TREE. */
699 if (is_new_name (old
))
700 bitmap_ior_into (names_replaced_by (new_tree
), names_replaced_by (old
));
702 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
704 SET_BIT (new_ssa_names
, SSA_NAME_VERSION (new_tree
));
705 SET_BIT (old_ssa_names
, SSA_NAME_VERSION (old
));
707 /* Update mapping counter to use in the virtual mapping heuristic. */
708 update_ssa_stats
.num_total_mappings
++;
710 timevar_pop (TV_TREE_SSA_INCREMENTAL
);
714 /* Call back for walk_dominator_tree used to collect definition sites
715 for every variable in the function. For every statement S in block
718 1- Variables defined by S in the DEFS of S are marked in the bitmap
721 2- If S uses a variable VAR and there is no preceding kill of VAR,
722 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
724 This information is used to determine which variables are live
725 across block boundaries to reduce the number of PHI nodes
729 mark_def_sites (basic_block bb
, gimple stmt
, bitmap kills
)
735 /* Since this is the first time that we rewrite the program into SSA
736 form, force an operand scan on every statement. */
739 gcc_assert (blocks_to_update
== NULL
);
740 set_register_defs (stmt
, false);
741 set_rewrite_uses (stmt
, false);
743 if (is_gimple_debug (stmt
))
745 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
747 tree sym
= USE_FROM_PTR (use_p
);
748 gcc_assert (DECL_P (sym
));
749 set_rewrite_uses (stmt
, true);
751 if (rewrite_uses_p (stmt
))
752 SET_BIT (interesting_blocks
, bb
->index
);
756 /* If a variable is used before being set, then the variable is live
757 across a block boundary, so mark it live-on-entry to BB. */
758 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
760 tree sym
= USE_FROM_PTR (use_p
);
761 gcc_assert (DECL_P (sym
));
762 if (!bitmap_bit_p (kills
, DECL_UID (sym
)))
763 set_livein_block (sym
, bb
);
764 set_rewrite_uses (stmt
, true);
767 /* Now process the defs. Mark BB as the definition block and add
768 each def to the set of killed symbols. */
769 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_DEF
)
771 gcc_assert (DECL_P (def
));
772 set_def_block (def
, bb
, false);
773 bitmap_set_bit (kills
, DECL_UID (def
));
774 set_register_defs (stmt
, true);
777 /* If we found the statement interesting then also mark the block BB
779 if (rewrite_uses_p (stmt
) || register_defs_p (stmt
))
780 SET_BIT (interesting_blocks
, bb
->index
);
783 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
784 in the dfs numbering of the dominance tree. */
788 /* Basic block whose index this entry corresponds to. */
791 /* The dfs number of this node. */
795 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
799 cmp_dfsnum (const void *a
, const void *b
)
801 const struct dom_dfsnum
*const da
= (const struct dom_dfsnum
*) a
;
802 const struct dom_dfsnum
*const db
= (const struct dom_dfsnum
*) b
;
804 return (int) da
->dfs_num
- (int) db
->dfs_num
;
807 /* Among the intervals starting at the N points specified in DEFS, find
808 the one that contains S, and return its bb_index. */
811 find_dfsnum_interval (struct dom_dfsnum
*defs
, unsigned n
, unsigned s
)
813 unsigned f
= 0, t
= n
, m
;
818 if (defs
[m
].dfs_num
<= s
)
824 return defs
[f
].bb_index
;
827 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
828 KILLS is a bitmap of blocks where the value is defined before any use. */
831 prune_unused_phi_nodes (bitmap phis
, bitmap kills
, bitmap uses
)
833 VEC(int, heap
) *worklist
;
835 unsigned i
, b
, p
, u
, top
;
837 basic_block def_bb
, use_bb
;
841 struct dom_dfsnum
*defs
;
842 unsigned n_defs
, adef
;
844 if (bitmap_empty_p (uses
))
850 /* The phi must dominate a use, or an argument of a live phi. Also, we
851 do not create any phi nodes in def blocks, unless they are also livein. */
852 to_remove
= BITMAP_ALLOC (NULL
);
853 bitmap_and_compl (to_remove
, kills
, uses
);
854 bitmap_and_compl_into (phis
, to_remove
);
855 if (bitmap_empty_p (phis
))
857 BITMAP_FREE (to_remove
);
861 /* We want to remove the unnecessary phi nodes, but we do not want to compute
862 liveness information, as that may be linear in the size of CFG, and if
863 there are lot of different variables to rewrite, this may lead to quadratic
866 Instead, we basically emulate standard dce. We put all uses to worklist,
867 then for each of them find the nearest def that dominates them. If this
868 def is a phi node, we mark it live, and if it was not live before, we
869 add the predecessors of its basic block to the worklist.
871 To quickly locate the nearest def that dominates use, we use dfs numbering
872 of the dominance tree (that is already available in order to speed up
873 queries). For each def, we have the interval given by the dfs number on
874 entry to and on exit from the corresponding subtree in the dominance tree.
875 The nearest dominator for a given use is the smallest of these intervals
876 that contains entry and exit dfs numbers for the basic block with the use.
877 If we store the bounds for all the uses to an array and sort it, we can
878 locate the nearest dominating def in logarithmic time by binary search.*/
879 bitmap_ior (to_remove
, kills
, phis
);
880 n_defs
= bitmap_count_bits (to_remove
);
881 defs
= XNEWVEC (struct dom_dfsnum
, 2 * n_defs
+ 1);
882 defs
[0].bb_index
= 1;
885 EXECUTE_IF_SET_IN_BITMAP (to_remove
, 0, i
, bi
)
887 def_bb
= BASIC_BLOCK (i
);
888 defs
[adef
].bb_index
= i
;
889 defs
[adef
].dfs_num
= bb_dom_dfs_in (CDI_DOMINATORS
, def_bb
);
890 defs
[adef
+ 1].bb_index
= i
;
891 defs
[adef
+ 1].dfs_num
= bb_dom_dfs_out (CDI_DOMINATORS
, def_bb
);
894 BITMAP_FREE (to_remove
);
895 gcc_assert (adef
== 2 * n_defs
+ 1);
896 qsort (defs
, adef
, sizeof (struct dom_dfsnum
), cmp_dfsnum
);
897 gcc_assert (defs
[0].bb_index
== 1);
899 /* Now each DEFS entry contains the number of the basic block to that the
900 dfs number corresponds. Change them to the number of basic block that
901 corresponds to the interval following the dfs number. Also, for the
902 dfs_out numbers, increase the dfs number by one (so that it corresponds
903 to the start of the following interval, not to the end of the current
904 one). We use WORKLIST as a stack. */
905 worklist
= VEC_alloc (int, heap
, n_defs
+ 1);
906 VEC_quick_push (int, worklist
, 1);
909 for (i
= 1; i
< adef
; i
++)
911 b
= defs
[i
].bb_index
;
914 /* This is a closing element. Interval corresponding to the top
915 of the stack after removing it follows. */
916 VEC_pop (int, worklist
);
917 top
= VEC_index (int, worklist
, VEC_length (int, worklist
) - 1);
918 defs
[n_defs
].bb_index
= top
;
919 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
+ 1;
923 /* Opening element. Nothing to do, just push it to the stack and move
924 it to the correct position. */
925 defs
[n_defs
].bb_index
= defs
[i
].bb_index
;
926 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
;
927 VEC_quick_push (int, worklist
, b
);
931 /* If this interval starts at the same point as the previous one, cancel
933 if (defs
[n_defs
].dfs_num
== defs
[n_defs
- 1].dfs_num
)
934 defs
[n_defs
- 1].bb_index
= defs
[n_defs
].bb_index
;
938 VEC_pop (int, worklist
);
939 gcc_assert (VEC_empty (int, worklist
));
941 /* Now process the uses. */
942 live_phis
= BITMAP_ALLOC (NULL
);
943 EXECUTE_IF_SET_IN_BITMAP (uses
, 0, i
, bi
)
945 VEC_safe_push (int, heap
, worklist
, i
);
948 while (!VEC_empty (int, worklist
))
950 b
= VEC_pop (int, worklist
);
951 if (b
== ENTRY_BLOCK
)
954 /* If there is a phi node in USE_BB, it is made live. Otherwise,
955 find the def that dominates the immediate dominator of USE_BB
956 (the kill in USE_BB does not dominate the use). */
957 if (bitmap_bit_p (phis
, b
))
961 use_bb
= get_immediate_dominator (CDI_DOMINATORS
, BASIC_BLOCK (b
));
962 p
= find_dfsnum_interval (defs
, n_defs
,
963 bb_dom_dfs_in (CDI_DOMINATORS
, use_bb
));
964 if (!bitmap_bit_p (phis
, p
))
968 /* If the phi node is already live, there is nothing to do. */
969 if (!bitmap_set_bit (live_phis
, p
))
972 /* Add the new uses to the worklist. */
973 def_bb
= BASIC_BLOCK (p
);
974 FOR_EACH_EDGE (e
, ei
, def_bb
->preds
)
977 if (bitmap_bit_p (uses
, u
))
980 /* In case there is a kill directly in the use block, do not record
981 the use (this is also necessary for correctness, as we assume that
982 uses dominated by a def directly in their block have been filtered
984 if (bitmap_bit_p (kills
, u
))
987 bitmap_set_bit (uses
, u
);
988 VEC_safe_push (int, heap
, worklist
, u
);
992 VEC_free (int, heap
, worklist
);
993 bitmap_copy (phis
, live_phis
);
994 BITMAP_FREE (live_phis
);
998 /* Return the set of blocks where variable VAR is defined and the blocks
999 where VAR is live on entry (livein). Return NULL, if no entry is
1000 found in DEF_BLOCKS. */
1002 static inline struct def_blocks_d
*
1003 find_def_blocks_for (tree var
)
1005 struct def_blocks_d dm
;
1007 return (struct def_blocks_d
*) htab_find (def_blocks
, &dm
);
1011 /* Retrieve or create a default definition for symbol SYM. */
1014 get_default_def_for (tree sym
)
1016 tree ddef
= gimple_default_def (cfun
, sym
);
1018 if (ddef
== NULL_TREE
)
1020 ddef
= make_ssa_name (sym
, gimple_build_nop ());
1021 set_default_def (sym
, ddef
);
1028 /* Marks phi node PHI in basic block BB for rewrite. */
1031 mark_phi_for_rewrite (basic_block bb
, gimple phi
)
1034 unsigned i
, idx
= bb
->index
;
1036 if (rewrite_uses_p (phi
))
1039 set_rewrite_uses (phi
, true);
1041 if (!blocks_with_phis_to_rewrite
)
1044 bitmap_set_bit (blocks_with_phis_to_rewrite
, idx
);
1045 VEC_reserve (gimple_vec
, heap
, phis_to_rewrite
, last_basic_block
+ 1);
1046 for (i
= VEC_length (gimple_vec
, phis_to_rewrite
); i
<= idx
; i
++)
1047 VEC_quick_push (gimple_vec
, phis_to_rewrite
, NULL
);
1049 phis
= VEC_index (gimple_vec
, phis_to_rewrite
, idx
);
1051 phis
= VEC_alloc (gimple
, heap
, 10);
1053 VEC_safe_push (gimple
, heap
, phis
, phi
);
1054 VEC_replace (gimple_vec
, phis_to_rewrite
, idx
, phis
);
1057 /* Insert PHI nodes for variable VAR using the iterated dominance
1058 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1059 function assumes that the caller is incrementally updating the
1060 existing SSA form, in which case VAR may be an SSA name instead of
1063 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1064 PHI node for VAR. On exit, only the nodes that received a PHI node
1065 for VAR will be present in PHI_INSERTION_POINTS. */
1068 insert_phi_nodes_for (tree var
, bitmap phi_insertion_points
, bool update_p
)
1075 struct def_blocks_d
*def_map
;
1077 def_map
= find_def_blocks_for (var
);
1078 gcc_assert (def_map
);
1080 /* Remove the blocks where we already have PHI nodes for VAR. */
1081 bitmap_and_compl_into (phi_insertion_points
, def_map
->phi_blocks
);
1083 /* Remove obviously useless phi nodes. */
1084 prune_unused_phi_nodes (phi_insertion_points
, def_map
->def_blocks
,
1085 def_map
->livein_blocks
);
1087 /* And insert the PHI nodes. */
1088 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points
, 0, bb_index
, bi
)
1090 bb
= BASIC_BLOCK (bb_index
);
1092 mark_block_for_update (bb
);
1096 if (TREE_CODE (var
) == SSA_NAME
)
1098 /* If we are rewriting SSA names, create the LHS of the PHI
1099 node by duplicating VAR. This is useful in the case of
1100 pointers, to also duplicate pointer attributes (alias
1101 information, in particular). */
1105 gcc_assert (update_p
);
1106 phi
= create_phi_node (var
, bb
);
1108 new_lhs
= duplicate_ssa_name (var
, phi
);
1109 gimple_phi_set_result (phi
, new_lhs
);
1110 add_new_name_mapping (new_lhs
, var
);
1112 /* Add VAR to every argument slot of PHI. We need VAR in
1113 every argument so that rewrite_update_phi_arguments knows
1114 which name is this PHI node replacing. If VAR is a
1115 symbol marked for renaming, this is not necessary, the
1116 renamer will use the symbol on the LHS to get its
1117 reaching definition. */
1118 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1119 add_phi_arg (phi
, var
, e
, UNKNOWN_LOCATION
);
1125 gcc_assert (DECL_P (var
));
1126 phi
= create_phi_node (var
, bb
);
1128 tracked_var
= target_for_debug_bind (var
);
1131 gimple note
= gimple_build_debug_bind (tracked_var
,
1134 gimple_stmt_iterator si
= gsi_after_labels (bb
);
1135 gsi_insert_before (&si
, note
, GSI_SAME_STMT
);
1139 /* Mark this PHI node as interesting for update_ssa. */
1140 set_register_defs (phi
, true);
1141 mark_phi_for_rewrite (bb
, phi
);
1146 /* Insert PHI nodes at the dominance frontier of blocks with variable
1147 definitions. DFS contains the dominance frontier information for
1151 insert_phi_nodes (bitmap_head
*dfs
)
1153 referenced_var_iterator rvi
;
1159 timevar_push (TV_TREE_INSERT_PHI_NODES
);
1161 /* Do two stages to avoid code generation differences for UID
1162 differences but no UID ordering differences. */
1164 vars
= BITMAP_ALLOC (NULL
);
1165 FOR_EACH_REFERENCED_VAR (cfun
, var
, rvi
)
1167 struct def_blocks_d
*def_map
;
1169 def_map
= find_def_blocks_for (var
);
1170 if (def_map
== NULL
)
1173 if (get_phi_state (var
) != NEED_PHI_STATE_NO
)
1174 bitmap_set_bit (vars
, DECL_UID (var
));
1177 EXECUTE_IF_SET_IN_BITMAP (vars
, 0, uid
, bi
)
1179 tree var
= referenced_var (uid
);
1180 struct def_blocks_d
*def_map
;
1183 def_map
= find_def_blocks_for (var
);
1184 idf
= compute_idf (def_map
->def_blocks
, dfs
);
1185 insert_phi_nodes_for (var
, idf
, false);
1191 timevar_pop (TV_TREE_INSERT_PHI_NODES
);
1195 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1196 register DEF (an SSA_NAME) to be a new definition for SYM. */
1199 register_new_def (tree def
, tree sym
)
1203 /* If this variable is set in a single basic block and all uses are
1204 dominated by the set(s) in that single basic block, then there is
1205 no reason to record anything for this variable in the block local
1206 definition stacks. Doing so just wastes time and memory.
1208 This is the same test to prune the set of variables which may
1209 need PHI nodes. So we just use that information since it's already
1210 computed and available for us to use. */
1211 if (get_phi_state (sym
) == NEED_PHI_STATE_NO
)
1213 set_current_def (sym
, def
);
1217 currdef
= get_current_def (sym
);
1219 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1220 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1221 in the stack so that we know which symbol is being defined by
1222 this SSA name when we unwind the stack. */
1223 if (currdef
&& !is_gimple_reg (sym
))
1224 VEC_safe_push (tree
, heap
, block_defs_stack
, sym
);
1226 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1227 stack is later used by the dominator tree callbacks to restore
1228 the reaching definitions for all the variables defined in the
1229 block after a recursive visit to all its immediately dominated
1230 blocks. If there is no current reaching definition, then just
1231 record the underlying _DECL node. */
1232 VEC_safe_push (tree
, heap
, block_defs_stack
, currdef
? currdef
: sym
);
1234 /* Set the current reaching definition for SYM to be DEF. */
1235 set_current_def (sym
, def
);
1239 /* Perform a depth-first traversal of the dominator tree looking for
1240 variables to rename. BB is the block where to start searching.
1241 Renaming is a five step process:
1243 1- Every definition made by PHI nodes at the start of the blocks is
1244 registered as the current definition for the corresponding variable.
1246 2- Every statement in BB is rewritten. USE and VUSE operands are
1247 rewritten with their corresponding reaching definition. DEF and
1248 VDEF targets are registered as new definitions.
1250 3- All the PHI nodes in successor blocks of BB are visited. The
1251 argument corresponding to BB is replaced with its current reaching
1254 4- Recursively rewrite every dominator child block of BB.
1256 5- Restore (in reverse order) the current reaching definition for every
1257 new definition introduced in this block. This is done so that when
1258 we return from the recursive call, all the current reaching
1259 definitions are restored to the names that were valid in the
1260 dominator parent of BB. */
1262 /* Return the current definition for variable VAR. If none is found,
1263 create a new SSA name to act as the zeroth definition for VAR. */
1266 get_reaching_def (tree var
)
1270 /* Lookup the current reaching definition for VAR. */
1271 currdef
= get_current_def (var
);
1273 /* If there is no reaching definition for VAR, create and register a
1274 default definition for it (if needed). */
1275 if (currdef
== NULL_TREE
)
1277 tree sym
= DECL_P (var
) ? var
: SSA_NAME_VAR (var
);
1278 currdef
= get_default_def_for (sym
);
1279 set_current_def (var
, currdef
);
1282 /* Return the current reaching definition for VAR, or the default
1283 definition, if we had to create one. */
1288 /* Helper function for rewrite_stmt. Rewrite uses in a debug stmt. */
1291 rewrite_debug_stmt_uses (gimple stmt
)
1293 use_operand_p use_p
;
1295 bool update
= false;
1297 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1299 tree var
= USE_FROM_PTR (use_p
), def
= NULL_TREE
;
1300 gcc_assert (DECL_P (var
));
1301 if (var_ann (var
) == NULL
)
1303 if (TREE_CODE (var
) == PARM_DECL
&& single_succ_p (ENTRY_BLOCK_PTR
))
1305 gimple_stmt_iterator gsi
1306 = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR
));
1308 /* Search a few source bind stmts at the start of first bb to
1309 see if a DEBUG_EXPR_DECL can't be reused. */
1311 !gsi_end_p (gsi
) && lim
> 0;
1312 gsi_next (&gsi
), lim
--)
1314 gimple gstmt
= gsi_stmt (gsi
);
1315 if (!gimple_debug_source_bind_p (gstmt
))
1317 if (gimple_debug_source_bind_get_value (gstmt
) == var
)
1319 def
= gimple_debug_source_bind_get_var (gstmt
);
1320 if (TREE_CODE (def
) == DEBUG_EXPR_DECL
)
1326 /* If not, add a new source bind stmt. */
1327 if (def
== NULL_TREE
)
1330 def
= make_node (DEBUG_EXPR_DECL
);
1331 def_temp
= gimple_build_debug_source_bind (def
, var
, NULL
);
1332 DECL_ARTIFICIAL (def
) = 1;
1333 TREE_TYPE (def
) = TREE_TYPE (var
);
1334 DECL_MODE (def
) = DECL_MODE (var
);
1335 gsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR
));
1336 gsi_insert_before (&gsi
, def_temp
, GSI_SAME_STMT
);
1343 def
= get_current_def (var
);
1344 /* Check if get_current_def can be trusted. */
1347 basic_block bb
= gimple_bb (stmt
);
1349 = SSA_NAME_IS_DEFAULT_DEF (def
)
1350 ? NULL
: gimple_bb (SSA_NAME_DEF_STMT (def
));
1352 /* If definition is in current bb, it is fine. */
1355 /* If definition bb doesn't dominate the current bb,
1356 it can't be used. */
1357 else if (def_bb
&& !dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
1359 /* If there is just one definition and dominates the current
1361 else if (get_phi_state (var
) == NEED_PHI_STATE_NO
)
1365 struct def_blocks_d
*db_p
= get_def_blocks_for (var
);
1367 /* If there are some non-debug uses in the current bb,
1369 if (bitmap_bit_p (db_p
->livein_blocks
, bb
->index
))
1371 /* Otherwise give up for now. */
1379 gimple_debug_bind_reset_value (stmt
);
1383 SET_USE (use_p
, def
);
1389 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1390 the block with its immediate reaching definitions. Update the current
1391 definition of a variable when a new real or virtual definition is found. */
1394 rewrite_stmt (gimple_stmt_iterator si
)
1396 use_operand_p use_p
;
1397 def_operand_p def_p
;
1399 gimple stmt
= gsi_stmt (si
);
1401 /* If mark_def_sites decided that we don't need to rewrite this
1402 statement, ignore it. */
1403 gcc_assert (blocks_to_update
== NULL
);
1404 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
1407 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1409 fprintf (dump_file
, "Renaming statement ");
1410 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1411 fprintf (dump_file
, "\n");
1414 /* Step 1. Rewrite USES in the statement. */
1415 if (rewrite_uses_p (stmt
))
1417 if (is_gimple_debug (stmt
))
1418 rewrite_debug_stmt_uses (stmt
);
1420 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1422 tree var
= USE_FROM_PTR (use_p
);
1423 gcc_assert (DECL_P (var
));
1424 SET_USE (use_p
, get_reaching_def (var
));
1428 /* Step 2. Register the statement's DEF operands. */
1429 if (register_defs_p (stmt
))
1430 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_DEF
)
1432 tree var
= DEF_FROM_PTR (def_p
);
1433 tree name
= make_ssa_name (var
, stmt
);
1435 gcc_assert (DECL_P (var
));
1436 SET_DEF (def_p
, name
);
1437 register_new_def (DEF_FROM_PTR (def_p
), var
);
1439 tracked_var
= target_for_debug_bind (var
);
1442 gimple note
= gimple_build_debug_bind (tracked_var
, name
, stmt
);
1443 gsi_insert_after (&si
, note
, GSI_SAME_STMT
);
1449 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1450 PHI nodes. For every PHI node found, add a new argument containing the
1451 current reaching definition for the variable and the edge through which
1452 that definition is reaching the PHI node. */
1455 rewrite_add_phi_arguments (basic_block bb
)
1460 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1463 gimple_stmt_iterator gsi
;
1465 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
);
1471 phi
= gsi_stmt (gsi
);
1472 currdef
= get_reaching_def (SSA_NAME_VAR (gimple_phi_result (phi
)));
1473 stmt
= SSA_NAME_DEF_STMT (currdef
);
1474 add_phi_arg (phi
, currdef
, e
, gimple_location (stmt
));
1479 /* SSA Rewriting Step 1. Initialization, create a block local stack
1480 of reaching definitions for new SSA names produced in this block
1481 (BLOCK_DEFS). Register new definitions for every PHI node in the
1485 rewrite_enter_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1489 gimple_stmt_iterator gsi
;
1491 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1492 fprintf (dump_file
, "\n\nRenaming block #%d\n\n", bb
->index
);
1494 /* Mark the unwind point for this block. */
1495 VEC_safe_push (tree
, heap
, block_defs_stack
, NULL_TREE
);
1497 /* Step 1. Register new definitions for every PHI node in the block.
1498 Conceptually, all the PHI nodes are executed in parallel and each PHI
1499 node introduces a new version for the associated variable. */
1500 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1504 phi
= gsi_stmt (gsi
);
1505 result
= gimple_phi_result (phi
);
1506 gcc_assert (is_gimple_reg (result
));
1507 register_new_def (result
, SSA_NAME_VAR (result
));
1510 /* Step 2. Rewrite every variable used in each statement in the block
1511 with its immediate reaching definitions. Update the current definition
1512 of a variable when a new real or virtual definition is found. */
1513 if (TEST_BIT (interesting_blocks
, bb
->index
))
1514 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1517 /* Step 3. Visit all the successor blocks of BB looking for PHI nodes.
1518 For every PHI node found, add a new argument containing the current
1519 reaching definition for the variable and the edge through which that
1520 definition is reaching the PHI node. */
1521 rewrite_add_phi_arguments (bb
);
1526 /* Called after visiting all the statements in basic block BB and all
1527 of its dominator children. Restore CURRDEFS to its original value. */
1530 rewrite_leave_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1531 basic_block bb ATTRIBUTE_UNUSED
)
1533 /* Restore CURRDEFS to its original state. */
1534 while (VEC_length (tree
, block_defs_stack
) > 0)
1536 tree tmp
= VEC_pop (tree
, block_defs_stack
);
1537 tree saved_def
, var
;
1539 if (tmp
== NULL_TREE
)
1542 if (TREE_CODE (tmp
) == SSA_NAME
)
1544 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1545 current definition of its underlying variable. Note that
1546 if the SSA_NAME is not for a GIMPLE register, the symbol
1547 being defined is stored in the next slot in the stack.
1548 This mechanism is needed because an SSA name for a
1549 non-register symbol may be the definition for more than
1550 one symbol (e.g., SFTs, aliased variables, etc). */
1552 var
= SSA_NAME_VAR (saved_def
);
1553 if (!is_gimple_reg (var
))
1554 var
= VEC_pop (tree
, block_defs_stack
);
1558 /* If we recorded anything else, it must have been a _DECL
1559 node and its current reaching definition must have been
1565 set_current_def (var
, saved_def
);
1570 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1573 dump_decl_set (FILE *file
, bitmap set
)
1580 fprintf (file
, "{ ");
1582 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
1584 tree var
= referenced_var_lookup (cfun
, i
);
1586 print_generic_expr (file
, var
, 0);
1588 fprintf (file
, "D.%u", i
);
1589 fprintf (file
, " ");
1592 fprintf (file
, "}");
1595 fprintf (file
, "NIL");
1599 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1602 debug_decl_set (bitmap set
)
1604 dump_decl_set (stderr
, set
);
1605 fprintf (stderr
, "\n");
1609 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1610 stack up to a maximum of N levels. If N is -1, the whole stack is
1611 dumped. New levels are created when the dominator tree traversal
1612 used for renaming enters a new sub-tree. */
1615 dump_defs_stack (FILE *file
, int n
)
1619 fprintf (file
, "\n\nRenaming stack");
1621 fprintf (file
, " (up to %d levels)", n
);
1622 fprintf (file
, "\n\n");
1625 fprintf (file
, "Level %d (current level)\n", i
);
1626 for (j
= (int) VEC_length (tree
, block_defs_stack
) - 1; j
>= 0; j
--)
1630 name
= VEC_index (tree
, block_defs_stack
, j
);
1631 if (name
== NULL_TREE
)
1636 fprintf (file
, "\nLevel %d\n", i
);
1647 var
= SSA_NAME_VAR (name
);
1648 if (!is_gimple_reg (var
))
1651 var
= VEC_index (tree
, block_defs_stack
, j
);
1655 fprintf (file
, " Previous CURRDEF (");
1656 print_generic_expr (file
, var
, 0);
1657 fprintf (file
, ") = ");
1659 print_generic_expr (file
, name
, 0);
1661 fprintf (file
, "<NIL>");
1662 fprintf (file
, "\n");
1667 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1668 stack up to a maximum of N levels. If N is -1, the whole stack is
1669 dumped. New levels are created when the dominator tree traversal
1670 used for renaming enters a new sub-tree. */
1673 debug_defs_stack (int n
)
1675 dump_defs_stack (stderr
, n
);
1679 /* Dump the current reaching definition of every symbol to FILE. */
1682 dump_currdefs (FILE *file
)
1684 referenced_var_iterator i
;
1687 fprintf (file
, "\n\nCurrent reaching definitions\n\n");
1688 FOR_EACH_REFERENCED_VAR (cfun
, var
, i
)
1689 if (SYMS_TO_RENAME (cfun
) == NULL
1690 || bitmap_bit_p (SYMS_TO_RENAME (cfun
), DECL_UID (var
)))
1692 fprintf (file
, "CURRDEF (");
1693 print_generic_expr (file
, var
, 0);
1694 fprintf (file
, ") = ");
1695 if (get_current_def (var
))
1696 print_generic_expr (file
, get_current_def (var
), 0);
1698 fprintf (file
, "<NIL>");
1699 fprintf (file
, "\n");
1704 /* Dump the current reaching definition of every symbol to stderr. */
1707 debug_currdefs (void)
1709 dump_currdefs (stderr
);
1713 /* Dump SSA information to FILE. */
1716 dump_tree_ssa (FILE *file
)
1718 const char *funcname
1719 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
1721 fprintf (file
, "SSA renaming information for %s\n\n", funcname
);
1723 dump_def_blocks (file
);
1724 dump_defs_stack (file
, -1);
1725 dump_currdefs (file
);
1726 dump_tree_ssa_stats (file
);
1730 /* Dump SSA information to stderr. */
1733 debug_tree_ssa (void)
1735 dump_tree_ssa (stderr
);
1739 /* Dump statistics for the hash table HTAB. */
1742 htab_statistics (FILE *file
, htab_t htab
)
1744 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
1745 (long) htab_size (htab
),
1746 (long) htab_elements (htab
),
1747 htab_collisions (htab
));
1751 /* Dump SSA statistics on FILE. */
1754 dump_tree_ssa_stats (FILE *file
)
1756 if (def_blocks
|| repl_tbl
)
1757 fprintf (file
, "\nHash table statistics:\n");
1761 fprintf (file
, " def_blocks: ");
1762 htab_statistics (file
, def_blocks
);
1767 fprintf (file
, " repl_tbl: ");
1768 htab_statistics (file
, repl_tbl
);
1771 if (def_blocks
|| repl_tbl
)
1772 fprintf (file
, "\n");
1776 /* Dump SSA statistics on stderr. */
1779 debug_tree_ssa_stats (void)
1781 dump_tree_ssa_stats (stderr
);
1785 /* Hashing and equality functions for DEF_BLOCKS. */
1788 def_blocks_hash (const void *p
)
1790 return htab_hash_pointer
1791 ((const void *)((const struct def_blocks_d
*)p
)->var
);
1795 def_blocks_eq (const void *p1
, const void *p2
)
1797 return ((const struct def_blocks_d
*)p1
)->var
1798 == ((const struct def_blocks_d
*)p2
)->var
;
1802 /* Free memory allocated by one entry in DEF_BLOCKS. */
1805 def_blocks_free (void *p
)
1807 struct def_blocks_d
*entry
= (struct def_blocks_d
*) p
;
1808 BITMAP_FREE (entry
->def_blocks
);
1809 BITMAP_FREE (entry
->phi_blocks
);
1810 BITMAP_FREE (entry
->livein_blocks
);
1815 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1818 debug_def_blocks_r (void **slot
, void *data
)
1820 FILE *file
= (FILE *) data
;
1821 struct def_blocks_d
*db_p
= (struct def_blocks_d
*) *slot
;
1823 fprintf (file
, "VAR: ");
1824 print_generic_expr (file
, db_p
->var
, dump_flags
);
1825 bitmap_print (file
, db_p
->def_blocks
, ", DEF_BLOCKS: { ", "}");
1826 bitmap_print (file
, db_p
->livein_blocks
, ", LIVEIN_BLOCKS: { ", "}");
1827 bitmap_print (file
, db_p
->phi_blocks
, ", PHI_BLOCKS: { ", "}\n");
1833 /* Dump the DEF_BLOCKS hash table on FILE. */
1836 dump_def_blocks (FILE *file
)
1838 fprintf (file
, "\n\nDefinition and live-in blocks:\n\n");
1840 htab_traverse (def_blocks
, debug_def_blocks_r
, file
);
1844 /* Dump the DEF_BLOCKS hash table on stderr. */
1847 debug_def_blocks (void)
1849 dump_def_blocks (stderr
);
1853 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1856 register_new_update_single (tree new_name
, tree old_name
)
1858 tree currdef
= get_current_def (old_name
);
1860 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1861 This stack is later used by the dominator tree callbacks to
1862 restore the reaching definitions for all the variables
1863 defined in the block after a recursive visit to all its
1864 immediately dominated blocks. */
1865 VEC_reserve (tree
, heap
, block_defs_stack
, 2);
1866 VEC_quick_push (tree
, block_defs_stack
, currdef
);
1867 VEC_quick_push (tree
, block_defs_stack
, old_name
);
1869 /* Set the current reaching definition for OLD_NAME to be
1871 set_current_def (old_name
, new_name
);
1875 /* Register NEW_NAME to be the new reaching definition for all the
1876 names in OLD_NAMES. Used by the incremental SSA update routines to
1877 replace old SSA names with new ones. */
1880 register_new_update_set (tree new_name
, bitmap old_names
)
1885 EXECUTE_IF_SET_IN_BITMAP (old_names
, 0, i
, bi
)
1886 register_new_update_single (new_name
, ssa_name (i
));
1891 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1892 it is a symbol marked for renaming, replace it with USE_P's current
1893 reaching definition. */
1896 maybe_replace_use (use_operand_p use_p
)
1898 tree rdef
= NULL_TREE
;
1899 tree use
= USE_FROM_PTR (use_p
);
1900 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1902 if (symbol_marked_for_renaming (sym
))
1903 rdef
= get_reaching_def (sym
);
1904 else if (is_old_name (use
))
1905 rdef
= get_reaching_def (use
);
1907 if (rdef
&& rdef
!= use
)
1908 SET_USE (use_p
, rdef
);
1912 /* Same as maybe_replace_use, but without introducing default stmts,
1913 returning false to indicate a need to do so. */
1916 maybe_replace_use_in_debug_stmt (use_operand_p use_p
)
1918 tree rdef
= NULL_TREE
;
1919 tree use
= USE_FROM_PTR (use_p
);
1920 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1922 if (symbol_marked_for_renaming (sym
))
1923 rdef
= get_current_def (sym
);
1924 else if (is_old_name (use
))
1926 rdef
= get_current_def (use
);
1927 /* We can't assume that, if there's no current definition, the
1928 default one should be used. It could be the case that we've
1929 rearranged blocks so that the earlier definition no longer
1930 dominates the use. */
1931 if (!rdef
&& SSA_NAME_IS_DEFAULT_DEF (use
))
1937 if (rdef
&& rdef
!= use
)
1938 SET_USE (use_p
, rdef
);
1940 return rdef
!= NULL_TREE
;
1944 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1945 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1946 register it as the current definition for the names replaced by
1950 maybe_register_def (def_operand_p def_p
, gimple stmt
,
1951 gimple_stmt_iterator gsi
)
1953 tree def
= DEF_FROM_PTR (def_p
);
1954 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
1956 /* If DEF is a naked symbol that needs renaming, create a new
1958 if (symbol_marked_for_renaming (sym
))
1964 def
= make_ssa_name (def
, stmt
);
1965 SET_DEF (def_p
, def
);
1967 tracked_var
= target_for_debug_bind (sym
);
1970 gimple note
= gimple_build_debug_bind (tracked_var
, def
, stmt
);
1971 /* If stmt ends the bb, insert the debug stmt on the single
1972 non-EH edge from the stmt. */
1973 if (gsi_one_before_end_p (gsi
) && stmt_ends_bb_p (stmt
))
1975 basic_block bb
= gsi_bb (gsi
);
1978 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1979 if (!(e
->flags
& EDGE_EH
))
1984 /* If there are other predecessors to ef->dest, then
1985 there must be PHI nodes for the modified
1986 variable, and therefore there will be debug bind
1987 stmts after the PHI nodes. The debug bind notes
1988 we'd insert would force the creation of a new
1989 block (diverging codegen) and be redundant with
1990 the post-PHI bind stmts, so don't add them.
1992 As for the exit edge, there wouldn't be redundant
1993 bind stmts, but there wouldn't be a PC to bind
1994 them to either, so avoid diverging the CFG. */
1995 if (ef
&& single_pred_p (ef
->dest
)
1996 && ef
->dest
!= EXIT_BLOCK_PTR
)
1998 /* If there were PHI nodes in the node, we'd
1999 have to make sure the value we're binding
2000 doesn't need rewriting. But there shouldn't
2001 be PHI nodes in a single-predecessor block,
2002 so we just add the note. */
2003 gsi_insert_on_edge_immediate (ef
, note
);
2007 gsi_insert_after (&gsi
, note
, GSI_SAME_STMT
);
2011 register_new_update_single (def
, sym
);
2015 /* If DEF is a new name, register it as a new definition
2016 for all the names replaced by DEF. */
2017 if (is_new_name (def
))
2018 register_new_update_set (def
, names_replaced_by (def
));
2020 /* If DEF is an old name, register DEF as a new
2021 definition for itself. */
2022 if (is_old_name (def
))
2023 register_new_update_single (def
, def
);
2028 /* Update every variable used in the statement pointed-to by SI. The
2029 statement is assumed to be in SSA form already. Names in
2030 OLD_SSA_NAMES used by SI will be updated to their current reaching
2031 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
2032 will be registered as a new definition for their corresponding name
2033 in OLD_SSA_NAMES. */
2036 rewrite_update_stmt (gimple stmt
, gimple_stmt_iterator gsi
)
2038 use_operand_p use_p
;
2039 def_operand_p def_p
;
2042 /* Only update marked statements. */
2043 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
2046 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2048 fprintf (dump_file
, "Updating SSA information for statement ");
2049 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
2052 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
2053 symbol is marked for renaming. */
2054 if (rewrite_uses_p (stmt
))
2056 if (is_gimple_debug (stmt
))
2058 bool failed
= false;
2060 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
2061 if (!maybe_replace_use_in_debug_stmt (use_p
))
2069 /* DOM sometimes threads jumps in such a way that a
2070 debug stmt ends up referencing a SSA variable that no
2071 longer dominates the debug stmt, but such that all
2072 incoming definitions refer to the same definition in
2073 an earlier dominator. We could try to recover that
2074 definition somehow, but this will have to do for now.
2076 Introducing a default definition, which is what
2077 maybe_replace_use() would do in such cases, may
2078 modify code generation, for the otherwise-unused
2079 default definition would never go away, modifying SSA
2080 version numbers all over. */
2081 gimple_debug_bind_reset_value (stmt
);
2087 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
2088 maybe_replace_use (use_p
);
2092 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
2093 Also register definitions for names whose underlying symbol is
2094 marked for renaming. */
2095 if (register_defs_p (stmt
))
2096 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_ALL_DEFS
)
2097 maybe_register_def (def_p
, stmt
, gsi
);
2101 /* Visit all the successor blocks of BB looking for PHI nodes. For
2102 every PHI node found, check if any of its arguments is in
2103 OLD_SSA_NAMES. If so, and if the argument has a current reaching
2104 definition, replace it. */
2107 rewrite_update_phi_arguments (basic_block bb
)
2113 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2118 if (!bitmap_bit_p (blocks_with_phis_to_rewrite
, e
->dest
->index
))
2121 phis
= VEC_index (gimple_vec
, phis_to_rewrite
, e
->dest
->index
);
2122 FOR_EACH_VEC_ELT (gimple
, phis
, i
, phi
)
2124 tree arg
, lhs_sym
, reaching_def
= NULL
;
2125 use_operand_p arg_p
;
2127 gcc_assert (rewrite_uses_p (phi
));
2129 arg_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
2130 arg
= USE_FROM_PTR (arg_p
);
2132 if (arg
&& !DECL_P (arg
) && TREE_CODE (arg
) != SSA_NAME
)
2135 lhs_sym
= SSA_NAME_VAR (gimple_phi_result (phi
));
2137 if (arg
== NULL_TREE
)
2139 /* When updating a PHI node for a recently introduced
2140 symbol we may find NULL arguments. That's why we
2141 take the symbol from the LHS of the PHI node. */
2142 reaching_def
= get_reaching_def (lhs_sym
);
2147 tree sym
= DECL_P (arg
) ? arg
: SSA_NAME_VAR (arg
);
2149 if (symbol_marked_for_renaming (sym
))
2150 reaching_def
= get_reaching_def (sym
);
2151 else if (is_old_name (arg
))
2152 reaching_def
= get_reaching_def (arg
);
2155 /* Update the argument if there is a reaching def. */
2159 source_location locus
;
2160 int arg_i
= PHI_ARG_INDEX_FROM_USE (arg_p
);
2162 SET_USE (arg_p
, reaching_def
);
2163 stmt
= SSA_NAME_DEF_STMT (reaching_def
);
2165 /* Single element PHI nodes behave like copies, so get the
2166 location from the phi argument. */
2167 if (gimple_code (stmt
) == GIMPLE_PHI
&&
2168 gimple_phi_num_args (stmt
) == 1)
2169 locus
= gimple_phi_arg_location (stmt
, 0);
2171 locus
= gimple_location (stmt
);
2173 gimple_phi_arg_set_location (phi
, arg_i
, locus
);
2177 if (e
->flags
& EDGE_ABNORMAL
)
2178 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p
)) = 1;
2184 /* Initialization of block data structures for the incremental SSA
2185 update pass. Create a block local stack of reaching definitions
2186 for new SSA names produced in this block (BLOCK_DEFS). Register
2187 new definitions for every PHI node in the block. */
2190 rewrite_update_enter_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
2193 bool is_abnormal_phi
;
2194 gimple_stmt_iterator gsi
;
2196 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2197 fprintf (dump_file
, "Registering new PHI nodes in block #%d\n",
2200 /* Mark the unwind point for this block. */
2201 VEC_safe_push (tree
, heap
, block_defs_stack
, NULL_TREE
);
2203 if (!bitmap_bit_p (blocks_to_update
, bb
->index
))
2206 /* Mark the LHS if any of the arguments flows through an abnormal
2208 is_abnormal_phi
= bb_has_abnormal_pred (bb
);
2210 /* If any of the PHI nodes is a replacement for a name in
2211 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2212 register it as a new definition for its corresponding name. Also
2213 register definitions for names whose underlying symbols are
2214 marked for renaming. */
2215 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2218 gimple phi
= gsi_stmt (gsi
);
2220 if (!register_defs_p (phi
))
2223 lhs
= gimple_phi_result (phi
);
2224 lhs_sym
= SSA_NAME_VAR (lhs
);
2226 if (symbol_marked_for_renaming (lhs_sym
))
2227 register_new_update_single (lhs
, lhs_sym
);
2231 /* If LHS is a new name, register a new definition for all
2232 the names replaced by LHS. */
2233 if (is_new_name (lhs
))
2234 register_new_update_set (lhs
, names_replaced_by (lhs
));
2236 /* If LHS is an OLD name, register it as a new definition
2238 if (is_old_name (lhs
))
2239 register_new_update_single (lhs
, lhs
);
2242 if (is_abnormal_phi
)
2243 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
) = 1;
2246 /* Step 2. Rewrite every variable used in each statement in the block. */
2247 if (TEST_BIT (interesting_blocks
, bb
->index
))
2249 gcc_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2250 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2251 rewrite_update_stmt (gsi_stmt (gsi
), gsi
);
2254 /* Step 3. Update PHI nodes. */
2255 rewrite_update_phi_arguments (bb
);
2258 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
2259 the current reaching definition of every name re-written in BB to
2260 the original reaching definition before visiting BB. This
2261 unwinding must be done in the opposite order to what is done in
2262 register_new_update_set. */
2265 rewrite_update_leave_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
2266 basic_block bb ATTRIBUTE_UNUSED
)
2268 while (VEC_length (tree
, block_defs_stack
) > 0)
2270 tree var
= VEC_pop (tree
, block_defs_stack
);
2273 /* NULL indicates the unwind stop point for this block (see
2274 rewrite_update_enter_block). */
2278 saved_def
= VEC_pop (tree
, block_defs_stack
);
2279 set_current_def (var
, saved_def
);
2284 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2287 ENTRY indicates the block where to start. Every block dominated by
2288 ENTRY will be rewritten.
2290 WHAT indicates what actions will be taken by the renamer (see enum
2293 BLOCKS are the set of interesting blocks for the dominator walker
2294 to process. If this set is NULL, then all the nodes dominated
2295 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2296 are not present in BLOCKS are ignored. */
2299 rewrite_blocks (basic_block entry
, enum rewrite_mode what
)
2301 struct dom_walk_data walk_data
;
2303 /* Rewrite all the basic blocks in the program. */
2304 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS
);
2306 /* Setup callbacks for the generic dominator tree walker. */
2307 memset (&walk_data
, 0, sizeof (walk_data
));
2309 walk_data
.dom_direction
= CDI_DOMINATORS
;
2311 if (what
== REWRITE_ALL
)
2313 walk_data
.before_dom_children
= rewrite_enter_block
;
2314 walk_data
.after_dom_children
= rewrite_leave_block
;
2316 else if (what
== REWRITE_UPDATE
)
2318 walk_data
.before_dom_children
= rewrite_update_enter_block
;
2319 walk_data
.after_dom_children
= rewrite_update_leave_block
;
2324 block_defs_stack
= VEC_alloc (tree
, heap
, 10);
2326 /* Initialize the dominator walker. */
2327 init_walk_dominator_tree (&walk_data
);
2329 /* Recursively walk the dominator tree rewriting each statement in
2330 each basic block. */
2331 walk_dominator_tree (&walk_data
, entry
);
2333 /* Finalize the dominator walker. */
2334 fini_walk_dominator_tree (&walk_data
);
2336 /* Debugging dumps. */
2337 if (dump_file
&& (dump_flags
& TDF_STATS
))
2339 dump_dfa_stats (dump_file
);
2341 dump_tree_ssa_stats (dump_file
);
2344 VEC_free (tree
, heap
, block_defs_stack
);
2346 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS
);
2350 /* Block processing routine for mark_def_sites. Clear the KILLS bitmap
2351 at the start of each block, and call mark_def_sites for each statement. */
2354 mark_def_sites_block (struct dom_walk_data
*walk_data
, basic_block bb
)
2356 struct mark_def_sites_global_data
*gd
;
2358 gimple_stmt_iterator gsi
;
2360 gd
= (struct mark_def_sites_global_data
*) walk_data
->global_data
;
2363 bitmap_clear (kills
);
2364 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2365 mark_def_sites (bb
, gsi_stmt (gsi
), kills
);
2369 /* Mark the definition site blocks for each variable, so that we know
2370 where the variable is actually live.
2372 The INTERESTING_BLOCKS global will be filled in with all the blocks
2373 that should be processed by the renamer. It is assumed that the
2374 caller has already initialized and zeroed it. */
2377 mark_def_site_blocks (void)
2379 struct dom_walk_data walk_data
;
2380 struct mark_def_sites_global_data mark_def_sites_global_data
;
2382 /* Setup callbacks for the generic dominator tree walker to find and
2383 mark definition sites. */
2384 walk_data
.dom_direction
= CDI_DOMINATORS
;
2385 walk_data
.initialize_block_local_data
= NULL
;
2386 walk_data
.before_dom_children
= mark_def_sites_block
;
2387 walk_data
.after_dom_children
= NULL
;
2389 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2390 large enough to accommodate all the variables referenced in the
2391 function, not just the ones we are renaming. */
2392 mark_def_sites_global_data
.kills
= BITMAP_ALLOC (NULL
);
2393 walk_data
.global_data
= &mark_def_sites_global_data
;
2395 /* We do not have any local data. */
2396 walk_data
.block_local_data_size
= 0;
2398 /* Initialize the dominator walker. */
2399 init_walk_dominator_tree (&walk_data
);
2401 /* Recursively walk the dominator tree. */
2402 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
2404 /* Finalize the dominator walker. */
2405 fini_walk_dominator_tree (&walk_data
);
2407 /* We no longer need this bitmap, clear and free it. */
2408 BITMAP_FREE (mark_def_sites_global_data
.kills
);
2412 /* Initialize internal data needed during renaming. */
2415 init_ssa_renamer (void)
2418 referenced_var_iterator rvi
;
2420 cfun
->gimple_df
->in_ssa_p
= false;
2422 /* Allocate memory for the DEF_BLOCKS hash table. */
2423 gcc_assert (def_blocks
== NULL
);
2424 def_blocks
= htab_create (num_referenced_vars
, def_blocks_hash
,
2425 def_blocks_eq
, def_blocks_free
);
2427 FOR_EACH_REFERENCED_VAR (cfun
, var
, rvi
)
2428 set_current_def (var
, NULL_TREE
);
2432 /* Deallocate internal data structures used by the renamer. */
2435 fini_ssa_renamer (void)
2439 htab_delete (def_blocks
);
2443 cfun
->gimple_df
->in_ssa_p
= true;
2446 /* Main entry point into the SSA builder. The renaming process
2447 proceeds in four main phases:
2449 1- Compute dominance frontier and immediate dominators, needed to
2450 insert PHI nodes and rename the function in dominator tree
2453 2- Find and mark all the blocks that define variables
2454 (mark_def_site_blocks).
2456 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2458 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2460 Steps 3 and 4 are done using the dominator tree walker
2461 (walk_dominator_tree). */
2464 rewrite_into_ssa (void)
2469 /* Initialize operand data structures. */
2470 init_ssa_operands (cfun
);
2472 /* Initialize internal data needed by the renamer. */
2473 init_ssa_renamer ();
2475 /* Initialize the set of interesting blocks. The callback
2476 mark_def_sites will add to this set those blocks that the renamer
2478 interesting_blocks
= sbitmap_alloc (last_basic_block
);
2479 sbitmap_zero (interesting_blocks
);
2481 /* Initialize dominance frontier. */
2482 dfs
= XNEWVEC (bitmap_head
, last_basic_block
);
2484 bitmap_initialize (&dfs
[bb
->index
], &bitmap_default_obstack
);
2486 /* 1- Compute dominance frontiers. */
2487 calculate_dominance_info (CDI_DOMINATORS
);
2488 compute_dominance_frontiers (dfs
);
2490 /* 2- Find and mark definition sites. */
2491 mark_def_site_blocks ();
2493 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2494 insert_phi_nodes (dfs
);
2496 /* 4- Rename all the blocks. */
2497 rewrite_blocks (ENTRY_BLOCK_PTR
, REWRITE_ALL
);
2499 /* Free allocated memory. */
2501 bitmap_clear (&dfs
[bb
->index
]);
2504 sbitmap_free (interesting_blocks
);
2506 fini_ssa_renamer ();
2512 struct gimple_opt_pass pass_build_ssa
=
2518 rewrite_into_ssa
, /* execute */
2521 0, /* static_pass_number */
2522 TV_TREE_SSA_OTHER
, /* tv_id */
2523 PROP_cfg
| PROP_referenced_vars
, /* properties_required */
2524 PROP_ssa
, /* properties_provided */
2525 0, /* properties_destroyed */
2526 0, /* todo_flags_start */
2527 TODO_update_ssa_only_virtuals
2529 | TODO_remove_unused_locals
/* todo_flags_finish */
2534 /* Mark the definition of VAR at STMT and BB as interesting for the
2535 renamer. BLOCKS is the set of blocks that need updating. */
2538 mark_def_interesting (tree var
, gimple stmt
, basic_block bb
, bool insert_phi_p
)
2540 gcc_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2541 set_register_defs (stmt
, true);
2545 bool is_phi_p
= gimple_code (stmt
) == GIMPLE_PHI
;
2547 set_def_block (var
, bb
, is_phi_p
);
2549 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2550 site for both itself and all the old names replaced by it. */
2551 if (TREE_CODE (var
) == SSA_NAME
&& is_new_name (var
))
2555 bitmap set
= names_replaced_by (var
);
2557 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
2558 set_def_block (ssa_name (i
), bb
, is_phi_p
);
2564 /* Mark the use of VAR at STMT and BB as interesting for the
2565 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2569 mark_use_interesting (tree var
, gimple stmt
, basic_block bb
, bool insert_phi_p
)
2571 basic_block def_bb
= gimple_bb (stmt
);
2573 mark_block_for_update (def_bb
);
2574 mark_block_for_update (bb
);
2576 if (gimple_code (stmt
) == GIMPLE_PHI
)
2577 mark_phi_for_rewrite (def_bb
, stmt
);
2580 set_rewrite_uses (stmt
, true);
2582 if (is_gimple_debug (stmt
))
2586 /* If VAR has not been defined in BB, then it is live-on-entry
2587 to BB. Note that we cannot just use the block holding VAR's
2588 definition because if VAR is one of the names in OLD_SSA_NAMES,
2589 it will have several definitions (itself and all the names that
2593 struct def_blocks_d
*db_p
= get_def_blocks_for (var
);
2594 if (!bitmap_bit_p (db_p
->def_blocks
, bb
->index
))
2595 set_livein_block (var
, bb
);
2600 /* Do a dominator walk starting at BB processing statements that
2601 reference symbols in SYMS_TO_RENAME. This is very similar to
2602 mark_def_sites, but the scan handles statements whose operands may
2603 already be SSA names.
2605 If INSERT_PHI_P is true, mark those uses as live in the
2606 corresponding block. This is later used by the PHI placement
2607 algorithm to make PHI pruning decisions.
2609 FIXME. Most of this would be unnecessary if we could associate a
2610 symbol to all the SSA names that reference it. But that
2611 sounds like it would be expensive to maintain. Still, it
2612 would be interesting to see if it makes better sense to do
2616 prepare_block_for_update (basic_block bb
, bool insert_phi_p
)
2619 gimple_stmt_iterator si
;
2623 mark_block_for_update (bb
);
2625 /* Process PHI nodes marking interesting those that define or use
2626 the symbols that we are interested in. */
2627 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
2629 gimple phi
= gsi_stmt (si
);
2630 tree lhs_sym
, lhs
= gimple_phi_result (phi
);
2632 lhs_sym
= DECL_P (lhs
) ? lhs
: SSA_NAME_VAR (lhs
);
2634 if (!symbol_marked_for_renaming (lhs_sym
))
2637 mark_def_interesting (lhs_sym
, phi
, bb
, insert_phi_p
);
2639 /* Mark the uses in phi nodes as interesting. It would be more correct
2640 to process the arguments of the phi nodes of the successor edges of
2641 BB at the end of prepare_block_for_update, however, that turns out
2642 to be significantly more expensive. Doing it here is conservatively
2643 correct -- it may only cause us to believe a value to be live in a
2644 block that also contains its definition, and thus insert a few more
2645 phi nodes for it. */
2646 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2647 mark_use_interesting (lhs_sym
, phi
, e
->src
, insert_phi_p
);
2650 /* Process the statements. */
2651 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
2655 use_operand_p use_p
;
2656 def_operand_p def_p
;
2658 stmt
= gsi_stmt (si
);
2660 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, i
, SSA_OP_ALL_USES
)
2662 tree use
= USE_FROM_PTR (use_p
);
2663 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
2664 if (symbol_marked_for_renaming (sym
))
2665 mark_use_interesting (sym
, stmt
, bb
, insert_phi_p
);
2668 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, i
, SSA_OP_ALL_DEFS
)
2670 tree def
= DEF_FROM_PTR (def_p
);
2671 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
2672 if (symbol_marked_for_renaming (sym
))
2673 mark_def_interesting (sym
, stmt
, bb
, insert_phi_p
);
2677 /* Now visit all the blocks dominated by BB. */
2678 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
2680 son
= next_dom_son (CDI_DOMINATORS
, son
))
2681 prepare_block_for_update (son
, insert_phi_p
);
2685 /* Helper for prepare_names_to_update. Mark all the use sites for
2686 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2687 prepare_names_to_update. */
2690 prepare_use_sites_for (tree name
, bool insert_phi_p
)
2692 use_operand_p use_p
;
2693 imm_use_iterator iter
;
2695 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
2697 gimple stmt
= USE_STMT (use_p
);
2698 basic_block bb
= gimple_bb (stmt
);
2700 if (gimple_code (stmt
) == GIMPLE_PHI
)
2702 int ix
= PHI_ARG_INDEX_FROM_USE (use_p
);
2703 edge e
= gimple_phi_arg_edge (stmt
, ix
);
2704 mark_use_interesting (name
, stmt
, e
->src
, insert_phi_p
);
2708 /* For regular statements, mark this as an interesting use
2710 mark_use_interesting (name
, stmt
, bb
, insert_phi_p
);
2716 /* Helper for prepare_names_to_update. Mark the definition site for
2717 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2718 prepare_names_to_update. */
2721 prepare_def_site_for (tree name
, bool insert_phi_p
)
2726 gcc_assert (names_to_release
== NULL
2727 || !bitmap_bit_p (names_to_release
, SSA_NAME_VERSION (name
)));
2729 stmt
= SSA_NAME_DEF_STMT (name
);
2730 bb
= gimple_bb (stmt
);
2733 gcc_assert (bb
->index
< last_basic_block
);
2734 mark_block_for_update (bb
);
2735 mark_def_interesting (name
, stmt
, bb
, insert_phi_p
);
2740 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2741 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2742 PHI nodes for newly created names. */
2745 prepare_names_to_update (bool insert_phi_p
)
2749 sbitmap_iterator sbi
;
2751 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2752 remove it from NEW_SSA_NAMES so that we don't try to visit its
2753 defining basic block (which most likely doesn't exist). Notice
2754 that we cannot do the same with names in OLD_SSA_NAMES because we
2755 want to replace existing instances. */
2756 if (names_to_release
)
2757 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2758 RESET_BIT (new_ssa_names
, i
);
2760 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2761 names may be considered to be live-in on blocks that contain
2762 definitions for their replacements. */
2763 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
2764 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2766 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2767 OLD_SSA_NAMES, but we have to ignore its definition site. */
2768 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
2770 if (names_to_release
== NULL
|| !bitmap_bit_p (names_to_release
, i
))
2771 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2772 prepare_use_sites_for (ssa_name (i
), insert_phi_p
);
2777 /* Dump all the names replaced by NAME to FILE. */
2780 dump_names_replaced_by (FILE *file
, tree name
)
2786 print_generic_expr (file
, name
, 0);
2787 fprintf (file
, " -> { ");
2789 old_set
= names_replaced_by (name
);
2790 EXECUTE_IF_SET_IN_BITMAP (old_set
, 0, i
, bi
)
2792 print_generic_expr (file
, ssa_name (i
), 0);
2793 fprintf (file
, " ");
2796 fprintf (file
, "}\n");
2800 /* Dump all the names replaced by NAME to stderr. */
2803 debug_names_replaced_by (tree name
)
2805 dump_names_replaced_by (stderr
, name
);
2809 /* Dump SSA update information to FILE. */
2812 dump_update_ssa (FILE *file
)
2817 if (!need_ssa_update_p (cfun
))
2820 if (new_ssa_names
&& sbitmap_first_set_bit (new_ssa_names
) >= 0)
2822 sbitmap_iterator sbi
;
2824 fprintf (file
, "\nSSA replacement table\n");
2825 fprintf (file
, "N_i -> { O_1 ... O_j } means that N_i replaces "
2826 "O_1, ..., O_j\n\n");
2828 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
2829 dump_names_replaced_by (file
, ssa_name (i
));
2831 fprintf (file
, "\n");
2832 fprintf (file
, "Number of virtual NEW -> OLD mappings: %7u\n",
2833 update_ssa_stats
.num_virtual_mappings
);
2834 fprintf (file
, "Number of real NEW -> OLD mappings: %7u\n",
2835 update_ssa_stats
.num_total_mappings
2836 - update_ssa_stats
.num_virtual_mappings
);
2837 fprintf (file
, "Number of total NEW -> OLD mappings: %7u\n",
2838 update_ssa_stats
.num_total_mappings
);
2840 fprintf (file
, "\nNumber of virtual symbols: %u\n",
2841 update_ssa_stats
.num_virtual_symbols
);
2844 if (!bitmap_empty_p (SYMS_TO_RENAME (cfun
)))
2846 fprintf (file
, "\nSymbols to be put in SSA form\n");
2847 dump_decl_set (file
, SYMS_TO_RENAME (cfun
));
2848 fprintf (file
, "\n");
2851 if (names_to_release
&& !bitmap_empty_p (names_to_release
))
2853 fprintf (file
, "\nSSA names to release after updating the SSA web\n\n");
2854 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2856 print_generic_expr (file
, ssa_name (i
), 0);
2857 fprintf (file
, " ");
2859 fprintf (file
, "\n");
2864 /* Dump SSA update information to stderr. */
2867 debug_update_ssa (void)
2869 dump_update_ssa (stderr
);
2873 /* Initialize data structures used for incremental SSA updates. */
2876 init_update_ssa (struct function
*fn
)
2878 /* Reserve more space than the current number of names. The calls to
2879 add_new_name_mapping are typically done after creating new SSA
2880 names, so we'll need to reallocate these arrays. */
2881 old_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2882 sbitmap_zero (old_ssa_names
);
2884 new_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2885 sbitmap_zero (new_ssa_names
);
2887 repl_tbl
= htab_create (20, repl_map_hash
, repl_map_eq
, repl_map_free
);
2888 names_to_release
= NULL
;
2889 memset (&update_ssa_stats
, 0, sizeof (update_ssa_stats
));
2890 update_ssa_stats
.virtual_symbols
= BITMAP_ALLOC (NULL
);
2891 update_ssa_initialized_fn
= fn
;
2895 /* Deallocate data structures used for incremental SSA updates. */
2898 delete_update_ssa (void)
2903 sbitmap_free (old_ssa_names
);
2904 old_ssa_names
= NULL
;
2906 sbitmap_free (new_ssa_names
);
2907 new_ssa_names
= NULL
;
2909 htab_delete (repl_tbl
);
2912 bitmap_clear (SYMS_TO_RENAME (update_ssa_initialized_fn
));
2913 BITMAP_FREE (update_ssa_stats
.virtual_symbols
);
2915 if (names_to_release
)
2917 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2918 release_ssa_name (ssa_name (i
));
2919 BITMAP_FREE (names_to_release
);
2922 clear_ssa_name_info ();
2924 fini_ssa_renamer ();
2926 if (blocks_with_phis_to_rewrite
)
2927 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite
, 0, i
, bi
)
2929 gimple_vec phis
= VEC_index (gimple_vec
, phis_to_rewrite
, i
);
2931 VEC_free (gimple
, heap
, phis
);
2932 VEC_replace (gimple_vec
, phis_to_rewrite
, i
, NULL
);
2935 BITMAP_FREE (blocks_with_phis_to_rewrite
);
2936 BITMAP_FREE (blocks_to_update
);
2937 update_ssa_initialized_fn
= NULL
;
2941 /* Create a new name for OLD_NAME in statement STMT and replace the
2942 operand pointed to by DEF_P with the newly created name. Return
2943 the new name and register the replacement mapping <NEW, OLD> in
2944 update_ssa's tables. */
2947 create_new_def_for (tree old_name
, gimple stmt
, def_operand_p def
)
2949 tree new_name
= duplicate_ssa_name (old_name
, stmt
);
2951 SET_DEF (def
, new_name
);
2953 if (gimple_code (stmt
) == GIMPLE_PHI
)
2955 basic_block bb
= gimple_bb (stmt
);
2957 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2958 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name
) = bb_has_abnormal_pred (bb
);
2961 register_new_name_mapping (new_name
, old_name
);
2963 /* For the benefit of passes that will be updating the SSA form on
2964 their own, set the current reaching definition of OLD_NAME to be
2966 set_current_def (old_name
, new_name
);
2972 /* Register name NEW to be a replacement for name OLD. This function
2973 must be called for every replacement that should be performed by
2977 register_new_name_mapping (tree new_tree
, tree old
)
2979 if (!update_ssa_initialized_fn
)
2980 init_update_ssa (cfun
);
2982 gcc_assert (update_ssa_initialized_fn
== cfun
);
2984 add_new_name_mapping (new_tree
, old
);
2988 /* Register symbol SYM to be renamed by update_ssa. */
2991 mark_sym_for_renaming (tree sym
)
2993 bitmap_set_bit (SYMS_TO_RENAME (cfun
), DECL_UID (sym
));
2997 /* Register all the symbols in SET to be renamed by update_ssa. */
3000 mark_set_for_renaming (bitmap set
)
3005 if (set
== NULL
|| bitmap_empty_p (set
))
3008 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
3009 mark_sym_for_renaming (referenced_var (i
));
3013 /* Return true if there is any work to be done by update_ssa
3017 need_ssa_update_p (struct function
*fn
)
3019 gcc_assert (fn
!= NULL
);
3020 return (update_ssa_initialized_fn
== fn
3022 && !bitmap_empty_p (SYMS_TO_RENAME (fn
))));
3025 /* Return true if SSA name mappings have been registered for SSA updating. */
3028 name_mappings_registered_p (void)
3030 if (!update_ssa_initialized_fn
)
3033 gcc_assert (update_ssa_initialized_fn
== cfun
);
3035 return repl_tbl
&& htab_elements (repl_tbl
) > 0;
3038 /* Return true if name N has been registered in the replacement table. */
3041 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED
)
3043 if (!update_ssa_initialized_fn
)
3046 gcc_assert (update_ssa_initialized_fn
== cfun
);
3048 return is_new_name (n
) || is_old_name (n
);
3052 /* Mark NAME to be released after update_ssa has finished. */
3055 release_ssa_name_after_update_ssa (tree name
)
3057 gcc_assert (cfun
&& update_ssa_initialized_fn
== cfun
);
3059 if (names_to_release
== NULL
)
3060 names_to_release
= BITMAP_ALLOC (NULL
);
3062 bitmap_set_bit (names_to_release
, SSA_NAME_VERSION (name
));
3066 /* Insert new PHI nodes to replace VAR. DFS contains dominance
3067 frontier information. BLOCKS is the set of blocks to be updated.
3069 This is slightly different than the regular PHI insertion
3070 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
3071 real names (i.e., GIMPLE registers) are inserted:
3073 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
3074 nodes inside the region affected by the block that defines VAR
3075 and the blocks that define all its replacements. All these
3076 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
3078 First, we compute the entry point to the region (ENTRY). This is
3079 given by the nearest common dominator to all the definition
3080 blocks. When computing the iterated dominance frontier (IDF), any
3081 block not strictly dominated by ENTRY is ignored.
3083 We then call the standard PHI insertion algorithm with the pruned
3086 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
3087 names is not pruned. PHI nodes are inserted at every IDF block. */
3090 insert_updated_phi_nodes_for (tree var
, bitmap_head
*dfs
, bitmap blocks
,
3091 unsigned update_flags
)
3094 struct def_blocks_d
*db
;
3095 bitmap idf
, pruned_idf
;
3099 if (TREE_CODE (var
) == SSA_NAME
)
3100 gcc_checking_assert (is_old_name (var
));
3102 gcc_checking_assert (symbol_marked_for_renaming (var
));
3104 /* Get all the definition sites for VAR. */
3105 db
= find_def_blocks_for (var
);
3107 /* No need to do anything if there were no definitions to VAR. */
3108 if (db
== NULL
|| bitmap_empty_p (db
->def_blocks
))
3111 /* Compute the initial iterated dominance frontier. */
3112 idf
= compute_idf (db
->def_blocks
, dfs
);
3113 pruned_idf
= BITMAP_ALLOC (NULL
);
3115 if (TREE_CODE (var
) == SSA_NAME
)
3117 if (update_flags
== TODO_update_ssa
)
3119 /* If doing regular SSA updates for GIMPLE registers, we are
3120 only interested in IDF blocks dominated by the nearest
3121 common dominator of all the definition blocks. */
3122 entry
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3124 if (entry
!= ENTRY_BLOCK_PTR
)
3125 EXECUTE_IF_SET_IN_BITMAP (idf
, 0, i
, bi
)
3126 if (BASIC_BLOCK (i
) != entry
3127 && dominated_by_p (CDI_DOMINATORS
, BASIC_BLOCK (i
), entry
))
3128 bitmap_set_bit (pruned_idf
, i
);
3132 /* Otherwise, do not prune the IDF for VAR. */
3133 gcc_assert (update_flags
== TODO_update_ssa_full_phi
);
3134 bitmap_copy (pruned_idf
, idf
);
3139 /* Otherwise, VAR is a symbol that needs to be put into SSA form
3140 for the first time, so we need to compute the full IDF for
3142 bitmap_copy (pruned_idf
, idf
);
3145 if (!bitmap_empty_p (pruned_idf
))
3147 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3148 are included in the region to be updated. The feeding blocks
3149 are important to guarantee that the PHI arguments are renamed
3152 /* FIXME, this is not needed if we are updating symbols. We are
3153 already starting at the ENTRY block anyway. */
3154 bitmap_ior_into (blocks
, pruned_idf
);
3155 EXECUTE_IF_SET_IN_BITMAP (pruned_idf
, 0, i
, bi
)
3159 basic_block bb
= BASIC_BLOCK (i
);
3161 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3162 if (e
->src
->index
>= 0)
3163 bitmap_set_bit (blocks
, e
->src
->index
);
3166 insert_phi_nodes_for (var
, pruned_idf
, true);
3169 BITMAP_FREE (pruned_idf
);
3174 /* Heuristic to determine whether SSA name mappings for virtual names
3175 should be discarded and their symbols rewritten from scratch. When
3176 there is a large number of mappings for virtual names, the
3177 insertion of PHI nodes for the old names in the mappings takes
3178 considerable more time than if we inserted PHI nodes for the
3181 Currently the heuristic takes these stats into account:
3183 - Number of mappings for virtual SSA names.
3184 - Number of distinct virtual symbols involved in those mappings.
3186 If the number of virtual mappings is much larger than the number of
3187 virtual symbols, then it will be faster to compute PHI insertion
3188 spots for the symbols. Even if this involves traversing the whole
3189 CFG, which is what happens when symbols are renamed from scratch. */
3192 switch_virtuals_to_full_rewrite_p (void)
3194 if (update_ssa_stats
.num_virtual_mappings
< (unsigned) MIN_VIRTUAL_MAPPINGS
)
3197 if (update_ssa_stats
.num_virtual_mappings
3198 > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
3199 * update_ssa_stats
.num_virtual_symbols
)
3206 /* Remove every virtual mapping and mark all the affected virtual
3207 symbols for renaming. */
3210 switch_virtuals_to_full_rewrite (void)
3213 sbitmap_iterator sbi
;
3217 fprintf (dump_file
, "\nEnabled virtual name mapping heuristic.\n");
3218 fprintf (dump_file
, "\tNumber of virtual mappings: %7u\n",
3219 update_ssa_stats
.num_virtual_mappings
);
3220 fprintf (dump_file
, "\tNumber of unique virtual symbols: %7u\n",
3221 update_ssa_stats
.num_virtual_symbols
);
3222 fprintf (dump_file
, "Updating FUD-chains from top of CFG will be "
3223 "faster than processing\nthe name mappings.\n\n");
3226 /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
3227 Note that it is not really necessary to remove the mappings from
3228 REPL_TBL, that would only waste time. */
3229 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
3230 if (!is_gimple_reg (ssa_name (i
)))
3231 RESET_BIT (new_ssa_names
, i
);
3233 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
3234 if (!is_gimple_reg (ssa_name (i
)))
3235 RESET_BIT (old_ssa_names
, i
);
3237 mark_set_for_renaming (update_ssa_stats
.virtual_symbols
);
3241 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3242 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3244 1- The names in OLD_SSA_NAMES dominated by the definitions of
3245 NEW_SSA_NAMES are all re-written to be reached by the
3246 appropriate definition from NEW_SSA_NAMES.
3248 2- If needed, new PHI nodes are added to the iterated dominance
3249 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3251 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3252 calling register_new_name_mapping for every pair of names that the
3253 caller wants to replace.
3255 The caller identifies the new names that have been inserted and the
3256 names that need to be replaced by calling register_new_name_mapping
3257 for every pair <NEW, OLD>. Note that the function assumes that the
3258 new names have already been inserted in the IL.
3260 For instance, given the following code:
3263 2 x_1 = PHI (0, x_5)
3274 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3277 2 x_1 = PHI (0, x_5)
3290 We want to replace all the uses of x_1 with the new definitions of
3291 x_10 and x_11. Note that the only uses that should be replaced are
3292 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3293 *not* be replaced (this is why we cannot just mark symbol 'x' for
3296 Additionally, we may need to insert a PHI node at line 11 because
3297 that is a merge point for x_10 and x_11. So the use of x_1 at line
3298 11 will be replaced with the new PHI node. The insertion of PHI
3299 nodes is optional. They are not strictly necessary to preserve the
3300 SSA form, and depending on what the caller inserted, they may not
3301 even be useful for the optimizers. UPDATE_FLAGS controls various
3302 aspects of how update_ssa operates, see the documentation for
3303 TODO_update_ssa*. */
3306 update_ssa (unsigned update_flags
)
3308 basic_block bb
, start_bb
;
3312 sbitmap_iterator sbi
;
3314 if (!need_ssa_update_p (cfun
))
3317 timevar_push (TV_TREE_SSA_INCREMENTAL
);
3319 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3320 fprintf (dump_file
, "\nUpdating SSA:\n");
3322 if (!update_ssa_initialized_fn
)
3323 init_update_ssa (cfun
);
3324 gcc_assert (update_ssa_initialized_fn
== cfun
);
3326 blocks_with_phis_to_rewrite
= BITMAP_ALLOC (NULL
);
3327 if (!phis_to_rewrite
)
3328 phis_to_rewrite
= VEC_alloc (gimple_vec
, heap
, last_basic_block
);
3329 blocks_to_update
= BITMAP_ALLOC (NULL
);
3331 /* Ensure that the dominance information is up-to-date. */
3332 calculate_dominance_info (CDI_DOMINATORS
);
3334 /* Only one update flag should be set. */
3335 gcc_assert (update_flags
== TODO_update_ssa
3336 || update_flags
== TODO_update_ssa_no_phi
3337 || update_flags
== TODO_update_ssa_full_phi
3338 || update_flags
== TODO_update_ssa_only_virtuals
);
3340 /* If we only need to update virtuals, remove all the mappings for
3341 real names before proceeding. The caller is responsible for
3342 having dealt with the name mappings before calling update_ssa. */
3343 if (update_flags
== TODO_update_ssa_only_virtuals
)
3345 sbitmap_zero (old_ssa_names
);
3346 sbitmap_zero (new_ssa_names
);
3347 htab_empty (repl_tbl
);
3350 insert_phi_p
= (update_flags
!= TODO_update_ssa_no_phi
);
3354 /* If the caller requested PHI nodes to be added, initialize
3355 live-in information data structures (DEF_BLOCKS). */
3357 /* For each SSA name N, the DEF_BLOCKS table describes where the
3358 name is defined, which blocks have PHI nodes for N, and which
3359 blocks have uses of N (i.e., N is live-on-entry in those
3361 def_blocks
= htab_create (num_ssa_names
, def_blocks_hash
,
3362 def_blocks_eq
, def_blocks_free
);
3369 /* Heuristic to avoid massive slow downs when the replacement
3370 mappings include lots of virtual names. */
3371 if (insert_phi_p
&& switch_virtuals_to_full_rewrite_p ())
3372 switch_virtuals_to_full_rewrite ();
3374 /* If there are names defined in the replacement table, prepare
3375 definition and use sites for all the names in NEW_SSA_NAMES and
3377 if (sbitmap_first_set_bit (new_ssa_names
) >= 0)
3379 prepare_names_to_update (insert_phi_p
);
3381 /* If all the names in NEW_SSA_NAMES had been marked for
3382 removal, and there are no symbols to rename, then there's
3383 nothing else to do. */
3384 if (sbitmap_first_set_bit (new_ssa_names
) < 0
3385 && bitmap_empty_p (SYMS_TO_RENAME (cfun
)))
3389 /* Next, determine the block at which to start the renaming process. */
3390 if (!bitmap_empty_p (SYMS_TO_RENAME (cfun
)))
3392 /* If we have to rename some symbols from scratch, we need to
3393 start the process at the root of the CFG. FIXME, it should
3394 be possible to determine the nearest block that had a
3395 definition for each of the symbols that are marked for
3396 updating. For now this seems more work than it's worth. */
3397 start_bb
= ENTRY_BLOCK_PTR
;
3399 /* Traverse the CFG looking for existing definitions and uses of
3400 symbols in SYMS_TO_RENAME. Mark interesting blocks and
3401 statements and set local live-in information for the PHI
3402 placement heuristics. */
3403 prepare_block_for_update (start_bb
, insert_phi_p
);
3407 /* Otherwise, the entry block to the region is the nearest
3408 common dominator for the blocks in BLOCKS. */
3409 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3413 /* If requested, insert PHI nodes at the iterated dominance frontier
3414 of every block, creating new definitions for names in OLD_SSA_NAMES
3415 and for symbols in SYMS_TO_RENAME. */
3420 /* If the caller requested PHI nodes to be added, compute
3421 dominance frontiers. */
3422 dfs
= XNEWVEC (bitmap_head
, last_basic_block
);
3424 bitmap_initialize (&dfs
[bb
->index
], &bitmap_default_obstack
);
3425 compute_dominance_frontiers (dfs
);
3427 if (sbitmap_first_set_bit (old_ssa_names
) >= 0)
3429 sbitmap_iterator sbi
;
3431 /* insert_update_phi_nodes_for will call add_new_name_mapping
3432 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3433 will grow while we are traversing it (but it will not
3434 gain any new members). Copy OLD_SSA_NAMES to a temporary
3436 sbitmap tmp
= sbitmap_alloc (old_ssa_names
->n_bits
);
3437 sbitmap_copy (tmp
, old_ssa_names
);
3438 EXECUTE_IF_SET_IN_SBITMAP (tmp
, 0, i
, sbi
)
3439 insert_updated_phi_nodes_for (ssa_name (i
), dfs
, blocks_to_update
,
3444 EXECUTE_IF_SET_IN_BITMAP (SYMS_TO_RENAME (cfun
), 0, i
, bi
)
3445 insert_updated_phi_nodes_for (referenced_var (i
), dfs
, blocks_to_update
,
3449 bitmap_clear (&dfs
[bb
->index
]);
3452 /* Insertion of PHI nodes may have added blocks to the region.
3453 We need to re-compute START_BB to include the newly added
3455 if (start_bb
!= ENTRY_BLOCK_PTR
)
3456 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3460 /* Reset the current definition for name and symbol before renaming
3462 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
3463 set_current_def (ssa_name (i
), NULL_TREE
);
3465 EXECUTE_IF_SET_IN_BITMAP (SYMS_TO_RENAME (cfun
), 0, i
, bi
)
3466 set_current_def (referenced_var (i
), NULL_TREE
);
3468 /* Now start the renaming process at START_BB. */
3469 interesting_blocks
= sbitmap_alloc (last_basic_block
);
3470 sbitmap_zero (interesting_blocks
);
3471 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3472 SET_BIT (interesting_blocks
, i
);
3474 rewrite_blocks (start_bb
, REWRITE_UPDATE
);
3476 sbitmap_free (interesting_blocks
);
3478 /* Debugging dumps. */
3484 dump_update_ssa (dump_file
);
3486 fprintf (dump_file
, "Incremental SSA update started at block: %d\n",
3490 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3492 fprintf (dump_file
, "Number of blocks in CFG: %d\n", last_basic_block
);
3493 fprintf (dump_file
, "Number of blocks to update: %d (%3.0f%%)\n",
3494 c
, PERCENT (c
, last_basic_block
));
3496 if (dump_flags
& TDF_DETAILS
)
3498 fprintf (dump_file
, "Affected blocks:");
3499 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3500 fprintf (dump_file
, " %u", i
);
3501 fprintf (dump_file
, "\n");
3504 fprintf (dump_file
, "\n\n");
3507 /* Free allocated memory. */
3509 delete_update_ssa ();
3511 timevar_pop (TV_TREE_SSA_INCREMENTAL
);