PR tree-optimization/17468
[official-gcc.git] / gcc / tree-into-ssa.c
blob80e36dc29d20cc7cc9aba7773a3298002e42067d
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>
5 This file is part of GCC.
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.
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.
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. */
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"
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. */
58 /* Structure to map a variable VAR to the set of blocks that contain
59 definitions for VAR. */
60 struct def_blocks_d
62 /* The variable. */
63 tree var;
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;
69 /* Blocks that contain a phi node for VAR. */
70 bitmap phi_blocks;
72 /* Blocks where VAR is live-on-entry. Similar semantics as
73 DEF_BLOCKS. */
74 bitmap livein_blocks;
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;
86 /* Global data to attach to the main dominator walk structure. */
87 struct mark_def_sites_global_data
89 /* This sbitmap contains the variables which are set before they
90 are used in a basic block. We keep it as a global variable
91 solely to avoid the overhead of allocating and deallocating
92 the bitmap. */
93 sbitmap kills;
95 /* Bitmap of names to rename. */
96 sbitmap names_to_rename;
99 struct rewrite_block_data
101 varray_type block_defs;
104 /* Information stored for ssa names. */
106 struct ssa_name_info
108 /* This field indicates whether or not the variable may need PHI nodes.
109 See the enum's definition for more detailed information about the
110 states. */
111 ENUM_BITFIELD (need_phi_state) need_phi_state : 2;
113 /* The actual definition of the ssa name. */
114 tree current_def;
117 /* Local functions. */
118 static void rewrite_finalize_block (struct dom_walk_data *, basic_block);
119 static void rewrite_initialize_block_local_data (struct dom_walk_data *,
120 basic_block, bool);
121 static void rewrite_initialize_block (struct dom_walk_data *, basic_block);
122 static void rewrite_add_phi_arguments (struct dom_walk_data *, basic_block);
123 static void mark_def_sites (struct dom_walk_data *walk_data,
124 basic_block bb, block_stmt_iterator);
125 static void mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
126 basic_block bb);
127 static void set_def_block (tree, basic_block, bool, bool);
128 static void set_livein_block (tree, basic_block);
129 static bool prepare_use_operand_for_rename (use_operand_p, size_t *uid_p);
130 static bool prepare_def_operand_for_rename (tree def, size_t *uid_p);
131 static void insert_phi_nodes (bitmap *, bitmap);
132 static void rewrite_stmt (struct dom_walk_data *, basic_block,
133 block_stmt_iterator);
134 static inline void rewrite_operand (use_operand_p);
135 static void insert_phi_nodes_for (tree, bitmap *, varray_type *);
136 static tree get_reaching_def (tree);
137 static hashval_t def_blocks_hash (const void *);
138 static int def_blocks_eq (const void *, const void *);
139 static void def_blocks_free (void *);
140 static int debug_def_blocks_r (void **, void *);
141 static inline struct def_blocks_d *get_def_blocks_for (tree);
142 static inline struct def_blocks_d *find_def_blocks_for (tree);
143 static void htab_statistics (FILE *, htab_t);
145 /* Get the information associated with NAME. */
147 static inline struct ssa_name_info *
148 get_ssa_name_ann (tree name)
150 if (!SSA_NAME_AUX (name))
151 SSA_NAME_AUX (name) = xcalloc (1, sizeof (struct ssa_name_info));
153 return SSA_NAME_AUX (name);
156 /* Gets phi_state field for VAR. */
158 static inline enum need_phi_state
159 get_phi_state (tree var)
161 if (TREE_CODE (var) == SSA_NAME)
162 return get_ssa_name_ann (var)->need_phi_state;
163 else
164 return var_ann (var)->need_phi_state;
167 /* Sets phi_state field for VAR to STATE. */
169 static inline void
170 set_phi_state (tree var, enum need_phi_state state)
172 if (TREE_CODE (var) == SSA_NAME)
173 get_ssa_name_ann (var)->need_phi_state = state;
174 else
175 var_ann (var)->need_phi_state = state;
178 /* Return the current definition for VAR. */
180 static inline tree
181 get_current_def (tree var)
183 if (TREE_CODE (var) == SSA_NAME)
184 return get_ssa_name_ann (var)->current_def;
185 else
186 return var_ann (var)->current_def;
189 /* Sets current definition of VAR to DEF. */
191 static inline void
192 set_current_def (tree var, tree def)
194 if (TREE_CODE (var) == SSA_NAME)
195 get_ssa_name_ann (var)->current_def = def;
196 else
197 var_ann (var)->current_def = def;
200 /* Compute global livein information given the set of blockx where
201 an object is locally live at the start of the block (LIVEIN)
202 and the set of blocks where the object is defined (DEF_BLOCKS).
204 Note: This routine augments the existing local livein information
205 to include global livein (i.e., it modifies the underlying bitmap
206 for LIVEIN). */
208 void
209 compute_global_livein (bitmap livein, bitmap def_blocks)
211 basic_block bb, *worklist, *tos;
212 int i;
214 tos = worklist
215 = (basic_block *) xmalloc (sizeof (basic_block) * (n_basic_blocks + 1));
217 EXECUTE_IF_SET_IN_BITMAP (livein, 0, i,
219 *tos++ = BASIC_BLOCK (i);
222 /* Iterate until the worklist is empty. */
223 while (tos != worklist)
225 edge e;
227 /* Pull a block off the worklist. */
228 bb = *--tos;
230 /* For each predecessor block. */
231 for (e = bb->pred; e; e = e->pred_next)
233 basic_block pred = e->src;
234 int pred_index = pred->index;
236 /* None of this is necessary for the entry block. */
237 if (pred != ENTRY_BLOCK_PTR
238 && ! bitmap_bit_p (livein, pred_index)
239 && ! bitmap_bit_p (def_blocks, pred_index))
241 *tos++ = pred;
242 bitmap_set_bit (livein, pred_index);
247 free (worklist);
251 /* Block initialization routine for mark_def_sites. Clear the
252 KILLS bitmap at the start of each block. */
254 static void
255 mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
256 basic_block bb ATTRIBUTE_UNUSED)
258 struct mark_def_sites_global_data *gd = walk_data->global_data;
259 sbitmap kills = gd->kills;
261 sbitmap_zero (kills);
264 /* Block initialization routine for mark_def_sites. Clear the
265 KILLS bitmap at the start of each block. */
267 static void
268 ssa_mark_def_sites_initialize_block (struct dom_walk_data *walk_data,
269 basic_block bb)
271 struct mark_def_sites_global_data *gd = walk_data->global_data;
272 sbitmap kills = gd->kills;
273 tree phi, def;
274 unsigned def_uid;
276 sbitmap_zero (kills);
278 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
280 def = PHI_RESULT (phi);
281 def_uid = SSA_NAME_VERSION (def);
283 if (!TEST_BIT (gd->names_to_rename, def_uid))
284 continue;
286 set_def_block (def, bb, true, true);
287 SET_BIT (kills, def_uid);
291 /* Marks ssa names used as arguments of phis at the end of BB. */
293 static void
294 ssa_mark_phi_uses (struct dom_walk_data *walk_data, basic_block bb)
296 struct mark_def_sites_global_data *gd = walk_data->global_data;
297 sbitmap kills = gd->kills;
298 edge e;
299 tree phi, use;
300 unsigned uid;
302 for (e = bb->succ; e; e = e->succ_next)
304 if (e->dest == EXIT_BLOCK_PTR)
305 continue;
307 for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
309 use = PHI_ARG_DEF_FROM_EDGE (phi, e);
310 if (TREE_CODE (use) != SSA_NAME)
311 continue;
313 uid = SSA_NAME_VERSION (use);
315 if (TEST_BIT (gd->names_to_rename, uid)
316 && !TEST_BIT (kills, uid))
317 set_livein_block (use, bb);
322 /* Call back for walk_dominator_tree used to collect definition sites
323 for every variable in the function. For every statement S in block
326 1- Variables defined by S in DEF_OPS(S) are marked in the bitmap
327 WALK_DATA->GLOBAL_DATA->KILLS.
329 2- If S uses a variable VAR and there is no preceding kill of VAR,
330 then it is marked in marked in the LIVEIN_BLOCKS bitmap
331 associated with VAR.
333 This information is used to determine which variables are live
334 across block boundaries to reduce the number of PHI nodes
335 we create. */
337 static void
338 mark_def_sites (struct dom_walk_data *walk_data,
339 basic_block bb,
340 block_stmt_iterator bsi)
342 struct mark_def_sites_global_data *gd = walk_data->global_data;
343 sbitmap kills = gd->kills;
344 size_t uid;
345 tree stmt, def;
346 use_operand_p use_p;
347 def_operand_p def_p;
348 ssa_op_iter iter;
350 /* Mark all the blocks that have definitions for each variable in the
351 VARS_TO_RENAME bitmap. */
352 stmt = bsi_stmt (bsi);
353 get_stmt_operands (stmt);
355 /* If a variable is used before being set, then the variable is live
356 across a block boundary, so mark it live-on-entry to BB. */
358 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE | SSA_OP_VUSE)
360 if (prepare_use_operand_for_rename (use_p, &uid)
361 && !TEST_BIT (kills, uid))
362 set_livein_block (USE_FROM_PTR (use_p), bb);
365 /* Note that virtual definitions are irrelevant for computing KILLS
366 because a V_MAY_DEF does not constitute a killing definition of the
367 variable. However, the operand of a virtual definitions is a use
368 of the variable, so it may cause the variable to be considered
369 live-on-entry. */
371 FOR_EACH_SSA_MAYDEF_OPERAND (def_p, use_p, stmt, iter)
373 if (prepare_use_operand_for_rename (use_p, &uid))
375 /* If we do not already have an SSA_NAME for our destination,
376 then set the destination to the source. */
377 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
378 SET_DEF (def_p, USE_FROM_PTR (use_p));
380 set_livein_block (USE_FROM_PTR (use_p), bb);
381 set_def_block (DEF_FROM_PTR (def_p), bb, false, false);
385 /* Now process the virtual must-defs made by this statement. */
386 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_DEF | SSA_OP_VMUSTDEF)
388 if (prepare_def_operand_for_rename (def, &uid))
390 set_def_block (def, bb, false, false);
391 SET_BIT (kills, uid);
397 /* Ditto, but works over ssa names. */
399 static void
400 ssa_mark_def_sites (struct dom_walk_data *walk_data,
401 basic_block bb,
402 block_stmt_iterator bsi)
404 struct mark_def_sites_global_data *gd = walk_data->global_data;
405 sbitmap kills = gd->kills;
406 size_t uid, def_uid;
407 tree stmt, use, def;
408 ssa_op_iter iter;
410 /* Mark all the blocks that have definitions for each variable in the
411 names_to_rename bitmap. */
412 stmt = bsi_stmt (bsi);
413 get_stmt_operands (stmt);
415 /* If a variable is used before being set, then the variable is live
416 across a block boundary, so mark it live-on-entry to BB. */
417 FOR_EACH_SSA_TREE_OPERAND (use, stmt, iter, SSA_OP_ALL_USES)
419 uid = SSA_NAME_VERSION (use);
421 if (TEST_BIT (gd->names_to_rename, uid)
422 && !TEST_BIT (kills, uid))
423 set_livein_block (use, bb);
426 /* Now process the definition made by this statement. Mark the
427 variables in KILLS. */
428 FOR_EACH_SSA_TREE_OPERAND (def, stmt, iter, SSA_OP_ALL_DEFS)
430 def_uid = SSA_NAME_VERSION (def);
432 if (TEST_BIT (gd->names_to_rename, def_uid))
434 set_def_block (def, bb, false, true);
435 SET_BIT (kills, def_uid);
440 /* Mark block BB as the definition site for variable VAR. PHI_P is true if
441 VAR is defined by a phi node. SSA_P is true if we are called from
442 rewrite_ssa_into_ssa. */
444 static void
445 set_def_block (tree var, basic_block bb, bool phi_p, bool ssa_p)
447 struct def_blocks_d *db_p;
448 enum need_phi_state state;
450 if (!ssa_p
451 && TREE_CODE (var) == SSA_NAME)
452 var = SSA_NAME_VAR (var);
454 state = get_phi_state (var);
455 db_p = get_def_blocks_for (var);
457 /* Set the bit corresponding to the block where VAR is defined. */
458 bitmap_set_bit (db_p->def_blocks, bb->index);
459 if (phi_p)
460 bitmap_set_bit (db_p->phi_blocks, bb->index);
462 /* Keep track of whether or not we may need to insert phi nodes.
464 If we are in the UNKNOWN state, then this is the first definition
465 of VAR. Additionally, we have not seen any uses of VAR yet, so
466 we do not need a phi node for this variable at this time (i.e.,
467 transition to NEED_PHI_STATE_NO).
469 If we are in any other state, then we either have multiple definitions
470 of this variable occurring in different blocks or we saw a use of the
471 variable which was not dominated by the block containing the
472 definition(s). In this case we may need a PHI node, so enter
473 state NEED_PHI_STATE_MAYBE. */
474 if (state == NEED_PHI_STATE_UNKNOWN)
475 set_phi_state (var, NEED_PHI_STATE_NO);
476 else
477 set_phi_state (var, NEED_PHI_STATE_MAYBE);
481 /* Mark block BB as having VAR live at the entry to BB. */
483 static void
484 set_livein_block (tree var, basic_block bb)
486 struct def_blocks_d *db_p;
487 enum need_phi_state state = get_phi_state (var);
489 db_p = get_def_blocks_for (var);
491 /* Set the bit corresponding to the block where VAR is live in. */
492 bitmap_set_bit (db_p->livein_blocks, bb->index);
494 /* Keep track of whether or not we may need to insert phi nodes.
496 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
497 by the single block containing the definition(s) of this variable. If
498 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
499 NEED_PHI_STATE_MAYBE. */
500 if (state == NEED_PHI_STATE_NO)
502 int def_block_index = bitmap_first_set_bit (db_p->def_blocks);
504 if (def_block_index == -1
505 || ! dominated_by_p (CDI_DOMINATORS, bb,
506 BASIC_BLOCK (def_block_index)))
507 set_phi_state (var, NEED_PHI_STATE_MAYBE);
509 else
510 set_phi_state (var, NEED_PHI_STATE_MAYBE);
514 /* If the use operand pointed to by OP_P needs to be renamed, then strip away
515 any SSA_NAME wrapping the operand, set *UID_P to the underlying variable's
516 uid, and return true. Otherwise return false. If the operand was an
517 SSA_NAME, change it to the stripped name. */
519 static bool
520 prepare_use_operand_for_rename (use_operand_p op_p, size_t *uid_p)
522 tree use = USE_FROM_PTR (op_p);
523 tree var = (TREE_CODE (use) != SSA_NAME) ? use : SSA_NAME_VAR (use);
524 *uid_p = var_ann (var)->uid;
526 /* Ignore variables that don't need to be renamed. */
527 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
528 return false;
530 /* The variable needs to be renamed. If this is a use which already
531 has an SSA_NAME, then strip it off.
533 By not throwing away SSA_NAMEs on assignments, we avoid a lot of
534 useless churn of SSA_NAMEs without having to overly complicate the
535 renamer. */
536 if (TREE_CODE (use) == SSA_NAME)
537 SET_USE (op_p, var);
539 return true;
542 /* If the def variable DEF needs to be renamed, then strip away any SSA_NAME
543 wrapping the operand, set *UID_P to the underlying variable's uid and return
544 true. Otherwise return false. */
546 static bool
547 prepare_def_operand_for_rename (tree def, size_t *uid_p)
549 tree var = (TREE_CODE (def) != SSA_NAME) ? def : SSA_NAME_VAR (def);
550 *uid_p = var_ann (var)->uid;
552 /* Ignore variables that don't need to be renamed. */
553 if (vars_to_rename && !bitmap_bit_p (vars_to_rename, *uid_p))
554 return false;
556 return true;
559 /* Helper for insert_phi_nodes. If VAR needs PHI nodes, insert them
560 at the dominance frontier (DFS) of blocks defining VAR.
561 WORK_STACK is the varray used to implement the worklist of basic
562 blocks. */
564 static inline
565 void insert_phi_nodes_1 (tree var, bitmap *dfs, varray_type *work_stack)
567 if (get_phi_state (var) != NEED_PHI_STATE_NO)
568 insert_phi_nodes_for (var, dfs, work_stack);
571 /* Insert PHI nodes at the dominance frontier of blocks with variable
572 definitions. DFS contains the dominance frontier information for
573 the flowgraph. PHI nodes will only be inserted at the dominance
574 frontier of definition blocks for variables whose NEED_PHI_STATE
575 annotation is marked as ``maybe'' or ``unknown'' (computed by
576 mark_def_sites). If NAMES_TO_RENAME is not NULL, do the same but
577 for ssa name rewriting. */
579 static void
580 insert_phi_nodes (bitmap *dfs, bitmap names_to_rename)
582 size_t i;
583 varray_type work_stack;
585 timevar_push (TV_TREE_INSERT_PHI_NODES);
587 /* Array WORK_STACK is a stack of CFG blocks. Each block that contains
588 an assignment or PHI node will be pushed to this stack. */
589 VARRAY_GENERIC_PTR_NOGC_INIT (work_stack, last_basic_block, "work_stack");
591 /* Iterate over all variables in VARS_TO_RENAME. For each variable, add
592 to the work list all the blocks that have a definition for the
593 variable. PHI nodes will be added to the dominance frontier blocks of
594 each definition block. */
595 if (names_to_rename)
597 EXECUTE_IF_SET_IN_BITMAP (names_to_rename, 0, i,
599 if (ssa_name (i))
600 insert_phi_nodes_1 (ssa_name (i), dfs, &work_stack);
603 else if (vars_to_rename)
604 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i,
605 insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack));
606 else
607 for (i = 0; i < num_referenced_vars; i++)
608 insert_phi_nodes_1 (referenced_var (i), dfs, &work_stack);
610 VARRAY_FREE (work_stack);
612 timevar_pop (TV_TREE_INSERT_PHI_NODES);
616 /* Perform a depth-first traversal of the dominator tree looking for
617 variables to rename. BB is the block where to start searching.
618 Renaming is a five step process:
620 1- Every definition made by PHI nodes at the start of the blocks is
621 registered as the current definition for the corresponding variable.
623 2- Every statement in BB is rewritten. USE and VUSE operands are
624 rewritten with their corresponding reaching definition. DEF and
625 VDEF targets are registered as new definitions.
627 3- All the PHI nodes in successor blocks of BB are visited. The
628 argument corresponding to BB is replaced with its current reaching
629 definition.
631 4- Recursively rewrite every dominator child block of BB.
633 5- Restore (in reverse order) the current reaching definition for every
634 new definition introduced in this block. This is done so that when
635 we return from the recursive call, all the current reaching
636 definitions are restored to the names that were valid in the
637 dominator parent of BB. */
639 /* Initialize the local stacks.
641 BLOCK_DEFS is used to save all the existing reaching definitions for
642 the new SSA names introduced in this block. Before registering a
643 new definition for a variable, the existing reaching definition is
644 pushed into this stack so that we can restore it in Step 5. */
646 static void
647 rewrite_initialize_block_local_data (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
648 basic_block bb ATTRIBUTE_UNUSED,
649 bool recycled ATTRIBUTE_UNUSED)
651 struct rewrite_block_data *bd
652 = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
654 /* We get cleared memory from the allocator, so if the memory is
655 not cleared, then we are re-using a previously allocated entry. In
656 that case, we can also re-use the underlying virtual arrays. Just
657 make sure we clear them before using them! */
658 gcc_assert (!recycled || !bd->block_defs || !(VARRAY_ACTIVE_SIZE (bd->block_defs) > 0));
662 /* SSA Rewriting Step 1. Initialization, create a block local stack
663 of reaching definitions for new SSA names produced in this block
664 (BLOCK_DEFS). Register new definitions for every PHI node in the
665 block. */
667 static void
668 rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
670 tree phi;
671 struct rewrite_block_data *bd
672 = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
674 if (dump_file && (dump_flags & TDF_DETAILS))
675 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
677 /* Step 1. Register new definitions for every PHI node in the block.
678 Conceptually, all the PHI nodes are executed in parallel and each PHI
679 node introduces a new version for the associated variable. */
680 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
682 tree result = PHI_RESULT (phi);
684 register_new_def (result, &bd->block_defs);
688 /* Register DEF (an SSA_NAME) to be a new definition for the original
689 ssa name VAR and push VAR's current reaching definition
690 into the stack pointed by BLOCK_DEFS_P. */
692 static void
693 ssa_register_new_def (tree var, tree def, varray_type *block_defs_p)
695 tree currdef;
697 /* If this variable is set in a single basic block and all uses are
698 dominated by the set(s) in that single basic block, then there is
699 nothing to do. TODO we should not be called at all, and just
700 keep the original name. */
701 if (get_phi_state (var) == NEED_PHI_STATE_NO)
703 set_current_def (var, def);
704 return;
707 currdef = get_current_def (var);
708 if (! *block_defs_p)
709 VARRAY_TREE_INIT (*block_defs_p, 20, "block_defs");
711 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
712 later used by the dominator tree callbacks to restore the reaching
713 definitions for all the variables defined in the block after a recursive
714 visit to all its immediately dominated blocks. */
715 VARRAY_PUSH_TREE (*block_defs_p, var);
716 VARRAY_PUSH_TREE (*block_defs_p, currdef);
718 /* Set the current reaching definition for VAR to be DEF. */
719 set_current_def (var, def);
722 /* Ditto, for rewriting ssa names. */
724 static void
725 ssa_rewrite_initialize_block (struct dom_walk_data *walk_data, basic_block bb)
727 tree phi, new_name;
728 struct rewrite_block_data *bd
729 = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
730 sbitmap names_to_rename = walk_data->global_data;
731 edge e;
732 bool abnormal_phi;
734 if (dump_file && (dump_flags & TDF_DETAILS))
735 fprintf (dump_file, "\n\nRenaming block #%d\n\n", bb->index);
737 for (e = bb->pred; e; e = e->pred_next)
738 if (e->flags & EDGE_ABNORMAL)
739 break;
740 abnormal_phi = (e != NULL);
742 /* Step 1. Register new definitions for every PHI node in the block.
743 Conceptually, all the PHI nodes are executed in parallel and each PHI
744 node introduces a new version for the associated variable. */
745 for (phi = phi_nodes (bb); phi; phi = TREE_CHAIN (phi))
747 tree result = PHI_RESULT (phi);
749 if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (result)))
751 new_name = duplicate_ssa_name (result, phi);
752 SET_PHI_RESULT (phi, new_name);
754 if (abnormal_phi)
755 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (new_name) = 1;
757 else
758 new_name = result;
760 ssa_register_new_def (result, new_name, &bd->block_defs);
764 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
765 PHI nodes. For every PHI node found, add a new argument containing the
766 current reaching definition for the variable and the edge through which
767 that definition is reaching the PHI node. */
769 static void
770 rewrite_add_phi_arguments (struct dom_walk_data *walk_data ATTRIBUTE_UNUSED,
771 basic_block bb)
773 edge e;
775 for (e = bb->succ; e; e = e->succ_next)
777 tree phi;
779 for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
781 tree currdef;
783 /* If this PHI node has already been rewritten, then there is
784 nothing to do for this PHI or any following PHIs since we
785 always add new PHI nodes at the start of the PHI chain. */
786 if (PHI_REWRITTEN (phi))
787 break;
789 currdef = get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi)));
790 add_phi_arg (&phi, currdef, e);
795 /* Ditto, for ssa name rewriting. */
797 static void
798 ssa_rewrite_phi_arguments (struct dom_walk_data *walk_data, basic_block bb)
800 edge e;
801 sbitmap names_to_rename = walk_data->global_data;
802 use_operand_p op;
804 for (e = bb->succ; e; e = e->succ_next)
806 tree phi;
808 if (e->dest == EXIT_BLOCK_PTR)
809 continue;
811 for (phi = phi_nodes (e->dest); phi; phi = TREE_CHAIN (phi))
813 op = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
814 if (TREE_CODE (USE_FROM_PTR (op)) != SSA_NAME)
815 continue;
817 if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (op))))
818 continue;
820 SET_USE (op, get_reaching_def (USE_FROM_PTR (op)));
821 if (e->flags & EDGE_ABNORMAL)
822 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (USE_FROM_PTR (op)) = 1;
827 /* SSA Rewriting Step 5. Restore the current reaching definition for each
828 variable referenced in the block (in reverse order). */
830 static void
831 rewrite_finalize_block (struct dom_walk_data *walk_data,
832 basic_block bb ATTRIBUTE_UNUSED)
834 struct rewrite_block_data *bd
835 = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
837 /* Step 5. Restore the current reaching definition for each variable
838 referenced in the block (in reverse order). */
839 while (bd->block_defs && VARRAY_ACTIVE_SIZE (bd->block_defs) > 0)
841 tree tmp = VARRAY_TOP_TREE (bd->block_defs);
842 tree saved_def, var;
844 VARRAY_POP (bd->block_defs);
845 if (TREE_CODE (tmp) == SSA_NAME)
847 saved_def = tmp;
848 var = SSA_NAME_VAR (saved_def);
850 else
852 saved_def = NULL;
853 var = tmp;
856 set_current_def (var, saved_def);
860 /* Ditto, for rewriting ssa names. */
862 static void
863 ssa_rewrite_finalize_block (struct dom_walk_data *walk_data,
864 basic_block bb ATTRIBUTE_UNUSED)
866 struct rewrite_block_data *bd
867 = (struct rewrite_block_data *)VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
869 /* Step 5. Restore the current reaching definition for each variable
870 referenced in the block (in reverse order). */
871 while (bd->block_defs && VARRAY_ACTIVE_SIZE (bd->block_defs) > 0)
873 tree var;
874 tree saved_def = VARRAY_TOP_TREE (bd->block_defs);
875 VARRAY_POP (bd->block_defs);
877 var = VARRAY_TOP_TREE (bd->block_defs);
878 VARRAY_POP (bd->block_defs);
880 set_current_def (var, saved_def);
884 /* Dump SSA information to FILE. */
886 void
887 dump_tree_ssa (FILE *file)
889 basic_block bb;
890 const char *funcname
891 = lang_hooks.decl_printable_name (current_function_decl, 2);
893 fprintf (file, "SSA information for %s\n\n", funcname);
895 FOR_EACH_BB (bb)
897 dump_bb (bb, file, 0);
898 fputs (" ", file);
899 print_generic_stmt (file, phi_nodes (bb), dump_flags);
900 fputs ("\n\n", file);
905 /* Dump SSA information to stderr. */
907 void
908 debug_tree_ssa (void)
910 dump_tree_ssa (stderr);
914 /* Dump SSA statistics on FILE. */
916 void
917 dump_tree_ssa_stats (FILE *file)
919 fprintf (file, "\nHash table statistics:\n");
921 fprintf (file, " def_blocks: ");
922 htab_statistics (file, def_blocks);
924 fprintf (file, "\n");
928 /* Dump SSA statistics on stderr. */
930 void
931 debug_tree_ssa_stats (void)
933 dump_tree_ssa_stats (stderr);
937 /* Dump statistics for the hash table HTAB. */
939 static void
940 htab_statistics (FILE *file, htab_t htab)
942 fprintf (file, "size %ld, %ld elements, %f collision/search ratio\n",
943 (long) htab_size (htab),
944 (long) htab_elements (htab),
945 htab_collisions (htab));
949 /* Insert PHI nodes for variable VAR using the dominance frontier
950 information given in DFS. WORK_STACK is the varray used to
951 implement the worklist of basic blocks. */
953 static void
954 insert_phi_nodes_for (tree var, bitmap *dfs, varray_type *work_stack)
956 struct def_blocks_d *def_map;
957 bitmap phi_insertion_points;
958 int bb_index;
959 edge e;
960 tree phi;
961 basic_block bb;
963 def_map = find_def_blocks_for (var);
964 if (def_map == NULL)
965 return;
967 phi_insertion_points = BITMAP_XMALLOC ();
969 EXECUTE_IF_SET_IN_BITMAP (def_map->def_blocks, 0, bb_index,
971 VARRAY_PUSH_GENERIC_PTR_NOGC (*work_stack, BASIC_BLOCK (bb_index));
974 /* Pop a block off the worklist, add every block that appears in
975 the original block's dfs that we have not already processed to
976 the worklist. Iterate until the worklist is empty. Blocks
977 which are added to the worklist are potential sites for
978 PHI nodes.
980 The iteration step could be done during PHI insertion just as
981 easily. We do it here for historical reasons -- we used to have
982 a heuristic which used the potential PHI insertion points to
983 determine if fully pruned or semi pruned SSA form was appropriate.
985 We now always use fully pruned SSA form. */
986 while (VARRAY_ACTIVE_SIZE (*work_stack) > 0)
988 int dfs_index;
990 bb = VARRAY_TOP_GENERIC_PTR_NOGC (*work_stack);
991 bb_index = bb->index;
993 VARRAY_POP (*work_stack);
995 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs[bb_index],
996 phi_insertion_points,
997 0, dfs_index,
999 basic_block bb = BASIC_BLOCK (dfs_index);
1001 VARRAY_PUSH_GENERIC_PTR_NOGC (*work_stack, bb);
1002 bitmap_set_bit (phi_insertion_points, dfs_index);
1006 /* Remove the blocks where we already have the phis. */
1007 bitmap_operation (phi_insertion_points, phi_insertion_points,
1008 def_map->phi_blocks, BITMAP_AND_COMPL);
1010 /* Now compute global livein for this variable. Note this modifies
1011 def_map->livein_blocks. */
1012 compute_global_livein (def_map->livein_blocks, def_map->def_blocks);
1014 /* And insert the PHI nodes. */
1015 EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points, def_map->livein_blocks,
1016 0, bb_index,
1019 bb = BASIC_BLOCK (bb_index);
1021 phi = create_phi_node (var, bb);
1023 /* If we are rewriting ssa names, add also the phi arguments. */
1024 if (TREE_CODE (var) == SSA_NAME)
1026 for (e = bb->pred; e; e = e->pred_next)
1027 add_phi_arg (&phi, var, e);
1030 while (0));
1032 BITMAP_XFREE (phi_insertion_points);
1035 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
1036 the block with its immediate reaching definitions. Update the current
1037 definition of a variable when a new real or virtual definition is found. */
1039 static void
1040 rewrite_stmt (struct dom_walk_data *walk_data,
1041 basic_block bb ATTRIBUTE_UNUSED,
1042 block_stmt_iterator si)
1044 stmt_ann_t ann;
1045 tree stmt;
1046 use_operand_p use_p;
1047 def_operand_p def_p;
1048 ssa_op_iter iter;
1049 struct rewrite_block_data *bd;
1051 bd = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
1053 stmt = bsi_stmt (si);
1054 ann = stmt_ann (stmt);
1056 if (dump_file && (dump_flags & TDF_DETAILS))
1058 fprintf (dump_file, "Renaming statement ");
1059 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1060 fprintf (dump_file, "\n");
1063 /* We have just scanned the code for operands. No statement should
1064 be modified. */
1065 gcc_assert (!ann->modified);
1067 /* Step 1. Rewrite USES and VUSES in the statement. */
1068 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1069 rewrite_operand (use_p);
1071 /* Step 2. Register the statement's DEF and VDEF operands. */
1072 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1074 if (TREE_CODE (DEF_FROM_PTR (def_p)) != SSA_NAME)
1075 SET_DEF (def_p, make_ssa_name (DEF_FROM_PTR (def_p), stmt));
1077 /* FIXME: We shouldn't be registering new defs if the variable
1078 doesn't need to be renamed. */
1079 register_new_def (DEF_FROM_PTR (def_p), &bd->block_defs);
1083 /* Ditto, for rewriting ssa names. */
1085 static void
1086 ssa_rewrite_stmt (struct dom_walk_data *walk_data,
1087 basic_block bb ATTRIBUTE_UNUSED,
1088 block_stmt_iterator si)
1090 stmt_ann_t ann;
1091 tree stmt, var;
1092 ssa_op_iter iter;
1093 use_operand_p use_p;
1094 def_operand_p def_p;
1095 struct rewrite_block_data *bd;
1096 sbitmap names_to_rename = walk_data->global_data;
1098 bd = VARRAY_TOP_GENERIC_PTR (walk_data->block_data_stack);
1100 stmt = bsi_stmt (si);
1101 ann = stmt_ann (stmt);
1103 if (dump_file && (dump_flags & TDF_DETAILS))
1105 fprintf (dump_file, "Renaming statement ");
1106 print_generic_stmt (dump_file, stmt, TDF_SLIM);
1107 fprintf (dump_file, "\n");
1110 /* We have just scanned the code for operands. No statement should
1111 be modified. */
1112 gcc_assert (!ann->modified);
1114 /* Step 1. Rewrite USES and VUSES in the statement. */
1115 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_ALL_USES)
1117 if (TEST_BIT (names_to_rename, SSA_NAME_VERSION (USE_FROM_PTR (use_p))))
1118 SET_USE (use_p, get_reaching_def (USE_FROM_PTR (use_p)));
1121 /* Step 2. Register the statement's DEF and VDEF operands. */
1122 FOR_EACH_SSA_DEF_OPERAND (def_p, stmt, iter, SSA_OP_ALL_DEFS)
1124 var = DEF_FROM_PTR (def_p);
1126 if (!TEST_BIT (names_to_rename, SSA_NAME_VERSION (var)))
1127 continue;
1129 SET_DEF (def_p, duplicate_ssa_name (var, stmt));
1130 ssa_register_new_def (var, DEF_FROM_PTR (def_p), &bd->block_defs);
1134 /* Replace the operand pointed by OP_P with its immediate reaching
1135 definition. */
1137 static inline void
1138 rewrite_operand (use_operand_p op_p)
1140 if (TREE_CODE (USE_FROM_PTR (op_p)) != SSA_NAME)
1141 SET_USE (op_p, get_reaching_def (USE_FROM_PTR (op_p)));
1144 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
1145 variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
1146 into the stack pointed by BLOCK_DEFS_P. */
1148 void
1149 register_new_def (tree def, varray_type *block_defs_p)
1151 tree var = SSA_NAME_VAR (def);
1152 tree currdef;
1154 /* If this variable is set in a single basic block and all uses are
1155 dominated by the set(s) in that single basic block, then there is
1156 no reason to record anything for this variable in the block local
1157 definition stacks. Doing so just wastes time and memory.
1159 This is the same test to prune the set of variables which may
1160 need PHI nodes. So we just use that information since it's already
1161 computed and available for us to use. */
1162 if (get_phi_state (var) == NEED_PHI_STATE_NO)
1164 set_current_def (var, def);
1165 return;
1168 currdef = get_current_def (var);
1169 if (! *block_defs_p)
1170 VARRAY_TREE_INIT (*block_defs_p, 20, "block_defs");
1172 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
1173 later used by the dominator tree callbacks to restore the reaching
1174 definitions for all the variables defined in the block after a recursive
1175 visit to all its immediately dominated blocks. If there is no current
1176 reaching definition, then just record the underlying _DECL node. */
1177 VARRAY_PUSH_TREE (*block_defs_p, currdef ? currdef : var);
1179 /* Set the current reaching definition for VAR to be DEF. */
1180 set_current_def (var, def);
1183 /* Return the current definition for variable VAR. If none is found,
1184 create a new SSA name to act as the zeroth definition for VAR. If VAR
1185 is call clobbered and there exists a more recent definition of
1186 GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
1187 has been clobbered by a function call since its last assignment. */
1189 static tree
1190 get_reaching_def (tree var)
1192 tree default_d, currdef_var, avar;
1194 /* Lookup the current reaching definition for VAR. */
1195 default_d = NULL_TREE;
1196 currdef_var = get_current_def (var);
1198 /* If there is no reaching definition for VAR, create and register a
1199 default definition for it (if needed). */
1200 if (currdef_var == NULL_TREE)
1202 if (TREE_CODE (var) == SSA_NAME)
1203 avar = SSA_NAME_VAR (var);
1204 else
1205 avar = var;
1207 default_d = default_def (avar);
1208 if (default_d == NULL_TREE)
1210 default_d = make_ssa_name (avar, build_empty_stmt ());
1211 set_default_def (avar, default_d);
1213 set_current_def (var, default_d);
1216 /* Return the current reaching definition for VAR, or the default
1217 definition, if we had to create one. */
1218 return (currdef_var) ? currdef_var : default_d;
1222 /* Hashing and equality functions for DEF_BLOCKS. */
1224 static hashval_t
1225 def_blocks_hash (const void *p)
1227 return htab_hash_pointer
1228 ((const void *)((const struct def_blocks_d *)p)->var);
1231 static int
1232 def_blocks_eq (const void *p1, const void *p2)
1234 return ((const struct def_blocks_d *)p1)->var
1235 == ((const struct def_blocks_d *)p2)->var;
1238 /* Free memory allocated by one entry in DEF_BLOCKS. */
1240 static void
1241 def_blocks_free (void *p)
1243 struct def_blocks_d *entry = p;
1244 BITMAP_XFREE (entry->def_blocks);
1245 BITMAP_XFREE (entry->phi_blocks);
1246 BITMAP_XFREE (entry->livein_blocks);
1247 free (entry);
1251 /* Dump the DEF_BLOCKS hash table on stderr. */
1253 void
1254 debug_def_blocks (void)
1256 htab_traverse (def_blocks, debug_def_blocks_r, NULL);
1259 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
1261 static int
1262 debug_def_blocks_r (void **slot, void *data ATTRIBUTE_UNUSED)
1264 unsigned long i;
1265 struct def_blocks_d *db_p = (struct def_blocks_d *) *slot;
1267 fprintf (stderr, "VAR: ");
1268 print_generic_expr (stderr, db_p->var, dump_flags);
1269 fprintf (stderr, ", DEF_BLOCKS: { ");
1270 EXECUTE_IF_SET_IN_BITMAP (db_p->def_blocks, 0, i,
1271 fprintf (stderr, "%ld ", i));
1272 fprintf (stderr, "}");
1273 fprintf (stderr, ", LIVEIN_BLOCKS: { ");
1274 EXECUTE_IF_SET_IN_BITMAP (db_p->livein_blocks, 0, i,
1275 fprintf (stderr, "%ld ", i));
1276 fprintf (stderr, "}\n");
1278 return 1;
1282 /* Return the set of blocks where variable VAR is defined and the blocks
1283 where VAR is live on entry (livein). Return NULL, if no entry is
1284 found in DEF_BLOCKS. */
1286 static inline struct def_blocks_d *
1287 find_def_blocks_for (tree var)
1289 struct def_blocks_d dm;
1290 dm.var = var;
1291 return (struct def_blocks_d *) htab_find (def_blocks, &dm);
1295 /* Return the set of blocks where variable VAR is defined and the blocks
1296 where VAR is live on entry (livein). If no entry is found in
1297 DEF_BLOCKS, a new one is created and returned. */
1299 static inline struct def_blocks_d *
1300 get_def_blocks_for (tree var)
1302 struct def_blocks_d db, *db_p;
1303 void **slot;
1305 db.var = var;
1306 slot = htab_find_slot (def_blocks, (void *) &db, INSERT);
1307 if (*slot == NULL)
1309 db_p = xmalloc (sizeof (*db_p));
1310 db_p->var = var;
1311 db_p->def_blocks = BITMAP_XMALLOC ();
1312 db_p->phi_blocks = BITMAP_XMALLOC ();
1313 db_p->livein_blocks = BITMAP_XMALLOC ();
1314 *slot = (void *) db_p;
1316 else
1317 db_p = (struct def_blocks_d *) *slot;
1319 return db_p;
1322 /* If a variable V in VARS_TO_RENAME is a pointer, the renaming
1323 process will cause us to lose the name memory tags that may have
1324 been associated with the various SSA_NAMEs of V. This means that
1325 the variables aliased to those name tags also need to be renamed
1326 again.
1328 FIXME 1- We should either have a better scheme for renaming
1329 pointers that doesn't lose name tags or re-run alias
1330 analysis to recover points-to information.
1332 2- Currently we just invalidate *all* the name tags. This
1333 should be more selective. */
1335 static void
1336 invalidate_name_tags (bitmap vars_to_rename)
1338 size_t i;
1339 bool rename_name_tags_p;
1341 rename_name_tags_p = false;
1342 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename, 0, i,
1343 if (POINTER_TYPE_P (TREE_TYPE (referenced_var (i))))
1345 rename_name_tags_p = true;
1346 break;
1349 if (rename_name_tags_p)
1350 for (i = 0; i < num_referenced_vars; i++)
1352 var_ann_t ann = var_ann (referenced_var (i));
1354 if (ann->mem_tag_kind == NAME_TAG)
1356 size_t j;
1357 varray_type may_aliases = ann->may_aliases;
1359 bitmap_set_bit (vars_to_rename, ann->uid);
1360 if (ann->may_aliases)
1361 for (j = 0; j < VARRAY_ACTIVE_SIZE (may_aliases); j++)
1363 tree var = VARRAY_TREE (may_aliases, j);
1364 bitmap_set_bit (vars_to_rename, var_ann (var)->uid);
1371 /* Main entry point into the SSA builder. The renaming process
1372 proceeds in five main phases:
1374 1- If VARS_TO_RENAME has any entries, any existing PHI nodes for
1375 those variables are removed from the flow graph so that they can
1376 be computed again.
1378 2- Compute dominance frontier and immediate dominators, needed to
1379 insert PHI nodes and rename the function in dominator tree
1380 order.
1382 3- Find and mark all the blocks that define variables
1383 (mark_def_sites).
1385 4- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
1387 5- Rename all the blocks (rewrite_initialize_block,
1388 rewrite_add_phi_arguments) and statements in the program
1389 (rewrite_stmt).
1391 Steps 3 and 5 are done using the dominator tree walker
1392 (walk_dominator_tree).
1394 ALL is true if all variables should be renamed (otherwise just those
1395 mentioned in vars_to_rename are taken into account). */
1397 void
1398 rewrite_into_ssa (bool all)
1400 bitmap *dfs;
1401 basic_block bb;
1402 struct dom_walk_data walk_data;
1403 struct mark_def_sites_global_data mark_def_sites_global_data;
1404 bitmap old_vars_to_rename = vars_to_rename;
1405 unsigned i;
1407 timevar_push (TV_TREE_SSA_OTHER);
1409 if (all)
1410 vars_to_rename = NULL;
1411 else
1413 /* Initialize the array of variables to rename. */
1414 gcc_assert (vars_to_rename);
1416 if (bitmap_first_set_bit (vars_to_rename) < 0)
1418 timevar_pop (TV_TREE_SSA_OTHER);
1419 return;
1422 invalidate_name_tags (vars_to_rename);
1424 /* Now remove all the existing PHI nodes (if any) for the variables
1425 that we are about to rename into SSA. */
1426 remove_all_phi_nodes_for (vars_to_rename);
1429 /* Allocate memory for the DEF_BLOCKS hash table. */
1430 def_blocks = htab_create (VARRAY_ACTIVE_SIZE (referenced_vars),
1431 def_blocks_hash, def_blocks_eq, def_blocks_free);
1433 /* Initialize dominance frontier and immediate dominator bitmaps.
1434 Also count the number of predecessors for each block. Doing so
1435 can save significant time during PHI insertion for large graphs. */
1436 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1437 FOR_EACH_BB (bb)
1439 edge e;
1440 int count = 0;
1442 for (e = bb->pred; e; e = e->pred_next)
1443 count++;
1445 bb_ann (bb)->num_preds = count;
1446 dfs[bb->index] = BITMAP_XMALLOC ();
1449 for (i = 0; i < num_referenced_vars; i++)
1450 set_current_def (referenced_var (i), NULL_TREE);
1452 /* Ensure that the dominance information is OK. */
1453 calculate_dominance_info (CDI_DOMINATORS);
1455 /* Compute dominance frontiers. */
1456 compute_dominance_frontiers (dfs);
1458 /* Setup callbacks for the generic dominator tree walker to find and
1459 mark definition sites. */
1460 walk_data.walk_stmts_backward = false;
1461 walk_data.dom_direction = CDI_DOMINATORS;
1462 walk_data.initialize_block_local_data = NULL;
1463 walk_data.before_dom_children_before_stmts = mark_def_sites_initialize_block;
1464 walk_data.before_dom_children_walk_stmts = mark_def_sites;
1465 walk_data.before_dom_children_after_stmts = NULL;
1466 walk_data.after_dom_children_before_stmts = NULL;
1467 walk_data.after_dom_children_walk_stmts = NULL;
1468 walk_data.after_dom_children_after_stmts = NULL;
1470 /* Notice that this bitmap is indexed using variable UIDs, so it must be
1471 large enough to accommodate all the variables referenced in the
1472 function, not just the ones we are renaming. */
1473 mark_def_sites_global_data.kills = sbitmap_alloc (num_referenced_vars);
1474 walk_data.global_data = &mark_def_sites_global_data;
1476 /* We do not have any local data. */
1477 walk_data.block_local_data_size = 0;
1479 /* Initialize the dominator walker. */
1480 init_walk_dominator_tree (&walk_data);
1482 /* Recursively walk the dominator tree. */
1483 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1485 /* Finalize the dominator walker. */
1486 fini_walk_dominator_tree (&walk_data);
1488 /* We no longer need this bitmap, clear and free it. */
1489 sbitmap_free (mark_def_sites_global_data.kills);
1491 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1492 insert_phi_nodes (dfs, NULL);
1494 /* Rewrite all the basic blocks in the program. */
1495 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1497 /* Setup callbacks for the generic dominator tree walker. */
1498 walk_data.walk_stmts_backward = false;
1499 walk_data.dom_direction = CDI_DOMINATORS;
1500 walk_data.initialize_block_local_data = rewrite_initialize_block_local_data;
1501 walk_data.before_dom_children_before_stmts = rewrite_initialize_block;
1502 walk_data.before_dom_children_walk_stmts = rewrite_stmt;
1503 walk_data.before_dom_children_after_stmts = rewrite_add_phi_arguments;
1504 walk_data.after_dom_children_before_stmts = NULL;
1505 walk_data.after_dom_children_walk_stmts = NULL;
1506 walk_data.after_dom_children_after_stmts = rewrite_finalize_block;
1507 walk_data.global_data = NULL;
1508 walk_data.block_local_data_size = sizeof (struct rewrite_block_data);
1510 /* Initialize the dominator walker. */
1511 init_walk_dominator_tree (&walk_data);
1513 /* Recursively walk the dominator tree rewriting each statement in
1514 each basic block. */
1515 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1517 /* Finalize the dominator walker. */
1518 fini_walk_dominator_tree (&walk_data);
1520 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1522 /* Debugging dumps. */
1523 if (dump_file && (dump_flags & TDF_STATS))
1525 dump_dfa_stats (dump_file);
1526 dump_tree_ssa_stats (dump_file);
1529 /* Free allocated memory. */
1530 FOR_EACH_BB (bb)
1531 BITMAP_XFREE (dfs[bb->index]);
1532 free (dfs);
1534 htab_delete (def_blocks);
1536 vars_to_rename = old_vars_to_rename;
1537 timevar_pop (TV_TREE_SSA_OTHER);
1540 /* The marked ssa names may have more than one definition;
1541 add phi nodes and rewrite them to fix this. */
1543 void
1544 rewrite_ssa_into_ssa (void)
1546 bitmap *dfs;
1547 basic_block bb;
1548 struct dom_walk_data walk_data;
1549 struct mark_def_sites_global_data mark_def_sites_global_data;
1550 unsigned i;
1551 sbitmap snames_to_rename;
1552 tree name;
1553 bitmap to_rename;
1555 if (!any_marked_for_rewrite_p ())
1556 return;
1557 to_rename = marked_ssa_names ();
1559 timevar_push (TV_TREE_SSA_OTHER);
1561 /* Allocate memory for the DEF_BLOCKS hash table. */
1562 def_blocks = htab_create (num_ssa_names,
1563 def_blocks_hash, def_blocks_eq, def_blocks_free);
1565 /* Initialize dominance frontier and immediate dominator bitmaps.
1566 Also count the number of predecessors for each block. Doing so
1567 can save significant time during PHI insertion for large graphs. */
1568 dfs = (bitmap *) xmalloc (last_basic_block * sizeof (bitmap *));
1569 FOR_EACH_BB (bb)
1571 edge e;
1572 int count = 0;
1574 for (e = bb->pred; e; e = e->pred_next)
1575 count++;
1577 bb_ann (bb)->num_preds = count;
1578 dfs[bb->index] = BITMAP_XMALLOC ();
1581 /* Ensure that the dominance information is OK. */
1582 calculate_dominance_info (CDI_DOMINATORS);
1584 /* Compute dominance frontiers. */
1585 compute_dominance_frontiers (dfs);
1587 /* Setup callbacks for the generic dominator tree walker to find and
1588 mark definition sites. */
1589 walk_data.walk_stmts_backward = false;
1590 walk_data.dom_direction = CDI_DOMINATORS;
1591 walk_data.initialize_block_local_data = NULL;
1592 walk_data.before_dom_children_before_stmts
1593 = ssa_mark_def_sites_initialize_block;
1594 walk_data.before_dom_children_walk_stmts = ssa_mark_def_sites;
1595 walk_data.before_dom_children_after_stmts = ssa_mark_phi_uses;
1596 walk_data.after_dom_children_before_stmts = NULL;
1597 walk_data.after_dom_children_walk_stmts = NULL;
1598 walk_data.after_dom_children_after_stmts = NULL;
1600 snames_to_rename = sbitmap_alloc (num_ssa_names);
1601 sbitmap_zero (snames_to_rename);
1602 EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i,
1603 SET_BIT (snames_to_rename, i));
1605 mark_def_sites_global_data.kills = sbitmap_alloc (num_ssa_names);
1606 mark_def_sites_global_data.names_to_rename = snames_to_rename;
1607 walk_data.global_data = &mark_def_sites_global_data;
1609 /* We do not have any local data. */
1610 walk_data.block_local_data_size = 0;
1612 /* Initialize the dominator walker. */
1613 init_walk_dominator_tree (&walk_data);
1615 /* Recursively walk the dominator tree. */
1616 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1618 /* Finalize the dominator walker. */
1619 fini_walk_dominator_tree (&walk_data);
1621 /* We no longer need this bitmap, clear and free it. */
1622 sbitmap_free (mark_def_sites_global_data.kills);
1624 for (i = 1; i < num_ssa_names; i++)
1625 set_current_def (ssa_name (i), NULL_TREE);
1627 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1628 insert_phi_nodes (dfs, to_rename);
1630 /* Rewrite all the basic blocks in the program. */
1631 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS);
1633 /* Setup callbacks for the generic dominator tree walker. */
1634 walk_data.walk_stmts_backward = false;
1635 walk_data.dom_direction = CDI_DOMINATORS;
1636 walk_data.initialize_block_local_data
1637 = rewrite_initialize_block_local_data;
1638 walk_data.before_dom_children_before_stmts = ssa_rewrite_initialize_block;
1639 walk_data.before_dom_children_walk_stmts = ssa_rewrite_stmt;
1640 walk_data.before_dom_children_after_stmts = ssa_rewrite_phi_arguments;
1641 walk_data.after_dom_children_before_stmts = NULL;
1642 walk_data.after_dom_children_walk_stmts = NULL;
1643 walk_data.after_dom_children_after_stmts = ssa_rewrite_finalize_block;
1644 walk_data.global_data = snames_to_rename;
1645 walk_data.block_local_data_size = sizeof (struct rewrite_block_data);
1647 /* Initialize the dominator walker. */
1648 init_walk_dominator_tree (&walk_data);
1650 /* Recursively walk the dominator tree rewriting each statement in
1651 each basic block. */
1652 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
1654 /* Finalize the dominator walker. */
1655 fini_walk_dominator_tree (&walk_data);
1657 unmark_all_for_rewrite ();
1659 EXECUTE_IF_SET_IN_BITMAP (to_rename, 0, i, release_ssa_name (ssa_name (i)));
1661 sbitmap_free (snames_to_rename);
1663 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS);
1665 /* Debugging dumps. */
1666 if (dump_file && (dump_flags & TDF_STATS))
1668 dump_dfa_stats (dump_file);
1669 dump_tree_ssa_stats (dump_file);
1672 /* Free allocated memory. */
1673 FOR_EACH_BB (bb)
1674 BITMAP_XFREE (dfs[bb->index]);
1675 free (dfs);
1677 htab_delete (def_blocks);
1679 for (i = 1; i < num_ssa_names; i++)
1681 name = ssa_name (i);
1682 if (!SSA_NAME_AUX (name))
1683 continue;
1685 free (SSA_NAME_AUX (name));
1686 SSA_NAME_AUX (name) = NULL;
1689 BITMAP_XFREE (to_rename);
1690 timevar_pop (TV_TREE_SSA_OTHER);
1693 /* Rewrites all variables into ssa. */
1695 static void
1696 rewrite_all_into_ssa (void)
1698 rewrite_into_ssa (true);
1701 struct tree_opt_pass pass_build_ssa =
1703 "ssa", /* name */
1704 NULL, /* gate */
1705 rewrite_all_into_ssa, /* execute */
1706 NULL, /* sub */
1707 NULL, /* next */
1708 0, /* static_pass_number */
1709 0, /* tv_id */
1710 PROP_cfg | PROP_referenced_vars, /* properties_required */
1711 PROP_ssa, /* properties_provided */
1712 0, /* properties_destroyed */
1713 0, /* todo_flags_start */
1714 TODO_dump_func | TODO_verify_ssa, /* todo_flags_finish */
1715 0 /* letter */