re PR middle-end/78153 (strlen return value can be assumed to be less than PTRDIFF_MAX)
authorPrathamesh Kulkarni <prathamesh.kulkarni@linaro.org>
Wed, 23 Nov 2016 18:04:14 +0000 (18:04 +0000)
committerPrathamesh Kulkarni <prathamesh3492@gcc.gnu.org>
Wed, 23 Nov 2016 18:04:14 +0000 (18:04 +0000)
2016-11-23  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>

PR middle-end/78153
* gimple-fold.c (fold_stmt_1): Handle case for GIMPLE_RETURN.
* tree-vrp.c (extract_range_basic): Handle case for
CFN_BUILT_IN_STRLEN.

testsuite/
* gcc.dg/tree-ssa/pr78153-1.c: New test.
* gcc.dg/tree-ssa/pr78153-2.c: Likewise.

From-SVN: r242786

gcc/ChangeLog
gcc/gimple-fold.c
gcc/testsuite/ChangeLog
gcc/testsuite/gcc.dg/tree-ssa/pr78153-1.c [new file with mode: 0644]
gcc/testsuite/gcc.dg/tree-ssa/pr78153-2.c [new file with mode: 0644]
gcc/tree-vrp.c

index 62e354c1a001a35e4b7bb44e81f027cd08ef8684..db002f7ae0e396a066cb8ed0901f3511cd6abc18 100644 (file)
@@ -1,3 +1,10 @@
+2016-11-23  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
+
+       PR middle-end/78153
+       * gimple-fold.c (fold_stmt_1): Handle case for GIMPLE_RETURN.
+       * tree-vrp.c (extract_range_basic): Handle case for
+       CFN_BUILT_IN_STRLEN.
+
 2016-11-23  Jeff Law  <law@redhat.com>
 
        * config/ia64/ia64.c (ia64_emit_insn_before): Fix prototype.
index aabc8ff2a7192b088e0258e07ef61f8c699d9059..684230161187dc3791f92878e900906b7eed47f3 100644 (file)
@@ -4406,6 +4406,23 @@ fold_stmt_1 (gimple_stmt_iterator *gsi, bool inplace, tree (*valueize) (tree))
        }
       break;
 
+    case GIMPLE_RETURN:
+      {
+       greturn *ret_stmt = as_a<greturn *> (stmt);
+       tree ret = gimple_return_retval(ret_stmt);
+
+       if (ret && TREE_CODE (ret) == SSA_NAME && valueize)
+         {
+           tree val = valueize (ret);
+           if (val && val != ret)
+             {
+               gimple_return_set_retval (ret_stmt, val);
+               changed = true;
+             }
+         }
+      }
+      break;
+
     default:;
     }
 
index 0b5c305921473c5970d017320ed33d985bc24687..084f3663562df471e60f678c3459a1229c51b084 100644 (file)
@@ -1,3 +1,9 @@
+2016-11-23  Prathamesh Kulkarni  <prathamesh.kulkarni@linaro.org>
+
+       PR middle-end/78153
+       * gcc.dg/tree-ssa/pr78153-1.c: New test.
+       * gcc.dg/tree-ssa/pr78153-2.c: Likewise.
+
 2016-11-23  James Greenhalgh  <james.greenhalgh@arm.com>
 
        PR target/63250
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr78153-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr78153-1.c
new file mode 100644 (file)
index 0000000..2530ba0
--- /dev/null
@@ -0,0 +1,10 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-evrp-slim" } */
+
+void f(const char *s)
+{
+  if (__PTRDIFF_MAX__ <= __builtin_strlen (s))
+    __builtin_abort ();
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin_abort" "evrp" } } */
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr78153-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr78153-2.c
new file mode 100644 (file)
index 0000000..de70450
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-evrp-slim" } */
+
+void f(const char *s)
+{
+  __PTRDIFF_TYPE__ n = __builtin_strlen (s);
+  if (n < 0)
+    __builtin_abort ();
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin_abort" "evrp" } } */
index 5bd4418eea321cf4cedf910574f795775b4c7fb5..33e0a755cfee6fa929a7ae89dadba4ded4e44d33 100644 (file)
@@ -4027,6 +4027,16 @@ extract_range_basic (value_range *vr, gimple *stmt)
                                  : vrp_val_max (type), NULL);
          }
          return;
+       case CFN_BUILT_IN_STRLEN:
+         {
+           tree type = TREE_TYPE (gimple_call_lhs (stmt));
+           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 (type); 
+           tree range_max = wide_int_to_tree (type, wmax - 1);
+           set_value_range (vr, VR_RANGE, range_min, range_max, NULL);
+         }
+         return;
        default:
          break;
        }