Bounds checks with unsafe array access

Paul Sandoz paul.sandoz at
Fri Sep 12 14:37:43 UTC 2014

On Sep 10, 2014, at 10:20 PM, John Rose <john.r.rose at> wrote:

> On Sep 10, 2014, at 3:47 AM, Paul Sandoz <paul.sandoz at> wrote:
>> The patch for JDK-8003585 makes no difference.
> My first thought, is, why not?  Isn't that just a bug?

I would presume so, it seems a general pattern, but I dunno enough about the C2 graph representation. Should i start by rendering graphs and eye-balling them?

> For virtualizing arrays, we need either an explicit range-check intrinsic, or robust canonicalization of the standard idioms into CmpU (which is what we use internally).


> Actually, we now have Integer.compareUnsigned!  

Ah! now it becomes obvious :-)

        if (Integer.compareUnsigned(index, a.length) >= 0)
            throw new ArrayIndexOutOfBoundsException();

That it should in principle, vis some intrinsification, reduce to something like a cmp/jae or test/jbe depending on what comes before it.

> If we agree that is an intrinsic for range checks, the JIT will have a better optimization target to aim at.   Ultimately, we need an intrinsic (perhaps more intentional than Integer.compareUnsigned) that will encourage the JIT to treat it like a range check, doing iteration range splitting, predication, and all the rest.
>> (Note: in general we cannot assume that "int index = i & (a.length - 1)" always occurs before the bounds checks, otherwise i would have explicitly written "if (a.length == 0) throw ...")
> Right.  You want to factor the range check into a bit of code that doesn't know how it's going to be used, but then gets the same perks as normal array code, including the a.length==0 stuff, and the loop opts I mentioned.
>> Ideally similar code as shown for an aaload should be generated. Any suggestions/ideas on how to make that happen?
> First, agree on a range check intrinsic.  Then, treat optimization equity failures as JIT bugs.

Many thanks, seems like an excellent starting point is to intrinsify Integer.compareUnsigned and see what that yields.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 841 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <>

More information about the hotspot-compiler-dev mailing list