https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91183
Bug ID: 91183
Summary: strlen of a strcpy result with a conditional source
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: ---
While testing a solutuion for pr91147 I noticed that in the following GCC folds
the strlen call in f() into a constant but it doesn't know how to do that for
the second strlen call.
$ cat a.c && gcc -O2 -S -Wall -Wextra -Wpedantic
-fdump-tree-optimized=/dev/stdout a.c
void f (int i)
{
if (__builtin_strlen (i ? "123" : "456") != 3) // folded to false
__builtin_abort ();
}
void g (int i)
{
char a[4];
__builtin_strcpy (a, i ? "123" : "456"); // transformed to MEM_REF
if (3 != __builtin_strlen (a)) // not folded but could be
__builtin_abort ();
}
;; Function f (f, funcdef_no=0, decl_uid=1908, cgraph_uid=1, symbol_order=0)
f (int i)
{
<bb 2> [local count: 1073741824]:
return;
}
;; Function g (g, funcdef_no=1, decl_uid=1911, cgraph_uid=2, symbol_order=1)
Removing basic block 3
g (int i)
{
char a[4];
long unsigned int _1;
unsigned int _4;
<bb 2> [local count: 1073741824]:
if (i_3(D) != 0)
goto <bb 3>; [50.00%]
else
goto <bb 4>; [50.00%]
<bb 3> [local count: 536870912]:
<bb 4> [local count: 1073741824]:
# _4 = PHI <3552564(2), 3355185(3)>
MEM <unsigned int> [(char * {ref-all})&a] = _4;
_1 = __builtin_strlen (&a);
if (_1 != 3)
goto <bb 5>; [0.00%]
else
goto <bb 6>; [100.00%]
<bb 5> [count: 0]:
__builtin_abort ();
<bb 6> [local count: 1073741824]:
a ={v} {CLOBBER};
return;
}