OSDN Git Service

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