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 r13-1187] match.pd: Remove "+ 0x80000000" in int comparisons [PR94899]
Date: Tue, 21 Jun 2022 10:22:14 +0000 (GMT)	[thread overview]
Message-ID: <20220621102214.B42AD3857B8F@sourceware.org> (raw)

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

commit r13-1187-gab981aab92cbc71918fbaadcf6fa64bdb2b69be7
Author: Arjun Shankar <arjun@redhat.com>
Date:   Tue Jun 21 12:12:11 2022 +0200

    match.pd: Remove "+ 0x80000000" in int comparisons [PR94899]
    
    Expressions of the form "X + CST < Y + CST" where:
    
    * CST is an unsigned integer constant with only the MSB set, and
    * X and Y's types have integer conversion ranks <= CST's
    
    can be simplified to "(signed) X < (signed) Y".
    
    This is because, assuming a 32-bit signed numbers,
    (unsigned) INT_MIN + 0x80000000 is 0, and
    (unsigned) INT_MAX + 0x80000000 is UINT_MAX.
    
    i.e. the result increases monotonically with signed input.
    
    This means:
    ((signed) X < (signed) Y) iff (X + 0x80000000 < Y + 0x80000000)
    
    gcc/
            PR tree-optimization/94899
            * match.pd (X + C < Y + C -> (signed) X < (signed) Y, if C is
            0x80000000): New simplification.
    gcc/testsuite/
            * gcc.dg/pr94899.c: New test.

Diff:
---
 gcc/match.pd                   | 13 +++++++++++
 gcc/testsuite/gcc.dg/pr94899.c | 49 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/gcc/match.pd b/gcc/match.pd
index a63b649841b..4a570894b2e 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -2089,6 +2089,19 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
   (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))
        && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0)))
    (op @0 @1))))
+
+/* As a special case, X + C < Y + C is the same as (signed) X < (signed) Y
+   when C is an unsigned integer constant with only the MSB set, and X and
+   Y have types of equal or lower integer conversion rank than C's.  */
+(for op (lt le ge gt)
+ (simplify
+  (op (plus @1 INTEGER_CST@0) (plus @2 @0))
+  (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))
+       && TYPE_UNSIGNED (TREE_TYPE (@0))
+       && wi::only_sign_bit_p (wi::to_wide (@0)))
+   (with { tree stype = signed_type_for (TREE_TYPE (@0)); }
+    (op (convert:stype @1) (convert:stype @2))))))
+
 /* For equality and subtraction, this is also true with wrapping overflow.  */
 (for op (eq ne minus)
  (simplify
diff --git a/gcc/testsuite/gcc.dg/pr94899.c b/gcc/testsuite/gcc.dg/pr94899.c
new file mode 100644
index 00000000000..9fad057e4d2
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr94899.c
@@ -0,0 +1,49 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+typedef __INT16_TYPE__ int16_t;
+typedef __INT32_TYPE__ int32_t;
+typedef __UINT16_TYPE__ uint16_t;
+typedef __UINT32_TYPE__ uint32_t;
+
+#define MAGIC (~ (uint32_t) 0 / 2 + 1)
+
+int
+f_i16_i16 (int16_t x, int16_t y)
+{
+  return x + MAGIC < y + MAGIC;
+}
+
+int
+f_i16_i32 (int16_t x, int32_t y)
+{
+  return x + MAGIC < y + MAGIC;
+}
+
+int
+f_i32_i32 (int32_t x, int32_t y)
+{
+  return x + MAGIC < y + MAGIC;
+}
+
+int
+f_u32_i32 (uint32_t x, int32_t y)
+{
+  return x + MAGIC < y + MAGIC;
+}
+
+int
+f_u32_u32 (uint32_t x, uint32_t y)
+{
+  return x + MAGIC < y + MAGIC;
+}
+
+int
+f_i32_i32_sub (int32_t x, int32_t y)
+{
+  return x - MAGIC < y - MAGIC;
+}
+
+/* The addition/subtraction of constants should be optimized away.  */
+/* { dg-final { scan-tree-dump-not " \\+ " "optimized"} } */
+/* { dg-final { scan-tree-dump-not " \\- " "optimized"} } */


                 reply	other threads:[~2022-06-21 10:22 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=20220621102214.B42AD3857B8F@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).