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.

That makes SIGNBIT -1 in cases such as (short) ((x >> 20) & 0xffff0806)
>= 0, where it used to be 1 and the combination was rejected.  This
exposes a latent problem, also present without narrowing, as in
((x >> 20) & 0x0806) >= 0: when turning a sign test of a signed field
into a bit test, fold_truth_andor_for_ifcombine tested the sign bit of
the field, bit 11 here, even though the mask clears all the extension
bits, so that the masked value can't be negative.  Treat such fields
like zero-extended ones, as we already do when checking constants.

The same holds when comparing two fields: a signed field whose mask
clears all the extension bits is effectively zero-extended, so it
can't be compared with a sign-extended field, as in

  (short) ((x >> 20) & 0xffff0fff) == (short) (y >> 20)

or, without narrowing, ((x >> 20) & 0xfff) == (y >> 20), by comparing
the 12-bit fields only, because that drops the requirement that bit 11
of y be clear.  The latter was already miscompiled, the former became
exposed with the mask truncation.  Compare whether the fields are
zero-extended, rather than just their signedness, when combining
compares of non-constant operands.

Bootstrap and regtest running on x86_64-unknown-linux-gnu.

        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.
        (fold_truth_andor_for_ifcombine): Don't test the sign bit of a
        field whose extension bits are all masked out in sign tests.
        Reject combining compares of a zero-extended field with a
        sign-extended one, considering fields whose extension bits are
        masked out as zero-extended.

        * gcc.dg/torture/pr128597-1.c: New testcase.
        * gcc.dg/torture/pr128597-2.c: New testcase.
        * gcc.dg/pr128597-3.c: New testcase.
        * gcc.dg/pr128597-4.c: New testcase.
        * gcc.dg/pr128597-5.c: New testcase.
---
 gcc/gimple-fold.cc                        |  30 ++-
 gcc/testsuite/gcc.dg/pr128597-3.c         | 160 +++++++++++++
 gcc/testsuite/gcc.dg/pr128597-4.c         | 197 ++++++++++++++++
 gcc/testsuite/gcc.dg/pr128597-5.c         | 271 ++++++++++++++++++++++
 gcc/testsuite/gcc.dg/torture/pr128597-1.c |  44 ++++
 gcc/testsuite/gcc.dg/torture/pr128597-2.c |  81 +++++++
 6 files changed, 778 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/pr128597-3.c
 create mode 100644 gcc/testsuite/gcc.dg/pr128597-4.c
 create mode 100644 gcc/testsuite/gcc.dg/pr128597-5.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..9bc88da352b 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;
@@ -8729,8 +8739,10 @@ fold_truth_andor_for_ifcombine (enum tree_code code, 
tree truth_type,
         us to mask the entire field out, yielding zero, i.e., the sign bit of
         the zero-extended value.  We know the masked value is being compared
         with zero, so the compare will get us the result we're looking
-        for: TRUE if EQ_EXPR, FALSE if NE_EXPR.  */
-      if (lsignbit > ll_bitsize && ll_unsignedp)
+        for: TRUE if EQ_EXPR, FALSE if NE_EXPR.  Likewise if ll_arg is
+        masked so that all the extension bits are clear (ll_signbit < 0):
+        it can't be negative then, even if the field is signed.  */
+      if (lsignbit > ll_bitsize && (ll_unsignedp || ll_signbit < 0))
        sign <<= 1;
       if (!ll_and_mask.get_precision ())
        ll_and_mask = sign;
@@ -8753,7 +8765,7 @@ fold_truth_andor_for_ifcombine (enum tree_code code, tree 
truth_type,
   if (rsignbit)
     {
       wide_int sign = wi::mask (rl_bitsize - 1, true, rl_bitsize);
-      if (rsignbit > rl_bitsize && rl_unsignedp)
+      if (rsignbit > rl_bitsize && (rl_unsignedp || rl_signbit < 0))
        sign <<= 1;
       if (!rl_and_mask.get_precision ())
        rl_and_mask = sign;
@@ -9092,11 +9104,19 @@ fold_truth_andor_for_ifcombine (enum tree_code code, 
tree truth_type,
 
   /* Likewise, if the right sides are not constant, align them for the combined
      compare.  Also, disallow this optimization if a size, signedness or
-     storage order mismatch occurs between the left and right sides.  */
+     storage order mismatch occurs between the left and right sides.
+     A signed field whose mask clears all the extension bits
+     (signbit < 0) is effectively zero-extended, so it must not be
+     compared with a sign-extended field: clipping both to the field
+     width would drop the test that the sign bit of the latter is
+     clear.  */
   else
     {
       if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
-         || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
+         || ((ll_unsignedp || ll_signbit < 0)
+             != (lr_unsignedp || lr_signbit < 0))
+         || ((rl_unsignedp || rl_signbit < 0)
+             != (rr_unsignedp || rr_signbit < 0))
          || ll_reversep != lr_reversep
          /* Make sure the two fields on the right
             correspond to the left without being swapped.  */
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/pr128597-4.c 
b/gcc/testsuite/gcc.dg/pr128597-4.c
new file mode 100644
index 00000000000..06399cec9c2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr128597-4.c
@@ -0,0 +1,197 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int32 } */
+/* { dg-options "-O2 -fgimple -fdump-tree-ifcombine-details" } */
+
+/* Check that ifcombine turns sign tests of masked fields whose
+   extension bits are all masked out into tests that are always true or
+   always false, rather than into tests of the sign bit of the field.
+   Starting at ifcombine keeps earlier passes from simplifying the
+   compares.  */
+
+/* S is (short) (((X >> 20) & 0x0806) | (other bits discarded by the
+   conversion)): bits 12 to 15 are clear, so S >= 0 always holds, even
+   though bit 11, a copy of the sign bit of X, may be set.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f1 (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) -63482;
+  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;
+}
+
+/* Likewise without the narrowing conversion.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f2 (int x)
+{
+  int a;
+  int t;
+  int u;
+  int r;
+
+  __BB(2):
+  a_2 = x_1(D) >> 20;
+  t_3 = a_2 & 2054;
+  if (t_3 >= 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;
+}
+
+/* Like f1, with the sign test as the second compare.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f3 (int x)
+{
+  int a;
+  int t;
+  short s;
+  int u;
+  int r;
+
+  __BB(2):
+  u_5 = x_1(D) & 267386880;
+  if (u_5 == 0)
+    goto __BB3;
+  else
+    goto __BB4;
+
+  __BB(3):
+  a_2 = x_1(D) >> 20;
+  t_3 = a_2 & _Literal (int) -63482;
+  s_4 = (short) t_3;
+  if (s_4 >= _Literal (short) 0)
+    goto __BB5;
+  else
+    goto __BB4;
+
+  __BB(4):
+  goto __BB5;
+
+  __BB(5):
+  r_6 = __PHI (__BB3: 1, __BB4: 0);
+  return r_6;
+}
+
+/* S < 0 never holds, so S < 0 || U != 0 is U != 0.  */
+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) -63482;
+  s_4 = (short) t_3;
+  if (s_4 < _Literal (short) 0)
+    goto __BB4;
+  else
+    goto __BB3;
+
+  __BB(3):
+  u_5 = x_1(D) & 267386880;
+  if (u_5 != 0)
+    goto __BB4;
+  else
+    goto __BB5;
+
+  __BB(4):
+  goto __BB5;
+
+  __BB(5):
+  r_6 = __PHI (__BB4: 1, __BB3: 0);
+  return r_6;
+}
+
+/* Without a mask, S >= 0 does test bit 11 of the field, i.e., the sign
+   bit of X.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f5 (int x)
+{
+  int a;
+  short s;
+  int u;
+  int r;
+
+  __BB(2):
+  a_2 = x_1(D) >> 20;
+  s_4 = (short) a_2;
+  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 (min) != 1 || f1 (-1) != 0 || f1 (0) != 1 || f1 (0x00600000) != 0)
+    __builtin_abort ();
+  if (f2 (min) != 1 || f2 (-1) != 0 || f2 (0) != 1 || f2 (0x00600000) != 0)
+    __builtin_abort ();
+  if (f3 (min) != 1 || f3 (-1) != 0 || f3 (0) != 1 || f3 (0x00600000) != 0)
+    __builtin_abort ();
+  if (f4 (min) != 0 || f4 (-1) != 1 || f4 (0) != 0 || f4 (0x00600000) != 1)
+    __builtin_abort ();
+  if (f5 (min) != 0 || f5 (-1) != 0 || f5 (0) != 1 || f5 (0x00600000) != 0)
+    __builtin_abort ();
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "optimizing two comparisons" 5 
"ifcombine" } } */
diff --git a/gcc/testsuite/gcc.dg/pr128597-5.c 
b/gcc/testsuite/gcc.dg/pr128597-5.c
new file mode 100644
index 00000000000..df1caed8a69
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr128597-5.c
@@ -0,0 +1,271 @@
+/* { dg-do run } */
+/* { dg-require-effective-target int32 } */
+/* { dg-options "-O2 -fgimple -fdump-tree-ifcombine-details" } */
+
+/* Check that ifcombine doesn't merge a compare of a signed field whose
+   mask clears all the extension bits, so that it's effectively
+   zero-extended, with a sign-extended field of the same width.
+   Starting at ifcombine keeps earlier passes from simplifying the
+   compares.  */
+
+/* S is (short) ((X >> 20) & 0xffff0fff): bits 12 to 15 are clear, whereas
+   they are copies of bit 11 in C.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f1 (int x, int y)
+{
+  int a;
+  int t;
+  short s;
+  int b;
+  short c;
+  int u;
+  int v;
+  int r;
+
+  __BB(2):
+  a_3 = x_1(D) >> 20;
+  t_4 = a_3 & _Literal (int) -61441;
+  s_5 = (short) t_4;
+  b_6 = y_2(D) >> 20;
+  c_7 = (short) b_6;
+  if (s_5 == c_7)
+    goto __BB3;
+  else
+    goto __BB4;
+
+  __BB(3):
+  u_10 = x_1(D) & 255;
+  v_11 = y_2(D) & 255;
+  if (u_10 == v_11)
+    goto __BB5;
+  else
+    goto __BB4;
+
+  __BB(4):
+  goto __BB5;
+
+  __BB(5):
+  r_20 = __PHI (__BB3: 1, __BB4: 0);
+  return r_20;
+}
+
+/* Likewise without the narrowing conversion.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f2 (int x, int y)
+{
+  int a;
+  int t;
+  int b;
+  int u;
+  int v;
+  int r;
+
+  __BB(2):
+  a_3 = x_1(D) >> 20;
+  t_4 = a_3 & 4095;
+  b_6 = y_2(D) >> 20;
+  if (t_4 == b_6)
+    goto __BB3;
+  else
+    goto __BB4;
+
+  __BB(3):
+  u_10 = x_1(D) & 255;
+  v_11 = y_2(D) & 255;
+  if (u_10 == v_11)
+    goto __BB5;
+  else
+    goto __BB4;
+
+  __BB(4):
+  goto __BB5;
+
+  __BB(5):
+  r_20 = __PHI (__BB3: 1, __BB4: 0);
+  return r_20;
+}
+
+/* Like f2, with the masked compare second.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f3 (int x, int y)
+{
+  int a;
+  int t;
+  int b;
+  int u;
+  int v;
+  int r;
+
+  __BB(2):
+  u_10 = x_1(D) & 255;
+  v_11 = y_2(D) & 255;
+  if (u_10 == v_11)
+    goto __BB3;
+  else
+    goto __BB4;
+
+  __BB(3):
+  a_3 = x_1(D) >> 20;
+  t_4 = a_3 & 4095;
+  b_6 = y_2(D) >> 20;
+  if (t_4 == b_6)
+    goto __BB5;
+  else
+    goto __BB4;
+
+  __BB(4):
+  goto __BB5;
+
+  __BB(5):
+  r_20 = __PHI (__BB3: 1, __BB4: 0);
+  return r_20;
+}
+
+/* Like f2, with the masked field on the right-hand side.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f4 (int x, int y)
+{
+  int a;
+  int t;
+  int b;
+  int u;
+  int v;
+  int r;
+
+  __BB(2):
+  a_3 = x_1(D) >> 20;
+  t_4 = a_3 & 4095;
+  b_6 = y_2(D) >> 20;
+  if (b_6 == t_4)
+    goto __BB3;
+  else
+    goto __BB4;
+
+  __BB(3):
+  u_10 = x_1(D) & 255;
+  v_11 = y_2(D) & 255;
+  if (u_10 == v_11)
+    goto __BB5;
+  else
+    goto __BB4;
+
+  __BB(4):
+  goto __BB5;
+
+  __BB(5):
+  r_20 = __PHI (__BB3: 1, __BB4: 0);
+  return r_20;
+}
+
+/* Both fields are masked so that they are zero-extended: this can be
+   combined.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f5 (int x, int y)
+{
+  int a;
+  int t;
+  int b;
+  int w;
+  int u;
+  int v;
+  int r;
+
+  __BB(2):
+  a_3 = x_1(D) >> 20;
+  t_4 = a_3 & 4095;
+  b_6 = y_2(D) >> 20;
+  w_7 = b_6 & 4095;
+  if (t_4 == w_7)
+    goto __BB3;
+  else
+    goto __BB4;
+
+  __BB(3):
+  u_10 = x_1(D) & 255;
+  v_11 = y_2(D) & 255;
+  if (u_10 == v_11)
+    goto __BB5;
+  else
+    goto __BB4;
+
+  __BB(4):
+  goto __BB5;
+
+  __BB(5):
+  r_20 = __PHI (__BB3: 1, __BB4: 0);
+  return r_20;
+}
+
+/* The masked signed field is compared with an unsigned field, both are
+   zero-extended: this can be combined.  */
+int __GIMPLE (ssa,startwith("ifcombine")) __attribute__((noipa))
+f6 (int x, int y)
+{
+  int a;
+  int t;
+  unsigned int c;
+  unsigned int d;
+  int e;
+  int w;
+  int u;
+  int v;
+  int r;
+
+  __BB(2):
+  a_3 = x_1(D) >> 20;
+  t_4 = a_3 & 4095;
+  c_5 = (unsigned int) y_2(D);
+  d_6 = c_5 >> 20;
+  e_7 = (int) d_6;
+  if (t_4 == e_7)
+    goto __BB3;
+  else
+    goto __BB4;
+
+  __BB(3):
+  u_10 = x_1(D) & 255;
+  w_8 = (int) c_5;
+  v_11 = w_8 & 255;
+  if (u_10 == v_11)
+    goto __BB5;
+  else
+    goto __BB4;
+
+  __BB(4):
+  goto __BB5;
+
+  __BB(5):
+  r_20 = __PHI (__BB3: 1, __BB4: 0);
+  return r_20;
+}
+
+#define M1 (-1)
+#define X1 ((int) 0x7ff000ff)
+#define X2 0x7fffffff
+#define X3 ((int) 0xfff000ff)
+
+int
+main ()
+{
+  if (f1 (M1, M1) != 0 || f1 (X1, X1) != 1 || f1 (M1, X2) != 0
+      || f1 (X3, X3) != 0)
+    __builtin_abort ();
+  if (f2 (M1, M1) != 0 || f2 (X1, X1) != 1 || f2 (M1, X2) != 0
+      || f2 (X3, X3) != 0)
+    __builtin_abort ();
+  if (f3 (M1, M1) != 0 || f3 (X1, X1) != 1 || f3 (M1, X2) != 0
+      || f3 (X3, X3) != 0)
+    __builtin_abort ();
+  if (f4 (M1, M1) != 0 || f4 (X1, X1) != 1 || f4 (X2, M1) != 0
+      || f4 (X3, X3) != 0)
+    __builtin_abort ();
+  if (f5 (M1, M1) != 1 || f5 (X1, X1) != 1 || f5 (M1, X2) != 0
+      || f5 (X3, X3) != 1 || f5 (X1, X3) != 0)
+    __builtin_abort ();
+  if (f6 (M1, M1) != 1 || f6 (X1, X1) != 1 || f6 (X1, X3) != 0
+      || f6 (X3, X3) != 1 || f6 (M1, X3) != 1 || f6 (M1, X2) != 0)
+    __builtin_abort ();
+  return 0;
+}
+
+/* { dg-final { scan-tree-dump-times "optimizing two comparisons" 2 
"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: 2a6eaf4a4676583f7e03f3e92aa16dd49ef65837
prerequisite-patch-id: b3759adf5ade37e49f082989b92c6250651f9e01
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

Reply via email to