public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug rtl-optimization/101044] New: -ABS(A) produces two neg instructions
@ 2021-06-13  2:37 pinskia at gcc dot gnu.org
  2021-06-13 12:00 ` [Bug rtl-optimization/101044] " ubizjak at gmail dot com
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-06-13  2:37 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 101044
           Summary: -ABS(A) produces two neg instructions
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Keywords: missed-optimization
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pinskia at gcc dot gnu.org
  Target Milestone: ---
            Target: x86_64-linux-gnu

Take:
int f4(int A)
{
  return A <= 0? A : -A;
}
int f5(int A)
{
return A < 0?  A : -A;
}
int f2(int A)
{
  A = A < 0 ? -A : A;
  return -A;
}
---- CUT ---
On x86_64 we get two neg for each of these functions now, we used to get none
sometime ago.
Right now we get for each of them:
        movl    %edi, %eax
        negl    %eax
        cmovs   %edi, %eax
        negl    %eax
----- CUT ---
Note aarch64 seems to be fine:
        cmp     w0, 0
        csneg   w0, w0, w0, lt

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

* [Bug rtl-optimization/101044] -ABS(A) produces two neg instructions
  2021-06-13  2:37 [Bug rtl-optimization/101044] New: -ABS(A) produces two neg instructions pinskia at gcc dot gnu.org
@ 2021-06-13 12:00 ` ubizjak at gmail dot com
  2021-06-29  9:38 ` ubizjak at gmail dot com
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2021-06-13 12:00 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Uroš Bizjak <ubizjak at gmail dot com> ---
The first neg also sets sign flag (SF) for the following CMOVS.

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

* [Bug rtl-optimization/101044] -ABS(A) produces two neg instructions
  2021-06-13  2:37 [Bug rtl-optimization/101044] New: -ABS(A) produces two neg instructions pinskia at gcc dot gnu.org
  2021-06-13 12:00 ` [Bug rtl-optimization/101044] " ubizjak at gmail dot com
@ 2021-06-29  9:38 ` ubizjak at gmail dot com
  2021-06-29  9:51 ` ubizjak at gmail dot com
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2021-06-29  9:38 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-06-29
     Ever confirmed|0                           |1
           Assignee|unassigned at gcc dot gnu.org      |ubizjak at gmail dot com
             Status|UNCONFIRMED                 |ASSIGNED

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

Patch introduces nabs patterns where we reverse the condition of the cmove.

The compilation results in:

        movl    %edi, %eax
        negl    %eax
        cmovns  %edi, %eax

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

* [Bug rtl-optimization/101044] -ABS(A) produces two neg instructions
  2021-06-13  2:37 [Bug rtl-optimization/101044] New: -ABS(A) produces two neg instructions pinskia at gcc dot gnu.org
  2021-06-13 12:00 ` [Bug rtl-optimization/101044] " ubizjak at gmail dot com
  2021-06-29  9:38 ` ubizjak at gmail dot com
@ 2021-06-29  9:51 ` ubizjak at gmail dot com
  2021-07-01  9:21 ` [Bug target/101044] " cvs-commit at gcc dot gnu.org
  2021-07-01  9:24 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2021-06-29  9:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Uroš Bizjak <ubizjak at gmail dot com> ---
The compound nabs insn also needs to be handled in the STV pass.

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

* [Bug target/101044] -ABS(A) produces two neg instructions
  2021-06-13  2:37 [Bug rtl-optimization/101044] New: -ABS(A) produces two neg instructions pinskia at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2021-06-29  9:51 ` ubizjak at gmail dot com
@ 2021-07-01  9:21 ` cvs-commit at gcc dot gnu.org
  2021-07-01  9:24 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2021-07-01  9:21 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 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:9f6aeb85ee87c6b4e580b6b71e26cbe99e1dab70

commit r12-1950-g9f6aeb85ee87c6b4e580b6b71e26cbe99e1dab70
Author: Uros Bizjak <ubizjak@gmail.com>
Date:   Thu Jul 1 10:56:32 2021 +0200

    i386: Add integer nabs instructions [PR101044]

    The patch adds integer nabs "(NEG (ABS (...)))" instructions, adds STV
    conversion and adjusts STV cost calculations accordingly.  When CMOV
    instruction is used to implement abs, the sign is determined from the
    preceeding operand negation, and CMOVS is used to select between
    negated and non-negated value.

    To implement nabs, just reverse the condition and emit CMOVNS instead.

    The STV costs are adjusted for inherent NOT of nabs insn. V2DI NOT is
    somehow costly operation, since it is implemented as a load of zero,
    followed by a SUB insn. OTOH, integer nabs with inherent NOT is relatively
    cheap, so some STV chains became less profitable for conversion.

    The patch rewrites operand scanner in compute_convert_gain to a switch
    and reorders case instances in general_scalar_to_vector_candidate_p
    to benefit from fallthroughs, and to remove special processing of
    andnot in the later case.

    gcc/

    2021-07-01  Uroš Bizjak  <ubizjak@gmail.com>

            PR target/101044
            * config/i386/i386.md (*nabs<dwi>2_doubleword):
            New insn_and_split pattern.
            (*nabs<dwi>2_1): Ditto.
            * config/i386/i386-features.c
            (general_scalar_chain::compute_convert_gain):
            Handle (NEG (ABS (...))) RTX.  Rewrite src code
            scanner as switch statement.
            (general_scalar_chain::convert_insn):
            Handle (NEG (ABS (...))) RTX.
            (general_scalar_to_vector_candidate_p):
            Detect  (NEG (ABS (...))) RTX.  Reorder case statements
            for (AND (NOT (...) ...)) fallthrough.

    gcc/testsuite/

    2021-07-01  Uroš Bizjak  <ubizjak@gmail.com>

            PR target/101044
            * gcc.target/i386/pr101044.c: New test.

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

* [Bug target/101044] -ABS(A) produces two neg instructions
  2021-06-13  2:37 [Bug rtl-optimization/101044] New: -ABS(A) produces two neg instructions pinskia at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2021-07-01  9:21 ` [Bug target/101044] " cvs-commit at gcc dot gnu.org
@ 2021-07-01  9:24 ` ubizjak at gmail dot com
  4 siblings, 0 replies; 6+ messages in thread
From: ubizjak at gmail dot com @ 2021-07-01  9:24 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #5 from Uroš Bizjak <ubizjak at gmail dot com> ---
Fixed.

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

end of thread, other threads:[~2021-07-01  9:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-13  2:37 [Bug rtl-optimization/101044] New: -ABS(A) produces two neg instructions pinskia at gcc dot gnu.org
2021-06-13 12:00 ` [Bug rtl-optimization/101044] " ubizjak at gmail dot com
2021-06-29  9:38 ` ubizjak at gmail dot com
2021-06-29  9:51 ` ubizjak at gmail dot com
2021-07-01  9:21 ` [Bug target/101044] " cvs-commit at gcc dot gnu.org
2021-07-01  9:24 ` 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).