]> gcc.gnu.org Git - gcc.git/blob - gcc/tree-ssa-dse.cc
tree-optimization/99407 - DSE with data-ref analysis
[gcc.git] / gcc / tree-ssa-dse.cc
1 /* Dead and redundant store elimination
2 Copyright (C) 2004-2022 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
20 #include "config.h"
21 #define INCLUDE_MEMORY
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "gimple.h"
28 #include "tree-pass.h"
29 #include "ssa.h"
30 #include "gimple-pretty-print.h"
31 #include "fold-const.h"
32 #include "gimple-iterator.h"
33 #include "tree-cfg.h"
34 #include "tree-dfa.h"
35 #include "tree-cfgcleanup.h"
36 #include "alias.h"
37 #include "tree-ssa-loop.h"
38 #include "tree-ssa-dse.h"
39 #include "builtins.h"
40 #include "gimple-fold.h"
41 #include "gimplify.h"
42 #include "tree-eh.h"
43 #include "cfganal.h"
44 #include "cgraph.h"
45 #include "ipa-modref-tree.h"
46 #include "ipa-modref.h"
47 #include "target.h"
48 #include "tree-ssa-loop-niter.h"
49 #include "cfgloop.h"
50 #include "tree-data-ref.h"
51
52 /* This file implements dead store elimination.
53
54 A dead store is a store into a memory location which will later be
55 overwritten by another store without any intervening loads. In this
56 case the earlier store can be deleted or trimmed if the store
57 was partially dead.
58
59 A redundant store is a store into a memory location which stores
60 the exact same value as a prior store to the same memory location.
61 While this can often be handled by dead store elimination, removing
62 the redundant store is often better than removing or trimming the
63 dead store.
64
65 In our SSA + virtual operand world we use immediate uses of virtual
66 operands to detect these cases. If a store's virtual definition
67 is used precisely once by a later store to the same location which
68 post dominates the first store, then the first store is dead. If
69 the data stored is the same, then the second store is redundant.
70
71 The single use of the store's virtual definition ensures that
72 there are no intervening aliased loads and the requirement that
73 the second load post dominate the first ensures that if the earlier
74 store executes, then the later stores will execute before the function
75 exits.
76
77 It may help to think of this as first moving the earlier store to
78 the point immediately before the later store. Again, the single
79 use of the virtual definition and the post-dominance relationship
80 ensure that such movement would be safe. Clearly if there are
81 back to back stores, then the second is makes the first dead. If
82 the second store stores the same value, then the second store is
83 redundant.
84
85 Reviewing section 10.7.2 in Morgan's "Building an Optimizing Compiler"
86 may also help in understanding this code since it discusses the
87 relationship between dead store and redundant load elimination. In
88 fact, they are the same transformation applied to different views of
89 the CFG. */
90
91 static void delete_dead_or_redundant_call (gimple_stmt_iterator *, const char *);
92
93 /* Bitmap of blocks that have had EH statements cleaned. We should
94 remove their dead edges eventually. */
95 static bitmap need_eh_cleanup;
96 static bitmap need_ab_cleanup;
97
98 /* STMT is a statement that may write into memory. Analyze it and
99 initialize WRITE to describe how STMT affects memory. When
100 MAY_DEF_OK is true then the function initializes WRITE to what
101 the stmt may define.
102
103 Return TRUE if the statement was analyzed, FALSE otherwise.
104
105 It is always safe to return FALSE. But typically better optimziation
106 can be achieved by analyzing more statements. */
107
108 static bool
109 initialize_ao_ref_for_dse (gimple *stmt, ao_ref *write, bool may_def_ok = false)
110 {
111 /* It's advantageous to handle certain mem* functions. */
112 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
113 {
114 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
115 {
116 case BUILT_IN_MEMCPY:
117 case BUILT_IN_MEMMOVE:
118 case BUILT_IN_MEMSET:
119 case BUILT_IN_MEMCPY_CHK:
120 case BUILT_IN_MEMMOVE_CHK:
121 case BUILT_IN_MEMSET_CHK:
122 case BUILT_IN_STRNCPY:
123 case BUILT_IN_STRNCPY_CHK:
124 {
125 tree size = gimple_call_arg (stmt, 2);
126 tree ptr = gimple_call_arg (stmt, 0);
127 ao_ref_init_from_ptr_and_size (write, ptr, size);
128 return true;
129 }
130
131 /* A calloc call can never be dead, but it can make
132 subsequent stores redundant if they store 0 into
133 the same memory locations. */
134 case BUILT_IN_CALLOC:
135 {
136 tree nelem = gimple_call_arg (stmt, 0);
137 tree selem = gimple_call_arg (stmt, 1);
138 tree lhs;
139 if (TREE_CODE (nelem) == INTEGER_CST
140 && TREE_CODE (selem) == INTEGER_CST
141 && (lhs = gimple_call_lhs (stmt)) != NULL_TREE)
142 {
143 tree size = fold_build2 (MULT_EXPR, TREE_TYPE (nelem),
144 nelem, selem);
145 ao_ref_init_from_ptr_and_size (write, lhs, size);
146 return true;
147 }
148 }
149
150 default:
151 break;
152 }
153 }
154 else if (is_gimple_call (stmt)
155 && gimple_call_internal_p (stmt))
156 {
157 switch (gimple_call_internal_fn (stmt))
158 {
159 case IFN_LEN_STORE:
160 ao_ref_init_from_ptr_and_size
161 (write, gimple_call_arg (stmt, 0),
162 int_const_binop (MINUS_EXPR,
163 gimple_call_arg (stmt, 2),
164 gimple_call_arg (stmt, 4)));
165 return true;
166 case IFN_MASK_STORE:
167 /* We cannot initialize a must-def ao_ref (in all cases) but we
168 can provide a may-def variant. */
169 if (may_def_ok)
170 {
171 ao_ref_init_from_ptr_and_size
172 (write, gimple_call_arg (stmt, 0),
173 TYPE_SIZE_UNIT (TREE_TYPE (gimple_call_arg (stmt, 2))));
174 return true;
175 }
176 break;
177 default:;
178 }
179 }
180 else if (tree lhs = gimple_get_lhs (stmt))
181 {
182 if (TREE_CODE (lhs) != SSA_NAME)
183 {
184 ao_ref_init (write, lhs);
185 return true;
186 }
187 }
188 return false;
189 }
190
191 /* Given REF from the alias oracle, return TRUE if it is a valid
192 kill memory reference for dead store elimination, false otherwise.
193
194 In particular, the reference must have a known base, known maximum
195 size, start at a byte offset and have a size that is one or more
196 bytes. */
197
198 static bool
199 valid_ao_ref_kill_for_dse (ao_ref *ref)
200 {
201 return (ao_ref_base (ref)
202 && known_size_p (ref->max_size)
203 && maybe_ne (ref->size, 0)
204 && known_eq (ref->max_size, ref->size)
205 && known_ge (ref->offset, 0));
206 }
207
208 /* Given REF from the alias oracle, return TRUE if it is a valid
209 load or store memory reference for dead store elimination, false otherwise.
210
211 Unlike for valid_ao_ref_kill_for_dse we can accept writes where max_size
212 is not same as size since we can handle conservatively the larger range. */
213
214 static bool
215 valid_ao_ref_for_dse (ao_ref *ref)
216 {
217 return (ao_ref_base (ref)
218 && known_size_p (ref->max_size)
219 && known_ge (ref->offset, 0));
220 }
221
222 /* Initialize OFFSET and SIZE to a range known to contain REF
223 where the boundaries are divisible by BITS_PER_UNIT (bit still in bits).
224 Return false if this is impossible. */
225
226 static bool
227 get_byte_aligned_range_containing_ref (ao_ref *ref, poly_int64 *offset,
228 HOST_WIDE_INT *size)
229 {
230 if (!known_size_p (ref->max_size))
231 return false;
232 *offset = aligned_lower_bound (ref->offset, BITS_PER_UNIT);
233 poly_int64 end = aligned_upper_bound (ref->offset + ref->max_size,
234 BITS_PER_UNIT);
235 return (end - *offset).is_constant (size);
236 }
237
238 /* Initialize OFFSET and SIZE to a range known to be contained REF
239 where the boundaries are divisible by BITS_PER_UNIT (but still in bits).
240 Return false if this is impossible. */
241
242 static bool
243 get_byte_aligned_range_contained_in_ref (ao_ref *ref, poly_int64 *offset,
244 HOST_WIDE_INT *size)
245 {
246 if (!known_size_p (ref->size)
247 || !known_eq (ref->size, ref->max_size))
248 return false;
249 *offset = aligned_upper_bound (ref->offset, BITS_PER_UNIT);
250 poly_int64 end = aligned_lower_bound (ref->offset + ref->max_size,
251 BITS_PER_UNIT);
252 /* For bit accesses we can get -1 here, but also 0 sized kill is not
253 useful. */
254 if (!known_gt (end, *offset))
255 return false;
256 return (end - *offset).is_constant (size);
257 }
258
259 /* Compute byte range (returned iN REF_OFFSET and RET_SIZE) for access COPY
260 inside REF. If KILL is true, then COPY represent a kill and the byte range
261 needs to be fully contained in bit range given by COPY. If KILL is false
262 then the byte range returned must contain the range of COPY. */
263
264 static bool
265 get_byte_range (ao_ref *copy, ao_ref *ref, bool kill,
266 HOST_WIDE_INT *ret_offset, HOST_WIDE_INT *ret_size)
267 {
268 HOST_WIDE_INT copy_size, ref_size;
269 poly_int64 copy_offset, ref_offset;
270 HOST_WIDE_INT diff;
271
272 /* First translate from bits to bytes, rounding to bigger or smaller ranges
273 as needed. Kills needs to be always rounded to smaller ranges while
274 uses and stores to larger ranges. */
275 if (kill)
276 {
277 if (!get_byte_aligned_range_contained_in_ref (copy, &copy_offset,
278 &copy_size))
279 return false;
280 }
281 else
282 {
283 if (!get_byte_aligned_range_containing_ref (copy, &copy_offset,
284 &copy_size))
285 return false;
286 }
287
288 if (!get_byte_aligned_range_containing_ref (ref, &ref_offset, &ref_size)
289 || !ordered_p (copy_offset, ref_offset))
290 return false;
291
292 /* Switch sizes from bits to bytes so we do not need to care about
293 overflows. Offset calculation needs to stay in bits until we compute
294 the difference and can switch to HOST_WIDE_INT. */
295 copy_size /= BITS_PER_UNIT;
296 ref_size /= BITS_PER_UNIT;
297
298 /* If COPY starts before REF, then reset the beginning of
299 COPY to match REF and decrease the size of COPY by the
300 number of bytes removed from COPY. */
301 if (maybe_lt (copy_offset, ref_offset))
302 {
303 if (!(ref_offset - copy_offset).is_constant (&diff)
304 || copy_size < diff / BITS_PER_UNIT)
305 return false;
306 copy_size -= diff / BITS_PER_UNIT;
307 copy_offset = ref_offset;
308 }
309
310 if (!(copy_offset - ref_offset).is_constant (&diff)
311 || ref_size <= diff / BITS_PER_UNIT)
312 return false;
313
314 /* If COPY extends beyond REF, chop off its size appropriately. */
315 HOST_WIDE_INT limit = ref_size - diff / BITS_PER_UNIT;
316
317 if (copy_size > limit)
318 copy_size = limit;
319 *ret_size = copy_size;
320 if (!(copy_offset - ref_offset).is_constant (ret_offset))
321 return false;
322 *ret_offset /= BITS_PER_UNIT;
323 return true;
324 }
325
326 /* Update LIVE_BYTES tracking REF for write to WRITE:
327 Verify we have the same base memory address, the write
328 has a known size and overlaps with REF. */
329 static void
330 clear_live_bytes_for_ref (sbitmap live_bytes, ao_ref *ref, ao_ref *write)
331 {
332 HOST_WIDE_INT start, size;
333
334 if (valid_ao_ref_kill_for_dse (write)
335 && operand_equal_p (write->base, ref->base, OEP_ADDRESS_OF)
336 && get_byte_range (write, ref, true, &start, &size))
337 bitmap_clear_range (live_bytes, start, size);
338 }
339
340 /* Clear any bytes written by STMT from the bitmap LIVE_BYTES. The base
341 address written by STMT must match the one found in REF, which must
342 have its base address previously initialized.
343
344 This routine must be conservative. If we don't know the offset or
345 actual size written, assume nothing was written. */
346
347 static void
348 clear_bytes_written_by (sbitmap live_bytes, gimple *stmt, ao_ref *ref)
349 {
350 ao_ref write;
351
352 if (gcall *call = dyn_cast <gcall *> (stmt))
353 {
354 bool interposed;
355 modref_summary *summary = get_modref_function_summary (call, &interposed);
356
357 if (summary && !interposed)
358 for (auto kill : summary->kills)
359 if (kill.get_ao_ref (as_a <gcall *> (stmt), &write))
360 clear_live_bytes_for_ref (live_bytes, ref, &write);
361 }
362 if (!initialize_ao_ref_for_dse (stmt, &write))
363 return;
364
365 clear_live_bytes_for_ref (live_bytes, ref, &write);
366 }
367
368 /* REF is a memory write. Extract relevant information from it and
369 initialize the LIVE_BYTES bitmap. If successful, return TRUE.
370 Otherwise return FALSE. */
371
372 static bool
373 setup_live_bytes_from_ref (ao_ref *ref, sbitmap live_bytes)
374 {
375 HOST_WIDE_INT const_size;
376 if (valid_ao_ref_for_dse (ref)
377 && ((aligned_upper_bound (ref->offset + ref->max_size, BITS_PER_UNIT)
378 - aligned_lower_bound (ref->offset,
379 BITS_PER_UNIT)).is_constant (&const_size))
380 && (const_size / BITS_PER_UNIT <= param_dse_max_object_size)
381 && const_size > 1)
382 {
383 bitmap_clear (live_bytes);
384 bitmap_set_range (live_bytes, 0, const_size / BITS_PER_UNIT);
385 return true;
386 }
387 return false;
388 }
389
390 /* Compute the number of elements that we can trim from the head and
391 tail of ORIG resulting in a bitmap that is a superset of LIVE.
392
393 Store the number of elements trimmed from the head and tail in
394 TRIM_HEAD and TRIM_TAIL.
395
396 STMT is the statement being trimmed and is used for debugging dump
397 output only. */
398
399 static void
400 compute_trims (ao_ref *ref, sbitmap live, int *trim_head, int *trim_tail,
401 gimple *stmt)
402 {
403 /* We use sbitmaps biased such that ref->offset is bit zero and the bitmap
404 extends through ref->size. So we know that in the original bitmap
405 bits 0..ref->size were true. We don't actually need the bitmap, just
406 the REF to compute the trims. */
407
408 /* Now identify how much, if any of the tail we can chop off. */
409 HOST_WIDE_INT const_size;
410 int last_live = bitmap_last_set_bit (live);
411 if (ref->size.is_constant (&const_size))
412 {
413 int last_orig = (const_size / BITS_PER_UNIT) - 1;
414 /* We can leave inconvenient amounts on the tail as
415 residual handling in mem* and str* functions is usually
416 reasonably efficient. */
417 *trim_tail = last_orig - last_live;
418
419 /* But don't trim away out of bounds accesses, as this defeats
420 proper warnings.
421
422 We could have a type with no TYPE_SIZE_UNIT or we could have a VLA
423 where TYPE_SIZE_UNIT is not a constant. */
424 if (*trim_tail
425 && TYPE_SIZE_UNIT (TREE_TYPE (ref->base))
426 && TREE_CODE (TYPE_SIZE_UNIT (TREE_TYPE (ref->base))) == INTEGER_CST
427 && compare_tree_int (TYPE_SIZE_UNIT (TREE_TYPE (ref->base)),
428 last_orig) <= 0)
429 *trim_tail = 0;
430 }
431 else
432 *trim_tail = 0;
433
434 /* Identify how much, if any of the head we can chop off. */
435 int first_orig = 0;
436 int first_live = bitmap_first_set_bit (live);
437 *trim_head = first_live - first_orig;
438
439 /* If REF is aligned, try to maintain this alignment if it reduces
440 the number of (power-of-two sized aligned) writes to memory. */
441 unsigned int align_bits;
442 unsigned HOST_WIDE_INT bitpos;
443 if ((*trim_head || *trim_tail)
444 && last_live - first_live >= 2
445 && ao_ref_alignment (ref, &align_bits, &bitpos)
446 && align_bits >= 32
447 && bitpos == 0
448 && align_bits % BITS_PER_UNIT == 0)
449 {
450 unsigned int align_units = align_bits / BITS_PER_UNIT;
451 if (align_units > 16)
452 align_units = 16;
453 while ((first_live | (align_units - 1)) > (unsigned int)last_live)
454 align_units >>= 1;
455
456 if (*trim_head)
457 {
458 unsigned int pos = first_live & (align_units - 1);
459 for (unsigned int i = 1; i <= align_units; i <<= 1)
460 {
461 unsigned int mask = ~(i - 1);
462 unsigned int bytes = align_units - (pos & mask);
463 if (wi::popcount (bytes) <= 1)
464 {
465 *trim_head &= mask;
466 break;
467 }
468 }
469 }
470
471 if (*trim_tail)
472 {
473 unsigned int pos = last_live & (align_units - 1);
474 for (unsigned int i = 1; i <= align_units; i <<= 1)
475 {
476 int mask = i - 1;
477 unsigned int bytes = (pos | mask) + 1;
478 if ((last_live | mask) > (last_live + *trim_tail))
479 break;
480 if (wi::popcount (bytes) <= 1)
481 {
482 unsigned int extra = (last_live | mask) - last_live;
483 *trim_tail -= extra;
484 break;
485 }
486 }
487 }
488 }
489
490 if ((*trim_head || *trim_tail)
491 && dump_file && (dump_flags & TDF_DETAILS))
492 {
493 fprintf (dump_file, " Trimming statement (head = %d, tail = %d): ",
494 *trim_head, *trim_tail);
495 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
496 fprintf (dump_file, "\n");
497 }
498 }
499
500 /* STMT initializes an object from COMPLEX_CST where one or more of the
501 bytes written may be dead stores. REF is a representation of the
502 memory written. LIVE is the bitmap of stores that are actually live.
503
504 Attempt to rewrite STMT so that only the real or imaginary part of
505 the object is actually stored. */
506
507 static void
508 maybe_trim_complex_store (ao_ref *ref, sbitmap live, gimple *stmt)
509 {
510 int trim_head, trim_tail;
511 compute_trims (ref, live, &trim_head, &trim_tail, stmt);
512
513 /* The amount of data trimmed from the head or tail must be at
514 least half the size of the object to ensure we're trimming
515 the entire real or imaginary half. By writing things this
516 way we avoid more O(n) bitmap operations. */
517 if (known_ge (trim_tail * 2 * BITS_PER_UNIT, ref->size))
518 {
519 /* TREE_REALPART is live */
520 tree x = TREE_REALPART (gimple_assign_rhs1 (stmt));
521 tree y = gimple_assign_lhs (stmt);
522 y = build1 (REALPART_EXPR, TREE_TYPE (x), y);
523 gimple_assign_set_lhs (stmt, y);
524 gimple_assign_set_rhs1 (stmt, x);
525 }
526 else if (known_ge (trim_head * 2 * BITS_PER_UNIT, ref->size))
527 {
528 /* TREE_IMAGPART is live */
529 tree x = TREE_IMAGPART (gimple_assign_rhs1 (stmt));
530 tree y = gimple_assign_lhs (stmt);
531 y = build1 (IMAGPART_EXPR, TREE_TYPE (x), y);
532 gimple_assign_set_lhs (stmt, y);
533 gimple_assign_set_rhs1 (stmt, x);
534 }
535
536 /* Other cases indicate parts of both the real and imag subobjects
537 are live. We do not try to optimize those cases. */
538 }
539
540 /* STMT initializes an object using a CONSTRUCTOR where one or more of the
541 bytes written are dead stores. ORIG is the bitmap of bytes stored by
542 STMT. LIVE is the bitmap of stores that are actually live.
543
544 Attempt to rewrite STMT so that only the real or imaginary part of
545 the object is actually stored.
546
547 The most common case for getting here is a CONSTRUCTOR with no elements
548 being used to zero initialize an object. We do not try to handle other
549 cases as those would force us to fully cover the object with the
550 CONSTRUCTOR node except for the components that are dead. */
551
552 static void
553 maybe_trim_constructor_store (ao_ref *ref, sbitmap live, gimple *stmt)
554 {
555 tree ctor = gimple_assign_rhs1 (stmt);
556
557 /* This is the only case we currently handle. It actually seems to
558 catch most cases of actual interest. */
559 gcc_assert (CONSTRUCTOR_NELTS (ctor) == 0);
560
561 int head_trim = 0;
562 int tail_trim = 0;
563 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
564
565 /* Now we want to replace the constructor initializer
566 with memset (object + head_trim, 0, size - head_trim - tail_trim). */
567 if (head_trim || tail_trim)
568 {
569 /* We want &lhs for the MEM_REF expression. */
570 tree lhs_addr = build_fold_addr_expr (gimple_assign_lhs (stmt));
571
572 if (! is_gimple_min_invariant (lhs_addr))
573 return;
574
575 /* The number of bytes for the new constructor. */
576 poly_int64 ref_bytes = exact_div (ref->size, BITS_PER_UNIT);
577 poly_int64 count = ref_bytes - head_trim - tail_trim;
578
579 /* And the new type for the CONSTRUCTOR. Essentially it's just
580 a char array large enough to cover the non-trimmed parts of
581 the original CONSTRUCTOR. Note we want explicit bounds here
582 so that we know how many bytes to clear when expanding the
583 CONSTRUCTOR. */
584 tree type = build_array_type_nelts (char_type_node, count);
585
586 /* Build a suitable alias type rather than using alias set zero
587 to avoid pessimizing. */
588 tree alias_type = reference_alias_ptr_type (gimple_assign_lhs (stmt));
589
590 /* Build a MEM_REF representing the whole accessed area, starting
591 at the first byte not trimmed. */
592 tree exp = fold_build2 (MEM_REF, type, lhs_addr,
593 build_int_cst (alias_type, head_trim));
594
595 /* Now update STMT with a new RHS and LHS. */
596 gimple_assign_set_lhs (stmt, exp);
597 gimple_assign_set_rhs1 (stmt, build_constructor (type, NULL));
598 }
599 }
600
601 /* STMT is a memcpy, memmove or memset. Decrement the number of bytes
602 copied/set by DECREMENT. */
603 static void
604 decrement_count (gimple *stmt, int decrement)
605 {
606 tree *countp = gimple_call_arg_ptr (stmt, 2);
607 gcc_assert (TREE_CODE (*countp) == INTEGER_CST);
608 *countp = wide_int_to_tree (TREE_TYPE (*countp), (TREE_INT_CST_LOW (*countp)
609 - decrement));
610 }
611
612 static void
613 increment_start_addr (gimple *stmt, tree *where, int increment)
614 {
615 if (tree lhs = gimple_call_lhs (stmt))
616 if (where == gimple_call_arg_ptr (stmt, 0))
617 {
618 gassign *newop = gimple_build_assign (lhs, unshare_expr (*where));
619 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
620 gsi_insert_after (&gsi, newop, GSI_SAME_STMT);
621 gimple_call_set_lhs (stmt, NULL_TREE);
622 update_stmt (stmt);
623 }
624
625 if (TREE_CODE (*where) == SSA_NAME)
626 {
627 tree tem = make_ssa_name (TREE_TYPE (*where));
628 gassign *newop
629 = gimple_build_assign (tem, POINTER_PLUS_EXPR, *where,
630 build_int_cst (sizetype, increment));
631 gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
632 gsi_insert_before (&gsi, newop, GSI_SAME_STMT);
633 *where = tem;
634 update_stmt (stmt);
635 return;
636 }
637
638 *where = build_fold_addr_expr (fold_build2 (MEM_REF, char_type_node,
639 *where,
640 build_int_cst (ptr_type_node,
641 increment)));
642 }
643
644 /* STMT is builtin call that writes bytes in bitmap ORIG, some bytes are dead
645 (ORIG & ~NEW) and need not be stored. Try to rewrite STMT to reduce
646 the amount of data it actually writes.
647
648 Right now we only support trimming from the head or the tail of the
649 memory region. In theory we could split the mem* call, but it's
650 likely of marginal value. */
651
652 static void
653 maybe_trim_memstar_call (ao_ref *ref, sbitmap live, gimple *stmt)
654 {
655 int head_trim, tail_trim;
656 switch (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)))
657 {
658 case BUILT_IN_STRNCPY:
659 case BUILT_IN_STRNCPY_CHK:
660 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
661 if (head_trim)
662 {
663 /* Head trimming of strncpy is only possible if we can
664 prove all bytes we would trim are non-zero (or we could
665 turn the strncpy into memset if there must be zero
666 among the head trimmed bytes). If we don't know anything
667 about those bytes, the presence or absence of '\0' bytes
668 in there will affect whether it acts for the non-trimmed
669 bytes as memset or memcpy/strncpy. */
670 c_strlen_data lendata = { };
671 int orig_head_trim = head_trim;
672 tree srcstr = gimple_call_arg (stmt, 1);
673 if (!get_range_strlen (srcstr, &lendata, /*eltsize=*/1)
674 || !tree_fits_uhwi_p (lendata.minlen))
675 head_trim = 0;
676 else if (tree_to_uhwi (lendata.minlen) < (unsigned) head_trim)
677 {
678 head_trim = tree_to_uhwi (lendata.minlen);
679 if ((orig_head_trim & (UNITS_PER_WORD - 1)) == 0)
680 head_trim &= ~(UNITS_PER_WORD - 1);
681 }
682 if (orig_head_trim != head_trim
683 && dump_file
684 && (dump_flags & TDF_DETAILS))
685 fprintf (dump_file,
686 " Adjusting strncpy trimming to (head = %d,"
687 " tail = %d)\n", head_trim, tail_trim);
688 }
689 goto do_memcpy;
690
691 case BUILT_IN_MEMCPY:
692 case BUILT_IN_MEMMOVE:
693 case BUILT_IN_MEMCPY_CHK:
694 case BUILT_IN_MEMMOVE_CHK:
695 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
696
697 do_memcpy:
698 /* Tail trimming is easy, we can just reduce the count. */
699 if (tail_trim)
700 decrement_count (stmt, tail_trim);
701
702 /* Head trimming requires adjusting all the arguments. */
703 if (head_trim)
704 {
705 /* For __*_chk need to adjust also the last argument. */
706 if (gimple_call_num_args (stmt) == 4)
707 {
708 tree size = gimple_call_arg (stmt, 3);
709 if (!tree_fits_uhwi_p (size))
710 break;
711 if (!integer_all_onesp (size))
712 {
713 unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
714 if (sz < (unsigned) head_trim)
715 break;
716 tree arg = wide_int_to_tree (TREE_TYPE (size),
717 sz - head_trim);
718 gimple_call_set_arg (stmt, 3, arg);
719 }
720 }
721 tree *dst = gimple_call_arg_ptr (stmt, 0);
722 increment_start_addr (stmt, dst, head_trim);
723 tree *src = gimple_call_arg_ptr (stmt, 1);
724 increment_start_addr (stmt, src, head_trim);
725 decrement_count (stmt, head_trim);
726 }
727 break;
728
729 case BUILT_IN_MEMSET:
730 case BUILT_IN_MEMSET_CHK:
731 compute_trims (ref, live, &head_trim, &tail_trim, stmt);
732
733 /* Tail trimming is easy, we can just reduce the count. */
734 if (tail_trim)
735 decrement_count (stmt, tail_trim);
736
737 /* Head trimming requires adjusting all the arguments. */
738 if (head_trim)
739 {
740 /* For __*_chk need to adjust also the last argument. */
741 if (gimple_call_num_args (stmt) == 4)
742 {
743 tree size = gimple_call_arg (stmt, 3);
744 if (!tree_fits_uhwi_p (size))
745 break;
746 if (!integer_all_onesp (size))
747 {
748 unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
749 if (sz < (unsigned) head_trim)
750 break;
751 tree arg = wide_int_to_tree (TREE_TYPE (size),
752 sz - head_trim);
753 gimple_call_set_arg (stmt, 3, arg);
754 }
755 }
756 tree *dst = gimple_call_arg_ptr (stmt, 0);
757 increment_start_addr (stmt, dst, head_trim);
758 decrement_count (stmt, head_trim);
759 }
760 break;
761
762 default:
763 break;
764 }
765 }
766
767 /* STMT is a memory write where one or more bytes written are dead
768 stores. ORIG is the bitmap of bytes stored by STMT. LIVE is the
769 bitmap of stores that are actually live.
770
771 Attempt to rewrite STMT so that it writes fewer memory locations. Right
772 now we only support trimming at the start or end of the memory region.
773 It's not clear how much there is to be gained by trimming from the middle
774 of the region. */
775
776 static void
777 maybe_trim_partially_dead_store (ao_ref *ref, sbitmap live, gimple *stmt)
778 {
779 if (is_gimple_assign (stmt)
780 && TREE_CODE (gimple_assign_lhs (stmt)) != TARGET_MEM_REF)
781 {
782 switch (gimple_assign_rhs_code (stmt))
783 {
784 case CONSTRUCTOR:
785 maybe_trim_constructor_store (ref, live, stmt);
786 break;
787 case COMPLEX_CST:
788 maybe_trim_complex_store (ref, live, stmt);
789 break;
790 default:
791 break;
792 }
793 }
794 }
795
796 /* Return TRUE if USE_REF reads bytes from LIVE where live is
797 derived from REF, a write reference.
798
799 While this routine may modify USE_REF, it's passed by value, not
800 location. So callers do not see those modifications. */
801
802 static bool
803 live_bytes_read (ao_ref *use_ref, ao_ref *ref, sbitmap live)
804 {
805 /* We have already verified that USE_REF and REF hit the same object.
806 Now verify that there's actually an overlap between USE_REF and REF. */
807 HOST_WIDE_INT start, size;
808 if (get_byte_range (use_ref, ref, false, &start, &size))
809 {
810 /* If USE_REF covers all of REF, then it will hit one or more
811 live bytes. This avoids useless iteration over the bitmap
812 below. */
813 if (start == 0 && known_eq (size * 8, ref->size))
814 return true;
815
816 /* Now check if any of the remaining bits in use_ref are set in LIVE. */
817 return bitmap_bit_in_range_p (live, start, (start + size - 1));
818 }
819 return true;
820 }
821
822 /* Callback for dse_classify_store calling for_each_index. Verify that
823 indices are invariant in the loop with backedge PHI in basic-block DATA. */
824
825 static bool
826 check_name (tree, tree *idx, void *data)
827 {
828 basic_block phi_bb = (basic_block) data;
829 if (TREE_CODE (*idx) == SSA_NAME
830 && !SSA_NAME_IS_DEFAULT_DEF (*idx)
831 && dominated_by_p (CDI_DOMINATORS, gimple_bb (SSA_NAME_DEF_STMT (*idx)),
832 phi_bb))
833 return false;
834 return true;
835 }
836
837 /* STMT stores the value 0 into one or more memory locations
838 (via memset, empty constructor, calloc call, etc).
839
840 See if there is a subsequent store of the value 0 to one
841 or more of the same memory location(s). If so, the subsequent
842 store is redundant and can be removed.
843
844 The subsequent stores could be via memset, empty constructors,
845 simple MEM stores, etc. */
846
847 static void
848 dse_optimize_redundant_stores (gimple *stmt)
849 {
850 int cnt = 0;
851
852 /* TBAA state of STMT, if it is a call it is effectively alias-set zero. */
853 alias_set_type earlier_set = 0;
854 alias_set_type earlier_base_set = 0;
855 if (is_gimple_assign (stmt))
856 {
857 ao_ref lhs_ref;
858 ao_ref_init (&lhs_ref, gimple_assign_lhs (stmt));
859 earlier_set = ao_ref_alias_set (&lhs_ref);
860 earlier_base_set = ao_ref_base_alias_set (&lhs_ref);
861 }
862
863 /* We could do something fairly complex and look through PHIs
864 like DSE_CLASSIFY_STORE, but it doesn't seem to be worth
865 the effort.
866
867 Look at all the immediate uses of the VDEF (which are obviously
868 dominated by STMT). See if one or more stores 0 into the same
869 memory locations a STMT, if so remove the immediate use statements. */
870 tree defvar = gimple_vdef (stmt);
871 imm_use_iterator ui;
872 gimple *use_stmt;
873 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
874 {
875 /* Limit stmt walking. */
876 if (++cnt > param_dse_max_alias_queries_per_store)
877 break;
878
879 /* If USE_STMT stores 0 into one or more of the same locations
880 as STMT and STMT would kill USE_STMT, then we can just remove
881 USE_STMT. */
882 tree fndecl;
883 if ((is_gimple_assign (use_stmt)
884 && gimple_vdef (use_stmt)
885 && (gimple_assign_single_p (use_stmt)
886 && initializer_zerop (gimple_assign_rhs1 (use_stmt))))
887 || (gimple_call_builtin_p (use_stmt, BUILT_IN_NORMAL)
888 && (fndecl = gimple_call_fndecl (use_stmt)) != NULL
889 && (DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
890 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET_CHK)
891 && integer_zerop (gimple_call_arg (use_stmt, 1))))
892 {
893 ao_ref write;
894
895 if (!initialize_ao_ref_for_dse (use_stmt, &write))
896 break;
897
898 if (valid_ao_ref_for_dse (&write)
899 && stmt_kills_ref_p (stmt, &write))
900 {
901 gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
902 if (is_gimple_assign (use_stmt))
903 {
904 ao_ref lhs_ref;
905 ao_ref_init (&lhs_ref, gimple_assign_lhs (use_stmt));
906 if ((earlier_set == ao_ref_alias_set (&lhs_ref)
907 || alias_set_subset_of (ao_ref_alias_set (&lhs_ref),
908 earlier_set))
909 && (earlier_base_set == ao_ref_base_alias_set (&lhs_ref)
910 || alias_set_subset_of
911 (ao_ref_base_alias_set (&lhs_ref),
912 earlier_base_set)))
913 delete_dead_or_redundant_assignment (&gsi, "redundant",
914 need_eh_cleanup,
915 need_ab_cleanup);
916 }
917 else if (is_gimple_call (use_stmt))
918 {
919 if ((earlier_set == 0
920 || alias_set_subset_of (0, earlier_set))
921 && (earlier_base_set == 0
922 || alias_set_subset_of (0, earlier_base_set)))
923 delete_dead_or_redundant_call (&gsi, "redundant");
924 }
925 else
926 gcc_unreachable ();
927 }
928 }
929 }
930 }
931
932 /* Return whether PHI contains ARG as an argument. */
933
934 static bool
935 contains_phi_arg (gphi *phi, tree arg)
936 {
937 for (unsigned i = 0; i < gimple_phi_num_args (phi); ++i)
938 if (gimple_phi_arg_def (phi, i) == arg)
939 return true;
940 return false;
941 }
942
943 /* Hash map of the memory use in a GIMPLE assignment to its
944 data reference. If NULL data-ref analysis isn't used. */
945 static hash_map<gimple *, data_reference_p> *dse_stmt_to_dr_map;
946
947 /* A helper of dse_optimize_stmt.
948 Given a GIMPLE_ASSIGN in STMT that writes to REF, classify it
949 according to downstream uses and defs. Sets *BY_CLOBBER_P to true
950 if only clobber statements influenced the classification result.
951 Returns the classification. */
952
953 dse_store_status
954 dse_classify_store (ao_ref *ref, gimple *stmt,
955 bool byte_tracking_enabled, sbitmap live_bytes,
956 bool *by_clobber_p, tree stop_at_vuse)
957 {
958 gimple *temp;
959 int cnt = 0;
960 auto_bitmap visited;
961 std::unique_ptr<data_reference, void(*)(data_reference_p)>
962 dra (nullptr, free_data_ref);
963
964 if (by_clobber_p)
965 *by_clobber_p = true;
966
967 /* Find the first dominated statement that clobbers (part of) the
968 memory stmt stores to with no intermediate statement that may use
969 part of the memory stmt stores. That is, find a store that may
970 prove stmt to be a dead store. */
971 temp = stmt;
972 do
973 {
974 gimple *use_stmt;
975 imm_use_iterator ui;
976 bool fail = false;
977 tree defvar;
978
979 if (gimple_code (temp) == GIMPLE_PHI)
980 {
981 /* If we visit this PHI by following a backedge then we have to
982 make sure ref->ref only refers to SSA names that are invariant
983 with respect to the loop represented by this PHI node. */
984 if (dominated_by_p (CDI_DOMINATORS, gimple_bb (stmt),
985 gimple_bb (temp))
986 && !for_each_index (ref->ref ? &ref->ref : &ref->base,
987 check_name, gimple_bb (temp)))
988 return DSE_STORE_LIVE;
989 defvar = PHI_RESULT (temp);
990 bitmap_set_bit (visited, SSA_NAME_VERSION (defvar));
991 }
992 else
993 defvar = gimple_vdef (temp);
994
995 /* If we're instructed to stop walking at region boundary, do so. */
996 if (defvar == stop_at_vuse)
997 return DSE_STORE_LIVE;
998
999 auto_vec<gimple *, 10> defs;
1000 gphi *first_phi_def = NULL;
1001 gphi *last_phi_def = NULL;
1002 FOR_EACH_IMM_USE_STMT (use_stmt, ui, defvar)
1003 {
1004 /* Limit stmt walking. */
1005 if (++cnt > param_dse_max_alias_queries_per_store)
1006 {
1007 fail = true;
1008 break;
1009 }
1010
1011 /* In simple cases we can look through PHI nodes, but we
1012 have to be careful with loops and with memory references
1013 containing operands that are also operands of PHI nodes.
1014 See gcc.c-torture/execute/20051110-*.c. */
1015 if (gimple_code (use_stmt) == GIMPLE_PHI)
1016 {
1017 /* If we already visited this PHI ignore it for further
1018 processing. */
1019 if (!bitmap_bit_p (visited,
1020 SSA_NAME_VERSION (PHI_RESULT (use_stmt))))
1021 {
1022 defs.safe_push (use_stmt);
1023 if (!first_phi_def)
1024 first_phi_def = as_a <gphi *> (use_stmt);
1025 last_phi_def = as_a <gphi *> (use_stmt);
1026 }
1027 }
1028 /* If the statement is a use the store is not dead. */
1029 else if (ref_maybe_used_by_stmt_p (use_stmt, ref))
1030 {
1031 if (dse_stmt_to_dr_map
1032 && ref->ref
1033 && is_gimple_assign (use_stmt))
1034 {
1035 if (!dra)
1036 dra.reset (create_data_ref (NULL, NULL, ref->ref, stmt,
1037 false, false));
1038 bool existed_p;
1039 data_reference_p &drb
1040 = dse_stmt_to_dr_map->get_or_insert (use_stmt, &existed_p);
1041 if (!existed_p)
1042 drb = create_data_ref (NULL, NULL,
1043 gimple_assign_rhs1 (use_stmt),
1044 use_stmt, false, false);
1045 if (!dr_may_alias_p (dra.get (), drb, NULL))
1046 {
1047 if (gimple_vdef (use_stmt))
1048 defs.safe_push (use_stmt);
1049 continue;
1050 }
1051 }
1052
1053 /* Handle common cases where we can easily build an ao_ref
1054 structure for USE_STMT and in doing so we find that the
1055 references hit non-live bytes and thus can be ignored.
1056
1057 TODO: We can also use modref summary to handle calls. */
1058 if (byte_tracking_enabled
1059 && is_gimple_assign (use_stmt))
1060 {
1061 ao_ref use_ref;
1062 ao_ref_init (&use_ref, gimple_assign_rhs1 (use_stmt));
1063 if (valid_ao_ref_for_dse (&use_ref)
1064 && operand_equal_p (use_ref.base, ref->base,
1065 OEP_ADDRESS_OF)
1066 && !live_bytes_read (&use_ref, ref, live_bytes))
1067 {
1068 /* If this is a store, remember it as we possibly
1069 need to walk the defs uses. */
1070 if (gimple_vdef (use_stmt))
1071 defs.safe_push (use_stmt);
1072 continue;
1073 }
1074 }
1075
1076 fail = true;
1077 break;
1078 }
1079 /* We have visited ourselves already so ignore STMT for the
1080 purpose of chaining. */
1081 else if (use_stmt == stmt)
1082 ;
1083 /* If this is a store, remember it as we possibly need to walk the
1084 defs uses. */
1085 else if (gimple_vdef (use_stmt))
1086 defs.safe_push (use_stmt);
1087 }
1088
1089 if (fail)
1090 {
1091 /* STMT might be partially dead and we may be able to reduce
1092 how many memory locations it stores into. */
1093 if (byte_tracking_enabled && !gimple_clobber_p (stmt))
1094 return DSE_STORE_MAYBE_PARTIAL_DEAD;
1095 return DSE_STORE_LIVE;
1096 }
1097
1098 /* If we didn't find any definition this means the store is dead
1099 if it isn't a store to global reachable memory. In this case
1100 just pretend the stmt makes itself dead. Otherwise fail. */
1101 if (defs.is_empty ())
1102 {
1103 if (ref_may_alias_global_p (ref, false))
1104 return DSE_STORE_LIVE;
1105
1106 if (by_clobber_p)
1107 *by_clobber_p = false;
1108 return DSE_STORE_DEAD;
1109 }
1110
1111 /* Process defs and remove those we need not process further. */
1112 for (unsigned i = 0; i < defs.length ();)
1113 {
1114 gimple *def = defs[i];
1115 gimple *use_stmt;
1116 use_operand_p use_p;
1117 tree vdef = (gimple_code (def) == GIMPLE_PHI
1118 ? gimple_phi_result (def) : gimple_vdef (def));
1119 gphi *phi_def;
1120 /* If the path to check starts with a kill we do not need to
1121 process it further.
1122 ??? With byte tracking we need only kill the bytes currently
1123 live. */
1124 if (stmt_kills_ref_p (def, ref))
1125 {
1126 if (by_clobber_p && !gimple_clobber_p (def))
1127 *by_clobber_p = false;
1128 defs.unordered_remove (i);
1129 }
1130 /* If the path ends here we do not need to process it further.
1131 This for example happens with calls to noreturn functions. */
1132 else if (has_zero_uses (vdef))
1133 {
1134 /* But if the store is to global memory it is definitely
1135 not dead. */
1136 if (ref_may_alias_global_p (ref, false))
1137 return DSE_STORE_LIVE;
1138 defs.unordered_remove (i);
1139 }
1140 /* In addition to kills we can remove defs whose only use
1141 is another def in defs. That can only ever be PHIs of which
1142 we track two for simplicity reasons, the first and last in
1143 {first,last}_phi_def (we fail for multiple PHIs anyways).
1144 We can also ignore defs that feed only into
1145 already visited PHIs. */
1146 else if (single_imm_use (vdef, &use_p, &use_stmt)
1147 && (use_stmt == first_phi_def
1148 || use_stmt == last_phi_def
1149 || (gimple_code (use_stmt) == GIMPLE_PHI
1150 && bitmap_bit_p (visited,
1151 SSA_NAME_VERSION
1152 (PHI_RESULT (use_stmt))))))
1153 {
1154 defs.unordered_remove (i);
1155 if (def == first_phi_def)
1156 first_phi_def = NULL;
1157 else if (def == last_phi_def)
1158 last_phi_def = NULL;
1159 }
1160 /* If def is a PHI and one of its arguments is another PHI node still
1161 in consideration we can defer processing it. */
1162 else if ((phi_def = dyn_cast <gphi *> (def))
1163 && ((last_phi_def
1164 && phi_def != last_phi_def
1165 && contains_phi_arg (phi_def,
1166 gimple_phi_result (last_phi_def)))
1167 || (first_phi_def
1168 && phi_def != first_phi_def
1169 && contains_phi_arg
1170 (phi_def, gimple_phi_result (first_phi_def)))))
1171 {
1172 defs.unordered_remove (i);
1173 if (phi_def == first_phi_def)
1174 first_phi_def = NULL;
1175 else if (phi_def == last_phi_def)
1176 last_phi_def = NULL;
1177 }
1178 else
1179 ++i;
1180 }
1181
1182 /* If all defs kill the ref we are done. */
1183 if (defs.is_empty ())
1184 return DSE_STORE_DEAD;
1185 /* If more than one def survives fail. */
1186 if (defs.length () > 1)
1187 {
1188 /* STMT might be partially dead and we may be able to reduce
1189 how many memory locations it stores into. */
1190 if (byte_tracking_enabled && !gimple_clobber_p (stmt))
1191 return DSE_STORE_MAYBE_PARTIAL_DEAD;
1192 return DSE_STORE_LIVE;
1193 }
1194 temp = defs[0];
1195
1196 /* Track partial kills. */
1197 if (byte_tracking_enabled)
1198 {
1199 clear_bytes_written_by (live_bytes, temp, ref);
1200 if (bitmap_empty_p (live_bytes))
1201 {
1202 if (by_clobber_p && !gimple_clobber_p (temp))
1203 *by_clobber_p = false;
1204 return DSE_STORE_DEAD;
1205 }
1206 }
1207 }
1208 /* Continue walking until there are no more live bytes. */
1209 while (1);
1210 }
1211
1212
1213 /* Delete a dead call at GSI, which is mem* call of some kind. */
1214 static void
1215 delete_dead_or_redundant_call (gimple_stmt_iterator *gsi, const char *type)
1216 {
1217 gimple *stmt = gsi_stmt (*gsi);
1218 if (dump_file && (dump_flags & TDF_DETAILS))
1219 {
1220 fprintf (dump_file, " Deleted %s call: ", type);
1221 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1222 fprintf (dump_file, "\n");
1223 }
1224
1225 basic_block bb = gimple_bb (stmt);
1226 tree lhs = gimple_call_lhs (stmt);
1227 if (lhs)
1228 {
1229 tree ptr = gimple_call_arg (stmt, 0);
1230 gimple *new_stmt = gimple_build_assign (lhs, ptr);
1231 unlink_stmt_vdef (stmt);
1232 if (gsi_replace (gsi, new_stmt, true))
1233 bitmap_set_bit (need_eh_cleanup, bb->index);
1234 }
1235 else
1236 {
1237 /* Then we need to fix the operand of the consuming stmt. */
1238 unlink_stmt_vdef (stmt);
1239
1240 /* Remove the dead store. */
1241 if (gsi_remove (gsi, true))
1242 bitmap_set_bit (need_eh_cleanup, bb->index);
1243 release_defs (stmt);
1244 }
1245 }
1246
1247 /* Delete a dead store at GSI, which is a gimple assignment. */
1248
1249 void
1250 delete_dead_or_redundant_assignment (gimple_stmt_iterator *gsi,
1251 const char *type,
1252 bitmap need_eh_cleanup,
1253 bitmap need_ab_cleanup)
1254 {
1255 gimple *stmt = gsi_stmt (*gsi);
1256 if (dump_file && (dump_flags & TDF_DETAILS))
1257 {
1258 fprintf (dump_file, " Deleted %s store: ", type);
1259 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1260 fprintf (dump_file, "\n");
1261 }
1262
1263 /* Then we need to fix the operand of the consuming stmt. */
1264 unlink_stmt_vdef (stmt);
1265
1266 /* Remove the dead store. */
1267 basic_block bb = gimple_bb (stmt);
1268 if (need_ab_cleanup && stmt_can_make_abnormal_goto (stmt))
1269 bitmap_set_bit (need_ab_cleanup, bb->index);
1270 if (gsi_remove (gsi, true) && need_eh_cleanup)
1271 bitmap_set_bit (need_eh_cleanup, bb->index);
1272
1273 /* And release any SSA_NAMEs set in this statement back to the
1274 SSA_NAME manager. */
1275 release_defs (stmt);
1276 }
1277
1278 /* Try to prove, using modref summary, that all memory written to by a call is
1279 dead and remove it. Assume that if return value is written to memory
1280 it is already proved to be dead. */
1281
1282 static bool
1283 dse_optimize_call (gimple_stmt_iterator *gsi, sbitmap live_bytes)
1284 {
1285 gcall *stmt = dyn_cast <gcall *> (gsi_stmt (*gsi));
1286
1287 if (!stmt)
1288 return false;
1289
1290 tree callee = gimple_call_fndecl (stmt);
1291
1292 if (!callee)
1293 return false;
1294
1295 /* Pure/const functions are optimized by normal DCE
1296 or handled as store above. */
1297 int flags = gimple_call_flags (stmt);
1298 if ((flags & (ECF_PURE|ECF_CONST|ECF_NOVOPS))
1299 && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
1300 return false;
1301
1302 cgraph_node *node = cgraph_node::get (callee);
1303 if (!node)
1304 return false;
1305
1306 if (stmt_could_throw_p (cfun, stmt)
1307 && !cfun->can_delete_dead_exceptions)
1308 return false;
1309
1310 /* If return value is used the call is not dead. */
1311 tree lhs = gimple_call_lhs (stmt);
1312 if (lhs && TREE_CODE (lhs) == SSA_NAME)
1313 {
1314 imm_use_iterator ui;
1315 gimple *use_stmt;
1316 FOR_EACH_IMM_USE_STMT (use_stmt, ui, lhs)
1317 if (!is_gimple_debug (use_stmt))
1318 return false;
1319 }
1320
1321 /* Verify that there are no side-effects except for return value
1322 and memory writes tracked by modref. */
1323 modref_summary *summary = get_modref_function_summary (node);
1324 if (!summary || !summary->try_dse)
1325 return false;
1326
1327 bool by_clobber_p = false;
1328
1329 /* Walk all memory writes and verify that they are dead. */
1330 for (auto base_node : summary->stores->bases)
1331 for (auto ref_node : base_node->refs)
1332 for (auto access_node : ref_node->accesses)
1333 {
1334 tree arg = access_node.get_call_arg (stmt);
1335
1336 if (!arg || !POINTER_TYPE_P (TREE_TYPE (arg)))
1337 return false;
1338
1339 if (integer_zerop (arg)
1340 && !targetm.addr_space.zero_address_valid
1341 (TYPE_ADDR_SPACE (TREE_TYPE (arg))))
1342 continue;
1343
1344 ao_ref ref;
1345
1346 if (!access_node.get_ao_ref (stmt, &ref))
1347 return false;
1348 ref.ref_alias_set = ref_node->ref;
1349 ref.base_alias_set = base_node->base;
1350
1351 bool byte_tracking_enabled
1352 = setup_live_bytes_from_ref (&ref, live_bytes);
1353 enum dse_store_status store_status;
1354
1355 store_status = dse_classify_store (&ref, stmt,
1356 byte_tracking_enabled,
1357 live_bytes, &by_clobber_p);
1358 if (store_status != DSE_STORE_DEAD)
1359 return false;
1360 }
1361 delete_dead_or_redundant_assignment (gsi, "dead", need_eh_cleanup,
1362 need_ab_cleanup);
1363 return true;
1364 }
1365
1366 /* Attempt to eliminate dead stores in the statement referenced by BSI.
1367
1368 A dead store is a store into a memory location which will later be
1369 overwritten by another store without any intervening loads. In this
1370 case the earlier store can be deleted.
1371
1372 In our SSA + virtual operand world we use immediate uses of virtual
1373 operands to detect dead stores. If a store's virtual definition
1374 is used precisely once by a later store to the same location which
1375 post dominates the first store, then the first store is dead. */
1376
1377 static void
1378 dse_optimize_stmt (function *fun, gimple_stmt_iterator *gsi, sbitmap live_bytes)
1379 {
1380 gimple *stmt = gsi_stmt (*gsi);
1381
1382 /* Don't return early on *this_2(D) ={v} {CLOBBER}. */
1383 if (gimple_has_volatile_ops (stmt)
1384 && (!gimple_clobber_p (stmt)
1385 || TREE_CODE (gimple_assign_lhs (stmt)) != MEM_REF))
1386 return;
1387
1388 ao_ref ref;
1389 /* If this is not a store we can still remove dead call using
1390 modref summary. Note we specifically allow ref to be initialized
1391 to a conservative may-def since we are looking for followup stores
1392 to kill all of it. */
1393 if (!initialize_ao_ref_for_dse (stmt, &ref, true))
1394 {
1395 dse_optimize_call (gsi, live_bytes);
1396 return;
1397 }
1398
1399 /* We know we have virtual definitions. We can handle assignments and
1400 some builtin calls. */
1401 if (gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
1402 {
1403 tree fndecl = gimple_call_fndecl (stmt);
1404 switch (DECL_FUNCTION_CODE (fndecl))
1405 {
1406 case BUILT_IN_MEMCPY:
1407 case BUILT_IN_MEMMOVE:
1408 case BUILT_IN_STRNCPY:
1409 case BUILT_IN_MEMSET:
1410 case BUILT_IN_MEMCPY_CHK:
1411 case BUILT_IN_MEMMOVE_CHK:
1412 case BUILT_IN_STRNCPY_CHK:
1413 case BUILT_IN_MEMSET_CHK:
1414 {
1415 /* Occasionally calls with an explicit length of zero
1416 show up in the IL. It's pointless to do analysis
1417 on them, they're trivially dead. */
1418 tree size = gimple_call_arg (stmt, 2);
1419 if (integer_zerop (size))
1420 {
1421 delete_dead_or_redundant_call (gsi, "dead");
1422 return;
1423 }
1424
1425 /* If this is a memset call that initializes an object
1426 to zero, it may be redundant with an earlier memset
1427 or empty CONSTRUCTOR of a larger object. */
1428 if ((DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET
1429 || DECL_FUNCTION_CODE (fndecl) == BUILT_IN_MEMSET_CHK)
1430 && integer_zerop (gimple_call_arg (stmt, 1)))
1431 dse_optimize_redundant_stores (stmt);
1432
1433 enum dse_store_status store_status;
1434 bool byte_tracking_enabled
1435 = setup_live_bytes_from_ref (&ref, live_bytes);
1436 store_status = dse_classify_store (&ref, stmt,
1437 byte_tracking_enabled,
1438 live_bytes);
1439 if (store_status == DSE_STORE_LIVE)
1440 return;
1441
1442 if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
1443 {
1444 maybe_trim_memstar_call (&ref, live_bytes, stmt);
1445 return;
1446 }
1447
1448 if (store_status == DSE_STORE_DEAD)
1449 delete_dead_or_redundant_call (gsi, "dead");
1450 return;
1451 }
1452
1453 case BUILT_IN_CALLOC:
1454 /* We already know the arguments are integer constants. */
1455 dse_optimize_redundant_stores (stmt);
1456 return;
1457
1458 default:
1459 return;
1460 }
1461 }
1462 else if (is_gimple_call (stmt)
1463 && gimple_call_internal_p (stmt))
1464 {
1465 switch (gimple_call_internal_fn (stmt))
1466 {
1467 case IFN_LEN_STORE:
1468 case IFN_MASK_STORE:
1469 {
1470 enum dse_store_status store_status;
1471 store_status = dse_classify_store (&ref, stmt, false, live_bytes);
1472 if (store_status == DSE_STORE_DEAD)
1473 delete_dead_or_redundant_call (gsi, "dead");
1474 return;
1475 }
1476 default:;
1477 }
1478 }
1479
1480 bool by_clobber_p = false;
1481
1482 /* Check if this statement stores zero to a memory location,
1483 and if there is a subsequent store of zero to the same
1484 memory location. If so, remove the subsequent store. */
1485 if (gimple_assign_single_p (stmt)
1486 && initializer_zerop (gimple_assign_rhs1 (stmt)))
1487 dse_optimize_redundant_stores (stmt);
1488
1489 /* Self-assignments are zombies. */
1490 if (is_gimple_assign (stmt)
1491 && operand_equal_p (gimple_assign_rhs1 (stmt),
1492 gimple_assign_lhs (stmt), 0))
1493 ;
1494 else
1495 {
1496 bool byte_tracking_enabled
1497 = setup_live_bytes_from_ref (&ref, live_bytes);
1498 enum dse_store_status store_status;
1499 store_status = dse_classify_store (&ref, stmt,
1500 byte_tracking_enabled,
1501 live_bytes, &by_clobber_p);
1502 if (store_status == DSE_STORE_LIVE)
1503 return;
1504
1505 if (store_status == DSE_STORE_MAYBE_PARTIAL_DEAD)
1506 {
1507 maybe_trim_partially_dead_store (&ref, live_bytes, stmt);
1508 return;
1509 }
1510 }
1511
1512 /* Now we know that use_stmt kills the LHS of stmt. */
1513
1514 /* But only remove *this_2(D) ={v} {CLOBBER} if killed by
1515 another clobber stmt. */
1516 if (gimple_clobber_p (stmt)
1517 && !by_clobber_p)
1518 return;
1519
1520 if (is_gimple_call (stmt)
1521 && (gimple_has_side_effects (stmt)
1522 || (stmt_could_throw_p (fun, stmt)
1523 && !fun->can_delete_dead_exceptions)))
1524 {
1525 /* See if we can remove complete call. */
1526 if (dse_optimize_call (gsi, live_bytes))
1527 return;
1528 /* Make sure we do not remove a return slot we cannot reconstruct
1529 later. */
1530 if (gimple_call_return_slot_opt_p (as_a <gcall *>(stmt))
1531 && (TREE_ADDRESSABLE (TREE_TYPE (gimple_call_fntype (stmt)))
1532 || !poly_int_tree_p
1533 (TYPE_SIZE (TREE_TYPE (gimple_call_fntype (stmt))))))
1534 return;
1535 if (dump_file && (dump_flags & TDF_DETAILS))
1536 {
1537 fprintf (dump_file, " Deleted dead store in call LHS: ");
1538 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1539 fprintf (dump_file, "\n");
1540 }
1541 gimple_call_set_lhs (stmt, NULL_TREE);
1542 update_stmt (stmt);
1543 }
1544 else if (!stmt_could_throw_p (fun, stmt)
1545 || fun->can_delete_dead_exceptions)
1546 delete_dead_or_redundant_assignment (gsi, "dead", need_eh_cleanup,
1547 need_ab_cleanup);
1548 }
1549
1550 namespace {
1551
1552 const pass_data pass_data_dse =
1553 {
1554 GIMPLE_PASS, /* type */
1555 "dse", /* name */
1556 OPTGROUP_NONE, /* optinfo_flags */
1557 TV_TREE_DSE, /* tv_id */
1558 ( PROP_cfg | PROP_ssa ), /* properties_required */
1559 0, /* properties_provided */
1560 0, /* properties_destroyed */
1561 0, /* todo_flags_start */
1562 0, /* todo_flags_finish */
1563 };
1564
1565 class pass_dse : public gimple_opt_pass
1566 {
1567 public:
1568 pass_dse (gcc::context *ctxt)
1569 : gimple_opt_pass (pass_data_dse, ctxt), use_dr_analysis_p (false)
1570 {}
1571
1572 /* opt_pass methods: */
1573 opt_pass * clone () final override { return new pass_dse (m_ctxt); }
1574 void set_pass_param (unsigned n, bool param) final override
1575 {
1576 gcc_assert (n == 0);
1577 use_dr_analysis_p = param;
1578 }
1579 bool gate (function *) final override { return flag_tree_dse != 0; }
1580 unsigned int execute (function *) final override;
1581
1582 private:
1583 bool use_dr_analysis_p;
1584 }; // class pass_dse
1585
1586 unsigned int
1587 pass_dse::execute (function *fun)
1588 {
1589 unsigned todo = 0;
1590 bool released_def = false;
1591
1592 need_eh_cleanup = BITMAP_ALLOC (NULL);
1593 need_ab_cleanup = BITMAP_ALLOC (NULL);
1594 auto_sbitmap live_bytes (param_dse_max_object_size);
1595 if (flag_expensive_optimizations && use_dr_analysis_p)
1596 dse_stmt_to_dr_map = new hash_map<gimple *, data_reference_p>;
1597
1598 renumber_gimple_stmt_uids (fun);
1599
1600 calculate_dominance_info (CDI_DOMINATORS);
1601
1602 /* Dead store elimination is fundamentally a reverse program order walk. */
1603 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (fun) - NUM_FIXED_BLOCKS);
1604 int n = pre_and_rev_post_order_compute_fn (fun, NULL, rpo, false);
1605 for (int i = n; i != 0; --i)
1606 {
1607 basic_block bb = BASIC_BLOCK_FOR_FN (fun, rpo[i-1]);
1608 gimple_stmt_iterator gsi;
1609
1610 for (gsi = gsi_last_bb (bb); !gsi_end_p (gsi);)
1611 {
1612 gimple *stmt = gsi_stmt (gsi);
1613
1614 if (gimple_vdef (stmt))
1615 dse_optimize_stmt (fun, &gsi, live_bytes);
1616 else if (def_operand_p
1617 def_p = single_ssa_def_operand (stmt, SSA_OP_DEF))
1618 {
1619 /* When we remove dead stores make sure to also delete trivially
1620 dead SSA defs. */
1621 if (has_zero_uses (DEF_FROM_PTR (def_p))
1622 && !gimple_has_side_effects (stmt)
1623 && !is_ctrl_altering_stmt (stmt)
1624 && (!stmt_could_throw_p (fun, stmt)
1625 || fun->can_delete_dead_exceptions))
1626 {
1627 if (dump_file && (dump_flags & TDF_DETAILS))
1628 {
1629 fprintf (dump_file, " Deleted trivially dead stmt: ");
1630 print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1631 fprintf (dump_file, "\n");
1632 }
1633 if (gsi_remove (&gsi, true) && need_eh_cleanup)
1634 bitmap_set_bit (need_eh_cleanup, bb->index);
1635 release_defs (stmt);
1636 released_def = true;
1637 }
1638 }
1639 if (gsi_end_p (gsi))
1640 gsi = gsi_last_bb (bb);
1641 else
1642 gsi_prev (&gsi);
1643 }
1644 bool removed_phi = false;
1645 for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);)
1646 {
1647 gphi *phi = si.phi ();
1648 if (has_zero_uses (gimple_phi_result (phi)))
1649 {
1650 if (dump_file && (dump_flags & TDF_DETAILS))
1651 {
1652 fprintf (dump_file, " Deleted trivially dead PHI: ");
1653 print_gimple_stmt (dump_file, phi, 0, dump_flags);
1654 fprintf (dump_file, "\n");
1655 }
1656 remove_phi_node (&si, true);
1657 removed_phi = true;
1658 released_def = true;
1659 }
1660 else
1661 gsi_next (&si);
1662 }
1663 if (removed_phi && gimple_seq_empty_p (phi_nodes (bb)))
1664 todo |= TODO_cleanup_cfg;
1665 }
1666 free (rpo);
1667
1668 /* Removal of stores may make some EH edges dead. Purge such edges from
1669 the CFG as needed. */
1670 if (!bitmap_empty_p (need_eh_cleanup))
1671 {
1672 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
1673 todo |= TODO_cleanup_cfg;
1674 }
1675 if (!bitmap_empty_p (need_ab_cleanup))
1676 {
1677 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
1678 todo |= TODO_cleanup_cfg;
1679 }
1680
1681 BITMAP_FREE (need_eh_cleanup);
1682 BITMAP_FREE (need_ab_cleanup);
1683
1684 if (released_def)
1685 free_numbers_of_iterations_estimates (fun);
1686
1687 if (flag_expensive_optimizations && use_dr_analysis_p)
1688 {
1689 for (auto i = dse_stmt_to_dr_map->begin ();
1690 i != dse_stmt_to_dr_map->end (); ++i)
1691 free_data_ref ((*i).second);
1692 delete dse_stmt_to_dr_map;
1693 dse_stmt_to_dr_map = NULL;
1694 }
1695
1696 return todo;
1697 }
1698
1699 } // anon namespace
1700
1701 gimple_opt_pass *
1702 make_pass_dse (gcc::context *ctxt)
1703 {
1704 return new pass_dse (ctxt);
1705 }
This page took 0.113136 seconds and 5 git commands to generate.