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 */ 23 24 25 package org.graalvm.compiler.hotspot.amd64; 26 27 import static org.graalvm.compiler.hotspot.HotSpotBackend.Options.GraalArithmeticStubs; 28 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_COS_STUB; 29 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_EXP_STUB; 30 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_LOG10_STUB; 31 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_LOG_STUB; 32 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_POW_STUB; 33 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_SIN_STUB; 34 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_TAN_STUB; 35 36 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor; 37 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider; 38 import org.graalvm.compiler.debug.DebugHandlersFactory; 39 import org.graalvm.compiler.graph.Node; 40 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 41 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; 42 import org.graalvm.compiler.hotspot.meta.DefaultHotSpotLoweringProvider; 43 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 44 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider; 45 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode; 46 import org.graalvm.compiler.hotspot.replacements.profiling.ProbabilisticProfileSnippets; 47 import org.graalvm.compiler.nodes.calc.FloatConvertNode; 48 import org.graalvm.compiler.nodes.spi.LoweringTool; 49 import org.graalvm.compiler.options.OptionValues; 50 import org.graalvm.compiler.replacements.amd64.AMD64ConvertSnippets; 51 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation; 52 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation; 53 54 import jdk.vm.ci.code.TargetDescription; 55 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider; 56 import jdk.vm.ci.meta.MetaAccessProvider; 57 58 public class AMD64HotSpotLoweringProvider extends DefaultHotSpotLoweringProvider { 59 60 private AMD64ConvertSnippets.Templates convertSnippets; 61 private ProbabilisticProfileSnippets.Templates profileSnippets; 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 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()); 71 profileSnippets = ProfileNode.Options.ProbabilisticProfiling.getValue(options) 72 ? new ProbabilisticProfileSnippets.Templates(options, factories, providers, providers.getCodeCache().getTarget()) 73 : null; 74 super.initialize(options, factories, providers, config); 75 } 76 77 @Override 78 public void lower(Node n, LoweringTool tool) { 79 if (n instanceof FloatConvertNode) { 80 convertSnippets.lower((FloatConvertNode) n, tool); 81 } else if (profileSnippets != null && n instanceof ProfileNode) { 82 profileSnippets.lower((ProfileNode) n, tool); 83 } else { 84 super.lower(n, tool); 85 } 86 } 87 88 @Override 89 protected ForeignCallDescriptor toForeignCall(UnaryOperation operation) { 90 if (GraalArithmeticStubs.getValue(runtime.getOptions())) { 91 switch (operation) { 92 case LOG: 93 return ARITHMETIC_LOG_STUB; 94 case LOG10: 95 return ARITHMETIC_LOG10_STUB; 96 case SIN: 97 return ARITHMETIC_SIN_STUB; 98 case COS: 99 return ARITHMETIC_COS_STUB; 100 case TAN: 101 return ARITHMETIC_TAN_STUB; 102 case EXP: 103 return ARITHMETIC_EXP_STUB; 104 } 105 } else if (operation == UnaryOperation.EXP) { 106 return operation.foreignCallDescriptor; 107 } 108 // Lower only using LIRGenerator 109 return null; 110 } 111 112 @Override 113 protected ForeignCallDescriptor toForeignCall(BinaryOperation operation) { 114 if (GraalArithmeticStubs.getValue(runtime.getOptions())) { 115 switch (operation) { 116 case POW: 117 return ARITHMETIC_POW_STUB; 118 } 119 } else if (operation == BinaryOperation.POW) { 120 return operation.foreignCallDescriptor; 121 } 122 // Lower only using LIRGenerator 123 return null; 124 } 125 126 @Override 127 public Integer smallestCompareWidth() { 128 return 8; 129 } 130 } | 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 */ 23 24 25 package org.graalvm.compiler.hotspot.amd64; 26 27 import static org.graalvm.compiler.hotspot.HotSpotBackend.Options.GraalArithmeticStubs; 28 29 import org.graalvm.compiler.api.replacements.Snippet; 30 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider; 31 import org.graalvm.compiler.debug.DebugHandlersFactory; 32 import org.graalvm.compiler.graph.Node; 33 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; 34 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; 35 import org.graalvm.compiler.hotspot.meta.DefaultHotSpotLoweringProvider; 36 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 37 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider; 38 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode; 39 import org.graalvm.compiler.hotspot.replacements.profiling.ProbabilisticProfileSnippets; 40 import org.graalvm.compiler.nodes.StructuredGraph; 41 import org.graalvm.compiler.nodes.calc.FloatConvertNode; 42 import org.graalvm.compiler.nodes.extended.ForeignCallNode; 43 import org.graalvm.compiler.nodes.spi.LoweringTool; 44 import org.graalvm.compiler.options.OptionValues; 45 import org.graalvm.compiler.replacements.amd64.AMD64ConvertSnippets; 46 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode; 47 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation; 48 import org.graalvm.compiler.serviceprovider.JavaVersionUtil; 49 50 import jdk.vm.ci.code.TargetDescription; 51 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider; 52 import jdk.vm.ci.meta.MetaAccessProvider; 53 import jdk.vm.ci.meta.ResolvedJavaMethod; 54 55 public class AMD64HotSpotLoweringProvider extends DefaultHotSpotLoweringProvider { 56 57 private AMD64ConvertSnippets.Templates convertSnippets; 58 private ProbabilisticProfileSnippets.Templates profileSnippets; 59 private AMD64X87MathSnippets.Templates mathSnippets; 60 61 public AMD64HotSpotLoweringProvider(HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, ForeignCallsProvider foreignCalls, HotSpotRegistersProvider registers, 62 HotSpotConstantReflectionProvider constantReflection, TargetDescription target) { 63 super(runtime, metaAccess, foreignCalls, registers, constantReflection, target); 64 } 65 66 @Override 67 public void initialize(OptionValues options, Iterable<DebugHandlersFactory> factories, HotSpotProviders providers, GraalHotSpotVMConfig config) { 68 convertSnippets = new AMD64ConvertSnippets.Templates(options, factories, providers, providers.getSnippetReflection(), providers.getCodeCache().getTarget()); 69 profileSnippets = ProfileNode.Options.ProbabilisticProfiling.getValue(options) && !JavaVersionUtil.Java8OrEarlier 70 ? new ProbabilisticProfileSnippets.Templates(options, factories, providers, providers.getCodeCache().getTarget()) 71 : null; 72 mathSnippets = new AMD64X87MathSnippets.Templates(options, factories, providers, providers.getSnippetReflection(), providers.getCodeCache().getTarget()); 73 super.initialize(options, factories, providers, config); 74 } 75 76 @Override 77 public void lower(Node n, LoweringTool tool) { 78 if (n instanceof FloatConvertNode) { 79 convertSnippets.lower((FloatConvertNode) n, tool); 80 } else if (profileSnippets != null && n instanceof ProfileNode) { 81 profileSnippets.lower((ProfileNode) n, tool); 82 } else if (n instanceof UnaryMathIntrinsicNode) { 83 lowerUnaryMath((UnaryMathIntrinsicNode) n, tool); 84 } else { 85 super.lower(n, tool); 86 } 87 } 88 89 private void lowerUnaryMath(UnaryMathIntrinsicNode math, LoweringTool tool) { 90 if (tool.getLoweringStage() == LoweringTool.StandardLoweringStage.HIGH_TIER) { 91 return; 92 } 93 StructuredGraph graph = math.graph(); 94 ResolvedJavaMethod method = graph.method(); 95 if (method != null) { 96 if (method.getAnnotation(Snippet.class) != null) { 97 // In the context of SnippetStub, i.e., Graal-generated stubs, use the LIR 98 // lowering to emit the stub assembly code instead of the Node lowering. 99 return; 100 } 101 } 102 if (!GraalArithmeticStubs.getValue(graph.getOptions())) { 103 switch (math.getOperation()) { 104 case SIN: 105 case COS: 106 case TAN: 107 // Math.sin(), .cos() and .tan() guarantee a value within 1 ULP of the exact 108 // result, but x87 trigonometric FPU instructions are only that accurate within 109 // [-pi/4, pi/4]. The snippets fall back to a foreign call to HotSpot stubs 110 // should the inputs outside of that interval. 111 mathSnippets.lower(math, tool); 112 return; 113 case LOG: 114 math.replaceAtUsages(graph.addOrUnique(new AMD64X87MathIntrinsicNode(math.getValue(), UnaryOperation.LOG))); 115 return; 116 case LOG10: 117 math.replaceAtUsages(graph.addOrUnique(new AMD64X87MathIntrinsicNode(math.getValue(), UnaryOperation.LOG10))); 118 return; 119 } 120 } 121 122 ForeignCallNode call = graph.add(new ForeignCallNode(foreignCalls, math.getOperation().foreignCallDescriptor, math.getValue())); 123 graph.addAfterFixed(tool.lastFixedNode(), call); 124 math.replaceAtUsages(call); 125 } 126 127 @Override 128 public Integer smallestCompareWidth() { 129 return 8; 130 } 131 } |