A simple optimization proposal
vladimir.kozlov at oracle.com
Wed Feb 12 15:45:30 PST 2014
On 2/12/14 3:32 PM, Martin Grajcar wrote:
> Hi John, Chris, and Vladimir,
> I guess the Gist replacing
> (x & array.length - 1) u< array.length
> array.length != 0
> can be easily changed to implement
> (x & (m-1)) u< m, if and only if (m > 0)
Yes, that is what Kris did.
> in general, but I wonder if it will result in the optimal code for
> arrays, which can look like
> int mask = array.length - 1; // already there
> if (mask < 0) goAway();
C2 generates uncommon trap in such cases and go to interpreter to throw
exception. As result the hot path in compiled code is compact.
> where the second line on i86 translates to simply jump if sign, i.e., a
> conditional branch without any test as the flags has been already set.
C2 always generates test instruction before jump. It is one of
shortfalls of C2. Note that modern x86 cpus fuse tst+jcc (and cmp+jcc)
instructions into 1 micro-instruction. So there should be no performance
penalty to have a separate test instruction.
> This uses the equivalence m > 0 if and only if (m-1) >= 0 which holds
> with the exception of m = Integer.MIN_VALUE. When checking array bounds,
> this exception can't occur.
> On Wed, Feb 12, 2014 at 11:00 PM, Vladimir Kozlov
> <vladimir.kozlov at oracle.com <mailto:vladimir.kozlov at oracle.com>> wrote:
> Looks reasonable. Kris, you need also look for other patterns listed
> in JDK-8003585.
More information about the hotspot-compiler-dev