public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Aldy Hernandez <aldyh@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-3090] range-op: Keep nonzero mask up to date with truncating casts.
Date: Wed,  5 Oct 2022 13:49:45 +0000 (GMT)	[thread overview]
Message-ID: <20221005134945.D7ED4385801A@sourceware.org> (raw)

https://gcc.gnu.org/g:df4c584c567263fdcd57d8376f24f29477a892b2

commit r13-3090-gdf4c584c567263fdcd57d8376f24f29477a892b2
Author: Aldy Hernandez <aldyh@redhat.com>
Date:   Wed Oct 5 13:24:49 2022 +0200

    range-op: Keep nonzero mask up to date with truncating casts.
    
    gcc/ChangeLog:
    
            * range-op.cc (operator_cast::fold_range): Handle truncating casts
            for nonzero masks.

Diff:
---
 gcc/range-op.cc | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/gcc/range-op.cc b/gcc/range-op.cc
index 6fa27a8904e..df0735cb42a 100644
--- a/gcc/range-op.cc
+++ b/gcc/range-op.cc
@@ -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;
 }

                 reply	other threads:[~2022-10-05 13:49 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20221005134945.D7ED4385801A@sourceware.org \
    --to=aldyh@gcc.gnu.org \
    --cc=gcc-cvs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).