https://gcc.gnu.org/bugzilla/show_bug.cgi?id=71315
Bug ID: 71315
Summary: missing strlen optimization on a POINTER_PLUS
expression
Product: gcc
Version: 7.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: middle-end
Assignee: unassigned at gcc dot gnu.org
Reporter: msebor at gcc dot gnu.org
Target Milestone: ---
In addition to bug 71303 and bug 71304, another optimization missing from
tree-ssa-strlen.c is for calls to strlen with arguments that point past the
first element of a string are not optimized into constants, resulting in
(repeated) calls to strlen with the same argument.
Clang emits optimal code for this test case.
$ cat z.cpp && /build/gcc-6-branch/gcc/xgcc -B /build/gcc-6-branch/gcc -O2 -S
-Wall -Wextra -Wpedantic -fdump-tree-optimized=/dev/stdout zz.cpp
template<class T1>
class C
{
template<class T2>
class C2 { };
};
template<> template<class X, class Y>
class C<int>::C2 { };
template<> template<int>
class C<float>::C2 { };
;; Function void g() (_Z1gv, funcdef_no=0, decl_uid=2254, cgraph_uid=0,
symbol_order=0)
void g() ()
{
char s[5];
long unsigned int _3;
unsigned int _4;
long unsigned int _6;
unsigned int _7;
<bb 2>:
s = "1234";
_3 = __builtin_strlen (&MEM[(void *)&s + 1B]);
_4 = (unsigned int) _3;
f (_4);
_6 = __builtin_strlen (&MEM[(void *)&s + 1B]);
_7 = (unsigned int) _6;
f (_7);
s ={v} {CLOBBER};
return;
}