| Issue |
238449
|
| Summary |
[SelectionDAG] Widen i32 sdiv by constant with a negative magic to an i64 multiply on 64-bit targets
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
amritk
|
This is the signed counterpart of #181288 / #207634 (32-bit `udiv` with a 33-bit magic widened to a 64-bit multiply).
For a 32-bit `sdiv` by a positive constant `d` whose Hacker's Delight magic is negative as an `i32` (for example 7, 60, 365, 3600 or 86400; 33 of the non-power-of-two divisors up to 100), `BuildSDIV` emits the `MULHS` plus an add-back of the numerator. On a target with a legal 64-bit multiply there is a spare bit: `|x| <= 2^31`, and a multiplier `c = ceil(2^s / d)` with `s = 31 + ceil(log2 d)` is below `2^32`, so `x * c` fits in a signed `i64`. Then `x / d == trunc((sext(x) * c) >> s) + (x >>> 31)` for every `i32` `x`, with no add-back. The derivation is in D. Lemire, "Beating the compiler at signed 32-bit division" (2026-10-31), using Theorem 1 of Lemire, Bartlett, Kaser, *Integer division by constants: optimal bounds* (Heliyon, 2021).
### Reproducer
```llvm
define i32 @plain(i32 %x) {
%q = sdiv i32 %x, 7
ret i32 %q
}
define i32 @wide(i32 %x) {
%w = sext i32 %x to i64
%p = mul i64 %w, 2454267027
%s = ashr i64 %p, 34
%t = trunc i64 %s to i32
%n = lshr i32 %x, 31
%q = add i32 %t, %n
ret i32 %q
}
```
`opt -O2` leaves `@wide` as it is. `llc -O2` (LLVM 18.1.3; the relevant part of `BuildSDIV` on `main` at f920192 is unchanged):
x86-64:
```
plain: wide:
movslq %edi, %rax movslq %edi, %rax
imulq $-1840700269, %rax, %rcx movl $2454267027, %ecx
shrq $32, %rcx imulq %rax, %rcx
addl %ecx, %eax sarq $34, %rcx
movl %eax, %ecx shrl $31, %eax
shrl $31, %ecx addl %ecx, %eax
sarl $2, %eax retq
addl %ecx, %eax
retq
```
AArch64:
```
plain: wide:
mov w8, #9363 sxtw x8, w0
movk w8, #37449, lsl #16 mov w9, #9363
smull x8, w0, w8 movk w9, #37449, lsl #16
lsr x8, x8, #32 mul x8, x8, x9
add w8, w8, w0 asr x8, x8, #34
asr w9, w8, #2 add w0, w8, w0, lsr #31
add w0, w9, w8, lsr #31 ret
ret
```
### Correctness
I checked the rewrite against all 2^32 inputs, for both `/` and `%` (`%` taken as `x - q*d`), for d = 7, 60, 365, 3600 and 86400. There were no mismatches. The constants are (s, c) = (34, 2454267027), (37, 2290649225), (40, 3012360625), (43, 2443359173) and (48, 3257812231).
### Measurements
The machine is an Intel Xeon (Emerald Rapids, family 6 model 207) in a shared 4-core VM. The code is built with `clang -O3 -flto` (LLVM 18) over 16384 random `i32` values of both signs. Each figure is the median of 7 interleaved runs, each run the best of 15 rounds. The controls (d = 10 and d = 1000, whose magic is positive, so nothing is rewritten) moved by 2% or less in the latency loop, and by up to 7% in the independent loop, which is the noisier of the two on this VM.
| scalar loop | d = 7 | 60 | 365 | 3600 | 86400 |
| --- | --- | --- | --- | --- | --- |
| dependent chain (latency), ns/div | 3.31 → 2.32 (−30%) | 3.27 → 2.34 (−29%) | 3.29 → 2.33 (−29%) | 3.28 → 2.30 (−30%) | 3.19 → 2.38 (−26%) |
| independent divisions, vectorizer off, ns/div | 0.75 → 0.64 (−14%) | 0.75 → 0.65 (−13%) | 0.78 → 0.61 (−22%) | 0.74 → 0.61 (−18%) | 0.76 → 0.61 (−20%) |
Lemire reports the same latency drop (10 → 8 cycles) on Emerald Rapids, and about 12% on an Apple M4.
### Scope
- **Scalar `i32` only, as the unsigned widening does (`VT == MVT::i32`).** I wrote the rewrite in IR ahead of the loop vectorizer, and there it made vectorized loops 5–10% slower with AVX2. The vector form needs a 64-bit lane multiply, which has to be put together from several `vpmuludq`, where the current lowering uses one `vpmuldq`. Doing the rewrite in `BuildSDIV` for scalar types avoids that.
- **Only when the magic needs the add-back (`d > 0` and `magics.Magic.isNegative()`).** When the magic is positive (d = 1000, say), AArch64 is slightly slower with the wide form: it needs a separate `sxtw` where `smull` sign-extends for free. Lemire measured 1.57 ns → 1.77 ns on an M4. Negative divisors could negate the result, but I haven't measured that.
- **Gate on a legal i64 `MUL` and a free `sext` from i32 to i64.** This parallels the `isZExtFree` check in #207634.
Related: #97909, and the open PR #189287 (positive-magic `sdiv`, which reads the sign from the dividend) touch the same function.
---
_Note: This issue description was generated using AI. All benchmark measurements, assembly listings, and exhaustive correctness tests were verified on my VM.._
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs