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.

Reply via email to