Hi Jakub,
Based on your suggestions in PR83501, I have updated the patch to
check for integer_zerop for 2nd operand of mem_ref.

With the patch, Warray-bounds started warning for the following test
in Warray-bounds-3.c in line 362 and thus I removed xfail on it:
TM (a5, "0123", ma.a5 + i, ma.a5);

Does it look OK ?
Validation in progress.

Thanks,
Prathamesh
2017-12-21  Prathamesh Kulkarni  <prathamesh.kulka...@linaro.org>

        * tree-ssa-strlen.c (get_string_cst): New.
        (handle_char_store): Call get_string_cst.

testsuite/
        * gcc.dg/tree-ssa/pr83501-1.c: New test.
        * c-c++-common/Warray-bounds-3.c (test_strcpy_bounds_memarray_range):
        Remove xfail and adjust test for a5.
diff --git a/gcc/testsuite/c-c++-common/Warray-bounds-3.c 
b/gcc/testsuite/c-c++-common/Warray-bounds-3.c
index 73103f43d01..62667344c0a 100644
--- a/gcc/testsuite/c-c++-common/Warray-bounds-3.c
+++ b/gcc/testsuite/c-c++-common/Warray-bounds-3.c
@@ -359,7 +359,7 @@ void test_strcpy_bounds_memarray_range (void)
   TM (a5, "0",    ma.a5 + i, ma.a5);
   TM (a5, "01",   ma.a5 + i, ma.a5);
   TM (a5, "012",  ma.a5 + i, ma.a5);
-  TM (a5, "0123", ma.a5 + i, ma.a5);     /* { dg-warning "offset 10 from the 
object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with 
type .char\\\[5]. at offset 4" "strcpy" { xfail *-*-* } } */
+  TM (a5, "0123", ma.a5 + i, ma.a5);     /* { dg-warning "offset 10 from the 
object at .ma. is out of the bounds of referenced subobject .\(MA::\)?a5. with 
type .char ?\\\[5]' at offset 4" } */
 
   TM (a11, "0",       ma.a5, ma.a11);
   TM (a11, "01",      ma.a5, ma.a11);
diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c 
b/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c
new file mode 100644
index 00000000000..d8d3bf6039a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr83501.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-strlen" } */
+
+char a[4];
+
+void f (void)
+{
+  __builtin_strcpy (a, "abc");
+
+  if (__builtin_strlen (a) != 3)
+    __builtin_abort ();
+}
+
+/* { dg-final { scan-tree-dump-not "__builtin_strlen" "strlen" } } */
diff --git a/gcc/tree-ssa-strlen.c b/gcc/tree-ssa-strlen.c
index 8971c7df4f3..f1d4f6ef059 100644
--- a/gcc/tree-ssa-strlen.c
+++ b/gcc/tree-ssa-strlen.c
@@ -2769,6 +2769,21 @@ handle_pointer_plus (gimple_stmt_iterator *gsi)
     }
 }
 
+/* Check if RHS is string_cst possibly wrapped by mem_ref.  */
+static tree
+get_string_cst (tree rhs)
+{
+  if (TREE_CODE (rhs) == MEM_REF
+      && integer_zerop (TREE_OPERAND (rhs, 1)))
+    {
+      rhs = TREE_OPERAND (rhs, 0);
+      if (TREE_CODE (rhs) == ADDR_EXPR)
+       rhs = TREE_OPERAND (rhs, 0);
+    }
+
+  return (TREE_CODE (rhs) == STRING_CST) ? rhs : NULL_TREE;
+}
+
 /* Handle a single character store.  */
 
 static bool
@@ -2924,11 +2939,11 @@ handle_char_store (gimple_stmt_iterator *gsi)
        }
     }
   else if (idx == 0
-          && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST
+          && (rhs = get_string_cst (gimple_assign_rhs1 (stmt)))
           && ssaname == NULL_TREE
           && TREE_CODE (TREE_TYPE (lhs)) == ARRAY_TYPE)
     {
-      size_t l = strlen (TREE_STRING_POINTER (gimple_assign_rhs1 (stmt)));
+      size_t l = strlen (TREE_STRING_POINTER (rhs));
       HOST_WIDE_INT a = int_size_in_bytes (TREE_TYPE (lhs));
       if (a > 0 && (unsigned HOST_WIDE_INT) a > l)
        {

Reply via email to