Fix typo in ChangeLog entry date.
[official-gcc.git] / gcc / tree-into-ssa.c
blobd1f0ff7820eeee8f712e7ab042184f9a9a9d63ef
1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "output.h"
34 #include "expr.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "bitmap.h"
38 #include "tree-flow.h"
39 #include "gimple.h"
40 #include "tree-inline.h"
41 #include "varray.h"
42 #include "timevar.h"
43 #include "hashtab.h"
44 #include "tree-dump.h"
45 #include "tree-pass.h"
46 #include "cfgloop.h"
47 #include "domwalk.h"
48 #include "ggc.h"
49 #include "params.h"
50 #include "vecprim.h"
53 /* This file builds the SSA form for a function as described in:
54 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
55 Computing Static Single Assignment Form and the Control Dependence
56 Graph. ACM Transactions on Programming Languages and Systems,
57 13(4):451-490, October 1991. */
59 /* Structure to map a variable VAR to the set of blocks that contain
60 definitions for VAR. */
61 struct def_blocks_d
63 /* The variable. */
64 tree var;
66 /* Blocks that contain definitions of VAR. Bit I will be set if the
67 Ith block contains a definition of VAR. */
68 bitmap def_blocks;
70 /* Blocks that contain a PHI node for VAR. */
71 bitmap phi_blocks;
73 /* Blocks where VAR is live-on-entry. Similar semantics as
74 DEF_BLOCKS. */
75 bitmap livein_blocks;
79 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
80 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
81 basic blocks where VAR is defined (assigned a new value). It also
82 contains a bitmap of all the blocks where VAR is live-on-entry
83 (i.e., there is a use of VAR in block B without a preceding
84 definition in B). The live-on-entry information is used when
85 computing PHI pruning heuristics. */
86 static htab_t def_blocks;
88 /* Stack of trees used to restore the global currdefs to its original
89 state after completing rewriting of a block and its dominator
90 children. Its elements have the following properties:
92 - An SSA_NAME (N) indicates that the current definition of the
93 underlying variable should be set to the given SSA_NAME. If the
94 symbol associated with the SSA_NAME is not a GIMPLE register, the
95 next slot in the stack must be a _DECL node (SYM). In this case,
96 the name N in the previous slot is the current reaching
97 definition for SYM.
99 - A _DECL node indicates that the underlying variable has no
100 current definition.
102 - A NULL node at the top entry is used to mark the last slot
103 associated with the current block. */
104 static VEC(tree,heap) *block_defs_stack;
107 /* Set of existing SSA names being replaced by update_ssa. */
108 static sbitmap old_ssa_names;
110 /* Set of new SSA names being added by update_ssa. Note that both
111 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
112 the operations done on them are presence tests. */
113 static sbitmap new_ssa_names;
116 /* Set of SSA names that have been marked to be released after they
117 were registered in the replacement table. They will be finally
118 released after we finish updating the SSA web. */
119 static bitmap names_to_release;
121 static VEC(gimple_vec, heap) *phis_to_rewrite;
123 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
124 static bitmap blocks_with_phis_to_rewrite;
126 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
127 to grow as the callers to register_new_name_mapping will typically
128 create new names on the fly. FIXME. Currently set to 1/3 to avoid
129 frequent reallocations but still need to find a reasonable growth
130 strategy. */
131 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
133 /* Tuple used to represent replacement mappings. */
134 struct repl_map_d
136 tree name;
137 bitmap set;
141 /* NEW -> OLD_SET replacement table. If we are replacing several
142 existing SSA names O_1, O_2, ..., O_j with a new name N_i,
143 then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }. */
144 static htab_t repl_tbl;
146 /* The function the SSA updating data structures have been initialized for.
147 NULL if they need to be initialized by register_new_name_mapping. */
148 static struct function *update_ssa_initialized_fn = NULL;
150 /* Statistics kept by update_ssa to use in the virtual mapping
151 heuristic. If the number of virtual mappings is beyond certain
152 threshold, the updater will switch from using the mappings into
153 renaming the virtual symbols from scratch. In some cases, the
154 large number of name mappings for virtual names causes significant
155 slowdowns in the PHI insertion code. */
156 struct update_ssa_stats_d
158 unsigned num_virtual_mappings;
159 unsigned num_total_mappings;
160 bitmap virtual_symbols;
161 unsigned num_virtual_symbols;
163 static struct update_ssa_stats_d update_ssa_stats;
165 /* Global data to attach to the main dominator walk structure. */
166 struct mark_def_sites_global_data
168 /* This bitmap contains the variables which are set before they
169 are used in a basic block. */
170 bitmap kills;
172 /* Bitmap of names to rename. */
173 sbitmap names_to_rename;
175 /* Set of blocks that mark_def_sites deems interesting for the
176 renamer to process. */
177 sbitmap interesting_blocks;
181 /* Information stored for SSA names. */
182 struct ssa_name_info
184 /* The current reaching definition replacing this SSA name. */
185 tree current_def;
187 /* This field indicates whether or not the variable may need PHI nodes.
188 See the enum's definition for more detailed information about the
189 states. */
190 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
192 /* Age of this record (so that info_for_ssa_name table can be cleared
193 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
194 are assumed to be null. */
195 unsigned age;
198 /* The information associated with names. */
199 typedef struct ssa_name_info *ssa_name_info_p;
200 DEF_VEC_P (ssa_name_info_p);
201 DEF_VEC_ALLOC_P (ssa_name_info_p, heap);
203 static VEC(ssa_name_info_p, heap) *info_for_ssa_name;
204 static unsigned current_info_for_ssa_name_age;
206 /* The set of blocks affected by update_ssa. */
207 static bitmap blocks_to_update;
209 /* The main entry point to the SSA renamer (rewrite_blocks) may be
210 called several times to do different, but related, tasks.
211 Initially, we need it to rename the whole program into SSA form.
212 At other times, we may need it to only rename into SSA newly
213 exposed symbols. Finally, we can also call it to incrementally fix
214 an already built SSA web. */
215 enum rewrite_mode {
216 /* Convert the whole function into SSA form. */
217 REWRITE_ALL,
219 /* Incrementally update the SSA web by replacing existing SSA
220 names with new ones. See update_ssa for details. */
221 REWRITE_UPDATE
227 /* Prototypes for debugging functions. */
228 extern void dump_tree_ssa (FILE *);
229 extern void debug_tree_ssa (void);
230 extern void debug_def_blocks (void);
231 extern void dump_tree_ssa_stats (FILE *);
232 extern void debug_tree_ssa_stats (void);
233 extern void dump_update_ssa (FILE *);
234 extern void debug_update_ssa (void);
235 extern void dump_names_replaced_by (FILE *, tree);
236 extern void debug_names_replaced_by (tree);
237 extern void dump_def_blocks (FILE *);
238 extern void debug_def_blocks (void);
239 extern void dump_defs_stack (FILE *, int);
240 extern void debug_defs_stack (int);
241 extern void dump_currdefs (FILE *);
242 extern void debug_currdefs (void);
244 /* Return true if STMT needs to be rewritten. When renaming a subset
245 of the variables, not all statements will be processed. This is
246 decided in mark_def_sites. */
248 static inline bool
249 rewrite_uses_p (gimple stmt)
251 return gimple_visited_p (stmt);
255 /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
257 static inline void
258 set_rewrite_uses (gimple stmt, bool rewrite_p)
260 gimple_set_visited (stmt, rewrite_p);
264 /* Return true if the DEFs created by statement STMT should be
265 registered when marking new definition sites. This is slightly
266 different than rewrite_uses_p: it's used by update_ssa to
267 distinguish statements that need to have both uses and defs
268 processed from those that only need to have their defs processed.
269 Statements that define new SSA names only need to have their defs
270 registered, but they don't need to have their uses renamed. */
272 static inline bool
273 register_defs_p (gimple stmt)
275 return gimple_plf (stmt, GF_PLF_1) != 0;
279 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
281 static inline void
282 set_register_defs (gimple stmt, bool register_defs_p)
284 gimple_set_plf (stmt, GF_PLF_1, register_defs_p);
288 /* Get the information associated with NAME. */
290 static inline ssa_name_info_p
291 get_ssa_name_ann (tree name)
293 unsigned ver = SSA_NAME_VERSION (name);
294 unsigned len = VEC_length (ssa_name_info_p, info_for_ssa_name);
295 struct ssa_name_info *info;
297 if (ver >= len)
299 unsigned new_len = num_ssa_names;
301 VEC_reserve (ssa_name_info_p, heap, info_for_ssa_name, new_len);
302 while (len++ < new_len)
304 struct ssa_name_info *info = XCNEW (struct ssa_name_info);
305 info->age = current_info_for_ssa_name_age;
306 VEC_quick_push (ssa_name_info_p, info_for_ssa_name, info);
310 info = VEC_index (ssa_name_info_p, info_for_ssa_name, ver);
311 if (info->age < current_info_for_ssa_name_age)
313 info->need_phi_state = NEED_PHI_STATE_UNKNOWN;
314 info->current_def = NULL_TREE;
315 info->age = current_info_for_ssa_name_age;
318 return info;
322 /* Clears info for SSA names. */
324 static void
325 clear_ssa_name_info (void)
327 current_info_for_ssa_name_age++;
331 /* Get phi_state field for VAR. */
333 static inline enum need_phi_state
334 get_phi_state (tree var)
336 if (TREE_CODE (var) == SSA_NAME)
337 return get_ssa_name_ann (var)->need_phi_state;
338 else
339 return var_ann (var)->need_phi_state;
343 /* Sets phi_state field for VAR to STATE. */
345 static inline void
346 set_phi_state (tree var, enum need_phi_state state)
348 if (TREE_CODE (var) == SSA_NAME)
349 get_ssa_name_ann (var)->need_phi_state = state;
350 else
351 var_ann (var)->need_phi_state = state;
355 /* Return the current definition for VAR. */
357 tree
358 get_current_def (tree var)
360 if (TREE_CODE (var) == SSA_NAME)
361 return get_ssa_name_ann (var)->current_def;
362 else
363 return var_ann (var)->current_def;
367 /* Sets current definition of VAR to DEF. */
369 void
370 set_current_def (tree var, tree def)
372 if (TREE_CODE (var) == SSA_NAME)
373 get_ssa_name_ann (var)->current_def = def;
374 else
375 var_ann (var)->current_def = def;
379 /* Compute global livein information given the set of blocks where
380 an object is locally live at the start of the block (LIVEIN)
381 and the set of blocks where the object is defined (DEF_BLOCKS).
383 Note: This routine augments the existing local livein information
384 to include global livein (i.e., it modifies the underlying bitmap
385 for LIVEIN). */
387 void
388 compute_global_livein (bitmap livein ATTRIBUTE_UNUSED, bitmap def_blocks ATTRIBUTE_UNUSED)
390 basic_block bb, *worklist, *tos;
391 unsigned i;
392 bitmap_iterator bi;
394 tos = worklist
395 = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
397 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
398 *tos++ = BASIC_BLOCK (i);
400 /* Iterate until the worklist is empty. */
401 while (tos != worklist)
403 edge e;
404 edge_iterator ei;
406 /* Pull a block off the worklist. */
407 bb = *--tos;
409 /* For each predecessor block. */
410 FOR_EACH_EDGE (e, ei, bb->preds)
412 basic_block pred = e->src;
413 int pred_index = pred->index;
415 /* None of this is necessary for the entry block. */
416 if (pred != ENTRY_BLOCK_PTR
417 && ! bitmap_bit_p (livein, pred_index)
418 && ! bitmap_bit_p (def_blocks, pred_index))
420 *tos++ = pred;
421 bitmap_set_bit (livein, pred_index);
426 free (worklist);
430 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
431 all statements in basic block BB. */
433 static void
434 initialize_flags_in_bb (basic_block bb)
436 gimple stmt;
437 gimple_stmt_iterator gsi;
439 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
441 gimple phi = gsi_stmt (gsi);
442 set_rewrite_uses (phi, false);
443 set_register_defs (phi, false);
446 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
448 stmt = gsi_stmt (gsi);
450 /* We are going to use the operand cache API, such as
451 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
452 cache for each statement should be up-to-date. */
453 gcc_assert (!gimple_modified_p (stmt));
454 set_rewrite_uses (stmt, false);
455 set_register_defs (stmt, false);
459 /* Mark block BB as interesting for update_ssa. */
461 static void
462 mark_block_for_update (basic_block bb)
464 gcc_assert (blocks_to_update != NULL);
465 if (bitmap_bit_p (blocks_to_update, bb->index))
466 return;
467 bitmap_set_bit (blocks_to_update, bb->index);
468 initialize_flags_in_bb (bb);
471 /* Return the set of blocks where variable VAR is defined and the blocks
472 where VAR is live on entry (livein). If no entry is found in
473 DEF_BLOCKS, a new one is created and returned. */
475 static inline struct def_blocks_d *
476 get_def_blocks_for (tree var)
478 struct def_blocks_d db, *db_p;
479 void **slot;
481 db.var = var;
482 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
483 if (*slot == NULL)
485 db_p = XNEW (struct def_blocks_d);
486 db_p->var = var;
487 db_p->def_blocks = BITMAP_ALLOC (NULL);
488 db_p->phi_blocks = BITMAP_ALLOC (NULL);
489 db_p->livein_blocks = BITMAP_ALLOC (NULL);
490 *slot = (void *) db_p;
492 else
493 db_p = (struct def_blocks_d *) *slot;
495 return db_p;
499 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
500 VAR is defined by a PHI node. */
502 static void
503 set_def_block (tree var, basic_block bb, bool phi_p)
505 struct def_blocks_d *db_p;
506 enum need_phi_state state;
508 state = get_phi_state (var);
509 db_p = get_def_blocks_for (var);
511 /* Set the bit corresponding to the block where VAR is defined. */
512 bitmap_set_bit (db_p->def_blocks, bb->index);
513 if (phi_p)
514 bitmap_set_bit (db_p->phi_blocks, bb->index);
516 /* Keep track of whether or not we may need to insert PHI nodes.
518 If we are in the UNKNOWN state, then this is the first definition
519 of VAR. Additionally, we have not seen any uses of VAR yet, so
520 we do not need a PHI node for this variable at this time (i.e.,
521 transition to NEED_PHI_STATE_NO).
523 If we are in any other state, then we either have multiple definitions
524 of this variable occurring in different blocks or we saw a use of the
525 variable which was not dominated by the block containing the
526 definition(s). In this case we may need a PHI node, so enter
527 state NEED_PHI_STATE_MAYBE. */
528 if (state == NEED_PHI_STATE_UNKNOWN)
529 set_phi_state (var, NEED_PHI_STATE_NO);
530 else
531 set_phi_state (var, NEED_PHI_STATE_MAYBE);
535 /* Mark block BB as having VAR live at the entry to BB. */
537 static void
538 set_livein_block (tree var, basic_block bb)
540 struct def_blocks_d *db_p;
541 enum need_phi_state state = get_phi_state (var);
543 db_p = get_def_blocks_for (var);
545 /* Set the bit corresponding to the block where VAR is live in. */
546 bitmap_set_bit (db_p->livein_blocks, bb->index);
548 /* Keep track of whether or not we may need to insert PHI nodes.
550 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
551 by the single block containing the definition(s) of this variable. If
552 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
553 NEED_PHI_STATE_MAYBE. */
554 if (state == NEED_PHI_STATE_NO)
556 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
558 if (def_block_index == -1
559 || ! dominated_by_p (CDI_DOMINATORS, bb,
560 BASIC_BLOCK (def_block_index)))
561 set_phi_state (var, NEED_PHI_STATE_MAYBE);
563 else
564 set_phi_state (var, NEED_PHI_STATE_MAYBE);
568 /* Return true if symbol SYM is marked for renaming. */
570 static inline bool
571 symbol_marked_for_renaming (tree sym)
573 return bitmap_bit_p (SYMS_TO_RENAME (cfun), DECL_UID (sym));
577 /* Return true if NAME is in OLD_SSA_NAMES. */
579 static inline bool
580 is_old_name (tree name)
582 unsigned ver = SSA_NAME_VERSION (name);
583 if (!new_ssa_names)
584 return false;
585 return ver < new_ssa_names->n_bits && TEST_BIT (old_ssa_names, ver);
589 /* Return true if NAME is in NEW_SSA_NAMES. */
591 static inline bool
592 is_new_name (tree name)
594 unsigned ver = SSA_NAME_VERSION (name);
595 if (!new_ssa_names)
596 return false;
597 return ver < new_ssa_names->n_bits && TEST_BIT (new_ssa_names, ver);
601 /* Hashing and equality functions for REPL_TBL. */
603 static hashval_t
604 repl_map_hash (const void *p)
606 return htab_hash_pointer ((const void *)((const struct repl_map_d *)p)->name);
609 static int
610 repl_map_eq (const void *p1, const void *p2)
612 return ((const struct repl_map_d *)p1)->name
613 == ((const struct repl_map_d *)p2)->name;
616 static void
617 repl_map_free (void *p)
619 BITMAP_FREE (((struct repl_map_d *)p)->set);
620 free (p);
624 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
626 static inline bitmap
627 names_replaced_by (tree new_tree)
629 struct repl_map_d m;
630 void **slot;
632 m.name = new_tree;
633 slot = htab_find_slot (repl_tbl, (void *) &m, NO_INSERT);
635 /* If N was not registered in the replacement table, return NULL. */
636 if (slot == NULL || *slot == NULL)
637 return NULL;
639 return ((struct repl_map_d *) *slot)->set;
643 /* Add OLD to REPL_TBL[NEW_TREE].SET. */
645 static inline void
646 add_to_repl_tbl (tree new_tree, tree old)
648 struct repl_map_d m, *mp;
649 void **slot;
651 m.name = new_tree;
652 slot = htab_find_slot (repl_tbl, (void *) &m, INSERT);
653 if (*slot == NULL)
655 mp = XNEW (struct repl_map_d);
656 mp->name = new_tree;
657 mp->set = BITMAP_ALLOC (NULL);
658 *slot = (void *) mp;
660 else
661 mp = (struct repl_map_d *) *slot;
663 bitmap_set_bit (mp->set, SSA_NAME_VERSION (old));
667 /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
668 represents the set of names O_1 ... O_j replaced by N_i. This is
669 used by update_ssa and its helpers to introduce new SSA names in an
670 already formed SSA web. */
672 static void
673 add_new_name_mapping (tree new_tree, tree old)
675 timevar_push (TV_TREE_SSA_INCREMENTAL);
677 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
678 gcc_assert (new_tree != old && SSA_NAME_VAR (new_tree) == SSA_NAME_VAR (old));
680 /* If this mapping is for virtual names, we will need to update
681 virtual operands. If this is a mapping for .MEM, then we gather
682 the symbols associated with each name. */
683 if (!is_gimple_reg (new_tree))
685 tree sym;
687 update_ssa_stats.num_virtual_mappings++;
688 update_ssa_stats.num_virtual_symbols++;
690 /* Keep counts of virtual mappings and symbols to use in the
691 virtual mapping heuristic. If we have large numbers of
692 virtual mappings for a relatively low number of symbols, it
693 will make more sense to rename the symbols from scratch.
694 Otherwise, the insertion of PHI nodes for each of the old
695 names in these mappings will be very slow. */
696 sym = SSA_NAME_VAR (new_tree);
697 bitmap_set_bit (update_ssa_stats.virtual_symbols, DECL_UID (sym));
700 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
701 caller may have created new names since the set was created. */
702 if (new_ssa_names->n_bits <= num_ssa_names - 1)
704 unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
705 new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
706 old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
709 /* Update the REPL_TBL table. */
710 add_to_repl_tbl (new_tree, old);
712 /* If OLD had already been registered as a new name, then all the
713 names that OLD replaces should also be replaced by NEW_TREE. */
714 if (is_new_name (old))
715 bitmap_ior_into (names_replaced_by (new_tree), names_replaced_by (old));
717 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
718 respectively. */
719 SET_BIT (new_ssa_names, SSA_NAME_VERSION (new_tree));
720 SET_BIT (old_ssa_names, SSA_NAME_VERSION (old));
722 /* Update mapping counter to use in the virtual mapping heuristic. */
723 update_ssa_stats.num_total_mappings++;
725 timevar_pop (TV_TREE_SSA_INCREMENTAL);
729 /* Call back for walk_dominator_tree used to collect definition sites
730 for every variable in the function. For every statement S in block
733 1- Variables defined by S in the DEFS of S are marked in the bitmap
734 WALK_DATA->GLOBAL_DATA->KILLS.
736 2- If S uses a variable VAR and there is no preceding kill of VAR,
737 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
739 This information is used to determine which variables are live
740 across block boundaries to reduce the number of PHI nodes
741 we create. */
743 static void
744 mark_def_sites (struct dom_walk_data *walk_data, basic_block bb,
745 gimple_stmt_iterator gsi)
747 struct mark_def_sites_global_data *gd;
748 bitmap kills;
749 tree def;
750 gimple stmt;
751 use_operand_p use_p;
752 ssa_op_iter iter;
754 /* Since this is the first time that we rewrite the program into SSA
755 form, force an operand scan on every statement. */
756 stmt = gsi_stmt (gsi);
757 update_stmt (stmt);
759 gd = (struct mark_def_sites_global_data *) walk_data->global_data;
760 kills = gd->kills;
762 gcc_assert (blocks_to_update == NULL);
763 set_register_defs (stmt, false);
764 set_rewrite_uses (stmt, false);
766 /* If a variable is used before being set, then the variable is live
767 across a block boundary, so mark it live-on-entry to BB. */
768 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
770 tree sym = USE_FROM_PTR (use_p);
771 gcc_assert (DECL_P (sym));
772 if (!bitmap_bit_p (kills, DECL_UID (sym)))
773 set_livein_block (sym, bb);
774 set_rewrite_uses (stmt, true);
777 /* Now process the defs. Mark BB as the definition block and add
778 each def to the set of killed symbols. */
779 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
781 gcc_assert (DECL_P (def));
782 set_def_block (def, bb, false);
783 bitmap_set_bit (kills, DECL_UID (def));
784 set_register_defs (stmt, true);
787 /* If we found the statement interesting then also mark the block BB
788 as interesting. */
789 if (rewrite_uses_p (stmt) || register_defs_p (stmt))
790 SET_BIT (gd->interesting_blocks, bb->index);
793 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
794 in the dfs numbering of the dominance tree. */
796 struct dom_dfsnum
798 /* Basic block whose index this entry corresponds to. */
799 unsigned bb_index;
801 /* The dfs number of this node. */
802 unsigned dfs_num;
805 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
806 for qsort. */
808 static int
809 cmp_dfsnum (const void *a, const void *b)
811 const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a;
812 const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b;
814 return (int) da->dfs_num - (int) db->dfs_num;
817 /* Among the intervals starting at the N points specified in DEFS, find
818 the one that contains S, and return its bb_index. */
820 static unsigned
821 find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
823 unsigned f = 0, t = n, m;
825 while (t > f + 1)
827 m = (f + t) / 2;
828 if (defs[m].dfs_num <= s)
829 f = m;
830 else
831 t = m;
834 return defs[f].bb_index;
837 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
838 KILLS is a bitmap of blocks where the value is defined before any use. */
840 static void
841 prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
843 VEC(int, heap) *worklist;
844 bitmap_iterator bi;
845 unsigned i, b, p, u, top;
846 bitmap live_phis;
847 basic_block def_bb, use_bb;
848 edge e;
849 edge_iterator ei;
850 bitmap to_remove;
851 struct dom_dfsnum *defs;
852 unsigned n_defs, adef;
854 if (bitmap_empty_p (uses))
856 bitmap_clear (phis);
857 return;
860 /* The phi must dominate a use, or an argument of a live phi. Also, we
861 do not create any phi nodes in def blocks, unless they are also livein. */
862 to_remove = BITMAP_ALLOC (NULL);
863 bitmap_and_compl (to_remove, kills, uses);
864 bitmap_and_compl_into (phis, to_remove);
865 if (bitmap_empty_p (phis))
867 BITMAP_FREE (to_remove);
868 return;
871 /* We want to remove the unnecessary phi nodes, but we do not want to compute
872 liveness information, as that may be linear in the size of CFG, and if
873 there are lot of different variables to rewrite, this may lead to quadratic
874 behavior.
876 Instead, we basically emulate standard dce. We put all uses to worklist,
877 then for each of them find the nearest def that dominates them. If this
878 def is a phi node, we mark it live, and if it was not live before, we
879 add the predecessors of its basic block to the worklist.
881 To quickly locate the nearest def that dominates use, we use dfs numbering
882 of the dominance tree (that is already available in order to speed up
883 queries). For each def, we have the interval given by the dfs number on
884 entry to and on exit from the corresponding subtree in the dominance tree.
885 The nearest dominator for a given use is the smallest of these intervals
886 that contains entry and exit dfs numbers for the basic block with the use.
887 If we store the bounds for all the uses to an array and sort it, we can
888 locate the nearest dominating def in logarithmic time by binary search.*/
889 bitmap_ior (to_remove, kills, phis);
890 n_defs = bitmap_count_bits (to_remove);
891 defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
892 defs[0].bb_index = 1;
893 defs[0].dfs_num = 0;
894 adef = 1;
895 EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
897 def_bb = BASIC_BLOCK (i);
898 defs[adef].bb_index = i;
899 defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
900 defs[adef + 1].bb_index = i;
901 defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
902 adef += 2;
904 BITMAP_FREE (to_remove);
905 gcc_assert (adef == 2 * n_defs + 1);
906 qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
907 gcc_assert (defs[0].bb_index == 1);
909 /* Now each DEFS entry contains the number of the basic block to that the
910 dfs number corresponds. Change them to the number of basic block that
911 corresponds to the interval following the dfs number. Also, for the
912 dfs_out numbers, increase the dfs number by one (so that it corresponds
913 to the start of the following interval, not to the end of the current
914 one). We use WORKLIST as a stack. */
915 worklist = VEC_alloc (int, heap, n_defs + 1);
916 VEC_quick_push (int, worklist, 1);
917 top = 1;
918 n_defs = 1;
919 for (i = 1; i < adef; i++)
921 b = defs[i].bb_index;
922 if (b == top)
924 /* This is a closing element. Interval corresponding to the top
925 of the stack after removing it follows. */
926 VEC_pop (int, worklist);
927 top = VEC_index (int, worklist, VEC_length (int, worklist) - 1);
928 defs[n_defs].bb_index = top;
929 defs[n_defs].dfs_num = defs[i].dfs_num + 1;
931 else
933 /* Opening element. Nothing to do, just push it to the stack and move
934 it to the correct position. */
935 defs[n_defs].bb_index = defs[i].bb_index;
936 defs[n_defs].dfs_num = defs[i].dfs_num;
937 VEC_quick_push (int, worklist, b);
938 top = b;
941 /* If this interval starts at the same point as the previous one, cancel
942 the previous one. */
943 if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
944 defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
945 else
946 n_defs++;
948 VEC_pop (int, worklist);
949 gcc_assert (VEC_empty (int, worklist));
951 /* Now process the uses. */
952 live_phis = BITMAP_ALLOC (NULL);
953 EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
955 VEC_safe_push (int, heap, worklist, i);
958 while (!VEC_empty (int, worklist))
960 b = VEC_pop (int, worklist);
961 if (b == ENTRY_BLOCK)
962 continue;
964 /* If there is a phi node in USE_BB, it is made live. Otherwise,
965 find the def that dominates the immediate dominator of USE_BB
966 (the kill in USE_BB does not dominate the use). */
967 if (bitmap_bit_p (phis, b))
968 p = b;
969 else
971 use_bb = get_immediate_dominator (CDI_DOMINATORS, BASIC_BLOCK (b));
972 p = find_dfsnum_interval (defs, n_defs,
973 bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
974 if (!bitmap_bit_p (phis, p))
975 continue;
978 /* If the phi node is already live, there is nothing to do. */
979 if (bitmap_bit_p (live_phis, p))
980 continue;
982 /* Mark the phi as live, and add the new uses to the worklist. */
983 bitmap_set_bit (live_phis, p);
984 def_bb = BASIC_BLOCK (p);
985 FOR_EACH_EDGE (e, ei, def_bb->preds)
987 u = e->src->index;
988 if (bitmap_bit_p (uses, u))
989 continue;
991 /* In case there is a kill directly in the use block, do not record
992 the use (this is also necessary for correctness, as we assume that
993 uses dominated by a def directly in their block have been filtered
994 out before). */
995 if (bitmap_bit_p (kills, u))
996 continue;
998 bitmap_set_bit (uses, u);
999 VEC_safe_push (int, heap, worklist, u);
1003 VEC_free (int, heap, worklist);
1004 bitmap_copy (phis, live_phis);
1005 BITMAP_FREE (live_phis);
1006 free (defs);
1009 /* Return the set of blocks where variable VAR is defined and the blocks
1010 where VAR is live on entry (livein). Return NULL, if no entry is
1011 found in DEF_BLOCKS. */
1013 static inline struct def_blocks_d *
1014 find_def_blocks_for (tree var)
1016 struct def_blocks_d dm;
1017 dm.var = var;
1018 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1022 /* Retrieve or create a default definition for symbol SYM. */
1024 static inline tree
1025 get_default_def_for (tree sym)
1027 tree ddef = gimple_default_def (cfun, sym);
1029 if (ddef == NULL_TREE)
1031 ddef = make_ssa_name (sym, gimple_build_nop ());
1032 set_default_def (sym, ddef);
1035 return ddef;
1039 /* Marks phi node PHI in basic block BB for rewrite. */
1041 static void
1042 mark_phi_for_rewrite (basic_block bb, gimple phi)
1044 gimple_vec phis;
1045 unsigned i, idx = bb->index;
1047 if (rewrite_uses_p (phi))
1048 return;
1050 set_rewrite_uses (phi, true);
1052 if (!blocks_with_phis_to_rewrite)
1053 return;
1055 bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
1056 VEC_reserve (gimple_vec, heap, phis_to_rewrite, last_basic_block + 1);
1057 for (i = VEC_length (gimple_vec, phis_to_rewrite); i <= idx; i++)
1058 VEC_quick_push (gimple_vec, phis_to_rewrite, NULL);
1060 phis = VEC_index (gimple_vec, phis_to_rewrite, idx);
1061 if (!phis)
1062 phis = VEC_alloc (gimple, heap, 10);
1064 VEC_safe_push (gimple, heap, phis, phi);
1065 VEC_replace (gimple_vec, phis_to_rewrite, idx, phis);
1069 /* Insert PHI nodes for variable VAR using the iterated dominance
1070 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1071 function assumes that the caller is incrementally updating the
1072 existing SSA form, in which case VAR may be an SSA name instead of
1073 a symbol.
1075 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1076 PHI node for VAR. On exit, only the nodes that received a PHI node
1077 for VAR will be present in PHI_INSERTION_POINTS. */
1079 static void
1080 insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
1082 unsigned bb_index;
1083 edge e;
1084 gimple phi;
1085 basic_block bb;
1086 bitmap_iterator bi;
1087 struct def_blocks_d *def_map;
1089 def_map = find_def_blocks_for (var);
1090 gcc_assert (def_map);
1092 /* Remove the blocks where we already have PHI nodes for VAR. */
1093 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1095 /* Remove obviously useless phi nodes. */
1096 prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
1097 def_map->livein_blocks);
1099 /* And insert the PHI nodes. */
1100 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
1102 bb = BASIC_BLOCK (bb_index);
1103 if (update_p)
1104 mark_block_for_update (bb);
1106 phi = NULL;
1108 if (TREE_CODE (var) == SSA_NAME)
1110 /* If we are rewriting SSA names, create the LHS of the PHI
1111 node by duplicating VAR. This is useful in the case of
1112 pointers, to also duplicate pointer attributes (alias
1113 information, in particular). */
1114 edge_iterator ei;
1115 tree new_lhs;
1117 gcc_assert (update_p);
1118 phi = create_phi_node (var, bb);
1120 new_lhs = duplicate_ssa_name (var, phi);
1121 gimple_phi_set_result (phi, new_lhs);
1122 add_new_name_mapping (new_lhs, var);
1124 /* Add VAR to every argument slot of PHI. We need VAR in
1125 every argument so that rewrite_update_phi_arguments knows
1126 which name is this PHI node replacing. If VAR is a
1127 symbol marked for renaming, this is not necessary, the
1128 renamer will use the symbol on the LHS to get its
1129 reaching definition. */
1130 FOR_EACH_EDGE (e, ei, bb->preds)
1131 add_phi_arg (phi, var, e);
1133 else
1135 gcc_assert (DECL_P (var));
1136 phi = create_phi_node (var, bb);
1139 /* Mark this PHI node as interesting for update_ssa. */
1140 set_register_defs (phi, true);
1141 mark_phi_for_rewrite (bb, phi);
1146 /* Insert PHI nodes at the dominance frontier of blocks with variable
1147 definitions. DFS contains the dominance frontier information for
1148 the flowgraph. */
1150 static void
1151 insert_phi_nodes (bitmap *dfs)
1153 referenced_var_iterator rvi;
1154 tree var;
1156 timevar_push (TV_TREE_INSERT_PHI_NODES);
1158 FOR_EACH_REFERENCED_VAR (var, rvi)
1160 struct def_blocks_d *def_map;
1161 bitmap idf;
1163 def_map = find_def_blocks_for (var);
1164 if (def_map == NULL)
1165 continue;
1167 if (get_phi_state (var) != NEED_PHI_STATE_NO)
1169 idf = compute_idf (def_map->def_blocks, dfs);
1170 insert_phi_nodes_for (var, idf, false);
1171 BITMAP_FREE (idf);
1175 timevar_pop (TV_TREE_INSERT_PHI_NODES);
1179 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1180 register DEF (an SSA_NAME) to be a new definition for SYM. */
1182 static void
1183 register_new_def (tree def, tree sym)
1185 tree currdef;
1187 /* If this variable is set in a single basic block and all uses are
1188 dominated by the set(s) in that single basic block, then there is
1189 no reason to record anything for this variable in the block local
1190 definition stacks. Doing so just wastes time and memory.
1192 This is the same test to prune the set of variables which may
1193 need PHI nodes. So we just use that information since it's already
1194 computed and available for us to use. */
1195 if (get_phi_state (sym) == NEED_PHI_STATE_NO)
1197 set_current_def (sym, def);
1198 return;
1201 currdef = get_current_def (sym);
1203 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1204 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1205 in the stack so that we know which symbol is being defined by
1206 this SSA name when we unwind the stack. */
1207 if (currdef && !is_gimple_reg (sym))
1208 VEC_safe_push (tree, heap, block_defs_stack, sym);
1210 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1211 stack is later used by the dominator tree callbacks to restore
1212 the reaching definitions for all the variables defined in the
1213 block after a recursive visit to all its immediately dominated
1214 blocks. If there is no current reaching definition, then just
1215 record the underlying _DECL node. */
1216 VEC_safe_push (tree, heap, block_defs_stack, currdef ? currdef : sym);
1218 /* Set the current reaching definition for SYM to be DEF. */
1219 set_current_def (sym, def);
1223 /* Perform a depth-first traversal of the dominator tree looking for
1224 variables to rename. BB is the block where to start searching.
1225 Renaming is a five step process:
1227 1- Every definition made by PHI nodes at the start of the blocks is
1228 registered as the current definition for the corresponding variable.
1230 2- Every statement in BB is rewritten. USE and VUSE operands are
1231 rewritten with their corresponding reaching definition. DEF and
1232 VDEF targets are registered as new definitions.
1234 3- All the PHI nodes in successor blocks of BB are visited. The
1235 argument corresponding to BB is replaced with its current reaching
1236 definition.
1238 4- Recursively rewrite every dominator child block of BB.
1240 5- Restore (in reverse order) the current reaching definition for every
1241 new definition introduced in this block. This is done so that when
1242 we return from the recursive call, all the current reaching
1243 definitions are restored to the names that were valid in the
1244 dominator parent of BB. */
1246 /* SSA Rewriting Step 1. Initialization, create a block local stack
1247 of reaching definitions for new SSA names produced in this block
1248 (BLOCK_DEFS). Register new definitions for every PHI node in the
1249 block. */
1251 static void
1252 rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1253 basic_block bb)
1255 gimple phi;
1256 gimple_stmt_iterator gsi;
1258 if (dump_file && (dump_flags & TDF_DETAILS))
1259 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1261 /* Mark the unwind point for this block. */
1262 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1264 /* Step 1. Register new definitions for every PHI node in the block.
1265 Conceptually, all the PHI nodes are executed in parallel and each PHI
1266 node introduces a new version for the associated variable. */
1267 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1269 tree result;
1271 phi = gsi_stmt (gsi);
1272 result = gimple_phi_result (phi);
1273 gcc_assert (is_gimple_reg (result));
1274 register_new_def (result, SSA_NAME_VAR (result));
1279 /* Return the current definition for variable VAR. If none is found,
1280 create a new SSA name to act as the zeroth definition for VAR. */
1282 static tree
1283 get_reaching_def (tree var)
1285 tree currdef;
1287 /* Lookup the current reaching definition for VAR. */
1288 currdef = get_current_def (var);
1290 /* If there is no reaching definition for VAR, create and register a
1291 default definition for it (if needed). */
1292 if (currdef == NULL_TREE)
1294 tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1295 currdef = get_default_def_for (sym);
1296 set_current_def (var, currdef);
1299 /* Return the current reaching definition for VAR, or the default
1300 definition, if we had to create one. */
1301 return currdef;
1305 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1306 the block with its immediate reaching definitions. Update the current
1307 definition of a variable when a new real or virtual definition is found. */
1309 static void
1310 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1311 basic_block bb ATTRIBUTE_UNUSED, gimple_stmt_iterator si)
1313 gimple stmt;
1314 use_operand_p use_p;
1315 def_operand_p def_p;
1316 ssa_op_iter iter;
1318 stmt = gsi_stmt (si);
1320 /* If mark_def_sites decided that we don't need to rewrite this
1321 statement, ignore it. */
1322 gcc_assert (blocks_to_update == NULL);
1323 if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1324 return;
1326 if (dump_file && (dump_flags & TDF_DETAILS))
1328 fprintf (dump_file, "Renaming statement ");
1329 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1330 fprintf (dump_file, "\n");
1333 /* Step 1. Rewrite USES in the statement. */
1334 if (rewrite_uses_p (stmt))
1335 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1337 tree var = USE_FROM_PTR (use_p);
1338 gcc_assert (DECL_P (var));
1339 SET_USE (use_p, get_reaching_def (var));
1342 /* Step 2. Register the statement's DEF operands. */
1343 if (register_defs_p (stmt))
1344 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1346 tree var = DEF_FROM_PTR (def_p);
1347 gcc_assert (DECL_P (var));
1348 SET_DEF (def_p, make_ssa_name (var, stmt));
1349 register_new_def (DEF_FROM_PTR (def_p), var);
1354 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1355 PHI nodes. For every PHI node found, add a new argument containing the
1356 current reaching definition for the variable and the edge through which
1357 that definition is reaching the PHI node. */
1359 static void
1360 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1361 basic_block bb)
1363 edge e;
1364 edge_iterator ei;
1366 FOR_EACH_EDGE (e, ei, bb->succs)
1368 gimple phi;
1369 gimple_stmt_iterator gsi;
1371 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
1372 gsi_next (&gsi))
1374 tree currdef;
1375 phi = gsi_stmt (gsi);
1376 currdef = get_reaching_def (SSA_NAME_VAR (gimple_phi_result (phi)));
1377 add_phi_arg (phi, currdef, e);
1383 /* Called after visiting all the statements in basic block BB and all
1384 of its dominator children. Restore CURRDEFS to its original value. */
1386 static void
1387 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1388 basic_block bb ATTRIBUTE_UNUSED)
1390 /* Restore CURRDEFS to its original state. */
1391 while (VEC_length (tree, block_defs_stack) > 0)
1393 tree tmp = VEC_pop (tree, block_defs_stack);
1394 tree saved_def, var;
1396 if (tmp == NULL_TREE)
1397 break;
1399 if (TREE_CODE (tmp) == SSA_NAME)
1401 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1402 current definition of its underlying variable. Note that
1403 if the SSA_NAME is not for a GIMPLE register, the symbol
1404 being defined is stored in the next slot in the stack.
1405 This mechanism is needed because an SSA name for a
1406 non-register symbol may be the definition for more than
1407 one symbol (e.g., SFTs, aliased variables, etc). */
1408 saved_def = tmp;
1409 var = SSA_NAME_VAR (saved_def);
1410 if (!is_gimple_reg (var))
1411 var = VEC_pop (tree, block_defs_stack);
1413 else
1415 /* If we recorded anything else, it must have been a _DECL
1416 node and its current reaching definition must have been
1417 NULL. */
1418 saved_def = NULL;
1419 var = tmp;
1422 set_current_def (var, saved_def);
1427 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1429 void
1430 dump_decl_set (FILE *file, bitmap set)
1432 if (set)
1434 bitmap_iterator bi;
1435 unsigned i;
1437 fprintf (file, "{ ");
1439 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
1441 print_generic_expr (file, referenced_var (i), 0);
1442 fprintf (file, " ");
1445 fprintf (file, "}");
1447 else
1448 fprintf (file, "NIL");
1452 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1454 void
1455 debug_decl_set (bitmap set)
1457 dump_decl_set (stderr, set);
1458 fprintf (stderr, "\n");
1462 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1463 stack up to a maximum of N levels. If N is -1, the whole stack is
1464 dumped. New levels are created when the dominator tree traversal
1465 used for renaming enters a new sub-tree. */
1467 void
1468 dump_defs_stack (FILE *file, int n)
1470 int i, j;
1472 fprintf (file, "\n\nRenaming stack");
1473 if (n > 0)
1474 fprintf (file, " (up to %d levels)", n);
1475 fprintf (file, "\n\n");
1477 i = 1;
1478 fprintf (file, "Level %d (current level)\n", i);
1479 for (j = (int) VEC_length (tree, block_defs_stack) - 1; j >= 0; j--)
1481 tree name, var;
1483 name = VEC_index (tree, block_defs_stack, j);
1484 if (name == NULL_TREE)
1486 i++;
1487 if (n > 0 && i > n)
1488 break;
1489 fprintf (file, "\nLevel %d\n", i);
1490 continue;
1493 if (DECL_P (name))
1495 var = name;
1496 name = NULL_TREE;
1498 else
1500 var = SSA_NAME_VAR (name);
1501 if (!is_gimple_reg (var))
1503 j--;
1504 var = VEC_index (tree, block_defs_stack, j);
1508 fprintf (file, " Previous CURRDEF (");
1509 print_generic_expr (file, var, 0);
1510 fprintf (file, ") = ");
1511 if (name)
1512 print_generic_expr (file, name, 0);
1513 else
1514 fprintf (file, "<NIL>");
1515 fprintf (file, "\n");
1520 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1521 stack up to a maximum of N levels. If N is -1, the whole stack is
1522 dumped. New levels are created when the dominator tree traversal
1523 used for renaming enters a new sub-tree. */
1525 void
1526 debug_defs_stack (int n)
1528 dump_defs_stack (stderr, n);
1532 /* Dump the current reaching definition of every symbol to FILE. */
1534 void
1535 dump_currdefs (FILE *file)
1537 referenced_var_iterator i;
1538 tree var;
1540 fprintf (file, "\n\nCurrent reaching definitions\n\n");
1541 FOR_EACH_REFERENCED_VAR (var, i)
1542 if (SYMS_TO_RENAME (cfun) == NULL
1543 || bitmap_bit_p (SYMS_TO_RENAME (cfun), DECL_UID (var)))
1545 fprintf (file, "CURRDEF (");
1546 print_generic_expr (file, var, 0);
1547 fprintf (file, ") = ");
1548 if (get_current_def (var))
1549 print_generic_expr (file, get_current_def (var), 0);
1550 else
1551 fprintf (file, "<NIL>");
1552 fprintf (file, "\n");
1557 /* Dump the current reaching definition of every symbol to stderr. */
1559 void
1560 debug_currdefs (void)
1562 dump_currdefs (stderr);
1566 /* Dump SSA information to FILE. */
1568 void
1569 dump_tree_ssa (FILE *file)
1571 const char *funcname
1572 = lang_hooks.decl_printable_name (current_function_decl, 2);
1574 fprintf (file, "SSA renaming information for %s\n\n", funcname);
1576 dump_def_blocks (file);
1577 dump_defs_stack (file, -1);
1578 dump_currdefs (file);
1579 dump_tree_ssa_stats (file);
1583 /* Dump SSA information to stderr. */
1585 void
1586 debug_tree_ssa (void)
1588 dump_tree_ssa (stderr);
1592 /* Dump statistics for the hash table HTAB. */
1594 static void
1595 htab_statistics (FILE *file, htab_t htab)
1597 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1598 (long) htab_size (htab),
1599 (long) htab_elements (htab),
1600 htab_collisions (htab));
1604 /* Dump SSA statistics on FILE. */
1606 void
1607 dump_tree_ssa_stats (FILE *file)
1609 if (def_blocks || repl_tbl)
1610 fprintf (file, "\nHash table statistics:\n");
1612 if (def_blocks)
1614 fprintf (file, " def_blocks: ");
1615 htab_statistics (file, def_blocks);
1618 if (repl_tbl)
1620 fprintf (file, " repl_tbl: ");
1621 htab_statistics (file, repl_tbl);
1624 if (def_blocks || repl_tbl)
1625 fprintf (file, "\n");
1629 /* Dump SSA statistics on stderr. */
1631 void
1632 debug_tree_ssa_stats (void)
1634 dump_tree_ssa_stats (stderr);
1638 /* Hashing and equality functions for DEF_BLOCKS. */
1640 static hashval_t
1641 def_blocks_hash (const void *p)
1643 return htab_hash_pointer
1644 ((const void *)((const struct def_blocks_d *)p)->var);
1647 static int
1648 def_blocks_eq (const void *p1, const void *p2)
1650 return ((const struct def_blocks_d *)p1)->var
1651 == ((const struct def_blocks_d *)p2)->var;
1655 /* Free memory allocated by one entry in DEF_BLOCKS. */
1657 static void
1658 def_blocks_free (void *p)
1660 struct def_blocks_d *entry = (struct def_blocks_d *) p;
1661 BITMAP_FREE (entry->def_blocks);
1662 BITMAP_FREE (entry->phi_blocks);
1663 BITMAP_FREE (entry->livein_blocks);
1664 free (entry);
1668 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1670 static int
1671 debug_def_blocks_r (void **slot, void *data)
1673 FILE *file = (FILE *) data;
1674 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1676 fprintf (file, "VAR: ");
1677 print_generic_expr (file, db_p->var, dump_flags);
1678 bitmap_print (file, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1679 bitmap_print (file, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}");
1680 bitmap_print (file, db_p->phi_blocks, ", PHI_BLOCKS: { ", "}\n");
1682 return 1;
1686 /* Dump the DEF_BLOCKS hash table on FILE. */
1688 void
1689 dump_def_blocks (FILE *file)
1691 fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
1692 if (def_blocks)
1693 htab_traverse (def_blocks, debug_def_blocks_r, file);
1697 /* Dump the DEF_BLOCKS hash table on stderr. */
1699 void
1700 debug_def_blocks (void)
1702 dump_def_blocks (stderr);
1706 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1708 static inline void
1709 register_new_update_single (tree new_name, tree old_name)
1711 tree currdef = get_current_def (old_name);
1713 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1714 This stack is later used by the dominator tree callbacks to
1715 restore the reaching definitions for all the variables
1716 defined in the block after a recursive visit to all its
1717 immediately dominated blocks. */
1718 VEC_reserve (tree, heap, block_defs_stack, 2);
1719 VEC_quick_push (tree, block_defs_stack, currdef);
1720 VEC_quick_push (tree, block_defs_stack, old_name);
1722 /* Set the current reaching definition for OLD_NAME to be
1723 NEW_NAME. */
1724 set_current_def (old_name, new_name);
1728 /* Register NEW_NAME to be the new reaching definition for all the
1729 names in OLD_NAMES. Used by the incremental SSA update routines to
1730 replace old SSA names with new ones. */
1732 static inline void
1733 register_new_update_set (tree new_name, bitmap old_names)
1735 bitmap_iterator bi;
1736 unsigned i;
1738 EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1739 register_new_update_single (new_name, ssa_name (i));
1743 /* Initialization of block data structures for the incremental SSA
1744 update pass. Create a block local stack of reaching definitions
1745 for new SSA names produced in this block (BLOCK_DEFS). Register
1746 new definitions for every PHI node in the block. */
1748 static void
1749 rewrite_update_init_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1750 basic_block bb)
1752 edge e;
1753 edge_iterator ei;
1754 bool is_abnormal_phi;
1755 gimple_stmt_iterator gsi;
1757 if (dump_file && (dump_flags & TDF_DETAILS))
1758 fprintf (dump_file, "\n\nRegistering new PHI nodes in block #%d\n\n",
1759 bb->index);
1761 /* Mark the unwind point for this block. */
1762 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1764 if (!bitmap_bit_p (blocks_to_update, bb->index))
1765 return;
1767 /* Mark the LHS if any of the arguments flows through an abnormal
1768 edge. */
1769 is_abnormal_phi = false;
1770 FOR_EACH_EDGE (e, ei, bb->preds)
1771 if (e->flags & EDGE_ABNORMAL)
1773 is_abnormal_phi = true;
1774 break;
1777 /* If any of the PHI nodes is a replacement for a name in
1778 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
1779 register it as a new definition for its corresponding name. Also
1780 register definitions for names whose underlying symbols are
1781 marked for renaming. */
1782 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1784 tree lhs, lhs_sym;
1785 gimple phi = gsi_stmt (gsi);
1787 if (!register_defs_p (phi))
1788 continue;
1790 lhs = gimple_phi_result (phi);
1791 lhs_sym = SSA_NAME_VAR (lhs);
1793 if (symbol_marked_for_renaming (lhs_sym))
1794 register_new_update_single (lhs, lhs_sym);
1795 else
1798 /* If LHS is a new name, register a new definition for all
1799 the names replaced by LHS. */
1800 if (is_new_name (lhs))
1801 register_new_update_set (lhs, names_replaced_by (lhs));
1803 /* If LHS is an OLD name, register it as a new definition
1804 for itself. */
1805 if (is_old_name (lhs))
1806 register_new_update_single (lhs, lhs);
1809 if (is_abnormal_phi)
1810 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
1815 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
1816 the current reaching definition of every name re-written in BB to
1817 the original reaching definition before visiting BB. This
1818 unwinding must be done in the opposite order to what is done in
1819 register_new_update_set. */
1821 static void
1822 rewrite_update_fini_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1823 basic_block bb ATTRIBUTE_UNUSED)
1825 while (VEC_length (tree, block_defs_stack) > 0)
1827 tree var = VEC_pop (tree, block_defs_stack);
1828 tree saved_def;
1830 /* NULL indicates the unwind stop point for this block (see
1831 rewrite_update_init_block). */
1832 if (var == NULL)
1833 return;
1835 saved_def = VEC_pop (tree, block_defs_stack);
1836 set_current_def (var, saved_def);
1841 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1842 it is a symbol marked for renaming, replace it with USE_P's current
1843 reaching definition. */
1845 static inline void
1846 maybe_replace_use (use_operand_p use_p)
1848 tree rdef = NULL_TREE;
1849 tree use = USE_FROM_PTR (use_p);
1850 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1852 if (symbol_marked_for_renaming (sym))
1853 rdef = get_reaching_def (sym);
1854 else if (is_old_name (use))
1855 rdef = get_reaching_def (use);
1857 if (rdef && rdef != use)
1858 SET_USE (use_p, rdef);
1862 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1863 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1864 register it as the current definition for the names replaced by
1865 DEF_P. */
1867 static inline void
1868 maybe_register_def (def_operand_p def_p, gimple stmt)
1870 tree def = DEF_FROM_PTR (def_p);
1871 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
1873 /* If DEF is a naked symbol that needs renaming, create a new
1874 name for it. */
1875 if (symbol_marked_for_renaming (sym))
1877 if (DECL_P (def))
1879 def = make_ssa_name (def, stmt);
1880 SET_DEF (def_p, def);
1883 register_new_update_single (def, sym);
1885 else
1887 /* If DEF is a new name, register it as a new definition
1888 for all the names replaced by DEF. */
1889 if (is_new_name (def))
1890 register_new_update_set (def, names_replaced_by (def));
1892 /* If DEF is an old name, register DEF as a new
1893 definition for itself. */
1894 if (is_old_name (def))
1895 register_new_update_single (def, def);
1900 /* Update every variable used in the statement pointed-to by SI. The
1901 statement is assumed to be in SSA form already. Names in
1902 OLD_SSA_NAMES used by SI will be updated to their current reaching
1903 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1904 will be registered as a new definition for their corresponding name
1905 in OLD_SSA_NAMES. */
1907 static void
1908 rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1909 basic_block bb ATTRIBUTE_UNUSED,
1910 gimple_stmt_iterator si)
1912 gimple stmt;
1913 use_operand_p use_p;
1914 def_operand_p def_p;
1915 ssa_op_iter iter;
1917 stmt = gsi_stmt (si);
1919 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
1921 /* Only update marked statements. */
1922 if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1923 return;
1925 if (dump_file && (dump_flags & TDF_DETAILS))
1927 fprintf (dump_file, "Updating SSA information for statement ");
1928 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1929 fprintf (dump_file, "\n");
1932 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1933 symbol is marked for renaming. */
1934 if (rewrite_uses_p (stmt))
1935 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1936 maybe_replace_use (use_p);
1938 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1939 Also register definitions for names whose underlying symbol is
1940 marked for renaming. */
1941 if (register_defs_p (stmt))
1942 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1943 maybe_register_def (def_p, stmt);
1947 /* Visit all the successor blocks of BB looking for PHI nodes. For
1948 every PHI node found, check if any of its arguments is in
1949 OLD_SSA_NAMES. If so, and if the argument has a current reaching
1950 definition, replace it. */
1952 static void
1953 rewrite_update_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1954 basic_block bb)
1956 edge e;
1957 edge_iterator ei;
1958 unsigned i;
1960 FOR_EACH_EDGE (e, ei, bb->succs)
1962 gimple phi;
1963 gimple_vec phis;
1965 if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
1966 continue;
1968 phis = VEC_index (gimple_vec, phis_to_rewrite, e->dest->index);
1969 for (i = 0; VEC_iterate (gimple, phis, i, phi); i++)
1971 tree arg, lhs_sym;
1972 use_operand_p arg_p;
1974 gcc_assert (rewrite_uses_p (phi));
1976 arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
1977 arg = USE_FROM_PTR (arg_p);
1979 if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
1980 continue;
1982 lhs_sym = SSA_NAME_VAR (gimple_phi_result (phi));
1984 if (arg == NULL_TREE)
1986 /* When updating a PHI node for a recently introduced
1987 symbol we may find NULL arguments. That's why we
1988 take the symbol from the LHS of the PHI node. */
1989 SET_USE (arg_p, get_reaching_def (lhs_sym));
1991 else
1993 tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
1995 if (symbol_marked_for_renaming (sym))
1996 SET_USE (arg_p, get_reaching_def (sym));
1997 else if (is_old_name (arg))
1998 SET_USE (arg_p, get_reaching_def (arg));
2001 if (e->flags & EDGE_ABNORMAL)
2002 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
2008 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2009 form.
2011 ENTRY indicates the block where to start. Every block dominated by
2012 ENTRY will be rewritten.
2014 WHAT indicates what actions will be taken by the renamer (see enum
2015 rewrite_mode).
2017 BLOCKS are the set of interesting blocks for the dominator walker
2018 to process. If this set is NULL, then all the nodes dominated
2019 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2020 are not present in BLOCKS are ignored. */
2022 static void
2023 rewrite_blocks (basic_block entry, enum rewrite_mode what, sbitmap blocks)
2025 struct dom_walk_data walk_data;
2027 /* Rewrite all the basic blocks in the program. */
2028 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
2030 /* Setup callbacks for the generic dominator tree walker. */
2031 memset (&walk_data, 0, sizeof (walk_data));
2033 walk_data.dom_direction = CDI_DOMINATORS;
2034 walk_data.interesting_blocks = blocks;
2036 if (what == REWRITE_ALL)
2037 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
2038 else
2039 walk_data.before_dom_children_before_stmts = rewrite_update_init_block;
2041 if (what == REWRITE_ALL)
2042 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
2043 else if (what == REWRITE_UPDATE)
2044 walk_data.before_dom_children_walk_stmts = rewrite_update_stmt;
2045 else
2046 gcc_unreachable ();
2048 if (what == REWRITE_ALL)
2049 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
2050 else if (what == REWRITE_UPDATE)
2051 walk_data.before_dom_children_after_stmts = rewrite_update_phi_arguments;
2052 else
2053 gcc_unreachable ();
2055 if (what == REWRITE_ALL)
2056 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
2057 else if (what == REWRITE_UPDATE)
2058 walk_data.after_dom_children_after_stmts = rewrite_update_fini_block;
2059 else
2060 gcc_unreachable ();
2062 block_defs_stack = VEC_alloc (tree, heap, 10);
2064 /* Initialize the dominator walker. */
2065 init_walk_dominator_tree (&walk_data);
2067 /* Recursively walk the dominator tree rewriting each statement in
2068 each basic block. */
2069 walk_dominator_tree (&walk_data, entry);
2071 /* Finalize the dominator walker. */
2072 fini_walk_dominator_tree (&walk_data);
2074 /* Debugging dumps. */
2075 if (dump_file && (dump_flags & TDF_STATS))
2077 dump_dfa_stats (dump_file);
2078 if (def_blocks)
2079 dump_tree_ssa_stats (dump_file);
2082 VEC_free (tree, heap, block_defs_stack);
2084 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
2088 /* Block initialization routine for mark_def_sites. Clear the
2089 KILLS bitmap at the start of each block. */
2091 static void
2092 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
2093 basic_block bb ATTRIBUTE_UNUSED)
2095 struct mark_def_sites_global_data *gd;
2096 gd = (struct mark_def_sites_global_data *) walk_data->global_data;
2097 bitmap_clear (gd->kills);
2101 /* Mark the definition site blocks for each variable, so that we know
2102 where the variable is actually live.
2104 INTERESTING_BLOCKS will be filled in with all the blocks that
2105 should be processed by the renamer. It is assumed to be
2106 initialized and zeroed by the caller. */
2108 static void
2109 mark_def_site_blocks (sbitmap interesting_blocks)
2111 struct dom_walk_data walk_data;
2112 struct mark_def_sites_global_data mark_def_sites_global_data;
2114 /* Setup callbacks for the generic dominator tree walker to find and
2115 mark definition sites. */
2116 walk_data.walk_stmts_backward = false;
2117 walk_data.dom_direction = CDI_DOMINATORS;
2118 walk_data.initialize_block_local_data = NULL;
2119 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
2120 walk_data.before_dom_children_walk_stmts = mark_def_sites;
2121 walk_data.before_dom_children_after_stmts = NULL;
2122 walk_data.after_dom_children_before_stmts = NULL;
2123 walk_data.after_dom_children_walk_stmts = NULL;
2124 walk_data.after_dom_children_after_stmts = NULL;
2125 walk_data.interesting_blocks = NULL;
2127 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2128 large enough to accommodate all the variables referenced in the
2129 function, not just the ones we are renaming. */
2130 mark_def_sites_global_data.kills = BITMAP_ALLOC (NULL);
2132 /* Create the set of interesting blocks that will be filled by
2133 mark_def_sites. */
2134 mark_def_sites_global_data.interesting_blocks = interesting_blocks;
2135 walk_data.global_data = &mark_def_sites_global_data;
2137 /* We do not have any local data. */
2138 walk_data.block_local_data_size = 0;
2140 /* Initialize the dominator walker. */
2141 init_walk_dominator_tree (&walk_data);
2143 /* Recursively walk the dominator tree. */
2144 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
2146 /* Finalize the dominator walker. */
2147 fini_walk_dominator_tree (&walk_data);
2149 /* We no longer need this bitmap, clear and free it. */
2150 BITMAP_FREE (mark_def_sites_global_data.kills);
2154 /* Initialize internal data needed during renaming. */
2156 static void
2157 init_ssa_renamer (void)
2159 tree var;
2160 referenced_var_iterator rvi;
2162 cfun->gimple_df->in_ssa_p = false;
2164 /* Allocate memory for the DEF_BLOCKS hash table. */
2165 gcc_assert (def_blocks == NULL);
2166 def_blocks = htab_create (num_referenced_vars, def_blocks_hash,
2167 def_blocks_eq, def_blocks_free);
2169 FOR_EACH_REFERENCED_VAR(var, rvi)
2170 set_current_def (var, NULL_TREE);
2174 /* Deallocate internal data structures used by the renamer. */
2176 static void
2177 fini_ssa_renamer (void)
2179 if (def_blocks)
2181 htab_delete (def_blocks);
2182 def_blocks = NULL;
2185 cfun->gimple_df->in_ssa_p = true;
2188 /* Main entry point into the SSA builder. The renaming process
2189 proceeds in four main phases:
2191 1- Compute dominance frontier and immediate dominators, needed to
2192 insert PHI nodes and rename the function in dominator tree
2193 order.
2195 2- Find and mark all the blocks that define variables
2196 (mark_def_site_blocks).
2198 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2200 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2202 Steps 3 and 4 are done using the dominator tree walker
2203 (walk_dominator_tree). */
2205 static unsigned int
2206 rewrite_into_ssa (void)
2208 bitmap *dfs;
2209 basic_block bb;
2210 sbitmap interesting_blocks;
2212 timevar_push (TV_TREE_SSA_OTHER);
2214 /* Initialize operand data structures. */
2215 init_ssa_operands ();
2217 /* Initialize internal data needed by the renamer. */
2218 init_ssa_renamer ();
2220 /* Initialize the set of interesting blocks. The callback
2221 mark_def_sites will add to this set those blocks that the renamer
2222 should process. */
2223 interesting_blocks = sbitmap_alloc (last_basic_block);
2224 sbitmap_zero (interesting_blocks);
2226 /* Initialize dominance frontier. */
2227 dfs = XNEWVEC (bitmap, last_basic_block);
2228 FOR_EACH_BB (bb)
2229 dfs[bb->index] = BITMAP_ALLOC (NULL);
2231 /* 1- Compute dominance frontiers. */
2232 calculate_dominance_info (CDI_DOMINATORS);
2233 compute_dominance_frontiers (dfs);
2235 /* 2- Find and mark definition sites. */
2236 mark_def_site_blocks (interesting_blocks);
2238 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2239 insert_phi_nodes (dfs);
2241 /* 4- Rename all the blocks. */
2242 rewrite_blocks (ENTRY_BLOCK_PTR, REWRITE_ALL, interesting_blocks);
2244 /* Free allocated memory. */
2245 FOR_EACH_BB (bb)
2246 BITMAP_FREE (dfs[bb->index]);
2247 free (dfs);
2248 sbitmap_free (interesting_blocks);
2250 fini_ssa_renamer ();
2252 timevar_pop (TV_TREE_SSA_OTHER);
2253 return 0;
2257 struct gimple_opt_pass pass_build_ssa =
2260 GIMPLE_PASS,
2261 "ssa", /* name */
2262 NULL, /* gate */
2263 rewrite_into_ssa, /* execute */
2264 NULL, /* sub */
2265 NULL, /* next */
2266 0, /* static_pass_number */
2267 TV_NONE, /* tv_id */
2268 PROP_cfg | PROP_referenced_vars, /* properties_required */
2269 PROP_ssa, /* properties_provided */
2270 0, /* properties_destroyed */
2271 0, /* todo_flags_start */
2272 TODO_dump_func
2273 | TODO_update_ssa_only_virtuals
2274 | TODO_verify_ssa
2275 | TODO_remove_unused_locals /* todo_flags_finish */
2280 /* Mark the definition of VAR at STMT and BB as interesting for the
2281 renamer. BLOCKS is the set of blocks that need updating. */
2283 static void
2284 mark_def_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
2286 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
2287 set_register_defs (stmt, true);
2289 if (insert_phi_p)
2291 bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI;
2293 set_def_block (var, bb, is_phi_p);
2295 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2296 site for both itself and all the old names replaced by it. */
2297 if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2299 bitmap_iterator bi;
2300 unsigned i;
2301 bitmap set = names_replaced_by (var);
2302 if (set)
2303 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2304 set_def_block (ssa_name (i), bb, is_phi_p);
2310 /* Mark the use of VAR at STMT and BB as interesting for the
2311 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2312 nodes. */
2314 static inline void
2315 mark_use_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
2317 basic_block def_bb = gimple_bb (stmt);
2319 mark_block_for_update (def_bb);
2320 mark_block_for_update (bb);
2322 if (gimple_code (stmt) == GIMPLE_PHI)
2323 mark_phi_for_rewrite (def_bb, stmt);
2324 else
2325 set_rewrite_uses (stmt, true);
2327 /* If VAR has not been defined in BB, then it is live-on-entry
2328 to BB. Note that we cannot just use the block holding VAR's
2329 definition because if VAR is one of the names in OLD_SSA_NAMES,
2330 it will have several definitions (itself and all the names that
2331 replace it). */
2332 if (insert_phi_p)
2334 struct def_blocks_d *db_p = get_def_blocks_for (var);
2335 if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2336 set_livein_block (var, bb);
2341 /* Do a dominator walk starting at BB processing statements that
2342 reference symbols in SYMS_TO_RENAME. This is very similar to
2343 mark_def_sites, but the scan handles statements whose operands may
2344 already be SSA names.
2346 If INSERT_PHI_P is true, mark those uses as live in the
2347 corresponding block. This is later used by the PHI placement
2348 algorithm to make PHI pruning decisions.
2350 FIXME. Most of this would be unnecessary if we could associate a
2351 symbol to all the SSA names that reference it. But that
2352 sounds like it would be expensive to maintain. Still, it
2353 would be interesting to see if it makes better sense to do
2354 that. */
2356 static void
2357 prepare_block_for_update (basic_block bb, bool insert_phi_p)
2359 basic_block son;
2360 gimple_stmt_iterator si;
2361 edge e;
2362 edge_iterator ei;
2364 mark_block_for_update (bb);
2366 /* Process PHI nodes marking interesting those that define or use
2367 the symbols that we are interested in. */
2368 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
2370 gimple phi = gsi_stmt (si);
2371 tree lhs_sym, lhs = gimple_phi_result (phi);
2373 lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
2375 if (!symbol_marked_for_renaming (lhs_sym))
2376 continue;
2378 mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2380 /* Mark the uses in phi nodes as interesting. It would be more correct
2381 to process the arguments of the phi nodes of the successor edges of
2382 BB at the end of prepare_block_for_update, however, that turns out
2383 to be significantly more expensive. Doing it here is conservatively
2384 correct -- it may only cause us to believe a value to be live in a
2385 block that also contains its definition, and thus insert a few more
2386 phi nodes for it. */
2387 FOR_EACH_EDGE (e, ei, bb->preds)
2388 mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
2391 /* Process the statements. */
2392 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2394 gimple stmt;
2395 ssa_op_iter i;
2396 use_operand_p use_p;
2397 def_operand_p def_p;
2399 stmt = gsi_stmt (si);
2401 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
2403 tree use = USE_FROM_PTR (use_p);
2404 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2405 if (symbol_marked_for_renaming (sym))
2406 mark_use_interesting (sym, stmt, bb, insert_phi_p);
2409 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_ALL_DEFS)
2411 tree def = DEF_FROM_PTR (def_p);
2412 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2413 if (symbol_marked_for_renaming (sym))
2414 mark_def_interesting (sym, stmt, bb, insert_phi_p);
2418 /* Now visit all the blocks dominated by BB. */
2419 for (son = first_dom_son (CDI_DOMINATORS, bb);
2420 son;
2421 son = next_dom_son (CDI_DOMINATORS, son))
2422 prepare_block_for_update (son, insert_phi_p);
2426 /* Helper for prepare_names_to_update. Mark all the use sites for
2427 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2428 prepare_names_to_update. */
2430 static void
2431 prepare_use_sites_for (tree name, bool insert_phi_p)
2433 use_operand_p use_p;
2434 imm_use_iterator iter;
2436 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2438 gimple stmt = USE_STMT (use_p);
2439 basic_block bb = gimple_bb (stmt);
2441 if (gimple_code (stmt) == GIMPLE_PHI)
2443 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
2444 edge e = gimple_phi_arg_edge (stmt, ix);
2445 mark_use_interesting (name, stmt, e->src, insert_phi_p);
2447 else
2449 /* For regular statements, mark this as an interesting use
2450 for NAME. */
2451 mark_use_interesting (name, stmt, bb, insert_phi_p);
2457 /* Helper for prepare_names_to_update. Mark the definition site for
2458 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2459 prepare_names_to_update. */
2461 static void
2462 prepare_def_site_for (tree name, bool insert_phi_p)
2464 gimple stmt;
2465 basic_block bb;
2467 gcc_assert (names_to_release == NULL
2468 || !bitmap_bit_p (names_to_release, SSA_NAME_VERSION (name)));
2470 stmt = SSA_NAME_DEF_STMT (name);
2471 bb = gimple_bb (stmt);
2472 if (bb)
2474 gcc_assert (bb->index < last_basic_block);
2475 mark_block_for_update (bb);
2476 mark_def_interesting (name, stmt, bb, insert_phi_p);
2481 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2482 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2483 PHI nodes for newly created names. */
2485 static void
2486 prepare_names_to_update (bool insert_phi_p)
2488 unsigned i = 0;
2489 bitmap_iterator bi;
2490 sbitmap_iterator sbi;
2492 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2493 remove it from NEW_SSA_NAMES so that we don't try to visit its
2494 defining basic block (which most likely doesn't exist). Notice
2495 that we cannot do the same with names in OLD_SSA_NAMES because we
2496 want to replace existing instances. */
2497 if (names_to_release)
2498 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2499 RESET_BIT (new_ssa_names, i);
2501 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2502 names may be considered to be live-in on blocks that contain
2503 definitions for their replacements. */
2504 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2505 prepare_def_site_for (ssa_name (i), insert_phi_p);
2507 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2508 OLD_SSA_NAMES, but we have to ignore its definition site. */
2509 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2511 if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2512 prepare_def_site_for (ssa_name (i), insert_phi_p);
2513 prepare_use_sites_for (ssa_name (i), insert_phi_p);
2518 /* Dump all the names replaced by NAME to FILE. */
2520 void
2521 dump_names_replaced_by (FILE *file, tree name)
2523 unsigned i;
2524 bitmap old_set;
2525 bitmap_iterator bi;
2527 print_generic_expr (file, name, 0);
2528 fprintf (file, " -> { ");
2530 old_set = names_replaced_by (name);
2531 EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2533 print_generic_expr (file, ssa_name (i), 0);
2534 fprintf (file, " ");
2537 fprintf (file, "}\n");
2541 /* Dump all the names replaced by NAME to stderr. */
2543 void
2544 debug_names_replaced_by (tree name)
2546 dump_names_replaced_by (stderr, name);
2550 /* Dump SSA update information to FILE. */
2552 void
2553 dump_update_ssa (FILE *file)
2555 unsigned i = 0;
2556 bitmap_iterator bi;
2558 if (!need_ssa_update_p (cfun))
2559 return;
2561 if (new_ssa_names && sbitmap_first_set_bit (new_ssa_names) >= 0)
2563 sbitmap_iterator sbi;
2565 fprintf (file, "\nSSA replacement table\n");
2566 fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2567 "O_1, ..., O_j\n\n");
2569 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2570 dump_names_replaced_by (file, ssa_name (i));
2572 fprintf (file, "\n");
2573 fprintf (file, "Number of virtual NEW -> OLD mappings: %7u\n",
2574 update_ssa_stats.num_virtual_mappings);
2575 fprintf (file, "Number of real NEW -> OLD mappings: %7u\n",
2576 update_ssa_stats.num_total_mappings
2577 - update_ssa_stats.num_virtual_mappings);
2578 fprintf (file, "Number of total NEW -> OLD mappings: %7u\n",
2579 update_ssa_stats.num_total_mappings);
2581 fprintf (file, "\nNumber of virtual symbols: %u\n",
2582 update_ssa_stats.num_virtual_symbols);
2585 if (!bitmap_empty_p (SYMS_TO_RENAME (cfun)))
2587 fprintf (file, "\n\nSymbols to be put in SSA form\n\n");
2588 dump_decl_set (file, SYMS_TO_RENAME (cfun));
2589 fprintf (file, "\n");
2592 if (names_to_release && !bitmap_empty_p (names_to_release))
2594 fprintf (file, "\n\nSSA names to release after updating the SSA web\n\n");
2595 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2597 print_generic_expr (file, ssa_name (i), 0);
2598 fprintf (file, " ");
2602 fprintf (file, "\n\n");
2606 /* Dump SSA update information to stderr. */
2608 void
2609 debug_update_ssa (void)
2611 dump_update_ssa (stderr);
2615 /* Initialize data structures used for incremental SSA updates. */
2617 static void
2618 init_update_ssa (struct function *fn)
2620 /* Reserve more space than the current number of names. The calls to
2621 add_new_name_mapping are typically done after creating new SSA
2622 names, so we'll need to reallocate these arrays. */
2623 old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2624 sbitmap_zero (old_ssa_names);
2626 new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2627 sbitmap_zero (new_ssa_names);
2629 repl_tbl = htab_create (20, repl_map_hash, repl_map_eq, repl_map_free);
2630 names_to_release = NULL;
2631 memset (&update_ssa_stats, 0, sizeof (update_ssa_stats));
2632 update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL);
2633 update_ssa_initialized_fn = fn;
2637 /* Deallocate data structures used for incremental SSA updates. */
2639 void
2640 delete_update_ssa (void)
2642 unsigned i;
2643 bitmap_iterator bi;
2645 sbitmap_free (old_ssa_names);
2646 old_ssa_names = NULL;
2648 sbitmap_free (new_ssa_names);
2649 new_ssa_names = NULL;
2651 htab_delete (repl_tbl);
2652 repl_tbl = NULL;
2654 bitmap_clear (SYMS_TO_RENAME (update_ssa_initialized_fn));
2655 BITMAP_FREE (update_ssa_stats.virtual_symbols);
2657 if (names_to_release)
2659 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2660 release_ssa_name (ssa_name (i));
2661 BITMAP_FREE (names_to_release);
2664 clear_ssa_name_info ();
2666 fini_ssa_renamer ();
2668 if (blocks_with_phis_to_rewrite)
2669 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
2671 gimple_vec phis = VEC_index (gimple_vec, phis_to_rewrite, i);
2673 VEC_free (gimple, heap, phis);
2674 VEC_replace (gimple_vec, phis_to_rewrite, i, NULL);
2677 BITMAP_FREE (blocks_with_phis_to_rewrite);
2678 BITMAP_FREE (blocks_to_update);
2679 update_ssa_initialized_fn = NULL;
2683 /* Create a new name for OLD_NAME in statement STMT and replace the
2684 operand pointed to by DEF_P with the newly created name. Return
2685 the new name and register the replacement mapping <NEW, OLD> in
2686 update_ssa's tables. */
2688 tree
2689 create_new_def_for (tree old_name, gimple stmt, def_operand_p def)
2691 tree new_name = duplicate_ssa_name (old_name, stmt);
2693 SET_DEF (def, new_name);
2695 if (gimple_code (stmt) == GIMPLE_PHI)
2697 edge e;
2698 edge_iterator ei;
2699 basic_block bb = gimple_bb (stmt);
2701 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2702 FOR_EACH_EDGE (e, ei, bb->preds)
2703 if (e->flags & EDGE_ABNORMAL)
2705 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
2706 break;
2710 register_new_name_mapping (new_name, old_name);
2712 /* For the benefit of passes that will be updating the SSA form on
2713 their own, set the current reaching definition of OLD_NAME to be
2714 NEW_NAME. */
2715 set_current_def (old_name, new_name);
2717 return new_name;
2721 /* Register name NEW to be a replacement for name OLD. This function
2722 must be called for every replacement that should be performed by
2723 update_ssa. */
2725 void
2726 register_new_name_mapping (tree new_tree, tree old)
2728 if (!update_ssa_initialized_fn)
2729 init_update_ssa (cfun);
2731 gcc_assert (update_ssa_initialized_fn == cfun);
2733 add_new_name_mapping (new_tree, old);
2737 /* Register symbol SYM to be renamed by update_ssa. */
2739 void
2740 mark_sym_for_renaming (tree sym)
2742 bitmap_set_bit (SYMS_TO_RENAME (cfun), DECL_UID (sym));
2746 /* Register all the symbols in SET to be renamed by update_ssa. */
2748 void
2749 mark_set_for_renaming (bitmap set)
2751 bitmap_iterator bi;
2752 unsigned i;
2754 if (set == NULL || bitmap_empty_p (set))
2755 return;
2757 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2758 mark_sym_for_renaming (referenced_var (i));
2762 /* Return true if there is any work to be done by update_ssa
2763 for function FN. */
2765 bool
2766 need_ssa_update_p (struct function *fn)
2768 gcc_assert (fn != NULL);
2769 return (update_ssa_initialized_fn == fn
2770 || (fn->gimple_df
2771 && !bitmap_empty_p (SYMS_TO_RENAME (fn))));
2774 /* Return true if SSA name mappings have been registered for SSA updating. */
2776 bool
2777 name_mappings_registered_p (void)
2779 if (!update_ssa_initialized_fn)
2780 return false;
2782 gcc_assert (update_ssa_initialized_fn == cfun);
2784 return repl_tbl && htab_elements (repl_tbl) > 0;
2787 /* Return true if name N has been registered in the replacement table. */
2789 bool
2790 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED)
2792 if (!update_ssa_initialized_fn)
2793 return false;
2795 gcc_assert (update_ssa_initialized_fn == cfun);
2797 return is_new_name (n) || is_old_name (n);
2801 /* Return the set of all the SSA names marked to be replaced. */
2803 bitmap
2804 ssa_names_to_replace (void)
2806 unsigned i = 0;
2807 bitmap ret;
2808 sbitmap_iterator sbi;
2810 gcc_assert (update_ssa_initialized_fn == NULL
2811 || update_ssa_initialized_fn == cfun);
2813 ret = BITMAP_ALLOC (NULL);
2814 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2815 bitmap_set_bit (ret, i);
2817 return ret;
2821 /* Mark NAME to be released after update_ssa has finished. */
2823 void
2824 release_ssa_name_after_update_ssa (tree name)
2826 gcc_assert (cfun && update_ssa_initialized_fn == cfun);
2828 if (names_to_release == NULL)
2829 names_to_release = BITMAP_ALLOC (NULL);
2831 bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
2835 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2836 frontier information. BLOCKS is the set of blocks to be updated.
2838 This is slightly different than the regular PHI insertion
2839 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
2840 real names (i.e., GIMPLE registers) are inserted:
2842 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2843 nodes inside the region affected by the block that defines VAR
2844 and the blocks that define all its replacements. All these
2845 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2847 First, we compute the entry point to the region (ENTRY). This is
2848 given by the nearest common dominator to all the definition
2849 blocks. When computing the iterated dominance frontier (IDF), any
2850 block not strictly dominated by ENTRY is ignored.
2852 We then call the standard PHI insertion algorithm with the pruned
2853 IDF.
2855 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
2856 names is not pruned. PHI nodes are inserted at every IDF block. */
2858 static void
2859 insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks,
2860 unsigned update_flags)
2862 basic_block entry;
2863 struct def_blocks_d *db;
2864 bitmap idf, pruned_idf;
2865 bitmap_iterator bi;
2866 unsigned i;
2868 #if defined ENABLE_CHECKING
2869 if (TREE_CODE (var) == SSA_NAME)
2870 gcc_assert (is_old_name (var));
2871 else
2872 gcc_assert (symbol_marked_for_renaming (var));
2873 #endif
2875 /* Get all the definition sites for VAR. */
2876 db = find_def_blocks_for (var);
2878 /* No need to do anything if there were no definitions to VAR. */
2879 if (db == NULL || bitmap_empty_p (db->def_blocks))
2880 return;
2882 /* Compute the initial iterated dominance frontier. */
2883 idf = compute_idf (db->def_blocks, dfs);
2884 pruned_idf = BITMAP_ALLOC (NULL);
2886 if (TREE_CODE (var) == SSA_NAME)
2888 if (update_flags == TODO_update_ssa)
2890 /* If doing regular SSA updates for GIMPLE registers, we are
2891 only interested in IDF blocks dominated by the nearest
2892 common dominator of all the definition blocks. */
2893 entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
2894 db->def_blocks);
2895 if (entry != ENTRY_BLOCK_PTR)
2896 EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
2897 if (BASIC_BLOCK (i) != entry
2898 && dominated_by_p (CDI_DOMINATORS, BASIC_BLOCK (i), entry))
2899 bitmap_set_bit (pruned_idf, i);
2901 else
2903 /* Otherwise, do not prune the IDF for VAR. */
2904 gcc_assert (update_flags == TODO_update_ssa_full_phi);
2905 bitmap_copy (pruned_idf, idf);
2908 else
2910 /* Otherwise, VAR is a symbol that needs to be put into SSA form
2911 for the first time, so we need to compute the full IDF for
2912 it. */
2913 bitmap_copy (pruned_idf, idf);
2916 if (!bitmap_empty_p (pruned_idf))
2918 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
2919 are included in the region to be updated. The feeding blocks
2920 are important to guarantee that the PHI arguments are renamed
2921 properly. */
2923 /* FIXME, this is not needed if we are updating symbols. We are
2924 already starting at the ENTRY block anyway. */
2925 bitmap_ior_into (blocks, pruned_idf);
2926 EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
2928 edge e;
2929 edge_iterator ei;
2930 basic_block bb = BASIC_BLOCK (i);
2932 FOR_EACH_EDGE (e, ei, bb->preds)
2933 if (e->src->index >= 0)
2934 bitmap_set_bit (blocks, e->src->index);
2937 insert_phi_nodes_for (var, pruned_idf, true);
2940 BITMAP_FREE (pruned_idf);
2941 BITMAP_FREE (idf);
2945 /* Heuristic to determine whether SSA name mappings for virtual names
2946 should be discarded and their symbols rewritten from scratch. When
2947 there is a large number of mappings for virtual names, the
2948 insertion of PHI nodes for the old names in the mappings takes
2949 considerable more time than if we inserted PHI nodes for the
2950 symbols instead.
2952 Currently the heuristic takes these stats into account:
2954 - Number of mappings for virtual SSA names.
2955 - Number of distinct virtual symbols involved in those mappings.
2957 If the number of virtual mappings is much larger than the number of
2958 virtual symbols, then it will be faster to compute PHI insertion
2959 spots for the symbols. Even if this involves traversing the whole
2960 CFG, which is what happens when symbols are renamed from scratch. */
2962 static bool
2963 switch_virtuals_to_full_rewrite_p (void)
2965 if (update_ssa_stats.num_virtual_mappings < (unsigned) MIN_VIRTUAL_MAPPINGS)
2966 return false;
2968 if (update_ssa_stats.num_virtual_mappings
2969 > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
2970 * update_ssa_stats.num_virtual_symbols)
2971 return true;
2973 return false;
2977 /* Remove every virtual mapping and mark all the affected virtual
2978 symbols for renaming. */
2980 static void
2981 switch_virtuals_to_full_rewrite (void)
2983 unsigned i = 0;
2984 sbitmap_iterator sbi;
2986 if (dump_file)
2988 fprintf (dump_file, "\nEnabled virtual name mapping heuristic.\n");
2989 fprintf (dump_file, "\tNumber of virtual mappings: %7u\n",
2990 update_ssa_stats.num_virtual_mappings);
2991 fprintf (dump_file, "\tNumber of unique virtual symbols: %7u\n",
2992 update_ssa_stats.num_virtual_symbols);
2993 fprintf (dump_file, "Updating FUD-chains from top of CFG will be "
2994 "faster than processing\nthe name mappings.\n\n");
2997 /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
2998 Note that it is not really necessary to remove the mappings from
2999 REPL_TBL, that would only waste time. */
3000 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
3001 if (!is_gimple_reg (ssa_name (i)))
3002 RESET_BIT (new_ssa_names, i);
3004 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3005 if (!is_gimple_reg (ssa_name (i)))
3006 RESET_BIT (old_ssa_names, i);
3008 mark_set_for_renaming (update_ssa_stats.virtual_symbols);
3012 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3013 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3015 1- The names in OLD_SSA_NAMES dominated by the definitions of
3016 NEW_SSA_NAMES are all re-written to be reached by the
3017 appropriate definition from NEW_SSA_NAMES.
3019 2- If needed, new PHI nodes are added to the iterated dominance
3020 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3022 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3023 calling register_new_name_mapping for every pair of names that the
3024 caller wants to replace.
3026 The caller identifies the new names that have been inserted and the
3027 names that need to be replaced by calling register_new_name_mapping
3028 for every pair <NEW, OLD>. Note that the function assumes that the
3029 new names have already been inserted in the IL.
3031 For instance, given the following code:
3033 1 L0:
3034 2 x_1 = PHI (0, x_5)
3035 3 if (x_1 < 10)
3036 4 if (x_1 > 7)
3037 5 y_2 = 0
3038 6 else
3039 7 y_3 = x_1 + x_7
3040 8 endif
3041 9 x_5 = x_1 + 1
3042 10 goto L0;
3043 11 endif
3045 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3047 1 L0:
3048 2 x_1 = PHI (0, x_5)
3049 3 if (x_1 < 10)
3050 4 x_10 = ...
3051 5 if (x_1 > 7)
3052 6 y_2 = 0
3053 7 else
3054 8 x_11 = ...
3055 9 y_3 = x_1 + x_7
3056 10 endif
3057 11 x_5 = x_1 + 1
3058 12 goto L0;
3059 13 endif
3061 We want to replace all the uses of x_1 with the new definitions of
3062 x_10 and x_11. Note that the only uses that should be replaced are
3063 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3064 *not* be replaced (this is why we cannot just mark symbol 'x' for
3065 renaming).
3067 Additionally, we may need to insert a PHI node at line 11 because
3068 that is a merge point for x_10 and x_11. So the use of x_1 at line
3069 11 will be replaced with the new PHI node. The insertion of PHI
3070 nodes is optional. They are not strictly necessary to preserve the
3071 SSA form, and depending on what the caller inserted, they may not
3072 even be useful for the optimizers. UPDATE_FLAGS controls various
3073 aspects of how update_ssa operates, see the documentation for
3074 TODO_update_ssa*. */
3076 void
3077 update_ssa (unsigned update_flags)
3079 basic_block bb, start_bb;
3080 bitmap_iterator bi;
3081 unsigned i = 0;
3082 sbitmap tmp;
3083 bool insert_phi_p;
3084 sbitmap_iterator sbi;
3086 if (!need_ssa_update_p (cfun))
3087 return;
3089 timevar_push (TV_TREE_SSA_INCREMENTAL);
3091 if (!update_ssa_initialized_fn)
3092 init_update_ssa (cfun);
3093 gcc_assert (update_ssa_initialized_fn == cfun);
3095 blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
3096 if (!phis_to_rewrite)
3097 phis_to_rewrite = VEC_alloc (gimple_vec, heap, last_basic_block);
3098 blocks_to_update = BITMAP_ALLOC (NULL);
3100 /* Ensure that the dominance information is up-to-date. */
3101 calculate_dominance_info (CDI_DOMINATORS);
3103 /* Only one update flag should be set. */
3104 gcc_assert (update_flags == TODO_update_ssa
3105 || update_flags == TODO_update_ssa_no_phi
3106 || update_flags == TODO_update_ssa_full_phi
3107 || update_flags == TODO_update_ssa_only_virtuals);
3109 /* If we only need to update virtuals, remove all the mappings for
3110 real names before proceeding. The caller is responsible for
3111 having dealt with the name mappings before calling update_ssa. */
3112 if (update_flags == TODO_update_ssa_only_virtuals)
3114 sbitmap_zero (old_ssa_names);
3115 sbitmap_zero (new_ssa_names);
3116 htab_empty (repl_tbl);
3119 insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
3121 if (insert_phi_p)
3123 /* If the caller requested PHI nodes to be added, initialize
3124 live-in information data structures (DEF_BLOCKS). */
3126 /* For each SSA name N, the DEF_BLOCKS table describes where the
3127 name is defined, which blocks have PHI nodes for N, and which
3128 blocks have uses of N (i.e., N is live-on-entry in those
3129 blocks). */
3130 def_blocks = htab_create (num_ssa_names, def_blocks_hash,
3131 def_blocks_eq, def_blocks_free);
3133 else
3135 def_blocks = NULL;
3138 /* Heuristic to avoid massive slow downs when the replacement
3139 mappings include lots of virtual names. */
3140 if (insert_phi_p && switch_virtuals_to_full_rewrite_p ())
3141 switch_virtuals_to_full_rewrite ();
3143 /* If there are names defined in the replacement table, prepare
3144 definition and use sites for all the names in NEW_SSA_NAMES and
3145 OLD_SSA_NAMES. */
3146 if (sbitmap_first_set_bit (new_ssa_names) >= 0)
3148 prepare_names_to_update (insert_phi_p);
3150 /* If all the names in NEW_SSA_NAMES had been marked for
3151 removal, and there are no symbols to rename, then there's
3152 nothing else to do. */
3153 if (sbitmap_first_set_bit (new_ssa_names) < 0
3154 && bitmap_empty_p (SYMS_TO_RENAME (cfun)))
3155 goto done;
3158 /* Next, determine the block at which to start the renaming process. */
3159 if (!bitmap_empty_p (SYMS_TO_RENAME (cfun)))
3161 /* If we have to rename some symbols from scratch, we need to
3162 start the process at the root of the CFG. FIXME, it should
3163 be possible to determine the nearest block that had a
3164 definition for each of the symbols that are marked for
3165 updating. For now this seems more work than it's worth. */
3166 start_bb = ENTRY_BLOCK_PTR;
3168 /* Traverse the CFG looking for existing definitions and uses of
3169 symbols in SYMS_TO_RENAME. Mark interesting blocks and
3170 statements and set local live-in information for the PHI
3171 placement heuristics. */
3172 prepare_block_for_update (start_bb, insert_phi_p);
3174 else
3176 /* Otherwise, the entry block to the region is the nearest
3177 common dominator for the blocks in BLOCKS. */
3178 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3179 blocks_to_update);
3182 /* If requested, insert PHI nodes at the iterated dominance frontier
3183 of every block, creating new definitions for names in OLD_SSA_NAMES
3184 and for symbols in SYMS_TO_RENAME. */
3185 if (insert_phi_p)
3187 bitmap *dfs;
3189 /* If the caller requested PHI nodes to be added, compute
3190 dominance frontiers. */
3191 dfs = XNEWVEC (bitmap, last_basic_block);
3192 FOR_EACH_BB (bb)
3193 dfs[bb->index] = BITMAP_ALLOC (NULL);
3194 compute_dominance_frontiers (dfs);
3196 if (sbitmap_first_set_bit (old_ssa_names) >= 0)
3198 sbitmap_iterator sbi;
3200 /* insert_update_phi_nodes_for will call add_new_name_mapping
3201 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3202 will grow while we are traversing it (but it will not
3203 gain any new members). Copy OLD_SSA_NAMES to a temporary
3204 for traversal. */
3205 sbitmap tmp = sbitmap_alloc (old_ssa_names->n_bits);
3206 sbitmap_copy (tmp, old_ssa_names);
3207 EXECUTE_IF_SET_IN_SBITMAP (tmp, 0, i, sbi)
3208 insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
3209 update_flags);
3210 sbitmap_free (tmp);
3213 EXECUTE_IF_SET_IN_BITMAP (SYMS_TO_RENAME (cfun), 0, i, bi)
3214 insert_updated_phi_nodes_for (referenced_var (i), dfs, blocks_to_update,
3215 update_flags);
3217 FOR_EACH_BB (bb)
3218 BITMAP_FREE (dfs[bb->index]);
3219 free (dfs);
3221 /* Insertion of PHI nodes may have added blocks to the region.
3222 We need to re-compute START_BB to include the newly added
3223 blocks. */
3224 if (start_bb != ENTRY_BLOCK_PTR)
3225 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3226 blocks_to_update);
3229 /* Reset the current definition for name and symbol before renaming
3230 the sub-graph. */
3231 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3232 set_current_def (ssa_name (i), NULL_TREE);
3234 EXECUTE_IF_SET_IN_BITMAP (SYMS_TO_RENAME (cfun), 0, i, bi)
3235 set_current_def (referenced_var (i), NULL_TREE);
3237 /* Now start the renaming process at START_BB. */
3238 tmp = sbitmap_alloc (last_basic_block);
3239 sbitmap_zero (tmp);
3240 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3241 SET_BIT (tmp, i);
3243 rewrite_blocks (start_bb, REWRITE_UPDATE, tmp);
3245 sbitmap_free (tmp);
3247 /* Debugging dumps. */
3248 if (dump_file)
3250 int c;
3251 unsigned i;
3253 dump_update_ssa (dump_file);
3255 fprintf (dump_file, "Incremental SSA update started at block: %d\n\n",
3256 start_bb->index);
3258 c = 0;
3259 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3260 c++;
3261 fprintf (dump_file, "Number of blocks in CFG: %d\n", last_basic_block);
3262 fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n\n",
3263 c, PERCENT (c, last_basic_block));
3265 if (dump_flags & TDF_DETAILS)
3267 fprintf (dump_file, "Affected blocks: ");
3268 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3269 fprintf (dump_file, "%u ", i);
3270 fprintf (dump_file, "\n");
3273 fprintf (dump_file, "\n\n");
3276 /* Free allocated memory. */
3277 done:
3278 delete_update_ssa ();
3280 timevar_pop (TV_TREE_SSA_INCREMENTAL);