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         return features;
 126     }
 127 
 128     protected EnumSet<AMD64.Flag> computeFlags(HotSpotVMConfig config) {
 129         EnumSet<AMD64.Flag> flags = EnumSet.noneOf(AMD64.Flag.class);
 130         if (config.useCountLeadingZerosInstruction) {
 131             flags.add(AMD64.Flag.UseCountLeadingZerosInstruction);
 132         }
 133         if (config.useCountTrailingZerosInstruction) {
 134             flags.add(AMD64.Flag.UseCountTrailingZerosInstruction);
 135         }
 136         return flags;
 137     }
 138 
 139     protected TargetDescription createTarget(HotSpotVMConfig config) {
 140         final int stackFrameAlignment = 16;
 141         final int implicitNullCheckLimit = 4096;
 142         final boolean inlineObjects = true;
 143         Architecture arch = new AMD64(computeFeatures(config), computeFlags(config));
 144         return new TargetDescription(arch, true, stackFrameAlignment, implicitNullCheckLimit, inlineObjects);
 145     }
 146 
 147     protected HotSpotConstantReflectionProvider createConstantReflection(HotSpotJVMCIRuntimeProvider runtime) {
 148         return new HotSpotConstantReflectionProvider(runtime);
 149     }
 150 
 151     protected RegisterConfig createRegisterConfig(HotSpotJVMCIRuntimeProvider runtime, TargetDescription target) {
 152         return new AMD64HotSpotRegisterConfig(target.arch, runtime.getConfig());
 153     }
 154 
 155     protected HotSpotCodeCacheProvider createCodeCache(HotSpotJVMCIRuntimeProvider runtime, TargetDescription target, RegisterConfig regConfig) {
 156         return new HotSpotCodeCacheProvider(runtime, runtime.getConfig(), target, regConfig);
 157     }
 158 
 159     protected HotSpotMetaAccessProvider createMetaAccess(HotSpotJVMCIRuntimeProvider runtime) {
 160         return new HotSpotMetaAccessProvider(runtime);
 161     }
 162 
 163     @Override
 164     public String getArchitecture() {
 165         return "AMD64";
 166     }
 167 
 168     @Override
 169     public String toString() {
 170         return "JVMCIBackend:" + getArchitecture();
 171     }
 172 
 173     @SuppressWarnings("try")
 174     public JVMCIBackend createJVMCIBackend(HotSpotJVMCIRuntimeProvider runtime, JVMCIBackend host) {
 175 
 176         assert host == null;
 177         TargetDescription target = createTarget(runtime.getConfig());
 178 
 179         RegisterConfig regConfig;
 180         HotSpotCodeCacheProvider codeCache;
 181         ConstantReflectionProvider constantReflection;
 182         HotSpotMetaAccessProvider metaAccess;
 183         StackIntrospection stackIntrospection;
 184         try (InitTimer t = timer("create providers")) {
 185             try (InitTimer rt = timer("create MetaAccess provider")) {
 186                 metaAccess = createMetaAccess(runtime);
 187             }
 188             try (InitTimer rt = timer("create RegisterConfig")) {
 189                 regConfig = createRegisterConfig(runtime, target);
 190             }
 191             try (InitTimer rt = timer("create CodeCache provider")) {
 192                 codeCache = createCodeCache(runtime, target, regConfig);
 193             }
 194             try (InitTimer rt = timer("create ConstantReflection provider")) {
 195                 constantReflection = createConstantReflection(runtime);
 196             }
 197             try (InitTimer rt = timer("create StackIntrospection provider")) {
 198                 stackIntrospection = new HotSpotStackIntrospection(runtime);
 199             }
 200         }
 201         try (InitTimer rt = timer("instantiate backend")) {
 202             return createBackend(metaAccess, codeCache, constantReflection, stackIntrospection);
 203         }
 204     }
 205 
 206     protected JVMCIBackend createBackend(HotSpotMetaAccessProvider metaAccess, HotSpotCodeCacheProvider codeCache, ConstantReflectionProvider constantReflection, StackIntrospection stackIntrospection) {
 207         return new JVMCIBackend(metaAccess, codeCache, constantReflection, stackIntrospection);
 208     }
 209 }