public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Uros Bizjak <ubizjak@gmail.com>
To: Roger Sayle <roger@nextmovesoftware.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [x86_64 PATCH] Decrement followed by cmov improvements.
Date: Fri, 30 Jul 2021 11:32:13 +0200	[thread overview]
Message-ID: <CAFULd4YkFsE7VDTe8MV3LsMiPT3Y1u4BEYGUzFnWr-dELgxRYQ@mail.gmail.com> (raw)
In-Reply-To: <02a101d78211$326e66f0$974b34d0$@nextmovesoftware.com>

On Mon, Jul 26, 2021 at 1:27 PM Roger Sayle <roger@nextmovesoftware.com> wrote:
>
>
> The following patch to the x86_64 backend improves the code generated
> for a decrement followed by a conditional move.  The primary change is
> to recognize that after subtracting one, checking the result is -1 (or
> equivalently that the original value was zero) can be implemented using
> the borrow/carry flag instead of requiring an explicit test instruction.
> This is achieved by a new define_insn_and_split that allows combine to
> split the desired sequence/composite into a *subsi_3 and *movsicc_noc.
>
> The other change with this patch is/are a pair of peephole2 optimizations
> to eliminate register-to-register moves generated during register
> allocation.  During reload, the compiler doesn't know that inverting
> the condition of a conditional cmove can sometimes reduce register
> pressure, but this is easy to tidy up during the peephole2 pass (where
> swapping the order of the insn's operands performs the required
> logic inversion).
>
> Both improvements are demonstrated by the case below:
>
> int foo(int x) {
>   if (x == 0)
>     x = 16;
>   else x--;
>   return x;
> }
>
> Before:
> foo:    leal    -1(%rdi), %eax
>         testl   %edi, %edi
>         movl    $16, %edx
>         cmove   %edx, %eax
>         ret
>
> After:
> foo:    subl    $1, %edi
>         movl    $16, %eax
>         cmovnc  %edi, %eax
>         ret
>
> And the value of the peephole2 clean-up can be seen on its own in:
>
> int bar(int x) {
>   x--;
>   if (x == 0)
>     x = 16;
>   return x;
> }
>
> Before:
> bar:    movl    %edi, %eax
>         movl    $16, %edx
>         subl    $1, %eax
>         cmove   %edx, %eax
>         ret
>
> After:
> bar:    subl    $1, %edi
>         movl    $16, %eax
>         cmovne  %edi, %eax
>         ret
>
> These idioms were inspired by the source code of NIST SciMark4's
> Random_nextDouble function, where the tweaks above result in
> a ~1% improvement in the MonteCarlo benchmark kernel.
>
> This patch has been tested on x86_64-pc-linux-gnu with a
> "make boostrap" and "make -k check" with no new failures.
>
> Ok for mainline?
>
>
> 2021-07-26  Roger Sayle  <roger@nextmovesoftware.com>
>
> gcc/ChangeLog
>         * config/i386/i386.md (*dec_cmov<mode>): New define_insn_and_split
>         to generate a conditional move using the carry flag after sub $1.
>         (peephole2): Eliminate a register-to-register move by inverting
>         the condition of a conditional move.
>
> gcc/testsuite/ChangeLog
>         * gcc.target/i386/dec-cmov-1.c: New test.
>         * gcc.target/i386/dec-cmov-2.c: New test.

Please also allow ia32 in the testcases. #ifdef __x86_64__ 64bit
specific (long long) tests and add:

/* { dg-additional-options "-march=pentiumpro -mregparm=3" { target ia32 } } */

(cmov generation uses ancient ix86_arch_features, it gets enabled by
using -march=pentiumpro).

OK with the above change.

Thanks,
Uros.

      reply	other threads:[~2021-07-30  9:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-26 11:27 Roger Sayle
2021-07-30  9:32 ` Uros Bizjak [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAFULd4YkFsE7VDTe8MV3LsMiPT3Y1u4BEYGUzFnWr-dELgxRYQ@mail.gmail.com \
    --to=ubizjak@gmail.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=roger@nextmovesoftware.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).