use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class UnsafeAutomaticSubstitutionProcessor method processUnsafeArrayBaseOffsetInvoke.
/**
* Process call to {@link sun.misc.Unsafe#arrayBaseOffset(Class)}. The matching logic below
* applies to the following code pattern:
*
* <code> static final long arrayBaseOffsets = Unsafe.getUnsafe().arrayBaseOffset(byte[].class); </code>
*/
private void processUnsafeArrayBaseOffsetInvoke(ResolvedJavaType type, Invoke unsafeArrayBaseOffsetInvoke) {
SnippetReflectionProvider snippetReflectionProvider = GraalAccess.getOriginalSnippetReflection();
List<String> unsuccessfulReasons = new ArrayList<>();
Class<?> arrayClass = null;
ValueNode arrayClassArgument = unsafeArrayBaseOffsetInvoke.callTarget().arguments().get(1);
if (arrayClassArgument.isJavaConstant()) {
arrayClass = snippetReflectionProvider.asObject(Class.class, arrayClassArgument.asJavaConstant());
} else {
unsuccessfulReasons.add("The first argument of the call to Unsafe.arrayBaseOffset() is not a constant.");
}
/*
* If the value returned by the call to Unsafe.arrayBaseOffset() is stored into a field then
* that must be the offset field.
*/
ResolvedJavaField offsetField = extractValueStoreField(unsafeArrayBaseOffsetInvoke.asNode(), unsuccessfulReasons);
if (arrayClass != null && offsetField != null) {
Class<?> finalArrayClass = arrayClass;
Supplier<ComputedValueField> supplier = () -> new ComputedValueField(offsetField, null, Kind.ArrayBaseOffset, finalArrayClass, null, true);
if (tryAutomaticRecomputation(offsetField, Kind.ArrayBaseOffset, supplier)) {
reportSuccessfulAutomaticRecomputation(Kind.ArrayBaseOffset, offsetField, arrayClass.getCanonicalName());
}
} else {
reportUnsuccessfulAutomaticRecomputation(type, unsafeArrayBaseOffsetInvoke, Kind.ArrayBaseOffset, unsuccessfulReasons);
}
}
use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class ComputedValueField method readValue.
@Override
public JavaConstant readValue(JavaConstant receiver) {
if (constantValue != null) {
return constantValue;
}
if (kind == RecomputeFieldValue.Kind.None || kind == RecomputeFieldValue.Kind.Manual) {
return ReadableJavaField.readFieldValue(GraalAccess.getOriginalProviders().getConstantReflection(), original, receiver);
}
JavaConstant result = valueCache.get(receiver);
if (result != null) {
return result;
}
SnippetReflectionProvider originalSnippetReflection = GraalAccess.getOriginalSnippetReflection();
switch(kind) {
case ArrayBaseOffset:
constantValue = asConstant(ConfigurationValues.getObjectLayout().getArrayBaseOffset(JavaKind.fromJavaClass(targetClass.getComponentType())));
return constantValue;
case ArrayIndexScale:
constantValue = asConstant(ConfigurationValues.getObjectLayout().getArrayIndexScale(JavaKind.fromJavaClass(targetClass.getComponentType())));
return constantValue;
case ArrayIndexShift:
constantValue = asConstant(ConfigurationValues.getObjectLayout().getArrayIndexShift(JavaKind.fromJavaClass(targetClass.getComponentType())));
return constantValue;
case NewInstance:
try {
Constructor<?> constructor = targetClass.getDeclaredConstructor();
constructor.setAccessible(true);
result = originalSnippetReflection.forObject(constructor.newInstance());
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException ex) {
throw shouldNotReachHere("Error performing field recomputation for alias " + annotated.format("%H.%n"), ex);
}
break;
case AtomicFieldUpdaterOffset:
result = computeAtomicFieldUpdaterOffset(receiver);
break;
case TranslateFieldOffset:
result = translateFieldOffset(receiver, targetClass);
break;
case Custom:
try {
Constructor<?>[] constructors = targetClass.getDeclaredConstructors();
if (constructors.length != 1) {
throw shouldNotReachHere("The " + CustomFieldValueComputer.class.getSimpleName() + " class " + targetClass.getName() + " has more than one constructor");
}
Constructor<?> constructor = constructors[0];
Object[] constructorArgs = new Object[constructor.getParameterCount()];
for (int i = 0; i < constructorArgs.length; i++) {
constructorArgs[i] = configurationValue(constructor.getParameterTypes()[i]);
}
constructor.setAccessible(true);
CustomFieldValueComputer computer = (CustomFieldValueComputer) constructor.newInstance(constructorArgs);
Object receiverValue = receiver == null ? null : originalSnippetReflection.asObject(Object.class, receiver);
result = originalSnippetReflection.forBoxed(annotated.getJavaKind(), computer.compute(original, annotated, receiverValue));
assert result.getJavaKind() == annotated.getJavaKind();
} catch (InvocationTargetException | InstantiationException | IllegalAccessException ex) {
throw shouldNotReachHere("Error performing field recomputation for alias " + annotated.format("%H.%n"), ex);
}
break;
default:
throw shouldNotReachHere("Field recomputation of kind " + kind + " specified by alias " + annotated.format("%H.%n") + " not yet supported");
}
valueCache.put(receiver, result);
return result;
}
use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class ComputedValueField method computeAtomicFieldUpdaterOffset.
private JavaConstant computeAtomicFieldUpdaterOffset(JavaConstant receiver) {
assert !Modifier.isStatic(original.getModifiers());
assert receiver.isNonNull();
/*
* Explanation: java.util.concurrent is full of objects and classes that cache the offset of
* particular fields in the JDK. Here, Atomic<X>FieldUpdater implementation objects cache
* the offset of some specified field. Which field? Oh...only Doug Lea knows that. We have
* to search the declaring class for a field that has the same "unsafe" offset as the cached
* offset in this atomic updater object.
*/
ResolvedJavaField tclassField = findField(original.getDeclaringClass(), "tclass");
SnippetReflectionProvider originalSnippetReflection = GraalAccess.getOriginalSnippetReflection();
Class<?> tclass = originalSnippetReflection.asObject(Class.class, ReadableJavaField.readFieldValue(GraalAccess.getOriginalProviders().getConstantReflection(), tclassField, receiver));
return translateFieldOffset(receiver, tclass);
}
use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class IntrinsifyMethodHandlesInvocationPlugin method processInvokeWithMethodHandle.
@SuppressWarnings("try")
private void processInvokeWithMethodHandle(GraphBuilderContext b, BytecodeProvider bytecodeProvider, ResolvedJavaMethod methodHandleMethod, ValueNode[] methodHandleArguments) {
Plugins graphBuilderPlugins = new Plugins(((ReplacementsImpl) originalProviders.getReplacements()).getGraphBuilderPlugins());
registerInvocationPlugins(graphBuilderPlugins.getInvocationPlugins(), bytecodeProvider);
graphBuilderPlugins.prependParameterPlugin(new MethodHandlesParameterPlugin(methodHandleArguments));
graphBuilderPlugins.clearInlineInvokePlugins();
graphBuilderPlugins.prependInlineInvokePlugin(new MethodHandlesInlineInvokePlugin());
graphBuilderPlugins.prependNodePlugin(new MethodHandlePlugin(originalProviders.getConstantReflection().getMethodHandleAccess(), false));
/* We do all the word type rewriting because parameters to the lambda can be word types. */
SnippetReflectionProvider originalSnippetReflection = GraalAccess.getOriginalSnippetReflection();
WordOperationPlugin wordOperationPlugin = new WordOperationPlugin(originalSnippetReflection, new WordTypes(originalProviders.getMetaAccess(), FrameAccess.getWordKind()));
graphBuilderPlugins.appendInlineInvokePlugin(wordOperationPlugin);
graphBuilderPlugins.appendTypePlugin(wordOperationPlugin);
graphBuilderPlugins.appendTypePlugin(new TrustedInterfaceTypePlugin());
graphBuilderPlugins.appendNodePlugin(wordOperationPlugin);
GraphBuilderConfiguration graphBuilderConfig = GraphBuilderConfiguration.getSnippetDefault(graphBuilderPlugins);
GraphBuilderPhase.Instance graphBuilder = new GraphBuilderPhase.Instance(originalProviders.getMetaAccess(), originalProviders.getStampProvider(), originalProviders.getConstantReflection(), originalProviders.getConstantFieldProvider(), graphBuilderConfig, OptimisticOptimizations.NONE, null);
DebugContext debug = b.getDebug();
StructuredGraph graph = new StructuredGraph.Builder(b.getOptions(), debug).method(toOriginal(methodHandleMethod)).build();
try (DebugContext.Scope s = debug.scope("IntrinsifyMethodHandles", graph)) {
graphBuilder.apply(graph);
/*
* We do not care about the improved type information from Pi nodes, so we just delete
* them to simplify our graph.
*/
for (PiNode pi : graph.getNodes(PiNode.TYPE)) {
pi.replaceAndDelete(pi.object());
}
/*
* Support for MethodHandle that adapt the input type to a more generic type, i.e., a
* MethodHandle that does a dynamic type check on a parameter.
*/
for (UnaryOpLogicNode node : graph.getNodes().filter(UnaryOpLogicNode.class).filter(v -> v instanceof IsNullNode || v instanceof InstanceOfNode)) {
ValueNode value = node.getValue();
if (value instanceof ParameterNode) {
/*
* We just assume that the InstanceOfNode or IsNullNode are used in an If and
* the true-successor is actually the branch we want. If that assumption is
* wrong, nothing bad happens - we will just continue to report the invocation
* as unsupported because the updated stamp for the parameter will not simplify
* the graph.
*/
if (node instanceof InstanceOfNode) {
InstanceOfNode inst = (InstanceOfNode) node;
TypeReference typeRef = inst.type();
value.setStamp(new ObjectStamp(typeRef.getType(), typeRef.isExact(), !inst.allowsNull(), false));
} else {
assert node instanceof IsNullNode;
ResolvedJavaType type = value.stamp(NodeView.DEFAULT).javaType(originalProviders.getMetaAccess());
value.setStamp(new ObjectStamp(type, false, /* non-null */
true, false));
}
}
}
/*
* The canonicalizer converts unsafe field accesses for get/set method handles back to
* high-level field load and store nodes.
*/
new CanonicalizerPhase().apply(graph, new PhaseContext(originalProviders));
for (FixedGuardNode guard : graph.getNodes(FixedGuardNode.TYPE)) {
if (guard.next() instanceof AccessFieldNode && guard.condition() instanceof IsNullNode && guard.isNegated() && ((IsNullNode) guard.condition()).getValue() == ((AccessFieldNode) guard.next()).object()) {
/*
* Method handles to load and stores fields have null checks. Remove them, since
* the null check is implicitly done by the field access.
*/
GraphUtil.removeFixedWithUnusedInputs(guard);
}
}
debug.dump(DebugContext.VERY_DETAILED_LEVEL, graph, "Final intrinisfication graph");
/*
* After parsing (and recursive inlining during parsing), the graph must contain only
* one invocation (and therefore only one MethodCallTargetNode), plus the parameters,
* constants, start, and return nodes.
*/
Node singleFunctionality = null;
ReturnNode singleReturn = null;
for (Node node : graph.getNodes()) {
if (node == graph.start() || node instanceof ParameterNode || node instanceof ConstantNode || node instanceof FrameState) {
/* Ignore the allowed framework around the nodes we care about. */
continue;
} else if (node instanceof Invoke) {
/* We check the MethodCallTargetNode, so we can ignore the invoke. */
continue;
} else if ((node instanceof MethodCallTargetNode || node instanceof LoadFieldNode || node instanceof StoreFieldNode) && singleFunctionality == null) {
singleFunctionality = node;
continue;
} else if (node instanceof ReturnNode && singleReturn == null) {
singleReturn = (ReturnNode) node;
continue;
}
throw new UnsupportedFeatureException("Invoke with MethodHandle argument could not be reduced to at most a single call: " + methodHandleMethod.format("%H.%n(%p)"));
}
if (singleFunctionality instanceof MethodCallTargetNode) {
MethodCallTargetNode singleCallTarget = (MethodCallTargetNode) singleFunctionality;
assert singleReturn.result() == null || singleReturn.result() == singleCallTarget.invoke();
/*
* Replace the originalTarget with the replacementTarget. Note that the
* replacementTarget node belongs to a different graph than originalTarget, so we
* need to match parameter back to the original graph and allocate a new
* MethodCallTargetNode for the original graph.
*/
ValueNode[] replacedArguments = new ValueNode[singleCallTarget.arguments().size()];
for (int i = 0; i < replacedArguments.length; i++) {
replacedArguments[i] = lookup(b, methodHandleArguments, singleCallTarget.arguments().get(i));
}
b.handleReplacedInvoke(singleCallTarget.invokeKind(), lookup(singleCallTarget.targetMethod()), replacedArguments, false);
} else if (singleFunctionality instanceof LoadFieldNode) {
LoadFieldNode fieldLoad = (LoadFieldNode) singleFunctionality;
b.addPush(b.getInvokeReturnType().getJavaKind(), LoadFieldNode.create(null, lookup(b, methodHandleArguments, fieldLoad.object()), lookup(fieldLoad.field())));
} else if (singleFunctionality instanceof StoreFieldNode) {
StoreFieldNode fieldStore = (StoreFieldNode) singleFunctionality;
b.add(new StoreFieldNode(lookup(b, methodHandleArguments, fieldStore.object()), lookup(fieldStore.field()), lookup(b, methodHandleArguments, fieldStore.value())));
} else if (singleReturn.result() != null) {
/* Replace the invocation with he constant result. */
JavaConstant constantResult = singleReturn.result().asJavaConstant();
assert b.getInvokeReturnType().getJavaKind() == constantResult.getJavaKind();
b.addPush(constantResult.getJavaKind(), ConstantNode.forConstant(lookup(constantResult), universeProviders.getMetaAccess()));
} else {
/* No invoke and no return value, so nothing to do. */
assert b.getInvokeReturnType().getJavaKind() == JavaKind.Void;
}
} catch (Throwable ex) {
throw debug.handle(ex);
}
}
use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class UnsafeAutomaticSubstitutionProcessor method processUnsafeArrayIndexScaleInvoke.
/**
* Process call to {@link sun.misc.Unsafe#arrayIndexScale(Class)}. The matching logic below
* applies to the following code pattern:
*
* <code> static final long byteArrayIndexScale = Unsafe.getUnsafe().arrayIndexScale(byte[].class); </code>
*/
private void processUnsafeArrayIndexScaleInvoke(ResolvedJavaType type, Invoke unsafeArrayIndexScale, StructuredGraph clinitGraph) {
SnippetReflectionProvider snippetReflectionProvider = GraalAccess.getOriginalSnippetReflection();
List<String> unsuccessfulReasons = new ArrayList<>();
Class<?> arrayClass = null;
ValueNode arrayClassArgument = unsafeArrayIndexScale.callTarget().arguments().get(1);
if (arrayClassArgument.isJavaConstant()) {
arrayClass = snippetReflectionProvider.asObject(Class.class, arrayClassArgument.asJavaConstant());
} else {
unsuccessfulReasons.add("The first argument of the call to Unsafe.arrayIndexScale() is not a constant.");
}
/*
* If the value returned by the call to Unsafe.unsafeArrayIndexScale() is stored into a
* field then that must be the offset field.
*/
ResolvedJavaField indexScaleField = extractValueStoreField(unsafeArrayIndexScale.asNode(), unsuccessfulReasons);
boolean indexScaleComputed = false;
boolean indexShiftComputed = false;
if (arrayClass != null) {
if (indexScaleField != null) {
Class<?> finalArrayClass = arrayClass;
Supplier<ComputedValueField> supplier = () -> new ComputedValueField(indexScaleField, null, Kind.ArrayIndexScale, finalArrayClass, null, true);
if (tryAutomaticRecomputation(indexScaleField, Kind.ArrayIndexScale, supplier)) {
reportSuccessfulAutomaticRecomputation(Kind.ArrayIndexScale, indexScaleField, arrayClass.getCanonicalName());
indexScaleComputed = true;
/* Try substitution for the array index shift computation if present. */
indexShiftComputed = processArrayIndexShiftFromField(type, indexScaleField, arrayClass, clinitGraph);
}
} else {
/*
* The index scale is not stored into a field, it might be used to compute the index
* shift.
*/
indexShiftComputed = processArrayIndexShiftFromLocal(type, unsafeArrayIndexScale, arrayClass);
}
}
if (!indexScaleComputed && !indexShiftComputed) {
reportUnsuccessfulAutomaticRecomputation(type, unsafeArrayIndexScale, Kind.ArrayIndexScale, unsuccessfulReasons);
}
}
Aggregations