From: Jakub Jelinek Date: Fri, 1 Feb 2019 08:44:23 +0000 (+0100) Subject: re PR tree-optimization/89143 (comparison of abs(i) against excessive constant less... X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=1651b09414923fd441adcc66902bdc52372ccb36;p=gcc.git re PR tree-optimization/89143 (comparison of abs(i) against excessive constant less than UXXX_MAX no longer folded) PR tree-optimization/89143 * wide-int-range.h (wide_int_range_absu): Declare. * wide-int-range.cc (wide_int_range_absu): New function. * tree-vrp.c (extract_range_from_unary_expr): Handle ABSU_EXPR. * gcc.dg/tree-ssa/vrp121.c: New test. From-SVN: r268445 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index 485bac73f2b..0da61908fee 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,5 +1,10 @@ 2019-02-01 Jakub Jelinek + PR tree-optimization/89143 + * wide-int-range.h (wide_int_range_absu): Declare. + * wide-int-range.cc (wide_int_range_absu): New function. + * tree-vrp.c (extract_range_from_unary_expr): Handle ABSU_EXPR. + PR tree-optimization/88107 * tree-cfg.c (find_outermost_region_in_block): Add ALL argument, instead of assertion that eh_region_outermost is non-NULL, if it diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index fb18cf11873..9d50c3e082a 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,5 +1,8 @@ 2019-02-01 Jakub Jelinek + PR tree-optimization/89143 + * gcc.dg/tree-ssa/vrp121.c: New test. + PR tree-optimization/88107 * gcc.dg/gomp/pr88107.c: New test. diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp121.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp121.c new file mode 100644 index 00000000000..3af5a9a2ec7 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp121.c @@ -0,0 +1,67 @@ +/* PR tree-optimization/89143 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-not "link_error \\\(" "optimized" } } */ + +void link_error (void); + +void +f1 (signed char i) +{ + if (__builtin_abs (i) < 0 || __builtin_abs (i) > __SCHAR_MAX__ + 1) + link_error (); +} + +void +f2 (signed char i) +{ + if (i < 0 || i > 15) + __builtin_unreachable (); + if (__builtin_abs (i) < 0 || __builtin_abs (i) > 15) + link_error (); +} + +void +f3 (signed char i) +{ + if (i < 19 || i > 25) + __builtin_unreachable (); + if (__builtin_abs (i) < 19 || __builtin_abs (i) > 25) + link_error (); +} + +void +f4 (signed char i) +{ + if (i > -60) + __builtin_unreachable (); + if (__builtin_abs (i) < 60 || __builtin_abs (i) > __SCHAR_MAX__ + 1) + link_error (); +} + +void +f5 (signed char i) +{ + if (i < -__SCHAR_MAX__ || i > -30) + __builtin_unreachable (); + if (__builtin_abs (i) < 30 || __builtin_abs (i) > __SCHAR_MAX__) + link_error (); +} + +void +f6 (signed char i) +{ + if (i < -__SCHAR_MAX__ || i > 30) + __builtin_unreachable (); + if (__builtin_abs (i) < 0 || __builtin_abs (i) > __SCHAR_MAX__) + link_error (); +} + +void +f7 (signed char i) +{ + if (i < -31 || i > 30) + __builtin_unreachable (); + if (__builtin_abs (i) < 0 || __builtin_abs (i) > 31) + link_error (); +} diff --git a/gcc/tree-vrp.c b/gcc/tree-vrp.c index 8d18e19d6e4..bde0751ec1e 100644 --- a/gcc/tree-vrp.c +++ b/gcc/tree-vrp.c @@ -2196,6 +2196,16 @@ extract_range_from_unary_expr (value_range_base *vr, vr->set_varying (); return; } + else if (code == ABSU_EXPR) + { + wide_int wmin, wmax; + wide_int vr0_min, vr0_max; + extract_range_into_wide_ints (&vr0, SIGNED, prec, vr0_min, vr0_max); + wide_int_range_absu (wmin, wmax, prec, vr0_min, vr0_max); + vr->set (VR_RANGE, wide_int_to_tree (type, wmin), + wide_int_to_tree (type, wmax)); + return; + } /* For unhandled operations fall back to varying. */ vr->set_varying (); diff --git a/gcc/wide-int-range.cc b/gcc/wide-int-range.cc index f18cb29447f..90c58f6bb6e 100644 --- a/gcc/wide-int-range.cc +++ b/gcc/wide-int-range.cc @@ -735,6 +735,37 @@ wide_int_range_abs (wide_int &min, wide_int &max, return true; } +/* Calculate ABSU_EXPR on a range and store the result in [MIN, MAX]. */ + +void +wide_int_range_absu (wide_int &min, wide_int &max, + unsigned prec, const wide_int &vr0_min, + const wide_int &vr0_max) +{ + /* Pass through VR0 the easy cases. */ + if (wi::ges_p (vr0_min, 0)) + { + min = vr0_min; + max = vr0_max; + return; + } + + min = wi::abs (vr0_min); + max = wi::abs (vr0_max); + + /* If the range contains zero then we know that the minimum value in the + range will be zero. */ + if (wi::ges_p (vr0_max, 0)) + { + if (wi::gtu_p (min, max)) + max = min; + min = wi::zero (prec); + } + else + /* Otherwise, swap MIN and MAX. */ + std::swap (min, max); +} + /* Convert range in [VR0_MIN, VR0_MAX] with INNER_SIGN and INNER_PREC, to a range in [MIN, MAX] with OUTER_SIGN and OUTER_PREC. diff --git a/gcc/wide-int-range.h b/gcc/wide-int-range.h index 7fbdffd84b3..fc9af72b127 100644 --- a/gcc/wide-int-range.h +++ b/gcc/wide-int-range.h @@ -107,6 +107,10 @@ extern bool wide_int_range_abs (wide_int &min, wide_int &max, const wide_int &vr0_min, const wide_int &vr0_max, bool overflow_undefined); +extern void wide_int_range_absu (wide_int &min, wide_int &max, + unsigned prec, + const wide_int &vr0_min, + const wide_int &vr0_max); extern bool wide_int_range_convert (wide_int &min, wide_int &max, signop inner_sign, unsigned inner_prec,