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.