[Ada] Unnesting: handle conditional expressions
[official-gcc.git] / gcc / tree-ssa-dse.c
blobdf05a55ce78d40430f891b275505e5a85a6eefb5
1 /* Dead and redundant store elimination
2 Copyright (C) 2004-2019 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"
38 #include "tree-ssa-loop.h"
40 /* This file implements dead store elimination.
42 A dead store is a store into a memory location which will later be
43 overwritten by another store without any intervening loads. In this
44 case the earlier store can be deleted or trimmed if the store
45 was partially dead.
47 A redundant store is a store into a memory location which stores
48 the exact same value as a prior store to the same memory location.
49 While this can often be handled by dead store elimination, removing
50 the redundant store is often better than removing or trimming the
51 dead store.
53 In our SSA + virtual operand world we use immediate uses of virtual
54 operands to detect these cases. If a store's virtual definition
55 is used precisely once by a later store to the same location which
56 post dominates the first store, then the first store is dead. If
57 the data stored is the same, then the second store is redundant.
59 The single use of the store's virtual definition ensures that
60 there are no intervening aliased loads and the requirement that
61 the second load post dominate the first ensures that if the earlier
62 store executes, then the later stores will execute before the function
63 exits.
65 It may help to think of this as first moving the earlier store to
66 the point immediately before the later store. Again, the single
67 use of the virtual definition and the post-dominance relationship
68 ensure that such movement would be safe. Clearly if there are
69 back to back stores, then the second is makes the first dead. If
70 the second store stores the same value, then the second store is
71 redundant.
73 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
74 may also help in understanding this code since it discusses the
75 relationship between dead store and redundant load elimination. In
76 fact, they are the same transformation applied to different views of
77 the CFG. */
79 static void delete_dead_or_redundant_assignment (gimple_stmt_iterator *, const char *);
80 static void delete_dead_or_redundant_call (gimple_stmt_iterator *, const char *);
82 /* Bitmap of blocks that have had EH statements cleaned. We should
83 remove their dead edges eventually. */
84 static bitmap need_eh_cleanup;
86 /* Return value from dse_classify_store */
87 enum dse_store_status
89 DSE_STORE_LIVE,
90 DSE_STORE_MAYBE_PARTIAL_DEAD,
91 DSE_STORE_DEAD
94 /* STMT is a statement that may write into memory. Analyze it and
95 initialize WRITE to describe how STMT affects memory.
97 Return TRUE if the the statement was analyzed, FALSE otherwise.
99 It is always safe to return FALSE. But typically better optimziation
100 can be achieved by analyzing more statements. */
102 static bool
103 initialize_ao_ref_for_dse (gimple *stmt, ao_ref *write)
105 /* It's advantageous to handle certain mem* functions. */
106 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
108 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
110 case BUILT_IN_MEMCPY:
111 case BUILT_IN_MEMMOVE:
112 case BUILT_IN_MEMSET:
113 case BUILT_IN_MEMCPY_CHK:
114 case BUILT_IN_MEMMOVE_CHK:
115 case BUILT_IN_MEMSET_CHK:
117 tree size = NULL_TREE;
118 if (gimple_call_num_args (stmt) == 3)
119 size = gimple_call_arg (stmt, 2);
120 tree ptr = gimple_call_arg (stmt, 0);
121 ao_ref_init_from_ptr_and_size (write, ptr, size);
122 return true;
125 /* A calloc call can never be dead, but it can make
126 subsequent stores redundant if they store 0 into
127 the same memory locations. */
128 case BUILT_IN_CALLOC:
130 tree nelem = gimple_call_arg (stmt, 0);
131 tree selem = gimple_call_arg (stmt, 1);
132 tree lhs;
133 if (TREE_CODE (nelem) == INTEGER_CST
134 && TREE_CODE (selem) == INTEGER_CST
135 && (lhs = gimple_call_lhs (stmt)) != NULL_TREE)
137 tree size = fold_build2 (MULT_EXPR, TREE_TYPE (nelem),
138 nelem, selem);
139 ao_ref_init_from_ptr_and_size (write, lhs, size);
140 return true;
144 default:
145 break;
148 else if (is_gimple_assign (stmt))
150 ao_ref_init (write, gimple_assign_lhs (stmt));
151 return true;
153 return false;
156 /* Given REF from the the alias oracle, return TRUE if it is a valid
157 memory reference for dead store elimination, false otherwise.
159 In particular, the reference must have a known base, known maximum
160 size, start at a byte offset and have a size that is one or more
161 bytes. */
163 static bool
164 valid_ao_ref_for_dse (ao_ref *ref)
166 return (ao_ref_base (ref)
167 && known_size_p (ref->max_size)
168 && maybe_ne (ref->size, 0)
169 && known_eq (ref->max_size, ref->size)
170 && known_ge (ref->offset, 0)
171 && multiple_p (ref->offset, BITS_PER_UNIT)
172 && multiple_p (ref->size, BITS_PER_UNIT));
175 /* Try to normalize COPY (an ao_ref) relative to REF. Essentially when we are
176 done COPY will only refer bytes found within REF. Return true if COPY
177 is known to intersect at least one byte of REF. */
179 static bool
180 normalize_ref (ao_ref *copy, ao_ref *ref)
182 if (!ordered_p (copy->offset, ref->offset))
183 return false;
185 /* If COPY starts before REF, then reset the beginning of
186 COPY to match REF and decrease the size of COPY by the
187 number of bytes removed from COPY. */
188 if (maybe_lt (copy->offset, ref->offset))
190 poly_int64 diff = ref->offset - copy->offset;
191 if (maybe_le (copy->size, diff))
192 return false;
193 copy->size -= diff;
194 copy->offset = ref->offset;
197 poly_int64 diff = copy->offset - ref->offset;
198 if (maybe_le (ref->size, diff))
199 return false;
201 /* If COPY extends beyond REF, chop off its size appropriately. */
202 poly_int64 limit = ref->size - diff;
203 if (!ordered_p (limit, copy->size))
204 return false;
206 if (maybe_gt (copy->size, limit))
207 copy->size = limit;
208 return true;
211 /* Clear any bytes written by STMT from the bitmap LIVE_BYTES. The base
212 address written by STMT must match the one found in REF, which must
213 have its base address previously initialized.
215 This routine must be conservative. If we don't know the offset or
216 actual size written, assume nothing was written. */
218 static void
219 clear_bytes_written_by (sbitmap live_bytes, gimple *stmt, ao_ref *ref)
221 ao_ref write;
222 if (!initialize_ao_ref_for_dse (stmt, &write))
223 return;
225 /* Verify we have the same base memory address, the write
226 has a known size and overlaps with REF. */
227 HOST_WIDE_INT start, size;
228 if (valid_ao_ref_for_dse (&write)
229 && operand_equal_p (write.base, ref->base, OEP_ADDRESS_OF)
230 && known_eq (write.size, write.max_size)
231 && normalize_ref (&write, ref)
232 && (write.offset - ref->offset).is_constant (&start)
233 && write.size.is_constant (&size))
234 bitmap_clear_range (live_bytes, start / BITS_PER_UNIT,
235 size / BITS_PER_UNIT);
238 /* REF is a memory write. Extract relevant information from it and
239 initialize the LIVE_BYTES bitmap. If successful, return TRUE.
240 Otherwise return FALSE. */
242 static bool
243 setup_live_bytes_from_ref (ao_ref *ref, sbitmap live_bytes)
245 HOST_WIDE_INT const_size;
246 if (valid_ao_ref_for_dse (ref)
247 && ref->size.is_constant (&const_size)
248 && (const_size / BITS_PER_UNIT
249 <= PARAM_VALUE (PARAM_DSE_MAX_OBJECT_SIZE)))
251 bitmap_clear (live_bytes);
252 bitmap_set_range (live_bytes, 0, const_size / BITS_PER_UNIT);
253 return true;
255 return false;
258 /* Compute the number of elements that we can trim from the head and
259 tail of ORIG resulting in a bitmap that is a superset of LIVE.
261 Store the number of elements trimmed from the head and tail in
262 TRIM_HEAD and TRIM_TAIL.
264 STMT is the statement being trimmed and is used for debugging dump
265 output only. */
267 static void
268 compute_trims (ao_ref *ref, sbitmap live, int *trim_head, int *trim_tail,
269 gimple *stmt)
271 /* We use sbitmaps biased such that ref->offset is bit zero and the bitmap
272 extends through ref->size. So we know that in the original bitmap
273 bits 0..ref->size were true. We don't actually need the bitmap, just
274 the REF to compute the trims. */
276 /* Now identify how much, if any of the tail we can chop off. */
277 HOST_WIDE_INT const_size;
278 int last_live = bitmap_last_set_bit (live);
279 if (ref->size.is_constant (&const_size))
281 int last_orig = (const_size / BITS_PER_UNIT) - 1;
282 /* We can leave inconvenient amounts on the tail as
283 residual handling in mem* and str* functions is usually
284 reasonably efficient. */
285 *trim_tail = last_orig - last_live;
287 /* But don't trim away out of bounds accesses, as this defeats
288 proper warnings.
290 We could have a type with no TYPE_SIZE_UNIT or we could have a VLA
291 where TYPE_SIZE_UNIT is not a constant. */
292 if (*trim_tail
293 && TYPE_SIZE_UNIT (TREE_TYPE (ref->base))
294 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref->base))) == INTEGER_CST
295 && compare_tree_int (TYPE_SIZE_UNIT (TREE_TYPE (ref->base)),
296 last_orig) <= 0)
297 *trim_tail = 0;
299 else
300 *trim_tail = 0;
302 /* Identify how much, if any of the head we can chop off. */
303 int first_orig = 0;
304 int first_live = bitmap_first_set_bit (live);
305 *trim_head = first_live - first_orig;
307 /* If more than a word remains, then make sure to keep the
308 starting point at least word aligned. */
309 if (last_live - first_live > UNITS_PER_WORD)
310 *trim_head &= ~(UNITS_PER_WORD - 1);
312 if ((*trim_head || *trim_tail)
313 && dump_file && (dump_flags & TDF_DETAILS))
315 fprintf (dump_file, " Trimming statement (head = %d, tail = %d): ",
316 *trim_head, *trim_tail);
317 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
318 fprintf (dump_file, "\n");
322 /* STMT initializes an object from COMPLEX_CST where one or more of the
323 bytes written may be dead stores. REF is a representation of the
324 memory written. LIVE is the bitmap of stores that are actually live.
326 Attempt to rewrite STMT so that only the real or imaginary part of
327 the object is actually stored. */
329 static void
330 maybe_trim_complex_store (ao_ref *ref, sbitmap live, gimple *stmt)
332 int trim_head, trim_tail;
333 compute_trims (ref, live, &trim_head, &trim_tail, stmt);
335 /* The amount of data trimmed from the head or tail must be at
336 least half the size of the object to ensure we're trimming
337 the entire real or imaginary half. By writing things this
338 way we avoid more O(n) bitmap operations. */
339 if (known_ge (trim_tail * 2 * BITS_PER_UNIT, ref->size))
341 /* TREE_REALPART is live */
342 tree x = TREE_REALPART (gimple_assign_rhs1 (stmt));
343 tree y = gimple_assign_lhs (stmt);
344 y = build1 (REALPART_EXPR, TREE_TYPE (x), y);
345 gimple_assign_set_lhs (stmt, y);
346 gimple_assign_set_rhs1 (stmt, x);
348 else if (known_ge (trim_head * 2 * BITS_PER_UNIT, ref->size))
350 /* TREE_IMAGPART is live */
351 tree x = TREE_IMAGPART (gimple_assign_rhs1 (stmt));
352 tree y = gimple_assign_lhs (stmt);
353 y = build1 (IMAGPART_EXPR, TREE_TYPE (x), y);
354 gimple_assign_set_lhs (stmt, y);
355 gimple_assign_set_rhs1 (stmt, x);
358 /* Other cases indicate parts of both the real and imag subobjects
359 are live. We do not try to optimize those cases. */
362 /* STMT initializes an object using a CONSTRUCTOR where one or more of the
363 bytes written are dead stores. ORIG is the bitmap of bytes stored by
364 STMT. LIVE is the bitmap of stores that are actually live.
366 Attempt to rewrite STMT so that only the real or imaginary part of
367 the object is actually stored.
369 The most common case for getting here is a CONSTRUCTOR with no elements
370 being used to zero initialize an object. We do not try to handle other
371 cases as those would force us to fully cover the object with the
372 CONSTRUCTOR node except for the components that are dead. */
374 static void
375 maybe_trim_constructor_store (ao_ref *ref, sbitmap live, gimple *stmt)
377 tree ctor = gimple_assign_rhs1 (stmt);
379 /* This is the only case we currently handle. It actually seems to
380 catch most cases of actual interest. */
381 gcc_assert (CONSTRUCTOR_NELTS (ctor) == 0);
383 int head_trim = 0;
384 int tail_trim = 0;
385 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
387 /* Now we want to replace the constructor initializer
388 with memset (object + head_trim, 0, size - head_trim - tail_trim). */
389 if (head_trim || tail_trim)
391 /* We want &lhs for the MEM_REF expression. */
392 tree lhs_addr = build_fold_addr_expr (gimple_assign_lhs (stmt));
394 if (! is_gimple_min_invariant (lhs_addr))
395 return;
397 /* The number of bytes for the new constructor. */
398 poly_int64 ref_bytes = exact_div (ref->size, BITS_PER_UNIT);
399 poly_int64 count = ref_bytes - head_trim - tail_trim;
401 /* And the new type for the CONSTRUCTOR. Essentially it's just
402 a char array large enough to cover the non-trimmed parts of
403 the original CONSTRUCTOR. Note we want explicit bounds here
404 so that we know how many bytes to clear when expanding the
405 CONSTRUCTOR. */
406 tree type = build_array_type_nelts (char_type_node, count);
408 /* Build a suitable alias type rather than using alias set zero
409 to avoid pessimizing. */
410 tree alias_type = reference_alias_ptr_type (gimple_assign_lhs (stmt));
412 /* Build a MEM_REF representing the whole accessed area, starting
413 at the first byte not trimmed. */
414 tree exp = fold_build2 (MEM_REF, type, lhs_addr,
415 build_int_cst (alias_type, head_trim));
417 /* Now update STMT with a new RHS and LHS. */
418 gimple_assign_set_lhs (stmt, exp);
419 gimple_assign_set_rhs1 (stmt, build_constructor (type, NULL));
423 /* STMT is a memcpy, memmove or memset. Decrement the number of bytes
424 copied/set by DECREMENT. */
425 static void
426 decrement_count (gimple *stmt, int decrement)
428 tree *countp = gimple_call_arg_ptr (stmt, 2);
429 gcc_assert (TREE_CODE (*countp) == INTEGER_CST);
430 *countp = wide_int_to_tree (TREE_TYPE (*countp), (TREE_INT_CST_LOW (*countp)
431 - decrement));
435 static void
436 increment_start_addr (gimple *stmt, tree *where, int increment)
438 if (TREE_CODE (*where) == SSA_NAME)
440 tree tem = make_ssa_name (TREE_TYPE (*where));
441 gassign *newop
442 = gimple_build_assign (tem, POINTER_PLUS_EXPR, *where,
443 build_int_cst (sizetype, increment));
444 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
445 gsi_insert_before (&gsi, newop, GSI_SAME_STMT);
446 *where = tem;
447 update_stmt (gsi_stmt (gsi));
448 return;
451 *where = build_fold_addr_expr (fold_build2 (MEM_REF, char_type_node,
452 *where,
453 build_int_cst (ptr_type_node,
454 increment)));
457 /* STMT is builtin call that writes bytes in bitmap ORIG, some bytes are dead
458 (ORIG & ~NEW) and need not be stored. Try to rewrite STMT to reduce
459 the amount of data it actually writes.
461 Right now we only support trimming from the head or the tail of the
462 memory region. In theory we could split the mem* call, but it's
463 likely of marginal value. */
465 static void
466 maybe_trim_memstar_call (ao_ref *ref, sbitmap live, gimple *stmt)
468 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
470 case BUILT_IN_MEMCPY:
471 case BUILT_IN_MEMMOVE:
472 case BUILT_IN_MEMCPY_CHK:
473 case BUILT_IN_MEMMOVE_CHK:
475 int head_trim, tail_trim;
476 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
478 /* Tail trimming is easy, we can just reduce the count. */
479 if (tail_trim)
480 decrement_count (stmt, tail_trim);
482 /* Head trimming requires adjusting all the arguments. */
483 if (head_trim)
485 tree *dst = gimple_call_arg_ptr (stmt, 0);
486 increment_start_addr (stmt, dst, head_trim);
487 tree *src = gimple_call_arg_ptr (stmt, 1);
488 increment_start_addr (stmt, src, head_trim);
489 decrement_count (stmt, head_trim);
491 break;
494 case BUILT_IN_MEMSET:
495 case BUILT_IN_MEMSET_CHK:
497 int head_trim, tail_trim;
498 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
500 /* Tail trimming is easy, we can just reduce the count. */
501 if (tail_trim)
502 decrement_count (stmt, tail_trim);
504 /* Head trimming requires adjusting all the arguments. */
505 if (head_trim)
507 tree *dst = gimple_call_arg_ptr (stmt, 0);
508 increment_start_addr (stmt, dst, head_trim);
509 decrement_count (stmt, head_trim);
511 break;
514 default:
515 break;
519 /* STMT is a memory write where one or more bytes written are dead
520 stores. ORIG is the bitmap of bytes stored by STMT. LIVE is the
521 bitmap of stores that are actually live.
523 Attempt to rewrite STMT so that it writes fewer memory locations. Right
524 now we only support trimming at the start or end of the memory region.
525 It's not clear how much there is to be gained by trimming from the middle
526 of the region. */
528 static void
529 maybe_trim_partially_dead_store (ao_ref *ref, sbitmap live, gimple *stmt)
531 if (is_gimple_assign (stmt)
532 && TREE_CODE (gimple_assign_lhs (stmt)) != TARGET_MEM_REF)
534 switch (gimple_assign_rhs_code (stmt))
536 case CONSTRUCTOR:
537 maybe_trim_constructor_store (ref, live, stmt);
538 break;
539 case COMPLEX_CST:
540 maybe_trim_complex_store (ref, live, stmt);
541 break;
542 default:
543 break;
548 /* Return TRUE if USE_REF reads bytes from LIVE where live is
549 derived from REF, a write reference.
551 While this routine may modify USE_REF, it's passed by value, not
552 location. So callers do not see those modifications. */
554 static bool
555 live_bytes_read (ao_ref use_ref, ao_ref *ref, sbitmap live)
557 /* We have already verified that USE_REF and REF hit the same object.
558 Now verify that there's actually an overlap between USE_REF and REF. */
559 HOST_WIDE_INT start, size;
560 if (normalize_ref (&use_ref, ref)
561 && (use_ref.offset - ref->offset).is_constant (&start)
562 && use_ref.size.is_constant (&size))
564 /* If USE_REF covers all of REF, then it will hit one or more
565 live bytes. This avoids useless iteration over the bitmap
566 below. */
567 if (start == 0 && known_eq (size, ref->size))
568 return true;
570 /* Now check if any of the remaining bits in use_ref are set in LIVE. */
571 return bitmap_bit_in_range_p (live, start / BITS_PER_UNIT,
572 (start + size - 1) / BITS_PER_UNIT);
574 return true;
577 /* Callback for dse_classify_store calling for_each_index. Verify that
578 indices are invariant in the loop with backedge PHI in basic-block DATA. */
580 static bool
581 check_name (tree, tree *idx, void *data)
583 basic_block phi_bb = (basic_block) data;
584 if (TREE_CODE (*idx) == SSA_NAME
585 && !SSA_NAME_IS_DEFAULT_DEF (*idx)
586 && dominated_by_p (CDI_DOMINATORS, gimple_bb (SSA_NAME_DEF_STMT (*idx)),
587 phi_bb))
588 return false;
589 return true;
592 /* STMT stores the value 0 into one or more memory locations
593 (via memset, empty constructor, calloc call, etc).
595 See if there is a subsequent store of the value 0 to one
596 or more of the same memory location(s). If so, the subsequent
597 store is redundant and can be removed.
599 The subsequent stores could be via memset, empty constructors,
600 simple MEM stores, etc. */
602 static void
603 dse_optimize_redundant_stores (gimple *stmt)
605 int cnt = 0;
607 /* We could do something fairly complex and look through PHIs
608 like DSE_CLASSIFY_STORE, but it doesn't seem to be worth
609 the effort.
611 Look at all the immediate uses of the VDEF (which are obviously
612 dominated by STMT). See if one or more stores 0 into the same
613 memory locations a STMT, if so remove the immediate use statements. */
614 tree defvar = gimple_vdef (stmt);
615 imm_use_iterator ui;
616 gimple *use_stmt;
617 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
619 /* Limit stmt walking. */
620 if (++cnt > PARAM_VALUE (PARAM_DSE_MAX_ALIAS_QUERIES_PER_STORE))
621 BREAK_FROM_IMM_USE_STMT (ui);
623 /* If USE_STMT stores 0 into one or more of the same locations
624 as STMT and STMT would kill USE_STMT, then we can just remove
625 USE_STMT. */
626 tree fndecl;
627 if ((is_gimple_assign (use_stmt)
628 && gimple_vdef (use_stmt)
629 && ((gimple_assign_rhs_code (use_stmt) == CONSTRUCTOR
630 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (use_stmt)) == 0
631 && !gimple_clobber_p (stmt))
632 || (gimple_assign_rhs_code (use_stmt) == INTEGER_CST
633 && integer_zerop (gimple_assign_rhs1 (use_stmt)))))
634 || (gimple_call_builtin_p (use_stmt, BUILT_IN_NORMAL)
635 && (fndecl = gimple_call_fndecl (use_stmt)) != NULL
636 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
637 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET_CHK)
638 && integer_zerop (gimple_call_arg (use_stmt, 1))))
640 ao_ref write;
642 if (!initialize_ao_ref_for_dse (use_stmt, &write))
643 BREAK_FROM_IMM_USE_STMT (ui)
645 if (valid_ao_ref_for_dse (&write)
646 && stmt_kills_ref_p (stmt, &write))
648 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
649 if (is_gimple_assign (use_stmt))
650 delete_dead_or_redundant_assignment (&gsi, "redundant");
651 else if (is_gimple_call (use_stmt))
652 delete_dead_or_redundant_call (&gsi, "redundant");
653 else
654 gcc_unreachable ();
660 /* A helper of dse_optimize_stmt.
661 Given a GIMPLE_ASSIGN in STMT that writes to REF, classify it
662 according to downstream uses and defs. Sets *BY_CLOBBER_P to true
663 if only clobber statements influenced the classification result.
664 Returns the classification. */
666 static dse_store_status
667 dse_classify_store (ao_ref *ref, gimple *stmt,
668 bool byte_tracking_enabled, sbitmap live_bytes,
669 bool *by_clobber_p = NULL)
671 gimple *temp;
672 int cnt = 0;
673 auto_bitmap visited;
675 if (by_clobber_p)
676 *by_clobber_p = true;
678 /* Find the first dominated statement that clobbers (part of) the
679 memory stmt stores to with no intermediate statement that may use
680 part of the memory stmt stores. That is, find a store that may
681 prove stmt to be a dead store. */
682 temp = stmt;
685 gimple *use_stmt;
686 imm_use_iterator ui;
687 bool fail = false;
688 tree defvar;
690 if (gimple_code (temp) == GIMPLE_PHI)
692 /* If we visit this PHI by following a backedge then we have to
693 make sure ref->ref only refers to SSA names that are invariant
694 with respect to the loop represented by this PHI node. */
695 if (dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt),
696 gimple_bb (temp))
697 && !for_each_index (ref->ref ? &ref->ref : &ref->base,
698 check_name, gimple_bb (temp)))
699 return DSE_STORE_LIVE;
700 defvar = PHI_RESULT (temp);
701 bitmap_set_bit (visited, SSA_NAME_VERSION (defvar));
703 else
704 defvar = gimple_vdef (temp);
705 auto_vec<gimple *, 10> defs;
706 gimple *phi_def = NULL;
707 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
709 /* Limit stmt walking. */
710 if (++cnt > PARAM_VALUE (PARAM_DSE_MAX_ALIAS_QUERIES_PER_STORE))
712 fail = true;
713 BREAK_FROM_IMM_USE_STMT (ui);
716 /* We have visited ourselves already so ignore STMT for the
717 purpose of chaining. */
718 if (use_stmt == stmt)
720 /* In simple cases we can look through PHI nodes, but we
721 have to be careful with loops and with memory references
722 containing operands that are also operands of PHI nodes.
723 See gcc.c-torture/execute/20051110-*.c. */
724 else if (gimple_code (use_stmt) == GIMPLE_PHI)
726 /* If we already visited this PHI ignore it for further
727 processing. */
728 if (!bitmap_bit_p (visited,
729 SSA_NAME_VERSION (PHI_RESULT (use_stmt))))
731 defs.safe_push (use_stmt);
732 phi_def = use_stmt;
735 /* If the statement is a use the store is not dead. */
736 else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
738 /* Handle common cases where we can easily build an ao_ref
739 structure for USE_STMT and in doing so we find that the
740 references hit non-live bytes and thus can be ignored. */
741 if (byte_tracking_enabled
742 && is_gimple_assign (use_stmt))
744 ao_ref use_ref;
745 ao_ref_init (&use_ref, gimple_assign_rhs1 (use_stmt));
746 if (valid_ao_ref_for_dse (&use_ref)
747 && use_ref.base == ref->base
748 && known_eq (use_ref.size, use_ref.max_size)
749 && !live_bytes_read (use_ref, ref, live_bytes))
751 /* If this is a store, remember it as we possibly
752 need to walk the defs uses. */
753 if (gimple_vdef (use_stmt))
754 defs.safe_push (use_stmt);
755 continue;
759 fail = true;
760 BREAK_FROM_IMM_USE_STMT (ui);
762 /* If this is a store, remember it as we possibly need to walk the
763 defs uses. */
764 else if (gimple_vdef (use_stmt))
765 defs.safe_push (use_stmt);
768 if (fail)
770 /* STMT might be partially dead and we may be able to reduce
771 how many memory locations it stores into. */
772 if (byte_tracking_enabled && !gimple_clobber_p (stmt))
773 return DSE_STORE_MAYBE_PARTIAL_DEAD;
774 return DSE_STORE_LIVE;
777 /* If we didn't find any definition this means the store is dead
778 if it isn't a store to global reachable memory. In this case
779 just pretend the stmt makes itself dead. Otherwise fail. */
780 if (defs.is_empty ())
782 if (ref_may_alias_global_p (ref))
783 return DSE_STORE_LIVE;
785 if (by_clobber_p)
786 *by_clobber_p = false;
787 return DSE_STORE_DEAD;
790 /* Process defs and remove those we need not process further. */
791 for (unsigned i = 0; i < defs.length ();)
793 gimple *def = defs[i];
794 gimple *use_stmt;
795 use_operand_p use_p;
796 /* If the path to check starts with a kill we do not need to
797 process it further.
798 ??? With byte tracking we need only kill the bytes currently
799 live. */
800 if (stmt_kills_ref_p (def, ref))
802 if (by_clobber_p && !gimple_clobber_p (def))
803 *by_clobber_p = false;
804 defs.unordered_remove (i);
806 /* In addition to kills we can remove defs whose only use
807 is another def in defs. That can only ever be PHIs of which
808 we track a single for simplicity reasons (we fail for multiple
809 PHIs anyways). We can also ignore defs that feed only into
810 already visited PHIs. */
811 else if (gimple_code (def) != GIMPLE_PHI
812 && single_imm_use (gimple_vdef (def), &use_p, &use_stmt)
813 && (use_stmt == phi_def
814 || (gimple_code (use_stmt) == GIMPLE_PHI
815 && bitmap_bit_p (visited,
816 SSA_NAME_VERSION
817 (PHI_RESULT (use_stmt))))))
818 defs.unordered_remove (i);
819 else
820 ++i;
823 /* If all defs kill the ref we are done. */
824 if (defs.is_empty ())
825 return DSE_STORE_DEAD;
826 /* If more than one def survives fail. */
827 if (defs.length () > 1)
829 /* STMT might be partially dead and we may be able to reduce
830 how many memory locations it stores into. */
831 if (byte_tracking_enabled && !gimple_clobber_p (stmt))
832 return DSE_STORE_MAYBE_PARTIAL_DEAD;
833 return DSE_STORE_LIVE;
835 temp = defs[0];
837 /* Track partial kills. */
838 if (byte_tracking_enabled)
840 clear_bytes_written_by (live_bytes, temp, ref);
841 if (bitmap_empty_p (live_bytes))
843 if (by_clobber_p && !gimple_clobber_p (temp))
844 *by_clobber_p = false;
845 return DSE_STORE_DEAD;
849 /* Continue walking until there are no more live bytes. */
850 while (1);
854 class dse_dom_walker : public dom_walker
856 public:
857 dse_dom_walker (cdi_direction direction)
858 : dom_walker (direction),
859 m_live_bytes (PARAM_VALUE (PARAM_DSE_MAX_OBJECT_SIZE)),
860 m_byte_tracking_enabled (false) {}
862 virtual edge before_dom_children (basic_block);
864 private:
865 auto_sbitmap m_live_bytes;
866 bool m_byte_tracking_enabled;
867 void dse_optimize_stmt (gimple_stmt_iterator *);
870 /* Delete a dead call at GSI, which is mem* call of some kind. */
871 static void
872 delete_dead_or_redundant_call (gimple_stmt_iterator *gsi, const char *type)
874 gimple *stmt = gsi_stmt (*gsi);
875 if (dump_file && (dump_flags & TDF_DETAILS))
877 fprintf (dump_file, " Deleted %s call: ", type);
878 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
879 fprintf (dump_file, "\n");
882 tree lhs = gimple_call_lhs (stmt);
883 if (lhs)
885 tree ptr = gimple_call_arg (stmt, 0);
886 gimple *new_stmt = gimple_build_assign (lhs, ptr);
887 unlink_stmt_vdef (stmt);
888 if (gsi_replace (gsi, new_stmt, true))
889 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
891 else
893 /* Then we need to fix the operand of the consuming stmt. */
894 unlink_stmt_vdef (stmt);
896 /* Remove the dead store. */
897 if (gsi_remove (gsi, true))
898 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
899 release_defs (stmt);
903 /* Delete a dead store at GSI, which is a gimple assignment. */
905 static void
906 delete_dead_or_redundant_assignment (gimple_stmt_iterator *gsi, const char *type)
908 gimple *stmt = gsi_stmt (*gsi);
909 if (dump_file && (dump_flags & TDF_DETAILS))
911 fprintf (dump_file, " Deleted %s store: ", type);
912 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
913 fprintf (dump_file, "\n");
916 /* Then we need to fix the operand of the consuming stmt. */
917 unlink_stmt_vdef (stmt);
919 /* Remove the dead store. */
920 basic_block bb = gimple_bb (stmt);
921 if (gsi_remove (gsi, true))
922 bitmap_set_bit (need_eh_cleanup, bb->index);
924 /* And release any SSA_NAMEs set in this statement back to the
925 SSA_NAME manager. */
926 release_defs (stmt);
929 /* Attempt to eliminate dead stores in the statement referenced by BSI.
931 A dead store is a store into a memory location which will later be
932 overwritten by another store without any intervening loads. In this
933 case the earlier store can be deleted.
935 In our SSA + virtual operand world we use immediate uses of virtual
936 operands to detect dead stores. If a store's virtual definition
937 is used precisely once by a later store to the same location which
938 post dominates the first store, then the first store is dead. */
940 void
941 dse_dom_walker::dse_optimize_stmt (gimple_stmt_iterator *gsi)
943 gimple *stmt = gsi_stmt (*gsi);
945 /* If this statement has no virtual defs, then there is nothing
946 to do. */
947 if (!gimple_vdef (stmt))
948 return;
950 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
951 if (gimple_has_volatile_ops (stmt)
952 && (!gimple_clobber_p (stmt)
953 || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
954 return;
956 ao_ref ref;
957 if (!initialize_ao_ref_for_dse (stmt, &ref))
958 return;
960 /* We know we have virtual definitions. We can handle assignments and
961 some builtin calls. */
962 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
964 tree fndecl = gimple_call_fndecl (stmt);
965 switch (DECL_FUNCTION_CODE (fndecl))
967 case BUILT_IN_MEMCPY:
968 case BUILT_IN_MEMMOVE:
969 case BUILT_IN_MEMSET:
970 case BUILT_IN_MEMCPY_CHK:
971 case BUILT_IN_MEMMOVE_CHK:
972 case BUILT_IN_MEMSET_CHK:
974 /* Occasionally calls with an explicit length of zero
975 show up in the IL. It's pointless to do analysis
976 on them, they're trivially dead. */
977 tree size = gimple_call_arg (stmt, 2);
978 if (integer_zerop (size))
980 delete_dead_or_redundant_call (gsi, "dead");
981 return;
984 /* If this is a memset call that initializes an object
985 to zero, it may be redundant with an earlier memset
986 or empty CONSTRUCTOR of a larger object. */
987 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
988 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET_CHK)
989 && integer_zerop (gimple_call_arg (stmt, 1)))
990 dse_optimize_redundant_stores (stmt);
992 enum dse_store_status store_status;
993 m_byte_tracking_enabled
994 = setup_live_bytes_from_ref (&ref, m_live_bytes);
995 store_status = dse_classify_store (&ref, stmt,
996 m_byte_tracking_enabled,
997 m_live_bytes);
998 if (store_status == DSE_STORE_LIVE)
999 return;
1001 if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
1003 maybe_trim_memstar_call (&ref, m_live_bytes, stmt);
1004 return;
1007 if (store_status == DSE_STORE_DEAD)
1008 delete_dead_or_redundant_call (gsi, "dead");
1009 return;
1012 case BUILT_IN_CALLOC:
1013 /* We already know the arguments are integer constants. */
1014 dse_optimize_redundant_stores (stmt);
1016 default:
1017 return;
1021 if (is_gimple_assign (stmt))
1023 bool by_clobber_p = false;
1025 /* First see if this store is a CONSTRUCTOR and if there
1026 are subsequent CONSTRUCTOR stores which are totally
1027 subsumed by this statement. If so remove the subsequent
1028 CONSTRUCTOR store.
1030 This will tend to make fewer calls into memset with longer
1031 arguments. */
1032 if (gimple_assign_rhs_code (stmt) == CONSTRUCTOR
1033 && CONSTRUCTOR_NELTS (gimple_assign_rhs1 (stmt)) == 0
1034 && !gimple_clobber_p (stmt))
1035 dse_optimize_redundant_stores (stmt);
1037 /* Self-assignments are zombies. */
1038 if (operand_equal_p (gimple_assign_rhs1 (stmt),
1039 gimple_assign_lhs (stmt), 0))
1041 else
1043 m_byte_tracking_enabled
1044 = setup_live_bytes_from_ref (&ref, m_live_bytes);
1045 enum dse_store_status store_status;
1046 store_status = dse_classify_store (&ref, stmt,
1047 m_byte_tracking_enabled,
1048 m_live_bytes, &by_clobber_p);
1049 if (store_status == DSE_STORE_LIVE)
1050 return;
1052 if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
1054 maybe_trim_partially_dead_store (&ref, m_live_bytes, stmt);
1055 return;
1059 /* Now we know that use_stmt kills the LHS of stmt. */
1061 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
1062 another clobber stmt. */
1063 if (gimple_clobber_p (stmt)
1064 && !by_clobber_p)
1065 return;
1067 delete_dead_or_redundant_assignment (gsi, "dead");
1071 edge
1072 dse_dom_walker::before_dom_children (basic_block bb)
1074 gimple_stmt_iterator gsi;
1076 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
1078 dse_optimize_stmt (&gsi);
1079 if (gsi_end_p (gsi))
1080 gsi = gsi_last_bb (bb);
1081 else
1082 gsi_prev (&gsi);
1084 return NULL;
1087 namespace {
1089 const pass_data pass_data_dse =
1091 GIMPLE_PASS, /* type */
1092 "dse", /* name */
1093 OPTGROUP_NONE, /* optinfo_flags */
1094 TV_TREE_DSE, /* tv_id */
1095 ( PROP_cfg | PROP_ssa ), /* properties_required */
1096 0, /* properties_provided */
1097 0, /* properties_destroyed */
1098 0, /* todo_flags_start */
1099 0, /* todo_flags_finish */
1102 class pass_dse : public gimple_opt_pass
1104 public:
1105 pass_dse (gcc::context *ctxt)
1106 : gimple_opt_pass (pass_data_dse, ctxt)
1109 /* opt_pass methods: */
1110 opt_pass * clone () { return new pass_dse (m_ctxt); }
1111 virtual bool gate (function *) { return flag_tree_dse != 0; }
1112 virtual unsigned int execute (function *);
1114 }; // class pass_dse
1116 unsigned int
1117 pass_dse::execute (function *fun)
1119 need_eh_cleanup = BITMAP_ALLOC (NULL);
1121 renumber_gimple_stmt_uids ();
1123 /* We might consider making this a property of each pass so that it
1124 can be [re]computed on an as-needed basis. Particularly since
1125 this pass could be seen as an extension of DCE which needs post
1126 dominators. */
1127 calculate_dominance_info (CDI_POST_DOMINATORS);
1128 calculate_dominance_info (CDI_DOMINATORS);
1130 /* Dead store elimination is fundamentally a walk of the post-dominator
1131 tree and a backwards walk of statements within each block. */
1132 dse_dom_walker (CDI_POST_DOMINATORS).walk (fun->cfg->x_exit_block_ptr);
1134 /* Removal of stores may make some EH edges dead. Purge such edges from
1135 the CFG as needed. */
1136 if (!bitmap_empty_p (need_eh_cleanup))
1138 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
1139 cleanup_tree_cfg ();
1142 BITMAP_FREE (need_eh_cleanup);
1144 /* For now, just wipe the post-dominator information. */
1145 free_dominance_info (CDI_POST_DOMINATORS);
1146 return 0;
1149 } // anon namespace
1151 gimple_opt_pass *
1152 make_pass_dse (gcc::context *ctxt)
1154 return new pass_dse (ctxt);