public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug tree-optimization/114701] New: Missed optimization of loop invariant
@ 2024-04-12  7:14 652023330028 at smail dot nju.edu.cn
  2024-04-12 12:03 ` [Bug tree-optimization/114701] " rguenth at gcc dot gnu.org
  0 siblings, 1 reply; 2+ messages in thread
From: 652023330028 at smail dot nju.edu.cn @ 2024-04-12  7:14 UTC (permalink / raw)
  To: gcc-bugs

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.

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

* [Bug tree-optimization/114701] Missed optimization of loop invariant
  2024-04-12  7:14 [Bug tree-optimization/114701] New: Missed optimization of loop invariant 652023330028 at smail dot nju.edu.cn
@ 2024-04-12 12:03 ` rguenth at gcc dot gnu.org
  0 siblings, 0 replies; 2+ messages in thread
From: rguenth at gcc dot gnu.org @ 2024-04-12 12:03 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-04-12
             Status|UNCONFIRMED                 |NEW
           Keywords|                            |missed-optimization
     Ever confirmed|0                           |1

--- Comment #1 from Richard Biener <rguenth at gcc dot gnu.org> ---
There were similar bugs reported already, if not exact dups (b + d being
invariant).  As with the others this depends on store motion.  As with
the others the SCCP pass could compute the overall increment to 'a'.
SCEV would be required to detect that b + d is invariant and to hoist that.

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

end of thread, other threads:[~2024-04-12 12:03 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-12  7:14 [Bug tree-optimization/114701] New: Missed optimization of loop invariant 652023330028 at smail dot nju.edu.cn
2024-04-12 12:03 ` [Bug tree-optimization/114701] " rguenth at gcc dot gnu.org

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).