# HG changeset patch # User Josef Eisl # Date 1428930106 -7200 # Node ID 4278c6500f2a2ee390b6b746877b2d0563effa72 # Parent 9b6ea36013c4ef5dbe23e0752a30afc8fe19473c Add RegisterAllocationConfig. diff -r 9b6ea36013c4 -r 4278c6500f2a graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/alloc/RegisterAllocationConfig.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/graal/com.oracle.graal.compiler.common/src/com/oracle/graal/compiler/common/alloc/RegisterAllocationConfig.java Mon Apr 13 15:01:46 2015 +0200 @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2015, 2015, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +package com.oracle.graal.compiler.common.alloc; + +import static com.oracle.graal.compiler.common.GraalOptions.*; + +import java.util.*; + +import com.oracle.graal.api.code.*; +import com.oracle.graal.api.meta.*; +import com.oracle.graal.compiler.common.*; + +/** + * Configuration for register allocation. This is different to {@link RegisterConfig} as it only + * returns registers specified by {@link GraalOptions#RegisterPressure}. + */ +public class RegisterAllocationConfig { + + private static Register findRegister(String name, Register[] all) { + for (Register reg : all) { + if (reg.name.equals(name)) { + return reg; + } + } + throw new IllegalArgumentException("register " + name + " is not allocatable"); + } + + private static Register[] initAllocatable(Register[] registers) { + if (RegisterPressure.getValue() != null) { + String[] names = RegisterPressure.getValue().split(","); + Register[] regs = new Register[names.length]; + for (int i = 0; i < names.length; i++) { + regs[i] = findRegister(names[i], registers); + } + return regs; + } + + return registers; + } + + private final RegisterConfig registerConfig; + private final Map categorized = new HashMap<>(); + private Register[] cachedRegisters; + + public RegisterAllocationConfig(RegisterConfig registerConfig) { + this.registerConfig = registerConfig; + } + + /** + * Gets the set of registers that can be used by the register allocator for a value of a + * particular kind. + */ + public Register[] getAllocatableRegisters(PlatformKind kind) { + PlatformKind.Key key = kind.getKey(); + if (categorized.containsKey(key)) { + Register[] val = categorized.get(key); + return val; + } + + Register[] ret = registerConfig.getAllocatableRegisters(kind, getAllocatableRegisters()); + categorized.put(key, ret); + return ret; + + } + + /** + * Gets the set of registers that can be used by the register allocator. + */ + public Register[] getAllocatableRegisters() { + if (cachedRegisters == null) { + cachedRegisters = initAllocatable(registerConfig.getAllocatableRegisters()); + } + assert cachedRegisters != null; + return cachedRegisters; + } + + public RegisterConfig getRegisterConfig() { + return registerConfig; + } + +}