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 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 }