OSDN Git Service

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