public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/94174] New: __builtin_add_overflow vs ccmp
@ 2020-03-14  9:48 rth at gcc dot gnu.org
  2020-03-14  9:51 ` [Bug target/94174] Missed ccmp optimizations rth at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: rth at gcc dot gnu.org @ 2020-03-14  9:48 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 94174
           Summary: __builtin_add_overflow vs ccmp
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: rth at gcc dot gnu.org
  Target Milestone: ---

(1) Case 1:

void doit(void);
void test(unsigned long a, unsigned long l)
{
  if (!__builtin_add_overflow(a, 8 - 1, &a) && a <= l)
    doit();
}

currently generates as

        adds    x0, x0, #7
        cset    x2, cs
        cmp     x0, x1
        eor     w2, w2, 1
        cset    w0, ls
        tst     w0, w2
        bne     .L22

but could be

        adds    x2, x0, #7
        ccmp    x2, x1, #0, cc
        b.ls    .L22

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

* [Bug target/94174] Missed ccmp optimizations
  2020-03-14  9:48 [Bug target/94174] New: __builtin_add_overflow vs ccmp rth at gcc dot gnu.org
@ 2020-03-14  9:51 ` rth at gcc dot gnu.org
  2020-03-14 10:20 ` rth at gcc dot gnu.org
  2021-10-02 20:25 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rth at gcc dot gnu.org @ 2020-03-14  9:51 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Henderson <rth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|__builtin_add_overflow vs   |Missed ccmp optimizations
                   |ccmp                        |
             Target|                            |aarch64-*

--- Comment #1 from Richard Henderson <rth at gcc dot gnu.org> ---
Case 2:

void test2(unsigned long a, unsigned long l)
{
  if (l + 1 == 0 || a <= l + 1)
    doit();
}

currently generates as

        cmn     x1, #1
        beq     .L13
        add     x1, x1, 1
        cmp     x1, x0
        bcc     .L12

but could be

        adds    x2, x1, #1
        ccmp    x0, x2, #0, ne
        b.hi    .L12

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

* [Bug target/94174] Missed ccmp optimizations
  2020-03-14  9:48 [Bug target/94174] New: __builtin_add_overflow vs ccmp rth at gcc dot gnu.org
  2020-03-14  9:51 ` [Bug target/94174] Missed ccmp optimizations rth at gcc dot gnu.org
@ 2020-03-14 10:20 ` rth at gcc dot gnu.org
  2021-10-02 20:25 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: rth at gcc dot gnu.org @ 2020-03-14 10:20 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Henderson <rth at gcc dot gnu.org> ---
Case 3:

void test3(__int128 a, unsigned long l)
{
  if ((__int128_t)a - l <= 1)
    doit(); 
}

currently generates as

        subs    x0, x0, x2
        sbc     x1, x1, xzr
        cmp     x1, 0
        ble     .L11
.L7:
        ret
.L11:
        bne     .L10
        cmp     x0, 1
        bhi     .L7
.L10:
        b       doit

but at least the bne + cmp can be 

        ccmp     x0, 1, #2, eq

Note that clang attempts a branchless double-word comparison

        subs    x8, x0, x2
        sbcs    x9, x1, xzr
        cmp     x8, #1
        cset    w8, hi
        cmp     x9, #0
        cset    w9, gt
        csel    w8, w8, w9, eq
        tbnz    w8, #0, .LBB0_2

we can do better than that:

        subs    x8, x0, x2
        sbcs    x9, x1, xzr
        // x9 < 0 || (x9 == 0 && x8 <= 1)
        cset    x10, lt
        ccmp    x8, #1, #2, ne    (nzCv: eq -> hi)
        ccmp    x10, #0, #4, hi   (nZcv: ls -> eq)
        b.eq    .L10

It's not 100% clear this is better than the 2 branch
version (with the ccmp), but at least it's no larger.

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

* [Bug target/94174] Missed ccmp optimizations
  2020-03-14  9:48 [Bug target/94174] New: __builtin_add_overflow vs ccmp rth at gcc dot gnu.org
  2020-03-14  9:51 ` [Bug target/94174] Missed ccmp optimizations rth at gcc dot gnu.org
  2020-03-14 10:20 ` rth at gcc dot gnu.org
@ 2021-10-02 20:25 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-10-02 20:25 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2021-10-02
                 CC|                            |pinskia at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed.

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

end of thread, other threads:[~2021-10-02 20:25 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-03-14  9:48 [Bug target/94174] New: __builtin_add_overflow vs ccmp rth at gcc dot gnu.org
2020-03-14  9:51 ` [Bug target/94174] Missed ccmp optimizations rth at gcc dot gnu.org
2020-03-14 10:20 ` rth at gcc dot gnu.org
2021-10-02 20:25 ` pinskia 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).