public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/40748] simple switch/case, if/else and arithmetics result in different code
       [not found] <bug-40748-4@http.gcc.gnu.org/bugzilla/>
@ 2021-08-29  4:06 ` pinskia at gcc dot gnu.org
  2021-10-25 12:25 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-29  4:06 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|middle-end                  |tree-optimization

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
So we handle f1 and foo correctly since GCC 9 (well almost, f1 is handled
correctly on the trunk that is turn into MIN_EXPR which happens because PHI-OPT
uses match-and-simplify which was done in r12-1152).

f3 has always been handled.

f2 is the one which is not handled, GCC knows how to convert it to a switch
table but does not because of "cost".

Here is a similar function which messes up GCC and LLVM fully and never
recovers (for GCC it started in 11, for clang it started in clang 12):
unsigned f2(unsigned i)
{
        if (i == 0) return 0;
        if (i == 1) return 1;
        if (i == 2) return 2;
        if (i == 3) return 3;
        if (i == 4) return 4;
        if (i == 5) return 4;
        if (i == 6) return 4;
        return 4;
}

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug tree-optimization/40748] simple switch/case, if/else and arithmetics result in different code
       [not found] <bug-40748-4@http.gcc.gnu.org/bugzilla/>
  2021-08-29  4:06 ` [Bug tree-optimization/40748] simple switch/case, if/else and arithmetics result in different code pinskia at gcc dot gnu.org
@ 2021-10-25 12:25 ` pinskia at gcc dot gnu.org
  2021-10-25 12:37 ` marxin at gcc dot gnu.org
  2021-10-25 12:42 ` marxin at gcc dot gnu.org
  3 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-25 12:25 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gabravier at gmail dot com

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 102927 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug tree-optimization/40748] simple switch/case, if/else and arithmetics result in different code
       [not found] <bug-40748-4@http.gcc.gnu.org/bugzilla/>
  2021-08-29  4:06 ` [Bug tree-optimization/40748] simple switch/case, if/else and arithmetics result in different code pinskia at gcc dot gnu.org
  2021-10-25 12:25 ` pinskia at gcc dot gnu.org
@ 2021-10-25 12:37 ` marxin at gcc dot gnu.org
  2021-10-25 12:42 ` marxin at gcc dot gnu.org
  3 siblings, 0 replies; 4+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-10-25 12:37 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |marxin at gcc dot gnu.org

--- Comment #6 from Martin Liška <marxin at gcc dot gnu.org> ---
(In reply to Marc Glisse from comment #3)
> We also miss the even simpler case that should be optimized to "return n;"
> 
> int foo(int n){
>     switch(n){
>         case 0:
>             return 0;
>         case 1:
>             return 1;
>         case 2:
>             return 2;
>         case 3:
>             return 3;
>         default:
>             __builtin_unreachable();
>     }
> }
> 
> llvm performs the expected optimization in both cases.

Note this one is implemented since 9.1.0.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug tree-optimization/40748] simple switch/case, if/else and arithmetics result in different code
       [not found] <bug-40748-4@http.gcc.gnu.org/bugzilla/>
                   ` (2 preceding siblings ...)
  2021-10-25 12:37 ` marxin at gcc dot gnu.org
@ 2021-10-25 12:42 ` marxin at gcc dot gnu.org
  3 siblings, 0 replies; 4+ messages in thread
From: marxin at gcc dot gnu.org @ 2021-10-25 12:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Martin Liška <marxin at gcc dot gnu.org> ---
> {
> 	if (i == 0) return 0;
> 	if (i == 1) return 1;
> 	if (i == 2) return 2;
> 	if (i == 3) return 3;
> 	if (i == 4) return 4;
> 	if (i == 5) return 4;
> 	if (i == 6) return 4;
> 	return 4;
> }

Here we produce:

  <bb 2> [local count: 1073741824]:
  if (i_2(D) <= 6)
    goto <bb 3>; [50.00%]
  else
    goto <bb 4>; [50.00%]

  <bb 3> [local count: 536870913]:
  _4 = CSWTCH.1[i_2(D)];

  <bb 4> [local count: 1073741824]:
  # _1 = PHI <4(2), _4(3)>
  return _1;

Am I right that we can do better with:

  if (i_2(D) <= 3)
...

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-10-25 12:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <bug-40748-4@http.gcc.gnu.org/bugzilla/>
2021-08-29  4:06 ` [Bug tree-optimization/40748] simple switch/case, if/else and arithmetics result in different code pinskia at gcc dot gnu.org
2021-10-25 12:25 ` pinskia at gcc dot gnu.org
2021-10-25 12:37 ` marxin at gcc dot gnu.org
2021-10-25 12:42 ` marxin at gcc dot gnu.org

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