PR tree-optimization/86428 - strlen of const array initialized with a string of the same length not folded
gcc/ChangeLog:
PR tree-optimization/86428
* expr.c (string_constant): Handle string literals of
length up to the size of the array they are stored in.
gcc/testsuite/ChangeLog:
PR tree-optimization/86428
* gcc.dg/strlenopt-49.c: New test.
* gcc.c-torture/execute/builtins/strlen-3.c: Adjust.
===================================================================
@@ -11358,7 +11358,6 @@ string_constant (tree arg, tree *ptr_offset)
}
else if (VAR_P (array) || TREE_CODE (array) == CONST_DECL)
{
- int length;
tree init = ctor_for_folding (array);
/* Variables initialized to string literals can be handled too. */
@@ -11367,22 +11366,25 @@ string_constant (tree arg, tree *ptr_offset)
|| TREE_CODE (init) != STRING_CST)
return 0;
- /* Avoid const char foo[4] = "abcde"; */
- if (DECL_SIZE_UNIT (array) == NULL_TREE
- || TREE_CODE (DECL_SIZE_UNIT (array)) != INTEGER_CST
- || (length = TREE_STRING_LENGTH (init)) <= 0
- || compare_tree_int (DECL_SIZE_UNIT (array), length) < 0)
- return 0;
+ tree array_size = DECL_SIZE_UNIT (array);
+ if (!array_size || TREE_CODE (array_size) != INTEGER_CST)
+ return NULL_TREE;
- /* If variable is bigger than the string literal, OFFSET must be constant
- and inside of the bounds of the string literal. */
- offset = fold_convert (sizetype, offset);
- if (compare_tree_int (DECL_SIZE_UNIT (array), length) > 0
- && (! tree_fits_uhwi_p (offset)
- || compare_tree_int (offset, length) >= 0))
- return 0;
+ /* Avoid returning a string that doesn't fit in the array
+ it is stored in, like
+ const char a[4] = "abcde";
+ but do handle those that fit even if they have excess
+ initializers, such as in
+ const char a[4] = "abc\000\000";
+ The excess elements contribute to TREE_STRING_LENGTH()
+ but not to strlen(). */
+ unsigned HOST_WIDE_INT length = strlen (TREE_STRING_POINTER (init));
+ if (compare_tree_int (array_size, length + 1) < 0)
+ return NULL_TREE;
- *ptr_offset = offset;
+ /* Callers should verify that OFFSET is within the bounds
+ of the array and warn for out-of-bounds offsets. */
+ *ptr_offset = fold_convert (sizetype, offset);
return init;
}
===================================================================
@@ -2,8 +2,11 @@
Test strlen on const variables initialized to string literals.
- Written by Jakub Jelinek, 9/14/2004. */
+ Written by Jakub Jelinek, 9/14/2004.
+ { dg-do compile }
+ { dg-options "-O2 -Wall -fdump-tree-optimized" } */
+
extern void abort (void);
extern __SIZE_TYPE__ strlen (const char *);
extern char *strcpy (char *, const char *);
@@ -10,7 +13,6 @@ extern char *strcpy (char *, const char *);
static const char bar[] = "Hello, World!";
static const char baz[] = "hello, world?";
static const char larger[20] = "short string";
-extern int inside_main;
int l1 = 1;
int x = 6;
@@ -59,12 +61,10 @@ main_test(void)
if (strlen (&larger[10]) != 2)
abort ();
- inside_main = 0;
- /* This will result in strlen call, because larger
- array is bigger than its initializer. */
if (strlen (larger + (x++ & 7)) != 5)
abort ();
if (x != 8)
abort ();
- inside_main = 1;
}
+
+/* { dg-final { scan-tree-dump-not "strlen" "optimized" } } */
===================================================================
@@ -0,0 +1,53 @@
+/* PR tree-optimization/86428 - strlen of const array initialized with
+ a string of the same length not folded
+ { dg-do compile }
+ { dg-options "-O0 -Wall -fdump-tree-gimple" } */
+
+#include "strlenopt.h"
+
+const char a1[1] = "\0";
+const char a2[2] = "1\0";
+const char a3[3] = "12\0";
+const char a8[8] = "1234567\0";
+const char a9[9] = "12345678\0";
+
+const char ax[9] = "12345678\0\0\0\0"; /* { dg-warning "initializer-string for array of chars is too long" } */
+const char ay[9] = "\00012345678\0\0\0\0"; /* { dg-warning "initializer-string for array of chars is too long" } */
+
+
+int len1 (void)
+{
+ size_t len0 = strlen (a1);
+ return len0;
+}
+
+int len (void)
+{
+ size_t len = strlen (a2) + strlen (a3) + strlen (a8) + strlen (a9);
+ return len;
+}
+
+int lenx (void)
+{
+ size_t lenx = strlen (ax);
+ return lenx;
+}
+
+int leny (void)
+{
+ size_t leny = strlen (ay);
+ return leny;
+}
+
+int cmp88 (void)
+{
+ int cmp88 = memcmp (a8, "1234567\0", sizeof a8);
+ return cmp88;
+}
+
+/* { dg-final { scan-tree-dump-times "strlen" 0 "gimple" } }
+ { dg-final { scan-tree-dump-times "len0 = 0;" 1 "gimple" } }
+ { dg-final { scan-tree-dump-times "len = 18;" 1 "gimple" } }
+ { dg-final { scan-tree-dump-times "lenx = 8;" 1 "gimple" } }
+ { dg-final { scan-tree-dump-times "leny = 0;" 1 "gimple" } }
+ { dg-final { scan-tree-dump-times "cmp88 = 0;" 1 "gimple" } } */