43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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.core.common.alloc;
|
|
24 |
|
48861
|
25 |
import org.graalvm.collections.EconomicMap;
|
|
26 |
import org.graalvm.collections.Equivalence;
|
46344
|
27 |
import org.graalvm.compiler.core.common.GraalOptions;
|
43972
|
28 |
|
|
29 |
import jdk.vm.ci.code.Register;
|
46459
|
30 |
import jdk.vm.ci.code.Register.RegisterCategory;
|
43972
|
31 |
import jdk.vm.ci.code.RegisterArray;
|
|
32 |
import jdk.vm.ci.code.RegisterConfig;
|
|
33 |
import jdk.vm.ci.meta.PlatformKind;
|
|
34 |
|
|
35 |
/**
|
|
36 |
* Configuration for register allocation. This is different to {@link RegisterConfig} as it only
|
|
37 |
* returns registers specified by {@link GraalOptions#RegisterPressure}.
|
|
38 |
*/
|
|
39 |
public class RegisterAllocationConfig {
|
|
40 |
|
|
41 |
public static final class AllocatableRegisters {
|
|
42 |
public final Register[] allocatableRegisters;
|
|
43 |
public final int minRegisterNumber;
|
|
44 |
public final int maxRegisterNumber;
|
|
45 |
|
|
46 |
public AllocatableRegisters(RegisterArray allocatableRegisters, int minRegisterNumber, int maxRegisterNumber) {
|
|
47 |
this.allocatableRegisters = allocatableRegisters.toArray();
|
|
48 |
this.minRegisterNumber = minRegisterNumber;
|
|
49 |
this.maxRegisterNumber = maxRegisterNumber;
|
|
50 |
assert verify(allocatableRegisters, minRegisterNumber, maxRegisterNumber);
|
|
51 |
}
|
|
52 |
|
|
53 |
private static boolean verify(RegisterArray allocatableRegisters, int minRegisterNumber, int maxRegisterNumber) {
|
|
54 |
int min = Integer.MAX_VALUE;
|
|
55 |
int max = Integer.MIN_VALUE;
|
|
56 |
for (Register reg : allocatableRegisters) {
|
|
57 |
int number = reg.number;
|
|
58 |
if (number < min) {
|
|
59 |
min = number;
|
|
60 |
}
|
|
61 |
if (number > max) {
|
|
62 |
max = number;
|
|
63 |
}
|
|
64 |
}
|
|
65 |
assert minRegisterNumber == min;
|
|
66 |
assert maxRegisterNumber == max;
|
|
67 |
return true;
|
|
68 |
}
|
|
69 |
}
|
|
70 |
|
|
71 |
public static final String ALL_REGISTERS = "<all>";
|
|
72 |
|
|
73 |
private static Register findRegister(String name, RegisterArray all) {
|
|
74 |
for (Register reg : all) {
|
|
75 |
if (reg.name.equals(name)) {
|
|
76 |
return reg;
|
|
77 |
}
|
|
78 |
}
|
|
79 |
throw new IllegalArgumentException("register " + name + " is not allocatable");
|
|
80 |
}
|
|
81 |
|
|
82 |
protected RegisterArray initAllocatable(RegisterArray registers) {
|
46344
|
83 |
if (allocationRestrictedTo != null) {
|
|
84 |
Register[] regs = new Register[allocationRestrictedTo.length];
|
|
85 |
for (int i = 0; i < allocationRestrictedTo.length; i++) {
|
|
86 |
regs[i] = findRegister(allocationRestrictedTo[i], registers);
|
43972
|
87 |
}
|
|
88 |
return new RegisterArray(regs);
|
|
89 |
}
|
|
90 |
|
|
91 |
return registers;
|
|
92 |
}
|
|
93 |
|
|
94 |
protected final RegisterConfig registerConfig;
|
46344
|
95 |
private final EconomicMap<PlatformKind.Key, AllocatableRegisters> categorized = EconomicMap.create(Equivalence.DEFAULT);
|
|
96 |
private final String[] allocationRestrictedTo;
|
43972
|
97 |
private RegisterArray cachedRegisters;
|
|
98 |
|
46344
|
99 |
/**
|
|
100 |
* @param allocationRestrictedTo if not {@code null}, register allocation will be restricted to
|
|
101 |
* registers whose names appear in this array
|
|
102 |
*/
|
|
103 |
public RegisterAllocationConfig(RegisterConfig registerConfig, String[] allocationRestrictedTo) {
|
43972
|
104 |
assert registerConfig != null;
|
|
105 |
this.registerConfig = registerConfig;
|
46344
|
106 |
this.allocationRestrictedTo = allocationRestrictedTo;
|
43972
|
107 |
}
|
|
108 |
|
|
109 |
/**
|
|
110 |
* Gets the set of registers that can be used by the register allocator for a value of a
|
|
111 |
* particular kind.
|
|
112 |
*/
|
|
113 |
public AllocatableRegisters getAllocatableRegisters(PlatformKind kind) {
|
|
114 |
PlatformKind.Key key = kind.getKey();
|
|
115 |
if (categorized.containsKey(key)) {
|
|
116 |
AllocatableRegisters val = categorized.get(key);
|
|
117 |
return val;
|
|
118 |
}
|
|
119 |
AllocatableRegisters ret = createAllocatableRegisters(registerConfig.filterAllocatableRegisters(kind, getAllocatableRegisters()));
|
|
120 |
categorized.put(key, ret);
|
|
121 |
return ret;
|
|
122 |
}
|
|
123 |
|
46459
|
124 |
/**
|
|
125 |
* Gets the {@link RegisterCategory} for the given {@link PlatformKind}.
|
|
126 |
*/
|
|
127 |
public RegisterCategory getRegisterCategory(PlatformKind kind) {
|
|
128 |
return getAllocatableRegisters(kind).allocatableRegisters[0].getRegisterCategory();
|
|
129 |
}
|
|
130 |
|
43972
|
131 |
protected AllocatableRegisters createAllocatableRegisters(RegisterArray registers) {
|
|
132 |
int min = Integer.MAX_VALUE;
|
|
133 |
int max = Integer.MIN_VALUE;
|
|
134 |
for (Register reg : registers) {
|
|
135 |
int number = reg.number;
|
|
136 |
if (number < min) {
|
|
137 |
min = number;
|
|
138 |
}
|
|
139 |
if (number > max) {
|
|
140 |
max = number;
|
|
141 |
}
|
|
142 |
}
|
|
143 |
assert min < max;
|
|
144 |
return new AllocatableRegisters(registers, min, max);
|
|
145 |
|
|
146 |
}
|
|
147 |
|
|
148 |
/**
|
|
149 |
* Gets the set of registers that can be used by the register allocator.
|
|
150 |
*/
|
|
151 |
public RegisterArray getAllocatableRegisters() {
|
|
152 |
if (cachedRegisters == null) {
|
|
153 |
cachedRegisters = initAllocatable(registerConfig.getAllocatableRegisters());
|
|
154 |
}
|
|
155 |
assert cachedRegisters != null;
|
|
156 |
return cachedRegisters;
|
|
157 |
}
|
|
158 |
|
|
159 |
public RegisterConfig getRegisterConfig() {
|
|
160 |
return registerConfig;
|
|
161 |
}
|
|
162 |
|
|
163 |
}
|