1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001-2024 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 "tree-pass.h"
30 #include "gimple-pretty-print.h"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
34 #include "gimple-iterator.h"
36 #include "tree-into-ssa.h"
40 #include "statistics.h"
41 #include "stringpool.h"
44 #include "attr-fnspec.h"
46 #define PERCENT(x,y) ((float)(x) * 100.0 / (float)(y))
48 /* This file builds the SSA form for a function as described in:
49 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
50 Computing Static Single Assignment Form and the Control Dependence
51 Graph. ACM Transactions on Programming Languages and Systems,
52 13(4):451-490, October 1991. */
54 /* Structure to map a variable VAR to the set of blocks that contain
55 definitions for VAR. */
58 /* Blocks that contain definitions of VAR. Bit I will be set if the
59 Ith block contains a definition of VAR. */
62 /* Blocks that contain a PHI node for VAR. */
65 /* Blocks where VAR is live-on-entry. Similar semantics as
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
> 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 static 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 bitmap names_to_release
;
104 /* vec of vec 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
< vec
<gphi
*> > 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 create_new_def_for will create new names on
115 FIXME. Currently set to 1/3 to avoid frequent reallocations but still
116 need to find a reasonable growth strategy. */
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 create_new_def_for. */
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 /* It is advantageous to avoid things like life analysis for variables which
133 do not need PHI nodes. This enum describes whether or not a particular
134 variable may need a PHI node. */
136 enum need_phi_state
{
137 /* This is the default. If we are still in this state after finding
138 all the definition and use sites, then we will assume the variable
139 needs PHI nodes. This is probably an overly conservative assumption. */
140 NEED_PHI_STATE_UNKNOWN
,
142 /* This state indicates that we have seen one or more sets of the
143 variable in a single basic block and that the sets dominate all
144 uses seen so far. If after finding all definition and use sites
145 we are still in this state, then the variable does not need any
149 /* This state indicates that we have either seen multiple definitions of
150 the variable in multiple blocks, or that we encountered a use in a
151 block that was not dominated by the block containing the set(s) of
152 this variable. This variable is assumed to need PHI nodes. */
156 /* Information stored for both SSA names and decls. */
159 /* This field indicates whether or not the variable may need PHI nodes.
160 See the enum's definition for more detailed information about the
162 ENUM_BITFIELD (need_phi_state
) need_phi_state
: 2;
164 /* The current reaching definition replacing this var. */
167 /* Definitions for this var. */
168 struct def_blocks def_blocks
;
171 /* Information stored for decls. */
177 /* Information stored for both SSA names and decls. */
182 /* VAR_INFOS hashtable helpers. */
184 struct var_info_hasher
: free_ptr_hash
<var_info
>
186 static inline hashval_t
hash (const value_type
&);
187 static inline bool equal (const value_type
&, const compare_type
&);
191 var_info_hasher::hash (const value_type
&p
)
193 return DECL_UID (p
->var
);
197 var_info_hasher::equal (const value_type
&p1
, const compare_type
&p2
)
199 return p1
->var
== p2
->var
;
203 /* Each entry in VAR_INFOS contains an element of type STRUCT
205 static hash_table
<var_info_hasher
> *var_infos
;
208 /* Information stored for SSA names. */
211 /* Age of this record (so that info_for_ssa_name table can be cleared
212 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
213 are assumed to be null. */
216 /* Replacement mappings, allocated from update_ssa_obstack. */
219 /* Information stored for both SSA names and decls. */
223 static vec
<ssa_name_info
*> info_for_ssa_name
;
224 static unsigned current_info_for_ssa_name_age
;
226 static bitmap_obstack update_ssa_obstack
;
228 /* The set of blocks affected by update_ssa. */
229 static bitmap blocks_to_update
;
231 /* The main entry point to the SSA renamer (rewrite_blocks) may be
232 called several times to do different, but related, tasks.
233 Initially, we need it to rename the whole program into SSA form.
234 At other times, we may need it to only rename into SSA newly
235 exposed symbols. Finally, we can also call it to incrementally fix
236 an already built SSA web. */
238 /* Convert the whole function into SSA form. */
241 /* Incrementally update the SSA web by replacing existing SSA
242 names with new ones. See update_ssa for details. */
244 REWRITE_UPDATE_REGION
247 /* The set of symbols we ought to re-write into SSA form in update_ssa. */
248 static bitmap symbols_to_rename_set
;
249 static vec
<tree
> symbols_to_rename
;
251 /* Mark SYM for renaming. */
254 mark_for_renaming (tree sym
)
256 if (!symbols_to_rename_set
)
257 symbols_to_rename_set
= BITMAP_ALLOC (NULL
);
258 if (bitmap_set_bit (symbols_to_rename_set
, DECL_UID (sym
)))
259 symbols_to_rename
.safe_push (sym
);
262 /* Return true if SYM is marked for renaming. */
265 marked_for_renaming (tree sym
)
267 if (!symbols_to_rename_set
|| sym
== NULL_TREE
)
269 return bitmap_bit_p (symbols_to_rename_set
, DECL_UID (sym
));
273 /* Return true if STMT needs to be rewritten. When renaming a subset
274 of the variables, not all statements will be processed. This is
275 decided in mark_def_sites. */
278 rewrite_uses_p (gimple
*stmt
)
280 return gimple_visited_p (stmt
);
284 /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
287 set_rewrite_uses (gimple
*stmt
, bool rewrite_p
)
289 gimple_set_visited (stmt
, rewrite_p
);
293 /* Return true if the DEFs created by statement STMT should be
294 registered when marking new definition sites. This is slightly
295 different than rewrite_uses_p: it's used by update_ssa to
296 distinguish statements that need to have both uses and defs
297 processed from those that only need to have their defs processed.
298 Statements that define new SSA names only need to have their defs
299 registered, but they don't need to have their uses renamed. */
302 register_defs_p (gimple
*stmt
)
304 return gimple_plf (stmt
, GF_PLF_1
) != 0;
308 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
311 set_register_defs (gimple
*stmt
, bool register_defs_p
)
313 gimple_set_plf (stmt
, GF_PLF_1
, register_defs_p
);
317 /* Get the information associated with NAME. */
319 static inline ssa_name_info
*
320 get_ssa_name_ann (tree name
)
322 unsigned ver
= SSA_NAME_VERSION (name
);
323 unsigned len
= info_for_ssa_name
.length ();
324 struct ssa_name_info
*info
;
326 /* Re-allocate the vector at most once per update/into-SSA. */
328 info_for_ssa_name
.safe_grow_cleared (num_ssa_names
, true);
330 /* But allocate infos lazily. */
331 info
= info_for_ssa_name
[ver
];
334 info
= XCNEW (struct ssa_name_info
);
335 info
->age
= current_info_for_ssa_name_age
;
336 info
->info
.need_phi_state
= NEED_PHI_STATE_UNKNOWN
;
337 info_for_ssa_name
[ver
] = info
;
340 if (info
->age
< current_info_for_ssa_name_age
)
342 info
->age
= current_info_for_ssa_name_age
;
343 info
->repl_set
= NULL
;
344 info
->info
.need_phi_state
= NEED_PHI_STATE_UNKNOWN
;
345 info
->info
.current_def
= NULL_TREE
;
346 info
->info
.def_blocks
.def_blocks
= NULL
;
347 info
->info
.def_blocks
.phi_blocks
= NULL
;
348 info
->info
.def_blocks
.livein_blocks
= NULL
;
354 /* Return and allocate the auxiliar information for DECL. */
356 static inline var_info
*
357 get_var_info (tree decl
)
362 slot
= var_infos
->find_slot_with_hash (&vi
, DECL_UID (decl
), INSERT
);
365 var_info
*v
= XCNEW (var_info
);
374 /* Clears info for SSA names. */
377 clear_ssa_name_info (void)
379 current_info_for_ssa_name_age
++;
381 /* If current_info_for_ssa_name_age wraps we use stale information.
382 Asser that this does not happen. */
383 gcc_assert (current_info_for_ssa_name_age
!= 0);
387 /* Get access to the auxiliar information stored per SSA name or decl. */
389 static inline common_info
*
390 get_common_info (tree var
)
392 if (TREE_CODE (var
) == SSA_NAME
)
393 return &get_ssa_name_ann (var
)->info
;
395 return &get_var_info (var
)->info
;
399 /* Return the current definition for VAR. */
402 get_current_def (tree var
)
404 return get_common_info (var
)->current_def
;
408 /* Sets current definition of VAR to DEF. */
411 set_current_def (tree var
, tree def
)
413 get_common_info (var
)->current_def
= def
;
416 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
417 all statements in basic block BB. */
420 initialize_flags_in_bb (basic_block bb
)
423 gimple_stmt_iterator gsi
;
425 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
427 gimple
*phi
= gsi_stmt (gsi
);
428 set_rewrite_uses (phi
, false);
429 set_register_defs (phi
, false);
432 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
434 stmt
= gsi_stmt (gsi
);
436 /* We are going to use the operand cache API, such as
437 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
438 cache for each statement should be up-to-date. */
439 gcc_checking_assert (!gimple_modified_p (stmt
));
440 set_rewrite_uses (stmt
, false);
441 set_register_defs (stmt
, false);
445 /* Mark block BB as interesting for update_ssa. */
448 mark_block_for_update (basic_block bb
)
450 gcc_checking_assert (blocks_to_update
!= NULL
);
451 if (!bitmap_set_bit (blocks_to_update
, bb
->index
))
453 initialize_flags_in_bb (bb
);
456 /* Return the set of blocks where variable VAR is defined and the blocks
457 where VAR is live on entry (livein). If no entry is found in
458 DEF_BLOCKS, a new one is created and returned. */
460 static inline def_blocks
*
461 get_def_blocks_for (common_info
*info
)
463 def_blocks
*db_p
= &info
->def_blocks
;
464 if (!db_p
->def_blocks
)
466 db_p
->def_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
467 db_p
->phi_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
468 db_p
->livein_blocks
= BITMAP_ALLOC (&update_ssa_obstack
);
475 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
476 VAR is defined by a PHI node. */
479 set_def_block (tree var
, basic_block bb
, bool phi_p
)
484 info
= get_common_info (var
);
485 db_p
= get_def_blocks_for (info
);
487 /* Set the bit corresponding to the block where VAR is defined. */
488 bitmap_set_bit (db_p
->def_blocks
, bb
->index
);
490 bitmap_set_bit (db_p
->phi_blocks
, bb
->index
);
492 /* Keep track of whether or not we may need to insert PHI nodes.
494 If we are in the UNKNOWN state, then this is the first definition
495 of VAR. Additionally, we have not seen any uses of VAR yet, so
496 we do not need a PHI node for this variable at this time (i.e.,
497 transition to NEED_PHI_STATE_NO).
499 If we are in any other state, then we either have multiple definitions
500 of this variable occurring in different blocks or we saw a use of the
501 variable which was not dominated by the block containing the
502 definition(s). In this case we may need a PHI node, so enter
503 state NEED_PHI_STATE_MAYBE. */
504 if (info
->need_phi_state
== NEED_PHI_STATE_UNKNOWN
)
505 info
->need_phi_state
= NEED_PHI_STATE_NO
;
507 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
511 /* Mark block BB as having VAR live at the entry to BB. */
514 set_livein_block (tree var
, basic_block bb
)
519 info
= get_common_info (var
);
520 db_p
= get_def_blocks_for (info
);
522 /* Set the bit corresponding to the block where VAR is live in. */
523 bitmap_set_bit (db_p
->livein_blocks
, bb
->index
);
525 /* Keep track of whether or not we may need to insert PHI nodes.
527 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
528 by the single block containing the definition(s) of this variable. If
529 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
530 NEED_PHI_STATE_MAYBE. */
531 if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
533 int def_block_index
= bitmap_first_set_bit (db_p
->def_blocks
);
535 if (def_block_index
== -1
536 || ! dominated_by_p (CDI_DOMINATORS
, bb
,
537 BASIC_BLOCK_FOR_FN (cfun
, def_block_index
)))
538 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
541 info
->need_phi_state
= NEED_PHI_STATE_MAYBE
;
545 /* Return true if NAME is in OLD_SSA_NAMES. */
548 is_old_name (tree name
)
550 unsigned ver
= SSA_NAME_VERSION (name
);
553 return (ver
< SBITMAP_SIZE (old_ssa_names
)
554 && bitmap_bit_p (old_ssa_names
, ver
));
558 /* Return true if NAME is in NEW_SSA_NAMES. */
561 is_new_name (tree name
)
563 unsigned ver
= SSA_NAME_VERSION (name
);
566 return (ver
< SBITMAP_SIZE (new_ssa_names
)
567 && bitmap_bit_p (new_ssa_names
, ver
));
571 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
574 names_replaced_by (tree new_tree
)
576 return get_ssa_name_ann (new_tree
)->repl_set
;
580 /* Add OLD to REPL_TBL[NEW_TREE].SET. */
583 add_to_repl_tbl (tree new_tree
, tree old
)
585 bitmap
*set
= &get_ssa_name_ann (new_tree
)->repl_set
;
587 *set
= BITMAP_ALLOC (&update_ssa_obstack
);
588 bitmap_set_bit (*set
, SSA_NAME_VERSION (old
));
591 /* Debugging aid to fence old_ssa_names changes when iterating over it. */
592 static bool iterating_old_ssa_names
;
594 /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
595 represents the set of names O_1 ... O_j replaced by N_i. This is
596 used by update_ssa and its helpers to introduce new SSA names in an
597 already formed SSA web. */
600 add_new_name_mapping (tree new_tree
, tree old
)
602 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
603 gcc_checking_assert (new_tree
!= old
604 && SSA_NAME_VAR (new_tree
) == SSA_NAME_VAR (old
));
606 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
607 caller may have created new names since the set was created. */
608 if (SBITMAP_SIZE (new_ssa_names
) <= SSA_NAME_VERSION (new_tree
))
610 unsigned int new_sz
= num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
;
611 new_ssa_names
= sbitmap_resize (new_ssa_names
, new_sz
, 0);
613 if (SBITMAP_SIZE (old_ssa_names
) <= SSA_NAME_VERSION (old
))
615 gcc_assert (!iterating_old_ssa_names
);
616 unsigned int new_sz
= num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
;
617 old_ssa_names
= sbitmap_resize (old_ssa_names
, new_sz
, 0);
620 /* Update the REPL_TBL table. */
621 add_to_repl_tbl (new_tree
, old
);
623 /* If OLD had already been registered as a new name, then all the
624 names that OLD replaces should also be replaced by NEW_TREE. */
625 if (is_new_name (old
))
626 bitmap_ior_into (names_replaced_by (new_tree
), names_replaced_by (old
));
628 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
630 if (iterating_old_ssa_names
)
631 gcc_assert (bitmap_bit_p (old_ssa_names
, SSA_NAME_VERSION (old
)));
633 bitmap_set_bit (old_ssa_names
, SSA_NAME_VERSION (old
));
634 bitmap_set_bit (new_ssa_names
, SSA_NAME_VERSION (new_tree
));
638 /* Call back for walk_dominator_tree used to collect definition sites
639 for every variable in the function. For every statement S in block
642 1- Variables defined by S in the DEFS of S are marked in the bitmap
645 2- If S uses a variable VAR and there is no preceding kill of VAR,
646 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
648 This information is used to determine which variables are live
649 across block boundaries to reduce the number of PHI nodes
653 mark_def_sites (basic_block bb
, gimple
*stmt
, bitmap kills
)
659 /* Since this is the first time that we rewrite the program into SSA
660 form, force an operand scan on every statement. */
663 gcc_checking_assert (blocks_to_update
== NULL
);
664 set_register_defs (stmt
, false);
665 set_rewrite_uses (stmt
, false);
667 if (is_gimple_debug (stmt
))
669 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
671 tree sym
= USE_FROM_PTR (use_p
);
672 gcc_checking_assert (DECL_P (sym
));
673 set_rewrite_uses (stmt
, true);
675 if (rewrite_uses_p (stmt
))
676 bitmap_set_bit (interesting_blocks
, bb
->index
);
680 /* If a variable is used before being set, then the variable is live
681 across a block boundary, so mark it live-on-entry to BB. */
682 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
684 tree sym
= USE_FROM_PTR (use_p
);
685 if (TREE_CODE (sym
) == SSA_NAME
)
687 gcc_checking_assert (DECL_P (sym
));
688 if (!bitmap_bit_p (kills
, DECL_UID (sym
)))
689 set_livein_block (sym
, bb
);
690 set_rewrite_uses (stmt
, true);
693 /* Now process the defs. Mark BB as the definition block and add
694 each def to the set of killed symbols. */
695 FOR_EACH_SSA_TREE_OPERAND (def
, stmt
, iter
, SSA_OP_ALL_DEFS
)
697 if (TREE_CODE (def
) == SSA_NAME
)
699 gcc_checking_assert (DECL_P (def
));
700 set_def_block (def
, bb
, false);
701 bitmap_set_bit (kills
, DECL_UID (def
));
702 set_register_defs (stmt
, true);
705 /* If we found the statement interesting then also mark the block BB
707 if (rewrite_uses_p (stmt
) || register_defs_p (stmt
))
708 bitmap_set_bit (interesting_blocks
, bb
->index
);
711 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
712 in the dfs numbering of the dominance tree. */
716 /* Basic block whose index this entry corresponds to. */
719 /* The dfs number of this node. */
723 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
727 cmp_dfsnum (const void *a
, const void *b
)
729 const struct dom_dfsnum
*const da
= (const struct dom_dfsnum
*) a
;
730 const struct dom_dfsnum
*const db
= (const struct dom_dfsnum
*) b
;
732 return (int) da
->dfs_num
- (int) db
->dfs_num
;
735 /* Among the intervals starting at the N points specified in DEFS, find
736 the one that contains S, and return its bb_index. */
739 find_dfsnum_interval (struct dom_dfsnum
*defs
, unsigned n
, unsigned s
)
741 unsigned f
= 0, t
= n
, m
;
746 if (defs
[m
].dfs_num
<= s
)
752 return defs
[f
].bb_index
;
755 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
756 KILLS is a bitmap of blocks where the value is defined before any use. */
759 prune_unused_phi_nodes (bitmap phis
, bitmap kills
, bitmap uses
)
762 unsigned i
, b
, p
, u
, top
;
764 basic_block def_bb
, use_bb
;
768 struct dom_dfsnum
*defs
;
769 unsigned n_defs
, adef
;
771 if (bitmap_empty_p (uses
))
777 /* The phi must dominate a use, or an argument of a live phi. Also, we
778 do not create any phi nodes in def blocks, unless they are also livein. */
779 to_remove
= BITMAP_ALLOC (NULL
);
780 bitmap_and_compl (to_remove
, kills
, uses
);
781 bitmap_and_compl_into (phis
, to_remove
);
782 if (bitmap_empty_p (phis
))
784 BITMAP_FREE (to_remove
);
788 /* We want to remove the unnecessary phi nodes, but we do not want to compute
789 liveness information, as that may be linear in the size of CFG, and if
790 there are lot of different variables to rewrite, this may lead to quadratic
793 Instead, we basically emulate standard dce. We put all uses to worklist,
794 then for each of them find the nearest def that dominates them. If this
795 def is a phi node, we mark it live, and if it was not live before, we
796 add the predecessors of its basic block to the worklist.
798 To quickly locate the nearest def that dominates use, we use dfs numbering
799 of the dominance tree (that is already available in order to speed up
800 queries). For each def, we have the interval given by the dfs number on
801 entry to and on exit from the corresponding subtree in the dominance tree.
802 The nearest dominator for a given use is the smallest of these intervals
803 that contains entry and exit dfs numbers for the basic block with the use.
804 If we store the bounds for all the uses to an array and sort it, we can
805 locate the nearest dominating def in logarithmic time by binary search.*/
806 bitmap_ior (to_remove
, kills
, phis
);
807 n_defs
= bitmap_count_bits (to_remove
);
808 adef
= 2 * n_defs
+ 1;
809 defs
= XNEWVEC (struct dom_dfsnum
, adef
);
810 defs
[0].bb_index
= 1;
812 struct dom_dfsnum
*head
= defs
+ 1, *tail
= defs
+ adef
;
813 EXECUTE_IF_SET_IN_BITMAP (to_remove
, 0, i
, bi
)
815 def_bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
817 head
->dfs_num
= bb_dom_dfs_in (CDI_DOMINATORS
, def_bb
);
820 tail
->dfs_num
= bb_dom_dfs_out (CDI_DOMINATORS
, def_bb
);
822 gcc_checking_assert (head
== tail
);
823 BITMAP_FREE (to_remove
);
824 qsort (defs
, adef
, sizeof (struct dom_dfsnum
), cmp_dfsnum
);
825 gcc_assert (defs
[0].bb_index
== 1);
827 /* Now each DEFS entry contains the number of the basic block to that the
828 dfs number corresponds. Change them to the number of basic block that
829 corresponds to the interval following the dfs number. Also, for the
830 dfs_out numbers, increase the dfs number by one (so that it corresponds
831 to the start of the following interval, not to the end of the current
832 one). We use WORKLIST as a stack. */
833 auto_vec
<int> worklist (n_defs
+ 1);
834 worklist
.quick_push (1);
837 for (i
= 1; i
< adef
; i
++)
839 b
= defs
[i
].bb_index
;
842 /* This is a closing element. Interval corresponding to the top
843 of the stack after removing it follows. */
845 top
= worklist
[worklist
.length () - 1];
846 defs
[n_defs
].bb_index
= top
;
847 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
+ 1;
851 /* Opening element. Nothing to do, just push it to the stack and move
852 it to the correct position. */
853 defs
[n_defs
].bb_index
= defs
[i
].bb_index
;
854 defs
[n_defs
].dfs_num
= defs
[i
].dfs_num
;
855 worklist
.quick_push (b
);
859 /* If this interval starts at the same point as the previous one, cancel
861 if (defs
[n_defs
].dfs_num
== defs
[n_defs
- 1].dfs_num
)
862 defs
[n_defs
- 1].bb_index
= defs
[n_defs
].bb_index
;
867 gcc_assert (worklist
.is_empty ());
869 /* Now process the uses. */
870 live_phis
= BITMAP_ALLOC (NULL
);
871 EXECUTE_IF_SET_IN_BITMAP (uses
, 0, i
, bi
)
873 worklist
.safe_push (i
);
876 while (!worklist
.is_empty ())
879 if (b
== ENTRY_BLOCK
)
882 /* If there is a phi node in USE_BB, it is made live. Otherwise,
883 find the def that dominates the immediate dominator of USE_BB
884 (the kill in USE_BB does not dominate the use). */
885 if (bitmap_bit_p (phis
, b
))
889 use_bb
= get_immediate_dominator (CDI_DOMINATORS
,
890 BASIC_BLOCK_FOR_FN (cfun
, b
));
891 p
= find_dfsnum_interval (defs
, n_defs
,
892 bb_dom_dfs_in (CDI_DOMINATORS
, use_bb
));
893 if (!bitmap_bit_p (phis
, p
))
897 /* If the phi node is already live, there is nothing to do. */
898 if (!bitmap_set_bit (live_phis
, p
))
901 /* Add the new uses to the worklist. */
902 def_bb
= BASIC_BLOCK_FOR_FN (cfun
, p
);
903 FOR_EACH_EDGE (e
, ei
, def_bb
->preds
)
906 if (bitmap_bit_p (uses
, u
))
909 /* In case there is a kill directly in the use block, do not record
910 the use (this is also necessary for correctness, as we assume that
911 uses dominated by a def directly in their block have been filtered
913 if (bitmap_bit_p (kills
, u
))
916 bitmap_set_bit (uses
, u
);
917 worklist
.safe_push (u
);
921 bitmap_copy (phis
, live_phis
);
922 BITMAP_FREE (live_phis
);
926 /* Return the set of blocks where variable VAR is defined and the blocks
927 where VAR is live on entry (livein). Return NULL, if no entry is
928 found in DEF_BLOCKS. */
930 static inline def_blocks
*
931 find_def_blocks_for (tree var
)
933 def_blocks
*p
= &get_common_info (var
)->def_blocks
;
940 /* Marks phi node PHI in basic block BB for rewrite. */
943 mark_phi_for_rewrite (basic_block bb
, gphi
*phi
)
946 unsigned n
, idx
= bb
->index
;
948 if (rewrite_uses_p (phi
))
951 set_rewrite_uses (phi
, true);
953 if (!blocks_with_phis_to_rewrite
)
956 if (bitmap_set_bit (blocks_with_phis_to_rewrite
, idx
))
958 n
= (unsigned) last_basic_block_for_fn (cfun
) + 1;
959 if (phis_to_rewrite
.length () < n
)
960 phis_to_rewrite
.safe_grow_cleared (n
, true);
962 phis
= phis_to_rewrite
[idx
];
963 gcc_assert (!phis
.exists ());
967 phis
= phis_to_rewrite
[idx
];
969 phis
.safe_push (phi
);
970 phis_to_rewrite
[idx
] = phis
;
973 /* Insert PHI nodes for variable VAR using the iterated dominance
974 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
975 function assumes that the caller is incrementally updating the
976 existing SSA form, in which case VAR may be an SSA name instead of
979 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
980 PHI node for VAR. On exit, only the nodes that received a PHI node
981 for VAR will be present in PHI_INSERTION_POINTS. */
984 insert_phi_nodes_for (tree var
, bitmap phi_insertion_points
, bool update_p
)
991 def_blocks
*def_map
= find_def_blocks_for (var
);
993 /* Remove the blocks where we already have PHI nodes for VAR. */
994 bitmap_and_compl_into (phi_insertion_points
, def_map
->phi_blocks
);
996 /* Remove obviously useless phi nodes. */
997 prune_unused_phi_nodes (phi_insertion_points
, def_map
->def_blocks
,
998 def_map
->livein_blocks
);
1000 /* And insert the PHI nodes. */
1001 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points
, 0, bb_index
, bi
)
1003 bb
= BASIC_BLOCK_FOR_FN (cfun
, bb_index
);
1005 mark_block_for_update (bb
);
1007 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1009 fprintf (dump_file
, "creating PHI node in block #%d for ", bb_index
);
1010 print_generic_expr (dump_file
, var
, TDF_SLIM
);
1011 fprintf (dump_file
, "\n");
1015 if (TREE_CODE (var
) == SSA_NAME
)
1017 /* If we are rewriting SSA names, create the LHS of the PHI
1018 node by duplicating VAR. This is useful in the case of
1019 pointers, to also duplicate pointer attributes (alias
1020 information, in particular). */
1024 gcc_checking_assert (update_p
);
1025 new_lhs
= duplicate_ssa_name (var
, NULL
);
1026 phi
= create_phi_node (new_lhs
, bb
);
1027 add_new_name_mapping (new_lhs
, var
);
1029 /* Add VAR to every argument slot of PHI. We need VAR in
1030 every argument so that rewrite_update_phi_arguments knows
1031 which name is this PHI node replacing. If VAR is a
1032 symbol marked for renaming, this is not necessary, the
1033 renamer will use the symbol on the LHS to get its
1034 reaching definition. */
1035 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1036 add_phi_arg (phi
, var
, e
, UNKNOWN_LOCATION
);
1042 gcc_checking_assert (DECL_P (var
));
1043 phi
= create_phi_node (var
, bb
);
1045 tracked_var
= target_for_debug_bind (var
);
1048 gimple
*note
= gimple_build_debug_bind (tracked_var
,
1051 gimple_stmt_iterator si
= gsi_after_labels (bb
);
1052 gsi_insert_before (&si
, note
, GSI_SAME_STMT
);
1056 /* Mark this PHI node as interesting for update_ssa. */
1057 set_register_defs (phi
, true);
1058 mark_phi_for_rewrite (bb
, phi
);
1062 /* Sort var_infos after DECL_UID of their var. */
1065 insert_phi_nodes_compare_var_infos (const void *a
, const void *b
)
1067 const var_info
*defa
= *(var_info
* const *)a
;
1068 const var_info
*defb
= *(var_info
* const *)b
;
1069 if (DECL_UID (defa
->var
) < DECL_UID (defb
->var
))
1075 /* Insert PHI nodes at the dominance frontier of blocks with variable
1076 definitions. DFS contains the dominance frontier information for
1080 insert_phi_nodes (bitmap_head
*dfs
)
1082 hash_table
<var_info_hasher
>::iterator hi
;
1086 /* When the gimplifier introduces SSA names it cannot easily avoid
1087 situations where abnormal edges added by CFG construction break
1088 the use-def dominance requirement. For this case rewrite SSA
1089 names with broken use-def dominance out-of-SSA and register them
1090 for PHI insertion. We only need to do this if abnormal edges
1091 can appear in the function. */
1093 if (cfun
->calls_setjmp
1094 || cfun
->has_nonlocal_label
)
1095 FOR_EACH_SSA_NAME (i
, name
, cfun
)
1097 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1098 if (SSA_NAME_IS_DEFAULT_DEF (name
))
1101 basic_block def_bb
= gimple_bb (def_stmt
);
1102 imm_use_iterator it
;
1104 bool need_phis
= false;
1105 FOR_EACH_IMM_USE_STMT (use_stmt
, it
, name
)
1107 basic_block use_bb
= gimple_bb (use_stmt
);
1108 if (use_bb
!= def_bb
1109 && ! dominated_by_p (CDI_DOMINATORS
, use_bb
, def_bb
))
1114 tree var
= create_tmp_reg (TREE_TYPE (name
));
1115 use_operand_p use_p
;
1116 FOR_EACH_IMM_USE_STMT (use_stmt
, it
, name
)
1118 basic_block use_bb
= gimple_bb (use_stmt
);
1119 FOR_EACH_IMM_USE_ON_STMT (use_p
, it
)
1120 SET_USE (use_p
, var
);
1121 update_stmt (use_stmt
);
1122 set_livein_block (var
, use_bb
);
1123 set_rewrite_uses (use_stmt
, true);
1124 bitmap_set_bit (interesting_blocks
, use_bb
->index
);
1126 def_operand_p def_p
;
1128 FOR_EACH_SSA_DEF_OPERAND (def_p
, def_stmt
, dit
, SSA_OP_DEF
)
1129 if (DEF_FROM_PTR (def_p
) == name
)
1130 SET_DEF (def_p
, var
);
1131 update_stmt (def_stmt
);
1132 set_def_block (var
, def_bb
, false);
1133 set_register_defs (def_stmt
, true);
1134 bitmap_set_bit (interesting_blocks
, def_bb
->index
);
1135 release_ssa_name (name
);
1139 auto_vec
<var_info
*> vars (var_infos
->elements ());
1140 FOR_EACH_HASH_TABLE_ELEMENT (*var_infos
, info
, var_info_p
, hi
)
1141 if (info
->info
.need_phi_state
!= NEED_PHI_STATE_NO
)
1142 vars
.quick_push (info
);
1144 /* Do two stages to avoid code generation differences for UID
1145 differences but no UID ordering differences. */
1146 vars
.qsort (insert_phi_nodes_compare_var_infos
);
1148 FOR_EACH_VEC_ELT (vars
, i
, info
)
1150 bitmap idf
= compute_idf (info
->info
.def_blocks
.def_blocks
, dfs
);
1151 insert_phi_nodes_for (info
->var
, idf
, false);
1157 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1158 register DEF (an SSA_NAME) to be a new definition for SYM. */
1161 register_new_def (tree def
, tree sym
)
1163 common_info
*info
= get_common_info (sym
);
1166 /* If this variable is set in a single basic block and all uses are
1167 dominated by the set(s) in that single basic block, then there is
1168 no reason to record anything for this variable in the block local
1169 definition stacks. Doing so just wastes time and memory.
1171 This is the same test to prune the set of variables which may
1172 need PHI nodes. So we just use that information since it's already
1173 computed and available for us to use. */
1174 if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
1176 info
->current_def
= def
;
1180 currdef
= info
->current_def
;
1182 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1183 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1184 in the stack so that we know which symbol is being defined by
1185 this SSA name when we unwind the stack. */
1186 if (currdef
&& !is_gimple_reg (sym
))
1187 block_defs_stack
.safe_push (sym
);
1189 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1190 stack is later used by the dominator tree callbacks to restore
1191 the reaching definitions for all the variables defined in the
1192 block after a recursive visit to all its immediately dominated
1193 blocks. If there is no current reaching definition, then just
1194 record the underlying _DECL node. */
1195 block_defs_stack
.safe_push (currdef
? currdef
: sym
);
1197 /* Set the current reaching definition for SYM to be DEF. */
1198 info
->current_def
= def
;
1202 /* Perform a depth-first traversal of the dominator tree looking for
1203 variables to rename. BB is the block where to start searching.
1204 Renaming is a five step process:
1206 1- Every definition made by PHI nodes at the start of the blocks is
1207 registered as the current definition for the corresponding variable.
1209 2- Every statement in BB is rewritten. USE and VUSE operands are
1210 rewritten with their corresponding reaching definition. DEF and
1211 VDEF targets are registered as new definitions.
1213 3- All the PHI nodes in successor blocks of BB are visited. The
1214 argument corresponding to BB is replaced with its current reaching
1217 4- Recursively rewrite every dominator child block of BB.
1219 5- Restore (in reverse order) the current reaching definition for every
1220 new definition introduced in this block. This is done so that when
1221 we return from the recursive call, all the current reaching
1222 definitions are restored to the names that were valid in the
1223 dominator parent of BB. */
1225 /* Return the current definition for variable VAR. If none is found,
1226 create a new SSA name to act as the zeroth definition for VAR. */
1229 get_reaching_def (tree var
)
1231 common_info
*info
= get_common_info (var
);
1234 /* Lookup the current reaching definition for VAR. */
1235 currdef
= info
->current_def
;
1237 /* If there is no reaching definition for VAR, create and register a
1238 default definition for it (if needed). */
1239 if (currdef
== NULL_TREE
)
1241 tree sym
= DECL_P (var
) ? var
: SSA_NAME_VAR (var
);
1243 sym
= create_tmp_reg (TREE_TYPE (var
));
1244 currdef
= get_or_create_ssa_default_def (cfun
, sym
);
1247 /* Return the current reaching definition for VAR, or the default
1248 definition, if we had to create one. */
1253 /* Helper function for rewrite_stmt. Rewrite uses in a debug stmt. */
1256 rewrite_debug_stmt_uses (gimple
*stmt
)
1258 use_operand_p use_p
;
1260 bool update
= false;
1262 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
1264 tree var
= USE_FROM_PTR (use_p
), def
;
1265 common_info
*info
= get_common_info (var
);
1266 gcc_checking_assert (DECL_P (var
));
1267 def
= info
->current_def
;
1270 if (TREE_CODE (var
) == PARM_DECL
1271 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun
)))
1273 gimple_stmt_iterator gsi
1275 gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
1277 /* Search a few source bind stmts at the start of first bb to
1278 see if a DEBUG_EXPR_DECL can't be reused. */
1280 !gsi_end_p (gsi
) && lim
> 0;
1281 gsi_next (&gsi
), lim
--)
1283 gimple
*gstmt
= gsi_stmt (gsi
);
1284 if (!gimple_debug_source_bind_p (gstmt
))
1286 if (gimple_debug_source_bind_get_value (gstmt
) == var
)
1288 def
= gimple_debug_source_bind_get_var (gstmt
);
1289 if (TREE_CODE (def
) == DEBUG_EXPR_DECL
)
1295 /* If not, add a new source bind stmt. */
1296 if (def
== NULL_TREE
)
1299 def
= build_debug_expr_decl (TREE_TYPE (var
));
1300 /* FIXME: Is setting the mode really necessary? */
1301 SET_DECL_MODE (def
, DECL_MODE (var
));
1302 def_temp
= gimple_build_debug_source_bind (def
, var
, NULL
);
1304 gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)));
1305 gsi_insert_before (&gsi
, def_temp
, GSI_SAME_STMT
);
1312 /* Check if info->current_def can be trusted. */
1313 basic_block bb
= gimple_bb (stmt
);
1315 = SSA_NAME_IS_DEFAULT_DEF (def
)
1316 ? NULL
: gimple_bb (SSA_NAME_DEF_STMT (def
));
1318 /* If definition is in current bb, it is fine. */
1321 /* If definition bb doesn't dominate the current bb,
1322 it can't be used. */
1323 else if (def_bb
&& !dominated_by_p (CDI_DOMINATORS
, bb
, def_bb
))
1325 /* If there is just one definition and dominates the current
1327 else if (info
->need_phi_state
== NEED_PHI_STATE_NO
)
1331 def_blocks
*db_p
= get_def_blocks_for (info
);
1333 /* If there are some non-debug uses in the current bb,
1335 if (bitmap_bit_p (db_p
->livein_blocks
, bb
->index
))
1337 /* Otherwise give up for now. */
1344 gimple_debug_bind_reset_value (stmt
);
1348 SET_USE (use_p
, def
);
1354 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1355 the block with its immediate reaching definitions. Update the current
1356 definition of a variable when a new real or virtual definition is found. */
1359 rewrite_stmt (gimple_stmt_iterator
*si
)
1361 use_operand_p use_p
;
1362 def_operand_p def_p
;
1364 gimple
*stmt
= gsi_stmt (*si
);
1366 /* If mark_def_sites decided that we don't need to rewrite this
1367 statement, ignore it. */
1368 gcc_assert (blocks_to_update
== NULL
);
1369 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
1372 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1374 fprintf (dump_file
, "Renaming statement ");
1375 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
1376 fprintf (dump_file
, "\n");
1379 /* Step 1. Rewrite USES in the statement. */
1380 if (rewrite_uses_p (stmt
))
1382 if (is_gimple_debug (stmt
))
1383 rewrite_debug_stmt_uses (stmt
);
1385 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
1387 tree var
= USE_FROM_PTR (use_p
);
1388 if (TREE_CODE (var
) == SSA_NAME
)
1390 gcc_checking_assert (DECL_P (var
));
1391 SET_USE (use_p
, get_reaching_def (var
));
1395 /* Step 2. Register the statement's DEF operands. */
1396 if (register_defs_p (stmt
))
1397 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_ALL_DEFS
)
1399 tree var
= DEF_FROM_PTR (def_p
);
1403 if (TREE_CODE (var
) == SSA_NAME
)
1405 gcc_checking_assert (DECL_P (var
));
1407 if (gimple_clobber_p (stmt
)
1408 && is_gimple_reg (var
))
1410 /* If we rewrite a DECL into SSA form then drop its
1411 clobber stmts and replace uses with a new default def. */
1412 gcc_checking_assert (VAR_P (var
) && !gimple_vdef (stmt
));
1413 gsi_replace (si
, gimple_build_nop (), true);
1414 register_new_def (get_or_create_ssa_default_def (cfun
, var
), var
);
1418 name
= make_ssa_name (var
, stmt
);
1419 SET_DEF (def_p
, name
);
1420 register_new_def (DEF_FROM_PTR (def_p
), var
);
1422 /* Do not insert debug stmts if the stmt ends the BB. */
1423 if (stmt_ends_bb_p (stmt
))
1426 tracked_var
= target_for_debug_bind (var
);
1429 gimple
*note
= gimple_build_debug_bind (tracked_var
, name
, stmt
);
1430 gsi_insert_after (si
, note
, GSI_SAME_STMT
);
1436 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1437 PHI nodes. For every PHI node found, add a new argument containing the
1438 current reaching definition for the variable and the edge through which
1439 that definition is reaching the PHI node. */
1442 rewrite_add_phi_arguments (basic_block bb
)
1447 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1452 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
);
1459 res
= gimple_phi_result (phi
);
1460 currdef
= get_reaching_def (SSA_NAME_VAR (res
));
1461 /* Virtual operand PHI args do not need a location. */
1462 if (virtual_operand_p (res
))
1463 loc
= UNKNOWN_LOCATION
;
1465 loc
= gimple_location (SSA_NAME_DEF_STMT (currdef
));
1466 add_phi_arg (phi
, currdef
, e
, loc
);
1471 class rewrite_dom_walker
: public dom_walker
1474 rewrite_dom_walker (cdi_direction direction
)
1475 : dom_walker (direction
, ALL_BLOCKS
, NULL
) {}
1477 edge
before_dom_children (basic_block
) final override
;
1478 void after_dom_children (basic_block
) final override
;
1481 /* SSA Rewriting Step 1. Initialization, create a block local stack
1482 of reaching definitions for new SSA names produced in this block
1483 (BLOCK_DEFS). Register new definitions for every PHI node in the
1487 rewrite_dom_walker::before_dom_children (basic_block bb
)
1489 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1490 fprintf (dump_file
, "\n\nRenaming block #%d\n\n", bb
->index
);
1492 /* Mark the unwind point for this block. */
1493 block_defs_stack
.safe_push (NULL_TREE
);
1495 /* Step 1. Register new definitions for every PHI node in the block.
1496 Conceptually, all the PHI nodes are executed in parallel and each PHI
1497 node introduces a new version for the associated variable. */
1498 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
1501 tree result
= gimple_phi_result (gsi_stmt (gsi
));
1502 register_new_def (result
, SSA_NAME_VAR (result
));
1505 /* Step 2. Rewrite every variable used in each statement in the block
1506 with its immediate reaching definitions. Update the current definition
1507 of a variable when a new real or virtual definition is found. */
1508 if (bitmap_bit_p (interesting_blocks
, bb
->index
))
1509 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);
1511 rewrite_stmt (&gsi
);
1513 /* Step 3. Visit all the successor blocks of BB looking for PHI nodes.
1514 For every PHI node found, add a new argument containing the current
1515 reaching definition for the variable and the edge through which that
1516 definition is reaching the PHI node. */
1517 rewrite_add_phi_arguments (bb
);
1524 /* Called after visiting all the statements in basic block BB and all
1525 of its dominator children. Restore CURRDEFS to its original value. */
1528 rewrite_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED
)
1530 /* Restore CURRDEFS to its original state. */
1531 while (block_defs_stack
.length () > 0)
1533 tree tmp
= block_defs_stack
.pop ();
1534 tree saved_def
, var
;
1536 if (tmp
== NULL_TREE
)
1539 if (TREE_CODE (tmp
) == SSA_NAME
)
1541 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1542 current definition of its underlying variable. Note that
1543 if the SSA_NAME is not for a GIMPLE register, the symbol
1544 being defined is stored in the next slot in the stack.
1545 This mechanism is needed because an SSA name for a
1546 non-register symbol may be the definition for more than
1547 one symbol (e.g., SFTs, aliased variables, etc). */
1549 var
= SSA_NAME_VAR (saved_def
);
1550 if (!is_gimple_reg (var
))
1551 var
= block_defs_stack
.pop ();
1555 /* If we recorded anything else, it must have been a _DECL
1556 node and its current reaching definition must have been
1562 get_common_info (var
)->current_def
= saved_def
;
1567 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1570 debug_decl_set (bitmap set
)
1572 dump_decl_set (stderr
, set
);
1573 fprintf (stderr
, "\n");
1577 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1578 stack up to a maximum of N levels. If N is -1, the whole stack is
1579 dumped. New levels are created when the dominator tree traversal
1580 used for renaming enters a new sub-tree. */
1583 dump_defs_stack (FILE *file
, int n
)
1587 fprintf (file
, "\n\nRenaming stack");
1589 fprintf (file
, " (up to %d levels)", n
);
1590 fprintf (file
, "\n\n");
1593 fprintf (file
, "Level %d (current level)\n", i
);
1594 for (j
= (int) block_defs_stack
.length () - 1; j
>= 0; j
--)
1598 name
= block_defs_stack
[j
];
1599 if (name
== NULL_TREE
)
1604 fprintf (file
, "\nLevel %d\n", i
);
1615 var
= SSA_NAME_VAR (name
);
1616 if (!is_gimple_reg (var
))
1619 var
= block_defs_stack
[j
];
1623 fprintf (file
, " Previous CURRDEF (");
1624 print_generic_expr (file
, var
);
1625 fprintf (file
, ") = ");
1627 print_generic_expr (file
, name
);
1629 fprintf (file
, "<NIL>");
1630 fprintf (file
, "\n");
1635 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1636 stack up to a maximum of N levels. If N is -1, the whole stack is
1637 dumped. New levels are created when the dominator tree traversal
1638 used for renaming enters a new sub-tree. */
1641 debug_defs_stack (int n
)
1643 dump_defs_stack (stderr
, n
);
1647 /* Dump the current reaching definition of every symbol to FILE. */
1650 dump_currdefs (FILE *file
)
1652 if (symbols_to_rename
.is_empty ())
1655 fprintf (file
, "\n\nCurrent reaching definitions\n\n");
1656 for (tree var
: symbols_to_rename
)
1658 common_info
*info
= get_common_info (var
);
1659 fprintf (file
, "CURRDEF (");
1660 print_generic_expr (file
, var
);
1661 fprintf (file
, ") = ");
1662 if (info
->current_def
)
1663 print_generic_expr (file
, info
->current_def
);
1665 fprintf (file
, "<NIL>");
1666 fprintf (file
, "\n");
1671 /* Dump the current reaching definition of every symbol to stderr. */
1674 debug_currdefs (void)
1676 dump_currdefs (stderr
);
1680 /* Dump SSA information to FILE. */
1683 dump_tree_ssa (FILE *file
)
1685 const char *funcname
1686 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
1688 fprintf (file
, "SSA renaming information for %s\n\n", funcname
);
1690 dump_var_infos (file
);
1691 dump_defs_stack (file
, -1);
1692 dump_currdefs (file
);
1693 dump_tree_ssa_stats (file
);
1697 /* Dump SSA information to stderr. */
1700 debug_tree_ssa (void)
1702 dump_tree_ssa (stderr
);
1706 /* Dump statistics for the hash table HTAB. */
1709 htab_statistics (FILE *file
, const hash_table
<var_info_hasher
> &htab
)
1711 fprintf (file
, "size " HOST_SIZE_T_PRINT_DEC
", " HOST_SIZE_T_PRINT_DEC
1712 " elements, %f collision/search ratio\n",
1713 (fmt_size_t
) htab
.size (),
1714 (fmt_size_t
) htab
.elements (),
1715 htab
.collisions ());
1719 /* Dump SSA statistics on FILE. */
1722 dump_tree_ssa_stats (FILE *file
)
1726 fprintf (file
, "\nHash table statistics:\n");
1727 fprintf (file
, " var_infos: ");
1728 htab_statistics (file
, *var_infos
);
1729 fprintf (file
, "\n");
1734 /* Dump SSA statistics on stderr. */
1737 debug_tree_ssa_stats (void)
1739 dump_tree_ssa_stats (stderr
);
1743 /* Callback for htab_traverse to dump the VAR_INFOS hash table. */
1746 debug_var_infos_r (var_info
**slot
, FILE *file
)
1748 var_info
*info
= *slot
;
1750 fprintf (file
, "VAR: ");
1751 print_generic_expr (file
, info
->var
, dump_flags
);
1752 bitmap_print (file
, info
->info
.def_blocks
.def_blocks
,
1753 ", DEF_BLOCKS: { ", "}");
1754 bitmap_print (file
, info
->info
.def_blocks
.livein_blocks
,
1755 ", LIVEIN_BLOCKS: { ", "}");
1756 bitmap_print (file
, info
->info
.def_blocks
.phi_blocks
,
1757 ", PHI_BLOCKS: { ", "}\n");
1763 /* Dump the VAR_INFOS hash table on FILE. */
1766 dump_var_infos (FILE *file
)
1768 fprintf (file
, "\n\nDefinition and live-in blocks:\n\n");
1770 var_infos
->traverse
<FILE *, debug_var_infos_r
> (file
);
1774 /* Dump the VAR_INFOS hash table on stderr. */
1777 debug_var_infos (void)
1779 dump_var_infos (stderr
);
1783 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1786 register_new_update_single (tree new_name
, tree old_name
)
1788 common_info
*info
= get_common_info (old_name
);
1789 tree currdef
= info
->current_def
;
1791 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1792 This stack is later used by the dominator tree callbacks to
1793 restore the reaching definitions for all the variables
1794 defined in the block after a recursive visit to all its
1795 immediately dominated blocks. */
1796 block_defs_stack
.reserve (2);
1797 block_defs_stack
.quick_push (currdef
);
1798 block_defs_stack
.quick_push (old_name
);
1800 /* Set the current reaching definition for OLD_NAME to be
1802 info
->current_def
= new_name
;
1806 /* Register NEW_NAME to be the new reaching definition for all the
1807 names in OLD_NAMES. Used by the incremental SSA update routines to
1808 replace old SSA names with new ones. */
1811 register_new_update_set (tree new_name
, bitmap old_names
)
1816 EXECUTE_IF_SET_IN_BITMAP (old_names
, 0, i
, bi
)
1817 register_new_update_single (new_name
, ssa_name (i
));
1822 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1823 it is a symbol marked for renaming, replace it with USE_P's current
1824 reaching definition. */
1827 maybe_replace_use (use_operand_p use_p
)
1829 tree rdef
= NULL_TREE
;
1830 tree use
= USE_FROM_PTR (use_p
);
1831 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1833 if (marked_for_renaming (sym
))
1834 rdef
= get_reaching_def (sym
);
1835 else if (is_old_name (use
))
1836 rdef
= get_reaching_def (use
);
1838 if (rdef
&& rdef
!= use
)
1839 SET_USE (use_p
, rdef
);
1843 /* Same as maybe_replace_use, but without introducing default stmts,
1844 returning false to indicate a need to do so. */
1847 maybe_replace_use_in_debug_stmt (use_operand_p use_p
)
1849 tree rdef
= NULL_TREE
;
1850 tree use
= USE_FROM_PTR (use_p
);
1851 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
1853 if (marked_for_renaming (sym
))
1854 rdef
= get_var_info (sym
)->info
.current_def
;
1855 else if (is_old_name (use
))
1857 rdef
= get_ssa_name_ann (use
)->info
.current_def
;
1858 /* We can't assume that, if there's no current definition, the
1859 default one should be used. It could be the case that we've
1860 rearranged blocks so that the earlier definition no longer
1861 dominates the use. */
1862 if (!rdef
&& SSA_NAME_IS_DEFAULT_DEF (use
))
1868 if (rdef
&& rdef
!= use
)
1869 SET_USE (use_p
, rdef
);
1871 return rdef
!= NULL_TREE
;
1875 /* If DEF has x_5 = ASAN_POISON () as its current def, add
1876 ASAN_POISON_USE (x_5) stmt before GSI to denote the stmt writes into
1877 a poisoned (out of scope) variable. */
1880 maybe_add_asan_poison_write (tree def
, gimple_stmt_iterator
*gsi
)
1882 tree cdef
= get_current_def (def
);
1884 && TREE_CODE (cdef
) == SSA_NAME
1885 && gimple_call_internal_p (SSA_NAME_DEF_STMT (cdef
), IFN_ASAN_POISON
))
1888 = gimple_build_call_internal (IFN_ASAN_POISON_USE
, 1, cdef
);
1889 gimple_set_location (call
, gimple_location (gsi_stmt (*gsi
)));
1890 gsi_insert_before (gsi
, call
, GSI_SAME_STMT
);
1895 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1896 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1897 register it as the current definition for the names replaced by
1898 DEF_P. Returns whether the statement should be removed. */
1901 maybe_register_def (def_operand_p def_p
, gimple
*stmt
,
1902 gimple_stmt_iterator gsi
)
1904 tree def
= DEF_FROM_PTR (def_p
);
1905 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
1906 bool to_delete
= false;
1908 /* If DEF is a naked symbol that needs renaming, create a new
1910 if (marked_for_renaming (sym
))
1914 if (gimple_clobber_p (stmt
) && is_gimple_reg (sym
))
1920 defvar
= create_tmp_reg (TREE_TYPE (sym
));
1921 /* Replace clobber stmts with a default def. This new use of a
1922 default definition may make it look like SSA_NAMEs have
1923 conflicting lifetimes, so we need special code to let them
1924 coalesce properly. */
1926 def
= get_or_create_ssa_default_def (cfun
, defvar
);
1930 if (asan_sanitize_use_after_scope ())
1931 maybe_add_asan_poison_write (def
, &gsi
);
1932 def
= make_ssa_name (def
, stmt
);
1934 SET_DEF (def_p
, def
);
1936 tree tracked_var
= target_for_debug_bind (sym
);
1939 /* If stmt ends the bb, insert the debug stmt on the non-EH
1940 edge(s) from the stmt. */
1941 if (gsi_one_before_end_p (gsi
) && stmt_ends_bb_p (stmt
))
1943 basic_block bb
= gsi_bb (gsi
);
1946 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
1947 if (!(e
->flags
& EDGE_EH
))
1949 /* asm goto can have multiple non-EH edges from the
1950 stmt. Insert on all of them where it is
1952 gcc_checking_assert (!ef
|| (gimple_code (stmt
)
1955 /* If there are other predecessors to ef->dest, then
1956 there must be PHI nodes for the modified
1957 variable, and therefore there will be debug bind
1958 stmts after the PHI nodes. The debug bind notes
1959 we'd insert would force the creation of a new
1960 block (diverging codegen) and be redundant with
1961 the post-PHI bind stmts, so don't add them.
1963 As for the exit edge, there wouldn't be redundant
1964 bind stmts, but there wouldn't be a PC to bind
1965 them to either, so avoid diverging the CFG. */
1967 && single_pred_p (e
->dest
)
1968 && gimple_seq_empty_p (phi_nodes (e
->dest
))
1969 && e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
))
1971 /* If there were PHI nodes in the node, we'd
1972 have to make sure the value we're binding
1973 doesn't need rewriting. But there shouldn't
1974 be PHI nodes in a single-predecessor block,
1975 so we just add the note. */
1977 = gimple_build_debug_bind (tracked_var
, def
,
1979 gsi_insert_on_edge_immediate (ef
, note
);
1986 = gimple_build_debug_bind (tracked_var
, def
, stmt
);
1987 gsi_insert_after (&gsi
, note
, GSI_SAME_STMT
);
1992 register_new_update_single (def
, sym
);
1996 /* If DEF is a new name, register it as a new definition
1997 for all the names replaced by DEF. */
1998 if (is_new_name (def
))
1999 register_new_update_set (def
, names_replaced_by (def
));
2001 /* If DEF is an old name, register DEF as a new
2002 definition for itself. */
2003 if (is_old_name (def
))
2004 register_new_update_single (def
, def
);
2011 /* Update every variable used in the statement pointed-to by SI. The
2012 statement is assumed to be in SSA form already. Names in
2013 OLD_SSA_NAMES used by SI will be updated to their current reaching
2014 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
2015 will be registered as a new definition for their corresponding name
2016 in OLD_SSA_NAMES. Returns whether STMT should be removed. */
2019 rewrite_update_stmt (gimple
*stmt
, gimple_stmt_iterator gsi
)
2021 use_operand_p use_p
;
2022 def_operand_p def_p
;
2025 /* Only update marked statements. */
2026 if (!rewrite_uses_p (stmt
) && !register_defs_p (stmt
))
2029 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2031 fprintf (dump_file
, "Updating SSA information for statement ");
2032 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
2035 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
2036 symbol is marked for renaming. */
2037 if (rewrite_uses_p (stmt
))
2039 if (is_gimple_debug (stmt
))
2041 bool failed
= false;
2043 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
2044 if (!maybe_replace_use_in_debug_stmt (use_p
))
2052 /* DOM sometimes threads jumps in such a way that a
2053 debug stmt ends up referencing a SSA variable that no
2054 longer dominates the debug stmt, but such that all
2055 incoming definitions refer to the same definition in
2056 an earlier dominator. We could try to recover that
2057 definition somehow, but this will have to do for now.
2059 Introducing a default definition, which is what
2060 maybe_replace_use() would do in such cases, may
2061 modify code generation, for the otherwise-unused
2062 default definition would never go away, modifying SSA
2063 version numbers all over. */
2064 gimple_debug_bind_reset_value (stmt
);
2070 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_ALL_USES
)
2071 maybe_replace_use (use_p
);
2075 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
2076 Also register definitions for names whose underlying symbol is
2077 marked for renaming. */
2078 bool to_delete
= false;
2079 if (register_defs_p (stmt
))
2080 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, iter
, SSA_OP_ALL_DEFS
)
2081 to_delete
|= maybe_register_def (def_p
, stmt
, gsi
);
2087 /* Visit all the successor blocks of BB looking for PHI nodes. For
2088 every PHI node found, check if any of its arguments is in
2089 OLD_SSA_NAMES. If so, and if the argument has a current reaching
2090 definition, replace it. */
2093 rewrite_update_phi_arguments (basic_block bb
)
2098 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
2102 if (!bitmap_bit_p (blocks_with_phis_to_rewrite
, e
->dest
->index
))
2105 phis
= phis_to_rewrite
[e
->dest
->index
];
2106 for (gphi
*phi
: phis
)
2108 tree arg
, lhs_sym
, reaching_def
= NULL
;
2109 use_operand_p arg_p
;
2111 gcc_checking_assert (rewrite_uses_p (phi
));
2113 arg_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
2114 arg
= USE_FROM_PTR (arg_p
);
2116 if (arg
&& !DECL_P (arg
) && TREE_CODE (arg
) != SSA_NAME
)
2119 lhs_sym
= SSA_NAME_VAR (gimple_phi_result (phi
));
2121 if (arg
== NULL_TREE
)
2123 /* When updating a PHI node for a recently introduced
2124 symbol we may find NULL arguments. That's why we
2125 take the symbol from the LHS of the PHI node. */
2126 reaching_def
= get_reaching_def (lhs_sym
);
2130 tree sym
= DECL_P (arg
) ? arg
: SSA_NAME_VAR (arg
);
2132 if (marked_for_renaming (sym
))
2133 reaching_def
= get_reaching_def (sym
);
2134 else if (is_old_name (arg
))
2135 reaching_def
= get_reaching_def (arg
);
2138 /* Update the argument if there is a reaching def different
2140 if (reaching_def
&& reaching_def
!= arg
)
2143 int arg_i
= PHI_ARG_INDEX_FROM_USE (arg_p
);
2145 SET_USE (arg_p
, reaching_def
);
2147 /* Virtual operands do not need a location. */
2148 if (virtual_operand_p (reaching_def
))
2149 locus
= UNKNOWN_LOCATION
;
2150 /* If SSA update didn't insert this PHI the argument
2151 might have a location already, keep that. */
2152 else if (gimple_phi_arg_has_location (phi
, arg_i
))
2153 locus
= gimple_phi_arg_location (phi
, arg_i
);
2156 gimple
*stmt
= SSA_NAME_DEF_STMT (reaching_def
);
2157 gphi
*other_phi
= dyn_cast
<gphi
*> (stmt
);
2159 /* Single element PHI nodes behave like copies, so get the
2160 location from the phi argument. */
2162 && gimple_phi_num_args (other_phi
) == 1)
2163 locus
= gimple_phi_arg_location (other_phi
, 0);
2165 locus
= gimple_location (stmt
);
2168 gimple_phi_arg_set_location (phi
, arg_i
, locus
);
2171 if (e
->flags
& EDGE_ABNORMAL
)
2172 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p
)) = 1;
2177 class rewrite_update_dom_walker
: public dom_walker
2180 rewrite_update_dom_walker (cdi_direction direction
, int in_region_flag
= -1)
2181 : dom_walker (direction
, ALL_BLOCKS
, (int *)(uintptr_t)-1),
2182 m_in_region_flag (in_region_flag
) {}
2184 edge
before_dom_children (basic_block
) final override
;
2185 void after_dom_children (basic_block
) final override
;
2187 int m_in_region_flag
;
2190 /* Initialization of block data structures for the incremental SSA
2191 update pass. Create a block local stack of reaching definitions
2192 for new SSA names produced in this block (BLOCK_DEFS). Register
2193 new definitions for every PHI node in the block. */
2196 rewrite_update_dom_walker::before_dom_children (basic_block bb
)
2198 bool is_abnormal_phi
;
2200 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2201 fprintf (dump_file
, "Registering new PHI nodes in block #%d\n",
2204 /* Mark the unwind point for this block. */
2205 block_defs_stack
.safe_push (NULL_TREE
);
2207 if (m_in_region_flag
!= -1
2208 && !(bb
->flags
& m_in_region_flag
))
2211 if (!bitmap_bit_p (blocks_to_update
, bb
->index
))
2214 /* Mark the LHS if any of the arguments flows through an abnormal
2216 is_abnormal_phi
= bb_has_abnormal_pred (bb
);
2218 /* If any of the PHI nodes is a replacement for a name in
2219 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2220 register it as a new definition for its corresponding name. Also
2221 register definitions for names whose underlying symbols are
2222 marked for renaming. */
2223 for (gphi_iterator gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
);
2227 gphi
*phi
= gsi
.phi ();
2229 if (!register_defs_p (phi
))
2232 lhs
= gimple_phi_result (phi
);
2233 lhs_sym
= SSA_NAME_VAR (lhs
);
2235 if (marked_for_renaming (lhs_sym
))
2236 register_new_update_single (lhs
, lhs_sym
);
2240 /* If LHS is a new name, register a new definition for all
2241 the names replaced by LHS. */
2242 if (is_new_name (lhs
))
2243 register_new_update_set (lhs
, names_replaced_by (lhs
));
2245 /* If LHS is an OLD name, register it as a new definition
2247 if (is_old_name (lhs
))
2248 register_new_update_single (lhs
, lhs
);
2251 if (is_abnormal_phi
)
2252 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs
) = 1;
2255 /* Step 2. Rewrite every variable used in each statement in the block. */
2256 for (gimple_stmt_iterator gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); )
2257 if (rewrite_update_stmt (gsi_stmt (gsi
), gsi
))
2258 gsi_remove (&gsi
, true);
2262 /* Step 3. Update PHI nodes. */
2263 rewrite_update_phi_arguments (bb
);
2268 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
2269 the current reaching definition of every name re-written in BB to
2270 the original reaching definition before visiting BB. This
2271 unwinding must be done in the opposite order to what is done in
2272 register_new_update_set. */
2275 rewrite_update_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED
)
2277 while (block_defs_stack
.length () > 0)
2279 tree var
= block_defs_stack
.pop ();
2282 /* NULL indicates the unwind stop point for this block (see
2283 rewrite_update_enter_block). */
2287 saved_def
= block_defs_stack
.pop ();
2288 get_common_info (var
)->current_def
= saved_def
;
2293 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2296 ENTRY indicates the block where to start. Every block dominated by
2297 ENTRY will be rewritten.
2299 WHAT indicates what actions will be taken by the renamer (see enum
2302 REGION is a SEME region of interesting blocks for the dominator walker
2303 to process. If this set is invalid, then all the nodes dominated
2304 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2305 are not present in BLOCKS are ignored. */
2308 rewrite_blocks (basic_block entry
, enum rewrite_mode what
)
2310 block_defs_stack
.create (10);
2312 /* Recursively walk the dominator tree rewriting each statement in
2313 each basic block. */
2314 if (what
== REWRITE_ALL
)
2315 rewrite_dom_walker (CDI_DOMINATORS
).walk (entry
);
2316 else if (what
== REWRITE_UPDATE
)
2317 rewrite_update_dom_walker (CDI_DOMINATORS
).walk (entry
);
2318 else if (what
== REWRITE_UPDATE_REGION
)
2320 /* First mark all blocks in the SEME region dominated by
2321 entry and exited by blocks not backwards reachable from
2322 blocks_to_update. Optimize for dense blocks_to_update
2323 so instead of seeding the worklist with a copy of
2324 blocks_to_update treat those blocks explicit. */
2325 auto_bb_flag
in_region (cfun
);
2326 auto_vec
<basic_block
, 64> extra_rgn
;
2329 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, idx
, bi
)
2331 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, idx
);
2332 bb
->flags
|= in_region
;
2334 auto_bitmap worklist
;
2335 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, idx
, bi
)
2337 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, idx
);
2342 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2344 if ((e
->src
->flags
& in_region
)
2345 || dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
2347 bitmap_set_bit (worklist
, e
->src
->index
);
2351 while (!bitmap_empty_p (worklist
))
2353 int idx
= bitmap_clear_first_set_bit (worklist
);
2354 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, idx
);
2355 bb
->flags
|= in_region
;
2356 extra_rgn
.safe_push (bb
);
2361 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2363 if ((e
->src
->flags
& in_region
)
2364 || dominated_by_p (CDI_DOMINATORS
, e
->src
, bb
))
2366 bitmap_set_bit (worklist
, e
->src
->index
);
2370 rewrite_update_dom_walker (CDI_DOMINATORS
, in_region
).walk (entry
);
2371 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, idx
, bi
)
2373 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, idx
);
2374 bb
->flags
&= ~in_region
;
2376 for (auto bb
: extra_rgn
)
2377 bb
->flags
&= ~in_region
;
2382 /* Debugging dumps. */
2383 if (dump_file
&& (dump_flags
& TDF_STATS
))
2385 dump_dfa_stats (dump_file
);
2387 dump_tree_ssa_stats (dump_file
);
2390 block_defs_stack
.release ();
2393 class mark_def_dom_walker
: public dom_walker
2396 mark_def_dom_walker (cdi_direction direction
);
2397 ~mark_def_dom_walker ();
2399 edge
before_dom_children (basic_block
) final override
;
2402 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2403 large enough to accommodate all the variables referenced in the
2404 function, not just the ones we are renaming. */
2408 mark_def_dom_walker::mark_def_dom_walker (cdi_direction direction
)
2409 : dom_walker (direction
, ALL_BLOCKS
, NULL
), m_kills (BITMAP_ALLOC (NULL
))
2413 mark_def_dom_walker::~mark_def_dom_walker ()
2415 BITMAP_FREE (m_kills
);
2418 /* Block processing routine for mark_def_sites. Clear the KILLS bitmap
2419 at the start of each block, and call mark_def_sites for each statement. */
2422 mark_def_dom_walker::before_dom_children (basic_block bb
)
2424 gimple_stmt_iterator gsi
;
2426 bitmap_clear (m_kills
);
2427 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
2428 mark_def_sites (bb
, gsi_stmt (gsi
), m_kills
);
2432 /* Initialize internal data needed during renaming. */
2435 init_ssa_renamer (void)
2437 cfun
->gimple_df
->in_ssa_p
= false;
2439 /* Allocate memory for the DEF_BLOCKS hash table. */
2440 gcc_assert (!var_infos
);
2441 var_infos
= new hash_table
<var_info_hasher
>
2442 (vec_safe_length (cfun
->local_decls
));
2444 bitmap_obstack_initialize (&update_ssa_obstack
);
2448 /* Deallocate internal data structures used by the renamer. */
2451 fini_ssa_renamer (void)
2456 bitmap_obstack_release (&update_ssa_obstack
);
2458 cfun
->gimple_df
->ssa_renaming_needed
= 0;
2459 cfun
->gimple_df
->rename_vops
= 0;
2460 cfun
->gimple_df
->in_ssa_p
= true;
2463 /* Main entry point into the SSA builder. The renaming process
2464 proceeds in four main phases:
2466 1- Compute dominance frontier and immediate dominators, needed to
2467 insert PHI nodes and rename the function in dominator tree
2470 2- Find and mark all the blocks that define variables.
2472 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2474 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2476 Steps 3 and 4 are done using the dominator tree walker
2477 (walk_dominator_tree). */
2481 const pass_data pass_data_build_ssa
=
2483 GIMPLE_PASS
, /* type */
2485 OPTGROUP_NONE
, /* optinfo_flags */
2486 TV_TREE_INTO_SSA
, /* tv_id */
2487 PROP_cfg
, /* properties_required */
2488 PROP_ssa
, /* properties_provided */
2489 0, /* properties_destroyed */
2490 0, /* todo_flags_start */
2491 TODO_remove_unused_locals
, /* todo_flags_finish */
2494 class pass_build_ssa
: public gimple_opt_pass
2497 pass_build_ssa (gcc::context
*ctxt
)
2498 : gimple_opt_pass (pass_data_build_ssa
, ctxt
)
2501 /* opt_pass methods: */
2502 bool gate (function
*fun
) final override
2504 /* Do nothing for functions that were produced already in SSA form. */
2505 return !(fun
->curr_properties
& PROP_ssa
);
2508 unsigned int execute (function
*) final override
;
2510 }; // class pass_build_ssa
2513 pass_build_ssa::execute (function
*fun
)
2518 /* Increase the set of variables we can rewrite into SSA form
2519 by clearing TREE_ADDRESSABLE and transform the IL to support this. */
2521 execute_update_addresses_taken ();
2523 /* Initialize operand data structures. */
2524 init_ssa_operands (fun
);
2526 /* Initialize internal data needed by the renamer. */
2527 init_ssa_renamer ();
2529 /* Initialize the set of interesting blocks. The callback
2530 mark_def_sites will add to this set those blocks that the renamer
2532 interesting_blocks
= sbitmap_alloc (last_basic_block_for_fn (fun
));
2533 bitmap_clear (interesting_blocks
);
2535 /* Initialize dominance frontier. */
2536 dfs
= XNEWVEC (bitmap_head
, last_basic_block_for_fn (fun
));
2537 FOR_EACH_BB_FN (bb
, fun
)
2538 bitmap_initialize (&dfs
[bb
->index
], &bitmap_default_obstack
);
2540 /* 1- Compute dominance frontiers. */
2541 calculate_dominance_info (CDI_DOMINATORS
);
2542 compute_dominance_frontiers (dfs
);
2544 /* 2- Find and mark definition sites. */
2545 mark_def_dom_walker (CDI_DOMINATORS
).walk (fun
->cfg
->x_entry_block_ptr
);
2547 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2548 insert_phi_nodes (dfs
);
2550 /* 4- Rename all the blocks. */
2551 rewrite_blocks (ENTRY_BLOCK_PTR_FOR_FN (fun
), REWRITE_ALL
);
2553 /* Free allocated memory. */
2554 FOR_EACH_BB_FN (bb
, fun
)
2555 bitmap_clear (&dfs
[bb
->index
]);
2558 sbitmap_free (interesting_blocks
);
2559 interesting_blocks
= NULL
;
2561 fini_ssa_renamer ();
2563 /* Try to get rid of all gimplifier generated temporaries by making
2564 its SSA names anonymous. This way we can garbage collect them
2565 all after removing unused locals which we do in our TODO. */
2569 FOR_EACH_SSA_NAME (i
, name
, cfun
)
2571 if (SSA_NAME_IS_DEFAULT_DEF (name
))
2573 tree decl
= SSA_NAME_VAR (name
);
2576 && !VAR_DECL_IS_VIRTUAL_OPERAND (decl
)
2577 && DECL_IGNORED_P (decl
))
2578 SET_SSA_NAME_VAR_OR_IDENTIFIER (name
, DECL_NAME (decl
));
2581 /* Initialize SSA_NAME_POINTS_TO_READONLY_MEMORY. */
2583 = lookup_attribute ("fn spec",
2584 TYPE_ATTRIBUTES (TREE_TYPE (fun
->decl
)));
2587 attr_fnspec
fnspec (TREE_VALUE (TREE_VALUE (fnspec_tree
)));
2589 for (tree arg
= DECL_ARGUMENTS (cfun
->decl
);
2590 arg
; arg
= DECL_CHAIN (arg
), ++i
)
2592 if (!fnspec
.arg_specified_p (i
))
2594 if (fnspec
.arg_readonly_p (i
))
2596 tree name
= ssa_default_def (fun
, arg
);
2598 SSA_NAME_POINTS_TO_READONLY_MEMORY (name
) = 1;
2609 make_pass_build_ssa (gcc::context
*ctxt
)
2611 return new pass_build_ssa (ctxt
);
2615 /* Mark the definition of VAR at STMT and BB as interesting for the
2616 renamer. BLOCKS is the set of blocks that need updating. */
2619 mark_def_interesting (tree var
, gimple
*stmt
, basic_block bb
,
2622 gcc_checking_assert (bitmap_bit_p (blocks_to_update
, bb
->index
));
2623 set_register_defs (stmt
, true);
2627 bool is_phi_p
= gimple_code (stmt
) == GIMPLE_PHI
;
2629 set_def_block (var
, bb
, is_phi_p
);
2631 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2632 site for both itself and all the old names replaced by it. */
2633 if (TREE_CODE (var
) == SSA_NAME
&& is_new_name (var
))
2637 bitmap set
= names_replaced_by (var
);
2639 EXECUTE_IF_SET_IN_BITMAP (set
, 0, i
, bi
)
2640 set_def_block (ssa_name (i
), bb
, is_phi_p
);
2646 /* Mark the use of VAR at STMT and BB as interesting for the
2647 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2651 mark_use_interesting (tree var
, gimple
*stmt
, basic_block bb
,
2654 basic_block def_bb
= gimple_bb (stmt
);
2656 mark_block_for_update (def_bb
);
2657 mark_block_for_update (bb
);
2659 if (gimple_code (stmt
) == GIMPLE_PHI
)
2660 mark_phi_for_rewrite (def_bb
, as_a
<gphi
*> (stmt
));
2663 set_rewrite_uses (stmt
, true);
2665 if (is_gimple_debug (stmt
))
2669 /* If VAR has not been defined in BB, then it is live-on-entry
2670 to BB. Note that we cannot just use the block holding VAR's
2671 definition because if VAR is one of the names in OLD_SSA_NAMES,
2672 it will have several definitions (itself and all the names that
2676 def_blocks
*db_p
= get_def_blocks_for (get_common_info (var
));
2677 if (!bitmap_bit_p (db_p
->def_blocks
, bb
->index
))
2678 set_livein_block (var
, bb
);
2682 /* Processing statements in BB that reference symbols in SSA operands.
2683 This is very similar to mark_def_sites, but the scan handles
2684 statements whose operands may already be SSA names.
2686 If INSERT_PHI_P is true, mark those uses as live in the
2687 corresponding block. This is later used by the PHI placement
2688 algorithm to make PHI pruning decisions.
2690 FIXME. Most of this would be unnecessary if we could associate a
2691 symbol to all the SSA names that reference it. But that
2692 sounds like it would be expensive to maintain. Still, it
2693 would be interesting to see if it makes better sense to do
2697 prepare_block_for_update_1 (basic_block bb
, bool insert_phi_p
)
2702 mark_block_for_update (bb
);
2704 /* Process PHI nodes marking interesting those that define or use
2705 the symbols that we are interested in. */
2706 for (gphi_iterator si
= gsi_start_phis (bb
); !gsi_end_p (si
);
2709 gphi
*phi
= si
.phi ();
2710 tree lhs_sym
, lhs
= gimple_phi_result (phi
);
2712 if (TREE_CODE (lhs
) == SSA_NAME
2713 && (! virtual_operand_p (lhs
)
2714 || ! cfun
->gimple_df
->rename_vops
))
2717 lhs_sym
= DECL_P (lhs
) ? lhs
: SSA_NAME_VAR (lhs
);
2718 mark_for_renaming (lhs_sym
);
2719 mark_def_interesting (lhs_sym
, phi
, bb
, insert_phi_p
);
2721 /* Mark the uses in phi nodes as interesting. It would be more correct
2722 to process the arguments of the phi nodes of the successor edges of
2723 BB at the end of prepare_block_for_update, however, that turns out
2724 to be significantly more expensive. Doing it here is conservatively
2725 correct -- it may only cause us to believe a value to be live in a
2726 block that also contains its definition, and thus insert a few more
2727 phi nodes for it. */
2728 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
2729 mark_use_interesting (lhs_sym
, phi
, e
->src
, insert_phi_p
);
2732 /* Process the statements. */
2733 for (gimple_stmt_iterator si
= gsi_start_bb (bb
); !gsi_end_p (si
);
2738 use_operand_p use_p
;
2739 def_operand_p def_p
;
2741 stmt
= gsi_stmt (si
);
2743 if (cfun
->gimple_df
->rename_vops
2744 && gimple_vuse (stmt
))
2746 tree use
= gimple_vuse (stmt
);
2747 tree sym
= DECL_P (use
) ? use
: SSA_NAME_VAR (use
);
2748 mark_for_renaming (sym
);
2749 mark_use_interesting (sym
, stmt
, bb
, insert_phi_p
);
2752 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, i
, SSA_OP_USE
)
2754 tree use
= USE_FROM_PTR (use_p
);
2757 mark_for_renaming (use
);
2758 mark_use_interesting (use
, stmt
, bb
, insert_phi_p
);
2761 if (cfun
->gimple_df
->rename_vops
2762 && gimple_vdef (stmt
))
2764 tree def
= gimple_vdef (stmt
);
2765 tree sym
= DECL_P (def
) ? def
: SSA_NAME_VAR (def
);
2766 mark_for_renaming (sym
);
2767 mark_def_interesting (sym
, stmt
, bb
, insert_phi_p
);
2770 FOR_EACH_SSA_DEF_OPERAND (def_p
, stmt
, i
, SSA_OP_DEF
)
2772 tree def
= DEF_FROM_PTR (def_p
);
2775 mark_for_renaming (def
);
2776 mark_def_interesting (def
, stmt
, bb
, insert_phi_p
);
2782 /* Do a dominator walk starting at BB processing statements that
2783 reference symbols in SSA operands. This is very similar to
2784 mark_def_sites, but the scan handles statements whose operands may
2785 already be SSA names.
2787 If INSERT_PHI_P is true, mark those uses as live in the
2788 corresponding block. This is later used by the PHI placement
2789 algorithm to make PHI pruning decisions.
2791 FIXME. Most of this would be unnecessary if we could associate a
2792 symbol to all the SSA names that reference it. But that
2793 sounds like it would be expensive to maintain. Still, it
2794 would be interesting to see if it makes better sense to do
2797 prepare_block_for_update (basic_block bb
, bool insert_phi_p
)
2800 basic_block
*worklist
;
2802 /* Allocate the worklist. */
2803 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
2804 /* Add the BB to the worklist. */
2805 worklist
[sp
++] = bb
;
2812 /* Pick a block from the worklist. */
2813 bb
= worklist
[--sp
];
2815 prepare_block_for_update_1 (bb
, insert_phi_p
);
2817 /* Now add all the blocks dominated by BB to the worklist. */
2818 for (son
= first_dom_son (CDI_DOMINATORS
, bb
);
2820 son
= next_dom_son (CDI_DOMINATORS
, son
))
2821 worklist
[sp
++] = son
;
2826 /* Helper for prepare_names_to_update. Mark all the use sites for
2827 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2828 prepare_names_to_update. */
2831 prepare_use_sites_for (tree name
, bool insert_phi_p
)
2833 use_operand_p use_p
;
2834 imm_use_iterator iter
;
2836 /* If we rename virtual operands do not update them. */
2837 if (virtual_operand_p (name
)
2838 && cfun
->gimple_df
->rename_vops
)
2841 FOR_EACH_IMM_USE_FAST (use_p
, iter
, name
)
2843 gimple
*stmt
= USE_STMT (use_p
);
2844 basic_block bb
= gimple_bb (stmt
);
2846 if (gimple_code (stmt
) == GIMPLE_PHI
)
2848 int ix
= PHI_ARG_INDEX_FROM_USE (use_p
);
2849 edge e
= gimple_phi_arg_edge (as_a
<gphi
*> (stmt
), ix
);
2850 mark_use_interesting (name
, stmt
, e
->src
, insert_phi_p
);
2854 /* For regular statements, mark this as an interesting use
2856 mark_use_interesting (name
, stmt
, bb
, insert_phi_p
);
2862 /* Helper for prepare_names_to_update. Mark the definition site for
2863 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2864 prepare_names_to_update. */
2867 prepare_def_site_for (tree name
, bool insert_phi_p
)
2872 gcc_checking_assert (names_to_release
== NULL
2873 || !bitmap_bit_p (names_to_release
,
2874 SSA_NAME_VERSION (name
)));
2876 /* If we rename virtual operands do not update them. */
2877 if (virtual_operand_p (name
)
2878 && cfun
->gimple_df
->rename_vops
)
2881 stmt
= SSA_NAME_DEF_STMT (name
);
2882 bb
= gimple_bb (stmt
);
2885 gcc_checking_assert (bb
->index
< last_basic_block_for_fn (cfun
));
2886 mark_block_for_update (bb
);
2887 mark_def_interesting (name
, stmt
, bb
, insert_phi_p
);
2892 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2893 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2894 PHI nodes for newly created names. */
2897 prepare_names_to_update (bool insert_phi_p
)
2901 sbitmap_iterator sbi
;
2903 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2904 remove it from NEW_SSA_NAMES so that we don't try to visit its
2905 defining basic block (which most likely doesn't exist). Notice
2906 that we cannot do the same with names in OLD_SSA_NAMES because we
2907 want to replace existing instances. */
2908 if (names_to_release
)
2909 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2910 bitmap_clear_bit (new_ssa_names
, i
);
2912 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2913 names may be considered to be live-in on blocks that contain
2914 definitions for their replacements. */
2915 EXECUTE_IF_SET_IN_BITMAP (new_ssa_names
, 0, i
, sbi
)
2916 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2918 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2919 OLD_SSA_NAMES, but we have to ignore its definition site. */
2920 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names
, 0, i
, sbi
)
2922 if (names_to_release
== NULL
|| !bitmap_bit_p (names_to_release
, i
))
2923 prepare_def_site_for (ssa_name (i
), insert_phi_p
);
2924 prepare_use_sites_for (ssa_name (i
), insert_phi_p
);
2929 /* Dump all the names replaced by NAME to FILE. */
2932 dump_names_replaced_by (FILE *file
, tree name
)
2938 print_generic_expr (file
, name
);
2939 fprintf (file
, " -> { ");
2941 old_set
= names_replaced_by (name
);
2942 EXECUTE_IF_SET_IN_BITMAP (old_set
, 0, i
, bi
)
2944 print_generic_expr (file
, ssa_name (i
));
2945 fprintf (file
, " ");
2948 fprintf (file
, "}\n");
2952 /* Dump all the names replaced by NAME to stderr. */
2955 debug_names_replaced_by (tree name
)
2957 dump_names_replaced_by (stderr
, name
);
2961 /* Dump SSA update information to FILE. */
2964 dump_update_ssa (FILE *file
)
2969 if (!need_ssa_update_p (cfun
))
2972 if (new_ssa_names
&& !bitmap_empty_p (new_ssa_names
))
2974 sbitmap_iterator sbi
;
2976 fprintf (file
, "\nSSA replacement table\n");
2977 fprintf (file
, "N_i -> { O_1 ... O_j } means that N_i replaces "
2978 "O_1, ..., O_j\n\n");
2980 EXECUTE_IF_SET_IN_BITMAP (new_ssa_names
, 0, i
, sbi
)
2981 dump_names_replaced_by (file
, ssa_name (i
));
2984 if (symbols_to_rename_set
&& !bitmap_empty_p (symbols_to_rename_set
))
2986 fprintf (file
, "\nSymbols to be put in SSA form\n");
2987 dump_decl_set (file
, symbols_to_rename_set
);
2988 fprintf (file
, "\n");
2991 if (names_to_release
&& !bitmap_empty_p (names_to_release
))
2993 fprintf (file
, "\nSSA names to release after updating the SSA web\n\n");
2994 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
2996 print_generic_expr (file
, ssa_name (i
));
2997 fprintf (file
, " ");
2999 fprintf (file
, "\n");
3004 /* Dump SSA update information to stderr. */
3007 debug_update_ssa (void)
3009 dump_update_ssa (stderr
);
3013 /* Initialize data structures used for incremental SSA updates. */
3016 init_update_ssa (struct function
*fn
)
3018 /* Reserve more space than the current number of names. The calls to
3019 add_new_name_mapping are typically done after creating new SSA
3020 names, so we'll need to reallocate these arrays. */
3021 old_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
3022 bitmap_clear (old_ssa_names
);
3024 new_ssa_names
= sbitmap_alloc (num_ssa_names
+ NAME_SETS_GROWTH_FACTOR
);
3025 bitmap_clear (new_ssa_names
);
3027 bitmap_obstack_initialize (&update_ssa_obstack
);
3029 names_to_release
= NULL
;
3030 update_ssa_initialized_fn
= fn
;
3034 /* Deallocate data structures used for incremental SSA updates. */
3037 delete_update_ssa (void)
3042 sbitmap_free (old_ssa_names
);
3043 old_ssa_names
= NULL
;
3045 sbitmap_free (new_ssa_names
);
3046 new_ssa_names
= NULL
;
3048 BITMAP_FREE (symbols_to_rename_set
);
3049 symbols_to_rename_set
= NULL
;
3050 symbols_to_rename
.release ();
3052 if (names_to_release
)
3054 EXECUTE_IF_SET_IN_BITMAP (names_to_release
, 0, i
, bi
)
3055 release_ssa_name (ssa_name (i
));
3056 BITMAP_FREE (names_to_release
);
3059 clear_ssa_name_info ();
3061 fini_ssa_renamer ();
3063 if (blocks_with_phis_to_rewrite
)
3064 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite
, 0, i
, bi
)
3065 phis_to_rewrite
[i
].release ();
3067 BITMAP_FREE (blocks_with_phis_to_rewrite
);
3068 BITMAP_FREE (blocks_to_update
);
3070 update_ssa_initialized_fn
= NULL
;
3074 /* Create a new name for OLD_NAME in statement STMT and replace the
3075 operand pointed to by DEF_P with the newly created name. If DEF_P
3076 is NULL then STMT should be a GIMPLE assignment.
3077 Return the new name and register the replacement mapping <NEW, OLD> in
3078 update_ssa's tables. */
3081 create_new_def_for (tree old_name
, gimple
*stmt
, def_operand_p def
)
3085 timevar_push (TV_TREE_SSA_INCREMENTAL
);
3087 if (!update_ssa_initialized_fn
)
3088 init_update_ssa (cfun
);
3090 gcc_assert (update_ssa_initialized_fn
== cfun
);
3092 new_name
= duplicate_ssa_name (old_name
, stmt
);
3094 SET_DEF (def
, new_name
);
3096 gimple_assign_set_lhs (stmt
, new_name
);
3098 if (gimple_code (stmt
) == GIMPLE_PHI
)
3100 basic_block bb
= gimple_bb (stmt
);
3102 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
3103 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name
) = bb_has_abnormal_pred (bb
);
3106 add_new_name_mapping (new_name
, old_name
);
3108 /* For the benefit of passes that will be updating the SSA form on
3109 their own, set the current reaching definition of OLD_NAME to be
3111 get_ssa_name_ann (old_name
)->info
.current_def
= new_name
;
3113 timevar_pop (TV_TREE_SSA_INCREMENTAL
);
3119 /* Mark virtual operands of FN for renaming by update_ssa. */
3122 mark_virtual_operands_for_renaming (struct function
*fn
)
3124 fn
->gimple_df
->ssa_renaming_needed
= 1;
3125 fn
->gimple_df
->rename_vops
= 1;
3128 /* Replace all uses of NAME by underlying variable and mark it
3129 for renaming. This assumes the defining statement of NAME is
3130 going to be removed. */
3133 mark_virtual_operand_for_renaming (tree name
)
3135 tree name_var
= SSA_NAME_VAR (name
);
3137 imm_use_iterator iter
;
3138 use_operand_p use_p
;
3141 gcc_assert (VAR_DECL_IS_VIRTUAL_OPERAND (name_var
));
3142 FOR_EACH_IMM_USE_STMT (stmt
, iter
, name
)
3144 FOR_EACH_IMM_USE_ON_STMT (use_p
, iter
)
3145 SET_USE (use_p
, name_var
);
3149 mark_virtual_operands_for_renaming (cfun
);
3152 /* Replace all uses of the virtual PHI result by its underlying variable
3153 and mark it for renaming. This assumes the PHI node is going to be
3157 mark_virtual_phi_result_for_renaming (gphi
*phi
)
3159 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3161 fprintf (dump_file
, "Marking result for renaming : ");
3162 print_gimple_stmt (dump_file
, phi
, 0, TDF_SLIM
);
3163 fprintf (dump_file
, "\n");
3166 mark_virtual_operand_for_renaming (gimple_phi_result (phi
));
3169 /* Return true if there is any work to be done by update_ssa
3173 need_ssa_update_p (struct function
*fn
)
3175 gcc_assert (fn
!= NULL
);
3176 return (update_ssa_initialized_fn
== fn
3177 || (fn
->gimple_df
&& fn
->gimple_df
->ssa_renaming_needed
));
3180 /* Return true if name N has been registered in the replacement table. */
3183 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED
)
3185 if (!update_ssa_initialized_fn
)
3188 gcc_assert (update_ssa_initialized_fn
== cfun
);
3190 return is_new_name (n
) || is_old_name (n
);
3194 /* Mark NAME to be released after update_ssa has finished. */
3197 release_ssa_name_after_update_ssa (tree name
)
3199 gcc_assert (cfun
&& update_ssa_initialized_fn
== cfun
);
3201 if (names_to_release
== NULL
)
3202 names_to_release
= BITMAP_ALLOC (NULL
);
3204 bitmap_set_bit (names_to_release
, SSA_NAME_VERSION (name
));
3208 /* Insert new PHI nodes to replace VAR. DFS contains dominance
3209 frontier information.
3211 This is slightly different than the regular PHI insertion
3212 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
3213 real names (i.e., GIMPLE registers) are inserted:
3215 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
3216 nodes inside the region affected by the block that defines VAR
3217 and the blocks that define all its replacements. All these
3218 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
3220 First, we compute the entry point to the region (ENTRY). This is
3221 given by the nearest common dominator to all the definition
3222 blocks. When computing the iterated dominance frontier (IDF), any
3223 block not strictly dominated by ENTRY is ignored.
3225 We then call the standard PHI insertion algorithm with the pruned
3228 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
3229 names is not pruned. PHI nodes are inserted at every IDF block. */
3232 insert_updated_phi_nodes_for (tree var
, bitmap_head
*dfs
,
3233 unsigned update_flags
)
3241 if (TREE_CODE (var
) == SSA_NAME
)
3242 gcc_checking_assert (is_old_name (var
));
3244 gcc_checking_assert (marked_for_renaming (var
));
3246 /* Get all the definition sites for VAR. */
3247 db
= find_def_blocks_for (var
);
3249 /* No need to do anything if there were no definitions to VAR. */
3250 if (db
== NULL
|| bitmap_empty_p (db
->def_blocks
))
3253 /* Compute the initial iterated dominance frontier. */
3254 pruned_idf
= compute_idf (db
->def_blocks
, dfs
);
3256 if (TREE_CODE (var
) == SSA_NAME
)
3258 if (update_flags
== TODO_update_ssa
)
3260 /* If doing regular SSA updates for GIMPLE registers, we are
3261 only interested in IDF blocks dominated by the nearest
3262 common dominator of all the definition blocks. */
3263 entry
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3265 if (entry
!= single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun
)))
3267 unsigned to_remove
= ~0U;
3268 EXECUTE_IF_SET_IN_BITMAP (pruned_idf
, 0, i
, bi
)
3270 if (to_remove
!= ~0U)
3272 bitmap_clear_bit (pruned_idf
, to_remove
);
3275 if (BASIC_BLOCK_FOR_FN (cfun
, i
) == entry
3276 || !dominated_by_p (CDI_DOMINATORS
,
3277 BASIC_BLOCK_FOR_FN (cfun
, i
), entry
))
3280 if (to_remove
!= ~0U)
3281 bitmap_clear_bit (pruned_idf
, to_remove
);
3285 /* Otherwise, do not prune the IDF for VAR. */
3286 gcc_checking_assert (update_flags
== TODO_update_ssa_full_phi
);
3288 /* Otherwise, VAR is a symbol that needs to be put into SSA form
3289 for the first time, so we need to compute the full IDF for
3292 if (!bitmap_empty_p (pruned_idf
))
3294 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3295 are included in the region to be updated. The feeding blocks
3296 are important to guarantee that the PHI arguments are renamed
3299 /* FIXME, this is not needed if we are updating symbols. We are
3300 already starting at the ENTRY block anyway. */
3301 EXECUTE_IF_SET_IN_BITMAP (pruned_idf
, 0, i
, bi
)
3305 basic_block bb
= BASIC_BLOCK_FOR_FN (cfun
, i
);
3307 mark_block_for_update (bb
);
3308 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
3309 if (e
->src
->index
>= NUM_FIXED_BLOCKS
)
3310 mark_block_for_update (e
->src
);
3313 insert_phi_nodes_for (var
, pruned_idf
, true);
3316 BITMAP_FREE (pruned_idf
);
3319 /* Sort symbols_to_rename after their DECL_UID. */
3322 insert_updated_phi_nodes_compare_uids (const void *a
, const void *b
)
3324 const_tree syma
= *(const const_tree
*)a
;
3325 const_tree symb
= *(const const_tree
*)b
;
3326 if (DECL_UID (syma
) == DECL_UID (symb
))
3328 return DECL_UID (syma
) < DECL_UID (symb
) ? -1 : 1;
3331 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3332 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3334 1- The names in OLD_SSA_NAMES dominated by the definitions of
3335 NEW_SSA_NAMES are all re-written to be reached by the
3336 appropriate definition from NEW_SSA_NAMES.
3338 2- If needed, new PHI nodes are added to the iterated dominance
3339 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3341 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3342 calling create_new_def_for to create new defs for names that the
3343 caller wants to replace.
3345 The caller cretaes the new names to be inserted and the names that need
3346 to be replaced by calling create_new_def_for for each old definition
3347 to be replaced. Note that the function assumes that the
3348 new defining statement has already been inserted in the IL.
3350 For instance, given the following code:
3353 2 x_1 = PHI (0, x_5)
3364 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3367 2 x_1 = PHI (0, x_5)
3380 We want to replace all the uses of x_1 with the new definitions of
3381 x_10 and x_11. Note that the only uses that should be replaced are
3382 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3383 *not* be replaced (this is why we cannot just mark symbol 'x' for
3386 Additionally, we may need to insert a PHI node at line 11 because
3387 that is a merge point for x_10 and x_11. So the use of x_1 at line
3388 11 will be replaced with the new PHI node. The insertion of PHI
3389 nodes is optional. They are not strictly necessary to preserve the
3390 SSA form, and depending on what the caller inserted, they may not
3391 even be useful for the optimizers. UPDATE_FLAGS controls various
3392 aspects of how update_ssa operates, see the documentation for
3393 TODO_update_ssa*. */
3396 update_ssa (unsigned update_flags
)
3398 basic_block bb
, start_bb
;
3402 sbitmap_iterator sbi
;
3405 /* Only one update flag should be set. */
3406 gcc_assert (update_flags
== TODO_update_ssa
3407 || update_flags
== TODO_update_ssa_no_phi
3408 || update_flags
== TODO_update_ssa_full_phi
3409 || update_flags
== TODO_update_ssa_only_virtuals
);
3411 if (!need_ssa_update_p (cfun
))
3416 timevar_push (TV_TREE_STMT_VERIFY
);
3420 FOR_EACH_BB_FN (bb
, cfun
)
3422 gimple_stmt_iterator gsi
;
3423 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3425 gimple
*stmt
= gsi_stmt (gsi
);
3428 use_operand_p use_p
;
3429 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, i
, SSA_OP_ALL_USES
)
3431 tree use
= USE_FROM_PTR (use_p
);
3432 if (TREE_CODE (use
) != SSA_NAME
)
3435 if (SSA_NAME_IN_FREE_LIST (use
))
3437 error ("statement uses released SSA name");
3438 debug_gimple_stmt (stmt
);
3439 fprintf (stderr
, "The use of ");
3440 print_generic_expr (stderr
, use
);
3441 fprintf (stderr
," should have been replaced\n");
3449 internal_error ("cannot update SSA form");
3451 timevar_pop (TV_TREE_STMT_VERIFY
);
3454 timevar_push (TV_TREE_SSA_INCREMENTAL
);
3456 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3457 fprintf (dump_file
, "\nUpdating SSA:\n");
3459 if (!update_ssa_initialized_fn
)
3460 init_update_ssa (cfun
);
3461 else if (update_flags
== TODO_update_ssa_only_virtuals
)
3463 /* If we only need to update virtuals, remove all the mappings for
3464 real names before proceeding. The caller is responsible for
3465 having dealt with the name mappings before calling update_ssa. */
3466 bitmap_clear (old_ssa_names
);
3467 bitmap_clear (new_ssa_names
);
3470 gcc_assert (update_ssa_initialized_fn
== cfun
);
3472 blocks_with_phis_to_rewrite
= BITMAP_ALLOC (NULL
);
3473 if (!phis_to_rewrite
.exists ())
3474 phis_to_rewrite
.create (last_basic_block_for_fn (cfun
) + 1);
3475 blocks_to_update
= BITMAP_ALLOC (NULL
);
3477 insert_phi_p
= (update_flags
!= TODO_update_ssa_no_phi
);
3479 /* Ensure that the dominance information is up-to-date and when we
3480 are going to compute dominance frontiers fast queries are possible. */
3481 if (insert_phi_p
|| dom_info_state (CDI_DOMINATORS
) == DOM_NONE
)
3482 calculate_dominance_info (CDI_DOMINATORS
);
3484 /* If there are names defined in the replacement table, prepare
3485 definition and use sites for all the names in NEW_SSA_NAMES and
3487 if (!bitmap_empty_p (new_ssa_names
))
3489 statistics_counter_event (cfun
, "Incremental SSA update", 1);
3491 prepare_names_to_update (insert_phi_p
);
3493 /* If all the names in NEW_SSA_NAMES had been marked for
3494 removal, and there are no symbols to rename, then there's
3495 nothing else to do. */
3496 if (bitmap_empty_p (new_ssa_names
)
3497 && !cfun
->gimple_df
->ssa_renaming_needed
)
3501 /* Next, determine the block at which to start the renaming process. */
3502 if (cfun
->gimple_df
->ssa_renaming_needed
)
3504 statistics_counter_event (cfun
, "Symbol to SSA rewrite", 1);
3506 /* If we rename bare symbols initialize the mapping to
3507 auxiliar info we need to keep track of. */
3508 var_infos
= new hash_table
<var_info_hasher
> (47);
3510 /* If we have to rename some symbols from scratch, we need to
3511 start the process at the root of the CFG. FIXME, it should
3512 be possible to determine the nearest block that had a
3513 definition for each of the symbols that are marked for
3514 updating. For now this seems more work than it's worth. */
3515 start_bb
= ENTRY_BLOCK_PTR_FOR_FN (cfun
);
3517 /* Traverse the CFG looking for existing definitions and uses of
3518 symbols in SSA operands. Mark interesting blocks and
3519 statements and set local live-in information for the PHI
3520 placement heuristics. */
3521 prepare_block_for_update (start_bb
, insert_phi_p
);
3526 FOR_EACH_SSA_NAME (i
, name
, cfun
)
3528 if (virtual_operand_p (name
))
3531 /* For all but virtual operands, which do not have SSA names
3532 with overlapping life ranges, ensure that symbols marked
3533 for renaming do not have existing SSA names associated with
3534 them as we do not re-write them out-of-SSA before going
3535 into SSA for the remaining symbol uses. */
3536 if (marked_for_renaming (SSA_NAME_VAR (name
)))
3538 fprintf (stderr
, "Existing SSA name for symbol marked for "
3540 print_generic_expr (stderr
, name
, TDF_SLIM
);
3541 fprintf (stderr
, "\n");
3542 internal_error ("SSA corruption");
3548 /* Otherwise, the entry block to the region is the nearest
3549 common dominator for the blocks in BLOCKS. */
3550 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3554 /* If requested, insert PHI nodes at the iterated dominance frontier
3555 of every block, creating new definitions for names in OLD_SSA_NAMES
3556 and for symbols found. */
3561 /* If the caller requested PHI nodes to be added, compute
3562 dominance frontiers. */
3563 dfs
= XNEWVEC (bitmap_head
, last_basic_block_for_fn (cfun
));
3564 FOR_EACH_BB_FN (bb
, cfun
)
3565 bitmap_initialize (&dfs
[bb
->index
], &bitmap_default_obstack
);
3566 compute_dominance_frontiers (dfs
);
3568 bitmap_tree_view (blocks_to_update
);
3570 /* insert_update_phi_nodes_for will call add_new_name_mapping
3571 when inserting new PHI nodes, but it will not add any
3572 new members to OLD_SSA_NAMES. */
3573 iterating_old_ssa_names
= true;
3574 sbitmap_iterator sbi
;
3575 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names
, 0, i
, sbi
)
3576 insert_updated_phi_nodes_for (ssa_name (i
), dfs
, update_flags
);
3577 iterating_old_ssa_names
= false;
3579 symbols_to_rename
.qsort (insert_updated_phi_nodes_compare_uids
);
3580 FOR_EACH_VEC_ELT (symbols_to_rename
, i
, sym
)
3581 insert_updated_phi_nodes_for (sym
, dfs
, update_flags
);
3583 bitmap_list_view (blocks_to_update
);
3585 FOR_EACH_BB_FN (bb
, cfun
)
3586 bitmap_clear (&dfs
[bb
->index
]);
3589 /* Insertion of PHI nodes may have added blocks to the region.
3590 We need to re-compute START_BB to include the newly added
3592 if (start_bb
!= ENTRY_BLOCK_PTR_FOR_FN (cfun
))
3593 start_bb
= nearest_common_dominator_for_set (CDI_DOMINATORS
,
3597 /* Reset the current definition for name and symbol before renaming
3599 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names
, 0, i
, sbi
)
3600 get_ssa_name_ann (ssa_name (i
))->info
.current_def
= NULL_TREE
;
3602 FOR_EACH_VEC_ELT (symbols_to_rename
, i
, sym
)
3603 get_var_info (sym
)->info
.current_def
= NULL_TREE
;
3605 /* Now start the renaming process at START_BB. When not inserting PHIs
3606 and thus we are avoiding work on all blocks, try to confine the
3607 rewriting domwalk to the affected region, otherwise it's not worth it. */
3608 rewrite_blocks (start_bb
,
3609 insert_phi_p
? REWRITE_UPDATE
: REWRITE_UPDATE_REGION
);
3611 /* Debugging dumps. */
3617 dump_update_ssa (dump_file
);
3619 fprintf (dump_file
, "Incremental SSA update started at block: %d\n",
3623 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3625 fprintf (dump_file
, "Number of blocks in CFG: %d\n",
3626 last_basic_block_for_fn (cfun
));
3627 fprintf (dump_file
, "Number of blocks to update: %d (%3.0f%%)\n",
3628 c
, PERCENT (c
, last_basic_block_for_fn (cfun
)));
3630 if (dump_flags
& TDF_DETAILS
)
3632 fprintf (dump_file
, "Affected blocks:");
3633 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update
, 0, i
, bi
)
3634 fprintf (dump_file
, " %u", i
);
3635 fprintf (dump_file
, "\n");
3638 fprintf (dump_file
, "\n\n");
3641 /* Free allocated memory. */
3643 delete_update_ssa ();
3645 timevar_pop (TV_TREE_SSA_INCREMENTAL
);