public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/109845] New: Addition overflow/carry flag unnecessarily put in a temporary register
@ 2023-05-13 18:32 chfast at gmail dot com
  2023-05-13 18:39 ` [Bug rtl-optimization/109845] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: chfast at gmail dot com @ 2023-05-13 18:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109845
           Summary: Addition overflow/carry flag unnecessarily put in a
                    temporary register
           Product: gcc
           Version: 13.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: chfast at gmail dot com
  Target Milestone: ---

When we have an addition and an overflow check and the overflow flag is
combined with some other condition the codegen may generate variant when the
overflow flag is temporary register.

    unsigned s = y + z;
    _Bool ov = s < y;

    if (x || ov) 
        return;

This produces

        add     esi, edx
        setc    al
        test    edi, edi
        jne     .L1
        test    eax, eax
        jne     .L1

while it could be

        add     esi, edx
        jc      .L6
        test    edi, edi
        jne     .L6


There are easy workaround to the C code which make the assembly optimal:

1. Change the order of checks 
    if (ov || x)

2. Split if into two
    if (x)
        return;
    if (ov) 
        return;

https://godbolt.org/z/rxsrnhPdc

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

end of thread, other threads:[~2023-05-15  6:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-13 18:32 [Bug rtl-optimization/109845] New: Addition overflow/carry flag unnecessarily put in a temporary register chfast at gmail dot com
2023-05-13 18:39 ` [Bug rtl-optimization/109845] " pinskia at gcc dot gnu.org
2023-05-13 18:45 ` [Bug middle-end/109845] " pinskia at gcc dot gnu.org
2023-05-15  6:52 ` 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).