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