public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "goon.pri.low at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/112645] New: missed-optimization: cswitch optimization missed in nested if-statement
Date: Tue, 21 Nov 2023 01:21:18 +0000	[thread overview]
Message-ID: <bug-112645-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 112645
           Summary: missed-optimization: cswitch optimization missed in
                    nested if-statement
           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: ---

These two functions should theoretically generate the same code, though the
second one uses a constant array.

int a(int v) {
    switch (v & 2) {
    case 0: //0x
        switch(v & 1) {
        case 0: //00
            return 643;
        case 1: //01
            return 223;
        }
    case 2: //1x
        switch (v & 1) {
        case 0: //10
            return 444;
        case 1: //11
            return 532;
        }
    }
}

a:
        test    dil, 2
        je      .L7
        and     edi, 1
        cmp     edi, 1
        sbb     eax, eax
        and     eax, -88
        add     eax, 532
        ret
.L7:
        and     edi, 1
        cmp     edi, 1
        sbb     eax, eax
        and     eax, 420
        add     eax, 223
        ret

int b(int v) {
    switch (v & 3) {
    case 0: //00
        return 643;
    case 1: //01
        return 223;
    case 2: //10
        return 444;
    case 3: //11
        return 532;
    }
}

b:
        and     edi, 3
        mov     eax, 643
        sub     edi, 1
        cmp     edi, 2
        ja      .L8
        mov     eax, DWORD PTR CSWTCH.2[0+rdi*4]
.L8:
        ret
CSWTCH.2:
        .long   223
        .long   444
        .long   532

Additionally while testing this, I found this function which should just use a
simple binary and

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

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

though has an unnecessary comparison and conditional move?

             reply	other threads:[~2023-11-21  1:21 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-21  1:21 goon.pri.low at gmail dot com [this message]
2023-11-21  8:23 ` [Bug tree-optimization/112645] " rguenth at gcc dot gnu.org
2023-11-23 21:32 ` goon.pri.low at gmail dot com

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