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
This commit is contained in:
Prathamesh Kulkarni 2018-05-01 18:20:39 +00:00 committed by Jeff Law
parent 1a142d6e68
commit 41e2c1b0b4
4 changed files with 76 additions and 0 deletions

View File

@ -1,3 +1,9 @@
2018-05-01 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
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 <jakub@redhat.com>
* configure.ac (LD_AS_NEEDED_OPTION, LD_NO_AS_NEEDED_OPTION): Use

View File

@ -1,3 +1,8 @@
2018-05-01 Prathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
PR tree-optimization/82665
* gcc.dg/tree-ssa/pr82665.c: New test.
2018-04-30 David Malcolm <dmalcolm@redhat.com>
PR c++/85523

View File

@ -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" } } */

View File

@ -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<gcall *>(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