https://gcc.gnu.org/bugzilla/show_bug.cgi?id=81384
Bug ID: 81384
Summary: built-in form of strnlen missing
Product: gcc
Version: 8.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: ---
GCC doesn't provide a built-in form of the POSIX strnlen function. Providing
such a built-in would make it possible to emit more efficient code for programs
that make use of it. The Linux kernel, for example, has on the order of 260
instances of calls to strnlen.
The test case below shows one instance of a missed optimization opportunity
involving strnlen.
$ cat b.c && gcc -O2 -S -Wall -Wextra -fdump-tree-optimized=/dev/stdout b.c
typedef __SIZE_TYPE__ size_t;
size_t strnlen (const char*, size_t);
void f (const char *s)
{
unsigned n = strnlen (s, 7);
if (n > 7) // can never hold
__builtin_abort (); // can be eliminated
}
;; Function f (f, funcdef_no=0, decl_uid=1819, cgraph_uid=0, symbol_order=0)
f (const char * s)
{
unsigned int n;
long unsigned int _1;
<bb 2> [100.00%] [count: INV]:
_1 = strnlen (s_3(D), 7);
n_5 = (unsigned int) _1;
if (n_5 > 7)
goto <bb 3>; [0.04%] [count: 0]
else
goto <bb 4>; [99.96%] [count: INV]
<bb 3> [0.04%] [count: 0]:
__builtin_abort ();
<bb 4> [99.96%] [count: INV]:
return;
}