From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id 5B2A63858D39; Fri, 30 Jun 2023 02:52:23 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5B2A63858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688093543; bh=pxFNzyppQVk0QIXURFNSv7M7aZf62X67bNCJ6qpMy5Y=; h=From:To:Subject:Date:From; b=NB5ZDz5GOnpp3xlNLUIKibdZW9LA2jTMYPK01RAh/4HZUzBjiq+nmmd8U25b0PniY 7qvYiSa9tVYTTStX6XEosmC7Nnt0vLar2NJoWzB+03SINDtrQ0oR6bOlwcnDq9nT8z stK6p6TYo66YzrAxUB3vSCZTvcnTts5pQuVqF6s0= From: "qufanat at gmail dot com" To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/110492] New: Attempted optimization of switch statement pessimizes it instead Date: Fri, 30 Jun 2023 02:52:22 +0000 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: c++ X-Bugzilla-Version: 13.1.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: qufanat 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=3D110492 Bug ID: 110492 Summary: Attempted optimization of switch statement pessimizes it instead Product: gcc Version: 13.1.0 Status: UNCONFIRMED Severity: normal Priority: P3 Component: c++ Assignee: unassigned at gcc dot gnu.org Reporter: qufanat at gmail dot com Target Milestone: --- This happens on my local GCC 11.3, but you can also see it on 13.1 at this godbolt link https://godbolt.org/z/G3qecWxPr I'm creating a switch statement of hashed strings, which compiles to a bina= ry search on the hashes, all well and good. However, with -O3 specified, GCC peels back the last multiplication of the hash for some of the comparison branches, but is unable to do it for others, resulting in longer assembly w= ith twice as many comparisons as is necessary. Here is lines 15..19 in get_choice_1() (end of the hash loop) imul esi, eax, 16777619 test dl, dl jne .L3 (start of the switch) cmp eax, 1954414351 je .L8 cmp esi, 1901626525 ja .L4 eax is the hash without the last imul, and esi is the final hash. If we prevent inlining of the hash function, the compiler can't make this "optimization" and gives the assembly I expect. Here is lines 90..93 in get_choice_2() call hash32_noinline(char const*) cmp eax, 1901626525 je .L33 jbe .L46 Now it only does one comparison per entry and uses it for both the =3D=3D a= nd <=3D branches. This isn't that important to my program but I thought you'd like to know.=