Value type sorting
Tobias Hartmann
tobias.hartmann at oracle.com
Thu Aug 30 13:00:27 UTC 2018
Hi John,
On 27.08.2018 19:29, John Rose wrote:
> So this doesn't fold up: ((a < b) ? -1 : (a == b) ? 0 : 1) > 0.
> I suspect it *would* fold up (via split-if?) if by pure chance
> the expression were (a > b) ? 1 : (… something <= 0 …).
Yes, that expression is folded.
> This variation ((a < b) ? -1 : (a == b) ? 0 : 1) < 0 probably
> *does* fold up.
Yes, there are some cases that C2 is able to optimize. I've created a test that checks the most
common cases:
http://cr.openjdk.java.net/~thartmann/8210215/webrev.00/test/hotspot/jtreg/compiler/codegen/TestTrichotomyExpressions.java.html
Out of these 54 tests, C2 can optimize only 16. For all other 38 tests, C2 emits two comparisons.
> C2 doesn't handle trichotomy calculations as well as it should.
> There is no TypeInt::CC_NE, for example. And these trivial
> but nested ?:?: expressions are not inspected vigorously
> enough. They should be. I don't think we can do anything
> about CC_NE, though. But maybe we can transform (?:?:)
> cmp x by reordering the (?:?:) so the top-level ?: shows
> sides that appear on either side of x.
Yes, I've filed JDK-8210215 [1] for that and implemented a prototype:
http://cr.openjdk.java.net/~thartmann/8210215/webrev.00/
(not yet ready for review/production but I'll follow up with a RFR for JDK 12 soon)
Unfortunately, the fix is a bit more complicated than expected because there are many different
cases. However, with this patch, C2 is able to optimize *all* 54 cases of the above test and nicely
folds them to a single comparison.
Thanks,
Tobias
[1] https://bugs.openjdk.java.net/browse/JDK-8210215
More information about the valhalla-dev
mailing list