1 /* 2 * Copyright (c) 2013, 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 org.graalvm.compiler.lir.amd64; 24 25 import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK; 26 import static jdk.vm.ci.code.ValueUtil.asStackSlot; 27 import static jdk.vm.ci.code.ValueUtil.isStackSlot; 28 29 import java.util.Arrays; 30 import java.util.Set; 31 32 import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler; 33 import org.graalvm.compiler.lir.LIRInstructionClass; 34 import org.graalvm.compiler.lir.LIRValueUtil; 35 import org.graalvm.compiler.lir.Opcode; 36 import org.graalvm.compiler.lir.StandardOp.SaveRegistersOp; 37 import org.graalvm.compiler.lir.asm.CompilationResultBuilder; 38 import org.graalvm.compiler.lir.framemap.FrameMap; 39 40 import jdk.vm.ci.amd64.AMD64Kind; 41 import jdk.vm.ci.code.Register; 42 import jdk.vm.ci.code.RegisterSaveLayout; 43 import jdk.vm.ci.code.StackSlot; 44 import jdk.vm.ci.meta.AllocatableValue; 45 46 /** 47 * Saves registers to stack slots. 48 */ 49 @Opcode("SAVE_REGISTER") 50 public class AMD64SaveRegistersOp extends AMD64LIRInstruction implements SaveRegistersOp { 51 public static final LIRInstructionClass<AMD64SaveRegistersOp> TYPE = LIRInstructionClass.create(AMD64SaveRegistersOp.class); 52 53 /** 54 * The registers (potentially) saved by this operation. 55 */ 56 protected final Register[] savedRegisters; 57 58 /** 59 * The slots to which the registers are saved. 60 */ 61 @Def(STACK) protected final AllocatableValue[] slots; 62 63 /** 64 * Specifies if {@link #remove(Set)} should have an effect. 65 */ 66 protected final boolean supportsRemove; 67 68 /** 69 * 70 * @param savedRegisters the registers saved by this operation which may be subject to 71 * {@linkplain #remove(Set) pruning} 72 * @param savedRegisterLocations the slots to which the registers are saved 73 * @param supportsRemove determines if registers can be {@linkplain #remove(Set) pruned} 74 */ 75 public AMD64SaveRegistersOp(Register[] savedRegisters, AllocatableValue[] savedRegisterLocations, boolean supportsRemove) { 76 this(TYPE, savedRegisters, savedRegisterLocations, supportsRemove); 77 } 78 79 public AMD64SaveRegistersOp(LIRInstructionClass<? extends AMD64SaveRegistersOp> c, Register[] savedRegisters, AllocatableValue[] savedRegisterLocations, boolean supportsRemove) { 80 super(c); 81 assert Arrays.asList(savedRegisterLocations).stream().allMatch(LIRValueUtil::isVirtualStackSlot); 82 this.savedRegisters = savedRegisters; 83 this.slots = savedRegisterLocations; 84 this.supportsRemove = supportsRemove; 85 } 86 87 protected void saveRegister(CompilationResultBuilder crb, AMD64MacroAssembler masm, StackSlot result, Register input) { 88 AMD64Move.reg2stack((AMD64Kind) result.getPlatformKind(), crb, masm, result, input); 89 } 90 91 @Override 92 public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) { 93 for (int i = 0; i < savedRegisters.length; i++) { 94 if (savedRegisters[i] != null) { 95 assert isStackSlot(slots[i]) : "not a StackSlot: " + slots[i]; 96 saveRegister(crb, masm, asStackSlot(slots[i]), savedRegisters[i]); 97 } 98 } 99 } 100 101 public AllocatableValue[] getSlots() { 102 return slots; 103 } 104 105 @Override 106 public boolean supportsRemove() { 107 return supportsRemove; 108 } 109 110 @Override 111 public int remove(Set<Register> doNotSave) { 112 if (!supportsRemove) { 113 throw new UnsupportedOperationException(); 114 } 115 return prune(doNotSave, savedRegisters); 116 } 117 118 static int prune(Set<Register> toRemove, Register[] registers) { 119 int pruned = 0; 120 for (int i = 0; i < registers.length; i++) { 121 if (registers[i] != null) { 122 if (toRemove.contains(registers[i])) { 123 registers[i] = null; 124 pruned++; 125 } 126 } 127 } 128 return pruned; 129 } 130 131 @Override 132 public RegisterSaveLayout getMap(FrameMap frameMap) { 133 int total = 0; 134 for (int i = 0; i < savedRegisters.length; i++) { 135 if (savedRegisters[i] != null) { 136 total++; 137 } 138 } 139 Register[] keys = new Register[total]; 140 int[] values = new int[total]; 141 if (total != 0) { 142 int mapIndex = 0; 143 for (int i = 0; i < savedRegisters.length; i++) { 144 if (savedRegisters[i] != null) { 145 keys[mapIndex] = savedRegisters[i]; 146 assert isStackSlot(slots[i]) : "not a StackSlot: " + slots[i]; 147 StackSlot slot = asStackSlot(slots[i]); 148 values[mapIndex] = indexForStackSlot(frameMap, slot); 149 mapIndex++; 150 } 151 } 152 assert mapIndex == total; 153 } 154 return new RegisterSaveLayout(keys, values); 155 } 156 157 /** 158 * Computes the index of a stack slot relative to slot 0. This is also the bit index of stack 159 * slots in the reference map. 160 * 161 * @param slot a stack slot 162 * @return the index of the stack slot 163 */ 164 private static int indexForStackSlot(FrameMap frameMap, StackSlot slot) { 165 assert frameMap.offsetForStackSlot(slot) % frameMap.getTarget().wordSize == 0; 166 int value = frameMap.offsetForStackSlot(slot) / frameMap.getTarget().wordSize; 167 return value; 168 } 169 }