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

            Bug ID: 119424
           Summary: memcpy->memset optimization should happen even for
                    non-constant form of memcpy
           Product: gcc
           Version: 14.1.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: enhancement
          Priority: P3
         Component: tree-optimization
          Assignee: pinskia at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---

Take:
```
void g(int*,int s);
void f(int s)
{
  //s = 222;
  int buffer[222]={};
  int buffer1[222];
  __builtin_memcpy(&buffer1[0], &buffer[0], s*sizeof(int));
  g(buffer1,s);
}
void f1(int s)
{
  //s = 222;
  int buffer[222]={};
  int buffer1[222];
  __builtin_memset(&buffer1[0], 0, s*sizeof(int));
  g(buffer1,s);
}
```

We should be able to transform f to f1 but we current does not. If s was
constant, we could do it.  In this case if s was `>= 222`, then it would be
undefined behavior so doing it always seems like a good idea.

This alone won't solve the testcase in PR 103483 but it might improve other
cases.

Reply via email to