[llvm] [SelectionDAG] Optimize 32-bit udiv with 33-bit magic constants on 64-bit targets (PR #181288)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Fri Feb 13 20:18:29 PST 2026
topperc wrote:
> Oh, when I assembled that code with the original llc-18, it output the same code as the llc I proposed this time.
>
> ```
> % llc-18 propose.ll -O3 -mattr=bmi2 -o -
> .text
> .file "propose.ll"
> .globl udiv_by_19 # -- Begin function udiv_by_19
> .p2align 4, 0x90
> .type udiv_by_19, at function
> udiv_by_19: # @udiv_by_19
> # %bb.0:
> movl %edi, %edx
> movabsq $970881267037344822, %rax # imm = 0xD79435E50D79436
> mulxq %rax, %rax, %rax
> # kill: def $eax killed $eax killed $rax
> retq
> ```
>
> However, the next code remained the same as the previous slow code.
>
> ```c++
> uint32_t div7ext(uint32_t x)
> {
> return uint32_t(uint64_t(x) / 7);
> }
> ```
>
> ```shell
> % clang++-18 -O3 -march=native -S -masm=intel t.cpp -o -
> _Z7div7extj:
> .cfi_startproc
> # %bb.0:
> mov eax, edi
> imul rax, rax, 613566757
> shr rax, 32
> sub edi, eax
> shr edi
> add eax, edi
> shr eax, 2
> # kill: def $eax killed $eax killed $rax
> ret
> ```
The middle end optimizations would remove the casts.
I think the constants between your approach and the zext have a similar magnitude and upper bits but differ in the lower bits.
https://github.com/llvm/llvm-project/pull/181288
More information about the llvm-commits
mailing list