public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cassio.neri at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/107564] New: Fail to recognize overflow check for addition of __uint128_t operands
Date: Tue, 08 Nov 2022 00:00:40 +0000	[thread overview]
Message-ID: <bug-107564-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 107564
           Summary: Fail to recognize overflow check for addition of
                    __uint128_t operands
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: cassio.neri at gmail dot com
  Target Milestone: ---

Consider:

char f128(__uint128_t m, __uint128_t n) {
    #if !defined(USE_BUILTIN_ADD_OVERFLOW)
        m += n;
        return m < n;
    #else
        __uint128_t r;
        return __builtin_add_overflow(m, n, &r);
    #endif
}

When USE_BUILTIN_ADD_OVERFLOW is undefined, GCC fails to recognise this is an
overflow check and with -O3 generates this:

        mov     r8, rdi
        mov     rax, rsi
        mov     rdi, rax
        mov     rsi, r8
        mov     rax, rdx
        mov     rdx, rcx
        add     rsi, rax
        adc     rdi, rcx
        cmp     rsi, rax
        mov     rcx, rdi
        sbb     rcx, rdx
        setc    al
        ret

When USE_BUILTIN_ADD_OVERFLOW is defined, it generates better code but still
suboptimal:

        mov     r8, rdi
        mov     rax, rsi
        mov     rsi, r8
        mov     rdi, rax
        add     rsi, rdx
        adc     rdi, rcx
        setc    al
        ret

For other unsigned integer types GCC generates the same optimal code for both
methods. For instance for uint64_t:

        add     rdi, rsi
        setc    al
        ret

https://godbolt.org/z/bj4M5no4j

             reply	other threads:[~2022-11-08  0:00 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-08  0:00 cassio.neri at gmail dot com [this message]
2022-11-08  0:07 ` [Bug tree-optimization/107564] " pinskia at gcc dot gnu.org
2022-11-08  9:38 ` [Bug target/107564] " 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-107564-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).