1 /*
   2  * Copyright (c) 2013, 2016, 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 package org.graalvm.compiler.hotspot.amd64;
  24 
  25 import static org.graalvm.compiler.hotspot.HotSpotBackend.Options.GraalArithmeticStubs;
  26 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_COS_STUB;
  27 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_EXP_STUB;
  28 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_LOG10_STUB;
  29 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_LOG_STUB;
  30 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_SIN_STUB;
  31 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_POW_STUB;
  32 import static org.graalvm.compiler.hotspot.amd64.AMD64HotSpotForeignCallsProvider.ARITHMETIC_TAN_STUB;
  33 
  34 import org.graalvm.compiler.core.common.spi.ForeignCallDescriptor;
  35 import org.graalvm.compiler.core.common.spi.ForeignCallsProvider;
  36 import org.graalvm.compiler.graph.Node;
  37 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  38 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  39 import org.graalvm.compiler.hotspot.meta.DefaultHotSpotLoweringProvider;
  40 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  41 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
  42 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode;
  43 import org.graalvm.compiler.hotspot.replacements.profiling.ProbabilisticProfileSnippets;
  44 import org.graalvm.compiler.nodes.calc.FloatConvertNode;
  45 import org.graalvm.compiler.nodes.spi.LoweringTool;
  46 import org.graalvm.compiler.replacements.amd64.AMD64ConvertSnippets;
  47 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation;
  48 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
  49 
  50 import jdk.vm.ci.code.TargetDescription;
  51 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
  52 import jdk.vm.ci.meta.MetaAccessProvider;
  53 
  54 public class AMD64HotSpotLoweringProvider extends DefaultHotSpotLoweringProvider {
  55 
  56     private AMD64ConvertSnippets.Templates convertSnippets;
  57     private ProbabilisticProfileSnippets.Templates profileSnippets;
  58 
  59     public AMD64HotSpotLoweringProvider(HotSpotGraalRuntimeProvider runtime, MetaAccessProvider metaAccess, ForeignCallsProvider foreignCalls, HotSpotRegistersProvider registers,
  60                     HotSpotConstantReflectionProvider constantReflection, TargetDescription target) {
  61         super(runtime, metaAccess, foreignCalls, registers, constantReflection, target);
  62     }
  63 
  64     @Override
  65     public void initialize(HotSpotProviders providers, GraalHotSpotVMConfig config) {
  66         convertSnippets = new AMD64ConvertSnippets.Templates(providers, providers.getSnippetReflection(), providers.getCodeCache().getTarget());
  67         profileSnippets = ProfileNode.Options.ProbabilisticProfiling.getValue() ? new ProbabilisticProfileSnippets.Templates(providers, providers.getCodeCache().getTarget()) : null;
  68         super.initialize(providers, config);
  69     }
  70 
  71     @Override
  72     public void lower(Node n, LoweringTool tool) {
  73         if (n instanceof FloatConvertNode) {
  74             convertSnippets.lower((FloatConvertNode) n, tool);
  75         } else if (profileSnippets != null && n instanceof ProfileNode) {
  76             profileSnippets.lower((ProfileNode) n, tool);
  77         } else {
  78             super.lower(n, tool);
  79         }
  80     }
  81 
  82     @Override
  83     protected ForeignCallDescriptor toForeignCall(UnaryOperation operation) {
  84         if (GraalArithmeticStubs.getValue()) {
  85             switch (operation) {
  86                 case LOG:
  87                     return ARITHMETIC_LOG_STUB;
  88                 case LOG10:
  89                     return ARITHMETIC_LOG10_STUB;
  90                 case SIN:
  91                     return ARITHMETIC_SIN_STUB;
  92                 case COS:
  93                     return ARITHMETIC_COS_STUB;
  94                 case TAN:
  95                     return ARITHMETIC_TAN_STUB;
  96                 case EXP:
  97                     return ARITHMETIC_EXP_STUB;
  98             }
  99         } else if (operation == UnaryOperation.EXP) {
 100             return operation.foreignCallDescriptor;
 101         }
 102         // Lower only using LIRGenerator
 103         return null;
 104     }
 105 
 106     @Override
 107     protected ForeignCallDescriptor toForeignCall(BinaryOperation operation) {
 108         if (GraalArithmeticStubs.getValue()) {
 109             switch (operation) {
 110                 case POW:
 111                     return ARITHMETIC_POW_STUB;
 112             }
 113         } else if (operation == BinaryOperation.POW) {
 114             return operation.foreignCallDescriptor;
 115         }
 116         // Lower only using LIRGenerator
 117         return null;
 118     }
 119 
 120     @Override
 121     public boolean supportSubwordCompare(int bits) {
 122         return true;
 123     }
 124 }