public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals
@ 2023-04-19 12:39 i.nixman at autistici dot org
  2023-04-19 12:42 ` [Bug middle-end/109555] " pinskia at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: i.nixman at autistici dot org @ 2023-04-19 12:39 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109555
           Summary: suboptimal code for comparing strings with string
                    literals
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: i.nixman at autistici dot org
  Target Milestone: ---

hello!


the example:

#include <cstring>
int main(int argc, char **argv) {
    return std::memcmp(argv[0], "MCRYF", 5) == 0;
}


this code compiled with GCC (-std=c++11 -O2) will be translated as the
following assembler code (https://godbolt.org/z/zzjaGdWdT):

main:
        mov     rax, QWORD PTR [rsi]
        cmp     DWORD PTR [rax], 1498563405
        je      .L5
.L2:
        mov     eax, 1
.L3:
        xor     eax, 1
        ret
.L5:
        cmp     BYTE PTR [rax+4], 70
        jne     .L2
        xor     eax, eax
        jmp     .L3



but if the code is compiled using CLang, we get a more optimal branch-less
assembler code (https://godbolt.org/z/E9o1oMzra):

main:                                   # @main
        mov     rax, qword ptr [rsi]
        mov     ecx, 1498563405
        xor     ecx, dword ptr [rax]
        movzx   edx, byte ptr [rax + 4]
        xor     edx, 70
        xor     eax, eax
        or      edx, ecx
        sete    al
        ret



maybe it's not a difficult fix, and maybe someone will see the point to improve
this...


thanks!

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

* [Bug middle-end/109555] suboptimal code for comparing strings with string literals
  2023-04-19 12:39 [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals i.nixman at autistici dot org
@ 2023-04-19 12:42 ` pinskia at gcc dot gnu.org
  2023-04-19 12:45 ` i.nixman at autistici dot org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-19 12:42 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note branchless might be worse. Anyways there is a duplicate of this bug
already. I think I filed it.

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

* [Bug middle-end/109555] suboptimal code for comparing strings with string literals
  2023-04-19 12:39 [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals i.nixman at autistici dot org
  2023-04-19 12:42 ` [Bug middle-end/109555] " pinskia at gcc dot gnu.org
@ 2023-04-19 12:45 ` i.nixman at autistici dot org
  2023-04-19 12:49 ` pinskia at gcc dot gnu.org
  2023-04-19 15:58 ` [Bug middle-end/109555] memcmp with string literals cause branchy code maybe could use branchless? pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: i.nixman at autistici dot org @ 2023-04-19 12:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from niXman <i.nixman at autistici dot org> ---
(In reply to Andrew Pinski from comment #1)
> Note branchless might be worse.

really?
could you explain please?



> Anyways there is a duplicate of this bug
> already. I think I filed it.

thank you!

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

* [Bug middle-end/109555] suboptimal code for comparing strings with string literals
  2023-04-19 12:39 [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals i.nixman at autistici dot org
  2023-04-19 12:42 ` [Bug middle-end/109555] " pinskia at gcc dot gnu.org
  2023-04-19 12:45 ` i.nixman at autistici dot org
@ 2023-04-19 12:49 ` pinskia at gcc dot gnu.org
  2023-04-19 15:58 ` [Bug middle-end/109555] memcmp with string literals cause branchy code maybe could use branchless? pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-19 12:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to niXman from comment #2)
> (In reply to Andrew Pinski from comment #1)
> > Note branchless might be worse.
> 
> really?
> could you explain please?

Yes cache misses. That is the second load might miss the cache.
Plus the non reduced case will most likely have a branch on the result so in
the end, it would just do unnecessary things if it is different.


> 
> 
> 
> > Anyways there is a duplicate of this bug
> > already. I think I filed it.
> 
> thank you!

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

* [Bug middle-end/109555] memcmp with string literals cause branchy code maybe could use branchless?
  2023-04-19 12:39 [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals i.nixman at autistici dot org
                   ` (2 preceding siblings ...)
  2023-04-19 12:49 ` pinskia at gcc dot gnu.org
@ 2023-04-19 15:58 ` pinskia at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-19 15:58 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
            Summary|suboptimal code for         |memcmp with string literals
                   |comparing strings with      |cause branchy code maybe
                   |string literals             |could use branchless?

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

end of thread, other threads:[~2023-04-19 15:58 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-19 12:39 [Bug middle-end/109555] New: suboptimal code for comparing strings with string literals i.nixman at autistici dot org
2023-04-19 12:42 ` [Bug middle-end/109555] " pinskia at gcc dot gnu.org
2023-04-19 12:45 ` i.nixman at autistici dot org
2023-04-19 12:49 ` pinskia at gcc dot gnu.org
2023-04-19 15:58 ` [Bug middle-end/109555] memcmp with string literals cause branchy code maybe could use branchless? pinskia 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).