OSDN Git Service

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