OSDN Git Service

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