decode_field_reference sets *PSIGNBIT when the AND mask, before being
clipped to the field width, has bits that would be extensions of the
sign bit of a signed field. fold_truth_andor_for_ifcombine then adds
the sign bit of the field to the test mask. That's correct when the
mask applies to a sign-extended field, as in (int) s & M, but not when
the mask applies before a narrowing conversion, as in (short) (x & M):
there the upper bits of M select bits of X that the conversion
discards, and they say nothing about the sign bit of the narrowed
value.
For instance, given
int t = x & 0xc7ff3906;
short s = t;
if (s != 0) return 0;
if (t < 0) return 0;
return 1;
ifcombine combined the two tests into (x & 0x8000b906) == 0 instead
of (x & 0x80003906) == 0, because the nonzero upper half of the mask
caused bit 15 to be added to it.
We can't just ignore all upper bits of such masks, though: if the
masked value was shifted right, as in (short) ((x >> 20) & 0x1006),
the field is narrower than the conversion, and mask bits between the
field width and the precision of the narrow type, such as bit 12
here, survive the conversion and do test extensions of the sign bit.
Fix this by truncating the mask to the precision of the narrow type
when the mask is found after a narrowing conversion was dropped, so
that only the bits discarded by the conversion are ignored when
computing SIGNBIT.
Bootstrapped on x86_64-unknown-linux-gnu, testing in progress.
Richard.
PR tree-optimization/128597
* gimple-fold.cc (decode_field_reference): Truncate a mask
applied before a narrowing conversion to the precision of the
narrow type.
* gcc.dg/torture/pr128597-1.c: New testcase.
* gcc.dg/torture/pr128597-2.c: Likewise.
* gcc.dg/pr128597-3.c: Likewise.
---
gcc/gimple-fold.cc | 10 ++
gcc/testsuite/gcc.dg/pr128597-3.c | 160 ++++++++++++++++++++++
gcc/testsuite/gcc.dg/torture/pr128597-1.c | 44 ++++++
gcc/testsuite/gcc.dg/torture/pr128597-2.c | 81 +++++++++++
4 files changed, 295 insertions(+)
create mode 100644 gcc/testsuite/gcc.dg/pr128597-3.c
create mode 100644 gcc/testsuite/gcc.dg/torture/pr128597-1.c
create mode 100644 gcc/testsuite/gcc.dg/torture/pr128597-2.c
diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
index cd881f583fc..afd927f53bf 100644
--- a/gcc/gimple-fold.cc
+++ b/gcc/gimple-fold.cc
@@ -8027,6 +8027,16 @@ decode_field_reference (tree *pexp, HOST_WIDE_INT
*pbitsize,
else
and_mask &= wide_int::from (*pand_mask, prec_op, UNSIGNED);
}
+ /* If we have already dropped a narrowing conversion, it applies to
+ the result of the masking, so mask bits at or above the precision
+ of OUTER_TYPE are discarded by the conversion. Drop them from the
+ mask, so that they're not mistaken for tests of extensions of the
+ sign bit below. Bits below that precision but above the field
+ width, e.g. after a right shift, still test such extensions. */
+ if (outer_type
+ && TYPE_PRECISION (outer_type) < and_mask.get_precision ())
+ and_mask = wide_int::from (and_mask, TYPE_PRECISION (outer_type),
+ UNSIGNED);
}
else
and_mask = *pand_mask;
diff --git a/gcc/testsuite/gcc.dg/pr128597-3.c
b/gcc/testsuite/gcc.dg/pr128597-3.c
new file mode 100644
index 00000000000..e4195f5fe29
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr128597-3.c
@@ -0,0 +1,160 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int32 } */
+/* { dg-options "-O2 -fgimple -fdump-tree-ifcombine-details" } */
+
+/* Check how ifcombine handles masks applied before a narrowing
+ conversion. Starting at ifcombine keeps earlier passes from
+ simplifying the compares. */
+
+/* Mask bits 16 and up are discarded by the conversion: bit 15 must not
+ be added to the combined test. */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f1 (int x)
+{
+ int t;
+ short s;
+ int r;
+
+ __BB(2):
+ t_2 = x_1(D) & _Literal (int) -939575034;
+ s_3 = (short) t_2;
+ if (s_3 == _Literal (short) 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ if (t_2 >= 0)
+ goto __BB5;
+ else
+ goto __BB4;
+
+ __BB(4):
+ goto __BB5;
+
+ __BB(5):
+ r_4 = __PHI (__BB3: 1, __BB4: 0);
+ return r_4;
+}
+
+/* Bit 12 of the mask survives the conversion and tests a copy of the
+ sign bit of X brought in by the shift. */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f2 (int x)
+{
+ int a;
+ int t;
+ short s;
+ int u;
+ int r;
+
+ __BB(2):
+ a_2 = x_1(D) >> 20;
+ t_3 = a_2 & 4102;
+ s_4 = (short) t_3;
+ if (s_4 == _Literal (short) 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ u_5 = x_1(D) & 267386880;
+ if (u_5 == 0)
+ goto __BB5;
+ else
+ goto __BB4;
+
+ __BB(4):
+ goto __BB5;
+
+ __BB(5):
+ r_6 = __PHI (__BB3: 1, __BB4: 0);
+ return r_6;
+}
+
+/* Bits 12 to 15 of the mask are clear, so S is never negative, and the
+ combined condition is always false. */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f3 (int x)
+{
+ int a;
+ int t;
+ short s;
+ int u;
+ int r;
+
+ __BB(2):
+ a_2 = x_1(D) >> 20;
+ t_3 = a_2 & 4094;
+ s_4 = (short) t_3;
+ if (s_4 == _Literal (short) -2)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ u_5 = x_1(D) & 267386880;
+ if (u_5 == 266338304)
+ goto __BB5;
+ else
+ goto __BB4;
+
+ __BB(4):
+ goto __BB5;
+
+ __BB(5):
+ r_6 = __PHI (__BB3: 1, __BB4: 0);
+ return r_6;
+}
+
+/* Mask bits 16 and up are discarded, bits 12 to 15 are clear: the sign
+ bit of X must not be tested. */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f4 (int x)
+{
+ int a;
+ int t;
+ short s;
+ int u;
+ int r;
+
+ __BB(2):
+ a_2 = x_1(D) >> 20;
+ t_3 = a_2 & _Literal (int) -65530;
+ s_4 = (short) t_3;
+ if (s_4 == _Literal (short) 0)
+ goto __BB3;
+ else
+ goto __BB4;
+
+ __BB(3):
+ u_5 = x_1(D) & 267386880;
+ if (u_5 == 0)
+ goto __BB5;
+ else
+ goto __BB4;
+
+ __BB(4):
+ goto __BB5;
+
+ __BB(5):
+ r_6 = __PHI (__BB3: 1, __BB4: 0);
+ return r_6;
+}
+
+int
+main ()
+{
+ int min = -0x7fffffff - 1;
+ if (f1 (0x8000) != 1 || f1 (0x100) != 0 || f1 (min) != 0 || f1 (0) != 1)
+ __builtin_abort ();
+ if (f2 (min) != 0 || f2 (0) != 1 || f2 (0x00600000) != 0)
+ __builtin_abort ();
+ if (f3 ((int) 0xffe00000u) != 0 || f3 (0x0fe00000) != 0)
+ __builtin_abort ();
+ if (f4 (min) != 1 || f4 (0) != 1 || f4 (0x00600000) != 0)
+ __builtin_abort ();
+ return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "optimizing two comparisons" 4
"ifcombine" } } */
diff --git a/gcc/testsuite/gcc.dg/torture/pr128597-1.c
b/gcc/testsuite/gcc.dg/torture/pr128597-1.c
new file mode 100644
index 00000000000..80e136d82d1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128597-1.c
@@ -0,0 +1,44 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int32plus } */
+
+/* Check that upper bits of a mask applied before a narrowing conversion
+ are not taken as tests of extensions of the sign bit of the narrowed
+ field. */
+
+__attribute__((noipa)) int
+f (int x)
+{
+ int t = x & 0xc7ff3906;
+ short s = t;
+ if (s != 0)
+ return 0;
+ if (t < 0)
+ return 0;
+ return 1;
+}
+
+__attribute__((noipa)) int
+g (long long x)
+{
+ long long t = x & 0xffff00007fffff00LL;
+ int s = t;
+ if (s != 0)
+ return 0;
+ if (t < 0)
+ return 0;
+ return 1;
+}
+
+int
+main (void)
+{
+ /* Bit 15 of x is not in the mask, so t == 0. */
+ if (f (0x8000) != 1)
+ __builtin_abort ();
+ if (f (0x80000000) != 0 || f (0x100) != 0)
+ __builtin_abort ();
+ /* Bit 31 of x is set, but it is masked out. */
+ if (sizeof (long long) > sizeof (int) && g (0x80000000LL) != 1)
+ __builtin_abort ();
+ return 0;
+}
diff --git a/gcc/testsuite/gcc.dg/torture/pr128597-2.c
b/gcc/testsuite/gcc.dg/torture/pr128597-2.c
new file mode 100644
index 00000000000..0843caf38fc
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128597-2.c
@@ -0,0 +1,81 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int32 } */
+
+/* Check that, when a mask is applied to a right-shifted value before a
+ narrowing conversion, mask bits that survive the conversion but lie
+ above the shifted field still test extensions of its sign bit, while
+ mask bits discarded by the conversion do not. */
+
+/* Bit 12 of the mask survives the conversion and tests a copy of bit 31
+ of X. */
+__attribute__((noipa)) int
+f1 (int x)
+{
+ int a = x >> 20;
+ int t = a & 0x1006;
+ short s = (short) t;
+ if (s != 0)
+ return 0;
+ if ((x & 0x0ff00000) != 0)
+ return 0;
+ return 1;
+}
+
+/* Likewise, with an unsigned narrow type. */
+__attribute__((noipa)) int
+f2 (int x)
+{
+ int a = x >> 20;
+ int t = a & 0x1006;
+ unsigned short s = (unsigned short) t;
+ if (s != 0)
+ return 0;
+ if ((x & 0x0ff00000) != 0)
+ return 0;
+ return 1;
+}
+
+/* Mask bits 16 and up are discarded by the conversion, and bits 12 to 15
+ are clear, so the sign bit of X is not tested. */
+__attribute__((noipa)) int
+f3 (int x)
+{
+ int a = x >> 20;
+ int t = a & 0xffff0006;
+ short s = (short) t;
+ if (s != 0)
+ return 0;
+ if ((x & 0x0ff00000) != 0)
+ return 0;
+ return 1;
+}
+
+/* Mask bits 16 and up are discarded, but bit 15 survives and tests a
+ copy of bit 31 of X. */
+__attribute__((noipa)) int
+f4 (int x)
+{
+ int a = x >> 20;
+ int t = a & 0x7fff8006;
+ short s = (short) t;
+ if (s != 0)
+ return 0;
+ if ((x & 0x0ff00000) != 0)
+ return 0;
+ return 1;
+}
+
+int
+main (void)
+{
+ int min = -0x7fffffff - 1;
+ if (f1 (min) != 0 || f1 (0) != 1 || f1 (0x00600000) != 0)
+ __builtin_abort ();
+ if (f2 (min) != 0 || f2 (0) != 1 || f2 (0x00600000) != 0)
+ __builtin_abort ();
+ if (f3 (min) != 1 || f3 (0) != 1 || f3 (0x00600000) != 0)
+ __builtin_abort ();
+ if (f4 (min) != 0 || f4 (0) != 1 || f4 (0x00600000) != 0)
+ __builtin_abort ();
+ return 0;
+}
base-commit: c0b52c7383e311d970984ae66941d6267de3199d
prerequisite-patch-id: e52d299db98b0892f974152cd9fa5f6859fec5b7
prerequisite-patch-id: 39da6ec3264bae454fa504f127cd5b80a0662f04
prerequisite-patch-id: 6d6c263a964425334ad8c2edd4dd89dcbff44bfe
prerequisite-patch-id: 9a12a1706cbdff1edf8959682cd3cf9d900cd8f1
prerequisite-patch-id: b24057443c46d6979436cce479836178b19355de
prerequisite-patch-id: d61bf2c4249ecd3d9da5eeda002d332ed3a8b4fd
prerequisite-patch-id: 59cbeca60f782f48f945bc532c2235a50de6f0e5
prerequisite-patch-id: 8eddd3172e2007d0fc591124a56b372dfe83e683
prerequisite-patch-id: 862842dd3742066d039f66ffe9528bc490308c50
prerequisite-patch-id: e413d39d9af45f25483eaf229cf883c771444fbb
prerequisite-patch-id: 2faef186ad1d816bb104d89638bc4cec6d863a46
prerequisite-patch-id: b32cceb111f4f57798e4b1d58822281e7bbaf25e
prerequisite-patch-id: 08ff1c06921c5e253d911128be3a8ebb76137d3c
prerequisite-patch-id: 72aa7ba47bd81909871a9e1a2e9de14f2cf42174
prerequisite-patch-id: 2ce10434bf0a7ab22e27ff177073e10b4b7d0d72
prerequisite-patch-id: e286b2e30887f3f43a3458c7c9b3f8c8b513a91b
prerequisite-patch-id: dc07b9957107f8b453a87ad64fd8477ae26e370c
prerequisite-patch-id: 146a9a34e1f46284793e3329e70d1d689efb4145
--
2.51.0