(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
--- /dev/null
+/* { 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"} } */