1 /*
   2  * Copyright (c) 2012, 2014, 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 com.oracle.graal.hotspot.amd64;
  24 
  25 import static com.oracle.graal.hotspot.InitTimer.*;
  26 
  27 import java.util.*;
  28 
  29 import com.oracle.graal.amd64.*;
  30 import com.oracle.graal.api.code.*;
  31 import com.oracle.graal.api.meta.*;
  32 import com.oracle.graal.api.replacements.*;
  33 import com.oracle.graal.api.runtime.*;
  34 import com.oracle.graal.hotspot.*;
  35 import com.oracle.graal.hotspot.meta.*;
  36 import com.oracle.graal.nodes.spi.*;
  37 import com.oracle.graal.phases.util.*;
  38 
  39 @ServiceProvider(HotSpotBackendFactory.class)
  40 public class AMD64HotSpotBackendFactory implements HotSpotBackendFactory {
  41 
  42     protected Architecture createArchitecture(HotSpotVMConfig config) {
  43         return new AMD64(computeFeatures(config));
  44     }
  45 
  46     protected EnumSet<AMD64.CPUFeature> computeFeatures(HotSpotVMConfig config) {
  47         // Configure the feature set using the HotSpot flag settings.
  48         EnumSet<AMD64.CPUFeature> features = EnumSet.noneOf(AMD64.CPUFeature.class);
  49         assert config.useSSE >= 2 : "minimum config for x64";
  50         features.add(AMD64.CPUFeature.SSE);
  51         features.add(AMD64.CPUFeature.SSE2);
  52         if ((config.x86CPUFeatures & config.cpuSSE3) != 0) {
  53             features.add(AMD64.CPUFeature.SSE3);
  54         }
  55         if ((config.x86CPUFeatures & config.cpuSSSE3) != 0) {
  56             features.add(AMD64.CPUFeature.SSSE3);
  57         }
  58         if ((config.x86CPUFeatures & config.cpuSSE4A) != 0) {
  59             features.add(AMD64.CPUFeature.SSE4a);
  60         }
  61         if ((config.x86CPUFeatures & config.cpuSSE41) != 0) {
  62             features.add(AMD64.CPUFeature.SSE4_1);
  63         }
  64         if ((config.x86CPUFeatures & config.cpuSSE42) != 0) {
  65             features.add(AMD64.CPUFeature.SSE4_2);
  66         }
  67         if ((config.x86CPUFeatures & config.cpuAVX) != 0) {
  68             features.add(AMD64.CPUFeature.AVX);
  69         }
  70         if ((config.x86CPUFeatures & config.cpuAVX2) != 0) {
  71             features.add(AMD64.CPUFeature.AVX2);
  72         }
  73         if ((config.x86CPUFeatures & config.cpuERMS) != 0) {
  74             features.add(AMD64.CPUFeature.ERMS);
  75         }
  76         if ((config.x86CPUFeatures & config.cpuLZCNT) != 0) {
  77             features.add(AMD64.CPUFeature.LZCNT);
  78         }
  79         if ((config.x86CPUFeatures & config.cpuPOPCNT) != 0) {
  80             features.add(AMD64.CPUFeature.POPCNT);
  81         }
  82         if ((config.x86CPUFeatures & config.cpuAES) != 0) {
  83             features.add(AMD64.CPUFeature.AES);
  84         }
  85         if ((config.x86CPUFeatures & config.cpu3DNOWPREFETCH) != 0) {
  86             features.add(AMD64.CPUFeature.AMD_3DNOW_PREFETCH);
  87         }
  88         if ((config.x86CPUFeatures & config.cpuBMI1) != 0) {
  89             features.add(AMD64.CPUFeature.BMI1);
  90         }
  91         return features;
  92     }
  93 
  94     protected TargetDescription createTarget(HotSpotVMConfig config) {
  95         final int stackFrameAlignment = 16;
  96         final int implicitNullCheckLimit = 4096;
  97         final boolean inlineObjects = true;
  98         return new HotSpotTargetDescription(createArchitecture(config), true, stackFrameAlignment, implicitNullCheckLimit, inlineObjects);
  99     }
 100 
 101     @Override
 102     public HotSpotBackend createBackend(HotSpotGraalRuntime runtime, HotSpotBackend host) {
 103         assert host == null;
 104         TargetDescription target = createTarget(runtime.getConfig());
 105 
 106         HotSpotProviders providers;
 107         HotSpotRegistersProvider registers;
 108         RegisterConfig regConfig;
 109         HotSpotCodeCacheProvider codeCache;
 110         HotSpotConstantReflectionProvider constantReflection;
 111         HotSpotHostForeignCallsProvider foreignCalls;
 112         Value[] nativeABICallerSaveRegisters;
 113         HotSpotMetaAccessProvider metaAccess;
 114         HotSpotLoweringProvider lowerer;
 115         HotSpotSnippetReflectionProvider snippetReflection;
 116         Replacements replacements;
 117         HotSpotDisassemblerProvider disassembler;
 118         HotSpotSuitesProvider suites;
 119         try (InitTimer t = timer("create providers")) {
 120             try (InitTimer rt = timer("create HotSpotRegisters provider")) {
 121                 registers = createRegisters();
 122             }
 123             try (InitTimer rt = timer("create MetaAccess provider")) {
 124                 metaAccess = createMetaAccess(runtime);
 125             }
 126             try (InitTimer rt = timer("create RegisterConfig")) {
 127                 regConfig = createRegisterConfig(runtime, target);
 128             }
 129             try (InitTimer rt = timer("create CodeCache provider")) {
 130                 codeCache = createCodeCache(runtime, target, regConfig);
 131             }
 132             try (InitTimer rt = timer("create ConstantReflection provider")) {
 133                 constantReflection = createConstantReflection(runtime);
 134             }
 135             try (InitTimer rt = timer("create NativeABICallerSaveRegisters")) {
 136                 nativeABICallerSaveRegisters = createNativeABICallerSaveRegisters(runtime.getConfig(), codeCache.getRegisterConfig());
 137             }
 138             try (InitTimer rt = timer("create ForeignCalls provider")) {
 139                 foreignCalls = createForeignCalls(runtime, metaAccess, codeCache, nativeABICallerSaveRegisters);
 140             }
 141             try (InitTimer rt = timer("create Lowerer provider")) {
 142                 lowerer = createLowerer(runtime, metaAccess, foreignCalls, registers, target);
 143             }
 144             // Replacements cannot have speculative optimizations since they have
 145             // to be valid for the entire run of the VM.
 146             Assumptions assumptions = new Assumptions(false);
 147             Providers p = new Providers(metaAccess, codeCache, constantReflection, foreignCalls, lowerer, null);
 148             try (InitTimer rt = timer("create SnippetReflection provider")) {
 149                 snippetReflection = createSnippetReflection();
 150             }
 151             try (InitTimer rt = timer("create Replacements provider")) {
 152                 replacements = createReplacements(runtime, assumptions, p, snippetReflection);
 153             }
 154             try (InitTimer rt = timer("create Disassembler provider")) {
 155                 disassembler = createDisassembler(runtime);
 156             }
 157             try (InitTimer rt = timer("create Suites provider")) {
 158                 suites = createSuites(runtime);
 159             }
 160             providers = new HotSpotProviders(metaAccess, codeCache, constantReflection, foreignCalls, lowerer, replacements, disassembler, suites, registers, snippetReflection);
 161         }
 162         try (InitTimer rt = timer("instantiate backend")) {
 163             return createBackend(runtime, providers);
 164         }
 165     }
 166 
 167     protected AMD64HotSpotBackend createBackend(HotSpotGraalRuntime runtime, HotSpotProviders providers) {
 168         return new AMD64HotSpotBackend(runtime, providers);
 169     }
 170 
 171     protected HotSpotRegistersProvider createRegisters() {
 172         return new HotSpotRegisters(AMD64.r15, AMD64.r12, AMD64.rsp);
 173     }
 174 
 175     protected HotSpotDisassemblerProvider createDisassembler(HotSpotGraalRuntime runtime) {
 176         return new HotSpotDisassemblerProvider(runtime);
 177     }
 178 
 179     protected Replacements createReplacements(HotSpotGraalRuntime runtime, Assumptions assumptions, Providers p, SnippetReflectionProvider snippetReflection) {
 180         return new HotSpotReplacementsImpl(p, snippetReflection, runtime.getConfig(), assumptions, p.getCodeCache().getTarget());
 181     }
 182 
 183     protected AMD64HotSpotForeignCallsProvider createForeignCalls(HotSpotGraalRuntime runtime, HotSpotMetaAccessProvider metaAccess, HotSpotCodeCacheProvider codeCache,
 184                     Value[] nativeABICallerSaveRegisters) {
 185         return new AMD64HotSpotForeignCallsProvider(runtime, metaAccess, codeCache, nativeABICallerSaveRegisters);
 186     }
 187 
 188     protected HotSpotConstantReflectionProvider createConstantReflection(HotSpotGraalRuntime runtime) {
 189         return new HotSpotConstantReflectionProvider(runtime);
 190     }
 191 
 192     protected RegisterConfig createRegisterConfig(HotSpotGraalRuntime runtime, TargetDescription target) {
 193         return new AMD64HotSpotRegisterConfig(target.arch, runtime.getConfig());
 194     }
 195 
 196     protected HotSpotCodeCacheProvider createCodeCache(HotSpotGraalRuntime runtime, TargetDescription target, RegisterConfig regConfig) {
 197         return new HotSpotCodeCacheProvider(runtime, target, regConfig);
 198     }
 199 
 200     protected HotSpotMetaAccessProvider createMetaAccess(HotSpotGraalRuntime runtime) {
 201         return new HotSpotMetaAccessProvider(runtime);
 202     }
 203 
 204     protected HotSpotSuitesProvider createSuites(HotSpotGraalRuntime runtime) {
 205         return new HotSpotSuitesProvider(runtime);
 206     }
 207 
 208     protected HotSpotSnippetReflectionProvider createSnippetReflection() {
 209         return new HotSpotSnippetReflectionProvider();
 210     }
 211 
 212     protected HotSpotLoweringProvider createLowerer(HotSpotGraalRuntime runtime, HotSpotMetaAccessProvider metaAccess, HotSpotForeignCallsProvider foreignCalls, HotSpotRegistersProvider registers,
 213                     TargetDescription target) {
 214         return new AMD64HotSpotLoweringProvider(runtime, metaAccess, foreignCalls, registers, target);
 215     }
 216 
 217     protected Value[] createNativeABICallerSaveRegisters(HotSpotVMConfig config, RegisterConfig regConfig) {
 218         List<Register> callerSave = new ArrayList<>(Arrays.asList(regConfig.getAllocatableRegisters()));
 219         if (config.windowsOs) {
 220             // http://msdn.microsoft.com/en-us/library/9z1stfyw.aspx
 221             callerSave.remove(AMD64.rdi);
 222             callerSave.remove(AMD64.rsi);
 223             callerSave.remove(AMD64.rbx);
 224             callerSave.remove(AMD64.rbp);
 225             callerSave.remove(AMD64.rsp);
 226             callerSave.remove(AMD64.r12);
 227             callerSave.remove(AMD64.r13);
 228             callerSave.remove(AMD64.r14);
 229             callerSave.remove(AMD64.r15);
 230             callerSave.remove(AMD64.xmm6);
 231             callerSave.remove(AMD64.xmm7);
 232             callerSave.remove(AMD64.xmm8);
 233             callerSave.remove(AMD64.xmm9);
 234             callerSave.remove(AMD64.xmm10);
 235             callerSave.remove(AMD64.xmm11);
 236             callerSave.remove(AMD64.xmm12);
 237             callerSave.remove(AMD64.xmm13);
 238             callerSave.remove(AMD64.xmm14);
 239             callerSave.remove(AMD64.xmm15);
 240         } else {
 241             /*
 242              * System V Application Binary Interface, AMD64 Architecture Processor Supplement
 243              * 
 244              * Draft Version 0.96
 245              * 
 246              * http://www.uclibc.org/docs/psABI-x86_64.pdf
 247              * 
 248              * 3.2.1
 249              * 
 250              * ...
 251              * 
 252              * This subsection discusses usage of each register. Registers %rbp, %rbx and %r12
 253              * through %r15 "belong" to the calling function and the called function is required to
 254              * preserve their values. In other words, a called function must preserve these
 255              * registers' values for its caller. Remaining registers "belong" to the called
 256              * function. If a calling function wants to preserve such a register value across a
 257              * function call, it must save the value in its local stack frame.
 258              */
 259             callerSave.remove(AMD64.rbp);
 260             callerSave.remove(AMD64.rbx);
 261             callerSave.remove(AMD64.r12);
 262             callerSave.remove(AMD64.r13);
 263             callerSave.remove(AMD64.r14);
 264             callerSave.remove(AMD64.r15);
 265         }
 266         Value[] nativeABICallerSaveRegisters = new Value[callerSave.size()];
 267         for (int i = 0; i < callerSave.size(); i++) {
 268             nativeABICallerSaveRegisters[i] = callerSave.get(i).asValue();
 269         }
 270         return nativeABICallerSaveRegisters;
 271     }
 272 
 273     public String getArchitecture() {
 274         return "AMD64";
 275     }
 276 
 277     public String getGraalRuntimeName() {
 278         return "basic";
 279     }
 280 
 281     @Override
 282     public String toString() {
 283         return getGraalRuntimeName() + ":" + getArchitecture();
 284     }
 285 }