public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-optimization/94899: Remove "+ 0x80000000" in int comparisons
@ 2022-02-01  4:53 Arjun Shankar
  2022-02-01  7:18 ` Richard Biener
  0 siblings, 1 reply; 9+ messages in thread
From: Arjun Shankar @ 2022-02-01  4:53 UTC (permalink / raw)
  To: gcc-patches

[-- Attachment #1: Type: text/plain, Size: 573 bytes --]

Expressions of the form "X + CST < Y + CST" where X and Y are of int
type and CST is of unsigned type with only the MSB on can be simplified
to "X < Y" because "X + 0x80000000" increases monotonically with X.

gcc/
        * match.pd (X + C < Y + C -> X < Y, if C is 0x80000000): New
        simplification.
gcc/testsuite/
        * gcc.dg/pr94899.c: New test.
---
 gcc/match.pd                   | 18 ++++++++++++++++++
 gcc/testsuite/gcc.dg/pr94899.c | 28 ++++++++++++++++++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr94899.c

[-- Attachment #2: pr94899.patch --]
[-- Type: text/x-patch, Size: 1957 bytes --]

diff --git a/gcc/match.pd b/gcc/match.pd
index b942cb2930a..49ac0c43f83 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -1975,6 +1975,24 @@ 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 X < Y even with wrapping
+   overflow if X and Y are signed integers of the same size, and C is an
+   unsigned constant with all bits except MSB set to 0 and size >= that of
+   X/Y.  */
+(for op (lt le ge gt)
+ (simplify
+  (op (plus:c (convert@0 @1) @4) (plus:c (convert@2 @3) @4))
+  (if (CONSTANT_CLASS_P (@4)
+       && TYPE_UNSIGNED (TREE_TYPE (@4))
+       && !TYPE_UNSIGNED (TREE_TYPE (@1))
+       && !TYPE_UNSIGNED (TREE_TYPE (@3))
+       && (TYPE_PRECISION (TREE_TYPE (@1)) == TYPE_PRECISION (TREE_TYPE (@3)))
+       && (TYPE_PRECISION (TREE_TYPE (@1)) <= TYPE_PRECISION (TREE_TYPE (@4)))
+       && wi::only_sign_bit_p (wi::to_wide (@4),
+                               TYPE_PRECISION (TREE_TYPE (@0))))
+   (op @1 @3))))
+
 /* 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..304aaf3c6e6
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr94899.c
@@ -0,0 +1,28 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-original" } */
+
+typedef __INT16_TYPE__ int16_t;
+typedef __INT32_TYPE__ int32_t;
+
+#define MAGIC 0x80000000
+
+int
+f_i16_i16 (int16_t x, int16_t y)
+{
+  return x + MAGIC < y + MAGIC;
+}
+
+int
+f_i32_i32 (int32_t x, int32_t y)
+{
+  return x + MAGIC < y + MAGIC;
+}
+
+int
+f_i32_i32_sub (int32_t x, int32_t y)
+{
+  return x - MAGIC < y - MAGIC;
+}
+
+/* The constants above should have been optimized away.  */
+/* { dg-final { scan-tree-dump-times "2147483648" 0 "original"} } */
-- 
2.31.1


^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-02-04 13:21 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-01  4:53 [PATCH] tree-optimization/94899: Remove "+ 0x80000000" in int comparisons Arjun Shankar
2022-02-01  7:18 ` Richard Biener
2022-02-01 15:21   ` Arjun Shankar
2022-02-02  9:20     ` Richard Biener
2022-02-02 15:55       ` Arjun Shankar
2022-02-03  3:50       ` [PATCH v2] " Arjun Shankar
2022-02-04 11:14         ` Richard Biener
2022-02-04 11:50           ` Jakub Jelinek
2022-02-04 13:21             ` Richard Biener

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).