public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "chfast at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug rtl-optimization/109845] New: Addition overflow/carry flag unnecessarily put in a temporary register
Date: Sat, 13 May 2023 18:32:00 +0000	[thread overview]
Message-ID: <bug-109845-4@http.gcc.gnu.org/bugzilla/> (raw)

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

             reply	other threads:[~2023-05-13 18:32 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-13 18:32 chfast at gmail dot com [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-109845-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).