https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90626
Bug ID: 90626
Summary: fold strcmp(a, b) == 0 to zero when one string length
is exact and the other is unequal
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: ---
Similar to pr90625 but simpler, the strcmp equality in the function below can
safely be folded to zero because the two strings are of unequal lengths. The
code in handle_builtin_string_cmp() in strlen.c (committed in r261039) should
have all it needs to implement this optimization, it just also needs to
consider minimum string lengths.
$ cat b.c && gcc -O2 -S -Wall -fdump-tree-strlen=/dev/stdout b.c
int f (char * restrict a, char * restrict b)
{
__builtin_memcpy (a, "1234", 4); // length >= 4
__builtin_strcpy (b, "123"); // length == 3
return __builtin_strcmp (a, b) == 0; // must be false
}
;; Function f (f, funcdef_no=0, decl_uid=1907, cgraph_uid=1, symbol_order=0)
f (char * restrict a, char * restrict b)
{
int _1;
_Bool _2;
int _8;
<bb 2> [local count: 1073741824]:
__builtin_memcpy (a_4(D), "1234", 4);
__builtin_memcpy (b_6(D), "123", 4);
_1 = __builtin_strcmp (a_4(D), b_6(D));
_2 = _1 == 0;
_8 = (int) _2;
return _8;
}