OSDN Git Service

Fix PR c++/43704
[pf3gnuchains/gcc-fork.git] / gcc / tree-into-ssa.c
1 /* Rewrite a program in Normal form into SSA.
2    Copyright (C) 2001, 2002, 2003, 2004, 2005, 2007, 2008, 2009, 2010
3    Free Software Foundation, Inc.
4    Contributed by Diego Novillo <dnovillo@redhat.com>
5
6 This file is part of GCC.
7
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.
12
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.
17
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/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "tm_p.h"
29 #include "langhooks.h"
30 #include "basic-block.h"
31 #include "output.h"
32 #include "expr.h"
33 #include "function.h"
34 #include "diagnostic.h"
35 #include "tree-pretty-print.h"
36 #include "gimple-pretty-print.h"
37 #include "bitmap.h"
38 #include "tree-flow.h"
39 #include "gimple.h"
40 #include "tree-inline.h"
41 #include "timevar.h"
42 #include "hashtab.h"
43 #include "tree-dump.h"
44 #include "tree-pass.h"
45 #include "cfgloop.h"
46 #include "domwalk.h"
47 #include "params.h"
48 #include "vecprim.h"
49
50
51 /* This file builds the SSA form for a function as described in:
52    R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
53    Computing Static Single Assignment Form and the Control Dependence
54    Graph. ACM Transactions on Programming Languages and Systems,
55    13(4):451-490, October 1991.  */
56
57 /* Structure to map a variable VAR to the set of blocks that contain
58    definitions for VAR.  */
59 struct def_blocks_d
60 {
61   /* The variable.  */
62   tree var;
63
64   /* Blocks that contain definitions of VAR.  Bit I will be set if the
65      Ith block contains a definition of VAR.  */
66   bitmap def_blocks;
67
68   /* Blocks that contain a PHI node for VAR.  */
69   bitmap phi_blocks;
70
71   /* Blocks where VAR is live-on-entry.  Similar semantics as
72      DEF_BLOCKS.  */
73   bitmap livein_blocks;
74 };
75
76
77 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
78    DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
79    basic blocks where VAR is defined (assigned a new value).  It also
80    contains a bitmap of all the blocks where VAR is live-on-entry
81    (i.e., there is a use of VAR in block B without a preceding
82    definition in B).  The live-on-entry information is used when
83    computing PHI pruning heuristics.  */
84 static htab_t def_blocks;
85
86 /* Stack of trees used to restore the global currdefs to its original
87    state after completing rewriting of a block and its dominator
88    children.  Its elements have the following properties:
89
90    - An SSA_NAME (N) indicates that the current definition of the
91      underlying variable should be set to the given SSA_NAME.  If the
92      symbol associated with the SSA_NAME is not a GIMPLE register, the
93      next slot in the stack must be a _DECL node (SYM).  In this case,
94      the name N in the previous slot is the current reaching
95      definition for SYM.
96
97    - A _DECL node indicates that the underlying variable has no
98      current definition.
99
100    - A NULL node at the top entry is used to mark the last slot
101      associated with the current block.  */
102 static VEC(tree,heap) *block_defs_stack;
103
104
105 /* Set of existing SSA names being replaced by update_ssa.  */
106 static sbitmap old_ssa_names;
107
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;
112
113 sbitmap interesting_blocks;
114
115 /* Set of SSA names that have been marked to be released after they
116    were registered in the replacement table.  They will be finally
117    released after we finish updating the SSA web.  */
118 static bitmap names_to_release;
119
120 static VEC(gimple_vec, heap) *phis_to_rewrite;
121
122 /* The bitmap of non-NULL elements of PHIS_TO_REWRITE.  */
123 static bitmap blocks_with_phis_to_rewrite;
124
125 /* Growth factor for NEW_SSA_NAMES and OLD_SSA_NAMES.  These sets need
126    to grow as the callers to register_new_name_mapping will typically
127    create new names on the fly.  FIXME.  Currently set to 1/3 to avoid
128    frequent reallocations but still need to find a reasonable growth
129    strategy.  */
130 #define NAME_SETS_GROWTH_FACTOR (MAX (3, num_ssa_names / 3))
131
132 /* Tuple used to represent replacement mappings.  */
133 struct repl_map_d
134 {
135   tree name;
136   bitmap set;
137 };
138
139
140 /* NEW -> OLD_SET replacement table.  If we are replacing several
141    existing SSA names O_1, O_2, ..., O_j with a new name N_i,
142    then REPL_TBL[N_i] = { O_1, O_2, ..., O_j }.  */
143 static htab_t repl_tbl;
144
145 /* The function the SSA updating data structures have been initialized for.
146    NULL if they need to be initialized by register_new_name_mapping.  */
147 static struct function *update_ssa_initialized_fn = NULL;
148
149 /* Statistics kept by update_ssa to use in the virtual mapping
150    heuristic.  If the number of virtual mappings is beyond certain
151    threshold, the updater will switch from using the mappings into
152    renaming the virtual symbols from scratch.  In some cases, the
153    large number of name mappings for virtual names causes significant
154    slowdowns in the PHI insertion code.  */
155 struct update_ssa_stats_d
156 {
157   unsigned num_virtual_mappings;
158   unsigned num_total_mappings;
159   bitmap virtual_symbols;
160   unsigned num_virtual_symbols;
161 };
162 static struct update_ssa_stats_d update_ssa_stats;
163
164 /* Global data to attach to the main dominator walk structure.  */
165 struct mark_def_sites_global_data
166 {
167   /* This bitmap contains the variables which are set before they
168      are used in a basic block.  */
169   bitmap kills;
170 };
171
172
173 /* Information stored for SSA names.  */
174 struct ssa_name_info
175 {
176   /* The current reaching definition replacing this SSA name.  */
177   tree current_def;
178
179   /* This field indicates whether or not the variable may need PHI nodes.
180      See the enum's definition for more detailed information about the
181      states.  */
182   ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
183
184   /* Age of this record (so that info_for_ssa_name table can be cleared
185      quickly); if AGE < CURRENT_INFO_FOR_SSA_NAME_AGE, then the fields
186      are assumed to be null.  */
187   unsigned age;
188 };
189
190 /* The information associated with names.  */
191 typedef struct ssa_name_info *ssa_name_info_p;
192 DEF_VEC_P (ssa_name_info_p);
193 DEF_VEC_ALLOC_P (ssa_name_info_p, heap);
194
195 static VEC(ssa_name_info_p, heap) *info_for_ssa_name;
196 static unsigned current_info_for_ssa_name_age;
197
198 /* The set of blocks affected by update_ssa.  */
199 static bitmap blocks_to_update;
200
201 /* The main entry point to the SSA renamer (rewrite_blocks) may be
202    called several times to do different, but related, tasks.
203    Initially, we need it to rename the whole program into SSA form.
204    At other times, we may need it to only rename into SSA newly
205    exposed symbols.  Finally, we can also call it to incrementally fix
206    an already built SSA web.  */
207 enum rewrite_mode {
208     /* Convert the whole function into SSA form.  */
209     REWRITE_ALL,
210
211     /* Incrementally update the SSA web by replacing existing SSA
212        names with new ones.  See update_ssa for details.  */
213     REWRITE_UPDATE
214 };
215
216
217
218
219 /* Prototypes for debugging functions.  */
220 extern void dump_tree_ssa (FILE *);
221 extern void debug_tree_ssa (void);
222 extern void debug_def_blocks (void);
223 extern void dump_tree_ssa_stats (FILE *);
224 extern void debug_tree_ssa_stats (void);
225 extern void dump_update_ssa (FILE *);
226 extern void debug_update_ssa (void);
227 extern void dump_names_replaced_by (FILE *, tree);
228 extern void debug_names_replaced_by (tree);
229 extern void dump_def_blocks (FILE *);
230 extern void debug_def_blocks (void);
231 extern void dump_defs_stack (FILE *, int);
232 extern void debug_defs_stack (int);
233 extern void dump_currdefs (FILE *);
234 extern void debug_currdefs (void);
235
236 /* Return true if STMT needs to be rewritten.  When renaming a subset
237    of the variables, not all statements will be processed.  This is
238    decided in mark_def_sites.  */
239
240 static inline bool
241 rewrite_uses_p (gimple stmt)
242 {
243   return gimple_visited_p (stmt);
244 }
245
246
247 /* Set the rewrite marker on STMT to the value given by REWRITE_P.  */
248
249 static inline void
250 set_rewrite_uses (gimple stmt, bool rewrite_p)
251 {
252   gimple_set_visited (stmt, rewrite_p);
253 }
254
255
256 /* Return true if the DEFs created by statement STMT should be
257    registered when marking new definition sites.  This is slightly
258    different than rewrite_uses_p: it's used by update_ssa to
259    distinguish statements that need to have both uses and defs
260    processed from those that only need to have their defs processed.
261    Statements that define new SSA names only need to have their defs
262    registered, but they don't need to have their uses renamed.  */
263
264 static inline bool
265 register_defs_p (gimple stmt)
266 {
267   return gimple_plf (stmt, GF_PLF_1) != 0;
268 }
269
270
271 /* If REGISTER_DEFS_P is true, mark STMT to have its DEFs registered.  */
272
273 static inline void
274 set_register_defs (gimple stmt, bool register_defs_p)
275 {
276   gimple_set_plf (stmt, GF_PLF_1, register_defs_p);
277 }
278
279
280 /* Get the information associated with NAME.  */
281
282 static inline ssa_name_info_p
283 get_ssa_name_ann (tree name)
284 {
285   unsigned ver = SSA_NAME_VERSION (name);
286   unsigned len = VEC_length (ssa_name_info_p, info_for_ssa_name);
287   struct ssa_name_info *info;
288
289   if (ver >= len)
290     {
291       unsigned new_len = num_ssa_names;
292
293       VEC_reserve (ssa_name_info_p, heap, info_for_ssa_name, new_len);
294       while (len++ < new_len)
295         {
296           struct ssa_name_info *info = XCNEW (struct ssa_name_info);
297           info->age = current_info_for_ssa_name_age;
298           VEC_quick_push (ssa_name_info_p, info_for_ssa_name, info);
299         }
300     }
301
302   info = VEC_index (ssa_name_info_p, info_for_ssa_name, ver);
303   if (info->age < current_info_for_ssa_name_age)
304     {
305       info->need_phi_state = NEED_PHI_STATE_UNKNOWN;
306       info->current_def = NULL_TREE;
307       info->age = current_info_for_ssa_name_age;
308     }
309
310   return info;
311 }
312
313
314 /* Clears info for SSA names.  */
315
316 static void
317 clear_ssa_name_info (void)
318 {
319   current_info_for_ssa_name_age++;
320 }
321
322
323 /* Get phi_state field for VAR.  */
324
325 static inline enum need_phi_state
326 get_phi_state (tree var)
327 {
328   if (TREE_CODE (var) == SSA_NAME)
329     return get_ssa_name_ann (var)->need_phi_state;
330   else
331     return var_ann (var)->need_phi_state;
332 }
333
334
335 /* Sets phi_state field for VAR to STATE.  */
336
337 static inline void
338 set_phi_state (tree var, enum need_phi_state state)
339 {
340   if (TREE_CODE (var) == SSA_NAME)
341     get_ssa_name_ann (var)->need_phi_state = state;
342   else
343     var_ann (var)->need_phi_state = state;
344 }
345
346
347 /* Return the current definition for VAR.  */
348
349 tree
350 get_current_def (tree var)
351 {
352   if (TREE_CODE (var) == SSA_NAME)
353     return get_ssa_name_ann (var)->current_def;
354   else
355     return var_ann (var)->current_def;
356 }
357
358
359 /* Sets current definition of VAR to DEF.  */
360
361 void
362 set_current_def (tree var, tree def)
363 {
364   if (TREE_CODE (var) == SSA_NAME)
365     get_ssa_name_ann (var)->current_def = def;
366   else
367     var_ann (var)->current_def = def;
368 }
369
370
371 /* Compute global livein information given the set of blocks where
372    an object is locally live at the start of the block (LIVEIN)
373    and the set of blocks where the object is defined (DEF_BLOCKS).
374
375    Note: This routine augments the existing local livein information
376    to include global livein (i.e., it modifies the underlying bitmap
377    for LIVEIN).  */
378
379 void
380 compute_global_livein (bitmap livein ATTRIBUTE_UNUSED, bitmap def_blocks ATTRIBUTE_UNUSED)
381 {
382   basic_block bb, *worklist, *tos;
383   unsigned i;
384   bitmap_iterator bi;
385
386   tos = worklist
387     = (basic_block *) xmalloc (sizeof (basic_block) * (last_basic_block + 1));
388
389   EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
390     *tos++ = BASIC_BLOCK (i);
391
392   /* Iterate until the worklist is empty.  */
393   while (tos != worklist)
394     {
395       edge e;
396       edge_iterator ei;
397
398       /* Pull a block off the worklist.  */
399       bb = *--tos;
400
401       /* For each predecessor block.  */
402       FOR_EACH_EDGE (e, ei, bb->preds)
403         {
404           basic_block pred = e->src;
405           int pred_index = pred->index;
406
407           /* None of this is necessary for the entry block.  */
408           if (pred != ENTRY_BLOCK_PTR
409               && ! bitmap_bit_p (livein, pred_index)
410               && ! bitmap_bit_p (def_blocks, pred_index))
411             {
412               *tos++ = pred;
413               bitmap_set_bit (livein, pred_index);
414             }
415         }
416     }
417
418   free (worklist);
419 }
420
421
422 /* Cleans up the REWRITE_THIS_STMT and REGISTER_DEFS_IN_THIS_STMT flags for
423    all statements in basic block BB.  */
424
425 static void
426 initialize_flags_in_bb (basic_block bb)
427 {
428   gimple stmt;
429   gimple_stmt_iterator gsi;
430
431   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
432     {
433       gimple phi = gsi_stmt (gsi);
434       set_rewrite_uses (phi, false);
435       set_register_defs (phi, false);
436     }
437
438   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
439     {
440       stmt = gsi_stmt (gsi);
441
442       /* We are going to use the operand cache API, such as
443          SET_USE, SET_DEF, and FOR_EACH_IMM_USE_FAST.  The operand
444          cache for each statement should be up-to-date.  */
445       gcc_assert (!gimple_modified_p (stmt));
446       set_rewrite_uses (stmt, false);
447       set_register_defs (stmt, false);
448     }
449 }
450
451 /* Mark block BB as interesting for update_ssa.  */
452
453 static void
454 mark_block_for_update (basic_block bb)
455 {
456   gcc_assert (blocks_to_update != NULL);
457   if (bitmap_bit_p (blocks_to_update, bb->index))
458     return;
459   bitmap_set_bit (blocks_to_update, bb->index);
460   initialize_flags_in_bb (bb);
461 }
462
463 /* Return the set of blocks where variable VAR is defined and the blocks
464    where VAR is live on entry (livein).  If no entry is found in
465    DEF_BLOCKS, a new one is created and returned.  */
466
467 static inline struct def_blocks_d *
468 get_def_blocks_for (tree var)
469 {
470   struct def_blocks_d db, *db_p;
471   void **slot;
472
473   db.var = var;
474   slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
475   if (*slot == NULL)
476     {
477       db_p = XNEW (struct def_blocks_d);
478       db_p->var = var;
479       db_p->def_blocks = BITMAP_ALLOC (NULL);
480       db_p->phi_blocks = BITMAP_ALLOC (NULL);
481       db_p->livein_blocks = BITMAP_ALLOC (NULL);
482       *slot = (void *) db_p;
483     }
484   else
485     db_p = (struct def_blocks_d *) *slot;
486
487   return db_p;
488 }
489
490
491 /* Mark block BB as the definition site for variable VAR.  PHI_P is true if
492    VAR is defined by a PHI node.  */
493
494 static void
495 set_def_block (tree var, basic_block bb, bool phi_p)
496 {
497   struct def_blocks_d *db_p;
498   enum need_phi_state state;
499
500   state = get_phi_state (var);
501   db_p = get_def_blocks_for (var);
502
503   /* Set the bit corresponding to the block where VAR is defined.  */
504   bitmap_set_bit (db_p->def_blocks, bb->index);
505   if (phi_p)
506     bitmap_set_bit (db_p->phi_blocks, bb->index);
507
508   /* Keep track of whether or not we may need to insert PHI nodes.
509
510      If we are in the UNKNOWN state, then this is the first definition
511      of VAR.  Additionally, we have not seen any uses of VAR yet, so
512      we do not need a PHI node for this variable at this time (i.e.,
513      transition to NEED_PHI_STATE_NO).
514
515      If we are in any other state, then we either have multiple definitions
516      of this variable occurring in different blocks or we saw a use of the
517      variable which was not dominated by the block containing the
518      definition(s).  In this case we may need a PHI node, so enter
519      state NEED_PHI_STATE_MAYBE.  */
520   if (state == NEED_PHI_STATE_UNKNOWN)
521     set_phi_state (var, NEED_PHI_STATE_NO);
522   else
523     set_phi_state (var, NEED_PHI_STATE_MAYBE);
524 }
525
526
527 /* Mark block BB as having VAR live at the entry to BB.  */
528
529 static void
530 set_livein_block (tree var, basic_block bb)
531 {
532   struct def_blocks_d *db_p;
533   enum need_phi_state state = get_phi_state (var);
534
535   db_p = get_def_blocks_for (var);
536
537   /* Set the bit corresponding to the block where VAR is live in.  */
538   bitmap_set_bit (db_p->livein_blocks, bb->index);
539
540   /* Keep track of whether or not we may need to insert PHI nodes.
541
542      If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
543      by the single block containing the definition(s) of this variable.  If
544      it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
545      NEED_PHI_STATE_MAYBE.  */
546   if (state == NEED_PHI_STATE_NO)
547     {
548       int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
549
550       if (def_block_index == -1
551           || ! dominated_by_p (CDI_DOMINATORS, bb,
552                                BASIC_BLOCK (def_block_index)))
553         set_phi_state (var, NEED_PHI_STATE_MAYBE);
554     }
555   else
556     set_phi_state (var, NEED_PHI_STATE_MAYBE);
557 }
558
559
560 /* Return true if symbol SYM is marked for renaming.  */
561
562 static inline bool
563 symbol_marked_for_renaming (tree sym)
564 {
565   return bitmap_bit_p (SYMS_TO_RENAME (cfun), DECL_UID (sym));
566 }
567
568
569 /* Return true if NAME is in OLD_SSA_NAMES.  */
570
571 static inline bool
572 is_old_name (tree name)
573 {
574   unsigned ver = SSA_NAME_VERSION (name);
575   if (!new_ssa_names)
576     return false;
577   return ver < new_ssa_names->n_bits && TEST_BIT (old_ssa_names, ver);
578 }
579
580
581 /* Return true if NAME is in NEW_SSA_NAMES.  */
582
583 static inline bool
584 is_new_name (tree name)
585 {
586   unsigned ver = SSA_NAME_VERSION (name);
587   if (!new_ssa_names)
588     return false;
589   return ver < new_ssa_names->n_bits && TEST_BIT (new_ssa_names, ver);
590 }
591
592
593 /* Hashing and equality functions for REPL_TBL.  */
594
595 static hashval_t
596 repl_map_hash (const void *p)
597 {
598   return htab_hash_pointer ((const void *)((const struct repl_map_d *)p)->name);
599 }
600
601 static int
602 repl_map_eq (const void *p1, const void *p2)
603 {
604   return ((const struct repl_map_d *)p1)->name
605          == ((const struct repl_map_d *)p2)->name;
606 }
607
608 static void
609 repl_map_free (void *p)
610 {
611   BITMAP_FREE (((struct repl_map_d *)p)->set);
612   free (p);
613 }
614
615
616 /* Return the names replaced by NEW_TREE (i.e., REPL_TBL[NEW_TREE].SET).  */
617
618 static inline bitmap
619 names_replaced_by (tree new_tree)
620 {
621   struct repl_map_d m;
622   void **slot;
623
624   m.name = new_tree;
625   slot = htab_find_slot (repl_tbl, (void *) &m, NO_INSERT);
626
627   /* If N was not registered in the replacement table, return NULL.  */
628   if (slot == NULL || *slot == NULL)
629     return NULL;
630
631   return ((struct repl_map_d *) *slot)->set;
632 }
633
634
635 /* Add OLD to REPL_TBL[NEW_TREE].SET.  */
636
637 static inline void
638 add_to_repl_tbl (tree new_tree, tree old)
639 {
640   struct repl_map_d m, *mp;
641   void **slot;
642
643   m.name = new_tree;
644   slot = htab_find_slot (repl_tbl, (void *) &m, INSERT);
645   if (*slot == NULL)
646     {
647       mp = XNEW (struct repl_map_d);
648       mp->name = new_tree;
649       mp->set = BITMAP_ALLOC (NULL);
650       *slot = (void *) mp;
651     }
652   else
653     mp = (struct repl_map_d *) *slot;
654
655   bitmap_set_bit (mp->set, SSA_NAME_VERSION (old));
656 }
657
658
659 /* Add a new mapping NEW_TREE -> OLD REPL_TBL.  Every entry N_i in REPL_TBL
660    represents the set of names O_1 ... O_j replaced by N_i.  This is
661    used by update_ssa and its helpers to introduce new SSA names in an
662    already formed SSA web.  */
663
664 static void
665 add_new_name_mapping (tree new_tree, tree old)
666 {
667   timevar_push (TV_TREE_SSA_INCREMENTAL);
668
669   /* OLD and NEW_TREE must be different SSA names for the same symbol.  */
670   gcc_assert (new_tree != old && SSA_NAME_VAR (new_tree) == SSA_NAME_VAR (old));
671
672   /* If this mapping is for virtual names, we will need to update
673      virtual operands.  If this is a mapping for .MEM, then we gather
674      the symbols associated with each name.  */
675   if (!is_gimple_reg (new_tree))
676     {
677       tree sym;
678
679       update_ssa_stats.num_virtual_mappings++;
680       update_ssa_stats.num_virtual_symbols++;
681
682       /* Keep counts of virtual mappings and symbols to use in the
683          virtual mapping heuristic.  If we have large numbers of
684          virtual mappings for a relatively low number of symbols, it
685          will make more sense to rename the symbols from scratch.
686          Otherwise, the insertion of PHI nodes for each of the old
687          names in these mappings will be very slow.  */
688       sym = SSA_NAME_VAR (new_tree);
689       bitmap_set_bit (update_ssa_stats.virtual_symbols, DECL_UID (sym));
690     }
691
692   /* We may need to grow NEW_SSA_NAMES and OLD_SSA_NAMES because our
693      caller may have created new names since the set was created.  */
694   if (new_ssa_names->n_bits <= num_ssa_names - 1)
695     {
696       unsigned int new_sz = num_ssa_names + NAME_SETS_GROWTH_FACTOR;
697       new_ssa_names = sbitmap_resize (new_ssa_names, new_sz, 0);
698       old_ssa_names = sbitmap_resize (old_ssa_names, new_sz, 0);
699     }
700
701   /* Update the REPL_TBL table.  */
702   add_to_repl_tbl (new_tree, old);
703
704   /* If OLD had already been registered as a new name, then all the
705      names that OLD replaces should also be replaced by NEW_TREE.  */
706   if (is_new_name (old))
707     bitmap_ior_into (names_replaced_by (new_tree), names_replaced_by (old));
708
709   /* Register NEW_TREE and OLD in NEW_SSA_NAMES and OLD_SSA_NAMES,
710      respectively.  */
711   SET_BIT (new_ssa_names, SSA_NAME_VERSION (new_tree));
712   SET_BIT (old_ssa_names, SSA_NAME_VERSION (old));
713
714   /* Update mapping counter to use in the virtual mapping heuristic.  */
715   update_ssa_stats.num_total_mappings++;
716
717   timevar_pop (TV_TREE_SSA_INCREMENTAL);
718 }
719
720
721 /* Call back for walk_dominator_tree used to collect definition sites
722    for every variable in the function.  For every statement S in block
723    BB:
724
725    1- Variables defined by S in the DEFS of S are marked in the bitmap
726       KILLS.
727
728    2- If S uses a variable VAR and there is no preceding kill of VAR,
729       then it is marked in the LIVEIN_BLOCKS bitmap associated with VAR.
730
731    This information is used to determine which variables are live
732    across block boundaries to reduce the number of PHI nodes
733    we create.  */
734
735 static void
736 mark_def_sites (basic_block bb, gimple stmt, bitmap kills)
737 {
738   tree def;
739   use_operand_p use_p;
740   ssa_op_iter iter;
741
742   /* Since this is the first time that we rewrite the program into SSA
743      form, force an operand scan on every statement.  */
744   update_stmt (stmt);
745
746   gcc_assert (blocks_to_update == NULL);
747   set_register_defs (stmt, false);
748   set_rewrite_uses (stmt, false);
749
750   if (is_gimple_debug (stmt))
751     return;
752
753   /* If a variable is used before being set, then the variable is live
754      across a block boundary, so mark it live-on-entry to BB.  */
755   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
756     {
757       tree sym = USE_FROM_PTR (use_p);
758       gcc_assert (DECL_P (sym));
759       if (!bitmap_bit_p (kills, DECL_UID (sym)))
760         set_livein_block (sym, bb);
761       set_rewrite_uses (stmt, true);
762     }
763
764   /* Now process the defs.  Mark BB as the definition block and add
765      each def to the set of killed symbols.  */
766   FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF)
767     {
768       gcc_assert (DECL_P (def));
769       set_def_block (def, bb, false);
770       bitmap_set_bit (kills, DECL_UID (def));
771       set_register_defs (stmt, true);
772     }
773
774   /* If we found the statement interesting then also mark the block BB
775      as interesting.  */
776   if (rewrite_uses_p (stmt) || register_defs_p (stmt))
777     SET_BIT (interesting_blocks, bb->index);
778 }
779
780 /* Structure used by prune_unused_phi_nodes to record bounds of the intervals
781    in the dfs numbering of the dominance tree.  */
782
783 struct dom_dfsnum
784 {
785   /* Basic block whose index this entry corresponds to.  */
786   unsigned bb_index;
787
788   /* The dfs number of this node.  */
789   unsigned dfs_num;
790 };
791
792 /* Compares two entries of type struct dom_dfsnum by dfs_num field.  Callback
793    for qsort.  */
794
795 static int
796 cmp_dfsnum (const void *a, const void *b)
797 {
798   const struct dom_dfsnum *const da = (const struct dom_dfsnum *) a;
799   const struct dom_dfsnum *const db = (const struct dom_dfsnum *) b;
800
801   return (int) da->dfs_num - (int) db->dfs_num;
802 }
803
804 /* Among the intervals starting at the N points specified in DEFS, find
805    the one that contains S, and return its bb_index.  */
806
807 static unsigned
808 find_dfsnum_interval (struct dom_dfsnum *defs, unsigned n, unsigned s)
809 {
810   unsigned f = 0, t = n, m;
811
812   while (t > f + 1)
813     {
814       m = (f + t) / 2;
815       if (defs[m].dfs_num <= s)
816         f = m;
817       else
818         t = m;
819     }
820
821   return defs[f].bb_index;
822 }
823
824 /* Clean bits from PHIS for phi nodes whose value cannot be used in USES.
825    KILLS is a bitmap of blocks where the value is defined before any use.  */
826
827 static void
828 prune_unused_phi_nodes (bitmap phis, bitmap kills, bitmap uses)
829 {
830   VEC(int, heap) *worklist;
831   bitmap_iterator bi;
832   unsigned i, b, p, u, top;
833   bitmap live_phis;
834   basic_block def_bb, use_bb;
835   edge e;
836   edge_iterator ei;
837   bitmap to_remove;
838   struct dom_dfsnum *defs;
839   unsigned n_defs, adef;
840
841   if (bitmap_empty_p (uses))
842     {
843       bitmap_clear (phis);
844       return;
845     }
846
847   /* The phi must dominate a use, or an argument of a live phi.  Also, we
848      do not create any phi nodes in def blocks, unless they are also livein.  */
849   to_remove = BITMAP_ALLOC (NULL);
850   bitmap_and_compl (to_remove, kills, uses);
851   bitmap_and_compl_into (phis, to_remove);
852   if (bitmap_empty_p (phis))
853     {
854       BITMAP_FREE (to_remove);
855       return;
856     }
857
858   /* We want to remove the unnecessary phi nodes, but we do not want to compute
859      liveness information, as that may be linear in the size of CFG, and if
860      there are lot of different variables to rewrite, this may lead to quadratic
861      behavior.
862
863      Instead, we basically emulate standard dce.  We put all uses to worklist,
864      then for each of them find the nearest def that dominates them.  If this
865      def is a phi node, we mark it live, and if it was not live before, we
866      add the predecessors of its basic block to the worklist.
867
868      To quickly locate the nearest def that dominates use, we use dfs numbering
869      of the dominance tree (that is already available in order to speed up
870      queries).  For each def, we have the interval given by the dfs number on
871      entry to and on exit from the corresponding subtree in the dominance tree.
872      The nearest dominator for a given use is the smallest of these intervals
873      that contains entry and exit dfs numbers for the basic block with the use.
874      If we store the bounds for all the uses to an array and sort it, we can
875      locate the nearest dominating def in logarithmic time by binary search.*/
876   bitmap_ior (to_remove, kills, phis);
877   n_defs = bitmap_count_bits (to_remove);
878   defs = XNEWVEC (struct dom_dfsnum, 2 * n_defs + 1);
879   defs[0].bb_index = 1;
880   defs[0].dfs_num = 0;
881   adef = 1;
882   EXECUTE_IF_SET_IN_BITMAP (to_remove, 0, i, bi)
883     {
884       def_bb = BASIC_BLOCK (i);
885       defs[adef].bb_index = i;
886       defs[adef].dfs_num = bb_dom_dfs_in (CDI_DOMINATORS, def_bb);
887       defs[adef + 1].bb_index = i;
888       defs[adef + 1].dfs_num = bb_dom_dfs_out (CDI_DOMINATORS, def_bb);
889       adef += 2;
890     }
891   BITMAP_FREE (to_remove);
892   gcc_assert (adef == 2 * n_defs + 1);
893   qsort (defs, adef, sizeof (struct dom_dfsnum), cmp_dfsnum);
894   gcc_assert (defs[0].bb_index == 1);
895
896   /* Now each DEFS entry contains the number of the basic block to that the
897      dfs number corresponds.  Change them to the number of basic block that
898      corresponds to the interval following the dfs number.  Also, for the
899      dfs_out numbers, increase the dfs number by one (so that it corresponds
900      to the start of the following interval, not to the end of the current
901      one).  We use WORKLIST as a stack.  */
902   worklist = VEC_alloc (int, heap, n_defs + 1);
903   VEC_quick_push (int, worklist, 1);
904   top = 1;
905   n_defs = 1;
906   for (i = 1; i < adef; i++)
907     {
908       b = defs[i].bb_index;
909       if (b == top)
910         {
911           /* This is a closing element.  Interval corresponding to the top
912              of the stack after removing it follows.  */
913           VEC_pop (int, worklist);
914           top = VEC_index (int, worklist, VEC_length (int, worklist) - 1);
915           defs[n_defs].bb_index = top;
916           defs[n_defs].dfs_num = defs[i].dfs_num + 1;
917         }
918       else
919         {
920           /* Opening element.  Nothing to do, just push it to the stack and move
921              it to the correct position.  */
922           defs[n_defs].bb_index = defs[i].bb_index;
923           defs[n_defs].dfs_num = defs[i].dfs_num;
924           VEC_quick_push (int, worklist, b);
925           top = b;
926         }
927
928       /* If this interval starts at the same point as the previous one, cancel
929          the previous one.  */
930       if (defs[n_defs].dfs_num == defs[n_defs - 1].dfs_num)
931         defs[n_defs - 1].bb_index = defs[n_defs].bb_index;
932       else
933         n_defs++;
934     }
935   VEC_pop (int, worklist);
936   gcc_assert (VEC_empty (int, worklist));
937
938   /* Now process the uses.  */
939   live_phis = BITMAP_ALLOC (NULL);
940   EXECUTE_IF_SET_IN_BITMAP (uses, 0, i, bi)
941     {
942       VEC_safe_push (int, heap, worklist, i);
943     }
944
945   while (!VEC_empty (int, worklist))
946     {
947       b = VEC_pop (int, worklist);
948       if (b == ENTRY_BLOCK)
949         continue;
950
951       /* If there is a phi node in USE_BB, it is made live.  Otherwise,
952          find the def that dominates the immediate dominator of USE_BB
953          (the kill in USE_BB does not dominate the use).  */
954       if (bitmap_bit_p (phis, b))
955         p = b;
956       else
957         {
958           use_bb = get_immediate_dominator (CDI_DOMINATORS, BASIC_BLOCK (b));
959           p = find_dfsnum_interval (defs, n_defs,
960                                     bb_dom_dfs_in (CDI_DOMINATORS, use_bb));
961           if (!bitmap_bit_p (phis, p))
962             continue;
963         }
964
965       /* If the phi node is already live, there is nothing to do.  */
966       if (bitmap_bit_p (live_phis, p))
967         continue;
968
969       /* Mark the phi as live, and add the new uses to the worklist.  */
970       bitmap_set_bit (live_phis, p);
971       def_bb = BASIC_BLOCK (p);
972       FOR_EACH_EDGE (e, ei, def_bb->preds)
973         {
974           u = e->src->index;
975           if (bitmap_bit_p (uses, u))
976             continue;
977
978           /* In case there is a kill directly in the use block, do not record
979              the use (this is also necessary for correctness, as we assume that
980              uses dominated by a def directly in their block have been filtered
981              out before).  */
982           if (bitmap_bit_p (kills, u))
983             continue;
984
985           bitmap_set_bit (uses, u);
986           VEC_safe_push (int, heap, worklist, u);
987         }
988     }
989
990   VEC_free (int, heap, worklist);
991   bitmap_copy (phis, live_phis);
992   BITMAP_FREE (live_phis);
993   free (defs);
994 }
995
996 /* Return the set of blocks where variable VAR is defined and the blocks
997    where VAR is live on entry (livein).  Return NULL, if no entry is
998    found in DEF_BLOCKS.  */
999
1000 static inline struct def_blocks_d *
1001 find_def_blocks_for (tree var)
1002 {
1003   struct def_blocks_d dm;
1004   dm.var = var;
1005   return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1006 }
1007
1008
1009 /* Retrieve or create a default definition for symbol SYM.  */
1010
1011 static inline tree
1012 get_default_def_for (tree sym)
1013 {
1014   tree ddef = gimple_default_def (cfun, sym);
1015
1016   if (ddef == NULL_TREE)
1017     {
1018       ddef = make_ssa_name (sym, gimple_build_nop ());
1019       set_default_def (sym, ddef);
1020     }
1021
1022   return ddef;
1023 }
1024
1025
1026 /* Marks phi node PHI in basic block BB for rewrite.  */
1027
1028 static void
1029 mark_phi_for_rewrite (basic_block bb, gimple phi)
1030 {
1031   gimple_vec phis;
1032   unsigned i, idx = bb->index;
1033
1034   if (rewrite_uses_p (phi))
1035     return;
1036
1037   set_rewrite_uses (phi, true);
1038
1039   if (!blocks_with_phis_to_rewrite)
1040     return;
1041
1042   bitmap_set_bit (blocks_with_phis_to_rewrite, idx);
1043   VEC_reserve (gimple_vec, heap, phis_to_rewrite, last_basic_block + 1);
1044   for (i = VEC_length (gimple_vec, phis_to_rewrite); i <= idx; i++)
1045     VEC_quick_push (gimple_vec, phis_to_rewrite, NULL);
1046
1047   phis = VEC_index (gimple_vec, phis_to_rewrite, idx);
1048   if (!phis)
1049     phis = VEC_alloc (gimple, heap, 10);
1050
1051   VEC_safe_push (gimple, heap, phis, phi);
1052   VEC_replace (gimple_vec, phis_to_rewrite, idx, phis);
1053 }
1054
1055 /* Insert PHI nodes for variable VAR using the iterated dominance
1056    frontier given in PHI_INSERTION_POINTS.  If UPDATE_P is true, this
1057    function assumes that the caller is incrementally updating the
1058    existing SSA form, in which case VAR may be an SSA name instead of
1059    a symbol.
1060
1061    PHI_INSERTION_POINTS is updated to reflect nodes that already had a
1062    PHI node for VAR.  On exit, only the nodes that received a PHI node
1063    for VAR will be present in PHI_INSERTION_POINTS.  */
1064
1065 static void
1066 insert_phi_nodes_for (tree var, bitmap phi_insertion_points, bool update_p)
1067 {
1068   unsigned bb_index;
1069   edge e;
1070   gimple phi;
1071   basic_block bb;
1072   bitmap_iterator bi;
1073   struct def_blocks_d *def_map;
1074
1075   def_map = find_def_blocks_for (var);
1076   gcc_assert (def_map);
1077
1078   /* Remove the blocks where we already have PHI nodes for VAR.  */
1079   bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1080
1081   /* Remove obviously useless phi nodes.  */
1082   prune_unused_phi_nodes (phi_insertion_points, def_map->def_blocks,
1083                           def_map->livein_blocks);
1084
1085   /* And insert the PHI nodes.  */
1086   EXECUTE_IF_SET_IN_BITMAP (phi_insertion_points, 0, bb_index, bi)
1087     {
1088       bb = BASIC_BLOCK (bb_index);
1089       if (update_p)
1090         mark_block_for_update (bb);
1091
1092       phi = NULL;
1093
1094       if (TREE_CODE (var) == SSA_NAME)
1095         {
1096           /* If we are rewriting SSA names, create the LHS of the PHI
1097              node by duplicating VAR.  This is useful in the case of
1098              pointers, to also duplicate pointer attributes (alias
1099              information, in particular).  */
1100           edge_iterator ei;
1101           tree new_lhs;
1102
1103           gcc_assert (update_p);
1104           phi = create_phi_node (var, bb);
1105
1106           new_lhs = duplicate_ssa_name (var, phi);
1107           gimple_phi_set_result (phi, new_lhs);
1108           add_new_name_mapping (new_lhs, var);
1109
1110           /* Add VAR to every argument slot of PHI.  We need VAR in
1111              every argument so that rewrite_update_phi_arguments knows
1112              which name is this PHI node replacing.  If VAR is a
1113              symbol marked for renaming, this is not necessary, the
1114              renamer will use the symbol on the LHS to get its
1115              reaching definition.  */
1116           FOR_EACH_EDGE (e, ei, bb->preds)
1117             add_phi_arg (phi, var, e, UNKNOWN_LOCATION);
1118         }
1119       else
1120         {
1121           tree tracked_var;
1122
1123           gcc_assert (DECL_P (var));
1124           phi = create_phi_node (var, bb);
1125
1126           tracked_var = target_for_debug_bind (var);
1127           if (tracked_var)
1128             {
1129               gimple note = gimple_build_debug_bind (tracked_var,
1130                                                      PHI_RESULT (phi),
1131                                                      phi);
1132               gimple_stmt_iterator si = gsi_after_labels (bb);
1133               gsi_insert_before (&si, note, GSI_SAME_STMT);
1134             }
1135         }
1136
1137       /* Mark this PHI node as interesting for update_ssa.  */
1138       set_register_defs (phi, true);
1139       mark_phi_for_rewrite (bb, phi);
1140     }
1141 }
1142
1143
1144 /* Insert PHI nodes at the dominance frontier of blocks with variable
1145    definitions.  DFS contains the dominance frontier information for
1146    the flowgraph.  */
1147
1148 static void
1149 insert_phi_nodes (bitmap *dfs)
1150 {
1151   referenced_var_iterator rvi;
1152   bitmap_iterator bi;
1153   tree var;
1154   bitmap vars;
1155   unsigned uid;
1156
1157   timevar_push (TV_TREE_INSERT_PHI_NODES);
1158
1159   /* Do two stages to avoid code generation differences for UID
1160      differences but no UID ordering differences.  */
1161
1162   vars = BITMAP_ALLOC (NULL);
1163   FOR_EACH_REFERENCED_VAR (var, rvi)
1164     {
1165       struct def_blocks_d *def_map;
1166
1167       def_map = find_def_blocks_for (var);
1168       if (def_map == NULL)
1169         continue;
1170
1171       if (get_phi_state (var) != NEED_PHI_STATE_NO)
1172         bitmap_set_bit (vars, DECL_UID (var));
1173     }
1174
1175   EXECUTE_IF_SET_IN_BITMAP (vars, 0, uid, bi)
1176     {
1177       tree var = referenced_var (uid);
1178       struct def_blocks_d *def_map;
1179       bitmap idf;
1180
1181       def_map = find_def_blocks_for (var);
1182       idf = compute_idf (def_map->def_blocks, dfs);
1183       insert_phi_nodes_for (var, idf, false);
1184       BITMAP_FREE (idf);
1185     }
1186
1187   BITMAP_FREE (vars);
1188
1189   timevar_pop (TV_TREE_INSERT_PHI_NODES);
1190 }
1191
1192
1193 /* Push SYM's current reaching definition into BLOCK_DEFS_STACK and
1194    register DEF (an SSA_NAME) to be a new definition for SYM.  */
1195
1196 static void
1197 register_new_def (tree def, tree sym)
1198 {
1199   tree currdef;
1200
1201   /* If this variable is set in a single basic block and all uses are
1202      dominated by the set(s) in that single basic block, then there is
1203      no reason to record anything for this variable in the block local
1204      definition stacks.  Doing so just wastes time and memory.
1205
1206      This is the same test to prune the set of variables which may
1207      need PHI nodes.  So we just use that information since it's already
1208      computed and available for us to use.  */
1209   if (get_phi_state (sym) == NEED_PHI_STATE_NO)
1210     {
1211       set_current_def (sym, def);
1212       return;
1213     }
1214
1215   currdef = get_current_def (sym);
1216
1217   /* If SYM is not a GIMPLE register, then CURRDEF may be a name whose
1218      SSA_NAME_VAR is not necessarily SYM.  In this case, also push SYM
1219      in the stack so that we know which symbol is being defined by
1220      this SSA name when we unwind the stack.  */
1221   if (currdef && !is_gimple_reg (sym))
1222     VEC_safe_push (tree, heap, block_defs_stack, sym);
1223
1224   /* Push the current reaching definition into BLOCK_DEFS_STACK.  This
1225      stack is later used by the dominator tree callbacks to restore
1226      the reaching definitions for all the variables defined in the
1227      block after a recursive visit to all its immediately dominated
1228      blocks.  If there is no current reaching definition, then just
1229      record the underlying _DECL node.  */
1230   VEC_safe_push (tree, heap, block_defs_stack, currdef ? currdef : sym);
1231
1232   /* Set the current reaching definition for SYM to be DEF.  */
1233   set_current_def (sym, def);
1234 }
1235
1236
1237 /* Perform a depth-first traversal of the dominator tree looking for
1238    variables to rename.  BB is the block where to start searching.
1239    Renaming is a five step process:
1240
1241    1- Every definition made by PHI nodes at the start of the blocks is
1242       registered as the current definition for the corresponding variable.
1243
1244    2- Every statement in BB is rewritten.  USE and VUSE operands are
1245       rewritten with their corresponding reaching definition.  DEF and
1246       VDEF targets are registered as new definitions.
1247
1248    3- All the PHI nodes in successor blocks of BB are visited.  The
1249       argument corresponding to BB is replaced with its current reaching
1250       definition.
1251
1252    4- Recursively rewrite every dominator child block of BB.
1253
1254    5- Restore (in reverse order) the current reaching definition for every
1255       new definition introduced in this block.  This is done so that when
1256       we return from the recursive call, all the current reaching
1257       definitions are restored to the names that were valid in the
1258       dominator parent of BB.  */
1259
1260 /* Return the current definition for variable VAR.  If none is found,
1261    create a new SSA name to act as the zeroth definition for VAR.  */
1262
1263 static tree
1264 get_reaching_def (tree var)
1265 {
1266   tree currdef;
1267
1268   /* Lookup the current reaching definition for VAR.  */
1269   currdef = get_current_def (var);
1270
1271   /* If there is no reaching definition for VAR, create and register a
1272      default definition for it (if needed).  */
1273   if (currdef == NULL_TREE)
1274     {
1275       tree sym = DECL_P (var) ? var : SSA_NAME_VAR (var);
1276       currdef = get_default_def_for (sym);
1277       set_current_def (var, currdef);
1278     }
1279
1280   /* Return the current reaching definition for VAR, or the default
1281      definition, if we had to create one.  */
1282   return currdef;
1283 }
1284
1285
1286 /* SSA Rewriting Step 2.  Rewrite every variable used in each statement in
1287    the block with its immediate reaching definitions.  Update the current
1288    definition of a variable when a new real or virtual definition is found.  */
1289
1290 static void
1291 rewrite_stmt (gimple_stmt_iterator si)
1292 {
1293   use_operand_p use_p;
1294   def_operand_p def_p;
1295   ssa_op_iter iter;
1296   gimple stmt = gsi_stmt (si);
1297
1298   /* If mark_def_sites decided that we don't need to rewrite this
1299      statement, ignore it.  */
1300   gcc_assert (blocks_to_update == NULL);
1301   if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1302     return;
1303
1304   if (dump_file && (dump_flags & TDF_DETAILS))
1305     {
1306       fprintf (dump_file, "Renaming statement ");
1307       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1308       fprintf (dump_file, "\n");
1309     }
1310
1311   /* Step 1.  Rewrite USES in the statement.  */
1312   if (rewrite_uses_p (stmt))
1313     FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1314       {
1315         tree var = USE_FROM_PTR (use_p);
1316         gcc_assert (DECL_P (var));
1317         SET_USE (use_p, get_reaching_def (var));
1318       }
1319
1320   /* Step 2.  Register the statement's DEF operands.  */
1321   if (register_defs_p (stmt))
1322     FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_DEF)
1323       {
1324         tree var = DEF_FROM_PTR (def_p);
1325         tree name = make_ssa_name (var, stmt);
1326         tree tracked_var;
1327         gcc_assert (DECL_P (var));
1328         SET_DEF (def_p, name);
1329         register_new_def (DEF_FROM_PTR (def_p), var);
1330
1331         tracked_var = target_for_debug_bind (var);
1332         if (tracked_var)
1333           {
1334             gimple note = gimple_build_debug_bind (tracked_var, name, stmt);
1335             gsi_insert_after (&si, note, GSI_SAME_STMT);
1336           }
1337       }
1338 }
1339
1340
1341 /* SSA Rewriting Step 3.  Visit all the successor blocks of BB looking for
1342    PHI nodes.  For every PHI node found, add a new argument containing the
1343    current reaching definition for the variable and the edge through which
1344    that definition is reaching the PHI node.  */
1345
1346 static void
1347 rewrite_add_phi_arguments (basic_block bb)
1348 {
1349   edge e;
1350   edge_iterator ei;
1351
1352   FOR_EACH_EDGE (e, ei, bb->succs)
1353     {
1354       gimple phi;
1355       gimple_stmt_iterator gsi;
1356
1357       for (gsi = gsi_start_phis (e->dest); !gsi_end_p (gsi);
1358            gsi_next (&gsi))
1359         {
1360           tree currdef;
1361           gimple stmt;
1362
1363           phi = gsi_stmt (gsi);
1364           currdef = get_reaching_def (SSA_NAME_VAR (gimple_phi_result (phi)));
1365           stmt = SSA_NAME_DEF_STMT (currdef);
1366           add_phi_arg (phi, currdef, e, gimple_location (stmt));
1367         }
1368     }
1369 }
1370
1371 /* SSA Rewriting Step 1.  Initialization, create a block local stack
1372    of reaching definitions for new SSA names produced in this block
1373    (BLOCK_DEFS).  Register new definitions for every PHI node in the
1374    block.  */
1375
1376 static void
1377 rewrite_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1378                      basic_block bb)
1379 {
1380   gimple phi;
1381   gimple_stmt_iterator gsi;
1382
1383   if (dump_file && (dump_flags & TDF_DETAILS))
1384     fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
1385
1386   /* Mark the unwind point for this block.  */
1387   VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
1388
1389   /* Step 1.  Register new definitions for every PHI node in the block.
1390      Conceptually, all the PHI nodes are executed in parallel and each PHI
1391      node introduces a new version for the associated variable.  */
1392   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1393     {
1394       tree result;
1395
1396       phi = gsi_stmt (gsi);
1397       result = gimple_phi_result (phi);
1398       gcc_assert (is_gimple_reg (result));
1399       register_new_def (result, SSA_NAME_VAR (result));
1400     }
1401
1402   /* Step 2.  Rewrite every variable used in each statement in the block
1403      with its immediate reaching definitions.  Update the current definition
1404      of a variable when a new real or virtual definition is found.  */
1405   if (TEST_BIT (interesting_blocks, bb->index))
1406     for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
1407       rewrite_stmt (gsi);
1408
1409   /* Step 3.  Visit all the successor blocks of BB looking for PHI nodes.
1410      For every PHI node found, add a new argument containing the current
1411      reaching definition for the variable and the edge through which that
1412      definition is reaching the PHI node.  */
1413   rewrite_add_phi_arguments (bb);
1414 }
1415
1416
1417
1418 /* Called after visiting all the statements in basic block BB and all
1419    of its dominator children.  Restore CURRDEFS to its original value.  */
1420
1421 static void
1422 rewrite_leave_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1423                      basic_block bb ATTRIBUTE_UNUSED)
1424 {
1425   /* Restore CURRDEFS to its original state.  */
1426   while (VEC_length (tree, block_defs_stack) > 0)
1427     {
1428       tree tmp = VEC_pop (tree, block_defs_stack);
1429       tree saved_def, var;
1430
1431       if (tmp == NULL_TREE)
1432         break;
1433
1434       if (TREE_CODE (tmp) == SSA_NAME)
1435         {
1436           /* If we recorded an SSA_NAME, then make the SSA_NAME the
1437              current definition of its underlying variable.  Note that
1438              if the SSA_NAME is not for a GIMPLE register, the symbol
1439              being defined is stored in the next slot in the stack.
1440              This mechanism is needed because an SSA name for a
1441              non-register symbol may be the definition for more than
1442              one symbol (e.g., SFTs, aliased variables, etc).  */
1443           saved_def = tmp;
1444           var = SSA_NAME_VAR (saved_def);
1445           if (!is_gimple_reg (var))
1446             var = VEC_pop (tree, block_defs_stack);
1447         }
1448       else
1449         {
1450           /* If we recorded anything else, it must have been a _DECL
1451              node and its current reaching definition must have been
1452              NULL.  */
1453           saved_def = NULL;
1454           var = tmp;
1455         }
1456
1457       set_current_def (var, saved_def);
1458     }
1459 }
1460
1461
1462 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE.  */
1463
1464 void
1465 dump_decl_set (FILE *file, bitmap set)
1466 {
1467   if (set)
1468     {
1469       bitmap_iterator bi;
1470       unsigned i;
1471
1472       fprintf (file, "{ ");
1473
1474       EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
1475         {
1476           struct tree_decl_minimal in;
1477           tree var;
1478           in.uid = i;
1479           var = (tree) htab_find_with_hash (gimple_referenced_vars (cfun),
1480                                             &in, i);
1481           if (var)
1482             print_generic_expr (file, var, 0);
1483           else
1484             fprintf (file, "D.%u", i);
1485           fprintf (file, " ");
1486         }
1487
1488       fprintf (file, "}");
1489     }
1490   else
1491     fprintf (file, "NIL");
1492 }
1493
1494
1495 /* Dump bitmap SET (assumed to contain VAR_DECLs) to FILE.  */
1496
1497 void
1498 debug_decl_set (bitmap set)
1499 {
1500   dump_decl_set (stderr, set);
1501   fprintf (stderr, "\n");
1502 }
1503
1504
1505 /* Dump the renaming stack (block_defs_stack) to FILE.  Traverse the
1506    stack up to a maximum of N levels.  If N is -1, the whole stack is
1507    dumped.  New levels are created when the dominator tree traversal
1508    used for renaming enters a new sub-tree.  */
1509
1510 void
1511 dump_defs_stack (FILE *file, int n)
1512 {
1513   int i, j;
1514
1515   fprintf (file, "\n\nRenaming stack");
1516   if (n > 0)
1517     fprintf (file, " (up to %d levels)", n);
1518   fprintf (file, "\n\n");
1519
1520   i = 1;
1521   fprintf (file, "Level %d (current level)\n", i);
1522   for (j = (int) VEC_length (tree, block_defs_stack) - 1; j >= 0; j--)
1523     {
1524       tree name, var;
1525
1526       name = VEC_index (tree, block_defs_stack, j);
1527       if (name == NULL_TREE)
1528         {
1529           i++;
1530           if (n > 0 && i > n)
1531             break;
1532           fprintf (file, "\nLevel %d\n", i);
1533           continue;
1534         }
1535
1536       if (DECL_P (name))
1537         {
1538           var = name;
1539           name = NULL_TREE;
1540         }
1541       else
1542         {
1543           var = SSA_NAME_VAR (name);
1544           if (!is_gimple_reg (var))
1545             {
1546               j--;
1547               var = VEC_index (tree, block_defs_stack, j);
1548             }
1549         }
1550
1551       fprintf (file, "    Previous CURRDEF (");
1552       print_generic_expr (file, var, 0);
1553       fprintf (file, ") = ");
1554       if (name)
1555         print_generic_expr (file, name, 0);
1556       else
1557         fprintf (file, "<NIL>");
1558       fprintf (file, "\n");
1559     }
1560 }
1561
1562
1563 /* Dump the renaming stack (block_defs_stack) to stderr.  Traverse the
1564    stack up to a maximum of N levels.  If N is -1, the whole stack is
1565    dumped.  New levels are created when the dominator tree traversal
1566    used for renaming enters a new sub-tree.  */
1567
1568 void
1569 debug_defs_stack (int n)
1570 {
1571   dump_defs_stack (stderr, n);
1572 }
1573
1574
1575 /* Dump the current reaching definition of every symbol to FILE.  */
1576
1577 void
1578 dump_currdefs (FILE *file)
1579 {
1580   referenced_var_iterator i;
1581   tree var;
1582
1583   fprintf (file, "\n\nCurrent reaching definitions\n\n");
1584   FOR_EACH_REFERENCED_VAR (var, i)
1585     if (SYMS_TO_RENAME (cfun) == NULL
1586         || bitmap_bit_p (SYMS_TO_RENAME (cfun), DECL_UID (var)))
1587       {
1588         fprintf (file, "CURRDEF (");
1589         print_generic_expr (file, var, 0);
1590         fprintf (file, ") = ");
1591         if (get_current_def (var))
1592           print_generic_expr (file, get_current_def (var), 0);
1593         else
1594           fprintf (file, "<NIL>");
1595         fprintf (file, "\n");
1596       }
1597 }
1598
1599
1600 /* Dump the current reaching definition of every symbol to stderr.  */
1601
1602 void
1603 debug_currdefs (void)
1604 {
1605   dump_currdefs (stderr);
1606 }
1607
1608
1609 /* Dump SSA information to FILE.  */
1610
1611 void
1612 dump_tree_ssa (FILE *file)
1613 {
1614   const char *funcname
1615     = lang_hooks.decl_printable_name (current_function_decl, 2);
1616
1617   fprintf (file, "SSA renaming information for %s\n\n", funcname);
1618
1619   dump_def_blocks (file);
1620   dump_defs_stack (file, -1);
1621   dump_currdefs (file);
1622   dump_tree_ssa_stats (file);
1623 }
1624
1625
1626 /* Dump SSA information to stderr.  */
1627
1628 void
1629 debug_tree_ssa (void)
1630 {
1631   dump_tree_ssa (stderr);
1632 }
1633
1634
1635 /* Dump statistics for the hash table HTAB.  */
1636
1637 static void
1638 htab_statistics (FILE *file, htab_t htab)
1639 {
1640   fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
1641            (long) htab_size (htab),
1642            (long) htab_elements (htab),
1643            htab_collisions (htab));
1644 }
1645
1646
1647 /* Dump SSA statistics on FILE.  */
1648
1649 void
1650 dump_tree_ssa_stats (FILE *file)
1651 {
1652   if (def_blocks || repl_tbl)
1653     fprintf (file, "\nHash table statistics:\n");
1654
1655   if (def_blocks)
1656     {
1657       fprintf (file, "    def_blocks:   ");
1658       htab_statistics (file, def_blocks);
1659     }
1660
1661   if (repl_tbl)
1662     {
1663       fprintf (file, "    repl_tbl:     ");
1664       htab_statistics (file, repl_tbl);
1665     }
1666
1667   if (def_blocks || repl_tbl)
1668     fprintf (file, "\n");
1669 }
1670
1671
1672 /* Dump SSA statistics on stderr.  */
1673
1674 void
1675 debug_tree_ssa_stats (void)
1676 {
1677   dump_tree_ssa_stats (stderr);
1678 }
1679
1680
1681 /* Hashing and equality functions for DEF_BLOCKS.  */
1682
1683 static hashval_t
1684 def_blocks_hash (const void *p)
1685 {
1686   return htab_hash_pointer
1687         ((const void *)((const struct def_blocks_d *)p)->var);
1688 }
1689
1690 static int
1691 def_blocks_eq (const void *p1, const void *p2)
1692 {
1693   return ((const struct def_blocks_d *)p1)->var
1694          == ((const struct def_blocks_d *)p2)->var;
1695 }
1696
1697
1698 /* Free memory allocated by one entry in DEF_BLOCKS.  */
1699
1700 static void
1701 def_blocks_free (void *p)
1702 {
1703   struct def_blocks_d *entry = (struct def_blocks_d *) p;
1704   BITMAP_FREE (entry->def_blocks);
1705   BITMAP_FREE (entry->phi_blocks);
1706   BITMAP_FREE (entry->livein_blocks);
1707   free (entry);
1708 }
1709
1710
1711 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table.  */
1712
1713 static int
1714 debug_def_blocks_r (void **slot, void *data)
1715 {
1716   FILE *file = (FILE *) data;
1717   struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1718
1719   fprintf (file, "VAR: ");
1720   print_generic_expr (file, db_p->var, dump_flags);
1721   bitmap_print (file, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1722   bitmap_print (file, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}");
1723   bitmap_print (file, db_p->phi_blocks, ", PHI_BLOCKS: { ", "}\n");
1724
1725   return 1;
1726 }
1727
1728
1729 /* Dump the DEF_BLOCKS hash table on FILE.  */
1730
1731 void
1732 dump_def_blocks (FILE *file)
1733 {
1734   fprintf (file, "\n\nDefinition and live-in blocks:\n\n");
1735   if (def_blocks)
1736     htab_traverse (def_blocks, debug_def_blocks_r, file);
1737 }
1738
1739
1740 /* Dump the DEF_BLOCKS hash table on stderr.  */
1741
1742 void
1743 debug_def_blocks (void)
1744 {
1745   dump_def_blocks (stderr);
1746 }
1747
1748
1749 /* Register NEW_NAME to be the new reaching definition for OLD_NAME.  */
1750
1751 static inline void
1752 register_new_update_single (tree new_name, tree old_name)
1753 {
1754   tree currdef = get_current_def (old_name);
1755
1756   /* Push the current reaching definition into BLOCK_DEFS_STACK.
1757      This stack is later used by the dominator tree callbacks to
1758      restore the reaching definitions for all the variables
1759      defined in the block after a recursive visit to all its
1760      immediately dominated blocks.  */
1761   VEC_reserve (tree, heap, block_defs_stack, 2);
1762   VEC_quick_push (tree, block_defs_stack, currdef);
1763   VEC_quick_push (tree, block_defs_stack, old_name);
1764
1765   /* Set the current reaching definition for OLD_NAME to be
1766      NEW_NAME.  */
1767   set_current_def (old_name, new_name);
1768 }
1769
1770
1771 /* Register NEW_NAME to be the new reaching definition for all the
1772    names in OLD_NAMES.  Used by the incremental SSA update routines to
1773    replace old SSA names with new ones.  */
1774
1775 static inline void
1776 register_new_update_set (tree new_name, bitmap old_names)
1777 {
1778   bitmap_iterator bi;
1779   unsigned i;
1780
1781   EXECUTE_IF_SET_IN_BITMAP (old_names, 0, i, bi)
1782     register_new_update_single (new_name, ssa_name (i));
1783 }
1784
1785
1786
1787 /* If the operand pointed to by USE_P is a name in OLD_SSA_NAMES or
1788    it is a symbol marked for renaming, replace it with USE_P's current
1789    reaching definition.  */
1790
1791 static inline void
1792 maybe_replace_use (use_operand_p use_p)
1793 {
1794   tree rdef = NULL_TREE;
1795   tree use = USE_FROM_PTR (use_p);
1796   tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1797
1798   if (symbol_marked_for_renaming (sym))
1799     rdef = get_reaching_def (sym);
1800   else if (is_old_name (use))
1801     rdef = get_reaching_def (use);
1802
1803   if (rdef && rdef != use)
1804     SET_USE (use_p, rdef);
1805 }
1806
1807
1808 /* Same as maybe_replace_use, but without introducing default stmts,
1809    returning false to indicate a need to do so.  */
1810
1811 static inline bool
1812 maybe_replace_use_in_debug_stmt (use_operand_p use_p)
1813 {
1814   tree rdef = NULL_TREE;
1815   tree use = USE_FROM_PTR (use_p);
1816   tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
1817
1818   if (symbol_marked_for_renaming (sym))
1819     rdef = get_current_def (sym);
1820   else if (is_old_name (use))
1821     {
1822       rdef = get_current_def (use);
1823       /* We can't assume that, if there's no current definition, the
1824          default one should be used.  It could be the case that we've
1825          rearranged blocks so that the earlier definition no longer
1826          dominates the use.  */
1827       if (!rdef && SSA_NAME_IS_DEFAULT_DEF (use))
1828         rdef = use;
1829     }
1830   else
1831     rdef = use;
1832
1833   if (rdef && rdef != use)
1834     SET_USE (use_p, rdef);
1835
1836   return rdef != NULL_TREE;
1837 }
1838
1839
1840 /* If the operand pointed to by DEF_P is an SSA name in NEW_SSA_NAMES
1841    or OLD_SSA_NAMES, or if it is a symbol marked for renaming,
1842    register it as the current definition for the names replaced by
1843    DEF_P.  */
1844
1845 static inline void
1846 maybe_register_def (def_operand_p def_p, gimple stmt,
1847                     gimple_stmt_iterator gsi)
1848 {
1849   tree def = DEF_FROM_PTR (def_p);
1850   tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
1851
1852   /* If DEF is a naked symbol that needs renaming, create a new
1853      name for it.  */
1854   if (symbol_marked_for_renaming (sym))
1855     {
1856       if (DECL_P (def))
1857         {
1858           tree tracked_var;
1859
1860           def = make_ssa_name (def, stmt);
1861           SET_DEF (def_p, def);
1862
1863           tracked_var = target_for_debug_bind (sym);
1864           if (tracked_var)
1865             {
1866               gimple note = gimple_build_debug_bind (tracked_var, def, stmt);
1867               /* If stmt ends the bb, insert the debug stmt on the single
1868                  non-EH edge from the stmt.  */
1869               if (gsi_one_before_end_p (gsi) && stmt_ends_bb_p (stmt))
1870                 {
1871                   basic_block bb = gsi_bb (gsi);
1872                   edge_iterator ei;
1873                   edge e, ef = NULL;
1874                   FOR_EACH_EDGE (e, ei, bb->succs)
1875                     if (!(e->flags & EDGE_EH))
1876                       {
1877                         gcc_assert (!ef);
1878                         ef = e;
1879                       }
1880                   gcc_assert (ef
1881                               && single_pred_p (ef->dest)
1882                               && !phi_nodes (ef->dest)
1883                               && ef->dest != EXIT_BLOCK_PTR);
1884                   gsi_insert_on_edge_immediate (ef, note);
1885                 }
1886               else
1887                 gsi_insert_after (&gsi, note, GSI_SAME_STMT);
1888             }
1889         }
1890
1891       register_new_update_single (def, sym);
1892     }
1893   else
1894     {
1895       /* If DEF is a new name, register it as a new definition
1896          for all the names replaced by DEF.  */
1897       if (is_new_name (def))
1898         register_new_update_set (def, names_replaced_by (def));
1899
1900       /* If DEF is an old name, register DEF as a new
1901          definition for itself.  */
1902       if (is_old_name (def))
1903         register_new_update_single (def, def);
1904     }
1905 }
1906
1907
1908 /* Update every variable used in the statement pointed-to by SI.  The
1909    statement is assumed to be in SSA form already.  Names in
1910    OLD_SSA_NAMES used by SI will be updated to their current reaching
1911    definition.  Names in OLD_SSA_NAMES or NEW_SSA_NAMES defined by SI
1912    will be registered as a new definition for their corresponding name
1913    in OLD_SSA_NAMES.  */
1914
1915 static void
1916 rewrite_update_stmt (gimple stmt, gimple_stmt_iterator gsi)
1917 {
1918   use_operand_p use_p;
1919   def_operand_p def_p;
1920   ssa_op_iter iter;
1921
1922   /* Only update marked statements.  */
1923   if (!rewrite_uses_p (stmt) && !register_defs_p (stmt))
1924     return;
1925
1926   if (dump_file && (dump_flags & TDF_DETAILS))
1927     {
1928       fprintf (dump_file, "Updating SSA information for statement ");
1929       print_gimple_stmt (dump_file, stmt, 0, TDF_SLIM);
1930       fprintf (dump_file, "\n");
1931     }
1932
1933   /* Rewrite USES included in OLD_SSA_NAMES and USES whose underlying
1934      symbol is marked for renaming.  */
1935   if (rewrite_uses_p (stmt))
1936     {
1937       if (is_gimple_debug (stmt))
1938         {
1939           bool failed = false;
1940
1941           FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
1942             if (!maybe_replace_use_in_debug_stmt (use_p))
1943               {
1944                 failed = true;
1945                 break;
1946               }
1947
1948           if (failed)
1949             {
1950               /* DOM sometimes threads jumps in such a way that a
1951                  debug stmt ends up referencing a SSA variable that no
1952                  longer dominates the debug stmt, but such that all
1953                  incoming definitions refer to the same definition in
1954                  an earlier dominator.  We could try to recover that
1955                  definition somehow, but this will have to do for now.
1956
1957                  Introducing a default definition, which is what
1958                  maybe_replace_use() would do in such cases, may
1959                  modify code generation, for the otherwise-unused
1960                  default definition would never go away, modifying SSA
1961                  version numbers all over.  */
1962               gimple_debug_bind_reset_value (stmt);
1963               update_stmt (stmt);
1964             }
1965         }
1966       else
1967         {
1968           FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1969             maybe_replace_use (use_p);
1970         }
1971     }
1972
1973   /* Register definitions of names in NEW_SSA_NAMES and OLD_SSA_NAMES.
1974      Also register definitions for names whose underlying symbol is
1975      marked for renaming.  */
1976   if (register_defs_p (stmt))
1977     FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1978       maybe_register_def (def_p, stmt, gsi);
1979 }
1980
1981
1982 /* Visit all the successor blocks of BB looking for PHI nodes.  For
1983    every PHI node found, check if any of its arguments is in
1984    OLD_SSA_NAMES.  If so, and if the argument has a current reaching
1985    definition, replace it.  */
1986
1987 static void
1988 rewrite_update_phi_arguments (basic_block bb)
1989 {
1990   edge e;
1991   edge_iterator ei;
1992   unsigned i;
1993
1994   FOR_EACH_EDGE (e, ei, bb->succs)
1995     {
1996       gimple phi;
1997       gimple_vec phis;
1998
1999       if (!bitmap_bit_p (blocks_with_phis_to_rewrite, e->dest->index))
2000         continue;
2001
2002       phis = VEC_index (gimple_vec, phis_to_rewrite, e->dest->index);
2003       for (i = 0; VEC_iterate (gimple, phis, i, phi); i++)
2004         {
2005           tree arg, lhs_sym, reaching_def = NULL;
2006           use_operand_p arg_p;
2007
2008           gcc_assert (rewrite_uses_p (phi));
2009
2010           arg_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
2011           arg = USE_FROM_PTR (arg_p);
2012
2013           if (arg && !DECL_P (arg) && TREE_CODE (arg) != SSA_NAME)
2014             continue;
2015
2016           lhs_sym = SSA_NAME_VAR (gimple_phi_result (phi));
2017
2018           if (arg == NULL_TREE)
2019             {
2020               /* When updating a PHI node for a recently introduced
2021                  symbol we may find NULL arguments.  That's why we
2022                  take the symbol from the LHS of the PHI node.  */
2023               reaching_def = get_reaching_def (lhs_sym);
2024
2025             }
2026           else
2027             {
2028               tree sym = DECL_P (arg) ? arg : SSA_NAME_VAR (arg);
2029
2030               if (symbol_marked_for_renaming (sym))
2031                 reaching_def = get_reaching_def (sym);
2032               else if (is_old_name (arg))
2033                 reaching_def = get_reaching_def (arg);
2034             }
2035
2036           /* Update the argument if there is a reaching def.  */
2037           if (reaching_def)
2038             {
2039               gimple stmt;
2040               source_location locus;
2041               int arg_i = PHI_ARG_INDEX_FROM_USE (arg_p);
2042
2043               SET_USE (arg_p, reaching_def);
2044               stmt = SSA_NAME_DEF_STMT (reaching_def);
2045
2046               /* Single element PHI nodes  behave like copies, so get the
2047                  location from the phi argument.  */
2048               if (gimple_code (stmt) == GIMPLE_PHI &&
2049                   gimple_phi_num_args (stmt) == 1)
2050                 locus = gimple_phi_arg_location (stmt, 0);
2051               else
2052                 locus = gimple_location (stmt);
2053
2054               gimple_phi_arg_set_location (phi, arg_i, locus);
2055             }
2056
2057
2058           if (e->flags & EDGE_ABNORMAL)
2059             SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (arg_p)) = 1;
2060         }
2061     }
2062 }
2063
2064
2065 /* Initialization of block data structures for the incremental SSA
2066    update pass.  Create a block local stack of reaching definitions
2067    for new SSA names produced in this block (BLOCK_DEFS).  Register
2068    new definitions for every PHI node in the block.  */
2069
2070 static void
2071 rewrite_update_enter_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2072                             basic_block bb)
2073 {
2074   edge e;
2075   edge_iterator ei;
2076   bool is_abnormal_phi;
2077   gimple_stmt_iterator gsi;
2078
2079   if (dump_file && (dump_flags & TDF_DETAILS))
2080     fprintf (dump_file, "\n\nRegistering new PHI nodes in block #%d\n\n",
2081              bb->index);
2082
2083   /* Mark the unwind point for this block.  */
2084   VEC_safe_push (tree, heap, block_defs_stack, NULL_TREE);
2085
2086   if (!bitmap_bit_p (blocks_to_update, bb->index))
2087     return;
2088
2089   /* Mark the LHS if any of the arguments flows through an abnormal
2090      edge.  */
2091   is_abnormal_phi = false;
2092   FOR_EACH_EDGE (e, ei, bb->preds)
2093     if (e->flags & EDGE_ABNORMAL)
2094       {
2095         is_abnormal_phi = true;
2096         break;
2097       }
2098
2099   /* If any of the PHI nodes is a replacement for a name in
2100      OLD_SSA_NAMES or it's one of the names in NEW_SSA_NAMES, then
2101      register it as a new definition for its corresponding name.  Also
2102      register definitions for names whose underlying symbols are
2103      marked for renaming.  */
2104   for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2105     {
2106       tree lhs, lhs_sym;
2107       gimple phi = gsi_stmt (gsi);
2108
2109       if (!register_defs_p (phi))
2110         continue;
2111
2112       lhs = gimple_phi_result (phi);
2113       lhs_sym = SSA_NAME_VAR (lhs);
2114
2115       if (symbol_marked_for_renaming (lhs_sym))
2116         register_new_update_single (lhs, lhs_sym);
2117       else
2118         {
2119
2120           /* If LHS is a new name, register a new definition for all
2121              the names replaced by LHS.  */
2122           if (is_new_name (lhs))
2123             register_new_update_set (lhs, names_replaced_by (lhs));
2124
2125           /* If LHS is an OLD name, register it as a new definition
2126              for itself.  */
2127           if (is_old_name (lhs))
2128             register_new_update_single (lhs, lhs);
2129         }
2130
2131       if (is_abnormal_phi)
2132         SSA_NAME_OCCURS_IN_ABNORMAL_PHI (lhs) = 1;
2133     }
2134
2135   /* Step 2.  Rewrite every variable used in each statement in the block.  */
2136   if (TEST_BIT (interesting_blocks, bb->index))
2137     {
2138       gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
2139       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2140         rewrite_update_stmt (gsi_stmt (gsi), gsi);
2141     }
2142
2143   /* Step 3.  Update PHI nodes.  */
2144   rewrite_update_phi_arguments (bb);
2145 }
2146
2147 /* Called after visiting block BB.  Unwind BLOCK_DEFS_STACK to restore
2148    the current reaching definition of every name re-written in BB to
2149    the original reaching definition before visiting BB.  This
2150    unwinding must be done in the opposite order to what is done in
2151    register_new_update_set.  */
2152
2153 static void
2154 rewrite_update_leave_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
2155                             basic_block bb ATTRIBUTE_UNUSED)
2156 {
2157   while (VEC_length (tree, block_defs_stack) > 0)
2158     {
2159       tree var = VEC_pop (tree, block_defs_stack);
2160       tree saved_def;
2161
2162       /* NULL indicates the unwind stop point for this block (see
2163          rewrite_update_enter_block).  */
2164       if (var == NULL)
2165         return;
2166
2167       saved_def = VEC_pop (tree, block_defs_stack);
2168       set_current_def (var, saved_def);
2169     }
2170 }
2171
2172
2173 /* Rewrite the actual blocks, statements, and PHI arguments, to be in SSA
2174    form.
2175
2176    ENTRY indicates the block where to start.  Every block dominated by
2177       ENTRY will be rewritten.
2178
2179    WHAT indicates what actions will be taken by the renamer (see enum
2180       rewrite_mode).
2181
2182    BLOCKS are the set of interesting blocks for the dominator walker
2183       to process.  If this set is NULL, then all the nodes dominated
2184       by ENTRY are walked.  Otherwise, blocks dominated by ENTRY that
2185       are not present in BLOCKS are ignored.  */
2186
2187 static void
2188 rewrite_blocks (basic_block entry, enum rewrite_mode what)
2189 {
2190   struct dom_walk_data walk_data;
2191
2192   /* Rewrite all the basic blocks in the program.  */
2193   timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
2194
2195   /* Setup callbacks for the generic dominator tree walker.  */
2196   memset (&walk_data, 0, sizeof (walk_data));
2197
2198   walk_data.dom_direction = CDI_DOMINATORS;
2199
2200   if (what == REWRITE_ALL)
2201     {
2202       walk_data.before_dom_children = rewrite_enter_block;
2203       walk_data.after_dom_children = rewrite_leave_block;
2204     }
2205   else if (what == REWRITE_UPDATE)
2206     {
2207       walk_data.before_dom_children = rewrite_update_enter_block;
2208       walk_data.after_dom_children = rewrite_update_leave_block;
2209     }
2210   else
2211     gcc_unreachable ();
2212
2213   block_defs_stack = VEC_alloc (tree, heap, 10);
2214
2215   /* Initialize the dominator walker.  */
2216   init_walk_dominator_tree (&walk_data);
2217
2218   /* Recursively walk the dominator tree rewriting each statement in
2219      each basic block.  */
2220   walk_dominator_tree (&walk_data, entry);
2221
2222   /* Finalize the dominator walker.  */
2223   fini_walk_dominator_tree (&walk_data);
2224
2225   /* Debugging dumps.  */
2226   if (dump_file && (dump_flags & TDF_STATS))
2227     {
2228       dump_dfa_stats (dump_file);
2229       if (def_blocks)
2230         dump_tree_ssa_stats (dump_file);
2231     }
2232
2233   VEC_free (tree, heap, block_defs_stack);
2234
2235   timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
2236 }
2237
2238
2239 /* Block processing routine for mark_def_sites.  Clear the KILLS bitmap
2240    at the start of each block, and call mark_def_sites for each statement.  */
2241
2242 static void
2243 mark_def_sites_block (struct dom_walk_data *walk_data, basic_block bb)
2244 {
2245   struct mark_def_sites_global_data *gd;
2246   bitmap kills;
2247   gimple_stmt_iterator gsi;
2248
2249   gd = (struct mark_def_sites_global_data *) walk_data->global_data;
2250   kills = gd->kills;
2251
2252   bitmap_clear (kills);
2253   for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
2254     mark_def_sites (bb, gsi_stmt (gsi), kills);
2255 }
2256
2257
2258 /* Mark the definition site blocks for each variable, so that we know
2259    where the variable is actually live.
2260
2261    The INTERESTING_BLOCKS global will be filled in with all the blocks
2262    that should be processed by the renamer.  It is assumed that the
2263    caller has already initialized and zeroed it.  */
2264
2265 static void
2266 mark_def_site_blocks (void)
2267 {
2268   struct dom_walk_data walk_data;
2269   struct mark_def_sites_global_data mark_def_sites_global_data;
2270
2271   /* Setup callbacks for the generic dominator tree walker to find and
2272      mark definition sites.  */
2273   walk_data.dom_direction = CDI_DOMINATORS;
2274   walk_data.initialize_block_local_data = NULL;
2275   walk_data.before_dom_children = mark_def_sites_block;
2276   walk_data.after_dom_children = NULL;
2277
2278   /* Notice that this bitmap is indexed using variable UIDs, so it must be
2279      large enough to accommodate all the variables referenced in the
2280      function, not just the ones we are renaming.  */
2281   mark_def_sites_global_data.kills = BITMAP_ALLOC (NULL);
2282   walk_data.global_data = &mark_def_sites_global_data;
2283
2284   /* We do not have any local data.  */
2285   walk_data.block_local_data_size = 0;
2286
2287   /* Initialize the dominator walker.  */
2288   init_walk_dominator_tree (&walk_data);
2289
2290   /* Recursively walk the dominator tree.  */
2291   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
2292
2293   /* Finalize the dominator walker.  */
2294   fini_walk_dominator_tree (&walk_data);
2295
2296   /* We no longer need this bitmap, clear and free it.  */
2297   BITMAP_FREE (mark_def_sites_global_data.kills);
2298 }
2299
2300
2301 /* Initialize internal data needed during renaming.  */
2302
2303 static void
2304 init_ssa_renamer (void)
2305 {
2306   tree var;
2307   referenced_var_iterator rvi;
2308
2309   cfun->gimple_df->in_ssa_p = false;
2310
2311   /* Allocate memory for the DEF_BLOCKS hash table.  */
2312   gcc_assert (def_blocks == NULL);
2313   def_blocks = htab_create (num_referenced_vars, def_blocks_hash,
2314                             def_blocks_eq, def_blocks_free);
2315
2316   FOR_EACH_REFERENCED_VAR(var, rvi)
2317     set_current_def (var, NULL_TREE);
2318 }
2319
2320
2321 /* Deallocate internal data structures used by the renamer.  */
2322
2323 static void
2324 fini_ssa_renamer (void)
2325 {
2326   if (def_blocks)
2327     {
2328       htab_delete (def_blocks);
2329       def_blocks = NULL;
2330     }
2331
2332   cfun->gimple_df->in_ssa_p = true;
2333 }
2334
2335 /* Main entry point into the SSA builder.  The renaming process
2336    proceeds in four main phases:
2337
2338    1- Compute dominance frontier and immediate dominators, needed to
2339       insert PHI nodes and rename the function in dominator tree
2340       order.
2341
2342    2- Find and mark all the blocks that define variables
2343       (mark_def_site_blocks).
2344
2345    3- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
2346
2347    4- Rename all the blocks (rewrite_blocks) and statements in the program.
2348
2349    Steps 3 and 4 are done using the dominator tree walker
2350    (walk_dominator_tree).  */
2351
2352 static unsigned int
2353 rewrite_into_ssa (void)
2354 {
2355   bitmap *dfs;
2356   basic_block bb;
2357
2358   timevar_push (TV_TREE_SSA_OTHER);
2359
2360   /* Initialize operand data structures.  */
2361   init_ssa_operands ();
2362
2363   /* Initialize internal data needed by the renamer.  */
2364   init_ssa_renamer ();
2365
2366   /* Initialize the set of interesting blocks.  The callback
2367      mark_def_sites will add to this set those blocks that the renamer
2368      should process.  */
2369   interesting_blocks = sbitmap_alloc (last_basic_block);
2370   sbitmap_zero (interesting_blocks);
2371
2372   /* Initialize dominance frontier.  */
2373   dfs = XNEWVEC (bitmap, last_basic_block);
2374   FOR_EACH_BB (bb)
2375     dfs[bb->index] = BITMAP_ALLOC (NULL);
2376
2377   /* 1- Compute dominance frontiers.  */
2378   calculate_dominance_info (CDI_DOMINATORS);
2379   compute_dominance_frontiers (dfs);
2380
2381   /* 2- Find and mark definition sites.  */
2382   mark_def_site_blocks ();
2383
2384   /* 3- Insert PHI nodes at dominance frontiers of definition blocks.  */
2385   insert_phi_nodes (dfs);
2386
2387   /* 4- Rename all the blocks.  */
2388   rewrite_blocks (ENTRY_BLOCK_PTR, REWRITE_ALL);
2389
2390   /* Free allocated memory.  */
2391   FOR_EACH_BB (bb)
2392     BITMAP_FREE (dfs[bb->index]);
2393   free (dfs);
2394
2395   sbitmap_free (interesting_blocks);
2396
2397   fini_ssa_renamer ();
2398
2399   timevar_pop (TV_TREE_SSA_OTHER);
2400   return 0;
2401 }
2402
2403
2404 struct gimple_opt_pass pass_build_ssa =
2405 {
2406  {
2407   GIMPLE_PASS,
2408   "ssa",                                /* name */
2409   NULL,                                 /* gate */
2410   rewrite_into_ssa,                     /* execute */
2411   NULL,                                 /* sub */
2412   NULL,                                 /* next */
2413   0,                                    /* static_pass_number */
2414   TV_NONE,                              /* tv_id */
2415   PROP_cfg | PROP_referenced_vars,      /* properties_required */
2416   PROP_ssa,                             /* properties_provided */
2417   0,                                    /* properties_destroyed */
2418   0,                                    /* todo_flags_start */
2419   TODO_dump_func
2420     | TODO_update_ssa_only_virtuals
2421     | TODO_verify_ssa
2422     | TODO_remove_unused_locals         /* todo_flags_finish */
2423  }
2424 };
2425
2426
2427 /* Mark the definition of VAR at STMT and BB as interesting for the
2428    renamer.  BLOCKS is the set of blocks that need updating.  */
2429
2430 static void
2431 mark_def_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
2432 {
2433   gcc_assert (bitmap_bit_p (blocks_to_update, bb->index));
2434   set_register_defs (stmt, true);
2435
2436   if (insert_phi_p)
2437     {
2438       bool is_phi_p = gimple_code (stmt) == GIMPLE_PHI;
2439
2440       set_def_block (var, bb, is_phi_p);
2441
2442       /* If VAR is an SSA name in NEW_SSA_NAMES, this is a definition
2443          site for both itself and all the old names replaced by it.  */
2444       if (TREE_CODE (var) == SSA_NAME && is_new_name (var))
2445         {
2446           bitmap_iterator bi;
2447           unsigned i;
2448           bitmap set = names_replaced_by (var);
2449           if (set)
2450             EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2451               set_def_block (ssa_name (i), bb, is_phi_p);
2452         }
2453     }
2454 }
2455
2456
2457 /* Mark the use of VAR at STMT and BB as interesting for the
2458    renamer.  INSERT_PHI_P is true if we are going to insert new PHI
2459    nodes.  */
2460
2461 static inline void
2462 mark_use_interesting (tree var, gimple stmt, basic_block bb, bool insert_phi_p)
2463 {
2464   basic_block def_bb = gimple_bb (stmt);
2465
2466   mark_block_for_update (def_bb);
2467   mark_block_for_update (bb);
2468
2469   if (gimple_code (stmt) == GIMPLE_PHI)
2470     mark_phi_for_rewrite (def_bb, stmt);
2471   else
2472     {
2473       set_rewrite_uses (stmt, true);
2474
2475       if (is_gimple_debug (stmt))
2476         return;
2477     }
2478
2479   /* If VAR has not been defined in BB, then it is live-on-entry
2480      to BB.  Note that we cannot just use the block holding VAR's
2481      definition because if VAR is one of the names in OLD_SSA_NAMES,
2482      it will have several definitions (itself and all the names that
2483      replace it).  */
2484   if (insert_phi_p)
2485     {
2486       struct def_blocks_d *db_p = get_def_blocks_for (var);
2487       if (!bitmap_bit_p (db_p->def_blocks, bb->index))
2488         set_livein_block (var, bb);
2489     }
2490 }
2491
2492
2493 /* Do a dominator walk starting at BB processing statements that
2494    reference symbols in SYMS_TO_RENAME.  This is very similar to
2495    mark_def_sites, but the scan handles statements whose operands may
2496    already be SSA names.
2497
2498    If INSERT_PHI_P is true, mark those uses as live in the
2499    corresponding block.  This is later used by the PHI placement
2500    algorithm to make PHI pruning decisions.
2501
2502    FIXME.  Most of this would be unnecessary if we could associate a
2503            symbol to all the SSA names that reference it.  But that
2504            sounds like it would be expensive to maintain.  Still, it
2505            would be interesting to see if it makes better sense to do
2506            that.  */
2507
2508 static void
2509 prepare_block_for_update (basic_block bb, bool insert_phi_p)
2510 {
2511   basic_block son;
2512   gimple_stmt_iterator si;
2513   edge e;
2514   edge_iterator ei;
2515
2516   mark_block_for_update (bb);
2517
2518   /* Process PHI nodes marking interesting those that define or use
2519      the symbols that we are interested in.  */
2520   for (si = gsi_start_phis (bb); !gsi_end_p (si); gsi_next (&si))
2521     {
2522       gimple phi = gsi_stmt (si);
2523       tree lhs_sym, lhs = gimple_phi_result (phi);
2524
2525       lhs_sym = DECL_P (lhs) ? lhs : SSA_NAME_VAR (lhs);
2526
2527       if (!symbol_marked_for_renaming (lhs_sym))
2528         continue;
2529
2530       mark_def_interesting (lhs_sym, phi, bb, insert_phi_p);
2531
2532       /* Mark the uses in phi nodes as interesting.  It would be more correct
2533          to process the arguments of the phi nodes of the successor edges of
2534          BB at the end of prepare_block_for_update, however, that turns out
2535          to be significantly more expensive.  Doing it here is conservatively
2536          correct -- it may only cause us to believe a value to be live in a
2537          block that also contains its definition, and thus insert a few more
2538          phi nodes for it.  */
2539       FOR_EACH_EDGE (e, ei, bb->preds)
2540         mark_use_interesting (lhs_sym, phi, e->src, insert_phi_p);
2541     }
2542
2543   /* Process the statements.  */
2544   for (si = gsi_start_bb (bb); !gsi_end_p (si); gsi_next (&si))
2545     {
2546       gimple stmt;
2547       ssa_op_iter i;
2548       use_operand_p use_p;
2549       def_operand_p def_p;
2550
2551       stmt = gsi_stmt (si);
2552
2553       FOR_EACH_SSA_USE_OPERAND (use_p, stmt, i, SSA_OP_ALL_USES)
2554         {
2555           tree use = USE_FROM_PTR (use_p);
2556           tree sym = DECL_P (use) ? use : SSA_NAME_VAR (use);
2557           if (symbol_marked_for_renaming (sym))
2558             mark_use_interesting (sym, stmt, bb, insert_phi_p);
2559         }
2560
2561       FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, i, SSA_OP_ALL_DEFS)
2562         {
2563           tree def = DEF_FROM_PTR (def_p);
2564           tree sym = DECL_P (def) ? def : SSA_NAME_VAR (def);
2565           if (symbol_marked_for_renaming (sym))
2566             mark_def_interesting (sym, stmt, bb, insert_phi_p);
2567         }
2568     }
2569
2570   /* Now visit all the blocks dominated by BB.  */
2571   for (son = first_dom_son (CDI_DOMINATORS, bb);
2572        son;
2573        son = next_dom_son (CDI_DOMINATORS, son))
2574     prepare_block_for_update (son, insert_phi_p);
2575 }
2576
2577
2578 /* Helper for prepare_names_to_update.  Mark all the use sites for
2579    NAME as interesting.  BLOCKS and INSERT_PHI_P are as in
2580    prepare_names_to_update.  */
2581
2582 static void
2583 prepare_use_sites_for (tree name, bool insert_phi_p)
2584 {
2585   use_operand_p use_p;
2586   imm_use_iterator iter;
2587
2588   FOR_EACH_IMM_USE_FAST (use_p, iter, name)
2589     {
2590       gimple stmt = USE_STMT (use_p);
2591       basic_block bb = gimple_bb (stmt);
2592
2593       if (gimple_code (stmt) == GIMPLE_PHI)
2594         {
2595           int ix = PHI_ARG_INDEX_FROM_USE (use_p);
2596           edge e = gimple_phi_arg_edge (stmt, ix);
2597           mark_use_interesting (name, stmt, e->src, insert_phi_p);
2598         }
2599       else
2600         {
2601           /* For regular statements, mark this as an interesting use
2602              for NAME.  */
2603           mark_use_interesting (name, stmt, bb, insert_phi_p);
2604         }
2605     }
2606 }
2607
2608
2609 /* Helper for prepare_names_to_update.  Mark the definition site for
2610    NAME as interesting.  BLOCKS and INSERT_PHI_P are as in
2611    prepare_names_to_update.  */
2612
2613 static void
2614 prepare_def_site_for (tree name, bool insert_phi_p)
2615 {
2616   gimple stmt;
2617   basic_block bb;
2618
2619   gcc_assert (names_to_release == NULL
2620               || !bitmap_bit_p (names_to_release, SSA_NAME_VERSION (name)));
2621
2622   stmt = SSA_NAME_DEF_STMT (name);
2623   bb = gimple_bb (stmt);
2624   if (bb)
2625     {
2626       gcc_assert (bb->index < last_basic_block);
2627       mark_block_for_update (bb);
2628       mark_def_interesting (name, stmt, bb, insert_phi_p);
2629     }
2630 }
2631
2632
2633 /* Mark definition and use sites of names in NEW_SSA_NAMES and
2634    OLD_SSA_NAMES.  INSERT_PHI_P is true if the caller wants to insert
2635    PHI nodes for newly created names.  */
2636
2637 static void
2638 prepare_names_to_update (bool insert_phi_p)
2639 {
2640   unsigned i = 0;
2641   bitmap_iterator bi;
2642   sbitmap_iterator sbi;
2643
2644   /* If a name N from NEW_SSA_NAMES is also marked to be released,
2645      remove it from NEW_SSA_NAMES so that we don't try to visit its
2646      defining basic block (which most likely doesn't exist).  Notice
2647      that we cannot do the same with names in OLD_SSA_NAMES because we
2648      want to replace existing instances.  */
2649   if (names_to_release)
2650     EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2651       RESET_BIT (new_ssa_names, i);
2652
2653   /* First process names in NEW_SSA_NAMES.  Otherwise, uses of old
2654      names may be considered to be live-in on blocks that contain
2655      definitions for their replacements.  */
2656   EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2657     prepare_def_site_for (ssa_name (i), insert_phi_p);
2658
2659   /* If an old name is in NAMES_TO_RELEASE, we cannot remove it from
2660      OLD_SSA_NAMES, but we have to ignore its definition site.  */
2661   EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2662     {
2663       if (names_to_release == NULL || !bitmap_bit_p (names_to_release, i))
2664         prepare_def_site_for (ssa_name (i), insert_phi_p);
2665       prepare_use_sites_for (ssa_name (i), insert_phi_p);
2666     }
2667 }
2668
2669
2670 /* Dump all the names replaced by NAME to FILE.  */
2671
2672 void
2673 dump_names_replaced_by (FILE *file, tree name)
2674 {
2675   unsigned i;
2676   bitmap old_set;
2677   bitmap_iterator bi;
2678
2679   print_generic_expr (file, name, 0);
2680   fprintf (file, " -> { ");
2681
2682   old_set = names_replaced_by (name);
2683   EXECUTE_IF_SET_IN_BITMAP (old_set, 0, i, bi)
2684     {
2685       print_generic_expr (file, ssa_name (i), 0);
2686       fprintf (file, " ");
2687     }
2688
2689   fprintf (file, "}\n");
2690 }
2691
2692
2693 /* Dump all the names replaced by NAME to stderr.  */
2694
2695 void
2696 debug_names_replaced_by (tree name)
2697 {
2698   dump_names_replaced_by (stderr, name);
2699 }
2700
2701
2702 /* Dump SSA update information to FILE.  */
2703
2704 void
2705 dump_update_ssa (FILE *file)
2706 {
2707   unsigned i = 0;
2708   bitmap_iterator bi;
2709
2710   if (!need_ssa_update_p (cfun))
2711     return;
2712
2713   if (new_ssa_names && sbitmap_first_set_bit (new_ssa_names) >= 0)
2714     {
2715       sbitmap_iterator sbi;
2716
2717       fprintf (file, "\nSSA replacement table\n");
2718       fprintf (file, "N_i -> { O_1 ... O_j } means that N_i replaces "
2719                      "O_1, ..., O_j\n\n");
2720
2721       EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
2722         dump_names_replaced_by (file, ssa_name (i));
2723
2724       fprintf (file, "\n");
2725       fprintf (file, "Number of virtual NEW -> OLD mappings: %7u\n",
2726                update_ssa_stats.num_virtual_mappings);
2727       fprintf (file, "Number of real NEW -> OLD mappings:    %7u\n",
2728                update_ssa_stats.num_total_mappings
2729                - update_ssa_stats.num_virtual_mappings);
2730       fprintf (file, "Number of total NEW -> OLD mappings:   %7u\n",
2731                update_ssa_stats.num_total_mappings);
2732
2733       fprintf (file, "\nNumber of virtual symbols: %u\n",
2734                update_ssa_stats.num_virtual_symbols);
2735     }
2736
2737   if (!bitmap_empty_p (SYMS_TO_RENAME (cfun)))
2738     {
2739       fprintf (file, "\n\nSymbols to be put in SSA form\n\n");
2740       dump_decl_set (file, SYMS_TO_RENAME (cfun));
2741       fprintf (file, "\n");
2742     }
2743
2744   if (names_to_release && !bitmap_empty_p (names_to_release))
2745     {
2746       fprintf (file, "\n\nSSA names to release after updating the SSA web\n\n");
2747       EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2748         {
2749           print_generic_expr (file, ssa_name (i), 0);
2750           fprintf (file, " ");
2751         }
2752     }
2753
2754   fprintf (file, "\n\n");
2755 }
2756
2757
2758 /* Dump SSA update information to stderr.  */
2759
2760 void
2761 debug_update_ssa (void)
2762 {
2763   dump_update_ssa (stderr);
2764 }
2765
2766
2767 /* Initialize data structures used for incremental SSA updates.  */
2768
2769 static void
2770 init_update_ssa (struct function *fn)
2771 {
2772   /* Reserve more space than the current number of names.  The calls to
2773      add_new_name_mapping are typically done after creating new SSA
2774      names, so we'll need to reallocate these arrays.  */
2775   old_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2776   sbitmap_zero (old_ssa_names);
2777
2778   new_ssa_names = sbitmap_alloc (num_ssa_names + NAME_SETS_GROWTH_FACTOR);
2779   sbitmap_zero (new_ssa_names);
2780
2781   repl_tbl = htab_create (20, repl_map_hash, repl_map_eq, repl_map_free);
2782   names_to_release = NULL;
2783   memset (&update_ssa_stats, 0, sizeof (update_ssa_stats));
2784   update_ssa_stats.virtual_symbols = BITMAP_ALLOC (NULL);
2785   update_ssa_initialized_fn = fn;
2786 }
2787
2788
2789 /* Deallocate data structures used for incremental SSA updates.  */
2790
2791 void
2792 delete_update_ssa (void)
2793 {
2794   unsigned i;
2795   bitmap_iterator bi;
2796
2797   sbitmap_free (old_ssa_names);
2798   old_ssa_names = NULL;
2799
2800   sbitmap_free (new_ssa_names);
2801   new_ssa_names = NULL;
2802
2803   htab_delete (repl_tbl);
2804   repl_tbl = NULL;
2805
2806   bitmap_clear (SYMS_TO_RENAME (update_ssa_initialized_fn));
2807   BITMAP_FREE (update_ssa_stats.virtual_symbols);
2808
2809   if (names_to_release)
2810     {
2811       EXECUTE_IF_SET_IN_BITMAP (names_to_release, 0, i, bi)
2812         release_ssa_name (ssa_name (i));
2813       BITMAP_FREE (names_to_release);
2814     }
2815
2816   clear_ssa_name_info ();
2817
2818   fini_ssa_renamer ();
2819
2820   if (blocks_with_phis_to_rewrite)
2821     EXECUTE_IF_SET_IN_BITMAP (blocks_with_phis_to_rewrite, 0, i, bi)
2822       {
2823         gimple_vec phis = VEC_index (gimple_vec, phis_to_rewrite, i);
2824
2825         VEC_free (gimple, heap, phis);
2826         VEC_replace (gimple_vec, phis_to_rewrite, i, NULL);
2827       }
2828
2829   BITMAP_FREE (blocks_with_phis_to_rewrite);
2830   BITMAP_FREE (blocks_to_update);
2831   update_ssa_initialized_fn = NULL;
2832 }
2833
2834
2835 /* Create a new name for OLD_NAME in statement STMT and replace the
2836    operand pointed to by DEF_P with the newly created name.  Return
2837    the new name and register the replacement mapping <NEW, OLD> in
2838    update_ssa's tables.  */
2839
2840 tree
2841 create_new_def_for (tree old_name, gimple stmt, def_operand_p def)
2842 {
2843   tree new_name = duplicate_ssa_name (old_name, stmt);
2844
2845   SET_DEF (def, new_name);
2846
2847   if (gimple_code (stmt) == GIMPLE_PHI)
2848     {
2849       edge e;
2850       edge_iterator ei;
2851       basic_block bb = gimple_bb (stmt);
2852
2853       /* If needed, mark NEW_NAME as occurring in an abnormal PHI node. */
2854       FOR_EACH_EDGE (e, ei, bb->preds)
2855         if (e->flags & EDGE_ABNORMAL)
2856           {
2857             SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
2858             break;
2859           }
2860     }
2861
2862   register_new_name_mapping (new_name, old_name);
2863
2864   /* For the benefit of passes that will be updating the SSA form on
2865      their own, set the current reaching definition of OLD_NAME to be
2866      NEW_NAME.  */
2867   set_current_def (old_name, new_name);
2868
2869   return new_name;
2870 }
2871
2872
2873 /* Register name NEW to be a replacement for name OLD.  This function
2874    must be called for every replacement that should be performed by
2875    update_ssa.  */
2876
2877 void
2878 register_new_name_mapping (tree new_tree, tree old)
2879 {
2880   if (!update_ssa_initialized_fn)
2881     init_update_ssa (cfun);
2882
2883   gcc_assert (update_ssa_initialized_fn == cfun);
2884
2885   add_new_name_mapping (new_tree, old);
2886 }
2887
2888
2889 /* Register symbol SYM to be renamed by update_ssa.  */
2890
2891 void
2892 mark_sym_for_renaming (tree sym)
2893 {
2894   bitmap_set_bit (SYMS_TO_RENAME (cfun), DECL_UID (sym));
2895 }
2896
2897
2898 /* Register all the symbols in SET to be renamed by update_ssa.  */
2899
2900 void
2901 mark_set_for_renaming (bitmap set)
2902 {
2903   bitmap_iterator bi;
2904   unsigned i;
2905
2906   if (set == NULL || bitmap_empty_p (set))
2907     return;
2908
2909   EXECUTE_IF_SET_IN_BITMAP (set, 0, i, bi)
2910     mark_sym_for_renaming (referenced_var (i));
2911 }
2912
2913
2914 /* Return true if there is any work to be done by update_ssa
2915    for function FN.  */
2916
2917 bool
2918 need_ssa_update_p (struct function *fn)
2919 {
2920   gcc_assert (fn != NULL);
2921   return (update_ssa_initialized_fn == fn
2922           || (fn->gimple_df
2923               && !bitmap_empty_p (SYMS_TO_RENAME (fn))));
2924 }
2925
2926 /* Return true if SSA name mappings have been registered for SSA updating.  */
2927
2928 bool
2929 name_mappings_registered_p (void)
2930 {
2931   if (!update_ssa_initialized_fn)
2932     return false;
2933
2934   gcc_assert (update_ssa_initialized_fn == cfun);
2935
2936   return repl_tbl && htab_elements (repl_tbl) > 0;
2937 }
2938
2939 /* Return true if name N has been registered in the replacement table.  */
2940
2941 bool
2942 name_registered_for_update_p (tree n ATTRIBUTE_UNUSED)
2943 {
2944   if (!update_ssa_initialized_fn)
2945     return false;
2946
2947   gcc_assert (update_ssa_initialized_fn == cfun);
2948
2949   return is_new_name (n) || is_old_name (n);
2950 }
2951
2952
2953 /* Return the set of all the SSA names marked to be replaced.  */
2954
2955 bitmap
2956 ssa_names_to_replace (void)
2957 {
2958   unsigned i = 0;
2959   bitmap ret;
2960   sbitmap_iterator sbi;
2961
2962   gcc_assert (update_ssa_initialized_fn == NULL
2963               || update_ssa_initialized_fn == cfun);
2964
2965   ret = BITMAP_ALLOC (NULL);
2966   EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
2967     bitmap_set_bit (ret, i);
2968
2969   return ret;
2970 }
2971
2972
2973 /* Mark NAME to be released after update_ssa has finished.  */
2974
2975 void
2976 release_ssa_name_after_update_ssa (tree name)
2977 {
2978   gcc_assert (cfun && update_ssa_initialized_fn == cfun);
2979
2980   if (names_to_release == NULL)
2981     names_to_release = BITMAP_ALLOC (NULL);
2982
2983   bitmap_set_bit (names_to_release, SSA_NAME_VERSION (name));
2984 }
2985
2986
2987 /* Insert new PHI nodes to replace VAR.  DFS contains dominance
2988    frontier information.  BLOCKS is the set of blocks to be updated.
2989
2990    This is slightly different than the regular PHI insertion
2991    algorithm.  The value of UPDATE_FLAGS controls how PHI nodes for
2992    real names (i.e., GIMPLE registers) are inserted:
2993
2994    - If UPDATE_FLAGS == TODO_update_ssa, we are only interested in PHI
2995      nodes inside the region affected by the block that defines VAR
2996      and the blocks that define all its replacements.  All these
2997      definition blocks are stored in DEF_BLOCKS[VAR]->DEF_BLOCKS.
2998
2999      First, we compute the entry point to the region (ENTRY).  This is
3000      given by the nearest common dominator to all the definition
3001      blocks. When computing the iterated dominance frontier (IDF), any
3002      block not strictly dominated by ENTRY is ignored.
3003
3004      We then call the standard PHI insertion algorithm with the pruned
3005      IDF.
3006
3007    - If UPDATE_FLAGS == TODO_update_ssa_full_phi, the IDF for real
3008      names is not pruned.  PHI nodes are inserted at every IDF block.  */
3009
3010 static void
3011 insert_updated_phi_nodes_for (tree var, bitmap *dfs, bitmap blocks,
3012                               unsigned update_flags)
3013 {
3014   basic_block entry;
3015   struct def_blocks_d *db;
3016   bitmap idf, pruned_idf;
3017   bitmap_iterator bi;
3018   unsigned i;
3019
3020 #if defined ENABLE_CHECKING
3021   if (TREE_CODE (var) == SSA_NAME)
3022     gcc_assert (is_old_name (var));
3023   else
3024     gcc_assert (symbol_marked_for_renaming (var));
3025 #endif
3026
3027   /* Get all the definition sites for VAR.  */
3028   db = find_def_blocks_for (var);
3029
3030   /* No need to do anything if there were no definitions to VAR.  */
3031   if (db == NULL || bitmap_empty_p (db->def_blocks))
3032     return;
3033
3034   /* Compute the initial iterated dominance frontier.  */
3035   idf = compute_idf (db->def_blocks, dfs);
3036   pruned_idf = BITMAP_ALLOC (NULL);
3037
3038   if (TREE_CODE (var) == SSA_NAME)
3039     {
3040       if (update_flags == TODO_update_ssa)
3041         {
3042           /* If doing regular SSA updates for GIMPLE registers, we are
3043              only interested in IDF blocks dominated by the nearest
3044              common dominator of all the definition blocks.  */
3045           entry = nearest_common_dominator_for_set (CDI_DOMINATORS,
3046                                                     db->def_blocks);
3047           if (entry != ENTRY_BLOCK_PTR)
3048             EXECUTE_IF_SET_IN_BITMAP (idf, 0, i, bi)
3049               if (BASIC_BLOCK (i) != entry
3050                   && dominated_by_p (CDI_DOMINATORS, BASIC_BLOCK (i), entry))
3051                 bitmap_set_bit (pruned_idf, i);
3052         }
3053       else
3054         {
3055           /* Otherwise, do not prune the IDF for VAR.  */
3056           gcc_assert (update_flags == TODO_update_ssa_full_phi);
3057           bitmap_copy (pruned_idf, idf);
3058         }
3059     }
3060   else
3061     {
3062       /* Otherwise, VAR is a symbol that needs to be put into SSA form
3063          for the first time, so we need to compute the full IDF for
3064          it.  */
3065       bitmap_copy (pruned_idf, idf);
3066     }
3067
3068   if (!bitmap_empty_p (pruned_idf))
3069     {
3070       /* Make sure that PRUNED_IDF blocks and all their feeding blocks
3071          are included in the region to be updated.  The feeding blocks
3072          are important to guarantee that the PHI arguments are renamed
3073          properly.  */
3074
3075       /* FIXME, this is not needed if we are updating symbols.  We are
3076          already starting at the ENTRY block anyway.  */
3077       bitmap_ior_into (blocks, pruned_idf);
3078       EXECUTE_IF_SET_IN_BITMAP (pruned_idf, 0, i, bi)
3079         {
3080           edge e;
3081           edge_iterator ei;
3082           basic_block bb = BASIC_BLOCK (i);
3083
3084           FOR_EACH_EDGE (e, ei, bb->preds)
3085             if (e->src->index >= 0)
3086               bitmap_set_bit (blocks, e->src->index);
3087         }
3088
3089       insert_phi_nodes_for (var, pruned_idf, true);
3090     }
3091
3092   BITMAP_FREE (pruned_idf);
3093   BITMAP_FREE (idf);
3094 }
3095
3096
3097 /* Heuristic to determine whether SSA name mappings for virtual names
3098    should be discarded and their symbols rewritten from scratch.  When
3099    there is a large number of mappings for virtual names, the
3100    insertion of PHI nodes for the old names in the mappings takes
3101    considerable more time than if we inserted PHI nodes for the
3102    symbols instead.
3103
3104    Currently the heuristic takes these stats into account:
3105
3106         - Number of mappings for virtual SSA names.
3107         - Number of distinct virtual symbols involved in those mappings.
3108
3109    If the number of virtual mappings is much larger than the number of
3110    virtual symbols, then it will be faster to compute PHI insertion
3111    spots for the symbols.  Even if this involves traversing the whole
3112    CFG, which is what happens when symbols are renamed from scratch.  */
3113
3114 static bool
3115 switch_virtuals_to_full_rewrite_p (void)
3116 {
3117   if (update_ssa_stats.num_virtual_mappings < (unsigned) MIN_VIRTUAL_MAPPINGS)
3118     return false;
3119
3120   if (update_ssa_stats.num_virtual_mappings
3121       > (unsigned) VIRTUAL_MAPPINGS_TO_SYMS_RATIO
3122         * update_ssa_stats.num_virtual_symbols)
3123     return true;
3124
3125   return false;
3126 }
3127
3128
3129 /* Remove every virtual mapping and mark all the affected virtual
3130    symbols for renaming.  */
3131
3132 static void
3133 switch_virtuals_to_full_rewrite (void)
3134 {
3135   unsigned i = 0;
3136   sbitmap_iterator sbi;
3137
3138   if (dump_file)
3139     {
3140       fprintf (dump_file, "\nEnabled virtual name mapping heuristic.\n");
3141       fprintf (dump_file, "\tNumber of virtual mappings:       %7u\n",
3142                update_ssa_stats.num_virtual_mappings);
3143       fprintf (dump_file, "\tNumber of unique virtual symbols: %7u\n",
3144                update_ssa_stats.num_virtual_symbols);
3145       fprintf (dump_file, "Updating FUD-chains from top of CFG will be "
3146                           "faster than processing\nthe name mappings.\n\n");
3147     }
3148
3149   /* Remove all virtual names from NEW_SSA_NAMES and OLD_SSA_NAMES.
3150      Note that it is not really necessary to remove the mappings from
3151      REPL_TBL, that would only waste time.  */
3152   EXECUTE_IF_SET_IN_SBITMAP (new_ssa_names, 0, i, sbi)
3153     if (!is_gimple_reg (ssa_name (i)))
3154       RESET_BIT (new_ssa_names, i);
3155
3156   EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3157     if (!is_gimple_reg (ssa_name (i)))
3158       RESET_BIT (old_ssa_names, i);
3159
3160   mark_set_for_renaming (update_ssa_stats.virtual_symbols);
3161 }
3162
3163
3164 /* Given a set of newly created SSA names (NEW_SSA_NAMES) and a set of
3165    existing SSA names (OLD_SSA_NAMES), update the SSA form so that:
3166
3167    1- The names in OLD_SSA_NAMES dominated by the definitions of
3168       NEW_SSA_NAMES are all re-written to be reached by the
3169       appropriate definition from NEW_SSA_NAMES.
3170
3171    2- If needed, new PHI nodes are added to the iterated dominance
3172       frontier of the blocks where each of NEW_SSA_NAMES are defined.
3173
3174    The mapping between OLD_SSA_NAMES and NEW_SSA_NAMES is setup by
3175    calling register_new_name_mapping for every pair of names that the
3176    caller wants to replace.
3177
3178    The caller identifies the new names that have been inserted and the
3179    names that need to be replaced by calling register_new_name_mapping
3180    for every pair <NEW, OLD>.  Note that the function assumes that the
3181    new names have already been inserted in the IL.
3182
3183    For instance, given the following code:
3184
3185      1  L0:
3186      2  x_1 = PHI (0, x_5)
3187      3  if (x_1 < 10)
3188      4    if (x_1 > 7)
3189      5      y_2 = 0
3190      6    else
3191      7      y_3 = x_1 + x_7
3192      8    endif
3193      9    x_5 = x_1 + 1
3194      10   goto L0;
3195      11 endif
3196
3197    Suppose that we insert new names x_10 and x_11 (lines 4 and 8).
3198
3199      1  L0:
3200      2  x_1 = PHI (0, x_5)
3201      3  if (x_1 < 10)
3202      4    x_10 = ...
3203      5    if (x_1 > 7)
3204      6      y_2 = 0
3205      7    else
3206      8      x_11 = ...
3207      9      y_3 = x_1 + x_7
3208      10   endif
3209      11   x_5 = x_1 + 1
3210      12   goto L0;
3211      13 endif
3212
3213    We want to replace all the uses of x_1 with the new definitions of
3214    x_10 and x_11.  Note that the only uses that should be replaced are
3215    those at lines 5, 9 and 11.  Also, the use of x_7 at line 9 should
3216    *not* be replaced (this is why we cannot just mark symbol 'x' for
3217    renaming).
3218
3219    Additionally, we may need to insert a PHI node at line 11 because
3220    that is a merge point for x_10 and x_11.  So the use of x_1 at line
3221    11 will be replaced with the new PHI node.  The insertion of PHI
3222    nodes is optional.  They are not strictly necessary to preserve the
3223    SSA form, and depending on what the caller inserted, they may not
3224    even be useful for the optimizers.  UPDATE_FLAGS controls various
3225    aspects of how update_ssa operates, see the documentation for
3226    TODO_update_ssa*.  */
3227
3228 void
3229 update_ssa (unsigned update_flags)
3230 {
3231   basic_block bb, start_bb;
3232   bitmap_iterator bi;
3233   unsigned i = 0;
3234   bool insert_phi_p;
3235   sbitmap_iterator sbi;
3236
3237   if (!need_ssa_update_p (cfun))
3238     return;
3239
3240   timevar_push (TV_TREE_SSA_INCREMENTAL);
3241
3242   if (!update_ssa_initialized_fn)
3243     init_update_ssa (cfun);
3244   gcc_assert (update_ssa_initialized_fn == cfun);
3245
3246   blocks_with_phis_to_rewrite = BITMAP_ALLOC (NULL);
3247   if (!phis_to_rewrite)
3248     phis_to_rewrite = VEC_alloc (gimple_vec, heap, last_basic_block);
3249   blocks_to_update = BITMAP_ALLOC (NULL);
3250
3251   /* Ensure that the dominance information is up-to-date.  */
3252   calculate_dominance_info (CDI_DOMINATORS);
3253
3254   /* Only one update flag should be set.  */
3255   gcc_assert (update_flags == TODO_update_ssa
3256               || update_flags == TODO_update_ssa_no_phi
3257               || update_flags == TODO_update_ssa_full_phi
3258               || update_flags == TODO_update_ssa_only_virtuals);
3259
3260   /* If we only need to update virtuals, remove all the mappings for
3261      real names before proceeding.  The caller is responsible for
3262      having dealt with the name mappings before calling update_ssa.  */
3263   if (update_flags == TODO_update_ssa_only_virtuals)
3264     {
3265       sbitmap_zero (old_ssa_names);
3266       sbitmap_zero (new_ssa_names);
3267       htab_empty (repl_tbl);
3268     }
3269
3270   insert_phi_p = (update_flags != TODO_update_ssa_no_phi);
3271
3272   if (insert_phi_p)
3273     {
3274       /* If the caller requested PHI nodes to be added, initialize
3275          live-in information data structures (DEF_BLOCKS).  */
3276
3277       /* For each SSA name N, the DEF_BLOCKS table describes where the
3278          name is defined, which blocks have PHI nodes for N, and which
3279          blocks have uses of N (i.e., N is live-on-entry in those
3280          blocks).  */
3281       def_blocks = htab_create (num_ssa_names, def_blocks_hash,
3282                                 def_blocks_eq, def_blocks_free);
3283     }
3284   else
3285     {
3286       def_blocks = NULL;
3287     }
3288
3289   /* Heuristic to avoid massive slow downs when the replacement
3290      mappings include lots of virtual names.  */
3291   if (insert_phi_p && switch_virtuals_to_full_rewrite_p ())
3292     switch_virtuals_to_full_rewrite ();
3293
3294   /* If there are names defined in the replacement table, prepare
3295      definition and use sites for all the names in NEW_SSA_NAMES and
3296      OLD_SSA_NAMES.  */
3297   if (sbitmap_first_set_bit (new_ssa_names) >= 0)
3298     {
3299       prepare_names_to_update (insert_phi_p);
3300
3301       /* If all the names in NEW_SSA_NAMES had been marked for
3302          removal, and there are no symbols to rename, then there's
3303          nothing else to do.  */
3304       if (sbitmap_first_set_bit (new_ssa_names) < 0
3305           && bitmap_empty_p (SYMS_TO_RENAME (cfun)))
3306         goto done;
3307     }
3308
3309   /* Next, determine the block at which to start the renaming process.  */
3310   if (!bitmap_empty_p (SYMS_TO_RENAME (cfun)))
3311     {
3312       /* If we have to rename some symbols from scratch, we need to
3313          start the process at the root of the CFG.  FIXME, it should
3314          be possible to determine the nearest block that had a
3315          definition for each of the symbols that are marked for
3316          updating.  For now this seems more work than it's worth.  */
3317       start_bb = ENTRY_BLOCK_PTR;
3318
3319       /* Traverse the CFG looking for existing definitions and uses of
3320          symbols in SYMS_TO_RENAME.  Mark interesting blocks and
3321          statements and set local live-in information for the PHI
3322          placement heuristics.  */
3323       prepare_block_for_update (start_bb, insert_phi_p);
3324     }
3325   else
3326     {
3327       /* Otherwise, the entry block to the region is the nearest
3328          common dominator for the blocks in BLOCKS.  */
3329       start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3330                                                    blocks_to_update);
3331     }
3332
3333   /* If requested, insert PHI nodes at the iterated dominance frontier
3334      of every block, creating new definitions for names in OLD_SSA_NAMES
3335      and for symbols in SYMS_TO_RENAME.  */
3336   if (insert_phi_p)
3337     {
3338       bitmap *dfs;
3339
3340       /* If the caller requested PHI nodes to be added, compute
3341          dominance frontiers.  */
3342       dfs = XNEWVEC (bitmap, last_basic_block);
3343       FOR_EACH_BB (bb)
3344         dfs[bb->index] = BITMAP_ALLOC (NULL);
3345       compute_dominance_frontiers (dfs);
3346
3347       if (sbitmap_first_set_bit (old_ssa_names) >= 0)
3348         {
3349           sbitmap_iterator sbi;
3350
3351           /* insert_update_phi_nodes_for will call add_new_name_mapping
3352              when inserting new PHI nodes, so the set OLD_SSA_NAMES
3353              will grow while we are traversing it (but it will not
3354              gain any new members).  Copy OLD_SSA_NAMES to a temporary
3355              for traversal.  */
3356           sbitmap tmp = sbitmap_alloc (old_ssa_names->n_bits);
3357           sbitmap_copy (tmp, old_ssa_names);
3358           EXECUTE_IF_SET_IN_SBITMAP (tmp, 0, i, sbi)
3359             insert_updated_phi_nodes_for (ssa_name (i), dfs, blocks_to_update,
3360                                           update_flags);
3361           sbitmap_free (tmp);
3362         }
3363
3364       EXECUTE_IF_SET_IN_BITMAP (SYMS_TO_RENAME (cfun), 0, i, bi)
3365         insert_updated_phi_nodes_for (referenced_var (i), dfs, blocks_to_update,
3366                                       update_flags);
3367
3368       FOR_EACH_BB (bb)
3369         BITMAP_FREE (dfs[bb->index]);
3370       free (dfs);
3371
3372       /* Insertion of PHI nodes may have added blocks to the region.
3373          We need to re-compute START_BB to include the newly added
3374          blocks.  */
3375       if (start_bb != ENTRY_BLOCK_PTR)
3376         start_bb = nearest_common_dominator_for_set (CDI_DOMINATORS,
3377                                                      blocks_to_update);
3378     }
3379
3380   /* Reset the current definition for name and symbol before renaming
3381      the sub-graph.  */
3382   EXECUTE_IF_SET_IN_SBITMAP (old_ssa_names, 0, i, sbi)
3383     set_current_def (ssa_name (i), NULL_TREE);
3384
3385   EXECUTE_IF_SET_IN_BITMAP (SYMS_TO_RENAME (cfun), 0, i, bi)
3386     set_current_def (referenced_var (i), NULL_TREE);
3387
3388   /* Now start the renaming process at START_BB.  */
3389   interesting_blocks = sbitmap_alloc (last_basic_block);
3390   sbitmap_zero (interesting_blocks);
3391   EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3392     SET_BIT (interesting_blocks, i);
3393
3394   rewrite_blocks (start_bb, REWRITE_UPDATE);
3395
3396   sbitmap_free (interesting_blocks);
3397
3398   /* Debugging dumps.  */
3399   if (dump_file)
3400     {
3401       int c;
3402       unsigned i;
3403
3404       dump_update_ssa (dump_file);
3405
3406       fprintf (dump_file, "Incremental SSA update started at block: %d\n\n",
3407                start_bb->index);
3408
3409       c = 0;
3410       EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3411         c++;
3412       fprintf (dump_file, "Number of blocks in CFG: %d\n", last_basic_block);
3413       fprintf (dump_file, "Number of blocks to update: %d (%3.0f%%)\n\n",
3414                c, PERCENT (c, last_basic_block));
3415
3416       if (dump_flags & TDF_DETAILS)
3417         {
3418           fprintf (dump_file, "Affected blocks: ");
3419           EXECUTE_IF_SET_IN_BITMAP (blocks_to_update, 0, i, bi)
3420             fprintf (dump_file, "%u ", i);
3421           fprintf (dump_file, "\n");
3422         }
3423
3424       fprintf (dump_file, "\n\n");
3425     }
3426
3427   /* Free allocated memory.  */
3428 done:
3429   delete_update_ssa ();
3430
3431   timevar_pop (TV_TREE_SSA_INCREMENTAL);
3432 }