001/*
002 * Copyright (c) 2013, 2015, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.lir.amd64;
024
025import static com.oracle.graal.lir.LIRInstruction.OperandFlag.*;
026import static jdk.internal.jvmci.code.ValueUtil.*;
027
028import java.util.*;
029
030import jdk.internal.jvmci.code.*;
031
032import com.oracle.graal.asm.amd64.*;
033import com.oracle.graal.lir.*;
034import com.oracle.graal.lir.StandardOp.SaveRegistersOp;
035import com.oracle.graal.lir.asm.*;
036import com.oracle.graal.lir.framemap.*;
037
038/**
039 * Saves registers to stack slots.
040 */
041@Opcode("SAVE_REGISTER")
042public class AMD64SaveRegistersOp extends AMD64LIRInstruction implements SaveRegistersOp {
043    public static final LIRInstructionClass<AMD64SaveRegistersOp> TYPE = LIRInstructionClass.create(AMD64SaveRegistersOp.class);
044
045    /**
046     * The registers (potentially) saved by this operation.
047     */
048    protected final Register[] savedRegisters;
049
050    /**
051     * The slots to which the registers are saved.
052     */
053    @Def(STACK) protected final StackSlotValue[] slots;
054
055    /**
056     * Specifies if {@link #remove(Set)} should have an effect.
057     */
058    protected final boolean supportsRemove;
059
060    /**
061     *
062     * @param savedRegisters the registers saved by this operation which may be subject to
063     *            {@linkplain #remove(Set) pruning}
064     * @param savedRegisterLocations the slots to which the registers are saved
065     * @param supportsRemove determines if registers can be {@linkplain #remove(Set) pruned}
066     */
067    public AMD64SaveRegistersOp(Register[] savedRegisters, StackSlotValue[] savedRegisterLocations, boolean supportsRemove) {
068        this(TYPE, savedRegisters, savedRegisterLocations, supportsRemove);
069    }
070
071    public AMD64SaveRegistersOp(LIRInstructionClass<? extends AMD64SaveRegistersOp> c, Register[] savedRegisters, StackSlotValue[] savedRegisterLocations, boolean supportsRemove) {
072        super(c);
073        assert Arrays.asList(savedRegisterLocations).stream().allMatch(ValueUtil::isVirtualStackSlot);
074        this.savedRegisters = savedRegisters;
075        this.slots = savedRegisterLocations;
076        this.supportsRemove = supportsRemove;
077    }
078
079    protected void saveRegister(CompilationResultBuilder crb, AMD64MacroAssembler masm, StackSlot result, Register register) {
080        RegisterValue input = register.asValue(result.getLIRKind());
081        AMD64Move.move(crb, masm, result, input);
082    }
083
084    @Override
085    public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
086        for (int i = 0; i < savedRegisters.length; i++) {
087            if (savedRegisters[i] != null) {
088                assert isStackSlot(slots[i]) : "not a StackSlot: " + slots[i];
089                saveRegister(crb, masm, asStackSlot(slots[i]), savedRegisters[i]);
090            }
091        }
092    }
093
094    public StackSlotValue[] getSlots() {
095        return slots;
096    }
097
098    public boolean supportsRemove() {
099        return supportsRemove;
100    }
101
102    public int remove(Set<Register> doNotSave) {
103        if (!supportsRemove) {
104            throw new UnsupportedOperationException();
105        }
106        return prune(doNotSave, savedRegisters);
107    }
108
109    static int prune(Set<Register> toRemove, Register[] registers) {
110        int pruned = 0;
111        for (int i = 0; i < registers.length; i++) {
112            if (registers[i] != null) {
113                if (toRemove.contains(registers[i])) {
114                    registers[i] = null;
115                    pruned++;
116                }
117            }
118        }
119        return pruned;
120    }
121
122    public RegisterSaveLayout getMap(FrameMap frameMap) {
123        int total = 0;
124        for (int i = 0; i < savedRegisters.length; i++) {
125            if (savedRegisters[i] != null) {
126                total++;
127            }
128        }
129        Register[] keys = new Register[total];
130        int[] values = new int[total];
131        if (total != 0) {
132            int mapIndex = 0;
133            for (int i = 0; i < savedRegisters.length; i++) {
134                if (savedRegisters[i] != null) {
135                    keys[mapIndex] = savedRegisters[i];
136                    assert isStackSlot(slots[i]) : "not a StackSlot: " + slots[i];
137                    StackSlot slot = asStackSlot(slots[i]);
138                    values[mapIndex] = indexForStackSlot(frameMap, slot);
139                    mapIndex++;
140                }
141            }
142            assert mapIndex == total;
143        }
144        return new RegisterSaveLayout(keys, values);
145    }
146
147    /**
148     * Computes the index of a stack slot relative to slot 0. This is also the bit index of stack
149     * slots in the reference map.
150     *
151     * @param slot a stack slot
152     * @return the index of the stack slot
153     */
154    private static int indexForStackSlot(FrameMap frameMap, StackSlot slot) {
155        assert frameMap.offsetForStackSlot(slot) % frameMap.getTarget().wordSize == 0;
156        int value = frameMap.offsetForStackSlot(slot) / frameMap.getTarget().wordSize;
157        return value;
158    }
159}