comparison graal/GraalCompiler/src/com/sun/c1x/alloc/MoveResolver.java @ 2509:16b9a8b5ad39

Renamings Runtime=>GraalRuntime and Compiler=>GraalCompiler
author Thomas Wuerthinger <thomas@wuerthinger.net>
date Wed, 27 Apr 2011 11:50:44 +0200
parents graal/Compiler/src/com/sun/c1x/alloc/MoveResolver.java@9ec15d6914ca
children
comparison
equal deleted inserted replaced
2508:fea94949e0a2 2509:16b9a8b5ad39
1 /*
2 * Copyright (c) 2009, 2011, 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 com.sun.c1x.alloc;
24
25 import java.util.*;
26
27 import com.sun.c1x.*;
28 import com.sun.c1x.debug.*;
29 import com.sun.c1x.lir.*;
30 import com.sun.c1x.util.*;
31 import com.sun.cri.ci.*;
32
33 /**
34 *
35 * @author Thomas Wuerthinger
36 */
37 final class MoveResolver {
38
39 private final LinearScan allocator;
40
41 private LIRList insertList;
42 private int insertIdx;
43 private LIRInsertionBuffer insertionBuffer; // buffer where moves are inserted
44
45 private final List<Interval> mappingFrom;
46 private final List<CiValue> mappingFromOpr;
47 private final List<Interval> mappingTo;
48 private boolean multipleReadsAllowed;
49 private final int[] registerBlocked;
50
51 private int registerBlocked(int reg) {
52 return registerBlocked[reg];
53 }
54
55 private void setRegisterBlocked(int reg, int direction) {
56 assert direction == 1 || direction == -1 : "out of bounds";
57 registerBlocked[reg] += direction;
58 }
59
60 void setMultipleReadsAllowed() {
61 multipleReadsAllowed = true;
62 }
63
64 boolean hasMappings() {
65 return mappingFrom.size() > 0;
66 }
67
68 MoveResolver(LinearScan allocator) {
69
70 this.allocator = allocator;
71 this.multipleReadsAllowed = false;
72 this.mappingFrom = new ArrayList<Interval>(8);
73 this.mappingFromOpr = new ArrayList<CiValue>(8);
74 this.mappingTo = new ArrayList<Interval>(8);
75 this.insertIdx = -1;
76 this.insertionBuffer = new LIRInsertionBuffer();
77 this.registerBlocked = new int[allocator.registers.length];
78 assert checkEmpty();
79 }
80
81 boolean checkEmpty() {
82 assert mappingFrom.size() == 0 && mappingFromOpr.size() == 0 && mappingTo.size() == 0 : "list must be empty before and after processing";
83 for (int i = 0; i < allocator.registers.length; i++) {
84 assert registerBlocked(i) == 0 : "register map must be empty before and after processing";
85 }
86 assert !multipleReadsAllowed : "must have default value";
87 return true;
88 }
89
90 private boolean verifyBeforeResolve() {
91 assert mappingFrom.size() == mappingFromOpr.size() : "length must be equal";
92 assert mappingFrom.size() == mappingTo.size() : "length must be equal";
93 assert insertList != null && insertIdx != -1 : "insert position not set";
94
95 int i;
96 int j;
97 if (!multipleReadsAllowed) {
98 for (i = 0; i < mappingFrom.size(); i++) {
99 for (j = i + 1; j < mappingFrom.size(); j++) {
100 assert mappingFrom.get(i) == null || mappingFrom.get(i) != mappingFrom.get(j) : "cannot read from same interval twice";
101 }
102 }
103 }
104
105 for (i = 0; i < mappingTo.size(); i++) {
106 for (j = i + 1; j < mappingTo.size(); j++) {
107 assert mappingTo.get(i) != mappingTo.get(j) : "cannot write to same interval twice";
108 }
109 }
110
111 HashSet<CiValue> usedRegs = new HashSet<CiValue>();
112 if (!multipleReadsAllowed) {
113 for (i = 0; i < mappingFrom.size(); i++) {
114 Interval interval = mappingFrom.get(i);
115 if (interval != null) {
116 boolean unique = usedRegs.add(interval.location());
117 assert unique : "cannot read from same register twice";
118 }
119 }
120 }
121
122 usedRegs.clear();
123 for (i = 0; i < mappingTo.size(); i++) {
124 Interval interval = mappingTo.get(i);
125 boolean unique = usedRegs.add(interval.location());
126 assert unique : "cannot write to same register twice";
127 }
128
129 usedRegs.clear();
130 for (i = 0; i < mappingFrom.size(); i++) {
131 Interval interval = mappingFrom.get(i);
132 if (interval != null && !interval.location().isRegister()) {
133 usedRegs.add(interval.location());
134 }
135 }
136 for (i = 0; i < mappingTo.size(); i++) {
137 Interval interval = mappingTo.get(i);
138 assert !usedRegs.contains(interval.location()) || interval.location() == mappingFrom.get(i).location() : "stack slots used in mappingFrom must be disjoint to mappingTo";
139 }
140
141 return true;
142 }
143
144 // mark assignedReg and assignedRegHi of the interval as blocked
145 private void blockRegisters(Interval interval) {
146 CiValue location = interval.location();
147 if (location.isRegister()) {
148 int reg = location.asRegister().number;
149 assert multipleReadsAllowed || registerBlocked(reg) == 0 : "register already marked as used";
150 setRegisterBlocked(reg, 1);
151 }
152 }
153
154 // mark assignedReg and assignedRegHi of the interval as unblocked
155 private void unblockRegisters(Interval interval) {
156 CiValue location = interval.location();
157 if (location.isRegister()) {
158 int reg = location.asRegister().number;
159 assert registerBlocked(reg) > 0 : "register already marked as unused";
160 setRegisterBlocked(reg, -1);
161 }
162 }
163
164 /**
165 * Checks if the {@linkplain Interval#location() location} of {@code to} is not blocked
166 * or is only blocked by {@code from}.
167 */
168 private boolean safeToProcessMove(Interval from, Interval to) {
169 CiValue fromReg = from != null ? from.location() : null;
170
171 CiValue reg = to.location();
172 if (reg.isRegister()) {
173 if (registerBlocked(reg.asRegister().number) > 1 || (registerBlocked(reg.asRegister().number) == 1 && reg != fromReg)) {
174 return false;
175 }
176 }
177
178 return true;
179 }
180
181 private void createInsertionBuffer(LIRList list) {
182 assert !insertionBuffer.initialized() : "overwriting existing buffer";
183 insertionBuffer.init(list);
184 }
185
186 private void appendInsertionBuffer() {
187 if (insertionBuffer.initialized()) {
188 insertionBuffer.lirList().append(insertionBuffer);
189 }
190 assert !insertionBuffer.initialized() : "must be uninitialized now";
191
192 insertList = null;
193 insertIdx = -1;
194 }
195
196 private void insertMove(Interval fromInterval, Interval toInterval) {
197 assert fromInterval.operand != toInterval.operand : "from and to interval equal: " + fromInterval;
198 assert Util.archKindsEqual(fromInterval.kind(), toInterval.kind()) : "move between different types";
199 assert insertList != null && insertIdx != -1 : "must setup insert position first";
200 assert insertionBuffer.lirList() == insertList : "wrong insertion buffer";
201
202 CiValue fromOpr = fromInterval.operand;
203 CiValue toOpr = toInterval.operand;
204
205 insertionBuffer.move(insertIdx, fromOpr, toOpr, null);
206
207 if (C1XOptions.TraceLinearScanLevel >= 4) {
208 TTY.println("MoveResolver: inserted move from %d (%s) to %d (%s)", fromInterval.operandNumber, fromInterval.location(), toInterval.operandNumber, toInterval.location());
209 }
210 }
211
212 private void insertMove(CiValue fromOpr, Interval toInterval) {
213 assert Util.archKindsEqual(fromOpr.kind, toInterval.kind()) : "move between different types";
214 assert insertList != null && insertIdx != -1 : "must setup insert position first";
215 assert insertionBuffer.lirList() == insertList : "wrong insertion buffer";
216
217 CiValue toOpr = toInterval.operand;
218 insertionBuffer.move(insertIdx, fromOpr, toOpr, null);
219
220 if (C1XOptions.TraceLinearScanLevel >= 4) {
221 TTY.print("MoveResolver: inserted move from constant %s to %d (%s)", fromOpr, toInterval.operandNumber, toInterval.location());
222 }
223 }
224
225 private void resolveMappings() {
226 //if (C1XOptions.TraceLinearScanLevel >= 4) TTY.println("MoveResolver: resolving mappings for Block B%d, index %d", insertList.block() != null ? insertList.block().blockID : -1, insertIdx);
227 assert verifyBeforeResolve();
228
229 // Block all registers that are used as input operands of a move.
230 // When a register is blocked, no move to this register is emitted.
231 // This is necessary for detecting cycles in moves.
232 int i;
233 for (i = mappingFrom.size() - 1; i >= 0; i--) {
234 Interval fromInterval = mappingFrom.get(i);
235 if (fromInterval != null) {
236 blockRegisters(fromInterval);
237 }
238 }
239
240 int spillCandidate = -1;
241 while (mappingFrom.size() > 0) {
242 boolean processedInterval = false;
243
244 for (i = mappingFrom.size() - 1; i >= 0; i--) {
245 Interval fromInterval = mappingFrom.get(i);
246 Interval toInterval = mappingTo.get(i);
247
248 if (safeToProcessMove(fromInterval, toInterval)) {
249 // this interval can be processed because target is free
250 if (fromInterval != null) {
251 insertMove(fromInterval, toInterval);
252 unblockRegisters(fromInterval);
253 } else {
254 insertMove(mappingFromOpr.get(i), toInterval);
255 }
256 mappingFrom.remove(i);
257 mappingFromOpr.remove(i);
258 mappingTo.remove(i);
259
260 processedInterval = true;
261 } else if (fromInterval != null && fromInterval.location().isRegister()) {
262 // this interval cannot be processed now because target is not free
263 // it starts in a register, so it is a possible candidate for spilling
264 spillCandidate = i;
265 }
266 }
267
268 if (!processedInterval) {
269 // no move could be processed because there is a cycle in the move list
270 // (e.g. r1 . r2, r2 . r1), so one interval must be spilled to memory
271 assert spillCandidate != -1 : "no interval in register for spilling found";
272
273 // create a new spill interval and assign a stack slot to it
274 Interval fromInterval = mappingFrom.get(spillCandidate);
275 Interval spillInterval = allocator.createDerivedInterval(fromInterval);
276 spillInterval.setKind(fromInterval.kind());
277
278 // add a dummy range because real position is difficult to calculate
279 // Note: this range is a special case when the integrity of the allocation is checked
280 spillInterval.addRange(1, 2);
281
282 // do not allocate a new spill slot for temporary interval, but
283 // use spill slot assigned to fromInterval. Otherwise moves from
284 // one stack slot to another can happen (not allowed by LIRAssembler
285 CiStackSlot spillSlot = fromInterval.spillSlot();
286 if (spillSlot == null) {
287 spillSlot = allocator.allocateSpillSlot(spillInterval.kind());
288 fromInterval.setSpillSlot(spillSlot);
289 }
290 spillInterval.assignLocation(spillSlot);
291
292 if (C1XOptions.TraceLinearScanLevel >= 4) {
293 TTY.println("created new Interval %s for spilling", spillInterval.operand);
294 }
295
296 // insert a move from register to stack and update the mapping
297 insertMove(fromInterval, spillInterval);
298 mappingFrom.set(spillCandidate, spillInterval);
299 unblockRegisters(fromInterval);
300 }
301 }
302
303 // reset to default value
304 multipleReadsAllowed = false;
305
306 // check that all intervals have been processed
307 assert checkEmpty();
308 }
309
310 void setInsertPosition(LIRList insertList, int insertIdx) {
311 //if (C1XOptions.TraceLinearScanLevel >= 4) TTY.println("MoveResolver: setting insert position to Block B%d, index %d", insertList.block() != null ? insertList.block().blockID : -1, insertIdx);
312 assert this.insertList == null && this.insertIdx == -1 : "use moveInsertPosition instead of setInsertPosition when data already set";
313
314 createInsertionBuffer(insertList);
315 this.insertList = insertList;
316 this.insertIdx = insertIdx;
317 }
318
319 void moveInsertPosition(LIRList insertList, int insertIdx) {
320 //if (C1XOptions.TraceLinearScanLevel >= 4) TTY.println("MoveResolver: moving insert position to Block B%d, index %d", (insertList != null && insertList.block() != null) ? insertList.block().blockID : -1, insertIdx);
321
322 if (this.insertList != null && (this.insertList != insertList || this.insertIdx != insertIdx)) {
323 // insert position changed . resolve current mappings
324 resolveMappings();
325 }
326
327 if (this.insertList != insertList) {
328 // block changed . append insertionBuffer because it is
329 // bound to a specific block and create a new insertionBuffer
330 appendInsertionBuffer();
331 createInsertionBuffer(insertList);
332 }
333
334 this.insertList = insertList;
335 this.insertIdx = insertIdx;
336 }
337
338 void addMapping(Interval fromInterval, Interval toInterval) {
339 if (C1XOptions.TraceLinearScanLevel >= 4) {
340 TTY.println("MoveResolver: adding mapping from interval %d (%s) to interval %d (%s)", fromInterval.operandNumber, fromInterval.location(), toInterval.operandNumber, toInterval.location());
341 }
342
343 assert fromInterval.operand != toInterval.operand : "from and to interval equal: " + fromInterval;
344 assert Util.archKindsEqual(fromInterval.kind(), toInterval.kind());
345 mappingFrom.add(fromInterval);
346 mappingFromOpr.add(CiValue.IllegalValue);
347 mappingTo.add(toInterval);
348 }
349
350 void addMapping(CiValue fromOpr, Interval toInterval) {
351 if (C1XOptions.TraceLinearScanLevel >= 4) {
352 TTY.println("MoveResolver: adding mapping from %s to %d (%s)", fromOpr, toInterval.operandNumber, toInterval.location());
353 }
354 assert fromOpr.isConstant() : "only for constants";
355
356 mappingFrom.add(null);
357 mappingFromOpr.add(fromOpr);
358 mappingTo.add(toInterval);
359 }
360
361 void resolveAndAppendMoves() {
362 if (hasMappings()) {
363 resolveMappings();
364 }
365 appendInsertionBuffer();
366 }
367 }