public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "cvs-commit at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/95424] Failure to optimize division with numerator of 1
Date: Fri, 28 Jan 2022 18:37:31 +0000	[thread overview]
Message-ID: <bug-95424-4-boGLJUeS7P@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-95424-4@http.gcc.gnu.org/bugzilla/>

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jeff Law <law@gcc.gnu.org>:

https://gcc.gnu.org/g:c2b610e7c6c89fd422c5c31f01023bcddf3cf4a5

commit r12-6924-gc2b610e7c6c89fd422c5c31f01023bcddf3cf4a5
Author: Zhao Wei Liew <zhaoweiliew@gmail.com>
Date:   Fri Jan 28 13:36:39 2022 -0500

    match.pd: Simplify 1 / X for integer X [PR95424]

    This patch implements an optimization for the following C++ code:

    int f(int x) {
        return 1 / x;
    }

    int f(unsigned int x) {
        return 1 / x;
    }

    Before this patch, x86-64 gcc -std=c++20 -O3 produces the following
assembly:

    f(int):
        xor edx, edx
        mov eax, 1
        idiv edi
        ret
    f(unsigned int):
        xor edx, edx
        mov eax, 1
        div edi
        ret

    In comparison, clang++ -std=c++20 -O3 produces the following assembly:

    f(int):
        lea ecx, [rdi + 1]
        xor eax, eax
        cmp ecx, 3
        cmovb eax, edi
        ret
    f(unsigned int):
        xor eax, eax
        cmp edi, 1
        sete al
        ret

    Clang's output is more efficient as it avoids expensive div operations.

    With this patch, GCC now produces the following assembly:

    f(int):
        lea eax, [rdi + 1]
        cmp eax, 2
        mov eax, 0
        cmovbe eax, edi
        ret
    f(unsigned int):
        xor eax, eax
        cmp edi, 1
        sete al
        ret

    which is virtually identical to Clang's assembly output. Any slight
differences
    in the output for f(int) is possibly related to a different missed
optimization.

    v2: https://gcc.gnu.org/pipermail/gcc-patches/2022-January/587751.html
    Changes from v2:
    1. Refactor from using a switch statement to using the built-in
    if-else statement.

    v1: https://gcc.gnu.org/pipermail/gcc-patches/2022-January/587634.html
    Changes from v1:
    1. Refactor common if conditions.
    2. Use build_[minus_]one_cst (type) to get -1/1 of the correct type.
    3. Match only for TRUNC_DIV_EXPR and TYPE_PRECISION (type) > 1.

    gcc/ChangeLog:

            PR tree-optimization/95424
            * match.pd: Simplify 1 / X where X is an integer.

  parent reply	other threads:[~2022-01-28 18:37 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-29 20:21 [Bug tree-optimization/95424] New: " gabravier at gmail dot com
2020-06-02  6:46 ` [Bug tree-optimization/95424] " rguenth at gcc dot gnu.org
2021-08-14 23:59 ` pinskia at gcc dot gnu.org
2021-12-30  7:12 ` zhaoweiliew at gmail dot com
2021-12-31  1:16 ` zhaoweiliew at gmail dot com
2021-12-31  1:17 ` zhaoweiliew at gmail dot com
2021-12-31  1:18 ` zhaoweiliew at gmail dot com
2021-12-31  1:19 ` zhaoweiliew at gmail dot com
2022-01-05  3:33 ` zhaoweiliew at gmail dot com
2022-01-28 18:37 ` cvs-commit at gcc dot gnu.org [this message]
2022-01-28 18:41 ` law at gcc dot gnu.org
2022-01-29 16:56 ` cvs-commit at gcc dot gnu.org
2024-01-20 17:16 ` pinskia at gcc dot gnu.org

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=bug-95424-4-boGLJUeS7P@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).