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