public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug regression/105126] New: Optimization regression gcc inserts not needed movsx when using switch statement
@ 2022-04-01 13:42 andre.schackier at gmail dot com
  2022-04-01 15:39 ` [Bug regression/105126] " marxin at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: andre.schackier at gmail dot com @ 2022-04-01 13:42 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 105126
           Summary: Optimization regression gcc inserts not needed movsx
                    when using switch statement
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: regression
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andre.schackier at gmail dot com
  Target Milestone: ---

Given the following source code:

```cpp
bool is_bin_0(const char c) { return (c == '0' || c == '1'); }

bool is_bin_1(const char c) {
    switch (c) {
        case '0':
        case '1':
            return true;

        default:
            return false;
    }
}

```

compiling with `-O3` gives the following output:

```asm
is_bin_0(char):
        sub     edi, 48
        cmp     dil, 1
        setbe   al
        ret
is_bin_1(char):
        movsx   edi, dil
        sub     edi, 48
        cmp     edi, 1
        setbe   al
        ret
```

The version using a switch generates an extra movsx instruction which, as far
is I understand the x86 CPU registers is not needed since DIL is the lower 8
bits of EDI anyways, but correct me if I'm wrong there.

This also seems to be a regression introduced with GCC-8.1 since GCC-4.5 to
GCC-7.5 generate the same assembly as for the first and second function and all
version after that including trunk produce different outputs.

[link to godbolt](https://godbolt.org/z/r3vhYbK6o)

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

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

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-04-01 13:42 [Bug regression/105126] New: Optimization regression gcc inserts not needed movsx when using switch statement andre.schackier at gmail dot com
2022-04-01 15:39 ` [Bug regression/105126] " marxin at gcc dot gnu.org
2022-04-04  7:11 ` [Bug middle-end/105126] [9/10/11/12 Regression] " rguenth at gcc dot gnu.org
2022-04-20 10:18 ` jakub at gcc dot gnu.org
2022-05-27  9:47 ` [Bug middle-end/105126] [10/11/12/13 " rguenth at gcc dot gnu.org
2022-06-28 10:48 ` jakub at gcc dot gnu.org
2022-11-28 18:21 ` pinskia at gcc dot gnu.org
2023-01-11 12:36 ` marxin at gcc dot gnu.org
2023-07-07 10:42 ` [Bug middle-end/105126] [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).