public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "652023330028 at smail dot nju.edu.cn" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug tree-optimization/113301] New: [12/13/14 Regression] Missed optimization: (1/(x+1))/2 => 0 since gcc-12
Date: Wed, 10 Jan 2024 05:24:45 +0000	[thread overview]
Message-ID: <bug-113301-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 113301
           Summary: [12/13/14 Regression] Missed optimization: (1/(x+1))/2
                    => 0 since gcc-12
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 652023330028 at smail dot nju.edu.cn
  Target Milestone: ---

Hello, we noticed that GCC seems to have missed optimizations as stated in the
title, and it works as expected on gcc-11.4.

After analysis, we found that they (gcc-trunk and gcc-11.4) are different in
evrp(tree), and further, the difference is in the calculation of the value
range.

reduced code:
https://godbolt.org/z/Y7v1jsTKf

int c;
void func(int x){
    c=(1/(x+1))/2;
}

gcc(trunk) -O3 -fwrapv:
func(int):
        lea     edx, [rdi+1]
        add     edi, 2
        xor     eax, eax
        cmp     edi, 2
        cmova   edx, eax
        mov     eax, edx
        shr     eax, 31
        add     eax, edx
        sar     eax
        mov     DWORD PTR c[rip], eax
        ret

evrp (tree):
Folding statement: _7 = _6 <= 2;
Not folded
Folding statement: _2 = _7 ? _1 : 0;
Possible COND_EXPR adjustment. Range op1 : [irange] int VARYING and Range op2:
[irange] int [0, 0] NONZERO 0x0
Not folded
Folding statement: _3 = _2 / 2;
Global Exported: _3 = [irange] int [-1073741824, 1073741823]
Not folded
Folding statement: c = _3;
Not folded
Folding statement: return;
Not folded


Expected code:
gcc(11.4) -O3 -fwrapv:
func(int):
        mov     DWORD PTR c[rip], 0
        ret

evrp (tree):
=========== BB 2 ============
    <bb 2> :
    # DEBUG BEGIN_STMT
    _1 = x_4(D) + 1;
    _2 = 1 / _1;
    c = 0;
    return;

_2 : int [-1, 1]
Non-varying global ranges:
=========================:
_2  : int [-1, 1]


Also, the following code works as expected (gcc-trunk):
void func1(int x){
    c=(1/x)/2;
}

Thank you very much for your time and effort! We look forward to hearing from
you.

             reply	other threads:[~2024-01-10  5:24 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-10  5:24 652023330028 at smail dot nju.edu.cn [this message]
2024-01-10  5:50 ` [Bug tree-optimization/113301] " pinskia at gcc dot gnu.org
2024-01-10  7:49 ` rguenth at gcc dot gnu.org
2024-01-10  9:03 ` jakub at gcc dot gnu.org
2024-01-10 10:16 ` pinskia at gcc dot gnu.org
2024-01-10 17:11 ` jakub at gcc dot gnu.org
2024-01-10 20:37 ` amacleod at redhat dot com
2024-01-10 22:12 ` amacleod at redhat dot com
2024-01-10 22:25 ` pinskia at gcc dot gnu.org
2024-01-11  4:24 ` pinskia at gcc dot gnu.org
2024-01-11 18:01 ` cvs-commit at gcc dot gnu.org
2024-01-11 18:03 ` 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-113301-4@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).