public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "marxin at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/103799] switch expansion could be smarter
Date: Wed, 22 Dec 2021 08:56:21 +0000	[thread overview]
Message-ID: <bug-103799-4-OyyqW4P7FV@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-103799-4@http.gcc.gnu.org/bugzilla/>

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

Martin Liška <marxin at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-12-22
             Status|UNCONFIRMED                 |NEW
                 CC|                            |marxin at gcc dot gnu.org

--- Comment #2 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #0)
> Take:
> auto f(char c)
> {
>   int t1;
>   switch (c)
>     {
>        case '1':
>          t1 = 1;
>          break;
>        case '2':
>          t1 = 2;
>          break;
>        case '3':
>          t1 = 3;
>          break;
>        case '\0':
>          t1 = 4;
>          break;
>        default:
>          t1 = -1;
>     }
>    return t1;
> }

Confirmed. Here we have:

  switch (_1) <default: <D.1986>, case 0: <D.1985>, case 49: <D.1982>, case 50:
<D.1983>, case 51: <D.1984>>

that's rejected by switch conversion [0, 51] range is quite sparse.

> 
> 
> auto f1(char c)
> {
>   int t1;
>   switch (c)
>     {
>        case '1':
>          t1 = 1;
>          break;
>        case '2':
>          t1 = 2;
>          break;
>        case '3':
>          t1 = 3;
>          break;

Here the first part is transformed by switch conversion as:
Linear transformation with A = 1 and B = -48

>     default:
>       if (c == '\0') t1 = 4; else t1 = -1;

And this is a classical
# t1_2 = PHI <4(4), -1(5), t1_5(3)>

So the idea of the transformation is basically to remove cases that "block"
and interesting switch optimization and handle these in default. Smart,
but not easy to achieve :P

>     }
>    return t1;
> }
> 
> f1 produces better code than f.

      parent reply	other threads:[~2021-12-22  8:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-22  7:44 [Bug tree-optimization/103799] New: " pinskia at gcc dot gnu.org
2021-12-22  7:44 ` [Bug tree-optimization/103799] " pinskia at gcc dot gnu.org
2021-12-22  8:56 ` marxin at gcc dot gnu.org [this message]

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-103799-4-OyyqW4P7FV@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).