https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91883
Bug ID: 91883 Summary: Division by a constant could be optimized for known variables value range Product: gcc Version: 10.0 Status: UNCONFIRMED Keywords: missed-optimization Severity: normal Priority: P3 Component: middle-end Assignee: unassigned at gcc dot gnu.org Reporter: antoshkka at gmail dot com Target Milestone: --- Consider the example: unsigned long long kBorder = (1ull<<62); unsigned long long sample(unsigned long long m) { if (m >= kBorder) __builtin_unreachable(); return m / 10; } It produces the following assembly: sample(unsigned long long): movabs rdx, -3689348814741910323 mov rax, rdi mul rdx mov rax, rdx shr rax, 3 ret However, knowing that the higher bits are always 0, the constant could be adjusted to avoid the `shr rax, 3`: sample(unsigned long long): movabs rax, 1844674407370955162 mul rdi mov rax, rdx ret Godbolt playground: https://godbolt.org/z/YU2yAC This issue is probably related to PR 91881 P.S.: that optimization is important for std::to_chars(..., double) like functions, where a significant of a double is extracted into an unsigned long long variable, so its upper bits are always zero.