https://gcc.gnu.org/bugzilla/show_bug.cgi?id=78809

--- Comment #14 from Wilco <wilco at gcc dot gnu.org> ---
(In reply to Qing Zhao from comment #11)
> (In reply to Wilco from comment #9)
> 
> > str(n)cmp with a constant string can be changed into memcmp if the string 
> > has a
> > known alignment or is an array of known size. We should check the common 
> > cases
> > are implemented.
> 
> Please provide an example in which a str(n)cmp with a constant string can be
> changed into
> memcmp. 
> 
> (From my understanding, for the strcmp (p, “fish”),  since we don’t know
> what will be in the string
> pointed by “p”, and there might be NULL_terminator in any of the place of p,
> we have to compare
> each char in “p” one by one, do I miss anything here?)

The only reason we have to do a character by character comparison is because we
cannot read beyond the end of a string. However when we know the size or
alignment we can safely process a string one word at a time.

I would expect all these to optimize into memcmp - but none do:

#include <stdlib.h>

char s[100];
typedef struct { int x; char s[8]; } S;

int f1(S *s) { return __builtin_strcmp(s->s, "abc") != 0; }
int f2(void) { return __builtin_strcmp(s, "abc") != 0; }
int f3(char *s) { s = __builtin_assume_aligned (s, 8); return
__builtin_strcmp(s, "abc") != 0; }
int f4(void) { char *s = (char*)malloc(100); return __builtin_strcmp(s, "abc")
!= 0; }

Reply via email to