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