re PR tree-optimization/82665 (missing value range optimization for memchr)
authorPrathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
Tue, 1 May 2018 18:20:39 +0000 (18:20 +0000)
committerJeff Law <law@gcc.gnu.org>
Tue, 1 May 2018 18:20:39 +0000 (12:20 -0600)
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

gcc/ChangeLog
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/pr82665.c [new file with mode: 0644]
gcc/vr-values.c

index b1a5488..2afdd79 100644 (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
index 7e0a41e..cefe1ca 100644 (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
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr82665.c b/gcc/testsuite/gcc.dg/tree-ssa/pr82665.c
new file mode 100644 (file)
index 0000000..66db32f
--- /dev/null
@@ -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" } } */
index b1f587d..74f813e 100644 (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