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