re PR debug/91929 (missing inline subroutine information in build using sin/cos)
[official-gcc.git] / gcc / tree-ssa-dse.c
blob25cd4709b3111b300de80b06b59034e2a6e655a4
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"
39 #include "tree-ssa-dse.h"
41 /* This file implements dead store elimination.
43 A dead store is a store into a memory location which will later be
44 overwritten by another store without any intervening loads. In this
45 case the earlier store can be deleted or trimmed if the store
46 was partially dead.
48 A redundant store is a store into a memory location which stores
49 the exact same value as a prior store to the same memory location.
50 While this can often be handled by dead store elimination, removing
51 the redundant store is often better than removing or trimming the
52 dead store.
54 In our SSA + virtual operand world we use immediate uses of virtual
55 operands to detect these cases. If a store's virtual definition
56 is used precisely once by a later store to the same location which
57 post dominates the first store, then the first store is dead. If
58 the data stored is the same, then the second store is redundant.
60 The single use of the store's virtual definition ensures that
61 there are no intervening aliased loads and the requirement that
62 the second load post dominate the first ensures that if the earlier
63 store executes, then the later stores will execute before the function
64 exits.
66 It may help to think of this as first moving the earlier store to
67 the point immediately before the later store. Again, the single
68 use of the virtual definition and the post-dominance relationship
69 ensure that such movement would be safe. Clearly if there are
70 back to back stores, then the second is makes the first dead. If
71 the second store stores the same value, then the second store is
72 redundant.
74 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
75 may also help in understanding this code since it discusses the
76 relationship between dead store and redundant load elimination. In
77 fact, they are the same transformation applied to different views of
78 the CFG. */
80 void delete_dead_or_redundant_assignment (gimple_stmt_iterator *, const char *);
81 static void delete_dead_or_redundant_call (gimple_stmt_iterator *, const char *);
83 /* Bitmap of blocks that have had EH statements cleaned. We should
84 remove their dead edges eventually. */
85 static bitmap need_eh_cleanup;
87 /* STMT is a statement that may write into memory. Analyze it and
88 initialize WRITE to describe how STMT affects memory.
90 Return TRUE if the the statement was analyzed, FALSE otherwise.
92 It is always safe to return FALSE. But typically better optimziation
93 can be achieved by analyzing more statements. */
95 static bool
96 initialize_ao_ref_for_dse (gimple *stmt, ao_ref *write)
98 /* It's advantageous to handle certain mem* functions. */
99 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
101 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
103 case BUILT_IN_MEMCPY:
104 case BUILT_IN_MEMMOVE:
105 case BUILT_IN_MEMSET:
106 case BUILT_IN_MEMCPY_CHK:
107 case BUILT_IN_MEMMOVE_CHK:
108 case BUILT_IN_MEMSET_CHK:
109 case BUILT_IN_STRNCPY:
110 case BUILT_IN_STRNCPY_CHK:
112 tree size = gimple_call_arg (stmt, 2);
113 tree ptr = gimple_call_arg (stmt, 0);
114 ao_ref_init_from_ptr_and_size (write, ptr, size);
115 return true;
118 /* A calloc call can never be dead, but it can make
119 subsequent stores redundant if they store 0 into
120 the same memory locations. */
121 case BUILT_IN_CALLOC:
123 tree nelem = gimple_call_arg (stmt, 0);
124 tree selem = gimple_call_arg (stmt, 1);
125 tree lhs;
126 if (TREE_CODE (nelem) == INTEGER_CST
127 && TREE_CODE (selem) == INTEGER_CST
128 && (lhs = gimple_call_lhs (stmt)) != NULL_TREE)
130 tree size = fold_build2 (MULT_EXPR, TREE_TYPE (nelem),
131 nelem, selem);
132 ao_ref_init_from_ptr_and_size (write, lhs, size);
133 return true;
137 default:
138 break;
141 else if (is_gimple_assign (stmt))
143 ao_ref_init (write, gimple_assign_lhs (stmt));
144 return true;
146 return false;
149 /* Given REF from the the alias oracle, return TRUE if it is a valid
150 memory reference for dead store elimination, false otherwise.
152 In particular, the reference must have a known base, known maximum
153 size, start at a byte offset and have a size that is one or more
154 bytes. */
156 static bool
157 valid_ao_ref_for_dse (ao_ref *ref)
159 return (ao_ref_base (ref)
160 && known_size_p (ref->max_size)
161 && maybe_ne (ref->size, 0)
162 && known_eq (ref->max_size, ref->size)
163 && known_ge (ref->offset, 0)
164 && multiple_p (ref->offset, BITS_PER_UNIT)
165 && multiple_p (ref->size, BITS_PER_UNIT));
168 /* Try to normalize COPY (an ao_ref) relative to REF. Essentially when we are
169 done COPY will only refer bytes found within REF. Return true if COPY
170 is known to intersect at least one byte of REF. */
172 static bool
173 normalize_ref (ao_ref *copy, ao_ref *ref)
175 if (!ordered_p (copy->offset, ref->offset))
176 return false;
178 /* If COPY starts before REF, then reset the beginning of
179 COPY to match REF and decrease the size of COPY by the
180 number of bytes removed from COPY. */
181 if (maybe_lt (copy->offset, ref->offset))
183 poly_int64 diff = ref->offset - copy->offset;
184 if (maybe_le (copy->size, diff))
185 return false;
186 copy->size -= diff;
187 copy->offset = ref->offset;
190 poly_int64 diff = copy->offset - ref->offset;
191 if (maybe_le (ref->size, diff))
192 return false;
194 /* If COPY extends beyond REF, chop off its size appropriately. */
195 poly_int64 limit = ref->size - diff;
196 if (!ordered_p (limit, copy->size))
197 return false;
199 if (maybe_gt (copy->size, limit))
200 copy->size = limit;
201 return true;
204 /* Clear any bytes written by STMT from the bitmap LIVE_BYTES. The base
205 address written by STMT must match the one found in REF, which must
206 have its base address previously initialized.
208 This routine must be conservative. If we don't know the offset or
209 actual size written, assume nothing was written. */
211 static void
212 clear_bytes_written_by (sbitmap live_bytes, gimple *stmt, ao_ref *ref)
214 ao_ref write;
215 if (!initialize_ao_ref_for_dse (stmt, &write))
216 return;
218 /* Verify we have the same base memory address, the write
219 has a known size and overlaps with REF. */
220 HOST_WIDE_INT start, size;
221 if (valid_ao_ref_for_dse (&write)
222 && operand_equal_p (write.base, ref->base, OEP_ADDRESS_OF)
223 && known_eq (write.size, write.max_size)
224 && normalize_ref (&write, ref)
225 && (write.offset - ref->offset).is_constant (&start)
226 && write.size.is_constant (&size))
227 bitmap_clear_range (live_bytes, start / BITS_PER_UNIT,
228 size / BITS_PER_UNIT);
231 /* REF is a memory write. Extract relevant information from it and
232 initialize the LIVE_BYTES bitmap. If successful, return TRUE.
233 Otherwise return FALSE. */
235 static bool
236 setup_live_bytes_from_ref (ao_ref *ref, sbitmap live_bytes)
238 HOST_WIDE_INT const_size;
239 if (valid_ao_ref_for_dse (ref)
240 && ref->size.is_constant (&const_size)
241 && (const_size / BITS_PER_UNIT
242 <= PARAM_VALUE (PARAM_DSE_MAX_OBJECT_SIZE)))
244 bitmap_clear (live_bytes);
245 bitmap_set_range (live_bytes, 0, const_size / BITS_PER_UNIT);
246 return true;
248 return false;
251 /* Compute the number of elements that we can trim from the head and
252 tail of ORIG resulting in a bitmap that is a superset of LIVE.
254 Store the number of elements trimmed from the head and tail in
255 TRIM_HEAD and TRIM_TAIL.
257 STMT is the statement being trimmed and is used for debugging dump
258 output only. */
260 static void
261 compute_trims (ao_ref *ref, sbitmap live, int *trim_head, int *trim_tail,
262 gimple *stmt)
264 /* We use sbitmaps biased such that ref->offset is bit zero and the bitmap
265 extends through ref->size. So we know that in the original bitmap
266 bits 0..ref->size were true. We don't actually need the bitmap, just
267 the REF to compute the trims. */
269 /* Now identify how much, if any of the tail we can chop off. */
270 HOST_WIDE_INT const_size;
271 int last_live = bitmap_last_set_bit (live);
272 if (ref->size.is_constant (&const_size))
274 int last_orig = (const_size / BITS_PER_UNIT) - 1;
275 /* We can leave inconvenient amounts on the tail as
276 residual handling in mem* and str* functions is usually
277 reasonably efficient. */
278 *trim_tail = last_orig - last_live;
280 /* But don't trim away out of bounds accesses, as this defeats
281 proper warnings.
283 We could have a type with no TYPE_SIZE_UNIT or we could have a VLA
284 where TYPE_SIZE_UNIT is not a constant. */
285 if (*trim_tail
286 && TYPE_SIZE_UNIT (TREE_TYPE (ref->base))
287 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref->base))) == INTEGER_CST
288 && compare_tree_int (TYPE_SIZE_UNIT (TREE_TYPE (ref->base)),
289 last_orig) <= 0)
290 *trim_tail = 0;
292 else
293 *trim_tail = 0;
295 /* Identify how much, if any of the head we can chop off. */
296 int first_orig = 0;
297 int first_live = bitmap_first_set_bit (live);
298 *trim_head = first_live - first_orig;
300 /* If more than a word remains, then make sure to keep the
301 starting point at least word aligned. */
302 if (last_live - first_live > UNITS_PER_WORD)
303 *trim_head &= ~(UNITS_PER_WORD - 1);
305 if ((*trim_head || *trim_tail)
306 && dump_file && (dump_flags & TDF_DETAILS))
308 fprintf (dump_file, " Trimming statement (head = %d, tail = %d): ",
309 *trim_head, *trim_tail);
310 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
311 fprintf (dump_file, "\n");
315 /* STMT initializes an object from COMPLEX_CST where one or more of the
316 bytes written may be dead stores. REF is a representation of the
317 memory written. LIVE is the bitmap of stores that are actually live.
319 Attempt to rewrite STMT so that only the real or imaginary part of
320 the object is actually stored. */
322 static void
323 maybe_trim_complex_store (ao_ref *ref, sbitmap live, gimple *stmt)
325 int trim_head, trim_tail;
326 compute_trims (ref, live, &trim_head, &trim_tail, stmt);
328 /* The amount of data trimmed from the head or tail must be at
329 least half the size of the object to ensure we're trimming
330 the entire real or imaginary half. By writing things this
331 way we avoid more O(n) bitmap operations. */
332 if (known_ge (trim_tail * 2 * BITS_PER_UNIT, ref->size))
334 /* TREE_REALPART is live */
335 tree x = TREE_REALPART (gimple_assign_rhs1 (stmt));
336 tree y = gimple_assign_lhs (stmt);
337 y = build1 (REALPART_EXPR, TREE_TYPE (x), y);
338 gimple_assign_set_lhs (stmt, y);
339 gimple_assign_set_rhs1 (stmt, x);
341 else if (known_ge (trim_head * 2 * BITS_PER_UNIT, ref->size))
343 /* TREE_IMAGPART is live */
344 tree x = TREE_IMAGPART (gimple_assign_rhs1 (stmt));
345 tree y = gimple_assign_lhs (stmt);
346 y = build1 (IMAGPART_EXPR, TREE_TYPE (x), y);
347 gimple_assign_set_lhs (stmt, y);
348 gimple_assign_set_rhs1 (stmt, x);
351 /* Other cases indicate parts of both the real and imag subobjects
352 are live. We do not try to optimize those cases. */
355 /* STMT initializes an object using a CONSTRUCTOR where one or more of the
356 bytes written are dead stores. ORIG is the bitmap of bytes stored by
357 STMT. LIVE is the bitmap of stores that are actually live.
359 Attempt to rewrite STMT so that only the real or imaginary part of
360 the object is actually stored.
362 The most common case for getting here is a CONSTRUCTOR with no elements
363 being used to zero initialize an object. We do not try to handle other
364 cases as those would force us to fully cover the object with the
365 CONSTRUCTOR node except for the components that are dead. */
367 static void
368 maybe_trim_constructor_store (ao_ref *ref, sbitmap live, gimple *stmt)
370 tree ctor = gimple_assign_rhs1 (stmt);
372 /* This is the only case we currently handle. It actually seems to
373 catch most cases of actual interest. */
374 gcc_assert (CONSTRUCTOR_NELTS (ctor) == 0);
376 int head_trim = 0;
377 int tail_trim = 0;
378 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
380 /* Now we want to replace the constructor initializer
381 with memset (object + head_trim, 0, size - head_trim - tail_trim). */
382 if (head_trim || tail_trim)
384 /* We want &lhs for the MEM_REF expression. */
385 tree lhs_addr = build_fold_addr_expr (gimple_assign_lhs (stmt));
387 if (! is_gimple_min_invariant (lhs_addr))
388 return;
390 /* The number of bytes for the new constructor. */
391 poly_int64 ref_bytes = exact_div (ref->size, BITS_PER_UNIT);
392 poly_int64 count = ref_bytes - head_trim - tail_trim;
394 /* And the new type for the CONSTRUCTOR. Essentially it's just
395 a char array large enough to cover the non-trimmed parts of
396 the original CONSTRUCTOR. Note we want explicit bounds here
397 so that we know how many bytes to clear when expanding the
398 CONSTRUCTOR. */
399 tree type = build_array_type_nelts (char_type_node, count);
401 /* Build a suitable alias type rather than using alias set zero
402 to avoid pessimizing. */
403 tree alias_type = reference_alias_ptr_type (gimple_assign_lhs (stmt));
405 /* Build a MEM_REF representing the whole accessed area, starting
406 at the first byte not trimmed. */
407 tree exp = fold_build2 (MEM_REF, type, lhs_addr,
408 build_int_cst (alias_type, head_trim));
410 /* Now update STMT with a new RHS and LHS. */
411 gimple_assign_set_lhs (stmt, exp);
412 gimple_assign_set_rhs1 (stmt, build_constructor (type, NULL));
416 /* STMT is a memcpy, memmove or memset. Decrement the number of bytes
417 copied/set by DECREMENT. */
418 static void
419 decrement_count (gimple *stmt, int decrement)
421 tree *countp = gimple_call_arg_ptr (stmt, 2);
422 gcc_assert (TREE_CODE (*countp) == INTEGER_CST);
423 *countp = wide_int_to_tree (TREE_TYPE (*countp), (TREE_INT_CST_LOW (*countp)
424 - decrement));
428 static void
429 increment_start_addr (gimple *stmt, tree *where, int increment)
431 if (TREE_CODE (*where) == SSA_NAME)
433 tree tem = make_ssa_name (TREE_TYPE (*where));
434 gassign *newop
435 = gimple_build_assign (tem, POINTER_PLUS_EXPR, *where,
436 build_int_cst (sizetype, increment));
437 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
438 gsi_insert_before (&gsi, newop, GSI_SAME_STMT);
439 *where = tem;
440 update_stmt (gsi_stmt (gsi));
441 return;
444 *where = build_fold_addr_expr (fold_build2 (MEM_REF, char_type_node,
445 *where,
446 build_int_cst (ptr_type_node,
447 increment)));
450 /* STMT is builtin call that writes bytes in bitmap ORIG, some bytes are dead
451 (ORIG & ~NEW) and need not be stored. Try to rewrite STMT to reduce
452 the amount of data it actually writes.
454 Right now we only support trimming from the head or the tail of the
455 memory region. In theory we could split the mem* call, but it's
456 likely of marginal value. */
458 static void
459 maybe_trim_memstar_call (ao_ref *ref, sbitmap live, gimple *stmt)
461 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
463 case BUILT_IN_MEMCPY:
464 case BUILT_IN_MEMMOVE:
465 case BUILT_IN_STRNCPY:
466 case BUILT_IN_MEMCPY_CHK:
467 case BUILT_IN_MEMMOVE_CHK:
468 case BUILT_IN_STRNCPY_CHK:
470 int head_trim, tail_trim;
471 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
473 /* Tail trimming is easy, we can just reduce the count. */
474 if (tail_trim)
475 decrement_count (stmt, tail_trim);
477 /* Head trimming requires adjusting all the arguments. */
478 if (head_trim)
480 tree *dst = gimple_call_arg_ptr (stmt, 0);
481 increment_start_addr (stmt, dst, head_trim);
482 tree *src = gimple_call_arg_ptr (stmt, 1);
483 increment_start_addr (stmt, src, head_trim);
484 decrement_count (stmt, head_trim);
486 break;
489 case BUILT_IN_MEMSET:
490 case BUILT_IN_MEMSET_CHK:
492 int head_trim, tail_trim;
493 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
495 /* Tail trimming is easy, we can just reduce the count. */
496 if (tail_trim)
497 decrement_count (stmt, tail_trim);
499 /* Head trimming requires adjusting all the arguments. */
500 if (head_trim)
502 tree *dst = gimple_call_arg_ptr (stmt, 0);
503 increment_start_addr (stmt, dst, head_trim);
504 decrement_count (stmt, head_trim);
506 break;
509 default:
510 break;
514 /* STMT is a memory write where one or more bytes written are dead
515 stores. ORIG is the bitmap of bytes stored by STMT. LIVE is the
516 bitmap of stores that are actually live.
518 Attempt to rewrite STMT so that it writes fewer memory locations. Right
519 now we only support trimming at the start or end of the memory region.
520 It's not clear how much there is to be gained by trimming from the middle
521 of the region. */
523 static void
524 maybe_trim_partially_dead_store (ao_ref *ref, sbitmap live, gimple *stmt)
526 if (is_gimple_assign (stmt)
527 && TREE_CODE (gimple_assign_lhs (stmt)) != TARGET_MEM_REF)
529 switch (gimple_assign_rhs_code (stmt))
531 case CONSTRUCTOR:
532 maybe_trim_constructor_store (ref, live, stmt);
533 break;
534 case COMPLEX_CST:
535 maybe_trim_complex_store (ref, live, stmt);
536 break;
537 default:
538 break;
543 /* Return TRUE if USE_REF reads bytes from LIVE where live is
544 derived from REF, a write reference.
546 While this routine may modify USE_REF, it's passed by value, not
547 location. So callers do not see those modifications. */
549 static bool
550 live_bytes_read (ao_ref use_ref, ao_ref *ref, sbitmap live)
552 /* We have already verified that USE_REF and REF hit the same object.
553 Now verify that there's actually an overlap between USE_REF and REF. */
554 HOST_WIDE_INT start, size;
555 if (normalize_ref (&use_ref, ref)
556 && (use_ref.offset - ref->offset).is_constant (&start)
557 && use_ref.size.is_constant (&size))
559 /* If USE_REF covers all of REF, then it will hit one or more
560 live bytes. This avoids useless iteration over the bitmap
561 below. */
562 if (start == 0 && known_eq (size, ref->size))
563 return true;
565 /* Now check if any of the remaining bits in use_ref are set in LIVE. */
566 return bitmap_bit_in_range_p (live, start / BITS_PER_UNIT,
567 (start + size - 1) / BITS_PER_UNIT);
569 return true;
572 /* Callback for dse_classify_store calling for_each_index. Verify that
573 indices are invariant in the loop with backedge PHI in basic-block DATA. */
575 static bool
576 check_name (tree, tree *idx, void *data)
578 basic_block phi_bb = (basic_block) data;
579 if (TREE_CODE (*idx) == SSA_NAME
580 && !SSA_NAME_IS_DEFAULT_DEF (*idx)
581 && dominated_by_p (CDI_DOMINATORS, gimple_bb (SSA_NAME_DEF_STMT (*idx)),
582 phi_bb))
583 return false;
584 return true;
587 /* STMT stores the value 0 into one or more memory locations
588 (via memset, empty constructor, calloc call, etc).
590 See if there is a subsequent store of the value 0 to one
591 or more of the same memory location(s). If so, the subsequent
592 store is redundant and can be removed.
594 The subsequent stores could be via memset, empty constructors,
595 simple MEM stores, etc. */
597 static void
598 dse_optimize_redundant_stores (gimple *stmt)
600 int cnt = 0;
602 /* We could do something fairly complex and look through PHIs
603 like DSE_CLASSIFY_STORE, but it doesn't seem to be worth
604 the effort.
606 Look at all the immediate uses of the VDEF (which are obviously
607 dominated by STMT). See if one or more stores 0 into the same
608 memory locations a STMT, if so remove the immediate use statements. */
609 tree defvar = gimple_vdef (stmt);
610 imm_use_iterator ui;
611 gimple *use_stmt;
612 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
614 /* Limit stmt walking. */
615 if (++cnt > PARAM_VALUE (PARAM_DSE_MAX_ALIAS_QUERIES_PER_STORE))
616 BREAK_FROM_IMM_USE_STMT (ui);
618 /* If USE_STMT stores 0 into one or more of the same locations
619 as STMT and STMT would kill USE_STMT, then we can just remove
620 USE_STMT. */
621 tree fndecl;
622 if ((is_gimple_assign (use_stmt)
623 && gimple_vdef (use_stmt)
624 && (gimple_assign_single_p (use_stmt)
625 && initializer_zerop (gimple_assign_rhs1 (use_stmt))))
626 || (gimple_call_builtin_p (use_stmt, BUILT_IN_NORMAL)
627 && (fndecl = gimple_call_fndecl (use_stmt)) != NULL
628 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
629 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET_CHK)
630 && integer_zerop (gimple_call_arg (use_stmt, 1))))
632 ao_ref write;
634 if (!initialize_ao_ref_for_dse (use_stmt, &write))
635 BREAK_FROM_IMM_USE_STMT (ui)
637 if (valid_ao_ref_for_dse (&write)
638 && stmt_kills_ref_p (stmt, &write))
640 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
641 if (is_gimple_assign (use_stmt))
642 delete_dead_or_redundant_assignment (&gsi, "redundant");
643 else if (is_gimple_call (use_stmt))
644 delete_dead_or_redundant_call (&gsi, "redundant");
645 else
646 gcc_unreachable ();
652 /* A helper of dse_optimize_stmt.
653 Given a GIMPLE_ASSIGN in STMT that writes to REF, classify it
654 according to downstream uses and defs. Sets *BY_CLOBBER_P to true
655 if only clobber statements influenced the classification result.
656 Returns the classification. */
658 dse_store_status
659 dse_classify_store (ao_ref *ref, gimple *stmt,
660 bool byte_tracking_enabled, sbitmap live_bytes,
661 bool *by_clobber_p, tree stop_at_vuse)
663 gimple *temp;
664 int cnt = 0;
665 auto_bitmap visited;
667 if (by_clobber_p)
668 *by_clobber_p = true;
670 /* Find the first dominated statement that clobbers (part of) the
671 memory stmt stores to with no intermediate statement that may use
672 part of the memory stmt stores. That is, find a store that may
673 prove stmt to be a dead store. */
674 temp = stmt;
677 gimple *use_stmt;
678 imm_use_iterator ui;
679 bool fail = false;
680 tree defvar;
682 if (gimple_code (temp) == GIMPLE_PHI)
684 /* If we visit this PHI by following a backedge then we have to
685 make sure ref->ref only refers to SSA names that are invariant
686 with respect to the loop represented by this PHI node. */
687 if (dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt),
688 gimple_bb (temp))
689 && !for_each_index (ref->ref ? &ref->ref : &ref->base,
690 check_name, gimple_bb (temp)))
691 return DSE_STORE_LIVE;
692 defvar = PHI_RESULT (temp);
693 bitmap_set_bit (visited, SSA_NAME_VERSION (defvar));
695 else
696 defvar = gimple_vdef (temp);
698 /* If we're instructed to stop walking at region boundary, do so. */
699 if (defvar == stop_at_vuse)
700 return DSE_STORE_LIVE;
702 auto_vec<gimple *, 10> defs;
703 gimple *phi_def = NULL;
704 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
706 /* Limit stmt walking. */
707 if (++cnt > PARAM_VALUE (PARAM_DSE_MAX_ALIAS_QUERIES_PER_STORE))
709 fail = true;
710 BREAK_FROM_IMM_USE_STMT (ui);
713 /* We have visited ourselves already so ignore STMT for the
714 purpose of chaining. */
715 if (use_stmt == stmt)
717 /* In simple cases we can look through PHI nodes, but we
718 have to be careful with loops and with memory references
719 containing operands that are also operands of PHI nodes.
720 See gcc.c-torture/execute/20051110-*.c. */
721 else if (gimple_code (use_stmt) == GIMPLE_PHI)
723 /* If we already visited this PHI ignore it for further
724 processing. */
725 if (!bitmap_bit_p (visited,
726 SSA_NAME_VERSION (PHI_RESULT (use_stmt))))
728 defs.safe_push (use_stmt);
729 phi_def = use_stmt;
732 /* If the statement is a use the store is not dead. */
733 else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
735 /* Handle common cases where we can easily build an ao_ref
736 structure for USE_STMT and in doing so we find that the
737 references hit non-live bytes and thus can be ignored. */
738 if (byte_tracking_enabled
739 && is_gimple_assign (use_stmt))
741 ao_ref use_ref;
742 ao_ref_init (&use_ref, gimple_assign_rhs1 (use_stmt));
743 if (valid_ao_ref_for_dse (&use_ref)
744 && use_ref.base == ref->base
745 && known_eq (use_ref.size, use_ref.max_size)
746 && !live_bytes_read (use_ref, ref, live_bytes))
748 /* If this is a store, remember it as we possibly
749 need to walk the defs uses. */
750 if (gimple_vdef (use_stmt))
751 defs.safe_push (use_stmt);
752 continue;
756 fail = true;
757 BREAK_FROM_IMM_USE_STMT (ui);
759 /* If this is a store, remember it as we possibly need to walk the
760 defs uses. */
761 else if (gimple_vdef (use_stmt))
762 defs.safe_push (use_stmt);
765 if (fail)
767 /* STMT might be partially dead and we may be able to reduce
768 how many memory locations it stores into. */
769 if (byte_tracking_enabled && !gimple_clobber_p (stmt))
770 return DSE_STORE_MAYBE_PARTIAL_DEAD;
771 return DSE_STORE_LIVE;
774 /* If we didn't find any definition this means the store is dead
775 if it isn't a store to global reachable memory. In this case
776 just pretend the stmt makes itself dead. Otherwise fail. */
777 if (defs.is_empty ())
779 if (ref_may_alias_global_p (ref))
780 return DSE_STORE_LIVE;
782 if (by_clobber_p)
783 *by_clobber_p = false;
784 return DSE_STORE_DEAD;
787 /* Process defs and remove those we need not process further. */
788 for (unsigned i = 0; i < defs.length ();)
790 gimple *def = defs[i];
791 gimple *use_stmt;
792 use_operand_p use_p;
793 /* If the path to check starts with a kill we do not need to
794 process it further.
795 ??? With byte tracking we need only kill the bytes currently
796 live. */
797 if (stmt_kills_ref_p (def, ref))
799 if (by_clobber_p && !gimple_clobber_p (def))
800 *by_clobber_p = false;
801 defs.unordered_remove (i);
803 /* In addition to kills we can remove defs whose only use
804 is another def in defs. That can only ever be PHIs of which
805 we track a single for simplicity reasons (we fail for multiple
806 PHIs anyways). We can also ignore defs that feed only into
807 already visited PHIs. */
808 else if (gimple_code (def) != GIMPLE_PHI
809 && single_imm_use (gimple_vdef (def), &use_p, &use_stmt)
810 && (use_stmt == phi_def
811 || (gimple_code (use_stmt) == GIMPLE_PHI
812 && bitmap_bit_p (visited,
813 SSA_NAME_VERSION
814 (PHI_RESULT (use_stmt))))))
815 defs.unordered_remove (i);
816 else
817 ++i;
820 /* If all defs kill the ref we are done. */
821 if (defs.is_empty ())
822 return DSE_STORE_DEAD;
823 /* If more than one def survives fail. */
824 if (defs.length () > 1)
826 /* STMT might be partially dead and we may be able to reduce
827 how many memory locations it stores into. */
828 if (byte_tracking_enabled && !gimple_clobber_p (stmt))
829 return DSE_STORE_MAYBE_PARTIAL_DEAD;
830 return DSE_STORE_LIVE;
832 temp = defs[0];
834 /* Track partial kills. */
835 if (byte_tracking_enabled)
837 clear_bytes_written_by (live_bytes, temp, ref);
838 if (bitmap_empty_p (live_bytes))
840 if (by_clobber_p && !gimple_clobber_p (temp))
841 *by_clobber_p = false;
842 return DSE_STORE_DEAD;
846 /* Continue walking until there are no more live bytes. */
847 while (1);
851 class dse_dom_walker : public dom_walker
853 public:
854 dse_dom_walker (cdi_direction direction)
855 : dom_walker (direction),
856 m_live_bytes (PARAM_VALUE (PARAM_DSE_MAX_OBJECT_SIZE)),
857 m_byte_tracking_enabled (false) {}
859 virtual edge before_dom_children (basic_block);
861 private:
862 auto_sbitmap m_live_bytes;
863 bool m_byte_tracking_enabled;
864 void dse_optimize_stmt (gimple_stmt_iterator *);
867 /* Delete a dead call at GSI, which is mem* call of some kind. */
868 static void
869 delete_dead_or_redundant_call (gimple_stmt_iterator *gsi, const char *type)
871 gimple *stmt = gsi_stmt (*gsi);
872 if (dump_file && (dump_flags & TDF_DETAILS))
874 fprintf (dump_file, " Deleted %s call: ", type);
875 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
876 fprintf (dump_file, "\n");
879 tree lhs = gimple_call_lhs (stmt);
880 if (lhs)
882 tree ptr = gimple_call_arg (stmt, 0);
883 gimple *new_stmt = gimple_build_assign (lhs, ptr);
884 unlink_stmt_vdef (stmt);
885 if (gsi_replace (gsi, new_stmt, true))
886 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
888 else
890 /* Then we need to fix the operand of the consuming stmt. */
891 unlink_stmt_vdef (stmt);
893 /* Remove the dead store. */
894 if (gsi_remove (gsi, true))
895 bitmap_set_bit (need_eh_cleanup, gimple_bb (stmt)->index);
896 release_defs (stmt);
900 /* Delete a dead store at GSI, which is a gimple assignment. */
902 void
903 delete_dead_or_redundant_assignment (gimple_stmt_iterator *gsi, const char *type)
905 gimple *stmt = gsi_stmt (*gsi);
906 if (dump_file && (dump_flags & TDF_DETAILS))
908 fprintf (dump_file, " Deleted %s store: ", type);
909 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
910 fprintf (dump_file, "\n");
913 /* Then we need to fix the operand of the consuming stmt. */
914 unlink_stmt_vdef (stmt);
916 /* Remove the dead store. */
917 basic_block bb = gimple_bb (stmt);
918 if (gsi_remove (gsi, true))
919 bitmap_set_bit (need_eh_cleanup, bb->index);
921 /* And release any SSA_NAMEs set in this statement back to the
922 SSA_NAME manager. */
923 release_defs (stmt);
926 /* Attempt to eliminate dead stores in the statement referenced by BSI.
928 A dead store is a store into a memory location which will later be
929 overwritten by another store without any intervening loads. In this
930 case the earlier store can be deleted.
932 In our SSA + virtual operand world we use immediate uses of virtual
933 operands to detect dead stores. If a store's virtual definition
934 is used precisely once by a later store to the same location which
935 post dominates the first store, then the first store is dead. */
937 void
938 dse_dom_walker::dse_optimize_stmt (gimple_stmt_iterator *gsi)
940 gimple *stmt = gsi_stmt (*gsi);
942 /* If this statement has no virtual defs, then there is nothing
943 to do. */
944 if (!gimple_vdef (stmt))
945 return;
947 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
948 if (gimple_has_volatile_ops (stmt)
949 && (!gimple_clobber_p (stmt)
950 || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
951 return;
953 ao_ref ref;
954 if (!initialize_ao_ref_for_dse (stmt, &ref))
955 return;
957 /* We know we have virtual definitions. We can handle assignments and
958 some builtin calls. */
959 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
961 tree fndecl = gimple_call_fndecl (stmt);
962 switch (DECL_FUNCTION_CODE (fndecl))
964 case BUILT_IN_MEMCPY:
965 case BUILT_IN_MEMMOVE:
966 case BUILT_IN_STRNCPY:
967 case BUILT_IN_MEMSET:
968 case BUILT_IN_MEMCPY_CHK:
969 case BUILT_IN_MEMMOVE_CHK:
970 case BUILT_IN_STRNCPY_CHK:
971 case BUILT_IN_MEMSET_CHK:
973 /* Occasionally calls with an explicit length of zero
974 show up in the IL. It's pointless to do analysis
975 on them, they're trivially dead. */
976 tree size = gimple_call_arg (stmt, 2);
977 if (integer_zerop (size))
979 delete_dead_or_redundant_call (gsi, "dead");
980 return;
983 /* If this is a memset call that initializes an object
984 to zero, it may be redundant with an earlier memset
985 or empty CONSTRUCTOR of a larger object. */
986 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
987 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET_CHK)
988 && integer_zerop (gimple_call_arg (stmt, 1)))
989 dse_optimize_redundant_stores (stmt);
991 enum dse_store_status store_status;
992 m_byte_tracking_enabled
993 = setup_live_bytes_from_ref (&ref, m_live_bytes);
994 store_status = dse_classify_store (&ref, stmt,
995 m_byte_tracking_enabled,
996 m_live_bytes);
997 if (store_status == DSE_STORE_LIVE)
998 return;
1000 if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
1002 maybe_trim_memstar_call (&ref, m_live_bytes, stmt);
1003 return;
1006 if (store_status == DSE_STORE_DEAD)
1007 delete_dead_or_redundant_call (gsi, "dead");
1008 return;
1011 case BUILT_IN_CALLOC:
1012 /* We already know the arguments are integer constants. */
1013 dse_optimize_redundant_stores (stmt);
1014 return;
1016 default:
1017 return;
1021 if (is_gimple_assign (stmt))
1023 bool by_clobber_p = false;
1025 /* Check if this statement stores zero to a memory location,
1026 and if there is a subsequent store of zero to the same
1027 memory location. If so, remove the subsequent store. */
1028 if (gimple_assign_single_p (stmt)
1029 && initializer_zerop (gimple_assign_rhs1 (stmt)))
1030 dse_optimize_redundant_stores (stmt);
1032 /* Self-assignments are zombies. */
1033 if (operand_equal_p (gimple_assign_rhs1 (stmt),
1034 gimple_assign_lhs (stmt), 0))
1036 else
1038 m_byte_tracking_enabled
1039 = setup_live_bytes_from_ref (&ref, m_live_bytes);
1040 enum dse_store_status store_status;
1041 store_status = dse_classify_store (&ref, stmt,
1042 m_byte_tracking_enabled,
1043 m_live_bytes, &by_clobber_p);
1044 if (store_status == DSE_STORE_LIVE)
1045 return;
1047 if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
1049 maybe_trim_partially_dead_store (&ref, m_live_bytes, stmt);
1050 return;
1054 /* Now we know that use_stmt kills the LHS of stmt. */
1056 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
1057 another clobber stmt. */
1058 if (gimple_clobber_p (stmt)
1059 && !by_clobber_p)
1060 return;
1062 delete_dead_or_redundant_assignment (gsi, "dead");
1066 edge
1067 dse_dom_walker::before_dom_children (basic_block bb)
1069 gimple_stmt_iterator gsi;
1071 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
1073 dse_optimize_stmt (&gsi);
1074 if (gsi_end_p (gsi))
1075 gsi = gsi_last_bb (bb);
1076 else
1077 gsi_prev (&gsi);
1079 return NULL;
1082 namespace {
1084 const pass_data pass_data_dse =
1086 GIMPLE_PASS, /* type */
1087 "dse", /* name */
1088 OPTGROUP_NONE, /* optinfo_flags */
1089 TV_TREE_DSE, /* tv_id */
1090 ( PROP_cfg | PROP_ssa ), /* properties_required */
1091 0, /* properties_provided */
1092 0, /* properties_destroyed */
1093 0, /* todo_flags_start */
1094 0, /* todo_flags_finish */
1097 class pass_dse : public gimple_opt_pass
1099 public:
1100 pass_dse (gcc::context *ctxt)
1101 : gimple_opt_pass (pass_data_dse, ctxt)
1104 /* opt_pass methods: */
1105 opt_pass * clone () { return new pass_dse (m_ctxt); }
1106 virtual bool gate (function *) { return flag_tree_dse != 0; }
1107 virtual unsigned int execute (function *);
1109 }; // class pass_dse
1111 unsigned int
1112 pass_dse::execute (function *fun)
1114 need_eh_cleanup = BITMAP_ALLOC (NULL);
1116 renumber_gimple_stmt_uids (cfun);
1118 /* We might consider making this a property of each pass so that it
1119 can be [re]computed on an as-needed basis. Particularly since
1120 this pass could be seen as an extension of DCE which needs post
1121 dominators. */
1122 calculate_dominance_info (CDI_POST_DOMINATORS);
1123 calculate_dominance_info (CDI_DOMINATORS);
1125 /* Dead store elimination is fundamentally a walk of the post-dominator
1126 tree and a backwards walk of statements within each block. */
1127 dse_dom_walker (CDI_POST_DOMINATORS).walk (fun->cfg->x_exit_block_ptr);
1129 /* Removal of stores may make some EH edges dead. Purge such edges from
1130 the CFG as needed. */
1131 if (!bitmap_empty_p (need_eh_cleanup))
1133 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
1134 cleanup_tree_cfg ();
1137 BITMAP_FREE (need_eh_cleanup);
1139 /* For now, just wipe the post-dominator information. */
1140 free_dominance_info (CDI_POST_DOMINATORS);
1141 return 0;
1144 } // anon namespace
1146 gimple_opt_pass *
1147 make_pass_dse (gcc::context *ctxt)
1149 return new pass_dse (ctxt);