OSDN Git Service

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