RFR: 8366815: C2: Delay Mod/Div by constant transformation
Hannes Greule
hgreule at openjdk.org
Thu Oct 23 11:25:04 UTC 2025
On Thu, 23 Oct 2025 08:44:19 GMT, Emanuel Peter <epeter at openjdk.org> wrote:
> Manuel and I discussed in the office a little more :)
>
> Can you show us a concrete example, where `Div` gets `Idealized` early, and then the generated nodes do not propagate the value range sufficiently precise for the comparison to constant fold?
>
> I suspect that it is the value range "truncation" on the lower bits that are lost in `MulHiLNode`, but it would be nice to see that example ;)
>
> Because if there is a solution that just improves the `Value` of the mul/shift/... nodes, that would probably be preferable.
>
> But if we in the end need to build a `Value` optimization that pattern matches again through the nodes that `transform_int_divide` generated, that would probably be less nice, given the complexity. And then we should do the delay.
One very straightforward example would be something like
static boolean divFold(int a) {
return a / 100_000 >= 21475;
}
which isn't folded to `false` with early idealization but works with the changes from this PR and #26143 both applied.
>From my analysis, this comes from the the rounding adjustments: We need to round towards zero, so we need to add 1 (=subtract -1) for negative values. We achieve that by an right shift to produce either a 0 or a -1 and then do the subtraction with that value.
<img width="725" height="578" alt="image" src="https://github.com/user-attachments/assets/643d8af5-27a3-44d4-ae18-874261d3e4c6" />
The subtraction isn't aware of the relation between the param being negative and the adjustment, and as you said, to recognize that relation, you'd more or less need to recognize that these operations form a division.
Now, I *think* this is the only case, and it's only off by 1 (and if the sign of the dividend is known, it also isn't a problem), so I'm wondering if there are any common patterns where this would be relevant, otherwise it might really make sense to just delay Mod and accept this edge case for Div.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/27886#issuecomment-3436423466
More information about the hotspot-compiler-dev
mailing list