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.debug.DebugHandlersFactory;
  37 import org.graalvm.compiler.graph.Node;
  38 import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig;
  39 import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider;
  40 import org.graalvm.compiler.hotspot.meta.DefaultHotSpotLoweringProvider;
  41 import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
  42 import org.graalvm.compiler.hotspot.meta.HotSpotRegistersProvider;
  43 import org.graalvm.compiler.hotspot.nodes.profiling.ProfileNode;
  44 import org.graalvm.compiler.hotspot.replacements.profiling.ProbabilisticProfileSnippets;
  45 import org.graalvm.compiler.nodes.calc.FloatConvertNode;
  46 import org.graalvm.compiler.nodes.spi.LoweringTool;
  47 import org.graalvm.compiler.options.OptionValues;
  48 import org.graalvm.compiler.replacements.amd64.AMD64ConvertSnippets;
  49 import org.graalvm.compiler.replacements.nodes.BinaryMathIntrinsicNode.BinaryOperation;
  50 import org.graalvm.compiler.replacements.nodes.UnaryMathIntrinsicNode.UnaryOperation;
  51 
  52 import jdk.vm.ci.code.TargetDescription;
  53 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
  54 import jdk.vm.ci.meta.MetaAccessProvider;
  55 
  56 public class AMD64HotSpotLoweringProvider extends DefaultHotSpotLoweringProvider {
  57 
  58     private AMD64ConvertSnippets.Templates convertSnippets;
  59     private ProbabilisticProfileSnippets.Templates profileSnippets;
  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)
  70                         ? new ProbabilisticProfileSnippets.Templates(options, factories, providers, providers.getCodeCache().getTarget()) : null;
  71         super.initialize(options, factories, providers, config);
  72     }
  73 
  74     @Override
  75     public void lower(Node n, LoweringTool tool) {
  76         if (n instanceof FloatConvertNode) {
  77             convertSnippets.lower((FloatConvertNode) n, tool);
  78         } else if (profileSnippets != null && n instanceof ProfileNode) {
  79             profileSnippets.lower((ProfileNode) n, tool);
  80         } else {
  81             super.lower(n, tool);
  82         }
  83     }
  84 
  85     @Override
  86     protected ForeignCallDescriptor toForeignCall(UnaryOperation operation) {
  87         if (GraalArithmeticStubs.getValue(runtime.getOptions())) {
  88             switch (operation) {
  89                 case LOG:
  90                     return ARITHMETIC_LOG_STUB;
  91                 case LOG10:
  92                     return ARITHMETIC_LOG10_STUB;
  93                 case SIN:
  94                     return ARITHMETIC_SIN_STUB;
  95                 case COS:
  96                     return ARITHMETIC_COS_STUB;
  97                 case TAN:
  98                     return ARITHMETIC_TAN_STUB;
  99                 case EXP:
 100                     return ARITHMETIC_EXP_STUB;
 101             }
 102         } else if (operation == UnaryOperation.EXP) {
 103             return operation.foreignCallDescriptor;
 104         }
 105         // Lower only using LIRGenerator
 106         return null;
 107     }
 108 
 109     @Override
 110     protected ForeignCallDescriptor toForeignCall(BinaryOperation operation) {
 111         if (GraalArithmeticStubs.getValue(runtime.getOptions())) {
 112             switch (operation) {
 113                 case POW:
 114                     return ARITHMETIC_POW_STUB;
 115             }
 116         } else if (operation == BinaryOperation.POW) {
 117             return operation.foreignCallDescriptor;
 118         }
 119         // Lower only using LIRGenerator
 120         return null;
 121     }
 122 
 123     @Override
 124     public Integer smallestCompareWidth() {
 125         return 8;
 126     }
 127 }