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

            Bug ID: 101958
           Summary: compiling with "-O2 -fselective-scheduling2
                    -fno-tree-ch" produce bad result
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Keywords: missed-optimization, wrong-code
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: channhuang at 163 dot com
                CC: jakub at gcc dot gnu.org
  Target Milestone: ---
              Host: x86_64-pc-linux-gnu
            Target: x86_64-pc-linux-gnu
             Build: x86_64-pc-linux-gnu

gcc-11.2.0, x86_64-pc-linux-gnu, compiling with the following options:
"-O2 -fselective-scheduling2 -fno-tree-ch -lm"
The source code is for counting the number of primes up to 10000.

#include <stdio.h>
#include <math.h>

unsigned long  max_prime =10000;
int cpu_execute_event()
{
  unsigned long c,l;
  int n = 0;
  double t;

  for(c = 2; c < max_prime; c++)
  {
    t = sqrt((double)c);
    for(l = 2; l <= t; l++)
      if (c % l == 0)
        break;
    if (l > t )
      n++;
  }
  return n;                                                                     
}

int main ()
{
        int n;
        n = cpu_execute_event();
        printf("n:%d\n",n);
        return 0;
}

The real return value of cpu_execute_event() is 2 where it should be 1229.
It works correctly when I compiled with "-O2 -fselective-scheduling2" or "-O2
-fno-tree-ch".I suspect that instruction scheduling changes the meaning of the
code.And gcc10.2 also produces the same phenomenon.

The command line is the following:
gcc-11.2 cpu_execute_event.c -O2 -fselective-scheduling2 -fno-tree-ch -lm

There is no output message, no error, no warning even compiled with the options
"-Wall -Wextra".

I use gcc 11.2.0, configured with:
../gcc-11.2.0/configure --prefix=/home/chann/gcc/gcc11_2 --enable-threads=posix
--disable-checking --disable-multilib --enable-languages=c,c++

Reply via email to