https://gcc.gnu.org/g:6ed5ed56da3d7450bd55334771930125c1131881
commit r17-699-g6ed5ed56da3d7450bd55334771930125c1131881 Author: Daniel Barboza <[email protected]> Date: Sat Jan 17 16:01:53 2026 -0300 fold-const.cc: overhaul tree_expr_nonnegative_(warn)_p We want to eliminate another source of Wstrict-overflow warnings from tree_expr_nonnegative_warnv_p, via fold_overflow_warn(). At this point strict_overflow_p is marked as ATTRIBUTE_UNUSED and we can remove it. By removing it, the function would lost the "warnv" and be renamed to tree_expr_nonnegative_p. This matches an existing function, that has a different API. In a closer inspection we verify that tree_expr_nonnegative_p is a wrapper for tree_expr_nonnegative_warn_p, that throws a warning and leaves depth = 0 by default. This same behavior can be achieved by removing strict_overflow_p from tree_expr_nonnegative_warnv_p, so we'll go ahead with its rename and the removal of the pre-existing tree_expr_nonnegative_p. The additional strict_overflow_p local variables we've been adding to satisfy the RECURSE() macro are all eliminated. gcc/c-family/ChangeLog: * c-warn.cc (warn_for_sign_compare): Remove strict_overflow_p argument from tree_expr_nonnegative_p call. gcc/c/ChangeLog: * c-typeck.cc (build_conditional_expr): Likewise. gcc/ChangeLog: * fold-const.cc (fold_binary_loc): Likewise. (tree_expr_nonnegative_warnv_p): Renamed to tree_expr_nonnegative_p. Argument strict_overflow_p removed. (tree_expr_nonnegative_p): Removed. (RECURSE): Removed strict_overflow_p argument. (tree_unary_nonnegative_p): Local strict_overflow_p variable added to support the RECURSE() macro was removed. (tree_binary_nonnegative_p): Likewise. (tree_single_nonnegative_p): Likewise. (tree_call_nonnegative_p): Likewise. (tree_invalid_nonnegative_p): Likewise. (tree_binary_nonzero_p): Removed sub_strict_overflow_p variable from tree_expr_nonnegative_p call. * fold-const.h (tree_expr_nonnegative_p): Removed. (tree_expr_nonnegative_warnv_p): Renamed to tree_expr_nonnegative_p, removed strict_overflow_p argument. * tree-ssa-loop-manip.cc (create_iv): Removed ovf variable from tree_expr_nonnegative_p call. Diff: --- gcc/c-family/c-warn.cc | 3 +- gcc/c/c-typeck.cc | 6 +-- gcc/fold-const.cc | 108 +++++++-------------------------------------- gcc/fold-const.h | 3 +- gcc/tree-ssa-loop-manip.cc | 4 +- 5 files changed, 22 insertions(+), 102 deletions(-) diff --git a/gcc/c-family/c-warn.cc b/gcc/c-family/c-warn.cc index e956fae51caf..07d15c0b09c7 100644 --- a/gcc/c-family/c-warn.cc +++ b/gcc/c-family/c-warn.cc @@ -2377,7 +2377,6 @@ warn_for_sign_compare (location_t location, else { tree sop, uop, base_type; - bool ovf; if (op0_signed) sop = orig_op0, uop = orig_op1; @@ -2396,7 +2395,7 @@ warn_for_sign_compare (location_t location, literal (or some static constant expression involving such literals or a conditional expression involving such literals) and it is non-negative. */ - if (tree_expr_nonnegative_warnv_p (sop, &ovf)) + if (tree_expr_nonnegative_p (sop)) /* OK */; /* Do not warn if the comparison is an equality operation, the unsigned quantity is an integral constant, and it would fit diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc index 0c702f1a7e6e..6bf404ac9857 100644 --- a/gcc/c/c-typeck.cc +++ b/gcc/c/c-typeck.cc @@ -6811,8 +6811,6 @@ build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, if (unsigned_op1 ^ unsigned_op2) { - bool ovf; - /* Do not warn if the result type is signed, since the signed type will only be chosen if it can represent all the values of the unsigned type. */ @@ -6848,9 +6846,9 @@ build_conditional_expr (location_t colon_loc, tree ifexp, bool ifexp_bcp, if (warn_sign_compare) { if ((unsigned_op2 - && tree_expr_nonnegative_warnv_p (op1, &ovf)) + && tree_expr_nonnegative_p (op1)) || (unsigned_op1 - && tree_expr_nonnegative_warnv_p (op2, &ovf))) + && tree_expr_nonnegative_p (op2))) /* OK */; else if (unsigned_op2) warning_at (op1_loc, OPT_Wsign_compare, diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 5ac75a648846..36d1e02f0188 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -10909,7 +10909,6 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, enum tree_code_class kind = TREE_CODE_CLASS (code); tree arg0, arg1, tem; tree t1 = NULL_TREE; - bool strict_overflow_p; unsigned int prec; gcc_assert (IS_EXPR_CODE_CLASS (kind) @@ -11922,10 +11921,9 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, case FLOOR_DIV_EXPR: /* Simplify A / (B << N) where A and B are positive and B is a power of 2, to A >> (N + log2(B)). */ - strict_overflow_p = false; if (TREE_CODE (arg1) == LSHIFT_EXPR && (TYPE_UNSIGNED (type) - || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p))) + || tree_expr_nonnegative_p (op0))) { tree sval = TREE_OPERAND (arg1, 0); if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0) @@ -11934,11 +11932,6 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, tree pow2 = build_int_cst (TREE_TYPE (sh_cnt), wi::exact_log2 (wi::to_wide (sval))); - if (strict_overflow_p) - fold_overflow_warning (("assuming signed overflow does not " - "occur when simplifying A / (B << N)"), - WARN_STRICT_OVERFLOW_MISC); - sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt), sh_cnt, pow2); return fold_build2_loc (loc, RSHIFT_EXPR, type, @@ -12521,38 +12514,22 @@ fold_binary_loc (location_t loc, enum tree_code code, tree type, TREE_OPERAND (arg0, 0), arg1)); /* Convert ABS_EXPR<x> >= 0 to true. */ - strict_overflow_p = false; if (code == GE_EXPR && (integer_zerop (arg1) || (! HONOR_NANS (arg0) && real_zerop (arg1))) - && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) - { - if (strict_overflow_p) - fold_overflow_warning (("assuming signed overflow does not occur " - "when simplifying comparison of " - "absolute value and zero"), - WARN_STRICT_OVERFLOW_CONDITIONAL); - return omit_one_operand_loc (loc, type, - constant_boolean_node (true, type), - arg0); - } + && tree_expr_nonnegative_p (arg0)) + return omit_one_operand_loc (loc, type, + constant_boolean_node (true, type), + arg0); /* Convert ABS_EXPR<x> < 0 to false. */ - strict_overflow_p = false; if (code == LT_EXPR && (integer_zerop (arg1) || real_zerop (arg1)) - && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)) - { - if (strict_overflow_p) - fold_overflow_warning (("assuming signed overflow does not occur " - "when simplifying comparison of " - "absolute value and zero"), - WARN_STRICT_OVERFLOW_CONDITIONAL); - return omit_one_operand_loc (loc, type, - constant_boolean_node (false, type), - arg0); - } + && tree_expr_nonnegative_p (arg0)) + return omit_one_operand_loc (loc, type, + constant_boolean_node (false, type), + arg0); /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0 and similarly for >= into !=. */ @@ -14584,11 +14561,11 @@ tree_expr_maybe_real_minus_zero_p (const_tree x) return true; } -#define tree_expr_nonnegative_warnv_p(X, Y) \ +#define tree_expr_nonnegative_p(X, Y) \ _Pragma ("GCC error \"Use RECURSE for recursive calls\"") 0 #define RECURSE(X) \ - ((tree_expr_nonnegative_warnv_p) (X, strict_overflow_p, depth + 1)) + ((tree_expr_nonnegative_p) (X, depth + 1)) /* Return true if CODE or TYPE is known to be non-negative. */ @@ -14613,11 +14590,6 @@ tree_unary_nonnegative_p (enum tree_code code, tree type, tree op0, int depth) if (TYPE_UNSIGNED (type)) return true; - /* The RECURSE () macro counts with a strict_overflow_p bool - pointer being declared beforehand. */ - bool val = false; - bool *strict_overflow_p = &val; - switch (code) { case ABS_EXPR: @@ -14679,11 +14651,6 @@ tree_binary_nonnegative_p (enum tree_code code, tree type, tree op0, if (TYPE_UNSIGNED (type)) return true; - /* The RECURSE () macro counts with a strict_overflow_p bool - pointer being declared beforehand. */ - bool val = false; - bool *strict_overflow_p = &val; - switch (code) { case POINTER_PLUS_EXPR: @@ -14802,11 +14769,6 @@ tree_single_nonnegative_p (tree t, int depth) if (TYPE_UNSIGNED (TREE_TYPE (t))) return true; - /* The RECURSE () macro counts with a strict_overflow_p bool - pointer being declared beforehand. */ - bool val = false; - bool *strict_overflow_p = &val; - switch (TREE_CODE (t)) { case INTEGER_CST: @@ -14843,11 +14805,6 @@ bool tree_call_nonnegative_p (tree type, combined_fn fn, tree arg0, tree arg1, int depth) { - /* The RECURSE () macro counts with a strict_overflow_p bool - pointer being declared beforehand. */ - bool val = false; - bool *strict_overflow_p = &val; - switch (fn) { CASE_CFN_ACOS: @@ -15040,11 +14997,6 @@ tree_invalid_nonnegative_p (tree t, int depth) if (TYPE_UNSIGNED (TREE_TYPE (t))) return true; - /* The RECURSE () macro counts with a strict_overflow_p bool - pointer being declared beforehand. */ - bool val = false; - bool *strict_overflow_p = &val; - switch (code) { case TARGET_EXPR: @@ -15105,15 +15057,13 @@ tree_invalid_nonnegative_p (tree t, int depth) } #undef RECURSE -#undef tree_expr_nonnegative_warnv_p +#undef tree_expr_nonnegative_p /* Return true if T is known to be non-negative. DEPTH is the current nesting depth of the query. */ bool -tree_expr_nonnegative_warnv_p (tree t, - bool *strict_overflow_p ATTRIBUTE_UNUSED, - int depth) +tree_expr_nonnegative_p (tree t, int depth) { enum tree_code code; if (error_operand_p (t)) @@ -15174,24 +15124,6 @@ tree_expr_nonnegative_warnv_p (tree t, } } -/* Return true if `t' is known to be non-negative. Handle warnings - about undefined signed overflow. */ - -bool -tree_expr_nonnegative_p (tree t) -{ - bool ret, strict_overflow_p; - - strict_overflow_p = false; - ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p); - if (strict_overflow_p) - fold_overflow_warning (("assuming signed overflow does not occur when " - "determining that expression is always " - "non-negative"), - WARN_STRICT_OVERFLOW_MISC); - return ret; -} - /* Return true when (CODE OP0) is an address and is known to be nonzero. For floating point we further ensure that T is not denormal. @@ -15232,7 +15164,6 @@ tree_unary_nonzero_p (enum tree_code code, tree type, tree op0) bool tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1) { - bool sub_strict_overflow_p = false; switch (code) { case POINTER_PLUS_EXPR: @@ -15241,10 +15172,8 @@ tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1) { /* With the presence of negative values it is hard to say something. */ - if (!tree_expr_nonnegative_warnv_p (op0, - &sub_strict_overflow_p) - || !tree_expr_nonnegative_warnv_p (op1, - &sub_strict_overflow_p)) + if (!tree_expr_nonnegative_p (op0) + || !tree_expr_nonnegative_p (op1)) return false; /* One of operands must be positive and the other non-negative. */ return (tree_expr_nonzero_p (op0) @@ -15265,7 +15194,6 @@ tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1) break; case MAX_EXPR: - sub_strict_overflow_p = false; if (tree_expr_nonzero_p (op0)) { @@ -15274,13 +15202,11 @@ tree_binary_nonzero_p (enum tree_code code, tree type, tree op0, tree op1) return true; /* MAX where operand 0 is positive is positive. */ - return tree_expr_nonnegative_warnv_p (op0, - &sub_strict_overflow_p); + return tree_expr_nonnegative_p (op0); } /* MAX where operand 1 is positive is positive. */ else if (tree_expr_nonzero_p (op1) - && tree_expr_nonnegative_warnv_p (op1, - &sub_strict_overflow_p)) + && tree_expr_nonnegative_p (op1)) return true; break; diff --git a/gcc/fold-const.h b/gcc/fold-const.h index 30d044ae5fb9..cefb5acf26dd 100644 --- a/gcc/fold-const.h +++ b/gcc/fold-const.h @@ -199,8 +199,7 @@ extern tree size_diffop_loc (location_t, tree, tree); extern tree non_lvalue_loc (location_t, tree); extern bool tree_expr_nonzero_p (tree); -extern bool tree_expr_nonnegative_p (tree); -extern bool tree_expr_nonnegative_warnv_p (tree, bool *, int = 0); +extern bool tree_expr_nonnegative_p (tree, int = 0); extern bool tree_expr_finite_p (const_tree); extern bool tree_expr_infinite_p (const_tree); extern bool tree_expr_maybe_infinite_p (const_tree); diff --git a/gcc/tree-ssa-loop-manip.cc b/gcc/tree-ssa-loop-manip.cc index 03e255281b75..df6cd8629bcf 100644 --- a/gcc/tree-ssa-loop-manip.cc +++ b/gcc/tree-ssa-loop-manip.cc @@ -133,9 +133,7 @@ create_iv (tree base, tree_code incr_op, tree step, tree var, class loop *loop, } else { - bool ovf; - - if (!tree_expr_nonnegative_warnv_p (step, &ovf) + if (!tree_expr_nonnegative_p (step) && may_negate_without_overflow_p (step)) { incr_op = (incr_op == PLUS_EXPR ? MINUS_EXPR : PLUS_EXPR);
