OSDN Git Service

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