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/114701] New: Missed optimization of loop invariant
Date: Fri, 12 Apr 2024 07:14:14 +0000	[thread overview]
Message-ID: <bug-114701-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 114701
           Summary: Missed optimization of loop invariant
           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 maybe there is a missed optimization of loop invariant.

Loop invariant: 100/(b+d).

https://godbolt.org/z/jKs5qohMs

int a, b;
void func() {
  int d = 0;
  for (int i = 0; i < 1000; i++) {
    b += 1;
    d += -1;
    a += 100 / (b + d);
  }
}

GCC -O3 -fwrapv:
func():
        mov     r9d, DWORD PTR b[rip]
        mov     edi, DWORD PTR a[rip]
        xor     ecx, ecx
        lea     r8d, [r9+1]
.L2:
        mov     esi, r8d
        mov     eax, 100
        xor     edx, edx
        sub     esi, ecx
        sub     ecx, 1
        add     esi, ecx
        idiv    esi
        add     edi, eax
        cmp     ecx, -1000
        jne     .L2
        add     r9d, 1000
        mov     DWORD PTR a[rip], edi
        mov     DWORD PTR b[rip], r9d
        ret

Expected code (Clang -O3 -fwrapv -fno-tree-vectorize):
func():                               # @func()
        mov     ecx, dword ptr [rip + b]
        mov     esi, dword ptr [rip + a]
        mov     eax, 100
        xor     edx, edx
        idiv    ecx
        add     eax, eax
        mov     edx, 1000
        lea     edi, [rax + rax]
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        add     esi, eax
        add     esi, edi
        add     esi, edi
        add     edx, -10
        jne     .LBB0_1
        add     ecx, 1000
        mov     dword ptr [rip + b], ecx
        mov     dword ptr [rip + a], esi
        ret

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

             reply	other threads:[~2024-04-12  7:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-12  7:14 652023330028 at smail dot nju.edu.cn [this message]
2024-04-12 12:03 ` [Bug tree-optimization/114701] " rguenth 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-114701-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).