# HG changeset patch # User Andreas Woess # Date 1396027985 -3600 # Node ID 73546bd550f03d4f8bbe70b72021ce813700352c # Parent 69375786ef7072a38428489a394a3e8abccf8494 CompilerDirectives: add optional nonNull parameter to unsafeCast diff -r 69375786ef70 -r 73546bd550f0 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/FrameWithoutBoxing.java --- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/FrameWithoutBoxing.java Fri Mar 28 17:55:46 2014 +0100 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/FrameWithoutBoxing.java Fri Mar 28 18:33:05 2014 +0100 @@ -55,7 +55,7 @@ @Override public T getArguments(Class clazz) { - return CompilerDirectives.unsafeCast(arguments, clazz, true); + return CompilerDirectives.unsafeCast(arguments, clazz, true, true); } @Override @@ -85,15 +85,15 @@ } private Object[] getLocals() { - return CompilerDirectives.unsafeCast(locals, Object[].class, true); + return CompilerDirectives.unsafeCast(locals, Object[].class, true, true); } private long[] getPrimitiveLocals() { - return CompilerDirectives.unsafeCast(this.primitiveLocals, long[].class, true); + return CompilerDirectives.unsafeCast(this.primitiveLocals, long[].class, true, true); } private byte[] getTags() { - return CompilerDirectives.unsafeCast(tags, byte[].class, true); + return CompilerDirectives.unsafeCast(tags, byte[].class, true, true); } private Object getObjectUnsafe(FrameSlot slot) { diff -r 69375786ef70 -r 73546bd550f0 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/typesystem/UnsafeTypeCastMacroNode.java --- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/typesystem/UnsafeTypeCastMacroNode.java Fri Mar 28 17:55:46 2014 +0100 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/nodes/typesystem/UnsafeTypeCastMacroNode.java Fri Mar 28 18:33:05 2014 +0100 @@ -33,14 +33,15 @@ import com.oracle.truffle.api.*; /** - * Macro node for method {@link CompilerDirectives#unsafeCast(Object, Class, boolean)}. + * Macro node for method {@link CompilerDirectives#unsafeCast(Object, Class, boolean, boolean)}. */ public class UnsafeTypeCastMacroNode extends NeverPartOfCompilationNode implements Canonicalizable { - private static final int ARGUMENT_COUNT = 3; private static final int OBJECT_ARGUMENT_INDEX = 0; private static final int CLASS_ARGUMENT_INDEX = 1; private static final int CONDITION_ARGUMENT_INDEX = 2; + private static final int NONNULL_ARGUMENT_INDEX = 3; + private static final int ARGUMENT_COUNT = 4; public UnsafeTypeCastMacroNode(Invoke invoke) { super(invoke, "The class of the unsafe cast could not be reduced to a compile time constant."); @@ -50,7 +51,8 @@ @Override public Node canonical(CanonicalizerTool tool) { ValueNode classArgument = arguments.get(CLASS_ARGUMENT_INDEX); - if (classArgument.isConstant()) { + ValueNode nonNullArgument = arguments.get(NONNULL_ARGUMENT_INDEX); + if (classArgument.isConstant() && nonNullArgument.isConstant()) { ValueNode objectArgument = arguments.get(OBJECT_ARGUMENT_INDEX); ValueNode conditionArgument = arguments.get(CONDITION_ARGUMENT_INDEX); Class c = (Class) classArgument.asConstant().asObject(); @@ -58,8 +60,9 @@ return objectArgument; } ResolvedJavaType lookupJavaType = tool.getMetaAccess().lookupJavaType(c); + Stamp stamp = StampFactory.declared(lookupJavaType, nonNullArgument.asConstant().asInt() != 0); ConditionAnchorNode valueAnchorNode = graph().add(new ConditionAnchorNode(CompareNode.createCompareNode(graph(), Condition.EQ, conditionArgument, ConstantNode.forBoolean(true, graph())))); - UnsafeCastNode piCast = graph().unique(new UnsafeCastNode(objectArgument, StampFactory.declaredNonNull(lookupJavaType), valueAnchorNode)); + UnsafeCastNode piCast = graph().unique(new UnsafeCastNode(objectArgument, stamp, valueAnchorNode)); this.replaceAtUsages(piCast); return valueAnchorNode; } diff -r 69375786ef70 -r 73546bd550f0 graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/substitutions/CompilerDirectivesSubstitutions.java --- a/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/substitutions/CompilerDirectivesSubstitutions.java Fri Mar 28 17:55:46 2014 +0100 +++ b/graal/com.oracle.graal.truffle/src/com/oracle/graal/truffle/substitutions/CompilerDirectivesSubstitutions.java Fri Mar 28 18:33:05 2014 +0100 @@ -71,7 +71,7 @@ public static native void bailout(String reason); @MacroSubstitution(macro = UnsafeTypeCastMacroNode.class, isStatic = true) - public static native Object unsafeCast(Object value, Class clazz, boolean condition); + public static native Object unsafeCast(Object value, Class clazz, boolean condition, boolean nonNull); @MethodSubstitution private static Class getUnsafeFrameType() { diff -r 69375786ef70 -r 73546bd550f0 graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java --- a/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java Fri Mar 28 17:55:46 2014 +0100 +++ b/graal/com.oracle.truffle.api/src/com/oracle/truffle/api/CompilerDirectives.java Fri Mar 28 18:33:05 2014 +0100 @@ -76,7 +76,7 @@ /** * Returns a boolean value indicating whether the method is executed in the interpreter. - * + * * @return {@code true} when executed in the interpreter, {@code false} in compiled code. */ public static boolean inInterpreter() { @@ -86,7 +86,7 @@ /** * Directive for the compiler that the given runnable should only be executed in the interpreter * and ignored in the compiled code. - * + * * @param runnable the closure that should only be executed in the interpreter */ public static void interpreterOnly(Runnable runnable) { @@ -96,7 +96,7 @@ /** * Directive for the compiler that the given callable should only be executed in the * interpreter. - * + * * @param callable the closure that should only be executed in the interpreter * @return the result of executing the closure in the interpreter and null in the compiled code * @throws Exception If the closure throws an exception when executed in the interpreter. @@ -109,30 +109,30 @@ * Injects a probability for the given condition into the probability information of the * immediately succeeding branch instruction for the condition. The probability must be a value * between 0.0 and 1.0 (inclusive). The condition should not be a combined condition. - * + * * Example usage immediately before an if statement (it specifies that the likelihood for a to * be greater than b is 90%): - * + * * * if (injectBranchProbability(0.9, a > b)) { * // ... * } * - * + * * Example usage for a combined condition (it specifies that the likelihood for a to be greater * than b is 90% and under the assumption that this is true, the likelihood for a being 0 is * 10%): - * + * * * if (injectBranchProbability(0.9, a > b) && injectBranchProbability(0.1, a == 0)) { * // ... * } * - * + * * There are predefined constants for commonly used probabilities (see * {@link #LIKELY_PROBABILITY} , {@link #UNLIKELY_PROBABILITY}, {@link #SLOWPATH_PROBABILITY}, * {@link #FASTPATH_PROBABILITY} ). - * + * * @param probability the probability value between 0.0 and 1.0 that should be injected */ public static boolean injectBranchProbability(double probability, boolean condition) { @@ -142,7 +142,7 @@ /** * Bails out of a compilation (e.g., for guest language features that should never be compiled). - * + * * @param reason the reason for the bailout */ public static void bailout(String reason) { @@ -161,21 +161,37 @@ * Casts the given value to the value of the given type without any checks. The class must * evaluate to a constant. The condition parameter gives a hint to the compiler under which * circumstances this cast can be moved to an earlier location in the program. - * + * * @param value the value that is known to have the specified type * @param type the specified new type of the value * @param condition the condition that makes this cast safe also at an earlier location of the * program * @return the value to be casted to the new type */ + public static T unsafeCast(Object value, Class type, boolean condition) { + return unsafeCast(value, type, condition, false); + } + + /** + * Casts the given value to the value of the given type without any checks. The class must + * evaluate to a constant. The condition parameter gives a hint to the compiler under which + * circumstances this cast can be moved to an earlier location in the program. + * + * @param value the value that is known to have the specified type + * @param type the specified new type of the value + * @param condition the condition that makes this cast safe also at an earlier location of the + * program + * @param nonNull whether value is known to never be null + * @return the value to be casted to the new type + */ @SuppressWarnings("unchecked") - public static T unsafeCast(Object value, Class type, boolean condition) { + public static T unsafeCast(Object value, Class type, boolean condition, boolean nonNull) { return (T) value; } /** * Asserts that this value is not null and retrieved from a call to Frame.materialize. - * + * * @param value the value that is known to have been obtained via Frame.materialize * @return the value to be casted to the new type */ @@ -192,7 +208,7 @@ * the compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -210,7 +226,7 @@ * compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -228,7 +244,7 @@ * compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -246,7 +262,7 @@ * compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -264,7 +280,7 @@ * compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -282,7 +298,7 @@ * compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -300,7 +316,7 @@ * compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -318,7 +334,7 @@ * the compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -334,7 +350,7 @@ /** * Write a boolean value within an object. The location identity gives a hint to the compiler * for improved global value numbering. - * + * * @param receiver the object that is written to * @param offset the offset at which to write to the object in bytes * @param value the value to be written @@ -348,7 +364,7 @@ /** * Write a byte value within an object. The location identity gives a hint to the compiler for * improved global value numbering. - * + * * @param receiver the object that is written to * @param offset the offset at which to write to the object in bytes * @param value the value to be written @@ -362,7 +378,7 @@ /** * Write a short value within an object. The location identity gives a hint to the compiler for * improved global value numbering. - * + * * @param receiver the object that is written to * @param offset the offset at which to write to the object in bytes * @param value the value to be written @@ -376,7 +392,7 @@ /** * Write an int value within an object. The location identity gives a hint to the compiler for * improved global value numbering. - * + * * @param receiver the object that is written to * @param offset the offset at which to write to the object in bytes * @param value the value to be written @@ -390,7 +406,7 @@ /** * Write a long value within an object. The location identity gives a hint to the compiler for * improved global value numbering. - * + * * @param receiver the object that is written to * @param offset the offset at which to write to the object in bytes * @param value the value to be written @@ -404,7 +420,7 @@ /** * Write a float value within an object. The location identity gives a hint to the compiler for * improved global value numbering. - * + * * @param receiver the object that is written to * @param offset the offset at which to write to the object in bytes * @param value the value to be written @@ -418,7 +434,7 @@ /** * Write a double value within an object. The location identity gives a hint to the compiler for * improved global value numbering. - * + * * @param receiver the object that is written to * @param offset the offset at which to write to the object in bytes * @param value the value to be written @@ -432,7 +448,7 @@ /** * Write an Object value within an object. The location identity gives a hint to the compiler * for improved global value numbering. - * + * * @param receiver the object that is written to * @param offset the offset at which to write to the object in bytes * @param value the value to be written @@ -448,7 +464,7 @@ * to the compiler under which circumstances this access can be moved to an earlier location in * the program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -466,7 +482,7 @@ * the compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -484,7 +500,7 @@ * to the compiler under which circumstances this access can be moved to an earlier location in * the program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -502,7 +518,7 @@ * the compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -520,7 +536,7 @@ * the compiler under which circumstances this access can be moved to an earlier location in the * program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -538,7 +554,7 @@ * to the compiler under which circumstances this access can be moved to an earlier location in * the program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -556,7 +572,7 @@ * to the compiler under which circumstances this access can be moved to an earlier location in * the program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the @@ -574,7 +590,7 @@ * to the compiler under which circumstances this access can be moved to an earlier location in * the program. The location identity gives a hint to the compiler for improved global value * numbering. - * + * * @param receiver the object that is accessed * @param offset the offset at which to access the object in bytes * @param condition the condition that makes this access safe also at an earlier location in the