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.
Fix this by noting, when the mask is found after a narrowing
conversion was dropped, that its upper bits are not sign extensions,
and leaving SIGNBIT unset in that case.
Bootstrap and regtest running on x86_64-unknown-linux-gnu.
PR tree-optimization/128597
* gimple-fold.cc (decode_field_reference): Don't set SIGNBIT for
upper bits of a mask applied before a narrowing conversion.
* gcc.dg/torture/pr128597.c: New testcase.
---
gcc/gimple-fold.cc | 12 ++++++-
gcc/testsuite/gcc.dg/torture/pr128597.c | 44 +++++++++++++++++++++++++
2 files changed, 55 insertions(+), 1 deletion(-)
create mode 100644 gcc/testsuite/gcc.dg/torture/pr128597.c
diff --git a/gcc/gimple-fold.cc b/gcc/gimple-fold.cc
index 71dd8f1da74..fc0f59c9596 100644
--- a/gcc/gimple-fold.cc
+++ b/gcc/gimple-fold.cc
@@ -7968,6 +7968,7 @@ decode_field_reference (tree *pexp, HOST_WIDE_INT
*pbitsize,
tree res_ops[2];
machine_mode mode;
bool convert_before_shift = false;
+ bool narrow_after_mask = false;
int signbit = 0;
bool xorp = false;
tree xor_cmp_op;
@@ -8002,6 +8003,13 @@ decode_field_reference (tree *pexp, HOST_WIDE_INT
*pbitsize,
{
loc[1] = gimple_location (SSA_NAME_DEF_STMT (exp));
exp = res_ops[0];
+ /* If we have already dropped a narrowing conversion, it applies to
+ the result of the masking, so mask bits above the precision of
+ OUTER_TYPE are discarded by the conversion, rather than testing
+ extensions of the sign bit. */
+ if (outer_type
+ && TYPE_PRECISION (outer_type) < TYPE_PRECISION (TREE_TYPE (exp)))
+ narrow_after_mask = true;
and_mask = wi::to_wide (res_ops[1]);
unsigned prec_in = pand_mask->get_precision ();
if (prec_in)
@@ -8165,8 +8173,10 @@ decode_field_reference (tree *pexp, HOST_WIDE_INT
*pbitsize,
{
/* If the AND_MASK encompasses bits that would be extensions of
the sign bit, set SIGNBIT to 1 if any such bit is set, or -1 if
- they are all clear. */
+ they are all clear. Upper bits of a mask applied before a narrowing
+ conversion are not extensions of the sign bit. */
if (!unsignedp
+ && !narrow_after_mask
&& and_mask.get_precision () > bs)
{
if ((and_mask & wi::mask (bs, true, and_mask.get_precision ())) != 0)
diff --git a/gcc/testsuite/gcc.dg/torture/pr128597.c
b/gcc/testsuite/gcc.dg/torture/pr128597.c
new file mode 100644
index 00000000000..80e136d82d1
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr128597.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;
+}
base-commit: 03ca1503bc64747a25e7257667495ea0f67b6649
--
2.51.0