RFR (S): 8033666: Make sure @ForceInline is everywhere it needs to be in sun.misc and java.lang.invoke

John Rose john.r.rose at oracle.com
Wed Feb 26 01:32:56 UTC 2014


On Feb 25, 2014, at 5:17 PM, Christian Thalinger <christian.thalinger at oracle.com> wrote:

> While touching the code some time ago John and I were playing around with the idea to call Class.cast in the failing case:
> 
> +     static <T,U> T castReference(Class<? extends T> t, U x) {
> +         // inlined Class.cast because we can't ForceInline it
> +         if (x != null && !t.isInstance(x))
> +             t.cast(x);
> +         return (T) x;
> +     }
> + 
> 
> Then we don’t have to duplicate the throwing logic.

Here's the compromise version I have:

    static Error castReferenceFail(Object x, Class<?> t) {
        t.cast(x);
        throw new AssertionError();
    }

(a) It doesn't duplicate throw code from Class.cast.  (b) It has a real throw in the force-inlined method (not shown), so the optimizer can't fail to cut the path.

If the inlining of t.cast _does_ fail, or if the optimizer inlines t.cast but fails to prove that the throw test duplicates the dominating test, then the optimizer will not be able to push type information into x, because it won't be able to prove that the t.cast branch diverges from the main branch:

The duplicate throw (in the shown code) is just an extra touch; it is harmless and gives one more hint to the system.  This would be overkill which would pass back the seemingly promised exception:

    static ClassCastException castReferenceFail(Object x, Class<?> t) {
        try { t.cast(x); }
        catch (ClassCastException ex) { return ex; }
        throw new AssertionError();
    }

As with a method or two in MethodHandleStatics, this helper method would seem to create an exception for the client to immediately throw, but in fact would throw a suitable exception immediately.

— John


More information about the core-libs-dev mailing list