src/jdk.internal.vm.compiler/share/classes/org.graalvm.compiler.hotspot.amd64/src/org/graalvm/compiler/hotspot/amd64/AMD64HotSpotLoweringProvider.java
author jwilhelm
Tue, 12 Mar 2019 19:17:42 +0100
changeset 54084 84f10bbf993f
parent 52910 583fd71c47d6
child 54669 ad45b3802d4e
permissions -rw-r--r--
8218074: Update Graal Reviewed-by: kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     1
/*
52910
583fd71c47d6 8214023: Update Graal
dlong
parents: 50858
diff changeset
     2
 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved.
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     4
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     8
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    13
 * accompanied this code).
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    14
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    18
 *
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    21
 * questions.
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    22
 */
50858
2d3e99a72541 8205824: Update Graal
never
parents: 48861
diff changeset
    23
2d3e99a72541 8205824: Update Graal
never
parents: 48861
diff changeset
    24
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    25
package org.graalvm.compiler.hotspot.amd64;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    26
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    27
import static org.graalvm.compiler.hotspot.HotSpotBackend.Options.GraalArithmeticStubs;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    28
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    29
import org.graalvm.compiler.api.replacements.Snippet;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    30
import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46393
diff changeset
    31
import org.graalvm.compiler.debug.DebugHandlersFactory;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    32
import org.graalvm.compiler.graph.Node;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    33
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    34
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    35
import org.graalvm.compiler.hotspot.meta.DefaultHotSpotLoweringProvider;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    36
import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    37
import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    38
import org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    39
import org.graalvm.compiler.hotspot.replacements.profiling.ProbabilisticProfileSnippets;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    40
import org.graalvm.compiler.nodes.StructuredGraph;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    41
import org.graalvm.compiler.nodes.calc.FloatConvertNode;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    42
import org.graalvm.compiler.nodes.extended.ForeignCallNode;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    43
import org.graalvm.compiler.nodes.spi.LoweringTool;
46344
694c102fd8ed 8177046: Update Graal
iveresov
parents: 43972
diff changeset
    44
import org.graalvm.compiler.options.OptionValues;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    45
import org.graalvm.compiler.replacements.amd64.AMD64ConvertSnippets;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    46
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    47
import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    48
import org.graalvm.compiler.serviceprovider.JavaVersionUtil;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    49
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    50
import jdk.vm.ci.code.TargetDescription;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    51
import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    52
import jdk.vm.ci.meta.MetaAccessProvider;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    53
import jdk.vm.ci.meta.ResolvedJavaMethod;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    54
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    55
public class AMD64HotSpotLoweringProvider extends DefaultHotSpotLoweringProvider {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    56
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    57
    private AMD64ConvertSnippets.Templates convertSnippets;
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    58
    private ProbabilisticProfileSnippets.Templates profileSnippets;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    59
    private AMD64X87MathSnippets.Templates mathSnippets;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    60
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    61
    public AMD64HotSpotLoweringProvider(HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, ForeignCallsProvider foreignCalls, HotSpotRegistersProvider registers,
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    62
                    HotSpotConstantReflectionProvider constantReflection, TargetDescription target) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    63
        super(runtime, metaAccess, foreignCalls, registers, constantReflection, target);
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    64
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    65
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    66
    @Override
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46393
diff changeset
    67
    public void initialize(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, GraalHotSpotVMConfig config) {
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46393
diff changeset
    68
        convertSnippets = new AMD64ConvertSnippets.Templates(options, factories, providers, providers.getSnippetReflection(), providers.getCodeCache().getTarget());
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    69
        profileSnippets = ProfileNode.Options.ProbabilisticProfiling.getValue(options) && !JavaVersionUtil.Java8OrEarlier
48861
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    70
                        ? new ProbabilisticProfileSnippets.Templates(options, factories, providers, providers.getCodeCache().getTarget())
47f19ff9903c 8194819: Update Graal
iveresov
parents: 47216
diff changeset
    71
                        : null;
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    72
        mathSnippets = new AMD64X87MathSnippets.Templates(options, factories, providers, providers.getSnippetReflection(), providers.getCodeCache().getTarget());
46640
70bdce04c59b 8183991: Update Graal
iveresov
parents: 46393
diff changeset
    73
        super.initialize(options, factories, providers, config);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    74
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    75
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    76
    @Override
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    77
    public void lower(Node n, LoweringTool tool) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    78
        if (n instanceof FloatConvertNode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    79
            convertSnippets.lower((FloatConvertNode) n, tool);
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    80
        } else if (profileSnippets != null && n instanceof ProfileNode) {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    81
            profileSnippets.lower((ProfileNode) n, tool);
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    82
        } else if (n instanceof UnaryMathIntrinsicNode) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    83
            lowerUnaryMath((UnaryMathIntrinsicNode) n, tool);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    84
        } else {
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    85
            super.lower(n, tool);
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    86
        }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    87
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
    88
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    89
    private void lowerUnaryMath(UnaryMathIntrinsicNode math, LoweringTool tool) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    90
        if (tool.getLoweringStage() == LoweringTool.StandardLoweringStage.HIGH_TIER) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    91
            return;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    92
        }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    93
        StructuredGraph graph = math.graph();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    94
        ResolvedJavaMethod method = graph.method();
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    95
        if (method != null) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    96
            if (method.getAnnotation(Snippet.class) != null) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    97
                // In the context of SnippetStub, i.e., Graal-generated stubs, use the LIR
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    98
                // lowering to emit the stub assembly code instead of the Node lowering.
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
    99
                return;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   100
            }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   101
        }
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   102
        if (!GraalArithmeticStubs.getValue(graph.getOptions())) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   103
            switch (math.getOperation()) {
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   104
                case SIN:
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   105
                case COS:
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   106
                case TAN:
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   107
                    // Math.sin(), .cos() and .tan() guarantee a value within 1 ULP of the exact
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   108
                    // result, but x87 trigonometric FPU instructions are only that accurate within
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   109
                    // [-pi/4, pi/4]. The snippets fall back to a foreign call to HotSpot stubs
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   110
                    // should the inputs outside of that interval.
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   111
                    mathSnippets.lower(math, tool);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   112
                    return;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   113
                case LOG:
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   114
                    math.replaceAtUsages(graph.addOrUnique(new AMD64X87MathIntrinsicNode(math.getValue(), UnaryOperation.LOG)));
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   115
                    return;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   116
                case LOG10:
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   117
                    math.replaceAtUsages(graph.addOrUnique(new AMD64X87MathIntrinsicNode(math.getValue(), UnaryOperation.LOG10)));
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   118
                    return;
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   119
            }
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   120
        }
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   121
54084
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   122
        ForeignCallNode call = graph.add(new ForeignCallNode(foreignCalls, math.getOperation().foreignCallDescriptor, math.getValue()));
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   123
        graph.addAfterFixed(tool.lastFixedNode(), call);
84f10bbf993f 8218074: Update Graal
jwilhelm
parents: 52910
diff changeset
   124
        math.replaceAtUsages(call);
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   125
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   126
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   127
    @Override
46393
d497d892ab11 8178864: Update Graal
iveresov
parents: 46344
diff changeset
   128
    public Integer smallestCompareWidth() {
d497d892ab11 8178864: Update Graal
iveresov
parents: 46344
diff changeset
   129
        return 8;
43972
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   130
    }
1ade39b8381b 8174879: Rename jdk.vm.ci to jdk.internal.vm.ci
kvn
parents:
diff changeset
   131
}