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

            Bug ID: 107212
           Summary: -O2 and -O3 optimizer bug
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aosman9xx9 at gmail dot com
  Target Milestone: ---

There seams to be an optimizer bug in the recent gcc version.

=================================

[root@archlinux ~]# gcc --version
gcc (GCC) 12.2.0
(the current gcc version in Archlinux)

[root@archlinux ~]# cat GccError.c
#include <stdio.h>

int main() {
    unsigned int tab[6][2] = { {69, 73}, {36, 40}, {24, 16}, {16, 11}, {4, 5},
{3, 1} };

    int flag = 1;
    int sum_0 = 0;
    int sum_1 = 0;

    for(int t=0; t<6; t++) {
        sum_0 += tab[t][0];
        sum_1 += tab[t][1];
    }

    int x1 = (sum_0 < 100);
    int x2 = (sum_0 > 200);
    int x3 = (x1 || x2);

    if(sum_1 > 200) {
        flag=0;
    }

    printf("sum_0: %d\n", sum_0);
    printf("sum_1: %d\n", sum_1);
    printf("x1: %d\n", x1);
    printf("x2: %d\n", x2);
    printf("x1 || x2: %d\n", x3);
    printf("flag: %d\n", flag);

    return 0;
}

[root@archlinux ~]# gcc -O2 -Wall -Wextra -o GccError GccError.c

[root@archlinux ~]# ./GccError
sum_0: 152
sum_1: 146
x1: 0
x2: 0
x1 || x2: 1
flag: 1

=================================
The expected result of (x1 || x2) is 0, because x1 and x2 is 0. But the result
is 1. This only happens with -O2 or -O3 optimization.

Reply via email to