libcpp, c, middle-end: Optimize initializers using #embed in C
[official-gcc.git] / gcc / tree-into-ssa.cc
blobfc61d47ca777db857509cf62627d30de8347567a
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)
10 any later version.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "tree-pass.h"
29 #include "ssa.h"
30 #include "gimple-pretty-print.h"
31 #include "diagnostic-core.h"
32 #include "langhooks.h"
33 #include "cfganal.h"
34 #include "gimple-iterator.h"
35 #include "tree-cfg.h"
36 #include "tree-into-ssa.h"
37 #include "tree-dfa.h"
38 #include "tree-ssa.h"
39 #include "domwalk.h"
40 #include "statistics.h"
41 #include "stringpool.h"
42 #include "attribs.h"
43 #include "asan.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. */
56 struct def_blocks
58 /* Blocks that contain definitions of VAR. Bit I will be set if the
59 Ith block contains a definition of VAR. */
60 bitmap def_blocks;
62 /* Blocks that contain a PHI node for VAR. */
63 bitmap phi_blocks;
65 /* Blocks where VAR is live-on-entry. Similar semantics as
66 DEF_BLOCKS. */
67 bitmap livein_blocks;
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
79 definition for SYM.
81 - A _DECL node indicates that the underlying variable has no
82 current definition.
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 /* The bitmap of blocks with PHIs to rewrite. */
105 static bitmap blocks_with_phis_to_rewrite;
107 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
108 to grow as the callers to create_new_def_for will create new names on
109 the fly.
110 FIXME. Currently set to 1/3 to avoid frequent reallocations but still
111 need to find a reasonable growth strategy. */
112 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
115 /* The function the SSA updating data structures have been initialized for.
116 NULL if they need to be initialized by create_new_def_for. */
117 static struct function *update_ssa_initialized_fn = NULL;
119 /* Global data to attach to the main dominator walk structure. */
120 struct mark_def_sites_global_data
122 /* This bitmap contains the variables which are set before they
123 are used in a basic block. */
124 bitmap kills;
127 /* It is advantageous to avoid things like life analysis for variables which
128 do not need PHI nodes. This enum describes whether or not a particular
129 variable may need a PHI node. */
131 enum need_phi_state {
132 /* This is the default. If we are still in this state after finding
133 all the definition and use sites, then we will assume the variable
134 needs PHI nodes. This is probably an overly conservative assumption. */
135 NEED_PHI_STATE_UNKNOWN,
137 /* This state indicates that we have seen one or more sets of the
138 variable in a single basic block and that the sets dominate all
139 uses seen so far. If after finding all definition and use sites
140 we are still in this state, then the variable does not need any
141 PHI nodes. */
142 NEED_PHI_STATE_NO,
144 /* This state indicates that we have either seen multiple definitions of
145 the variable in multiple blocks, or that we encountered a use in a
146 block that was not dominated by the block containing the set(s) of
147 this variable. This variable is assumed to need PHI nodes. */
148 NEED_PHI_STATE_MAYBE
151 /* Information stored for both SSA names and decls. */
152 struct common_info
154 /* This field indicates whether or not the variable may need PHI nodes.
155 See the enum's definition for more detailed information about the
156 states. */
157 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
159 /* The current reaching definition replacing this var. */
160 tree current_def;
162 /* Definitions for this var. */
163 struct def_blocks def_blocks;
166 /* Information stored for decls. */
167 struct var_info
169 /* The variable. */
170 tree var;
172 /* Information stored for both SSA names and decls. */
173 common_info info;
177 /* VAR_INFOS hashtable helpers. */
179 struct var_info_hasher : free_ptr_hash <var_info>
181 static inline hashval_t hash (const value_type &);
182 static inline bool equal (const value_type &, const compare_type &);
185 inline hashval_t
186 var_info_hasher::hash (const value_type &p)
188 return DECL_UID (p->var);
191 inline bool
192 var_info_hasher::equal (const value_type &p1, const compare_type &p2)
194 return p1->var == p2->var;
198 /* Each entry in VAR_INFOS contains an element of type STRUCT
199 VAR_INFO_D. */
200 static hash_table<var_info_hasher> *var_infos;
203 /* Information stored for SSA names. */
204 struct ssa_name_info
206 /* Age of this record (so that info_for_ssa_name table can be cleared
207 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
208 are assumed to be null. */
209 unsigned age;
211 /* Replacement mappings, allocated from update_ssa_obstack. */
212 bitmap repl_set;
214 /* Information stored for both SSA names and decls. */
215 common_info info;
218 static vec<ssa_name_info *> info_for_ssa_name;
219 static unsigned current_info_for_ssa_name_age;
221 static bitmap_obstack update_ssa_obstack;
223 /* The set of blocks affected by update_ssa. */
224 static bitmap blocks_to_update;
226 /* The main entry point to the SSA renamer (rewrite_blocks) may be
227 called several times to do different, but related, tasks.
228 Initially, we need it to rename the whole program into SSA form.
229 At other times, we may need it to only rename into SSA newly
230 exposed symbols. Finally, we can also call it to incrementally fix
231 an already built SSA web. */
232 enum rewrite_mode {
233 /* Convert the whole function into SSA form. */
234 REWRITE_ALL,
236 /* Incrementally update the SSA web by replacing existing SSA
237 names with new ones. See update_ssa for details. */
238 REWRITE_UPDATE,
239 REWRITE_UPDATE_REGION
242 /* The set of symbols we ought to re-write into SSA form in update_ssa. */
243 static bitmap symbols_to_rename_set;
244 static vec<tree> symbols_to_rename;
246 /* Mark SYM for renaming. */
248 static void
249 mark_for_renaming (tree sym)
251 if (!symbols_to_rename_set)
252 symbols_to_rename_set = BITMAP_ALLOC (NULL);
253 if (bitmap_set_bit (symbols_to_rename_set, DECL_UID (sym)))
254 symbols_to_rename.safe_push (sym);
257 /* Return true if SYM is marked for renaming. */
259 static bool
260 marked_for_renaming (tree sym)
262 if (!symbols_to_rename_set || sym == NULL_TREE)
263 return false;
264 return bitmap_bit_p (symbols_to_rename_set, DECL_UID (sym));
268 /* Return true if STMT needs to be rewritten. When renaming a subset
269 of the variables, not all statements will be processed. This is
270 decided in mark_def_sites. */
272 static inline bool
273 rewrite_uses_p (gimple *stmt)
275 return gimple_visited_p (stmt);
279 /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
281 static inline void
282 set_rewrite_uses (gimple *stmt, bool rewrite_p)
284 gimple_set_visited (stmt, rewrite_p);
288 /* Return true if the DEFs created by statement STMT should be
289 registered when marking new definition sites. This is slightly
290 different than rewrite_uses_p: it's used by update_ssa to
291 distinguish statements that need to have both uses and defs
292 processed from those that only need to have their defs processed.
293 Statements that define new SSA names only need to have their defs
294 registered, but they don't need to have their uses renamed. */
296 static inline bool
297 register_defs_p (gimple *stmt)
299 return gimple_plf (stmt, GF_PLF_1) != 0;
303 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
305 static inline void
306 set_register_defs (gimple *stmt, bool register_defs_p)
308 gimple_set_plf (stmt, GF_PLF_1, register_defs_p);
312 /* Get the information associated with NAME. */
314 static inline ssa_name_info *
315 get_ssa_name_ann (tree name)
317 unsigned ver = SSA_NAME_VERSION (name);
318 unsigned len = info_for_ssa_name.length ();
319 struct ssa_name_info *info;
321 /* Re-allocate the vector at most once per update/into-SSA. */
322 if (ver >= len)
323 info_for_ssa_name.safe_grow_cleared (num_ssa_names, true);
325 /* But allocate infos lazily. */
326 info = info_for_ssa_name[ver];
327 if (!info)
329 info = XCNEW (struct ssa_name_info);
330 info->age = current_info_for_ssa_name_age;
331 info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN;
332 info_for_ssa_name[ver] = info;
335 if (info->age < current_info_for_ssa_name_age)
337 info->age = current_info_for_ssa_name_age;
338 info->repl_set = NULL;
339 info->info.need_phi_state = NEED_PHI_STATE_UNKNOWN;
340 info->info.current_def = NULL_TREE;
341 info->info.def_blocks.def_blocks = NULL;
342 info->info.def_blocks.phi_blocks = NULL;
343 info->info.def_blocks.livein_blocks = NULL;
346 return info;
349 /* Return and allocate the auxiliar information for DECL. */
351 static inline var_info *
352 get_var_info (tree decl)
354 var_info vi;
355 var_info **slot;
356 vi.var = decl;
357 slot = var_infos->find_slot_with_hash (&vi, DECL_UID (decl), INSERT);
358 if (*slot == NULL)
360 var_info *v = XCNEW (var_info);
361 v->var = decl;
362 *slot = v;
363 return v;
365 return *slot;
369 /* Clears info for SSA names. */
371 static void
372 clear_ssa_name_info (void)
374 current_info_for_ssa_name_age++;
376 /* If current_info_for_ssa_name_age wraps we use stale information.
377 Asser that this does not happen. */
378 gcc_assert (current_info_for_ssa_name_age != 0);
382 /* Get access to the auxiliar information stored per SSA name or decl. */
384 static inline common_info *
385 get_common_info (tree var)
387 if (TREE_CODE (var) == SSA_NAME)
388 return &get_ssa_name_ann (var)->info;
389 else
390 return &get_var_info (var)->info;
394 /* Return the current definition for VAR. */
396 tree
397 get_current_def (tree var)
399 return get_common_info (var)->current_def;
403 /* Sets current definition of VAR to DEF. */
405 void
406 set_current_def (tree var, tree def)
408 get_common_info (var)->current_def = def;
411 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
412 all statements in basic block BB. */
414 static void
415 initialize_flags_in_bb (basic_block bb)
417 gimple *stmt;
418 gimple_stmt_iterator gsi;
420 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
422 gimple *phi = gsi_stmt (gsi);
423 set_rewrite_uses (phi, false);
424 set_register_defs (phi, false);
427 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
429 stmt = gsi_stmt (gsi);
431 /* We are going to use the operand cache API, such as
432 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
433 cache for each statement should be up-to-date. */
434 gcc_checking_assert (!gimple_modified_p (stmt));
435 set_rewrite_uses (stmt, false);
436 set_register_defs (stmt, false);
440 /* Mark block BB as interesting for update_ssa. */
442 static void
443 mark_block_for_update (basic_block bb)
445 gcc_checking_assert (blocks_to_update != NULL);
446 if (!bitmap_set_bit (blocks_to_update, bb->index))
447 return;
448 initialize_flags_in_bb (bb);
451 /* Return the set of blocks where variable VAR is defined and the blocks
452 where VAR is live on entry (livein). If no entry is found in
453 DEF_BLOCKS, a new one is created and returned. */
455 static inline def_blocks *
456 get_def_blocks_for (common_info *info)
458 def_blocks *db_p = &info->def_blocks;
459 if (!db_p->def_blocks)
461 db_p->def_blocks = BITMAP_ALLOC (&update_ssa_obstack);
462 db_p->phi_blocks = BITMAP_ALLOC (&update_ssa_obstack);
463 db_p->livein_blocks = BITMAP_ALLOC (&update_ssa_obstack);
466 return db_p;
470 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
471 VAR is defined by a PHI node. */
473 static void
474 set_def_block (tree var, basic_block bb, bool phi_p)
476 def_blocks *db_p;
477 common_info *info;
479 info = get_common_info (var);
480 db_p = get_def_blocks_for (info);
482 /* Set the bit corresponding to the block where VAR is defined. */
483 bitmap_set_bit (db_p->def_blocks, bb->index);
484 if (phi_p)
485 bitmap_set_bit (db_p->phi_blocks, bb->index);
487 /* Keep track of whether or not we may need to insert PHI nodes.
489 If we are in the UNKNOWN state, then this is the first definition
490 of VAR. Additionally, we have not seen any uses of VAR yet, so
491 we do not need a PHI node for this variable at this time (i.e.,
492 transition to NEED_PHI_STATE_NO).
494 If we are in any other state, then we either have multiple definitions
495 of this variable occurring in different blocks or we saw a use of the
496 variable which was not dominated by the block containing the
497 definition(s). In this case we may need a PHI node, so enter
498 state NEED_PHI_STATE_MAYBE. */
499 if (info->need_phi_state == NEED_PHI_STATE_UNKNOWN)
500 info->need_phi_state = NEED_PHI_STATE_NO;
501 else
502 info->need_phi_state = NEED_PHI_STATE_MAYBE;
506 /* Mark block BB as having VAR live at the entry to BB. */
508 static void
509 set_livein_block (tree var, basic_block bb)
511 common_info *info;
512 def_blocks *db_p;
514 info = get_common_info (var);
515 db_p = get_def_blocks_for (info);
517 /* Set the bit corresponding to the block where VAR is live in. */
518 bitmap_set_bit (db_p->livein_blocks, bb->index);
520 /* Keep track of whether or not we may need to insert PHI nodes.
522 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
523 by the single block containing the definition(s) of this variable. If
524 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
525 NEED_PHI_STATE_MAYBE. */
526 if (info->need_phi_state == NEED_PHI_STATE_NO)
528 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
530 if (def_block_index == -1
531 || ! dominated_by_p (CDI_DOMINATORS, bb,
532 BASIC_BLOCK_FOR_FN (cfun, def_block_index)))
533 info->need_phi_state = NEED_PHI_STATE_MAYBE;
535 else
536 info->need_phi_state = NEED_PHI_STATE_MAYBE;
540 /* Return true if NAME is in OLD_SSA_NAMES. */
542 static inline bool
543 is_old_name (tree name)
545 unsigned ver = SSA_NAME_VERSION (name);
546 if (!old_ssa_names)
547 return false;
548 return (ver < SBITMAP_SIZE (old_ssa_names)
549 && bitmap_bit_p (old_ssa_names, ver));
553 /* Return true if NAME is in NEW_SSA_NAMES. */
555 static inline bool
556 is_new_name (tree name)
558 unsigned ver = SSA_NAME_VERSION (name);
559 if (!new_ssa_names)
560 return false;
561 return (ver < SBITMAP_SIZE (new_ssa_names)
562 && bitmap_bit_p (new_ssa_names, ver));
566 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
568 static inline bitmap
569 names_replaced_by (tree new_tree)
571 return get_ssa_name_ann (new_tree)->repl_set;
575 /* Add OLD to REPL_TBL[NEW_TREE].SET. */
577 static inline void
578 add_to_repl_tbl (tree new_tree, tree old)
580 bitmap *set = &get_ssa_name_ann (new_tree)->repl_set;
581 if (!*set)
582 *set = BITMAP_ALLOC (&update_ssa_obstack);
583 bitmap_set_bit (*set, SSA_NAME_VERSION (old));
586 /* Debugging aid to fence old_ssa_names changes when iterating over it. */
587 static bool iterating_old_ssa_names;
589 /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
590 represents the set of names O_1 ... O_j replaced by N_i. This is
591 used by update_ssa and its helpers to introduce new SSA names in an
592 already formed SSA web. */
594 static void
595 add_new_name_mapping (tree new_tree, tree old)
597 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
598 gcc_checking_assert (new_tree != old
599 && SSA_NAME_VAR (new_tree) == SSA_NAME_VAR (old));
601 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
602 caller may have created new names since the set was created. */
603 if (SBITMAP_SIZE (new_ssa_names) <= SSA_NAME_VERSION (new_tree))
605 unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
606 new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
608 if (SBITMAP_SIZE (old_ssa_names) <= SSA_NAME_VERSION (old))
610 gcc_assert (!iterating_old_ssa_names);
611 unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
612 old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
615 /* Update the REPL_TBL table. */
616 add_to_repl_tbl (new_tree, old);
618 /* If OLD had already been registered as a new name, then all the
619 names that OLD replaces should also be replaced by NEW_TREE. */
620 if (is_new_name (old))
621 bitmap_ior_into (names_replaced_by (new_tree), names_replaced_by (old));
623 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
624 respectively. */
625 if (iterating_old_ssa_names)
626 gcc_assert (bitmap_bit_p (old_ssa_names, SSA_NAME_VERSION (old)));
627 else
628 bitmap_set_bit (old_ssa_names, SSA_NAME_VERSION (old));
629 bitmap_set_bit (new_ssa_names, SSA_NAME_VERSION (new_tree));
633 /* Call back for walk_dominator_tree used to collect definition sites
634 for every variable in the function. For every statement S in block
637 1- Variables defined by S in the DEFS of S are marked in the bitmap
638 KILLS.
640 2- If S uses a variable VAR and there is no preceding kill of VAR,
641 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
643 This information is used to determine which variables are live
644 across block boundaries to reduce the number of PHI nodes
645 we create. */
647 static void
648 mark_def_sites (basic_block bb, gimple *stmt, bitmap kills)
650 tree def;
651 use_operand_p use_p;
652 ssa_op_iter iter;
654 /* Since this is the first time that we rewrite the program into SSA
655 form, force an operand scan on every statement. */
656 update_stmt (stmt);
658 gcc_checking_assert (blocks_to_update == NULL);
659 set_register_defs (stmt, false);
660 set_rewrite_uses (stmt, false);
662 if (is_gimple_debug (stmt))
664 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
666 tree sym = USE_FROM_PTR (use_p);
667 gcc_checking_assert (DECL_P (sym));
668 set_rewrite_uses (stmt, true);
670 if (rewrite_uses_p (stmt))
671 bitmap_set_bit (interesting_blocks, bb->index);
672 return;
675 /* If a variable is used before being set, then the variable is live
676 across a block boundary, so mark it live-on-entry to BB. */
677 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
679 tree sym = USE_FROM_PTR (use_p);
680 if (TREE_CODE (sym) == SSA_NAME)
681 continue;
682 gcc_checking_assert (DECL_P (sym));
683 if (!bitmap_bit_p (kills, DECL_UID (sym)))
684 set_livein_block (sym, bb);
685 set_rewrite_uses (stmt, true);
688 /* Now process the defs. Mark BB as the definition block and add
689 each def to the set of killed symbols. */
690 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
692 if (TREE_CODE (def) == SSA_NAME)
693 continue;
694 gcc_checking_assert (DECL_P (def));
695 set_def_block (def, bb, false);
696 bitmap_set_bit (kills, DECL_UID (def));
697 set_register_defs (stmt, true);
700 /* If we found the statement interesting then also mark the block BB
701 as interesting. */
702 if (rewrite_uses_p (stmt) || register_defs_p (stmt))
703 bitmap_set_bit (interesting_blocks, bb->index);
706 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
707 in the dfs numbering of the dominance tree. */
709 struct dom_dfsnum
711 /* Basic block whose index this entry corresponds to. */
712 unsigned bb_index;
714 /* The dfs number of this node. */
715 unsigned dfs_num;
718 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
719 for qsort. */
721 static int
722 cmp_dfsnum (const void *a, const void *b)
724 const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a;
725 const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b;
727 return (int) da->dfs_num - (int) db->dfs_num;
730 /* Among the intervals starting at the N points specified in DEFS, find
731 the one that contains S, and return its bb_index. */
733 static unsigned
734 find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
736 unsigned f = 0, t = n, m;
738 while (t > f + 1)
740 m = (f + t) / 2;
741 if (defs[m].dfs_num <= s)
742 f = m;
743 else
744 t = m;
747 return defs[f].bb_index;
750 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
751 KILLS is a bitmap of blocks where the value is defined before any use. */
753 static void
754 prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
756 bitmap_iterator bi;
757 unsigned i, b, p, u, top;
758 bitmap live_phis;
759 basic_block def_bb, use_bb;
760 edge e;
761 edge_iterator ei;
762 bitmap to_remove;
763 struct dom_dfsnum *defs;
764 unsigned n_defs, adef;
766 if (bitmap_empty_p (uses))
768 bitmap_clear (phis);
769 return;
772 /* The phi must dominate a use, or an argument of a live phi. Also, we
773 do not create any phi nodes in def blocks, unless they are also livein. */
774 to_remove = BITMAP_ALLOC (NULL);
775 bitmap_and_compl (to_remove, kills, uses);
776 bitmap_and_compl_into (phis, to_remove);
777 if (bitmap_empty_p (phis))
779 BITMAP_FREE (to_remove);
780 return;
783 /* We want to remove the unnecessary phi nodes, but we do not want to compute
784 liveness information, as that may be linear in the size of CFG, and if
785 there are lot of different variables to rewrite, this may lead to quadratic
786 behavior.
788 Instead, we basically emulate standard dce. We put all uses to worklist,
789 then for each of them find the nearest def that dominates them. If this
790 def is a phi node, we mark it live, and if it was not live before, we
791 add the predecessors of its basic block to the worklist.
793 To quickly locate the nearest def that dominates use, we use dfs numbering
794 of the dominance tree (that is already available in order to speed up
795 queries). For each def, we have the interval given by the dfs number on
796 entry to and on exit from the corresponding subtree in the dominance tree.
797 The nearest dominator for a given use is the smallest of these intervals
798 that contains entry and exit dfs numbers for the basic block with the use.
799 If we store the bounds for all the uses to an array and sort it, we can
800 locate the nearest dominating def in logarithmic time by binary search.*/
801 bitmap_ior (to_remove, kills, phis);
802 n_defs = bitmap_count_bits (to_remove);
803 adef = 2 * n_defs + 1;
804 defs = XNEWVEC (struct dom_dfsnum, adef);
805 defs[0].bb_index = 1;
806 defs[0].dfs_num = 0;
807 struct dom_dfsnum *head = defs + 1, *tail = defs + adef;
808 EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
810 def_bb = BASIC_BLOCK_FOR_FN (cfun, i);
811 head->bb_index = i;
812 head->dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
813 head++, tail--;
814 tail->bb_index = i;
815 tail->dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
817 gcc_checking_assert (head == tail);
818 BITMAP_FREE (to_remove);
819 qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
820 gcc_assert (defs[0].bb_index == 1);
822 /* Now each DEFS entry contains the number of the basic block to that the
823 dfs number corresponds. Change them to the number of basic block that
824 corresponds to the interval following the dfs number. Also, for the
825 dfs_out numbers, increase the dfs number by one (so that it corresponds
826 to the start of the following interval, not to the end of the current
827 one). We use WORKLIST as a stack. */
828 auto_vec<int> worklist (n_defs + 1);
829 worklist.quick_push (1);
830 top = 1;
831 n_defs = 1;
832 for (i = 1; i < adef; i++)
834 b = defs[i].bb_index;
835 if (b == top)
837 /* This is a closing element. Interval corresponding to the top
838 of the stack after removing it follows. */
839 worklist.pop ();
840 top = worklist[worklist.length () - 1];
841 defs[n_defs].bb_index = top;
842 defs[n_defs].dfs_num = defs[i].dfs_num + 1;
844 else
846 /* Opening element. Nothing to do, just push it to the stack and move
847 it to the correct position. */
848 defs[n_defs].bb_index = defs[i].bb_index;
849 defs[n_defs].dfs_num = defs[i].dfs_num;
850 worklist.quick_push (b);
851 top = b;
854 /* If this interval starts at the same point as the previous one, cancel
855 the previous one. */
856 if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
857 defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
858 else
859 n_defs++;
861 worklist.pop ();
862 gcc_assert (worklist.is_empty ());
864 /* Now process the uses. */
865 live_phis = BITMAP_ALLOC (NULL);
866 EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
868 worklist.safe_push (i);
871 while (!worklist.is_empty ())
873 b = worklist.pop ();
874 if (b == ENTRY_BLOCK)
875 continue;
877 /* If there is a phi node in USE_BB, it is made live. Otherwise,
878 find the def that dominates the immediate dominator of USE_BB
879 (the kill in USE_BB does not dominate the use). */
880 if (bitmap_bit_p (phis, b))
881 p = b;
882 else
884 use_bb = get_immediate_dominator (CDI_DOMINATORS,
885 BASIC_BLOCK_FOR_FN (cfun, b));
886 p = find_dfsnum_interval (defs, n_defs,
887 bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
888 if (!bitmap_bit_p (phis, p))
889 continue;
892 /* If the phi node is already live, there is nothing to do. */
893 if (!bitmap_set_bit (live_phis, p))
894 continue;
896 /* Add the new uses to the worklist. */
897 def_bb = BASIC_BLOCK_FOR_FN (cfun, p);
898 FOR_EACH_EDGE (e, ei, def_bb->preds)
900 u = e->src->index;
901 if (bitmap_bit_p (uses, u))
902 continue;
904 /* In case there is a kill directly in the use block, do not record
905 the use (this is also necessary for correctness, as we assume that
906 uses dominated by a def directly in their block have been filtered
907 out before). */
908 if (bitmap_bit_p (kills, u))
909 continue;
911 bitmap_set_bit (uses, u);
912 worklist.safe_push (u);
916 bitmap_copy (phis, live_phis);
917 BITMAP_FREE (live_phis);
918 free (defs);
921 /* Return the set of blocks where variable VAR is defined and the blocks
922 where VAR is live on entry (livein). Return NULL, if no entry is
923 found in DEF_BLOCKS. */
925 static inline def_blocks *
926 find_def_blocks_for (tree var)
928 def_blocks *p = &get_common_info (var)->def_blocks;
929 if (!p->def_blocks)
930 return NULL;
931 return p;
935 /* Marks phi node PHI in basic block BB for rewrite. */
937 static void
938 mark_phi_for_rewrite (basic_block bb, gphi *phi)
940 if (rewrite_uses_p (phi))
941 return;
943 set_rewrite_uses (phi, true);
945 if (!blocks_with_phis_to_rewrite)
946 return;
948 bitmap_set_bit (blocks_with_phis_to_rewrite, bb->index);
951 /* Insert PHI nodes for variable VAR using the iterated dominance
952 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
953 function assumes that the caller is incrementally updating the
954 existing SSA form, in which case VAR may be an SSA name instead of
955 a symbol.
957 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
958 PHI node for VAR. On exit, only the nodes that received a PHI node
959 for VAR will be present in PHI_INSERTION_POINTS. */
961 static void
962 insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
964 unsigned bb_index;
965 edge e;
966 gphi *phi;
967 basic_block bb;
968 bitmap_iterator bi;
969 def_blocks *def_map = find_def_blocks_for (var);
971 /* Remove the blocks where we already have PHI nodes for VAR. */
972 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
974 /* Remove obviously useless phi nodes. */
975 prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
976 def_map->livein_blocks);
978 /* And insert the PHI nodes. */
979 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
981 bb = BASIC_BLOCK_FOR_FN (cfun, bb_index);
982 if (update_p)
983 mark_block_for_update (bb);
985 if (dump_file && (dump_flags & TDF_DETAILS))
987 fprintf (dump_file, "creating PHI node in block #%d for ", bb_index);
988 print_generic_expr (dump_file, var, TDF_SLIM);
989 fprintf (dump_file, "\n");
991 phi = NULL;
993 if (TREE_CODE (var) == SSA_NAME)
995 /* If we are rewriting SSA names, create the LHS of the PHI
996 node by duplicating VAR. This is useful in the case of
997 pointers, to also duplicate pointer attributes (alias
998 information, in particular). */
999 edge_iterator ei;
1000 tree new_lhs;
1002 gcc_checking_assert (update_p);
1003 new_lhs = duplicate_ssa_name (var, NULL);
1004 phi = create_phi_node (new_lhs, bb);
1005 add_new_name_mapping (new_lhs, var);
1007 /* Add VAR to every argument slot of PHI. We need VAR in
1008 every argument so that rewrite_update_phi_arguments knows
1009 which name is this PHI node replacing. If VAR is a
1010 symbol marked for renaming, this is not necessary, the
1011 renamer will use the symbol on the LHS to get its
1012 reaching definition. */
1013 FOR_EACH_EDGE (e, ei, bb->preds)
1014 add_phi_arg (phi, var, e, UNKNOWN_LOCATION);
1016 else
1018 tree tracked_var;
1020 gcc_checking_assert (DECL_P (var));
1021 phi = create_phi_node (var, bb);
1023 tracked_var = target_for_debug_bind (var);
1024 if (tracked_var)
1026 gimple *note = gimple_build_debug_bind (tracked_var,
1027 PHI_RESULT (phi),
1028 phi);
1029 gimple_stmt_iterator si = gsi_after_labels (bb);
1030 gsi_insert_before (&si, note, GSI_SAME_STMT);
1034 /* Mark this PHI node as interesting for update_ssa. */
1035 set_register_defs (phi, true);
1036 mark_phi_for_rewrite (bb, phi);
1040 /* Sort var_infos after DECL_UID of their var. */
1042 static int
1043 insert_phi_nodes_compare_var_infos (const void *a, const void *b)
1045 const var_info *defa = *(var_info * const *)a;
1046 const var_info *defb = *(var_info * const *)b;
1047 if (DECL_UID (defa->var) < DECL_UID (defb->var))
1048 return -1;
1049 else
1050 return 1;
1053 /* Insert PHI nodes at the dominance frontier of blocks with variable
1054 definitions. DFS contains the dominance frontier information for
1055 the flowgraph. */
1057 static void
1058 insert_phi_nodes (bitmap_head *dfs)
1060 hash_table<var_info_hasher>::iterator hi;
1061 unsigned i;
1062 var_info *info;
1064 /* When the gimplifier introduces SSA names it cannot easily avoid
1065 situations where abnormal edges added by CFG construction break
1066 the use-def dominance requirement. For this case rewrite SSA
1067 names with broken use-def dominance out-of-SSA and register them
1068 for PHI insertion. We only need to do this if abnormal edges
1069 can appear in the function. */
1070 tree name;
1071 if (cfun->calls_setjmp
1072 || cfun->has_nonlocal_label)
1073 FOR_EACH_SSA_NAME (i, name, cfun)
1075 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1076 if (SSA_NAME_IS_DEFAULT_DEF (name))
1077 continue;
1079 basic_block def_bb = gimple_bb (def_stmt);
1080 imm_use_iterator it;
1081 gimple *use_stmt;
1082 bool need_phis = false;
1083 FOR_EACH_IMM_USE_STMT (use_stmt, it, name)
1085 basic_block use_bb = gimple_bb (use_stmt);
1086 if (use_bb != def_bb
1087 && ! dominated_by_p (CDI_DOMINATORS, use_bb, def_bb))
1088 need_phis = true;
1090 if (need_phis)
1092 tree var = create_tmp_reg (TREE_TYPE (name));
1093 use_operand_p use_p;
1094 FOR_EACH_IMM_USE_STMT (use_stmt, it, name)
1096 basic_block use_bb = gimple_bb (use_stmt);
1097 FOR_EACH_IMM_USE_ON_STMT (use_p, it)
1098 SET_USE (use_p, var);
1099 update_stmt (use_stmt);
1100 set_livein_block (var, use_bb);
1101 set_rewrite_uses (use_stmt, true);
1102 bitmap_set_bit (interesting_blocks, use_bb->index);
1104 def_operand_p def_p;
1105 ssa_op_iter dit;
1106 FOR_EACH_SSA_DEF_OPERAND (def_p, def_stmt, dit, SSA_OP_DEF)
1107 if (DEF_FROM_PTR (def_p) == name)
1108 SET_DEF (def_p, var);
1109 update_stmt (def_stmt);
1110 set_def_block (var, def_bb, false);
1111 set_register_defs (def_stmt, true);
1112 bitmap_set_bit (interesting_blocks, def_bb->index);
1113 release_ssa_name (name);
1117 auto_vec<var_info *> vars (var_infos->elements ());
1118 FOR_EACH_HASH_TABLE_ELEMENT (*var_infos, info, var_info_p, hi)
1119 if (info->info.need_phi_state != NEED_PHI_STATE_NO)
1120 vars.quick_push (info);
1122 /* Do two stages to avoid code generation differences for UID
1123 differences but no UID ordering differences. */
1124 vars.qsort (insert_phi_nodes_compare_var_infos);
1126 FOR_EACH_VEC_ELT (vars, i, info)
1128 bitmap idf = compute_idf (info->info.def_blocks.def_blocks, dfs);
1129 insert_phi_nodes_for (info->var, idf, false);
1130 BITMAP_FREE (idf);
1135 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1136 register DEF (an SSA_NAME) to be a new definition for SYM. */
1138 static void
1139 register_new_def (tree def, tree sym)
1141 common_info *info = get_common_info (sym);
1142 tree currdef;
1144 /* If this variable is set in a single basic block and all uses are
1145 dominated by the set(s) in that single basic block, then there is
1146 no reason to record anything for this variable in the block local
1147 definition stacks. Doing so just wastes time and memory.
1149 This is the same test to prune the set of variables which may
1150 need PHI nodes. So we just use that information since it's already
1151 computed and available for us to use. */
1152 if (info->need_phi_state == NEED_PHI_STATE_NO)
1154 info->current_def = def;
1155 return;
1158 currdef = info->current_def;
1160 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1161 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1162 in the stack so that we know which symbol is being defined by
1163 this SSA name when we unwind the stack. */
1164 if (currdef && !is_gimple_reg (sym))
1165 block_defs_stack.safe_push (sym);
1167 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1168 stack is later used by the dominator tree callbacks to restore
1169 the reaching definitions for all the variables defined in the
1170 block after a recursive visit to all its immediately dominated
1171 blocks. If there is no current reaching definition, then just
1172 record the underlying _DECL node. */
1173 block_defs_stack.safe_push (currdef ? currdef : sym);
1175 /* Set the current reaching definition for SYM to be DEF. */
1176 info->current_def = def;
1180 /* Perform a depth-first traversal of the dominator tree looking for
1181 variables to rename. BB is the block where to start searching.
1182 Renaming is a five step process:
1184 1- Every definition made by PHI nodes at the start of the blocks is
1185 registered as the current definition for the corresponding variable.
1187 2- Every statement in BB is rewritten. USE and VUSE operands are
1188 rewritten with their corresponding reaching definition. DEF and
1189 VDEF targets are registered as new definitions.
1191 3- All the PHI nodes in successor blocks of BB are visited. The
1192 argument corresponding to BB is replaced with its current reaching
1193 definition.
1195 4- Recursively rewrite every dominator child block of BB.
1197 5- Restore (in reverse order) the current reaching definition for every
1198 new definition introduced in this block. This is done so that when
1199 we return from the recursive call, all the current reaching
1200 definitions are restored to the names that were valid in the
1201 dominator parent of BB. */
1203 /* Return the current definition for variable VAR. If none is found,
1204 create a new SSA name to act as the zeroth definition for VAR. */
1206 static tree
1207 get_reaching_def (tree var)
1209 common_info *info = get_common_info (var);
1210 tree currdef;
1212 /* Lookup the current reaching definition for VAR. */
1213 currdef = info->current_def;
1215 /* If there is no reaching definition for VAR, create and register a
1216 default definition for it (if needed). */
1217 if (currdef == NULL_TREE)
1219 tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1220 if (! sym)
1221 sym = create_tmp_reg (TREE_TYPE (var));
1222 currdef = get_or_create_ssa_default_def (cfun, sym);
1225 /* Return the current reaching definition for VAR, or the default
1226 definition, if we had to create one. */
1227 return currdef;
1231 /* Helper function for rewrite_stmt. Rewrite uses in a debug stmt. */
1233 static void
1234 rewrite_debug_stmt_uses (gimple *stmt)
1236 use_operand_p use_p;
1237 ssa_op_iter iter;
1238 bool update = false;
1240 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1242 tree var = USE_FROM_PTR (use_p), def;
1243 common_info *info = get_common_info (var);
1244 gcc_checking_assert (DECL_P (var));
1245 def = info->current_def;
1246 if (!def)
1248 if (TREE_CODE (var) == PARM_DECL
1249 && single_succ_p (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
1251 gimple_stmt_iterator gsi
1253 gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1254 int lim;
1255 /* Search a few source bind stmts at the start of first bb to
1256 see if a DEBUG_EXPR_DECL can't be reused. */
1257 for (lim = 32;
1258 !gsi_end_p (gsi) && lim > 0;
1259 gsi_next (&gsi), lim--)
1261 gimple *gstmt = gsi_stmt (gsi);
1262 if (!gimple_debug_source_bind_p (gstmt))
1263 break;
1264 if (gimple_debug_source_bind_get_value (gstmt) == var)
1266 def = gimple_debug_source_bind_get_var (gstmt);
1267 if (TREE_CODE (def) == DEBUG_EXPR_DECL)
1268 break;
1269 else
1270 def = NULL_TREE;
1273 /* If not, add a new source bind stmt. */
1274 if (def == NULL_TREE)
1276 gimple *def_temp;
1277 def = build_debug_expr_decl (TREE_TYPE (var));
1278 /* FIXME: Is setting the mode really necessary? */
1279 SET_DECL_MODE (def, DECL_MODE (var));
1280 def_temp = gimple_build_debug_source_bind (def, var, NULL);
1281 gsi =
1282 gsi_after_labels (single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)));
1283 gsi_insert_before (&gsi, def_temp, GSI_SAME_STMT);
1285 update = true;
1288 else
1290 /* Check if info->current_def can be trusted. */
1291 basic_block bb = gimple_bb (stmt);
1292 basic_block def_bb
1293 = SSA_NAME_IS_DEFAULT_DEF (def)
1294 ? NULL : gimple_bb (SSA_NAME_DEF_STMT (def));
1296 /* If definition is in current bb, it is fine. */
1297 if (bb == def_bb)
1299 /* If definition bb doesn't dominate the current bb,
1300 it can't be used. */
1301 else if (def_bb && !dominated_by_p (CDI_DOMINATORS, bb, def_bb))
1302 def = NULL;
1303 /* If there is just one definition and dominates the current
1304 bb, it is fine. */
1305 else if (info->need_phi_state == NEED_PHI_STATE_NO)
1307 else
1309 def_blocks *db_p = get_def_blocks_for (info);
1311 /* If there are some non-debug uses in the current bb,
1312 it is fine. */
1313 if (bitmap_bit_p (db_p->livein_blocks, bb->index))
1315 /* Otherwise give up for now. */
1316 else
1317 def = NULL;
1320 if (def == NULL)
1322 gimple_debug_bind_reset_value (stmt);
1323 update_stmt (stmt);
1324 return;
1326 SET_USE (use_p, def);
1328 if (update)
1329 update_stmt (stmt);
1332 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1333 the block with its immediate reaching definitions. Update the current
1334 definition of a variable when a new real or virtual definition is found. */
1336 static void
1337 rewrite_stmt (gimple_stmt_iterator *si)
1339 use_operand_p use_p;
1340 def_operand_p def_p;
1341 ssa_op_iter iter;
1342 gimple *stmt = gsi_stmt (*si);
1344 /* If mark_def_sites decided that we don't need to rewrite this
1345 statement, ignore it. */
1346 gcc_assert (blocks_to_update == NULL);
1347 if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1348 return;
1350 if (dump_file && (dump_flags & TDF_DETAILS))
1352 fprintf (dump_file, "Renaming statement ");
1353 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1354 fprintf (dump_file, "\n");
1357 /* Step 1. Rewrite USES in the statement. */
1358 if (rewrite_uses_p (stmt))
1360 if (is_gimple_debug (stmt))
1361 rewrite_debug_stmt_uses (stmt);
1362 else
1363 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1365 tree var = USE_FROM_PTR (use_p);
1366 if (TREE_CODE (var) == SSA_NAME)
1367 continue;
1368 gcc_checking_assert (DECL_P (var));
1369 SET_USE (use_p, get_reaching_def (var));
1373 /* Step 2. Register the statement's DEF operands. */
1374 if (register_defs_p (stmt))
1375 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1377 tree var = DEF_FROM_PTR (def_p);
1378 tree name;
1379 tree tracked_var;
1381 if (TREE_CODE (var) == SSA_NAME)
1382 continue;
1383 gcc_checking_assert (DECL_P (var));
1385 if (gimple_clobber_p (stmt)
1386 && is_gimple_reg (var))
1388 /* If we rewrite a DECL into SSA form then drop its
1389 clobber stmts and replace uses with a new default def. */
1390 gcc_checking_assert (VAR_P (var) && !gimple_vdef (stmt));
1391 gsi_replace (si, gimple_build_nop (), true);
1392 register_new_def (get_or_create_ssa_default_def (cfun, var), var);
1393 break;
1396 name = make_ssa_name (var, stmt);
1397 SET_DEF (def_p, name);
1398 register_new_def (DEF_FROM_PTR (def_p), var);
1400 /* Do not insert debug stmts if the stmt ends the BB. */
1401 if (stmt_ends_bb_p (stmt))
1402 continue;
1404 tracked_var = target_for_debug_bind (var);
1405 if (tracked_var)
1407 gimple *note = gimple_build_debug_bind (tracked_var, name, stmt);
1408 gsi_insert_after (si, note, GSI_SAME_STMT);
1414 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1415 PHI nodes. For every PHI node found, add a new argument containing the
1416 current reaching definition for the variable and the edge through which
1417 that definition is reaching the PHI node. */
1419 static void
1420 rewrite_add_phi_arguments (basic_block bb)
1422 edge e;
1423 edge_iterator ei;
1425 FOR_EACH_EDGE (e, ei, bb->succs)
1427 gphi *phi;
1428 gphi_iterator gsi;
1430 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
1431 gsi_next (&gsi))
1433 tree currdef, res;
1434 location_t loc;
1436 phi = gsi.phi ();
1437 res = gimple_phi_result (phi);
1438 currdef = get_reaching_def (SSA_NAME_VAR (res));
1439 /* Virtual operand PHI args do not need a location. */
1440 if (virtual_operand_p (res))
1441 loc = UNKNOWN_LOCATION;
1442 else
1443 loc = gimple_location (SSA_NAME_DEF_STMT (currdef));
1444 add_phi_arg (phi, currdef, e, loc);
1449 class rewrite_dom_walker : public dom_walker
1451 public:
1452 rewrite_dom_walker (cdi_direction direction)
1453 : dom_walker (direction, ALL_BLOCKS, NULL) {}
1455 edge before_dom_children (basic_block) final override;
1456 void after_dom_children (basic_block) final override;
1459 /* SSA Rewriting Step 1. Initialization, create a block local stack
1460 of reaching definitions for new SSA names produced in this block
1461 (BLOCK_DEFS). Register new definitions for every PHI node in the
1462 block. */
1464 edge
1465 rewrite_dom_walker::before_dom_children (basic_block bb)
1467 if (dump_file && (dump_flags & TDF_DETAILS))
1468 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1470 /* Mark the unwind point for this block. */
1471 block_defs_stack.safe_push (NULL_TREE);
1473 /* Step 1. Register new definitions for every PHI node in the block.
1474 Conceptually, all the PHI nodes are executed in parallel and each PHI
1475 node introduces a new version for the associated variable. */
1476 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
1477 gsi_next (&gsi))
1479 tree result = gimple_phi_result (gsi_stmt (gsi));
1480 register_new_def (result, SSA_NAME_VAR (result));
1483 /* Step 2. Rewrite every variable used in each statement in the block
1484 with its immediate reaching definitions. Update the current definition
1485 of a variable when a new real or virtual definition is found. */
1486 if (bitmap_bit_p (interesting_blocks, bb->index))
1487 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi);
1488 gsi_next (&gsi))
1489 rewrite_stmt (&gsi);
1491 /* Step 3. Visit all the successor blocks of BB looking for PHI nodes.
1492 For every PHI node found, add a new argument containing the current
1493 reaching definition for the variable and the edge through which that
1494 definition is reaching the PHI node. */
1495 rewrite_add_phi_arguments (bb);
1497 return NULL;
1502 /* Called after visiting all the statements in basic block BB and all
1503 of its dominator children. Restore CURRDEFS to its original value. */
1505 void
1506 rewrite_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
1508 /* Restore CURRDEFS to its original state. */
1509 while (block_defs_stack.length () > 0)
1511 tree tmp = block_defs_stack.pop ();
1512 tree saved_def, var;
1514 if (tmp == NULL_TREE)
1515 break;
1517 if (TREE_CODE (tmp) == SSA_NAME)
1519 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1520 current definition of its underlying variable. Note that
1521 if the SSA_NAME is not for a GIMPLE register, the symbol
1522 being defined is stored in the next slot in the stack.
1523 This mechanism is needed because an SSA name for a
1524 non-register symbol may be the definition for more than
1525 one symbol (e.g., SFTs, aliased variables, etc). */
1526 saved_def = tmp;
1527 var = SSA_NAME_VAR (saved_def);
1528 if (!is_gimple_reg (var))
1529 var = block_defs_stack.pop ();
1531 else
1533 /* If we recorded anything else, it must have been a _DECL
1534 node and its current reaching definition must have been
1535 NULL. */
1536 saved_def = NULL;
1537 var = tmp;
1540 get_common_info (var)->current_def = saved_def;
1545 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1547 DEBUG_FUNCTION void
1548 debug_decl_set (bitmap set)
1550 dump_decl_set (stderr, set);
1551 fprintf (stderr, "\n");
1555 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1556 stack up to a maximum of N levels. If N is -1, the whole stack is
1557 dumped. New levels are created when the dominator tree traversal
1558 used for renaming enters a new sub-tree. */
1560 void
1561 dump_defs_stack (FILE *file, int n)
1563 int i, j;
1565 fprintf (file, "\n\nRenaming stack");
1566 if (n > 0)
1567 fprintf (file, " (up to %d levels)", n);
1568 fprintf (file, "\n\n");
1570 i = 1;
1571 fprintf (file, "Level %d (current level)\n", i);
1572 for (j = (int) block_defs_stack.length () - 1; j >= 0; j--)
1574 tree name, var;
1576 name = block_defs_stack[j];
1577 if (name == NULL_TREE)
1579 i++;
1580 if (n > 0 && i > n)
1581 break;
1582 fprintf (file, "\nLevel %d\n", i);
1583 continue;
1586 if (DECL_P (name))
1588 var = name;
1589 name = NULL_TREE;
1591 else
1593 var = SSA_NAME_VAR (name);
1594 if (!is_gimple_reg (var))
1596 j--;
1597 var = block_defs_stack[j];
1601 fprintf (file, " Previous CURRDEF (");
1602 print_generic_expr (file, var);
1603 fprintf (file, ") = ");
1604 if (name)
1605 print_generic_expr (file, name);
1606 else
1607 fprintf (file, "<NIL>");
1608 fprintf (file, "\n");
1613 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1614 stack up to a maximum of N levels. If N is -1, the whole stack is
1615 dumped. New levels are created when the dominator tree traversal
1616 used for renaming enters a new sub-tree. */
1618 DEBUG_FUNCTION void
1619 debug_defs_stack (int n)
1621 dump_defs_stack (stderr, n);
1625 /* Dump the current reaching definition of every symbol to FILE. */
1627 void
1628 dump_currdefs (FILE *file)
1630 if (symbols_to_rename.is_empty ())
1631 return;
1633 fprintf (file, "\n\nCurrent reaching definitions\n\n");
1634 for (tree var : symbols_to_rename)
1636 common_info *info = get_common_info (var);
1637 fprintf (file, "CURRDEF (");
1638 print_generic_expr (file, var);
1639 fprintf (file, ") = ");
1640 if (info->current_def)
1641 print_generic_expr (file, info->current_def);
1642 else
1643 fprintf (file, "<NIL>");
1644 fprintf (file, "\n");
1649 /* Dump the current reaching definition of every symbol to stderr. */
1651 DEBUG_FUNCTION void
1652 debug_currdefs (void)
1654 dump_currdefs (stderr);
1658 /* Dump SSA information to FILE. */
1660 void
1661 dump_tree_ssa (FILE *file)
1663 const char *funcname
1664 = lang_hooks.decl_printable_name (current_function_decl, 2);
1666 fprintf (file, "SSA renaming information for %s\n\n", funcname);
1668 dump_var_infos (file);
1669 dump_defs_stack (file, -1);
1670 dump_currdefs (file);
1671 dump_tree_ssa_stats (file);
1675 /* Dump SSA information to stderr. */
1677 DEBUG_FUNCTION void
1678 debug_tree_ssa (void)
1680 dump_tree_ssa (stderr);
1684 /* Dump statistics for the hash table HTAB. */
1686 static void
1687 htab_statistics (FILE *file, const hash_table<var_info_hasher> &htab)
1689 fprintf (file, "size " HOST_SIZE_T_PRINT_DEC ", " HOST_SIZE_T_PRINT_DEC
1690 " elements, %f collision/search ratio\n",
1691 (fmt_size_t) htab.size (),
1692 (fmt_size_t) htab.elements (),
1693 htab.collisions ());
1697 /* Dump SSA statistics on FILE. */
1699 void
1700 dump_tree_ssa_stats (FILE *file)
1702 if (var_infos)
1704 fprintf (file, "\nHash table statistics:\n");
1705 fprintf (file, " var_infos: ");
1706 htab_statistics (file, *var_infos);
1707 fprintf (file, "\n");
1712 /* Dump SSA statistics on stderr. */
1714 DEBUG_FUNCTION void
1715 debug_tree_ssa_stats (void)
1717 dump_tree_ssa_stats (stderr);
1721 /* Callback for htab_traverse to dump the VAR_INFOS hash table. */
1724 debug_var_infos_r (var_info **slot, FILE *file)
1726 var_info *info = *slot;
1728 fprintf (file, "VAR: ");
1729 print_generic_expr (file, info->var, dump_flags);
1730 bitmap_print (file, info->info.def_blocks.def_blocks,
1731 ", DEF_BLOCKS: { ", "}");
1732 bitmap_print (file, info->info.def_blocks.livein_blocks,
1733 ", LIVEIN_BLOCKS: { ", "}");
1734 bitmap_print (file, info->info.def_blocks.phi_blocks,
1735 ", PHI_BLOCKS: { ", "}\n");
1737 return 1;
1741 /* Dump the VAR_INFOS hash table on FILE. */
1743 void
1744 dump_var_infos (FILE *file)
1746 fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
1747 if (var_infos)
1748 var_infos->traverse <FILE *, debug_var_infos_r> (file);
1752 /* Dump the VAR_INFOS hash table on stderr. */
1754 DEBUG_FUNCTION void
1755 debug_var_infos (void)
1757 dump_var_infos (stderr);
1761 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1763 static inline void
1764 register_new_update_single (tree new_name, tree old_name)
1766 common_info *info = get_common_info (old_name);
1767 tree currdef = info->current_def;
1769 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1770 This stack is later used by the dominator tree callbacks to
1771 restore the reaching definitions for all the variables
1772 defined in the block after a recursive visit to all its
1773 immediately dominated blocks. */
1774 block_defs_stack.reserve (2);
1775 block_defs_stack.quick_push (currdef);
1776 block_defs_stack.quick_push (old_name);
1778 /* Set the current reaching definition for OLD_NAME to be
1779 NEW_NAME. */
1780 info->current_def = new_name;
1784 /* Register NEW_NAME to be the new reaching definition for all the
1785 names in OLD_NAMES. Used by the incremental SSA update routines to
1786 replace old SSA names with new ones. */
1788 static inline void
1789 register_new_update_set (tree new_name, bitmap old_names)
1791 bitmap_iterator bi;
1792 unsigned i;
1794 EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1795 register_new_update_single (new_name, ssa_name (i));
1800 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1801 it is a symbol marked for renaming, replace it with USE_P's current
1802 reaching definition. */
1804 static inline void
1805 maybe_replace_use (use_operand_p use_p)
1807 tree rdef = NULL_TREE;
1808 tree use = USE_FROM_PTR (use_p);
1809 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1811 if (marked_for_renaming (sym))
1812 rdef = get_reaching_def (sym);
1813 else if (is_old_name (use))
1814 rdef = get_reaching_def (use);
1816 if (rdef && rdef != use)
1817 SET_USE (use_p, rdef);
1821 /* Same as maybe_replace_use, but without introducing default stmts,
1822 returning false to indicate a need to do so. */
1824 static inline bool
1825 maybe_replace_use_in_debug_stmt (use_operand_p use_p)
1827 tree rdef = NULL_TREE;
1828 tree use = USE_FROM_PTR (use_p);
1829 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1831 if (marked_for_renaming (sym))
1832 rdef = get_var_info (sym)->info.current_def;
1833 else if (is_old_name (use))
1835 rdef = get_ssa_name_ann (use)->info.current_def;
1836 /* We can't assume that, if there's no current definition, the
1837 default one should be used. It could be the case that we've
1838 rearranged blocks so that the earlier definition no longer
1839 dominates the use. */
1840 if (!rdef && SSA_NAME_IS_DEFAULT_DEF (use))
1841 rdef = use;
1843 else
1844 rdef = use;
1846 if (rdef && rdef != use)
1847 SET_USE (use_p, rdef);
1849 return rdef != NULL_TREE;
1853 /* If DEF has x_5 = ASAN_POISON () as its current def, add
1854 ASAN_POISON_USE (x_5) stmt before GSI to denote the stmt writes into
1855 a poisoned (out of scope) variable. */
1857 static void
1858 maybe_add_asan_poison_write (tree def, gimple_stmt_iterator *gsi)
1860 tree cdef = get_current_def (def);
1861 if (cdef != NULL
1862 && TREE_CODE (cdef) == SSA_NAME
1863 && gimple_call_internal_p (SSA_NAME_DEF_STMT (cdef), IFN_ASAN_POISON))
1865 gcall *call
1866 = gimple_build_call_internal (IFN_ASAN_POISON_USE, 1, cdef);
1867 gimple_set_location (call, gimple_location (gsi_stmt (*gsi)));
1868 gsi_insert_before (gsi, call, GSI_SAME_STMT);
1873 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1874 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1875 register it as the current definition for the names replaced by
1876 DEF_P. Returns whether the statement should be removed. */
1878 static inline bool
1879 maybe_register_def (def_operand_p def_p, gimple *stmt,
1880 gimple_stmt_iterator gsi)
1882 tree def = DEF_FROM_PTR (def_p);
1883 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
1884 bool to_delete = false;
1886 /* If DEF is a naked symbol that needs renaming, create a new
1887 name for it. */
1888 if (marked_for_renaming (sym))
1890 if (DECL_P (def))
1892 if (gimple_clobber_p (stmt) && is_gimple_reg (sym))
1894 tree defvar;
1895 if (VAR_P (sym))
1896 defvar = sym;
1897 else
1898 defvar = create_tmp_reg (TREE_TYPE (sym));
1899 /* Replace clobber stmts with a default def. This new use of a
1900 default definition may make it look like SSA_NAMEs have
1901 conflicting lifetimes, so we need special code to let them
1902 coalesce properly. */
1903 to_delete = true;
1904 def = get_or_create_ssa_default_def (cfun, defvar);
1906 else
1908 if (asan_sanitize_use_after_scope ())
1909 maybe_add_asan_poison_write (def, &gsi);
1910 def = make_ssa_name (def, stmt);
1912 SET_DEF (def_p, def);
1914 tree tracked_var = target_for_debug_bind (sym);
1915 if (tracked_var)
1917 /* If stmt ends the bb, insert the debug stmt on the non-EH
1918 edge(s) from the stmt. */
1919 if (gsi_one_before_end_p (gsi) && stmt_ends_bb_p (stmt))
1921 basic_block bb = gsi_bb (gsi);
1922 edge_iterator ei;
1923 edge e, ef = NULL;
1924 FOR_EACH_EDGE (e, ei, bb->succs)
1925 if (!(e->flags & EDGE_EH))
1927 /* asm goto can have multiple non-EH edges from the
1928 stmt. Insert on all of them where it is
1929 possible. */
1930 gcc_checking_assert (!ef || (gimple_code (stmt)
1931 == GIMPLE_ASM));
1932 ef = e;
1933 /* If there are other predecessors to ef->dest, then
1934 there must be PHI nodes for the modified
1935 variable, and therefore there will be debug bind
1936 stmts after the PHI nodes. The debug bind notes
1937 we'd insert would force the creation of a new
1938 block (diverging codegen) and be redundant with
1939 the post-PHI bind stmts, so don't add them.
1941 As for the exit edge, there wouldn't be redundant
1942 bind stmts, but there wouldn't be a PC to bind
1943 them to either, so avoid diverging the CFG. */
1944 if (e
1945 && single_pred_p (e->dest)
1946 && gimple_seq_empty_p (phi_nodes (e->dest))
1947 && e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1949 /* If there were PHI nodes in the node, we'd
1950 have to make sure the value we're binding
1951 doesn't need rewriting. But there shouldn't
1952 be PHI nodes in a single-predecessor block,
1953 so we just add the note. */
1954 gimple *note
1955 = gimple_build_debug_bind (tracked_var, def,
1956 stmt);
1957 gsi_insert_on_edge_immediate (ef, note);
1961 else
1963 gimple *note
1964 = gimple_build_debug_bind (tracked_var, def, stmt);
1965 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
1970 register_new_update_single (def, sym);
1972 else
1974 /* If DEF is a new name, register it as a new definition
1975 for all the names replaced by DEF. */
1976 if (is_new_name (def))
1977 register_new_update_set (def, names_replaced_by (def));
1979 /* If DEF is an old name, register DEF as a new
1980 definition for itself. */
1981 if (is_old_name (def))
1982 register_new_update_single (def, def);
1985 return to_delete;
1989 /* Update every variable used in the statement pointed-to by SI. The
1990 statement is assumed to be in SSA form already. Names in
1991 OLD_SSA_NAMES used by SI will be updated to their current reaching
1992 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1993 will be registered as a new definition for their corresponding name
1994 in OLD_SSA_NAMES. Returns whether STMT should be removed. */
1996 static bool
1997 rewrite_update_stmt (gimple *stmt, gimple_stmt_iterator gsi)
1999 use_operand_p use_p;
2000 def_operand_p def_p;
2001 ssa_op_iter iter;
2003 /* Only update marked statements. */
2004 if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
2005 return false;
2007 if (dump_file && (dump_flags & TDF_DETAILS))
2009 fprintf (dump_file, "Updating SSA information for statement ");
2010 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
2013 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
2014 symbol is marked for renaming. */
2015 if (rewrite_uses_p (stmt))
2017 if (is_gimple_debug (stmt))
2019 bool failed = false;
2021 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
2022 if (!maybe_replace_use_in_debug_stmt (use_p))
2024 failed = true;
2025 break;
2028 if (failed)
2030 /* DOM sometimes threads jumps in such a way that a
2031 debug stmt ends up referencing a SSA variable that no
2032 longer dominates the debug stmt, but such that all
2033 incoming definitions refer to the same definition in
2034 an earlier dominator. We could try to recover that
2035 definition somehow, but this will have to do for now.
2037 Introducing a default definition, which is what
2038 maybe_replace_use() would do in such cases, may
2039 modify code generation, for the otherwise-unused
2040 default definition would never go away, modifying SSA
2041 version numbers all over. */
2042 gimple_debug_bind_reset_value (stmt);
2043 update_stmt (stmt);
2046 else
2048 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
2049 maybe_replace_use (use_p);
2053 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
2054 Also register definitions for names whose underlying symbol is
2055 marked for renaming. */
2056 bool to_delete = false;
2057 if (register_defs_p (stmt))
2058 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
2059 to_delete |= maybe_register_def (def_p, stmt, gsi);
2061 return to_delete;
2065 /* Visit all the successor blocks of BB looking for PHI nodes. For
2066 every PHI node found, check if any of its arguments is in
2067 OLD_SSA_NAMES. If so, and if the argument has a current reaching
2068 definition, replace it. */
2070 static void
2071 rewrite_update_phi_arguments (basic_block bb)
2073 edge e;
2074 edge_iterator ei;
2076 FOR_EACH_EDGE (e, ei, bb->succs)
2078 if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
2079 continue;
2081 for (auto gsi = gsi_start_phis (e->dest);
2082 !gsi_end_p (gsi); gsi_next(&gsi))
2084 tree arg, lhs_sym, reaching_def = NULL;
2085 use_operand_p arg_p;
2086 gphi *phi = *gsi;
2087 if (!rewrite_uses_p (*gsi))
2088 continue;
2090 arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
2091 arg = USE_FROM_PTR (arg_p);
2093 if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
2094 continue;
2096 lhs_sym = SSA_NAME_VAR (gimple_phi_result (phi));
2098 if (arg == NULL_TREE)
2100 /* When updating a PHI node for a recently introduced
2101 symbol we may find NULL arguments. That's why we
2102 take the symbol from the LHS of the PHI node. */
2103 reaching_def = get_reaching_def (lhs_sym);
2105 else
2107 tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
2109 if (marked_for_renaming (sym))
2110 reaching_def = get_reaching_def (sym);
2111 else if (is_old_name (arg))
2112 reaching_def = get_reaching_def (arg);
2115 /* Update the argument if there is a reaching def different
2116 from arg. */
2117 if (reaching_def && reaching_def != arg)
2119 location_t locus;
2120 int arg_i = PHI_ARG_INDEX_FROM_USE (arg_p);
2122 SET_USE (arg_p, reaching_def);
2124 /* Virtual operands do not need a location. */
2125 if (virtual_operand_p (reaching_def))
2126 locus = UNKNOWN_LOCATION;
2127 /* If SSA update didn't insert this PHI the argument
2128 might have a location already, keep that. */
2129 else if (gimple_phi_arg_has_location (phi, arg_i))
2130 locus = gimple_phi_arg_location (phi, arg_i);
2131 else
2133 gimple *stmt = SSA_NAME_DEF_STMT (reaching_def);
2134 gphi *other_phi = dyn_cast <gphi *> (stmt);
2136 /* Single element PHI nodes behave like copies, so get the
2137 location from the phi argument. */
2138 if (other_phi
2139 && gimple_phi_num_args (other_phi) == 1)
2140 locus = gimple_phi_arg_location (other_phi, 0);
2141 else
2142 locus = gimple_location (stmt);
2145 gimple_phi_arg_set_location (phi, arg_i, locus);
2148 if (e->flags & EDGE_ABNORMAL)
2149 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
2154 class rewrite_update_dom_walker : public dom_walker
2156 public:
2157 rewrite_update_dom_walker (cdi_direction direction, int in_region_flag = -1)
2158 : dom_walker (direction, ALL_BLOCKS, (int *)(uintptr_t)-1),
2159 m_in_region_flag (in_region_flag) {}
2161 edge before_dom_children (basic_block) final override;
2162 void after_dom_children (basic_block) final override;
2164 int m_in_region_flag;
2167 /* Initialization of block data structures for the incremental SSA
2168 update pass. Create a block local stack of reaching definitions
2169 for new SSA names produced in this block (BLOCK_DEFS). Register
2170 new definitions for every PHI node in the block. */
2172 edge
2173 rewrite_update_dom_walker::before_dom_children (basic_block bb)
2175 bool is_abnormal_phi;
2177 if (dump_file && (dump_flags & TDF_DETAILS))
2178 fprintf (dump_file, "Registering new PHI nodes in block #%d\n",
2179 bb->index);
2181 /* Mark the unwind point for this block. */
2182 block_defs_stack.safe_push (NULL_TREE);
2184 if (m_in_region_flag != -1
2185 && !(bb->flags & m_in_region_flag))
2186 return STOP;
2188 if (!bitmap_bit_p (blocks_to_update, bb->index))
2189 return NULL;
2191 /* Mark the LHS if any of the arguments flows through an abnormal
2192 edge. */
2193 is_abnormal_phi = bb_has_abnormal_pred (bb);
2195 /* If any of the PHI nodes is a replacement for a name in
2196 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2197 register it as a new definition for its corresponding name. Also
2198 register definitions for names whose underlying symbols are
2199 marked for renaming. */
2200 for (gphi_iterator gsi = gsi_start_phis (bb); !gsi_end_p (gsi);
2201 gsi_next (&gsi))
2203 tree lhs, lhs_sym;
2204 gphi *phi = gsi.phi ();
2206 if (!register_defs_p (phi))
2207 continue;
2209 lhs = gimple_phi_result (phi);
2210 lhs_sym = SSA_NAME_VAR (lhs);
2212 if (marked_for_renaming (lhs_sym))
2213 register_new_update_single (lhs, lhs_sym);
2214 else
2217 /* If LHS is a new name, register a new definition for all
2218 the names replaced by LHS. */
2219 if (is_new_name (lhs))
2220 register_new_update_set (lhs, names_replaced_by (lhs));
2222 /* If LHS is an OLD name, register it as a new definition
2223 for itself. */
2224 if (is_old_name (lhs))
2225 register_new_update_single (lhs, lhs);
2228 if (is_abnormal_phi)
2229 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
2232 /* Step 2. Rewrite every variable used in each statement in the block. */
2233 for (gimple_stmt_iterator gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
2234 if (rewrite_update_stmt (gsi_stmt (gsi), gsi))
2235 gsi_remove (&gsi, true);
2236 else
2237 gsi_next (&gsi);
2239 /* Step 3. Update PHI nodes. */
2240 rewrite_update_phi_arguments (bb);
2242 return NULL;
2245 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
2246 the current reaching definition of every name re-written in BB to
2247 the original reaching definition before visiting BB. This
2248 unwinding must be done in the opposite order to what is done in
2249 register_new_update_set. */
2251 void
2252 rewrite_update_dom_walker::after_dom_children (basic_block bb ATTRIBUTE_UNUSED)
2254 while (block_defs_stack.length () > 0)
2256 tree var = block_defs_stack.pop ();
2257 tree saved_def;
2259 /* NULL indicates the unwind stop point for this block (see
2260 rewrite_update_enter_block). */
2261 if (var == NULL)
2262 return;
2264 saved_def = block_defs_stack.pop ();
2265 get_common_info (var)->current_def = saved_def;
2270 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2271 form.
2273 ENTRY indicates the block where to start. Every block dominated by
2274 ENTRY will be rewritten.
2276 WHAT indicates what actions will be taken by the renamer (see enum
2277 rewrite_mode).
2279 REGION is a SEME region of interesting blocks for the dominator walker
2280 to process. If this set is invalid, then all the nodes dominated
2281 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2282 are not present in BLOCKS are ignored. */
2284 static void
2285 rewrite_blocks (basic_block entry, enum rewrite_mode what)
2287 block_defs_stack.create (10);
2289 /* Recursively walk the dominator tree rewriting each statement in
2290 each basic block. */
2291 if (what == REWRITE_ALL)
2292 rewrite_dom_walker (CDI_DOMINATORS).walk (entry);
2293 else if (what == REWRITE_UPDATE)
2294 rewrite_update_dom_walker (CDI_DOMINATORS).walk (entry);
2295 else if (what == REWRITE_UPDATE_REGION)
2297 /* First mark all blocks in the SEME region dominated by
2298 entry and exited by blocks not backwards reachable from
2299 blocks_to_update. Optimize for dense blocks_to_update
2300 so instead of seeding the worklist with a copy of
2301 blocks_to_update treat those blocks explicit. */
2302 auto_bb_flag in_region (cfun);
2303 auto_vec<basic_block, 64> extra_rgn;
2304 bitmap_iterator bi;
2305 unsigned int idx;
2306 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, idx, bi)
2308 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
2309 bb->flags |= in_region;
2311 auto_bitmap worklist;
2312 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, idx, bi)
2314 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
2315 if (bb != entry)
2317 edge_iterator ei;
2318 edge e;
2319 FOR_EACH_EDGE (e, ei, bb->preds)
2321 if ((e->src->flags & in_region)
2322 || dominated_by_p (CDI_DOMINATORS, e->src, bb))
2323 continue;
2324 bitmap_set_bit (worklist, e->src->index);
2328 while (!bitmap_empty_p (worklist))
2330 int idx = bitmap_clear_first_set_bit (worklist);
2331 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
2332 bb->flags |= in_region;
2333 extra_rgn.safe_push (bb);
2334 if (bb != entry)
2336 edge_iterator ei;
2337 edge e;
2338 FOR_EACH_EDGE (e, ei, bb->preds)
2340 if ((e->src->flags & in_region)
2341 || dominated_by_p (CDI_DOMINATORS, e->src, bb))
2342 continue;
2343 bitmap_set_bit (worklist, e->src->index);
2347 rewrite_update_dom_walker (CDI_DOMINATORS, in_region).walk (entry);
2348 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, idx, bi)
2350 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, idx);
2351 bb->flags &= ~in_region;
2353 for (auto bb : extra_rgn)
2354 bb->flags &= ~in_region;
2356 else
2357 gcc_unreachable ();
2359 /* Debugging dumps. */
2360 if (dump_file && (dump_flags & TDF_STATS))
2362 dump_dfa_stats (dump_file);
2363 if (var_infos)
2364 dump_tree_ssa_stats (dump_file);
2367 block_defs_stack.release ();
2370 class mark_def_dom_walker : public dom_walker
2372 public:
2373 mark_def_dom_walker (cdi_direction direction);
2374 ~mark_def_dom_walker ();
2376 edge before_dom_children (basic_block) final override;
2378 private:
2379 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2380 large enough to accommodate all the variables referenced in the
2381 function, not just the ones we are renaming. */
2382 bitmap m_kills;
2385 mark_def_dom_walker::mark_def_dom_walker (cdi_direction direction)
2386 : dom_walker (direction, ALL_BLOCKS, NULL), m_kills (BITMAP_ALLOC (NULL))
2390 mark_def_dom_walker::~mark_def_dom_walker ()
2392 BITMAP_FREE (m_kills);
2395 /* Block processing routine for mark_def_sites. Clear the KILLS bitmap
2396 at the start of each block, and call mark_def_sites for each statement. */
2398 edge
2399 mark_def_dom_walker::before_dom_children (basic_block bb)
2401 gimple_stmt_iterator gsi;
2403 bitmap_clear (m_kills);
2404 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2405 mark_def_sites (bb, gsi_stmt (gsi), m_kills);
2406 return NULL;
2409 /* Initialize internal data needed during renaming. */
2411 static void
2412 init_ssa_renamer (void)
2414 cfun->gimple_df->in_ssa_p = false;
2416 /* Allocate memory for the DEF_BLOCKS hash table. */
2417 gcc_assert (!var_infos);
2418 var_infos = new hash_table<var_info_hasher>
2419 (vec_safe_length (cfun->local_decls));
2421 bitmap_obstack_initialize (&update_ssa_obstack);
2425 /* Deallocate internal data structures used by the renamer. */
2427 static void
2428 fini_ssa_renamer (void)
2430 delete var_infos;
2431 var_infos = NULL;
2433 bitmap_obstack_release (&update_ssa_obstack);
2435 cfun->gimple_df->ssa_renaming_needed = 0;
2436 cfun->gimple_df->rename_vops = 0;
2437 cfun->gimple_df->in_ssa_p = true;
2440 /* Main entry point into the SSA builder. The renaming process
2441 proceeds in four main phases:
2443 1- Compute dominance frontier and immediate dominators, needed to
2444 insert PHI nodes and rename the function in dominator tree
2445 order.
2447 2- Find and mark all the blocks that define variables.
2449 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2451 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2453 Steps 3 and 4 are done using the dominator tree walker
2454 (walk_dominator_tree). */
2456 namespace {
2458 const pass_data pass_data_build_ssa =
2460 GIMPLE_PASS, /* type */
2461 "ssa", /* name */
2462 OPTGROUP_NONE, /* optinfo_flags */
2463 TV_TREE_INTO_SSA, /* tv_id */
2464 PROP_cfg, /* properties_required */
2465 PROP_ssa, /* properties_provided */
2466 0, /* properties_destroyed */
2467 0, /* todo_flags_start */
2468 TODO_remove_unused_locals, /* todo_flags_finish */
2471 class pass_build_ssa : public gimple_opt_pass
2473 public:
2474 pass_build_ssa (gcc::context *ctxt)
2475 : gimple_opt_pass (pass_data_build_ssa, ctxt)
2478 /* opt_pass methods: */
2479 bool gate (function *fun) final override
2481 /* Do nothing for functions that were produced already in SSA form. */
2482 return !(fun->curr_properties & PROP_ssa);
2485 unsigned int execute (function *) final override;
2487 }; // class pass_build_ssa
2489 unsigned int
2490 pass_build_ssa::execute (function *fun)
2492 bitmap_head *dfs;
2493 basic_block bb;
2495 /* Increase the set of variables we can rewrite into SSA form
2496 by clearing TREE_ADDRESSABLE and transform the IL to support this. */
2497 if (optimize)
2498 execute_update_addresses_taken ();
2500 /* Initialize operand data structures. */
2501 init_ssa_operands (fun);
2503 /* Initialize internal data needed by the renamer. */
2504 init_ssa_renamer ();
2506 /* Initialize the set of interesting blocks. The callback
2507 mark_def_sites will add to this set those blocks that the renamer
2508 should process. */
2509 interesting_blocks = sbitmap_alloc (last_basic_block_for_fn (fun));
2510 bitmap_clear (interesting_blocks);
2512 /* Initialize dominance frontier. */
2513 dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (fun));
2514 FOR_EACH_BB_FN (bb, fun)
2515 bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
2517 /* 1- Compute dominance frontiers. */
2518 calculate_dominance_info (CDI_DOMINATORS);
2519 compute_dominance_frontiers (dfs);
2521 /* 2- Find and mark definition sites. */
2522 mark_def_dom_walker (CDI_DOMINATORS).walk (fun->cfg->x_entry_block_ptr);
2524 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2525 insert_phi_nodes (dfs);
2527 /* 4- Rename all the blocks. */
2528 rewrite_blocks (ENTRY_BLOCK_PTR_FOR_FN (fun), REWRITE_ALL);
2530 /* Free allocated memory. */
2531 FOR_EACH_BB_FN (bb, fun)
2532 bitmap_clear (&dfs[bb->index]);
2533 free (dfs);
2535 sbitmap_free (interesting_blocks);
2536 interesting_blocks = NULL;
2538 fini_ssa_renamer ();
2540 /* Try to get rid of all gimplifier generated temporaries by making
2541 its SSA names anonymous. This way we can garbage collect them
2542 all after removing unused locals which we do in our TODO. */
2543 unsigned i;
2544 tree name;
2546 FOR_EACH_SSA_NAME (i, name, cfun)
2548 if (SSA_NAME_IS_DEFAULT_DEF (name))
2549 continue;
2550 tree decl = SSA_NAME_VAR (name);
2551 if (decl
2552 && VAR_P (decl)
2553 && !VAR_DECL_IS_VIRTUAL_OPERAND (decl)
2554 && DECL_IGNORED_P (decl))
2555 SET_SSA_NAME_VAR_OR_IDENTIFIER (name, DECL_NAME (decl));
2558 /* Initialize SSA_NAME_POINTS_TO_READONLY_MEMORY. */
2559 tree fnspec_tree
2560 = lookup_attribute ("fn spec",
2561 TYPE_ATTRIBUTES (TREE_TYPE (fun->decl)));
2562 if (fnspec_tree)
2564 attr_fnspec fnspec (TREE_VALUE (TREE_VALUE (fnspec_tree)));
2565 unsigned i = 0;
2566 for (tree arg = DECL_ARGUMENTS (cfun->decl);
2567 arg; arg = DECL_CHAIN (arg), ++i)
2569 if (!fnspec.arg_specified_p (i))
2570 break;
2571 if (fnspec.arg_readonly_p (i))
2573 tree name = ssa_default_def (fun, arg);
2574 if (name)
2575 SSA_NAME_POINTS_TO_READONLY_MEMORY (name) = 1;
2580 return 0;
2583 } // anon namespace
2585 gimple_opt_pass *
2586 make_pass_build_ssa (gcc::context *ctxt)
2588 return new pass_build_ssa (ctxt);
2592 /* Mark the definition of VAR at STMT and BB as interesting for the
2593 renamer. BLOCKS is the set of blocks that need updating. */
2595 static void
2596 mark_def_interesting (tree var, gimple *stmt, basic_block bb,
2597 bool insert_phi_p)
2599 gcc_checking_assert (bitmap_bit_p (blocks_to_update, bb->index));
2600 set_register_defs (stmt, true);
2602 if (insert_phi_p)
2604 bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI;
2606 set_def_block (var, bb, is_phi_p);
2608 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2609 site for both itself and all the old names replaced by it. */
2610 if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2612 bitmap_iterator bi;
2613 unsigned i;
2614 bitmap set = names_replaced_by (var);
2615 if (set)
2616 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2617 set_def_block (ssa_name (i), bb, is_phi_p);
2623 /* Mark the use of VAR at STMT and BB as interesting for the
2624 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2625 nodes. */
2627 static inline void
2628 mark_use_interesting (tree var, gimple *stmt, basic_block bb,
2629 bool insert_phi_p)
2631 basic_block def_bb = gimple_bb (stmt);
2633 mark_block_for_update (def_bb);
2634 mark_block_for_update (bb);
2636 if (gimple_code (stmt) == GIMPLE_PHI)
2637 mark_phi_for_rewrite (def_bb, as_a <gphi *> (stmt));
2638 else
2640 set_rewrite_uses (stmt, true);
2642 if (is_gimple_debug (stmt))
2643 return;
2646 /* If VAR has not been defined in BB, then it is live-on-entry
2647 to BB. Note that we cannot just use the block holding VAR's
2648 definition because if VAR is one of the names in OLD_SSA_NAMES,
2649 it will have several definitions (itself and all the names that
2650 replace it). */
2651 if (insert_phi_p)
2653 def_blocks *db_p = get_def_blocks_for (get_common_info (var));
2654 if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2655 set_livein_block (var, bb);
2659 /* Processing statements in BB that reference symbols in SSA operands.
2660 This is very similar to mark_def_sites, but the scan handles
2661 statements whose operands may already be SSA names.
2663 If INSERT_PHI_P is true, mark those uses as live in the
2664 corresponding block. This is later used by the PHI placement
2665 algorithm to make PHI pruning decisions.
2667 FIXME. Most of this would be unnecessary if we could associate a
2668 symbol to all the SSA names that reference it. But that
2669 sounds like it would be expensive to maintain. Still, it
2670 would be interesting to see if it makes better sense to do
2671 that. */
2673 static void
2674 prepare_block_for_update_1 (basic_block bb, bool insert_phi_p)
2676 edge e;
2677 edge_iterator ei;
2679 mark_block_for_update (bb);
2681 /* Process PHI nodes marking interesting those that define or use
2682 the symbols that we are interested in. */
2683 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
2684 gsi_next (&si))
2686 gphi *phi = si.phi ();
2687 tree lhs_sym, lhs = gimple_phi_result (phi);
2689 if (TREE_CODE (lhs) == SSA_NAME
2690 && (! virtual_operand_p (lhs)
2691 || ! cfun->gimple_df->rename_vops))
2692 continue;
2694 lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
2695 mark_for_renaming (lhs_sym);
2696 mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2698 /* Mark the uses in phi nodes as interesting. It would be more correct
2699 to process the arguments of the phi nodes of the successor edges of
2700 BB at the end of prepare_block_for_update, however, that turns out
2701 to be significantly more expensive. Doing it here is conservatively
2702 correct -- it may only cause us to believe a value to be live in a
2703 block that also contains its definition, and thus insert a few more
2704 phi nodes for it. */
2705 FOR_EACH_EDGE (e, ei, bb->preds)
2706 mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
2709 /* Process the statements. */
2710 for (gimple_stmt_iterator si = gsi_start_bb (bb); !gsi_end_p (si);
2711 gsi_next (&si))
2713 gimple *stmt;
2714 ssa_op_iter i;
2715 use_operand_p use_p;
2716 def_operand_p def_p;
2718 stmt = gsi_stmt (si);
2720 if (cfun->gimple_df->rename_vops
2721 && gimple_vuse (stmt))
2723 tree use = gimple_vuse (stmt);
2724 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2725 mark_for_renaming (sym);
2726 mark_use_interesting (sym, stmt, bb, insert_phi_p);
2729 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_USE)
2731 tree use = USE_FROM_PTR (use_p);
2732 if (!DECL_P (use))
2733 continue;
2734 mark_for_renaming (use);
2735 mark_use_interesting (use, stmt, bb, insert_phi_p);
2738 if (cfun->gimple_df->rename_vops
2739 && gimple_vdef (stmt))
2741 tree def = gimple_vdef (stmt);
2742 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2743 mark_for_renaming (sym);
2744 mark_def_interesting (sym, stmt, bb, insert_phi_p);
2747 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_DEF)
2749 tree def = DEF_FROM_PTR (def_p);
2750 if (!DECL_P (def))
2751 continue;
2752 mark_for_renaming (def);
2753 mark_def_interesting (def, stmt, bb, insert_phi_p);
2759 /* Do a dominator walk starting at BB processing statements that
2760 reference symbols in SSA operands. This is very similar to
2761 mark_def_sites, but the scan handles statements whose operands may
2762 already be SSA names.
2764 If INSERT_PHI_P is true, mark those uses as live in the
2765 corresponding block. This is later used by the PHI placement
2766 algorithm to make PHI pruning decisions.
2768 FIXME. Most of this would be unnecessary if we could associate a
2769 symbol to all the SSA names that reference it. But that
2770 sounds like it would be expensive to maintain. Still, it
2771 would be interesting to see if it makes better sense to do
2772 that. */
2773 static void
2774 prepare_block_for_update (basic_block bb, bool insert_phi_p)
2776 size_t sp = 0;
2777 basic_block *worklist;
2779 /* Allocate the worklist. */
2780 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
2781 /* Add the BB to the worklist. */
2782 worklist[sp++] = bb;
2784 while (sp)
2786 basic_block bb;
2787 basic_block son;
2789 /* Pick a block from the worklist. */
2790 bb = worklist[--sp];
2792 prepare_block_for_update_1 (bb, insert_phi_p);
2794 /* Now add all the blocks dominated by BB to the worklist. */
2795 for (son = first_dom_son (CDI_DOMINATORS, bb);
2796 son;
2797 son = next_dom_son (CDI_DOMINATORS, son))
2798 worklist[sp++] = son;
2800 free (worklist);
2803 /* Helper for prepare_names_to_update. Mark all the use sites for
2804 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2805 prepare_names_to_update. */
2807 static void
2808 prepare_use_sites_for (tree name, bool insert_phi_p)
2810 use_operand_p use_p;
2811 imm_use_iterator iter;
2813 /* If we rename virtual operands do not update them. */
2814 if (virtual_operand_p (name)
2815 && cfun->gimple_df->rename_vops)
2816 return;
2818 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2820 gimple *stmt = USE_STMT (use_p);
2821 basic_block bb = gimple_bb (stmt);
2823 if (gimple_code (stmt) == GIMPLE_PHI)
2825 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
2826 edge e = gimple_phi_arg_edge (as_a <gphi *> (stmt), ix);
2827 mark_use_interesting (name, stmt, e->src, insert_phi_p);
2829 else
2831 /* For regular statements, mark this as an interesting use
2832 for NAME. */
2833 mark_use_interesting (name, stmt, bb, insert_phi_p);
2839 /* Helper for prepare_names_to_update. Mark the definition site for
2840 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2841 prepare_names_to_update. */
2843 static void
2844 prepare_def_site_for (tree name, bool insert_phi_p)
2846 gimple *stmt;
2847 basic_block bb;
2849 gcc_checking_assert (names_to_release == NULL
2850 || !bitmap_bit_p (names_to_release,
2851 SSA_NAME_VERSION (name)));
2853 /* If we rename virtual operands do not update them. */
2854 if (virtual_operand_p (name)
2855 && cfun->gimple_df->rename_vops)
2856 return;
2858 stmt = SSA_NAME_DEF_STMT (name);
2859 bb = gimple_bb (stmt);
2860 if (bb)
2862 gcc_checking_assert (bb->index < last_basic_block_for_fn (cfun));
2863 mark_block_for_update (bb);
2864 mark_def_interesting (name, stmt, bb, insert_phi_p);
2869 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2870 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2871 PHI nodes for newly created names. */
2873 static void
2874 prepare_names_to_update (bool insert_phi_p)
2876 unsigned i = 0;
2877 bitmap_iterator bi;
2878 sbitmap_iterator sbi;
2880 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2881 remove it from NEW_SSA_NAMES so that we don't try to visit its
2882 defining basic block (which most likely doesn't exist). Notice
2883 that we cannot do the same with names in OLD_SSA_NAMES because we
2884 want to replace existing instances. */
2885 if (names_to_release)
2886 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2887 bitmap_clear_bit (new_ssa_names, i);
2889 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2890 names may be considered to be live-in on blocks that contain
2891 definitions for their replacements. */
2892 EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi)
2893 prepare_def_site_for (ssa_name (i), insert_phi_p);
2895 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2896 OLD_SSA_NAMES, but we have to ignore its definition site. */
2897 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
2899 if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2900 prepare_def_site_for (ssa_name (i), insert_phi_p);
2901 prepare_use_sites_for (ssa_name (i), insert_phi_p);
2906 /* Dump all the names replaced by NAME to FILE. */
2908 void
2909 dump_names_replaced_by (FILE *file, tree name)
2911 unsigned i;
2912 bitmap old_set;
2913 bitmap_iterator bi;
2915 print_generic_expr (file, name);
2916 fprintf (file, " -> { ");
2918 old_set = names_replaced_by (name);
2919 EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2921 print_generic_expr (file, ssa_name (i));
2922 fprintf (file, " ");
2925 fprintf (file, "}\n");
2929 /* Dump all the names replaced by NAME to stderr. */
2931 DEBUG_FUNCTION void
2932 debug_names_replaced_by (tree name)
2934 dump_names_replaced_by (stderr, name);
2938 /* Dump SSA update information to FILE. */
2940 void
2941 dump_update_ssa (FILE *file)
2943 unsigned i = 0;
2944 bitmap_iterator bi;
2946 if (!need_ssa_update_p (cfun))
2947 return;
2949 if (new_ssa_names && !bitmap_empty_p (new_ssa_names))
2951 sbitmap_iterator sbi;
2953 fprintf (file, "\nSSA replacement table\n");
2954 fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2955 "O_1, ..., O_j\n\n");
2957 EXECUTE_IF_SET_IN_BITMAP (new_ssa_names, 0, i, sbi)
2958 dump_names_replaced_by (file, ssa_name (i));
2961 if (symbols_to_rename_set && !bitmap_empty_p (symbols_to_rename_set))
2963 fprintf (file, "\nSymbols to be put in SSA form\n");
2964 dump_decl_set (file, symbols_to_rename_set);
2965 fprintf (file, "\n");
2968 if (names_to_release && !bitmap_empty_p (names_to_release))
2970 fprintf (file, "\nSSA names to release after updating the SSA web\n\n");
2971 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2973 print_generic_expr (file, ssa_name (i));
2974 fprintf (file, " ");
2976 fprintf (file, "\n");
2981 /* Dump SSA update information to stderr. */
2983 DEBUG_FUNCTION void
2984 debug_update_ssa (void)
2986 dump_update_ssa (stderr);
2990 /* Initialize data structures used for incremental SSA updates. */
2992 static void
2993 init_update_ssa (struct function *fn)
2995 /* Reserve more space than the current number of names. The calls to
2996 add_new_name_mapping are typically done after creating new SSA
2997 names, so we'll need to reallocate these arrays. */
2998 old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2999 bitmap_clear (old_ssa_names);
3001 new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
3002 bitmap_clear (new_ssa_names);
3004 bitmap_obstack_initialize (&update_ssa_obstack);
3006 names_to_release = NULL;
3007 update_ssa_initialized_fn = fn;
3011 /* Deallocate data structures used for incremental SSA updates. */
3013 void
3014 delete_update_ssa (void)
3016 unsigned i;
3017 bitmap_iterator bi;
3019 sbitmap_free (old_ssa_names);
3020 old_ssa_names = NULL;
3022 sbitmap_free (new_ssa_names);
3023 new_ssa_names = NULL;
3025 BITMAP_FREE (symbols_to_rename_set);
3026 symbols_to_rename_set = NULL;
3027 symbols_to_rename.release ();
3029 if (names_to_release)
3031 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
3032 release_ssa_name (ssa_name (i));
3033 BITMAP_FREE (names_to_release);
3036 clear_ssa_name_info ();
3038 fini_ssa_renamer ();
3040 BITMAP_FREE (blocks_with_phis_to_rewrite);
3041 BITMAP_FREE (blocks_to_update);
3043 update_ssa_initialized_fn = NULL;
3047 /* Create a new name for OLD_NAME in statement STMT and replace the
3048 operand pointed to by DEF_P with the newly created name. If DEF_P
3049 is NULL then STMT should be a GIMPLE assignment.
3050 Return the new name and register the replacement mapping <NEW, OLD> in
3051 update_ssa's tables. */
3053 tree
3054 create_new_def_for (tree old_name, gimple *stmt, def_operand_p def)
3056 tree new_name;
3058 timevar_push (TV_TREE_SSA_INCREMENTAL);
3060 if (!update_ssa_initialized_fn)
3061 init_update_ssa (cfun);
3063 gcc_assert (update_ssa_initialized_fn == cfun);
3065 new_name = duplicate_ssa_name (old_name, stmt);
3066 if (def)
3067 SET_DEF (def, new_name);
3068 else
3069 gimple_assign_set_lhs (stmt, new_name);
3071 if (gimple_code (stmt) == GIMPLE_PHI)
3073 basic_block bb = gimple_bb (stmt);
3075 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
3076 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = bb_has_abnormal_pred (bb);
3079 add_new_name_mapping (new_name, old_name);
3081 /* For the benefit of passes that will be updating the SSA form on
3082 their own, set the current reaching definition of OLD_NAME to be
3083 NEW_NAME. */
3084 get_ssa_name_ann (old_name)->info.current_def = new_name;
3086 timevar_pop (TV_TREE_SSA_INCREMENTAL);
3088 return new_name;
3092 /* Mark virtual operands of FN for renaming by update_ssa. */
3094 void
3095 mark_virtual_operands_for_renaming (struct function *fn)
3097 fn->gimple_df->ssa_renaming_needed = 1;
3098 fn->gimple_df->rename_vops = 1;
3101 /* Replace all uses of NAME by underlying variable and mark it
3102 for renaming. This assumes the defining statement of NAME is
3103 going to be removed. */
3105 void
3106 mark_virtual_operand_for_renaming (tree name)
3108 tree name_var = SSA_NAME_VAR (name);
3109 bool used = false;
3110 imm_use_iterator iter;
3111 use_operand_p use_p;
3112 gimple *stmt;
3114 gcc_assert (VAR_DECL_IS_VIRTUAL_OPERAND (name_var));
3115 FOR_EACH_IMM_USE_STMT (stmt, iter, name)
3117 FOR_EACH_IMM_USE_ON_STMT (use_p, iter)
3118 SET_USE (use_p, name_var);
3119 used = true;
3121 if (used)
3122 mark_virtual_operands_for_renaming (cfun);
3125 /* Replace all uses of the virtual PHI result by its underlying variable
3126 and mark it for renaming. This assumes the PHI node is going to be
3127 removed. */
3129 void
3130 mark_virtual_phi_result_for_renaming (gphi *phi)
3132 if (dump_file && (dump_flags & TDF_DETAILS))
3134 fprintf (dump_file, "Marking result for renaming : ");
3135 print_gimple_stmt (dump_file, phi, 0, TDF_SLIM);
3136 fprintf (dump_file, "\n");
3139 mark_virtual_operand_for_renaming (gimple_phi_result (phi));
3142 /* Return true if there is any work to be done by update_ssa
3143 for function FN. */
3145 bool
3146 need_ssa_update_p (struct function *fn)
3148 gcc_assert (fn != NULL);
3149 return (update_ssa_initialized_fn == fn
3150 || (fn->gimple_df && fn->gimple_df->ssa_renaming_needed));
3153 /* Return true if name N has been registered in the replacement table. */
3155 bool
3156 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED)
3158 if (!update_ssa_initialized_fn)
3159 return false;
3161 gcc_assert (update_ssa_initialized_fn == cfun);
3163 return is_new_name (n) || is_old_name (n);
3167 /* Mark NAME to be released after update_ssa has finished. */
3169 void
3170 release_ssa_name_after_update_ssa (tree name)
3172 gcc_assert (cfun && update_ssa_initialized_fn == cfun);
3174 if (names_to_release == NULL)
3175 names_to_release = BITMAP_ALLOC (NULL);
3177 bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
3181 /* Insert new PHI nodes to replace VAR. DFS contains dominance
3182 frontier information.
3184 This is slightly different than the regular PHI insertion
3185 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
3186 real names (i.e., GIMPLE registers) are inserted:
3188 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
3189 nodes inside the region affected by the block that defines VAR
3190 and the blocks that define all its replacements. All these
3191 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
3193 First, we compute the entry point to the region (ENTRY). This is
3194 given by the nearest common dominator to all the definition
3195 blocks. When computing the iterated dominance frontier (IDF), any
3196 block not strictly dominated by ENTRY is ignored.
3198 We then call the standard PHI insertion algorithm with the pruned
3199 IDF.
3201 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
3202 names is not pruned. PHI nodes are inserted at every IDF block. */
3204 static void
3205 insert_updated_phi_nodes_for (tree var, bitmap_head *dfs,
3206 unsigned update_flags)
3208 basic_block entry;
3209 def_blocks *db;
3210 bitmap pruned_idf;
3211 bitmap_iterator bi;
3212 unsigned i;
3214 if (TREE_CODE (var) == SSA_NAME)
3215 gcc_checking_assert (is_old_name (var));
3216 else
3217 gcc_checking_assert (marked_for_renaming (var));
3219 /* Get all the definition sites for VAR. */
3220 db = find_def_blocks_for (var);
3222 /* No need to do anything if there were no definitions to VAR. */
3223 if (db == NULL || bitmap_empty_p (db->def_blocks))
3224 return;
3226 /* Compute the initial iterated dominance frontier. */
3227 pruned_idf = compute_idf (db->def_blocks, dfs);
3229 if (TREE_CODE (var) == SSA_NAME)
3231 if (update_flags == TODO_update_ssa)
3233 /* If doing regular SSA updates for GIMPLE registers, we are
3234 only interested in IDF blocks dominated by the nearest
3235 common dominator of all the definition blocks. */
3236 entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
3237 db->def_blocks);
3238 if (entry != single_succ (ENTRY_BLOCK_PTR_FOR_FN (cfun)))
3240 unsigned to_remove = ~0U;
3241 EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
3243 if (to_remove != ~0U)
3245 bitmap_clear_bit (pruned_idf, to_remove);
3246 to_remove = ~0U;
3248 if (BASIC_BLOCK_FOR_FN (cfun, i) == entry
3249 || !dominated_by_p (CDI_DOMINATORS,
3250 BASIC_BLOCK_FOR_FN (cfun, i), entry))
3251 to_remove = i;
3253 if (to_remove != ~0U)
3254 bitmap_clear_bit (pruned_idf, to_remove);
3257 else
3258 /* Otherwise, do not prune the IDF for VAR. */
3259 gcc_checking_assert (update_flags == TODO_update_ssa_full_phi);
3261 /* Otherwise, VAR is a symbol that needs to be put into SSA form
3262 for the first time, so we need to compute the full IDF for
3263 it. */
3265 if (!bitmap_empty_p (pruned_idf))
3267 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3268 are included in the region to be updated. The feeding blocks
3269 are important to guarantee that the PHI arguments are renamed
3270 properly. */
3272 /* FIXME, this is not needed if we are updating symbols. We are
3273 already starting at the ENTRY block anyway. */
3274 EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
3276 edge e;
3277 edge_iterator ei;
3278 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, i);
3280 mark_block_for_update (bb);
3281 FOR_EACH_EDGE (e, ei, bb->preds)
3282 if (e->src->index >= NUM_FIXED_BLOCKS)
3283 mark_block_for_update (e->src);
3286 insert_phi_nodes_for (var, pruned_idf, true);
3289 BITMAP_FREE (pruned_idf);
3292 /* Sort symbols_to_rename after their DECL_UID. */
3294 static int
3295 insert_updated_phi_nodes_compare_uids (const void *a, const void *b)
3297 const_tree syma = *(const const_tree *)a;
3298 const_tree symb = *(const const_tree *)b;
3299 if (DECL_UID (syma) == DECL_UID (symb))
3300 return 0;
3301 return DECL_UID (syma) < DECL_UID (symb) ? -1 : 1;
3304 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3305 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3307 1- The names in OLD_SSA_NAMES dominated by the definitions of
3308 NEW_SSA_NAMES are all re-written to be reached by the
3309 appropriate definition from NEW_SSA_NAMES.
3311 2- If needed, new PHI nodes are added to the iterated dominance
3312 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3314 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3315 calling create_new_def_for to create new defs for names that the
3316 caller wants to replace.
3318 The caller cretaes the new names to be inserted and the names that need
3319 to be replaced by calling create_new_def_for for each old definition
3320 to be replaced. Note that the function assumes that the
3321 new defining statement has already been inserted in the IL.
3323 For instance, given the following code:
3325 1 L0:
3326 2 x_1 = PHI (0, x_5)
3327 3 if (x_1 < 10)
3328 4 if (x_1 > 7)
3329 5 y_2 = 0
3330 6 else
3331 7 y_3 = x_1 + x_7
3332 8 endif
3333 9 x_5 = x_1 + 1
3334 10 goto L0;
3335 11 endif
3337 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3339 1 L0:
3340 2 x_1 = PHI (0, x_5)
3341 3 if (x_1 < 10)
3342 4 x_10 = ...
3343 5 if (x_1 > 7)
3344 6 y_2 = 0
3345 7 else
3346 8 x_11 = ...
3347 9 y_3 = x_1 + x_7
3348 10 endif
3349 11 x_5 = x_1 + 1
3350 12 goto L0;
3351 13 endif
3353 We want to replace all the uses of x_1 with the new definitions of
3354 x_10 and x_11. Note that the only uses that should be replaced are
3355 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3356 *not* be replaced (this is why we cannot just mark symbol 'x' for
3357 renaming).
3359 Additionally, we may need to insert a PHI node at line 11 because
3360 that is a merge point for x_10 and x_11. So the use of x_1 at line
3361 11 will be replaced with the new PHI node. The insertion of PHI
3362 nodes is optional. They are not strictly necessary to preserve the
3363 SSA form, and depending on what the caller inserted, they may not
3364 even be useful for the optimizers. UPDATE_FLAGS controls various
3365 aspects of how update_ssa operates, see the documentation for
3366 TODO_update_ssa*. */
3368 void
3369 update_ssa (unsigned update_flags)
3371 basic_block bb, start_bb;
3372 bitmap_iterator bi;
3373 unsigned i = 0;
3374 bool insert_phi_p;
3375 sbitmap_iterator sbi;
3376 tree sym;
3378 /* Only one update flag should be set. */
3379 gcc_assert (update_flags == TODO_update_ssa
3380 || update_flags == TODO_update_ssa_no_phi
3381 || update_flags == TODO_update_ssa_full_phi
3382 || update_flags == TODO_update_ssa_only_virtuals);
3384 if (!need_ssa_update_p (cfun))
3385 return;
3387 if (flag_checking)
3389 timevar_push (TV_TREE_STMT_VERIFY);
3391 bool err = false;
3393 FOR_EACH_BB_FN (bb, cfun)
3395 gimple_stmt_iterator gsi;
3396 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
3398 gimple *stmt = gsi_stmt (gsi);
3400 ssa_op_iter i;
3401 use_operand_p use_p;
3402 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
3404 tree use = USE_FROM_PTR (use_p);
3405 if (TREE_CODE (use) != SSA_NAME)
3406 continue;
3408 if (SSA_NAME_IN_FREE_LIST (use))
3410 error ("statement uses released SSA name");
3411 debug_gimple_stmt (stmt);
3412 fprintf (stderr, "The use of ");
3413 print_generic_expr (stderr, use);
3414 fprintf (stderr," should have been replaced\n");
3415 err = true;
3421 if (err)
3422 internal_error ("cannot update SSA form");
3424 timevar_pop (TV_TREE_STMT_VERIFY);
3427 timevar_push (TV_TREE_SSA_INCREMENTAL);
3429 if (dump_file && (dump_flags & TDF_DETAILS))
3430 fprintf (dump_file, "\nUpdating SSA:\n");
3432 if (!update_ssa_initialized_fn)
3433 init_update_ssa (cfun);
3434 else if (update_flags == TODO_update_ssa_only_virtuals)
3436 /* If we only need to update virtuals, remove all the mappings for
3437 real names before proceeding. The caller is responsible for
3438 having dealt with the name mappings before calling update_ssa. */
3439 bitmap_clear (old_ssa_names);
3440 bitmap_clear (new_ssa_names);
3443 gcc_assert (update_ssa_initialized_fn == cfun);
3445 blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
3446 bitmap_tree_view (blocks_with_phis_to_rewrite);
3447 blocks_to_update = BITMAP_ALLOC (NULL);
3448 bitmap_tree_view (blocks_to_update);
3450 insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
3452 /* Ensure that the dominance information is up-to-date and when we
3453 are going to compute dominance frontiers fast queries are possible. */
3454 if (insert_phi_p || dom_info_state (CDI_DOMINATORS) == DOM_NONE)
3455 calculate_dominance_info (CDI_DOMINATORS);
3457 /* If there are names defined in the replacement table, prepare
3458 definition and use sites for all the names in NEW_SSA_NAMES and
3459 OLD_SSA_NAMES. */
3460 if (!bitmap_empty_p (new_ssa_names))
3462 statistics_counter_event (cfun, "Incremental SSA update", 1);
3464 prepare_names_to_update (insert_phi_p);
3466 /* If all the names in NEW_SSA_NAMES had been marked for
3467 removal, and there are no symbols to rename, then there's
3468 nothing else to do. */
3469 if (bitmap_empty_p (new_ssa_names)
3470 && !cfun->gimple_df->ssa_renaming_needed)
3471 goto done;
3474 /* Next, determine the block at which to start the renaming process. */
3475 if (cfun->gimple_df->ssa_renaming_needed)
3477 statistics_counter_event (cfun, "Symbol to SSA rewrite", 1);
3479 /* If we rename bare symbols initialize the mapping to
3480 auxiliar info we need to keep track of. */
3481 var_infos = new hash_table<var_info_hasher> (47);
3483 /* If we have to rename some symbols from scratch, we need to
3484 start the process at the root of the CFG. FIXME, it should
3485 be possible to determine the nearest block that had a
3486 definition for each of the symbols that are marked for
3487 updating. For now this seems more work than it's worth. */
3488 start_bb = ENTRY_BLOCK_PTR_FOR_FN (cfun);
3490 /* Traverse the CFG looking for existing definitions and uses of
3491 symbols in SSA operands. Mark interesting blocks and
3492 statements and set local live-in information for the PHI
3493 placement heuristics. */
3494 prepare_block_for_update (start_bb, insert_phi_p);
3496 bitmap_list_view (blocks_to_update);
3498 tree name;
3500 if (flag_checking)
3501 FOR_EACH_SSA_NAME (i, name, cfun)
3503 if (virtual_operand_p (name))
3504 continue;
3506 /* For all but virtual operands, which do not have SSA names
3507 with overlapping life ranges, ensure that symbols marked
3508 for renaming do not have existing SSA names associated with
3509 them as we do not re-write them out-of-SSA before going
3510 into SSA for the remaining symbol uses. */
3511 if (marked_for_renaming (SSA_NAME_VAR (name)))
3513 fprintf (stderr, "Existing SSA name for symbol marked for "
3514 "renaming: ");
3515 print_generic_expr (stderr, name, TDF_SLIM);
3516 fprintf (stderr, "\n");
3517 internal_error ("SSA corruption");
3521 else
3523 bitmap_list_view (blocks_to_update);
3525 /* Otherwise, the entry block to the region is the nearest
3526 common dominator for the blocks in BLOCKS. */
3527 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3528 blocks_to_update);
3531 /* If requested, insert PHI nodes at the iterated dominance frontier
3532 of every block, creating new definitions for names in OLD_SSA_NAMES
3533 and for symbols found. */
3534 if (insert_phi_p)
3536 bitmap_head *dfs;
3538 /* If the caller requested PHI nodes to be added, compute
3539 dominance frontiers. */
3540 dfs = XNEWVEC (bitmap_head, last_basic_block_for_fn (cfun));
3541 FOR_EACH_BB_FN (bb, cfun)
3542 bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
3543 compute_dominance_frontiers (dfs);
3545 bitmap_tree_view (blocks_to_update);
3547 /* insert_update_phi_nodes_for will call add_new_name_mapping
3548 when inserting new PHI nodes, but it will not add any
3549 new members to OLD_SSA_NAMES. */
3550 iterating_old_ssa_names = true;
3551 sbitmap_iterator sbi;
3552 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
3553 insert_updated_phi_nodes_for (ssa_name (i), dfs, update_flags);
3554 iterating_old_ssa_names = false;
3556 symbols_to_rename.qsort (insert_updated_phi_nodes_compare_uids);
3557 FOR_EACH_VEC_ELT (symbols_to_rename, i, sym)
3558 insert_updated_phi_nodes_for (sym, dfs, update_flags);
3560 bitmap_list_view (blocks_to_update);
3562 FOR_EACH_BB_FN (bb, cfun)
3563 bitmap_clear (&dfs[bb->index]);
3564 free (dfs);
3566 /* Insertion of PHI nodes may have added blocks to the region.
3567 We need to re-compute START_BB to include the newly added
3568 blocks. */
3569 if (start_bb != ENTRY_BLOCK_PTR_FOR_FN (cfun))
3570 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3571 blocks_to_update);
3574 /* Reset the current definition for name and symbol before renaming
3575 the sub-graph. */
3576 EXECUTE_IF_SET_IN_BITMAP (old_ssa_names, 0, i, sbi)
3577 get_ssa_name_ann (ssa_name (i))->info.current_def = NULL_TREE;
3579 FOR_EACH_VEC_ELT (symbols_to_rename, i, sym)
3580 get_var_info (sym)->info.current_def = NULL_TREE;
3582 /* Now start the renaming process at START_BB. When not inserting PHIs
3583 and thus we are avoiding work on all blocks, try to confine the
3584 rewriting domwalk to the affected region, otherwise it's not worth it. */
3585 rewrite_blocks (start_bb,
3586 insert_phi_p ? REWRITE_UPDATE : REWRITE_UPDATE_REGION);
3588 /* Debugging dumps. */
3589 if (dump_file)
3591 int c;
3592 unsigned i;
3594 dump_update_ssa (dump_file);
3596 fprintf (dump_file, "Incremental SSA update started at block: %d\n",
3597 start_bb->index);
3599 c = 0;
3600 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3601 c++;
3602 fprintf (dump_file, "Number of blocks in CFG: %d\n",
3603 last_basic_block_for_fn (cfun));
3604 fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n",
3605 c, PERCENT (c, last_basic_block_for_fn (cfun)));
3607 if (dump_flags & TDF_DETAILS)
3609 fprintf (dump_file, "Affected blocks:");
3610 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3611 fprintf (dump_file, " %u", i);
3612 fprintf (dump_file, "\n");
3615 fprintf (dump_file, "\n\n");
3618 /* Free allocated memory. */
3619 done:
3620 delete_update_ssa ();
3622 timevar_pop (TV_TREE_SSA_INCREMENTAL);