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), computeFlags(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 EnumSet<AMD64.Flag> computeFlags(HotSpotVMConfig config) {
  95         EnumSet<AMD64.Flag> flags = EnumSet.noneOf(AMD64.Flag.class);
  96         if (config.useCountLeadingZerosInstruction) {
  97             flags.add(AMD64.Flag.UseCountLeadingZerosInstruction);
  98         }
  99         if (config.useCountTrailingZerosInstruction) {
 100             flags.add(AMD64.Flag.UseCountTrailingZerosInstruction);
 101         }
 102         return flags;
 103     }
 104 
 105     protected TargetDescription createTarget(HotSpotVMConfig config) {
 106         final int stackFrameAlignment = 16;
 107         final int implicitNullCheckLimit = 4096;
 108         final boolean inlineObjects = true;
 109         return new HotSpotTargetDescription(createArchitecture(config), true, stackFrameAlignment, implicitNullCheckLimit, inlineObjects);
 110     }
 111 
 112     @Override
 113     public HotSpotBackend createBackend(HotSpotGraalRuntime runtime, HotSpotBackend host) {
 114         assert host == null;
 115         TargetDescription target = createTarget(runtime.getConfig());
 116 
 117         HotSpotProviders providers;
 118         HotSpotRegistersProvider registers;
 119         RegisterConfig regConfig;
 120         HotSpotCodeCacheProvider codeCache;
 121         HotSpotConstantReflectionProvider constantReflection;
 122         HotSpotHostForeignCallsProvider foreignCalls;
 123         Value[] nativeABICallerSaveRegisters;
 124         HotSpotMetaAccessProvider metaAccess;
 125         HotSpotLoweringProvider lowerer;
 126         HotSpotSnippetReflectionProvider snippetReflection;
 127         Replacements replacements;
 128         HotSpotDisassemblerProvider disassembler;
 129         HotSpotSuitesProvider suites;
 130         try (InitTimer t = timer("create providers")) {
 131             try (InitTimer rt = timer("create HotSpotRegisters provider")) {
 132                 registers = createRegisters();
 133             }
 134             try (InitTimer rt = timer("create MetaAccess provider")) {
 135                 metaAccess = createMetaAccess(runtime);
 136             }
 137             try (InitTimer rt = timer("create RegisterConfig")) {
 138                 regConfig = createRegisterConfig(runtime, target);
 139             }
 140             try (InitTimer rt = timer("create CodeCache provider")) {
 141                 codeCache = createCodeCache(runtime, target, regConfig);
 142             }
 143             try (InitTimer rt = timer("create ConstantReflection provider")) {
 144                 constantReflection = createConstantReflection(runtime);
 145             }
 146             try (InitTimer rt = timer("create NativeABICallerSaveRegisters")) {
 147                 nativeABICallerSaveRegisters = createNativeABICallerSaveRegisters(runtime.getConfig(), codeCache.getRegisterConfig());
 148             }
 149             try (InitTimer rt = timer("create ForeignCalls provider")) {
 150                 foreignCalls = createForeignCalls(runtime, metaAccess, codeCache, nativeABICallerSaveRegisters);
 151             }
 152             try (InitTimer rt = timer("create Lowerer provider")) {
 153                 lowerer = createLowerer(runtime, metaAccess, foreignCalls, registers, target);
 154             }
 155             // Replacements cannot have speculative optimizations since they have
 156             // to be valid for the entire run of the VM.
 157             Assumptions assumptions = new Assumptions(false);
 158             Providers p = new Providers(metaAccess, codeCache, constantReflection, foreignCalls, lowerer, null);
 159             try (InitTimer rt = timer("create SnippetReflection provider")) {
 160                 snippetReflection = createSnippetReflection();
 161             }
 162             try (InitTimer rt = timer("create Replacements provider")) {
 163                 replacements = createReplacements(runtime, assumptions, p, snippetReflection);
 164             }
 165             try (InitTimer rt = timer("create Disassembler provider")) {
 166                 disassembler = createDisassembler(runtime);
 167             }
 168             try (InitTimer rt = timer("create Suites provider")) {
 169                 suites = createSuites(runtime);
 170             }
 171             providers = new HotSpotProviders(metaAccess, codeCache, constantReflection, foreignCalls, lowerer, replacements, disassembler, suites, registers, snippetReflection);
 172         }
 173         try (InitTimer rt = timer("instantiate backend")) {
 174             return createBackend(runtime, providers);
 175         }
 176     }
 177 
 178     protected AMD64HotSpotBackend createBackend(HotSpotGraalRuntime runtime, HotSpotProviders providers) {
 179         return new AMD64HotSpotBackend(runtime, providers);
 180     }
 181 
 182     protected HotSpotRegistersProvider createRegisters() {
 183         return new HotSpotRegisters(AMD64.r15, AMD64.r12, AMD64.rsp);
 184     }
 185 
 186     protected HotSpotDisassemblerProvider createDisassembler(HotSpotGraalRuntime runtime) {
 187         return new HotSpotDisassemblerProvider(runtime);
 188     }
 189 
 190     protected Replacements createReplacements(HotSpotGraalRuntime runtime, Assumptions assumptions, Providers p, SnippetReflectionProvider snippetReflection) {
 191         return new HotSpotReplacementsImpl(p, snippetReflection, runtime.getConfig(), assumptions, p.getCodeCache().getTarget());
 192     }
 193 
 194     protected AMD64HotSpotForeignCallsProvider createForeignCalls(HotSpotGraalRuntime runtime, HotSpotMetaAccessProvider metaAccess, HotSpotCodeCacheProvider codeCache,
 195                     Value[] nativeABICallerSaveRegisters) {
 196         return new AMD64HotSpotForeignCallsProvider(runtime, metaAccess, codeCache, nativeABICallerSaveRegisters);
 197     }
 198 
 199     protected HotSpotConstantReflectionProvider createConstantReflection(HotSpotGraalRuntime runtime) {
 200         return new HotSpotConstantReflectionProvider(runtime);
 201     }
 202 
 203     protected RegisterConfig createRegisterConfig(HotSpotGraalRuntime runtime, TargetDescription target) {
 204         return new AMD64HotSpotRegisterConfig(target.arch, runtime.getConfig());
 205     }
 206 
 207     protected HotSpotCodeCacheProvider createCodeCache(HotSpotGraalRuntime runtime, TargetDescription target, RegisterConfig regConfig) {
 208         return new HotSpotCodeCacheProvider(runtime, target, regConfig);
 209     }
 210 
 211     protected HotSpotMetaAccessProvider createMetaAccess(HotSpotGraalRuntime runtime) {
 212         return new HotSpotMetaAccessProvider(runtime);
 213     }
 214 
 215     protected HotSpotSuitesProvider createSuites(HotSpotGraalRuntime runtime) {
 216         return new HotSpotSuitesProvider(runtime);
 217     }
 218 
 219     protected HotSpotSnippetReflectionProvider createSnippetReflection() {
 220         return new HotSpotSnippetReflectionProvider();
 221     }
 222 
 223     protected HotSpotLoweringProvider createLowerer(HotSpotGraalRuntime runtime, HotSpotMetaAccessProvider metaAccess, HotSpotForeignCallsProvider foreignCalls, HotSpotRegistersProvider registers,
 224                     TargetDescription target) {
 225         return new AMD64HotSpotLoweringProvider(runtime, metaAccess, foreignCalls, registers, target);
 226     }
 227 
 228     protected Value[] createNativeABICallerSaveRegisters(HotSpotVMConfig config, RegisterConfig regConfig) {
 229         List<Register> callerSave = new ArrayList<>(Arrays.asList(regConfig.getAllocatableRegisters()));
 230         if (config.windowsOs) {
 231             // http://msdn.microsoft.com/en-us/library/9z1stfyw.aspx
 232             callerSave.remove(AMD64.rdi);
 233             callerSave.remove(AMD64.rsi);
 234             callerSave.remove(AMD64.rbx);
 235             callerSave.remove(AMD64.rbp);
 236             callerSave.remove(AMD64.rsp);
 237             callerSave.remove(AMD64.r12);
 238             callerSave.remove(AMD64.r13);
 239             callerSave.remove(AMD64.r14);
 240             callerSave.remove(AMD64.r15);
 241             callerSave.remove(AMD64.xmm6);
 242             callerSave.remove(AMD64.xmm7);
 243             callerSave.remove(AMD64.xmm8);
 244             callerSave.remove(AMD64.xmm9);
 245             callerSave.remove(AMD64.xmm10);
 246             callerSave.remove(AMD64.xmm11);
 247             callerSave.remove(AMD64.xmm12);
 248             callerSave.remove(AMD64.xmm13);
 249             callerSave.remove(AMD64.xmm14);
 250             callerSave.remove(AMD64.xmm15);
 251         } else {
 252             /*
 253              * System V Application Binary Interface, AMD64 Architecture Processor Supplement
 254              *
 255              * Draft Version 0.96
 256              *
 257              * http://www.uclibc.org/docs/psABI-x86_64.pdf
 258              *
 259              * 3.2.1
 260              *
 261              * ...
 262              *
 263              * This subsection discusses usage of each register. Registers %rbp, %rbx and %r12
 264              * through %r15 "belong" to the calling function and the called function is required to
 265              * preserve their values. In other words, a called function must preserve these
 266              * registers' values for its caller. Remaining registers "belong" to the called
 267              * function. If a calling function wants to preserve such a register value across a
 268              * function call, it must save the value in its local stack frame.
 269              */
 270             callerSave.remove(AMD64.rbp);
 271             callerSave.remove(AMD64.rbx);
 272             callerSave.remove(AMD64.r12);
 273             callerSave.remove(AMD64.r13);
 274             callerSave.remove(AMD64.r14);
 275             callerSave.remove(AMD64.r15);
 276         }
 277         Value[] nativeABICallerSaveRegisters = new Value[callerSave.size()];
 278         for (int i = 0; i < callerSave.size(); i++) {
 279             nativeABICallerSaveRegisters[i] = callerSave.get(i).asValue();
 280         }
 281         return nativeABICallerSaveRegisters;
 282     }
 283 
 284     public String getArchitecture() {
 285         return "AMD64";
 286     }
 287 
 288     public String getGraalRuntimeName() {
 289         return "basic";
 290     }
 291 
 292     @Override
 293     public String toString() {
 294         return getGraalRuntimeName() + ":" + getArchitecture();
 295     }
 296 }