public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "jzwinck at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/108406] New: Missed integer optimization on x86-64 unless -fwrapv is used
Date: Sat, 14 Jan 2023 10:26:21 +0000	[thread overview]
Message-ID: <bug-108406-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 108406
           Summary: Missed integer optimization on x86-64 unless -fwrapv
                    is used
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jzwinck at gmail dot com
  Target Milestone: ---

Consider this C++ code:

    #include <cstdint>

    // returns a if less than b or if b is INT32_MIN
    int32_t special_min(int32_t a, int32_t b)
    {
        return a < b || b == INT32_MIN ? a : b;
    }

GCC with -fwrapv correctly realizes that subtracting 1 from b can eliminate the
special case, and it generates this code for x86-64:

    lea     edx, [rsi-1]
    mov     eax, edi
    cmp     edi, edx
    cmovg   eax, esi
    ret

But without -fwrapv it generates worse code:

    mov     eax, esi
    cmp     edi, esi
    jl      .L4
    cmp     esi, -2147483648
    je      .L4
    ret
    .L4:
    mov     eax, edi
    ret

If I wrote "hand optimized" C++ code trying to implement that optimization, I
understand -fwrapv would be required, otherwise the compiler could decide the
signed overflow is UB. But here the compiler is in control, it knows the
behavior of integer overflow on x86-64, and so it should not matter whether
-fwrapv is used.

Demo: https://godbolt.org/z/o881Mdqoa

Stack Overflow discussion:
https://stackoverflow.com/questions/75110108/gcc-wont-use-its-own-optimization-trick-without-fwrapv

This is somewhat related to #102032 in the sense that it's an optimization
missed without -fwrapv, but the type of optimization is different.  It is
possible there's a single solution that would solve both problems (and others).

             reply	other threads:[~2023-01-14 10:26 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-14 10:26 jzwinck at gmail dot com [this message]
2023-01-16  7:54 ` [Bug tree-optimization/108406] " 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-108406-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).