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

            Bug ID: 96326
           Summary: Incorrect loop optimization at -O3
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pterjan at gmail dot com
  Target Milestone: ---

Found in https://github.com/Sereal/Sereal/issues/229

#include <string.h>
#include <stdio.h>

int main() {
        char buf[128];
        char *src = buf;
        char *op = buf + 3;
        int len = 64;
        strcpy(buf, "abc");
        while (op - src < 8) {
                *(unsigned long*)op = *(const unsigned long*)src;
                len -= op - src;
                op += op - src;
        }
        while (len > 0) {
                *(unsigned long*)op = *(const unsigned long*)src;
                src += 8;
                op += 8;
                len -= 8;
        }
        printf("%ld\n", strlen(buf));
}

$ gcc -O2 -Wall t.c; ./a.out 
68

$ gcc -O3 -Wall t.c; ./a.out 
24

Adding a printf in the second loop also fixes it.

Reply via email to