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)
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. */
24 #include "coretypes.h"
30 #include "langhooks.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
37 #include "diagnostic.h"
39 #include "tree-flow.h"
40 #include "tree-gimple.h"
41 #include "tree-inline.h"
44 #include "tree-alias-common.h"
46 #include "tree-dump.h"
47 #include "tree-pass.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. */
65 /* Blocks that contain definitions of VAR. Bit I will be set if the
66 Ith block contains a definition of VAR. */
69 /* Blocks where VAR is live-on-entry. Similar semantics as
74 /* Each entry in DEF_BLOCKS contains an element of type STRUCT
75 DEF_BLOCKS_D, mapping a variable VAR to a bitmap describing all the
76 basic blocks where VAR is defined (assigned a new value). It also
77 contains a bitmap of all the blocks where VAR is live-on-entry
78 (i.e., there is a use of VAR in block B without a preceding
79 definition in B). The live-on-entry information is used when
80 computing PHI pruning heuristics. */
81 static htab_t def_blocks
;
83 /* Global data to attach to the main dominator walk structure. */
84 struct mark_def_sites_global_data
86 /* This sbitmap contains the variables which are set before they
87 are used in a basic block. We keep it as a global variable
88 solely to avoid the overhead of allocating and deallocating
93 struct rewrite_block_data
95 varray_type block_defs
;
99 /* Local functions. */
100 static void rewrite_finalize_block (struct dom_walk_data
*, basic_block
);
101 static void rewrite_initialize_block_local_data (struct dom_walk_data
*,
103 static void rewrite_initialize_block (struct dom_walk_data
*, basic_block
);
104 static void rewrite_add_phi_arguments (struct dom_walk_data
*, basic_block
);
105 static void mark_def_sites (struct dom_walk_data
*walk_data
,
106 basic_block bb
, block_stmt_iterator
);
107 static void mark_def_sites_initialize_block (struct dom_walk_data
*walk_data
,
109 static void compute_global_livein (bitmap
, bitmap
);
110 static void set_def_block (tree
, basic_block
);
111 static void set_livein_block (tree
, basic_block
);
112 static bool prepare_operand_for_rename (tree
*op_p
, size_t *uid_p
, bool);
113 static void insert_phi_nodes (bitmap
*);
114 static void rewrite_stmt (struct dom_walk_data
*, basic_block
,
115 block_stmt_iterator
);
116 static inline void rewrite_operand (tree
*);
117 static void insert_phi_nodes_for (tree
, bitmap
*, varray_type
*);
118 static tree
get_reaching_def (tree
);
119 static hashval_t
def_blocks_hash (const void *);
120 static int def_blocks_eq (const void *, const void *);
121 static void def_blocks_free (void *);
122 static int debug_def_blocks_r (void **, void *);
123 static inline struct def_blocks_d
*get_def_blocks_for (tree
);
124 static inline struct def_blocks_d
*find_def_blocks_for (tree
);
125 static void htab_statistics (FILE *, htab_t
);
127 /* Compute global livein information given the set of blockx where
128 an object is locally live at the start of the block (LIVEIN)
129 and the set of blocks where the object is defined (DEF_BLOCKS).
131 Note: This routine augments the existing local livein information
132 to include global livein (i.e., it modifies the underlying bitmap
136 compute_global_livein (bitmap livein
, bitmap def_blocks
)
138 basic_block bb
, *worklist
, *tos
;
142 = (basic_block
*) xmalloc (sizeof (basic_block
) * (last_basic_block
+ 1));
144 EXECUTE_IF_SET_IN_BITMAP (livein
, 0, i
,
146 *tos
++ = BASIC_BLOCK (i
);
149 /* Iterate until the worklist is empty. */
150 while (tos
!= worklist
)
154 /* Pull a block off the worklist. */
157 /* For each predecessor block. */
158 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
160 basic_block pred
= e
->src
;
161 int pred_index
= pred
->index
;
163 /* None of this is necessary for the entry block. */
164 if (pred
!= ENTRY_BLOCK_PTR
165 && ! bitmap_bit_p (livein
, pred_index
)
166 && ! bitmap_bit_p (def_blocks
, pred_index
))
169 bitmap_set_bit (livein
, pred_index
);
178 /* Block initialization routine for mark_def_sites. Clear the
179 KILLS bitmap at the start of each block. */
182 mark_def_sites_initialize_block (struct dom_walk_data
*walk_data
,
183 basic_block bb ATTRIBUTE_UNUSED
)
185 struct mark_def_sites_global_data
*gd
= walk_data
->global_data
;
186 sbitmap kills
= gd
->kills
;
188 sbitmap_zero (kills
);
192 /* Call back for walk_dominator_tree used to collect definition sites
193 for every variable in the function. For every statement S in block
196 1- Variables defined by S in DEF_OPS(S) are marked in the bitmap
197 WALK_DATA->GLOBAL_DATA->KILLS.
199 2- If S uses a variable VAR and there is no preceding kill of VAR,
200 then it is marked in marked in the LIVEIN_BLOCKS bitmap
203 This information is used to determine which variables are live
204 across block boundaries to reduce the number of PHI nodes
208 mark_def_sites (struct dom_walk_data
*walk_data
,
210 block_stmt_iterator bsi
)
212 struct mark_def_sites_global_data
*gd
= walk_data
->global_data
;
213 sbitmap kills
= gd
->kills
;
222 /* Mark all the blocks that have definitions for each variable in the
223 VARS_TO_RENAME bitmap. */
224 stmt
= bsi_stmt (bsi
);
225 get_stmt_operands (stmt
);
226 ann
= stmt_ann (stmt
);
228 /* If a variable is used before being set, then the variable is live
229 across a block boundary, so mark it live-on-entry to BB. */
230 uses
= USE_OPS (ann
);
231 for (i
= 0; i
< NUM_USES (uses
); i
++)
233 tree
*use_p
= USE_OP_PTR (uses
, i
);
235 if (prepare_operand_for_rename (use_p
, &uid
, true)
236 && !TEST_BIT (kills
, uid
))
237 set_livein_block (*use_p
, bb
);
240 /* Similarly for virtual uses. */
241 vuses
= VUSE_OPS (ann
);
242 for (i
= 0; i
< NUM_VUSES (vuses
); i
++)
244 tree
*use_p
= VUSE_OP_PTR (vuses
, i
);
246 if (prepare_operand_for_rename (use_p
, &uid
, true))
247 set_livein_block (*use_p
, bb
);
250 /* Note that virtual definitions are irrelevant for computing KILLS
251 because a VDEF does not constitute a killing definition of the
252 variable. However, the operand of a virtual definitions is a use
253 of the variable, so it may cause the variable to be considered
255 vdefs
= VDEF_OPS (ann
);
256 for (i
= 0; i
< NUM_VDEFS (vdefs
); i
++)
258 if (prepare_operand_for_rename (VDEF_OP_PTR (vdefs
, i
), &uid
, true))
260 /* If we do not already have an SSA_NAME for our destination,
261 then set the destination to the source. */
262 if (TREE_CODE (VDEF_RESULT (vdefs
, i
)) != SSA_NAME
)
263 VDEF_RESULT (vdefs
, i
) = VDEF_OP (vdefs
, i
);
265 set_livein_block (VDEF_OP (vdefs
, i
), bb
);
266 set_def_block (VDEF_RESULT (vdefs
, i
), bb
);
270 /* Now process the definition made by this statement. Mark the
271 variables in KILLS. */
272 defs
= DEF_OPS (ann
);
273 for (i
= 0; i
< NUM_DEFS (defs
); i
++)
275 tree
*def_p
= DEF_OP_PTR (defs
, i
);
277 if (prepare_operand_for_rename (def_p
, &uid
, false))
279 set_def_block (*def_p
, bb
);
280 SET_BIT (kills
, uid
);
286 /* Mark block BB as the definition site for variable VAR. */
289 set_def_block (tree var
, basic_block bb
)
291 struct def_blocks_d
*db_p
;
292 enum need_phi_state state
;
294 if (TREE_CODE (var
) == SSA_NAME
)
295 var
= SSA_NAME_VAR (var
);
297 state
= var_ann (var
)->need_phi_state
;
298 db_p
= get_def_blocks_for (var
);
300 /* Set the bit corresponding to the block where VAR is defined. */
301 bitmap_set_bit (db_p
->def_blocks
, bb
->index
);
303 /* Keep track of whether or not we may need to insert phi nodes.
305 If we are in the UNKNOWN state, then this is the first definition
306 of VAR. Additionally, we have not seen any uses of VAR yet, so
307 we do not need a phi node for this variable at this time (i.e.,
308 transition to NEED_PHI_STATE_NO).
310 If we are in any other state, then we either have multiple definitions
311 of this variable occurring in different blocks or we saw a use of the
312 variable which was not dominated by the block containing the
313 definition(s). In this case we may need a PHI node, so enter
314 state NEED_PHI_STATE_MAYBE. */
315 if (state
== NEED_PHI_STATE_UNKNOWN
)
316 var_ann (var
)->need_phi_state
= NEED_PHI_STATE_NO
;
318 var_ann (var
)->need_phi_state
= NEED_PHI_STATE_MAYBE
;
322 /* Mark block BB as having VAR live at the entry to BB. */
325 set_livein_block (tree var
, basic_block bb
)
327 struct def_blocks_d
*db_p
;
328 enum need_phi_state state
= var_ann (var
)->need_phi_state
;
330 db_p
= get_def_blocks_for (var
);
332 /* Set the bit corresponding to the block where VAR is live in. */
333 bitmap_set_bit (db_p
->livein_blocks
, bb
->index
);
335 /* Keep track of whether or not we may need to insert phi nodes.
337 If we reach here in NEED_PHI_STATE_NO, see if this use is dominated
338 by the single block containing the definition(s) of this variable. If
339 it is, then we remain in NEED_PHI_STATE_NO, otherwise we transition to
340 NEED_PHI_STATE_MAYBE. */
341 if (state
== NEED_PHI_STATE_NO
)
343 int def_block_index
= bitmap_first_set_bit (db_p
->def_blocks
);
345 if (def_block_index
== -1
346 || ! dominated_by_p (CDI_DOMINATORS
, bb
,
347 BASIC_BLOCK (def_block_index
)))
348 var_ann (var
)->need_phi_state
= NEED_PHI_STATE_MAYBE
;
351 var_ann (var
)->need_phi_state
= NEED_PHI_STATE_MAYBE
;
355 /* If the operand pointed to by OP_P needs to be renamed, then
357 1. If OP_P is used (rather than set), then strip away any SSA_NAME
358 wrapping the operand.
360 2. Set *UID_P to the underlying variable's uid.
364 Otherwise return false. */
367 prepare_operand_for_rename (tree
*op_p
, size_t *uid_p
, bool is_use
)
369 tree var
= (TREE_CODE (*op_p
) != SSA_NAME
) ? *op_p
: SSA_NAME_VAR (*op_p
);
370 *uid_p
= var_ann (var
)->uid
;
372 /* Ignore variables that don't need to be renamed. */
373 if (vars_to_rename
&& !bitmap_bit_p (vars_to_rename
, *uid_p
))
376 /* The variable needs to be renamed. If this is a use which already
377 has an SSA_NAME, then strip it off.
379 By not throwing away SSA_NAMEs on assignments, we avoid a lot of
380 useless churn of SSA_NAMEs without having to overly complicate the
382 if (TREE_CODE (*op_p
) == SSA_NAME
&& is_use
)
389 /* Helper for insert_phi_nodes. If VAR needs PHI nodes, insert them
390 at the dominance frontier (DFS) of blocks defining VAR. */
393 void insert_phi_nodes_1 (tree var
, bitmap
*dfs
, varray_type
*work_stack
)
395 var_ann_t ann
= var_ann (var
);
396 if (ann
->need_phi_state
!= NEED_PHI_STATE_NO
)
397 insert_phi_nodes_for (var
, dfs
, work_stack
);
401 /* Insert PHI nodes at the dominance frontier of blocks with variable
402 definitions. DFS contains the dominance frontier information for
403 the flowgraph. PHI nodes will only be inserted at the dominance
404 frontier of definition blocks for variables whose NEED_PHI_STATE
405 annotation is marked as ``maybe'' or ``unknown'' (computed by
409 insert_phi_nodes (bitmap
*dfs
)
412 varray_type work_stack
;
414 timevar_push (TV_TREE_INSERT_PHI_NODES
);
416 /* Array WORK_STACK is a stack of CFG blocks. Each block that contains
417 an assignment or PHI node will be pushed to this stack. */
418 VARRAY_BB_INIT (work_stack
, last_basic_block
, "work_stack");
420 /* Iterate over all variables in VARS_TO_RENAME. For each variable, add
421 to the work list all the blocks that have a definition for the
422 variable. PHI nodes will be added to the dominance frontier blocks of
423 each definition block. */
425 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename
, 0, i
,
426 insert_phi_nodes_1 (referenced_var (i
), dfs
, &work_stack
));
428 for (i
= 0; i
< num_referenced_vars
; i
++)
429 insert_phi_nodes_1 (referenced_var (i
), dfs
, &work_stack
);
431 timevar_pop (TV_TREE_INSERT_PHI_NODES
);
435 /* Perform a depth-first traversal of the dominator tree looking for
436 variables to rename. BB is the block where to start searching.
437 Renaming is a five step process:
439 1- Every definition made by PHI nodes at the start of the blocks is
440 registered as the current definition for the corresponding variable.
442 2- Every statement in BB is rewritten. USE and VUSE operands are
443 rewritten with their corresponding reaching definition. DEF and
444 VDEF targets are registered as new definitions.
446 3- All the PHI nodes in successor blocks of BB are visited. The
447 argument corresponding to BB is replaced with its current reaching
450 4- Recursively rewrite every dominator child block of BB.
452 5- Restore (in reverse order) the current reaching definition for every
453 new definition introduced in this block. This is done so that when
454 we return from the recursive call, all the current reaching
455 definitions are restored to the names that were valid in the
456 dominator parent of BB. */
458 /* Initialize the local stacks.
460 BLOCK_DEFS is used to save all the existing reaching definitions for
461 the new SSA names introduced in this block. Before registering a
462 new definition for a variable, the existing reaching definition is
463 pushed into this stack so that we can restore it in Step 5. */
466 rewrite_initialize_block_local_data (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
467 basic_block bb ATTRIBUTE_UNUSED
,
468 bool recycled ATTRIBUTE_UNUSED
)
470 #ifdef ENABLE_CHECKING
471 struct rewrite_block_data
*bd
472 = (struct rewrite_block_data
*)VARRAY_TOP_GENERIC_PTR (walk_data
->block_data_stack
);
474 /* We get cleared memory from the allocator, so if the memory is
475 not cleared, then we are re-using a previously allocated entry. In
476 that case, we can also re-use the underlying virtal arrays. Just
477 make sure we clear them before using them! */
478 if (recycled
&& bd
->block_defs
&& VARRAY_ACTIVE_SIZE (bd
->block_defs
) > 0)
484 /* SSA Rewriting Step 1. Initialization, create a block local stack
485 of reaching definitions for new SSA names produced in this block
486 (BLOCK_DEFS). Register new definitions for every PHI node in the
490 rewrite_initialize_block (struct dom_walk_data
*walk_data
, basic_block bb
)
493 struct rewrite_block_data
*bd
494 = (struct rewrite_block_data
*)VARRAY_TOP_GENERIC_PTR (walk_data
->block_data_stack
);
496 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
497 fprintf (dump_file
, "\n\nRenaming block #%d\n\n", bb
->index
);
499 /* Step 1. Register new definitions for every PHI node in the block.
500 Conceptually, all the PHI nodes are executed in parallel and each PHI
501 node introduces a new version for the associated variable. */
502 for (phi
= phi_nodes (bb
); phi
; phi
= TREE_CHAIN (phi
))
504 tree result
= PHI_RESULT (phi
);
506 register_new_def (result
, &bd
->block_defs
);
511 /* SSA Rewriting Step 3. Visit all the successor blocks of BB looking for
512 PHI nodes. For every PHI node found, add a new argument containing the
513 current reaching definition for the variable and the edge through which
514 that definition is reaching the PHI node. */
517 rewrite_add_phi_arguments (struct dom_walk_data
*walk_data ATTRIBUTE_UNUSED
,
522 for (e
= bb
->succ
; e
; e
= e
->succ_next
)
526 for (phi
= phi_nodes (e
->dest
); phi
; phi
= TREE_CHAIN (phi
))
530 /* If this PHI node has already been rewritten, then there is
531 nothing to do for this PHI or any following PHIs since we
532 always add new PHI nodes at the start of the PHI chain. */
533 if (PHI_REWRITTEN (phi
))
536 currdef
= get_reaching_def (SSA_NAME_VAR (PHI_RESULT (phi
)));
537 add_phi_arg (&phi
, currdef
, e
);
542 /* SSA Rewriting Step 5. Restore the current reaching definition for each
543 variable referenced in the block (in reverse order). */
546 rewrite_finalize_block (struct dom_walk_data
*walk_data
,
547 basic_block bb ATTRIBUTE_UNUSED
)
549 struct rewrite_block_data
*bd
550 = (struct rewrite_block_data
*)VARRAY_TOP_GENERIC_PTR (walk_data
->block_data_stack
);
552 /* Step 5. Restore the current reaching definition for each variable
553 referenced in the block (in reverse order). */
554 while (bd
->block_defs
&& VARRAY_ACTIVE_SIZE (bd
->block_defs
) > 0)
556 tree tmp
= VARRAY_TOP_TREE (bd
->block_defs
);
559 VARRAY_POP (bd
->block_defs
);
560 if (TREE_CODE (tmp
) == SSA_NAME
)
563 var
= SSA_NAME_VAR (saved_def
);
571 var_ann (var
)->current_def
= saved_def
;
576 /* Dump SSA information to FILE. */
579 dump_tree_ssa (FILE *file
)
583 = lang_hooks
.decl_printable_name (current_function_decl
, 2);
585 fprintf (file
, "SSA information for %s\n\n", funcname
);
589 dump_bb (bb
, file
, 0);
591 print_generic_stmt (file
, phi_nodes (bb
), dump_flags
);
592 fputs ("\n\n", file
);
597 /* Dump SSA information to stderr. */
600 debug_tree_ssa (void)
602 dump_tree_ssa (stderr
);
606 /* Dump SSA statistics on FILE. */
609 dump_tree_ssa_stats (FILE *file
)
611 fprintf (file
, "\nHash table statistics:\n");
613 fprintf (file
, " def_blocks: ");
614 htab_statistics (file
, def_blocks
);
616 fprintf (file
, "\n");
620 /* Dump SSA statistics on stderr. */
623 debug_tree_ssa_stats (void)
625 dump_tree_ssa_stats (stderr
);
629 /* Dump statistics for the hash table HTAB. */
632 htab_statistics (FILE *file
, htab_t htab
)
634 fprintf (file
, "size %ld, %ld elements, %f collision/search ratio\n",
635 (long) htab_size (htab
),
636 (long) htab_elements (htab
),
637 htab_collisions (htab
));
641 /* Insert PHI nodes for variable VAR using the dominance frontier
642 information given in DFS. */
645 insert_phi_nodes_for (tree var
, bitmap
*dfs
, varray_type
*work_stack
)
647 struct def_blocks_d
*def_map
;
648 bitmap phi_insertion_points
;
651 def_map
= find_def_blocks_for (var
);
655 phi_insertion_points
= BITMAP_XMALLOC ();
657 EXECUTE_IF_SET_IN_BITMAP (def_map
->def_blocks
, 0, bb_index
,
659 VARRAY_PUSH_BB (*work_stack
, BASIC_BLOCK (bb_index
));
662 /* Pop a block off the worklist, add every block that appears in
663 the original block's dfs that we have not already processed to
664 the worklist. Iterate until the worklist is empty. Blocks
665 which are added to the worklist are potential sites for
668 The iteration step could be done during PHI insertion just as
669 easily. We do it here for historical reasons -- we used to have
670 a heuristic which used the potential PHI insertion points to
671 determine if fully pruned or semi pruned SSA form was appropriate.
673 We now always use fully pruned SSA form. */
674 while (VARRAY_ACTIVE_SIZE (*work_stack
) > 0)
676 basic_block bb
= VARRAY_TOP_BB (*work_stack
);
677 int bb_index
= bb
->index
;
680 VARRAY_POP (*work_stack
);
682 EXECUTE_IF_AND_COMPL_IN_BITMAP (dfs
[bb_index
],
683 phi_insertion_points
,
686 basic_block bb
= BASIC_BLOCK (dfs_index
);
688 VARRAY_PUSH_BB (*work_stack
, bb
);
689 bitmap_set_bit (phi_insertion_points
, dfs_index
);
693 /* Now compute global livein for this variable. Note this modifies
694 def_map->livein_blocks. */
695 compute_global_livein (def_map
->livein_blocks
, def_map
->def_blocks
);
697 /* And insert the PHI nodes. */
698 EXECUTE_IF_AND_IN_BITMAP (phi_insertion_points
, def_map
->livein_blocks
,
701 create_phi_node (var
, BASIC_BLOCK (bb_index
));
704 BITMAP_XFREE (phi_insertion_points
);
707 /* SSA Rewriting Step 2. Rewrite every variable used in each statement in
708 the block with its immediate reaching definitions. Update the current
709 definition of a variable when a new real or virtual definition is found. */
712 rewrite_stmt (struct dom_walk_data
*walk_data
,
713 basic_block bb ATTRIBUTE_UNUSED
,
714 block_stmt_iterator si
)
723 struct rewrite_block_data
*bd
;
725 bd
= VARRAY_TOP_GENERIC_PTR (walk_data
->block_data_stack
);
727 stmt
= bsi_stmt (si
);
728 ann
= stmt_ann (stmt
);
730 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
732 fprintf (dump_file
, "Renaming statement ");
733 print_generic_stmt (dump_file
, stmt
, TDF_SLIM
);
734 fprintf (dump_file
, "\n");
737 #if defined ENABLE_CHECKING
738 /* We have just scanned the code for operands. No statement should
744 defs
= DEF_OPS (ann
);
745 uses
= USE_OPS (ann
);
746 vuses
= VUSE_OPS (ann
);
747 vdefs
= VDEF_OPS (ann
);
749 /* Step 1. Rewrite USES and VUSES in the statement. */
750 for (i
= 0; i
< NUM_USES (uses
); i
++)
751 rewrite_operand (USE_OP_PTR (uses
, i
));
753 /* Rewrite virtual uses in the statement. */
754 for (i
= 0; i
< NUM_VUSES (vuses
); i
++)
755 rewrite_operand (VUSE_OP_PTR (vuses
, i
));
757 /* Step 2. Register the statement's DEF and VDEF operands. */
758 for (i
= 0; i
< NUM_DEFS (defs
); i
++)
760 tree
*def_p
= DEF_OP_PTR (defs
, i
);
762 if (TREE_CODE (*def_p
) != SSA_NAME
)
763 *def_p
= make_ssa_name (*def_p
, stmt
);
765 /* FIXME: We shouldn't be registering new defs if the variable
766 doesn't need to be renamed. */
767 register_new_def (*def_p
, &bd
->block_defs
);
770 /* Register new virtual definitions made by the statement. */
771 for (i
= 0; i
< NUM_VDEFS (vdefs
); i
++)
773 rewrite_operand (VDEF_OP_PTR (vdefs
, i
));
775 if (TREE_CODE (VDEF_RESULT (vdefs
, i
)) != SSA_NAME
)
776 *VDEF_RESULT_PTR (vdefs
, i
)
777 = make_ssa_name (VDEF_RESULT (vdefs
, i
), stmt
);
779 /* FIXME: We shouldn't be registering new defs if the variable
780 doesn't need to be renamed. */
781 register_new_def (VDEF_RESULT (vdefs
, i
), &bd
->block_defs
);
786 /* Replace the operand pointed by OP_P with its immediate reaching
790 rewrite_operand (tree
*op_p
)
792 if (TREE_CODE (*op_p
) != SSA_NAME
)
793 *op_p
= get_reaching_def (*op_p
);
797 /* Register DEF (an SSA_NAME) to be a new definition for its underlying
798 variable (SSA_NAME_VAR (DEF)) and push VAR's current reaching definition
799 into the stack pointed by BLOCK_DEFS_P. */
802 register_new_def (tree def
, varray_type
*block_defs_p
)
804 tree var
= SSA_NAME_VAR (def
);
807 /* If this variable is set in a single basic block and all uses are
808 dominated by the set(s) in that single basic block, then there is
809 no reason to record anything for this variable in the block local
810 definition stacks. Doing so just wastes time and memory.
812 This is the same test to prune the set of variables which may
813 need PHI nodes. So we just use that information since it's already
814 computed and available for us to use. */
815 if (var_ann (var
)->need_phi_state
== NEED_PHI_STATE_NO
)
817 var_ann (var
)->current_def
= def
;
821 currdef
= var_ann (var
)->current_def
;
823 VARRAY_TREE_INIT (*block_defs_p
, 20, "block_defs");
825 /* Push the current reaching definition into *BLOCK_DEFS_P. This stack is
826 later used by the dominator tree callbacks to restore the reaching
827 definitions for all the variables defined in the block after a recursive
828 visit to all its immediately dominated blocks. If there is no current
829 reaching definition, then just record the underlying _DECL node. */
830 VARRAY_PUSH_TREE (*block_defs_p
, currdef
? currdef
: var
);
832 /* Set the current reaching definition for VAR to be DEF. */
833 var_ann (var
)->current_def
= def
;
837 /* Return the current definition for variable VAR. If none is found,
838 create a new SSA name to act as the zeroth definition for VAR. If VAR
839 is call clobbered and there exists a more recent definition of
840 GLOBAL_VAR, return the definition for GLOBAL_VAR. This means that VAR
841 has been clobbered by a function call since its last assignment. */
844 get_reaching_def (tree var
)
846 tree default_d
, currdef_var
;
848 /* Lookup the current reaching definition for VAR. */
849 default_d
= NULL_TREE
;
850 currdef_var
= var_ann (var
)->current_def
;
852 /* If there is no reaching definition for VAR, create and register a
853 default definition for it (if needed). */
854 if (currdef_var
== NULL_TREE
)
856 default_d
= default_def (var
);
857 if (default_d
== NULL_TREE
)
859 default_d
= make_ssa_name (var
, build_empty_stmt ());
860 set_default_def (var
, default_d
);
862 var_ann (var
)->current_def
= default_d
;
865 /* Return the current reaching definition for VAR, or the default
866 definition, if we had to create one. */
867 return (currdef_var
) ? currdef_var
: default_d
;
871 /* Hashing and equality functions for DEF_BLOCKS. */
874 def_blocks_hash (const void *p
)
876 return htab_hash_pointer
877 ((const void *)((const struct def_blocks_d
*)p
)->var
);
881 def_blocks_eq (const void *p1
, const void *p2
)
883 return ((const struct def_blocks_d
*)p1
)->var
884 == ((const struct def_blocks_d
*)p2
)->var
;
887 /* Free memory allocated by one entry in DEF_BLOCKS. */
890 def_blocks_free (void *p
)
892 struct def_blocks_d
*entry
= p
;
893 BITMAP_XFREE (entry
->def_blocks
);
894 BITMAP_XFREE (entry
->livein_blocks
);
899 /* Dump the DEF_BLOCKS hash table on stderr. */
902 debug_def_blocks (void)
904 htab_traverse (def_blocks
, debug_def_blocks_r
, NULL
);
907 /* Callback for htab_traverse to dump the DEF_BLOCKS hash table. */
910 debug_def_blocks_r (void **slot
, void *data ATTRIBUTE_UNUSED
)
913 struct def_blocks_d
*db_p
= (struct def_blocks_d
*) *slot
;
915 fprintf (stderr
, "VAR: ");
916 print_generic_expr (stderr
, db_p
->var
, dump_flags
);
917 fprintf (stderr
, ", DEF_BLOCKS: { ");
918 EXECUTE_IF_SET_IN_BITMAP (db_p
->def_blocks
, 0, i
,
919 fprintf (stderr
, "%ld ", i
));
920 fprintf (stderr
, "}");
921 fprintf (stderr
, ", LIVEIN_BLOCKS: { ");
922 EXECUTE_IF_SET_IN_BITMAP (db_p
->livein_blocks
, 0, i
,
923 fprintf (stderr
, "%ld ", i
));
924 fprintf (stderr
, "}\n");
930 /* Return the set of blocks where variable VAR is defined and the blocks
931 where VAR is live on entry (livein). Return NULL, if no entry is
932 found in DEF_BLOCKS. */
934 static inline struct def_blocks_d
*
935 find_def_blocks_for (tree var
)
937 struct def_blocks_d dm
;
939 return (struct def_blocks_d
*) htab_find (def_blocks
, &dm
);
943 /* Return the set of blocks where variable VAR is defined and the blocks
944 where VAR is live on entry (livein). If no entry is found in
945 DEF_BLOCKS, a new one is created and returned. */
947 static inline struct def_blocks_d
*
948 get_def_blocks_for (tree var
)
950 struct def_blocks_d db
, *db_p
;
954 slot
= htab_find_slot (def_blocks
, (void *) &db
, INSERT
);
957 db_p
= xmalloc (sizeof (*db_p
));
959 db_p
->def_blocks
= BITMAP_XMALLOC ();
960 db_p
->livein_blocks
= BITMAP_XMALLOC ();
961 *slot
= (void *) db_p
;
964 db_p
= (struct def_blocks_d
*) *slot
;
969 /* If a variable V in VARS_TO_RENAME is a pointer, the renaming
970 process will cause us to lose the name memory tags that may have
971 been associated with the various SSA_NAMEs of V. This means that
972 the variables aliased to those name tags also need to be renamed
975 FIXME 1- We should either have a better scheme for renaming
976 pointers that doesn't lose name tags or re-run alias
977 analysis to recover points-to information.
979 2- Currently we just invalidate *all* the name tags. This
980 should be more selective. */
983 invalidate_name_tags (bitmap vars_to_rename
)
986 bool rename_name_tags_p
;
988 rename_name_tags_p
= false;
989 EXECUTE_IF_SET_IN_BITMAP (vars_to_rename
, 0, i
,
990 if (POINTER_TYPE_P (TREE_TYPE (referenced_var (i
))))
992 rename_name_tags_p
= true;
996 if (rename_name_tags_p
)
997 for (i
= 0; i
< num_referenced_vars
; i
++)
999 var_ann_t ann
= var_ann (referenced_var (i
));
1001 if (ann
->mem_tag_kind
== NAME_TAG
)
1004 varray_type may_aliases
= ann
->may_aliases
;
1006 bitmap_set_bit (vars_to_rename
, ann
->uid
);
1007 if (ann
->may_aliases
)
1008 for (j
= 0; j
< VARRAY_ACTIVE_SIZE (may_aliases
); j
++)
1010 tree var
= VARRAY_TREE (may_aliases
, j
);
1011 bitmap_set_bit (vars_to_rename
, var_ann (var
)->uid
);
1018 /* Main entry point into the SSA builder. The renaming process
1019 proceeds in five main phases:
1021 1- If VARS_TO_RENAME has any entries, any existing PHI nodes for
1022 those variables are removed from the flow graph so that they can
1025 2- Compute dominance frontier and immediate dominators, needed to
1026 insert PHI nodes and rename the function in dominator tree
1029 3- Find and mark all the blocks that define variables
1032 4- Insert PHI nodes at dominance frontiers (insert_phi_nodes).
1034 5- Rename all the blocks (rewrite_initialize_block,
1035 rewrite_add_phi_arguments) and statements in the program
1038 Steps 3 and 5 are done using the dominator tree walker
1039 (walk_dominator_tree). */
1042 rewrite_into_ssa (void)
1046 struct dom_walk_data walk_data
;
1047 struct mark_def_sites_global_data mark_def_sites_global_data
;
1050 timevar_push (TV_TREE_SSA_OTHER
);
1052 /* Initialize the array of variables to rename. */
1053 if (vars_to_rename
!= NULL
)
1055 invalidate_name_tags (vars_to_rename
);
1057 /* Now remove all the existing PHI nodes (if any) for the variables
1058 that we are about to rename into SSA. */
1059 remove_all_phi_nodes_for (vars_to_rename
);
1062 /* Allocate memory for the DEF_BLOCKS hash table. */
1063 def_blocks
= htab_create (VARRAY_ACTIVE_SIZE (referenced_vars
),
1064 def_blocks_hash
, def_blocks_eq
, def_blocks_free
);
1066 /* Initialize dominance frontier and immediate dominator bitmaps.
1067 Also count the number of predecessors for each block. Doing so
1068 can save significant time during PHI insertion for large graphs. */
1069 dfs
= (bitmap
*) xmalloc (last_basic_block
* sizeof (bitmap
*));
1075 for (e
= bb
->pred
; e
; e
= e
->pred_next
)
1078 bb_ann (bb
)->num_preds
= count
;
1079 dfs
[bb
->index
] = BITMAP_XMALLOC ();
1082 for (i
= 0; i
< num_referenced_vars
; i
++)
1083 var_ann (referenced_var (i
))->current_def
= NULL
;
1085 /* Ensure that the dominance information is OK. */
1086 calculate_dominance_info (CDI_DOMINATORS
);
1088 /* Compute dominance frontiers. */
1089 compute_dominance_frontiers (dfs
);
1091 /* Setup callbacks for the generic dominator tree walker to find and
1092 mark definition sites. */
1093 walk_data
.walk_stmts_backward
= false;
1094 walk_data
.dom_direction
= CDI_DOMINATORS
;
1095 walk_data
.initialize_block_local_data
= NULL
;
1096 walk_data
.before_dom_children_before_stmts
= mark_def_sites_initialize_block
;
1097 walk_data
.before_dom_children_walk_stmts
= mark_def_sites
;
1098 walk_data
.before_dom_children_after_stmts
= NULL
;
1099 walk_data
.after_dom_children_before_stmts
= NULL
;
1100 walk_data
.after_dom_children_walk_stmts
= NULL
;
1101 walk_data
.after_dom_children_after_stmts
= NULL
;
1103 /* Notice that this bitmap is indexed using variable UIDs, so it must be
1104 large enough to accommodate all the variables referenced in the
1105 function, not just the ones we are renaming. */
1106 mark_def_sites_global_data
.kills
= sbitmap_alloc (num_referenced_vars
);
1107 walk_data
.global_data
= &mark_def_sites_global_data
;
1109 /* We do not have any local data. */
1110 walk_data
.block_local_data_size
= 0;
1112 /* Initialize the dominator walker. */
1113 init_walk_dominator_tree (&walk_data
);
1115 /* Recursively walk the dominator tree. */
1116 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
1118 /* Finalize the dominator walker. */
1119 fini_walk_dominator_tree (&walk_data
);
1121 /* We no longer need this bitmap, clear and free it. */
1122 sbitmap_free (mark_def_sites_global_data
.kills
);
1124 /* Insert PHI nodes at dominance frontiers of definition blocks. */
1125 insert_phi_nodes (dfs
);
1127 /* Rewrite all the basic blocks in the program. */
1128 timevar_push (TV_TREE_SSA_REWRITE_BLOCKS
);
1130 /* Setup callbacks for the generic dominator tree walker. */
1131 walk_data
.walk_stmts_backward
= false;
1132 walk_data
.dom_direction
= CDI_DOMINATORS
;
1133 walk_data
.initialize_block_local_data
= rewrite_initialize_block_local_data
;
1134 walk_data
.before_dom_children_before_stmts
= rewrite_initialize_block
;
1135 walk_data
.before_dom_children_walk_stmts
= rewrite_stmt
;
1136 walk_data
.before_dom_children_after_stmts
= rewrite_add_phi_arguments
;
1137 walk_data
.after_dom_children_before_stmts
= NULL
;
1138 walk_data
.after_dom_children_walk_stmts
= NULL
;
1139 walk_data
.after_dom_children_after_stmts
= rewrite_finalize_block
;
1140 walk_data
.global_data
= NULL
;
1141 walk_data
.block_local_data_size
= sizeof (struct rewrite_block_data
);
1143 /* Initialize the dominator walker. */
1144 init_walk_dominator_tree (&walk_data
);
1146 /* Recursively walk the dominator tree rewriting each statement in
1147 each basic block. */
1148 walk_dominator_tree (&walk_data
, ENTRY_BLOCK_PTR
);
1150 /* Finalize the dominator walker. */
1151 fini_walk_dominator_tree (&walk_data
);
1153 timevar_pop (TV_TREE_SSA_REWRITE_BLOCKS
);
1155 /* Debugging dumps. */
1156 if (dump_file
&& (dump_flags
& TDF_STATS
))
1158 dump_dfa_stats (dump_file
);
1159 dump_tree_ssa_stats (dump_file
);
1162 /* Free allocated memory. */
1164 BITMAP_XFREE (dfs
[bb
->index
]);
1167 htab_delete (def_blocks
);
1169 timevar_pop (TV_TREE_SSA_OTHER
);
1172 struct tree_opt_pass pass_build_ssa
=
1176 rewrite_into_ssa
, /* execute */
1179 0, /* static_pass_number */
1181 PROP_cfg
| PROP_referenced_vars
, /* properties_required */
1182 PROP_ssa
, /* properties_provided */
1183 0, /* properties_destroyed */
1184 0, /* todo_flags_start */
1185 TODO_dump_func
| TODO_verify_ssa
/* todo_flags_finish */