public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-9175] match.pd: Guard 2 simplifications on integral TYPE_OVERFLOW_UNDEFINED [PR114090]
Date: Mon, 26 Feb 2024 09:09:37 +0000 (GMT)	[thread overview]
Message-ID: <20240226090937.1E4173858C98@sourceware.org> (raw)

https://gcc.gnu.org/g:24aa051af7c59f37ec45aea754b48b97d210ea6d

commit r14-9175-g24aa051af7c59f37ec45aea754b48b97d210ea6d
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Feb 26 10:08:45 2024 +0100

    match.pd: Guard 2 simplifications on integral TYPE_OVERFLOW_UNDEFINED [PR114090]
    
    These 2 patterns are incorrect on floating point, or for -fwrapv, or
    for -ftrapv, or the first one for unsigned types (the second one is
    mathematically correct, but we ought to just fold that to 0 instead).
    
    So, the following patch properly guards this.
    
    I think we don't need && !TYPE_OVERFLOW_SANITIZED (type) because
    in both simplifications there would be UB before and after on
    signed integer minimum.
    
    2024-02-26  Jakub Jelinek  <jakub@redhat.com>
    
            PR tree-optimization/114090
            * match.pd ((x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x):
            Restrict pattern to ANY_INTEGRAL_TYPE_P and TYPE_OVERFLOW_UNDEFINED
            types.
            ((x <= 0 ? -x : 0) -> max(-x, 0)): Likewise.
    
            * gcc.dg/pr114090.c: New test.

Diff:
---
 gcc/match.pd                    | 10 ++++++----
 gcc/testsuite/gcc.dg/pr114090.c | 38 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 4 deletions(-)

diff --git a/gcc/match.pd b/gcc/match.pd
index 67007fc2017f..f3fffd8dec2b 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -453,8 +453,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
 /* (x >= 0 ? x : 0) + (x <= 0 ? -x : 0) -> abs x.  */
 (simplify
-  (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
-  (abs @0))
+ (plus:c (max @0 integer_zerop) (max (negate @0) integer_zerop))
+ (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
+  (abs @0)))
 
 /* X * 1, X / 1 -> X.  */
 (for op (mult trunc_div ceil_div floor_div round_div exact_div)
@@ -4218,8 +4219,9 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
 
 /* (x <= 0 ? -x : 0) -> max(-x, 0).  */
 (simplify
-  (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
-  (max @2 @1))
+ (cond (le @0 integer_zerop@1) (negate@2 @0) integer_zerop@1)
+ (if (ANY_INTEGRAL_TYPE_P (type) && TYPE_OVERFLOW_UNDEFINED (type))
+  (max @2 @1)))
 
 /* (zero_one == 0) ? y : z <op> y -> ((typeof(y))zero_one * z) <op> y */
 (for op (bit_xor bit_ior plus)
diff --git a/gcc/testsuite/gcc.dg/pr114090.c b/gcc/testsuite/gcc.dg/pr114090.c
new file mode 100644
index 000000000000..dcc2f8bb3721
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr114090.c
@@ -0,0 +1,38 @@
+/* PR tree-optimization/114090 */
+/* { dg-do run } */
+/* { dg-options "-O2 -fwrapv" } */
+
+__attribute__((noipa)) int
+foo (int x)
+{
+  int w = (x >= 0 ? x : 0);
+  int y = -x;
+  int z = (y >= 0 ? y : 0);
+  return w + z;
+}
+
+__attribute__((noipa)) int
+bar (int x)
+{
+  int w = (x >= 0 ? x : 0);
+  int z = (x <= 0 ? -x : 0);
+  return w + z;
+}
+
+__attribute__((noipa)) int
+baz (int x)
+{
+  return x <= 0 ? -x : 0;
+}
+
+int
+main ()
+{
+  int v = -__INT_MAX__ - 1;
+  if (foo (v) != 0)
+    __builtin_abort ();
+  if (bar (v) != v)
+    __builtin_abort ();
+  if (baz (v) != v)
+    __builtin_abort ();
+}

                 reply	other threads:[~2024-02-26  9:09 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=20240226090937.1E4173858C98@sourceware.org \
    --to=jakub@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).