Search in sources :

Example 1 with ConstantFieldProvider

use of org.graalvm.compiler.core.common.spi.ConstantFieldProvider in project graal by oracle.

the class AMD64HotSpotBackendFactory method createBackend.

@Override
@SuppressWarnings("try")
public HotSpotBackend createBackend(HotSpotGraalRuntimeProvider graalRuntime, CompilerConfiguration compilerConfiguration, HotSpotJVMCIRuntimeProvider jvmciRuntime, HotSpotBackend host) {
    assert host == null;
    OptionValues options = graalRuntime.getOptions();
    JVMCIBackend jvmci = jvmciRuntime.getHostJVMCIBackend();
    GraalHotSpotVMConfig config = graalRuntime.getVMConfig();
    HotSpotProviders providers;
    HotSpotRegistersProvider registers;
    HotSpotCodeCacheProvider codeCache = (HotSpotCodeCacheProvider) jvmci.getCodeCache();
    TargetDescription target = codeCache.getTarget();
    HotSpotHostForeignCallsProvider foreignCalls;
    Value[] nativeABICallerSaveRegisters;
    HotSpotMetaAccessProvider metaAccess = (HotSpotMetaAccessProvider) jvmci.getMetaAccess();
    HotSpotConstantReflectionProvider constantReflection = (HotSpotConstantReflectionProvider) jvmci.getConstantReflection();
    ConstantFieldProvider constantFieldProvider = new HotSpotGraalConstantFieldProvider(config, metaAccess);
    HotSpotLoweringProvider lowerer;
    HotSpotSnippetReflectionProvider snippetReflection;
    HotSpotReplacementsImpl replacements;
    HotSpotSuitesProvider suites;
    HotSpotWordTypes wordTypes;
    Plugins plugins;
    BytecodeProvider bytecodeProvider;
    try (InitTimer t = timer("create providers")) {
        try (InitTimer rt = timer("create HotSpotRegisters provider")) {
            registers = createRegisters();
        }
        try (InitTimer rt = timer("create NativeABICallerSaveRegisters")) {
            nativeABICallerSaveRegisters = createNativeABICallerSaveRegisters(config, codeCache.getRegisterConfig());
        }
        try (InitTimer rt = timer("create WordTypes")) {
            wordTypes = new HotSpotWordTypes(metaAccess, target.wordJavaKind);
        }
        try (InitTimer rt = timer("create ForeignCalls provider")) {
            foreignCalls = createForeignCalls(jvmciRuntime, graalRuntime, metaAccess, codeCache, wordTypes, nativeABICallerSaveRegisters);
        }
        try (InitTimer rt = timer("create Lowerer provider")) {
            lowerer = createLowerer(graalRuntime, metaAccess, foreignCalls, registers, constantReflection, target);
        }
        HotSpotStampProvider stampProvider = new HotSpotStampProvider();
        Providers p = new Providers(metaAccess, codeCache, constantReflection, constantFieldProvider, foreignCalls, lowerer, null, stampProvider);
        try (InitTimer rt = timer("create SnippetReflection provider")) {
            snippetReflection = createSnippetReflection(graalRuntime, constantReflection, wordTypes);
        }
        try (InitTimer rt = timer("create Bytecode provider")) {
            bytecodeProvider = new ClassfileBytecodeProvider(metaAccess, snippetReflection);
        }
        try (InitTimer rt = timer("create Replacements provider")) {
            replacements = createReplacements(options, p, snippetReflection, bytecodeProvider);
        }
        try (InitTimer rt = timer("create GraphBuilderPhase plugins")) {
            plugins = createGraphBuilderPlugins(compilerConfiguration, config, options, target, constantReflection, foreignCalls, lowerer, metaAccess, snippetReflection, replacements, wordTypes, stampProvider);
            replacements.setGraphBuilderPlugins(plugins);
        }
        try (InitTimer rt = timer("create Suites provider")) {
            suites = createSuites(config, graalRuntime, compilerConfiguration, plugins, registers, replacements, options);
        }
        providers = new HotSpotProviders(metaAccess, codeCache, constantReflection, constantFieldProvider, foreignCalls, lowerer, replacements, suites, registers, snippetReflection, wordTypes, plugins);
    }
    try (InitTimer rt = timer("instantiate backend")) {
        return createBackend(config, graalRuntime, providers);
    }
}
Also used : InitTimer(jdk.vm.ci.common.InitTimer) OptionValues(org.graalvm.compiler.options.OptionValues) JVMCIBackend(jdk.vm.ci.runtime.JVMCIBackend) HotSpotProviders(org.graalvm.compiler.hotspot.meta.HotSpotProviders) TargetDescription(jdk.vm.ci.code.TargetDescription) HotSpotProviders(org.graalvm.compiler.hotspot.meta.HotSpotProviders) Providers(org.graalvm.compiler.phases.util.Providers) HotSpotReplacementsImpl(org.graalvm.compiler.hotspot.HotSpotReplacementsImpl) HotSpotRegistersProvider(org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider) HotSpotSuitesProvider(org.graalvm.compiler.hotspot.meta.HotSpotSuitesProvider) AddressLoweringHotSpotSuitesProvider(org.graalvm.compiler.hotspot.meta.AddressLoweringHotSpotSuitesProvider) GraalHotSpotVMConfig(org.graalvm.compiler.hotspot.GraalHotSpotVMConfig) HotSpotStampProvider(org.graalvm.compiler.hotspot.meta.HotSpotStampProvider) HotSpotGraphBuilderPlugins(org.graalvm.compiler.hotspot.meta.HotSpotGraphBuilderPlugins) Plugins(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins) AMD64GraphBuilderPlugins(org.graalvm.compiler.replacements.amd64.AMD64GraphBuilderPlugins) ClassfileBytecodeProvider(org.graalvm.compiler.replacements.classfile.ClassfileBytecodeProvider) HotSpotHostForeignCallsProvider(org.graalvm.compiler.hotspot.meta.HotSpotHostForeignCallsProvider) HotSpotCodeCacheProvider(jdk.vm.ci.hotspot.HotSpotCodeCacheProvider) BytecodeProvider(org.graalvm.compiler.bytecode.BytecodeProvider) ClassfileBytecodeProvider(org.graalvm.compiler.replacements.classfile.ClassfileBytecodeProvider) HotSpotMetaAccessProvider(jdk.vm.ci.hotspot.HotSpotMetaAccessProvider) HotSpotSnippetReflectionProvider(org.graalvm.compiler.hotspot.meta.HotSpotSnippetReflectionProvider) HotSpotLoweringProvider(org.graalvm.compiler.hotspot.meta.HotSpotLoweringProvider) HotSpotWordTypes(org.graalvm.compiler.hotspot.word.HotSpotWordTypes) Value(jdk.vm.ci.meta.Value) HotSpotConstantReflectionProvider(jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider) HotSpotGraalConstantFieldProvider(org.graalvm.compiler.hotspot.meta.HotSpotGraalConstantFieldProvider) HotSpotGraalConstantFieldProvider(org.graalvm.compiler.hotspot.meta.HotSpotGraalConstantFieldProvider) ConstantFieldProvider(org.graalvm.compiler.core.common.spi.ConstantFieldProvider)

Example 2 with ConstantFieldProvider

use of org.graalvm.compiler.core.common.spi.ConstantFieldProvider in project graal by oracle.

the class ReplacementsImpl method getSubstitution.

@Override
public StructuredGraph getSubstitution(ResolvedJavaMethod method, int invokeBci, boolean trackNodeSourcePosition, NodeSourcePosition replaceePosition) {
    StructuredGraph result;
    InvocationPlugin plugin = graphBuilderPlugins.getInvocationPlugins().lookupInvocation(method);
    if (plugin != null && (!plugin.inlineOnly() || invokeBci >= 0)) {
        MetaAccessProvider metaAccess = providers.getMetaAccess();
        if (plugin instanceof MethodSubstitutionPlugin) {
            MethodSubstitutionPlugin msPlugin = (MethodSubstitutionPlugin) plugin;
            ResolvedJavaMethod substitute = msPlugin.getSubstitute(metaAccess);
            StructuredGraph graph = UseSnippetGraphCache.getValue(options) ? graphs.get(substitute) : null;
            if (graph == null || graph.trackNodeSourcePosition() != trackNodeSourcePosition) {
                try (DebugContext debug = openDebugContext("Substitution_", method)) {
                    graph = makeGraph(debug, msPlugin.getBytecodeProvider(), substitute, null, method, trackNodeSourcePosition, replaceePosition);
                    if (!UseSnippetGraphCache.getValue(options)) {
                        return graph;
                    }
                    graph.freeze();
                    graphs.putIfAbsent(substitute, graph);
                    graph = graphs.get(substitute);
                }
            }
            assert graph.isFrozen();
            result = graph;
        } else {
            Bytecode code = new ResolvedJavaMethodBytecode(method);
            ConstantReflectionProvider constantReflection = providers.getConstantReflection();
            ConstantFieldProvider constantFieldProvider = providers.getConstantFieldProvider();
            StampProvider stampProvider = providers.getStampProvider();
            try (DebugContext debug = openDebugContext("Substitution_", method)) {
                result = new IntrinsicGraphBuilder(options, debug, metaAccess, constantReflection, constantFieldProvider, stampProvider, code, invokeBci).buildGraph(plugin);
            }
        }
    } else {
        result = null;
    }
    return result;
}
Also used : StampProvider(org.graalvm.compiler.nodes.spi.StampProvider) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) ConstantReflectionProvider(jdk.vm.ci.meta.ConstantReflectionProvider) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) GeneratedInvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.GeneratedInvocationPlugin) ResolvedJavaMethodBytecode(org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode) Bytecode(org.graalvm.compiler.bytecode.Bytecode) DebugContext(org.graalvm.compiler.debug.DebugContext) MethodSubstitutionPlugin(org.graalvm.compiler.nodes.graphbuilderconf.MethodSubstitutionPlugin) ResolvedJavaMethodBytecode(org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) ConstantFieldProvider(org.graalvm.compiler.core.common.spi.ConstantFieldProvider)

Example 3 with ConstantFieldProvider

use of org.graalvm.compiler.core.common.spi.ConstantFieldProvider 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;
}
Also used : ForeignCallsProvider(org.graalvm.compiler.core.common.spi.ForeignCallsProvider) SubstrateForeignCallsProvider(com.oracle.svm.core.graal.meta.SubstrateForeignCallsProvider) SubstrateStampProvider(com.oracle.svm.core.graal.meta.SubstrateStampProvider) StampProvider(org.graalvm.compiler.nodes.spi.StampProvider) RegisterConfig(jdk.vm.ci.code.RegisterConfig) SubstrateAMD64RegisterConfig(com.oracle.svm.core.graal.code.amd64.SubstrateAMD64RegisterConfig) Replacements(org.graalvm.compiler.nodes.spi.Replacements) WordTypes(org.graalvm.compiler.word.WordTypes) SubstrateAMD64RegisterConfig(com.oracle.svm.core.graal.code.amd64.SubstrateAMD64RegisterConfig) LoweringProvider(org.graalvm.compiler.nodes.spi.LoweringProvider) SubstrateLoweringProvider(com.oracle.svm.core.graal.meta.SubstrateLoweringProvider) ConfigKind(com.oracle.svm.core.graal.code.amd64.SubstrateAMD64RegisterConfig.ConfigKind) Providers(org.graalvm.compiler.phases.util.Providers) SubstrateCodeCacheProvider(com.oracle.svm.core.graal.meta.SubstrateCodeCacheProvider) CodeCacheProvider(jdk.vm.ci.code.CodeCacheProvider) RuntimeConfiguration(com.oracle.svm.core.graal.meta.RuntimeConfiguration) SubstrateSnippetReflectionProvider(com.oracle.svm.core.graal.meta.SubstrateSnippetReflectionProvider) SnippetReflectionProvider(org.graalvm.compiler.api.replacements.SnippetReflectionProvider) Backend(org.graalvm.compiler.core.target.Backend) ConstantReflectionProvider(jdk.vm.ci.meta.ConstantReflectionProvider) EnumMap(java.util.EnumMap) ConstantFieldProvider(org.graalvm.compiler.core.common.spi.ConstantFieldProvider)

Example 4 with ConstantFieldProvider

use of org.graalvm.compiler.core.common.spi.ConstantFieldProvider in project graal by oracle.

the class IntegerSwitchNode method tryOptimizeEnumSwitch.

/**
 * For switch statements on enum values, the Java compiler has to generate complicated code:
 * because {@link Enum#ordinal()} can change when recompiling an enum, it cannot be used
 * directly as the value that is switched on. An intermediate int[] array, which is initialized
 * once at run time based on the actual {@link Enum#ordinal()} values, is used.
 *
 * The {@link ConstantFieldProvider} of Graal already detects the int[] arrays and marks them as
 * {@link ConstantNode#isDefaultStable() stable}, i.e., the array elements are constant. The
 * code in this method detects array loads from such a stable array and re-wires the switch to
 * use the keys from the array elements, so that the array load is unnecessary.
 */
private boolean tryOptimizeEnumSwitch(SimplifierTool tool) {
    if (!(value() instanceof LoadIndexedNode)) {
        /* Not the switch pattern we are looking for. */
        return false;
    }
    LoadIndexedNode loadIndexed = (LoadIndexedNode) value();
    if (loadIndexed.usages().count() > 1) {
        /*
             * The array load is necessary for other reasons too, so there is no benefit optimizing
             * the switch.
             */
        return false;
    }
    assert loadIndexed.usages().first() == this;
    ValueNode newValue = loadIndexed.index();
    JavaConstant arrayConstant = loadIndexed.array().asJavaConstant();
    if (arrayConstant == null || ((ConstantNode) loadIndexed.array()).getStableDimension() != 1 || !((ConstantNode) loadIndexed.array()).isDefaultStable()) {
        /*
             * The array is a constant that we can optimize. We require the array elements to be
             * constant too, since we put them as literal constants into the switch keys.
             */
        return false;
    }
    Integer optionalArrayLength = tool.getConstantReflection().readArrayLength(arrayConstant);
    if (optionalArrayLength == null) {
        /* Loading a constant value can be denied by the VM. */
        return false;
    }
    int arrayLength = optionalArrayLength;
    Map<Integer, List<Integer>> reverseArrayMapping = new HashMap<>();
    for (int i = 0; i < arrayLength; i++) {
        JavaConstant elementConstant = tool.getConstantReflection().readArrayElement(arrayConstant, i);
        if (elementConstant == null || elementConstant.getJavaKind() != JavaKind.Int) {
            /* Loading a constant value can be denied by the VM. */
            return false;
        }
        int element = elementConstant.asInt();
        /*
             * The value loaded from the array is the old switch key, the index into the array is
             * the new switch key. We build a mapping from the old switch key to new keys.
             */
        reverseArrayMapping.computeIfAbsent(element, e -> new ArrayList<>()).add(i);
    }
    /* Build high-level representation of new switch keys. */
    List<KeyData> newKeyDatas = new ArrayList<>(arrayLength);
    ArrayList<AbstractBeginNode> newSuccessors = new ArrayList<>(blockSuccessorCount());
    for (int i = 0; i < keys.length; i++) {
        List<Integer> newKeys = reverseArrayMapping.get(keys[i]);
        if (newKeys == null || newKeys.size() == 0) {
            /* The switch case is unreachable, we can ignore it. */
            continue;
        }
        /*
             * We do not have detailed profiling information about the individual new keys, so we
             * have to assume they split the probability of the old key.
             */
        double newKeyProbability = keyProbabilities[i] / newKeys.size();
        int newKeySuccessor = addNewSuccessor(keySuccessor(i), newSuccessors);
        for (int newKey : newKeys) {
            newKeyDatas.add(new KeyData(newKey, newKeyProbability, newKeySuccessor));
        }
    }
    int newDefaultSuccessor = addNewSuccessor(defaultSuccessor(), newSuccessors);
    double newDefaultProbability = keyProbabilities[keyProbabilities.length - 1];
    /*
         * We remove the array load, but we still need to preserve exception semantics by keeping
         * the bounds check. Fortunately the array length is a constant.
         */
    LogicNode boundsCheck = graph().unique(new IntegerBelowNode(newValue, ConstantNode.forInt(arrayLength, graph())));
    graph().addBeforeFixed(this, graph().add(new FixedGuardNode(boundsCheck, DeoptimizationReason.BoundsCheckException, DeoptimizationAction.InvalidateReprofile)));
    /*
         * Build the low-level representation of the new switch keys and replace ourself with a new
         * node.
         */
    doReplace(newValue, newKeyDatas, newSuccessors, newDefaultSuccessor, newDefaultProbability);
    /* The array load is now unnecessary. */
    assert loadIndexed.hasNoUsages();
    GraphUtil.removeFixedWithUnusedInputs(loadIndexed);
    return true;
}
Also used : DeoptimizationReason(jdk.vm.ci.meta.DeoptimizationReason) Arrays(java.util.Arrays) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) DeoptimizationAction(jdk.vm.ci.meta.DeoptimizationAction) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) NodeLIRBuilderTool(org.graalvm.compiler.nodes.spi.NodeLIRBuilderTool) SimplifierTool(org.graalvm.compiler.graph.spi.SimplifierTool) IntegerStamp(org.graalvm.compiler.core.common.type.IntegerStamp) StampFactory(org.graalvm.compiler.core.common.type.StampFactory) JavaKind(jdk.vm.ci.meta.JavaKind) NodeClass(org.graalvm.compiler.graph.NodeClass) Map(java.util.Map) LoadIndexedNode(org.graalvm.compiler.nodes.java.LoadIndexedNode) IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) NodeInfo(org.graalvm.compiler.nodeinfo.NodeInfo) GraphUtil(org.graalvm.compiler.nodes.util.GraphUtil) NodeView(org.graalvm.compiler.nodes.NodeView) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) PrimitiveStamp(org.graalvm.compiler.core.common.type.PrimitiveStamp) Stamp(org.graalvm.compiler.core.common.type.Stamp) LIRLowerable(org.graalvm.compiler.nodes.spi.LIRLowerable) JavaConstant(jdk.vm.ci.meta.JavaConstant) LogicNode(org.graalvm.compiler.nodes.LogicNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) List(java.util.List) ConstantFieldProvider(org.graalvm.compiler.core.common.spi.ConstantFieldProvider) Simplifiable(org.graalvm.compiler.graph.spi.Simplifiable) Comparator(java.util.Comparator) FixedWithNextNode(org.graalvm.compiler.nodes.FixedWithNextNode) IntegerBelowNode(org.graalvm.compiler.nodes.calc.IntegerBelowNode) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) JavaConstant(jdk.vm.ci.meta.JavaConstant) AbstractBeginNode(org.graalvm.compiler.nodes.AbstractBeginNode) FixedGuardNode(org.graalvm.compiler.nodes.FixedGuardNode) ConstantNode(org.graalvm.compiler.nodes.ConstantNode) LoadIndexedNode(org.graalvm.compiler.nodes.java.LoadIndexedNode) ValueNode(org.graalvm.compiler.nodes.ValueNode) ArrayList(java.util.ArrayList) List(java.util.List) LogicNode(org.graalvm.compiler.nodes.LogicNode)

Aggregations

ConstantFieldProvider (org.graalvm.compiler.core.common.spi.ConstantFieldProvider)4 ConstantReflectionProvider (jdk.vm.ci.meta.ConstantReflectionProvider)2 StampProvider (org.graalvm.compiler.nodes.spi.StampProvider)2 SubstrateAMD64RegisterConfig (com.oracle.svm.core.graal.code.amd64.SubstrateAMD64RegisterConfig)1 ConfigKind (com.oracle.svm.core.graal.code.amd64.SubstrateAMD64RegisterConfig.ConfigKind)1 RuntimeConfiguration (com.oracle.svm.core.graal.meta.RuntimeConfiguration)1 SubstrateCodeCacheProvider (com.oracle.svm.core.graal.meta.SubstrateCodeCacheProvider)1 SubstrateForeignCallsProvider (com.oracle.svm.core.graal.meta.SubstrateForeignCallsProvider)1 SubstrateLoweringProvider (com.oracle.svm.core.graal.meta.SubstrateLoweringProvider)1 SubstrateSnippetReflectionProvider (com.oracle.svm.core.graal.meta.SubstrateSnippetReflectionProvider)1 SubstrateStampProvider (com.oracle.svm.core.graal.meta.SubstrateStampProvider)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Comparator (java.util.Comparator)1 EnumMap (java.util.EnumMap)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CodeCacheProvider (jdk.vm.ci.code.CodeCacheProvider)1 RegisterConfig (jdk.vm.ci.code.RegisterConfig)1