Skip to content

Commit b708a4e

Browse files
committed
Prevent rematerialization of SVM's LoadCompressedObjectConstantOp
1 parent 0465567 commit b708a4e

File tree

11 files changed

+104
-25
lines changed

11 files changed

+104
-25
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/aarch64/AArch64HotSpotMove.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -27,7 +27,6 @@
2727
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.HINT;
2828
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.ILLEGAL;
2929
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.REG;
30-
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.STACK;
3130
import static jdk.vm.ci.aarch64.AArch64.zr;
3231
import static jdk.vm.ci.code.ValueUtil.asRegister;
3332
import static jdk.vm.ci.code.ValueUtil.isRegister;
@@ -51,7 +50,7 @@ public static class LoadHotSpotObjectConstantInline extends AArch64LIRInstructio
5150
public static final LIRInstructionClass<LoadHotSpotObjectConstantInline> TYPE = LIRInstructionClass.create(LoadHotSpotObjectConstantInline.class);
5251

5352
private HotSpotConstant constant;
54-
@Def({REG, STACK}) AllocatableValue result;
53+
@Def({REG}) AllocatableValue result;
5554

5655
public LoadHotSpotObjectConstantInline(HotSpotConstant constant, AllocatableValue result) {
5756
super(TYPE);
@@ -79,6 +78,11 @@ public AllocatableValue getResult() {
7978
public Constant getConstant() {
8079
return constant;
8180
}
81+
82+
@Override
83+
public boolean canRematerializeToStack() {
84+
return false;
85+
}
8286
}
8387

8488
/**

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/hotspot/amd64/AMD64HotSpotMove.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,12 +24,12 @@
2424
*/
2525
package jdk.graal.compiler.hotspot.amd64;
2626

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;
3027
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.HINT;
3128
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.REG;
3229
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.STACK;
30+
import static jdk.vm.ci.code.ValueUtil.asRegister;
31+
import static jdk.vm.ci.code.ValueUtil.isRegister;
32+
import static jdk.vm.ci.code.ValueUtil.isStackSlot;
3333

3434
import jdk.graal.compiler.asm.amd64.AMD64Address;
3535
import jdk.graal.compiler.asm.amd64.AMD64MacroAssembler;
@@ -41,7 +41,6 @@
4141
import jdk.graal.compiler.lir.StandardOp.LoadConstantOp;
4242
import jdk.graal.compiler.lir.amd64.AMD64LIRInstruction;
4343
import jdk.graal.compiler.lir.asm.CompilationResultBuilder;
44-
4544
import jdk.vm.ci.code.Register;
4645
import jdk.vm.ci.hotspot.HotSpotMetaspaceConstant;
4746
import jdk.vm.ci.hotspot.HotSpotObjectConstant;
@@ -104,6 +103,16 @@ public Constant getConstant() {
104103
public AllocatableValue getResult() {
105104
return result;
106105
}
106+
107+
@Override
108+
public boolean canRematerializeToStack() {
109+
/*
110+
* This is slightly too lenient, formally we would also need to check if the target
111+
* allows inlining objects. In practice that is always true, and we do not have access
112+
* to the relevant information here.
113+
*/
114+
return input.isCompressed();
115+
}
107116
}
108117

109118
public static final class BaseMove extends AMD64LIRInstruction {
@@ -167,6 +176,11 @@ public Constant getConstant() {
167176
public AllocatableValue getResult() {
168177
return result;
169178
}
179+
180+
@Override
181+
public boolean canRematerializeToStack() {
182+
return input.isCompressed();
183+
}
170184
}
171185

172186
/**

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/lir/StandardOp.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -289,6 +289,14 @@ static boolean isLoadConstantOp(LIRInstruction op) {
289289
return op.isLoadConstantOp();
290290
}
291291

292+
/**
293+
* Determine whether this constant load is safe to rematerialize directly to a stack slot.
294+
* Some constant load operations have special semantics or implementations that only allow
295+
* rematerialization to a register. Such operations must implement this method to return
296+
* {@code false}. Simple constant loads that can be implemented using a move-immediate
297+
* instruction to a stack slot, or similar, should return {@code true}.
298+
*/
299+
boolean canRematerializeToStack();
292300
}
293301

294302
/**

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/lir/aarch64/AArch64Move.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -108,6 +108,11 @@ public Constant getConstant() {
108108
public AllocatableValue getResult() {
109109
return result;
110110
}
111+
112+
@Override
113+
public boolean canRematerializeToStack() {
114+
return true;
115+
}
111116
}
112117

113118
@Opcode("MOVE")

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/lir/alloc/lsra/LinearScanLifetimeAnalysisPhase.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -918,6 +918,15 @@ protected Constant getMaterializedValue(LIRInstruction op, Value operand, Interv
918918
StandardOp.LoadConstantOp move = StandardOp.LoadConstantOp.asLoadConstantOp(op);
919919

920920
if (!allocator.neverSpillConstants()) {
921+
if (!move.canRematerializeToStack()) {
922+
/*
923+
* This allocator cannot ensure that a rematerialized value is always assigned a
924+
* register; sometimes it rematerializes directly to a stack slot. Therefore, we
925+
* must not try to rematerialize constant loads that do not support
926+
* rematerialization to the stack.
927+
*/
928+
return null;
929+
}
921930
/*
922931
* Check if the interval has any uses which would accept an stack location (priority
923932
* == ShouldHaveRegister). Rematerialization of such intervals can result in a

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/lir/amd64/AMD64Move.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
import jdk.vm.ci.meta.AllocatableValue;
6666
import jdk.vm.ci.meta.Constant;
6767
import jdk.vm.ci.meta.JavaConstant;
68+
import jdk.vm.ci.meta.JavaKind;
6869
import jdk.vm.ci.meta.Value;
6970

7071
public class AMD64Move {
@@ -165,6 +166,14 @@ public Constant getConstant() {
165166
public AllocatableValue getResult() {
166167
return result;
167168
}
169+
170+
@Override
171+
public boolean canRematerializeToStack() {
172+
if (input.getJavaKind() == JavaKind.Object) {
173+
return input.isNull();
174+
}
175+
return true;
176+
}
168177
}
169178

170179
@Opcode("STACKMOVE")

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/lir/amd64/vector/AMD64VectorMove.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2013, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -180,13 +180,18 @@ public Constant getConstant() {
180180
public AllocatableValue getResult() {
181181
return result;
182182
}
183+
184+
@Override
185+
public boolean canRematerializeToStack() {
186+
return true;
187+
}
183188
}
184189

185190
@Opcode("VMOVE")
186191
public static class MoveFromArrayConstOp extends AMD64LIRInstruction implements StandardOp.LoadConstantOp {
187192
public static final LIRInstructionClass<MoveFromArrayConstOp> TYPE = LIRInstructionClass.create(MoveFromArrayConstOp.class);
188193

189-
@Def({OperandFlag.REG, OperandFlag.STACK}) protected AllocatableValue result;
194+
@Def({OperandFlag.REG}) protected AllocatableValue result;
190195
private final DataPointerConstant input;
191196
private final AMD64SIMDInstructionEncoding encoding;
192197

@@ -214,6 +219,11 @@ public Constant getConstant() {
214219
public AllocatableValue getResult() {
215220
return result;
216221
}
222+
223+
@Override
224+
public boolean canRematerializeToStack() {
225+
return false;
226+
}
217227
}
218228

219229
@Opcode("VSTACKMOVE")

substratevm/src/com.oracle.svm.core.graal.aarch64/src/com/oracle/svm/core/graal/aarch64/AArch64LoadMethodPointerConstantOp.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022, 2022, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2022, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,18 +24,17 @@
2424
*/
2525
package com.oracle.svm.core.graal.aarch64;
2626

27-
import static jdk.vm.ci.code.ValueUtil.asRegister;
2827
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.HINT;
2928
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.REG;
29+
import static jdk.vm.ci.code.ValueUtil.asRegister;
30+
31+
import com.oracle.svm.core.meta.SubstrateMethodPointerConstant;
3032

3133
import jdk.graal.compiler.asm.aarch64.AArch64MacroAssembler;
3234
import jdk.graal.compiler.lir.LIRInstructionClass;
3335
import jdk.graal.compiler.lir.StandardOp;
3436
import jdk.graal.compiler.lir.aarch64.AArch64LIRInstruction;
3537
import jdk.graal.compiler.lir.asm.CompilationResultBuilder;
36-
37-
import com.oracle.svm.core.meta.SubstrateMethodPointerConstant;
38-
3938
import jdk.vm.ci.code.Register;
4039
import jdk.vm.ci.meta.AllocatableValue;
4140
import jdk.vm.ci.meta.Constant;
@@ -67,4 +66,9 @@ public AllocatableValue getResult() {
6766
public Constant getConstant() {
6867
return constant;
6968
}
69+
70+
@Override
71+
public boolean canRematerializeToStack() {
72+
return false;
73+
}
7074
}

substratevm/src/com.oracle.svm.core.graal.aarch64/src/com/oracle/svm/core/graal/aarch64/SubstrateAArch64Backend.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -1278,6 +1278,12 @@ public void emitCode(CompilationResultBuilder crb, AArch64MacroAssembler masm) {
12781278
masm.add(64, resultReg, baseReg, resultReg, ShiftType.LSL, getShift());
12791279
}
12801280
}
1281+
1282+
@Override
1283+
public boolean canRematerializeToStack() {
1284+
/* This operation MUST have a register as its destination. */
1285+
return false;
1286+
}
12811287
}
12821288

12831289
public FrameMapBuilder newFrameMapBuilder(RegisterConfig registerConfig) {

substratevm/src/com.oracle.svm.core.graal.amd64/src/com/oracle/svm/core/graal/amd64/AMD64LoadMethodPointerConstantOp.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -24,18 +24,17 @@
2424
*/
2525
package com.oracle.svm.core.graal.amd64;
2626

27-
import static jdk.vm.ci.code.ValueUtil.asRegister;
2827
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.HINT;
2928
import static jdk.graal.compiler.lir.LIRInstruction.OperandFlag.REG;
29+
import static jdk.vm.ci.code.ValueUtil.asRegister;
30+
31+
import com.oracle.svm.core.meta.SubstrateMethodPointerConstant;
3032

3133
import jdk.graal.compiler.asm.amd64.AMD64MacroAssembler;
3234
import jdk.graal.compiler.lir.LIRInstructionClass;
3335
import jdk.graal.compiler.lir.StandardOp;
3436
import jdk.graal.compiler.lir.amd64.AMD64LIRInstruction;
3537
import jdk.graal.compiler.lir.asm.CompilationResultBuilder;
36-
37-
import com.oracle.svm.core.meta.SubstrateMethodPointerConstant;
38-
3938
import jdk.vm.ci.code.Register;
4039
import jdk.vm.ci.meta.AllocatableValue;
4140

@@ -66,4 +65,9 @@ public AllocatableValue getResult() {
6665
public SubstrateMethodPointerConstant getConstant() {
6766
return constant;
6867
}
68+
69+
@Override
70+
public boolean canRematerializeToStack() {
71+
return false;
72+
}
6973
}

0 commit comments

Comments
 (0)