001/*
002 * Copyright (c) 2010, 2011, 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 jdk.internal.jvmci.code;
024
025import java.util.*;
026
027/**
028 * A collection of register attributes. The specific attribute values for a register may be local to
029 * a compilation context. For example, a {@link RegisterConfig} in use during a compilation will
030 * determine which registers are callee saved.
031 */
032public class RegisterAttributes {
033
034    private final boolean callerSave;
035    private final boolean calleeSave;
036    private final boolean allocatable;
037
038    public RegisterAttributes(boolean isCallerSave, boolean isCalleeSave, boolean isAllocatable) {
039        this.callerSave = isCallerSave;
040        this.calleeSave = isCalleeSave;
041        this.allocatable = isAllocatable;
042    }
043
044    public static final RegisterAttributes NONE = new RegisterAttributes(false, false, false);
045
046    /**
047     * Creates a map from register {@linkplain Register#number numbers} to register
048     * {@linkplain RegisterAttributes attributes} for a given register configuration and set of
049     * registers.
050     *
051     * @param registerConfig a register configuration
052     * @param registers a set of registers
053     * @return an array whose length is the max register number in {@code registers} plus 1. An
054     *         element at index i holds the attributes of the register whose number is i.
055     */
056    public static RegisterAttributes[] createMap(RegisterConfig registerConfig, Register[] registers) {
057        RegisterAttributes[] map = new RegisterAttributes[registers.length];
058        for (Register reg : registers) {
059            if (reg != null) {
060                CalleeSaveLayout csl = registerConfig.getCalleeSaveLayout();
061                RegisterAttributes attr = new RegisterAttributes(Arrays.asList(registerConfig.getCallerSaveRegisters()).contains(reg),
062                                csl == null ? false : Arrays.asList(csl.registers).contains(reg), Arrays.asList(registerConfig.getAllocatableRegisters()).contains(reg));
063                if (map.length <= reg.number) {
064                    map = Arrays.copyOf(map, reg.number + 1);
065                }
066                map[reg.number] = attr;
067            }
068        }
069        for (int i = 0; i < map.length; i++) {
070            if (map[i] == null) {
071                map[i] = NONE;
072            }
073        }
074        return map;
075    }
076
077    /**
078     * @return Denotes a register that is available for use by a register allocator.
079     */
080    public boolean isAllocatable() {
081        return allocatable;
082    }
083
084    /**
085     * @return Denotes a register whose value preservation (if required) across a call is the
086     *         responsibility of the callee.
087     */
088    public boolean isCalleeSave() {
089        return calleeSave;
090    }
091
092    /**
093     * @return Denotes a register whose value preservation (if required) across a call is the
094     *         responsibility of the caller.
095     */
096    public boolean isCallerSave() {
097        return callerSave;
098    }
099}