1 /* Dead store elimination
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
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 3, 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 COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
29 #include "basic-block.h"
31 #include "diagnostic.h"
32 #include "tree-flow.h"
33 #include "tree-pass.h"
34 #include "tree-dump.h"
37 #include "langhooks.h"
39 /* This file implements dead store elimination.
41 A dead store is a store into a memory location which will later be
42 overwritten by another store without any intervening loads. In this
43 case the earlier store can be deleted.
45 In our SSA + virtual operand world we use immediate uses of virtual
46 operands to detect dead stores. If a store's virtual definition
47 is used precisely once by a later store to the same location which
48 post dominates the first store, then the first store is dead.
50 The single use of the store's virtual definition ensures that
51 there are no intervening aliased loads and the requirement that
52 the second load post dominate the first ensures that if the earlier
53 store executes, then the later stores will execute before the function
56 It may help to think of this as first moving the earlier store to
57 the point immediately before the later store. Again, the single
58 use of the virtual definition and the post-dominance relationship
59 ensure that such movement would be safe. Clearly if there are
60 back to back stores, then the second is redundant.
62 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
63 may also help in understanding this code since it discusses the
64 relationship between dead store and redundant load elimination. In
65 fact, they are the same transformation applied to different views of
69 struct dse_global_data
71 /* This is the global bitmap for store statements.
73 Each statement has a unique ID. When we encounter a store statement
74 that we want to record, set the bit corresponding to the statement's
75 unique ID in this bitmap. */
79 /* We allocate a bitmap-per-block for stores which are encountered
80 during the scan of that block. This allows us to restore the
81 global bitmap of stores when we finish processing a block. */
82 struct dse_block_local_data
87 /* Basic blocks of the potentially dead store and the following
88 store, for memory_address_same. */
89 struct address_walk_data
91 basic_block store1_bb
, store2_bb
;
94 static bool gate_dse (void);
95 static unsigned int tree_ssa_dse (void);
96 static void dse_initialize_block_local_data (struct dom_walk_data
*,
99 static void dse_optimize_stmt (struct dom_walk_data
*,
101 gimple_stmt_iterator
);
102 static void dse_record_phis (struct dom_walk_data
*, basic_block
);
103 static void dse_finalize_block (struct dom_walk_data
*, basic_block
);
104 static void record_voperand_set (bitmap
, bitmap
*, unsigned int);
106 /* Returns uid of statement STMT. */
109 get_stmt_uid (gimple stmt
)
111 if (gimple_code (stmt
) == GIMPLE_PHI
)
112 return SSA_NAME_VERSION (gimple_phi_result (stmt
))
113 + gimple_stmt_max_uid (cfun
);
115 return gimple_uid (stmt
);
118 /* Set bit UID in bitmaps GLOBAL and *LOCAL, creating *LOCAL as needed. */
121 record_voperand_set (bitmap global
, bitmap
*local
, unsigned int uid
)
123 /* Lazily allocate the bitmap. Note that we do not get a notification
124 when the block local data structures die, so we allocate the local
125 bitmap backed by the GC system. */
127 *local
= BITMAP_GGC_ALLOC ();
129 /* Set the bit in the local and global bitmaps. */
130 bitmap_set_bit (*local
, uid
);
131 bitmap_set_bit (global
, uid
);
134 /* Initialize block local data structures. */
137 dse_initialize_block_local_data (struct dom_walk_data
*walk_data
,
138 basic_block bb ATTRIBUTE_UNUSED
,
141 struct dse_block_local_data
*bd
142 = (struct dse_block_local_data
*)
143 VEC_last (void_p
, walk_data
->block_data_stack
);
145 /* If we are given a recycled block local data structure, ensure any
146 bitmap associated with the block is cleared. */
150 bitmap_clear (bd
->stores
);
154 /* Helper function for memory_address_same via walk_tree. Returns
155 non-NULL if it finds an SSA_NAME which is part of the address,
156 such that the definition of the SSA_NAME post-dominates the store
157 we want to delete but not the store that we believe makes it
158 redundant. This indicates that the address may change between
162 memory_ssa_name_same (tree
*expr_p
, int *walk_subtrees ATTRIBUTE_UNUSED
,
165 struct address_walk_data
*walk_data
= (struct address_walk_data
*) data
;
170 if (TREE_CODE (expr
) != SSA_NAME
)
173 /* If we've found a default definition, then there's no problem. Both
174 stores will post-dominate it. And def_bb will be NULL. */
175 if (SSA_NAME_IS_DEFAULT_DEF (expr
))
178 def_stmt
= SSA_NAME_DEF_STMT (expr
);
179 def_bb
= gimple_bb (def_stmt
);
181 /* DEF_STMT must dominate both stores. So if it is in the same
182 basic block as one, it does not post-dominate that store. */
183 if (walk_data
->store1_bb
!= def_bb
184 && dominated_by_p (CDI_POST_DOMINATORS
, walk_data
->store1_bb
, def_bb
))
186 if (walk_data
->store2_bb
== def_bb
187 || !dominated_by_p (CDI_POST_DOMINATORS
, walk_data
->store2_bb
,
189 /* Return non-NULL to stop the walk. */
196 /* Return TRUE if the destination memory address in STORE1 and STORE2
197 might be modified after STORE1, before control reaches STORE2. */
200 memory_address_same (gimple store1
, gimple store2
)
202 struct address_walk_data walk_data
;
204 walk_data
.store1_bb
= gimple_bb (store1
);
205 walk_data
.store2_bb
= gimple_bb (store2
);
207 return (walk_tree (gimple_assign_lhs_ptr (store1
), memory_ssa_name_same
,
212 /* Return true if there is a stmt that kills the lhs of STMT and is in the
213 virtual def-use chain of STMT without a use in between the kill and STMT.
214 Returns false if no such stmt is found.
215 *FIRST_USE_P is set to the first use of the single virtual def of
216 STMT. *USE_P is set to the vop killed by *USE_STMT. */
219 get_kill_of_stmt_lhs (gimple stmt
,
220 use_operand_p
* first_use_p
,
221 use_operand_p
* use_p
, gimple
* use_stmt
)
225 gcc_assert (is_gimple_assign (stmt
));
227 lhs
= gimple_assign_lhs (stmt
);
229 /* We now walk the chain of single uses of the single VDEFs.
230 We succeeded finding a kill if the lhs of the use stmt is
231 equal to the original lhs. We can keep walking to the next
232 use if there are no possible uses of the original lhs in
239 /* The stmt must have a single VDEF. */
240 def_p
= SINGLE_SSA_DEF_OPERAND (stmt
, SSA_OP_VDEF
);
241 if (def_p
== NULL_DEF_OPERAND_P
)
244 /* Get the single immediate use of the def. */
245 if (!single_imm_use (DEF_FROM_PTR (def_p
), first_use_p
, &stmt
))
249 /* If there are possible hidden uses, give up. */
250 if (!gimple_assign_single_p (stmt
)
251 || (TREE_CODE (gimple_assign_rhs1 (stmt
)) != SSA_NAME
252 && !is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
255 /* If the use stmts lhs matches the original lhs we have
256 found the kill, otherwise continue walking. */
257 use_lhs
= gimple_assign_lhs (stmt
);
258 if (operand_equal_p (use_lhs
, lhs
, 0))
267 /* A helper of dse_optimize_stmt.
268 Given a GIMPLE_ASSIGN in STMT, check that each VDEF has one
269 use, and that one use is another VDEF clobbering the first one.
271 Return TRUE if the above conditions are met, otherwise FALSE. */
274 dse_possible_dead_store_p (gimple stmt
,
275 use_operand_p
*first_use_p
,
276 use_operand_p
*use_p
,
278 struct dse_global_data
*dse_gd
,
279 struct dse_block_local_data
*bd
)
285 tree defvar
= NULL_TREE
;
286 tree prev_defvar
= NULL_TREE
;
289 /* We want to verify that each virtual definition in STMT has
290 precisely one use and that all the virtual definitions are
291 used by the same single statement. When complete, we
292 want USE_STMT to refer to the one statement which uses
293 all of the virtual definitions from STMT. */
295 FOR_EACH_SSA_VDEF_OPERAND (var1
, vv
, stmt
, op_iter
)
297 defvar
= DEF_FROM_PTR (var1
);
299 /* If this virtual def does not have precisely one use, then
300 we will not be able to eliminate STMT. */
301 if (!has_single_use (defvar
))
307 /* Get the one and only immediate use of DEFVAR. */
308 single_imm_use (defvar
, use_p
, &temp
);
309 gcc_assert (*use_p
!= NULL_USE_OPERAND_P
);
310 *first_use_p
= *use_p
;
312 /* ??? If we hit a GIMPLE_PHI we could skip to the PHI_RESULT uses.
313 Don't bother to do that for now. */
314 if (gimple_code (temp
) == GIMPLE_PHI
)
320 /* In the case of memory partitions, we may get:
322 # MPT.764_162 = VDEF <MPT.764_161(D)>
324 # MPT.764_167 = VDEF <MPT.764_162>
327 So we must make sure we're talking about the same LHS.
329 if (is_gimple_assign (temp
))
331 tree base1
= get_base_address (gimple_assign_lhs (stmt
));
332 tree base2
= get_base_address (gimple_assign_lhs (temp
));
334 while (base1
&& INDIRECT_REF_P (base1
))
335 base1
= TREE_OPERAND (base1
, 0);
336 while (base2
&& INDIRECT_REF_P (base2
))
337 base2
= TREE_OPERAND (base2
, 0);
346 /* If the immediate use of DEF_VAR is not the same as the
347 previously find immediate uses, then we will not be able
348 to eliminate STMT. */
349 if (*use_stmt
== NULL
)
352 prev_defvar
= defvar
;
354 else if (temp
!= *use_stmt
)
363 record_voperand_set (dse_gd
->stores
, &bd
->stores
, gimple_uid (stmt
));
371 /* Attempt to eliminate dead stores in the statement referenced by BSI.
373 A dead store is a store into a memory location which will later be
374 overwritten by another store without any intervening loads. In this
375 case the earlier store can be deleted.
377 In our SSA + virtual operand world we use immediate uses of virtual
378 operands to detect dead stores. If a store's virtual definition
379 is used precisely once by a later store to the same location which
380 post dominates the first store, then the first store is dead. */
383 dse_optimize_stmt (struct dom_walk_data
*walk_data
,
384 basic_block bb ATTRIBUTE_UNUSED
,
385 gimple_stmt_iterator gsi
)
387 struct dse_block_local_data
*bd
388 = (struct dse_block_local_data
*)
389 VEC_last (void_p
, walk_data
->block_data_stack
);
390 struct dse_global_data
*dse_gd
391 = (struct dse_global_data
*) walk_data
->global_data
;
392 gimple stmt
= gsi_stmt (gsi
);
394 /* If this statement has no virtual defs, then there is nothing
396 if (ZERO_SSA_OPERANDS (stmt
, SSA_OP_VDEF
))
399 /* We know we have virtual definitions. If this is a GIMPLE_ASSIGN
400 that's not also a function call, then record it into our table. */
401 if (is_gimple_call (stmt
) && gimple_call_fndecl (stmt
))
404 if (gimple_has_volatile_ops (stmt
))
407 if (is_gimple_assign (stmt
))
409 use_operand_p first_use_p
= NULL_USE_OPERAND_P
;
410 use_operand_p use_p
= NULL
;
413 if (!dse_possible_dead_store_p (stmt
, &first_use_p
, &use_p
, &use_stmt
,
417 /* If we have precisely one immediate use at this point, then we may
418 have found redundant store. Make sure that the stores are to
419 the same memory location. This includes checking that any
420 SSA-form variables in the address will have the same values. */
421 if (use_p
!= NULL_USE_OPERAND_P
422 && bitmap_bit_p (dse_gd
->stores
, get_stmt_uid (use_stmt
))
423 && !operand_equal_p (gimple_assign_lhs (stmt
),
424 gimple_assign_lhs (use_stmt
), 0)
425 && memory_address_same (stmt
, use_stmt
))
427 /* If we have precisely one immediate use at this point, but
428 the stores are not to the same memory location then walk the
429 virtual def-use chain to get the stmt which stores to that same
431 if (!get_kill_of_stmt_lhs (stmt
, &first_use_p
, &use_p
, &use_stmt
))
433 record_voperand_set (dse_gd
->stores
, &bd
->stores
,
439 /* If we have precisely one immediate use at this point and the
440 stores are to the same memory location or there is a chain of
441 virtual uses from stmt and the stmt which stores to that same
442 memory location, then we may have found redundant store. */
443 if (use_p
!= NULL_USE_OPERAND_P
444 && bitmap_bit_p (dse_gd
->stores
, get_stmt_uid (use_stmt
))
445 && operand_equal_p (gimple_assign_lhs (stmt
),
446 gimple_assign_lhs (use_stmt
), 0)
447 && memory_address_same (stmt
, use_stmt
))
454 /* If use_stmt is or might be a nop assignment, e.g. for
455 struct { ... } S a, b, *p; ...
458 b = a; b = *p; where p might be &b,
460 *p = a; *p = b; where p might be &b,
462 *p = *u; *p = *v; where p might be v, then USE_STMT
463 acts as a use as well as definition, so store in STMT
465 if (gimple_loaded_syms (use_stmt
)
466 && bitmap_intersect_p (gimple_loaded_syms (use_stmt
),
467 gimple_stored_syms (use_stmt
)))
469 record_voperand_set (dse_gd
->stores
, &bd
->stores
,
474 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
476 fprintf (dump_file
, " Deleted dead store '");
477 print_gimple_stmt (dump_file
, gsi_stmt (gsi
), dump_flags
, 0);
478 fprintf (dump_file
, "'\n");
481 /* Then we need to fix the operand of the consuming stmt. */
482 stmt_lhs
= USE_FROM_PTR (first_use_p
);
483 FOR_EACH_SSA_VDEF_OPERAND (var1
, vv
, stmt
, op_iter
)
488 single_imm_use (DEF_FROM_PTR (var1
), &use_p
, &temp
);
489 gcc_assert (VUSE_VECT_NUM_ELEM (*vv
) == 1);
490 usevar
= VUSE_ELEMENT_VAR (*vv
, 0);
491 SET_USE (use_p
, usevar
);
493 /* Make sure we propagate the ABNORMAL bit setting. */
494 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (stmt_lhs
))
495 SSA_NAME_OCCURS_IN_ABNORMAL_PHI (usevar
) = 1;
498 /* Remove the dead store. */
499 gsi_remove (&gsi
, true);
501 /* And release any SSA_NAMEs set in this statement back to the
506 record_voperand_set (dse_gd
->stores
, &bd
->stores
, gimple_uid (stmt
));
510 /* Record that we have seen the PHIs at the start of BB which correspond
511 to virtual operands. */
513 dse_record_phis (struct dom_walk_data
*walk_data
, basic_block bb
)
515 struct dse_block_local_data
*bd
516 = (struct dse_block_local_data
*)
517 VEC_last (void_p
, walk_data
->block_data_stack
);
518 struct dse_global_data
*dse_gd
519 = (struct dse_global_data
*) walk_data
->global_data
;
521 gimple_stmt_iterator gsi
;
523 for (gsi
= gsi_start_phis (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
525 phi
= gsi_stmt (gsi
);
526 if (!is_gimple_reg (gimple_phi_result (phi
)))
527 record_voperand_set (dse_gd
->stores
, &bd
->stores
, get_stmt_uid (phi
));
532 dse_finalize_block (struct dom_walk_data
*walk_data
,
533 basic_block bb ATTRIBUTE_UNUSED
)
535 struct dse_block_local_data
*bd
536 = (struct dse_block_local_data
*)
537 VEC_last (void_p
, walk_data
->block_data_stack
);
538 struct dse_global_data
*dse_gd
539 = (struct dse_global_data
*) walk_data
->global_data
;
540 bitmap stores
= dse_gd
->stores
;
544 /* Unwind the stores noted in this basic block. */
546 EXECUTE_IF_SET_IN_BITMAP (bd
->stores
, 0, i
, bi
)
548 bitmap_clear_bit (stores
, i
);
552 /* Main entry point. */
557 struct dom_walk_data walk_data
;
558 struct dse_global_data dse_gd
;
560 renumber_gimple_stmt_uids ();
562 /* We might consider making this a property of each pass so that it
563 can be [re]computed on an as-needed basis. Particularly since
564 this pass could be seen as an extension of DCE which needs post
566 calculate_dominance_info (CDI_POST_DOMINATORS
);
568 /* Dead store elimination is fundamentally a walk of the post-dominator
569 tree and a backwards walk of statements within each block. */
570 walk_data
.walk_stmts_backward
= true;
571 walk_data
.dom_direction
= CDI_POST_DOMINATORS
;
572 walk_data
.initialize_block_local_data
= dse_initialize_block_local_data
;
573 walk_data
.before_dom_children_before_stmts
= NULL
;
574 walk_data
.before_dom_children_walk_stmts
= dse_optimize_stmt
;
575 walk_data
.before_dom_children_after_stmts
= dse_record_phis
;
576 walk_data
.after_dom_children_before_stmts
= NULL
;
577 walk_data
.after_dom_children_walk_stmts
= NULL
;
578 walk_data
.after_dom_children_after_stmts
= dse_finalize_block
;
579 walk_data
.interesting_blocks
= NULL
;
581 walk_data
.block_local_data_size
= sizeof (struct dse_block_local_data
);
583 /* This is the main hash table for the dead store elimination pass. */
584 dse_gd
.stores
= BITMAP_ALLOC (NULL
);
585 walk_data
.global_data
= &dse_gd
;
587 /* Initialize the dominator walker. */
588 init_walk_dominator_tree (&walk_data
);
590 /* Recursively walk the dominator tree. */
591 walk_dominator_tree (&walk_data
, EXIT_BLOCK_PTR
);
593 /* Finalize the dominator walker. */
594 fini_walk_dominator_tree (&walk_data
);
596 /* Release the main bitmap. */
597 BITMAP_FREE (dse_gd
.stores
);
599 /* For now, just wipe the post-dominator information. */
600 free_dominance_info (CDI_POST_DOMINATORS
);
607 return flag_tree_dse
!= 0;
610 struct gimple_opt_pass pass_dse
=
616 tree_ssa_dse
, /* execute */
619 0, /* static_pass_number */
620 TV_TREE_DSE
, /* tv_id */
623 | PROP_alias
, /* properties_required */
624 0, /* properties_provided */
625 0, /* properties_destroyed */
626 0, /* todo_flags_start */
629 | TODO_verify_ssa
/* todo_flags_finish */
633 /* A very simple dead store pass eliminating write only local variables.
634 The pass does not require alias information and thus can be run before
635 inlining to quickly eliminate artifacts of some common C++ constructs. */
638 execute_simple_dse (void)
640 gimple_stmt_iterator gsi
;
642 bitmap variables_loaded
= BITMAP_ALLOC (NULL
);
643 unsigned int todo
= 0;
645 /* Collect into VARIABLES LOADED all variables that are read in function
648 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
); gsi_next (&gsi
))
650 if (gimple_loaded_syms (gsi_stmt (gsi
)))
651 bitmap_ior_into (variables_loaded
,
652 gimple_loaded_syms (gsi_stmt (gsi
)));
654 /* Look for statements writing into the write only variables.
655 And try to remove them. */
658 for (gsi
= gsi_start_bb (bb
); !gsi_end_p (gsi
);)
660 gimple stmt
= gsi_stmt (gsi
);
662 bool removed
= false;
666 if (is_gimple_assign (stmt
)
667 && AGGREGATE_TYPE_P (TREE_TYPE (gimple_assign_lhs (stmt
)))
668 && (size
= lang_hooks
.expr_size (gimple_assign_lhs (stmt
)))
669 && integer_zerop (size
))
671 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
673 fprintf (dump_file
, " Deleted zero-sized store '");
674 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
675 fprintf (dump_file
, "'\n");
678 gsi_remove (&gsi
, true);
679 todo
|= TODO_cleanup_cfg
;
681 else if (gimple_stored_syms (stmt
)
682 && !bitmap_empty_p (gimple_stored_syms (stmt
))
683 && (is_gimple_assign (stmt
)
684 || (is_gimple_call (stmt
)
685 && gimple_call_lhs (stmt
)))
686 && !bitmap_intersect_p (gimple_stored_syms (stmt
),
693 /* See if STMT only stores to write-only variables and
694 verify that there are no volatile operands. tree-ssa-operands
695 sets has_volatile_ops flag for all statements involving
696 reads and writes when aliases are not built to prevent passes
697 from removing them as dead. The flag thus has no use for us
698 and we need to look into all operands. */
700 EXECUTE_IF_SET_IN_BITMAP (gimple_stored_syms (stmt
), 0, i
, bi
)
702 tree var
= referenced_var_lookup (i
);
703 if (TREE_ADDRESSABLE (var
)
704 || is_global_var (var
)
705 || TREE_THIS_VOLATILE (var
))
709 if (dead
&& gimple_loaded_syms (stmt
))
710 EXECUTE_IF_SET_IN_BITMAP (gimple_loaded_syms (stmt
), 0, i
, bi
)
711 if (TREE_THIS_VOLATILE (referenced_var_lookup (i
)))
715 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_ALL_OPERANDS
)
716 if (TREE_THIS_VOLATILE (op
))
719 /* Look for possible occurrence var = indirect_ref (...) where
720 indirect_ref itself is volatile. */
722 if (dead
&& is_gimple_assign (stmt
)
723 && TREE_THIS_VOLATILE (gimple_assign_rhs1 (stmt
)))
728 /* When LHS of var = call (); is dead, simplify it into
729 call (); saving one operand. */
730 if (is_gimple_call (stmt
)
731 && gimple_has_side_effects (stmt
))
733 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
735 fprintf (dump_file
, "Deleted LHS of call: ");
736 print_gimple_stmt (dump_file
, stmt
, 0, TDF_SLIM
);
737 fprintf (dump_file
, "\n");
739 push_stmt_changes (gsi_stmt_ptr (&gsi
));
740 gimple_call_set_lhs (stmt
, NULL
);
741 pop_stmt_changes (gsi_stmt_ptr (&gsi
));
745 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
747 fprintf (dump_file
, " Deleted dead store '");
748 print_gimple_stmt (dump_file
, stmt
, 0, dump_flags
);
749 fprintf (dump_file
, "'\n");
752 gsi_remove (&gsi
, true);
753 todo
|= TODO_cleanup_cfg
;
755 todo
|= TODO_remove_unused_locals
| TODO_ggc_collect
;
761 BITMAP_FREE (variables_loaded
);
765 struct gimple_opt_pass pass_simple_dse
=
771 execute_simple_dse
, /* execute */
774 0, /* static_pass_number */
776 PROP_ssa
, /* properties_required */
777 0, /* properties_provided */
778 0, /* properties_destroyed */
779 0, /* todo_flags_start */
780 TODO_dump_func
/* todo_flags_finish */