public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tkoenig at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/101301] Improving sparse switch statement
Date: Mon, 05 Jul 2021 05:33:34 +0000	[thread overview]
Message-ID: <bug-101301-4-LoLl72TUuJ@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-101301-4@http.gcc.gnu.org/bugzilla/>

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 :-)

  parent reply	other threads:[~2021-07-05  5:33 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-02 15:32 [Bug tree-optimization/101301] New: " tkoenig at gcc dot gnu.org
2021-07-03 10:41 ` [Bug tree-optimization/101301] " tkoenig at gcc dot gnu.org
2021-07-03 18:42 ` tkoenig at gcc dot gnu.org
2021-07-03 19:34 ` segher at gcc dot gnu.org
2021-07-03 21:57 ` tkoenig at gcc dot gnu.org
2021-07-04 17:37 ` segher at gcc dot gnu.org
2021-07-05  5:33 ` tkoenig at gcc dot gnu.org [this message]
2021-07-05 17:33 ` segher at gcc dot gnu.org
2021-07-06 13:39 ` tkoenig at gcc dot gnu.org
2021-07-06 16:35 ` segher at gcc dot gnu.org
2021-08-12 10:31 ` marxin at gcc dot gnu.org
2022-11-30 13:04 ` cvs-commit at gcc dot gnu.org
2023-04-03  9:04 ` marxin at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-101301-4-LoLl72TUuJ@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).