OSDN Git Service

* tree-into-ssa.c (insert_phi_nodes_for): Pass argument
[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 Free Software Foundation, Inc.
3    Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "flags.h"
28 #include "rtl.h"
29 #include "tm_p.h"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "output.h"
34 #include "errors.h"
35 #include "expr.h"
36 #include "function.h"
37 #include "diagnostic.h"
38 #include "bitmap.h"
39 #include "tree-flow.h"
40 #include "tree-gimple.h"
41 #include "tree-inline.h"
42 #include "varray.h"
43 #include "timevar.h"
44 #include "hashtab.h"
45 #include "tree-dump.h"
46 #include "tree-pass.h"
47 #include "cfgloop.h"
48 #include "domwalk.h"
49 #include "ggc.h"
50
51 /* This file builds the SSA form for a function as described in:
52    R. Cytron, J. Ferrante, B. Rosen, M. Wegman, and K. Zadeck. Efficiently
53    Computing Static Single Assignment Form and the Control Dependence
54    Graph. ACM Transactions on Programming Languages and Systems,
55    13(4):451-490, October 1991.  */
56
57
58 /* Structure to map a variable VAR to the set of blocks that contain
59    definitions for VAR.  */
60 struct def_blocks_d
61 {
62   /* The variable.  */
63   tree var;
64
65   /* Blocks that contain definitions of VAR.  Bit I will be set if the
66      Ith block contains a definition of VAR.  */
67   bitmap def_blocks;
68
69   /* Blocks that contain a phi node for VAR.  */
70   bitmap phi_blocks;
71
72   /* Blocks where VAR is live-on-entry.  Similar semantics as
73      DEF_BLOCKS.  */
74   bitmap livein_blocks;
75 };
76
77 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
78    DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
79    basic blocks where VAR is defined (assigned a new value).  It also
80    contains a bitmap of all the blocks where VAR is live-on-entry
81    (i.e., there is a use of VAR in block B without a preceding
82    definition in B).  The live-on-entry information is used when
83    computing PHI pruning heuristics.  */
84 static htab_t def_blocks;
85
86 /* Stack of trees used to restore the global currdefs to its original
87    state after completing rewriting of a block and its dominator children.
88
89    This vector is used in two contexts.  The first is rewriting of _DECL
90    nodes into SSA_NAMEs.  In that context it's elements have the
91    following properties:
92
93      An SSA_NAME indicates that the current definition of the underlying
94      variable should be set to the given SSA_NAME.
95                                                                                 
96      A _DECL node indicates that the underlying variable has no current
97      definition.
98                                                                                 
99      A NULL node is used to mark the last node associated with the
100      current block. 
101
102
103    This vector is also used when rewriting an SSA_NAME which has multiple
104    definition sites into multiple SSA_NAMEs.  In that context entries come
105    in pairs.
106
107      The top entry is an SSA_NAME and the top-1 entry is the
108      current value for that SSA_NAME. 
109
110      A NULL node at the top entry is used to mark the last node associated
111      with the current block.  */
112 static VEC(tree_on_heap) *block_defs_stack;
113
114 /* Basic block vectors used in this file ought to be allocated in the heap.  */
115 DEF_VEC_MALLOC_P(basic_block);
116
117 /* Global data to attach to the main dominator walk structure.  */
118 struct mark_def_sites_global_data
119 {
120   /* This sbitmap contains the variables which are set before they
121      are used in a basic block.  We keep it as a global variable
122      solely to avoid the overhead of allocating and deallocating
123      the bitmap.  */
124   sbitmap kills;
125
126   /* Bitmap of names to rename.  */
127   sbitmap names_to_rename;
128 };
129
130 /* Information stored for ssa names.  */
131
132 struct ssa_name_info
133 {
134   /* This field indicates whether or not the variable may need PHI nodes.
135      See the enum's definition for more detailed information about the
136      states.  */
137   ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
138
139   /* The actual definition of the ssa name.  */
140   tree current_def;
141 };
142
143 /* Local functions.  */
144 static void rewrite_finalize_block (struct dom_walk_data *, basic_block);
145 static void rewrite_initialize_block (struct dom_walk_data *, basic_block);
146 static void rewrite_add_phi_arguments (struct dom_walk_data *, basic_block);
147 static void mark_def_sites (struct dom_walk_data *walk_data,
148                             basic_block bb, block_stmt_iterator);
149 static void mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
150                                              basic_block bb);
151 static void set_def_block (tree, basic_block, bool, bool);
152 static void set_livein_block (tree, basic_block);
153 static bool prepare_use_operand_for_rename (use_operand_p, size_t *uid_p);
154 static bool prepare_def_operand_for_rename (tree def, size_t *uid_p);
155 static void insert_phi_nodes (bitmap *, bitmap);
156 static void rewrite_stmt (struct dom_walk_data *, basic_block,
157                           block_stmt_iterator);
158 static inline void rewrite_operand (use_operand_p);
159 static void insert_phi_nodes_for (tree, bitmap *, VEC(basic_block) **);
160 static tree get_reaching_def (tree);
161 static hashval_t def_blocks_hash (const void *);
162 static int def_blocks_eq (const void *, const void *);
163 static void def_blocks_free (void *);
164 static int debug_def_blocks_r (void **, void *);
165 static inline struct def_blocks_d *get_def_blocks_for (tree);
166 static inline struct def_blocks_d *find_def_blocks_for (tree);
167 static void htab_statistics (FILE *, htab_t);
168
169 /* Get the information associated with NAME.  */
170
171 static inline struct ssa_name_info *
172 get_ssa_name_ann (tree name)
173 {
174   if (!SSA_NAME_AUX (name))
175     SSA_NAME_AUX (name) = xcalloc (1, sizeof (struct ssa_name_info));
176
177   return SSA_NAME_AUX (name);
178 }
179
180 /* Gets phi_state field for VAR.  */
181
182 static inline enum need_phi_state
183 get_phi_state (tree var)
184 {
185   if (TREE_CODE (var) == SSA_NAME)
186     return get_ssa_name_ann (var)->need_phi_state;
187   else
188     return var_ann (var)->need_phi_state;
189 }
190
191 /* Sets phi_state field for VAR to STATE.  */
192
193 static inline void
194 set_phi_state (tree var, enum need_phi_state state)
195 {
196   if (TREE_CODE (var) == SSA_NAME)
197     get_ssa_name_ann (var)->need_phi_state = state;
198   else
199     var_ann (var)->need_phi_state = state;
200 }
201
202 /* Return the current definition for VAR.  */
203
204 static inline tree
205 get_current_def (tree var)
206 {
207   if (TREE_CODE (var) == SSA_NAME)
208     return get_ssa_name_ann (var)->current_def;
209   else
210     return var_ann (var)->current_def;
211 }
212
213 /* Sets current definition of VAR to DEF.  */
214
215 static inline void
216 set_current_def (tree var, tree def)
217 {
218   if (TREE_CODE (var) == SSA_NAME)
219     get_ssa_name_ann (var)->current_def = def;
220   else
221     var_ann (var)->current_def = def;
222 }
223
224 /* Compute global livein information given the set of blockx where
225    an object is locally live at the start of the block (LIVEIN)
226    and the set of blocks where the object is defined (DEF_BLOCKS).
227
228    Note: This routine augments the existing local livein information
229    to include global livein (i.e., it modifies the underlying bitmap
230    for LIVEIN).  */
231
232 void
233 compute_global_livein (bitmap livein, bitmap def_blocks)
234 {
235   basic_block bb, *worklist, *tos;
236   unsigned i;
237   bitmap_iterator bi;
238
239   tos = worklist
240     = (basic_block *) xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
241
242   EXECUTE_IF_SET_IN_BITMAP (livein, 0, i, bi)
243     {
244       *tos++ = BASIC_BLOCK (i);
245     }
246
247   /* Iterate until the worklist is empty.  */
248   while (tos != worklist)
249     {
250       edge e;
251       edge_iterator ei;
252
253       /* Pull a block off the worklist.  */
254       bb = *--tos;
255
256       /* For each predecessor block.  */
257       FOR_EACH_EDGE (e, ei, bb->preds)
258         {
259           basic_block pred = e->src;
260           int pred_index = pred->index;
261
262           /* None of this is necessary for the entry block.  */
263           if (pred != ENTRY_BLOCK_PTR
264               && ! bitmap_bit_p (livein, pred_index)
265               && ! bitmap_bit_p (def_blocks, pred_index))
266             {
267               *tos++ = pred;
268               bitmap_set_bit (livein, pred_index);
269             }
270         }
271     }
272
273   free (worklist);
274 }
275
276
277 /* Block initialization routine for mark_def_sites.  Clear the 
278    KILLS bitmap at the start of each block.  */
279
280 static void
281 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
282                                  basic_block bb ATTRIBUTE_UNUSED)
283 {
284   struct mark_def_sites_global_data *gd = walk_data->global_data;
285   sbitmap kills = gd->kills;
286
287   sbitmap_zero (kills);
288 }
289
290 /* Block initialization routine for mark_def_sites.  Clear the 
291    KILLS bitmap at the start of each block.  */
292
293 static void
294 ssa_mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
295                                      basic_block bb)
296 {
297   struct mark_def_sites_global_data *gd = walk_data->global_data;
298   sbitmap kills = gd->kills;
299   tree phi, def;
300   unsigned def_uid;
301
302   sbitmap_zero (kills);
303
304   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
305     {
306       def = PHI_RESULT (phi);
307       def_uid = SSA_NAME_VERSION (def);
308
309       if (!TEST_BIT (gd->names_to_rename, def_uid))
310         continue;
311
312       set_def_block (def, bb, true, true);
313       SET_BIT (kills, def_uid);
314     }
315 }
316
317 /* Marks ssa names used as arguments of phis at the end of BB.  */
318
319 static void
320 ssa_mark_phi_uses (struct dom_walk_data *walk_data, basic_block bb)
321 {
322   struct mark_def_sites_global_data *gd = walk_data->global_data;
323   sbitmap kills = gd->kills;
324   edge e;
325   tree phi, use;
326   unsigned uid;
327   edge_iterator ei;
328
329   FOR_EACH_EDGE (e, ei, bb->succs)
330     {
331       if (e->dest == EXIT_BLOCK_PTR)
332         continue;
333
334       for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
335         {
336           use = PHI_ARG_DEF_FROM_EDGE (phi, e);
337           if (TREE_CODE (use) != SSA_NAME)
338             continue;
339
340           uid = SSA_NAME_VERSION (use);
341
342           if (TEST_BIT (gd->names_to_rename, uid)
343               && !TEST_BIT (kills, uid))
344             set_livein_block (use, bb);
345         }
346     }
347 }
348
349 /* Call back for walk_dominator_tree used to collect definition sites
350    for every variable in the function.  For every statement S in block
351    BB:
352
353    1- Variables defined by S in DEF_OPS(S) are marked in the bitmap
354       WALK_DATA->GLOBAL_DATA->KILLS.
355
356    2- If S uses a variable VAR and there is no preceding kill of VAR,
357       then it is marked in marked in the LIVEIN_BLOCKS bitmap
358       associated with VAR.
359
360    This information is used to determine which variables are live
361    across block boundaries to reduce the number of PHI nodes
362    we create.  */
363
364 static void
365 mark_def_sites (struct dom_walk_data *walk_data,
366                 basic_block bb,
367                 block_stmt_iterator bsi)
368 {
369   struct mark_def_sites_global_data *gd = walk_data->global_data;
370   sbitmap kills = gd->kills;
371   size_t uid;
372   tree stmt, def;
373   use_operand_p use_p;
374   def_operand_p def_p;
375   ssa_op_iter iter;
376
377   /* Mark all the blocks that have definitions for each variable in the
378      VARS_TO_RENAME bitmap.  */
379   stmt = bsi_stmt (bsi);
380   get_stmt_operands (stmt);
381
382   /* If a variable is used before being set, then the variable is live
383      across a block boundary, so mark it live-on-entry to BB.  */
384
385   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE | SSA_OP_VMUSTDEFKILL)
386     {
387       if (prepare_use_operand_for_rename (use_p, &uid)
388           && !TEST_BIT (kills, uid))
389         set_livein_block (USE_FROM_PTR (use_p), bb);
390     }
391   
392   /* Note that virtual definitions are irrelevant for computing KILLS
393      because a V_MAY_DEF does not constitute a killing definition of the
394      variable.  However, the operand of a virtual definitions is a use
395      of the variable, so it may cause the variable to be considered
396      live-on-entry.  */
397
398   FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
399     {
400       if (prepare_use_operand_for_rename (use_p, &uid))
401         {
402           /* If we do not already have an SSA_NAME for our destination,
403              then set the destination to the source.  */
404           if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
405             SET_DEF (def_p, USE_FROM_PTR (use_p));
406             
407           set_livein_block (USE_FROM_PTR (use_p), bb);
408           set_def_block (DEF_FROM_PTR (def_p), bb, false, false);
409         }
410     }
411
412   /* Now process the virtual must-defs made by this statement.  */
413   FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF | SSA_OP_VMUSTDEF)
414     {
415       if (prepare_def_operand_for_rename (def, &uid))
416         {
417           set_def_block (def, bb, false, false);
418           SET_BIT (kills, uid);
419         }
420     }
421
422 }
423
424 /* Ditto, but works over ssa names.  */
425
426 static void
427 ssa_mark_def_sites (struct dom_walk_data *walk_data,
428                     basic_block bb,
429                     block_stmt_iterator bsi)
430 {
431   struct mark_def_sites_global_data *gd = walk_data->global_data;
432   sbitmap kills = gd->kills;
433   size_t uid, def_uid;
434   tree stmt, use, def;
435   ssa_op_iter iter;
436
437   /* Mark all the blocks that have definitions for each variable in the
438      names_to_rename bitmap.  */
439   stmt = bsi_stmt (bsi);
440   get_stmt_operands (stmt);
441
442   /* If a variable is used before being set, then the variable is live
443      across a block boundary, so mark it live-on-entry to BB.  */
444   FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
445     {
446       uid = SSA_NAME_VERSION (use);
447
448       if (TEST_BIT (gd->names_to_rename, uid)
449           && !TEST_BIT (kills, uid))
450         set_livein_block (use, bb);
451     }
452           
453   /* Now process the definition made by this statement.  Mark the
454      variables in KILLS.  */
455   FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
456     {
457       def_uid = SSA_NAME_VERSION (def);
458
459       if (TEST_BIT (gd->names_to_rename, def_uid))
460         {
461           set_def_block (def, bb, false, true);
462           SET_BIT (kills, def_uid);
463         }
464     }
465 }
466
467 /* Mark block BB as the definition site for variable VAR.  PHI_P is true if
468    VAR is defined by a phi node.  SSA_P is true if we are called from
469    rewrite_ssa_into_ssa.  */
470
471 static void
472 set_def_block (tree var, basic_block bb, bool phi_p, bool ssa_p)
473 {
474   struct def_blocks_d *db_p;
475   enum need_phi_state state;
476
477   if (!ssa_p
478       && TREE_CODE (var) == SSA_NAME)
479     var = SSA_NAME_VAR (var);
480
481   state = get_phi_state (var);
482   db_p = get_def_blocks_for (var);
483
484   /* Set the bit corresponding to the block where VAR is defined.  */
485   bitmap_set_bit (db_p->def_blocks, bb->index);
486   if (phi_p)
487     bitmap_set_bit (db_p->phi_blocks, bb->index);
488
489   /* Keep track of whether or not we may need to insert phi nodes.
490
491      If we are in the UNKNOWN state, then this is the first definition
492      of VAR.  Additionally, we have not seen any uses of VAR yet, so
493      we do not need a phi node for this variable at this time (i.e.,
494      transition to NEED_PHI_STATE_NO).
495
496      If we are in any other state, then we either have multiple definitions
497      of this variable occurring in different blocks or we saw a use of the
498      variable which was not dominated by the block containing the
499      definition(s).  In this case we may need a PHI node, so enter
500      state NEED_PHI_STATE_MAYBE.  */
501   if (state == NEED_PHI_STATE_UNKNOWN)
502     set_phi_state (var, NEED_PHI_STATE_NO);
503   else
504     set_phi_state (var, NEED_PHI_STATE_MAYBE);
505 }
506
507
508 /* Mark block BB as having VAR live at the entry to BB.  */
509
510 static void
511 set_livein_block (tree var, basic_block bb)
512 {
513   struct def_blocks_d *db_p;
514   enum need_phi_state state = get_phi_state (var);
515
516   db_p = get_def_blocks_for (var);
517
518   /* Set the bit corresponding to the block where VAR is live in.  */
519   bitmap_set_bit (db_p->livein_blocks, bb->index);
520
521   /* Keep track of whether or not we may need to insert phi nodes.
522
523      If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
524      by the single block containing the definition(s) of this variable.  If
525      it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
526      NEED_PHI_STATE_MAYBE.  */
527   if (state == NEED_PHI_STATE_NO)
528     {
529       int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
530
531       if (def_block_index == -1
532           || ! dominated_by_p (CDI_DOMINATORS, bb,
533                                BASIC_BLOCK (def_block_index)))
534         set_phi_state (var, NEED_PHI_STATE_MAYBE);
535     }
536   else
537     set_phi_state (var, NEED_PHI_STATE_MAYBE);
538 }
539
540
541 /* If the use operand pointed to by OP_P needs to be renamed, then strip away 
542    any SSA_NAME wrapping the operand, set *UID_P to the underlying variable's 
543    uid, and return true.  Otherwise return false.  If the operand was an 
544    SSA_NAME, change it to the stripped name.  */
545
546 static bool
547 prepare_use_operand_for_rename (use_operand_p op_p, size_t *uid_p)
548 {
549   tree use = USE_FROM_PTR (op_p);
550   tree var = (TREE_CODE (use) != SSA_NAME) ? use : SSA_NAME_VAR (use);
551   *uid_p = var_ann (var)->uid;
552
553   /* Ignore variables that don't need to be renamed.  */
554   if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
555     return false;
556
557   /* The variable needs to be renamed.  If this is a use which already
558      has an SSA_NAME, then strip it off.
559
560      By not throwing away SSA_NAMEs on assignments, we avoid a lot of 
561      useless churn of SSA_NAMEs without having to overly complicate the
562      renamer.  */
563   if (TREE_CODE (use) == SSA_NAME)
564     SET_USE (op_p, var);
565
566   return true;
567 }
568
569 /* If the def variable DEF needs to be renamed, then strip away any SSA_NAME 
570    wrapping the operand, set *UID_P to the underlying variable's uid and return
571    true.  Otherwise return false.  */
572
573 static bool
574 prepare_def_operand_for_rename (tree def, size_t *uid_p)
575 {
576   tree var = (TREE_CODE (def) != SSA_NAME) ? def : SSA_NAME_VAR (def);
577   *uid_p = var_ann (var)->uid;
578
579   /* Ignore variables that don't need to be renamed.  */
580   if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
581     return false;
582
583   return true;
584 }
585
586 /* Helper for insert_phi_nodes.  If VAR needs PHI nodes, insert them
587    at the dominance frontier (DFS) of blocks defining VAR.
588    WORK_STACK is the vector used to implement the worklist of basic
589    blocks.  */
590
591 static inline void
592 insert_phi_nodes_1 (tree var, bitmap *dfs, VEC(basic_block) **work_stack)
593 {
594   if (get_phi_state (var) != NEED_PHI_STATE_NO)
595     insert_phi_nodes_for (var, dfs, work_stack);
596 }
597
598 /* Insert PHI nodes at the dominance frontier of blocks with variable
599    definitions.  DFS contains the dominance frontier information for
600    the flowgraph.  PHI nodes will only be inserted at the dominance
601    frontier of definition blocks for variables whose NEED_PHI_STATE
602    annotation is marked as ``maybe'' or ``unknown'' (computed by
603    mark_def_sites).  If NAMES_TO_RENAME is not NULL, do the same but
604    for ssa name rewriting.  */
605
606 static void
607 insert_phi_nodes (bitmap *dfs, bitmap names_to_rename)
608 {
609   unsigned i;
610   VEC(basic_block) *work_stack;
611   bitmap_iterator bi;
612
613   timevar_push (TV_TREE_INSERT_PHI_NODES);
614
615   /* Vector WORK_STACK is a stack of CFG blocks.  Each block that contains
616      an assignment or PHI node will be pushed to this stack.  */
617   work_stack = VEC_alloc (basic_block, n_basic_blocks);
618
619   /* Iterate over all variables in VARS_TO_RENAME.  For each variable, add
620      to the work list all the blocks that have a definition for the
621      variable.  PHI nodes will be added to the dominance frontier blocks of
622      each definition block.  */
623   if (names_to_rename)
624     {
625       EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i, bi)
626         {
627           if (ssa_name (i))
628             insert_phi_nodes_1 (ssa_name (i), dfs, &work_stack);
629         }
630     }
631   else if (vars_to_rename)
632     EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i, bi)
633       {
634         insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
635       }
636   else
637     for (i = 0; i < num_referenced_vars; i++)
638       insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
639
640   VEC_free (basic_block, work_stack);
641
642   timevar_pop (TV_TREE_INSERT_PHI_NODES);
643 }
644
645
646 /* Perform a depth-first traversal of the dominator tree looking for
647    variables to rename.  BB is the block where to start searching.
648    Renaming is a five step process:
649
650    1- Every definition made by PHI nodes at the start of the blocks is
651       registered as the current definition for the corresponding variable.
652
653    2- Every statement in BB is rewritten.  USE and VUSE operands are
654       rewritten with their corresponding reaching definition.  DEF and
655       VDEF targets are registered as new definitions.
656       
657    3- All the PHI nodes in successor blocks of BB are visited.  The
658       argument corresponding to BB is replaced with its current reaching
659       definition.
660
661    4- Recursively rewrite every dominator child block of BB.
662
663    5- Restore (in reverse order) the current reaching definition for every
664       new definition introduced in this block.  This is done so that when
665       we return from the recursive call, all the current reaching
666       definitions are restored to the names that were valid in the
667       dominator parent of BB.  */
668
669 /* SSA Rewriting Step 1.  Initialization, create a block local stack
670    of reaching definitions for new SSA names produced in this block
671    (BLOCK_DEFS).  Register new definitions for every PHI node in the
672    block.  */
673
674 static void
675 rewrite_initialize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
676                           basic_block bb)
677 {
678   tree phi;
679
680   if (dump_file && (dump_flags & TDF_DETAILS))
681     fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
682
683   /* Mark the unwind point for this block.  */
684   VEC_safe_push (tree_on_heap, block_defs_stack, NULL_TREE);
685
686   /* Step 1.  Register new definitions for every PHI node in the block.
687      Conceptually, all the PHI nodes are executed in parallel and each PHI
688      node introduces a new version for the associated variable.  */
689   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
690     {
691       tree result = PHI_RESULT (phi);
692
693       register_new_def (result, &block_defs_stack);
694     }
695 }
696
697 /* Register DEF (an SSA_NAME) to be a new definition for the original
698    ssa name VAR and push VAR's current reaching definition
699    into the stack pointed by BLOCK_DEFS_P.  */
700
701 static void
702 ssa_register_new_def (tree var, tree def)
703 {
704   tree currdef;
705    
706   /* If this variable is set in a single basic block and all uses are
707      dominated by the set(s) in that single basic block, then there is
708      nothing to do.  TODO we should not be called at all, and just
709      keep the original name.  */
710   if (get_phi_state (var) == NEED_PHI_STATE_NO)
711     {
712       set_current_def (var, def);
713       return;
714     }
715
716   currdef = get_current_def (var);
717
718   /* Push the current reaching definition into *BLOCK_DEFS_P.  This stack is
719      later used by the dominator tree callbacks to restore the reaching
720      definitions for all the variables defined in the block after a recursive
721      visit to all its immediately dominated blocks.  */
722   VEC_safe_push (tree_on_heap, block_defs_stack, currdef);
723   VEC_safe_push (tree_on_heap, block_defs_stack, var);
724
725   /* Set the current reaching definition for VAR to be DEF.  */
726   set_current_def (var, def);
727 }
728
729 /* Ditto, for rewriting ssa names.  */
730
731 static void
732 ssa_rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
733 {
734   tree phi, new_name;
735   sbitmap names_to_rename = walk_data->global_data;
736   edge e;
737   bool abnormal_phi;
738   edge_iterator ei;
739
740   if (dump_file && (dump_flags & TDF_DETAILS))
741     fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
742
743   /* Mark the unwind point for this block.  */
744   VEC_safe_push (tree_on_heap, block_defs_stack, NULL_TREE);
745
746   FOR_EACH_EDGE (e, ei, bb->preds)
747     if (e->flags & EDGE_ABNORMAL)
748       break;
749   abnormal_phi = (e != NULL);
750
751   /* Step 1.  Register new definitions for every PHI node in the block.
752      Conceptually, all the PHI nodes are executed in parallel and each PHI
753      node introduces a new version for the associated variable.  */
754   for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
755     {
756       tree result = PHI_RESULT (phi);
757
758       if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (result)))
759         {
760           new_name = duplicate_ssa_name (result, phi);
761           SET_PHI_RESULT (phi, new_name);
762
763           if (abnormal_phi)
764             SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
765         }
766       else
767         new_name = result;
768
769       ssa_register_new_def (result, new_name);
770     }
771 }
772
773 /* SSA Rewriting Step 3.  Visit all the successor blocks of BB looking for
774    PHI nodes.  For every PHI node found, add a new argument containing the
775    current reaching definition for the variable and the edge through which
776    that definition is reaching the PHI node.  */
777
778 static void
779 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
780                            basic_block bb)
781 {
782   edge e;
783   edge_iterator ei;
784
785   FOR_EACH_EDGE (e, ei, bb->succs)
786     {
787       tree phi;
788
789       for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
790         {
791           tree currdef;
792
793           /* If this PHI node has already been rewritten, then there is
794              nothing to do for this PHI or any following PHIs since we
795              always add new PHI nodes at the start of the PHI chain.  */
796           if (PHI_REWRITTEN (phi))
797             break;
798
799           currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
800           add_phi_arg (phi, currdef, e);
801         }
802     }
803 }
804
805 /*  Rewrite existing virtual PHI arguments so that they have the correct
806     reaching definitions.  BB is the basic block whose successors contain the
807     phi nodes we want to add arguments for.  */
808
809 static void
810 rewrite_virtual_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED, 
811                                basic_block bb)
812 {
813   edge e;
814   use_operand_p op;
815   edge_iterator ei;
816
817   FOR_EACH_EDGE (e, ei, bb->succs)
818     {
819       tree phi;
820
821       if (e->dest == EXIT_BLOCK_PTR)
822         continue;
823
824       for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
825         {
826           tree result = PHI_RESULT (phi);
827           op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
828           
829           if (is_gimple_reg (result) 
830               || !bitmap_bit_p (vars_to_rename, 
831                                 var_ann (SSA_NAME_VAR (result))->uid))
832             continue;
833
834           SET_USE (op, get_reaching_def (SSA_NAME_VAR (result)));
835           if (e->flags & EDGE_ABNORMAL)
836             SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1;
837         }
838     }
839 }
840
841 /* Ditto, for ssa name rewriting.  */
842
843 static void
844 ssa_rewrite_phi_arguments (struct dom_walk_data *walk_data, basic_block bb)
845 {
846   edge e;
847   sbitmap names_to_rename = walk_data->global_data;
848   use_operand_p op;
849   edge_iterator ei;
850
851   FOR_EACH_EDGE (e, ei, bb->succs)
852     {
853       tree phi;
854
855       if (e->dest == EXIT_BLOCK_PTR)
856         continue;
857
858       for (phi = phi_nodes (e->dest); phi; phi = PHI_CHAIN (phi))
859         {
860           op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
861           if (TREE_CODE (USE_FROM_PTR (op)) != SSA_NAME)
862             continue;
863           
864           if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (op))))
865             continue; 
866
867           SET_USE (op, get_reaching_def (USE_FROM_PTR (op)));
868           if (e->flags & EDGE_ABNORMAL)
869             SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1;
870         }
871     }
872 }
873
874
875 /* Similar to restore_vars_to_original_value, except that it restores 
876    CURRDEFS to its original value.  */
877 static void
878 rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
879                         basic_block bb ATTRIBUTE_UNUSED)
880 {
881   /* Restore CURRDEFS to its original state.  */
882   while (VEC_length (tree_on_heap, block_defs_stack) > 0)
883     {
884       tree tmp = VEC_pop (tree_on_heap, block_defs_stack);
885       tree saved_def, var;
886
887       if (tmp == NULL_TREE)
888         break;
889
890       /* If we recorded an SSA_NAME, then make the SSA_NAME the current
891          definition of its underlying variable.  If we recorded anything
892          else, it must have been an _DECL node and its current reaching
893          definition must have been NULL.  */
894       if (TREE_CODE (tmp) == SSA_NAME)
895         {
896           saved_def = tmp;
897           var = SSA_NAME_VAR (saved_def);
898         }
899       else
900         {
901           saved_def = NULL;
902           var = tmp;
903         }
904                                                                                 
905       set_current_def (var, saved_def);
906     }
907 }
908
909 /* Ditto, for rewriting ssa names.  */
910
911 static void
912 ssa_rewrite_finalize_block (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
913                             basic_block bb ATTRIBUTE_UNUSED)
914 {
915
916   /* Step 5.  Restore the current reaching definition for each variable
917      referenced in the block (in reverse order).  */
918   while (VEC_length (tree_on_heap, block_defs_stack) > 0)
919     {
920       tree var = VEC_pop (tree_on_heap, block_defs_stack);
921       tree saved_def;
922       
923       if (var == NULL)
924         break;
925
926       saved_def = VEC_pop (tree_on_heap, block_defs_stack);
927
928       set_current_def (var, saved_def);
929     }
930 }
931
932 /* Dump SSA information to FILE.  */
933
934 void
935 dump_tree_ssa (FILE *file)
936 {
937   basic_block bb;
938   const char *funcname
939     = lang_hooks.decl_printable_name (current_function_decl, 2);
940
941   fprintf (file, "SSA information for %s\n\n", funcname);
942
943   FOR_EACH_BB (bb)
944     {
945       dump_bb (bb, file, 0);
946       fputs ("    ", file);
947       print_generic_stmt (file, phi_nodes (bb), dump_flags);
948       fputs ("\n\n", file);
949     }
950 }
951
952
953 /* Dump SSA information to stderr.  */
954
955 void
956 debug_tree_ssa (void)
957 {
958   dump_tree_ssa (stderr);
959 }
960
961
962 /* Dump SSA statistics on FILE.  */
963
964 void
965 dump_tree_ssa_stats (FILE *file)
966 {
967   fprintf (file, "\nHash table statistics:\n");
968
969   fprintf (file, "    def_blocks: ");
970   htab_statistics (file, def_blocks);
971
972   fprintf (file, "\n");
973 }
974
975
976 /* Dump SSA statistics on stderr.  */
977
978 void
979 debug_tree_ssa_stats (void)
980 {
981   dump_tree_ssa_stats (stderr);
982 }
983
984
985 /* Dump statistics for the hash table HTAB.  */
986
987 static void
988 htab_statistics (FILE *file, htab_t htab)
989 {
990   fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
991            (long) htab_size (htab),
992            (long) htab_elements (htab),
993            htab_collisions (htab));
994 }
995
996
997 /* Insert PHI nodes for variable VAR using the dominance frontier
998    information given in DFS.  WORK_STACK is the vector used to
999    implement the worklist of basic blocks.  */
1000
1001 static void
1002 insert_phi_nodes_for (tree var, bitmap *dfs, VEC(basic_block) **work_stack)
1003 {
1004   struct def_blocks_d *def_map;
1005   bitmap phi_insertion_points;
1006   unsigned bb_index;
1007   edge e;
1008   tree phi;
1009   basic_block bb;
1010   bitmap_iterator bi;
1011
1012   def_map = find_def_blocks_for (var);
1013   if (def_map == NULL)
1014     return;
1015
1016   phi_insertion_points = BITMAP_XMALLOC ();
1017
1018   EXECUTE_IF_SET_IN_BITMAP (def_map->def_blocks, 0, bb_index, bi)
1019     {
1020       VEC_safe_push (basic_block, *work_stack, BASIC_BLOCK (bb_index));
1021     }
1022
1023   /* Pop a block off the worklist, add every block that appears in
1024      the original block's dfs that we have not already processed to
1025      the worklist.  Iterate until the worklist is empty.   Blocks
1026      which are added to the worklist are potential sites for
1027      PHI nodes. 
1028
1029      The iteration step could be done during PHI insertion just as
1030      easily.  We do it here for historical reasons -- we used to have
1031      a heuristic which used the potential PHI insertion points to
1032      determine if fully pruned or semi pruned SSA form was appropriate.
1033
1034      We now always use fully pruned SSA form.  */
1035   while (VEC_length (basic_block, *work_stack) > 0)
1036     {
1037       unsigned dfs_index;
1038       bitmap_iterator bi;
1039
1040       bb = VEC_pop (basic_block, *work_stack);
1041       bb_index = bb->index;
1042       
1043       EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index],
1044                                       phi_insertion_points,
1045                                       0, dfs_index, bi)
1046         {
1047           basic_block bb = BASIC_BLOCK (dfs_index);
1048
1049           /* Use a safe push because if there is a definition of VAR
1050              in every basic block, then WORK_STACK may eventually have
1051              more than N_BASIC_BLOCK entries.  */
1052           VEC_safe_push (basic_block, *work_stack, bb);
1053           bitmap_set_bit (phi_insertion_points, dfs_index);
1054         }
1055     }
1056
1057   /* Remove the blocks where we already have the phis.  */
1058   bitmap_and_compl_into (phi_insertion_points, def_map->phi_blocks);
1059
1060   /* Now compute global livein for this variable.  Note this modifies
1061      def_map->livein_blocks.  */
1062   compute_global_livein (def_map->livein_blocks, def_map->def_blocks);
1063
1064   /* And insert the PHI nodes.  */
1065   EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points, def_map->livein_blocks,
1066                             0, bb_index, bi)
1067     {
1068       bb = BASIC_BLOCK (bb_index);
1069
1070       phi = create_phi_node (var, bb);
1071
1072       /* If we are rewriting ssa names, add also the phi arguments.  */
1073       if (TREE_CODE (var) == SSA_NAME)
1074         {
1075           edge_iterator ei;
1076           FOR_EACH_EDGE (e, ei, bb->preds)
1077             add_phi_arg (phi, var, e);
1078         }
1079     }
1080
1081   BITMAP_XFREE (phi_insertion_points);
1082 }
1083
1084 /* SSA Rewriting Step 2.  Rewrite every variable used in each statement in
1085    the block with its immediate reaching definitions.  Update the current
1086    definition of a variable when a new real or virtual definition is found.  */
1087
1088 static void
1089 rewrite_stmt (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
1090               basic_block bb ATTRIBUTE_UNUSED,
1091               block_stmt_iterator si)
1092 {
1093   stmt_ann_t ann;
1094   tree stmt;
1095   use_operand_p use_p;
1096   def_operand_p def_p;
1097   ssa_op_iter iter;
1098
1099   stmt = bsi_stmt (si);
1100   ann = stmt_ann (stmt);
1101
1102   if (dump_file && (dump_flags & TDF_DETAILS))
1103     {
1104       fprintf (dump_file, "Renaming statement ");
1105       print_generic_stmt (dump_file, stmt, TDF_SLIM);
1106       fprintf (dump_file, "\n");
1107     }
1108
1109   /* We have just scanned the code for operands.  No statement should
1110      be modified.  */
1111   gcc_assert (!ann->modified);
1112
1113   /* Step 1.  Rewrite USES and VUSES in the statement.  */
1114   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
1115     rewrite_operand (use_p);
1116
1117   /* Step 2.  Register the statement's DEF and VDEF operands.  */
1118   FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1119     {
1120       if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
1121         SET_DEF (def_p, make_ssa_name (DEF_FROM_PTR (def_p), stmt));
1122
1123       /* FIXME: We shouldn't be registering new defs if the variable
1124          doesn't need to be renamed.  */
1125       register_new_def (DEF_FROM_PTR (def_p), &block_defs_stack);
1126     }
1127 }
1128
1129 /* Ditto, for rewriting ssa names.  */
1130
1131 static void
1132 ssa_rewrite_stmt (struct dom_walk_data *walk_data,
1133                   basic_block bb ATTRIBUTE_UNUSED,
1134                   block_stmt_iterator si)
1135 {
1136   stmt_ann_t ann;
1137   tree stmt, var;
1138   ssa_op_iter iter;
1139   use_operand_p use_p;
1140   def_operand_p def_p;
1141   sbitmap names_to_rename = walk_data->global_data;
1142
1143   stmt = bsi_stmt (si);
1144   ann = stmt_ann (stmt);
1145
1146   if (dump_file && (dump_flags & TDF_DETAILS))
1147     {
1148       fprintf (dump_file, "Renaming statement ");
1149       print_generic_stmt (dump_file, stmt, TDF_SLIM);
1150       fprintf (dump_file, "\n");
1151     }
1152
1153   /* We have just scanned the code for operands.  No statement should
1154      be modified.  */
1155   gcc_assert (!ann->modified);
1156
1157   /* Step 1.  Rewrite USES and VUSES in the statement.  */
1158   FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES | SSA_OP_ALL_KILLS)
1159     {
1160       if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (use_p))))
1161         SET_USE (use_p, get_reaching_def (USE_FROM_PTR (use_p)));
1162     }
1163
1164   /* Step 2.  Register the statement's DEF and VDEF operands.  */
1165   FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1166     {
1167       var = DEF_FROM_PTR (def_p);
1168
1169       if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (var)))
1170         continue;
1171
1172       SET_DEF (def_p, duplicate_ssa_name (var, stmt));
1173       ssa_register_new_def (var, DEF_FROM_PTR (def_p));
1174     }
1175 }
1176
1177 /* Replace the operand pointed by OP_P with its immediate reaching
1178    definition.  */
1179
1180 static inline void
1181 rewrite_operand (use_operand_p op_p)
1182 {
1183   if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME)
1184     SET_USE (op_p, get_reaching_def (USE_FROM_PTR (op_p)));
1185 }
1186
1187 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
1188    variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
1189    into the stack pointed by BLOCK_DEFS_P.  */
1190
1191 void
1192 register_new_def (tree def, VEC (tree_on_heap) **block_defs_p)
1193 {
1194   tree var = SSA_NAME_VAR (def);
1195   tree currdef;
1196    
1197   /* If this variable is set in a single basic block and all uses are
1198      dominated by the set(s) in that single basic block, then there is
1199      no reason to record anything for this variable in the block local
1200      definition stacks.  Doing so just wastes time and memory.
1201
1202      This is the same test to prune the set of variables which may
1203      need PHI nodes.  So we just use that information since it's already
1204      computed and available for us to use.  */
1205   if (get_phi_state (var) == NEED_PHI_STATE_NO)
1206     {
1207       set_current_def (var, def);
1208       return;
1209     }
1210
1211   currdef = get_current_def (var);
1212
1213   /* Push the current reaching definition into *BLOCK_DEFS_P.  This stack is
1214      later used by the dominator tree callbacks to restore the reaching
1215      definitions for all the variables defined in the block after a recursive
1216      visit to all its immediately dominated blocks.  If there is no current
1217      reaching definition, then just record the underlying _DECL node.  */
1218   VEC_safe_push (tree_on_heap, *block_defs_p, currdef ? currdef : var);
1219
1220   /* Set the current reaching definition for VAR to be DEF.  */
1221   set_current_def (var, def);
1222 }
1223
1224 /* Return the current definition for variable VAR.  If none is found,
1225    create a new SSA name to act as the zeroth definition for VAR.  If VAR
1226    is call clobbered and there exists a more recent definition of
1227    GLOBAL_VAR, return the definition for GLOBAL_VAR.  This means that VAR
1228    has been clobbered by a function call since its last assignment.  */
1229
1230 static tree
1231 get_reaching_def (tree var)
1232 {
1233   tree default_d, currdef_var, avar;
1234   
1235   /* Lookup the current reaching definition for VAR.  */
1236   default_d = NULL_TREE;
1237   currdef_var = get_current_def (var);
1238
1239   /* If there is no reaching definition for VAR, create and register a
1240      default definition for it (if needed).  */
1241   if (currdef_var == NULL_TREE)
1242     {
1243       if (TREE_CODE (var) == SSA_NAME)
1244         avar = SSA_NAME_VAR (var);
1245       else
1246         avar = var;
1247
1248       default_d = default_def (avar);
1249       if (default_d == NULL_TREE)
1250         {
1251           default_d = make_ssa_name (avar, build_empty_stmt ());
1252           set_default_def (avar, default_d);
1253         }
1254       set_current_def (var, default_d);
1255     }
1256
1257   /* Return the current reaching definition for VAR, or the default
1258      definition, if we had to create one.  */
1259   return (currdef_var) ? currdef_var : default_d;
1260 }
1261
1262
1263 /* Hashing and equality functions for DEF_BLOCKS.  */
1264
1265 static hashval_t
1266 def_blocks_hash (const void *p)
1267 {
1268   return htab_hash_pointer
1269         ((const void *)((const struct def_blocks_d *)p)->var);
1270 }
1271
1272 static int
1273 def_blocks_eq (const void *p1, const void *p2)
1274 {
1275   return ((const struct def_blocks_d *)p1)->var
1276          == ((const struct def_blocks_d *)p2)->var;
1277 }
1278
1279 /* Free memory allocated by one entry in DEF_BLOCKS.  */
1280
1281 static void
1282 def_blocks_free (void *p)
1283 {
1284   struct def_blocks_d *entry = p;
1285   BITMAP_XFREE (entry->def_blocks);
1286   BITMAP_XFREE (entry->phi_blocks);
1287   BITMAP_XFREE (entry->livein_blocks);
1288   free (entry);
1289 }
1290
1291
1292 /* Dump the DEF_BLOCKS hash table on stderr.  */
1293
1294 void
1295 debug_def_blocks (void)
1296 {
1297   htab_traverse (def_blocks, debug_def_blocks_r, NULL);
1298 }
1299
1300 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table.  */
1301
1302 static int
1303 debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
1304 {
1305   struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1306   
1307   fprintf (stderr, "VAR: ");
1308   print_generic_expr (stderr, db_p->var, dump_flags);
1309   bitmap_print (stderr, db_p->def_blocks, ", DEF_BLOCKS: { ", "}");
1310   bitmap_print (stderr, db_p->livein_blocks, ", LIVEIN_BLOCKS: { ", "}\n");
1311
1312   return 1;
1313 }
1314
1315
1316 /* Return the set of blocks where variable VAR is defined and the blocks
1317    where VAR is live on entry (livein).  Return NULL, if no entry is
1318    found in DEF_BLOCKS.  */
1319
1320 static inline struct def_blocks_d *
1321 find_def_blocks_for (tree var)
1322 {
1323   struct def_blocks_d dm;
1324   dm.var = var;
1325   return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1326 }
1327
1328
1329 /* Return the set of blocks where variable VAR is defined and the blocks
1330    where VAR is live on entry (livein).  If no entry is found in
1331    DEF_BLOCKS, a new one is created and returned.  */
1332
1333 static inline struct def_blocks_d *
1334 get_def_blocks_for (tree var)
1335 {
1336   struct def_blocks_d db, *db_p;
1337   void **slot;
1338
1339   db.var = var;
1340   slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
1341   if (*slot == NULL)
1342     {
1343       db_p = xmalloc (sizeof (*db_p));
1344       db_p->var = var;
1345       db_p->def_blocks = BITMAP_XMALLOC ();
1346       db_p->phi_blocks = BITMAP_XMALLOC ();
1347       db_p->livein_blocks = BITMAP_XMALLOC ();
1348       *slot = (void *) db_p;
1349     }
1350   else
1351     db_p = (struct def_blocks_d *) *slot;
1352
1353   return db_p;
1354 }
1355
1356 /* If a variable V in VARS_TO_RENAME is a pointer, the renaming
1357    process will cause us to lose the name memory tags that may have
1358    been associated with the various SSA_NAMEs of V.  This means that
1359    the variables aliased to those name tags also need to be renamed
1360    again.
1361
1362    FIXME 1- We should either have a better scheme for renaming
1363             pointers that doesn't lose name tags or re-run alias
1364             analysis to recover points-to information.
1365
1366          2- Currently we just invalidate *all* the name tags.  This
1367             should be more selective.  */
1368
1369 static void
1370 invalidate_name_tags (bitmap vars_to_rename)
1371 {
1372   unsigned i;
1373   bool rename_name_tags_p;
1374   bitmap_iterator bi;
1375
1376   rename_name_tags_p = false;
1377   EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i, bi)
1378     {
1379       if (POINTER_TYPE_P (TREE_TYPE (referenced_var (i))))
1380         {
1381           rename_name_tags_p = true;
1382           break;
1383         }
1384     }
1385
1386   if (rename_name_tags_p)
1387     for (i = 0; i < num_referenced_vars; i++)
1388       {
1389         var_ann_t ann = var_ann (referenced_var (i));
1390
1391         if (ann->mem_tag_kind == NAME_TAG)
1392           {
1393             size_t j;
1394             varray_type may_aliases = ann->may_aliases;
1395
1396             bitmap_set_bit (vars_to_rename, ann->uid);
1397             if (ann->may_aliases)
1398               for (j = 0; j < VARRAY_ACTIVE_SIZE (may_aliases); j++)
1399                 {
1400                   tree var = VARRAY_TREE (may_aliases, j);
1401                   bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1402                 }
1403           }
1404       }
1405 }
1406
1407 /* Rewrite the actual blocks, statements, and phi arguments, to be in SSA
1408    form.  FIX_VIRTUAL_PHIS is true if we should only be fixing up virtual
1409    phi arguments, instead of adding new phi arguments for just added phi
1410    nodes.  */
1411
1412
1413 static void
1414 rewrite_blocks (bool fix_virtual_phis)
1415 {
1416   struct dom_walk_data walk_data;
1417   
1418   /* Rewrite all the basic blocks in the program.  */
1419   timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1420
1421   /* Setup callbacks for the generic dominator tree walker.  */
1422   walk_data.walk_stmts_backward = false;
1423   walk_data.dom_direction = CDI_DOMINATORS;
1424   walk_data.initialize_block_local_data = NULL;
1425   walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
1426   walk_data.before_dom_children_walk_stmts = rewrite_stmt;
1427   walk_data.before_dom_children_after_stmts = NULL;
1428   if (!fix_virtual_phis)
1429     walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
1430   else
1431     walk_data.before_dom_children_after_stmts = rewrite_virtual_phi_arguments;
1432   
1433   walk_data.after_dom_children_before_stmts =  NULL;
1434   walk_data.after_dom_children_walk_stmts =  NULL;
1435   walk_data.after_dom_children_after_stmts =  rewrite_finalize_block;
1436   walk_data.global_data = NULL;
1437   walk_data.block_local_data_size = 0;
1438
1439   block_defs_stack = VEC_alloc (tree_on_heap, 10);
1440
1441   /* Initialize the dominator walker.  */
1442   init_walk_dominator_tree (&walk_data);
1443
1444   /* Recursively walk the dominator tree rewriting each statement in
1445      each basic block.  */
1446   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1447
1448   /* Finalize the dominator walker.  */
1449   fini_walk_dominator_tree (&walk_data);
1450
1451   htab_delete (def_blocks);
1452   
1453   VEC_free (tree_on_heap, block_defs_stack);
1454   block_defs_stack = NULL;
1455
1456   timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1457 }
1458
1459 /* Mark the definition site blocks for each variable, so that we know where
1460    the variable is actually live.  */
1461
1462 static void 
1463 mark_def_site_blocks (void)
1464 {
1465   size_t i;
1466   struct dom_walk_data walk_data;
1467   struct mark_def_sites_global_data mark_def_sites_global_data;
1468
1469   /* Allocate memory for the DEF_BLOCKS hash table.  */
1470   def_blocks = htab_create (VARRAY_ACTIVE_SIZE (referenced_vars),
1471                             def_blocks_hash, def_blocks_eq, def_blocks_free);
1472
1473   for (i = 0; i < num_referenced_vars; i++)
1474     set_current_def (referenced_var (i), NULL_TREE);
1475
1476   /* Ensure that the dominance information is OK.  */
1477   calculate_dominance_info (CDI_DOMINATORS);
1478
1479
1480   /* Setup callbacks for the generic dominator tree walker to find and
1481      mark definition sites.  */
1482   walk_data.walk_stmts_backward = false;
1483   walk_data.dom_direction = CDI_DOMINATORS;
1484   walk_data.initialize_block_local_data = NULL;
1485   walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
1486   walk_data.before_dom_children_walk_stmts = mark_def_sites;
1487   walk_data.before_dom_children_after_stmts = NULL; 
1488   walk_data.after_dom_children_before_stmts =  NULL;
1489   walk_data.after_dom_children_walk_stmts =  NULL;
1490   walk_data.after_dom_children_after_stmts =  NULL;
1491
1492   /* Notice that this bitmap is indexed using variable UIDs, so it must be
1493      large enough to accommodate all the variables referenced in the
1494      function, not just the ones we are renaming.  */
1495   mark_def_sites_global_data.kills = sbitmap_alloc (num_referenced_vars);
1496   walk_data.global_data = &mark_def_sites_global_data;
1497
1498   /* We do not have any local data.  */
1499   walk_data.block_local_data_size = 0;
1500
1501   /* Initialize the dominator walker.  */
1502   init_walk_dominator_tree (&walk_data);
1503
1504   /* Recursively walk the dominator tree.  */
1505   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1506
1507   /* Finalize the dominator walker.  */
1508   fini_walk_dominator_tree (&walk_data);
1509
1510   /* We no longer need this bitmap, clear and free it.  */
1511   sbitmap_free (mark_def_sites_global_data.kills);
1512
1513 }
1514 /* Main entry point into the SSA builder.  The renaming process
1515    proceeds in five main phases:
1516
1517    1- If VARS_TO_RENAME has any entries, any existing PHI nodes for
1518       those variables are removed from the flow graph so that they can
1519       be computed again.
1520
1521    2- Compute dominance frontier and immediate dominators, needed to
1522       insert PHI nodes and rename the function in dominator tree
1523       order.
1524
1525    3- Find and mark all the blocks that define variables
1526       (mark_def_site_blocks).
1527
1528    4- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
1529
1530    5- Rename all the blocks (rewrite_blocks) and statements in the program.
1531
1532    Steps 3 and 5 are done using the dominator tree walker
1533    (walk_dominator_tree).
1534
1535    ALL is true if all variables should be renamed (otherwise just those
1536    mentioned in vars_to_rename are taken into account).  */
1537
1538 void
1539 rewrite_into_ssa (bool all)
1540 {
1541   bitmap *dfs;
1542   basic_block bb;
1543   bitmap old_vars_to_rename = vars_to_rename;
1544   
1545   timevar_push (TV_TREE_SSA_OTHER);
1546
1547   if (all)
1548     vars_to_rename = NULL;
1549   else
1550     {
1551       /* Initialize the array of variables to rename.  */
1552       gcc_assert (vars_to_rename);
1553
1554       if (bitmap_empty_p (vars_to_rename))
1555         {
1556           timevar_pop (TV_TREE_SSA_OTHER);
1557           return;
1558         }
1559       
1560       invalidate_name_tags (vars_to_rename);
1561
1562       /* Now remove all the existing PHI nodes (if any) for the variables
1563          that we are about to rename into SSA.  */
1564       remove_all_phi_nodes_for (vars_to_rename);
1565     }
1566
1567   mark_def_site_blocks ();
1568
1569   /* Initialize dominance frontier and immediate dominator bitmaps. 
1570      Also count the number of predecessors for each block.  Doing so
1571      can save significant time during PHI insertion for large graphs.  */
1572   dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1573   FOR_EACH_BB (bb)
1574     dfs[bb->index] = BITMAP_XMALLOC ();
1575
1576   /* Compute dominance frontiers.  */
1577   compute_dominance_frontiers (dfs);
1578
1579   /* Insert PHI nodes at dominance frontiers of definition blocks.  */
1580   insert_phi_nodes (dfs, NULL);
1581
1582   rewrite_blocks (false);
1583
1584   /* Debugging dumps.  */
1585   if (dump_file && (dump_flags & TDF_STATS))
1586     {
1587       dump_dfa_stats (dump_file);
1588       dump_tree_ssa_stats (dump_file);
1589     }
1590
1591   /* Free allocated memory.  */
1592   FOR_EACH_BB (bb)
1593     BITMAP_XFREE (dfs[bb->index]);
1594   free (dfs);
1595
1596   vars_to_rename = old_vars_to_rename;
1597   timevar_pop (TV_TREE_SSA_OTHER);
1598 }
1599
1600 /* Rewrite the def-def chains so that they have the correct reaching
1601    definitions.  */
1602
1603 void
1604 rewrite_def_def_chains (void)
1605 {
1606   /* Ensure that the dominance information is OK.  */
1607   calculate_dominance_info (CDI_DOMINATORS);
1608   mark_def_site_blocks ();
1609   rewrite_blocks (true);
1610
1611 }
1612 /* The marked ssa names may have more than one definition;
1613    add phi nodes and rewrite them to fix this.  */
1614
1615 void
1616 rewrite_ssa_into_ssa (void)
1617 {
1618   bitmap *dfs;
1619   basic_block bb;
1620   struct dom_walk_data walk_data;
1621   struct mark_def_sites_global_data mark_def_sites_global_data;
1622   unsigned i;
1623   sbitmap snames_to_rename;
1624   tree name;
1625   bitmap to_rename;
1626   bitmap_iterator bi;
1627   
1628   if (!any_marked_for_rewrite_p ())
1629     return;
1630   to_rename = marked_ssa_names ();
1631
1632   timevar_push (TV_TREE_SSA_OTHER);
1633
1634   /* Allocate memory for the DEF_BLOCKS hash table.  */
1635   def_blocks = htab_create (num_ssa_names,
1636                             def_blocks_hash, def_blocks_eq, def_blocks_free);
1637
1638   /* Initialize dominance frontier and immediate dominator bitmaps. 
1639      Also count the number of predecessors for each block.  Doing so
1640      can save significant time during PHI insertion for large graphs.  */
1641   dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1642   FOR_EACH_BB (bb)
1643     dfs[bb->index] = BITMAP_XMALLOC ();
1644
1645   /* Ensure that the dominance information is OK.  */
1646   calculate_dominance_info (CDI_DOMINATORS);
1647
1648   /* Compute dominance frontiers.  */
1649   compute_dominance_frontiers (dfs);
1650
1651   /* Setup callbacks for the generic dominator tree walker to find and
1652      mark definition sites.  */
1653   walk_data.walk_stmts_backward = false;
1654   walk_data.dom_direction = CDI_DOMINATORS;
1655   walk_data.initialize_block_local_data = NULL;
1656   walk_data.before_dom_children_before_stmts
1657           = ssa_mark_def_sites_initialize_block;
1658   walk_data.before_dom_children_walk_stmts = ssa_mark_def_sites;
1659   walk_data.before_dom_children_after_stmts = ssa_mark_phi_uses; 
1660   walk_data.after_dom_children_before_stmts =  NULL;
1661   walk_data.after_dom_children_walk_stmts =  NULL;
1662   walk_data.after_dom_children_after_stmts =  NULL;
1663
1664   snames_to_rename = sbitmap_alloc (num_ssa_names);
1665   sbitmap_zero (snames_to_rename);
1666   EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi)
1667     {
1668       SET_BIT (snames_to_rename, i);
1669     }
1670
1671   mark_def_sites_global_data.kills = sbitmap_alloc (num_ssa_names);
1672   mark_def_sites_global_data.names_to_rename = snames_to_rename;
1673   walk_data.global_data = &mark_def_sites_global_data;
1674
1675   block_defs_stack = VEC_alloc (tree_on_heap, 10);
1676
1677   /* We do not have any local data.  */
1678   walk_data.block_local_data_size = 0;
1679
1680   /* Initialize the dominator walker.  */
1681   init_walk_dominator_tree (&walk_data);
1682
1683   /* Recursively walk the dominator tree.  */
1684   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1685
1686   /* Finalize the dominator walker.  */
1687   fini_walk_dominator_tree (&walk_data);
1688
1689   /* We no longer need this bitmap, clear and free it.  */
1690   sbitmap_free (mark_def_sites_global_data.kills);
1691
1692   for (i = 1; i < num_ssa_names; i++)
1693     if (ssa_name (i))
1694       set_current_def (ssa_name (i), NULL_TREE);
1695
1696   /* Insert PHI nodes at dominance frontiers of definition blocks.  */
1697   insert_phi_nodes (dfs, to_rename);
1698
1699   /* Rewrite all the basic blocks in the program.  */
1700   timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1701
1702   /* Setup callbacks for the generic dominator tree walker.  */
1703   walk_data.walk_stmts_backward = false;
1704   walk_data.dom_direction = CDI_DOMINATORS;
1705   walk_data.initialize_block_local_data = NULL;
1706   walk_data.before_dom_children_before_stmts = ssa_rewrite_initialize_block;
1707   walk_data.before_dom_children_walk_stmts = ssa_rewrite_stmt;
1708   walk_data.before_dom_children_after_stmts = ssa_rewrite_phi_arguments;
1709   walk_data.after_dom_children_before_stmts = NULL;
1710   walk_data.after_dom_children_walk_stmts =  NULL;
1711   walk_data.after_dom_children_after_stmts =  ssa_rewrite_finalize_block;
1712   walk_data.global_data = snames_to_rename;
1713   walk_data.block_local_data_size = 0;
1714
1715   /* Initialize the dominator walker.  */
1716   init_walk_dominator_tree (&walk_data);
1717
1718   /* Recursively walk the dominator tree rewriting each statement in
1719      each basic block.  */
1720   walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1721
1722   /* Finalize the dominator walker.  */
1723   fini_walk_dominator_tree (&walk_data);
1724
1725   unmark_all_for_rewrite ();
1726
1727   EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, bi)
1728     {
1729       release_ssa_name (ssa_name (i));
1730     }
1731
1732   sbitmap_free (snames_to_rename);
1733
1734   timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1735
1736   /* Debugging dumps.  */
1737   if (dump_file && (dump_flags & TDF_STATS))
1738     {
1739       dump_dfa_stats (dump_file);
1740       dump_tree_ssa_stats (dump_file);
1741     }
1742
1743   /* Free allocated memory.  */
1744   FOR_EACH_BB (bb)
1745     BITMAP_XFREE (dfs[bb->index]);
1746   free (dfs);
1747
1748   htab_delete (def_blocks);
1749
1750   for (i = 1; i < num_ssa_names; i++)
1751     {
1752       name = ssa_name (i);
1753       if (!name || !SSA_NAME_AUX (name))
1754         continue;
1755
1756       free (SSA_NAME_AUX (name));
1757       SSA_NAME_AUX (name) = NULL;
1758     }
1759
1760   BITMAP_XFREE (to_rename);
1761   
1762   VEC_free (tree_on_heap, block_defs_stack);
1763   block_defs_stack = NULL;
1764   timevar_pop (TV_TREE_SSA_OTHER);
1765 }
1766
1767 /* Rewrites all variables into ssa.  */
1768
1769 static void
1770 rewrite_all_into_ssa (void)
1771 {
1772   rewrite_into_ssa (true);
1773 }
1774
1775 struct tree_opt_pass pass_build_ssa = 
1776 {
1777   "ssa",                                /* name */
1778   NULL,                                 /* gate */
1779   rewrite_all_into_ssa,                 /* execute */
1780   NULL,                                 /* sub */
1781   NULL,                                 /* next */
1782   0,                                    /* static_pass_number */
1783   0,                                    /* tv_id */
1784   PROP_cfg | PROP_referenced_vars,      /* properties_required */
1785   PROP_ssa,                             /* properties_provided */
1786   0,                                    /* properties_destroyed */
1787   0,                                    /* todo_flags_start */
1788   TODO_dump_func | TODO_verify_ssa,                     /* todo_flags_finish */
1789   0                                     /* letter */
1790 };