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

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