https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80261
Bug ID: 80261 Summary: Worse code generated compared to clang with modulo operation Product: gcc Version: 6.3.1 Status: UNCONFIRMED Severity: normal Priority: P3 Component: rtl-optimization Assignee: unassigned at gcc dot gnu.org Reporter: maksqwe1 at ukr dot net Target Milestone: --- .................... #include <cstdio> #include <stdint.h> typedef unsigned int uint; uintptr_t g_array[131]; uintptr_t foo1(void *ptr) { constexpr size_t size = sizeof(g_array) / sizeof(uintptr_t); return g_array[uint(uintptr_t(ptr)) % size]; } uintptr_t foo2(void *ptr) { constexpr size_t size = sizeof(g_array) / sizeof(uintptr_t); return g_array[uintptr_t(ptr) % size]; } .................... This code produce more worse assembly code for foo2() compared to clang. ----------- x86-64 GCC 6.3(-O2 -std=c++11) ----------- foo1(void*): mov eax, edi mov edx, -98358029 mul edx shr edx, 7 imul edx, edx, 131 sub edi, edx mov rax, QWORD PTR g_array[0+rdi*8] ret foo2(void*): mov rax, rdi movabs rdx, 281629680514649643 mul rdx shr rdx mov rax, rdx sal rax, 6 add rax, rdx lea rax, [rdx+rax*2] sub rdi, rax mov rax, QWORD PTR g_array[0+rdi*8] ret g_array: .zero 1048 ------------------------------------------------------ ----------- x86-64 CLang 3.9(-O2 -std=c++11) --------- foo1(void*): # @foo1(void*) mov ecx, edi movabs rdx, 281629680514649643 mov rax, rcx mul rdx shr rdx imul rax, rdx, 131 sub rcx, rax mov rax, qword ptr [8*rcx + g_array] ret foo2(void*): # @foo2(void*) movabs rcx, 281629680514649643 mov rax, rdi mul rcx shr rdx imul rax, rdx, 131 sub rdi, rax mov rax, qword ptr [8*rdi + g_array] ret g_array: .zero 1048 ------------------------------------------------------ https://godbolt.org/g/PEyju2