From: Prathamesh Kulkarni Date: Tue, 1 May 2018 18:20:39 +0000 (+0000) Subject: re PR tree-optimization/82665 (missing value range optimization for memchr) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=41e2c1b0b4effb21729299c5dd7b3b2abd03d87b;p=gcc.git re PR tree-optimization/82665 (missing value range optimization for memchr) PR tree-optimization/82665 * vr-values.c (vr_values::extract_range_from_binary_expr): Handle pointer subtraction where arguments come from a memchr call. PR tree-optimization/82665 * gcc.dg/tree-ssa/pr82665.c: New test. From-SVN: r259806 --- diff --git a/gcc/ChangeLog b/gcc/ChangeLog index b1a5488bd58..2afdd79b158 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,9 @@ +2018-05-01 Prathamesh Kulkarni + + PR tree-optimization/82665 + * vr-values.c (vr_values::extract_range_from_binary_expr): Handle + pointer subtraction where arguments come from a memchr call. + 2018-05-01 Jakub Jelinek * configure.ac (LD_AS_NEEDED_OPTION, LD_NO_AS_NEEDED_OPTION): Use diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 7e0a41ee7af..cefe1ca326d 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2018-05-01 Prathamesh Kulkarni + + PR tree-optimization/82665 + * gcc.dg/tree-ssa/pr82665.c: New test. + 2018-04-30 David Malcolm PR c++/85523 diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr82665.c b/gcc/testsuite/gcc.dg/tree-ssa/pr82665.c new file mode 100644 index 00000000000..66db32f46db --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr82665.c @@ -0,0 +1,32 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +void f1 (char *p, __SIZE_TYPE__ sz) +{ + char *q = __builtin_memchr (p, 0, sz); + __PTRDIFF_TYPE__ n = q - p; + + if (n >= __PTRDIFF_MAX__) + __builtin_abort (); +} + +void f2 (unsigned char *p, __SIZE_TYPE__ sz) +{ + unsigned char *q = __builtin_memchr (p, 0, sz); + __PTRDIFF_TYPE__ n = q - p; + + if (n >= __PTRDIFF_MAX__) + __builtin_abort (); +} + +void f3 (signed char *p, __SIZE_TYPE__ sz) +{ + signed char *q = __builtin_memchr (p, 0, sz); + __PTRDIFF_TYPE__ n = q - p; + + if (n >= __PTRDIFF_MAX__) + __builtin_abort (); +} + + +/* { dg-final { scan-tree-dump-not "memchr" "optimized" } } */ diff --git a/gcc/vr-values.c b/gcc/vr-values.c index b1f587d2b92..74f813e7334 100644 --- a/gcc/vr-values.c +++ b/gcc/vr-values.c @@ -793,6 +793,39 @@ vr_values::extract_range_from_binary_expr (value_range *vr, extract_range_from_binary_expr_1 (vr, code, expr_type, &vr0, &vr1); + /* Set value_range for n in following sequence: + def = __builtin_memchr (arg, 0, sz) + n = def - arg + Here the range for n can be set to [0, PTRDIFF_MAX - 1]. */ + + if (vr->type == VR_VARYING + && code == POINTER_DIFF_EXPR + && TREE_CODE (op0) == SSA_NAME + && TREE_CODE (op1) == SSA_NAME) + { + tree op0_ptype = TREE_TYPE (TREE_TYPE (op0)); + tree op1_ptype = TREE_TYPE (TREE_TYPE (op1)); + gcall *call_stmt = NULL; + + if (TYPE_MODE (op0_ptype) == TYPE_MODE (char_type_node) + && TYPE_PRECISION (op0_ptype) == TYPE_PRECISION (char_type_node) + && TYPE_MODE (op1_ptype) == TYPE_MODE (char_type_node) + && TYPE_PRECISION (op1_ptype) == TYPE_PRECISION (char_type_node) + && (call_stmt = dyn_cast(SSA_NAME_DEF_STMT (op0))) + && gimple_call_builtin_p (call_stmt, BUILT_IN_MEMCHR) + && operand_equal_p (op0, gimple_call_lhs (call_stmt), 0) + && operand_equal_p (op1, gimple_call_arg (call_stmt, 0), 0) + && integer_zerop (gimple_call_arg (call_stmt, 1))) + { + tree max = vrp_val_max (ptrdiff_type_node); + wide_int wmax = wi::to_wide (max, TYPE_PRECISION (TREE_TYPE (max))); + tree range_min = build_zero_cst (expr_type); + tree range_max = wide_int_to_tree (expr_type, wmax - 1); + set_value_range (vr, VR_RANGE, range_min, range_max, NULL); + return; + } + } + /* Try harder for PLUS and MINUS if the range of one operand is symbolic and based on the other operand, for example if it was deduced from a symbolic comparison. When a bound of the range of the first operand