1 /*
   2  * Copyright (c) 2012, 2015, 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 jdk.vm.ci.hotspot.amd64;
  24 
  25 import static jdk.vm.ci.inittimer.InitTimer.timer;
  26 
  27 import java.util.EnumSet;
  28 
  29 import jdk.vm.ci.amd64.AMD64;
  30 import jdk.vm.ci.code.Architecture;
  31 import jdk.vm.ci.code.RegisterConfig;
  32 import jdk.vm.ci.code.TargetDescription;
  33 import jdk.vm.ci.code.stack.StackIntrospection;
  34 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
  35 import jdk.vm.ci.hotspot.HotSpotConstantReflectionProvider;
  36 import jdk.vm.ci.hotspot.HotSpotJVMCIBackendFactory;
  37 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntimeProvider;
  38 import jdk.vm.ci.hotspot.HotSpotMetaAccessProvider;
  39 import jdk.vm.ci.hotspot.HotSpotStackIntrospection;
  40 import jdk.vm.ci.hotspot.HotSpotVMConfig;
  41 import jdk.vm.ci.inittimer.InitTimer;
  42 import jdk.vm.ci.meta.ConstantReflectionProvider;
  43 import jdk.vm.ci.runtime.JVMCIBackend;
  44 
  45 public class AMD64HotSpotJVMCIBackendFactory implements HotSpotJVMCIBackendFactory {
  46 
  47     protected EnumSet<AMD64.CPUFeature> computeFeatures(HotSpotVMConfig config) {
  48         // Configure the feature set using the HotSpot flag settings.
  49         EnumSet<AMD64.CPUFeature> features = EnumSet.noneOf(AMD64.CPUFeature.class);
  50         if ((config.vmVersionFeatures & config.amd643DNOWPREFETCH) != 0) {
  51             features.add(AMD64.CPUFeature.AMD_3DNOW_PREFETCH);
  52         }
  53         assert config.useSSE >= 2 : "minimum config for x64";
  54         features.add(AMD64.CPUFeature.SSE);
  55         features.add(AMD64.CPUFeature.SSE2);
  56         if ((config.vmVersionFeatures & config.amd64SSE3) != 0) {
  57             features.add(AMD64.CPUFeature.SSE3);
  58         }
  59         if ((config.vmVersionFeatures & config.amd64SSSE3) != 0) {
  60             features.add(AMD64.CPUFeature.SSSE3);
  61         }
  62         if ((config.vmVersionFeatures & config.amd64SSE4A) != 0) {
  63             features.add(AMD64.CPUFeature.SSE4A);
  64         }
  65         if ((config.vmVersionFeatures & config.amd64SSE41) != 0) {
  66             features.add(AMD64.CPUFeature.SSE4_1);
  67         }
  68         if ((config.vmVersionFeatures & config.amd64SSE42) != 0) {
  69             features.add(AMD64.CPUFeature.SSE4_2);
  70         }
  71         if ((config.vmVersionFeatures & config.amd64POPCNT) != 0) {
  72             features.add(AMD64.CPUFeature.POPCNT);
  73         }
  74         if ((config.vmVersionFeatures & config.amd64LZCNT) != 0) {
  75             features.add(AMD64.CPUFeature.LZCNT);
  76         }
  77         if ((config.vmVersionFeatures & config.amd64ERMS) != 0) {
  78             features.add(AMD64.CPUFeature.ERMS);
  79         }
  80         if ((config.vmVersionFeatures & config.amd64AVX) != 0) {
  81             features.add(AMD64.CPUFeature.AVX);
  82         }
  83         if ((config.vmVersionFeatures & config.amd64AVX2) != 0) {
  84             features.add(AMD64.CPUFeature.AVX2);
  85         }
  86         if ((config.vmVersionFeatures & config.amd64AES) != 0) {
  87             features.add(AMD64.CPUFeature.AES);
  88         }
  89         if ((config.vmVersionFeatures & config.amd643DNOWPREFETCH) != 0) {
  90             features.add(AMD64.CPUFeature.AMD_3DNOW_PREFETCH);
  91         }
  92         if ((config.vmVersionFeatures & config.amd64BMI1) != 0) {
  93             features.add(AMD64.CPUFeature.BMI1);
  94         }
  95         if ((config.vmVersionFeatures & config.amd64BMI2) != 0) {
  96             features.add(AMD64.CPUFeature.BMI2);
  97         }
  98         if ((config.vmVersionFeatures & config.amd64RTM) != 0) {
  99             features.add(AMD64.CPUFeature.RTM);
 100         }
 101         if ((config.vmVersionFeatures & config.amd64ADX) != 0) {
 102             features.add(AMD64.CPUFeature.ADX);
 103         }
 104         if ((config.vmVersionFeatures & config.amd64AVX512F) != 0) {
 105             features.add(AMD64.CPUFeature.AVX512F);
 106         }
 107         if ((config.vmVersionFeatures & config.amd64AVX512DQ) != 0) {
 108             features.add(AMD64.CPUFeature.AVX512DQ);
 109         }
 110         if ((config.vmVersionFeatures & config.amd64AVX512PF) != 0) {
 111             features.add(AMD64.CPUFeature.AVX512PF);
 112         }
 113         if ((config.vmVersionFeatures & config.amd64AVX512ER) != 0) {
 114             features.add(AMD64.CPUFeature.AVX512ER);
 115         }
 116         if ((config.vmVersionFeatures & config.amd64AVX512CD) != 0) {
 117             features.add(AMD64.CPUFeature.AVX512CD);
 118         }
 119         if ((config.vmVersionFeatures & config.amd64AVX512BW) != 0) {
 120             features.add(AMD64.CPUFeature.AVX512BW);
 121         }
 122         if ((config.vmVersionFeatures & config.amd64AVX512VL) != 0) {
 123             features.add(AMD64.CPUFeature.AVX512VL);
 124         }
 125         if ((config.vmVersionFeatures & config.amd64SHA) != 0) {
 126             features.add(AMD64.CPUFeature.SHA);
 127         }
 128         return features;
 129     }
 130 
 131     protected EnumSet<AMD64.Flag> computeFlags(HotSpotVMConfig config) {
 132         EnumSet<AMD64.Flag> flags = EnumSet.noneOf(AMD64.Flag.class);
 133         if (config.useCountLeadingZerosInstruction) {
 134             flags.add(AMD64.Flag.UseCountLeadingZerosInstruction);
 135         }
 136         if (config.useCountTrailingZerosInstruction) {
 137             flags.add(AMD64.Flag.UseCountTrailingZerosInstruction);
 138         }
 139         return flags;
 140     }
 141 
 142     protected TargetDescription createTarget(HotSpotVMConfig config) {
 143         final int stackFrameAlignment = 16;
 144         final int implicitNullCheckLimit = 4096;
 145         final boolean inlineObjects = true;
 146         Architecture arch = new AMD64(computeFeatures(config), computeFlags(config));
 147         return new TargetDescription(arch, true, stackFrameAlignment, implicitNullCheckLimit, inlineObjects);
 148     }
 149 
 150     protected HotSpotConstantReflectionProvider createConstantReflection(HotSpotJVMCIRuntimeProvider runtime) {
 151         return new HotSpotConstantReflectionProvider(runtime);
 152     }
 153 
 154     protected RegisterConfig createRegisterConfig(HotSpotJVMCIRuntimeProvider runtime, TargetDescription target) {
 155         return new AMD64HotSpotRegisterConfig(target.arch, runtime.getConfig());
 156     }
 157 
 158     protected HotSpotCodeCacheProvider createCodeCache(HotSpotJVMCIRuntimeProvider runtime, TargetDescription target, RegisterConfig regConfig) {
 159         return new HotSpotCodeCacheProvider(runtime, runtime.getConfig(), target, regConfig);
 160     }
 161 
 162     protected HotSpotMetaAccessProvider createMetaAccess(HotSpotJVMCIRuntimeProvider runtime) {
 163         return new HotSpotMetaAccessProvider(runtime);
 164     }
 165 
 166     @Override
 167     public String getArchitecture() {
 168         return "AMD64";
 169     }
 170 
 171     @Override
 172     public String toString() {
 173         return "JVMCIBackend:" + getArchitecture();
 174     }
 175 
 176     @SuppressWarnings("try")
 177     public JVMCIBackend createJVMCIBackend(HotSpotJVMCIRuntimeProvider runtime, JVMCIBackend host) {
 178 
 179         assert host == null;
 180         TargetDescription target = createTarget(runtime.getConfig());
 181 
 182         RegisterConfig regConfig;
 183         HotSpotCodeCacheProvider codeCache;
 184         ConstantReflectionProvider constantReflection;
 185         HotSpotMetaAccessProvider metaAccess;
 186         StackIntrospection stackIntrospection;
 187         try (InitTimer t = timer("create providers")) {
 188             try (InitTimer rt = timer("create MetaAccess provider")) {
 189                 metaAccess = createMetaAccess(runtime);
 190             }
 191             try (InitTimer rt = timer("create RegisterConfig")) {
 192                 regConfig = createRegisterConfig(runtime, target);
 193             }
 194             try (InitTimer rt = timer("create CodeCache provider")) {
 195                 codeCache = createCodeCache(runtime, target, regConfig);
 196             }
 197             try (InitTimer rt = timer("create ConstantReflection provider")) {
 198                 constantReflection = createConstantReflection(runtime);
 199             }
 200             try (InitTimer rt = timer("create StackIntrospection provider")) {
 201                 stackIntrospection = new HotSpotStackIntrospection(runtime);
 202             }
 203         }
 204         try (InitTimer rt = timer("instantiate backend")) {
 205             return createBackend(metaAccess, codeCache, constantReflection, stackIntrospection);
 206         }
 207     }
 208 
 209     protected JVMCIBackend createBackend(HotSpotMetaAccessProvider metaAccess, HotSpotCodeCacheProvider codeCache, ConstantReflectionProvider constantReflection, StackIntrospection stackIntrospection) {
 210         return new JVMCIBackend(metaAccess, codeCache, constantReflection, stackIntrospection);
 211     }
 212 }