===================================================================
@@ -2396,20 +2396,62 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
&& types_match (type, TREE_TYPE (@0)))
(non_lvalue @0)))
/* Do not handle
bool_var == 0 becomes !bool_var or
bool_var != 1 becomes !bool_var
here because that only is good in assignment context as long
as we require a tcc_comparison in GIMPLE_CONDs where we'd
replace if (x == 0) with tem = ~x; if (tem != 0) which is
clearly less optimal and which we'll transform again in forwprop. */
+/* To detect overflow in unsigned A - B, A < B is simpler than A - B > A.
+ However, the detection logic for SUB_OVERFLOW in tree-ssa-math-opts.c
+ expects the long form, so we restrict the transformation for now. */
+(for cmp (gt le)
+ (simplify
+ (cmp (minus@2 @0 @1) @0)
+ (if (single_use (@2)
+ && TYPE_UNSIGNED (TREE_TYPE (@0))
+ && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
+ (cmp @1 @0))))
+(for cmp (lt ge)
+ (simplify
+ (cmp @0 (minus@2 @0 @1))
+ (if (single_use (@2)
+ && TYPE_UNSIGNED (TREE_TYPE (@0))
+ && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@0)))
+ (cmp @0 @1))))
+/* Testing for overflow is unnecessary if we already know the result. */
+(for cmp (lt ge)
+ out (ne eq)
+ (simplify
+ (cmp @0 (realpart (IFN_SUB_OVERFLOW@2 @0 @1)))
+ (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
+(for cmp (gt le)
+ out (ne eq)
+ (simplify
+ (cmp (realpart (IFN_SUB_OVERFLOW@2 @0 @1)) @0)
+ (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
+(for cmp (lt ge)
+ out (ne eq)
+ (simplify
+ (cmp (realpart (IFN_ADD_OVERFLOW@2 @0 @1)) @0)
+ (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
+(for cmp (gt le)
+ out (ne eq)
+ (simplify
+ (cmp @0 (realpart (IFN_ADD_OVERFLOW@2 @0 @1)))
+ (if (TYPE_UNSIGNED (TREE_TYPE (@0)))
+ (out (imagpart @2) { build_zero_cst (TREE_TYPE (@0)); }))))
/* Simplification of math builtins. These rules must all be optimizations
as well as IL simplifications. If there is a possibility that the new
form could be a pessimization, the rule should go in the canonicalization
section that follows this one.
Rules can generally go in this section if they satisfy one of
the following:
- the rule describes an identity
===================================================================
@@ -0,0 +1,24 @@
+/* { dg-do compile } */
+/* { dg-options "-O -fdump-tree-optimized" } */
+
+int f(unsigned a, unsigned b) {
+ unsigned remove = a - b;
+ return remove > a;
+}
+
+int g(unsigned a, unsigned b) {
+ unsigned remove = a - b;
+ return remove <= a;
+}
+
+int h(unsigned a, unsigned b) {
+ unsigned remove = a - b;
+ return a < remove;
+}
+
+int i(unsigned a, unsigned b) {
+ unsigned remove = a - b;
+ return a >= remove;
+}
+
+/* { dg-final { scan-tree-dump-not "remove" "optimized" } } */
===================================================================
@@ -0,0 +1,48 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized-raw" } */
+
+int carry;
+int f(unsigned a, unsigned b) {
+ unsigned r;
+ carry = __builtin_sub_overflow(a, b, &r);
+ return r > a;
+}
+int g(unsigned a, unsigned b) {
+ unsigned r;
+ carry = __builtin_sub_overflow(a, b, &r);
+ return a < r;
+}
+int h(unsigned a, unsigned b) {
+ unsigned r;
+ carry = __builtin_sub_overflow(a, b, &r);
+ return r <= a;
+}
+int i(unsigned a, unsigned b) {
+ unsigned r;
+ carry = __builtin_sub_overflow(a, b, &r);
+ return a >= r;
+}
+int j(unsigned a, unsigned b) {
+ unsigned r;
+ carry = __builtin_add_overflow(a, b, &r);
+ return r < a;
+}
+int k(unsigned a, unsigned b) {
+ unsigned r;
+ carry = __builtin_add_overflow(a, b, &r);
+ return a > r;
+}
+int l(unsigned a, unsigned b) {
+ unsigned r;
+ carry = __builtin_add_overflow(a, b, &r);
+ return r >= a;
+}
+int m(unsigned a, unsigned b) {
+ unsigned r;
+ carry = __builtin_add_overflow(a, b, &r);
+ return a <= r;
+}
+
+/* { dg-final { scan-tree-dump-not "(le|lt|ge|gt)_expr" "optimized" } } */
+/* { dg-final { scan-tree-dump-times "ADD_OVERFLOW" 4 "optimized" } } */
+/* { dg-final { scan-tree-dump-times "SUB_OVERFLOW" 4 "optimized" } } */