[Bug tree-optimization/101301] Improving sparse switch statement

tkoenig at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Jul 5 05:33:34 GMT 2021


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

--- Comment #6 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
If I create a foo3 function with

int foo3 (int n)
{
  if (__builtin_expect_with_probability (n >= 55555, 1, 0.55))
    {
      if (__builtin_expect_with_probability (n >= 77777, 1, 0.33/0.55))
        {
          if (__builtin_expect_with_probability (n == 77777, 1, 0.1/0.33))
            return 7;
          if (__builtin_expect_with_probability (n == 88888, 1, 0.1/0.23))
            return 8;
          if (__builtin_expect_with_probability (n == 99999, 1, 0.1/0.11))
            return 9;

          return 0;
        }
      else
        {
          if (__builtin_expect_with_probability (n == 55555, 1, 0.1/0.22))
            return 5;
          if (__builtin_expect_with_probability (n == 66666, 1, 0.1/0.11))
            return 6;

          return 0;
        }
    }
  else
    {
      if (__builtin_expect_with_probability (n >= 33333, 1, 0.22/0.45))
        {
          if (__builtin_expect_with_probability (n == 33333, 1, 0.1/0.22))
            return 3;
          if (__builtin_expect_with_probability (n == 44444, 1, 0.1/0.11))
            return 4;

          return 0;
        }
      else
        {
          if (__builtin_expect_with_probability (n == 11111, 1, 0.1/0.23))
            return 1;
          if (__builtin_expect_with_probability (n == 22222, 1, 0.1/0.13))
            return 2;

          return 0;
        }
    }
}

the numbers on POWER9 become

[tkoenig@gcc135 ~]$ gcc -O3 bench.c a.c
[tkoenig@gcc135 ~]$ ./a.out
foo: 7.134855
foo2: 7.842507
foo3: 6.624406
[tkoenig@gcc135 ~]$ gcc -mcpu=native -O3 bench.c a.c
[tkoenig@gcc135 ~]$ ./a.out
foo: 6.458520
foo2: 7.696735
foo3: 6.196469

where, on a few runs, the differene betweeh foo and foo3 with -mcpu=native
sometimes disappears and sometimes is larger (gcc135 is not a benchmark
machine).

So, I'd say there some advantage in the compiler not lying to itself :-)


More information about the Gcc-bugs mailing list