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

            Bug ID: 96754
           Summary: Failure to optimize strcpy+strlen to memcpy when
                    strlen is done after strcpy
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gabravier at gmail dot com
  Target Milestone: ---

size_t f1(char *a, const char *b)
{
    strcpy(a, b);
    return strlen(b);
}

size_t f2(char *a, const char *b)
{
    size_t sz = strlen(b);
    memcpy(a, b, sz + 1);
    return sz;
}

f1 can be optimized to f2. Using this intermediary form :

size_t f(char *a, const char *b)
{
    size_t tmp = strlen(b);
    strcpy(a, b);
    return tmp;
}

Results in the optimization being done properly, but it would be nice if it was
doable even if the `strlen` wasn't done prior to the `strcpy`.

Reply via email to