public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/110492] New: Attempted optimization of switch statement pessimizes it instead
@ 2023-06-30  2:52 qufanat at gmail dot com
  2023-06-30  3:27 ` [Bug c++/110492] " pinskia at gcc dot gnu.org
  2023-06-30  7:16 ` [Bug middle-end/110492] " rguenth at gcc dot gnu.org
  0 siblings, 2 replies; 3+ messages in thread
From: qufanat at gmail dot com @ 2023-06-30  2:52 UTC (permalink / raw)
  To: gcc-bugs

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

            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 binary
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 with
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 == and <=
branches.

This isn't that important to my program but I thought you'd like to know.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug c++/110492] Attempted optimization of switch statement pessimizes it instead
  2023-06-30  2:52 [Bug c++/110492] New: Attempted optimization of switch statement pessimizes it instead qufanat at gmail dot com
@ 2023-06-30  3:27 ` pinskia at gcc dot gnu.org
  2023-06-30  7:16 ` [Bug middle-end/110492] " rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-06-30  3:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55431
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55431&action=edit
testcase

Next time please attach or paste inline the code instead of posting just a link
to godbolt.org .

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [Bug middle-end/110492] Attempted optimization of switch statement pessimizes it instead
  2023-06-30  2:52 [Bug c++/110492] New: Attempted optimization of switch statement pessimizes it instead qufanat at gmail dot com
  2023-06-30  3:27 ` [Bug c++/110492] " pinskia at gcc dot gnu.org
@ 2023-06-30  7:16 ` rguenth at gcc dot gnu.org
  1 sibling, 0 replies; 3+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-06-30  7:16 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-06-30
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
On GIMPLE there are exactly the same number of compare-and-branch stmts

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-06-30  7:16 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-30  2:52 [Bug c++/110492] New: Attempted optimization of switch statement pessimizes it instead qufanat at gmail dot com
2023-06-30  3:27 ` [Bug c++/110492] " pinskia at gcc dot gnu.org
2023-06-30  7:16 ` [Bug middle-end/110492] " rguenth at gcc dot gnu.org

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