2008-05-30 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / gcc / tree-into-ssa.c
blob5542ede5503f1e2c4b7121a47ba9fe615f983100
1 /* Rewrite a program in Normal form into SSA.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007
3 Free Software Foundation, Inc.
4 Contributed by Diego Novillo <dnovillo@redhat.com>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "output.h"
34 #include "expr.h"
35 #include "function.h"
36 #include "diagnostic.h"
37 #include "bitmap.h"
38 #include "tree-flow.h"
39 #include "tree-gimple.h"
40 #include "tree-inline.h"
41 #include "varray.h"
42 #include "timevar.h"
43 #include "hashtab.h"
44 #include "tree-dump.h"
45 #include "tree-pass.h"
46 #include "cfgloop.h"
47 #include "domwalk.h"
48 #include "ggc.h"
49 #include "params.h"
50 #include "vecprim.h"
52 /* This file builds the SSA form for a function as described in:
53 R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
54 Computing Static Single Assignment Form and the Control Dependence
55 Graph. ACM Transactions on Programming Languages and Systems,
56 13(4):451-490, October 1991. */
58 /* Structure to map a variable VAR to the set of blocks that contain
59 definitions for VAR. */
60 struct def_blocks_d
62 /* The variable. */
63 tree var;
65 /* Blocks that contain definitions of VAR. Bit I will be set if the
66 Ith block contains a definition of VAR. */
67 bitmap def_blocks;
69 /* Blocks that contain a PHI node for VAR. */
70 bitmap phi_blocks;
72 /* Blocks where VAR is live-on-entry. Similar semantics as
73 DEF_BLOCKS. */
74 bitmap livein_blocks;
78 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
79 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
80 basic blocks where VAR is defined (assigned a new value). It also
81 contains a bitmap of all the blocks where VAR is live-on-entry
82 (i.e., there is a use of VAR in block B without a preceding
83 definition in B). The live-on-entry information is used when
84 computing PHI pruning heuristics. */
85 static htab_t def_blocks;
87 /* Stack of trees used to restore the global currdefs to its original
88 state after completing rewriting of a block and its dominator
89 children. Its elements have the following properties:
91 - An SSA_NAME (N) indicates that the current definition of the
92 underlying variable should be set to the given SSA_NAME. If the
93 symbol associated with the SSA_NAME is not a GIMPLE register, the
94 next slot in the stack must be a _DECL node (SYM). In this case,
95 the name N in the previous slot is the current reaching
96 definition for SYM.
98 - A _DECL node indicates that the underlying variable has no
99 current definition.
101 - A NULL node at the top entry is used to mark the last slot
102 associated with the current block. */
103 static VEC(tree,heap) *block_defs_stack;
105 /* Set of existing SSA names being replaced by update_ssa. */
106 static sbitmap old_ssa_names;
108 /* Set of new SSA names being added by update_ssa. Note that both
109 NEW_SSA_NAMES and OLD_SSA_NAMES are dense bitmaps because most of
110 the operations done on them are presence tests. */
111 static sbitmap new_ssa_names;
113 /* Symbols whose SSA form needs to be updated or created for the first
114 time. */
115 static bitmap syms_to_rename;
117 /* Subset of SYMS_TO_RENAME. Contains all the GIMPLE register symbols
118 that have been marked for renaming. */
119 static bitmap regs_to_rename;
121 /* Subset of SYMS_TO_RENAME. Contains all the memory symbols
122 that have been marked for renaming. */
123 static bitmap mem_syms_to_rename;
125 /* Set of SSA names that have been marked to be released after they
126 were registered in the replacement table. They will be finally
127 released after we finish updating the SSA web. */
128 static bitmap names_to_release;
130 /* For each block, the PHI nodes that need to be rewritten are stored into
131 these vectors. */
132 typedef VEC(tree, heap) *tree_vec;
133 DEF_VEC_P (tree_vec);
134 DEF_VEC_ALLOC_P (tree_vec, heap);
136 static VEC(tree_vec, heap) *phis_to_rewrite;
138 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE. */
139 static bitmap blocks_with_phis_to_rewrite;
141 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES. These sets need
142 to grow as the callers to register_new_name_mapping will typically
143 create new names on the fly. FIXME. Currently set to 1/3 to avoid
144 frequent reallocations but still need to find a reasonable growth
145 strategy. */
146 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
148 /* Tuple used to represent replacement mappings. */
149 struct repl_map_d
151 tree name;
152 bitmap set;
155 /* NEW -> OLD_SET replacement table. If we are replacing several
156 existing SSA names O_1, O_2, ..., O_j with a new name N_i,
157 then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }. */
158 static htab_t repl_tbl;
160 /* true if register_new_name_mapping needs to initialize the data
161 structures needed by update_ssa. */
162 static bool need_to_initialize_update_ssa_p = true;
164 /* true if update_ssa needs to update virtual operands. */
165 static bool need_to_update_vops_p = false;
167 /* Statistics kept by update_ssa to use in the virtual mapping
168 heuristic. If the number of virtual mappings is beyond certain
169 threshold, the updater will switch from using the mappings into
170 renaming the virtual symbols from scratch. In some cases, the
171 large number of name mappings for virtual names causes significant
172 slowdowns in the PHI insertion code. */
173 struct update_ssa_stats_d
175 unsigned num_virtual_mappings;
176 unsigned num_total_mappings;
177 bitmap virtual_symbols;
178 unsigned num_virtual_symbols;
180 static struct update_ssa_stats_d update_ssa_stats;
182 /* Global data to attach to the main dominator walk structure. */
183 struct mark_def_sites_global_data
185 /* This bitmap contains the variables which are set before they
186 are used in a basic block. */
187 bitmap kills;
189 /* Bitmap of names to rename. */
190 sbitmap names_to_rename;
192 /* Set of blocks that mark_def_sites deems interesting for the
193 renamer to process. */
194 sbitmap interesting_blocks;
198 /* Information stored for SSA names. */
199 struct ssa_name_info
201 /* The current reaching definition replacing this SSA name. */
202 tree current_def;
204 /* This field indicates whether or not the variable may need PHI nodes.
205 See the enum's definition for more detailed information about the
206 states. */
207 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
209 /* Age of this record (so that info_for_ssa_name table can be cleared
210 quicky); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
211 are assumed to be null. */
212 unsigned age;
215 /* The information associated with names. */
216 typedef struct ssa_name_info *ssa_name_info_p;
217 DEF_VEC_P (ssa_name_info_p);
218 DEF_VEC_ALLOC_P (ssa_name_info_p, heap);
220 static VEC(ssa_name_info_p, heap) *info_for_ssa_name;
221 static unsigned current_info_for_ssa_name_age;
223 /* The set of blocks affected by update_ssa. */
224 static bitmap blocks_to_update;
226 /* The main entry point to the SSA renamer (rewrite_blocks) may be
227 called several times to do different, but related, tasks.
228 Initially, we need it to rename the whole program into SSA form.
229 At other times, we may need it to only rename into SSA newly
230 exposed symbols. Finally, we can also call it to incrementally fix
231 an already built SSA web. */
232 enum rewrite_mode {
233 /* Convert the whole function into SSA form. */
234 REWRITE_ALL,
236 /* Incrementally update the SSA web by replacing existing SSA
237 names with new ones. See update_ssa for details. */
238 REWRITE_UPDATE
242 /* Use TREE_VISITED to keep track of which statements we want to
243 rename. When renaming a subset of the variables, not all
244 statements will be processed. This is decided in mark_def_sites. */
245 #define REWRITE_THIS_STMT(T) TREE_VISITED (T)
247 /* Use the unsigned flag to keep track of which statements we want to
248 visit when marking new definition sites. This is slightly
249 different than REWRITE_THIS_STMT: it's used by update_ssa to
250 distinguish statements that need to have both uses and defs
251 processed from those that only need to have their defs processed.
252 Statements that define new SSA names only need to have their defs
253 registered, but they don't need to have their uses renamed. */
254 #define REGISTER_DEFS_IN_THIS_STMT(T) (T)->base.unsigned_flag
257 /* Prototypes for debugging functions. */
258 extern void dump_tree_ssa (FILE *);
259 extern void debug_tree_ssa (void);
260 extern void debug_def_blocks (void);
261 extern void dump_tree_ssa_stats (FILE *);
262 extern void debug_tree_ssa_stats (void);
263 extern void dump_update_ssa (FILE *);
264 extern void debug_update_ssa (void);
265 extern void dump_names_replaced_by (FILE *, tree);
266 extern void debug_names_replaced_by (tree);
267 extern void dump_def_blocks (FILE *);
268 extern void debug_def_blocks (void);
269 extern void dump_defs_stack (FILE *, int);
270 extern void debug_defs_stack (int);
271 extern void dump_currdefs (FILE *);
272 extern void debug_currdefs (void);
274 /* Get the information associated with NAME. */
276 static inline ssa_name_info_p
277 get_ssa_name_ann (tree name)
279 unsigned ver = SSA_NAME_VERSION (name);
280 unsigned len = VEC_length (ssa_name_info_p, info_for_ssa_name);
281 struct ssa_name_info *info;
283 if (ver >= len)
285 unsigned new_len = num_ssa_names;
287 VEC_reserve (ssa_name_info_p, heap, info_for_ssa_name, new_len);
288 while (len++ < new_len)
290 struct ssa_name_info *info = XCNEW (struct ssa_name_info);
291 info->age = current_info_for_ssa_name_age;
292 VEC_quick_push (ssa_name_info_p, info_for_ssa_name, info);
296 info = VEC_index (ssa_name_info_p, info_for_ssa_name, ver);
297 if (info->age < current_info_for_ssa_name_age)
299 info->need_phi_state = 0;
300 info->current_def = NULL_TREE;
301 info->age = current_info_for_ssa_name_age;
304 return info;
308 /* Clears info for SSA names. */
310 static void
311 clear_ssa_name_info (void)
313 current_info_for_ssa_name_age++;
317 /* Get phi_state field for VAR. */
319 static inline enum need_phi_state
320 get_phi_state (tree var)
322 if (TREE_CODE (var) == SSA_NAME)
323 return get_ssa_name_ann (var)->need_phi_state;
324 else
325 return var_ann (var)->need_phi_state;
329 /* Sets phi_state field for VAR to STATE. */
331 static inline void
332 set_phi_state (tree var, enum need_phi_state state)
334 if (TREE_CODE (var) == SSA_NAME)
335 get_ssa_name_ann (var)->need_phi_state = state;
336 else
337 var_ann (var)->need_phi_state = state;
341 /* Return the current definition for VAR. */
343 tree
344 get_current_def (tree var)
346 if (TREE_CODE (var) == SSA_NAME)
347 return get_ssa_name_ann (var)->current_def;
348 else
349 return var_ann (var)->current_def;
353 /* Sets current definition of VAR to DEF. */
355 void
356 set_current_def (tree var, tree def)
358 if (TREE_CODE (var) == SSA_NAME)
359 get_ssa_name_ann (var)->current_def = def;
360 else
361 var_ann (var)->current_def = def;
365 /* Compute global livein information given the set of blockx where
366 an object is locally live at the start of the block (LIVEIN)
367 and the set of blocks where the object is defined (DEF_BLOCKS).
369 Note: This routine augments the existing local livein information
370 to include global livein (i.e., it modifies the underlying bitmap
371 for LIVEIN). */
373 void
374 compute_global_livein (bitmap livein, bitmap def_blocks)
376 basic_block bb, *worklist, *tos;
377 unsigned i;
378 bitmap_iterator bi;
380 tos = worklist
381 = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
383 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
384 *tos++ = BASIC_BLOCK (i);
386 /* Iterate until the worklist is empty. */
387 while (tos != worklist)
389 edge e;
390 edge_iterator ei;
392 /* Pull a block off the worklist. */
393 bb = *--tos;
395 /* For each predecessor block. */
396 FOR_EACH_EDGE (e, ei, bb->preds)
398 basic_block pred = e->src;
399 int pred_index = pred->index;
401 /* None of this is necessary for the entry block. */
402 if (pred != ENTRY_BLOCK_PTR
403 && ! bitmap_bit_p (livein, pred_index)
404 && ! bitmap_bit_p (def_blocks, pred_index))
406 *tos++ = pred;
407 bitmap_set_bit (livein, pred_index);
412 free (worklist);
416 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
417 all statements in basic block BB. */
419 static void
420 initialize_flags_in_bb (basic_block bb)
422 tree phi, stmt;
423 block_stmt_iterator bsi;
425 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
427 REWRITE_THIS_STMT (phi) = 0;
428 REGISTER_DEFS_IN_THIS_STMT (phi) = 0;
431 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
433 stmt = bsi_stmt (bsi);
434 /* We are going to use the operand cache API, such as
435 SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST. The operand
436 cache for each statement should be up-to-date. */
437 gcc_assert (!stmt_modified_p (stmt));
438 REWRITE_THIS_STMT (stmt) = 0;
439 REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
443 /* Mark block BB as interesting for update_ssa. */
445 static void
446 mark_block_for_update (basic_block bb)
448 gcc_assert (blocks_to_update != NULL);
449 if (bitmap_bit_p (blocks_to_update, bb->index))
450 return;
451 bitmap_set_bit (blocks_to_update, bb->index);
452 initialize_flags_in_bb (bb);
455 /* Return the set of blocks where variable VAR is defined and the blocks
456 where VAR is live on entry (livein). If no entry is found in
457 DEF_BLOCKS, a new one is created and returned. */
459 static inline struct def_blocks_d *
460 get_def_blocks_for (tree var)
462 struct def_blocks_d db, *db_p;
463 void **slot;
465 db.var = var;
466 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
467 if (*slot == NULL)
469 db_p = XNEW (struct def_blocks_d);
470 db_p->var = var;
471 db_p->def_blocks = BITMAP_ALLOC (NULL);
472 db_p->phi_blocks = BITMAP_ALLOC (NULL);
473 db_p->livein_blocks = BITMAP_ALLOC (NULL);
474 *slot = (void *) db_p;
476 else
477 db_p = (struct def_blocks_d *) *slot;
479 return db_p;
483 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
484 VAR is defined by a PHI node. */
486 static void
487 set_def_block (tree var, basic_block bb, bool phi_p)
489 struct def_blocks_d *db_p;
490 enum need_phi_state state;
492 state = get_phi_state (var);
493 db_p = get_def_blocks_for (var);
495 /* Set the bit corresponding to the block where VAR is defined. */
496 bitmap_set_bit (db_p->def_blocks, bb->index);
497 if (phi_p)
498 bitmap_set_bit (db_p->phi_blocks, bb->index);
500 /* Keep track of whether or not we may need to insert PHI nodes.
502 If we are in the UNKNOWN state, then this is the first definition
503 of VAR. Additionally, we have not seen any uses of VAR yet, so
504 we do not need a PHI node for this variable at this time (i.e.,
505 transition to NEED_PHI_STATE_NO).
507 If we are in any other state, then we either have multiple definitions
508 of this variable occurring in different blocks or we saw a use of the
509 variable which was not dominated by the block containing the
510 definition(s). In this case we may need a PHI node, so enter
511 state NEED_PHI_STATE_MAYBE. */
512 if (state == NEED_PHI_STATE_UNKNOWN)
513 set_phi_state (var, NEED_PHI_STATE_NO);
514 else
515 set_phi_state (var, NEED_PHI_STATE_MAYBE);
519 /* Mark block BB as having VAR live at the entry to BB. */
521 static void
522 set_livein_block (tree var, basic_block bb)
524 struct def_blocks_d *db_p;
525 enum need_phi_state state = get_phi_state (var);
527 db_p = get_def_blocks_for (var);
529 /* Set the bit corresponding to the block where VAR is live in. */
530 bitmap_set_bit (db_p->livein_blocks, bb->index);
532 /* Keep track of whether or not we may need to insert PHI nodes.
534 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
535 by the single block containing the definition(s) of this variable. If
536 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
537 NEED_PHI_STATE_MAYBE. */
538 if (state == NEED_PHI_STATE_NO)
540 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
542 if (def_block_index == -1
543 || ! dominated_by_p (CDI_DOMINATORS, bb,
544 BASIC_BLOCK (def_block_index)))
545 set_phi_state (var, NEED_PHI_STATE_MAYBE);
547 else
548 set_phi_state (var, NEED_PHI_STATE_MAYBE);
552 /* Return true if symbol SYM is marked for renaming. */
554 static inline bool
555 symbol_marked_for_renaming (tree sym)
557 return bitmap_bit_p (syms_to_rename, DECL_UID (sym));
561 /* Return true if NAME is in OLD_SSA_NAMES. */
563 static inline bool
564 is_old_name (tree name)
566 unsigned ver = SSA_NAME_VERSION (name);
567 return ver < new_ssa_names->n_bits && TEST_BIT (old_ssa_names, ver);
571 /* Return true if NAME is in NEW_SSA_NAMES. */
573 static inline bool
574 is_new_name (tree name)
576 unsigned ver = SSA_NAME_VERSION (name);
577 return ver < new_ssa_names->n_bits && TEST_BIT (new_ssa_names, ver);
581 /* Hashing and equality functions for REPL_TBL. */
583 static hashval_t
584 repl_map_hash (const void *p)
586 return htab_hash_pointer ((const void *)((const struct repl_map_d *)p)->name);
589 static int
590 repl_map_eq (const void *p1, const void *p2)
592 return ((const struct repl_map_d *)p1)->name
593 == ((const struct repl_map_d *)p2)->name;
596 static void
597 repl_map_free (void *p)
599 BITMAP_FREE (((struct repl_map_d *)p)->set);
600 free (p);
604 /* Return the names replaced by NEW (i.e., REPL_TBL[NEW].SET). */
606 static inline bitmap
607 names_replaced_by (tree new)
609 struct repl_map_d m;
610 void **slot;
612 m.name = new;
613 slot = htab_find_slot (repl_tbl, (void *) &m, NO_INSERT);
615 /* If N was not registered in the replacement table, return NULL. */
616 if (slot == NULL || *slot == NULL)
617 return NULL;
619 return ((struct repl_map_d *) *slot)->set;
623 /* Add OLD to REPL_TBL[NEW].SET. */
625 static inline void
626 add_to_repl_tbl (tree new, tree old)
628 struct repl_map_d m, *mp;
629 void **slot;
631 m.name = new;
632 slot = htab_find_slot (repl_tbl, (void *) &m, INSERT);
633 if (*slot == NULL)
635 mp = XNEW (struct repl_map_d);
636 mp->name = new;
637 mp->set = BITMAP_ALLOC (NULL);
638 *slot = (void *) mp;
640 else
641 mp = (struct repl_map_d *) *slot;
643 bitmap_set_bit (mp->set, SSA_NAME_VERSION (old));
647 /* Add a new mapping NEW -> OLD REPL_TBL. Every entry N_i in REPL_TBL
648 represents the set of names O_1 ... O_j replaced by N_i. This is
649 used by update_ssa and its helpers to introduce new SSA names in an
650 already formed SSA web. */
652 static void
653 add_new_name_mapping (tree new, tree old)
655 timevar_push (TV_TREE_SSA_INCREMENTAL);
657 /* OLD and NEW must be different SSA names for the same symbol. */
658 gcc_assert (new != old && SSA_NAME_VAR (new) == SSA_NAME_VAR (old));
660 /* If this mapping is for virtual names, we will need to update
661 virtual operands. If this is a mapping for .MEM, then we gather
662 the symbols associated with each name. */
663 if (!is_gimple_reg (new))
665 tree sym;
667 need_to_update_vops_p = true;
669 update_ssa_stats.num_virtual_mappings++;
670 update_ssa_stats.num_virtual_symbols++;
672 /* Keep counts of virtual mappings and symbols to use in the
673 virtual mapping heuristic. If we have large numbers of
674 virtual mappings for a relatively low number of symbols, it
675 will make more sense to rename the symbols from scratch.
676 Otherwise, the insertion of PHI nodes for each of the old
677 names in these mappings will be very slow. */
678 sym = SSA_NAME_VAR (new);
679 bitmap_set_bit (update_ssa_stats.virtual_symbols, DECL_UID (sym));
682 /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
683 caller may have created new names since the set was created. */
684 if (new_ssa_names->n_bits <= num_ssa_names - 1)
686 unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
687 new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
688 old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
691 /* Update the REPL_TBL table. */
692 add_to_repl_tbl (new, old);
694 /* If OLD had already been registered as a new name, then all the
695 names that OLD replaces should also be replaced by NEW. */
696 if (is_new_name (old))
697 bitmap_ior_into (names_replaced_by (new), names_replaced_by (old));
699 /* Register NEW and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
700 respectively. */
701 SET_BIT (new_ssa_names, SSA_NAME_VERSION (new));
702 SET_BIT (old_ssa_names, SSA_NAME_VERSION (old));
704 /* Update mapping counter to use in the virtual mapping heuristic. */
705 update_ssa_stats.num_total_mappings++;
707 timevar_pop (TV_TREE_SSA_INCREMENTAL);
711 /* Call back for walk_dominator_tree used to collect definition sites
712 for every variable in the function. For every statement S in block
715 1- Variables defined by S in the DEFS of S are marked in the bitmap
716 WALK_DATA->GLOBAL_DATA->KILLS.
718 2- If S uses a variable VAR and there is no preceding kill of VAR,
719 then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
721 This information is used to determine which variables are live
722 across block boundaries to reduce the number of PHI nodes
723 we create. */
725 static void
726 mark_def_sites (struct dom_walk_data *walk_data, basic_block bb,
727 block_stmt_iterator bsi)
729 struct mark_def_sites_global_data *gd;
730 bitmap kills;
731 tree stmt, def;
732 use_operand_p use_p;
733 ssa_op_iter iter;
735 stmt = bsi_stmt (bsi);
736 update_stmt_if_modified (stmt);
738 gd = (struct mark_def_sites_global_data *) walk_data->global_data;
739 kills = gd->kills;
741 gcc_assert (blocks_to_update == NULL);
742 REGISTER_DEFS_IN_THIS_STMT (stmt) = 0;
743 REWRITE_THIS_STMT (stmt) = 0;
745 /* If a variable is used before being set, then the variable is live
746 across a block boundary, so mark it live-on-entry to BB. */
747 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
749 tree sym = USE_FROM_PTR (use_p);
750 gcc_assert (DECL_P (sym));
751 if (!bitmap_bit_p (kills, DECL_UID (sym)))
752 set_livein_block (sym, bb);
753 REWRITE_THIS_STMT (stmt) = 1;
756 /* Now process the defs. Mark BB as the definition block and add
757 each def to the set of killed symbols. */
758 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
760 gcc_assert (DECL_P (def));
761 set_def_block (def, bb, false);
762 bitmap_set_bit (kills, DECL_UID (def));
763 REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
766 /* If we found the statement interesting then also mark the block BB
767 as interesting. */
768 if (REWRITE_THIS_STMT (stmt) || REGISTER_DEFS_IN_THIS_STMT (stmt))
769 SET_BIT (gd->interesting_blocks, bb->index);
772 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
773 in the dfs numbering of the dominance tree. */
775 struct dom_dfsnum
777 /* Basic block whose index this entry corresponds to. */
778 unsigned bb_index;
780 /* The dfs number of this node. */
781 unsigned dfs_num;
784 /* Compares two entries of type struct dom_dfsnum by dfs_num field. Callback
785 for qsort. */
787 static int
788 cmp_dfsnum (const void *a, const void *b)
790 const struct dom_dfsnum *da = a;
791 const struct dom_dfsnum *db = b;
793 return (int) da->dfs_num - (int) db->dfs_num;
796 /* Among the intervals starting at the N points specified in DEFS, find
797 the one that contains S, and return its bb_index. */
799 static unsigned
800 find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
802 unsigned f = 0, t = n, m;
804 while (t > f + 1)
806 m = (f + t) / 2;
807 if (defs[m].dfs_num <= s)
808 f = m;
809 else
810 t = m;
813 return defs[f].bb_index;
816 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
817 KILLS is a bitmap of blocks where the value is defined before any use. */
819 static void
820 prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
822 VEC(int, heap) *worklist;
823 bitmap_iterator bi;
824 unsigned i, b, p, u, top;
825 bitmap live_phis;
826 basic_block def_bb, use_bb;
827 edge e;
828 edge_iterator ei;
829 bitmap to_remove;
830 struct dom_dfsnum *defs;
831 unsigned n_defs, adef;
833 if (bitmap_empty_p (uses))
835 bitmap_clear (phis);
836 return;
839 /* The phi must dominate a use, or an argument of a live phi. Also, we
840 do not create any phi nodes in def blocks, unless they are also livein. */
841 to_remove = BITMAP_ALLOC (NULL);
842 bitmap_and_compl (to_remove, kills, uses);
843 bitmap_and_compl_into (phis, to_remove);
844 if (bitmap_empty_p (phis))
846 BITMAP_FREE (to_remove);
847 return;
850 /* We want to remove the unnecessary phi nodes, but we do not want to compute
851 liveness information, as that may be linear in the size of CFG, and if
852 there are lot of different variables to rewrite, this may lead to quadratic
853 behavior.
855 Instead, we basically emulate standard dce. We put all uses to worklist,
856 then for each of them find the nearest def that dominates them. If this
857 def is a phi node, we mark it live, and if it was not live before, we
858 add the predecessors of its basic block to the worklist.
860 To quickly locate the nearest def that dominates use, we use dfs numbering
861 of the dominance tree (that is already available in order to speed up
862 queries). For each def, we have the interval given by the dfs number on
863 entry to and on exit from the corresponding subtree in the dominance tree.
864 The nearest dominator for a given use is the smallest of these intervals
865 that contains entry and exit dfs numbers for the basic block with the use.
866 If we store the bounds for all the uses to an array and sort it, we can
867 locate the nearest dominating def in logarithmic time by binary search.*/
868 bitmap_ior (to_remove, kills, phis);
869 n_defs = bitmap_count_bits (to_remove);
870 defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
871 defs[0].bb_index = 1;
872 defs[0].dfs_num = 0;
873 adef = 1;
874 EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
876 def_bb = BASIC_BLOCK (i);
877 defs[adef].bb_index = i;
878 defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
879 defs[adef + 1].bb_index = i;
880 defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
881 adef += 2;
883 BITMAP_FREE (to_remove);
884 gcc_assert (adef == 2 * n_defs + 1);
885 qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
886 gcc_assert (defs[0].bb_index == 1);
888 /* Now each DEFS entry contains the number of the basic block to that the
889 dfs number corresponds. Change them to the number of basic block that
890 corresponds to the interval following the dfs number. Also, for the
891 dfs_out numbers, increase the dfs number by one (so that it corresponds
892 to the start of the following interval, not to the end of the current
893 one). We use WORKLIST as a stack. */
894 worklist = VEC_alloc (int, heap, n_defs + 1);
895 VEC_quick_push (int, worklist, 1);
896 top = 1;
897 n_defs = 1;
898 for (i = 1; i < adef; i++)
900 b = defs[i].bb_index;
901 if (b == top)
903 /* This is a closing element. Interval corresponding to the top
904 of the stack after removing it follows. */
905 VEC_pop (int, worklist);
906 top = VEC_index (int, worklist, VEC_length (int, worklist) - 1);
907 defs[n_defs].bb_index = top;
908 defs[n_defs].dfs_num = defs[i].dfs_num + 1;
910 else
912 /* Opening element. Nothing to do, just push it to the stack and move
913 it to the correct position. */
914 defs[n_defs].bb_index = defs[i].bb_index;
915 defs[n_defs].dfs_num = defs[i].dfs_num;
916 VEC_quick_push (int, worklist, b);
917 top = b;
920 /* If this interval starts at the same point as the previous one, cancel
921 the previous one. */
922 if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
923 defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
924 else
925 n_defs++;
927 VEC_pop (int, worklist);
928 gcc_assert (VEC_empty (int, worklist));
930 /* Now process the uses. */
931 live_phis = BITMAP_ALLOC (NULL);
932 EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
934 VEC_safe_push (int, heap, worklist, i);
937 while (!VEC_empty (int, worklist))
939 b = VEC_pop (int, worklist);
940 if (b == ENTRY_BLOCK)
941 continue;
943 /* If there is a phi node in USE_BB, it is made live. Otherwise,
944 find the def that dominates the immediate dominator of USE_BB
945 (the kill in USE_BB does not dominate the use). */
946 if (bitmap_bit_p (phis, b))
947 p = b;
948 else
950 use_bb = get_immediate_dominator (CDI_DOMINATORS, BASIC_BLOCK (b));
951 p = find_dfsnum_interval (defs, n_defs,
952 bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
953 if (!bitmap_bit_p (phis, p))
954 continue;
957 /* If the phi node is already live, there is nothing to do. */
958 if (bitmap_bit_p (live_phis, p))
959 continue;
961 /* Mark the phi as live, and add the new uses to the worklist. */
962 bitmap_set_bit (live_phis, p);
963 def_bb = BASIC_BLOCK (p);
964 FOR_EACH_EDGE (e, ei, def_bb->preds)
966 u = e->src->index;
967 if (bitmap_bit_p (uses, u))
968 continue;
970 /* In case there is a kill directly in the use block, do not record
971 the use (this is also necessary for correctness, as we assume that
972 uses dominated by a def directly in their block have been filtered
973 out before). */
974 if (bitmap_bit_p (kills, u))
975 continue;
977 bitmap_set_bit (uses, u);
978 VEC_safe_push (int, heap, worklist, u);
982 VEC_free (int, heap, worklist);
983 bitmap_copy (phis, live_phis);
984 BITMAP_FREE (live_phis);
985 free (defs);
988 /* Given a set of blocks with variable definitions (DEF_BLOCKS),
989 return a bitmap with all the blocks in the iterated dominance
990 frontier of the blocks in DEF_BLOCKS. DFS contains dominance
991 frontier information as returned by compute_dominance_frontiers.
993 The resulting set of blocks are the potential sites where PHI nodes
994 are needed. The caller is responsible for freeing the memory
995 allocated for the return value. */
997 static bitmap
998 compute_idf (bitmap def_blocks, bitmap *dfs)
1000 bitmap_iterator bi;
1001 unsigned bb_index, i;
1002 VEC(int,heap) *work_stack;
1003 bitmap phi_insertion_points;
1005 work_stack = VEC_alloc (int, heap, n_basic_blocks);
1006 phi_insertion_points = BITMAP_ALLOC (NULL);
1008 /* Seed the work list with all the blocks in DEF_BLOCKS. We use
1009 VEC_quick_push here for speed. This is safe because we know that
1010 the number of definition blocks is no greater than the number of
1011 basic blocks, which is the initial capacity of WORK_STACK. */
1012 EXECUTE_IF_SET_IN_BITMAP (def_blocks, 0, bb_index, bi)
1013 VEC_quick_push (int, work_stack, bb_index);
1015 /* Pop a block off the worklist, add every block that appears in
1016 the original block's DF that we have not already processed to
1017 the worklist. Iterate until the worklist is empty. Blocks
1018 which are added to the worklist are potential sites for
1019 PHI nodes. */
1020 while (VEC_length (int, work_stack) > 0)
1022 bb_index = VEC_pop (int, work_stack);
1024 /* Since the registration of NEW -> OLD name mappings is done
1025 separately from the call to update_ssa, when updating the SSA
1026 form, the basic blocks where new and/or old names are defined
1027 may have disappeared by CFG cleanup calls. In this case,
1028 we may pull a non-existing block from the work stack. */
1029 gcc_assert (bb_index < (unsigned) last_basic_block);
1031 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index], phi_insertion_points,
1032 0, i, bi)
1034 /* Use a safe push because if there is a definition of VAR
1035 in every basic block, then WORK_STACK may eventually have
1036 more than N_BASIC_BLOCK entries. */
1037 VEC_safe_push (int, heap, work_stack, i);
1038 bitmap_set_bit (phi_insertion_points, i);
1042 VEC_free (int, heap, work_stack);
1044 return phi_insertion_points;
1048 /* Return the set of blocks where variable VAR is defined and the blocks
1049 where VAR is live on entry (livein). Return NULL, if no entry is
1050 found in DEF_BLOCKS. */
1052 static inline struct def_blocks_d *
1053 find_def_blocks_for (tree var)
1055 struct def_blocks_d dm;
1056 dm.var = var;
1057 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1061 /* Retrieve or create a default definition for symbol SYM. */
1063 static inline tree
1064 get_default_def_for (tree sym)
1066 tree ddef = gimple_default_def (cfun, sym);
1068 if (ddef == NULL_TREE)
1070 ddef = make_ssa_name (sym, build_empty_stmt ());
1071 set_default_def (sym, ddef);
1074 return ddef;
1078 /* Marks phi node PHI in basic block BB for rewrite. */
1080 static void
1081 mark_phi_for_rewrite (basic_block bb, tree phi)
1083 tree_vec phis;
1084 unsigned i, idx = bb->index;
1086 if (REWRITE_THIS_STMT (phi))
1087 return;
1089 REWRITE_THIS_STMT (phi) = 1;
1091 if (!blocks_with_phis_to_rewrite)
1092 return;
1094 bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
1095 VEC_reserve (tree_vec, heap, phis_to_rewrite, last_basic_block + 1);
1096 for (i = VEC_length (tree_vec, phis_to_rewrite); i <= idx; i++)
1097 VEC_quick_push (tree_vec, phis_to_rewrite, NULL);
1099 phis = VEC_index (tree_vec, phis_to_rewrite, idx);
1100 if (!phis)
1101 phis = VEC_alloc (tree, heap, 10);
1103 VEC_safe_push (tree, heap, phis, phi);
1104 VEC_replace (tree_vec, phis_to_rewrite, idx, phis);
1108 /* Insert PHI nodes for variable VAR using the iterated dominance
1109 frontier given in PHI_INSERTION_POINTS. If UPDATE_P is true, this
1110 function assumes that the caller is incrementally updating the
1111 existing SSA form, in which case VAR may be an SSA name instead of
1112 a symbol.
1114 PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1115 PHI node for VAR. On exit, only the nodes that received a PHI node
1116 for VAR will be present in PHI_INSERTION_POINTS. */
1118 static void
1119 insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
1121 unsigned bb_index;
1122 edge e;
1123 tree phi;
1124 basic_block bb;
1125 bitmap_iterator bi;
1126 struct def_blocks_d *def_map;
1128 def_map = find_def_blocks_for (var);
1129 gcc_assert (def_map);
1131 /* Remove the blocks where we already have PHI nodes for VAR. */
1132 bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1134 /* Remove obviously useless phi nodes. */
1135 prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
1136 def_map->livein_blocks);
1138 /* And insert the PHI nodes. */
1139 EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
1141 bb = BASIC_BLOCK (bb_index);
1142 if (update_p)
1143 mark_block_for_update (bb);
1145 phi = NULL_TREE;
1147 if (TREE_CODE (var) == SSA_NAME)
1149 /* If we are rewriting SSA names, create the LHS of the PHI
1150 node by duplicating VAR. This is useful in the case of
1151 pointers, to also duplicate pointer attributes (alias
1152 information, in particular). */
1153 edge_iterator ei;
1154 tree new_lhs;
1156 gcc_assert (update_p);
1157 phi = create_phi_node (var, bb);
1159 new_lhs = duplicate_ssa_name (var, phi);
1160 SET_PHI_RESULT (phi, new_lhs);
1161 add_new_name_mapping (new_lhs, var);
1163 /* Add VAR to every argument slot of PHI. We need VAR in
1164 every argument so that rewrite_update_phi_arguments knows
1165 which name is this PHI node replacing. If VAR is a
1166 symbol marked for renaming, this is not necessary, the
1167 renamer will use the symbol on the LHS to get its
1168 reaching definition. */
1169 FOR_EACH_EDGE (e, ei, bb->preds)
1170 add_phi_arg (phi, var, e);
1172 else
1174 tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1175 phi = create_phi_node (sym, bb);
1178 /* Mark this PHI node as interesting for update_ssa. */
1179 REGISTER_DEFS_IN_THIS_STMT (phi) = 1;
1180 mark_phi_for_rewrite (bb, phi);
1185 /* Insert PHI nodes at the dominance frontier of blocks with variable
1186 definitions. DFS contains the dominance frontier information for
1187 the flowgraph. */
1189 static void
1190 insert_phi_nodes (bitmap *dfs)
1192 referenced_var_iterator rvi;
1193 tree var;
1195 timevar_push (TV_TREE_INSERT_PHI_NODES);
1197 FOR_EACH_REFERENCED_VAR (var, rvi)
1199 struct def_blocks_d *def_map;
1200 bitmap idf;
1202 def_map = find_def_blocks_for (var);
1203 if (def_map == NULL)
1204 continue;
1206 if (get_phi_state (var) != NEED_PHI_STATE_NO)
1208 idf = compute_idf (def_map->def_blocks, dfs);
1209 insert_phi_nodes_for (var, idf, false);
1210 BITMAP_FREE (idf);
1214 timevar_pop (TV_TREE_INSERT_PHI_NODES);
1218 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1219 register DEF (an SSA_NAME) to be a new definition for SYM. */
1221 static void
1222 register_new_def (tree def, tree sym)
1224 tree currdef;
1226 /* If this variable is set in a single basic block and all uses are
1227 dominated by the set(s) in that single basic block, then there is
1228 no reason to record anything for this variable in the block local
1229 definition stacks. Doing so just wastes time and memory.
1231 This is the same test to prune the set of variables which may
1232 need PHI nodes. So we just use that information since it's already
1233 computed and available for us to use. */
1234 if (get_phi_state (sym) == NEED_PHI_STATE_NO)
1236 set_current_def (sym, def);
1237 return;
1240 currdef = get_current_def (sym);
1242 /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1243 SSA_NAME_VAR is not necessarily SYM. In this case, also push SYM
1244 in the stack so that we know which symbol is being defined by
1245 this SSA name when we unwind the stack. */
1246 if (currdef && !is_gimple_reg (sym))
1247 VEC_safe_push (tree, heap, block_defs_stack, sym);
1249 /* Push the current reaching definition into BLOCK_DEFS_STACK. This
1250 stack is later used by the dominator tree callbacks to restore
1251 the reaching definitions for all the variables defined in the
1252 block after a recursive visit to all its immediately dominated
1253 blocks. If there is no current reaching definition, then just
1254 record the underlying _DECL node. */
1255 VEC_safe_push (tree, heap, block_defs_stack, currdef ? currdef : sym);
1257 /* Set the current reaching definition for SYM to be DEF. */
1258 set_current_def (sym, def);
1262 /* Perform a depth-first traversal of the dominator tree looking for
1263 variables to rename. BB is the block where to start searching.
1264 Renaming is a five step process:
1266 1- Every definition made by PHI nodes at the start of the blocks is
1267 registered as the current definition for the corresponding variable.
1269 2- Every statement in BB is rewritten. USE and VUSE operands are
1270 rewritten with their corresponding reaching definition. DEF and
1271 VDEF targets are registered as new definitions.
1273 3- All the PHI nodes in successor blocks of BB are visited. The
1274 argument corresponding to BB is replaced with its current reaching
1275 definition.
1277 4- Recursively rewrite every dominator child block of BB.
1279 5- Restore (in reverse order) the current reaching definition for every
1280 new definition introduced in this block. This is done so that when
1281 we return from the recursive call, all the current reaching
1282 definitions are restored to the names that were valid in the
1283 dominator parent of BB. */
1285 /* SSA Rewriting Step 1. Initialization, create a block local stack
1286 of reaching definitions for new SSA names produced in this block
1287 (BLOCK_DEFS). Register new definitions for every PHI node in the
1288 block. */
1290 static void
1291 rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1292 basic_block bb)
1294 tree phi;
1296 if (dump_file && (dump_flags & TDF_DETAILS))
1297 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1299 /* Mark the unwind point for this block. */
1300 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1302 /* Step 1. Register new definitions for every PHI node in the block.
1303 Conceptually, all the PHI nodes are executed in parallel and each PHI
1304 node introduces a new version for the associated variable. */
1305 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1307 tree result = PHI_RESULT (phi);
1308 gcc_assert (is_gimple_reg (result));
1309 register_new_def (result, SSA_NAME_VAR (result));
1314 /* Return the current definition for variable VAR. If none is found,
1315 create a new SSA name to act as the zeroth definition for VAR. */
1317 static tree
1318 get_reaching_def (tree var)
1320 tree currdef;
1322 /* Lookup the current reaching definition for VAR. */
1323 currdef = get_current_def (var);
1325 /* If there is no reaching definition for VAR, create and register a
1326 default definition for it (if needed). */
1327 if (currdef == NULL_TREE)
1329 tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1330 currdef = get_default_def_for (sym);
1331 set_current_def (var, currdef);
1334 /* Return the current reaching definition for VAR, or the default
1335 definition, if we had to create one. */
1336 return currdef;
1340 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1341 the block with its immediate reaching definitions. Update the current
1342 definition of a variable when a new real or virtual definition is found. */
1344 static void
1345 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1346 basic_block bb ATTRIBUTE_UNUSED, block_stmt_iterator si)
1348 tree stmt;
1349 use_operand_p use_p;
1350 def_operand_p def_p;
1351 ssa_op_iter iter;
1353 stmt = bsi_stmt (si);
1355 /* If mark_def_sites decided that we don't need to rewrite this
1356 statement, ignore it. */
1357 gcc_assert (blocks_to_update == NULL);
1358 if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
1359 return;
1361 if (dump_file && (dump_flags & TDF_DETAILS))
1363 fprintf (dump_file, "Renaming statement ");
1364 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1365 fprintf (dump_file, "\n");
1368 /* Step 1. Rewrite USES in the statement. */
1369 if (REWRITE_THIS_STMT (stmt))
1370 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1372 tree var = USE_FROM_PTR (use_p);
1373 gcc_assert (DECL_P (var));
1374 SET_USE (use_p, get_reaching_def (var));
1377 /* Step 2. Register the statement's DEF operands. */
1378 if (REGISTER_DEFS_IN_THIS_STMT (stmt))
1379 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1381 tree var = DEF_FROM_PTR (def_p);
1382 gcc_assert (DECL_P (var));
1383 SET_DEF (def_p, make_ssa_name (var, stmt));
1384 register_new_def (DEF_FROM_PTR (def_p), var);
1389 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
1390 PHI nodes. For every PHI node found, add a new argument containing the
1391 current reaching definition for the variable and the edge through which
1392 that definition is reaching the PHI node. */
1394 static void
1395 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1396 basic_block bb)
1398 edge e;
1399 edge_iterator ei;
1401 FOR_EACH_EDGE (e, ei, bb->succs)
1403 tree phi;
1405 for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
1407 tree currdef;
1408 currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
1409 add_phi_arg (phi, currdef, e);
1415 /* Called after visiting all the statements in basic block BB and all
1416 of its dominator children. Restore CURRDEFS to its original value. */
1418 static void
1419 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1420 basic_block bb ATTRIBUTE_UNUSED)
1422 /* Restore CURRDEFS to its original state. */
1423 while (VEC_length (tree, block_defs_stack) > 0)
1425 tree tmp = VEC_pop (tree, block_defs_stack);
1426 tree saved_def, var;
1428 if (tmp == NULL_TREE)
1429 break;
1431 if (TREE_CODE (tmp) == SSA_NAME)
1433 /* If we recorded an SSA_NAME, then make the SSA_NAME the
1434 current definition of its underlying variable. Note that
1435 if the SSA_NAME is not for a GIMPLE register, the symbol
1436 being defined is stored in the next slot in the stack.
1437 This mechanism is needed because an SSA name for a
1438 non-register symbol may be the definition for more than
1439 one symbol (e.g., SFTs, aliased variables, etc). */
1440 saved_def = tmp;
1441 var = SSA_NAME_VAR (saved_def);
1442 if (!is_gimple_reg (var))
1443 var = VEC_pop (tree, block_defs_stack);
1445 else
1447 /* If we recorded anything else, it must have been a _DECL
1448 node and its current reaching definition must have been
1449 NULL. */
1450 saved_def = NULL;
1451 var = tmp;
1454 set_current_def (var, saved_def);
1459 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1461 void
1462 dump_decl_set (FILE *file, bitmap set)
1464 if (set)
1466 bitmap_iterator bi;
1467 unsigned i;
1469 fprintf (file, "{ ");
1471 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
1473 print_generic_expr (file, referenced_var (i), 0);
1474 fprintf (file, " ");
1477 fprintf (file, "}\n");
1479 else
1480 fprintf (file, "NIL\n");
1484 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE. */
1486 void
1487 debug_decl_set (bitmap set)
1489 dump_decl_set (stderr, set);
1493 /* Dump the renaming stack (block_defs_stack) to FILE. Traverse the
1494 stack up to a maximum of N levels. If N is -1, the whole stack is
1495 dumped. New levels are created when the dominator tree traversal
1496 used for renaming enters a new sub-tree. */
1498 void
1499 dump_defs_stack (FILE *file, int n)
1501 int i, j;
1503 fprintf (file, "\n\nRenaming stack");
1504 if (n > 0)
1505 fprintf (file, " (up to %d levels)", n);
1506 fprintf (file, "\n\n");
1508 i = 1;
1509 fprintf (file, "Level %d (current level)\n", i);
1510 for (j = (int) VEC_length (tree, block_defs_stack) - 1; j >= 0; j--)
1512 tree name, var;
1514 name = VEC_index (tree, block_defs_stack, j);
1515 if (name == NULL_TREE)
1517 i++;
1518 if (n > 0 && i > n)
1519 break;
1520 fprintf (file, "\nLevel %d\n", i);
1521 continue;
1524 if (DECL_P (name))
1526 var = name;
1527 name = NULL_TREE;
1529 else
1531 var = SSA_NAME_VAR (name);
1532 if (!is_gimple_reg (var))
1534 j--;
1535 var = VEC_index (tree, block_defs_stack, j);
1539 fprintf (file, " Previous CURRDEF (");
1540 print_generic_expr (file, var, 0);
1541 fprintf (file, ") = ");
1542 if (name)
1543 print_generic_expr (file, name, 0);
1544 else
1545 fprintf (file, "<NIL>");
1546 fprintf (file, "\n");
1551 /* Dump the renaming stack (block_defs_stack) to stderr. Traverse the
1552 stack up to a maximum of N levels. If N is -1, the whole stack is
1553 dumped. New levels are created when the dominator tree traversal
1554 used for renaming enters a new sub-tree. */
1556 void
1557 debug_defs_stack (int n)
1559 dump_defs_stack (stderr, n);
1563 /* Dump the current reaching definition of every symbol to FILE. */
1565 void
1566 dump_currdefs (FILE *file)
1568 referenced_var_iterator i;
1569 tree var;
1571 fprintf (file, "\n\nCurrent reaching definitions\n\n");
1572 FOR_EACH_REFERENCED_VAR (var, i)
1573 if (syms_to_rename == NULL || bitmap_bit_p (syms_to_rename, DECL_UID (var)))
1575 fprintf (file, "CURRDEF (");
1576 print_generic_expr (file, var, 0);
1577 fprintf (file, ") = ");
1578 if (get_current_def (var))
1579 print_generic_expr (file, get_current_def (var), 0);
1580 else
1581 fprintf (file, "<NIL>");
1582 fprintf (file, "\n");
1587 /* Dump the current reaching definition of every symbol to stderr. */
1589 void
1590 debug_currdefs (void)
1592 dump_currdefs (stderr);
1596 /* Dump SSA information to FILE. */
1598 void
1599 dump_tree_ssa (FILE *file)
1601 const char *funcname
1602 = lang_hooks.decl_printable_name (current_function_decl, 2);
1604 fprintf (file, "SSA renaming information for %s\n\n", funcname);
1606 dump_def_blocks (file);
1607 dump_defs_stack (file, -1);
1608 dump_currdefs (file);
1609 dump_tree_ssa_stats (file);
1613 /* Dump SSA information to stderr. */
1615 void
1616 debug_tree_ssa (void)
1618 dump_tree_ssa (stderr);
1622 /* Dump statistics for the hash table HTAB. */
1624 static void
1625 htab_statistics (FILE *file, htab_t htab)
1627 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1628 (long) htab_size (htab),
1629 (long) htab_elements (htab),
1630 htab_collisions (htab));
1634 /* Dump SSA statistics on FILE. */
1636 void
1637 dump_tree_ssa_stats (FILE *file)
1639 if (def_blocks || repl_tbl)
1640 fprintf (file, "\nHash table statistics:\n");
1642 if (def_blocks)
1644 fprintf (file, " def_blocks: ");
1645 htab_statistics (file, def_blocks);
1648 if (repl_tbl)
1650 fprintf (file, " repl_tbl: ");
1651 htab_statistics (file, repl_tbl);
1654 if (def_blocks || repl_tbl)
1655 fprintf (file, "\n");
1659 /* Dump SSA statistics on stderr. */
1661 void
1662 debug_tree_ssa_stats (void)
1664 dump_tree_ssa_stats (stderr);
1668 /* Hashing and equality functions for DEF_BLOCKS. */
1670 static hashval_t
1671 def_blocks_hash (const void *p)
1673 return htab_hash_pointer
1674 ((const void *)((const struct def_blocks_d *)p)->var);
1677 static int
1678 def_blocks_eq (const void *p1, const void *p2)
1680 return ((const struct def_blocks_d *)p1)->var
1681 == ((const struct def_blocks_d *)p2)->var;
1685 /* Free memory allocated by one entry in DEF_BLOCKS. */
1687 static void
1688 def_blocks_free (void *p)
1690 struct def_blocks_d *entry = (struct def_blocks_d *) p;
1691 BITMAP_FREE (entry->def_blocks);
1692 BITMAP_FREE (entry->phi_blocks);
1693 BITMAP_FREE (entry->livein_blocks);
1694 free (entry);
1698 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1700 static int
1701 debug_def_blocks_r (void **slot, void *data)
1703 FILE *file = (FILE *) data;
1704 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1706 fprintf (file, "VAR: ");
1707 print_generic_expr (file, db_p->var, dump_flags);
1708 bitmap_print (file, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1709 bitmap_print (file, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}");
1710 bitmap_print (file, db_p->phi_blocks, ", PHI_BLOCKS: { ", "}\n");
1712 return 1;
1716 /* Dump the DEF_BLOCKS hash table on FILE. */
1718 void
1719 dump_def_blocks (FILE *file)
1721 fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
1722 if (def_blocks)
1723 htab_traverse (def_blocks, debug_def_blocks_r, file);
1727 /* Dump the DEF_BLOCKS hash table on stderr. */
1729 void
1730 debug_def_blocks (void)
1732 dump_def_blocks (stderr);
1736 /* Register NEW_NAME to be the new reaching definition for OLD_NAME. */
1738 static inline void
1739 register_new_update_single (tree new_name, tree old_name)
1741 tree currdef = get_current_def (old_name);
1743 /* Push the current reaching definition into BLOCK_DEFS_STACK.
1744 This stack is later used by the dominator tree callbacks to
1745 restore the reaching definitions for all the variables
1746 defined in the block after a recursive visit to all its
1747 immediately dominated blocks. */
1748 VEC_reserve (tree, heap, block_defs_stack, 2);
1749 VEC_quick_push (tree, block_defs_stack, currdef);
1750 VEC_quick_push (tree, block_defs_stack, old_name);
1752 /* Set the current reaching definition for OLD_NAME to be
1753 NEW_NAME. */
1754 set_current_def (old_name, new_name);
1758 /* Register NEW_NAME to be the new reaching definition for all the
1759 names in OLD_NAMES. Used by the incremental SSA update routines to
1760 replace old SSA names with new ones. */
1762 static inline void
1763 register_new_update_set (tree new_name, bitmap old_names)
1765 bitmap_iterator bi;
1766 unsigned i;
1768 EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1769 register_new_update_single (new_name, ssa_name (i));
1773 /* Initialization of block data structures for the incremental SSA
1774 update pass. Create a block local stack of reaching definitions
1775 for new SSA names produced in this block (BLOCK_DEFS). Register
1776 new definitions for every PHI node in the block. */
1778 static void
1779 rewrite_update_init_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1780 basic_block bb)
1782 edge e;
1783 edge_iterator ei;
1784 tree phi;
1785 bool is_abnormal_phi;
1787 if (dump_file && (dump_flags & TDF_DETAILS))
1788 fprintf (dump_file, "\n\nRegistering new PHI nodes in block #%d\n\n",
1789 bb->index);
1791 /* Mark the unwind point for this block. */
1792 VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1794 if (!bitmap_bit_p (blocks_to_update, bb->index))
1795 return;
1797 /* Mark the LHS if any of the arguments flows through an abnormal
1798 edge. */
1799 is_abnormal_phi = false;
1800 FOR_EACH_EDGE (e, ei, bb->preds)
1801 if (e->flags & EDGE_ABNORMAL)
1803 is_abnormal_phi = true;
1804 break;
1807 /* If any of the PHI nodes is a replacement for a name in
1808 OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
1809 register it as a new definition for its corresponding name. Also
1810 register definitions for names whose underlying symbols are
1811 marked for renaming. */
1812 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
1814 tree lhs, lhs_sym;
1816 if (!REGISTER_DEFS_IN_THIS_STMT (phi))
1817 continue;
1819 lhs = PHI_RESULT (phi);
1820 lhs_sym = SSA_NAME_VAR (lhs);
1822 if (symbol_marked_for_renaming (lhs_sym))
1823 register_new_update_single (lhs, lhs_sym);
1824 else
1827 /* If LHS is a new name, register a new definition for all
1828 the names replaced by LHS. */
1829 if (is_new_name (lhs))
1830 register_new_update_set (lhs, names_replaced_by (lhs));
1832 /* If LHS is an OLD name, register it as a new definition
1833 for itself. */
1834 if (is_old_name (lhs))
1835 register_new_update_single (lhs, lhs);
1838 if (is_abnormal_phi)
1839 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
1844 /* Called after visiting block BB. Unwind BLOCK_DEFS_STACK to restore
1845 the current reaching definition of every name re-written in BB to
1846 the original reaching definition before visiting BB. This
1847 unwinding must be done in the opposite order to what is done in
1848 register_new_update_set. */
1850 static void
1851 rewrite_update_fini_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1852 basic_block bb ATTRIBUTE_UNUSED)
1854 while (VEC_length (tree, block_defs_stack) > 0)
1856 tree var = VEC_pop (tree, block_defs_stack);
1857 tree saved_def;
1859 /* NULL indicates the unwind stop point for this block (see
1860 rewrite_update_init_block). */
1861 if (var == NULL)
1862 return;
1864 saved_def = VEC_pop (tree, block_defs_stack);
1865 set_current_def (var, saved_def);
1870 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1871 it is a symbol marked for renaming, replace it with USE_P's current
1872 reaching definition. */
1874 static inline void
1875 maybe_replace_use (use_operand_p use_p)
1877 tree rdef = NULL_TREE;
1878 tree use = USE_FROM_PTR (use_p);
1879 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1881 if (symbol_marked_for_renaming (sym))
1882 rdef = get_reaching_def (sym);
1883 else if (is_old_name (use))
1884 rdef = get_reaching_def (use);
1886 if (rdef && rdef != use)
1887 SET_USE (use_p, rdef);
1891 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1892 or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1893 register it as the current definition for the names replaced by
1894 DEF_P. */
1896 static inline void
1897 maybe_register_def (def_operand_p def_p, tree stmt)
1899 tree def = DEF_FROM_PTR (def_p);
1900 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
1902 /* If DEF is a naked symbol that needs renaming, create a new
1903 name for it. */
1904 if (symbol_marked_for_renaming (sym))
1906 if (DECL_P (def))
1908 def = make_ssa_name (def, stmt);
1909 SET_DEF (def_p, def);
1912 register_new_update_single (def, sym);
1914 else
1916 /* If DEF is a new name, register it as a new definition
1917 for all the names replaced by DEF. */
1918 if (is_new_name (def))
1919 register_new_update_set (def, names_replaced_by (def));
1921 /* If DEF is an old name, register DEF as a new
1922 definition for itself. */
1923 if (is_old_name (def))
1924 register_new_update_single (def, def);
1929 /* Update every variable used in the statement pointed-to by SI. The
1930 statement is assumed to be in SSA form already. Names in
1931 OLD_SSA_NAMES used by SI will be updated to their current reaching
1932 definition. Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1933 will be registered as a new definition for their corresponding name
1934 in OLD_SSA_NAMES. */
1936 static void
1937 rewrite_update_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1938 basic_block bb ATTRIBUTE_UNUSED,
1939 block_stmt_iterator si)
1941 stmt_ann_t ann;
1942 tree stmt;
1943 use_operand_p use_p;
1944 def_operand_p def_p;
1945 ssa_op_iter iter;
1947 stmt = bsi_stmt (si);
1948 ann = stmt_ann (stmt);
1950 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
1952 /* Only update marked statements. */
1953 if (!REWRITE_THIS_STMT (stmt) && !REGISTER_DEFS_IN_THIS_STMT (stmt))
1954 return;
1956 if (dump_file && (dump_flags & TDF_DETAILS))
1958 fprintf (dump_file, "Updating SSA information for statement ");
1959 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1960 fprintf (dump_file, "\n");
1963 /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1964 symbol is marked for renaming. */
1965 if (REWRITE_THIS_STMT (stmt))
1967 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1968 maybe_replace_use (use_p);
1970 if (need_to_update_vops_p)
1971 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_VIRTUAL_USES)
1972 maybe_replace_use (use_p);
1975 /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1976 Also register definitions for names whose underlying symbol is
1977 marked for renaming. */
1978 if (REGISTER_DEFS_IN_THIS_STMT (stmt))
1980 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1981 maybe_register_def (def_p, stmt);
1983 if (need_to_update_vops_p)
1984 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_VIRTUAL_DEFS)
1985 maybe_register_def (def_p, stmt);
1990 /* Visit all the successor blocks of BB looking for PHI nodes. For
1991 every PHI node found, check if any of its arguments is in
1992 OLD_SSA_NAMES. If so, and if the argument has a current reaching
1993 definition, replace it. */
1995 static void
1996 rewrite_update_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1997 basic_block bb)
1999 edge e;
2000 edge_iterator ei;
2001 unsigned i;
2003 FOR_EACH_EDGE (e, ei, bb->succs)
2005 tree phi;
2006 tree_vec phis;
2008 if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
2009 continue;
2011 phis = VEC_index (tree_vec, phis_to_rewrite, e->dest->index);
2012 for (i = 0; VEC_iterate (tree, phis, i, phi); i++)
2014 tree arg, lhs_sym;
2015 use_operand_p arg_p;
2017 gcc_assert (REWRITE_THIS_STMT (phi));
2019 arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
2020 arg = USE_FROM_PTR (arg_p);
2022 if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
2023 continue;
2025 lhs_sym = SSA_NAME_VAR (PHI_RESULT (phi));
2027 if (arg == NULL_TREE)
2029 /* When updating a PHI node for a recently introduced
2030 symbol we may find NULL arguments. That's why we
2031 take the symbol from the LHS of the PHI node. */
2032 SET_USE (arg_p, get_reaching_def (lhs_sym));
2034 else
2036 tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
2038 if (symbol_marked_for_renaming (sym))
2039 SET_USE (arg_p, get_reaching_def (sym));
2040 else if (is_old_name (arg))
2041 SET_USE (arg_p, get_reaching_def (arg));
2044 if (e->flags & EDGE_ABNORMAL)
2045 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
2051 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2052 form.
2054 ENTRY indicates the block where to start. Every block dominated by
2055 ENTRY will be rewritten.
2057 WHAT indicates what actions will be taken by the renamer (see enum
2058 rewrite_mode).
2060 BLOCKS are the set of interesting blocks for the dominator walker
2061 to process. If this set is NULL, then all the nodes dominated
2062 by ENTRY are walked. Otherwise, blocks dominated by ENTRY that
2063 are not present in BLOCKS are ignored. */
2065 static void
2066 rewrite_blocks (basic_block entry, enum rewrite_mode what, sbitmap blocks)
2068 struct dom_walk_data walk_data;
2070 /* Rewrite all the basic blocks in the program. */
2071 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
2073 /* Setup callbacks for the generic dominator tree walker. */
2074 memset (&walk_data, 0, sizeof (walk_data));
2076 walk_data.dom_direction = CDI_DOMINATORS;
2077 walk_data.interesting_blocks = blocks;
2079 if (what == REWRITE_ALL)
2080 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
2081 else
2082 walk_data.before_dom_children_before_stmts = rewrite_update_init_block;
2084 if (what == REWRITE_ALL)
2085 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
2086 else if (what == REWRITE_UPDATE)
2087 walk_data.before_dom_children_walk_stmts = rewrite_update_stmt;
2088 else
2089 gcc_unreachable ();
2091 if (what == REWRITE_ALL)
2092 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
2093 else if (what == REWRITE_UPDATE)
2094 walk_data.before_dom_children_after_stmts = rewrite_update_phi_arguments;
2095 else
2096 gcc_unreachable ();
2098 if (what == REWRITE_ALL)
2099 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
2100 else if (what == REWRITE_UPDATE)
2101 walk_data.after_dom_children_after_stmts = rewrite_update_fini_block;
2102 else
2103 gcc_unreachable ();
2105 block_defs_stack = VEC_alloc (tree, heap, 10);
2107 /* Initialize the dominator walker. */
2108 init_walk_dominator_tree (&walk_data);
2110 /* Recursively walk the dominator tree rewriting each statement in
2111 each basic block. */
2112 walk_dominator_tree (&walk_data, entry);
2114 /* Finalize the dominator walker. */
2115 fini_walk_dominator_tree (&walk_data);
2117 /* Debugging dumps. */
2118 if (dump_file && (dump_flags & TDF_STATS))
2120 dump_dfa_stats (dump_file);
2121 if (def_blocks)
2122 dump_tree_ssa_stats (dump_file);
2125 VEC_free (tree, heap, block_defs_stack);
2127 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
2131 /* Block initialization routine for mark_def_sites. Clear the
2132 KILLS bitmap at the start of each block. */
2134 static void
2135 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
2136 basic_block bb ATTRIBUTE_UNUSED)
2138 struct mark_def_sites_global_data *gd;
2139 gd = (struct mark_def_sites_global_data *) walk_data->global_data;
2140 bitmap_clear (gd->kills);
2144 /* Mark the definition site blocks for each variable, so that we know
2145 where the variable is actually live.
2147 INTERESTING_BLOCKS will be filled in with all the blocks that
2148 should be processed by the renamer. It is assumed to be
2149 initialized and zeroed by the caller. */
2151 static void
2152 mark_def_site_blocks (sbitmap interesting_blocks)
2154 struct dom_walk_data walk_data;
2155 struct mark_def_sites_global_data mark_def_sites_global_data;
2157 /* Setup callbacks for the generic dominator tree walker to find and
2158 mark definition sites. */
2159 walk_data.walk_stmts_backward = false;
2160 walk_data.dom_direction = CDI_DOMINATORS;
2161 walk_data.initialize_block_local_data = NULL;
2162 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
2163 walk_data.before_dom_children_walk_stmts = mark_def_sites;
2164 walk_data.before_dom_children_after_stmts = NULL;
2165 walk_data.after_dom_children_before_stmts = NULL;
2166 walk_data.after_dom_children_walk_stmts = NULL;
2167 walk_data.after_dom_children_after_stmts = NULL;
2168 walk_data.interesting_blocks = NULL;
2170 /* Notice that this bitmap is indexed using variable UIDs, so it must be
2171 large enough to accommodate all the variables referenced in the
2172 function, not just the ones we are renaming. */
2173 mark_def_sites_global_data.kills = BITMAP_ALLOC (NULL);
2175 /* Create the set of interesting blocks that will be filled by
2176 mark_def_sites. */
2177 mark_def_sites_global_data.interesting_blocks = interesting_blocks;
2178 walk_data.global_data = &mark_def_sites_global_data;
2180 /* We do not have any local data. */
2181 walk_data.block_local_data_size = 0;
2183 /* Initialize the dominator walker. */
2184 init_walk_dominator_tree (&walk_data);
2186 /* Recursively walk the dominator tree. */
2187 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
2189 /* Finalize the dominator walker. */
2190 fini_walk_dominator_tree (&walk_data);
2192 /* We no longer need this bitmap, clear and free it. */
2193 BITMAP_FREE (mark_def_sites_global_data.kills);
2197 /* Initialize internal data needed during renaming. */
2199 static void
2200 init_ssa_renamer (void)
2202 tree var;
2203 referenced_var_iterator rvi;
2205 cfun->gimple_df->in_ssa_p = false;
2207 /* Allocate memory for the DEF_BLOCKS hash table. */
2208 gcc_assert (def_blocks == NULL);
2209 def_blocks = htab_create (num_referenced_vars, def_blocks_hash,
2210 def_blocks_eq, def_blocks_free);
2212 FOR_EACH_REFERENCED_VAR(var, rvi)
2213 set_current_def (var, NULL_TREE);
2217 /* Deallocate internal data structures used by the renamer. */
2219 static void
2220 fini_ssa_renamer (void)
2222 if (def_blocks)
2224 htab_delete (def_blocks);
2225 def_blocks = NULL;
2228 cfun->gimple_df->in_ssa_p = true;
2232 /* Main entry point into the SSA builder. The renaming process
2233 proceeds in four main phases:
2235 1- Compute dominance frontier and immediate dominators, needed to
2236 insert PHI nodes and rename the function in dominator tree
2237 order.
2239 2- Find and mark all the blocks that define variables
2240 (mark_def_site_blocks).
2242 3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2244 4- Rename all the blocks (rewrite_blocks) and statements in the program.
2246 Steps 3 and 4 are done using the dominator tree walker
2247 (walk_dominator_tree). */
2249 static unsigned int
2250 rewrite_into_ssa (void)
2252 bitmap *dfs;
2253 basic_block bb;
2254 sbitmap interesting_blocks;
2256 timevar_push (TV_TREE_SSA_OTHER);
2258 /* Initialize operand data structures. */
2259 init_ssa_operands ();
2261 /* Initialize internal data needed by the renamer. */
2262 init_ssa_renamer ();
2264 /* Initialize the set of interesting blocks. The callback
2265 mark_def_sites will add to this set those blocks that the renamer
2266 should process. */
2267 interesting_blocks = sbitmap_alloc (last_basic_block);
2268 sbitmap_zero (interesting_blocks);
2270 /* Initialize dominance frontier. */
2271 dfs = XNEWVEC (bitmap, last_basic_block);
2272 FOR_EACH_BB (bb)
2273 dfs[bb->index] = BITMAP_ALLOC (NULL);
2275 /* 1- Compute dominance frontiers. */
2276 calculate_dominance_info (CDI_DOMINATORS);
2277 compute_dominance_frontiers (dfs);
2279 /* 2- Find and mark definition sites. */
2280 mark_def_site_blocks (interesting_blocks);
2282 /* 3- Insert PHI nodes at dominance frontiers of definition blocks. */
2283 insert_phi_nodes (dfs);
2285 /* 4- Rename all the blocks. */
2286 rewrite_blocks (ENTRY_BLOCK_PTR, REWRITE_ALL, interesting_blocks);
2288 /* Free allocated memory. */
2289 FOR_EACH_BB (bb)
2290 BITMAP_FREE (dfs[bb->index]);
2291 free (dfs);
2292 sbitmap_free (interesting_blocks);
2294 fini_ssa_renamer ();
2296 timevar_pop (TV_TREE_SSA_OTHER);
2297 return 0;
2301 struct gimple_opt_pass pass_build_ssa =
2304 GIMPLE_PASS,
2305 "ssa", /* name */
2306 NULL, /* gate */
2307 rewrite_into_ssa, /* execute */
2308 NULL, /* sub */
2309 NULL, /* next */
2310 0, /* static_pass_number */
2311 0, /* tv_id */
2312 PROP_cfg | PROP_referenced_vars, /* properties_required */
2313 PROP_ssa, /* properties_provided */
2314 0, /* properties_destroyed */
2315 0, /* todo_flags_start */
2316 TODO_dump_func
2317 | TODO_verify_ssa
2318 | TODO_remove_unused_locals /* todo_flags_finish */
2323 /* Mark the definition of VAR at STMT and BB as interesting for the
2324 renamer. BLOCKS is the set of blocks that need updating. */
2326 static void
2327 mark_def_interesting (tree var, tree stmt, basic_block bb, bool insert_phi_p)
2329 gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
2330 REGISTER_DEFS_IN_THIS_STMT (stmt) = 1;
2332 if (insert_phi_p)
2334 bool is_phi_p = TREE_CODE (stmt) == PHI_NODE;
2336 set_def_block (var, bb, is_phi_p);
2338 /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2339 site for both itself and all the old names replaced by it. */
2340 if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2342 bitmap_iterator bi;
2343 unsigned i;
2344 bitmap set = names_replaced_by (var);
2345 if (set)
2346 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2347 set_def_block (ssa_name (i), bb, is_phi_p);
2353 /* Mark the use of VAR at STMT and BB as interesting for the
2354 renamer. INSERT_PHI_P is true if we are going to insert new PHI
2355 nodes. */
2357 static inline void
2358 mark_use_interesting (tree var, tree stmt, basic_block bb, bool insert_phi_p)
2360 basic_block def_bb = bb_for_stmt (stmt);
2362 mark_block_for_update (def_bb);
2363 mark_block_for_update (bb);
2365 if (TREE_CODE (stmt) == PHI_NODE)
2366 mark_phi_for_rewrite (def_bb, stmt);
2367 else
2368 REWRITE_THIS_STMT (stmt) = 1;
2370 /* If VAR has not been defined in BB, then it is live-on-entry
2371 to BB. Note that we cannot just use the block holding VAR's
2372 definition because if VAR is one of the names in OLD_SSA_NAMES,
2373 it will have several definitions (itself and all the names that
2374 replace it). */
2375 if (insert_phi_p)
2377 struct def_blocks_d *db_p = get_def_blocks_for (var);
2378 if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2379 set_livein_block (var, bb);
2384 /* Do a dominator walk starting at BB processing statements that
2385 reference symbols in SYMS_TO_RENAME. This is very similar to
2386 mark_def_sites, but the scan handles statements whose operands may
2387 already be SSA names.
2389 If INSERT_PHI_P is true, mark those uses as live in the
2390 corresponding block. This is later used by the PHI placement
2391 algorithm to make PHI pruning decisions.
2393 FIXME. Most of this would be unnecessary if we could associate a
2394 symbol to all the SSA names that reference it. But that
2395 sounds like it would be expensive to maintain. Still, it
2396 would be interesting to see if it makes better sense to do
2397 that. */
2399 static void
2400 prepare_block_for_update (basic_block bb, bool insert_phi_p)
2402 basic_block son;
2403 block_stmt_iterator si;
2404 tree phi;
2405 edge e;
2406 edge_iterator ei;
2408 mark_block_for_update (bb);
2410 /* Process PHI nodes marking interesting those that define or use
2411 the symbols that we are interested in. */
2412 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2414 tree lhs_sym, lhs = PHI_RESULT (phi);
2416 lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
2418 if (!symbol_marked_for_renaming (lhs_sym))
2419 continue;
2420 mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2422 /* Mark the uses in phi nodes as interesting. It would be more correct
2423 to process the arguments of the phi nodes of the successor edges of
2424 BB at the end of prepare_block_for_update, however, that turns out
2425 to be significantly more expensive. Doing it here is conservatively
2426 correct -- it may only cause us to believe a value to be live in a
2427 block that also contains its definition, and thus insert a few more
2428 phi nodes for it. */
2429 FOR_EACH_EDGE (e, ei, bb->preds)
2431 mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
2435 /* Process the statements. */
2436 for (si = bsi_start (bb); !bsi_end_p (si); bsi_next (&si))
2438 tree stmt;
2439 ssa_op_iter i;
2440 use_operand_p use_p;
2441 def_operand_p def_p;
2443 stmt = bsi_stmt (si);
2445 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
2447 tree use = USE_FROM_PTR (use_p);
2448 tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2449 if (symbol_marked_for_renaming (sym))
2450 mark_use_interesting (sym, stmt, bb, insert_phi_p);
2453 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_ALL_DEFS)
2455 tree def = DEF_FROM_PTR (def_p);
2456 tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2457 if (symbol_marked_for_renaming (sym))
2458 mark_def_interesting (sym, stmt, bb, insert_phi_p);
2462 /* Now visit all the blocks dominated by BB. */
2463 for (son = first_dom_son (CDI_DOMINATORS, bb);
2464 son;
2465 son = next_dom_son (CDI_DOMINATORS, son))
2466 prepare_block_for_update (son, insert_phi_p);
2470 /* Helper for prepare_names_to_update. Mark all the use sites for
2471 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2472 prepare_names_to_update. */
2474 static void
2475 prepare_use_sites_for (tree name, bool insert_phi_p)
2477 use_operand_p use_p;
2478 imm_use_iterator iter;
2480 FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2482 tree stmt = USE_STMT (use_p);
2483 basic_block bb = bb_for_stmt (stmt);
2485 if (TREE_CODE (stmt) == PHI_NODE)
2487 int ix = PHI_ARG_INDEX_FROM_USE (use_p);
2488 edge e = PHI_ARG_EDGE (stmt, ix);
2489 mark_use_interesting (name, stmt, e->src, insert_phi_p);
2491 else
2493 /* For regular statements, mark this as an interesting use
2494 for NAME. */
2495 mark_use_interesting (name, stmt, bb, insert_phi_p);
2501 /* Helper for prepare_names_to_update. Mark the definition site for
2502 NAME as interesting. BLOCKS and INSERT_PHI_P are as in
2503 prepare_names_to_update. */
2505 static void
2506 prepare_def_site_for (tree name, bool insert_phi_p)
2508 tree stmt;
2509 basic_block bb;
2511 gcc_assert (names_to_release == NULL
2512 || !bitmap_bit_p (names_to_release, SSA_NAME_VERSION (name)));
2514 stmt = SSA_NAME_DEF_STMT (name);
2515 bb = bb_for_stmt (stmt);
2516 if (bb)
2518 gcc_assert (bb->index < last_basic_block);
2519 mark_block_for_update (bb);
2520 mark_def_interesting (name, stmt, bb, insert_phi_p);
2525 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2526 OLD_SSA_NAMES. INSERT_PHI_P is true if the caller wants to insert
2527 PHI nodes for newly created names. */
2529 static void
2530 prepare_names_to_update (bool insert_phi_p)
2532 unsigned i = 0;
2533 bitmap_iterator bi;
2534 sbitmap_iterator sbi;
2536 /* If a name N from NEW_SSA_NAMES is also marked to be released,
2537 remove it from NEW_SSA_NAMES so that we don't try to visit its
2538 defining basic block (which most likely doesn't exist). Notice
2539 that we cannot do the same with names in OLD_SSA_NAMES because we
2540 want to replace existing instances. */
2541 if (names_to_release)
2542 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2543 RESET_BIT (new_ssa_names, i);
2545 /* First process names in NEW_SSA_NAMES. Otherwise, uses of old
2546 names may be considered to be live-in on blocks that contain
2547 definitions for their replacements. */
2548 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2549 prepare_def_site_for (ssa_name (i), insert_phi_p);
2551 /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2552 OLD_SSA_NAMES, but we have to ignore its definition site. */
2553 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2555 if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2556 prepare_def_site_for (ssa_name (i), insert_phi_p);
2557 prepare_use_sites_for (ssa_name (i), insert_phi_p);
2562 /* Dump all the names replaced by NAME to FILE. */
2564 void
2565 dump_names_replaced_by (FILE *file, tree name)
2567 unsigned i;
2568 bitmap old_set;
2569 bitmap_iterator bi;
2571 print_generic_expr (file, name, 0);
2572 fprintf (file, " -> { ");
2574 old_set = names_replaced_by (name);
2575 EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2577 print_generic_expr (file, ssa_name (i), 0);
2578 fprintf (file, " ");
2581 fprintf (file, "}\n");
2585 /* Dump all the names replaced by NAME to stderr. */
2587 void
2588 debug_names_replaced_by (tree name)
2590 dump_names_replaced_by (stderr, name);
2594 /* Dump SSA update information to FILE. */
2596 void
2597 dump_update_ssa (FILE *file)
2599 unsigned i = 0;
2600 bitmap_iterator bi;
2602 if (!need_ssa_update_p ())
2603 return;
2605 if (new_ssa_names && sbitmap_first_set_bit (new_ssa_names) >= 0)
2607 sbitmap_iterator sbi;
2609 fprintf (file, "\nSSA replacement table\n");
2610 fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2611 "O_1, ..., O_j\n\n");
2613 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2614 dump_names_replaced_by (file, ssa_name (i));
2616 fprintf (file, "\n");
2617 fprintf (file, "Number of virtual NEW -> OLD mappings: %7u\n",
2618 update_ssa_stats.num_virtual_mappings);
2619 fprintf (file, "Number of real NEW -> OLD mappings: %7u\n",
2620 update_ssa_stats.num_total_mappings
2621 - update_ssa_stats.num_virtual_mappings);
2622 fprintf (file, "Number of total NEW -> OLD mappings: %7u\n",
2623 update_ssa_stats.num_total_mappings);
2625 fprintf (file, "\nNumber of virtual symbols: %u\n",
2626 update_ssa_stats.num_virtual_symbols);
2629 if (syms_to_rename && !bitmap_empty_p (syms_to_rename))
2631 fprintf (file, "\n\nSymbols to be put in SSA form\n\n");
2632 dump_decl_set (file, syms_to_rename);
2635 if (names_to_release && !bitmap_empty_p (names_to_release))
2637 fprintf (file, "\n\nSSA names to release after updating the SSA web\n\n");
2638 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2640 print_generic_expr (file, ssa_name (i), 0);
2641 fprintf (file, " ");
2645 fprintf (file, "\n\n");
2649 /* Dump SSA update information to stderr. */
2651 void
2652 debug_update_ssa (void)
2654 dump_update_ssa (stderr);
2658 /* Initialize data structures used for incremental SSA updates. */
2660 static void
2661 init_update_ssa (void)
2663 /* Reserve more space than the current number of names. The calls to
2664 add_new_name_mapping are typically done after creating new SSA
2665 names, so we'll need to reallocate these arrays. */
2666 old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2667 sbitmap_zero (old_ssa_names);
2669 new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2670 sbitmap_zero (new_ssa_names);
2672 repl_tbl = htab_create (20, repl_map_hash, repl_map_eq, repl_map_free);
2673 need_to_initialize_update_ssa_p = false;
2674 need_to_update_vops_p = false;
2675 syms_to_rename = BITMAP_ALLOC (NULL);
2676 regs_to_rename = BITMAP_ALLOC (NULL);
2677 mem_syms_to_rename = BITMAP_ALLOC (NULL);
2678 names_to_release = NULL;
2679 memset (&update_ssa_stats, 0, sizeof (update_ssa_stats));
2680 update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL);
2684 /* Deallocate data structures used for incremental SSA updates. */
2686 void
2687 delete_update_ssa (void)
2689 unsigned i;
2690 bitmap_iterator bi;
2692 sbitmap_free (old_ssa_names);
2693 old_ssa_names = NULL;
2695 sbitmap_free (new_ssa_names);
2696 new_ssa_names = NULL;
2698 htab_delete (repl_tbl);
2699 repl_tbl = NULL;
2701 need_to_initialize_update_ssa_p = true;
2702 need_to_update_vops_p = false;
2703 BITMAP_FREE (syms_to_rename);
2704 BITMAP_FREE (regs_to_rename);
2705 BITMAP_FREE (mem_syms_to_rename);
2706 BITMAP_FREE (update_ssa_stats.virtual_symbols);
2708 if (names_to_release)
2710 EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2711 release_ssa_name (ssa_name (i));
2712 BITMAP_FREE (names_to_release);
2715 clear_ssa_name_info ();
2717 fini_ssa_renamer ();
2719 if (blocks_with_phis_to_rewrite)
2720 EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
2722 tree_vec phis = VEC_index (tree_vec, phis_to_rewrite, i);
2724 VEC_free (tree, heap, phis);
2725 VEC_replace (tree_vec, phis_to_rewrite, i, NULL);
2728 BITMAP_FREE (blocks_with_phis_to_rewrite);
2729 BITMAP_FREE (blocks_to_update);
2733 /* Create a new name for OLD_NAME in statement STMT and replace the
2734 operand pointed to by DEF_P with the newly created name. Return
2735 the new name and register the replacement mapping <NEW, OLD> in
2736 update_ssa's tables. */
2738 tree
2739 create_new_def_for (tree old_name, tree stmt, def_operand_p def)
2741 tree new_name = duplicate_ssa_name (old_name, stmt);
2743 SET_DEF (def, new_name);
2745 if (TREE_CODE (stmt) == PHI_NODE)
2747 edge e;
2748 edge_iterator ei;
2749 basic_block bb = bb_for_stmt (stmt);
2751 /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2752 FOR_EACH_EDGE (e, ei, bb->preds)
2753 if (e->flags & EDGE_ABNORMAL)
2755 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
2756 break;
2760 register_new_name_mapping (new_name, old_name);
2762 /* For the benefit of passes that will be updating the SSA form on
2763 their own, set the current reaching definition of OLD_NAME to be
2764 NEW_NAME. */
2765 set_current_def (old_name, new_name);
2767 return new_name;
2771 /* Register name NEW to be a replacement for name OLD. This function
2772 must be called for every replacement that should be performed by
2773 update_ssa. */
2775 void
2776 register_new_name_mapping (tree new, tree old)
2778 if (need_to_initialize_update_ssa_p)
2779 init_update_ssa ();
2781 add_new_name_mapping (new, old);
2785 /* Register symbol SYM to be renamed by update_ssa. */
2787 void
2788 mark_sym_for_renaming (tree sym)
2790 if (need_to_initialize_update_ssa_p)
2791 init_update_ssa ();
2793 bitmap_set_bit (syms_to_rename, DECL_UID (sym));
2795 if (!is_gimple_reg (sym))
2797 need_to_update_vops_p = true;
2798 if (memory_partition (sym))
2799 bitmap_set_bit (syms_to_rename, DECL_UID (memory_partition (sym)));
2804 /* Register all the symbols in SET to be renamed by update_ssa. */
2806 void
2807 mark_set_for_renaming (bitmap set)
2809 bitmap_iterator bi;
2810 unsigned i;
2812 if (set == NULL || bitmap_empty_p (set))
2813 return;
2815 if (need_to_initialize_update_ssa_p)
2816 init_update_ssa ();
2818 EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2819 mark_sym_for_renaming (referenced_var (i));
2823 /* Return true if there is any work to be done by update_ssa. */
2825 bool
2826 need_ssa_update_p (void)
2828 return syms_to_rename || old_ssa_names || new_ssa_names;
2831 /* Return true if SSA name mappings have been registered for SSA updating. */
2833 bool
2834 name_mappings_registered_p (void)
2836 return repl_tbl && htab_elements (repl_tbl) > 0;
2839 /* Return true if name N has been registered in the replacement table. */
2841 bool
2842 name_registered_for_update_p (tree n)
2844 if (!need_ssa_update_p ())
2845 return false;
2847 return is_new_name (n)
2848 || is_old_name (n)
2849 || symbol_marked_for_renaming (SSA_NAME_VAR (n));
2853 /* Return the set of all the SSA names marked to be replaced. */
2855 bitmap
2856 ssa_names_to_replace (void)
2858 unsigned i = 0;
2859 bitmap ret;
2860 sbitmap_iterator sbi;
2862 ret = BITMAP_ALLOC (NULL);
2863 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2864 bitmap_set_bit (ret, i);
2866 return ret;
2870 /* Mark NAME to be released after update_ssa has finished. */
2872 void
2873 release_ssa_name_after_update_ssa (tree name)
2875 gcc_assert (!need_to_initialize_update_ssa_p);
2877 if (names_to_release == NULL)
2878 names_to_release = BITMAP_ALLOC (NULL);
2880 bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
2884 /* Insert new PHI nodes to replace VAR. DFS contains dominance
2885 frontier information. BLOCKS is the set of blocks to be updated.
2887 This is slightly different than the regular PHI insertion
2888 algorithm. The value of UPDATE_FLAGS controls how PHI nodes for
2889 real names (i.e., GIMPLE registers) are inserted:
2891 - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2892 nodes inside the region affected by the block that defines VAR
2893 and the blocks that define all its replacements. All these
2894 definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2896 First, we compute the entry point to the region (ENTRY). This is
2897 given by the nearest common dominator to all the definition
2898 blocks. When computing the iterated dominance frontier (IDF), any
2899 block not strictly dominated by ENTRY is ignored.
2901 We then call the standard PHI insertion algorithm with the pruned
2902 IDF.
2904 - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
2905 names is not pruned. PHI nodes are inserted at every IDF block. */
2907 static void
2908 insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks,
2909 unsigned update_flags)
2911 basic_block entry;
2912 struct def_blocks_d *db;
2913 bitmap idf, pruned_idf;
2914 bitmap_iterator bi;
2915 unsigned i;
2917 #if defined ENABLE_CHECKING
2918 if (TREE_CODE (var) == SSA_NAME)
2919 gcc_assert (is_old_name (var));
2920 else
2921 gcc_assert (symbol_marked_for_renaming (var));
2922 #endif
2924 /* Get all the definition sites for VAR. */
2925 db = find_def_blocks_for (var);
2927 /* No need to do anything if there were no definitions to VAR. */
2928 if (db == NULL || bitmap_empty_p (db->def_blocks))
2929 return;
2931 /* Compute the initial iterated dominance frontier. */
2932 idf = compute_idf (db->def_blocks, dfs);
2933 pruned_idf = BITMAP_ALLOC (NULL);
2935 if (TREE_CODE (var) == SSA_NAME)
2937 if (update_flags == TODO_update_ssa)
2939 /* If doing regular SSA updates for GIMPLE registers, we are
2940 only interested in IDF blocks dominated by the nearest
2941 common dominator of all the definition blocks. */
2942 entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
2943 db->def_blocks);
2944 if (entry != ENTRY_BLOCK_PTR)
2945 EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
2946 if (BASIC_BLOCK (i) != entry
2947 && dominated_by_p (CDI_DOMINATORS, BASIC_BLOCK (i), entry))
2948 bitmap_set_bit (pruned_idf, i);
2950 else
2952 /* Otherwise, do not prune the IDF for VAR. */
2953 gcc_assert (update_flags == TODO_update_ssa_full_phi);
2954 bitmap_copy (pruned_idf, idf);
2957 else
2959 /* Otherwise, VAR is a symbol that needs to be put into SSA form
2960 for the first time, so we need to compute the full IDF for
2961 it. */
2962 bitmap_copy (pruned_idf, idf);
2965 if (!bitmap_empty_p (pruned_idf))
2967 /* Make sure that PRUNED_IDF blocks and all their feeding blocks
2968 are included in the region to be updated. The feeding blocks
2969 are important to guarantee that the PHI arguments are renamed
2970 properly. */
2972 /* FIXME, this is not needed if we are updating symbols. We are
2973 already starting at the ENTRY block anyway. */
2974 bitmap_ior_into (blocks, pruned_idf);
2975 EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
2977 edge e;
2978 edge_iterator ei;
2979 basic_block bb = BASIC_BLOCK (i);
2981 FOR_EACH_EDGE (e, ei, bb->preds)
2982 if (e->src->index >= 0)
2983 bitmap_set_bit (blocks, e->src->index);
2986 insert_phi_nodes_for (var, pruned_idf, true);
2989 BITMAP_FREE (pruned_idf);
2990 BITMAP_FREE (idf);
2994 /* Heuristic to determine whether SSA name mappings for virtual names
2995 should be discarded and their symbols rewritten from scratch. When
2996 there is a large number of mappings for virtual names, the
2997 insertion of PHI nodes for the old names in the mappings takes
2998 considerable more time than if we inserted PHI nodes for the
2999 symbols instead.
3001 Currently the heuristic takes these stats into account:
3003 - Number of mappings for virtual SSA names.
3004 - Number of distinct virtual symbols involved in those mappings.
3006 If the number of virtual mappings is much larger than the number of
3007 virtual symbols, then it will be faster to compute PHI insertion
3008 spots for the symbols. Even if this involves traversing the whole
3009 CFG, which is what happens when symbols are renamed from scratch. */
3011 static bool
3012 switch_virtuals_to_full_rewrite_p (void)
3014 if (update_ssa_stats.num_virtual_mappings < (unsigned) MIN_VIRTUAL_MAPPINGS)
3015 return false;
3017 if (update_ssa_stats.num_virtual_mappings
3018 > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
3019 * update_ssa_stats.num_virtual_symbols)
3020 return true;
3022 return false;
3026 /* Remove every virtual mapping and mark all the affected virtual
3027 symbols for renaming. */
3029 static void
3030 switch_virtuals_to_full_rewrite (void)
3032 unsigned i = 0;
3033 sbitmap_iterator sbi;
3035 if (dump_file)
3037 fprintf (dump_file, "\nEnabled virtual name mapping heuristic.\n");
3038 fprintf (dump_file, "\tNumber of virtual mappings: %7u\n",
3039 update_ssa_stats.num_virtual_mappings);
3040 fprintf (dump_file, "\tNumber of unique virtual symbols: %7u\n",
3041 update_ssa_stats.num_virtual_symbols);
3042 fprintf (dump_file, "Updating FUD-chains from top of CFG will be "
3043 "faster than processing\nthe name mappings.\n\n");
3046 /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
3047 Note that it is not really necessary to remove the mappings from
3048 REPL_TBL, that would only waste time. */
3049 EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
3050 if (!is_gimple_reg (ssa_name (i)))
3051 RESET_BIT (new_ssa_names, i);
3053 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3054 if (!is_gimple_reg (ssa_name (i)))
3055 RESET_BIT (old_ssa_names, i);
3057 mark_set_for_renaming (update_ssa_stats.virtual_symbols);
3061 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3062 existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3064 1- The names in OLD_SSA_NAMES dominated by the definitions of
3065 NEW_SSA_NAMES are all re-written to be reached by the
3066 appropriate definition from NEW_SSA_NAMES.
3068 2- If needed, new PHI nodes are added to the iterated dominance
3069 frontier of the blocks where each of NEW_SSA_NAMES are defined.
3071 The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3072 calling register_new_name_mapping for every pair of names that the
3073 caller wants to replace.
3075 The caller identifies the new names that have been inserted and the
3076 names that need to be replaced by calling register_new_name_mapping
3077 for every pair <NEW, OLD>. Note that the function assumes that the
3078 new names have already been inserted in the IL.
3080 For instance, given the following code:
3082 1 L0:
3083 2 x_1 = PHI (0, x_5)
3084 3 if (x_1 < 10)
3085 4 if (x_1 > 7)
3086 5 y_2 = 0
3087 6 else
3088 7 y_3 = x_1 + x_7
3089 8 endif
3090 9 x_5 = x_1 + 1
3091 10 goto L0;
3092 11 endif
3094 Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3096 1 L0:
3097 2 x_1 = PHI (0, x_5)
3098 3 if (x_1 < 10)
3099 4 x_10 = ...
3100 5 if (x_1 > 7)
3101 6 y_2 = 0
3102 7 else
3103 8 x_11 = ...
3104 9 y_3 = x_1 + x_7
3105 10 endif
3106 11 x_5 = x_1 + 1
3107 12 goto L0;
3108 13 endif
3110 We want to replace all the uses of x_1 with the new definitions of
3111 x_10 and x_11. Note that the only uses that should be replaced are
3112 those at lines 5, 9 and 11. Also, the use of x_7 at line 9 should
3113 *not* be replaced (this is why we cannot just mark symbol 'x' for
3114 renaming).
3116 Additionally, we may need to insert a PHI node at line 11 because
3117 that is a merge point for x_10 and x_11. So the use of x_1 at line
3118 11 will be replaced with the new PHI node. The insertion of PHI
3119 nodes is optional. They are not strictly necessary to preserve the
3120 SSA form, and depending on what the caller inserted, they may not
3121 even be useful for the optimizers. UPDATE_FLAGS controls various
3122 aspects of how update_ssa operates, see the documentation for
3123 TODO_update_ssa*. */
3125 void
3126 update_ssa (unsigned update_flags)
3128 basic_block bb, start_bb;
3129 bitmap_iterator bi;
3130 unsigned i = 0;
3131 sbitmap tmp;
3132 bool insert_phi_p;
3133 sbitmap_iterator sbi;
3135 if (!need_ssa_update_p ())
3136 return;
3138 timevar_push (TV_TREE_SSA_INCREMENTAL);
3140 blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
3141 if (!phis_to_rewrite)
3142 phis_to_rewrite = VEC_alloc (tree_vec, heap, last_basic_block);
3143 blocks_to_update = BITMAP_ALLOC (NULL);
3145 /* Ensure that the dominance information is up-to-date. */
3146 calculate_dominance_info (CDI_DOMINATORS);
3148 /* Only one update flag should be set. */
3149 gcc_assert (update_flags == TODO_update_ssa
3150 || update_flags == TODO_update_ssa_no_phi
3151 || update_flags == TODO_update_ssa_full_phi
3152 || update_flags == TODO_update_ssa_only_virtuals);
3154 /* If we only need to update virtuals, remove all the mappings for
3155 real names before proceeding. The caller is responsible for
3156 having dealt with the name mappings before calling update_ssa. */
3157 if (update_flags == TODO_update_ssa_only_virtuals)
3159 sbitmap_zero (old_ssa_names);
3160 sbitmap_zero (new_ssa_names);
3161 htab_empty (repl_tbl);
3164 insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
3166 if (insert_phi_p)
3168 /* If the caller requested PHI nodes to be added, initialize
3169 live-in information data structures (DEF_BLOCKS). */
3171 /* For each SSA name N, the DEF_BLOCKS table describes where the
3172 name is defined, which blocks have PHI nodes for N, and which
3173 blocks have uses of N (i.e., N is live-on-entry in those
3174 blocks). */
3175 def_blocks = htab_create (num_ssa_names, def_blocks_hash,
3176 def_blocks_eq, def_blocks_free);
3178 else
3180 def_blocks = NULL;
3183 /* Heuristic to avoid massive slow downs when the replacement
3184 mappings include lots of virtual names. */
3185 if (insert_phi_p && switch_virtuals_to_full_rewrite_p ())
3186 switch_virtuals_to_full_rewrite ();
3188 /* If there are symbols to rename, identify those symbols that are
3189 GIMPLE registers into the set REGS_TO_RENAME and those that are
3190 memory symbols into the set MEM_SYMS_TO_RENAME. */
3191 if (!bitmap_empty_p (syms_to_rename))
3193 unsigned i;
3194 bitmap_iterator bi;
3196 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
3198 tree sym = referenced_var (i);
3199 if (is_gimple_reg (sym))
3200 bitmap_set_bit (regs_to_rename, i);
3201 else
3203 /* Memory partitioning information may have been
3204 computed after the symbol was marked for renaming,
3205 if SYM is inside a partition also mark the partition
3206 for renaming. */
3207 tree mpt = memory_partition (sym);
3208 if (mpt)
3209 bitmap_set_bit (syms_to_rename, DECL_UID (mpt));
3213 /* Memory symbols are those not in REGS_TO_RENAME. */
3214 bitmap_and_compl (mem_syms_to_rename, syms_to_rename, regs_to_rename);
3217 /* If there are names defined in the replacement table, prepare
3218 definition and use sites for all the names in NEW_SSA_NAMES and
3219 OLD_SSA_NAMES. */
3220 if (sbitmap_first_set_bit (new_ssa_names) >= 0)
3222 prepare_names_to_update (insert_phi_p);
3224 /* If all the names in NEW_SSA_NAMES had been marked for
3225 removal, and there are no symbols to rename, then there's
3226 nothing else to do. */
3227 if (sbitmap_first_set_bit (new_ssa_names) < 0
3228 && bitmap_empty_p (syms_to_rename))
3229 goto done;
3232 /* Next, determine the block at which to start the renaming process. */
3233 if (!bitmap_empty_p (syms_to_rename))
3235 /* If we have to rename some symbols from scratch, we need to
3236 start the process at the root of the CFG. FIXME, it should
3237 be possible to determine the nearest block that had a
3238 definition for each of the symbols that are marked for
3239 updating. For now this seems more work than it's worth. */
3240 start_bb = ENTRY_BLOCK_PTR;
3242 /* Traverse the CFG looking for existing definitions and uses of
3243 symbols in SYMS_TO_RENAME. Mark interesting blocks and
3244 statements and set local live-in information for the PHI
3245 placement heuristics. */
3246 prepare_block_for_update (start_bb, insert_phi_p);
3248 else
3250 /* Otherwise, the entry block to the region is the nearest
3251 common dominator for the blocks in BLOCKS. */
3252 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3253 blocks_to_update);
3256 /* If requested, insert PHI nodes at the iterated dominance frontier
3257 of every block, creating new definitions for names in OLD_SSA_NAMES
3258 and for symbols in SYMS_TO_RENAME. */
3259 if (insert_phi_p)
3261 bitmap *dfs;
3263 /* If the caller requested PHI nodes to be added, compute
3264 dominance frontiers. */
3265 dfs = XNEWVEC (bitmap, last_basic_block);
3266 FOR_EACH_BB (bb)
3267 dfs[bb->index] = BITMAP_ALLOC (NULL);
3268 compute_dominance_frontiers (dfs);
3270 if (sbitmap_first_set_bit (old_ssa_names) >= 0)
3272 sbitmap_iterator sbi;
3274 /* insert_update_phi_nodes_for will call add_new_name_mapping
3275 when inserting new PHI nodes, so the set OLD_SSA_NAMES
3276 will grow while we are traversing it (but it will not
3277 gain any new members). Copy OLD_SSA_NAMES to a temporary
3278 for traversal. */
3279 sbitmap tmp = sbitmap_alloc (old_ssa_names->n_bits);
3280 sbitmap_copy (tmp, old_ssa_names);
3281 EXECUTE_IF_SET_IN_SBITMAP (tmp, 0, i, sbi)
3282 insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
3283 update_flags);
3284 sbitmap_free (tmp);
3287 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
3288 insert_updated_phi_nodes_for (referenced_var (i), dfs, blocks_to_update,
3289 update_flags);
3291 FOR_EACH_BB (bb)
3292 BITMAP_FREE (dfs[bb->index]);
3293 free (dfs);
3295 /* Insertion of PHI nodes may have added blocks to the region.
3296 We need to re-compute START_BB to include the newly added
3297 blocks. */
3298 if (start_bb != ENTRY_BLOCK_PTR)
3299 start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3300 blocks_to_update);
3303 /* Reset the current definition for name and symbol before renaming
3304 the sub-graph. */
3305 EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3306 set_current_def (ssa_name (i), NULL_TREE);
3308 EXECUTE_IF_SET_IN_BITMAP (syms_to_rename, 0, i, bi)
3309 set_current_def (referenced_var (i), NULL_TREE);
3311 /* Now start the renaming process at START_BB. */
3312 tmp = sbitmap_alloc (last_basic_block);
3313 sbitmap_zero (tmp);
3314 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3315 SET_BIT (tmp, i);
3317 rewrite_blocks (start_bb, REWRITE_UPDATE, tmp);
3319 sbitmap_free (tmp);
3321 /* Debugging dumps. */
3322 if (dump_file)
3324 int c;
3325 unsigned i;
3327 dump_update_ssa (dump_file);
3329 fprintf (dump_file, "Incremental SSA update started at block: %d\n\n",
3330 start_bb->index);
3332 c = 0;
3333 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3334 c++;
3335 fprintf (dump_file, "Number of blocks in CFG: %d\n", last_basic_block);
3336 fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n\n",
3337 c, PERCENT (c, last_basic_block));
3339 if (dump_flags & TDF_DETAILS)
3341 fprintf (dump_file, "Affected blocks: ");
3342 EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3343 fprintf (dump_file, "%u ", i);
3344 fprintf (dump_file, "\n");
3347 fprintf (dump_file, "\n\n");
3350 /* Free allocated memory. */
3351 done:
3352 delete_update_ssa ();
3354 timevar_pop (TV_TREE_SSA_INCREMENTAL);