1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001-2013 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "langhooks.h"
29 #include "basic-block.h"
31 #include "gimple-pretty-print.h"
35 #include "tree-inline.h"
36 #include "hash-table.h"
37 #include "tree-pass.h"
41 #include "diagnostic-core.h"
44 /* This file builds the SSA form for a function as described in:
45 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
46 Computing Static Single Assignment Form and the Control Dependence
47 Graph. ACM Transactions on Programming Languages and Systems,
48 13(4):451-490, October 1991. */
50 /* Structure to map a variable VAR to the set of blocks that contain
51 definitions for VAR. */
54 /* Blocks that contain definitions of VAR. Bit I will be set if the
55 Ith block contains a definition of VAR. */
58 /* Blocks that contain a PHI node for VAR. */
61 /* Blocks where VAR is live-on-entry. Similar semantics as
66 typedef struct def_blocks_d
*def_blocks_p
;
69 /* Stack of trees used to restore the global currdefs to its original
70 state after completing rewriting of a block and its dominator
71 children. Its elements have the following properties:
73 - An SSA_NAME (N) indicates that the current definition of the
74 underlying variable should be set to the given SSA_NAME. If the
75 symbol associated with the SSA_NAME is not a GIMPLE register, the
76 next slot in the stack must be a _DECL node (SYM). In this case,
77 the name N in the previous slot is the current reaching
80 - A _DECL node indicates that the underlying variable has no
83 - A NULL node at the top entry is used to mark the last slot
84 associated with the current block. */
85 static vec
<tree
> block_defs_stack
;
88 /* Set of existing SSA names being replaced by update_ssa. */
89 static sbitmap old_ssa_names
;
91 /* Set of new SSA names being added by update_ssa. Note that both
92 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
93 the operations done on them are presence tests. */
94 static sbitmap new_ssa_names
;
96 static sbitmap interesting_blocks
;
98 /* Set of SSA names that have been marked to be released after they
99 were registered in the replacement table. They will be finally
100 released after we finish updating the SSA web. */
101 static bitmap names_to_release
;
103 /* vec of vec of PHIs to rewrite in a basic block. Element I corresponds
104 the to basic block with index I. Allocated once per compilation, *not*
105 released between different functions. */
106 static vec
<gimple_vec
> phis_to_rewrite
;
108 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
109 static bitmap blocks_with_phis_to_rewrite
;
111 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
112 to grow as the callers to create_new_def_for will create new names on
114 FIXME. Currently set to 1/3 to avoid frequent reallocations but still
115 need to find a reasonable growth strategy. */
116 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
119 /* The function the SSA updating data structures have been initialized for.
120 NULL if they need to be initialized by create_new_def_for. */
121 static struct function
*update_ssa_initialized_fn
= NULL
;
123 /* Global data to attach to the main dominator walk structure. */
124 struct mark_def_sites_global_data
126 /* This bitmap contains the variables which are set before they
127 are used in a basic block. */
131 /* Information stored for both SSA names and decls. */
134 /* This field indicates whether or not the variable may need PHI nodes.
135 See the enum's definition for more detailed information about the
137 ENUM_BITFIELD (need_phi_state
) need_phi_state
: 2;
139 /* The current reaching definition replacing this var. */
142 /* Definitions for this var. */
143 struct def_blocks_d def_blocks
;
146 /* The information associated with decls and SSA names. */
147 typedef struct common_info_d
*common_info_p
;
149 /* Information stored for decls. */
155 /* Information stored for both SSA names and decls. */
156 struct common_info_d info
;
159 /* The information associated with decls. */
160 typedef struct var_info_d
*var_info_p
;
163 /* VAR_INFOS hashtable helpers. */
165 struct var_info_hasher
: typed_free_remove
<var_info_d
>
167 typedef var_info_d value_type
;
168 typedef var_info_d compare_type
;
169 static inline hashval_t
hash (const value_type
*);
170 static inline bool equal (const value_type
*, const compare_type
*);
174 var_info_hasher::hash (const value_type
*p
)
176 return DECL_UID (p
->var
);
180 var_info_hasher::equal (const value_type
*p1
, const compare_type
*p2
)
182 return p1
->var
== p2
->var
;
186 /* Each entry in VAR_INFOS contains an element of type STRUCT
188 static hash_table
<var_info_hasher
> var_infos
;
191 /* Information stored for SSA names. */
194 /* Age of this record (so that info_for_ssa_name table can be cleared
195 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
196 are assumed to be null. */
199 /* Replacement mappings, allocated from update_ssa_obstack. */
202 /* Information stored for both SSA names and decls. */
203 struct common_info_d info
;
206 /* The information associated with names. */
207 typedef struct ssa_name_info
*ssa_name_info_p
;
209 static vec
<ssa_name_info_p
> info_for_ssa_name
;
210 static unsigned current_info_for_ssa_name_age
;
212 static bitmap_obstack update_ssa_obstack
;
214 /* The set of blocks affected by update_ssa. */
215 static bitmap blocks_to_update
;
217 /* The main entry point to the SSA renamer (rewrite_blocks) may be
218 called several times to do different, but related, tasks.
219 Initially, we need it to rename the whole program into SSA form.
220 At other times, we may need it to only rename into SSA newly
221 exposed symbols. Finally, we can also call it to incrementally fix
222 an already built SSA web. */
224 /* Convert the whole function into SSA form. */
227 /* Incrementally update the SSA web by replacing existing SSA
228 names with new ones. See update_ssa for details. */
235 /* Prototypes for debugging functions. */
236 extern void dump_tree_ssa (FILE *);
237 extern void debug_tree_ssa (void);
238 extern void debug_def_blocks (void);
239 extern void dump_tree_ssa_stats (FILE *);
240 extern void debug_tree_ssa_stats (void);
241 extern void dump_update_ssa (FILE *);
242 extern void debug_update_ssa (void);
243 extern void dump_names_replaced_by (FILE *, tree
);
244 extern void debug_names_replaced_by (tree
);
245 extern void dump_var_infos (FILE *);
246 extern void debug_var_infos (void);
247 extern void dump_defs_stack (FILE *, int);
248 extern void debug_defs_stack (int);
249 extern void dump_currdefs (FILE *);
250 extern void debug_currdefs (void);
253 /* The set of symbols we ought to re-write into SSA form in update_ssa. */
254 static bitmap symbols_to_rename_set
;
255 static vec
<tree
> symbols_to_rename
;
257 /* Mark SYM for renaming. */
260 mark_for_renaming (tree sym
)
262 if (!symbols_to_rename_set
)
263 symbols_to_rename_set
= BITMAP_ALLOC (NULL
);
264 if (bitmap_set_bit (symbols_to_rename_set
, DECL_UID (sym
)))
265 symbols_to_rename
.safe_push (sym
);
268 /* Return true if SYM is marked for renaming. */
271 marked_for_renaming (tree sym
)
273 if (!symbols_to_rename_set
|| sym
== NULL_TREE
)
275 return bitmap_bit_p (symbols_to_rename_set
, DECL_UID (sym
));
279 /* Return true if STMT needs to be rewritten. When renaming a subset
280 of the variables, not all statements will be processed. This is
281 decided in mark_def_sites. */
284 rewrite_uses_p (gimple stmt
)
286 return gimple_visited_p (stmt
);
290 /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
293 set_rewrite_uses (gimple stmt
, bool rewrite_p
)
295 gimple_set_visited (stmt
, rewrite_p
);
299 /* Return true if the DEFs created by statement STMT should be
300 registered when marking new definition sites. This is slightly
301 different than rewrite_uses_p: it's used by update_ssa to
302 distinguish statements that need to have both uses and defs
303 processed from those that only need to have their defs processed.
304 Statements that define new SSA names only need to have their defs
305 registered, but they don't need to have their uses renamed. */
308 register_defs_p (gimple stmt
)
310 return gimple_plf (stmt
, GF_PLF_1
) != 0;
314 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
317 set_register_defs (gimple stmt
, bool register_defs_p
)
319 gimple_set_plf (stmt
, GF_PLF_1
, register_defs_p
);
323 /* Get the information associated with NAME. */
325 static inline ssa_name_info_p
326 get_ssa_name_ann (tree name
)
328 unsigned ver
= SSA_NAME_VERSION (name
);
329 unsigned len
= info_for_ssa_name
.length ();
330 struct ssa_name_info
*info
;
332 /* Re-allocate the vector at most once per update/into-SSA. */
334 info_for_ssa_name
.safe_grow_cleared (num_ssa_names
);
336 /* But allocate infos lazily. */
337 info
= info_for_ssa_name
[ver
];
340 info
= XCNEW (struct ssa_name_info
);
341 info
->age
= current_info_for_ssa_name_age
;
342 info
->info
.need_phi_state
= NEED_PHI_STATE_UNKNOWN
;
343 info_for_ssa_name
[ver
] = info
;
346 if (info
->age
< current_info_for_ssa_name_age
)
348 info
->age
= current_info_for_ssa_name_age
;
349 info
->repl_set
= NULL
;
350 info
->info
.need_phi_state
= NEED_PHI_STATE_UNKNOWN
;
351 info
->info
.current_def
= NULL_TREE
;
352 info
->info
.def_blocks
.def_blocks
= NULL
;
353 info
->info
.def_blocks
.phi_blocks
= NULL
;
354 info
->info
.def_blocks
.livein_blocks
= NULL
;
360 /* Return and allocate the auxiliar information for DECL. */
362 static inline var_info_p
363 get_var_info (tree decl
)
365 struct var_info_d vi
;
368 slot
= var_infos
.find_slot_with_hash (&vi
, DECL_UID (decl
), INSERT
);
371 var_info_p v
= XCNEW (struct var_info_d
);
380 /* Clears info for SSA names. */
383 clear_ssa_name_info (void)
385 current_info_for_ssa_name_age
++;
387 /* If current_info_for_ssa_name_age wraps we use stale information.
388 Asser that this does not happen. */
389 gcc_assert (current_info_for_ssa_name_age
!= 0);
393 /* Get access to the auxiliar information stored per SSA name or decl. */
395 static inline common_info_p
396 get_common_info (tree var
)
398 if (TREE_CODE (var
) == SSA_NAME
)
399 return &get_ssa_name_ann (var
)->info
;
401 return &get_var_info (var
)->info
;
405 /* Return the current definition for VAR. */
408 get_current_def (tree var
)
410 return get_common_info (var
)->current_def
;
414 /* Sets current definition of VAR to DEF. */
417 set_current_def (tree var
, tree def
)
419 get_common_info (var
)->current_def
= def
;
422 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
423 all statements in basic block BB. */
426 initialize_flags_in_bb (basic_block bb
)
429 gimple_stmt_iterator gsi
;
431 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
433 gimple phi
= gsi_stmt (gsi
);
434 set_rewrite_uses (phi
, false);
435 set_register_defs (phi
, false);
438 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
440 stmt
= gsi_stmt (gsi
);
442 /* We are going to use the operand cache API, such as
443 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
444 cache for each statement should be up-to-date. */
445 gcc_checking_assert (!gimple_modified_p (stmt
));
446 set_rewrite_uses (stmt
, false);
447 set_register_defs (stmt
, false);
451 /* Mark block BB as interesting for update_ssa. */
454 mark_block_for_update (basic_block bb
)
456 gcc_checking_assert (blocks_to_update
!= NULL
);
457 if (!bitmap_set_bit (blocks_to_update
, bb
->index
))
459 initialize_flags_in_bb (bb
);
462 /* Return the set of blocks where variable VAR is defined and the blocks
463 where VAR is live on entry (livein). If no entry is found in
464 DEF_BLOCKS, a new one is created and returned. */
466 static inline struct def_blocks_d
*
467 get_def_blocks_for (common_info_p info
)
469 struct def_blocks_d
*db_p
= &info
->def_blocks
;
470 if (!db_p
->def_blocks
)
472 db_p
->def_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
473 db_p
->phi_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
474 db_p
->livein_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
481 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
482 VAR is defined by a PHI node. */
485 set_def_block (tree var
, basic_block bb
, bool phi_p
)
487 struct def_blocks_d
*db_p
;
490 info
= get_common_info (var
);
491 db_p
= get_def_blocks_for (info
);
493 /* Set the bit corresponding to the block where VAR is defined. */
494 bitmap_set_bit (db_p
->def_blocks
, bb
->index
);
496 bitmap_set_bit (db_p
->phi_blocks
, bb
->index
);
498 /* Keep track of whether or not we may need to insert PHI nodes.
500 If we are in the UNKNOWN state, then this is the first definition
501 of VAR. Additionally, we have not seen any uses of VAR yet, so
502 we do not need a PHI node for this variable at this time (i.e.,
503 transition to NEED_PHI_STATE_NO).
505 If we are in any other state, then we either have multiple definitions
506 of this variable occurring in different blocks or we saw a use of the
507 variable which was not dominated by the block containing the
508 definition(s). In this case we may need a PHI node, so enter
509 state NEED_PHI_STATE_MAYBE. */
510 if (info
->need_phi_state
== NEED_PHI_STATE_UNKNOWN
)
511 info
->need_phi_state
= NEED_PHI_STATE_NO
;
513 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
517 /* Mark block BB as having VAR live at the entry to BB. */
520 set_livein_block (tree var
, basic_block bb
)
523 struct def_blocks_d
*db_p
;
525 info
= get_common_info (var
);
526 db_p
= get_def_blocks_for (info
);
528 /* Set the bit corresponding to the block where VAR is live in. */
529 bitmap_set_bit (db_p
->livein_blocks
, bb
->index
);
531 /* Keep track of whether or not we may need to insert PHI nodes.
533 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
534 by the single block containing the definition(s) of this variable. If
535 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
536 NEED_PHI_STATE_MAYBE. */
537 if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
539 int def_block_index
= bitmap_first_set_bit (db_p
->def_blocks
);
541 if (def_block_index
== -1
542 || ! dominated_by_p (CDI_DOMINATORS
, bb
,
543 BASIC_BLOCK (def_block_index
)))
544 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
547 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
551 /* Return true if NAME is in OLD_SSA_NAMES. */
554 is_old_name (tree name
)
556 unsigned ver
= SSA_NAME_VERSION (name
);
559 return (ver
< SBITMAP_SIZE (new_ssa_names
)
560 && bitmap_bit_p (old_ssa_names
, ver
));
564 /* Return true if NAME is in NEW_SSA_NAMES. */
567 is_new_name (tree name
)
569 unsigned ver
= SSA_NAME_VERSION (name
);
572 return (ver
< SBITMAP_SIZE (new_ssa_names
)
573 && bitmap_bit_p (new_ssa_names
, ver
));
577 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
580 names_replaced_by (tree new_tree
)
582 return get_ssa_name_ann (new_tree
)->repl_set
;
586 /* Add OLD to REPL_TBL[NEW_TREE].SET. */
589 add_to_repl_tbl (tree new_tree
, tree old
)
591 bitmap
*set
= &get_ssa_name_ann (new_tree
)->repl_set
;
593 *set
= BITMAP_ALLOC (&update_ssa_obstack
);
594 bitmap_set_bit (*set
, SSA_NAME_VERSION (old
));
598 /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
599 represents the set of names O_1 ... O_j replaced by N_i. This is
600 used by update_ssa and its helpers to introduce new SSA names in an
601 already formed SSA web. */
604 add_new_name_mapping (tree new_tree
, tree old
)
606 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
607 gcc_checking_assert (new_tree
!= old
608 && SSA_NAME_VAR (new_tree
) == SSA_NAME_VAR (old
));
610 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
611 caller may have created new names since the set was created. */
612 if (SBITMAP_SIZE (new_ssa_names
) <= num_ssa_names
- 1)
614 unsigned int new_sz
= num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
;
615 new_ssa_names
= sbitmap_resize (new_ssa_names
, new_sz
, 0);
616 old_ssa_names
= sbitmap_resize (old_ssa_names
, new_sz
, 0);
619 /* Update the REPL_TBL table. */
620 add_to_repl_tbl (new_tree
, old
);
622 /* If OLD had already been registered as a new name, then all the
623 names that OLD replaces should also be replaced by NEW_TREE. */
624 if (is_new_name (old
))
625 bitmap_ior_into (names_replaced_by (new_tree
), names_replaced_by (old
));
627 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
629 bitmap_set_bit (new_ssa_names
, SSA_NAME_VERSION (new_tree
));
630 bitmap_set_bit (old_ssa_names
, SSA_NAME_VERSION (old
));
634 /* Call back for walk_dominator_tree used to collect definition sites
635 for every variable in the function. For every statement S in block
638 1- Variables defined by S in the DEFS of S are marked in the bitmap
641 2- If S uses a variable VAR and there is no preceding kill of VAR,
642 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
644 This information is used to determine which variables are live
645 across block boundaries to reduce the number of PHI nodes
649 mark_def_sites (basic_block bb
, gimple stmt
, bitmap kills
)
655 /* Since this is the first time that we rewrite the program into SSA
656 form, force an operand scan on every statement. */
659 gcc_checking_assert (blocks_to_update
== NULL
);
660 set_register_defs (stmt
, false);
661 set_rewrite_uses (stmt
, false);
663 if (is_gimple_debug (stmt
))
665 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
667 tree sym
= USE_FROM_PTR (use_p
);
668 gcc_checking_assert (DECL_P (sym
));
669 set_rewrite_uses (stmt
, true);
671 if (rewrite_uses_p (stmt
))
672 bitmap_set_bit (interesting_blocks
, bb
->index
);
676 /* If a variable is used before being set, then the variable is live
677 across a block boundary, so mark it live-on-entry to BB. */
678 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
680 tree sym
= USE_FROM_PTR (use_p
);
681 gcc_checking_assert (DECL_P (sym
));
682 if (!bitmap_bit_p (kills
, DECL_UID (sym
)))
683 set_livein_block (sym
, bb
);
684 set_rewrite_uses (stmt
, true);
687 /* Now process the defs. Mark BB as the definition block and add
688 each def to the set of killed symbols. */
689 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
691 gcc_checking_assert (DECL_P (def
));
692 set_def_block (def
, bb
, false);
693 bitmap_set_bit (kills
, DECL_UID (def
));
694 set_register_defs (stmt
, true);
697 /* If we found the statement interesting then also mark the block BB
699 if (rewrite_uses_p (stmt
) || register_defs_p (stmt
))
700 bitmap_set_bit (interesting_blocks
, bb
->index
);
703 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
704 in the dfs numbering of the dominance tree. */
708 /* Basic block whose index this entry corresponds to. */
711 /* The dfs number of this node. */
715 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
719 cmp_dfsnum (const void *a
, const void *b
)
721 const struct dom_dfsnum
*const da
= (const struct dom_dfsnum
*) a
;
722 const struct dom_dfsnum
*const db
= (const struct dom_dfsnum
*) b
;
724 return (int) da
->dfs_num
- (int) db
->dfs_num
;
727 /* Among the intervals starting at the N points specified in DEFS, find
728 the one that contains S, and return its bb_index. */
731 find_dfsnum_interval (struct dom_dfsnum
*defs
, unsigned n
, unsigned s
)
733 unsigned f
= 0, t
= n
, m
;
738 if (defs
[m
].dfs_num
<= s
)
744 return defs
[f
].bb_index
;
747 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
748 KILLS is a bitmap of blocks where the value is defined before any use. */
751 prune_unused_phi_nodes (bitmap phis
, bitmap kills
, bitmap uses
)
755 unsigned i
, b
, p
, u
, top
;
757 basic_block def_bb
, use_bb
;
761 struct dom_dfsnum
*defs
;
762 unsigned n_defs
, adef
;
764 if (bitmap_empty_p (uses
))
770 /* The phi must dominate a use, or an argument of a live phi. Also, we
771 do not create any phi nodes in def blocks, unless they are also livein. */
772 to_remove
= BITMAP_ALLOC (NULL
);
773 bitmap_and_compl (to_remove
, kills
, uses
);
774 bitmap_and_compl_into (phis
, to_remove
);
775 if (bitmap_empty_p (phis
))
777 BITMAP_FREE (to_remove
);
781 /* We want to remove the unnecessary phi nodes, but we do not want to compute
782 liveness information, as that may be linear in the size of CFG, and if
783 there are lot of different variables to rewrite, this may lead to quadratic
786 Instead, we basically emulate standard dce. We put all uses to worklist,
787 then for each of them find the nearest def that dominates them. If this
788 def is a phi node, we mark it live, and if it was not live before, we
789 add the predecessors of its basic block to the worklist.
791 To quickly locate the nearest def that dominates use, we use dfs numbering
792 of the dominance tree (that is already available in order to speed up
793 queries). For each def, we have the interval given by the dfs number on
794 entry to and on exit from the corresponding subtree in the dominance tree.
795 The nearest dominator for a given use is the smallest of these intervals
796 that contains entry and exit dfs numbers for the basic block with the use.
797 If we store the bounds for all the uses to an array and sort it, we can
798 locate the nearest dominating def in logarithmic time by binary search.*/
799 bitmap_ior (to_remove
, kills
, phis
);
800 n_defs
= bitmap_count_bits (to_remove
);
801 defs
= XNEWVEC (struct dom_dfsnum
, 2 * n_defs
+ 1);
802 defs
[0].bb_index
= 1;
805 EXECUTE_IF_SET_IN_BITMAP (to_remove
, 0, i
, bi
)
807 def_bb
= BASIC_BLOCK (i
);
808 defs
[adef
].bb_index
= i
;
809 defs
[adef
].dfs_num
= bb_dom_dfs_in (CDI_DOMINATORS
, def_bb
);
810 defs
[adef
+ 1].bb_index
= i
;
811 defs
[adef
+ 1].dfs_num
= bb_dom_dfs_out (CDI_DOMINATORS
, def_bb
);
814 BITMAP_FREE (to_remove
);
815 gcc_assert (adef
== 2 * n_defs
+ 1);
816 qsort (defs
, adef
, sizeof (struct dom_dfsnum
), cmp_dfsnum
);
817 gcc_assert (defs
[0].bb_index
== 1);
819 /* Now each DEFS entry contains the number of the basic block to that the
820 dfs number corresponds. Change them to the number of basic block that
821 corresponds to the interval following the dfs number. Also, for the
822 dfs_out numbers, increase the dfs number by one (so that it corresponds
823 to the start of the following interval, not to the end of the current
824 one). We use WORKLIST as a stack. */
825 worklist
.create (n_defs
+ 1);
826 worklist
.quick_push (1);
829 for (i
= 1; i
< adef
; i
++)
831 b
= defs
[i
].bb_index
;
834 /* This is a closing element. Interval corresponding to the top
835 of the stack after removing it follows. */
837 top
= worklist
[worklist
.length () - 1];
838 defs
[n_defs
].bb_index
= top
;
839 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
+ 1;
843 /* Opening element. Nothing to do, just push it to the stack and move
844 it to the correct position. */
845 defs
[n_defs
].bb_index
= defs
[i
].bb_index
;
846 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
;
847 worklist
.quick_push (b
);
851 /* If this interval starts at the same point as the previous one, cancel
853 if (defs
[n_defs
].dfs_num
== defs
[n_defs
- 1].dfs_num
)
854 defs
[n_defs
- 1].bb_index
= defs
[n_defs
].bb_index
;
859 gcc_assert (worklist
.is_empty ());
861 /* Now process the uses. */
862 live_phis
= BITMAP_ALLOC (NULL
);
863 EXECUTE_IF_SET_IN_BITMAP (uses
, 0, i
, bi
)
865 worklist
.safe_push (i
);
868 while (!worklist
.is_empty ())
871 if (b
== ENTRY_BLOCK
)
874 /* If there is a phi node in USE_BB, it is made live. Otherwise,
875 find the def that dominates the immediate dominator of USE_BB
876 (the kill in USE_BB does not dominate the use). */
877 if (bitmap_bit_p (phis
, b
))
881 use_bb
= get_immediate_dominator (CDI_DOMINATORS
, BASIC_BLOCK (b
));
882 p
= find_dfsnum_interval (defs
, n_defs
,
883 bb_dom_dfs_in (CDI_DOMINATORS
, use_bb
));
884 if (!bitmap_bit_p (phis
, p
))
888 /* If the phi node is already live, there is nothing to do. */
889 if (!bitmap_set_bit (live_phis
, p
))
892 /* Add the new uses to the worklist. */
893 def_bb
= BASIC_BLOCK (p
);
894 FOR_EACH_EDGE (e
, ei
, def_bb
->preds
)
897 if (bitmap_bit_p (uses
, u
))
900 /* In case there is a kill directly in the use block, do not record
901 the use (this is also necessary for correctness, as we assume that
902 uses dominated by a def directly in their block have been filtered
904 if (bitmap_bit_p (kills
, u
))
907 bitmap_set_bit (uses
, u
);
908 worklist
.safe_push (u
);
913 bitmap_copy (phis
, live_phis
);
914 BITMAP_FREE (live_phis
);
918 /* Return the set of blocks where variable VAR is defined and the blocks
919 where VAR is live on entry (livein). Return NULL, if no entry is
920 found in DEF_BLOCKS. */
922 static inline struct def_blocks_d
*
923 find_def_blocks_for (tree var
)
925 def_blocks_p p
= &get_common_info (var
)->def_blocks
;
932 /* Marks phi node PHI in basic block BB for rewrite. */
935 mark_phi_for_rewrite (basic_block bb
, gimple phi
)
938 unsigned n
, idx
= bb
->index
;
940 if (rewrite_uses_p (phi
))
943 set_rewrite_uses (phi
, true);
945 if (!blocks_with_phis_to_rewrite
)
948 bitmap_set_bit (blocks_with_phis_to_rewrite
, idx
);
950 n
= (unsigned) last_basic_block
+ 1;
951 if (phis_to_rewrite
.length () < n
)
952 phis_to_rewrite
.safe_grow_cleared (n
);
954 phis
= phis_to_rewrite
[idx
];
957 phis
.safe_push (phi
);
958 phis_to_rewrite
[idx
] = phis
;
961 /* Insert PHI nodes for variable VAR using the iterated dominance
962 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
963 function assumes that the caller is incrementally updating the
964 existing SSA form, in which case VAR may be an SSA name instead of
967 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
968 PHI node for VAR. On exit, only the nodes that received a PHI node
969 for VAR will be present in PHI_INSERTION_POINTS. */
972 insert_phi_nodes_for (tree var
, bitmap phi_insertion_points
, bool update_p
)
979 struct def_blocks_d
*def_map
= find_def_blocks_for (var
);
981 /* Remove the blocks where we already have PHI nodes for VAR. */
982 bitmap_and_compl_into (phi_insertion_points
, def_map
->phi_blocks
);
984 /* Remove obviously useless phi nodes. */
985 prune_unused_phi_nodes (phi_insertion_points
, def_map
->def_blocks
,
986 def_map
->livein_blocks
);
988 /* And insert the PHI nodes. */
989 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points
, 0, bb_index
, bi
)
991 bb
= BASIC_BLOCK (bb_index
);
993 mark_block_for_update (bb
);
995 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
997 fprintf (dump_file
, "creating PHI node in block #%d for ", bb_index
);
998 print_generic_expr (dump_file
, var
, TDF_SLIM
);
999 fprintf (dump_file
, "\n");
1003 if (TREE_CODE (var
) == SSA_NAME
)
1005 /* If we are rewriting SSA names, create the LHS of the PHI
1006 node by duplicating VAR. This is useful in the case of
1007 pointers, to also duplicate pointer attributes (alias
1008 information, in particular). */
1012 gcc_checking_assert (update_p
);
1013 new_lhs
= duplicate_ssa_name (var
, NULL
);
1014 phi
= create_phi_node (new_lhs
, bb
);
1015 add_new_name_mapping (new_lhs
, var
);
1017 /* Add VAR to every argument slot of PHI. We need VAR in
1018 every argument so that rewrite_update_phi_arguments knows
1019 which name is this PHI node replacing. If VAR is a
1020 symbol marked for renaming, this is not necessary, the
1021 renamer will use the symbol on the LHS to get its
1022 reaching definition. */
1023 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1024 add_phi_arg (phi
, var
, e
, UNKNOWN_LOCATION
);
1030 gcc_checking_assert (DECL_P (var
));
1031 phi
= create_phi_node (var
, bb
);
1033 tracked_var
= target_for_debug_bind (var
);
1036 gimple note
= gimple_build_debug_bind (tracked_var
,
1039 gimple_stmt_iterator si
= gsi_after_labels (bb
);
1040 gsi_insert_before (&si
, note
, GSI_SAME_STMT
);
1044 /* Mark this PHI node as interesting for update_ssa. */
1045 set_register_defs (phi
, true);
1046 mark_phi_for_rewrite (bb
, phi
);
1050 /* Sort var_infos after DECL_UID of their var. */
1053 insert_phi_nodes_compare_var_infos (const void *a
, const void *b
)
1055 const struct var_info_d
*defa
= *(struct var_info_d
* const *)a
;
1056 const struct var_info_d
*defb
= *(struct var_info_d
* const *)b
;
1057 if (DECL_UID (defa
->var
) < DECL_UID (defb
->var
))
1063 /* Insert PHI nodes at the dominance frontier of blocks with variable
1064 definitions. DFS contains the dominance frontier information for
1068 insert_phi_nodes (bitmap_head
*dfs
)
1070 hash_table
<var_info_hasher
>::iterator hi
;
1073 vec
<var_info_p
> vars
;
1075 timevar_push (TV_TREE_INSERT_PHI_NODES
);
1077 vars
.create (var_infos
.elements ());
1078 FOR_EACH_HASH_TABLE_ELEMENT (var_infos
, info
, var_info_p
, hi
)
1079 if (info
->info
.need_phi_state
!= NEED_PHI_STATE_NO
)
1080 vars
.quick_push (info
);
1082 /* Do two stages to avoid code generation differences for UID
1083 differences but no UID ordering differences. */
1084 vars
.qsort (insert_phi_nodes_compare_var_infos
);
1086 FOR_EACH_VEC_ELT (vars
, i
, info
)
1088 bitmap idf
= compute_idf (info
->info
.def_blocks
.def_blocks
, dfs
);
1089 insert_phi_nodes_for (info
->var
, idf
, false);
1095 timevar_pop (TV_TREE_INSERT_PHI_NODES
);
1099 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1100 register DEF (an SSA_NAME) to be a new definition for SYM. */
1103 register_new_def (tree def
, tree sym
)
1105 common_info_p info
= get_common_info (sym
);
1108 /* If this variable is set in a single basic block and all uses are
1109 dominated by the set(s) in that single basic block, then there is
1110 no reason to record anything for this variable in the block local
1111 definition stacks. Doing so just wastes time and memory.
1113 This is the same test to prune the set of variables which may
1114 need PHI nodes. So we just use that information since it's already
1115 computed and available for us to use. */
1116 if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
1118 info
->current_def
= def
;
1122 currdef
= info
->current_def
;
1124 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1125 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1126 in the stack so that we know which symbol is being defined by
1127 this SSA name when we unwind the stack. */
1128 if (currdef
&& !is_gimple_reg (sym
))
1129 block_defs_stack
.safe_push (sym
);
1131 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1132 stack is later used by the dominator tree callbacks to restore
1133 the reaching definitions for all the variables defined in the
1134 block after a recursive visit to all its immediately dominated
1135 blocks. If there is no current reaching definition, then just
1136 record the underlying _DECL node. */
1137 block_defs_stack
.safe_push (currdef
? currdef
: sym
);
1139 /* Set the current reaching definition for SYM to be DEF. */
1140 info
->current_def
= def
;
1144 /* Perform a depth-first traversal of the dominator tree looking for
1145 variables to rename. BB is the block where to start searching.
1146 Renaming is a five step process:
1148 1- Every definition made by PHI nodes at the start of the blocks is
1149 registered as the current definition for the corresponding variable.
1151 2- Every statement in BB is rewritten. USE and VUSE operands are
1152 rewritten with their corresponding reaching definition. DEF and
1153 VDEF targets are registered as new definitions.
1155 3- All the PHI nodes in successor blocks of BB are visited. The
1156 argument corresponding to BB is replaced with its current reaching
1159 4- Recursively rewrite every dominator child block of BB.
1161 5- Restore (in reverse order) the current reaching definition for every
1162 new definition introduced in this block. This is done so that when
1163 we return from the recursive call, all the current reaching
1164 definitions are restored to the names that were valid in the
1165 dominator parent of BB. */
1167 /* Return the current definition for variable VAR. If none is found,
1168 create a new SSA name to act as the zeroth definition for VAR. */
1171 get_reaching_def (tree var
)
1173 common_info_p info
= get_common_info (var
);
1176 /* Lookup the current reaching definition for VAR. */
1177 currdef
= info
->current_def
;
1179 /* If there is no reaching definition for VAR, create and register a
1180 default definition for it (if needed). */
1181 if (currdef
== NULL_TREE
)
1183 tree sym
= DECL_P (var
) ? var
: SSA_NAME_VAR (var
);
1184 currdef
= get_or_create_ssa_default_def (cfun
, sym
);
1187 /* Return the current reaching definition for VAR, or the default
1188 definition, if we had to create one. */
1193 /* Helper function for rewrite_stmt. Rewrite uses in a debug stmt. */
1196 rewrite_debug_stmt_uses (gimple stmt
)
1198 use_operand_p use_p
;
1200 bool update
= false;
1202 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1204 tree var
= USE_FROM_PTR (use_p
), def
;
1205 common_info_p info
= get_common_info (var
);
1206 gcc_checking_assert (DECL_P (var
));
1207 def
= info
->current_def
;
1210 if (TREE_CODE (var
) == PARM_DECL
&& single_succ_p (ENTRY_BLOCK_PTR
))
1212 gimple_stmt_iterator gsi
1213 = gsi_after_labels (single_succ (ENTRY_BLOCK_PTR
));
1215 /* Search a few source bind stmts at the start of first bb to
1216 see if a DEBUG_EXPR_DECL can't be reused. */
1218 !gsi_end_p (gsi
) && lim
> 0;
1219 gsi_next (&gsi
), lim
--)
1221 gimple gstmt
= gsi_stmt (gsi
);
1222 if (!gimple_debug_source_bind_p (gstmt
))
1224 if (gimple_debug_source_bind_get_value (gstmt
) == var
)
1226 def
= gimple_debug_source_bind_get_var (gstmt
);
1227 if (TREE_CODE (def
) == DEBUG_EXPR_DECL
)
1233 /* If not, add a new source bind stmt. */
1234 if (def
== NULL_TREE
)
1237 def
= make_node (DEBUG_EXPR_DECL
);
1238 def_temp
= gimple_build_debug_source_bind (def
, var
, NULL
);
1239 DECL_ARTIFICIAL (def
) = 1;
1240 TREE_TYPE (def
) = TREE_TYPE (var
);
1241 DECL_MODE (def
) = DECL_MODE (var
);
1242 gsi
= gsi_after_labels (single_succ (ENTRY_BLOCK_PTR
));
1243 gsi_insert_before (&gsi
, def_temp
, GSI_SAME_STMT
);
1250 /* Check if info->current_def can be trusted. */
1251 basic_block bb
= gimple_bb (stmt
);
1253 = SSA_NAME_IS_DEFAULT_DEF (def
)
1254 ? NULL
: gimple_bb (SSA_NAME_DEF_STMT (def
));
1256 /* If definition is in current bb, it is fine. */
1259 /* If definition bb doesn't dominate the current bb,
1260 it can't be used. */
1261 else if (def_bb
&& !dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
1263 /* If there is just one definition and dominates the current
1265 else if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
1269 struct def_blocks_d
*db_p
= get_def_blocks_for (info
);
1271 /* If there are some non-debug uses in the current bb,
1273 if (bitmap_bit_p (db_p
->livein_blocks
, bb
->index
))
1275 /* Otherwise give up for now. */
1282 gimple_debug_bind_reset_value (stmt
);
1286 SET_USE (use_p
, def
);
1292 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1293 the block with its immediate reaching definitions. Update the current
1294 definition of a variable when a new real or virtual definition is found. */
1297 rewrite_stmt (gimple_stmt_iterator
*si
)
1299 use_operand_p use_p
;
1300 def_operand_p def_p
;
1302 gimple stmt
= gsi_stmt (*si
);
1304 /* If mark_def_sites decided that we don't need to rewrite this
1305 statement, ignore it. */
1306 gcc_assert (blocks_to_update
== NULL
);
1307 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
1310 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1312 fprintf (dump_file
, "Renaming statement ");
1313 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1314 fprintf (dump_file
, "\n");
1317 /* Step 1. Rewrite USES in the statement. */
1318 if (rewrite_uses_p (stmt
))
1320 if (is_gimple_debug (stmt
))
1321 rewrite_debug_stmt_uses (stmt
);
1323 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
1325 tree var
= USE_FROM_PTR (use_p
);
1326 gcc_checking_assert (DECL_P (var
));
1327 SET_USE (use_p
, get_reaching_def (var
));
1331 /* Step 2. Register the statement's DEF operands. */
1332 if (register_defs_p (stmt
))
1333 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_ALL_DEFS
)
1335 tree var
= DEF_FROM_PTR (def_p
);
1339 gcc_checking_assert (DECL_P (var
));
1341 if (gimple_clobber_p (stmt
)
1342 && is_gimple_reg (var
))
1344 /* If we rewrite a DECL into SSA form then drop its
1345 clobber stmts and replace uses with a new default def. */
1346 gcc_checking_assert (TREE_CODE (var
) == VAR_DECL
1347 && !gimple_vdef (stmt
));
1348 gsi_replace (si
, gimple_build_nop (), true);
1349 register_new_def (get_or_create_ssa_default_def (cfun
, var
), var
);
1353 name
= make_ssa_name (var
, stmt
);
1354 SET_DEF (def_p
, name
);
1355 register_new_def (DEF_FROM_PTR (def_p
), var
);
1357 tracked_var
= target_for_debug_bind (var
);
1360 gimple note
= gimple_build_debug_bind (tracked_var
, name
, stmt
);
1361 gsi_insert_after (si
, note
, GSI_SAME_STMT
);
1367 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1368 PHI nodes. For every PHI node found, add a new argument containing the
1369 current reaching definition for the variable and the edge through which
1370 that definition is reaching the PHI node. */
1373 rewrite_add_phi_arguments (basic_block bb
)
1378 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1381 gimple_stmt_iterator gsi
;
1383 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
);
1389 phi
= gsi_stmt (gsi
);
1390 res
= gimple_phi_result (phi
);
1391 currdef
= get_reaching_def (SSA_NAME_VAR (res
));
1392 /* Virtual operand PHI args do not need a location. */
1393 if (virtual_operand_p (res
))
1394 loc
= UNKNOWN_LOCATION
;
1396 loc
= gimple_location (SSA_NAME_DEF_STMT (currdef
));
1397 add_phi_arg (phi
, currdef
, e
, loc
);
1402 /* SSA Rewriting Step 1. Initialization, create a block local stack
1403 of reaching definitions for new SSA names produced in this block
1404 (BLOCK_DEFS). Register new definitions for every PHI node in the
1408 rewrite_enter_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1411 gimple_stmt_iterator gsi
;
1413 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1414 fprintf (dump_file
, "\n\nRenaming block #%d\n\n", bb
->index
);
1416 /* Mark the unwind point for this block. */
1417 block_defs_stack
.safe_push (NULL_TREE
);
1419 /* Step 1. Register new definitions for every PHI node in the block.
1420 Conceptually, all the PHI nodes are executed in parallel and each PHI
1421 node introduces a new version for the associated variable. */
1422 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1424 tree result
= gimple_phi_result (gsi_stmt (gsi
));
1425 register_new_def (result
, SSA_NAME_VAR (result
));
1428 /* Step 2. Rewrite every variable used in each statement in the block
1429 with its immediate reaching definitions. Update the current definition
1430 of a variable when a new real or virtual definition is found. */
1431 if (bitmap_bit_p (interesting_blocks
, bb
->index
))
1432 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1433 rewrite_stmt (&gsi
);
1435 /* Step 3. Visit all the successor blocks of BB looking for PHI nodes.
1436 For every PHI node found, add a new argument containing the current
1437 reaching definition for the variable and the edge through which that
1438 definition is reaching the PHI node. */
1439 rewrite_add_phi_arguments (bb
);
1444 /* Called after visiting all the statements in basic block BB and all
1445 of its dominator children. Restore CURRDEFS to its original value. */
1448 rewrite_leave_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
1449 basic_block bb ATTRIBUTE_UNUSED
)
1451 /* Restore CURRDEFS to its original state. */
1452 while (block_defs_stack
.length () > 0)
1454 tree tmp
= block_defs_stack
.pop ();
1455 tree saved_def
, var
;
1457 if (tmp
== NULL_TREE
)
1460 if (TREE_CODE (tmp
) == SSA_NAME
)
1462 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1463 current definition of its underlying variable. Note that
1464 if the SSA_NAME is not for a GIMPLE register, the symbol
1465 being defined is stored in the next slot in the stack.
1466 This mechanism is needed because an SSA name for a
1467 non-register symbol may be the definition for more than
1468 one symbol (e.g., SFTs, aliased variables, etc). */
1470 var
= SSA_NAME_VAR (saved_def
);
1471 if (!is_gimple_reg (var
))
1472 var
= block_defs_stack
.pop ();
1476 /* If we recorded anything else, it must have been a _DECL
1477 node and its current reaching definition must have been
1483 get_common_info (var
)->current_def
= saved_def
;
1488 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1491 dump_decl_set (FILE *file
, bitmap set
)
1498 fprintf (file
, "{ ");
1500 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
1502 fprintf (file
, "D.%u", i
);
1503 fprintf (file
, " ");
1506 fprintf (file
, "}");
1509 fprintf (file
, "NIL");
1513 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1516 debug_decl_set (bitmap set
)
1518 dump_decl_set (stderr
, set
);
1519 fprintf (stderr
, "\n");
1523 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1524 stack up to a maximum of N levels. If N is -1, the whole stack is
1525 dumped. New levels are created when the dominator tree traversal
1526 used for renaming enters a new sub-tree. */
1529 dump_defs_stack (FILE *file
, int n
)
1533 fprintf (file
, "\n\nRenaming stack");
1535 fprintf (file
, " (up to %d levels)", n
);
1536 fprintf (file
, "\n\n");
1539 fprintf (file
, "Level %d (current level)\n", i
);
1540 for (j
= (int) block_defs_stack
.length () - 1; j
>= 0; j
--)
1544 name
= block_defs_stack
[j
];
1545 if (name
== NULL_TREE
)
1550 fprintf (file
, "\nLevel %d\n", i
);
1561 var
= SSA_NAME_VAR (name
);
1562 if (!is_gimple_reg (var
))
1565 var
= block_defs_stack
[j
];
1569 fprintf (file
, " Previous CURRDEF (");
1570 print_generic_expr (file
, var
, 0);
1571 fprintf (file
, ") = ");
1573 print_generic_expr (file
, name
, 0);
1575 fprintf (file
, "<NIL>");
1576 fprintf (file
, "\n");
1581 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1582 stack up to a maximum of N levels. If N is -1, the whole stack is
1583 dumped. New levels are created when the dominator tree traversal
1584 used for renaming enters a new sub-tree. */
1587 debug_defs_stack (int n
)
1589 dump_defs_stack (stderr
, n
);
1593 /* Dump the current reaching definition of every symbol to FILE. */
1596 dump_currdefs (FILE *file
)
1601 if (symbols_to_rename
.is_empty ())
1604 fprintf (file
, "\n\nCurrent reaching definitions\n\n");
1605 FOR_EACH_VEC_ELT (symbols_to_rename
, i
, var
)
1607 common_info_p info
= get_common_info (var
);
1608 fprintf (file
, "CURRDEF (");
1609 print_generic_expr (file
, var
, 0);
1610 fprintf (file
, ") = ");
1611 if (info
->current_def
)
1612 print_generic_expr (file
, info
->current_def
, 0);
1614 fprintf (file
, "<NIL>");
1615 fprintf (file
, "\n");
1620 /* Dump the current reaching definition of every symbol to stderr. */
1623 debug_currdefs (void)
1625 dump_currdefs (stderr
);
1629 /* Dump SSA information to FILE. */
1632 dump_tree_ssa (FILE *file
)
1634 const char *funcname
1635 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
1637 fprintf (file
, "SSA renaming information for %s\n\n", funcname
);
1639 dump_var_infos (file
);
1640 dump_defs_stack (file
, -1);
1641 dump_currdefs (file
);
1642 dump_tree_ssa_stats (file
);
1646 /* Dump SSA information to stderr. */
1649 debug_tree_ssa (void)
1651 dump_tree_ssa (stderr
);
1655 /* Dump statistics for the hash table HTAB. */
1658 htab_statistics (FILE *file
, hash_table
<var_info_hasher
> htab
)
1660 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
1661 (long) htab
.size (),
1662 (long) htab
.elements (),
1663 htab
.collisions ());
1667 /* Dump SSA statistics on FILE. */
1670 dump_tree_ssa_stats (FILE *file
)
1672 if (var_infos
.is_created ())
1674 fprintf (file
, "\nHash table statistics:\n");
1675 fprintf (file
, " var_infos: ");
1676 htab_statistics (file
, var_infos
);
1677 fprintf (file
, "\n");
1682 /* Dump SSA statistics on stderr. */
1685 debug_tree_ssa_stats (void)
1687 dump_tree_ssa_stats (stderr
);
1691 /* Callback for htab_traverse to dump the VAR_INFOS hash table. */
1694 debug_var_infos_r (var_info_d
**slot
, FILE *file
)
1696 struct var_info_d
*info
= *slot
;
1698 fprintf (file
, "VAR: ");
1699 print_generic_expr (file
, info
->var
, dump_flags
);
1700 bitmap_print (file
, info
->info
.def_blocks
.def_blocks
,
1701 ", DEF_BLOCKS: { ", "}");
1702 bitmap_print (file
, info
->info
.def_blocks
.livein_blocks
,
1703 ", LIVEIN_BLOCKS: { ", "}");
1704 bitmap_print (file
, info
->info
.def_blocks
.phi_blocks
,
1705 ", PHI_BLOCKS: { ", "}\n");
1711 /* Dump the VAR_INFOS hash table on FILE. */
1714 dump_var_infos (FILE *file
)
1716 fprintf (file
, "\n\nDefinition and live-in blocks:\n\n");
1717 if (var_infos
.is_created ())
1718 var_infos
.traverse
<FILE *, debug_var_infos_r
> (file
);
1722 /* Dump the VAR_INFOS hash table on stderr. */
1725 debug_var_infos (void)
1727 dump_var_infos (stderr
);
1731 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1734 register_new_update_single (tree new_name
, tree old_name
)
1736 common_info_p info
= get_common_info (old_name
);
1737 tree currdef
= info
->current_def
;
1739 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1740 This stack is later used by the dominator tree callbacks to
1741 restore the reaching definitions for all the variables
1742 defined in the block after a recursive visit to all its
1743 immediately dominated blocks. */
1744 block_defs_stack
.reserve (2);
1745 block_defs_stack
.quick_push (currdef
);
1746 block_defs_stack
.quick_push (old_name
);
1748 /* Set the current reaching definition for OLD_NAME to be
1750 info
->current_def
= new_name
;
1754 /* Register NEW_NAME to be the new reaching definition for all the
1755 names in OLD_NAMES. Used by the incremental SSA update routines to
1756 replace old SSA names with new ones. */
1759 register_new_update_set (tree new_name
, bitmap old_names
)
1764 EXECUTE_IF_SET_IN_BITMAP (old_names
, 0, i
, bi
)
1765 register_new_update_single (new_name
, ssa_name (i
));
1770 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1771 it is a symbol marked for renaming, replace it with USE_P's current
1772 reaching definition. */
1775 maybe_replace_use (use_operand_p use_p
)
1777 tree rdef
= NULL_TREE
;
1778 tree use
= USE_FROM_PTR (use_p
);
1779 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1781 if (marked_for_renaming (sym
))
1782 rdef
= get_reaching_def (sym
);
1783 else if (is_old_name (use
))
1784 rdef
= get_reaching_def (use
);
1786 if (rdef
&& rdef
!= use
)
1787 SET_USE (use_p
, rdef
);
1791 /* Same as maybe_replace_use, but without introducing default stmts,
1792 returning false to indicate a need to do so. */
1795 maybe_replace_use_in_debug_stmt (use_operand_p use_p
)
1797 tree rdef
= NULL_TREE
;
1798 tree use
= USE_FROM_PTR (use_p
);
1799 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1801 if (marked_for_renaming (sym
))
1802 rdef
= get_var_info (sym
)->info
.current_def
;
1803 else if (is_old_name (use
))
1805 rdef
= get_ssa_name_ann (use
)->info
.current_def
;
1806 /* We can't assume that, if there's no current definition, the
1807 default one should be used. It could be the case that we've
1808 rearranged blocks so that the earlier definition no longer
1809 dominates the use. */
1810 if (!rdef
&& SSA_NAME_IS_DEFAULT_DEF (use
))
1816 if (rdef
&& rdef
!= use
)
1817 SET_USE (use_p
, rdef
);
1819 return rdef
!= NULL_TREE
;
1823 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1824 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1825 register it as the current definition for the names replaced by
1829 maybe_register_def (def_operand_p def_p
, gimple stmt
,
1830 gimple_stmt_iterator gsi
)
1832 tree def
= DEF_FROM_PTR (def_p
);
1833 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
1835 /* If DEF is a naked symbol that needs renaming, create a new
1837 if (marked_for_renaming (sym
))
1843 def
= make_ssa_name (def
, stmt
);
1844 SET_DEF (def_p
, def
);
1846 tracked_var
= target_for_debug_bind (sym
);
1849 gimple note
= gimple_build_debug_bind (tracked_var
, def
, stmt
);
1850 /* If stmt ends the bb, insert the debug stmt on the single
1851 non-EH edge from the stmt. */
1852 if (gsi_one_before_end_p (gsi
) && stmt_ends_bb_p (stmt
))
1854 basic_block bb
= gsi_bb (gsi
);
1857 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1858 if (!(e
->flags
& EDGE_EH
))
1860 gcc_checking_assert (!ef
);
1863 /* If there are other predecessors to ef->dest, then
1864 there must be PHI nodes for the modified
1865 variable, and therefore there will be debug bind
1866 stmts after the PHI nodes. The debug bind notes
1867 we'd insert would force the creation of a new
1868 block (diverging codegen) and be redundant with
1869 the post-PHI bind stmts, so don't add them.
1871 As for the exit edge, there wouldn't be redundant
1872 bind stmts, but there wouldn't be a PC to bind
1873 them to either, so avoid diverging the CFG. */
1874 if (ef
&& single_pred_p (ef
->dest
)
1875 && ef
->dest
!= EXIT_BLOCK_PTR
)
1877 /* If there were PHI nodes in the node, we'd
1878 have to make sure the value we're binding
1879 doesn't need rewriting. But there shouldn't
1880 be PHI nodes in a single-predecessor block,
1881 so we just add the note. */
1882 gsi_insert_on_edge_immediate (ef
, note
);
1886 gsi_insert_after (&gsi
, note
, GSI_SAME_STMT
);
1890 register_new_update_single (def
, sym
);
1894 /* If DEF is a new name, register it as a new definition
1895 for all the names replaced by DEF. */
1896 if (is_new_name (def
))
1897 register_new_update_set (def
, names_replaced_by (def
));
1899 /* If DEF is an old name, register DEF as a new
1900 definition for itself. */
1901 if (is_old_name (def
))
1902 register_new_update_single (def
, def
);
1907 /* Update every variable used in the statement pointed-to by SI. The
1908 statement is assumed to be in SSA form already. Names in
1909 OLD_SSA_NAMES used by SI will be updated to their current reaching
1910 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1911 will be registered as a new definition for their corresponding name
1912 in OLD_SSA_NAMES. */
1915 rewrite_update_stmt (gimple stmt
, gimple_stmt_iterator gsi
)
1917 use_operand_p use_p
;
1918 def_operand_p def_p
;
1921 /* Only update marked statements. */
1922 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
1925 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1927 fprintf (dump_file
, "Updating SSA information for statement ");
1928 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1931 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1932 symbol is marked for renaming. */
1933 if (rewrite_uses_p (stmt
))
1935 if (is_gimple_debug (stmt
))
1937 bool failed
= false;
1939 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1940 if (!maybe_replace_use_in_debug_stmt (use_p
))
1948 /* DOM sometimes threads jumps in such a way that a
1949 debug stmt ends up referencing a SSA variable that no
1950 longer dominates the debug stmt, but such that all
1951 incoming definitions refer to the same definition in
1952 an earlier dominator. We could try to recover that
1953 definition somehow, but this will have to do for now.
1955 Introducing a default definition, which is what
1956 maybe_replace_use() would do in such cases, may
1957 modify code generation, for the otherwise-unused
1958 default definition would never go away, modifying SSA
1959 version numbers all over. */
1960 gimple_debug_bind_reset_value (stmt
);
1966 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
1967 maybe_replace_use (use_p
);
1971 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1972 Also register definitions for names whose underlying symbol is
1973 marked for renaming. */
1974 if (register_defs_p (stmt
))
1975 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_ALL_DEFS
)
1976 maybe_register_def (def_p
, stmt
, gsi
);
1980 /* Visit all the successor blocks of BB looking for PHI nodes. For
1981 every PHI node found, check if any of its arguments is in
1982 OLD_SSA_NAMES. If so, and if the argument has a current reaching
1983 definition, replace it. */
1986 rewrite_update_phi_arguments (basic_block bb
)
1992 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1997 if (!bitmap_bit_p (blocks_with_phis_to_rewrite
, e
->dest
->index
))
2000 phis
= phis_to_rewrite
[e
->dest
->index
];
2001 FOR_EACH_VEC_ELT (phis
, i
, phi
)
2003 tree arg
, lhs_sym
, reaching_def
= NULL
;
2004 use_operand_p arg_p
;
2006 gcc_checking_assert (rewrite_uses_p (phi
));
2008 arg_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
2009 arg
= USE_FROM_PTR (arg_p
);
2011 if (arg
&& !DECL_P (arg
) && TREE_CODE (arg
) != SSA_NAME
)
2014 lhs_sym
= SSA_NAME_VAR (gimple_phi_result (phi
));
2016 if (arg
== NULL_TREE
)
2018 /* When updating a PHI node for a recently introduced
2019 symbol we may find NULL arguments. That's why we
2020 take the symbol from the LHS of the PHI node. */
2021 reaching_def
= get_reaching_def (lhs_sym
);
2026 tree sym
= DECL_P (arg
) ? arg
: SSA_NAME_VAR (arg
);
2028 if (marked_for_renaming (sym
))
2029 reaching_def
= get_reaching_def (sym
);
2030 else if (is_old_name (arg
))
2031 reaching_def
= get_reaching_def (arg
);
2034 /* Update the argument if there is a reaching def. */
2037 source_location locus
;
2038 int arg_i
= PHI_ARG_INDEX_FROM_USE (arg_p
);
2040 SET_USE (arg_p
, reaching_def
);
2042 /* Virtual operands do not need a location. */
2043 if (virtual_operand_p (reaching_def
))
2044 locus
= UNKNOWN_LOCATION
;
2047 gimple stmt
= SSA_NAME_DEF_STMT (reaching_def
);
2049 /* Single element PHI nodes behave like copies, so get the
2050 location from the phi argument. */
2051 if (gimple_code (stmt
) == GIMPLE_PHI
2052 && gimple_phi_num_args (stmt
) == 1)
2053 locus
= gimple_phi_arg_location (stmt
, 0);
2055 locus
= gimple_location (stmt
);
2058 gimple_phi_arg_set_location (phi
, arg_i
, locus
);
2062 if (e
->flags
& EDGE_ABNORMAL
)
2063 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p
)) = 1;
2069 /* Initialization of block data structures for the incremental SSA
2070 update pass. Create a block local stack of reaching definitions
2071 for new SSA names produced in this block (BLOCK_DEFS). Register
2072 new definitions for every PHI node in the block. */
2075 rewrite_update_enter_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
2078 bool is_abnormal_phi
;
2079 gimple_stmt_iterator gsi
;
2081 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2082 fprintf (dump_file
, "Registering new PHI nodes in block #%d\n",
2085 /* Mark the unwind point for this block. */
2086 block_defs_stack
.safe_push (NULL_TREE
);
2088 if (!bitmap_bit_p (blocks_to_update
, bb
->index
))
2091 /* Mark the LHS if any of the arguments flows through an abnormal
2093 is_abnormal_phi
= bb_has_abnormal_pred (bb
);
2095 /* If any of the PHI nodes is a replacement for a name in
2096 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2097 register it as a new definition for its corresponding name. Also
2098 register definitions for names whose underlying symbols are
2099 marked for renaming. */
2100 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2103 gimple phi
= gsi_stmt (gsi
);
2105 if (!register_defs_p (phi
))
2108 lhs
= gimple_phi_result (phi
);
2109 lhs_sym
= SSA_NAME_VAR (lhs
);
2111 if (marked_for_renaming (lhs_sym
))
2112 register_new_update_single (lhs
, lhs_sym
);
2116 /* If LHS is a new name, register a new definition for all
2117 the names replaced by LHS. */
2118 if (is_new_name (lhs
))
2119 register_new_update_set (lhs
, names_replaced_by (lhs
));
2121 /* If LHS is an OLD name, register it as a new definition
2123 if (is_old_name (lhs
))
2124 register_new_update_single (lhs
, lhs
);
2127 if (is_abnormal_phi
)
2128 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
) = 1;
2131 /* Step 2. Rewrite every variable used in each statement in the block. */
2132 if (bitmap_bit_p (interesting_blocks
, bb
->index
))
2134 gcc_checking_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2135 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2136 rewrite_update_stmt (gsi_stmt (gsi
), gsi
);
2139 /* Step 3. Update PHI nodes. */
2140 rewrite_update_phi_arguments (bb
);
2143 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
2144 the current reaching definition of every name re-written in BB to
2145 the original reaching definition before visiting BB. This
2146 unwinding must be done in the opposite order to what is done in
2147 register_new_update_set. */
2150 rewrite_update_leave_block (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
2151 basic_block bb ATTRIBUTE_UNUSED
)
2153 while (block_defs_stack
.length () > 0)
2155 tree var
= block_defs_stack
.pop ();
2158 /* NULL indicates the unwind stop point for this block (see
2159 rewrite_update_enter_block). */
2163 saved_def
= block_defs_stack
.pop ();
2164 get_common_info (var
)->current_def
= saved_def
;
2169 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2172 ENTRY indicates the block where to start. Every block dominated by
2173 ENTRY will be rewritten.
2175 WHAT indicates what actions will be taken by the renamer (see enum
2178 BLOCKS are the set of interesting blocks for the dominator walker
2179 to process. If this set is NULL, then all the nodes dominated
2180 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2181 are not present in BLOCKS are ignored. */
2184 rewrite_blocks (basic_block entry
, enum rewrite_mode what
)
2186 struct dom_walk_data walk_data
;
2188 /* Rewrite all the basic blocks in the program. */
2189 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS
);
2191 /* Setup callbacks for the generic dominator tree walker. */
2192 memset (&walk_data
, 0, sizeof (walk_data
));
2194 walk_data
.dom_direction
= CDI_DOMINATORS
;
2196 if (what
== REWRITE_ALL
)
2198 walk_data
.before_dom_children
= rewrite_enter_block
;
2199 walk_data
.after_dom_children
= rewrite_leave_block
;
2201 else if (what
== REWRITE_UPDATE
)
2203 walk_data
.before_dom_children
= rewrite_update_enter_block
;
2204 walk_data
.after_dom_children
= rewrite_update_leave_block
;
2209 block_defs_stack
.create (10);
2211 /* Initialize the dominator walker. */
2212 init_walk_dominator_tree (&walk_data
);
2214 /* Recursively walk the dominator tree rewriting each statement in
2215 each basic block. */
2216 walk_dominator_tree (&walk_data
, entry
);
2218 /* Finalize the dominator walker. */
2219 fini_walk_dominator_tree (&walk_data
);
2221 /* Debugging dumps. */
2222 if (dump_file
&& (dump_flags
& TDF_STATS
))
2224 dump_dfa_stats (dump_file
);
2225 if (var_infos
.is_created ())
2226 dump_tree_ssa_stats (dump_file
);
2229 block_defs_stack
.release ();
2231 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS
);
2235 /* Block processing routine for mark_def_sites. Clear the KILLS bitmap
2236 at the start of each block, and call mark_def_sites for each statement. */
2239 mark_def_sites_block (struct dom_walk_data
*walk_data
, basic_block bb
)
2241 struct mark_def_sites_global_data
*gd
;
2243 gimple_stmt_iterator gsi
;
2245 gd
= (struct mark_def_sites_global_data
*) walk_data
->global_data
;
2248 bitmap_clear (kills
);
2249 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2250 mark_def_sites (bb
, gsi_stmt (gsi
), kills
);
2254 /* Mark the definition site blocks for each variable, so that we know
2255 where the variable is actually live.
2257 The INTERESTING_BLOCKS global will be filled in with all the blocks
2258 that should be processed by the renamer. It is assumed that the
2259 caller has already initialized and zeroed it. */
2262 mark_def_site_blocks (void)
2264 struct dom_walk_data walk_data
;
2265 struct mark_def_sites_global_data mark_def_sites_global_data
;
2267 /* Setup callbacks for the generic dominator tree walker to find and
2268 mark definition sites. */
2269 walk_data
.dom_direction
= CDI_DOMINATORS
;
2270 walk_data
.initialize_block_local_data
= NULL
;
2271 walk_data
.before_dom_children
= mark_def_sites_block
;
2272 walk_data
.after_dom_children
= NULL
;
2274 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2275 large enough to accommodate all the variables referenced in the
2276 function, not just the ones we are renaming. */
2277 mark_def_sites_global_data
.kills
= BITMAP_ALLOC (NULL
);
2278 walk_data
.global_data
= &mark_def_sites_global_data
;
2280 /* We do not have any local data. */
2281 walk_data
.block_local_data_size
= 0;
2283 /* Initialize the dominator walker. */
2284 init_walk_dominator_tree (&walk_data
);
2286 /* Recursively walk the dominator tree. */
2287 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
2289 /* Finalize the dominator walker. */
2290 fini_walk_dominator_tree (&walk_data
);
2292 /* We no longer need this bitmap, clear and free it. */
2293 BITMAP_FREE (mark_def_sites_global_data
.kills
);
2297 /* Initialize internal data needed during renaming. */
2300 init_ssa_renamer (void)
2302 cfun
->gimple_df
->in_ssa_p
= false;
2304 /* Allocate memory for the DEF_BLOCKS hash table. */
2305 gcc_assert (!var_infos
.is_created ());
2306 var_infos
.create (vec_safe_length (cfun
->local_decls
));
2308 bitmap_obstack_initialize (&update_ssa_obstack
);
2312 /* Deallocate internal data structures used by the renamer. */
2315 fini_ssa_renamer (void)
2317 if (var_infos
.is_created ())
2318 var_infos
.dispose ();
2320 bitmap_obstack_release (&update_ssa_obstack
);
2322 cfun
->gimple_df
->ssa_renaming_needed
= 0;
2323 cfun
->gimple_df
->rename_vops
= 0;
2324 cfun
->gimple_df
->in_ssa_p
= true;
2327 /* Main entry point into the SSA builder. The renaming process
2328 proceeds in four main phases:
2330 1- Compute dominance frontier and immediate dominators, needed to
2331 insert PHI nodes and rename the function in dominator tree
2334 2- Find and mark all the blocks that define variables
2335 (mark_def_site_blocks).
2337 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2339 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2341 Steps 3 and 4 are done using the dominator tree walker
2342 (walk_dominator_tree). */
2345 rewrite_into_ssa (void)
2351 /* Initialize operand data structures. */
2352 init_ssa_operands (cfun
);
2354 /* Initialize internal data needed by the renamer. */
2355 init_ssa_renamer ();
2357 /* Initialize the set of interesting blocks. The callback
2358 mark_def_sites will add to this set those blocks that the renamer
2360 interesting_blocks
= sbitmap_alloc (last_basic_block
);
2361 bitmap_clear (interesting_blocks
);
2363 /* Initialize dominance frontier. */
2364 dfs
= XNEWVEC (bitmap_head
, last_basic_block
);
2366 bitmap_initialize (&dfs
[bb
->index
], &bitmap_default_obstack
);
2368 /* 1- Compute dominance frontiers. */
2369 calculate_dominance_info (CDI_DOMINATORS
);
2370 compute_dominance_frontiers (dfs
);
2372 /* 2- Find and mark definition sites. */
2373 mark_def_site_blocks ();
2375 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2376 insert_phi_nodes (dfs
);
2378 /* 4- Rename all the blocks. */
2379 rewrite_blocks (ENTRY_BLOCK_PTR
, REWRITE_ALL
);
2381 /* Free allocated memory. */
2383 bitmap_clear (&dfs
[bb
->index
]);
2386 sbitmap_free (interesting_blocks
);
2388 fini_ssa_renamer ();
2390 /* Try to get rid of all gimplifier generated temporaries by making
2391 its SSA names anonymous. This way we can garbage collect them
2392 all after removing unused locals which we do in our TODO. */
2393 for (i
= 1; i
< num_ssa_names
; ++i
)
2395 tree decl
, name
= ssa_name (i
);
2397 || SSA_NAME_IS_DEFAULT_DEF (name
))
2399 decl
= SSA_NAME_VAR (name
);
2401 && TREE_CODE (decl
) == VAR_DECL
2402 && !VAR_DECL_IS_VIRTUAL_OPERAND (decl
)
2403 && DECL_ARTIFICIAL (decl
)
2404 && DECL_IGNORED_P (decl
)
2405 && !DECL_NAME (decl
))
2406 SET_SSA_NAME_VAR_OR_IDENTIFIER (name
, NULL_TREE
);
2412 /* Gate for IPCP optimization. */
2415 gate_into_ssa (void)
2417 /* Do nothing for funcions that was produced already in SSA form. */
2418 return !(cfun
->curr_properties
& PROP_ssa
);
2423 const pass_data pass_data_build_ssa
=
2425 GIMPLE_PASS
, /* type */
2427 OPTGROUP_NONE
, /* optinfo_flags */
2428 true, /* has_gate */
2429 true, /* has_execute */
2430 TV_TREE_SSA_OTHER
, /* tv_id */
2431 PROP_cfg
, /* properties_required */
2432 PROP_ssa
, /* properties_provided */
2433 0, /* properties_destroyed */
2434 0, /* todo_flags_start */
2435 ( TODO_verify_ssa
| TODO_remove_unused_locals
), /* todo_flags_finish */
2438 class pass_build_ssa
: public gimple_opt_pass
2441 pass_build_ssa(gcc::context
*ctxt
)
2442 : gimple_opt_pass(pass_data_build_ssa
, ctxt
)
2445 /* opt_pass methods: */
2446 bool gate () { return gate_into_ssa (); }
2447 unsigned int execute () { return rewrite_into_ssa (); }
2449 }; // class pass_build_ssa
2454 make_pass_build_ssa (gcc::context
*ctxt
)
2456 return new pass_build_ssa (ctxt
);
2460 /* Mark the definition of VAR at STMT and BB as interesting for the
2461 renamer. BLOCKS is the set of blocks that need updating. */
2464 mark_def_interesting (tree var
, gimple stmt
, basic_block bb
, bool insert_phi_p
)
2466 gcc_checking_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2467 set_register_defs (stmt
, true);
2471 bool is_phi_p
= gimple_code (stmt
) == GIMPLE_PHI
;
2473 set_def_block (var
, bb
, is_phi_p
);
2475 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2476 site for both itself and all the old names replaced by it. */
2477 if (TREE_CODE (var
) == SSA_NAME
&& is_new_name (var
))
2481 bitmap set
= names_replaced_by (var
);
2483 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
2484 set_def_block (ssa_name (i
), bb
, is_phi_p
);
2490 /* Mark the use of VAR at STMT and BB as interesting for the
2491 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2495 mark_use_interesting (tree var
, gimple stmt
, basic_block bb
, bool insert_phi_p
)
2497 basic_block def_bb
= gimple_bb (stmt
);
2499 mark_block_for_update (def_bb
);
2500 mark_block_for_update (bb
);
2502 if (gimple_code (stmt
) == GIMPLE_PHI
)
2503 mark_phi_for_rewrite (def_bb
, stmt
);
2506 set_rewrite_uses (stmt
, true);
2508 if (is_gimple_debug (stmt
))
2512 /* If VAR has not been defined in BB, then it is live-on-entry
2513 to BB. Note that we cannot just use the block holding VAR's
2514 definition because if VAR is one of the names in OLD_SSA_NAMES,
2515 it will have several definitions (itself and all the names that
2519 struct def_blocks_d
*db_p
= get_def_blocks_for (get_common_info (var
));
2520 if (!bitmap_bit_p (db_p
->def_blocks
, bb
->index
))
2521 set_livein_block (var
, bb
);
2526 /* Do a dominator walk starting at BB processing statements that
2527 reference symbols in SSA operands. This is very similar to
2528 mark_def_sites, but the scan handles statements whose operands may
2529 already be SSA names.
2531 If INSERT_PHI_P is true, mark those uses as live in the
2532 corresponding block. This is later used by the PHI placement
2533 algorithm to make PHI pruning decisions.
2535 FIXME. Most of this would be unnecessary if we could associate a
2536 symbol to all the SSA names that reference it. But that
2537 sounds like it would be expensive to maintain. Still, it
2538 would be interesting to see if it makes better sense to do
2542 prepare_block_for_update (basic_block bb
, bool insert_phi_p
)
2545 gimple_stmt_iterator si
;
2549 mark_block_for_update (bb
);
2551 /* Process PHI nodes marking interesting those that define or use
2552 the symbols that we are interested in. */
2553 for (si
= gsi_start_phis (bb
); !gsi_end_p (si
); gsi_next (&si
))
2555 gimple phi
= gsi_stmt (si
);
2556 tree lhs_sym
, lhs
= gimple_phi_result (phi
);
2558 if (TREE_CODE (lhs
) == SSA_NAME
2559 && (! virtual_operand_p (lhs
)
2560 || ! cfun
->gimple_df
->rename_vops
))
2563 lhs_sym
= DECL_P (lhs
) ? lhs
: SSA_NAME_VAR (lhs
);
2564 mark_for_renaming (lhs_sym
);
2565 mark_def_interesting (lhs_sym
, phi
, bb
, insert_phi_p
);
2567 /* Mark the uses in phi nodes as interesting. It would be more correct
2568 to process the arguments of the phi nodes of the successor edges of
2569 BB at the end of prepare_block_for_update, however, that turns out
2570 to be significantly more expensive. Doing it here is conservatively
2571 correct -- it may only cause us to believe a value to be live in a
2572 block that also contains its definition, and thus insert a few more
2573 phi nodes for it. */
2574 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2575 mark_use_interesting (lhs_sym
, phi
, e
->src
, insert_phi_p
);
2578 /* Process the statements. */
2579 for (si
= gsi_start_bb (bb
); !gsi_end_p (si
); gsi_next (&si
))
2583 use_operand_p use_p
;
2584 def_operand_p def_p
;
2586 stmt
= gsi_stmt (si
);
2588 if (cfun
->gimple_df
->rename_vops
2589 && gimple_vuse (stmt
))
2591 tree use
= gimple_vuse (stmt
);
2592 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
2593 mark_for_renaming (sym
);
2594 mark_use_interesting (sym
, stmt
, bb
, insert_phi_p
);
2597 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, i
, SSA_OP_USE
)
2599 tree use
= USE_FROM_PTR (use_p
);
2602 mark_for_renaming (use
);
2603 mark_use_interesting (use
, stmt
, bb
, insert_phi_p
);
2606 if (cfun
->gimple_df
->rename_vops
2607 && gimple_vdef (stmt
))
2609 tree def
= gimple_vdef (stmt
);
2610 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
2611 mark_for_renaming (sym
);
2612 mark_def_interesting (sym
, stmt
, bb
, insert_phi_p
);
2615 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, i
, SSA_OP_DEF
)
2617 tree def
= DEF_FROM_PTR (def_p
);
2620 mark_for_renaming (def
);
2621 mark_def_interesting (def
, stmt
, bb
, insert_phi_p
);
2625 /* Now visit all the blocks dominated by BB. */
2626 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
2628 son
= next_dom_son (CDI_DOMINATORS
, son
))
2629 prepare_block_for_update (son
, insert_phi_p
);
2633 /* Helper for prepare_names_to_update. Mark all the use sites for
2634 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2635 prepare_names_to_update. */
2638 prepare_use_sites_for (tree name
, bool insert_phi_p
)
2640 use_operand_p use_p
;
2641 imm_use_iterator iter
;
2643 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
2645 gimple stmt
= USE_STMT (use_p
);
2646 basic_block bb
= gimple_bb (stmt
);
2648 if (gimple_code (stmt
) == GIMPLE_PHI
)
2650 int ix
= PHI_ARG_INDEX_FROM_USE (use_p
);
2651 edge e
= gimple_phi_arg_edge (stmt
, ix
);
2652 mark_use_interesting (name
, stmt
, e
->src
, insert_phi_p
);
2656 /* For regular statements, mark this as an interesting use
2658 mark_use_interesting (name
, stmt
, bb
, insert_phi_p
);
2664 /* Helper for prepare_names_to_update. Mark the definition site for
2665 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2666 prepare_names_to_update. */
2669 prepare_def_site_for (tree name
, bool insert_phi_p
)
2674 gcc_checking_assert (names_to_release
== NULL
2675 || !bitmap_bit_p (names_to_release
,
2676 SSA_NAME_VERSION (name
)));
2678 stmt
= SSA_NAME_DEF_STMT (name
);
2679 bb
= gimple_bb (stmt
);
2682 gcc_checking_assert (bb
->index
< last_basic_block
);
2683 mark_block_for_update (bb
);
2684 mark_def_interesting (name
, stmt
, bb
, insert_phi_p
);
2689 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2690 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2691 PHI nodes for newly created names. */
2694 prepare_names_to_update (bool insert_phi_p
)
2698 sbitmap_iterator sbi
;
2700 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2701 remove it from NEW_SSA_NAMES so that we don't try to visit its
2702 defining basic block (which most likely doesn't exist). Notice
2703 that we cannot do the same with names in OLD_SSA_NAMES because we
2704 want to replace existing instances. */
2705 if (names_to_release
)
2706 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2707 bitmap_clear_bit (new_ssa_names
, i
);
2709 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2710 names may be considered to be live-in on blocks that contain
2711 definitions for their replacements. */
2712 EXECUTE_IF_SET_IN_BITMAP (new_ssa_names
, 0, i
, sbi
)
2713 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2715 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2716 OLD_SSA_NAMES, but we have to ignore its definition site. */
2717 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names
, 0, i
, sbi
)
2719 if (names_to_release
== NULL
|| !bitmap_bit_p (names_to_release
, i
))
2720 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2721 prepare_use_sites_for (ssa_name (i
), insert_phi_p
);
2726 /* Dump all the names replaced by NAME to FILE. */
2729 dump_names_replaced_by (FILE *file
, tree name
)
2735 print_generic_expr (file
, name
, 0);
2736 fprintf (file
, " -> { ");
2738 old_set
= names_replaced_by (name
);
2739 EXECUTE_IF_SET_IN_BITMAP (old_set
, 0, i
, bi
)
2741 print_generic_expr (file
, ssa_name (i
), 0);
2742 fprintf (file
, " ");
2745 fprintf (file
, "}\n");
2749 /* Dump all the names replaced by NAME to stderr. */
2752 debug_names_replaced_by (tree name
)
2754 dump_names_replaced_by (stderr
, name
);
2758 /* Dump SSA update information to FILE. */
2761 dump_update_ssa (FILE *file
)
2766 if (!need_ssa_update_p (cfun
))
2769 if (new_ssa_names
&& bitmap_first_set_bit (new_ssa_names
) >= 0)
2771 sbitmap_iterator sbi
;
2773 fprintf (file
, "\nSSA replacement table\n");
2774 fprintf (file
, "N_i -> { O_1 ... O_j } means that N_i replaces "
2775 "O_1, ..., O_j\n\n");
2777 EXECUTE_IF_SET_IN_BITMAP (new_ssa_names
, 0, i
, sbi
)
2778 dump_names_replaced_by (file
, ssa_name (i
));
2781 if (symbols_to_rename_set
&& !bitmap_empty_p (symbols_to_rename_set
))
2783 fprintf (file
, "\nSymbols to be put in SSA form\n");
2784 dump_decl_set (file
, symbols_to_rename_set
);
2785 fprintf (file
, "\n");
2788 if (names_to_release
&& !bitmap_empty_p (names_to_release
))
2790 fprintf (file
, "\nSSA names to release after updating the SSA web\n\n");
2791 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2793 print_generic_expr (file
, ssa_name (i
), 0);
2794 fprintf (file
, " ");
2796 fprintf (file
, "\n");
2801 /* Dump SSA update information to stderr. */
2804 debug_update_ssa (void)
2806 dump_update_ssa (stderr
);
2810 /* Initialize data structures used for incremental SSA updates. */
2813 init_update_ssa (struct function
*fn
)
2815 /* Reserve more space than the current number of names. The calls to
2816 add_new_name_mapping are typically done after creating new SSA
2817 names, so we'll need to reallocate these arrays. */
2818 old_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2819 bitmap_clear (old_ssa_names
);
2821 new_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
2822 bitmap_clear (new_ssa_names
);
2824 bitmap_obstack_initialize (&update_ssa_obstack
);
2826 names_to_release
= NULL
;
2827 update_ssa_initialized_fn
= fn
;
2831 /* Deallocate data structures used for incremental SSA updates. */
2834 delete_update_ssa (void)
2839 sbitmap_free (old_ssa_names
);
2840 old_ssa_names
= NULL
;
2842 sbitmap_free (new_ssa_names
);
2843 new_ssa_names
= NULL
;
2845 BITMAP_FREE (symbols_to_rename_set
);
2846 symbols_to_rename_set
= NULL
;
2847 symbols_to_rename
.release ();
2849 if (names_to_release
)
2851 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2852 release_ssa_name (ssa_name (i
));
2853 BITMAP_FREE (names_to_release
);
2856 clear_ssa_name_info ();
2858 fini_ssa_renamer ();
2860 if (blocks_with_phis_to_rewrite
)
2861 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite
, 0, i
, bi
)
2863 gimple_vec phis
= phis_to_rewrite
[i
];
2865 phis_to_rewrite
[i
].create (0);
2868 BITMAP_FREE (blocks_with_phis_to_rewrite
);
2869 BITMAP_FREE (blocks_to_update
);
2871 update_ssa_initialized_fn
= NULL
;
2875 /* Create a new name for OLD_NAME in statement STMT and replace the
2876 operand pointed to by DEF_P with the newly created name. If DEF_P
2877 is NULL then STMT should be a GIMPLE assignment.
2878 Return the new name and register the replacement mapping <NEW, OLD> in
2879 update_ssa's tables. */
2882 create_new_def_for (tree old_name
, gimple stmt
, def_operand_p def
)
2886 timevar_push (TV_TREE_SSA_INCREMENTAL
);
2888 if (!update_ssa_initialized_fn
)
2889 init_update_ssa (cfun
);
2891 gcc_assert (update_ssa_initialized_fn
== cfun
);
2893 new_name
= duplicate_ssa_name (old_name
, stmt
);
2895 SET_DEF (def
, new_name
);
2897 gimple_assign_set_lhs (stmt
, new_name
);
2899 if (gimple_code (stmt
) == GIMPLE_PHI
)
2901 basic_block bb
= gimple_bb (stmt
);
2903 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2904 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name
) = bb_has_abnormal_pred (bb
);
2907 add_new_name_mapping (new_name
, old_name
);
2909 /* For the benefit of passes that will be updating the SSA form on
2910 their own, set the current reaching definition of OLD_NAME to be
2912 get_ssa_name_ann (old_name
)->info
.current_def
= new_name
;
2914 timevar_pop (TV_TREE_SSA_INCREMENTAL
);
2920 /* Mark virtual operands of FN for renaming by update_ssa. */
2923 mark_virtual_operands_for_renaming (struct function
*fn
)
2925 fn
->gimple_df
->ssa_renaming_needed
= 1;
2926 fn
->gimple_df
->rename_vops
= 1;
2930 /* Return true if there is any work to be done by update_ssa
2934 need_ssa_update_p (struct function
*fn
)
2936 gcc_assert (fn
!= NULL
);
2937 return (update_ssa_initialized_fn
== fn
2938 || (fn
->gimple_df
&& fn
->gimple_df
->ssa_renaming_needed
));
2941 /* Return true if name N has been registered in the replacement table. */
2944 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED
)
2946 if (!update_ssa_initialized_fn
)
2949 gcc_assert (update_ssa_initialized_fn
== cfun
);
2951 return is_new_name (n
) || is_old_name (n
);
2955 /* Mark NAME to be released after update_ssa has finished. */
2958 release_ssa_name_after_update_ssa (tree name
)
2960 gcc_assert (cfun
&& update_ssa_initialized_fn
== cfun
);
2962 if (names_to_release
== NULL
)
2963 names_to_release
= BITMAP_ALLOC (NULL
);
2965 bitmap_set_bit (names_to_release
, SSA_NAME_VERSION (name
));
2969 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2970 frontier information. BLOCKS is the set of blocks to be updated.
2972 This is slightly different than the regular PHI insertion
2973 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
2974 real names (i.e., GIMPLE registers) are inserted:
2976 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2977 nodes inside the region affected by the block that defines VAR
2978 and the blocks that define all its replacements. All these
2979 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2981 First, we compute the entry point to the region (ENTRY). This is
2982 given by the nearest common dominator to all the definition
2983 blocks. When computing the iterated dominance frontier (IDF), any
2984 block not strictly dominated by ENTRY is ignored.
2986 We then call the standard PHI insertion algorithm with the pruned
2989 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
2990 names is not pruned. PHI nodes are inserted at every IDF block. */
2993 insert_updated_phi_nodes_for (tree var
, bitmap_head
*dfs
, bitmap blocks
,
2994 unsigned update_flags
)
2997 struct def_blocks_d
*db
;
2998 bitmap idf
, pruned_idf
;
3002 if (TREE_CODE (var
) == SSA_NAME
)
3003 gcc_checking_assert (is_old_name (var
));
3005 gcc_checking_assert (marked_for_renaming (var
));
3007 /* Get all the definition sites for VAR. */
3008 db
= find_def_blocks_for (var
);
3010 /* No need to do anything if there were no definitions to VAR. */
3011 if (db
== NULL
|| bitmap_empty_p (db
->def_blocks
))
3014 /* Compute the initial iterated dominance frontier. */
3015 idf
= compute_idf (db
->def_blocks
, dfs
);
3016 pruned_idf
= BITMAP_ALLOC (NULL
);
3018 if (TREE_CODE (var
) == SSA_NAME
)
3020 if (update_flags
== TODO_update_ssa
)
3022 /* If doing regular SSA updates for GIMPLE registers, we are
3023 only interested in IDF blocks dominated by the nearest
3024 common dominator of all the definition blocks. */
3025 entry
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3027 if (entry
!= ENTRY_BLOCK_PTR
)
3028 EXECUTE_IF_SET_IN_BITMAP (idf
, 0, i
, bi
)
3029 if (BASIC_BLOCK (i
) != entry
3030 && dominated_by_p (CDI_DOMINATORS
, BASIC_BLOCK (i
), entry
))
3031 bitmap_set_bit (pruned_idf
, i
);
3035 /* Otherwise, do not prune the IDF for VAR. */
3036 gcc_checking_assert (update_flags
== TODO_update_ssa_full_phi
);
3037 bitmap_copy (pruned_idf
, idf
);
3042 /* Otherwise, VAR is a symbol that needs to be put into SSA form
3043 for the first time, so we need to compute the full IDF for
3045 bitmap_copy (pruned_idf
, idf
);
3048 if (!bitmap_empty_p (pruned_idf
))
3050 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3051 are included in the region to be updated. The feeding blocks
3052 are important to guarantee that the PHI arguments are renamed
3055 /* FIXME, this is not needed if we are updating symbols. We are
3056 already starting at the ENTRY block anyway. */
3057 bitmap_ior_into (blocks
, pruned_idf
);
3058 EXECUTE_IF_SET_IN_BITMAP (pruned_idf
, 0, i
, bi
)
3062 basic_block bb
= BASIC_BLOCK (i
);
3064 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3065 if (e
->src
->index
>= 0)
3066 bitmap_set_bit (blocks
, e
->src
->index
);
3069 insert_phi_nodes_for (var
, pruned_idf
, true);
3072 BITMAP_FREE (pruned_idf
);
3076 /* Sort symbols_to_rename after their DECL_UID. */
3079 insert_updated_phi_nodes_compare_uids (const void *a
, const void *b
)
3081 const_tree syma
= *(const const_tree
*)a
;
3082 const_tree symb
= *(const const_tree
*)b
;
3083 if (DECL_UID (syma
) == DECL_UID (symb
))
3085 return DECL_UID (syma
) < DECL_UID (symb
) ? -1 : 1;
3088 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3089 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3091 1- The names in OLD_SSA_NAMES dominated by the definitions of
3092 NEW_SSA_NAMES are all re-written to be reached by the
3093 appropriate definition from NEW_SSA_NAMES.
3095 2- If needed, new PHI nodes are added to the iterated dominance
3096 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3098 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3099 calling create_new_def_for to create new defs for names that the
3100 caller wants to replace.
3102 The caller cretaes the new names to be inserted and the names that need
3103 to be replaced by calling create_new_def_for for each old definition
3104 to be replaced. Note that the function assumes that the
3105 new defining statement has already been inserted in the IL.
3107 For instance, given the following code:
3110 2 x_1 = PHI (0, x_5)
3121 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3124 2 x_1 = PHI (0, x_5)
3137 We want to replace all the uses of x_1 with the new definitions of
3138 x_10 and x_11. Note that the only uses that should be replaced are
3139 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3140 *not* be replaced (this is why we cannot just mark symbol 'x' for
3143 Additionally, we may need to insert a PHI node at line 11 because
3144 that is a merge point for x_10 and x_11. So the use of x_1 at line
3145 11 will be replaced with the new PHI node. The insertion of PHI
3146 nodes is optional. They are not strictly necessary to preserve the
3147 SSA form, and depending on what the caller inserted, they may not
3148 even be useful for the optimizers. UPDATE_FLAGS controls various
3149 aspects of how update_ssa operates, see the documentation for
3150 TODO_update_ssa*. */
3153 update_ssa (unsigned update_flags
)
3155 basic_block bb
, start_bb
;
3159 sbitmap_iterator sbi
;
3162 /* Only one update flag should be set. */
3163 gcc_assert (update_flags
== TODO_update_ssa
3164 || update_flags
== TODO_update_ssa_no_phi
3165 || update_flags
== TODO_update_ssa_full_phi
3166 || update_flags
== TODO_update_ssa_only_virtuals
);
3168 if (!need_ssa_update_p (cfun
))
3171 timevar_push (TV_TREE_SSA_INCREMENTAL
);
3173 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3174 fprintf (dump_file
, "\nUpdating SSA:\n");
3176 if (!update_ssa_initialized_fn
)
3177 init_update_ssa (cfun
);
3178 else if (update_flags
== TODO_update_ssa_only_virtuals
)
3180 /* If we only need to update virtuals, remove all the mappings for
3181 real names before proceeding. The caller is responsible for
3182 having dealt with the name mappings before calling update_ssa. */
3183 bitmap_clear (old_ssa_names
);
3184 bitmap_clear (new_ssa_names
);
3187 gcc_assert (update_ssa_initialized_fn
== cfun
);
3189 blocks_with_phis_to_rewrite
= BITMAP_ALLOC (NULL
);
3190 if (!phis_to_rewrite
.exists ())
3191 phis_to_rewrite
.create (last_basic_block
+ 1);
3192 blocks_to_update
= BITMAP_ALLOC (NULL
);
3194 /* Ensure that the dominance information is up-to-date. */
3195 calculate_dominance_info (CDI_DOMINATORS
);
3197 insert_phi_p
= (update_flags
!= TODO_update_ssa_no_phi
);
3199 /* If there are names defined in the replacement table, prepare
3200 definition and use sites for all the names in NEW_SSA_NAMES and
3202 if (bitmap_first_set_bit (new_ssa_names
) >= 0)
3204 prepare_names_to_update (insert_phi_p
);
3206 /* If all the names in NEW_SSA_NAMES had been marked for
3207 removal, and there are no symbols to rename, then there's
3208 nothing else to do. */
3209 if (bitmap_first_set_bit (new_ssa_names
) < 0
3210 && !cfun
->gimple_df
->ssa_renaming_needed
)
3214 /* Next, determine the block at which to start the renaming process. */
3215 if (cfun
->gimple_df
->ssa_renaming_needed
)
3217 /* If we rename bare symbols initialize the mapping to
3218 auxiliar info we need to keep track of. */
3219 var_infos
.create (47);
3221 /* If we have to rename some symbols from scratch, we need to
3222 start the process at the root of the CFG. FIXME, it should
3223 be possible to determine the nearest block that had a
3224 definition for each of the symbols that are marked for
3225 updating. For now this seems more work than it's worth. */
3226 start_bb
= ENTRY_BLOCK_PTR
;
3228 /* Traverse the CFG looking for existing definitions and uses of
3229 symbols in SSA operands. Mark interesting blocks and
3230 statements and set local live-in information for the PHI
3231 placement heuristics. */
3232 prepare_block_for_update (start_bb
, insert_phi_p
);
3234 #ifdef ENABLE_CHECKING
3235 for (i
= 1; i
< num_ssa_names
; ++i
)
3237 tree name
= ssa_name (i
);
3239 || virtual_operand_p (name
))
3242 /* For all but virtual operands, which do not have SSA names
3243 with overlapping life ranges, ensure that symbols marked
3244 for renaming do not have existing SSA names associated with
3245 them as we do not re-write them out-of-SSA before going
3246 into SSA for the remaining symbol uses. */
3247 if (marked_for_renaming (SSA_NAME_VAR (name
)))
3249 fprintf (stderr
, "Existing SSA name for symbol marked for "
3251 print_generic_expr (stderr
, name
, TDF_SLIM
);
3252 fprintf (stderr
, "\n");
3253 internal_error ("SSA corruption");
3260 /* Otherwise, the entry block to the region is the nearest
3261 common dominator for the blocks in BLOCKS. */
3262 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3266 /* If requested, insert PHI nodes at the iterated dominance frontier
3267 of every block, creating new definitions for names in OLD_SSA_NAMES
3268 and for symbols found. */
3273 /* If the caller requested PHI nodes to be added, compute
3274 dominance frontiers. */
3275 dfs
= XNEWVEC (bitmap_head
, last_basic_block
);
3277 bitmap_initialize (&dfs
[bb
->index
], &bitmap_default_obstack
);
3278 compute_dominance_frontiers (dfs
);
3280 if (bitmap_first_set_bit (old_ssa_names
) >= 0)
3282 sbitmap_iterator sbi
;
3284 /* insert_update_phi_nodes_for will call add_new_name_mapping
3285 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3286 will grow while we are traversing it (but it will not
3287 gain any new members). Copy OLD_SSA_NAMES to a temporary
3289 sbitmap tmp
= sbitmap_alloc (SBITMAP_SIZE (old_ssa_names
));
3290 bitmap_copy (tmp
, old_ssa_names
);
3291 EXECUTE_IF_SET_IN_BITMAP (tmp
, 0, i
, sbi
)
3292 insert_updated_phi_nodes_for (ssa_name (i
), dfs
, blocks_to_update
,
3297 symbols_to_rename
.qsort (insert_updated_phi_nodes_compare_uids
);
3298 FOR_EACH_VEC_ELT (symbols_to_rename
, i
, sym
)
3299 insert_updated_phi_nodes_for (sym
, dfs
, blocks_to_update
,
3303 bitmap_clear (&dfs
[bb
->index
]);
3306 /* Insertion of PHI nodes may have added blocks to the region.
3307 We need to re-compute START_BB to include the newly added
3309 if (start_bb
!= ENTRY_BLOCK_PTR
)
3310 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3314 /* Reset the current definition for name and symbol before renaming
3316 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names
, 0, i
, sbi
)
3317 get_ssa_name_ann (ssa_name (i
))->info
.current_def
= NULL_TREE
;
3319 FOR_EACH_VEC_ELT (symbols_to_rename
, i
, sym
)
3320 get_var_info (sym
)->info
.current_def
= NULL_TREE
;
3322 /* Now start the renaming process at START_BB. */
3323 interesting_blocks
= sbitmap_alloc (last_basic_block
);
3324 bitmap_clear (interesting_blocks
);
3325 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3326 bitmap_set_bit (interesting_blocks
, i
);
3328 rewrite_blocks (start_bb
, REWRITE_UPDATE
);
3330 sbitmap_free (interesting_blocks
);
3332 /* Debugging dumps. */
3338 dump_update_ssa (dump_file
);
3340 fprintf (dump_file
, "Incremental SSA update started at block: %d\n",
3344 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3346 fprintf (dump_file
, "Number of blocks in CFG: %d\n", last_basic_block
);
3347 fprintf (dump_file
, "Number of blocks to update: %d (%3.0f%%)\n",
3348 c
, PERCENT (c
, last_basic_block
));
3350 if (dump_flags
& TDF_DETAILS
)
3352 fprintf (dump_file
, "Affected blocks:");
3353 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3354 fprintf (dump_file
, " %u", i
);
3355 fprintf (dump_file
, "\n");
3358 fprintf (dump_file
, "\n\n");
3361 /* Free allocated memory. */
3363 delete_update_ssa ();
3365 timevar_pop (TV_TREE_SSA_INCREMENTAL
);