RFR: 8282365: Consolidate and improve division by constant idealizations [v45]
Raffaello Giulietti
rgiulietti at openjdk.org
Sat Jan 20 10:54:49 UTC 2024
On Fri, 19 Jan 2024 20:06:05 GMT, Quan Anh Mai <qamai at openjdk.org> wrote:
>> This patch implements idealisation for unsigned divisions to change a division by a constant into a series of multiplication and shift. I also change the idealisation of `DivI` to get a more efficient series when the magic constant overflows an int32.
>>
>> In general, the idea behind a signed division transformation is that for a positive constant `d`, we would need to find constants `c` and `m` so that:
>>
>> floor(x / d) = floor(x * c / 2**m) for 0 < x < 2**(N - 1) (1)
>> ceil(x / d) = floor(x * c / 2**m) + 1 for -2**(N - 1) <= x < 0 (2)
>>
>> The implementation in the original book takes into consideration that the machine may not be able to perform the full multiplication `x * c`, so the constant overflow and we need to add back the dividend as in `DivLNode::Ideal` cases. However, for int32 division, `x * c` cannot overflow an int64. As a result, it is always feasible to just calculate the product and shift the result.
>>
>> For unsigned multiplication, the situation is somewhat trickier because the condition needs to be twice as strong (the condition (1) and (2) above are mostly the same). This results in the magic constant `c` calculated based on the method presented in Hacker's Delight by Henry S. Warren, Jr. may overflow an uintN. For int division, we can depend on the theorem devised by Arch D. Robison in N-Bit Unsigned Division Via N-Bit Multiply-Add, which states that there exists either:
>>
>> c1 in uint32 and m1, such that floor(x / d) = floor(x * c1 / 2**m1) for 0 < x < 2**32 (3)
>> c2 in uint32 and m2, such that floor(x / d) = floor((x + 1) * c2 / 2**m2) for 0 < x < 2**32 (4)
>>
>> which means that either `x * c1` never overflows an uint64 or `(x + 1) * c2` never overflows an uint64. And we can perform a full multiplication.
>>
>> For longs, there is no way to do a full multiplication so we do some basic transformations to achieve a computable formula. The details I have written as comments in the overflow case.
>>
>> More tests are added to cover the possible patterns.
>>
>> Please take a look and have some reviews. Thank you very much.
>
> Quan Anh Mai has updated the pull request incrementally with one additional commit since the last revision:
>
> suggestion
src/hotspot/share/opto/divconstants.cpp line 161:
> 159: c_ovf = c > min_signed;
> 160: c += c - 1;
> 161: rc += rc - d; // rc = 2 * rc - d
Well, `rc - d` usually underflows. This is benign on the supported CPUs, but I think the code proposed originally is clearer.
It also makes clear that the subtraction in `-=` is safe, since `rc > d - rc`. With an addition `+=` one has to reason in reverse, so to say.
Further, Common Subexpression Elimination can easily detect that the expression `d - rc` is identical to the one in the `if` condition. Not sure if CSE can detect that `rc - d` is `-(d - rc)` and emit a subtraction.
Suggestion:
rc -= d - rc; // rc = 2 * rc - d
src/hotspot/share/opto/divconstants.cpp line 170:
> 168: qv_ovf = qv >= min_signed;
> 169: qv += qv + 1;
> 170: rv += rv - v; // rv = 2 * rv - v
Same as above, with the difference that `rv - v` _always_ underflows.
Moreover, the quotient `qv` increases, so it's more natural to see a decrease in the remainder `rv`, rather than an increase, IMO.
Suggestion:
rv -= v - rv; // rv = 2 * rv - v
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/9947#discussion_r1460367093
PR Review Comment: https://git.openjdk.org/jdk/pull/9947#discussion_r1460367207
More information about the hotspot-compiler-dev
mailing list