43972
|
1 |
/*
|
|
2 |
* Copyright (c) 2011, 2016, 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.lir.amd64;
|
|
24 |
|
49451
|
25 |
import static java.lang.Double.doubleToRawLongBits;
|
|
26 |
import static java.lang.Float.floatToRawIntBits;
|
|
27 |
import static jdk.vm.ci.code.ValueUtil.asRegister;
|
|
28 |
import static jdk.vm.ci.code.ValueUtil.isRegister;
|
|
29 |
import static jdk.vm.ci.code.ValueUtil.isStackSlot;
|
47798
|
30 |
import static org.graalvm.compiler.asm.amd64.AMD64Assembler.ConditionFlag.Equal;
|
|
31 |
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC;
|
43972
|
32 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.COMPOSITE;
|
48861
|
33 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.CONST;
|
43972
|
34 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.HINT;
|
47798
|
35 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
|
43972
|
36 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.REG;
|
|
37 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.STACK;
|
|
38 |
import static org.graalvm.compiler.lir.LIRInstruction.OperandFlag.UNINITIALIZED;
|
|
39 |
import static org.graalvm.compiler.lir.LIRValueUtil.asJavaConstant;
|
|
40 |
import static org.graalvm.compiler.lir.LIRValueUtil.isJavaConstant;
|
|
41 |
|
47798
|
42 |
import org.graalvm.compiler.asm.Label;
|
43972
|
43 |
import org.graalvm.compiler.asm.amd64.AMD64Address;
|
|
44 |
import org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64MIOp;
|
|
45 |
import org.graalvm.compiler.asm.amd64.AMD64Assembler.AMD64MOp;
|
|
46 |
import org.graalvm.compiler.asm.amd64.AMD64Assembler.OperandSize;
|
|
47 |
import org.graalvm.compiler.asm.amd64.AMD64MacroAssembler;
|
49451
|
48 |
import org.graalvm.compiler.core.common.CompressEncoding;
|
|
49 |
import org.graalvm.compiler.core.common.LIRKind;
|
|
50 |
import org.graalvm.compiler.core.common.NumUtil;
|
47798
|
51 |
import org.graalvm.compiler.core.common.spi.LIRKindTool;
|
43972
|
52 |
import org.graalvm.compiler.core.common.type.DataPointerConstant;
|
|
53 |
import org.graalvm.compiler.debug.GraalError;
|
|
54 |
import org.graalvm.compiler.lir.LIRFrameState;
|
|
55 |
import org.graalvm.compiler.lir.LIRInstructionClass;
|
|
56 |
import org.graalvm.compiler.lir.Opcode;
|
|
57 |
import org.graalvm.compiler.lir.StandardOp.LoadConstantOp;
|
|
58 |
import org.graalvm.compiler.lir.StandardOp.NullCheck;
|
|
59 |
import org.graalvm.compiler.lir.StandardOp.ValueMoveOp;
|
|
60 |
import org.graalvm.compiler.lir.VirtualStackSlot;
|
|
61 |
import org.graalvm.compiler.lir.asm.CompilationResultBuilder;
|
49451
|
62 |
import org.graalvm.compiler.options.OptionValues;
|
43972
|
63 |
|
|
64 |
import jdk.vm.ci.amd64.AMD64;
|
|
65 |
import jdk.vm.ci.amd64.AMD64Kind;
|
|
66 |
import jdk.vm.ci.code.Register;
|
|
67 |
import jdk.vm.ci.code.StackSlot;
|
|
68 |
import jdk.vm.ci.meta.AllocatableValue;
|
|
69 |
import jdk.vm.ci.meta.Constant;
|
|
70 |
import jdk.vm.ci.meta.JavaConstant;
|
|
71 |
import jdk.vm.ci.meta.Value;
|
|
72 |
|
|
73 |
public class AMD64Move {
|
|
74 |
|
|
75 |
private abstract static class AbstractMoveOp extends AMD64LIRInstruction implements ValueMoveOp {
|
|
76 |
public static final LIRInstructionClass<AbstractMoveOp> TYPE = LIRInstructionClass.create(AbstractMoveOp.class);
|
|
77 |
|
|
78 |
private AMD64Kind moveKind;
|
|
79 |
|
|
80 |
protected AbstractMoveOp(LIRInstructionClass<? extends AbstractMoveOp> c, AMD64Kind moveKind) {
|
|
81 |
super(c);
|
|
82 |
this.moveKind = moveKind;
|
|
83 |
}
|
|
84 |
|
|
85 |
@Override
|
|
86 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
87 |
move(moveKind, crb, masm, getResult(), getInput());
|
|
88 |
}
|
|
89 |
}
|
|
90 |
|
|
91 |
@Opcode("MOVE")
|
|
92 |
public static final class MoveToRegOp extends AbstractMoveOp {
|
|
93 |
public static final LIRInstructionClass<MoveToRegOp> TYPE = LIRInstructionClass.create(MoveToRegOp.class);
|
|
94 |
|
|
95 |
@Def({REG, HINT}) protected AllocatableValue result;
|
|
96 |
@Use({REG, STACK}) protected AllocatableValue input;
|
|
97 |
|
|
98 |
public MoveToRegOp(AMD64Kind moveKind, AllocatableValue result, AllocatableValue input) {
|
|
99 |
super(TYPE, moveKind);
|
|
100 |
this.result = result;
|
|
101 |
this.input = input;
|
|
102 |
}
|
|
103 |
|
|
104 |
@Override
|
|
105 |
public AllocatableValue getInput() {
|
|
106 |
return input;
|
|
107 |
}
|
|
108 |
|
|
109 |
@Override
|
|
110 |
public AllocatableValue getResult() {
|
|
111 |
return result;
|
|
112 |
}
|
|
113 |
}
|
|
114 |
|
|
115 |
@Opcode("MOVE")
|
|
116 |
public static final class MoveFromRegOp extends AbstractMoveOp {
|
|
117 |
public static final LIRInstructionClass<MoveFromRegOp> TYPE = LIRInstructionClass.create(MoveFromRegOp.class);
|
|
118 |
|
|
119 |
@Def({REG, STACK}) protected AllocatableValue result;
|
|
120 |
@Use({REG, HINT}) protected AllocatableValue input;
|
|
121 |
|
|
122 |
public MoveFromRegOp(AMD64Kind moveKind, AllocatableValue result, AllocatableValue input) {
|
|
123 |
super(TYPE, moveKind);
|
|
124 |
this.result = result;
|
|
125 |
this.input = input;
|
|
126 |
}
|
|
127 |
|
|
128 |
@Override
|
|
129 |
public AllocatableValue getInput() {
|
|
130 |
return input;
|
|
131 |
}
|
|
132 |
|
|
133 |
@Override
|
|
134 |
public AllocatableValue getResult() {
|
|
135 |
return result;
|
|
136 |
}
|
|
137 |
}
|
|
138 |
|
|
139 |
@Opcode("MOVE")
|
|
140 |
public static class MoveFromConstOp extends AMD64LIRInstruction implements LoadConstantOp {
|
|
141 |
public static final LIRInstructionClass<MoveFromConstOp> TYPE = LIRInstructionClass.create(MoveFromConstOp.class);
|
|
142 |
|
|
143 |
@Def({REG, STACK}) protected AllocatableValue result;
|
|
144 |
private final JavaConstant input;
|
|
145 |
|
|
146 |
public MoveFromConstOp(AllocatableValue result, JavaConstant input) {
|
|
147 |
super(TYPE);
|
|
148 |
this.result = result;
|
|
149 |
this.input = input;
|
|
150 |
}
|
|
151 |
|
|
152 |
@Override
|
|
153 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
154 |
if (isRegister(result)) {
|
|
155 |
const2reg(crb, masm, asRegister(result), input);
|
|
156 |
} else {
|
|
157 |
assert isStackSlot(result);
|
|
158 |
const2stack(crb, masm, result, input);
|
|
159 |
}
|
|
160 |
}
|
|
161 |
|
|
162 |
@Override
|
|
163 |
public Constant getConstant() {
|
|
164 |
return input;
|
|
165 |
}
|
|
166 |
|
|
167 |
@Override
|
|
168 |
public AllocatableValue getResult() {
|
|
169 |
return result;
|
|
170 |
}
|
|
171 |
}
|
|
172 |
|
|
173 |
@Opcode("STACKMOVE")
|
|
174 |
public static final class AMD64StackMove extends AMD64LIRInstruction implements ValueMoveOp {
|
|
175 |
public static final LIRInstructionClass<AMD64StackMove> TYPE = LIRInstructionClass.create(AMD64StackMove.class);
|
|
176 |
|
|
177 |
@Def({STACK}) protected AllocatableValue result;
|
|
178 |
@Use({STACK, HINT}) protected AllocatableValue input;
|
|
179 |
@Alive({OperandFlag.STACK, OperandFlag.UNINITIALIZED}) private AllocatableValue backupSlot;
|
|
180 |
|
|
181 |
private Register scratch;
|
|
182 |
|
|
183 |
public AMD64StackMove(AllocatableValue result, AllocatableValue input, Register scratch, AllocatableValue backupSlot) {
|
|
184 |
super(TYPE);
|
|
185 |
this.result = result;
|
|
186 |
this.input = input;
|
|
187 |
this.backupSlot = backupSlot;
|
|
188 |
this.scratch = scratch;
|
|
189 |
}
|
|
190 |
|
|
191 |
@Override
|
|
192 |
public AllocatableValue getInput() {
|
|
193 |
return input;
|
|
194 |
}
|
|
195 |
|
|
196 |
@Override
|
|
197 |
public AllocatableValue getResult() {
|
|
198 |
return result;
|
|
199 |
}
|
|
200 |
|
|
201 |
public Register getScratchRegister() {
|
|
202 |
return scratch;
|
|
203 |
}
|
|
204 |
|
|
205 |
public AllocatableValue getBackupSlot() {
|
|
206 |
return backupSlot;
|
|
207 |
}
|
|
208 |
|
|
209 |
@Override
|
|
210 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
211 |
AMD64Kind backupKind = (AMD64Kind) backupSlot.getPlatformKind();
|
|
212 |
if (backupKind.isXMM()) {
|
|
213 |
// graal doesn't use vector values, so it's safe to backup using DOUBLE
|
|
214 |
backupKind = AMD64Kind.DOUBLE;
|
|
215 |
}
|
|
216 |
|
|
217 |
// backup scratch register
|
|
218 |
reg2stack(backupKind, crb, masm, backupSlot, scratch);
|
|
219 |
// move stack slot
|
|
220 |
stack2reg((AMD64Kind) getInput().getPlatformKind(), crb, masm, scratch, getInput());
|
|
221 |
reg2stack((AMD64Kind) getResult().getPlatformKind(), crb, masm, getResult(), scratch);
|
|
222 |
// restore scratch register
|
|
223 |
stack2reg(backupKind, crb, masm, scratch, backupSlot);
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
|
227 |
@Opcode("MULTISTACKMOVE")
|
|
228 |
public static final class AMD64MultiStackMove extends AMD64LIRInstruction {
|
|
229 |
public static final LIRInstructionClass<AMD64MultiStackMove> TYPE = LIRInstructionClass.create(AMD64MultiStackMove.class);
|
|
230 |
|
|
231 |
@Def({STACK}) protected AllocatableValue[] results;
|
|
232 |
@Use({STACK}) protected Value[] inputs;
|
|
233 |
@Alive({OperandFlag.STACK, OperandFlag.UNINITIALIZED}) private AllocatableValue backupSlot;
|
|
234 |
|
|
235 |
private Register scratch;
|
|
236 |
|
|
237 |
public AMD64MultiStackMove(AllocatableValue[] results, Value[] inputs, Register scratch, AllocatableValue backupSlot) {
|
|
238 |
super(TYPE);
|
|
239 |
this.results = results;
|
|
240 |
this.inputs = inputs;
|
|
241 |
this.backupSlot = backupSlot;
|
|
242 |
this.scratch = scratch;
|
|
243 |
}
|
|
244 |
|
|
245 |
@Override
|
|
246 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
247 |
AMD64Kind backupKind = (AMD64Kind) backupSlot.getPlatformKind();
|
|
248 |
if (backupKind.isXMM()) {
|
|
249 |
// graal doesn't use vector values, so it's safe to backup using DOUBLE
|
|
250 |
backupKind = AMD64Kind.DOUBLE;
|
|
251 |
}
|
|
252 |
|
|
253 |
// backup scratch register
|
|
254 |
move(backupKind, crb, masm, backupSlot, scratch.asValue(backupSlot.getValueKind()));
|
|
255 |
for (int i = 0; i < results.length; i++) {
|
|
256 |
Value input = inputs[i];
|
|
257 |
AllocatableValue result = results[i];
|
|
258 |
// move stack slot
|
|
259 |
move((AMD64Kind) input.getPlatformKind(), crb, masm, scratch.asValue(input.getValueKind()), input);
|
|
260 |
move((AMD64Kind) result.getPlatformKind(), crb, masm, result, scratch.asValue(result.getValueKind()));
|
|
261 |
}
|
|
262 |
// restore scratch register
|
|
263 |
move(backupKind, crb, masm, scratch.asValue(backupSlot.getValueKind()), backupSlot);
|
|
264 |
}
|
|
265 |
}
|
|
266 |
|
|
267 |
@Opcode("STACKMOVE")
|
|
268 |
public static final class AMD64PushPopStackMove extends AMD64LIRInstruction implements ValueMoveOp {
|
|
269 |
public static final LIRInstructionClass<AMD64PushPopStackMove> TYPE = LIRInstructionClass.create(AMD64PushPopStackMove.class);
|
|
270 |
|
|
271 |
@Def({STACK}) protected AllocatableValue result;
|
|
272 |
@Use({STACK, HINT}) protected AllocatableValue input;
|
|
273 |
private final OperandSize size;
|
|
274 |
|
|
275 |
public AMD64PushPopStackMove(OperandSize size, AllocatableValue result, AllocatableValue input) {
|
|
276 |
super(TYPE);
|
|
277 |
this.result = result;
|
|
278 |
this.input = input;
|
|
279 |
this.size = size;
|
|
280 |
}
|
|
281 |
|
|
282 |
@Override
|
|
283 |
public AllocatableValue getInput() {
|
|
284 |
return input;
|
|
285 |
}
|
|
286 |
|
|
287 |
@Override
|
|
288 |
public AllocatableValue getResult() {
|
|
289 |
return result;
|
|
290 |
}
|
|
291 |
|
|
292 |
@Override
|
|
293 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
294 |
AMD64MOp.PUSH.emit(masm, size, (AMD64Address) crb.asAddress(input));
|
|
295 |
AMD64MOp.POP.emit(masm, size, (AMD64Address) crb.asAddress(result));
|
|
296 |
}
|
|
297 |
}
|
|
298 |
|
|
299 |
public static final class LeaOp extends AMD64LIRInstruction {
|
|
300 |
public static final LIRInstructionClass<LeaOp> TYPE = LIRInstructionClass.create(LeaOp.class);
|
|
301 |
|
|
302 |
@Def({REG}) protected AllocatableValue result;
|
|
303 |
@Use({COMPOSITE, UNINITIALIZED}) protected AMD64AddressValue address;
|
48190
|
304 |
private final OperandSize size;
|
43972
|
305 |
|
48190
|
306 |
public LeaOp(AllocatableValue result, AMD64AddressValue address, OperandSize size) {
|
43972
|
307 |
super(TYPE);
|
|
308 |
this.result = result;
|
|
309 |
this.address = address;
|
48190
|
310 |
this.size = size;
|
43972
|
311 |
}
|
|
312 |
|
|
313 |
@Override
|
|
314 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
48190
|
315 |
if (size == OperandSize.QWORD) {
|
|
316 |
masm.leaq(asRegister(result, AMD64Kind.QWORD), address.toAddress());
|
|
317 |
} else {
|
|
318 |
assert size == OperandSize.DWORD;
|
|
319 |
masm.lead(asRegister(result, AMD64Kind.DWORD), address.toAddress());
|
|
320 |
}
|
43972
|
321 |
}
|
|
322 |
}
|
|
323 |
|
|
324 |
public static final class LeaDataOp extends AMD64LIRInstruction {
|
|
325 |
public static final LIRInstructionClass<LeaDataOp> TYPE = LIRInstructionClass.create(LeaDataOp.class);
|
|
326 |
|
|
327 |
@Def({REG}) protected AllocatableValue result;
|
|
328 |
private final DataPointerConstant data;
|
|
329 |
|
|
330 |
public LeaDataOp(AllocatableValue result, DataPointerConstant data) {
|
|
331 |
super(TYPE);
|
|
332 |
this.result = result;
|
|
333 |
this.data = data;
|
|
334 |
}
|
|
335 |
|
|
336 |
@Override
|
|
337 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
338 |
masm.leaq(asRegister(result), (AMD64Address) crb.recordDataReferenceInCode(data));
|
|
339 |
}
|
|
340 |
}
|
|
341 |
|
|
342 |
public static final class StackLeaOp extends AMD64LIRInstruction {
|
|
343 |
public static final LIRInstructionClass<StackLeaOp> TYPE = LIRInstructionClass.create(StackLeaOp.class);
|
|
344 |
|
|
345 |
@Def({REG}) protected AllocatableValue result;
|
|
346 |
@Use({STACK, UNINITIALIZED}) protected AllocatableValue slot;
|
|
347 |
|
|
348 |
public StackLeaOp(AllocatableValue result, AllocatableValue slot) {
|
|
349 |
super(TYPE);
|
|
350 |
this.result = result;
|
|
351 |
this.slot = slot;
|
|
352 |
assert slot instanceof VirtualStackSlot || slot instanceof StackSlot;
|
|
353 |
}
|
|
354 |
|
|
355 |
@Override
|
|
356 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
357 |
masm.leaq(asRegister(result, AMD64Kind.QWORD), (AMD64Address) crb.asAddress(slot));
|
|
358 |
}
|
|
359 |
}
|
|
360 |
|
|
361 |
public static final class MembarOp extends AMD64LIRInstruction {
|
|
362 |
public static final LIRInstructionClass<MembarOp> TYPE = LIRInstructionClass.create(MembarOp.class);
|
|
363 |
|
|
364 |
private final int barriers;
|
|
365 |
|
|
366 |
public MembarOp(final int barriers) {
|
|
367 |
super(TYPE);
|
|
368 |
this.barriers = barriers;
|
|
369 |
}
|
|
370 |
|
|
371 |
@Override
|
|
372 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
373 |
masm.membar(barriers);
|
|
374 |
}
|
|
375 |
}
|
|
376 |
|
|
377 |
public static final class NullCheckOp extends AMD64LIRInstruction implements NullCheck {
|
|
378 |
public static final LIRInstructionClass<NullCheckOp> TYPE = LIRInstructionClass.create(NullCheckOp.class);
|
|
379 |
|
|
380 |
@Use({COMPOSITE}) protected AMD64AddressValue address;
|
|
381 |
@State protected LIRFrameState state;
|
|
382 |
|
|
383 |
public NullCheckOp(AMD64AddressValue address, LIRFrameState state) {
|
|
384 |
super(TYPE);
|
|
385 |
this.address = address;
|
|
386 |
this.state = state;
|
|
387 |
}
|
|
388 |
|
|
389 |
@Override
|
|
390 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
391 |
crb.recordImplicitException(masm.position(), state);
|
|
392 |
masm.nullCheck(address.toAddress());
|
|
393 |
}
|
|
394 |
|
|
395 |
@Override
|
|
396 |
public Value getCheckedValue() {
|
|
397 |
return address.base;
|
|
398 |
}
|
|
399 |
|
|
400 |
@Override
|
|
401 |
public LIRFrameState getState() {
|
|
402 |
return state;
|
|
403 |
}
|
|
404 |
}
|
|
405 |
|
|
406 |
@Opcode("CAS")
|
|
407 |
public static final class CompareAndSwapOp extends AMD64LIRInstruction {
|
|
408 |
public static final LIRInstructionClass<CompareAndSwapOp> TYPE = LIRInstructionClass.create(CompareAndSwapOp.class);
|
|
409 |
|
|
410 |
private final AMD64Kind accessKind;
|
|
411 |
|
|
412 |
@Def protected AllocatableValue result;
|
|
413 |
@Use({COMPOSITE}) protected AMD64AddressValue address;
|
|
414 |
@Use protected AllocatableValue cmpValue;
|
|
415 |
@Use protected AllocatableValue newValue;
|
|
416 |
|
|
417 |
public CompareAndSwapOp(AMD64Kind accessKind, AllocatableValue result, AMD64AddressValue address, AllocatableValue cmpValue, AllocatableValue newValue) {
|
|
418 |
super(TYPE);
|
|
419 |
this.accessKind = accessKind;
|
|
420 |
this.result = result;
|
|
421 |
this.address = address;
|
|
422 |
this.cmpValue = cmpValue;
|
|
423 |
this.newValue = newValue;
|
|
424 |
}
|
|
425 |
|
|
426 |
@Override
|
|
427 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
428 |
assert asRegister(cmpValue).equals(AMD64.rax) && asRegister(result).equals(AMD64.rax);
|
|
429 |
|
|
430 |
if (crb.target.isMP) {
|
|
431 |
masm.lock();
|
|
432 |
}
|
|
433 |
switch (accessKind) {
|
|
434 |
case DWORD:
|
|
435 |
masm.cmpxchgl(asRegister(newValue), address.toAddress());
|
|
436 |
break;
|
|
437 |
case QWORD:
|
|
438 |
masm.cmpxchgq(asRegister(newValue), address.toAddress());
|
|
439 |
break;
|
|
440 |
default:
|
|
441 |
throw GraalError.shouldNotReachHere();
|
|
442 |
}
|
|
443 |
}
|
|
444 |
}
|
|
445 |
|
|
446 |
@Opcode("ATOMIC_READ_AND_ADD")
|
|
447 |
public static final class AtomicReadAndAddOp extends AMD64LIRInstruction {
|
|
448 |
public static final LIRInstructionClass<AtomicReadAndAddOp> TYPE = LIRInstructionClass.create(AtomicReadAndAddOp.class);
|
|
449 |
|
|
450 |
private final AMD64Kind accessKind;
|
|
451 |
|
|
452 |
@Def protected AllocatableValue result;
|
|
453 |
@Alive({COMPOSITE}) protected AMD64AddressValue address;
|
|
454 |
@Use protected AllocatableValue delta;
|
|
455 |
|
|
456 |
public AtomicReadAndAddOp(AMD64Kind accessKind, AllocatableValue result, AMD64AddressValue address, AllocatableValue delta) {
|
|
457 |
super(TYPE);
|
|
458 |
this.accessKind = accessKind;
|
|
459 |
this.result = result;
|
|
460 |
this.address = address;
|
|
461 |
this.delta = delta;
|
|
462 |
}
|
|
463 |
|
|
464 |
@Override
|
|
465 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
466 |
move(accessKind, crb, masm, result, delta);
|
|
467 |
if (crb.target.isMP) {
|
|
468 |
masm.lock();
|
|
469 |
}
|
|
470 |
switch (accessKind) {
|
|
471 |
case DWORD:
|
|
472 |
masm.xaddl(address.toAddress(), asRegister(result));
|
|
473 |
break;
|
|
474 |
case QWORD:
|
|
475 |
masm.xaddq(address.toAddress(), asRegister(result));
|
|
476 |
break;
|
|
477 |
default:
|
|
478 |
throw GraalError.shouldNotReachHere();
|
|
479 |
}
|
|
480 |
}
|
|
481 |
}
|
|
482 |
|
|
483 |
@Opcode("ATOMIC_READ_AND_WRITE")
|
|
484 |
public static final class AtomicReadAndWriteOp extends AMD64LIRInstruction {
|
|
485 |
public static final LIRInstructionClass<AtomicReadAndWriteOp> TYPE = LIRInstructionClass.create(AtomicReadAndWriteOp.class);
|
|
486 |
|
|
487 |
private final AMD64Kind accessKind;
|
|
488 |
|
|
489 |
@Def protected AllocatableValue result;
|
|
490 |
@Alive({COMPOSITE}) protected AMD64AddressValue address;
|
|
491 |
@Use protected AllocatableValue newValue;
|
|
492 |
|
|
493 |
public AtomicReadAndWriteOp(AMD64Kind accessKind, AllocatableValue result, AMD64AddressValue address, AllocatableValue newValue) {
|
|
494 |
super(TYPE);
|
|
495 |
this.accessKind = accessKind;
|
|
496 |
this.result = result;
|
|
497 |
this.address = address;
|
|
498 |
this.newValue = newValue;
|
|
499 |
}
|
|
500 |
|
|
501 |
@Override
|
|
502 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
503 |
move(accessKind, crb, masm, result, newValue);
|
|
504 |
switch (accessKind) {
|
|
505 |
case DWORD:
|
|
506 |
masm.xchgl(asRegister(result), address.toAddress());
|
|
507 |
break;
|
|
508 |
case QWORD:
|
|
509 |
masm.xchgq(asRegister(result), address.toAddress());
|
|
510 |
break;
|
|
511 |
default:
|
|
512 |
throw GraalError.shouldNotReachHere();
|
|
513 |
}
|
|
514 |
}
|
|
515 |
}
|
|
516 |
|
|
517 |
public static void move(CompilationResultBuilder crb, AMD64MacroAssembler masm, Value result, Value input) {
|
|
518 |
move((AMD64Kind) result.getPlatformKind(), crb, masm, result, input);
|
|
519 |
}
|
|
520 |
|
|
521 |
public static void move(AMD64Kind moveKind, CompilationResultBuilder crb, AMD64MacroAssembler masm, Value result, Value input) {
|
|
522 |
if (isRegister(input)) {
|
|
523 |
if (isRegister(result)) {
|
|
524 |
reg2reg(moveKind, masm, result, input);
|
|
525 |
} else if (isStackSlot(result)) {
|
|
526 |
reg2stack(moveKind, crb, masm, result, asRegister(input));
|
|
527 |
} else {
|
|
528 |
throw GraalError.shouldNotReachHere();
|
|
529 |
}
|
|
530 |
} else if (isStackSlot(input)) {
|
|
531 |
if (isRegister(result)) {
|
|
532 |
stack2reg(moveKind, crb, masm, asRegister(result), input);
|
|
533 |
} else {
|
|
534 |
throw GraalError.shouldNotReachHere();
|
|
535 |
}
|
|
536 |
} else if (isJavaConstant(input)) {
|
|
537 |
if (isRegister(result)) {
|
|
538 |
const2reg(crb, masm, asRegister(result), asJavaConstant(input));
|
|
539 |
} else if (isStackSlot(result)) {
|
|
540 |
const2stack(crb, masm, result, asJavaConstant(input));
|
|
541 |
} else {
|
|
542 |
throw GraalError.shouldNotReachHere();
|
|
543 |
}
|
|
544 |
} else {
|
|
545 |
throw GraalError.shouldNotReachHere();
|
|
546 |
}
|
|
547 |
}
|
|
548 |
|
|
549 |
private static void reg2reg(AMD64Kind kind, AMD64MacroAssembler masm, Value result, Value input) {
|
|
550 |
if (asRegister(input).equals(asRegister(result))) {
|
|
551 |
return;
|
|
552 |
}
|
|
553 |
switch (kind) {
|
|
554 |
case BYTE:
|
|
555 |
case WORD:
|
|
556 |
case DWORD:
|
|
557 |
masm.movl(asRegister(result), asRegister(input));
|
|
558 |
break;
|
|
559 |
case QWORD:
|
|
560 |
masm.movq(asRegister(result), asRegister(input));
|
|
561 |
break;
|
|
562 |
case SINGLE:
|
|
563 |
masm.movflt(asRegister(result, AMD64Kind.SINGLE), asRegister(input, AMD64Kind.SINGLE));
|
|
564 |
break;
|
|
565 |
case DOUBLE:
|
|
566 |
masm.movdbl(asRegister(result, AMD64Kind.DOUBLE), asRegister(input, AMD64Kind.DOUBLE));
|
|
567 |
break;
|
|
568 |
default:
|
|
569 |
throw GraalError.shouldNotReachHere("kind=" + kind);
|
|
570 |
}
|
|
571 |
}
|
|
572 |
|
|
573 |
public static void reg2stack(AMD64Kind kind, CompilationResultBuilder crb, AMD64MacroAssembler masm, Value result, Register input) {
|
|
574 |
AMD64Address dest = (AMD64Address) crb.asAddress(result);
|
|
575 |
switch (kind) {
|
|
576 |
case BYTE:
|
|
577 |
masm.movb(dest, input);
|
|
578 |
break;
|
|
579 |
case WORD:
|
|
580 |
masm.movw(dest, input);
|
|
581 |
break;
|
|
582 |
case DWORD:
|
|
583 |
masm.movl(dest, input);
|
|
584 |
break;
|
|
585 |
case QWORD:
|
|
586 |
masm.movq(dest, input);
|
|
587 |
break;
|
|
588 |
case SINGLE:
|
|
589 |
masm.movflt(dest, input);
|
|
590 |
break;
|
|
591 |
case DOUBLE:
|
|
592 |
masm.movsd(dest, input);
|
|
593 |
break;
|
|
594 |
default:
|
|
595 |
throw GraalError.shouldNotReachHere();
|
|
596 |
}
|
|
597 |
}
|
|
598 |
|
|
599 |
public static void stack2reg(AMD64Kind kind, CompilationResultBuilder crb, AMD64MacroAssembler masm, Register result, Value input) {
|
|
600 |
AMD64Address src = (AMD64Address) crb.asAddress(input);
|
|
601 |
switch (kind) {
|
|
602 |
case BYTE:
|
|
603 |
masm.movsbl(result, src);
|
|
604 |
break;
|
|
605 |
case WORD:
|
|
606 |
masm.movswl(result, src);
|
|
607 |
break;
|
|
608 |
case DWORD:
|
|
609 |
masm.movl(result, src);
|
|
610 |
break;
|
|
611 |
case QWORD:
|
|
612 |
masm.movq(result, src);
|
|
613 |
break;
|
|
614 |
case SINGLE:
|
|
615 |
masm.movflt(result, src);
|
|
616 |
break;
|
|
617 |
case DOUBLE:
|
|
618 |
masm.movdbl(result, src);
|
|
619 |
break;
|
|
620 |
default:
|
|
621 |
throw GraalError.shouldNotReachHere();
|
|
622 |
}
|
|
623 |
}
|
|
624 |
|
|
625 |
public static void const2reg(CompilationResultBuilder crb, AMD64MacroAssembler masm, Register result, JavaConstant input) {
|
|
626 |
/*
|
|
627 |
* Note: we use the kind of the input operand (and not the kind of the result operand)
|
|
628 |
* because they don't match in all cases. For example, an object constant can be loaded to a
|
|
629 |
* long register when unsafe casts occurred (e.g., for a write barrier where arithmetic
|
|
630 |
* operations are then performed on the pointer).
|
|
631 |
*/
|
|
632 |
switch (input.getJavaKind().getStackKind()) {
|
|
633 |
case Int:
|
|
634 |
// Do not optimize with an XOR as this instruction may be between
|
|
635 |
// a CMP and a Jcc in which case the XOR will modify the condition
|
|
636 |
// flags and interfere with the Jcc.
|
|
637 |
masm.movl(result, input.asInt());
|
|
638 |
|
|
639 |
break;
|
|
640 |
case Long:
|
|
641 |
// Do not optimize with an XOR as this instruction may be between
|
|
642 |
// a CMP and a Jcc in which case the XOR will modify the condition
|
|
643 |
// flags and interfere with the Jcc.
|
|
644 |
if (input.asLong() == (int) input.asLong()) {
|
|
645 |
// Sign extended to long
|
|
646 |
masm.movslq(result, (int) input.asLong());
|
|
647 |
} else if ((input.asLong() & 0xFFFFFFFFL) == input.asLong()) {
|
|
648 |
// Zero extended to long
|
|
649 |
masm.movl(result, (int) input.asLong());
|
|
650 |
} else {
|
|
651 |
masm.movq(result, input.asLong());
|
|
652 |
}
|
|
653 |
break;
|
|
654 |
case Float:
|
|
655 |
// This is *not* the same as 'constant == 0.0f' in the case where constant is -0.0f
|
|
656 |
if (Float.floatToRawIntBits(input.asFloat()) == Float.floatToRawIntBits(0.0f)) {
|
|
657 |
masm.xorps(result, result);
|
|
658 |
} else {
|
|
659 |
masm.movflt(result, (AMD64Address) crb.asFloatConstRef(input));
|
|
660 |
}
|
|
661 |
break;
|
|
662 |
case Double:
|
|
663 |
// This is *not* the same as 'constant == 0.0d' in the case where constant is -0.0d
|
|
664 |
if (Double.doubleToRawLongBits(input.asDouble()) == Double.doubleToRawLongBits(0.0d)) {
|
|
665 |
masm.xorpd(result, result);
|
|
666 |
} else {
|
|
667 |
masm.movdbl(result, (AMD64Address) crb.asDoubleConstRef(input));
|
|
668 |
}
|
|
669 |
break;
|
|
670 |
case Object:
|
|
671 |
// Do not optimize with an XOR as this instruction may be between
|
|
672 |
// a CMP and a Jcc in which case the XOR will modify the condition
|
|
673 |
// flags and interfere with the Jcc.
|
|
674 |
if (input.isNull()) {
|
|
675 |
masm.movq(result, 0x0L);
|
|
676 |
} else if (crb.target.inlineObjects) {
|
|
677 |
crb.recordInlineDataInCode(input);
|
|
678 |
masm.movq(result, 0xDEADDEADDEADDEADL);
|
|
679 |
} else {
|
|
680 |
masm.movq(result, (AMD64Address) crb.recordDataReferenceInCode(input, 0));
|
|
681 |
}
|
|
682 |
break;
|
|
683 |
default:
|
|
684 |
throw GraalError.shouldNotReachHere();
|
|
685 |
}
|
|
686 |
}
|
|
687 |
|
46680
|
688 |
public static boolean canMoveConst2Stack(JavaConstant input) {
|
|
689 |
switch (input.getJavaKind().getStackKind()) {
|
|
690 |
case Int:
|
|
691 |
break;
|
|
692 |
case Long:
|
|
693 |
break;
|
|
694 |
case Float:
|
|
695 |
break;
|
|
696 |
case Double:
|
|
697 |
break;
|
|
698 |
case Object:
|
|
699 |
if (input.isNull()) {
|
|
700 |
return true;
|
|
701 |
} else {
|
|
702 |
return false;
|
|
703 |
}
|
|
704 |
default:
|
|
705 |
return false;
|
|
706 |
}
|
|
707 |
return true;
|
|
708 |
}
|
|
709 |
|
43972
|
710 |
public static void const2stack(CompilationResultBuilder crb, AMD64MacroAssembler masm, Value result, JavaConstant input) {
|
|
711 |
AMD64Address dest = (AMD64Address) crb.asAddress(result);
|
|
712 |
final long imm;
|
|
713 |
switch (input.getJavaKind().getStackKind()) {
|
|
714 |
case Int:
|
|
715 |
imm = input.asInt();
|
|
716 |
break;
|
|
717 |
case Long:
|
|
718 |
imm = input.asLong();
|
|
719 |
break;
|
|
720 |
case Float:
|
|
721 |
imm = floatToRawIntBits(input.asFloat());
|
|
722 |
break;
|
|
723 |
case Double:
|
|
724 |
imm = doubleToRawLongBits(input.asDouble());
|
|
725 |
break;
|
|
726 |
case Object:
|
|
727 |
if (input.isNull()) {
|
|
728 |
imm = 0;
|
|
729 |
} else {
|
|
730 |
throw GraalError.shouldNotReachHere("Non-null object constants must be in register");
|
|
731 |
}
|
|
732 |
break;
|
|
733 |
default:
|
|
734 |
throw GraalError.shouldNotReachHere();
|
|
735 |
}
|
|
736 |
|
|
737 |
switch ((AMD64Kind) result.getPlatformKind()) {
|
|
738 |
case BYTE:
|
|
739 |
assert NumUtil.isByte(imm) : "Is not in byte range: " + imm;
|
|
740 |
AMD64MIOp.MOVB.emit(masm, OperandSize.BYTE, dest, (int) imm);
|
|
741 |
break;
|
|
742 |
case WORD:
|
|
743 |
assert NumUtil.isShort(imm) : "Is not in short range: " + imm;
|
|
744 |
AMD64MIOp.MOV.emit(masm, OperandSize.WORD, dest, (int) imm);
|
|
745 |
break;
|
|
746 |
case DWORD:
|
|
747 |
case SINGLE:
|
|
748 |
assert NumUtil.isInt(imm) : "Is not in int range: " + imm;
|
|
749 |
masm.movl(dest, (int) imm);
|
|
750 |
break;
|
|
751 |
case QWORD:
|
|
752 |
case DOUBLE:
|
|
753 |
masm.movlong(dest, imm);
|
|
754 |
break;
|
|
755 |
default:
|
|
756 |
throw GraalError.shouldNotReachHere("Unknown result Kind: " + result.getPlatformKind());
|
|
757 |
}
|
|
758 |
}
|
47798
|
759 |
|
48861
|
760 |
public abstract static class PointerCompressionOp extends AMD64LIRInstruction {
|
47798
|
761 |
protected final LIRKindTool lirKindTool;
|
|
762 |
protected final CompressEncoding encoding;
|
|
763 |
protected final boolean nonNull;
|
|
764 |
|
|
765 |
@Def({REG, HINT}) private AllocatableValue result;
|
48861
|
766 |
@Use({REG, CONST}) private Value input;
|
49451
|
767 |
@Alive({REG, ILLEGAL, UNINITIALIZED}) private AllocatableValue baseRegister;
|
47798
|
768 |
|
48861
|
769 |
protected PointerCompressionOp(LIRInstructionClass<? extends PointerCompressionOp> type, AllocatableValue result, Value input,
|
|
770 |
AllocatableValue baseRegister, CompressEncoding encoding, boolean nonNull, LIRKindTool lirKindTool) {
|
|
771 |
|
47798
|
772 |
super(type);
|
|
773 |
this.result = result;
|
|
774 |
this.input = input;
|
|
775 |
this.baseRegister = baseRegister;
|
|
776 |
this.encoding = encoding;
|
|
777 |
this.nonNull = nonNull;
|
|
778 |
this.lirKindTool = lirKindTool;
|
|
779 |
}
|
|
780 |
|
49451
|
781 |
public static boolean hasBase(OptionValues options, CompressEncoding encoding) {
|
|
782 |
return GeneratePIC.getValue(options) || encoding.hasBase();
|
47798
|
783 |
}
|
|
784 |
|
48861
|
785 |
public final Value getInput() {
|
|
786 |
return input;
|
|
787 |
}
|
|
788 |
|
|
789 |
public final AllocatableValue getResult() {
|
|
790 |
return result;
|
47798
|
791 |
}
|
|
792 |
|
|
793 |
protected final Register getBaseRegister() {
|
|
794 |
return asRegister(baseRegister);
|
|
795 |
}
|
|
796 |
|
|
797 |
protected final int getShift() {
|
|
798 |
return encoding.getShift();
|
|
799 |
}
|
|
800 |
|
|
801 |
protected final void move(LIRKind kind, CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
802 |
AMD64Move.move((AMD64Kind) kind.getPlatformKind(), crb, masm, result, input);
|
|
803 |
}
|
|
804 |
}
|
|
805 |
|
48861
|
806 |
public static class CompressPointerOp extends PointerCompressionOp {
|
|
807 |
public static final LIRInstructionClass<CompressPointerOp> TYPE = LIRInstructionClass.create(CompressPointerOp.class);
|
47798
|
808 |
|
48861
|
809 |
public CompressPointerOp(AllocatableValue result, Value input, AllocatableValue baseRegister, CompressEncoding encoding, boolean nonNull, LIRKindTool lirKindTool) {
|
|
810 |
this(TYPE, result, input, baseRegister, encoding, nonNull, lirKindTool);
|
|
811 |
}
|
|
812 |
|
|
813 |
protected CompressPointerOp(LIRInstructionClass<? extends PointerCompressionOp> type, AllocatableValue result, Value input,
|
|
814 |
AllocatableValue baseRegister, CompressEncoding encoding, boolean nonNull, LIRKindTool lirKindTool) {
|
|
815 |
|
|
816 |
super(type, result, input, baseRegister, encoding, nonNull, lirKindTool);
|
47798
|
817 |
}
|
|
818 |
|
|
819 |
@Override
|
|
820 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
821 |
move(lirKindTool.getObjectKind(), crb, masm);
|
|
822 |
|
48861
|
823 |
Register resReg = asRegister(getResult());
|
49451
|
824 |
if (hasBase(crb.getOptions(), encoding)) {
|
47798
|
825 |
Register baseReg = getBaseRegister();
|
|
826 |
if (!nonNull) {
|
|
827 |
masm.testq(resReg, resReg);
|
|
828 |
masm.cmovq(Equal, resReg, baseReg);
|
|
829 |
}
|
|
830 |
masm.subq(resReg, baseReg);
|
|
831 |
}
|
|
832 |
|
|
833 |
int shift = getShift();
|
|
834 |
if (shift != 0) {
|
|
835 |
masm.shrq(resReg, shift);
|
|
836 |
}
|
|
837 |
}
|
|
838 |
}
|
|
839 |
|
48861
|
840 |
public static class UncompressPointerOp extends PointerCompressionOp {
|
|
841 |
public static final LIRInstructionClass<UncompressPointerOp> TYPE = LIRInstructionClass.create(UncompressPointerOp.class);
|
47798
|
842 |
|
48861
|
843 |
public UncompressPointerOp(AllocatableValue result, Value input, AllocatableValue baseRegister, CompressEncoding encoding, boolean nonNull, LIRKindTool lirKindTool) {
|
|
844 |
this(TYPE, result, input, baseRegister, encoding, nonNull, lirKindTool);
|
|
845 |
}
|
|
846 |
|
|
847 |
protected UncompressPointerOp(LIRInstructionClass<? extends PointerCompressionOp> type, AllocatableValue result, Value input,
|
|
848 |
AllocatableValue baseRegister, CompressEncoding encoding, boolean nonNull, LIRKindTool lirKindTool) {
|
|
849 |
|
|
850 |
super(type, result, input, baseRegister, encoding, nonNull, lirKindTool);
|
47798
|
851 |
}
|
|
852 |
|
|
853 |
@Override
|
|
854 |
public void emitCode(CompilationResultBuilder crb, AMD64MacroAssembler masm) {
|
|
855 |
move(lirKindTool.getNarrowOopKind(), crb, masm);
|
49451
|
856 |
emitUncompressCode(masm, asRegister(getResult()), getShift(), hasBase(crb.getOptions(), encoding) ? getBaseRegister() : null, nonNull);
|
|
857 |
}
|
47798
|
858 |
|
49451
|
859 |
public static void emitUncompressCode(AMD64MacroAssembler masm, Register resReg, int shift, Register baseReg, boolean nonNull) {
|
47798
|
860 |
if (shift != 0) {
|
|
861 |
masm.shlq(resReg, shift);
|
|
862 |
}
|
|
863 |
|
49451
|
864 |
if (baseReg != null) {
|
47798
|
865 |
if (nonNull) {
|
|
866 |
masm.addq(resReg, baseReg);
|
|
867 |
return;
|
|
868 |
}
|
|
869 |
|
|
870 |
if (shift == 0) {
|
|
871 |
// if encoding.shift != 0, the flags are already set by the shlq
|
|
872 |
masm.testq(resReg, resReg);
|
|
873 |
}
|
|
874 |
|
|
875 |
Label done = new Label();
|
|
876 |
masm.jccb(Equal, done);
|
|
877 |
masm.addq(resReg, baseReg);
|
|
878 |
masm.bind(done);
|
|
879 |
}
|
|
880 |
}
|
|
881 |
}
|
43972
|
882 |
}
|