* builtins.def (BUILT_IN_SETJMP): Revert latest change.
[official-gcc.git] / gcc / tree-ssa-dse.c
blob6f58fffc6938e2c8dade8668e7e212c921e570af
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)
9 any later version.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "gimple.h"
27 #include "tree-pass.h"
28 #include "ssa.h"
29 #include "gimple-pretty-print.h"
30 #include "fold-const.h"
31 #include "gimple-iterator.h"
32 #include "tree-cfg.h"
33 #include "tree-dfa.h"
34 #include "domwalk.h"
35 #include "tree-cfgcleanup.h"
36 #include "params.h"
37 #include "alias.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
54 exits.
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
66 the CFG. */
69 /* Bitmap of blocks that have had EH statements cleaned. We should
70 remove their dead edges eventually. */
71 static bitmap need_eh_cleanup;
73 /* Return value from dse_classify_store */
74 enum dse_store_status
76 DSE_STORE_LIVE,
77 DSE_STORE_MAYBE_PARTIAL_DEAD,
78 DSE_STORE_DEAD
81 /* STMT is a statement that may write into memory. Analyze it and
82 initialize WRITE to describe how STMT affects memory.
84 Return TRUE if the the statement was analyzed, FALSE otherwise.
86 It is always safe to return FALSE. But typically better optimziation
87 can be achieved by analyzing more statements. */
89 static bool
90 initialize_ao_ref_for_dse (gimple *stmt, ao_ref *write)
92 /* It's advantageous to handle certain mem* functions. */
93 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
95 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
97 case BUILT_IN_MEMCPY:
98 case BUILT_IN_MEMMOVE:
99 case BUILT_IN_MEMSET:
101 tree size = NULL_TREE;
102 if (gimple_call_num_args (stmt) == 3)
103 size = gimple_call_arg (stmt, 2);
104 tree ptr = gimple_call_arg (stmt, 0);
105 ao_ref_init_from_ptr_and_size (write, ptr, size);
106 return true;
108 default:
109 break;
112 else if (is_gimple_assign (stmt))
114 ao_ref_init (write, gimple_assign_lhs (stmt));
115 return true;
117 return false;
120 /* Given REF from the the alias oracle, return TRUE if it is a valid
121 memory reference for dead store elimination, false otherwise.
123 In particular, the reference must have a known base, known maximum
124 size, start at a byte offset and have a size that is one or more
125 bytes. */
127 static bool
128 valid_ao_ref_for_dse (ao_ref *ref)
130 return (ao_ref_base (ref)
131 && ref->max_size != -1
132 && ref->size != 0
133 && ref->max_size == ref->size
134 && (ref->offset % BITS_PER_UNIT) == 0
135 && (ref->size % BITS_PER_UNIT) == 0
136 && (ref->size != -1));
139 /* Normalize COPY (an ao_ref) relative to REF. Essentially when we are
140 done COPY will only refer bytes found within REF.
142 We have already verified that COPY intersects at least one
143 byte with REF. */
145 static void
146 normalize_ref (ao_ref *copy, ao_ref *ref)
148 /* If COPY starts before REF, then reset the beginning of
149 COPY to match REF and decrease the size of COPY by the
150 number of bytes removed from COPY. */
151 if (copy->offset < ref->offset)
153 copy->size -= (ref->offset - copy->offset);
154 copy->offset = ref->offset;
157 /* If COPY extends beyond REF, chop off its size appropriately. */
158 if (copy->offset + copy->size > ref->offset + ref->size)
159 copy->size -= (copy->offset + copy->size - (ref->offset + ref->size));
162 /* Clear any bytes written by STMT from the bitmap LIVE_BYTES. The base
163 address written by STMT must match the one found in REF, which must
164 have its base address previously initialized.
166 This routine must be conservative. If we don't know the offset or
167 actual size written, assume nothing was written. */
169 static void
170 clear_bytes_written_by (sbitmap live_bytes, gimple *stmt, ao_ref *ref)
172 ao_ref write;
173 if (!initialize_ao_ref_for_dse (stmt, &write))
174 return;
176 /* Verify we have the same base memory address, the write
177 has a known size and overlaps with REF. */
178 if (valid_ao_ref_for_dse (&write)
179 && operand_equal_p (write.base, ref->base, OEP_ADDRESS_OF)
180 && write.size == write.max_size
181 && ((write.offset < ref->offset
182 && write.offset + write.size > ref->offset)
183 || (write.offset >= ref->offset
184 && write.offset < ref->offset + ref->size)))
186 normalize_ref (&write, ref);
187 bitmap_clear_range (live_bytes,
188 (write.offset - ref->offset) / BITS_PER_UNIT,
189 write.size / BITS_PER_UNIT);
193 /* REF is a memory write. Extract relevant information from it and
194 initialize the LIVE_BYTES bitmap. If successful, return TRUE.
195 Otherwise return FALSE. */
197 static bool
198 setup_live_bytes_from_ref (ao_ref *ref, sbitmap live_bytes)
200 if (valid_ao_ref_for_dse (ref)
201 && (ref->size / BITS_PER_UNIT
202 <= PARAM_VALUE (PARAM_DSE_MAX_OBJECT_SIZE)))
204 bitmap_clear (live_bytes);
205 bitmap_set_range (live_bytes, 0, ref->size / BITS_PER_UNIT);
206 return true;
208 return false;
211 /* Compute the number of elements that we can trim from the head and
212 tail of ORIG resulting in a bitmap that is a superset of LIVE.
214 Store the number of elements trimmed from the head and tail in
215 TRIM_HEAD and TRIM_TAIL.
217 STMT is the statement being trimmed and is used for debugging dump
218 output only. */
220 static void
221 compute_trims (ao_ref *ref, sbitmap live, int *trim_head, int *trim_tail,
222 gimple *stmt)
224 /* We use sbitmaps biased such that ref->offset is bit zero and the bitmap
225 extends through ref->size. So we know that in the original bitmap
226 bits 0..ref->size were true. We don't actually need the bitmap, just
227 the REF to compute the trims. */
229 /* Now identify how much, if any of the tail we can chop off. */
230 int last_orig = (ref->size / BITS_PER_UNIT) - 1;
231 int last_live = bitmap_last_set_bit (live);
232 *trim_tail = (last_orig - last_live) & ~0x1;
234 /* Identify how much, if any of the head we can chop off. */
235 int first_orig = 0;
236 int first_live = bitmap_first_set_bit (live);
237 *trim_head = (first_live - first_orig) & ~0x1;
239 if ((*trim_head || *trim_tail)
240 && dump_file && (dump_flags & TDF_DETAILS))
242 fprintf (dump_file, " Trimming statement (head = %d, tail = %d): ",
243 *trim_head, *trim_tail);
244 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
245 fprintf (dump_file, "\n");
249 /* STMT initializes an object from COMPLEX_CST where one or more of the
250 bytes written may be dead stores. REF is a representation of the
251 memory written. LIVE is the bitmap of stores that are actually live.
253 Attempt to rewrite STMT so that only the real or imaginary part of
254 the object is actually stored. */
256 static void
257 maybe_trim_complex_store (ao_ref *ref, sbitmap live, gimple *stmt)
259 int trim_head, trim_tail;
260 compute_trims (ref, live, &trim_head, &trim_tail, stmt);
262 /* The amount of data trimmed from the head or tail must be at
263 least half the size of the object to ensure we're trimming
264 the entire real or imaginary half. By writing things this
265 way we avoid more O(n) bitmap operations. */
266 if (trim_tail * 2 >= ref->size / BITS_PER_UNIT)
268 /* TREE_REALPART is live */
269 tree x = TREE_REALPART (gimple_assign_rhs1 (stmt));
270 tree y = gimple_assign_lhs (stmt);
271 y = build1 (REALPART_EXPR, TREE_TYPE (x), y);
272 gimple_assign_set_lhs (stmt, y);
273 gimple_assign_set_rhs1 (stmt, x);
275 else if (trim_head * 2 >= ref->size / BITS_PER_UNIT)
277 /* TREE_IMAGPART is live */
278 tree x = TREE_IMAGPART (gimple_assign_rhs1 (stmt));
279 tree y = gimple_assign_lhs (stmt);
280 y = build1 (IMAGPART_EXPR, TREE_TYPE (x), y);
281 gimple_assign_set_lhs (stmt, y);
282 gimple_assign_set_rhs1 (stmt, x);
285 /* Other cases indicate parts of both the real and imag subobjects
286 are live. We do not try to optimize those cases. */
289 /* STMT initializes an object using a CONSTRUCTOR where one or more of the
290 bytes written are dead stores. ORIG is the bitmap of bytes stored by
291 STMT. LIVE is the bitmap of stores that are actually live.
293 Attempt to rewrite STMT so that only the real or imaginary part of
294 the object is actually stored.
296 The most common case for getting here is a CONSTRUCTOR with no elements
297 being used to zero initialize an object. We do not try to handle other
298 cases as those would force us to fully cover the object with the
299 CONSTRUCTOR node except for the components that are dead. */
301 static void
302 maybe_trim_constructor_store (ao_ref *ref, sbitmap live, gimple *stmt)
304 tree ctor = gimple_assign_rhs1 (stmt);
306 /* This is the only case we currently handle. It actually seems to
307 catch most cases of actual interest. */
308 gcc_assert (CONSTRUCTOR_NELTS (ctor) == 0);
310 int head_trim = 0;
311 int tail_trim = 0;
312 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
314 /* Now we want to replace the constructor initializer
315 with memset (object + head_trim, 0, size - head_trim - tail_trim). */
316 if (head_trim || tail_trim)
318 /* We want &lhs for the MEM_REF expression. */
319 tree lhs_addr = build_fold_addr_expr (gimple_assign_lhs (stmt));
321 if (! is_gimple_min_invariant (lhs_addr))
322 return;
324 /* The number of bytes for the new constructor. */
325 int count = (ref->size / BITS_PER_UNIT) - head_trim - tail_trim;
327 /* And the new type for the CONSTRUCTOR. Essentially it's just
328 a char array large enough to cover the non-trimmed parts of
329 the original CONSTRUCTOR. Note we want explicit bounds here
330 so that we know how many bytes to clear when expanding the
331 CONSTRUCTOR. */
332 tree type = build_array_type_nelts (char_type_node, count);
334 /* Build a suitable alias type rather than using alias set zero
335 to avoid pessimizing. */
336 tree alias_type = reference_alias_ptr_type (gimple_assign_lhs (stmt));
338 /* Build a MEM_REF representing the whole accessed area, starting
339 at the first byte not trimmed. */
340 tree exp = fold_build2 (MEM_REF, type, lhs_addr,
341 build_int_cst (alias_type, head_trim));
343 /* Now update STMT with a new RHS and LHS. */
344 gimple_assign_set_lhs (stmt, exp);
345 gimple_assign_set_rhs1 (stmt, build_constructor (type, NULL));
349 /* STMT is a memcpy, memmove or memset. Decrement the number of bytes
350 copied/set by DECREMENT. */
351 static void
352 decrement_count (gimple *stmt, int decrement)
354 tree *countp = gimple_call_arg_ptr (stmt, 2);
355 gcc_assert (TREE_CODE (*countp) == INTEGER_CST);
356 *countp = wide_int_to_tree (TREE_TYPE (*countp), (TREE_INT_CST_LOW (*countp)
357 - decrement));
361 static void
362 increment_start_addr (gimple *stmt, tree *where, int increment)
364 if (TREE_CODE (*where) == SSA_NAME)
366 tree tem = make_ssa_name (TREE_TYPE (*where));
367 gassign *newop
368 = gimple_build_assign (tem, POINTER_PLUS_EXPR, *where,
369 build_int_cst (sizetype, increment));
370 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
371 gsi_insert_before (&gsi, newop, GSI_SAME_STMT);
372 *where = tem;
373 update_stmt (gsi_stmt (gsi));
374 return;
377 *where = build_fold_addr_expr (fold_build2 (MEM_REF, char_type_node,
378 *where,
379 build_int_cst (ptr_type_node,
380 increment)));
383 /* STMT is builtin call that writes bytes in bitmap ORIG, some bytes are dead
384 (ORIG & ~NEW) and need not be stored. Try to rewrite STMT to reduce
385 the amount of data it actually writes.
387 Right now we only support trimming from the head or the tail of the
388 memory region. In theory we could split the mem* call, but it's
389 likely of marginal value. */
391 static void
392 maybe_trim_memstar_call (ao_ref *ref, sbitmap live, gimple *stmt)
394 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
396 case BUILT_IN_MEMCPY:
397 case BUILT_IN_MEMMOVE:
399 int head_trim, tail_trim;
400 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
402 /* Tail trimming is easy, we can just reduce the count. */
403 if (tail_trim)
404 decrement_count (stmt, tail_trim);
406 /* Head trimming requires adjusting all the arguments. */
407 if (head_trim)
409 tree *dst = gimple_call_arg_ptr (stmt, 0);
410 increment_start_addr (stmt, dst, head_trim);
411 tree *src = gimple_call_arg_ptr (stmt, 1);
412 increment_start_addr (stmt, src, head_trim);
413 decrement_count (stmt, head_trim);
415 break;
418 case BUILT_IN_MEMSET:
420 int head_trim, tail_trim;
421 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
423 /* Tail trimming is easy, we can just reduce the count. */
424 if (tail_trim)
425 decrement_count (stmt, tail_trim);
427 /* Head trimming requires adjusting all the arguments. */
428 if (head_trim)
430 tree *dst = gimple_call_arg_ptr (stmt, 0);
431 increment_start_addr (stmt, dst, head_trim);
432 decrement_count (stmt, head_trim);
434 break;
437 default:
438 break;
442 /* STMT is a memory write where one or more bytes written are dead
443 stores. ORIG is the bitmap of bytes stored by STMT. LIVE is the
444 bitmap of stores that are actually live.
446 Attempt to rewrite STMT so that it writes fewer memory locations. Right
447 now we only support trimming at the start or end of the memory region.
448 It's not clear how much there is to be gained by trimming from the middle
449 of the region. */
451 static void
452 maybe_trim_partially_dead_store (ao_ref *ref, sbitmap live, gimple *stmt)
454 if (is_gimple_assign (stmt)
455 && TREE_CODE (gimple_assign_lhs (stmt)) != TARGET_MEM_REF)
457 switch (gimple_assign_rhs_code (stmt))
459 case CONSTRUCTOR:
460 maybe_trim_constructor_store (ref, live, stmt);
461 break;
462 case COMPLEX_CST:
463 maybe_trim_complex_store (ref, live, stmt);
464 break;
465 default:
466 break;
471 /* Return TRUE if USE_REF reads bytes from LIVE where live is
472 derived from REF, a write reference.
474 While this routine may modify USE_REF, it's passed by value, not
475 location. So callers do not see those modifications. */
477 static bool
478 live_bytes_read (ao_ref use_ref, ao_ref *ref, sbitmap live)
480 /* We have already verified that USE_REF and REF hit the same object.
481 Now verify that there's actually an overlap between USE_REF and REF. */
482 if (ranges_overlap_p (use_ref.offset, use_ref.size, ref->offset, ref->size))
484 normalize_ref (&use_ref, ref);
486 /* If USE_REF covers all of REF, then it will hit one or more
487 live bytes. This avoids useless iteration over the bitmap
488 below. */
489 if (use_ref.offset <= ref->offset
490 && use_ref.offset + use_ref.size >= ref->offset + ref->size)
491 return true;
493 /* Now check if any of the remaining bits in use_ref are set in LIVE. */
494 unsigned int start = (use_ref.offset - ref->offset) / BITS_PER_UNIT;
495 unsigned int end = (use_ref.offset + use_ref.size) / BITS_PER_UNIT;
496 return bitmap_bit_in_range_p (live, start, end);
498 return true;
501 /* A helper of dse_optimize_stmt.
502 Given a GIMPLE_ASSIGN in STMT that writes to REF, find a candidate
503 statement *USE_STMT that may prove STMT to be dead.
504 Return TRUE if the above conditions are met, otherwise FALSE. */
506 static dse_store_status
507 dse_classify_store (ao_ref *ref, gimple *stmt, gimple **use_stmt,
508 bool byte_tracking_enabled, sbitmap live_bytes)
510 gimple *temp;
511 unsigned cnt = 0;
513 *use_stmt = NULL;
515 /* Find the first dominated statement that clobbers (part of) the
516 memory stmt stores to with no intermediate statement that may use
517 part of the memory stmt stores. That is, find a store that may
518 prove stmt to be a dead store. */
519 temp = stmt;
522 gimple *use_stmt, *defvar_def;
523 imm_use_iterator ui;
524 bool fail = false;
525 tree defvar;
527 /* Limit stmt walking to be linear in the number of possibly
528 dead stores. */
529 if (++cnt > 256)
530 return DSE_STORE_LIVE;
532 if (gimple_code (temp) == GIMPLE_PHI)
533 defvar = PHI_RESULT (temp);
534 else
535 defvar = gimple_vdef (temp);
536 defvar_def = temp;
537 temp = NULL;
538 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
540 cnt++;
542 /* If we ever reach our DSE candidate stmt again fail. We
543 cannot handle dead stores in loops. */
544 if (use_stmt == stmt)
546 fail = true;
547 BREAK_FROM_IMM_USE_STMT (ui);
549 /* In simple cases we can look through PHI nodes, but we
550 have to be careful with loops and with memory references
551 containing operands that are also operands of PHI nodes.
552 See gcc.c-torture/execute/20051110-*.c. */
553 else if (gimple_code (use_stmt) == GIMPLE_PHI)
555 if (temp
556 /* Make sure we are not in a loop latch block. */
557 || gimple_bb (stmt) == gimple_bb (use_stmt)
558 || dominated_by_p (CDI_DOMINATORS,
559 gimple_bb (stmt), gimple_bb (use_stmt))
560 /* We can look through PHIs to regions post-dominating
561 the DSE candidate stmt. */
562 || !dominated_by_p (CDI_POST_DOMINATORS,
563 gimple_bb (stmt), gimple_bb (use_stmt)))
565 fail = true;
566 BREAK_FROM_IMM_USE_STMT (ui);
568 /* Do not consider the PHI as use if it dominates the
569 stmt defining the virtual operand we are processing,
570 we have processed it already in this case. */
571 if (gimple_bb (defvar_def) != gimple_bb (use_stmt)
572 && !dominated_by_p (CDI_DOMINATORS,
573 gimple_bb (defvar_def),
574 gimple_bb (use_stmt)))
575 temp = use_stmt;
577 /* If the statement is a use the store is not dead. */
578 else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
580 /* Handle common cases where we can easily build an ao_ref
581 structure for USE_STMT and in doing so we find that the
582 references hit non-live bytes and thus can be ignored. */
583 if (byte_tracking_enabled && (!gimple_vdef (use_stmt) || !temp))
585 if (is_gimple_assign (use_stmt))
587 /* Other cases were noted as non-aliasing by
588 the call to ref_maybe_used_by_stmt_p. */
589 ao_ref use_ref;
590 ao_ref_init (&use_ref, gimple_assign_rhs1 (use_stmt));
591 if (valid_ao_ref_for_dse (&use_ref)
592 && use_ref.base == ref->base
593 && use_ref.size == use_ref.max_size
594 && !live_bytes_read (use_ref, ref, live_bytes))
596 /* If this statement has a VDEF, then it is the
597 first store we have seen, so walk through it. */
598 if (gimple_vdef (use_stmt))
599 temp = use_stmt;
600 continue;
605 fail = true;
606 BREAK_FROM_IMM_USE_STMT (ui);
608 /* If this is a store, remember it or bail out if we have
609 multiple ones (the will be in different CFG parts then). */
610 else if (gimple_vdef (use_stmt))
612 if (temp)
614 fail = true;
615 BREAK_FROM_IMM_USE_STMT (ui);
617 temp = use_stmt;
621 if (fail)
623 /* STMT might be partially dead and we may be able to reduce
624 how many memory locations it stores into. */
625 if (byte_tracking_enabled && !gimple_clobber_p (stmt))
626 return DSE_STORE_MAYBE_PARTIAL_DEAD;
627 return DSE_STORE_LIVE;
630 /* If we didn't find any definition this means the store is dead
631 if it isn't a store to global reachable memory. In this case
632 just pretend the stmt makes itself dead. Otherwise fail. */
633 if (!temp)
635 if (ref_may_alias_global_p (ref))
636 return DSE_STORE_LIVE;
638 temp = stmt;
639 break;
642 if (byte_tracking_enabled && temp)
643 clear_bytes_written_by (live_bytes, temp, ref);
645 /* Continue walking until we reach a full kill as a single statement
646 or there are no more live bytes. */
647 while (!stmt_kills_ref_p (temp, ref)
648 && !(byte_tracking_enabled && bitmap_empty_p (live_bytes)));
650 *use_stmt = temp;
651 return DSE_STORE_DEAD;
655 class dse_dom_walker : public dom_walker
657 public:
658 dse_dom_walker (cdi_direction direction)
659 : dom_walker (direction),
660 m_live_bytes (PARAM_VALUE (PARAM_DSE_MAX_OBJECT_SIZE)),
661 m_byte_tracking_enabled (false) {}
663 virtual edge before_dom_children (basic_block);
665 private:
666 auto_sbitmap m_live_bytes;
667 bool m_byte_tracking_enabled;
668 void dse_optimize_stmt (gimple_stmt_iterator *);
671 /* Delete a dead call at GSI, which is mem* call of some kind. */
672 static void
673 delete_dead_call (gimple_stmt_iterator *gsi)
675 gimple *stmt = gsi_stmt (*gsi);
676 if (dump_file && (dump_flags & TDF_DETAILS))
678 fprintf (dump_file, " Deleted dead call: ");
679 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
680 fprintf (dump_file, "\n");
683 tree lhs = gimple_call_lhs (stmt);
684 if (lhs)
686 tree ptr = gimple_call_arg (stmt, 0);
687 gimple *new_stmt = gimple_build_assign (lhs, ptr);
688 unlink_stmt_vdef (stmt);
689 if (gsi_replace (gsi, new_stmt, true))
690 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
692 else
694 /* Then we need to fix the operand of the consuming stmt. */
695 unlink_stmt_vdef (stmt);
697 /* Remove the dead store. */
698 if (gsi_remove (gsi, true))
699 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
700 release_defs (stmt);
704 /* Delete a dead store at GSI, which is a gimple assignment. */
706 static void
707 delete_dead_assignment (gimple_stmt_iterator *gsi)
709 gimple *stmt = gsi_stmt (*gsi);
710 if (dump_file && (dump_flags & TDF_DETAILS))
712 fprintf (dump_file, " Deleted dead store: ");
713 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
714 fprintf (dump_file, "\n");
717 /* Then we need to fix the operand of the consuming stmt. */
718 unlink_stmt_vdef (stmt);
720 /* Remove the dead store. */
721 basic_block bb = gimple_bb (stmt);
722 if (gsi_remove (gsi, true))
723 bitmap_set_bit (need_eh_cleanup, bb->index);
725 /* And release any SSA_NAMEs set in this statement back to the
726 SSA_NAME manager. */
727 release_defs (stmt);
730 /* Attempt to eliminate dead stores in the statement referenced by BSI.
732 A dead store is a store into a memory location which will later be
733 overwritten by another store without any intervening loads. In this
734 case the earlier store can be deleted.
736 In our SSA + virtual operand world we use immediate uses of virtual
737 operands to detect dead stores. If a store's virtual definition
738 is used precisely once by a later store to the same location which
739 post dominates the first store, then the first store is dead. */
741 void
742 dse_dom_walker::dse_optimize_stmt (gimple_stmt_iterator *gsi)
744 gimple *stmt = gsi_stmt (*gsi);
746 /* If this statement has no virtual defs, then there is nothing
747 to do. */
748 if (!gimple_vdef (stmt))
749 return;
751 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
752 if (gimple_has_volatile_ops (stmt)
753 && (!gimple_clobber_p (stmt)
754 || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
755 return;
757 ao_ref ref;
758 if (!initialize_ao_ref_for_dse (stmt, &ref))
759 return;
761 /* We know we have virtual definitions. We can handle assignments and
762 some builtin calls. */
763 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
765 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
767 case BUILT_IN_MEMCPY:
768 case BUILT_IN_MEMMOVE:
769 case BUILT_IN_MEMSET:
771 /* Occasionally calls with an explicit length of zero
772 show up in the IL. It's pointless to do analysis
773 on them, they're trivially dead. */
774 tree size = gimple_call_arg (stmt, 2);
775 if (integer_zerop (size))
777 delete_dead_call (gsi);
778 return;
781 gimple *use_stmt;
782 enum dse_store_status store_status;
783 m_byte_tracking_enabled
784 = setup_live_bytes_from_ref (&ref, m_live_bytes);
785 store_status = dse_classify_store (&ref, stmt, &use_stmt,
786 m_byte_tracking_enabled,
787 m_live_bytes);
788 if (store_status == DSE_STORE_LIVE)
789 return;
791 if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
793 maybe_trim_memstar_call (&ref, m_live_bytes, stmt);
794 return;
797 if (store_status == DSE_STORE_DEAD)
798 delete_dead_call (gsi);
799 return;
802 default:
803 return;
807 if (is_gimple_assign (stmt))
809 gimple *use_stmt;
811 /* Self-assignments are zombies. */
812 if (operand_equal_p (gimple_assign_rhs1 (stmt),
813 gimple_assign_lhs (stmt), 0))
814 use_stmt = stmt;
815 else
817 m_byte_tracking_enabled
818 = setup_live_bytes_from_ref (&ref, m_live_bytes);
819 enum dse_store_status store_status;
820 store_status = dse_classify_store (&ref, stmt, &use_stmt,
821 m_byte_tracking_enabled,
822 m_live_bytes);
823 if (store_status == DSE_STORE_LIVE)
824 return;
826 if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
828 maybe_trim_partially_dead_store (&ref, m_live_bytes, stmt);
829 return;
833 /* Now we know that use_stmt kills the LHS of stmt. */
835 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
836 another clobber stmt. */
837 if (gimple_clobber_p (stmt)
838 && !gimple_clobber_p (use_stmt))
839 return;
841 delete_dead_assignment (gsi);
845 edge
846 dse_dom_walker::before_dom_children (basic_block bb)
848 gimple_stmt_iterator gsi;
850 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
852 dse_optimize_stmt (&gsi);
853 if (gsi_end_p (gsi))
854 gsi = gsi_last_bb (bb);
855 else
856 gsi_prev (&gsi);
858 return NULL;
861 namespace {
863 const pass_data pass_data_dse =
865 GIMPLE_PASS, /* type */
866 "dse", /* name */
867 OPTGROUP_NONE, /* optinfo_flags */
868 TV_TREE_DSE, /* tv_id */
869 ( PROP_cfg | PROP_ssa ), /* properties_required */
870 0, /* properties_provided */
871 0, /* properties_destroyed */
872 0, /* todo_flags_start */
873 0, /* todo_flags_finish */
876 class pass_dse : public gimple_opt_pass
878 public:
879 pass_dse (gcc::context *ctxt)
880 : gimple_opt_pass (pass_data_dse, ctxt)
883 /* opt_pass methods: */
884 opt_pass * clone () { return new pass_dse (m_ctxt); }
885 virtual bool gate (function *) { return flag_tree_dse != 0; }
886 virtual unsigned int execute (function *);
888 }; // class pass_dse
890 unsigned int
891 pass_dse::execute (function *fun)
893 need_eh_cleanup = BITMAP_ALLOC (NULL);
895 renumber_gimple_stmt_uids ();
897 /* We might consider making this a property of each pass so that it
898 can be [re]computed on an as-needed basis. Particularly since
899 this pass could be seen as an extension of DCE which needs post
900 dominators. */
901 calculate_dominance_info (CDI_POST_DOMINATORS);
902 calculate_dominance_info (CDI_DOMINATORS);
904 /* Dead store elimination is fundamentally a walk of the post-dominator
905 tree and a backwards walk of statements within each block. */
906 dse_dom_walker (CDI_POST_DOMINATORS).walk (fun->cfg->x_exit_block_ptr);
908 /* Removal of stores may make some EH edges dead. Purge such edges from
909 the CFG as needed. */
910 if (!bitmap_empty_p (need_eh_cleanup))
912 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
913 cleanup_tree_cfg ();
916 BITMAP_FREE (need_eh_cleanup);
918 /* For now, just wipe the post-dominator information. */
919 free_dominance_info (CDI_POST_DOMINATORS);
920 return 0;
923 } // anon namespace
925 gimple_opt_pass *
926 make_pass_dse (gcc::context *ctxt)
928 return new pass_dse (ctxt);