]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
range-op: Keep nonzero mask up to date with truncating casts.
authorAldy Hernandez <aldyh@redhat.com>
Wed, 5 Oct 2022 11:24:49 +0000 (13:24 +0200)
committerAldy Hernandez <aldyh@redhat.com>
Wed, 5 Oct 2022 13:49:01 +0000 (15:49 +0200)
gcc/ChangeLog:

* range-op.cc (operator_cast::fold_range): Handle truncating casts
for nonzero masks.

gcc/range-op.cc

index 6fa27a8904e8f8be1617c3dd83c2c80bf94fbb3c..df0735cb42a837577859ef322ac8920c539e0d5a 100644 (file)
@@ -2516,13 +2516,17 @@ operator_cast::fold_range (irange &r, tree type ATTRIBUTE_UNUSED,
        return true;
     }
 
-  // Pass nonzero mask through the cast.
-  if (!truncating_cast_p (inner, outer))
-    {
-      wide_int nz = inner.get_nonzero_bits ();
-      nz = wide_int::from (nz, TYPE_PRECISION (type), TYPE_SIGN (inner.type ()));
-      r.set_nonzero_bits (nz);
-    }
+  // Update the nonzero mask.  Truncating casts are problematic unless
+  // the conversion fits in the resulting outer type.
+  wide_int nz = inner.get_nonzero_bits ();
+  if (truncating_cast_p (inner, outer)
+      && wi::rshift (nz, wi::uhwi (TYPE_PRECISION (outer.type ()),
+                                  TYPE_PRECISION (inner.type ())),
+                    TYPE_SIGN (inner.type ())) != 0)
+    return true;
+  nz = wide_int::from (nz, TYPE_PRECISION (type), TYPE_SIGN (inner.type ()));
+  r.set_nonzero_bits (nz);
+
   return true;
 }