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)

Reply via email to