public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP
@ 2023-05-12 13:23 antoshkka at gmail dot com
  2023-05-12 18:16 ` [Bug tree-optimization/109829] " pinskia at gcc dot gnu.org
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: antoshkka at gmail dot com @ 2023-05-12 13:23 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 109829
           Summary: Optimizing __builtin_signbit(x) ? -x : x or abs for FP
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: antoshkka at gmail dot com
  Target Milestone: ---

Consider the following 2 functions:

__float128 abs1(__float128 x) { return __builtin_fabsf128(x); }
__float128 abs2(__float128 x) { return __builtin_signbit(x) ? -x : x; }

They should provide the same results, however the codegen is different:

abs1(__float128):
        pand    xmm0, XMMWORD PTR .LC0[rip]
        ret
abs2(__float128):
        movmskps        eax, xmm0
        test    al, 8
        je      .L4
        pxor    xmm0, XMMWORD PTR .LC1[rip]
.L4:
        ret


Looks like match.pd miss the __builtin_signbit(x) ? -x : x ->
__builtin_fabs*(x) pattern.

Playground: https://godbolt.org/z/bsxeozGqv

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
@ 2023-05-12 18:16 ` pinskia at gcc dot gnu.org
  2023-05-12 19:25 ` pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-12 18:16 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
   Last reconfirmed|                            |2023-05-12
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
From phiopt1:
phiopt match-simplify trying:
        _1 != 0 ? iftmp.0_5 : x_3(D)

Where _1 is defined as:
  _1 = signbitD.1026 (x_3(D));
and iftmp.0_5 is defined as:
  iftmp.0_5 = -x_3(D);

This should fix it then:
(simplify
 (cond (ne (SIGNBIT @0) zero_p@1) (neg @0) @0)
 (abs @0))

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
  2023-05-12 18:16 ` [Bug tree-optimization/109829] " pinskia at gcc dot gnu.org
@ 2023-05-12 19:25 ` pinskia at gcc dot gnu.org
  2023-05-12 19:34 ` pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-12 19:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |pinskia at gcc dot gnu.org

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
I will implement this since it is only 3 lines or so. Just need some testcases.

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
  2023-05-12 18:16 ` [Bug tree-optimization/109829] " pinskia at gcc dot gnu.org
  2023-05-12 19:25 ` pinskia at gcc dot gnu.org
@ 2023-05-12 19:34 ` pinskia at gcc dot gnu.org
  2023-05-12 20:02 ` jakub at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-12 19:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Should add this one too:

(simplify
 (cond (ne (SIGNBIT @0) zero_p@1) @0 (neg @0))
 (neg (abs @0)))

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
                   ` (2 preceding siblings ...)
  2023-05-12 19:34 ` pinskia at gcc dot gnu.org
@ 2023-05-12 20:02 ` jakub at gcc dot gnu.org
  2023-05-12 20:03 ` jakub at gcc dot gnu.org
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-12 20:02 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #4 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Don't you need also
(simplify
 (cond (eq (SIGNBIT @0) zero_p@1) (neg @0) @0)
 (neg (abs @0)))
(simplify
 (cond (eq (SIGNBIT @0) zero_p@1) @0 (neg @0))
 (abs @0))
?
Or does match.pd try to invert all the COND_EXPR conditions and swap the
operands?

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
                   ` (3 preceding siblings ...)
  2023-05-12 20:02 ` jakub at gcc dot gnu.org
@ 2023-05-12 20:03 ` jakub at gcc dot gnu.org
  2023-05-12 20:18 ` pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2023-05-12 20:03 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Or canonicalize COND_EXPRs such that only ne and not eq appears in condition
(and similarly to other comparisons)?

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
                   ` (4 preceding siblings ...)
  2023-05-12 20:03 ` jakub at gcc dot gnu.org
@ 2023-05-12 20:18 ` pinskia at gcc dot gnu.org
  2023-05-13 15:53 ` pinskia at gcc dot gnu.org
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-12 20:18 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Jakub Jelinek from comment #4)
> Or does match.pd try to invert all the COND_EXPR conditions and swap the
> operands?

match does not try but phiopt does in gimple_simplify_phiopt .

Or even something like this would work better. 
(for neeq (ne eq)
 (simplify
  (cond (neeq (SIGNBIT @0) zero_p@1) (neg @0) @0)
   (if (neqeq == NE_EXPR)
    (abs @0)
    (neg (abs @0))
   )
  )
 )
)

I need to double check if == 1 will show up here though.

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
                   ` (5 preceding siblings ...)
  2023-05-12 20:18 ` pinskia at gcc dot gnu.org
@ 2023-05-13 15:53 ` pinskia at gcc dot gnu.org
  2023-05-13 22:29 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-13 15:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #6)
> I need to double check if == 1 will show up here though.

The only thing we know about __builtin_signbit is that is 0 or non-zero. The
exact value is unspecified for the non-zero case. So we can only optimize the
!=/== 0 cases.

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
                   ` (6 preceding siblings ...)
  2023-05-13 15:53 ` pinskia at gcc dot gnu.org
@ 2023-05-13 22:29 ` pinskia at gcc dot gnu.org
  2023-05-14  4:04 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-13 22:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 55079
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=55079&action=edit
Patch which I am testing

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
                   ` (7 preceding siblings ...)
  2023-05-13 22:29 ` pinskia at gcc dot gnu.org
@ 2023-05-14  4:04 ` pinskia at gcc dot gnu.org
  2023-05-14 23:08 ` cvs-commit at gcc dot gnu.org
  2023-05-14 23:09 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-14  4:04 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch
                URL|                            |https://gcc.gnu.org/piperma
                   |                            |il/gcc-patches/2023-May/618
                   |                            |435.html

--- Comment #9 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Patch submitted:
https://gcc.gnu.org/pipermail/gcc-patches/2023-May/618435.html

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
                   ` (8 preceding siblings ...)
  2023-05-14  4:04 ` pinskia at gcc dot gnu.org
@ 2023-05-14 23:08 ` cvs-commit at gcc dot gnu.org
  2023-05-14 23:09 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-05-14 23:08 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #10 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:82502b5c3463bde98d4b7ffb9ecef9b123799ed1

commit r14-813-g82502b5c3463bde98d4b7ffb9ecef9b123799ed1
Author: Andrew Pinski <apinski@marvell.com>
Date:   Sat May 13 22:25:21 2023 +0000

    MATCH: Add pattern for `signbit(x) ? x : -x` into abs (and swapped)

    This adds a simple pattern to match.pd for `signbit(x) ? x : -x`
    into abs<x>. This can be done for all types even ones that honor
    signed zeros and NaNs because both signbit and - are considered
    only looking at/touching the sign bit of those types and does
    not trap either.

    OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

            PR tree-optimization/109829

    gcc/ChangeLog:

            * match.pd: Add pattern for `signbit(x) !=/== 0 ? x : -x`.

    gcc/testsuite/ChangeLog:

            * gcc.dg/tree-ssa/abs-3.c: New test.
            * gcc.dg/tree-ssa/abs-4.c: New test.

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

* [Bug tree-optimization/109829] Optimizing __builtin_signbit(x) ? -x : x or abs for FP
  2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
                   ` (9 preceding siblings ...)
  2023-05-14 23:08 ` cvs-commit at gcc dot gnu.org
@ 2023-05-14 23:09 ` pinskia at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-14 23:09 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
   Target Milestone|---                         |14.0
             Status|ASSIGNED                    |RESOLVED

--- Comment #11 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2023-05-14 23:09 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-12 13:23 [Bug tree-optimization/109829] New: Optimizing __builtin_signbit(x) ? -x : x or abs for FP antoshkka at gmail dot com
2023-05-12 18:16 ` [Bug tree-optimization/109829] " pinskia at gcc dot gnu.org
2023-05-12 19:25 ` pinskia at gcc dot gnu.org
2023-05-12 19:34 ` pinskia at gcc dot gnu.org
2023-05-12 20:02 ` jakub at gcc dot gnu.org
2023-05-12 20:03 ` jakub at gcc dot gnu.org
2023-05-12 20:18 ` pinskia at gcc dot gnu.org
2023-05-13 15:53 ` pinskia at gcc dot gnu.org
2023-05-13 22:29 ` pinskia at gcc dot gnu.org
2023-05-14  4:04 ` pinskia at gcc dot gnu.org
2023-05-14 23:08 ` cvs-commit at gcc dot gnu.org
2023-05-14 23:09 ` 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).