001/*
002 * Copyright (c) 2015, 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.hotspot.amd64;
024
025import static jdk.internal.jvmci.amd64.AMD64.*;
026
027import java.util.*;
028
029import jdk.internal.jvmci.code.*;
030
031import com.oracle.graal.compiler.common.alloc.*;
032
033class AMD64HotSpotRegisterAllocationConfig extends RegisterAllocationConfig {
034    /**
035     * Specify priority of register selection within phases of register allocation. Highest priority
036     * is first. A useful heuristic is to give registers a low priority when they are required by
037     * machine instructions, like EAX and EDX on I486, and choose no-save registers before
038     * save-on-call, & save-on-call before save-on-entry. Registers which participate in fixed
039     * calling sequences should come last. Registers which are used as pairs must fall on an even
040     * boundary.
041     *
042     * Adopted from x86_64.ad.
043     */
044    // @formatter:off
045    static final Register[] registerAllocationOrder = {
046        r10, r11, r8, r9, r12, rcx, rbx, rdi, rdx, rsi, rax, rbp, r13, r14, /*r15,*/ /*rsp,*/
047        xmm0, xmm1, xmm2,  xmm3,  xmm4,  xmm5,  xmm6,  xmm7,
048        xmm8, xmm9, xmm10, xmm11, xmm12, xmm13, xmm14, xmm15
049    };
050    // @formatter:on
051
052    public AMD64HotSpotRegisterAllocationConfig(RegisterConfig registerConfig) {
053        super(registerConfig);
054    }
055
056    @Override
057    protected Register[] initAllocatable(Register[] registers) {
058        BitSet regMap = new BitSet(registerConfig.getAllocatableRegisters().length);
059        for (Register reg : registers) {
060            regMap.set(reg.number);
061        }
062
063        Register[] allocatableRegisters = new Register[registers.length];
064        int i = 0;
065        for (Register reg : registerAllocationOrder) {
066            if (regMap.get(reg.number)) {
067                allocatableRegisters[i++] = reg;
068            }
069        }
070
071        assert i == allocatableRegisters.length;
072        return super.initAllocatable(allocatableRegisters);
073    }
074
075    @Override
076    protected AllocatableRegisters createAllocatableRegisters(Register[] registers) {
077        int min = Integer.MAX_VALUE;
078        int max = Integer.MIN_VALUE;
079        for (Register reg : registers) {
080            int number = reg.number;
081            if (number < min) {
082                min = number;
083            }
084            if (number > max) {
085                max = number;
086            }
087        }
088        assert min < max;
089        return new AllocatableRegisters(registers, min, max);
090    }
091}