public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/108406] New: Missed integer optimization on x86-64 unless -fwrapv is used
@ 2023-01-14 10:26 jzwinck at gmail dot com
  2023-01-16  7:54 ` [Bug tree-optimization/108406] " rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: jzwinck at gmail dot com @ 2023-01-14 10:26 UTC (permalink / raw)
  To: gcc-bugs

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).

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

* [Bug tree-optimization/108406] Missed integer optimization on x86-64 unless -fwrapv is used
  2023-01-14 10:26 [Bug tree-optimization/108406] New: Missed integer optimization on x86-64 unless -fwrapv is used jzwinck at gmail dot com
@ 2023-01-16  7:54 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-01-16  7:54 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2023-01-16

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed.  With -fwrapv

/* y == XXX_MIN || x < y --> x <= y - 1 */
(simplify
 (bit_ior:c (eq:s @1 min_value) (lt:cs @0 @1))
  (if (INTEGRAL_TYPE_P (TREE_TYPE (@1))
       && TYPE_OVERFLOW_WRAPS (TREE_TYPE (@1)))
  (le @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))

triggers.  We could with !TYPE_OVERFLOW_WRAPS write the
subtraction in an unsigned type to make it well-defined.

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

end of thread, other threads:[~2023-01-16  7:54 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-14 10:26 [Bug tree-optimization/108406] New: Missed integer optimization on x86-64 unless -fwrapv is used jzwinck at gmail dot com
2023-01-16  7:54 ` [Bug tree-optimization/108406] " 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).