public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "andre.schackier at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug regression/105126] New: Optimization regression gcc inserts not needed movsx when using switch statement
Date: Fri, 01 Apr 2022 13:42:13 +0000	[thread overview]
Message-ID: <bug-105126-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2022-04-01 13:42 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-01 13:42 andre.schackier at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-105126-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).