public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/51954] New: __int128_t negation can be optimized
@ 2012-01-23  1:43 svfuerst at gmail dot com
  2012-01-23  4:00 ` [Bug rtl-optimization/51954] __int128_t (and long long on x86) " pinskia at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: svfuerst at gmail dot com @ 2012-01-23  1:43 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51954

             Bug #: 51954
           Summary: __int128_t negation can be optimized
    Classification: Unclassified
           Product: gcc
           Version: 4.7.0
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: rtl-optimization
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: svfuerst@gmail.com


__int128_t neg(__int128_t x)
{
    return -x;
}

Compiles into with -O3 :

mov    %rdi, %rax
mov    %rsi, %rdx
neg    %rax
adc    $0x0, %rdx
neg    %rdx
retq

Note how the last three instructions before the return are dependent on each
other.  This can be  slightly improved with slightly more inter-instruction
parallelism:

mov    %rdi, %rax
mov    %rsi, %rdx
neg    %rdx
neg    %rax
sbb    $0x0, %rdx
retq


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

* [Bug rtl-optimization/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
@ 2012-01-23  4:00 ` pinskia at gcc dot gnu.org
  2021-12-26 22:34 ` [Bug target/51954] " pinskia at gcc dot gnu.org
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2012-01-23  4:00 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51954

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Target|                            |x86_64-*-* i?86-*-*
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |missed-optimization
   Last reconfirmed|                            |2012-01-23
     Ever Confirmed|0                           |1
            Summary|__int128_t negation can be  |__int128_t (and long long
                   |optimized                   |on x86) negation can be
                   |                            |optimized

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> 2012-01-23 02:44:13 UTC ---
Confirmed, 64bit long long has a similar improvement for x86:
     movl    4(%esp), %eax
    movl    8(%esp), %edx
    negl    %eax
    adcl    $0, %edx
    negl    %edx


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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
  2012-01-23  4:00 ` [Bug rtl-optimization/51954] __int128_t (and long long on x86) " pinskia at gcc dot gnu.org
@ 2021-12-26 22:34 ` pinskia at gcc dot gnu.org
  2022-02-21  5:57 ` pinskia at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-26 22:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
We get this now:

        movq    %rdi, %rax
        movq    %rsi, %rdx
        negq    %rax
        adcq    $0, %rdx
        negq    %rdx

ICC produces:
        xorl      %edx, %edx                                    #3.10
        xorl      %eax, %eax                                    #3.10
        subq      %rdi, %rax                                    #3.10
        sbbq      %rsi, %rdx                                    #3.10
        ret                                                     #3.10

Which seems even better.

LLVM does produce:
        movq    %rdi, %rax
        xorl    %edx, %edx
        negq    %rax
        sbbq    %rsi, %rdx

Which basically combines the addcq and negq together and changes one mov into
an exor.

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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
  2012-01-23  4:00 ` [Bug rtl-optimization/51954] __int128_t (and long long on x86) " pinskia at gcc dot gnu.org
  2021-12-26 22:34 ` [Bug target/51954] " pinskia at gcc dot gnu.org
@ 2022-02-21  5:57 ` pinskia at gcc dot gnu.org
  2022-02-21  6:29 ` crazylht at gmail dot com
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-21  5:57 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |gabravier at gmail dot com

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
*** Bug 94806 has been marked as a duplicate of this bug. ***

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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
                   ` (2 preceding siblings ...)
  2022-02-21  5:57 ` pinskia at gcc dot gnu.org
@ 2022-02-21  6:29 ` crazylht at gmail dot com
  2022-02-21  6:49 ` crazylht at gmail dot com
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: crazylht at gmail dot com @ 2022-02-21  6:29 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Hongtao.liu <crazylht at gmail dot com> ---
(In reply to Andrew Pinski from comment #2)
> We get this now:
> 
>         movq    %rdi, %rax
>         movq    %rsi, %rdx
>         negq    %rax
>         adcq    $0, %rdx
>         negq    %rdx
> 
> ICC produces:
>         xorl      %edx, %edx                                    #3.10
>         xorl      %eax, %eax                                    #3.10
>         subq      %rdi, %rax                                    #3.10
>         sbbq      %rsi, %rdx                                    #3.10
>         ret                                                     #3.10
> 
> Which seems even better.
> 
> LLVM does produce:
>         movq    %rdi, %rax
>         xorl    %edx, %edx
>         negq    %rax
>         sbbq    %rsi, %rdx
> 
> Which basically combines the addcq and negq together and changes one mov
> into an exor.

Yes, neg rdx = (0 - rdx) = (0 - (rdx + CF)(last def)) = (sbb 0, rdx)

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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
                   ` (3 preceding siblings ...)
  2022-02-21  6:29 ` crazylht at gmail dot com
@ 2022-02-21  6:49 ` crazylht at gmail dot com
  2022-02-21  7:45 ` ubizjak at gmail dot com
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: crazylht at gmail dot com @ 2022-02-21  6:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Hongtao.liu <crazylht at gmail dot com> ---
> 
> Yes, neg rdx = (0 - rdx) = (0 - (rdx + CF)(last def)) = (sbb 0, rdx)

And we need an extra register for it.

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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
                   ` (4 preceding siblings ...)
  2022-02-21  6:49 ` crazylht at gmail dot com
@ 2022-02-21  7:45 ` ubizjak at gmail dot com
  2022-02-21  7:52 ` ubizjak at gmail dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: ubizjak at gmail dot com @ 2022-02-21  7:45 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Uroš Bizjak <ubizjak at gmail dot com> ---
Created attachment 52481
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=52481&action=edit
Proposed patch

+;; Convert:
+;;   mov %esi, %edx
+;;   negl %eax
+;;   adcl $0, %edx
+;;   negl %edx
+;; to:
+;;   xorl %edx, %edx
+;;   negl %eax
+;;   sbbl %esi, %edx

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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
                   ` (5 preceding siblings ...)
  2022-02-21  7:45 ` ubizjak at gmail dot com
@ 2022-02-21  7:52 ` ubizjak at gmail dot com
  2022-02-21  7:57 ` ubizjak at gmail dot com
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: ubizjak at gmail dot com @ 2022-02-21  7:52 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Uroš Bizjak <ubizjak at gmail dot com> ---
(In reply to Hongtao.liu from comment #5)
> > 
> > Yes, neg rdx = (0 - rdx) = (0 - (rdx + CF)(last def)) = (sbb 0, rdx)
> 
> And we need an extra register for it.

The patch in Comment #6 does a conversion, but only when a move is also found.
I have tried to peephole2 with a new temporary, but an additional move to a
temporary besides clearing xor was needed in that case. So the following
testcae:

--cut here--
#ifdef __x86_64__
#define TYPE __int128
#else
#define TYPE long long
#endif

TYPE bar (TYPE x)
{
  return -x;
}
--cut here--

when compiled with -O2 -m32 -mregparm=3 leaves unconverted the sequence:

        negl    %eax
        adcl    $0, %edx
        negl    %edx

but generates:

        movl    4(%esp), %eax
        xorl    %edx, %edx
        negl    %eax
        sbbl    8(%esp), %edx

without -mregparm.

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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
                   ` (6 preceding siblings ...)
  2022-02-21  7:52 ` ubizjak at gmail dot com
@ 2022-02-21  7:57 ` ubizjak at gmail dot com
  2022-04-29 11:31 ` cvs-commit at gcc dot gnu.org
  2022-04-29 11:33 ` ubizjak at gmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: ubizjak at gmail dot com @ 2022-02-21  7:57 UTC (permalink / raw)
  To: gcc-bugs

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

Uroš Bizjak <ubizjak at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |ASSIGNED
           Assignee|unassigned at gcc dot gnu.org      |ubizjak at gmail dot com
   Target Milestone|---                         |13.0

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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
                   ` (7 preceding siblings ...)
  2022-02-21  7:57 ` ubizjak at gmail dot com
@ 2022-04-29 11:31 ` cvs-commit at gcc dot gnu.org
  2022-04-29 11:33 ` ubizjak at gmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-04-29 11:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Uros Bizjak <uros@gcc.gnu.org>:

https://gcc.gnu.org/g:59119253b3133b30114194a04171f9d353b5c7f7

commit r13-38-g59119253b3133b30114194a04171f9d353b5c7f7
Author: Uros Bizjak <ubizjak@gmail.com>
Date:   Fri Apr 29 13:27:48 2022 +0200

    i386: Optimize double-word negation [PR51954]

    Introduce peephole2 pattern to convert from:

       mov %esi, %edx
       negl %eax
       adcl $0, %edx
       negl %edx
     to:
       xorl %edx, %edx
       negl %eax
       sbbl %esi, %edx

    This conversion is profitable only when initial move is found.  Otherwise,
    additional move to a temporary together with clearing xor is needed.

    2022-04-29  Uroš Bizjak  <ubizjak@gmail.com>

    gcc/ChangeLog:

            PR target/51954
            * config/i386/i386.md (adcl/neg -> sbb peephole): New peephole2.
    gcc/testsuite/ChangeLog:

            PR target/51954
            * gcc.target/i386/pr51954.c: New test.

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

* [Bug target/51954] __int128_t (and long long on x86) negation can be optimized
  2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
                   ` (8 preceding siblings ...)
  2022-04-29 11:31 ` cvs-commit at gcc dot gnu.org
@ 2022-04-29 11:33 ` ubizjak at gmail dot com
  9 siblings, 0 replies; 11+ messages in thread
From: ubizjak at gmail dot com @ 2022-04-29 11:33 UTC (permalink / raw)
  To: gcc-bugs

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

Uroš Bizjak <ubizjak at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #9 from Uroš Bizjak <ubizjak at gmail dot com> ---
Implemented for gcc-13.

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

end of thread, other threads:[~2022-04-29 11:33 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-23  1:43 [Bug rtl-optimization/51954] New: __int128_t negation can be optimized svfuerst at gmail dot com
2012-01-23  4:00 ` [Bug rtl-optimization/51954] __int128_t (and long long on x86) " pinskia at gcc dot gnu.org
2021-12-26 22:34 ` [Bug target/51954] " pinskia at gcc dot gnu.org
2022-02-21  5:57 ` pinskia at gcc dot gnu.org
2022-02-21  6:29 ` crazylht at gmail dot com
2022-02-21  6:49 ` crazylht at gmail dot com
2022-02-21  7:45 ` ubizjak at gmail dot com
2022-02-21  7:52 ` ubizjak at gmail dot com
2022-02-21  7:57 ` ubizjak at gmail dot com
2022-04-29 11:31 ` cvs-commit at gcc dot gnu.org
2022-04-29 11:33 ` ubizjak at gmail dot com

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