From 45466eecf5ef669164c0922e5be8fd288b144886 Mon Sep 17 00:00:00 2001 From: Andrew Pinski Date: Tue, 16 May 2023 14:26:41 -0700 Subject: Add a != MIN/MAX_VALUE_CST ? CST-+1 : a to minmax_from_comparison This patch adds the support for match that was implemented for PR 87913 in phiopt. It implements it by adding support to minmax_from_comparison for the check. It uses the range information if available which allows to produce MIN/MAX expression when comparing against the lower/upper bound of the range instead of lower/upper of the type. minmax-20.c is the new testcase which tests the ranges part. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * fold-const.cc (minmax_from_comparison): Add support for NE_EXPR. * match.pd ((cond (cmp (convert1? x) c1) (convert2? x) c2) pattern): Add ne as a possible cmp. ((a CMP b) ? minmax : minmax pattern): Likewise. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/minmax-22.c: New test. --- gcc/fold-const.cc | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) (limited to 'gcc/fold-const.cc') diff --git a/gcc/fold-const.cc b/gcc/fold-const.cc index 5752150..84b0d06 100644 --- a/gcc/fold-const.cc +++ b/gcc/fold-const.cc @@ -173,6 +173,19 @@ minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp /* X > Y - 1 equals to X >= Y. */ if (cmp == GT_EXPR) code = GE_EXPR; + /* a != MIN_RANGE ? a : MIN_RANGE+1 -> MAX_EXPR+1, a> */ + if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME) + { + value_range r; + get_range_query (cfun)->range_of_expr (r, exp0); + if (r.undefined_p ()) + r.set_varying (TREE_TYPE (exp0)); + + widest_int min = widest_int::from (r.lower_bound (), + TYPE_SIGN (TREE_TYPE (exp0))); + if (min == wi::to_widest (exp1)) + code = MAX_EXPR; + } } if (wi::to_widest (exp1) == (wi::to_widest (exp3) + 1)) { @@ -182,6 +195,19 @@ minmax_from_comparison (tree_code cmp, tree exp0, tree exp1, tree exp2, tree exp /* X >= Y + 1 equals to X > Y. */ if (cmp == GE_EXPR) code = GT_EXPR; + /* a != MAX_RANGE ? a : MAX_RANGE-1 -> MIN_EXPR-1, a> */ + if (cmp == NE_EXPR && TREE_CODE (exp0) == SSA_NAME) + { + value_range r; + get_range_query (cfun)->range_of_expr (r, exp0); + if (r.undefined_p ()) + r.set_varying (TREE_TYPE (exp0)); + + widest_int max = widest_int::from (r.upper_bound (), + TYPE_SIGN (TREE_TYPE (exp0))); + if (max == wi::to_widest (exp1)) + code = MIN_EXPR; + } } } if (code != ERROR_MARK -- cgit v1.1