1 /* Dead store elimination
2 Copyright (C) 2004-2017 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3, or (at your option)
11 GCC is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
22 #include "coretypes.h"
27 #include "tree-pass.h"
29 #include "gimple-pretty-print.h"
30 #include "fold-const.h"
31 #include "gimple-iterator.h"
35 #include "tree-cfgcleanup.h"
38 /* This file implements dead store elimination.
40 A dead store is a store into a memory location which will later be
41 overwritten by another store without any intervening loads. In this
42 case the earlier store can be deleted.
44 In our SSA + virtual operand world we use immediate uses of virtual
45 operands to detect dead stores. If a store's virtual definition
46 is used precisely once by a later store to the same location which
47 post dominates the first store, then the first store is dead.
49 The single use of the store's virtual definition ensures that
50 there are no intervening aliased loads and the requirement that
51 the second load post dominate the first ensures that if the earlier
52 store executes, then the later stores will execute before the function
55 It may help to think of this as first moving the earlier store to
56 the point immediately before the later store. Again, the single
57 use of the virtual definition and the post-dominance relationship
58 ensure that such movement would be safe. Clearly if there are
59 back to back stores, then the second is redundant.
61 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
62 may also help in understanding this code since it discusses the
63 relationship between dead store and redundant load elimination. In
64 fact, they are the same transformation applied to different views of
68 /* Bitmap of blocks that have had EH statements cleaned. We should
69 remove their dead edges eventually. */
70 static bitmap need_eh_cleanup
;
72 /* Return value from dse_classify_store */
76 DSE_STORE_MAYBE_PARTIAL_DEAD
,
80 /* STMT is a statement that may write into memory. Analyze it and
81 initialize WRITE to describe how STMT affects memory.
83 Return TRUE if the the statement was analyzed, FALSE otherwise.
85 It is always safe to return FALSE. But typically better optimziation
86 can be achieved by analyzing more statements. */
89 initialize_ao_ref_for_dse (gimple
*stmt
, ao_ref
*write
)
91 /* It's advantageous to handle certain mem* functions. */
92 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
94 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt
)))
97 case BUILT_IN_MEMMOVE
:
100 tree size
= NULL_TREE
;
101 if (gimple_call_num_args (stmt
) == 3)
102 size
= gimple_call_arg (stmt
, 2);
103 tree ptr
= gimple_call_arg (stmt
, 0);
104 ao_ref_init_from_ptr_and_size (write
, ptr
, size
);
111 else if (is_gimple_assign (stmt
))
113 ao_ref_init (write
, gimple_assign_lhs (stmt
));
119 /* Given REF from the the alias oracle, return TRUE if it is a valid
120 memory reference for dead store elimination, false otherwise.
122 In particular, the reference must have a known base, known maximum
123 size, start at a byte offset and have a size that is one or more
127 valid_ao_ref_for_dse (ao_ref
*ref
)
129 return (ao_ref_base (ref
)
130 && ref
->max_size
!= -1
131 && (ref
->offset
% BITS_PER_UNIT
) == 0
132 && (ref
->size
% BITS_PER_UNIT
) == 0
133 && (ref
->size
!= -1));
136 /* Normalize COPY (an ao_ref) relative to REF. Essentially when we are
137 done COPY will only refer bytes found within REF.
139 We have already verified that COPY intersects at least one
143 normalize_ref (ao_ref
*copy
, ao_ref
*ref
)
145 /* If COPY starts before REF, then reset the beginning of
146 COPY to match REF and decrease the size of COPY by the
147 number of bytes removed from COPY. */
148 if (copy
->offset
< ref
->offset
)
150 copy
->size
-= (ref
->offset
- copy
->offset
);
151 copy
->offset
= ref
->offset
;
154 /* If COPY extends beyond REF, chop off its size appropriately. */
155 if (copy
->offset
+ copy
->size
> ref
->offset
+ ref
->size
)
156 copy
->size
-= (copy
->offset
+ copy
->size
- (ref
->offset
+ ref
->size
));
159 /* Clear any bytes written by STMT from the bitmap LIVE_BYTES. The base
160 address written by STMT must match the one found in REF, which must
161 have its base address previously initialized.
163 This routine must be conservative. If we don't know the offset or
164 actual size written, assume nothing was written. */
167 clear_bytes_written_by (sbitmap live_bytes
, gimple
*stmt
, ao_ref
*ref
)
170 if (!initialize_ao_ref_for_dse (stmt
, &write
))
173 /* Verify we have the same base memory address, the write
174 has a known size and overlaps with REF. */
175 if (valid_ao_ref_for_dse (&write
)
176 && write
.base
== ref
->base
177 && write
.size
== write
.max_size
178 && ((write
.offset
< ref
->offset
179 && write
.offset
+ write
.size
> ref
->offset
)
180 || (write
.offset
>= ref
->offset
181 && write
.offset
< ref
->offset
+ ref
->size
)))
183 normalize_ref (&write
, ref
);
184 bitmap_clear_range (live_bytes
,
185 (write
.offset
- ref
->offset
) / BITS_PER_UNIT
,
186 write
.size
/ BITS_PER_UNIT
);
190 /* REF is a memory write. Extract relevant information from it and
191 initialize the LIVE_BYTES bitmap. If successful, return TRUE.
192 Otherwise return FALSE. */
195 setup_live_bytes_from_ref (ao_ref
*ref
, sbitmap live_bytes
)
197 if (valid_ao_ref_for_dse (ref
)
198 && (ref
->size
/ BITS_PER_UNIT
199 <= PARAM_VALUE (PARAM_DSE_MAX_OBJECT_SIZE
)))
201 bitmap_clear (live_bytes
);
202 bitmap_set_range (live_bytes
, 0, ref
->size
/ BITS_PER_UNIT
);
208 /* Compute the number of elements that we can trim from the head and
209 tail of ORIG resulting in a bitmap that is a superset of LIVE.
211 Store the number of elements trimmed from the head and tail in
212 TRIM_HEAD and TRIM_TAIL. */
215 compute_trims (ao_ref
*ref
, sbitmap live
, int *trim_head
, int *trim_tail
)
217 /* We use sbitmaps biased such that ref->offset is bit zero and the bitmap
218 extends through ref->size. So we know that in the original bitmap
219 bits 0..ref->size were true. We don't actually need the bitmap, just
220 the REF to compute the trims. */
222 /* Now identify how much, if any of the tail we can chop off. */
224 int last_orig
= (ref
->size
/ BITS_PER_UNIT
) - 1;
225 int last_live
= bitmap_last_set_bit (live
);
226 *trim_tail
= (last_orig
- last_live
) & ~0x1;
228 /* Identify how much, if any of the head we can chop off. */
230 int first_live
= bitmap_first_set_bit (live
);
231 *trim_head
= (first_live
- first_orig
) & ~0x1;
234 /* STMT initializes an object from COMPLEX_CST where one or more of the
235 bytes written may be dead stores. REF is a representation of the
236 memory written. LIVE is the bitmap of stores that are actually live.
238 Attempt to rewrite STMT so that only the real or imaginary part of
239 the object is actually stored. */
242 maybe_trim_complex_store (ao_ref
*ref
, sbitmap live
, gimple
*stmt
)
244 int trim_head
, trim_tail
;
245 compute_trims (ref
, live
, &trim_head
, &trim_tail
);
247 /* The amount of data trimmed from the head or tail must be at
248 least half the size of the object to ensure we're trimming
249 the entire real or imaginary half. By writing things this
250 way we avoid more O(n) bitmap operations. */
251 if (trim_tail
* 2 >= ref
->size
/ BITS_PER_UNIT
)
253 /* TREE_REALPART is live */
254 tree x
= TREE_REALPART (gimple_assign_rhs1 (stmt
));
255 tree y
= gimple_assign_lhs (stmt
);
256 y
= build1 (REALPART_EXPR
, TREE_TYPE (x
), y
);
257 gimple_assign_set_lhs (stmt
, y
);
258 gimple_assign_set_rhs1 (stmt
, x
);
260 else if (trim_head
* 2 >= ref
->size
/ BITS_PER_UNIT
)
262 /* TREE_IMAGPART is live */
263 tree x
= TREE_IMAGPART (gimple_assign_rhs1 (stmt
));
264 tree y
= gimple_assign_lhs (stmt
);
265 y
= build1 (IMAGPART_EXPR
, TREE_TYPE (x
), y
);
266 gimple_assign_set_lhs (stmt
, y
);
267 gimple_assign_set_rhs1 (stmt
, x
);
270 /* Other cases indicate parts of both the real and imag subobjects
271 are live. We do not try to optimize those cases. */
274 /* STMT is a memory write where one or more bytes written are dead
275 stores. ORIG is the bitmap of bytes stored by STMT. LIVE is the
276 bitmap of stores that are actually live.
278 Attempt to rewrite STMT so that it writes fewer memory locations. Right
279 now we only support trimming at the start or end of the memory region.
280 It's not clear how much there is to be gained by trimming from the middle
284 maybe_trim_partially_dead_store (ao_ref
*ref
, sbitmap live
, gimple
*stmt
)
286 if (is_gimple_assign (stmt
))
288 switch (gimple_assign_rhs_code (stmt
))
291 maybe_trim_complex_store (ref
, live
, stmt
);
299 /* A helper of dse_optimize_stmt.
300 Given a GIMPLE_ASSIGN in STMT that writes to REF, find a candidate
301 statement *USE_STMT that may prove STMT to be dead.
302 Return TRUE if the above conditions are met, otherwise FALSE. */
304 static dse_store_status
305 dse_classify_store (ao_ref
*ref
, gimple
*stmt
, gimple
**use_stmt
,
306 bool byte_tracking_enabled
, sbitmap live_bytes
)
313 /* Find the first dominated statement that clobbers (part of) the
314 memory stmt stores to with no intermediate statement that may use
315 part of the memory stmt stores. That is, find a store that may
316 prove stmt to be a dead store. */
320 gimple
*use_stmt
, *defvar_def
;
325 /* Limit stmt walking to be linear in the number of possibly
328 return DSE_STORE_LIVE
;
330 if (gimple_code (temp
) == GIMPLE_PHI
)
331 defvar
= PHI_RESULT (temp
);
333 defvar
= gimple_vdef (temp
);
336 FOR_EACH_IMM_USE_STMT (use_stmt
, ui
, defvar
)
340 /* If we ever reach our DSE candidate stmt again fail. We
341 cannot handle dead stores in loops. */
342 if (use_stmt
== stmt
)
345 BREAK_FROM_IMM_USE_STMT (ui
);
347 /* In simple cases we can look through PHI nodes, but we
348 have to be careful with loops and with memory references
349 containing operands that are also operands of PHI nodes.
350 See gcc.c-torture/execute/20051110-*.c. */
351 else if (gimple_code (use_stmt
) == GIMPLE_PHI
)
354 /* Make sure we are not in a loop latch block. */
355 || gimple_bb (stmt
) == gimple_bb (use_stmt
)
356 || dominated_by_p (CDI_DOMINATORS
,
357 gimple_bb (stmt
), gimple_bb (use_stmt
))
358 /* We can look through PHIs to regions post-dominating
359 the DSE candidate stmt. */
360 || !dominated_by_p (CDI_POST_DOMINATORS
,
361 gimple_bb (stmt
), gimple_bb (use_stmt
)))
364 BREAK_FROM_IMM_USE_STMT (ui
);
366 /* Do not consider the PHI as use if it dominates the
367 stmt defining the virtual operand we are processing,
368 we have processed it already in this case. */
369 if (gimple_bb (defvar_def
) != gimple_bb (use_stmt
)
370 && !dominated_by_p (CDI_DOMINATORS
,
371 gimple_bb (defvar_def
),
372 gimple_bb (use_stmt
)))
375 /* If the statement is a use the store is not dead. */
376 else if (ref_maybe_used_by_stmt_p (use_stmt
, ref
))
379 BREAK_FROM_IMM_USE_STMT (ui
);
381 /* If this is a store, remember it or bail out if we have
382 multiple ones (the will be in different CFG parts then). */
383 else if (gimple_vdef (use_stmt
))
388 BREAK_FROM_IMM_USE_STMT (ui
);
396 /* STMT might be partially dead and we may be able to reduce
397 how many memory locations it stores into. */
398 if (byte_tracking_enabled
&& !gimple_clobber_p (stmt
))
399 return DSE_STORE_MAYBE_PARTIAL_DEAD
;
400 return DSE_STORE_LIVE
;
403 /* If we didn't find any definition this means the store is dead
404 if it isn't a store to global reachable memory. In this case
405 just pretend the stmt makes itself dead. Otherwise fail. */
408 if (ref_may_alias_global_p (ref
))
409 return DSE_STORE_LIVE
;
415 if (byte_tracking_enabled
&& temp
)
416 clear_bytes_written_by (live_bytes
, temp
, ref
);
418 /* Continue walking until we reach a full kill as a single statement
419 or there are no more live bytes. */
420 while (!stmt_kills_ref_p (temp
, ref
)
421 && !(byte_tracking_enabled
&& bitmap_empty_p (live_bytes
)));
424 return DSE_STORE_DEAD
;
428 class dse_dom_walker
: public dom_walker
431 dse_dom_walker (cdi_direction direction
)
432 : dom_walker (direction
), m_byte_tracking_enabled (false)
434 { m_live_bytes
= sbitmap_alloc (PARAM_VALUE (PARAM_DSE_MAX_OBJECT_SIZE
)); }
436 ~dse_dom_walker () { sbitmap_free (m_live_bytes
); }
438 virtual edge
before_dom_children (basic_block
);
441 sbitmap m_live_bytes
;
442 bool m_byte_tracking_enabled
;
443 void dse_optimize_stmt (gimple_stmt_iterator
*);
446 /* Delete a dead call STMT, which is mem* call of some kind. */
448 delete_dead_call (gimple
*stmt
)
450 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
452 fprintf (dump_file
, " Deleted dead call: ");
453 print_gimple_stmt (dump_file
, stmt
, dump_flags
, 0);
454 fprintf (dump_file
, "\n");
457 tree lhs
= gimple_call_lhs (stmt
);
458 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
461 tree ptr
= gimple_call_arg (stmt
, 0);
462 gimple
*new_stmt
= gimple_build_assign (lhs
, ptr
);
463 unlink_stmt_vdef (stmt
);
464 if (gsi_replace (&gsi
, new_stmt
, true))
465 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
469 /* Then we need to fix the operand of the consuming stmt. */
470 unlink_stmt_vdef (stmt
);
472 /* Remove the dead store. */
473 if (gsi_remove (&gsi
, true))
474 bitmap_set_bit (need_eh_cleanup
, gimple_bb (stmt
)->index
);
479 /* Delete a dead store STMT, which is a gimple assignment. */
482 delete_dead_assignment (gimple
*stmt
)
484 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
486 fprintf (dump_file
, " Deleted dead store: ");
487 print_gimple_stmt (dump_file
, stmt
, dump_flags
, 0);
488 fprintf (dump_file
, "\n");
491 /* Then we need to fix the operand of the consuming stmt. */
492 unlink_stmt_vdef (stmt
);
494 /* Remove the dead store. */
495 gimple_stmt_iterator gsi
= gsi_for_stmt (stmt
);
496 basic_block bb
= gimple_bb (stmt
);
497 if (gsi_remove (&gsi
, true))
498 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
500 /* And release any SSA_NAMEs set in this statement back to the
505 /* Attempt to eliminate dead stores in the statement referenced by BSI.
507 A dead store is a store into a memory location which will later be
508 overwritten by another store without any intervening loads. In this
509 case the earlier store can be deleted.
511 In our SSA + virtual operand world we use immediate uses of virtual
512 operands to detect dead stores. If a store's virtual definition
513 is used precisely once by a later store to the same location which
514 post dominates the first store, then the first store is dead. */
517 dse_dom_walker::dse_optimize_stmt (gimple_stmt_iterator
*gsi
)
519 gimple
*stmt
= gsi_stmt (*gsi
);
521 /* If this statement has no virtual defs, then there is nothing
523 if (!gimple_vdef (stmt
))
526 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
527 if (gimple_has_volatile_ops (stmt
)
528 && (!gimple_clobber_p (stmt
)
529 || TREE_CODE (gimple_assign_lhs (stmt
)) != MEM_REF
))
533 if (!initialize_ao_ref_for_dse (stmt
, &ref
))
536 /* We know we have virtual definitions. We can handle assignments and
537 some builtin calls. */
538 if (gimple_call_builtin_p (stmt
, BUILT_IN_NORMAL
))
540 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt
)))
542 case BUILT_IN_MEMCPY
:
543 case BUILT_IN_MEMMOVE
:
544 case BUILT_IN_MEMSET
:
547 enum dse_store_status store_status
;
548 m_byte_tracking_enabled
549 = setup_live_bytes_from_ref (&ref
, m_live_bytes
);
550 store_status
= dse_classify_store (&ref
, stmt
, &use_stmt
,
551 m_byte_tracking_enabled
,
553 if (store_status
== DSE_STORE_LIVE
)
556 if (store_status
== DSE_STORE_MAYBE_PARTIAL_DEAD
)
558 maybe_trim_partially_dead_store (&ref
, m_live_bytes
, stmt
);
562 if (store_status
== DSE_STORE_DEAD
)
563 delete_dead_call (stmt
);
572 if (is_gimple_assign (stmt
))
576 /* Self-assignments are zombies. */
577 if (operand_equal_p (gimple_assign_rhs1 (stmt
),
578 gimple_assign_lhs (stmt
), 0))
582 m_byte_tracking_enabled
583 = setup_live_bytes_from_ref (&ref
, m_live_bytes
);
584 enum dse_store_status store_status
;
585 store_status
= dse_classify_store (&ref
, stmt
, &use_stmt
,
586 m_byte_tracking_enabled
,
588 if (store_status
== DSE_STORE_LIVE
)
591 if (store_status
== DSE_STORE_MAYBE_PARTIAL_DEAD
)
593 maybe_trim_partially_dead_store (&ref
, m_live_bytes
, stmt
);
598 /* Now we know that use_stmt kills the LHS of stmt. */
600 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
601 another clobber stmt. */
602 if (gimple_clobber_p (stmt
)
603 && !gimple_clobber_p (use_stmt
))
606 delete_dead_assignment (stmt
);
611 dse_dom_walker::before_dom_children (basic_block bb
)
613 gimple_stmt_iterator gsi
;
615 for (gsi
= gsi_last_bb (bb
); !gsi_end_p (gsi
);)
617 dse_optimize_stmt (&gsi
);
619 gsi
= gsi_last_bb (bb
);
628 const pass_data pass_data_dse
=
630 GIMPLE_PASS
, /* type */
632 OPTGROUP_NONE
, /* optinfo_flags */
633 TV_TREE_DSE
, /* tv_id */
634 ( PROP_cfg
| PROP_ssa
), /* properties_required */
635 0, /* properties_provided */
636 0, /* properties_destroyed */
637 0, /* todo_flags_start */
638 0, /* todo_flags_finish */
641 class pass_dse
: public gimple_opt_pass
644 pass_dse (gcc::context
*ctxt
)
645 : gimple_opt_pass (pass_data_dse
, ctxt
)
648 /* opt_pass methods: */
649 opt_pass
* clone () { return new pass_dse (m_ctxt
); }
650 virtual bool gate (function
*) { return flag_tree_dse
!= 0; }
651 virtual unsigned int execute (function
*);
656 pass_dse::execute (function
*fun
)
658 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
660 renumber_gimple_stmt_uids ();
662 /* We might consider making this a property of each pass so that it
663 can be [re]computed on an as-needed basis. Particularly since
664 this pass could be seen as an extension of DCE which needs post
666 calculate_dominance_info (CDI_POST_DOMINATORS
);
667 calculate_dominance_info (CDI_DOMINATORS
);
669 /* Dead store elimination is fundamentally a walk of the post-dominator
670 tree and a backwards walk of statements within each block. */
671 dse_dom_walker (CDI_POST_DOMINATORS
).walk (fun
->cfg
->x_exit_block_ptr
);
673 /* Removal of stores may make some EH edges dead. Purge such edges from
674 the CFG as needed. */
675 if (!bitmap_empty_p (need_eh_cleanup
))
677 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
681 BITMAP_FREE (need_eh_cleanup
);
683 /* For now, just wipe the post-dominator information. */
684 free_dominance_info (CDI_POST_DOMINATORS
);
691 make_pass_dse (gcc::context
*ctxt
)
693 return new pass_dse (ctxt
);