public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/107443] New: Switch conversion removing code
@ 2022-10-27 18:52 amacleod at redhat dot com
  2022-10-27 23:34 ` [Bug middle-end/107443] [10/11/12/13 Regression] Removing switch with __builtin_unreachable causes missed optimizations pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: amacleod at redhat dot com @ 2022-10-27 18:52 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107443
           Summary: Switch conversion removing code
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: amacleod at redhat dot com
  Target Milestone: ---

Compile the following with -O2 -fdump-tree-all -fdisable-tree-evrp

void dead (unsigned n);
void foo (unsigned n);

void func (unsigned n)
{
  if (n == 0)
    __builtin_unreachable();
  if (n == 1)
    __builtin_unreachable();
  if (n == 2)
    __builtin_unreachable();
  if (n == 3)
    __builtin_unreachable();
  if (n == 4)
    __builtin_unreachable();
  if (n == 5)
    __builtin_unreachable();
  if (n == 6)
    __builtin_unreachable();
  if (n == 7)
    __builtin_unreachable();
  foo (n);
  if (n < 8)
    dead (n);
}

iftoswitch converts the series of if's into a switch:

  <bb 2> :
  switch (n_2(D)) <default: <L26> [INV], case 0: <L18> [INV], case 1: <L19>
[INV], case 2: <L20> [INV], case 3: <L21> [INV], case 4: <L22> [INV], case 5:
<L23> [INV], case 6: <L24> [INV], case 7: <L25> [INV]>
  <bb 3> :
<L18>:
  __builtin_unreachable ();
  <bb 4> :
<L19>:
  __builtin_unreachable ();
  <bb 5> :
<L20>:
  __builtin_unreachable ();
  <bb 6> :
<L21>:
  __builtin_unreachable ();
  <bb 7> :
<L22>:
  __builtin_unreachable ();
  <bb 8> :
<L23>:
  __builtin_unreachable ();
  <bb 9> :
<L24>:
  __builtin_unreachable ();
  <bb 10> :
<L25>:
  __builtin_unreachable ();
  <bb 11> :
<L26>:
  foo (n_2(D));
  if (n_2(D) <= 7)
    goto <bb 12>; [INV]
  else
    goto <bb 13>; [INV]

  <bb 12> :
  dead (n_2(D));


 switch conversion then runs, eliminates all the blocks, then bails on the
conversion, leaving the IL without any of the __builtin_unreachable calls:

Beginning to process the following SWITCH statement ((null):0) : -------
switch (n_2(D)) <default: <L26> [INV], case 0: <L18> [INV], case 1: <L19>
[INV], case 2: <L20> [INV], case 3: <L21> [INV], case 4: <L22> [INV], case 5:
<L23> [INV], case 6: <L24> [INV], case 7: <L25> [INV]>

Removing basic block 3
Removing basic block 4
Removing basic block 5
Removing basic block 6
Removing basic block 7
Removing basic block 8
Removing basic block 9
Removing basic block 10
Bailing out - switch is a degenerate case
--------------------------------
Merging blocks 2 and 11
void func (unsigned int n)
{
  <bb 2> :
  foo (n_2(D));
  if (n_2(D) <= 7)
    goto <bb 3>; [INV]
  else
    goto <bb 4>; [INV]

  <bb 3> :
  dead (n_2(D));

  <bb 4> :
  return;

}

And now the optimizers cannot remove the call to dead() because we lose all the
range information inferred from the __builtin_unreachable calls.

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

* [Bug middle-end/107443] [10/11/12/13 Regression] Removing switch with __builtin_unreachable causes missed optimizations
  2022-10-27 18:52 [Bug middle-end/107443] New: Switch conversion removing code amacleod at redhat dot com
@ 2022-10-27 23:34 ` pinskia at gcc dot gnu.org
  2022-10-28 11:57 ` rguenth at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-10-27 23:34 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |10.5
   Last reconfirmed|                            |2022-10-27
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
      Known to fail|                            |8.1.0
            Summary|Switch conversion removing  |[10/11/12/13 Regression]
                   |code                        |Removing switch with
                   |                            |__builtin_unreachable
                   |                            |causes missed optimizations
      Known to work|                            |7.1.0

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Testcase which shows this is a regression and switch conversion just exposed
it:
void dead (unsigned n);
void foo (unsigned n);

void func (unsigned n)
{
  switch(n) {
      case 0:
    __builtin_unreachable();
      case 1:
    __builtin_unreachable();
      case 2:
    __builtin_unreachable();
      case 3:
    __builtin_unreachable();
      case 4:
    __builtin_unreachable();
      case 5:
    __builtin_unreachable();
      case 6:
    __builtin_unreachable();
      case 7:
    __builtin_unreachable();
    default:
    ;
  }
  foo (n);
  if (n < 8)
    dead (n);
}

after building the CFG we get with the above testcase:
Removing basic block 10
Removing basic block 9
Removing basic block 8
Removing basic block 7
Removing basic block 6
Removing basic block 5
Removing basic block 4
Removing basic block 3
Merging blocks 2 and 11

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

* [Bug middle-end/107443] [10/11/12/13 Regression] Removing switch with __builtin_unreachable causes missed optimizations
  2022-10-27 18:52 [Bug middle-end/107443] New: Switch conversion removing code amacleod at redhat dot com
  2022-10-27 23:34 ` [Bug middle-end/107443] [10/11/12/13 Regression] Removing switch with __builtin_unreachable causes missed optimizations pinskia at gcc dot gnu.org
@ 2022-10-28 11:57 ` rguenth at gcc dot gnu.org
  2023-03-24  8:42 ` rguenth at gcc dot gnu.org
  2023-07-07 10:44 ` [Bug middle-end/107443] [11/12/13/14 " rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-10-28 11:57 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
I think we simply "optimize" the switch instead of, like for the if, preserving
the range asserting side-effect until some point in the compilation.

Ideally switch conversion would turn the switch side-effect into a single if.

But I also think the testcase is somewhat artificial so I wonder if we
should worry here at all ...

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

* [Bug middle-end/107443] [10/11/12/13 Regression] Removing switch with __builtin_unreachable causes missed optimizations
  2022-10-27 18:52 [Bug middle-end/107443] New: Switch conversion removing code amacleod at redhat dot com
  2022-10-27 23:34 ` [Bug middle-end/107443] [10/11/12/13 Regression] Removing switch with __builtin_unreachable causes missed optimizations pinskia at gcc dot gnu.org
  2022-10-28 11:57 ` rguenth at gcc dot gnu.org
@ 2023-03-24  8:42 ` rguenth at gcc dot gnu.org
  2023-07-07 10:44 ` [Bug middle-end/107443] [11/12/13/14 " rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-03-24  8:42 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

* [Bug middle-end/107443] [11/12/13/14 Regression] Removing switch with __builtin_unreachable causes missed optimizations
  2022-10-27 18:52 [Bug middle-end/107443] New: Switch conversion removing code amacleod at redhat dot com
                   ` (2 preceding siblings ...)
  2023-03-24  8:42 ` rguenth at gcc dot gnu.org
@ 2023-07-07 10:44 ` rguenth at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-07 10:44 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.5                        |11.5

--- Comment #3 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 10 branch is being closed.

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

end of thread, other threads:[~2023-07-07 10:44 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-27 18:52 [Bug middle-end/107443] New: Switch conversion removing code amacleod at redhat dot com
2022-10-27 23:34 ` [Bug middle-end/107443] [10/11/12/13 Regression] Removing switch with __builtin_unreachable causes missed optimizations pinskia at gcc dot gnu.org
2022-10-28 11:57 ` rguenth at gcc dot gnu.org
2023-03-24  8:42 ` rguenth at gcc dot gnu.org
2023-07-07 10:44 ` [Bug middle-end/107443] [11/12/13/14 " rguenth 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).