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

            Bug ID: 95396
           Summary: GCC produces incorrect code with -O3 for loops
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: vsevolod.livinskij at frtk dot ru
  Target Milestone: ---

Error:
>$ g++ -O0 small.cpp && ./a.out 
42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 42 0 0 0 
>$ g++ -O3 small.cpp && ./a.out 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 42 0 0 0 

Reproducer:
>$ cat small.cpp 
#include <stdio.h>

unsigned short arr_26 [20];
signed char arr_25 [2000];

unsigned char b;

int c(int d, int f) { return d > f ? d : f; }

void test() { 
    for (char e = c(b, 241) + 15; e < 17; e += 1)
        arr_26[e] = 42;
}

int main() {
    test();
    for (size_t i_0 = 0; i_0 < 20; ++i_0)
        printf("%d ", arr_26 [i_0]);
    printf("\n");
}

GCC version:
11.0.0 (1852a26b925970f64f8d31518ba732fe9c3ade23)

Reply via email to