public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/112687] New: missed-optimization: switch statement does not simplify to it's expression
@ 2023-11-23 21:43 goon.pri.low at gmail dot com
  2023-11-24  8:04 ` [Bug tree-optimization/112687] " rguenth at gcc dot gnu.org
  2024-01-26 11:59 ` fkastl at suse dot cz
  0 siblings, 2 replies; 3+ messages in thread
From: goon.pri.low at gmail dot com @ 2023-11-23 21:43 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112687
           Summary: missed-optimization: switch statement does not
                    simplify to it's expression
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: goon.pri.low at gmail dot com
  Target Milestone: ---

This following code:
int unopt(int v) {
    switch (v & 3) {
        case 0: return 0;
        case 1: return 1;
        case 2: return 2;
        case 3: return 3;
        default: __builtin_unreachable();
    }
}

unopt:
        mov     eax, edi
        and     eax, 3
        lea     edx, [rax-1]
        cmp     edx, 3
        mov     edx, 0
        cmovnb  eax, edx
        ret

Ought to be optimized to:
int opt(int v) {
    return v & 3;
}

opt:
        mov     eax, edi
        and     eax, 3
        ret

Note: I decided to separate this problem from
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=112645 since it was unrelated to
the main bug, though Richard offers some initial insight into the problem.

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

* [Bug tree-optimization/112687] missed-optimization: switch statement does not simplify to it's expression
  2023-11-23 21:43 [Bug tree-optimization/112687] New: missed-optimization: switch statement does not simplify to it's expression goon.pri.low at gmail dot com
@ 2023-11-24  8:04 ` rguenth at gcc dot gnu.org
  2024-01-26 11:59 ` fkastl at suse dot cz
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-11-24  8:04 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2023-11-24
             Status|UNCONFIRMED                 |NEW
                 CC|                            |fkastl at suse dot cz

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.

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

* [Bug tree-optimization/112687] missed-optimization: switch statement does not simplify to it's expression
  2023-11-23 21:43 [Bug tree-optimization/112687] New: missed-optimization: switch statement does not simplify to it's expression goon.pri.low at gmail dot com
  2023-11-24  8:04 ` [Bug tree-optimization/112687] " rguenth at gcc dot gnu.org
@ 2024-01-26 11:59 ` fkastl at suse dot cz
  1 sibling, 0 replies; 3+ messages in thread
From: fkastl at suse dot cz @ 2024-01-26 11:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Filip Kastl <fkastl at suse dot cz> ---
Created attachment 57222
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=57222&action=edit
WIP patch to fix the missed optimization, version 0

I'm working on a patch. The problem (as Richard stated in pr112645) is that the
testcase gets optimized into

switch (v & 3) {
    default:
        return 0;
    case 1:
        return 1;
    case 2:
        return 2;
    case 3:
        return 3;
    }

so the information that there are only 4 possible values of the expression 'v &
3' is lost.

I add capability for switch conversion to recover from this. With my patch,
before processing the switch, switch conversion finds out if the default case
only corresponds to one possible value of the index expression. If so, it
creates a case for this value and marks default as unreachable.

With the patch applied, functions opt() and unopt() compile into the same
sequence of assembly instructions. I've attached the patch. Do you think this
is a good approach? I'll also appreciate any suggestions/comments for the
patch.

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

end of thread, other threads:[~2024-01-26 11:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-23 21:43 [Bug tree-optimization/112687] New: missed-optimization: switch statement does not simplify to it's expression goon.pri.low at gmail dot com
2023-11-24  8:04 ` [Bug tree-optimization/112687] " rguenth at gcc dot gnu.org
2024-01-26 11:59 ` fkastl at suse dot cz

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