1 /* 2 * Copyright (c) 2013, 2018, Oracle and/or its affiliates. All rights reserved. 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 */ 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 }