https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90662
Bug ID: 90662
Summary: strlen of a string in a vla plus offset not folded
Product: gcc
Version: 9.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
In the test case below, GCC can compute the string length in f() and h() but
not in g().
The root cause is that the get_stridx() function in tree-ssa-strlen.c that
retrieves the length record for a non-constant string only handles
POINTER_PLUS_EXPR but the &a[2] in in g() is represented as '&*a.1_9[2]' or
ADDR_EXPR.
$ cat a.c && gcc -O2 -S -Wall -fdump-tree-optimized=/dev/stdout a.c
void f (int n)
{
char a[6];
__builtin_strcpy (a, "12345");
if (__builtin_strlen (&a[2]) != 3) // folded to false
__builtin_abort ();
}
void g (int n)
{
char a[n];
__builtin_strcpy (a, "12345");
if (__builtin_strlen (&a[2]) != 5) // not folded
__builtin_abort ();
}
void h (int n)
{
char *a = __builtin_malloc (6);
__builtin_strcpy (a, "12345");
if (__builtin_strlen (&a[2]) != 3) // folded to false
__builtin_abort ();
}
;; Function f (f, funcdef_no=0, decl_uid=1906, cgraph_uid=1, symbol_order=0)
f (int n)
{
<bb 2> [local count: 1073741824]:
return;
}
;; Function g (g, funcdef_no=1, decl_uid=1910, cgraph_uid=2, symbol_order=1)
g (int n)
{
char[0:D.1921] * a.1;
sizetype _1;
char * _6;
long unsigned int _7;
<bb 2> [local count: 1073741824]:
_1 = (sizetype) n_2(D);
a.1_9 = __builtin_alloca_with_align (_1, 8);
__builtin_memcpy (a.1_9, "12345", 6);
_6 = &*a.1_9[2];
_7 = __builtin_strlen (_6);
if (_7 != 5)
goto <bb 3>; [0.00%]
else
goto <bb 4>; [100.00%]
<bb 3> [count: 0]:
__builtin_abort ();
<bb 4> [local count: 1073741824]:
return;
}
;; Function h (h, funcdef_no=2, decl_uid=1914, cgraph_uid=3, symbol_order=2)
h (int n)
{
<bb 2> [local count: 1073741824]:
return;
}