use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class UnsafeAutomaticSubstitutionProcessor method processUnsafeObjectFieldOffsetInvoke.
/**
* Process call to {@link sun.misc.Unsafe#objectFieldOffset(Field)}. The matching logic below
* applies to the following code pattern:
*
* <code> static final long fieldOffset = Unsafe.getUnsafe().objectFieldOffset(X.class.getDeclaredField("f")); </code>
*/
private void processUnsafeObjectFieldOffsetInvoke(ResolvedJavaType type, Invoke unsafeObjectFieldOffsetInvoke) {
SnippetReflectionProvider snippetReflectionProvider = GraalAccess.getOriginalSnippetReflection();
List<String> unsuccessfulReasons = new ArrayList<>();
Class<?> targetFieldHolder = null;
String targetFieldName = null;
ValueNode fieldArgument = unsafeObjectFieldOffsetInvoke.callTarget().arguments().get(1);
if (fieldArgument instanceof Invoke && isInvokeTo((Invoke) fieldArgument, classGetDeclaredFieldMethod)) {
Invoke getDeclaredFieldInvoke = (Invoke) fieldArgument;
/*
* If the first argument of Unsafe.objectFieldOffset() is a call to
* Class.getDeclaredField() and the arguments of Class.getDeclaredField(), i.e., the
* class and the field name, are constants then unwrap them.
*/
ValueNode holderClassNode = getDeclaredFieldInvoke.callTarget().arguments().get(0);
if (holderClassNode.isJavaConstant()) {
targetFieldHolder = snippetReflectionProvider.asObject(Class.class, holderClassNode.asJavaConstant());
} else {
unsuccessfulReasons.add("The receiver of the call to Class.getDeclaredField(), i.e., the field holder class, is not a constant.");
}
ValueNode fieldNameNode = getDeclaredFieldInvoke.callTarget().arguments().get(1);
if (fieldNameNode.isJavaConstant()) {
targetFieldName = snippetReflectionProvider.asObject(String.class, fieldNameNode.asJavaConstant());
} else {
unsuccessfulReasons.add("The first argument of the call to Class.getDeclaredField(), i.e., the field name, is not a constant.");
}
} else {
unsuccessfulReasons.add("The first argument of Unsafe.objectFieldOffset() is not a call to Class.getDeclaredField()");
}
/*
* If the value returned by the call to Unsafe.objectFieldOffset() is stored into a field
* then that must be the offset field.
*/
ResolvedJavaField offsetField = extractValueStoreField(unsafeObjectFieldOffsetInvoke.asNode(), unsuccessfulReasons);
/*
* If the target field holder and name, and the offset field were found try to register a
* substitution.
*/
if (targetFieldHolder != null && targetFieldName != null && offsetField != null) {
Class<?> finalTargetFieldHolder = targetFieldHolder;
String finalTargetFieldName = targetFieldName;
Supplier<ComputedValueField> supplier = () -> new ComputedValueField(offsetField, null, Kind.FieldOffset, finalTargetFieldHolder, finalTargetFieldName, false);
if (tryAutomaticRecomputation(offsetField, Kind.FieldOffset, supplier)) {
reportSuccessfulAutomaticRecomputation(Kind.FieldOffset, offsetField, targetFieldHolder.getName() + "." + targetFieldName);
}
} else {
reportUnsuccessfulAutomaticRecomputation(type, unsafeObjectFieldOffsetInvoke, Kind.FieldOffset, unsuccessfulReasons);
}
}
use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class GraalTruffleRuntime method getCallTargetForCallNode.
@Override
public JavaConstant getCallTargetForCallNode(JavaConstant callNodeConstant) {
SnippetReflectionProvider snippetReflection = getGraalRuntime().getRequiredCapability(SnippetReflectionProvider.class);
OptimizedDirectCallNode callNode = snippetReflection.asObject(OptimizedDirectCallNode.class, callNodeConstant);
return snippetReflection.forObject(callNode.getCallTarget());
}
use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class TruffleInlining method getPosition.
@Override
public SourceLanguagePosition getPosition(JavaConstant node) {
SnippetReflectionProvider snippetReflection = runtime().getGraalRuntime().getRequiredCapability(SnippetReflectionProvider.class);
Node truffleNode = snippetReflection.asObject(Node.class, node);
if (truffleNode == null) {
return null;
}
SourceSection section = null;
if (truffleNode instanceof DirectCallNode) {
section = ((DirectCallNode) truffleNode).getCurrentRootNode().getSourceSection();
}
if (section == null) {
section = truffleNode.getSourceSection();
}
if (section == null) {
Node cur = truffleNode.getParent();
while (cur != null) {
section = cur.getSourceSection();
if (section != null) {
break;
}
cur = cur.getParent();
}
}
if (section != null) {
return new TruffleSourceLanguagePosition(section);
}
return null;
}
use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class TruffleInlining method findDecision.
@Override
public Decision findDecision(JavaConstant callNodeConstant) {
SnippetReflectionProvider snippetReflection = runtime().getGraalRuntime().getRequiredCapability(SnippetReflectionProvider.class);
OptimizedDirectCallNode callNode = snippetReflection.asObject(OptimizedDirectCallNode.class, callNodeConstant);
return findByCall(callNode);
}
use of org.graalvm.compiler.api.replacements.SnippetReflectionProvider in project graal by oracle.
the class SharedRuntimeConfigurationBuilder method build.
public SharedRuntimeConfigurationBuilder build() {
wordTypes = new WordTypes(metaAccess, FrameAccess.getWordKind());
Providers p = createProviders(null, null, null, null, null, null, null, null);
StampProvider stampProvider = createStampProvider(p);
p = createProviders(null, null, null, null, null, null, stampProvider, null);
ConstantReflectionProvider constantReflection = createConstantReflectionProvider(p);
ConstantFieldProvider constantFieldProvider = createConstantFieldProvider(p);
createProviders(null, constantReflection, constantFieldProvider, null, null, null, stampProvider, null);
SnippetReflectionProvider snippetReflection = createSnippetReflectionProvider();
createProviders(null, constantReflection, constantFieldProvider, null, null, null, stampProvider, snippetReflection);
ForeignCallsProvider foreignCalls = createForeignCallsProvider();
p = createProviders(null, constantReflection, constantFieldProvider, foreignCalls, null, null, stampProvider, snippetReflection);
LoweringProvider lowerer = createLoweringProvider(p);
p = createProviders(null, constantReflection, constantFieldProvider, foreignCalls, lowerer, null, stampProvider, snippetReflection);
Replacements replacements = createReplacements(p, snippetReflection);
p = createProviders(null, constantReflection, constantFieldProvider, foreignCalls, lowerer, replacements, stampProvider, snippetReflection);
EnumMap<ConfigKind, Backend> backends = new EnumMap<>(ConfigKind.class);
for (ConfigKind config : ConfigKind.values()) {
RegisterConfig registerConfig = new SubstrateAMD64RegisterConfig(config, metaAccess, ConfigurationValues.getTarget());
CodeCacheProvider codeCacheProvider = createCodeCacheProvider(registerConfig);
Providers newProviders = createProviders(codeCacheProvider, constantReflection, constantFieldProvider, foreignCalls, lowerer, replacements, stampProvider, snippetReflection);
backends.put(config, GraalConfiguration.instance().createBackend(newProviders));
}
runtimeConfig = new RuntimeConfiguration(p, snippetReflection, backends, wordTypes);
return this;
}
Aggregations