* sv.po: Update.
[official-gcc.git] / gcc / tree-into-ssa.c
blob41e88eb2118729196ccb1624796c53bc73a0e611
1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
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 "tm_p.h"
29 #include "langhooks.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "function.h"
33 #include "tree-pretty-print.h"
34 #include "gimple-pretty-print.h"
35 #include "bitmap.h"
36 #include "tree-flow.h"
37 #include "gimple.h"
38 #include "tree-inline.h"
39 #include "timevar.h"
40 #include "hashtab.h"
41 #include "tree-dump.h"
42 #include "tree-pass.h"
43 #include "cfgloop.h"
44 #include "domwalk.h"
45 #include "params.h"
46 #include "vecprim.h"
49 /* This file builds the SSA form for a function as described in:
50 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
51 Computing Static Single Assignment Form and the Control Dependence
52 Graph. ACM Transactions on Programming Languages and Systems,
53 13(4):451-490, October 1991. */
55 /* Structure to map a variable VAR to the set of blocks that contain
56 definitions for VAR. */
57 struct def_blocks_d
59 /* The variable. */
60 tree var;
62 /* Blocks that contain definitions of VAR. Bit I will be set if the
63 Ith block contains a definition of VAR. */
64 bitmap def_blocks;
66 /* Blocks that contain a PHI node for VAR. */
67 bitmap phi_blocks;
69 /* Blocks where VAR is live-on-entry. Similar semantics as
70 DEF_BLOCKS. */
71 bitmap livein_blocks;
75 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
76 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
77 basic blocks where VAR is defined (assigned a new value). It also
78 contains a bitmap of all the blocks where VAR is live-on-entry
79 (i.e., there is a use of VAR in block B without a preceding
80 definition in B). The live-on-entry information is used when
81 computing PHI pruning heuristics. */
82 static htab_t def_blocks;
84 /* Stack of trees used to restore the global currdefs to its original
85 state after completing rewriting of a block and its dominator
86 children. Its elements have the following properties:
88 - An SSA_NAME (N) indicates that the current definition of the
89 underlying variable should be set to the given SSA_NAME. If the
90 symbol associated with the SSA_NAME is not a GIMPLE register, the
91 next slot in the stack must be a _DECL node (SYM). In this case,
92 the name N in the previous slot is the current reaching
93 definition for SYM.
95 - A _DECL node indicates that the underlying variable has no
96 current definition.
98 - A NULL node at the top entry is used to mark the last slot
99 associated with the current block. */
100 static VEC(tree,heap) *block_defs_stack;
103 /* Set of existing SSA names being replaced by update_ssa. */
104 static sbitmap old_ssa_names;
106 /* Set of new SSA names being added by update_ssa. Note that both
107 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
108 the operations done on them are presence tests. */
109 static sbitmap new_ssa_names;
111 sbitmap interesting_blocks;
113 /* Set of SSA names that have been marked to be released after they
114 were registered in the replacement table. They will be finally
115 released after we finish updating the SSA web. */
116 static bitmap names_to_release;
118 static VEC(gimple_vec, heap) *phis_to_rewrite;
120 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
121 static bitmap blocks_with_phis_to_rewrite;
123 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
124 to grow as the callers to register_new_name_mapping will typically
125 create new names on the fly. FIXME. Currently set to 1/3 to avoid
126 frequent reallocations but still need to find a reasonable growth
127 strategy. */
128 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
130 /* Tuple used to represent replacement mappings. */
131 struct repl_map_d
133 tree name;
134 bitmap set;
138 /* NEW -> OLD_SET replacement table. If we are replacing several
139 existing SSA names O_1, O_2, ..., O_j with a new name N_i,
140 then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }. */
141 static htab_t repl_tbl;
143 /* The function the SSA updating data structures have been initialized for.
144 NULL if they need to be initialized by register_new_name_mapping. */
145 static struct function *update_ssa_initialized_fn = NULL;
147 /* Statistics kept by update_ssa to use in the virtual mapping
148 heuristic. If the number of virtual mappings is beyond certain
149 threshold, the updater will switch from using the mappings into
150 renaming the virtual symbols from scratch. In some cases, the
151 large number of name mappings for virtual names causes significant
152 slowdowns in the PHI insertion code. */
153 struct update_ssa_stats_d
155 unsigned num_virtual_mappings;
156 unsigned num_total_mappings;
157 bitmap virtual_symbols;
158 unsigned num_virtual_symbols;
160 static struct update_ssa_stats_d update_ssa_stats;
162 /* Global data to attach to the main dominator walk structure. */
163 struct mark_def_sites_global_data
165 /* This bitmap contains the variables which are set before they
166 are used in a basic block. */
167 bitmap kills;
171 /* Information stored for SSA names. */
172 struct ssa_name_info
174 /* The current reaching definition replacing this SSA name. */
175 tree current_def;
177 /* This field indicates whether or not the variable may need PHI nodes.
178 See the enum's definition for more detailed information about the
179 states. */
180 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
182 /* Age of this record (so that info_for_ssa_name table can be cleared
183 quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
184 are assumed to be null. */
185 unsigned age;
188 /* The information associated with names. */
189 typedef struct ssa_name_info *ssa_name_info_p;
190 DEF_VEC_P (ssa_name_info_p);
191 DEF_VEC_ALLOC_P (ssa_name_info_p, heap);
193 static VEC(ssa_name_info_p, heap) *info_for_ssa_name;
194 static unsigned current_info_for_ssa_name_age;
196 /* The set of blocks affected by update_ssa. */
197 static bitmap blocks_to_update;
199 /* The main entry point to the SSA renamer (rewrite_blocks) may be
200 called several times to do different, but related, tasks.
201 Initially, we need it to rename the whole program into SSA form.
202 At other times, we may need it to only rename into SSA newly
203 exposed symbols. Finally, we can also call it to incrementally fix
204 an already built SSA web. */
205 enum rewrite_mode {
206 /* Convert the whole function into SSA form. */
207 REWRITE_ALL,
209 /* Incrementally update the SSA web by replacing existing SSA
210 names with new ones. See update_ssa for details. */
211 REWRITE_UPDATE
217 /* Prototypes for debugging functions. */
218 extern void dump_tree_ssa (FILE *);
219 extern void debug_tree_ssa (void);
220 extern void debug_def_blocks (void);
221 extern void dump_tree_ssa_stats (FILE *);
222 extern void debug_tree_ssa_stats (void);
223 extern void dump_update_ssa (FILE *);
224 extern void debug_update_ssa (void);
225 extern void dump_names_replaced_by (FILE *, tree);
226 extern void debug_names_replaced_by (tree);
227 extern void dump_def_blocks (FILE *);
228 extern void debug_def_blocks (void);
229 extern void dump_defs_stack (FILE *, int);
230 extern void debug_defs_stack (int);
231 extern void dump_currdefs (FILE *);
232 extern void debug_currdefs (void);
234 /* Return true if STMT needs to be rewritten. When renaming a subset
235 of the variables, not all statements will be processed. This is
236 decided in mark_def_sites. */
238 static inline bool
239 rewrite_uses_p (gimple stmt)
241 return gimple_visited_p (stmt);
245 /* Set the rewrite marker on STMT to the value given by REWRITE_P. */
247 static inline void
248 set_rewrite_uses (gimple stmt, bool rewrite_p)
250 gimple_set_visited (stmt, rewrite_p);
254 /* Return true if the DEFs created by statement STMT should be
255 registered when marking new definition sites. This is slightly
256 different than rewrite_uses_p: it's used by update_ssa to
257 distinguish statements that need to have both uses and defs
258 processed from those that only need to have their defs processed.
259 Statements that define new SSA names only need to have their defs
260 registered, but they don't need to have their uses renamed. */
262 static inline bool
263 register_defs_p (gimple stmt)
265 return gimple_plf (stmt, GF_PLF_1) != 0;
269 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered. */
271 static inline void
272 set_register_defs (gimple stmt, bool register_defs_p)
274 gimple_set_plf (stmt, GF_PLF_1, register_defs_p);
278 /* Get the information associated with NAME. */
280 static inline ssa_name_info_p
281 get_ssa_name_ann (tree name)
283 unsigned ver = SSA_NAME_VERSION (name);
284 unsigned len = VEC_length (ssa_name_info_p, info_for_ssa_name);
285 struct ssa_name_info *info;
287 if (ver >= len)
289 unsigned new_len = num_ssa_names;
291 VEC_reserve (ssa_name_info_p, heap, info_for_ssa_name, new_len);
292 while (len++ < new_len)
294 struct ssa_name_info *info = XCNEW (struct ssa_name_info);
295 info->age = current_info_for_ssa_name_age;
296 VEC_quick_push (ssa_name_info_p, info_for_ssa_name, info);
300 info = VEC_index (ssa_name_info_p, info_for_ssa_name, ver);
301 if (info->age < current_info_for_ssa_name_age)
303 info->need_phi_state = NEED_PHI_STATE_UNKNOWN;
304 info->current_def = NULL_TREE;
305 info->age = current_info_for_ssa_name_age;
308 return info;
312 /* Clears info for SSA names. */
314 static void
315 clear_ssa_name_info (void)
317 current_info_for_ssa_name_age++;
321 /* Get phi_state field for VAR. */
323 static inline enum need_phi_state
324 get_phi_state (tree var)
326 if (TREE_CODE (var) == SSA_NAME)
327 return get_ssa_name_ann (var)->need_phi_state;
328 else
329 return var_ann (var)->need_phi_state;
333 /* Sets phi_state field for VAR to STATE. */
335 static inline void
336 set_phi_state (tree var, enum need_phi_state state)
338 if (TREE_CODE (var) == SSA_NAME)
339 get_ssa_name_ann (var)->need_phi_state = state;
340 else
341 var_ann (var)->need_phi_state = state;
345 /* Return the current definition for VAR. */
347 tree
348 get_current_def (tree var)
350 if (TREE_CODE (var) == SSA_NAME)
351 return get_ssa_name_ann (var)->current_def;
352 else
353 return var_ann (var)->current_def;
357 /* Sets current definition of VAR to DEF. */
359 void
360 set_current_def (tree var, tree def)
362 if (TREE_CODE (var) == SSA_NAME)
363 get_ssa_name_ann (var)->current_def = def;
364 else
365 var_ann (var)->current_def = def;
369 /* Compute global livein information given the set of blocks where
370 an object is locally live at the start of the block (LIVEIN)
371 and the set of blocks where the object is defined (DEF_BLOCKS).
373 Note: This routine augments the existing local livein information
374 to include global livein (i.e., it modifies the underlying bitmap
375 for LIVEIN). */
377 void
378 compute_global_livein (bitmap livein ATTRIBUTE_UNUSED, bitmap def_blocks ATTRIBUTE_UNUSED)
380 basic_block bb, *worklist, *tos;
381 unsigned i;
382 bitmap_iterator bi;
384 tos = worklist
385 = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
387 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
388 *tos++ = BASIC_BLOCK (i);
390 /* Iterate until the worklist is empty. */
391 while (tos != worklist)
393 edge e;
394 edge_iterator ei;
396 /* Pull a block off the worklist. */
397 bb = *--tos;
399 /* For each predecessor block. */
400 FOR_EACH_EDGE (e, ei, bb->preds)
402 basic_block pred = e->src;
403 int pred_index = pred->index;
405 /* None of this is necessary for the entry block. */
406 if (pred != ENTRY_BLOCK_PTR
407 && ! bitmap_bit_p (livein, pred_index)
408 && ! bitmap_bit_p (def_blocks, pred_index))
410 *tos++ = pred;
411 bitmap_set_bit (livein, pred_index);
416 free (worklist);
420 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
421 all statements in basic block BB. */
423 static void
424 initialize_flags_in_bb (basic_block bb)
426 gimple stmt;
427 gimple_stmt_iterator gsi;
429 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
431 gimple phi = gsi_stmt (gsi);
432 set_rewrite_uses (phi, false);
433 set_register_defs (phi, false);
436 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
438 stmt = gsi_stmt (gsi);
440 /* We are going to use the operand cache API, such as
441 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
442 cache for each statement should be up-to-date. */
443 gcc_assert (!gimple_modified_p (stmt));
444 set_rewrite_uses (stmt, false);
445 set_register_defs (stmt, false);
449 /* Mark block BB as interesting for update_ssa. */
451 static void
452 mark_block_for_update (basic_block bb)
454 gcc_assert (blocks_to_update != NULL);
455 if (!bitmap_set_bit (blocks_to_update, bb->index))
456 return;
457 initialize_flags_in_bb (bb);
460 /* Return the set of blocks where variable VAR is defined and the blocks
461 where VAR is live on entry (livein). If no entry is found in
462 DEF_BLOCKS, a new one is created and returned. */
464 static inline struct def_blocks_d *
465 get_def_blocks_for (tree var)
467 struct def_blocks_d db, *db_p;
468 void **slot;
470 db.var = var;
471 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
472 if (*slot == NULL)
474 db_p = XNEW (struct def_blocks_d);
475 db_p->var = var;
476 db_p->def_blocks = BITMAP_ALLOC (NULL);
477 db_p->phi_blocks = BITMAP_ALLOC (NULL);
478 db_p->livein_blocks = BITMAP_ALLOC (NULL);
479 *slot = (void *) db_p;
481 else
482 db_p = (struct def_blocks_d *) *slot;
484 return db_p;
488 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
489 VAR is defined by a PHI node. */
491 static void
492 set_def_block (tree var, basic_block bb, bool phi_p)
494 struct def_blocks_d *db_p;
495 enum need_phi_state state;
497 state = get_phi_state (var);
498 db_p = get_def_blocks_for (var);
500 /* Set the bit corresponding to the block where VAR is defined. */
501 bitmap_set_bit (db_p->def_blocks, bb->index);
502 if (phi_p)
503 bitmap_set_bit (db_p->phi_blocks, bb->index);
505 /* Keep track of whether or not we may need to insert PHI nodes.
507 If we are in the UNKNOWN state, then this is the first definition
508 of VAR. Additionally, we have not seen any uses of VAR yet, so
509 we do not need a PHI node for this variable at this time (i.e.,
510 transition to NEED_PHI_STATE_NO).
512 If we are in any other state, then we either have multiple definitions
513 of this variable occurring in different blocks or we saw a use of the
514 variable which was not dominated by the block containing the
515 definition(s). In this case we may need a PHI node, so enter
516 state NEED_PHI_STATE_MAYBE. */
517 if (state == NEED_PHI_STATE_UNKNOWN)
518 set_phi_state (var, NEED_PHI_STATE_NO);
519 else
520 set_phi_state (var, NEED_PHI_STATE_MAYBE);
524 /* Mark block BB as having VAR live at the entry to BB. */
526 static void
527 set_livein_block (tree var, basic_block bb)
529 struct def_blocks_d *db_p;
530 enum need_phi_state state = get_phi_state (var);
532 db_p = get_def_blocks_for (var);
534 /* Set the bit corresponding to the block where VAR is live in. */
535 bitmap_set_bit (db_p->livein_blocks, bb->index);
537 /* Keep track of whether or not we may need to insert PHI nodes.
539 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
540 by the single block containing the definition(s) of this variable. If
541 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
542 NEED_PHI_STATE_MAYBE. */
543 if (state == NEED_PHI_STATE_NO)
545 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
547 if (def_block_index == -1
548 || ! dominated_by_p (CDI_DOMINATORS, bb,
549 BASIC_BLOCK (def_block_index)))
550 set_phi_state (var, NEED_PHI_STATE_MAYBE);
552 else
553 set_phi_state (var, NEED_PHI_STATE_MAYBE);
557 /* Return true if symbol SYM is marked for renaming. */
559 bool
560 symbol_marked_for_renaming (tree sym)
562 return bitmap_bit_p (SYMS_TO_RENAME (cfun), DECL_UID (sym));
566 /* Return true if NAME is in OLD_SSA_NAMES. */
568 static inline bool
569 is_old_name (tree name)
571 unsigned ver = SSA_NAME_VERSION (name);
572 if (!new_ssa_names)
573 return false;
574 return ver < new_ssa_names->n_bits && TEST_BIT (old_ssa_names, ver);
578 /* Return true if NAME is in NEW_SSA_NAMES. */
580 static inline bool
581 is_new_name (tree name)
583 unsigned ver = SSA_NAME_VERSION (name);
584 if (!new_ssa_names)
585 return false;
586 return ver < new_ssa_names->n_bits && TEST_BIT (new_ssa_names, ver);
590 /* Hashing and equality functions for REPL_TBL. */
592 static hashval_t
593 repl_map_hash (const void *p)
595 return htab_hash_pointer ((const void *)((const struct repl_map_d *)p)->name);
598 static int
599 repl_map_eq (const void *p1, const void *p2)
601 return ((const struct repl_map_d *)p1)->name
602 == ((const struct repl_map_d *)p2)->name;
605 static void
606 repl_map_free (void *p)
608 BITMAP_FREE (((struct repl_map_d *)p)->set);
609 free (p);
613 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET). */
615 static inline bitmap
616 names_replaced_by (tree new_tree)
618 struct repl_map_d m;
619 void **slot;
621 m.name = new_tree;
622 slot = htab_find_slot (repl_tbl, (void *) &m, NO_INSERT);
624 /* If N was not registered in the replacement table, return NULL. */
625 if (slot == NULL || *slot == NULL)
626 return NULL;
628 return ((struct repl_map_d *) *slot)->set;
632 /* Add OLD to REPL_TBL[NEW_TREE].SET. */
634 static inline void
635 add_to_repl_tbl (tree new_tree, tree old)
637 struct repl_map_d m, *mp;
638 void **slot;
640 m.name = new_tree;
641 slot = htab_find_slot (repl_tbl, (void *) &m, INSERT);
642 if (*slot == NULL)
644 mp = XNEW (struct repl_map_d);
645 mp->name = new_tree;
646 mp->set = BITMAP_ALLOC (NULL);
647 *slot = (void *) mp;
649 else
650 mp = (struct repl_map_d *) *slot;
652 bitmap_set_bit (mp->set, SSA_NAME_VERSION (old));
656 /* Add a new mapping NEW_TREE -> OLD REPL_TBL. Every entry N_i in REPL_TBL
657 represents the set of names O_1 ... O_j replaced by N_i. This is
658 used by update_ssa and its helpers to introduce new SSA names in an
659 already formed SSA web. */
661 static void
662 add_new_name_mapping (tree new_tree, tree old)
664 timevar_push (TV_TREE_SSA_INCREMENTAL);
666 /* OLD and NEW_TREE must be different SSA names for the same symbol. */
667 gcc_assert (new_tree != old && SSA_NAME_VAR (new_tree) == SSA_NAME_VAR (old));
669 /* If this mapping is for virtual names, we will need to update
670 virtual operands. If this is a mapping for .MEM, then we gather
671 the symbols associated with each name. */
672 if (!is_gimple_reg (new_tree))
674 tree sym;
676 update_ssa_stats.num_virtual_mappings++;
677 update_ssa_stats.num_virtual_symbols++;
679 /* Keep counts of virtual mappings and symbols to use in the
680 virtual mapping heuristic. If we have large numbers of
681 virtual mappings for a relatively low number of symbols, it
682 will make more sense to rename the symbols from scratch.
683 Otherwise, the insertion of PHI nodes for each of the old
684 names in these mappings will be very slow. */
685 sym = SSA_NAME_VAR (new_tree);
686 bitmap_set_bit (update_ssa_stats.virtual_symbols, DECL_UID (sym));
689 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
690 caller may have created new names since the set was created. */
691 if (new_ssa_names->n_bits <= num_ssa_names - 1)
693 unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
694 new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
695 old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
698 /* Update the REPL_TBL table. */
699 add_to_repl_tbl (new_tree, old);
701 /* If OLD had already been registered as a new name, then all the
702 names that OLD replaces should also be replaced by NEW_TREE. */
703 if (is_new_name (old))
704 bitmap_ior_into (names_replaced_by (new_tree), names_replaced_by (old));
706 /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
707 respectively. */
708 SET_BIT (new_ssa_names, SSA_NAME_VERSION (new_tree));
709 SET_BIT (old_ssa_names, SSA_NAME_VERSION (old));
711 /* Update mapping counter to use in the virtual mapping heuristic. */
712 update_ssa_stats.num_total_mappings++;
714 timevar_pop (TV_TREE_SSA_INCREMENTAL);
718 /* Call back for walk_dominator_tree used to collect definition sites
719 for every variable in the function. For every statement S in block
722 1- Variables defined by S in the DEFS of S are marked in the bitmap
723 KILLS.
725 2- If S uses a variable VAR and there is no preceding kill of VAR,
726 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
728 This information is used to determine which variables are live
729 across block boundaries to reduce the number of PHI nodes
730 we create. */
732 static void
733 mark_def_sites (basic_block bb, gimple stmt, bitmap kills)
735 tree def;
736 use_operand_p use_p;
737 ssa_op_iter iter;
739 /* Since this is the first time that we rewrite the program into SSA
740 form, force an operand scan on every statement. */
741 update_stmt (stmt);
743 gcc_assert (blocks_to_update == NULL);
744 set_register_defs (stmt, false);
745 set_rewrite_uses (stmt, false);
747 if (is_gimple_debug (stmt))
748 return;
750 /* If a variable is used before being set, then the variable is live
751 across a block boundary, so mark it live-on-entry to BB. */
752 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
754 tree sym = USE_FROM_PTR (use_p);
755 gcc_assert (DECL_P (sym));
756 if (!bitmap_bit_p (kills, DECL_UID (sym)))
757 set_livein_block (sym, bb);
758 set_rewrite_uses (stmt, true);
761 /* Now process the defs. Mark BB as the definition block and add
762 each def to the set of killed symbols. */
763 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
765 gcc_assert (DECL_P (def));
766 set_def_block (def, bb, false);
767 bitmap_set_bit (kills, DECL_UID (def));
768 set_register_defs (stmt, true);
771 /* If we found the statement interesting then also mark the block BB
772 as interesting. */
773 if (rewrite_uses_p (stmt) || register_defs_p (stmt))
774 SET_BIT (interesting_blocks, bb->index);
777 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
778 in the dfs numbering of the dominance tree. */
780 struct dom_dfsnum
782 /* Basic block whose index this entry corresponds to. */
783 unsigned bb_index;
785 /* The dfs number of this node. */
786 unsigned dfs_num;
789 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
790 for qsort. */
792 static int
793 cmp_dfsnum (const void *a, const void *b)
795 const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a;
796 const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b;
798 return (int) da->dfs_num - (int) db->dfs_num;
801 /* Among the intervals starting at the N points specified in DEFS, find
802 the one that contains S, and return its bb_index. */
804 static unsigned
805 find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
807 unsigned f = 0, t = n, m;
809 while (t > f + 1)
811 m = (f + t) / 2;
812 if (defs[m].dfs_num <= s)
813 f = m;
814 else
815 t = m;
818 return defs[f].bb_index;
821 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
822 KILLS is a bitmap of blocks where the value is defined before any use. */
824 static void
825 prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
827 VEC(int, heap) *worklist;
828 bitmap_iterator bi;
829 unsigned i, b, p, u, top;
830 bitmap live_phis;
831 basic_block def_bb, use_bb;
832 edge e;
833 edge_iterator ei;
834 bitmap to_remove;
835 struct dom_dfsnum *defs;
836 unsigned n_defs, adef;
838 if (bitmap_empty_p (uses))
840 bitmap_clear (phis);
841 return;
844 /* The phi must dominate a use, or an argument of a live phi. Also, we
845 do not create any phi nodes in def blocks, unless they are also livein. */
846 to_remove = BITMAP_ALLOC (NULL);
847 bitmap_and_compl (to_remove, kills, uses);
848 bitmap_and_compl_into (phis, to_remove);
849 if (bitmap_empty_p (phis))
851 BITMAP_FREE (to_remove);
852 return;
855 /* We want to remove the unnecessary phi nodes, but we do not want to compute
856 liveness information, as that may be linear in the size of CFG, and if
857 there are lot of different variables to rewrite, this may lead to quadratic
858 behavior.
860 Instead, we basically emulate standard dce. We put all uses to worklist,
861 then for each of them find the nearest def that dominates them. If this
862 def is a phi node, we mark it live, and if it was not live before, we
863 add the predecessors of its basic block to the worklist.
865 To quickly locate the nearest def that dominates use, we use dfs numbering
866 of the dominance tree (that is already available in order to speed up
867 queries). For each def, we have the interval given by the dfs number on
868 entry to and on exit from the corresponding subtree in the dominance tree.
869 The nearest dominator for a given use is the smallest of these intervals
870 that contains entry and exit dfs numbers for the basic block with the use.
871 If we store the bounds for all the uses to an array and sort it, we can
872 locate the nearest dominating def in logarithmic time by binary search.*/
873 bitmap_ior (to_remove, kills, phis);
874 n_defs = bitmap_count_bits (to_remove);
875 defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
876 defs[0].bb_index = 1;
877 defs[0].dfs_num = 0;
878 adef = 1;
879 EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
881 def_bb = BASIC_BLOCK (i);
882 defs[adef].bb_index = i;
883 defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
884 defs[adef + 1].bb_index = i;
885 defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
886 adef += 2;
888 BITMAP_FREE (to_remove);
889 gcc_assert (adef == 2 * n_defs + 1);
890 qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
891 gcc_assert (defs[0].bb_index == 1);
893 /* Now each DEFS entry contains the number of the basic block to that the
894 dfs number corresponds. Change them to the number of basic block that
895 corresponds to the interval following the dfs number. Also, for the
896 dfs_out numbers, increase the dfs number by one (so that it corresponds
897 to the start of the following interval, not to the end of the current
898 one). We use WORKLIST as a stack. */
899 worklist = VEC_alloc (int, heap, n_defs + 1);
900 VEC_quick_push (int, worklist, 1);
901 top = 1;
902 n_defs = 1;
903 for (i = 1; i < adef; i++)
905 b = defs[i].bb_index;
906 if (b == top)
908 /* This is a closing element. Interval corresponding to the top
909 of the stack after removing it follows. */
910 VEC_pop (int, worklist);
911 top = VEC_index (int, worklist, VEC_length (int, worklist) - 1);
912 defs[n_defs].bb_index = top;
913 defs[n_defs].dfs_num = defs[i].dfs_num + 1;
915 else
917 /* Opening element. Nothing to do, just push it to the stack and move
918 it to the correct position. */
919 defs[n_defs].bb_index = defs[i].bb_index;
920 defs[n_defs].dfs_num = defs[i].dfs_num;
921 VEC_quick_push (int, worklist, b);
922 top = b;
925 /* If this interval starts at the same point as the previous one, cancel
926 the previous one. */
927 if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
928 defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
929 else
930 n_defs++;
932 VEC_pop (int, worklist);
933 gcc_assert (VEC_empty (int, worklist));
935 /* Now process the uses. */
936 live_phis = BITMAP_ALLOC (NULL);
937 EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
939 VEC_safe_push (int, heap, worklist, i);
942 while (!VEC_empty (int, worklist))
944 b = VEC_pop (int, worklist);
945 if (b == ENTRY_BLOCK)
946 continue;
948 /* If there is a phi node in USE_BB, it is made live. Otherwise,
949 find the def that dominates the immediate dominator of USE_BB
950 (the kill in USE_BB does not dominate the use). */
951 if (bitmap_bit_p (phis, b))
952 p = b;
953 else
955 use_bb = get_immediate_dominator (CDI_DOMINATORS, BASIC_BLOCK (b));
956 p = find_dfsnum_interval (defs, n_defs,
957 bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
958 if (!bitmap_bit_p (phis, p))
959 continue;
962 /* If the phi node is already live, there is nothing to do. */
963 if (!bitmap_set_bit (live_phis, p))
964 continue;
966 /* Add the new uses to the worklist. */
967 def_bb = BASIC_BLOCK (p);
968 FOR_EACH_EDGE (e, ei, def_bb->preds)
970 u = e->src->index;
971 if (bitmap_bit_p (uses, u))
972 continue;
974 /* In case there is a kill directly in the use block, do not record
975 the use (this is also necessary for correctness, as we assume that
976 uses dominated by a def directly in their block have been filtered
977 out before). */
978 if (bitmap_bit_p (kills, u))
979 continue;
981 bitmap_set_bit (uses, u);
982 VEC_safe_push (int, heap, worklist, u);
986 VEC_free (int, heap, worklist);
987 bitmap_copy (phis, live_phis);
988 BITMAP_FREE (live_phis);
989 free (defs);
992 /* Return the set of blocks where variable VAR is defined and the blocks
993 where VAR is live on entry (livein). Return NULL, if no entry is
994 found in DEF_BLOCKS. */
996 static inline struct def_blocks_d *
997 find_def_blocks_for (tree var)
999 struct def_blocks_d dm;
1000 dm.var = var;
1001 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1005 /* Retrieve or create a default definition for symbol SYM. */
1007 static inline tree
1008 get_default_def_for (tree sym)
1010 tree ddef = gimple_default_def (cfun, sym);
1012 if (ddef == NULL_TREE)
1014 ddef = make_ssa_name (sym, gimple_build_nop ());
1015 set_default_def (sym, ddef);
1018 return ddef;
1022 /* Marks phi node PHI in basic block BB for rewrite. */
1024 static void
1025 mark_phi_for_rewrite (basic_block bb, gimple phi)
1027 gimple_vec phis;
1028 unsigned i, idx = bb->index;
1030 if (rewrite_uses_p (phi))
1031 return;
1033 set_rewrite_uses (phi, true);
1035 if (!blocks_with_phis_to_rewrite)
1036 return;
1038 bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
1039 VEC_reserve (gimple_vec, heap, phis_to_rewrite, last_basic_block + 1);
1040 for (i = VEC_length (gimple_vec, phis_to_rewrite); i <= idx; i++)
1041 VEC_quick_push (gimple_vec, phis_to_rewrite, NULL);
1043 phis = VEC_index (gimple_vec, phis_to_rewrite, idx);
1044 if (!phis)
1045 phis = VEC_alloc (gimple, heap, 10);
1047 VEC_safe_push (gimple, heap, phis, phi);
1048 VEC_replace (gimple_vec, phis_to_rewrite, idx, phis);
1051 /* Insert PHI nodes for variable VAR using the iterated dominance
1052 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1053 function assumes that the caller is incrementally updating the
1054 existing SSA form, in which case VAR may be an SSA name instead of
1055 a symbol.
1057 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1058 PHI node for VAR. On exit, only the nodes that received a PHI node
1059 for VAR will be present in PHI_INSERTION_POINTS. */
1061 static void
1062 insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
1064 unsigned bb_index;
1065 edge e;
1066 gimple phi;
1067 basic_block bb;
1068 bitmap_iterator bi;
1069 struct def_blocks_d *def_map;
1071 def_map = find_def_blocks_for (var);
1072 gcc_assert (def_map);
1074 /* Remove the blocks where we already have PHI nodes for VAR. */
1075 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1077 /* Remove obviously useless phi nodes. */
1078 prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
1079 def_map->livein_blocks);
1081 /* And insert the PHI nodes. */
1082 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
1084 bb = BASIC_BLOCK (bb_index);
1085 if (update_p)
1086 mark_block_for_update (bb);
1088 phi = NULL;
1090 if (TREE_CODE (var) == SSA_NAME)
1092 /* If we are rewriting SSA names, create the LHS of the PHI
1093 node by duplicating VAR. This is useful in the case of
1094 pointers, to also duplicate pointer attributes (alias
1095 information, in particular). */
1096 edge_iterator ei;
1097 tree new_lhs;
1099 gcc_assert (update_p);
1100 phi = create_phi_node (var, bb);
1102 new_lhs = duplicate_ssa_name (var, phi);
1103 gimple_phi_set_result (phi, new_lhs);
1104 add_new_name_mapping (new_lhs, var);
1106 /* Add VAR to every argument slot of PHI. We need VAR in
1107 every argument so that rewrite_update_phi_arguments knows
1108 which name is this PHI node replacing. If VAR is a
1109 symbol marked for renaming, this is not necessary, the
1110 renamer will use the symbol on the LHS to get its
1111 reaching definition. */
1112 FOR_EACH_EDGE (e, ei, bb->preds)
1113 add_phi_arg (phi, var, e, UNKNOWN_LOCATION);
1115 else
1117 tree tracked_var;
1119 gcc_assert (DECL_P (var));
1120 phi = create_phi_node (var, bb);
1122 tracked_var = target_for_debug_bind (var);
1123 if (tracked_var)
1125 gimple note = gimple_build_debug_bind (tracked_var,
1126 PHI_RESULT (phi),
1127 phi);
1128 gimple_stmt_iterator si = gsi_after_labels (bb);
1129 gsi_insert_before (&si, note, GSI_SAME_STMT);
1133 /* Mark this PHI node as interesting for update_ssa. */
1134 set_register_defs (phi, true);
1135 mark_phi_for_rewrite (bb, phi);
1140 /* Insert PHI nodes at the dominance frontier of blocks with variable
1141 definitions. DFS contains the dominance frontier information for
1142 the flowgraph. */
1144 static void
1145 insert_phi_nodes (bitmap_head *dfs)
1147 referenced_var_iterator rvi;
1148 bitmap_iterator bi;
1149 tree var;
1150 bitmap vars;
1151 unsigned uid;
1153 timevar_push (TV_TREE_INSERT_PHI_NODES);
1155 /* Do two stages to avoid code generation differences for UID
1156 differences but no UID ordering differences. */
1158 vars = BITMAP_ALLOC (NULL);
1159 FOR_EACH_REFERENCED_VAR (var, rvi)
1161 struct def_blocks_d *def_map;
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)
1168 bitmap_set_bit (vars, DECL_UID (var));
1171 EXECUTE_IF_SET_IN_BITMAP (vars, 0, uid, bi)
1173 tree var = referenced_var (uid);
1174 struct def_blocks_d *def_map;
1175 bitmap idf;
1177 def_map = find_def_blocks_for (var);
1178 idf = compute_idf (def_map->def_blocks, dfs);
1179 insert_phi_nodes_for (var, idf, false);
1180 BITMAP_FREE (idf);
1183 BITMAP_FREE (vars);
1185 timevar_pop (TV_TREE_INSERT_PHI_NODES);
1189 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1190 register DEF (an SSA_NAME) to be a new definition for SYM. */
1192 static void
1193 register_new_def (tree def, tree sym)
1195 tree currdef;
1197 /* If this variable is set in a single basic block and all uses are
1198 dominated by the set(s) in that single basic block, then there is
1199 no reason to record anything for this variable in the block local
1200 definition stacks. Doing so just wastes time and memory.
1202 This is the same test to prune the set of variables which may
1203 need PHI nodes. So we just use that information since it's already
1204 computed and available for us to use. */
1205 if (get_phi_state (sym) == NEED_PHI_STATE_NO)
1207 set_current_def (sym, def);
1208 return;
1211 currdef = get_current_def (sym);
1213 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1214 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1215 in the stack so that we know which symbol is being defined by
1216 this SSA name when we unwind the stack. */
1217 if (currdef && !is_gimple_reg (sym))
1218 VEC_safe_push (tree, heap, block_defs_stack, sym);
1220 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1221 stack is later used by the dominator tree callbacks to restore
1222 the reaching definitions for all the variables defined in the
1223 block after a recursive visit to all its immediately dominated
1224 blocks. If there is no current reaching definition, then just
1225 record the underlying _DECL node. */
1226 VEC_safe_push (tree, heap, block_defs_stack, currdef ? currdef : sym);
1228 /* Set the current reaching definition for SYM to be DEF. */
1229 set_current_def (sym, def);
1233 /* Perform a depth-first traversal of the dominator tree looking for
1234 variables to rename. BB is the block where to start searching.
1235 Renaming is a five step process:
1237 1- Every definition made by PHI nodes at the start of the blocks is
1238 registered as the current definition for the corresponding variable.
1240 2- Every statement in BB is rewritten. USE and VUSE operands are
1241 rewritten with their corresponding reaching definition. DEF and
1242 VDEF targets are registered as new definitions.
1244 3- All the PHI nodes in successor blocks of BB are visited. The
1245 argument corresponding to BB is replaced with its current reaching
1246 definition.
1248 4- Recursively rewrite every dominator child block of BB.
1250 5- Restore (in reverse order) the current reaching definition for every
1251 new definition introduced in this block. This is done so that when
1252 we return from the recursive call, all the current reaching
1253 definitions are restored to the names that were valid in the
1254 dominator parent of BB. */
1256 /* Return the current definition for variable VAR. If none is found,
1257 create a new SSA name to act as the zeroth definition for VAR. */
1259 static tree
1260 get_reaching_def (tree var)
1262 tree currdef;
1264 /* Lookup the current reaching definition for VAR. */
1265 currdef = get_current_def (var);
1267 /* If there is no reaching definition for VAR, create and register a
1268 default definition for it (if needed). */
1269 if (currdef == NULL_TREE)
1271 tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1272 currdef = get_default_def_for (sym);
1273 set_current_def (var, currdef);
1276 /* Return the current reaching definition for VAR, or the default
1277 definition, if we had to create one. */
1278 return currdef;
1282 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1283 the block with its immediate reaching definitions. Update the current
1284 definition of a variable when a new real or virtual definition is found. */
1286 static void
1287 rewrite_stmt (gimple_stmt_iterator si)
1289 use_operand_p use_p;
1290 def_operand_p def_p;
1291 ssa_op_iter iter;
1292 gimple stmt = gsi_stmt (si);
1294 /* If mark_def_sites decided that we don't need to rewrite this
1295 statement, ignore it. */
1296 gcc_assert (blocks_to_update == NULL);
1297 if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1298 return;
1300 if (dump_file && (dump_flags & TDF_DETAILS))
1302 fprintf (dump_file, "Renaming statement ");
1303 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1304 fprintf (dump_file, "\n");
1307 /* Step 1. Rewrite USES in the statement. */
1308 if (rewrite_uses_p (stmt))
1309 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1311 tree var = USE_FROM_PTR (use_p);
1312 gcc_assert (DECL_P (var));
1313 SET_USE (use_p, get_reaching_def (var));
1316 /* Step 2. Register the statement's DEF operands. */
1317 if (register_defs_p (stmt))
1318 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1320 tree var = DEF_FROM_PTR (def_p);
1321 tree name = make_ssa_name (var, stmt);
1322 tree tracked_var;
1323 gcc_assert (DECL_P (var));
1324 SET_DEF (def_p, name);
1325 register_new_def (DEF_FROM_PTR (def_p), var);
1327 tracked_var = target_for_debug_bind (var);
1328 if (tracked_var)
1330 gimple note = gimple_build_debug_bind (tracked_var, name, stmt);
1331 gsi_insert_after (&si, note, GSI_SAME_STMT);
1337 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1338 PHI nodes. For every PHI node found, add a new argument containing the
1339 current reaching definition for the variable and the edge through which
1340 that definition is reaching the PHI node. */
1342 static void
1343 rewrite_add_phi_arguments (basic_block bb)
1345 edge e;
1346 edge_iterator ei;
1348 FOR_EACH_EDGE (e, ei, bb->succs)
1350 gimple phi;
1351 gimple_stmt_iterator gsi;
1353 for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
1354 gsi_next (&gsi))
1356 tree currdef;
1357 gimple stmt;
1359 phi = gsi_stmt (gsi);
1360 currdef = get_reaching_def (SSA_NAME_VAR (gimple_phi_result (phi)));
1361 stmt = SSA_NAME_DEF_STMT (currdef);
1362 add_phi_arg (phi, currdef, e, gimple_location (stmt));
1367 /* SSA Rewriting Step 1. Initialization, create a block local stack
1368 of reaching definitions for new SSA names produced in this block
1369 (BLOCK_DEFS). Register new definitions for every PHI node in the
1370 block. */
1372 static void
1373 rewrite_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1374 basic_block bb)
1376 gimple phi;
1377 gimple_stmt_iterator gsi;
1379 if (dump_file && (dump_flags & TDF_DETAILS))
1380 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1382 /* Mark the unwind point for this block. */
1383 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1385 /* Step 1. Register new definitions for every PHI node in the block.
1386 Conceptually, all the PHI nodes are executed in parallel and each PHI
1387 node introduces a new version for the associated variable. */
1388 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1390 tree result;
1392 phi = gsi_stmt (gsi);
1393 result = gimple_phi_result (phi);
1394 gcc_assert (is_gimple_reg (result));
1395 register_new_def (result, SSA_NAME_VAR (result));
1398 /* Step 2. Rewrite every variable used in each statement in the block
1399 with its immediate reaching definitions. Update the current definition
1400 of a variable when a new real or virtual definition is found. */
1401 if (TEST_BIT (interesting_blocks, bb->index))
1402 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1403 rewrite_stmt (gsi);
1405 /* Step 3. Visit all the successor blocks of BB looking for PHI nodes.
1406 For every PHI node found, add a new argument containing the current
1407 reaching definition for the variable and the edge through which that
1408 definition is reaching the PHI node. */
1409 rewrite_add_phi_arguments (bb);
1414 /* Called after visiting all the statements in basic block BB and all
1415 of its dominator children. Restore CURRDEFS to its original value. */
1417 static void
1418 rewrite_leave_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1419 basic_block bb ATTRIBUTE_UNUSED)
1421 /* Restore CURRDEFS to its original state. */
1422 while (VEC_length (tree, block_defs_stack) > 0)
1424 tree tmp = VEC_pop (tree, block_defs_stack);
1425 tree saved_def, var;
1427 if (tmp == NULL_TREE)
1428 break;
1430 if (TREE_CODE (tmp) == SSA_NAME)
1432 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1433 current definition of its underlying variable. Note that
1434 if the SSA_NAME is not for a GIMPLE register, the symbol
1435 being defined is stored in the next slot in the stack.
1436 This mechanism is needed because an SSA name for a
1437 non-register symbol may be the definition for more than
1438 one symbol (e.g., SFTs, aliased variables, etc). */
1439 saved_def = tmp;
1440 var = SSA_NAME_VAR (saved_def);
1441 if (!is_gimple_reg (var))
1442 var = VEC_pop (tree, block_defs_stack);
1444 else
1446 /* If we recorded anything else, it must have been a _DECL
1447 node and its current reaching definition must have been
1448 NULL. */
1449 saved_def = NULL;
1450 var = tmp;
1453 set_current_def (var, saved_def);
1458 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1460 void
1461 dump_decl_set (FILE *file, bitmap set)
1463 if (set)
1465 bitmap_iterator bi;
1466 unsigned i;
1468 fprintf (file, "{ ");
1470 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
1472 tree var = referenced_var_lookup (i);
1473 if (var)
1474 print_generic_expr (file, var, 0);
1475 else
1476 fprintf (file, "D.%u", i);
1477 fprintf (file, " ");
1480 fprintf (file, "}");
1482 else
1483 fprintf (file, "NIL");
1487 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1489 DEBUG_FUNCTION void
1490 debug_decl_set (bitmap set)
1492 dump_decl_set (stderr, set);
1493 fprintf (stderr, "\n");
1497 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1498 stack up to a maximum of N levels. If N is -1, the whole stack is
1499 dumped. New levels are created when the dominator tree traversal
1500 used for renaming enters a new sub-tree. */
1502 void
1503 dump_defs_stack (FILE *file, int n)
1505 int i, j;
1507 fprintf (file, "\n\nRenaming stack");
1508 if (n > 0)
1509 fprintf (file, " (up to %d levels)", n);
1510 fprintf (file, "\n\n");
1512 i = 1;
1513 fprintf (file, "Level %d (current level)\n", i);
1514 for (j = (int) VEC_length (tree, block_defs_stack) - 1; j >= 0; j--)
1516 tree name, var;
1518 name = VEC_index (tree, block_defs_stack, j);
1519 if (name == NULL_TREE)
1521 i++;
1522 if (n > 0 && i > n)
1523 break;
1524 fprintf (file, "\nLevel %d\n", i);
1525 continue;
1528 if (DECL_P (name))
1530 var = name;
1531 name = NULL_TREE;
1533 else
1535 var = SSA_NAME_VAR (name);
1536 if (!is_gimple_reg (var))
1538 j--;
1539 var = VEC_index (tree, block_defs_stack, j);
1543 fprintf (file, " Previous CURRDEF (");
1544 print_generic_expr (file, var, 0);
1545 fprintf (file, ") = ");
1546 if (name)
1547 print_generic_expr (file, name, 0);
1548 else
1549 fprintf (file, "<NIL>");
1550 fprintf (file, "\n");
1555 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1556 stack up to a maximum of N levels. If N is -1, the whole stack is
1557 dumped. New levels are created when the dominator tree traversal
1558 used for renaming enters a new sub-tree. */
1560 DEBUG_FUNCTION void
1561 debug_defs_stack (int n)
1563 dump_defs_stack (stderr, n);
1567 /* Dump the current reaching definition of every symbol to FILE. */
1569 void
1570 dump_currdefs (FILE *file)
1572 referenced_var_iterator i;
1573 tree var;
1575 fprintf (file, "\n\nCurrent reaching definitions\n\n");
1576 FOR_EACH_REFERENCED_VAR (var, i)
1577 if (SYMS_TO_RENAME (cfun) == NULL
1578 || bitmap_bit_p (SYMS_TO_RENAME (cfun), DECL_UID (var)))
1580 fprintf (file, "CURRDEF (");
1581 print_generic_expr (file, var, 0);
1582 fprintf (file, ") = ");
1583 if (get_current_def (var))
1584 print_generic_expr (file, get_current_def (var), 0);
1585 else
1586 fprintf (file, "<NIL>");
1587 fprintf (file, "\n");
1592 /* Dump the current reaching definition of every symbol to stderr. */
1594 DEBUG_FUNCTION void
1595 debug_currdefs (void)
1597 dump_currdefs (stderr);
1601 /* Dump SSA information to FILE. */
1603 void
1604 dump_tree_ssa (FILE *file)
1606 const char *funcname
1607 = lang_hooks.decl_printable_name (current_function_decl, 2);
1609 fprintf (file, "SSA renaming information for %s\n\n", funcname);
1611 dump_def_blocks (file);
1612 dump_defs_stack (file, -1);
1613 dump_currdefs (file);
1614 dump_tree_ssa_stats (file);
1618 /* Dump SSA information to stderr. */
1620 DEBUG_FUNCTION void
1621 debug_tree_ssa (void)
1623 dump_tree_ssa (stderr);
1627 /* Dump statistics for the hash table HTAB. */
1629 static void
1630 htab_statistics (FILE *file, htab_t htab)
1632 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1633 (long) htab_size (htab),
1634 (long) htab_elements (htab),
1635 htab_collisions (htab));
1639 /* Dump SSA statistics on FILE. */
1641 void
1642 dump_tree_ssa_stats (FILE *file)
1644 if (def_blocks || repl_tbl)
1645 fprintf (file, "\nHash table statistics:\n");
1647 if (def_blocks)
1649 fprintf (file, " def_blocks: ");
1650 htab_statistics (file, def_blocks);
1653 if (repl_tbl)
1655 fprintf (file, " repl_tbl: ");
1656 htab_statistics (file, repl_tbl);
1659 if (def_blocks || repl_tbl)
1660 fprintf (file, "\n");
1664 /* Dump SSA statistics on stderr. */
1666 DEBUG_FUNCTION void
1667 debug_tree_ssa_stats (void)
1669 dump_tree_ssa_stats (stderr);
1673 /* Hashing and equality functions for DEF_BLOCKS. */
1675 static hashval_t
1676 def_blocks_hash (const void *p)
1678 return htab_hash_pointer
1679 ((const void *)((const struct def_blocks_d *)p)->var);
1682 static int
1683 def_blocks_eq (const void *p1, const void *p2)
1685 return ((const struct def_blocks_d *)p1)->var
1686 == ((const struct def_blocks_d *)p2)->var;
1690 /* Free memory allocated by one entry in DEF_BLOCKS. */
1692 static void
1693 def_blocks_free (void *p)
1695 struct def_blocks_d *entry = (struct def_blocks_d *) p;
1696 BITMAP_FREE (entry->def_blocks);
1697 BITMAP_FREE (entry->phi_blocks);
1698 BITMAP_FREE (entry->livein_blocks);
1699 free (entry);
1703 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1705 static int
1706 debug_def_blocks_r (void **slot, void *data)
1708 FILE *file = (FILE *) data;
1709 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1711 fprintf (file, "VAR: ");
1712 print_generic_expr (file, db_p->var, dump_flags);
1713 bitmap_print (file, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1714 bitmap_print (file, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}");
1715 bitmap_print (file, db_p->phi_blocks, ", PHI_BLOCKS: { ", "}\n");
1717 return 1;
1721 /* Dump the DEF_BLOCKS hash table on FILE. */
1723 void
1724 dump_def_blocks (FILE *file)
1726 fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
1727 if (def_blocks)
1728 htab_traverse (def_blocks, debug_def_blocks_r, file);
1732 /* Dump the DEF_BLOCKS hash table on stderr. */
1734 DEBUG_FUNCTION void
1735 debug_def_blocks (void)
1737 dump_def_blocks (stderr);
1741 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1743 static inline void
1744 register_new_update_single (tree new_name, tree old_name)
1746 tree currdef = get_current_def (old_name);
1748 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1749 This stack is later used by the dominator tree callbacks to
1750 restore the reaching definitions for all the variables
1751 defined in the block after a recursive visit to all its
1752 immediately dominated blocks. */
1753 VEC_reserve (tree, heap, block_defs_stack, 2);
1754 VEC_quick_push (tree, block_defs_stack, currdef);
1755 VEC_quick_push (tree, block_defs_stack, old_name);
1757 /* Set the current reaching definition for OLD_NAME to be
1758 NEW_NAME. */
1759 set_current_def (old_name, new_name);
1763 /* Register NEW_NAME to be the new reaching definition for all the
1764 names in OLD_NAMES. Used by the incremental SSA update routines to
1765 replace old SSA names with new ones. */
1767 static inline void
1768 register_new_update_set (tree new_name, bitmap old_names)
1770 bitmap_iterator bi;
1771 unsigned i;
1773 EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1774 register_new_update_single (new_name, ssa_name (i));
1779 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1780 it is a symbol marked for renaming, replace it with USE_P's current
1781 reaching definition. */
1783 static inline void
1784 maybe_replace_use (use_operand_p use_p)
1786 tree rdef = NULL_TREE;
1787 tree use = USE_FROM_PTR (use_p);
1788 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1790 if (symbol_marked_for_renaming (sym))
1791 rdef = get_reaching_def (sym);
1792 else if (is_old_name (use))
1793 rdef = get_reaching_def (use);
1795 if (rdef && rdef != use)
1796 SET_USE (use_p, rdef);
1800 /* Same as maybe_replace_use, but without introducing default stmts,
1801 returning false to indicate a need to do so. */
1803 static inline bool
1804 maybe_replace_use_in_debug_stmt (use_operand_p use_p)
1806 tree rdef = NULL_TREE;
1807 tree use = USE_FROM_PTR (use_p);
1808 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1810 if (symbol_marked_for_renaming (sym))
1811 rdef = get_current_def (sym);
1812 else if (is_old_name (use))
1814 rdef = get_current_def (use);
1815 /* We can't assume that, if there's no current definition, the
1816 default one should be used. It could be the case that we've
1817 rearranged blocks so that the earlier definition no longer
1818 dominates the use. */
1819 if (!rdef && SSA_NAME_IS_DEFAULT_DEF (use))
1820 rdef = use;
1822 else
1823 rdef = use;
1825 if (rdef && rdef != use)
1826 SET_USE (use_p, rdef);
1828 return rdef != NULL_TREE;
1832 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1833 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1834 register it as the current definition for the names replaced by
1835 DEF_P. */
1837 static inline void
1838 maybe_register_def (def_operand_p def_p, gimple stmt,
1839 gimple_stmt_iterator gsi)
1841 tree def = DEF_FROM_PTR (def_p);
1842 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
1844 /* If DEF is a naked symbol that needs renaming, create a new
1845 name for it. */
1846 if (symbol_marked_for_renaming (sym))
1848 if (DECL_P (def))
1850 tree tracked_var;
1852 def = make_ssa_name (def, stmt);
1853 SET_DEF (def_p, def);
1855 tracked_var = target_for_debug_bind (sym);
1856 if (tracked_var)
1858 gimple note = gimple_build_debug_bind (tracked_var, def, stmt);
1859 /* If stmt ends the bb, insert the debug stmt on the single
1860 non-EH edge from the stmt. */
1861 if (gsi_one_before_end_p (gsi) && stmt_ends_bb_p (stmt))
1863 basic_block bb = gsi_bb (gsi);
1864 edge_iterator ei;
1865 edge e, ef = NULL;
1866 FOR_EACH_EDGE (e, ei, bb->succs)
1867 if (!(e->flags & EDGE_EH))
1869 gcc_assert (!ef);
1870 ef = e;
1872 gcc_assert (ef
1873 && single_pred_p (ef->dest)
1874 && !phi_nodes (ef->dest)
1875 && ef->dest != EXIT_BLOCK_PTR);
1876 gsi_insert_on_edge_immediate (ef, note);
1878 else
1879 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
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 (gimple stmt, gimple_stmt_iterator gsi)
1910 use_operand_p use_p;
1911 def_operand_p def_p;
1912 ssa_op_iter iter;
1914 /* Only update marked statements. */
1915 if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1916 return;
1918 if (dump_file && (dump_flags & TDF_DETAILS))
1920 fprintf (dump_file, "Updating SSA information for statement ");
1921 print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1922 fprintf (dump_file, "\n");
1925 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1926 symbol is marked for renaming. */
1927 if (rewrite_uses_p (stmt))
1929 if (is_gimple_debug (stmt))
1931 bool failed = false;
1933 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1934 if (!maybe_replace_use_in_debug_stmt (use_p))
1936 failed = true;
1937 break;
1940 if (failed)
1942 /* DOM sometimes threads jumps in such a way that a
1943 debug stmt ends up referencing a SSA variable that no
1944 longer dominates the debug stmt, but such that all
1945 incoming definitions refer to the same definition in
1946 an earlier dominator. We could try to recover that
1947 definition somehow, but this will have to do for now.
1949 Introducing a default definition, which is what
1950 maybe_replace_use() would do in such cases, may
1951 modify code generation, for the otherwise-unused
1952 default definition would never go away, modifying SSA
1953 version numbers all over. */
1954 gimple_debug_bind_reset_value (stmt);
1955 update_stmt (stmt);
1958 else
1960 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1961 maybe_replace_use (use_p);
1965 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1966 Also register definitions for names whose underlying symbol is
1967 marked for renaming. */
1968 if (register_defs_p (stmt))
1969 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1970 maybe_register_def (def_p, stmt, gsi);
1974 /* Visit all the successor blocks of BB looking for PHI nodes. For
1975 every PHI node found, check if any of its arguments is in
1976 OLD_SSA_NAMES. If so, and if the argument has a current reaching
1977 definition, replace it. */
1979 static void
1980 rewrite_update_phi_arguments (basic_block bb)
1982 edge e;
1983 edge_iterator ei;
1984 unsigned i;
1986 FOR_EACH_EDGE (e, ei, bb->succs)
1988 gimple phi;
1989 gimple_vec phis;
1991 if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
1992 continue;
1994 phis = VEC_index (gimple_vec, phis_to_rewrite, e->dest->index);
1995 FOR_EACH_VEC_ELT (gimple, phis, i, phi)
1997 tree arg, lhs_sym, reaching_def = NULL;
1998 use_operand_p arg_p;
2000 gcc_assert (rewrite_uses_p (phi));
2002 arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
2003 arg = USE_FROM_PTR (arg_p);
2005 if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
2006 continue;
2008 lhs_sym = SSA_NAME_VAR (gimple_phi_result (phi));
2010 if (arg == NULL_TREE)
2012 /* When updating a PHI node for a recently introduced
2013 symbol we may find NULL arguments. That's why we
2014 take the symbol from the LHS of the PHI node. */
2015 reaching_def = get_reaching_def (lhs_sym);
2018 else
2020 tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
2022 if (symbol_marked_for_renaming (sym))
2023 reaching_def = get_reaching_def (sym);
2024 else if (is_old_name (arg))
2025 reaching_def = get_reaching_def (arg);
2028 /* Update the argument if there is a reaching def. */
2029 if (reaching_def)
2031 gimple stmt;
2032 source_location locus;
2033 int arg_i = PHI_ARG_INDEX_FROM_USE (arg_p);
2035 SET_USE (arg_p, reaching_def);
2036 stmt = SSA_NAME_DEF_STMT (reaching_def);
2038 /* Single element PHI nodes behave like copies, so get the
2039 location from the phi argument. */
2040 if (gimple_code (stmt) == GIMPLE_PHI &&
2041 gimple_phi_num_args (stmt) == 1)
2042 locus = gimple_phi_arg_location (stmt, 0);
2043 else
2044 locus = gimple_location (stmt);
2046 gimple_phi_arg_set_location (phi, arg_i, locus);
2050 if (e->flags & EDGE_ABNORMAL)
2051 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
2057 /* Initialization of block data structures for the incremental SSA
2058 update pass. Create a block local stack of reaching definitions
2059 for new SSA names produced in this block (BLOCK_DEFS). Register
2060 new definitions for every PHI node in the block. */
2062 static void
2063 rewrite_update_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2064 basic_block bb)
2066 bool is_abnormal_phi;
2067 gimple_stmt_iterator gsi;
2069 if (dump_file && (dump_flags & TDF_DETAILS))
2070 fprintf (dump_file, "\n\nRegistering new PHI nodes in block #%d\n\n",
2071 bb->index);
2073 /* Mark the unwind point for this block. */
2074 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
2076 if (!bitmap_bit_p (blocks_to_update, bb->index))
2077 return;
2079 /* Mark the LHS if any of the arguments flows through an abnormal
2080 edge. */
2081 is_abnormal_phi = bb_has_abnormal_pred (bb);
2083 /* If any of the PHI nodes is a replacement for a name in
2084 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2085 register it as a new definition for its corresponding name. Also
2086 register definitions for names whose underlying symbols are
2087 marked for renaming. */
2088 for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2090 tree lhs, lhs_sym;
2091 gimple phi = gsi_stmt (gsi);
2093 if (!register_defs_p (phi))
2094 continue;
2096 lhs = gimple_phi_result (phi);
2097 lhs_sym = SSA_NAME_VAR (lhs);
2099 if (symbol_marked_for_renaming (lhs_sym))
2100 register_new_update_single (lhs, lhs_sym);
2101 else
2104 /* If LHS is a new name, register a new definition for all
2105 the names replaced by LHS. */
2106 if (is_new_name (lhs))
2107 register_new_update_set (lhs, names_replaced_by (lhs));
2109 /* If LHS is an OLD name, register it as a new definition
2110 for itself. */
2111 if (is_old_name (lhs))
2112 register_new_update_single (lhs, lhs);
2115 if (is_abnormal_phi)
2116 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
2119 /* Step 2. Rewrite every variable used in each statement in the block. */
2120 if (TEST_BIT (interesting_blocks, bb->index))
2122 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
2123 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2124 rewrite_update_stmt (gsi_stmt (gsi), gsi);
2127 /* Step 3. Update PHI nodes. */
2128 rewrite_update_phi_arguments (bb);
2131 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
2132 the current reaching definition of every name re-written in BB to
2133 the original reaching definition before visiting BB. This
2134 unwinding must be done in the opposite order to what is done in
2135 register_new_update_set. */
2137 static void
2138 rewrite_update_leave_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2139 basic_block bb ATTRIBUTE_UNUSED)
2141 while (VEC_length (tree, block_defs_stack) > 0)
2143 tree var = VEC_pop (tree, block_defs_stack);
2144 tree saved_def;
2146 /* NULL indicates the unwind stop point for this block (see
2147 rewrite_update_enter_block). */
2148 if (var == NULL)
2149 return;
2151 saved_def = VEC_pop (tree, block_defs_stack);
2152 set_current_def (var, saved_def);
2157 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2158 form.
2160 ENTRY indicates the block where to start. Every block dominated by
2161 ENTRY will be rewritten.
2163 WHAT indicates what actions will be taken by the renamer (see enum
2164 rewrite_mode).
2166 BLOCKS are the set of interesting blocks for the dominator walker
2167 to process. If this set is NULL, then all the nodes dominated
2168 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2169 are not present in BLOCKS are ignored. */
2171 static void
2172 rewrite_blocks (basic_block entry, enum rewrite_mode what)
2174 struct dom_walk_data walk_data;
2176 /* Rewrite all the basic blocks in the program. */
2177 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
2179 /* Setup callbacks for the generic dominator tree walker. */
2180 memset (&walk_data, 0, sizeof (walk_data));
2182 walk_data.dom_direction = CDI_DOMINATORS;
2184 if (what == REWRITE_ALL)
2186 walk_data.before_dom_children = rewrite_enter_block;
2187 walk_data.after_dom_children = rewrite_leave_block;
2189 else if (what == REWRITE_UPDATE)
2191 walk_data.before_dom_children = rewrite_update_enter_block;
2192 walk_data.after_dom_children = rewrite_update_leave_block;
2194 else
2195 gcc_unreachable ();
2197 block_defs_stack = VEC_alloc (tree, heap, 10);
2199 /* Initialize the dominator walker. */
2200 init_walk_dominator_tree (&walk_data);
2202 /* Recursively walk the dominator tree rewriting each statement in
2203 each basic block. */
2204 walk_dominator_tree (&walk_data, entry);
2206 /* Finalize the dominator walker. */
2207 fini_walk_dominator_tree (&walk_data);
2209 /* Debugging dumps. */
2210 if (dump_file && (dump_flags & TDF_STATS))
2212 dump_dfa_stats (dump_file);
2213 if (def_blocks)
2214 dump_tree_ssa_stats (dump_file);
2217 VEC_free (tree, heap, block_defs_stack);
2219 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
2223 /* Block processing routine for mark_def_sites. Clear the KILLS bitmap
2224 at the start of each block, and call mark_def_sites for each statement. */
2226 static void
2227 mark_def_sites_block (struct dom_walk_data *walk_data, basic_block bb)
2229 struct mark_def_sites_global_data *gd;
2230 bitmap kills;
2231 gimple_stmt_iterator gsi;
2233 gd = (struct mark_def_sites_global_data *) walk_data->global_data;
2234 kills = gd->kills;
2236 bitmap_clear (kills);
2237 for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2238 mark_def_sites (bb, gsi_stmt (gsi), kills);
2242 /* Mark the definition site blocks for each variable, so that we know
2243 where the variable is actually live.
2245 The INTERESTING_BLOCKS global will be filled in with all the blocks
2246 that should be processed by the renamer. It is assumed that the
2247 caller has already initialized and zeroed it. */
2249 static void
2250 mark_def_site_blocks (void)
2252 struct dom_walk_data walk_data;
2253 struct mark_def_sites_global_data mark_def_sites_global_data;
2255 /* Setup callbacks for the generic dominator tree walker to find and
2256 mark definition sites. */
2257 walk_data.dom_direction = CDI_DOMINATORS;
2258 walk_data.initialize_block_local_data = NULL;
2259 walk_data.before_dom_children = mark_def_sites_block;
2260 walk_data.after_dom_children = NULL;
2262 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2263 large enough to accommodate all the variables referenced in the
2264 function, not just the ones we are renaming. */
2265 mark_def_sites_global_data.kills = BITMAP_ALLOC (NULL);
2266 walk_data.global_data = &mark_def_sites_global_data;
2268 /* We do not have any local data. */
2269 walk_data.block_local_data_size = 0;
2271 /* Initialize the dominator walker. */
2272 init_walk_dominator_tree (&walk_data);
2274 /* Recursively walk the dominator tree. */
2275 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
2277 /* Finalize the dominator walker. */
2278 fini_walk_dominator_tree (&walk_data);
2280 /* We no longer need this bitmap, clear and free it. */
2281 BITMAP_FREE (mark_def_sites_global_data.kills);
2285 /* Initialize internal data needed during renaming. */
2287 static void
2288 init_ssa_renamer (void)
2290 tree var;
2291 referenced_var_iterator rvi;
2293 cfun->gimple_df->in_ssa_p = false;
2295 /* Allocate memory for the DEF_BLOCKS hash table. */
2296 gcc_assert (def_blocks == NULL);
2297 def_blocks = htab_create (num_referenced_vars, def_blocks_hash,
2298 def_blocks_eq, def_blocks_free);
2300 FOR_EACH_REFERENCED_VAR(var, rvi)
2301 set_current_def (var, NULL_TREE);
2305 /* Deallocate internal data structures used by the renamer. */
2307 static void
2308 fini_ssa_renamer (void)
2310 if (def_blocks)
2312 htab_delete (def_blocks);
2313 def_blocks = NULL;
2316 cfun->gimple_df->in_ssa_p = true;
2319 /* Main entry point into the SSA builder. The renaming process
2320 proceeds in four main phases:
2322 1- Compute dominance frontier and immediate dominators, needed to
2323 insert PHI nodes and rename the function in dominator tree
2324 order.
2326 2- Find and mark all the blocks that define variables
2327 (mark_def_site_blocks).
2329 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2331 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2333 Steps 3 and 4 are done using the dominator tree walker
2334 (walk_dominator_tree). */
2336 static unsigned int
2337 rewrite_into_ssa (void)
2339 bitmap_head *dfs;
2340 basic_block bb;
2342 timevar_push (TV_TREE_SSA_OTHER);
2344 /* Initialize operand data structures. */
2345 init_ssa_operands ();
2347 /* Initialize internal data needed by the renamer. */
2348 init_ssa_renamer ();
2350 /* Initialize the set of interesting blocks. The callback
2351 mark_def_sites will add to this set those blocks that the renamer
2352 should process. */
2353 interesting_blocks = sbitmap_alloc (last_basic_block);
2354 sbitmap_zero (interesting_blocks);
2356 /* Initialize dominance frontier. */
2357 dfs = XNEWVEC (bitmap_head, last_basic_block);
2358 FOR_EACH_BB (bb)
2359 bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
2361 /* 1- Compute dominance frontiers. */
2362 calculate_dominance_info (CDI_DOMINATORS);
2363 compute_dominance_frontiers (dfs);
2365 /* 2- Find and mark definition sites. */
2366 mark_def_site_blocks ();
2368 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2369 insert_phi_nodes (dfs);
2371 /* 4- Rename all the blocks. */
2372 rewrite_blocks (ENTRY_BLOCK_PTR, REWRITE_ALL);
2374 /* Free allocated memory. */
2375 FOR_EACH_BB (bb)
2376 bitmap_clear (&dfs[bb->index]);
2377 free (dfs);
2379 sbitmap_free (interesting_blocks);
2381 fini_ssa_renamer ();
2383 timevar_pop (TV_TREE_SSA_OTHER);
2384 return 0;
2388 struct gimple_opt_pass pass_build_ssa =
2391 GIMPLE_PASS,
2392 "ssa", /* name */
2393 NULL, /* gate */
2394 rewrite_into_ssa, /* execute */
2395 NULL, /* sub */
2396 NULL, /* next */
2397 0, /* static_pass_number */
2398 TV_NONE, /* tv_id */
2399 PROP_cfg | PROP_referenced_vars, /* properties_required */
2400 PROP_ssa, /* properties_provided */
2401 0, /* properties_destroyed */
2402 0, /* todo_flags_start */
2403 TODO_dump_func
2404 | TODO_update_ssa_only_virtuals
2405 | TODO_verify_ssa
2406 | TODO_remove_unused_locals /* todo_flags_finish */
2411 /* Mark the definition of VAR at STMT and BB as interesting for the
2412 renamer. BLOCKS is the set of blocks that need updating. */
2414 static void
2415 mark_def_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
2417 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
2418 set_register_defs (stmt, true);
2420 if (insert_phi_p)
2422 bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI;
2424 set_def_block (var, bb, is_phi_p);
2426 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2427 site for both itself and all the old names replaced by it. */
2428 if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2430 bitmap_iterator bi;
2431 unsigned i;
2432 bitmap set = names_replaced_by (var);
2433 if (set)
2434 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2435 set_def_block (ssa_name (i), bb, is_phi_p);
2441 /* Mark the use of VAR at STMT and BB as interesting for the
2442 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2443 nodes. */
2445 static inline void
2446 mark_use_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
2448 basic_block def_bb = gimple_bb (stmt);
2450 mark_block_for_update (def_bb);
2451 mark_block_for_update (bb);
2453 if (gimple_code (stmt) == GIMPLE_PHI)
2454 mark_phi_for_rewrite (def_bb, stmt);
2455 else
2457 set_rewrite_uses (stmt, true);
2459 if (is_gimple_debug (stmt))
2460 return;
2463 /* If VAR has not been defined in BB, then it is live-on-entry
2464 to BB. Note that we cannot just use the block holding VAR's
2465 definition because if VAR is one of the names in OLD_SSA_NAMES,
2466 it will have several definitions (itself and all the names that
2467 replace it). */
2468 if (insert_phi_p)
2470 struct def_blocks_d *db_p = get_def_blocks_for (var);
2471 if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2472 set_livein_block (var, bb);
2477 /* Do a dominator walk starting at BB processing statements that
2478 reference symbols in SYMS_TO_RENAME. This is very similar to
2479 mark_def_sites, but the scan handles statements whose operands may
2480 already be SSA names.
2482 If INSERT_PHI_P is true, mark those uses as live in the
2483 corresponding block. This is later used by the PHI placement
2484 algorithm to make PHI pruning decisions.
2486 FIXME. Most of this would be unnecessary if we could associate a
2487 symbol to all the SSA names that reference it. But that
2488 sounds like it would be expensive to maintain. Still, it
2489 would be interesting to see if it makes better sense to do
2490 that. */
2492 static void
2493 prepare_block_for_update (basic_block bb, bool insert_phi_p)
2495 basic_block son;
2496 gimple_stmt_iterator si;
2497 edge e;
2498 edge_iterator ei;
2500 mark_block_for_update (bb);
2502 /* Process PHI nodes marking interesting those that define or use
2503 the symbols that we are interested in. */
2504 for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
2506 gimple phi = gsi_stmt (si);
2507 tree lhs_sym, lhs = gimple_phi_result (phi);
2509 lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
2511 if (!symbol_marked_for_renaming (lhs_sym))
2512 continue;
2514 mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2516 /* Mark the uses in phi nodes as interesting. It would be more correct
2517 to process the arguments of the phi nodes of the successor edges of
2518 BB at the end of prepare_block_for_update, however, that turns out
2519 to be significantly more expensive. Doing it here is conservatively
2520 correct -- it may only cause us to believe a value to be live in a
2521 block that also contains its definition, and thus insert a few more
2522 phi nodes for it. */
2523 FOR_EACH_EDGE (e, ei, bb->preds)
2524 mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
2527 /* Process the statements. */
2528 for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2530 gimple stmt;
2531 ssa_op_iter i;
2532 use_operand_p use_p;
2533 def_operand_p def_p;
2535 stmt = gsi_stmt (si);
2537 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
2539 tree use = USE_FROM_PTR (use_p);
2540 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2541 if (symbol_marked_for_renaming (sym))
2542 mark_use_interesting (sym, stmt, bb, insert_phi_p);
2545 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_ALL_DEFS)
2547 tree def = DEF_FROM_PTR (def_p);
2548 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2549 if (symbol_marked_for_renaming (sym))
2550 mark_def_interesting (sym, stmt, bb, insert_phi_p);
2554 /* Now visit all the blocks dominated by BB. */
2555 for (son = first_dom_son (CDI_DOMINATORS, bb);
2556 son;
2557 son = next_dom_son (CDI_DOMINATORS, son))
2558 prepare_block_for_update (son, insert_phi_p);
2562 /* Helper for prepare_names_to_update. Mark all the use sites for
2563 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2564 prepare_names_to_update. */
2566 static void
2567 prepare_use_sites_for (tree name, bool insert_phi_p)
2569 use_operand_p use_p;
2570 imm_use_iterator iter;
2572 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2574 gimple stmt = USE_STMT (use_p);
2575 basic_block bb = gimple_bb (stmt);
2577 if (gimple_code (stmt) == GIMPLE_PHI)
2579 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
2580 edge e = gimple_phi_arg_edge (stmt, ix);
2581 mark_use_interesting (name, stmt, e->src, insert_phi_p);
2583 else
2585 /* For regular statements, mark this as an interesting use
2586 for NAME. */
2587 mark_use_interesting (name, stmt, bb, insert_phi_p);
2593 /* Helper for prepare_names_to_update. Mark the definition site for
2594 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2595 prepare_names_to_update. */
2597 static void
2598 prepare_def_site_for (tree name, bool insert_phi_p)
2600 gimple stmt;
2601 basic_block bb;
2603 gcc_assert (names_to_release == NULL
2604 || !bitmap_bit_p (names_to_release, SSA_NAME_VERSION (name)));
2606 stmt = SSA_NAME_DEF_STMT (name);
2607 bb = gimple_bb (stmt);
2608 if (bb)
2610 gcc_assert (bb->index < last_basic_block);
2611 mark_block_for_update (bb);
2612 mark_def_interesting (name, stmt, bb, insert_phi_p);
2617 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2618 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2619 PHI nodes for newly created names. */
2621 static void
2622 prepare_names_to_update (bool insert_phi_p)
2624 unsigned i = 0;
2625 bitmap_iterator bi;
2626 sbitmap_iterator sbi;
2628 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2629 remove it from NEW_SSA_NAMES so that we don't try to visit its
2630 defining basic block (which most likely doesn't exist). Notice
2631 that we cannot do the same with names in OLD_SSA_NAMES because we
2632 want to replace existing instances. */
2633 if (names_to_release)
2634 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2635 RESET_BIT (new_ssa_names, i);
2637 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2638 names may be considered to be live-in on blocks that contain
2639 definitions for their replacements. */
2640 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2641 prepare_def_site_for (ssa_name (i), insert_phi_p);
2643 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2644 OLD_SSA_NAMES, but we have to ignore its definition site. */
2645 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2647 if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2648 prepare_def_site_for (ssa_name (i), insert_phi_p);
2649 prepare_use_sites_for (ssa_name (i), insert_phi_p);
2654 /* Dump all the names replaced by NAME to FILE. */
2656 void
2657 dump_names_replaced_by (FILE *file, tree name)
2659 unsigned i;
2660 bitmap old_set;
2661 bitmap_iterator bi;
2663 print_generic_expr (file, name, 0);
2664 fprintf (file, " -> { ");
2666 old_set = names_replaced_by (name);
2667 EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2669 print_generic_expr (file, ssa_name (i), 0);
2670 fprintf (file, " ");
2673 fprintf (file, "}\n");
2677 /* Dump all the names replaced by NAME to stderr. */
2679 DEBUG_FUNCTION void
2680 debug_names_replaced_by (tree name)
2682 dump_names_replaced_by (stderr, name);
2686 /* Dump SSA update information to FILE. */
2688 void
2689 dump_update_ssa (FILE *file)
2691 unsigned i = 0;
2692 bitmap_iterator bi;
2694 if (!need_ssa_update_p (cfun))
2695 return;
2697 if (new_ssa_names && sbitmap_first_set_bit (new_ssa_names) >= 0)
2699 sbitmap_iterator sbi;
2701 fprintf (file, "\nSSA replacement table\n");
2702 fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2703 "O_1, ..., O_j\n\n");
2705 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2706 dump_names_replaced_by (file, ssa_name (i));
2708 fprintf (file, "\n");
2709 fprintf (file, "Number of virtual NEW -> OLD mappings: %7u\n",
2710 update_ssa_stats.num_virtual_mappings);
2711 fprintf (file, "Number of real NEW -> OLD mappings: %7u\n",
2712 update_ssa_stats.num_total_mappings
2713 - update_ssa_stats.num_virtual_mappings);
2714 fprintf (file, "Number of total NEW -> OLD mappings: %7u\n",
2715 update_ssa_stats.num_total_mappings);
2717 fprintf (file, "\nNumber of virtual symbols: %u\n",
2718 update_ssa_stats.num_virtual_symbols);
2721 if (!bitmap_empty_p (SYMS_TO_RENAME (cfun)))
2723 fprintf (file, "\n\nSymbols to be put in SSA form\n\n");
2724 dump_decl_set (file, SYMS_TO_RENAME (cfun));
2725 fprintf (file, "\n");
2728 if (names_to_release && !bitmap_empty_p (names_to_release))
2730 fprintf (file, "\n\nSSA names to release after updating the SSA web\n\n");
2731 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2733 print_generic_expr (file, ssa_name (i), 0);
2734 fprintf (file, " ");
2738 fprintf (file, "\n\n");
2742 /* Dump SSA update information to stderr. */
2744 DEBUG_FUNCTION void
2745 debug_update_ssa (void)
2747 dump_update_ssa (stderr);
2751 /* Initialize data structures used for incremental SSA updates. */
2753 static void
2754 init_update_ssa (struct function *fn)
2756 /* Reserve more space than the current number of names. The calls to
2757 add_new_name_mapping are typically done after creating new SSA
2758 names, so we'll need to reallocate these arrays. */
2759 old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2760 sbitmap_zero (old_ssa_names);
2762 new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2763 sbitmap_zero (new_ssa_names);
2765 repl_tbl = htab_create (20, repl_map_hash, repl_map_eq, repl_map_free);
2766 names_to_release = NULL;
2767 memset (&update_ssa_stats, 0, sizeof (update_ssa_stats));
2768 update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL);
2769 update_ssa_initialized_fn = fn;
2773 /* Deallocate data structures used for incremental SSA updates. */
2775 void
2776 delete_update_ssa (void)
2778 unsigned i;
2779 bitmap_iterator bi;
2781 sbitmap_free (old_ssa_names);
2782 old_ssa_names = NULL;
2784 sbitmap_free (new_ssa_names);
2785 new_ssa_names = NULL;
2787 htab_delete (repl_tbl);
2788 repl_tbl = NULL;
2790 bitmap_clear (SYMS_TO_RENAME (update_ssa_initialized_fn));
2791 BITMAP_FREE (update_ssa_stats.virtual_symbols);
2793 if (names_to_release)
2795 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2796 release_ssa_name (ssa_name (i));
2797 BITMAP_FREE (names_to_release);
2800 clear_ssa_name_info ();
2802 fini_ssa_renamer ();
2804 if (blocks_with_phis_to_rewrite)
2805 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
2807 gimple_vec phis = VEC_index (gimple_vec, phis_to_rewrite, i);
2809 VEC_free (gimple, heap, phis);
2810 VEC_replace (gimple_vec, phis_to_rewrite, i, NULL);
2813 BITMAP_FREE (blocks_with_phis_to_rewrite);
2814 BITMAP_FREE (blocks_to_update);
2815 update_ssa_initialized_fn = NULL;
2819 /* Create a new name for OLD_NAME in statement STMT and replace the
2820 operand pointed to by DEF_P with the newly created name. Return
2821 the new name and register the replacement mapping <NEW, OLD> in
2822 update_ssa's tables. */
2824 tree
2825 create_new_def_for (tree old_name, gimple stmt, def_operand_p def)
2827 tree new_name = duplicate_ssa_name (old_name, stmt);
2829 SET_DEF (def, new_name);
2831 if (gimple_code (stmt) == GIMPLE_PHI)
2833 basic_block bb = gimple_bb (stmt);
2835 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2836 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = bb_has_abnormal_pred (bb);
2839 register_new_name_mapping (new_name, old_name);
2841 /* For the benefit of passes that will be updating the SSA form on
2842 their own, set the current reaching definition of OLD_NAME to be
2843 NEW_NAME. */
2844 set_current_def (old_name, new_name);
2846 return new_name;
2850 /* Register name NEW to be a replacement for name OLD. This function
2851 must be called for every replacement that should be performed by
2852 update_ssa. */
2854 void
2855 register_new_name_mapping (tree new_tree, tree old)
2857 if (!update_ssa_initialized_fn)
2858 init_update_ssa (cfun);
2860 gcc_assert (update_ssa_initialized_fn == cfun);
2862 add_new_name_mapping (new_tree, old);
2866 /* Register symbol SYM to be renamed by update_ssa. */
2868 void
2869 mark_sym_for_renaming (tree sym)
2871 bitmap_set_bit (SYMS_TO_RENAME (cfun), DECL_UID (sym));
2875 /* Register all the symbols in SET to be renamed by update_ssa. */
2877 void
2878 mark_set_for_renaming (bitmap set)
2880 bitmap_iterator bi;
2881 unsigned i;
2883 if (set == NULL || bitmap_empty_p (set))
2884 return;
2886 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2887 mark_sym_for_renaming (referenced_var (i));
2891 /* Return true if there is any work to be done by update_ssa
2892 for function FN. */
2894 bool
2895 need_ssa_update_p (struct function *fn)
2897 gcc_assert (fn != NULL);
2898 return (update_ssa_initialized_fn == fn
2899 || (fn->gimple_df
2900 && !bitmap_empty_p (SYMS_TO_RENAME (fn))));
2903 /* Return true if SSA name mappings have been registered for SSA updating. */
2905 bool
2906 name_mappings_registered_p (void)
2908 if (!update_ssa_initialized_fn)
2909 return false;
2911 gcc_assert (update_ssa_initialized_fn == cfun);
2913 return repl_tbl && htab_elements (repl_tbl) > 0;
2916 /* Return true if name N has been registered in the replacement table. */
2918 bool
2919 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED)
2921 if (!update_ssa_initialized_fn)
2922 return false;
2924 gcc_assert (update_ssa_initialized_fn == cfun);
2926 return is_new_name (n) || is_old_name (n);
2930 /* Return the set of all the SSA names marked to be replaced. */
2932 bitmap
2933 ssa_names_to_replace (void)
2935 unsigned i = 0;
2936 bitmap ret;
2937 sbitmap_iterator sbi;
2939 gcc_assert (update_ssa_initialized_fn == NULL
2940 || update_ssa_initialized_fn == cfun);
2942 ret = BITMAP_ALLOC (NULL);
2943 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2944 bitmap_set_bit (ret, i);
2946 return ret;
2950 /* Mark NAME to be released after update_ssa has finished. */
2952 void
2953 release_ssa_name_after_update_ssa (tree name)
2955 gcc_assert (cfun && update_ssa_initialized_fn == cfun);
2957 if (names_to_release == NULL)
2958 names_to_release = BITMAP_ALLOC (NULL);
2960 bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
2964 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2965 frontier information. BLOCKS is the set of blocks to be updated.
2967 This is slightly different than the regular PHI insertion
2968 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
2969 real names (i.e., GIMPLE registers) are inserted:
2971 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2972 nodes inside the region affected by the block that defines VAR
2973 and the blocks that define all its replacements. All these
2974 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2976 First, we compute the entry point to the region (ENTRY). This is
2977 given by the nearest common dominator to all the definition
2978 blocks. When computing the iterated dominance frontier (IDF), any
2979 block not strictly dominated by ENTRY is ignored.
2981 We then call the standard PHI insertion algorithm with the pruned
2982 IDF.
2984 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
2985 names is not pruned. PHI nodes are inserted at every IDF block. */
2987 static void
2988 insert_updated_phi_nodes_for (tree var, bitmap_head *dfs, bitmap blocks,
2989 unsigned update_flags)
2991 basic_block entry;
2992 struct def_blocks_d *db;
2993 bitmap idf, pruned_idf;
2994 bitmap_iterator bi;
2995 unsigned i;
2997 if (TREE_CODE (var) == SSA_NAME)
2998 gcc_checking_assert (is_old_name (var));
2999 else
3000 gcc_checking_assert (symbol_marked_for_renaming (var));
3002 /* Get all the definition sites for VAR. */
3003 db = find_def_blocks_for (var);
3005 /* No need to do anything if there were no definitions to VAR. */
3006 if (db == NULL || bitmap_empty_p (db->def_blocks))
3007 return;
3009 /* Compute the initial iterated dominance frontier. */
3010 idf = compute_idf (db->def_blocks, dfs);
3011 pruned_idf = BITMAP_ALLOC (NULL);
3013 if (TREE_CODE (var) == SSA_NAME)
3015 if (update_flags == TODO_update_ssa)
3017 /* If doing regular SSA updates for GIMPLE registers, we are
3018 only interested in IDF blocks dominated by the nearest
3019 common dominator of all the definition blocks. */
3020 entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
3021 db->def_blocks);
3022 if (entry != ENTRY_BLOCK_PTR)
3023 EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
3024 if (BASIC_BLOCK (i) != entry
3025 && dominated_by_p (CDI_DOMINATORS, BASIC_BLOCK (i), entry))
3026 bitmap_set_bit (pruned_idf, i);
3028 else
3030 /* Otherwise, do not prune the IDF for VAR. */
3031 gcc_assert (update_flags == TODO_update_ssa_full_phi);
3032 bitmap_copy (pruned_idf, idf);
3035 else
3037 /* Otherwise, VAR is a symbol that needs to be put into SSA form
3038 for the first time, so we need to compute the full IDF for
3039 it. */
3040 bitmap_copy (pruned_idf, idf);
3043 if (!bitmap_empty_p (pruned_idf))
3045 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3046 are included in the region to be updated. The feeding blocks
3047 are important to guarantee that the PHI arguments are renamed
3048 properly. */
3050 /* FIXME, this is not needed if we are updating symbols. We are
3051 already starting at the ENTRY block anyway. */
3052 bitmap_ior_into (blocks, pruned_idf);
3053 EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
3055 edge e;
3056 edge_iterator ei;
3057 basic_block bb = BASIC_BLOCK (i);
3059 FOR_EACH_EDGE (e, ei, bb->preds)
3060 if (e->src->index >= 0)
3061 bitmap_set_bit (blocks, e->src->index);
3064 insert_phi_nodes_for (var, pruned_idf, true);
3067 BITMAP_FREE (pruned_idf);
3068 BITMAP_FREE (idf);
3072 /* Heuristic to determine whether SSA name mappings for virtual names
3073 should be discarded and their symbols rewritten from scratch. When
3074 there is a large number of mappings for virtual names, the
3075 insertion of PHI nodes for the old names in the mappings takes
3076 considerable more time than if we inserted PHI nodes for the
3077 symbols instead.
3079 Currently the heuristic takes these stats into account:
3081 - Number of mappings for virtual SSA names.
3082 - Number of distinct virtual symbols involved in those mappings.
3084 If the number of virtual mappings is much larger than the number of
3085 virtual symbols, then it will be faster to compute PHI insertion
3086 spots for the symbols. Even if this involves traversing the whole
3087 CFG, which is what happens when symbols are renamed from scratch. */
3089 static bool
3090 switch_virtuals_to_full_rewrite_p (void)
3092 if (update_ssa_stats.num_virtual_mappings < (unsigned) MIN_VIRTUAL_MAPPINGS)
3093 return false;
3095 if (update_ssa_stats.num_virtual_mappings
3096 > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
3097 * update_ssa_stats.num_virtual_symbols)
3098 return true;
3100 return false;
3104 /* Remove every virtual mapping and mark all the affected virtual
3105 symbols for renaming. */
3107 static void
3108 switch_virtuals_to_full_rewrite (void)
3110 unsigned i = 0;
3111 sbitmap_iterator sbi;
3113 if (dump_file)
3115 fprintf (dump_file, "\nEnabled virtual name mapping heuristic.\n");
3116 fprintf (dump_file, "\tNumber of virtual mappings: %7u\n",
3117 update_ssa_stats.num_virtual_mappings);
3118 fprintf (dump_file, "\tNumber of unique virtual symbols: %7u\n",
3119 update_ssa_stats.num_virtual_symbols);
3120 fprintf (dump_file, "Updating FUD-chains from top of CFG will be "
3121 "faster than processing\nthe name mappings.\n\n");
3124 /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
3125 Note that it is not really necessary to remove the mappings from
3126 REPL_TBL, that would only waste time. */
3127 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
3128 if (!is_gimple_reg (ssa_name (i)))
3129 RESET_BIT (new_ssa_names, i);
3131 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3132 if (!is_gimple_reg (ssa_name (i)))
3133 RESET_BIT (old_ssa_names, i);
3135 mark_set_for_renaming (update_ssa_stats.virtual_symbols);
3139 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3140 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3142 1- The names in OLD_SSA_NAMES dominated by the definitions of
3143 NEW_SSA_NAMES are all re-written to be reached by the
3144 appropriate definition from NEW_SSA_NAMES.
3146 2- If needed, new PHI nodes are added to the iterated dominance
3147 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3149 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3150 calling register_new_name_mapping for every pair of names that the
3151 caller wants to replace.
3153 The caller identifies the new names that have been inserted and the
3154 names that need to be replaced by calling register_new_name_mapping
3155 for every pair <NEW, OLD>. Note that the function assumes that the
3156 new names have already been inserted in the IL.
3158 For instance, given the following code:
3160 1 L0:
3161 2 x_1 = PHI (0, x_5)
3162 3 if (x_1 < 10)
3163 4 if (x_1 > 7)
3164 5 y_2 = 0
3165 6 else
3166 7 y_3 = x_1 + x_7
3167 8 endif
3168 9 x_5 = x_1 + 1
3169 10 goto L0;
3170 11 endif
3172 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3174 1 L0:
3175 2 x_1 = PHI (0, x_5)
3176 3 if (x_1 < 10)
3177 4 x_10 = ...
3178 5 if (x_1 > 7)
3179 6 y_2 = 0
3180 7 else
3181 8 x_11 = ...
3182 9 y_3 = x_1 + x_7
3183 10 endif
3184 11 x_5 = x_1 + 1
3185 12 goto L0;
3186 13 endif
3188 We want to replace all the uses of x_1 with the new definitions of
3189 x_10 and x_11. Note that the only uses that should be replaced are
3190 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3191 *not* be replaced (this is why we cannot just mark symbol 'x' for
3192 renaming).
3194 Additionally, we may need to insert a PHI node at line 11 because
3195 that is a merge point for x_10 and x_11. So the use of x_1 at line
3196 11 will be replaced with the new PHI node. The insertion of PHI
3197 nodes is optional. They are not strictly necessary to preserve the
3198 SSA form, and depending on what the caller inserted, they may not
3199 even be useful for the optimizers. UPDATE_FLAGS controls various
3200 aspects of how update_ssa operates, see the documentation for
3201 TODO_update_ssa*. */
3203 void
3204 update_ssa (unsigned update_flags)
3206 basic_block bb, start_bb;
3207 bitmap_iterator bi;
3208 unsigned i = 0;
3209 bool insert_phi_p;
3210 sbitmap_iterator sbi;
3212 if (!need_ssa_update_p (cfun))
3213 return;
3215 timevar_push (TV_TREE_SSA_INCREMENTAL);
3217 if (!update_ssa_initialized_fn)
3218 init_update_ssa (cfun);
3219 gcc_assert (update_ssa_initialized_fn == cfun);
3221 blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
3222 if (!phis_to_rewrite)
3223 phis_to_rewrite = VEC_alloc (gimple_vec, heap, last_basic_block);
3224 blocks_to_update = BITMAP_ALLOC (NULL);
3226 /* Ensure that the dominance information is up-to-date. */
3227 calculate_dominance_info (CDI_DOMINATORS);
3229 /* Only one update flag should be set. */
3230 gcc_assert (update_flags == TODO_update_ssa
3231 || update_flags == TODO_update_ssa_no_phi
3232 || update_flags == TODO_update_ssa_full_phi
3233 || update_flags == TODO_update_ssa_only_virtuals);
3235 /* If we only need to update virtuals, remove all the mappings for
3236 real names before proceeding. The caller is responsible for
3237 having dealt with the name mappings before calling update_ssa. */
3238 if (update_flags == TODO_update_ssa_only_virtuals)
3240 sbitmap_zero (old_ssa_names);
3241 sbitmap_zero (new_ssa_names);
3242 htab_empty (repl_tbl);
3245 insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
3247 if (insert_phi_p)
3249 /* If the caller requested PHI nodes to be added, initialize
3250 live-in information data structures (DEF_BLOCKS). */
3252 /* For each SSA name N, the DEF_BLOCKS table describes where the
3253 name is defined, which blocks have PHI nodes for N, and which
3254 blocks have uses of N (i.e., N is live-on-entry in those
3255 blocks). */
3256 def_blocks = htab_create (num_ssa_names, def_blocks_hash,
3257 def_blocks_eq, def_blocks_free);
3259 else
3261 def_blocks = NULL;
3264 /* Heuristic to avoid massive slow downs when the replacement
3265 mappings include lots of virtual names. */
3266 if (insert_phi_p && switch_virtuals_to_full_rewrite_p ())
3267 switch_virtuals_to_full_rewrite ();
3269 /* If there are names defined in the replacement table, prepare
3270 definition and use sites for all the names in NEW_SSA_NAMES and
3271 OLD_SSA_NAMES. */
3272 if (sbitmap_first_set_bit (new_ssa_names) >= 0)
3274 prepare_names_to_update (insert_phi_p);
3276 /* If all the names in NEW_SSA_NAMES had been marked for
3277 removal, and there are no symbols to rename, then there's
3278 nothing else to do. */
3279 if (sbitmap_first_set_bit (new_ssa_names) < 0
3280 && bitmap_empty_p (SYMS_TO_RENAME (cfun)))
3281 goto done;
3284 /* Next, determine the block at which to start the renaming process. */
3285 if (!bitmap_empty_p (SYMS_TO_RENAME (cfun)))
3287 /* If we have to rename some symbols from scratch, we need to
3288 start the process at the root of the CFG. FIXME, it should
3289 be possible to determine the nearest block that had a
3290 definition for each of the symbols that are marked for
3291 updating. For now this seems more work than it's worth. */
3292 start_bb = ENTRY_BLOCK_PTR;
3294 /* Traverse the CFG looking for existing definitions and uses of
3295 symbols in SYMS_TO_RENAME. Mark interesting blocks and
3296 statements and set local live-in information for the PHI
3297 placement heuristics. */
3298 prepare_block_for_update (start_bb, insert_phi_p);
3300 else
3302 /* Otherwise, the entry block to the region is the nearest
3303 common dominator for the blocks in BLOCKS. */
3304 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3305 blocks_to_update);
3308 /* If requested, insert PHI nodes at the iterated dominance frontier
3309 of every block, creating new definitions for names in OLD_SSA_NAMES
3310 and for symbols in SYMS_TO_RENAME. */
3311 if (insert_phi_p)
3313 bitmap_head *dfs;
3315 /* If the caller requested PHI nodes to be added, compute
3316 dominance frontiers. */
3317 dfs = XNEWVEC (bitmap_head, last_basic_block);
3318 FOR_EACH_BB (bb)
3319 bitmap_initialize (&dfs[bb->index], &bitmap_default_obstack);
3320 compute_dominance_frontiers (dfs);
3322 if (sbitmap_first_set_bit (old_ssa_names) >= 0)
3324 sbitmap_iterator sbi;
3326 /* insert_update_phi_nodes_for will call add_new_name_mapping
3327 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3328 will grow while we are traversing it (but it will not
3329 gain any new members). Copy OLD_SSA_NAMES to a temporary
3330 for traversal. */
3331 sbitmap tmp = sbitmap_alloc (old_ssa_names->n_bits);
3332 sbitmap_copy (tmp, old_ssa_names);
3333 EXECUTE_IF_SET_IN_SBITMAP (tmp, 0, i, sbi)
3334 insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
3335 update_flags);
3336 sbitmap_free (tmp);
3339 EXECUTE_IF_SET_IN_BITMAP (SYMS_TO_RENAME (cfun), 0, i, bi)
3340 insert_updated_phi_nodes_for (referenced_var (i), dfs, blocks_to_update,
3341 update_flags);
3343 FOR_EACH_BB (bb)
3344 bitmap_clear (&dfs[bb->index]);
3345 free (dfs);
3347 /* Insertion of PHI nodes may have added blocks to the region.
3348 We need to re-compute START_BB to include the newly added
3349 blocks. */
3350 if (start_bb != ENTRY_BLOCK_PTR)
3351 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3352 blocks_to_update);
3355 /* Reset the current definition for name and symbol before renaming
3356 the sub-graph. */
3357 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3358 set_current_def (ssa_name (i), NULL_TREE);
3360 EXECUTE_IF_SET_IN_BITMAP (SYMS_TO_RENAME (cfun), 0, i, bi)
3361 set_current_def (referenced_var (i), NULL_TREE);
3363 /* Now start the renaming process at START_BB. */
3364 interesting_blocks = sbitmap_alloc (last_basic_block);
3365 sbitmap_zero (interesting_blocks);
3366 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3367 SET_BIT (interesting_blocks, i);
3369 rewrite_blocks (start_bb, REWRITE_UPDATE);
3371 sbitmap_free (interesting_blocks);
3373 /* Debugging dumps. */
3374 if (dump_file)
3376 int c;
3377 unsigned i;
3379 dump_update_ssa (dump_file);
3381 fprintf (dump_file, "Incremental SSA update started at block: %d\n\n",
3382 start_bb->index);
3384 c = 0;
3385 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3386 c++;
3387 fprintf (dump_file, "Number of blocks in CFG: %d\n", last_basic_block);
3388 fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n\n",
3389 c, PERCENT (c, last_basic_block));
3391 if (dump_flags & TDF_DETAILS)
3393 fprintf (dump_file, "Affected blocks: ");
3394 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3395 fprintf (dump_file, "%u ", i);
3396 fprintf (dump_file, "\n");
3399 fprintf (dump_file, "\n\n");
3402 /* Free allocated memory. */
3403 done:
3404 delete_update_ssa ();
3406 timevar_pop (TV_TREE_SSA_INCREMENTAL);