From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 513813858D37; Tue, 21 Nov 2023 01:21:19 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 513813858D37 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1700529679; bh=73lW4TNxvoh2O4Bs2eeHNFm0LElF7qDVhG8OVAzc5eY=; h=From:To:Subject:Date:From; b=l4oRp2A8fRRGkoqSnRZo1YMDcsdxnwIIvoSpqkwljT+NttQ4FGLhchMjtr/thuyRK QIGXp1rlZOYxy4I6xmVAr1UQB9Jw5922iN5/eNfa8FrsUN8+YzxitK2r2Sit+tIC3A /UtzOe+mdhfozGnxpD0iZldksXbvMAqN9b0rx9no= From: "goon.pri.low at gmail dot com" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: tree-optimization X-Bugzilla-Version: 13.2.1 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: goon.pri.low at gmail dot com X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D112645 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 us= e 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?=