This is the final patch in the kit to improve our DSE implementation.
It's based on a observation by Richi. Namely that a read from bytes of memory that are dead can be ignored. By ignoring such reads we can sometimes find additional stores that allow us to either eliminate or trim an earlier store more aggressively.
This only hit (by hit I mean the ability to ignore resulted in finding a full or partially dead store that we didn't otherwise find) once during a bootstrap, but does hit often in the libstdc++ testsuite.
There's nothing in the BZ database on this issue and I can't reasonably call it a bugfix. I wouldn't lose sleep if this deferred to gcc-8.
Bootstrapped and regression tested on x86-64-linux-gnu. OK for the trunk or defer to gcc-8?
* tree-ssa-dse.c (dse_possible_dead_store_p): Ignore reads of dead bytes. * testsuite/gcc.dg/tree-ssa/ssa-dse-XX.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/dse-2.c b/gcc/testsuite/gcc.dg/tree-ssa/dse-2.c new file mode 100644 index 0000000..6605dfe --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/dse-2.c @@ -0,0 +1,33 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-dse1-details" } */ + +enum constraint_expr_type +{ + SCALAR, DEREF, ADDRESSOF +}; +typedef struct constraint_expr +{ + enum constraint_expr_type type; + unsigned int var; + long offset; +} constraint_expr ; +typedef struct constraint +{ + struct constraint_expr lhs; + struct constraint_expr rhs; +} constraint; +static _Bool +constraint_expr_equal (struct constraint_expr x, struct constraint_expr y) +{ + return x.type == y.type && x.var == y.var && x.offset == y.offset; +} + +_Bool +constraint_equal (struct constraint a, struct constraint b) +{ + return constraint_expr_equal (a.lhs, b.lhs) + && constraint_expr_equal (a.rhs, b.rhs); +} + +/* { dg-final { scan-tree-dump-times "Deleted dead store" 2 "dse1" } } */ + diff --git a/gcc/tree-ssa-dse.c b/gcc/tree-ssa-dse.c index a868f11..1482c7f 100644 --- a/gcc/tree-ssa-dse.c +++ b/gcc/tree-ssa-dse.c @@ -439,6 +439,50 @@ dse_possible_dead_store_p (ao_ref *ref, gimple *stmt, gimple **use_stmt) /* If the statement is a use the store is not dead. */ else if (ref_maybe_used_by_stmt_p (use_stmt, ref)) { + /* Handle common cases where we can easily build a ao_ref + structure for USE_STMT and in doing so we find that the + references hit non-live bytes and thus can be ignored. */ + if (live_bytes) + { + if (is_gimple_assign (use_stmt)) + { + /* Other cases were noted as non-aliasing by + the call to ref_maybe_used_by_stmt_p. */ + ao_ref use_ref; + ao_ref_init (&use_ref, gimple_assign_rhs1 (use_stmt)); + if (valid_ao_ref_for_dse (&use_ref) + && use_ref.base == ref->base + && use_ref.size == use_ref.max_size) + { + size_t start = use_ref.offset / BITS_PER_UNIT; + size_t end = use_ref.size / BITS_PER_UNIT; + bitmap z = BITMAP_ALLOC (NULL); + bitmap_set_range (z, start, end); + bitmap_and_into (z, live_bytes); + if (bitmap_empty_p (z)) + { + BITMAP_FREE (z); + if (gimple_vdef (use_stmt)) + { + /* If we have already seen a store and + this is also a store, then we have to + fail. */ + if (temp) + { + fail = true; + BREAK_FROM_IMM_USE_STMT (ui); + } + + /* Otherwise walk through this store. */ + temp = use_stmt; + } + continue; + } + BITMAP_FREE (z); + } + } + } + fail = true; BREAK_FROM_IMM_USE_STMT (ui); }