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. */
55 /* Blocks that contain definitions of VAR. Bit I will be set if the
56 Ith block contains a definition of VAR. */
59 /* Blocks that contain a PHI node for VAR. */
62 /* Blocks where VAR is live-on-entry. Similar semantics as
67 typedef struct def_blocks_d
*def_blocks_p
;
70 /* Stack of trees used to restore the global currdefs to its original
71 state after completing rewriting of a block and its dominator
72 children. Its elements have the following properties:
74 - An SSA_NAME (N) indicates that the current definition of the
75 underlying variable should be set to the given SSA_NAME. If the
76 symbol associated with the SSA_NAME is not a GIMPLE register, the
77 next slot in the stack must be a _DECL node (SYM). In this case,
78 the name N in the previous slot is the current reaching
81 - A _DECL node indicates that the underlying variable has no
84 - A NULL node at the top entry is used to mark the last slot
85 associated with the current block. */
86 static VEC(tree
,heap
) *block_defs_stack
;
89 /* Set of existing SSA names being replaced by update_ssa. */
90 static sbitmap old_ssa_names
;
92 /* Set of new SSA names being added by update_ssa. Note that both
93 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
94 the operations done on them are presence tests. */
95 static sbitmap new_ssa_names
;
97 sbitmap interesting_blocks
;
99 /* Set of SSA names that have been marked to be released after they
100 were registered in the replacement table. They will be finally
101 released after we finish updating the SSA web. */
102 static bitmap names_to_release
;
104 /* VEC of VECs of PHIs to rewrite in a basic block. Element I corresponds
105 the to basic block with index I. Allocated once per compilation, *not*
106 released between different functions. */
107 static VEC(gimple_vec
, heap
) *phis_to_rewrite
;
109 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
110 static bitmap blocks_with_phis_to_rewrite
;
112 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
113 to grow as the callers to register_new_name_mapping will typically
114 create new names on the fly. FIXME. Currently set to 1/3 to avoid
115 frequent reallocations but still need to find a reasonable growth
117 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
120 /* The function the SSA updating data structures have been initialized for.
121 NULL if they need to be initialized by register_new_name_mapping. */
122 static struct function
*update_ssa_initialized_fn
= NULL
;
124 /* Global data to attach to the main dominator walk structure. */
125 struct mark_def_sites_global_data
127 /* This bitmap contains the variables which are set before they
128 are used in a basic block. */
132 /* Information stored for both SSA names and decls. */
135 /* This field indicates whether or not the variable may need PHI nodes.
136 See the enum's definition for more detailed information about the
138 ENUM_BITFIELD (need_phi_state
) need_phi_state
: 2;
140 /* The current reaching definition replacing this var. */
143 /* Definitions for this var. */
144 struct def_blocks_d def_blocks
;
147 /* The information associated with decls and SSA names. */
148 typedef struct common_info_d
*common_info_p
;
150 /* Information stored for decls. */
156 /* Information stored for both SSA names and decls. */
157 struct common_info_d info
;
160 /* The information associated with decls. */
161 typedef struct var_info_d
*var_info_p
;
163 DEF_VEC_P(var_info_p
);
164 DEF_VEC_ALLOC_P(var_info_p
,heap
);
166 /* Each entry in VAR_INFOS contains an element of type STRUCT
168 static htab_t var_infos
;
171 /* Information stored for SSA names. */
174 /* Age of this record (so that info_for_ssa_name table can be cleared
175 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
176 are assumed to be null. */
179 /* Replacement mappings, allocated from update_ssa_obstack. */
182 /* Information stored for both SSA names and decls. */
183 struct common_info_d info
;
186 /* The information associated with names. */
187 typedef struct ssa_name_info
*ssa_name_info_p
;
188 DEF_VEC_P (ssa_name_info_p
);
189 DEF_VEC_ALLOC_P (ssa_name_info_p
, heap
);
191 static VEC(ssa_name_info_p
, heap
) *info_for_ssa_name
;
192 static unsigned current_info_for_ssa_name_age
;
194 static bitmap_obstack update_ssa_obstack
;
196 /* The set of blocks affected by update_ssa. */
197 static bitmap blocks_to_update
;
199 /* The main entry point to the SSA renamer (rewrite_blocks) may be
200 called several times to do different, but related, tasks.
201 Initially, we need it to rename the whole program into SSA form.
202 At other times, we may need it to only rename into SSA newly
203 exposed symbols. Finally, we can also call it to incrementally fix
204 an already built SSA web. */
206 /* Convert the whole function into SSA form. */
209 /* Incrementally update the SSA web by replacing existing SSA
210 names with new ones. See update_ssa for details. */
217 /* Prototypes for debugging functions. */
218 extern void dump_tree_ssa (FILE *);
219 extern void debug_tree_ssa (void);
220 extern void debug_def_blocks (void);
221 extern void dump_tree_ssa_stats (FILE *);
222 extern void debug_tree_ssa_stats (void);
223 extern void dump_update_ssa (FILE *);
224 extern void debug_update_ssa (void);
225 extern void dump_names_replaced_by (FILE *, tree
);
226 extern void debug_names_replaced_by (tree
);
227 extern void dump_var_infos (FILE *);
228 extern void debug_var_infos (void);
229 extern void dump_defs_stack (FILE *, int);
230 extern void debug_defs_stack (int);
231 extern void dump_currdefs (FILE *);
232 extern void debug_currdefs (void);
235 /* The set of symbols we ought to re-write into SSA form in update_ssa. */
236 static bitmap symbols_to_rename_set
;
237 static VEC(tree
,heap
) *symbols_to_rename
;
239 /* Mark SYM for renaming. */
242 mark_for_renaming (tree sym
)
244 if (!symbols_to_rename_set
)
245 symbols_to_rename_set
= BITMAP_ALLOC (NULL
);
246 if (bitmap_set_bit (symbols_to_rename_set
, DECL_UID (sym
)))
247 VEC_safe_push (tree
, heap
, symbols_to_rename
, sym
);
250 /* Return true if SYM is marked for renaming. */
253 marked_for_renaming (tree sym
)
255 if (!symbols_to_rename_set
|| sym
== NULL_TREE
)
257 return bitmap_bit_p (symbols_to_rename_set
, DECL_UID (sym
));
261 /* Return true if STMT needs to be rewritten. When renaming a subset
262 of the variables, not all statements will be processed. This is
263 decided in mark_def_sites. */
266 rewrite_uses_p (gimple stmt
)
268 return gimple_visited_p (stmt
);
272 /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
275 set_rewrite_uses (gimple stmt
, bool rewrite_p
)
277 gimple_set_visited (stmt
, rewrite_p
);
281 /* Return true if the DEFs created by statement STMT should be
282 registered when marking new definition sites. This is slightly
283 different than rewrite_uses_p: it's used by update_ssa to
284 distinguish statements that need to have both uses and defs
285 processed from those that only need to have their defs processed.
286 Statements that define new SSA names only need to have their defs
287 registered, but they don't need to have their uses renamed. */
290 register_defs_p (gimple stmt
)
292 return gimple_plf (stmt
, GF_PLF_1
) != 0;
296 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
299 set_register_defs (gimple stmt
, bool register_defs_p
)
301 gimple_set_plf (stmt
, GF_PLF_1
, register_defs_p
);
305 /* Get the information associated with NAME. */
307 static inline ssa_name_info_p
308 get_ssa_name_ann (tree name
)
310 unsigned ver
= SSA_NAME_VERSION (name
);
311 unsigned len
= VEC_length (ssa_name_info_p
, info_for_ssa_name
);
312 struct ssa_name_info
*info
;
316 unsigned old_len
= VEC_length (ssa_name_info_p
, info_for_ssa_name
);
317 unsigned new_len
= num_ssa_names
;
319 VEC_reserve (ssa_name_info_p
, heap
, info_for_ssa_name
,
321 while (len
++ < new_len
)
323 struct ssa_name_info
*info
= XCNEW (struct ssa_name_info
);
324 info
->age
= current_info_for_ssa_name_age
;
325 VEC_quick_push (ssa_name_info_p
, info_for_ssa_name
, info
);
329 info
= VEC_index (ssa_name_info_p
, info_for_ssa_name
, ver
);
330 if (info
->age
< current_info_for_ssa_name_age
)
332 info
->age
= current_info_for_ssa_name_age
;
333 info
->repl_set
= NULL
;
334 info
->info
.need_phi_state
= NEED_PHI_STATE_UNKNOWN
;
335 info
->info
.current_def
= NULL_TREE
;
336 info
->info
.def_blocks
.def_blocks
= NULL
;
337 info
->info
.def_blocks
.phi_blocks
= NULL
;
338 info
->info
.def_blocks
.livein_blocks
= NULL
;
344 /* Return and allocate the auxiliar information for DECL. */
346 static inline var_info_p
347 get_var_info (tree decl
)
349 struct var_info_d vi
;
352 slot
= htab_find_slot_with_hash (var_infos
, &vi
, DECL_UID (decl
), INSERT
);
355 var_info_p v
= XCNEW (struct var_info_d
);
360 return (var_info_p
) *slot
;
364 /* Clears info for SSA names. */
367 clear_ssa_name_info (void)
369 current_info_for_ssa_name_age
++;
371 /* If current_info_for_ssa_name_age wraps we use stale information.
372 Asser that this does not happen. */
373 gcc_assert (current_info_for_ssa_name_age
!= 0);
377 /* Get access to the auxiliar information stored per SSA name or decl. */
379 static inline common_info_p
380 get_common_info (tree var
)
382 if (TREE_CODE (var
) == SSA_NAME
)
383 return &get_ssa_name_ann (var
)->info
;
385 return &get_var_info (var
)->info
;
389 /* Return the current definition for VAR. */
392 get_current_def (tree var
)
394 return get_common_info (var
)->current_def
;
398 /* Sets current definition of VAR to DEF. */
401 set_current_def (tree var
, tree def
)
403 get_common_info (var
)->current_def
= def
;
407 /* Compute global livein information given the set of blocks where
408 an object is locally live at the start of the block (LIVEIN)
409 and the set of blocks where the object is defined (DEF_BLOCKS).
411 Note: This routine augments the existing local livein information
412 to include global livein (i.e., it modifies the underlying bitmap
416 compute_global_livein (bitmap livein
, bitmap def_blocks
)
420 VEC (basic_block
, heap
) *worklist
;
422 /* Normally the work list size is bounded by the number of basic
423 blocks in the largest loop. We don't know this number, but we
424 can be fairly sure that it will be relatively small. */
425 worklist
= VEC_alloc (basic_block
, heap
, MAX (8, n_basic_blocks
/ 128));
427 EXECUTE_IF_SET_IN_BITMAP (livein
, 0, i
, bi
)
428 VEC_safe_push (basic_block
, heap
, worklist
, BASIC_BLOCK (i
));
430 /* Iterate until the worklist is empty. */
431 while (! VEC_empty (basic_block
, worklist
))
436 /* Pull a block off the worklist. */
437 basic_block bb
= VEC_pop (basic_block
, worklist
);
439 /* Make sure we have at least enough room in the work list
440 for all predecessors of this block. */
441 VEC_reserve (basic_block
, heap
, worklist
, EDGE_COUNT (bb
->preds
));
443 /* For each predecessor block. */
444 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
446 basic_block pred
= e
->src
;
447 int pred_index
= pred
->index
;
449 /* None of this is necessary for the entry block. */
450 if (pred
!= ENTRY_BLOCK_PTR
451 && ! bitmap_bit_p (def_blocks
, pred_index
)
452 && bitmap_set_bit (livein
, pred_index
))
454 VEC_quick_push (basic_block
, worklist
, pred
);
459 VEC_free (basic_block
, heap
, worklist
);
463 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
464 all statements in basic block BB. */
467 initialize_flags_in_bb (basic_block bb
)
470 gimple_stmt_iterator gsi
;
472 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
474 gimple phi
= gsi_stmt (gsi
);
475 set_rewrite_uses (phi
, false);
476 set_register_defs (phi
, false);
479 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
481 stmt
= gsi_stmt (gsi
);
483 /* We are going to use the operand cache API, such as
484 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
485 cache for each statement should be up-to-date. */
486 gcc_assert (!gimple_modified_p (stmt
));
487 set_rewrite_uses (stmt
, false);
488 set_register_defs (stmt
, false);
492 /* Mark block BB as interesting for update_ssa. */
495 mark_block_for_update (basic_block bb
)
497 gcc_assert (blocks_to_update
!= NULL
);
498 if (!bitmap_set_bit (blocks_to_update
, bb
->index
))
500 initialize_flags_in_bb (bb
);
503 /* Return the set of blocks where variable VAR is defined and the blocks
504 where VAR is live on entry (livein). If no entry is found in
505 DEF_BLOCKS, a new one is created and returned. */
507 static inline struct def_blocks_d
*
508 get_def_blocks_for (common_info_p info
)
510 struct def_blocks_d
*db_p
= &info
->def_blocks
;
511 if (!db_p
->def_blocks
)
513 db_p
->def_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
514 db_p
->phi_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
515 db_p
->livein_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
522 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
523 VAR is defined by a PHI node. */
526 set_def_block (tree var
, basic_block bb
, bool phi_p
)
528 struct def_blocks_d
*db_p
;
531 info
= get_common_info (var
);
532 db_p
= get_def_blocks_for (info
);
534 /* Set the bit corresponding to the block where VAR is defined. */
535 bitmap_set_bit (db_p
->def_blocks
, bb
->index
);
537 bitmap_set_bit (db_p
->phi_blocks
, bb
->index
);
539 /* Keep track of whether or not we may need to insert PHI nodes.
541 If we are in the UNKNOWN state, then this is the first definition
542 of VAR. Additionally, we have not seen any uses of VAR yet, so
543 we do not need a PHI node for this variable at this time (i.e.,
544 transition to NEED_PHI_STATE_NO).
546 If we are in any other state, then we either have multiple definitions
547 of this variable occurring in different blocks or we saw a use of the
548 variable which was not dominated by the block containing the
549 definition(s). In this case we may need a PHI node, so enter
550 state NEED_PHI_STATE_MAYBE. */
551 if (info
->need_phi_state
== NEED_PHI_STATE_UNKNOWN
)
552 info
->need_phi_state
= NEED_PHI_STATE_NO
;
554 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
558 /* Mark block BB as having VAR live at the entry to BB. */
561 set_livein_block (tree var
, basic_block bb
)
564 struct def_blocks_d
*db_p
;
566 info
= get_common_info (var
);
567 db_p
= get_def_blocks_for (info
);
569 /* Set the bit corresponding to the block where VAR is live in. */
570 bitmap_set_bit (db_p
->livein_blocks
, bb
->index
);
572 /* Keep track of whether or not we may need to insert PHI nodes.
574 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
575 by the single block containing the definition(s) of this variable. If
576 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
577 NEED_PHI_STATE_MAYBE. */
578 if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
580 int def_block_index
= bitmap_first_set_bit (db_p
->def_blocks
);
582 if (def_block_index
== -1
583 || ! dominated_by_p (CDI_DOMINATORS
, bb
,
584 BASIC_BLOCK (def_block_index
)))
585 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
588 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
592 /* Return true if NAME is in OLD_SSA_NAMES. */
595 is_old_name (tree name
)
597 unsigned ver
= SSA_NAME_VERSION (name
);
600 return (ver
< SBITMAP_SIZE (new_ssa_names
)
601 && TEST_BIT (old_ssa_names
, ver
));
605 /* Return true if NAME is in NEW_SSA_NAMES. */
608 is_new_name (tree name
)
610 unsigned ver
= SSA_NAME_VERSION (name
);
613 return (ver
< SBITMAP_SIZE (new_ssa_names
)
614 && TEST_BIT (new_ssa_names
, ver
));
618 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
621 names_replaced_by (tree new_tree
)
623 return get_ssa_name_ann (new_tree
)->repl_set
;
627 /* Add OLD to REPL_TBL[NEW_TREE].SET. */
630 add_to_repl_tbl (tree new_tree
, tree old
)
632 bitmap
*set
= &get_ssa_name_ann (new_tree
)->repl_set
;
634 *set
= BITMAP_ALLOC (&update_ssa_obstack
);
635 bitmap_set_bit (*set
, SSA_NAME_VERSION (old
));
639 /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
640 represents the set of names O_1 ... O_j replaced by N_i. This is
641 used by update_ssa and its helpers to introduce new SSA names in an
642 already formed SSA web. */
645 add_new_name_mapping (tree new_tree
, tree old
)
647 timevar_push (TV_TREE_SSA_INCREMENTAL
);
649 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
650 gcc_assert (new_tree
!= old
&& SSA_NAME_VAR (new_tree
) == SSA_NAME_VAR (old
));
652 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
653 caller may have created new names since the set was created. */
654 if (SBITMAP_SIZE (new_ssa_names
) <= num_ssa_names
- 1)
656 unsigned int new_sz
= num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
;
657 new_ssa_names
= sbitmap_resize (new_ssa_names
, new_sz
, 0);
658 old_ssa_names
= sbitmap_resize (old_ssa_names
, new_sz
, 0);
661 /* Update the REPL_TBL table. */
662 add_to_repl_tbl (new_tree
, old
);
664 /* If OLD had already been registered as a new name, then all the
665 names that OLD replaces should also be replaced by NEW_TREE. */
666 if (is_new_name (old
))
667 bitmap_ior_into (names_replaced_by (new_tree
), names_replaced_by (old
));
669 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
671 SET_BIT (new_ssa_names
, SSA_NAME_VERSION (new_tree
));
672 SET_BIT (old_ssa_names
, SSA_NAME_VERSION (old
));
674 timevar_pop (TV_TREE_SSA_INCREMENTAL
);
678 /* Call back for walk_dominator_tree used to collect definition sites
679 for every variable in the function. For every statement S in block
682 1- Variables defined by S in the DEFS of S are marked in the bitmap
685 2- If S uses a variable VAR and there is no preceding kill of VAR,
686 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
688 This information is used to determine which variables are live
689 across block boundaries to reduce the number of PHI nodes
693 mark_def_sites (basic_block bb
, gimple stmt
, bitmap kills
)
699 /* Since this is the first time that we rewrite the program into SSA
700 form, force an operand scan on every statement. */
703 gcc_assert (blocks_to_update
== NULL
);
704 set_register_defs (stmt
, false);
705 set_rewrite_uses (stmt
, false);
707 if (is_gimple_debug (stmt
))
709 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
711 tree sym
= USE_FROM_PTR (use_p
);
712 gcc_assert (DECL_P (sym
));
713 set_rewrite_uses (stmt
, true);
715 if (rewrite_uses_p (stmt
))
716 SET_BIT (interesting_blocks
, bb
->index
);
720 /* If a variable is used before being set, then the variable is live
721 across a block boundary, so mark it live-on-entry to BB. */
722 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
724 tree sym
= USE_FROM_PTR (use_p
);
725 gcc_assert (DECL_P (sym
));
726 if (!bitmap_bit_p (kills
, DECL_UID (sym
)))
727 set_livein_block (sym
, bb
);
728 set_rewrite_uses (stmt
, true);
731 /* Now process the defs. Mark BB as the definition block and add
732 each def to the set of killed symbols. */
733 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
735 gcc_assert (DECL_P (def
));
736 set_def_block (def
, bb
, false);
737 bitmap_set_bit (kills
, DECL_UID (def
));
738 set_register_defs (stmt
, true);
741 /* If we found the statement interesting then also mark the block BB
743 if (rewrite_uses_p (stmt
) || register_defs_p (stmt
))
744 SET_BIT (interesting_blocks
, bb
->index
);
747 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
748 in the dfs numbering of the dominance tree. */
752 /* Basic block whose index this entry corresponds to. */
755 /* The dfs number of this node. */
759 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
763 cmp_dfsnum (const void *a
, const void *b
)
765 const struct dom_dfsnum
*const da
= (const struct dom_dfsnum
*) a
;
766 const struct dom_dfsnum
*const db
= (const struct dom_dfsnum
*) b
;
768 return (int) da
->dfs_num
- (int) db
->dfs_num
;
771 /* Among the intervals starting at the N points specified in DEFS, find
772 the one that contains S, and return its bb_index. */
775 find_dfsnum_interval (struct dom_dfsnum
*defs
, unsigned n
, unsigned s
)
777 unsigned f
= 0, t
= n
, m
;
782 if (defs
[m
].dfs_num
<= s
)
788 return defs
[f
].bb_index
;
791 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
792 KILLS is a bitmap of blocks where the value is defined before any use. */
795 prune_unused_phi_nodes (bitmap phis
, bitmap kills
, bitmap uses
)
797 VEC(int, heap
) *worklist
;
799 unsigned i
, b
, p
, u
, top
;
801 basic_block def_bb
, use_bb
;
805 struct dom_dfsnum
*defs
;
806 unsigned n_defs
, adef
;
808 if (bitmap_empty_p (uses
))
814 /* The phi must dominate a use, or an argument of a live phi. Also, we
815 do not create any phi nodes in def blocks, unless they are also livein. */
816 to_remove
= BITMAP_ALLOC (NULL
);
817 bitmap_and_compl (to_remove
, kills
, uses
);
818 bitmap_and_compl_into (phis
, to_remove
);
819 if (bitmap_empty_p (phis
))
821 BITMAP_FREE (to_remove
);
825 /* We want to remove the unnecessary phi nodes, but we do not want to compute
826 liveness information, as that may be linear in the size of CFG, and if
827 there are lot of different variables to rewrite, this may lead to quadratic
830 Instead, we basically emulate standard dce. We put all uses to worklist,
831 then for each of them find the nearest def that dominates them. If this
832 def is a phi node, we mark it live, and if it was not live before, we
833 add the predecessors of its basic block to the worklist.
835 To quickly locate the nearest def that dominates use, we use dfs numbering
836 of the dominance tree (that is already available in order to speed up
837 queries). For each def, we have the interval given by the dfs number on
838 entry to and on exit from the corresponding subtree in the dominance tree.
839 The nearest dominator for a given use is the smallest of these intervals
840 that contains entry and exit dfs numbers for the basic block with the use.
841 If we store the bounds for all the uses to an array and sort it, we can
842 locate the nearest dominating def in logarithmic time by binary search.*/
843 bitmap_ior (to_remove
, kills
, phis
);
844 n_defs
= bitmap_count_bits (to_remove
);
845 defs
= XNEWVEC (struct dom_dfsnum
, 2 * n_defs
+ 1);
846 defs
[0].bb_index
= 1;
849 EXECUTE_IF_SET_IN_BITMAP (to_remove
, 0, i
, bi
)
851 def_bb
= BASIC_BLOCK (i
);
852 defs
[adef
].bb_index
= i
;
853 defs
[adef
].dfs_num
= bb_dom_dfs_in (CDI_DOMINATORS
, def_bb
);
854 defs
[adef
+ 1].bb_index
= i
;
855 defs
[adef
+ 1].dfs_num
= bb_dom_dfs_out (CDI_DOMINATORS
, def_bb
);
858 BITMAP_FREE (to_remove
);
859 gcc_assert (adef
== 2 * n_defs
+ 1);
860 qsort (defs
, adef
, sizeof (struct dom_dfsnum
), cmp_dfsnum
);
861 gcc_assert (defs
[0].bb_index
== 1);
863 /* Now each DEFS entry contains the number of the basic block to that the
864 dfs number corresponds. Change them to the number of basic block that
865 corresponds to the interval following the dfs number. Also, for the
866 dfs_out numbers, increase the dfs number by one (so that it corresponds
867 to the start of the following interval, not to the end of the current
868 one). We use WORKLIST as a stack. */
869 worklist
= VEC_alloc (int, heap
, n_defs
+ 1);
870 VEC_quick_push (int, worklist
, 1);
873 for (i
= 1; i
< adef
; i
++)
875 b
= defs
[i
].bb_index
;
878 /* This is a closing element. Interval corresponding to the top
879 of the stack after removing it follows. */
880 VEC_pop (int, worklist
);
881 top
= VEC_index (int, worklist
, VEC_length (int, worklist
) - 1);
882 defs
[n_defs
].bb_index
= top
;
883 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
+ 1;
887 /* Opening element. Nothing to do, just push it to the stack and move
888 it to the correct position. */
889 defs
[n_defs
].bb_index
= defs
[i
].bb_index
;
890 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
;
891 VEC_quick_push (int, worklist
, b
);
895 /* If this interval starts at the same point as the previous one, cancel
897 if (defs
[n_defs
].dfs_num
== defs
[n_defs
- 1].dfs_num
)
898 defs
[n_defs
- 1].bb_index
= defs
[n_defs
].bb_index
;
902 VEC_pop (int, worklist
);
903 gcc_assert (VEC_empty (int, worklist
));
905 /* Now process the uses. */
906 live_phis
= BITMAP_ALLOC (NULL
);
907 EXECUTE_IF_SET_IN_BITMAP (uses
, 0, i
, bi
)
909 VEC_safe_push (int, heap
, worklist
, i
);
912 while (!VEC_empty (int, worklist
))
914 b
= VEC_pop (int, worklist
);
915 if (b
== ENTRY_BLOCK
)
918 /* If there is a phi node in USE_BB, it is made live. Otherwise,
919 find the def that dominates the immediate dominator of USE_BB
920 (the kill in USE_BB does not dominate the use). */
921 if (bitmap_bit_p (phis
, b
))
925 use_bb
= get_immediate_dominator (CDI_DOMINATORS
, BASIC_BLOCK (b
));
926 p
= find_dfsnum_interval (defs
, n_defs
,
927 bb_dom_dfs_in (CDI_DOMINATORS
, use_bb
));
928 if (!bitmap_bit_p (phis
, p
))
932 /* If the phi node is already live, there is nothing to do. */
933 if (!bitmap_set_bit (live_phis
, p
))
936 /* Add the new uses to the worklist. */
937 def_bb
= BASIC_BLOCK (p
);
938 FOR_EACH_EDGE (e
, ei
, def_bb
->preds
)
941 if (bitmap_bit_p (uses
, u
))
944 /* In case there is a kill directly in the use block, do not record
945 the use (this is also necessary for correctness, as we assume that
946 uses dominated by a def directly in their block have been filtered
948 if (bitmap_bit_p (kills
, u
))
951 bitmap_set_bit (uses
, u
);
952 VEC_safe_push (int, heap
, worklist
, u
);
956 VEC_free (int, heap
, worklist
);
957 bitmap_copy (phis
, live_phis
);
958 BITMAP_FREE (live_phis
);
962 /* Return the set of blocks where variable VAR is defined and the blocks
963 where VAR is live on entry (livein). Return NULL, if no entry is
964 found in DEF_BLOCKS. */
966 static inline struct def_blocks_d
*
967 find_def_blocks_for (tree var
)
969 def_blocks_p p
= &get_common_info (var
)->def_blocks
;
976 /* Marks phi node PHI in basic block BB for rewrite. */
979 mark_phi_for_rewrite (basic_block bb
, gimple phi
)
982 unsigned n
, idx
= bb
->index
;
984 if (rewrite_uses_p (phi
))
987 set_rewrite_uses (phi
, true);
989 if (!blocks_with_phis_to_rewrite
)
992 bitmap_set_bit (blocks_with_phis_to_rewrite
, idx
);
994 n
= (unsigned) last_basic_block
+ 1;
995 if (VEC_length (gimple_vec
, phis_to_rewrite
) < n
)
996 VEC_safe_grow_cleared (gimple_vec
, heap
, phis_to_rewrite
, n
);
998 phis
= VEC_index (gimple_vec
, phis_to_rewrite
, idx
);
1000 phis
= VEC_alloc (gimple
, heap
, 10);
1002 VEC_safe_push (gimple
, heap
, phis
, phi
);
1003 VEC_replace (gimple_vec
, phis_to_rewrite
, idx
, phis
);
1006 /* Insert PHI nodes for variable VAR using the iterated dominance
1007 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1008 function assumes that the caller is incrementally updating the
1009 existing SSA form, in which case VAR may be an SSA name instead of
1012 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1013 PHI node for VAR. On exit, only the nodes that received a PHI node
1014 for VAR will be present in PHI_INSERTION_POINTS. */
1017 insert_phi_nodes_for (tree var
, bitmap phi_insertion_points
, bool update_p
)
1024 struct def_blocks_d
*def_map
;
1026 def_map
= find_def_blocks_for (var
);
1027 gcc_assert (def_map
);
1029 /* Remove the blocks where we already have PHI nodes for VAR. */
1030 bitmap_and_compl_into (phi_insertion_points
, def_map
->phi_blocks
);
1032 /* Remove obviously useless phi nodes. */
1033 prune_unused_phi_nodes (phi_insertion_points
, def_map
->def_blocks
,
1034 def_map
->livein_blocks
);
1036 /* And insert the PHI nodes. */
1037 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points
, 0, bb_index
, bi
)
1039 bb
= BASIC_BLOCK (bb_index
);
1041 mark_block_for_update (bb
);
1045 if (TREE_CODE (var
) == SSA_NAME
)
1047 /* If we are rewriting SSA names, create the LHS of the PHI
1048 node by duplicating VAR. This is useful in the case of
1049 pointers, to also duplicate pointer attributes (alias
1050 information, in particular). */
1054 gcc_assert (update_p
);
1055 new_lhs
= duplicate_ssa_name (var
, NULL
);
1056 phi
= create_phi_node (new_lhs
, bb
);
1057 add_new_name_mapping (new_lhs
, var
);
1059 /* Add VAR to every argument slot of PHI. We need VAR in
1060 every argument so that rewrite_update_phi_arguments knows
1061 which name is this PHI node replacing. If VAR is a
1062 symbol marked for renaming, this is not necessary, the
1063 renamer will use the symbol on the LHS to get its
1064 reaching definition. */
1065 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1066 add_phi_arg (phi
, var
, e
, UNKNOWN_LOCATION
);
1072 gcc_assert (DECL_P (var
));
1073 phi
= create_phi_node (var
, bb
);
1075 tracked_var
= target_for_debug_bind (var
);
1078 gimple note
= gimple_build_debug_bind (tracked_var
,
1081 gimple_stmt_iterator si
= gsi_after_labels (bb
);
1082 gsi_insert_before (&si
, note
, GSI_SAME_STMT
);
1086 /* Mark this PHI node as interesting for update_ssa. */
1087 set_register_defs (phi
, true);
1088 mark_phi_for_rewrite (bb
, phi
);
1092 /* Sort var_infos after DECL_UID of their var. */
1095 insert_phi_nodes_compare_var_infos (const void *a
, const void *b
)
1097 const struct var_info_d
*defa
= *(struct var_info_d
* const *)a
;
1098 const struct var_info_d
*defb
= *(struct var_info_d
* const *)b
;
1099 if (DECL_UID (defa
->var
) < DECL_UID (defb
->var
))
1105 /* Insert PHI nodes at the dominance frontier of blocks with variable
1106 definitions. DFS contains the dominance frontier information for
1110 insert_phi_nodes (bitmap_head
*dfs
)
1115 VEC(var_info_p
,heap
) *vars
;
1117 timevar_push (TV_TREE_INSERT_PHI_NODES
);
1119 vars
= VEC_alloc (var_info_p
, heap
, htab_elements (var_infos
));
1120 FOR_EACH_HTAB_ELEMENT (var_infos
, info
, var_info_p
, hi
)
1121 if (info
->info
.need_phi_state
!= NEED_PHI_STATE_NO
)
1122 VEC_quick_push (var_info_p
, vars
, info
);
1124 /* Do two stages to avoid code generation differences for UID
1125 differences but no UID ordering differences. */
1126 VEC_qsort (var_info_p
, vars
, insert_phi_nodes_compare_var_infos
);
1128 FOR_EACH_VEC_ELT (var_info_p
, vars
, i
, info
)
1130 bitmap idf
= compute_idf (info
->info
.def_blocks
.def_blocks
, dfs
);
1131 insert_phi_nodes_for (info
->var
, idf
, false);
1135 VEC_free(var_info_p
, heap
, vars
);
1137 timevar_pop (TV_TREE_INSERT_PHI_NODES
);
1141 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1142 register DEF (an SSA_NAME) to be a new definition for SYM. */
1145 register_new_def (tree def
, tree sym
)
1147 common_info_p info
= get_common_info (sym
);
1150 /* If this variable is set in a single basic block and all uses are
1151 dominated by the set(s) in that single basic block, then there is
1152 no reason to record anything for this variable in the block local
1153 definition stacks. Doing so just wastes time and memory.
1155 This is the same test to prune the set of variables which may
1156 need PHI nodes. So we just use that information since it's already
1157 computed and available for us to use. */
1158 if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
1160 info
->current_def
= def
;
1164 currdef
= info
->current_def
;
1166 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1167 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1168 in the stack so that we know which symbol is being defined by
1169 this SSA name when we unwind the stack. */
1170 if (currdef
&& !is_gimple_reg (sym
))
1171 VEC_safe_push (tree
, heap
, block_defs_stack
, sym
);
1173 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1174 stack is later used by the dominator tree callbacks to restore
1175 the reaching definitions for all the variables defined in the
1176 block after a recursive visit to all its immediately dominated
1177 blocks. If there is no current reaching definition, then just
1178 record the underlying _DECL node. */
1179 VEC_safe_push (tree
, heap
, block_defs_stack
, currdef
? currdef
: sym
);
1181 /* Set the current reaching definition for SYM to be DEF. */
1182 info
->current_def
= def
;
1186 /* Perform a depth-first traversal of the dominator tree looking for
1187 variables to rename. BB is the block where to start searching.
1188 Renaming is a five step process:
1190 1- Every definition made by PHI nodes at the start of the blocks is
1191 registered as the current definition for the corresponding variable.
1193 2- Every statement in BB is rewritten. USE and VUSE operands are
1194 rewritten with their corresponding reaching definition. DEF and
1195 VDEF targets are registered as new definitions.
1197 3- All the PHI nodes in successor blocks of BB are visited. The
1198 argument corresponding to BB is replaced with its current reaching
1201 4- Recursively rewrite every dominator child block of BB.
1203 5- Restore (in reverse order) the current reaching definition for every
1204 new definition introduced in this block. This is done so that when
1205 we return from the recursive call, all the current reaching
1206 definitions are restored to the names that were valid in the
1207 dominator parent of BB. */
1209 /* Return the current definition for variable VAR. If none is found,
1210 create a new SSA name to act as the zeroth definition for VAR. */
1213 get_reaching_def (tree var
)
1215 common_info_p info
= get_common_info (var
);
1218 /* Lookup the current reaching definition for VAR. */
1219 currdef
= info
->current_def
;
1221 /* If there is no reaching definition for VAR, create and register a
1222 default definition for it (if needed). */
1223 if (currdef
== NULL_TREE
)
1225 tree sym
= DECL_P (var
) ? var
: SSA_NAME_VAR (var
);
1226 currdef
= get_or_create_ssa_default_def (cfun
, sym
);
1229 /* Return the current reaching definition for VAR, or the default
1230 definition, if we had to create one. */
1235 /* Helper function for rewrite_stmt. Rewrite uses in a debug stmt. */
1238 rewrite_debug_stmt_uses (gimple stmt
)
1240 use_operand_p use_p
;
1242 bool update
= false;
1244 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1246 tree var
= USE_FROM_PTR (use_p
), def
;
1247 common_info_p info
= get_common_info (var
);
1248 gcc_assert (DECL_P (var
));
1249 def
= info
->current_def
;
1252 if (TREE_CODE (var
) == PARM_DECL
&& single_succ_p (ENTRY_BLOCK_PTR
))
1254 gimple_stmt_iterator gsi
1255 = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR
));
1257 /* Search a few source bind stmts at the start of first bb to
1258 see if a DEBUG_EXPR_DECL can't be reused. */
1260 !gsi_end_p (gsi
) && lim
> 0;
1261 gsi_next (&gsi
), lim
--)
1263 gimple gstmt
= gsi_stmt (gsi
);
1264 if (!gimple_debug_source_bind_p (gstmt
))
1266 if (gimple_debug_source_bind_get_value (gstmt
) == var
)
1268 def
= gimple_debug_source_bind_get_var (gstmt
);
1269 if (TREE_CODE (def
) == DEBUG_EXPR_DECL
)
1275 /* If not, add a new source bind stmt. */
1276 if (def
== NULL_TREE
)
1279 def
= make_node (DEBUG_EXPR_DECL
);
1280 def_temp
= gimple_build_debug_source_bind (def
, var
, NULL
);
1281 DECL_ARTIFICIAL (def
) = 1;
1282 TREE_TYPE (def
) = TREE_TYPE (var
);
1283 DECL_MODE (def
) = DECL_MODE (var
);
1284 gsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR
));
1285 gsi_insert_before (&gsi
, def_temp
, GSI_SAME_STMT
);
1292 /* Check if info->current_def can be trusted. */
1293 basic_block bb
= gimple_bb (stmt
);
1295 = SSA_NAME_IS_DEFAULT_DEF (def
)
1296 ? NULL
: gimple_bb (SSA_NAME_DEF_STMT (def
));
1298 /* If definition is in current bb, it is fine. */
1301 /* If definition bb doesn't dominate the current bb,
1302 it can't be used. */
1303 else if (def_bb
&& !dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
1305 /* If there is just one definition and dominates the current
1307 else if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
1311 struct def_blocks_d
*db_p
= get_def_blocks_for (info
);
1313 /* If there are some non-debug uses in the current bb,
1315 if (bitmap_bit_p (db_p
->livein_blocks
, bb
->index
))
1317 /* Otherwise give up for now. */
1324 gimple_debug_bind_reset_value (stmt
);
1328 SET_USE (use_p
, def
);
1334 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1335 the block with its immediate reaching definitions. Update the current
1336 definition of a variable when a new real or virtual definition is found. */
1339 rewrite_stmt (gimple_stmt_iterator
*si
)
1341 use_operand_p use_p
;
1342 def_operand_p def_p
;
1344 gimple stmt
= gsi_stmt (*si
);
1346 /* If mark_def_sites decided that we don't need to rewrite this
1347 statement, ignore it. */
1348 gcc_assert (blocks_to_update
== NULL
);
1349 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
1352 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1354 fprintf (dump_file
, "Renaming statement ");
1355 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1356 fprintf (dump_file
, "\n");
1359 /* Step 1. Rewrite USES in the statement. */
1360 if (rewrite_uses_p (stmt
))
1362 if (is_gimple_debug (stmt
))
1363 rewrite_debug_stmt_uses (stmt
);
1365 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
1367 tree var
= USE_FROM_PTR (use_p
);
1368 gcc_assert (DECL_P (var
));
1369 SET_USE (use_p
, get_reaching_def (var
));
1373 /* Step 2. Register the statement's DEF operands. */
1374 if (register_defs_p (stmt
))
1375 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_ALL_DEFS
)
1377 tree var
= DEF_FROM_PTR (def_p
);
1381 gcc_assert (DECL_P (var
));
1383 if (gimple_clobber_p (stmt
)
1384 && is_gimple_reg (var
))
1386 /* If we rewrite a DECL into SSA form then drop its
1387 clobber stmts and replace uses with a new default def. */
1388 gcc_assert (TREE_CODE (var
) == VAR_DECL
1389 && !gimple_vdef (stmt
));
1390 gsi_replace (si
, gimple_build_nop (), true);
1391 register_new_def (get_or_create_ssa_default_def (cfun
, var
), var
);
1395 name
= make_ssa_name (var
, stmt
);
1396 SET_DEF (def_p
, name
);
1397 register_new_def (DEF_FROM_PTR (def_p
), var
);
1399 tracked_var
= target_for_debug_bind (var
);
1402 gimple note
= gimple_build_debug_bind (tracked_var
, name
, stmt
);
1403 gsi_insert_after (si
, note
, GSI_SAME_STMT
);
1409 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1410 PHI nodes. For every PHI node found, add a new argument containing the
1411 current reaching definition for the variable and the edge through which
1412 that definition is reaching the PHI node. */
1415 rewrite_add_phi_arguments (basic_block bb
)
1420 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1423 gimple_stmt_iterator gsi
;
1425 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
);
1431 phi
= gsi_stmt (gsi
);
1432 currdef
= get_reaching_def (SSA_NAME_VAR (gimple_phi_result (phi
)));
1433 stmt
= SSA_NAME_DEF_STMT (currdef
);
1434 add_phi_arg (phi
, currdef
, e
, gimple_location (stmt
));
1439 /* SSA Rewriting Step 1. Initialization, create a block local stack
1440 of reaching definitions for new SSA names produced in this block
1441 (BLOCK_DEFS). Register new definitions for every PHI node in the
1445 rewrite_enter_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1448 gimple_stmt_iterator gsi
;
1450 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1451 fprintf (dump_file
, "\n\nRenaming block #%d\n\n", bb
->index
);
1453 /* Mark the unwind point for this block. */
1454 VEC_safe_push (tree
, heap
, block_defs_stack
, NULL_TREE
);
1456 /* Step 1. Register new definitions for every PHI node in the block.
1457 Conceptually, all the PHI nodes are executed in parallel and each PHI
1458 node introduces a new version for the associated variable. */
1459 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1461 tree result
= gimple_phi_result (gsi_stmt (gsi
));
1462 register_new_def (result
, SSA_NAME_VAR (result
));
1465 /* Step 2. Rewrite every variable used in each statement in the block
1466 with its immediate reaching definitions. Update the current definition
1467 of a variable when a new real or virtual definition is found. */
1468 if (TEST_BIT (interesting_blocks
, bb
->index
))
1469 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1470 rewrite_stmt (&gsi
);
1472 /* Step 3. Visit all the successor blocks of BB looking for PHI nodes.
1473 For every PHI node found, add a new argument containing the current
1474 reaching definition for the variable and the edge through which that
1475 definition is reaching the PHI node. */
1476 rewrite_add_phi_arguments (bb
);
1481 /* Called after visiting all the statements in basic block BB and all
1482 of its dominator children. Restore CURRDEFS to its original value. */
1485 rewrite_leave_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1486 basic_block bb ATTRIBUTE_UNUSED
)
1488 /* Restore CURRDEFS to its original state. */
1489 while (VEC_length (tree
, block_defs_stack
) > 0)
1491 tree tmp
= VEC_pop (tree
, block_defs_stack
);
1492 tree saved_def
, var
;
1494 if (tmp
== NULL_TREE
)
1497 if (TREE_CODE (tmp
) == SSA_NAME
)
1499 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1500 current definition of its underlying variable. Note that
1501 if the SSA_NAME is not for a GIMPLE register, the symbol
1502 being defined is stored in the next slot in the stack.
1503 This mechanism is needed because an SSA name for a
1504 non-register symbol may be the definition for more than
1505 one symbol (e.g., SFTs, aliased variables, etc). */
1507 var
= SSA_NAME_VAR (saved_def
);
1508 if (!is_gimple_reg (var
))
1509 var
= VEC_pop (tree
, block_defs_stack
);
1513 /* If we recorded anything else, it must have been a _DECL
1514 node and its current reaching definition must have been
1520 get_common_info (var
)->current_def
= saved_def
;
1525 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1528 dump_decl_set (FILE *file
, bitmap set
)
1535 fprintf (file
, "{ ");
1537 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
1539 fprintf (file
, "D.%u", i
);
1540 fprintf (file
, " ");
1543 fprintf (file
, "}");
1546 fprintf (file
, "NIL");
1550 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1553 debug_decl_set (bitmap set
)
1555 dump_decl_set (stderr
, set
);
1556 fprintf (stderr
, "\n");
1560 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1561 stack up to a maximum of N levels. If N is -1, the whole stack is
1562 dumped. New levels are created when the dominator tree traversal
1563 used for renaming enters a new sub-tree. */
1566 dump_defs_stack (FILE *file
, int n
)
1570 fprintf (file
, "\n\nRenaming stack");
1572 fprintf (file
, " (up to %d levels)", n
);
1573 fprintf (file
, "\n\n");
1576 fprintf (file
, "Level %d (current level)\n", i
);
1577 for (j
= (int) VEC_length (tree
, block_defs_stack
) - 1; j
>= 0; j
--)
1581 name
= VEC_index (tree
, block_defs_stack
, j
);
1582 if (name
== NULL_TREE
)
1587 fprintf (file
, "\nLevel %d\n", i
);
1598 var
= SSA_NAME_VAR (name
);
1599 if (!is_gimple_reg (var
))
1602 var
= VEC_index (tree
, block_defs_stack
, j
);
1606 fprintf (file
, " Previous CURRDEF (");
1607 print_generic_expr (file
, var
, 0);
1608 fprintf (file
, ") = ");
1610 print_generic_expr (file
, name
, 0);
1612 fprintf (file
, "<NIL>");
1613 fprintf (file
, "\n");
1618 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1619 stack up to a maximum of N levels. If N is -1, the whole stack is
1620 dumped. New levels are created when the dominator tree traversal
1621 used for renaming enters a new sub-tree. */
1624 debug_defs_stack (int n
)
1626 dump_defs_stack (stderr
, n
);
1630 /* Dump the current reaching definition of every symbol to FILE. */
1633 dump_currdefs (FILE *file
)
1638 if (VEC_empty (tree
, symbols_to_rename
))
1641 fprintf (file
, "\n\nCurrent reaching definitions\n\n");
1642 FOR_EACH_VEC_ELT (tree
, symbols_to_rename
, i
, var
)
1644 common_info_p info
= get_common_info (var
);
1645 fprintf (file
, "CURRDEF (");
1646 print_generic_expr (file
, var
, 0);
1647 fprintf (file
, ") = ");
1648 if (info
->current_def
)
1649 print_generic_expr (file
, info
->current_def
, 0);
1651 fprintf (file
, "<NIL>");
1652 fprintf (file
, "\n");
1657 /* Dump the current reaching definition of every symbol to stderr. */
1660 debug_currdefs (void)
1662 dump_currdefs (stderr
);
1666 /* Dump SSA information to FILE. */
1669 dump_tree_ssa (FILE *file
)
1671 const char *funcname
1672 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
1674 fprintf (file
, "SSA renaming information for %s\n\n", funcname
);
1676 dump_var_infos (file
);
1677 dump_defs_stack (file
, -1);
1678 dump_currdefs (file
);
1679 dump_tree_ssa_stats (file
);
1683 /* Dump SSA information to stderr. */
1686 debug_tree_ssa (void)
1688 dump_tree_ssa (stderr
);
1692 /* Dump statistics for the hash table HTAB. */
1695 htab_statistics (FILE *file
, htab_t htab
)
1697 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
1698 (long) htab_size (htab
),
1699 (long) htab_elements (htab
),
1700 htab_collisions (htab
));
1704 /* Dump SSA statistics on FILE. */
1707 dump_tree_ssa_stats (FILE *file
)
1711 fprintf (file
, "\nHash table statistics:\n");
1712 fprintf (file
, " var_infos: ");
1713 htab_statistics (file
, var_infos
);
1714 fprintf (file
, "\n");
1719 /* Dump SSA statistics on stderr. */
1722 debug_tree_ssa_stats (void)
1724 dump_tree_ssa_stats (stderr
);
1728 /* Hashing and equality functions for VAR_INFOS. */
1731 var_info_hash (const void *p
)
1733 return DECL_UID (((const struct var_info_d
*)p
)->var
);
1737 var_info_eq (const void *p1
, const void *p2
)
1739 return ((const struct var_info_d
*)p1
)->var
1740 == ((const struct var_info_d
*)p2
)->var
;
1744 /* Callback for htab_traverse to dump the VAR_INFOS hash table. */
1747 debug_var_infos_r (void **slot
, void *data
)
1749 FILE *file
= (FILE *) data
;
1750 struct var_info_d
*info
= (struct var_info_d
*) *slot
;
1752 fprintf (file
, "VAR: ");
1753 print_generic_expr (file
, info
->var
, dump_flags
);
1754 bitmap_print (file
, info
->info
.def_blocks
.def_blocks
,
1755 ", DEF_BLOCKS: { ", "}");
1756 bitmap_print (file
, info
->info
.def_blocks
.livein_blocks
,
1757 ", LIVEIN_BLOCKS: { ", "}");
1758 bitmap_print (file
, info
->info
.def_blocks
.phi_blocks
,
1759 ", PHI_BLOCKS: { ", "}\n");
1765 /* Dump the VAR_INFOS hash table on FILE. */
1768 dump_var_infos (FILE *file
)
1770 fprintf (file
, "\n\nDefinition and live-in blocks:\n\n");
1772 htab_traverse (var_infos
, debug_var_infos_r
, file
);
1776 /* Dump the VAR_INFOS hash table on stderr. */
1779 debug_var_infos (void)
1781 dump_var_infos (stderr
);
1785 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1788 register_new_update_single (tree new_name
, tree old_name
)
1790 common_info_p info
= get_common_info (old_name
);
1791 tree currdef
= info
->current_def
;
1793 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1794 This stack is later used by the dominator tree callbacks to
1795 restore the reaching definitions for all the variables
1796 defined in the block after a recursive visit to all its
1797 immediately dominated blocks. */
1798 VEC_reserve (tree
, heap
, block_defs_stack
, 2);
1799 VEC_quick_push (tree
, block_defs_stack
, currdef
);
1800 VEC_quick_push (tree
, block_defs_stack
, old_name
);
1802 /* Set the current reaching definition for OLD_NAME to be
1804 info
->current_def
= new_name
;
1808 /* Register NEW_NAME to be the new reaching definition for all the
1809 names in OLD_NAMES. Used by the incremental SSA update routines to
1810 replace old SSA names with new ones. */
1813 register_new_update_set (tree new_name
, bitmap old_names
)
1818 EXECUTE_IF_SET_IN_BITMAP (old_names
, 0, i
, bi
)
1819 register_new_update_single (new_name
, ssa_name (i
));
1824 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1825 it is a symbol marked for renaming, replace it with USE_P's current
1826 reaching definition. */
1829 maybe_replace_use (use_operand_p use_p
)
1831 tree rdef
= NULL_TREE
;
1832 tree use
= USE_FROM_PTR (use_p
);
1833 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1835 if (marked_for_renaming (sym
))
1836 rdef
= get_reaching_def (sym
);
1837 else if (is_old_name (use
))
1838 rdef
= get_reaching_def (use
);
1840 if (rdef
&& rdef
!= use
)
1841 SET_USE (use_p
, rdef
);
1845 /* Same as maybe_replace_use, but without introducing default stmts,
1846 returning false to indicate a need to do so. */
1849 maybe_replace_use_in_debug_stmt (use_operand_p use_p
)
1851 tree rdef
= NULL_TREE
;
1852 tree use
= USE_FROM_PTR (use_p
);
1853 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1855 if (marked_for_renaming (sym
))
1856 rdef
= get_var_info (sym
)->info
.current_def
;
1857 else if (is_old_name (use
))
1859 rdef
= get_ssa_name_ann (use
)->info
.current_def
;
1860 /* We can't assume that, if there's no current definition, the
1861 default one should be used. It could be the case that we've
1862 rearranged blocks so that the earlier definition no longer
1863 dominates the use. */
1864 if (!rdef
&& SSA_NAME_IS_DEFAULT_DEF (use
))
1870 if (rdef
&& rdef
!= use
)
1871 SET_USE (use_p
, rdef
);
1873 return rdef
!= NULL_TREE
;
1877 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1878 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1879 register it as the current definition for the names replaced by
1883 maybe_register_def (def_operand_p def_p
, gimple stmt
,
1884 gimple_stmt_iterator gsi
)
1886 tree def
= DEF_FROM_PTR (def_p
);
1887 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
1889 /* If DEF is a naked symbol that needs renaming, create a new
1891 if (marked_for_renaming (sym
))
1897 def
= make_ssa_name (def
, stmt
);
1898 SET_DEF (def_p
, def
);
1900 tracked_var
= target_for_debug_bind (sym
);
1903 gimple note
= gimple_build_debug_bind (tracked_var
, def
, stmt
);
1904 /* If stmt ends the bb, insert the debug stmt on the single
1905 non-EH edge from the stmt. */
1906 if (gsi_one_before_end_p (gsi
) && stmt_ends_bb_p (stmt
))
1908 basic_block bb
= gsi_bb (gsi
);
1911 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1912 if (!(e
->flags
& EDGE_EH
))
1917 /* If there are other predecessors to ef->dest, then
1918 there must be PHI nodes for the modified
1919 variable, and therefore there will be debug bind
1920 stmts after the PHI nodes. The debug bind notes
1921 we'd insert would force the creation of a new
1922 block (diverging codegen) and be redundant with
1923 the post-PHI bind stmts, so don't add them.
1925 As for the exit edge, there wouldn't be redundant
1926 bind stmts, but there wouldn't be a PC to bind
1927 them to either, so avoid diverging the CFG. */
1928 if (ef
&& single_pred_p (ef
->dest
)
1929 && ef
->dest
!= EXIT_BLOCK_PTR
)
1931 /* If there were PHI nodes in the node, we'd
1932 have to make sure the value we're binding
1933 doesn't need rewriting. But there shouldn't
1934 be PHI nodes in a single-predecessor block,
1935 so we just add the note. */
1936 gsi_insert_on_edge_immediate (ef
, note
);
1940 gsi_insert_after (&gsi
, note
, GSI_SAME_STMT
);
1944 register_new_update_single (def
, sym
);
1948 /* If DEF is a new name, register it as a new definition
1949 for all the names replaced by DEF. */
1950 if (is_new_name (def
))
1951 register_new_update_set (def
, names_replaced_by (def
));
1953 /* If DEF is an old name, register DEF as a new
1954 definition for itself. */
1955 if (is_old_name (def
))
1956 register_new_update_single (def
, def
);
1961 /* Update every variable used in the statement pointed-to by SI. The
1962 statement is assumed to be in SSA form already. Names in
1963 OLD_SSA_NAMES used by SI will be updated to their current reaching
1964 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1965 will be registered as a new definition for their corresponding name
1966 in OLD_SSA_NAMES. */
1969 rewrite_update_stmt (gimple stmt
, gimple_stmt_iterator gsi
)
1971 use_operand_p use_p
;
1972 def_operand_p def_p
;
1975 /* Only update marked statements. */
1976 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
1979 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1981 fprintf (dump_file
, "Updating SSA information for statement ");
1982 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1985 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1986 symbol is marked for renaming. */
1987 if (rewrite_uses_p (stmt
))
1989 if (is_gimple_debug (stmt
))
1991 bool failed
= false;
1993 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1994 if (!maybe_replace_use_in_debug_stmt (use_p
))
2002 /* DOM sometimes threads jumps in such a way that a
2003 debug stmt ends up referencing a SSA variable that no
2004 longer dominates the debug stmt, but such that all
2005 incoming definitions refer to the same definition in
2006 an earlier dominator. We could try to recover that
2007 definition somehow, but this will have to do for now.
2009 Introducing a default definition, which is what
2010 maybe_replace_use() would do in such cases, may
2011 modify code generation, for the otherwise-unused
2012 default definition would never go away, modifying SSA
2013 version numbers all over. */
2014 gimple_debug_bind_reset_value (stmt
);
2020 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
2021 maybe_replace_use (use_p
);
2025 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
2026 Also register definitions for names whose underlying symbol is
2027 marked for renaming. */
2028 if (register_defs_p (stmt
))
2029 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_ALL_DEFS
)
2030 maybe_register_def (def_p
, stmt
, gsi
);
2034 /* Visit all the successor blocks of BB looking for PHI nodes. For
2035 every PHI node found, check if any of its arguments is in
2036 OLD_SSA_NAMES. If so, and if the argument has a current reaching
2037 definition, replace it. */
2040 rewrite_update_phi_arguments (basic_block bb
)
2046 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2051 if (!bitmap_bit_p (blocks_with_phis_to_rewrite
, e
->dest
->index
))
2054 phis
= VEC_index (gimple_vec
, phis_to_rewrite
, e
->dest
->index
);
2055 FOR_EACH_VEC_ELT (gimple
, phis
, i
, phi
)
2057 tree arg
, lhs_sym
, reaching_def
= NULL
;
2058 use_operand_p arg_p
;
2060 gcc_assert (rewrite_uses_p (phi
));
2062 arg_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
2063 arg
= USE_FROM_PTR (arg_p
);
2065 if (arg
&& !DECL_P (arg
) && TREE_CODE (arg
) != SSA_NAME
)
2068 lhs_sym
= SSA_NAME_VAR (gimple_phi_result (phi
));
2070 if (arg
== NULL_TREE
)
2072 /* When updating a PHI node for a recently introduced
2073 symbol we may find NULL arguments. That's why we
2074 take the symbol from the LHS of the PHI node. */
2075 reaching_def
= get_reaching_def (lhs_sym
);
2080 tree sym
= DECL_P (arg
) ? arg
: SSA_NAME_VAR (arg
);
2082 if (marked_for_renaming (sym
))
2083 reaching_def
= get_reaching_def (sym
);
2084 else if (is_old_name (arg
))
2085 reaching_def
= get_reaching_def (arg
);
2088 /* Update the argument if there is a reaching def. */
2092 source_location locus
;
2093 int arg_i
= PHI_ARG_INDEX_FROM_USE (arg_p
);
2095 SET_USE (arg_p
, reaching_def
);
2096 stmt
= SSA_NAME_DEF_STMT (reaching_def
);
2098 /* Single element PHI nodes behave like copies, so get the
2099 location from the phi argument. */
2100 if (gimple_code (stmt
) == GIMPLE_PHI
&&
2101 gimple_phi_num_args (stmt
) == 1)
2102 locus
= gimple_phi_arg_location (stmt
, 0);
2104 locus
= gimple_location (stmt
);
2106 gimple_phi_arg_set_location (phi
, arg_i
, locus
);
2110 if (e
->flags
& EDGE_ABNORMAL
)
2111 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p
)) = 1;
2117 /* Initialization of block data structures for the incremental SSA
2118 update pass. Create a block local stack of reaching definitions
2119 for new SSA names produced in this block (BLOCK_DEFS). Register
2120 new definitions for every PHI node in the block. */
2123 rewrite_update_enter_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
2126 bool is_abnormal_phi
;
2127 gimple_stmt_iterator gsi
;
2129 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2130 fprintf (dump_file
, "Registering new PHI nodes in block #%d\n",
2133 /* Mark the unwind point for this block. */
2134 VEC_safe_push (tree
, heap
, block_defs_stack
, NULL_TREE
);
2136 if (!bitmap_bit_p (blocks_to_update
, bb
->index
))
2139 /* Mark the LHS if any of the arguments flows through an abnormal
2141 is_abnormal_phi
= bb_has_abnormal_pred (bb
);
2143 /* If any of the PHI nodes is a replacement for a name in
2144 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2145 register it as a new definition for its corresponding name. Also
2146 register definitions for names whose underlying symbols are
2147 marked for renaming. */
2148 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2151 gimple phi
= gsi_stmt (gsi
);
2153 if (!register_defs_p (phi
))
2156 lhs
= gimple_phi_result (phi
);
2157 lhs_sym
= SSA_NAME_VAR (lhs
);
2159 if (marked_for_renaming (lhs_sym
))
2160 register_new_update_single (lhs
, lhs_sym
);
2164 /* If LHS is a new name, register a new definition for all
2165 the names replaced by LHS. */
2166 if (is_new_name (lhs
))
2167 register_new_update_set (lhs
, names_replaced_by (lhs
));
2169 /* If LHS is an OLD name, register it as a new definition
2171 if (is_old_name (lhs
))
2172 register_new_update_single (lhs
, lhs
);
2175 if (is_abnormal_phi
)
2176 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
) = 1;
2179 /* Step 2. Rewrite every variable used in each statement in the block. */
2180 if (TEST_BIT (interesting_blocks
, bb
->index
))
2182 gcc_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2183 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2184 rewrite_update_stmt (gsi_stmt (gsi
), gsi
);
2187 /* Step 3. Update PHI nodes. */
2188 rewrite_update_phi_arguments (bb
);
2191 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
2192 the current reaching definition of every name re-written in BB to
2193 the original reaching definition before visiting BB. This
2194 unwinding must be done in the opposite order to what is done in
2195 register_new_update_set. */
2198 rewrite_update_leave_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
2199 basic_block bb ATTRIBUTE_UNUSED
)
2201 while (VEC_length (tree
, block_defs_stack
) > 0)
2203 tree var
= VEC_pop (tree
, block_defs_stack
);
2206 /* NULL indicates the unwind stop point for this block (see
2207 rewrite_update_enter_block). */
2211 saved_def
= VEC_pop (tree
, block_defs_stack
);
2212 get_common_info (var
)->current_def
= saved_def
;
2217 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2220 ENTRY indicates the block where to start. Every block dominated by
2221 ENTRY will be rewritten.
2223 WHAT indicates what actions will be taken by the renamer (see enum
2226 BLOCKS are the set of interesting blocks for the dominator walker
2227 to process. If this set is NULL, then all the nodes dominated
2228 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2229 are not present in BLOCKS are ignored. */
2232 rewrite_blocks (basic_block entry
, enum rewrite_mode what
)
2234 struct dom_walk_data walk_data
;
2236 /* Rewrite all the basic blocks in the program. */
2237 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS
);
2239 /* Setup callbacks for the generic dominator tree walker. */
2240 memset (&walk_data
, 0, sizeof (walk_data
));
2242 walk_data
.dom_direction
= CDI_DOMINATORS
;
2244 if (what
== REWRITE_ALL
)
2246 walk_data
.before_dom_children
= rewrite_enter_block
;
2247 walk_data
.after_dom_children
= rewrite_leave_block
;
2249 else if (what
== REWRITE_UPDATE
)
2251 walk_data
.before_dom_children
= rewrite_update_enter_block
;
2252 walk_data
.after_dom_children
= rewrite_update_leave_block
;
2257 block_defs_stack
= VEC_alloc (tree
, heap
, 10);
2259 /* Initialize the dominator walker. */
2260 init_walk_dominator_tree (&walk_data
);
2262 /* Recursively walk the dominator tree rewriting each statement in
2263 each basic block. */
2264 walk_dominator_tree (&walk_data
, entry
);
2266 /* Finalize the dominator walker. */
2267 fini_walk_dominator_tree (&walk_data
);
2269 /* Debugging dumps. */
2270 if (dump_file
&& (dump_flags
& TDF_STATS
))
2272 dump_dfa_stats (dump_file
);
2274 dump_tree_ssa_stats (dump_file
);
2277 VEC_free (tree
, heap
, block_defs_stack
);
2279 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS
);
2283 /* Block processing routine for mark_def_sites. Clear the KILLS bitmap
2284 at the start of each block, and call mark_def_sites for each statement. */
2287 mark_def_sites_block (struct dom_walk_data
*walk_data
, basic_block bb
)
2289 struct mark_def_sites_global_data
*gd
;
2291 gimple_stmt_iterator gsi
;
2293 gd
= (struct mark_def_sites_global_data
*) walk_data
->global_data
;
2296 bitmap_clear (kills
);
2297 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2298 mark_def_sites (bb
, gsi_stmt (gsi
), kills
);
2302 /* Mark the definition site blocks for each variable, so that we know
2303 where the variable is actually live.
2305 The INTERESTING_BLOCKS global will be filled in with all the blocks
2306 that should be processed by the renamer. It is assumed that the
2307 caller has already initialized and zeroed it. */
2310 mark_def_site_blocks (void)
2312 struct dom_walk_data walk_data
;
2313 struct mark_def_sites_global_data mark_def_sites_global_data
;
2315 /* Setup callbacks for the generic dominator tree walker to find and
2316 mark definition sites. */
2317 walk_data
.dom_direction
= CDI_DOMINATORS
;
2318 walk_data
.initialize_block_local_data
= NULL
;
2319 walk_data
.before_dom_children
= mark_def_sites_block
;
2320 walk_data
.after_dom_children
= NULL
;
2322 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2323 large enough to accommodate all the variables referenced in the
2324 function, not just the ones we are renaming. */
2325 mark_def_sites_global_data
.kills
= BITMAP_ALLOC (NULL
);
2326 walk_data
.global_data
= &mark_def_sites_global_data
;
2328 /* We do not have any local data. */
2329 walk_data
.block_local_data_size
= 0;
2331 /* Initialize the dominator walker. */
2332 init_walk_dominator_tree (&walk_data
);
2334 /* Recursively walk the dominator tree. */
2335 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
2337 /* Finalize the dominator walker. */
2338 fini_walk_dominator_tree (&walk_data
);
2340 /* We no longer need this bitmap, clear and free it. */
2341 BITMAP_FREE (mark_def_sites_global_data
.kills
);
2345 /* Initialize internal data needed during renaming. */
2348 init_ssa_renamer (void)
2350 cfun
->gimple_df
->in_ssa_p
= false;
2352 /* Allocate memory for the DEF_BLOCKS hash table. */
2353 gcc_assert (var_infos
== NULL
);
2354 var_infos
= htab_create (VEC_length (tree
, cfun
->local_decls
),
2355 var_info_hash
, var_info_eq
, NULL
);
2357 bitmap_obstack_initialize (&update_ssa_obstack
);
2361 /* Deallocate internal data structures used by the renamer. */
2364 fini_ssa_renamer (void)
2368 htab_delete (var_infos
);
2372 bitmap_obstack_release (&update_ssa_obstack
);
2374 cfun
->gimple_df
->ssa_renaming_needed
= 0;
2375 cfun
->gimple_df
->rename_vops
= 0;
2376 cfun
->gimple_df
->in_ssa_p
= true;
2379 /* Main entry point into the SSA builder. The renaming process
2380 proceeds in four main phases:
2382 1- Compute dominance frontier and immediate dominators, needed to
2383 insert PHI nodes and rename the function in dominator tree
2386 2- Find and mark all the blocks that define variables
2387 (mark_def_site_blocks).
2389 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2391 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2393 Steps 3 and 4 are done using the dominator tree walker
2394 (walk_dominator_tree). */
2397 rewrite_into_ssa (void)
2403 /* Initialize operand data structures. */
2404 init_ssa_operands (cfun
);
2406 /* Initialize internal data needed by the renamer. */
2407 init_ssa_renamer ();
2409 /* Initialize the set of interesting blocks. The callback
2410 mark_def_sites will add to this set those blocks that the renamer
2412 interesting_blocks
= sbitmap_alloc (last_basic_block
);
2413 sbitmap_zero (interesting_blocks
);
2415 /* Initialize dominance frontier. */
2416 dfs
= XNEWVEC (bitmap_head
, last_basic_block
);
2418 bitmap_initialize (&dfs
[bb
->index
], &bitmap_default_obstack
);
2420 /* 1- Compute dominance frontiers. */
2421 calculate_dominance_info (CDI_DOMINATORS
);
2422 compute_dominance_frontiers (dfs
);
2424 /* 2- Find and mark definition sites. */
2425 mark_def_site_blocks ();
2427 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2428 insert_phi_nodes (dfs
);
2430 /* 4- Rename all the blocks. */
2431 rewrite_blocks (ENTRY_BLOCK_PTR
, REWRITE_ALL
);
2433 /* Free allocated memory. */
2435 bitmap_clear (&dfs
[bb
->index
]);
2438 sbitmap_free (interesting_blocks
);
2440 fini_ssa_renamer ();
2442 /* Try to get rid of all gimplifier generated temporaries by making
2443 its SSA names anonymous. This way we can garbage collect them
2444 all after removing unused locals which we do in our TODO. */
2445 for (i
= 1; i
< num_ssa_names
; ++i
)
2447 tree decl
, name
= ssa_name (i
);
2449 || SSA_NAME_IS_DEFAULT_DEF (name
))
2451 decl
= SSA_NAME_VAR (name
);
2453 && TREE_CODE (decl
) == VAR_DECL
2454 && !VAR_DECL_IS_VIRTUAL_OPERAND (decl
)
2455 && DECL_ARTIFICIAL (decl
)
2456 && DECL_IGNORED_P (decl
)
2457 && !DECL_NAME (decl
))
2458 SET_SSA_NAME_VAR_OR_IDENTIFIER (name
, NULL_TREE
);
2465 struct gimple_opt_pass pass_build_ssa
=
2471 rewrite_into_ssa
, /* execute */
2474 0, /* static_pass_number */
2475 TV_TREE_SSA_OTHER
, /* tv_id */
2476 PROP_cfg
, /* properties_required */
2477 PROP_ssa
, /* properties_provided */
2478 0, /* properties_destroyed */
2479 0, /* todo_flags_start */
2481 | TODO_remove_unused_locals
/* todo_flags_finish */
2486 /* Mark the definition of VAR at STMT and BB as interesting for the
2487 renamer. BLOCKS is the set of blocks that need updating. */
2490 mark_def_interesting (tree var
, gimple stmt
, basic_block bb
, bool insert_phi_p
)
2492 gcc_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2493 set_register_defs (stmt
, true);
2497 bool is_phi_p
= gimple_code (stmt
) == GIMPLE_PHI
;
2499 set_def_block (var
, bb
, is_phi_p
);
2501 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2502 site for both itself and all the old names replaced by it. */
2503 if (TREE_CODE (var
) == SSA_NAME
&& is_new_name (var
))
2507 bitmap set
= names_replaced_by (var
);
2509 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
2510 set_def_block (ssa_name (i
), bb
, is_phi_p
);
2516 /* Mark the use of VAR at STMT and BB as interesting for the
2517 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2521 mark_use_interesting (tree var
, gimple stmt
, basic_block bb
, bool insert_phi_p
)
2523 basic_block def_bb
= gimple_bb (stmt
);
2525 mark_block_for_update (def_bb
);
2526 mark_block_for_update (bb
);
2528 if (gimple_code (stmt
) == GIMPLE_PHI
)
2529 mark_phi_for_rewrite (def_bb
, stmt
);
2532 set_rewrite_uses (stmt
, true);
2534 if (is_gimple_debug (stmt
))
2538 /* If VAR has not been defined in BB, then it is live-on-entry
2539 to BB. Note that we cannot just use the block holding VAR's
2540 definition because if VAR is one of the names in OLD_SSA_NAMES,
2541 it will have several definitions (itself and all the names that
2545 struct def_blocks_d
*db_p
= get_def_blocks_for (get_common_info (var
));
2546 if (!bitmap_bit_p (db_p
->def_blocks
, bb
->index
))
2547 set_livein_block (var
, bb
);
2552 /* Do a dominator walk starting at BB processing statements that
2553 reference symbols in SSA operands. This is very similar to
2554 mark_def_sites, but the scan handles statements whose operands may
2555 already be SSA names.
2557 If INSERT_PHI_P is true, mark those uses as live in the
2558 corresponding block. This is later used by the PHI placement
2559 algorithm to make PHI pruning decisions.
2561 FIXME. Most of this would be unnecessary if we could associate a
2562 symbol to all the SSA names that reference it. But that
2563 sounds like it would be expensive to maintain. Still, it
2564 would be interesting to see if it makes better sense to do
2568 prepare_block_for_update (basic_block bb
, bool insert_phi_p
)
2571 gimple_stmt_iterator si
;
2575 mark_block_for_update (bb
);
2577 /* Process PHI nodes marking interesting those that define or use
2578 the symbols that we are interested in. */
2579 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
2581 gimple phi
= gsi_stmt (si
);
2582 tree lhs_sym
, lhs
= gimple_phi_result (phi
);
2584 if (TREE_CODE (lhs
) == SSA_NAME
2585 && (! virtual_operand_p (lhs
)
2586 || ! cfun
->gimple_df
->rename_vops
))
2589 lhs_sym
= DECL_P (lhs
) ? lhs
: SSA_NAME_VAR (lhs
);
2590 mark_for_renaming (lhs_sym
);
2591 mark_def_interesting (lhs_sym
, phi
, bb
, insert_phi_p
);
2593 /* Mark the uses in phi nodes as interesting. It would be more correct
2594 to process the arguments of the phi nodes of the successor edges of
2595 BB at the end of prepare_block_for_update, however, that turns out
2596 to be significantly more expensive. Doing it here is conservatively
2597 correct -- it may only cause us to believe a value to be live in a
2598 block that also contains its definition, and thus insert a few more
2599 phi nodes for it. */
2600 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2601 mark_use_interesting (lhs_sym
, phi
, e
->src
, insert_phi_p
);
2604 /* Process the statements. */
2605 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
2609 use_operand_p use_p
;
2610 def_operand_p def_p
;
2612 stmt
= gsi_stmt (si
);
2614 if (cfun
->gimple_df
->rename_vops
2615 && gimple_vuse (stmt
))
2617 tree use
= gimple_vuse (stmt
);
2618 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
2619 mark_for_renaming (sym
);
2620 mark_use_interesting (sym
, stmt
, bb
, insert_phi_p
);
2623 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, i
, SSA_OP_USE
)
2625 tree use
= USE_FROM_PTR (use_p
);
2628 mark_for_renaming (use
);
2629 mark_use_interesting (use
, stmt
, bb
, insert_phi_p
);
2632 if (cfun
->gimple_df
->rename_vops
2633 && gimple_vdef (stmt
))
2635 tree def
= gimple_vdef (stmt
);
2636 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
2637 mark_for_renaming (sym
);
2638 mark_def_interesting (sym
, stmt
, bb
, insert_phi_p
);
2641 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, i
, SSA_OP_DEF
)
2643 tree def
= DEF_FROM_PTR (def_p
);
2646 mark_for_renaming (def
);
2647 mark_def_interesting (def
, stmt
, bb
, insert_phi_p
);
2651 /* Now visit all the blocks dominated by BB. */
2652 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
2654 son
= next_dom_son (CDI_DOMINATORS
, son
))
2655 prepare_block_for_update (son
, insert_phi_p
);
2659 /* Helper for prepare_names_to_update. Mark all the use sites for
2660 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2661 prepare_names_to_update. */
2664 prepare_use_sites_for (tree name
, bool insert_phi_p
)
2666 use_operand_p use_p
;
2667 imm_use_iterator iter
;
2669 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
2671 gimple stmt
= USE_STMT (use_p
);
2672 basic_block bb
= gimple_bb (stmt
);
2674 if (gimple_code (stmt
) == GIMPLE_PHI
)
2676 int ix
= PHI_ARG_INDEX_FROM_USE (use_p
);
2677 edge e
= gimple_phi_arg_edge (stmt
, ix
);
2678 mark_use_interesting (name
, stmt
, e
->src
, insert_phi_p
);
2682 /* For regular statements, mark this as an interesting use
2684 mark_use_interesting (name
, stmt
, bb
, insert_phi_p
);
2690 /* Helper for prepare_names_to_update. Mark the definition site for
2691 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2692 prepare_names_to_update. */
2695 prepare_def_site_for (tree name
, bool insert_phi_p
)
2700 gcc_assert (names_to_release
== NULL
2701 || !bitmap_bit_p (names_to_release
, SSA_NAME_VERSION (name
)));
2703 stmt
= SSA_NAME_DEF_STMT (name
);
2704 bb
= gimple_bb (stmt
);
2707 gcc_assert (bb
->index
< last_basic_block
);
2708 mark_block_for_update (bb
);
2709 mark_def_interesting (name
, stmt
, bb
, insert_phi_p
);
2714 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2715 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2716 PHI nodes for newly created names. */
2719 prepare_names_to_update (bool insert_phi_p
)
2723 sbitmap_iterator sbi
;
2725 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2726 remove it from NEW_SSA_NAMES so that we don't try to visit its
2727 defining basic block (which most likely doesn't exist). Notice
2728 that we cannot do the same with names in OLD_SSA_NAMES because we
2729 want to replace existing instances. */
2730 if (names_to_release
)
2731 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2732 RESET_BIT (new_ssa_names
, i
);
2734 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2735 names may be considered to be live-in on blocks that contain
2736 definitions for their replacements. */
2737 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
2738 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2740 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2741 OLD_SSA_NAMES, but we have to ignore its definition site. */
2742 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
2744 if (names_to_release
== NULL
|| !bitmap_bit_p (names_to_release
, i
))
2745 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2746 prepare_use_sites_for (ssa_name (i
), insert_phi_p
);
2751 /* Dump all the names replaced by NAME to FILE. */
2754 dump_names_replaced_by (FILE *file
, tree name
)
2760 print_generic_expr (file
, name
, 0);
2761 fprintf (file
, " -> { ");
2763 old_set
= names_replaced_by (name
);
2764 EXECUTE_IF_SET_IN_BITMAP (old_set
, 0, i
, bi
)
2766 print_generic_expr (file
, ssa_name (i
), 0);
2767 fprintf (file
, " ");
2770 fprintf (file
, "}\n");
2774 /* Dump all the names replaced by NAME to stderr. */
2777 debug_names_replaced_by (tree name
)
2779 dump_names_replaced_by (stderr
, name
);
2783 /* Dump SSA update information to FILE. */
2786 dump_update_ssa (FILE *file
)
2791 if (!need_ssa_update_p (cfun
))
2794 if (new_ssa_names
&& sbitmap_first_set_bit (new_ssa_names
) >= 0)
2796 sbitmap_iterator sbi
;
2798 fprintf (file
, "\nSSA replacement table\n");
2799 fprintf (file
, "N_i -> { O_1 ... O_j } means that N_i replaces "
2800 "O_1, ..., O_j\n\n");
2802 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names
, 0, i
, sbi
)
2803 dump_names_replaced_by (file
, ssa_name (i
));
2806 if (symbols_to_rename_set
&& !bitmap_empty_p (symbols_to_rename_set
))
2808 fprintf (file
, "\nSymbols to be put in SSA form\n");
2809 dump_decl_set (file
, symbols_to_rename_set
);
2810 fprintf (file
, "\n");
2813 if (names_to_release
&& !bitmap_empty_p (names_to_release
))
2815 fprintf (file
, "\nSSA names to release after updating the SSA web\n\n");
2816 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2818 print_generic_expr (file
, ssa_name (i
), 0);
2819 fprintf (file
, " ");
2821 fprintf (file
, "\n");
2826 /* Dump SSA update information to stderr. */
2829 debug_update_ssa (void)
2831 dump_update_ssa (stderr
);
2835 /* Initialize data structures used for incremental SSA updates. */
2838 init_update_ssa (struct function
*fn
)
2840 /* Reserve more space than the current number of names. The calls to
2841 add_new_name_mapping are typically done after creating new SSA
2842 names, so we'll need to reallocate these arrays. */
2843 old_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2844 sbitmap_zero (old_ssa_names
);
2846 new_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2847 sbitmap_zero (new_ssa_names
);
2849 bitmap_obstack_initialize (&update_ssa_obstack
);
2851 names_to_release
= NULL
;
2852 update_ssa_initialized_fn
= fn
;
2856 /* Deallocate data structures used for incremental SSA updates. */
2859 delete_update_ssa (void)
2864 sbitmap_free (old_ssa_names
);
2865 old_ssa_names
= NULL
;
2867 sbitmap_free (new_ssa_names
);
2868 new_ssa_names
= NULL
;
2870 BITMAP_FREE (symbols_to_rename_set
);
2871 symbols_to_rename_set
= NULL
;
2872 VEC_free (tree
, heap
, symbols_to_rename
);
2874 if (names_to_release
)
2876 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2877 release_ssa_name (ssa_name (i
));
2878 BITMAP_FREE (names_to_release
);
2881 clear_ssa_name_info ();
2883 fini_ssa_renamer ();
2885 if (blocks_with_phis_to_rewrite
)
2886 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite
, 0, i
, bi
)
2888 gimple_vec phis
= VEC_index (gimple_vec
, phis_to_rewrite
, i
);
2890 VEC_free (gimple
, heap
, phis
);
2891 VEC_replace (gimple_vec
, phis_to_rewrite
, i
, NULL
);
2894 BITMAP_FREE (blocks_with_phis_to_rewrite
);
2895 BITMAP_FREE (blocks_to_update
);
2897 update_ssa_initialized_fn
= NULL
;
2901 /* Create a new name for OLD_NAME in statement STMT and replace the
2902 operand pointed to by DEF_P with the newly created name. Return
2903 the new name and register the replacement mapping <NEW, OLD> in
2904 update_ssa's tables. */
2907 create_new_def_for (tree old_name
, gimple stmt
, def_operand_p def
)
2909 tree new_name
= duplicate_ssa_name (old_name
, stmt
);
2911 SET_DEF (def
, new_name
);
2913 if (gimple_code (stmt
) == GIMPLE_PHI
)
2915 basic_block bb
= gimple_bb (stmt
);
2917 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2918 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name
) = bb_has_abnormal_pred (bb
);
2921 register_new_name_mapping (new_name
, old_name
);
2923 /* For the benefit of passes that will be updating the SSA form on
2924 their own, set the current reaching definition of OLD_NAME to be
2926 get_ssa_name_ann (old_name
)->info
.current_def
= new_name
;
2932 /* Register name NEW to be a replacement for name OLD. This function
2933 must be called for every replacement that should be performed by
2937 register_new_name_mapping (tree new_tree
, tree old
)
2939 if (!update_ssa_initialized_fn
)
2940 init_update_ssa (cfun
);
2942 gcc_assert (update_ssa_initialized_fn
== cfun
);
2944 add_new_name_mapping (new_tree
, old
);
2948 /* Mark virtual operands of FN for renaming by update_ssa. */
2951 mark_virtual_operands_for_renaming (struct function
*fn
)
2953 fn
->gimple_df
->ssa_renaming_needed
= 1;
2954 fn
->gimple_df
->rename_vops
= 1;
2958 /* Return true if there is any work to be done by update_ssa
2962 need_ssa_update_p (struct function
*fn
)
2964 gcc_assert (fn
!= NULL
);
2965 return (update_ssa_initialized_fn
== fn
2966 || (fn
->gimple_df
&& fn
->gimple_df
->ssa_renaming_needed
));
2969 /* Return true if name N has been registered in the replacement table. */
2972 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED
)
2974 if (!update_ssa_initialized_fn
)
2977 gcc_assert (update_ssa_initialized_fn
== cfun
);
2979 return is_new_name (n
) || is_old_name (n
);
2983 /* Mark NAME to be released after update_ssa has finished. */
2986 release_ssa_name_after_update_ssa (tree name
)
2988 gcc_assert (cfun
&& update_ssa_initialized_fn
== cfun
);
2990 if (names_to_release
== NULL
)
2991 names_to_release
= BITMAP_ALLOC (NULL
);
2993 bitmap_set_bit (names_to_release
, SSA_NAME_VERSION (name
));
2997 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2998 frontier information. BLOCKS is the set of blocks to be updated.
3000 This is slightly different than the regular PHI insertion
3001 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
3002 real names (i.e., GIMPLE registers) are inserted:
3004 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
3005 nodes inside the region affected by the block that defines VAR
3006 and the blocks that define all its replacements. All these
3007 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
3009 First, we compute the entry point to the region (ENTRY). This is
3010 given by the nearest common dominator to all the definition
3011 blocks. When computing the iterated dominance frontier (IDF), any
3012 block not strictly dominated by ENTRY is ignored.
3014 We then call the standard PHI insertion algorithm with the pruned
3017 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
3018 names is not pruned. PHI nodes are inserted at every IDF block. */
3021 insert_updated_phi_nodes_for (tree var
, bitmap_head
*dfs
, bitmap blocks
,
3022 unsigned update_flags
)
3025 struct def_blocks_d
*db
;
3026 bitmap idf
, pruned_idf
;
3030 if (TREE_CODE (var
) == SSA_NAME
)
3031 gcc_checking_assert (is_old_name (var
));
3033 gcc_checking_assert (marked_for_renaming (var
));
3035 /* Get all the definition sites for VAR. */
3036 db
= find_def_blocks_for (var
);
3038 /* No need to do anything if there were no definitions to VAR. */
3039 if (db
== NULL
|| bitmap_empty_p (db
->def_blocks
))
3042 /* Compute the initial iterated dominance frontier. */
3043 idf
= compute_idf (db
->def_blocks
, dfs
);
3044 pruned_idf
= BITMAP_ALLOC (NULL
);
3046 if (TREE_CODE (var
) == SSA_NAME
)
3048 if (update_flags
== TODO_update_ssa
)
3050 /* If doing regular SSA updates for GIMPLE registers, we are
3051 only interested in IDF blocks dominated by the nearest
3052 common dominator of all the definition blocks. */
3053 entry
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3055 if (entry
!= ENTRY_BLOCK_PTR
)
3056 EXECUTE_IF_SET_IN_BITMAP (idf
, 0, i
, bi
)
3057 if (BASIC_BLOCK (i
) != entry
3058 && dominated_by_p (CDI_DOMINATORS
, BASIC_BLOCK (i
), entry
))
3059 bitmap_set_bit (pruned_idf
, i
);
3063 /* Otherwise, do not prune the IDF for VAR. */
3064 gcc_assert (update_flags
== TODO_update_ssa_full_phi
);
3065 bitmap_copy (pruned_idf
, idf
);
3070 /* Otherwise, VAR is a symbol that needs to be put into SSA form
3071 for the first time, so we need to compute the full IDF for
3073 bitmap_copy (pruned_idf
, idf
);
3076 if (!bitmap_empty_p (pruned_idf
))
3078 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3079 are included in the region to be updated. The feeding blocks
3080 are important to guarantee that the PHI arguments are renamed
3083 /* FIXME, this is not needed if we are updating symbols. We are
3084 already starting at the ENTRY block anyway. */
3085 bitmap_ior_into (blocks
, pruned_idf
);
3086 EXECUTE_IF_SET_IN_BITMAP (pruned_idf
, 0, i
, bi
)
3090 basic_block bb
= BASIC_BLOCK (i
);
3092 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3093 if (e
->src
->index
>= 0)
3094 bitmap_set_bit (blocks
, e
->src
->index
);
3097 insert_phi_nodes_for (var
, pruned_idf
, true);
3100 BITMAP_FREE (pruned_idf
);
3105 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3106 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3108 1- The names in OLD_SSA_NAMES dominated by the definitions of
3109 NEW_SSA_NAMES are all re-written to be reached by the
3110 appropriate definition from NEW_SSA_NAMES.
3112 2- If needed, new PHI nodes are added to the iterated dominance
3113 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3115 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3116 calling register_new_name_mapping for every pair of names that the
3117 caller wants to replace.
3119 The caller identifies the new names that have been inserted and the
3120 names that need to be replaced by calling register_new_name_mapping
3121 for every pair <NEW, OLD>. Note that the function assumes that the
3122 new names have already been inserted in the IL.
3124 For instance, given the following code:
3127 2 x_1 = PHI (0, x_5)
3138 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3141 2 x_1 = PHI (0, x_5)
3154 We want to replace all the uses of x_1 with the new definitions of
3155 x_10 and x_11. Note that the only uses that should be replaced are
3156 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3157 *not* be replaced (this is why we cannot just mark symbol 'x' for
3160 Additionally, we may need to insert a PHI node at line 11 because
3161 that is a merge point for x_10 and x_11. So the use of x_1 at line
3162 11 will be replaced with the new PHI node. The insertion of PHI
3163 nodes is optional. They are not strictly necessary to preserve the
3164 SSA form, and depending on what the caller inserted, they may not
3165 even be useful for the optimizers. UPDATE_FLAGS controls various
3166 aspects of how update_ssa operates, see the documentation for
3167 TODO_update_ssa*. */
3170 update_ssa (unsigned update_flags
)
3172 basic_block bb
, start_bb
;
3176 sbitmap_iterator sbi
;
3179 /* Only one update flag should be set. */
3180 gcc_assert (update_flags
== TODO_update_ssa
3181 || update_flags
== TODO_update_ssa_no_phi
3182 || update_flags
== TODO_update_ssa_full_phi
3183 || update_flags
== TODO_update_ssa_only_virtuals
);
3185 if (!need_ssa_update_p (cfun
))
3188 timevar_push (TV_TREE_SSA_INCREMENTAL
);
3190 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3191 fprintf (dump_file
, "\nUpdating SSA:\n");
3193 if (!update_ssa_initialized_fn
)
3194 init_update_ssa (cfun
);
3195 else if (update_flags
== TODO_update_ssa_only_virtuals
)
3197 /* If we only need to update virtuals, remove all the mappings for
3198 real names before proceeding. The caller is responsible for
3199 having dealt with the name mappings before calling update_ssa. */
3200 sbitmap_zero (old_ssa_names
);
3201 sbitmap_zero (new_ssa_names
);
3204 gcc_assert (update_ssa_initialized_fn
== cfun
);
3206 blocks_with_phis_to_rewrite
= BITMAP_ALLOC (NULL
);
3207 if (!phis_to_rewrite
)
3208 phis_to_rewrite
= VEC_alloc (gimple_vec
, heap
, last_basic_block
+ 1);
3209 blocks_to_update
= BITMAP_ALLOC (NULL
);
3211 /* Ensure that the dominance information is up-to-date. */
3212 calculate_dominance_info (CDI_DOMINATORS
);
3214 insert_phi_p
= (update_flags
!= TODO_update_ssa_no_phi
);
3216 /* If there are names defined in the replacement table, prepare
3217 definition and use sites for all the names in NEW_SSA_NAMES and
3219 if (sbitmap_first_set_bit (new_ssa_names
) >= 0)
3221 prepare_names_to_update (insert_phi_p
);
3223 /* If all the names in NEW_SSA_NAMES had been marked for
3224 removal, and there are no symbols to rename, then there's
3225 nothing else to do. */
3226 if (sbitmap_first_set_bit (new_ssa_names
) < 0
3227 && !cfun
->gimple_df
->ssa_renaming_needed
)
3231 /* Next, determine the block at which to start the renaming process. */
3232 if (cfun
->gimple_df
->ssa_renaming_needed
)
3234 /* If we rename bare symbols initialize the mapping to
3235 auxiliar info we need to keep track of. */
3236 var_infos
= htab_create (47, var_info_hash
, var_info_eq
, NULL
);
3238 /* If we have to rename some symbols from scratch, we need to
3239 start the process at the root of the CFG. FIXME, it should
3240 be possible to determine the nearest block that had a
3241 definition for each of the symbols that are marked for
3242 updating. For now this seems more work than it's worth. */
3243 start_bb
= ENTRY_BLOCK_PTR
;
3245 /* Traverse the CFG looking for existing definitions and uses of
3246 symbols in SSA operands. Mark interesting blocks and
3247 statements and set local live-in information for the PHI
3248 placement heuristics. */
3249 prepare_block_for_update (start_bb
, insert_phi_p
);
3253 /* Otherwise, the entry block to the region is the nearest
3254 common dominator for the blocks in BLOCKS. */
3255 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3259 /* If requested, insert PHI nodes at the iterated dominance frontier
3260 of every block, creating new definitions for names in OLD_SSA_NAMES
3261 and for symbols found. */
3266 /* If the caller requested PHI nodes to be added, compute
3267 dominance frontiers. */
3268 dfs
= XNEWVEC (bitmap_head
, last_basic_block
);
3270 bitmap_initialize (&dfs
[bb
->index
], &bitmap_default_obstack
);
3271 compute_dominance_frontiers (dfs
);
3273 if (sbitmap_first_set_bit (old_ssa_names
) >= 0)
3275 sbitmap_iterator sbi
;
3277 /* insert_update_phi_nodes_for will call add_new_name_mapping
3278 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3279 will grow while we are traversing it (but it will not
3280 gain any new members). Copy OLD_SSA_NAMES to a temporary
3282 sbitmap tmp
= sbitmap_alloc (SBITMAP_SIZE (old_ssa_names
));
3283 sbitmap_copy (tmp
, old_ssa_names
);
3284 EXECUTE_IF_SET_IN_SBITMAP (tmp
, 0, i
, sbi
)
3285 insert_updated_phi_nodes_for (ssa_name (i
), dfs
, blocks_to_update
,
3290 FOR_EACH_VEC_ELT (tree
, symbols_to_rename
, i
, sym
)
3291 insert_updated_phi_nodes_for (sym
, dfs
, blocks_to_update
,
3295 bitmap_clear (&dfs
[bb
->index
]);
3298 /* Insertion of PHI nodes may have added blocks to the region.
3299 We need to re-compute START_BB to include the newly added
3301 if (start_bb
!= ENTRY_BLOCK_PTR
)
3302 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3306 /* Reset the current definition for name and symbol before renaming
3308 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names
, 0, i
, sbi
)
3309 get_ssa_name_ann (ssa_name (i
))->info
.current_def
= NULL_TREE
;
3311 FOR_EACH_VEC_ELT (tree
, symbols_to_rename
, i
, sym
)
3312 get_var_info (sym
)->info
.current_def
= NULL_TREE
;
3314 /* Now start the renaming process at START_BB. */
3315 interesting_blocks
= sbitmap_alloc (last_basic_block
);
3316 sbitmap_zero (interesting_blocks
);
3317 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3318 SET_BIT (interesting_blocks
, i
);
3320 rewrite_blocks (start_bb
, REWRITE_UPDATE
);
3322 sbitmap_free (interesting_blocks
);
3324 /* Debugging dumps. */
3330 dump_update_ssa (dump_file
);
3332 fprintf (dump_file
, "Incremental SSA update started at block: %d\n",
3336 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3338 fprintf (dump_file
, "Number of blocks in CFG: %d\n", last_basic_block
);
3339 fprintf (dump_file
, "Number of blocks to update: %d (%3.0f%%)\n",
3340 c
, PERCENT (c
, last_basic_block
));
3342 if (dump_flags
& TDF_DETAILS
)
3344 fprintf (dump_file
, "Affected blocks:");
3345 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3346 fprintf (dump_file
, " %u", i
);
3347 fprintf (dump_file
, "\n");
3350 fprintf (dump_file
, "\n\n");
3353 /* Free allocated memory. */
3355 delete_update_ssa ();
3357 timevar_pop (TV_TREE_SSA_INCREMENTAL
);