diff options
author | Jakub Jelinek <jakub@redhat.com> | 2024-05-15 18:37:17 +0200 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2024-06-20 15:07:42 +0200 |
commit | 82bb4ee090342f2b787661420f701f2e0bf6624a (patch) | |
tree | 983c87e4da348198dc0b88b987910761375d9026 | |
parent | e4bd9558d431bee2f4a76875dd79c9a76d2c58ff (diff) | |
download | gcc-82bb4ee090342f2b787661420f701f2e0bf6624a.zip gcc-82bb4ee090342f2b787661420f701f2e0bf6624a.tar.gz gcc-82bb4ee090342f2b787661420f701f2e0bf6624a.tar.bz2 |
combine: Fix up simplify_compare_const [PR115092]
The following testcases are miscompiled (with tons of GIMPLE
optimization disabled) because combine sees GE comparison of
1-bit sign_extract (i.e. something with [-1, 0] value range)
with (const_int -1) (which is always true) and optimizes it into
NE comparison of 1-bit zero_extract ([0, 1] value range) against
(const_int 0).
The reason is that simplify_compare_const first (correctly)
simplifies the comparison to
GE (ashift:SI something (const_int 31)) (const_int -2147483648)
and then an optimization for when the second operand is power of 2
triggers. That optimization is fine for power of 2s which aren't
the signed minimum of the mode, or if it is NE, EQ, GEU or LTU
against the signed minimum of the mode, but for GE or LT optimizing
it into NE (or EQ) against const0_rtx is wrong, those cases
are always true or always false (but the function doesn't have
a standardized way to tell callers the comparison is now unconditional).
The following patch just disables the optimization in that case.
2024-05-15 Jakub Jelinek <jakub@redhat.com>
PR rtl-optimization/114902
PR rtl-optimization/115092
* combine.c (simplify_compare_const): Don't optimize
GE op0 SIGNED_MIN or LT op0 SIGNED_MIN into NE op0 const0_rtx or
EQ op0 const0_rtx.
* gcc.dg/pr114902.c: New test.
* gcc.dg/pr115092.c: New test.
(cherry picked from commit 0b93a0ae153ef70a82ff63e67926a01fdab9956b)
-rw-r--r-- | gcc/combine.c | 6 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/pr114902.c | 23 | ||||
-rw-r--r-- | gcc/testsuite/gcc.dg/pr115092.c | 16 |
3 files changed, 43 insertions, 2 deletions
diff --git a/gcc/combine.c b/gcc/combine.c index 5422852..0bbd186 100644 --- a/gcc/combine.c +++ b/gcc/combine.c @@ -11879,8 +11879,10 @@ simplify_compare_const (enum rtx_code code, machine_mode mode, `and'ed with that bit), we can replace this with a comparison with zero. */ if (const_op - && (code == EQ || code == NE || code == GE || code == GEU - || code == LT || code == LTU) + && (code == EQ || code == NE || code == GEU || code == LTU + /* This optimization is incorrect for signed >= INT_MIN or + < INT_MIN, those are always true or always false. */ + || ((code == GE || code == LT) && const_op > 0)) && is_a <scalar_int_mode> (mode, &int_mode) && GET_MODE_PRECISION (int_mode) - 1 < HOST_BITS_PER_WIDE_INT && pow2p_hwi (const_op & GET_MODE_MASK (int_mode)) diff --git a/gcc/testsuite/gcc.dg/pr114902.c b/gcc/testsuite/gcc.dg/pr114902.c new file mode 100644 index 0000000..60684fa --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr114902.c @@ -0,0 +1,23 @@ +/* PR rtl-optimization/114902 */ +/* { dg-do run } */ +/* { dg-options "-O1 -fno-tree-fre -fno-tree-forwprop -fno-tree-ccp -fno-tree-dominator-opts" } */ + +__attribute__((noipa)) +int foo (int x) +{ + int a = ~x; + int t = a & 1; + int e = -t; + int b = e >= -1; + if (b) + return 0; + __builtin_trap (); +} + +int +main () +{ + foo (-1); + foo (0); + foo (1); +} diff --git a/gcc/testsuite/gcc.dg/pr115092.c b/gcc/testsuite/gcc.dg/pr115092.c new file mode 100644 index 0000000..c9047f4 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr115092.c @@ -0,0 +1,16 @@ +/* PR rtl-optimization/115092 */ +/* { dg-do run } */ +/* { dg-options "-O1 -fgcse -ftree-pre -fno-tree-dominator-opts -fno-tree-fre -fno-guess-branch-probability" } */ + +int a, b, c = 1, d, e; + +int +main () +{ + int f, g = a; + b = -2; + f = -(1 >> ((c && b) & ~a)); + if (f <= b) + d = g / e; + return 0; +} |