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