Skip to content

Commit 9f2f6fb

Browse files
committed
[GR-71326] Allow signed 32-bit ranges in speculative guard movement
PullRequest: graal/22601
2 parents ecacee0 + c52248b commit 9f2f6fb

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

compiler/src/jdk.graal.compiler.test/src/jdk/graal/compiler/core/test/SpeculativeGuardMovementTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 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
@@ -26,6 +26,8 @@
2626

2727
import java.util.List;
2828

29+
import org.junit.Test;
30+
2931
import jdk.graal.compiler.api.directives.GraalDirectives;
3032
import jdk.graal.compiler.core.common.GraalOptions;
3133
import jdk.graal.compiler.core.common.cfg.CFGLoop;
@@ -37,8 +39,6 @@
3739
import jdk.graal.compiler.nodes.cfg.HIRBlock;
3840
import jdk.graal.compiler.nodes.java.InstanceOfNode;
3941
import jdk.graal.compiler.options.OptionValues;
40-
import org.junit.Test;
41-
4242
import jdk.vm.ci.code.InstalledCode;
4343
import jdk.vm.ci.meta.DeoptimizationReason;
4444

@@ -187,7 +187,7 @@ private boolean instanceOfGuardPresent(String toParse) {
187187
return iOf.usages().count() == 1 && iOf.usages().first() instanceof FixedGuardNode;
188188
}
189189

190-
private static int findDeoptLoopDepth(DeoptimizationReason reason, StructuredGraph g) {
190+
public static int findDeoptLoopDepth(DeoptimizationReason reason, StructuredGraph g) {
191191
ControlFlowGraph cfg = ControlFlowGraph.computeForSchedule(g);
192192
assertTrue(cfg.getLoops().size() > 0, "Loop(s) in graph expected!");
193193

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/loop/phases/SpeculativeGuardMovementPhase.java

Lines changed: 16 additions & 4 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
@@ -649,7 +649,8 @@ private OptimizedCompareTests shouldOptimizeCompare(CompareNode compare, Inducti
649649
if (boundStamp instanceof IntegerStamp && ivStamp instanceof IntegerStamp) {
650650
IntegerStamp integerBoundStamp = (IntegerStamp) boundStamp;
651651
IntegerStamp integerIvStamp = (IntegerStamp) ivStamp;
652-
if (fitsIn32Bit(integerBoundStamp) && fitsIn32Bit(integerIvStamp)) {
652+
NumUtil.Signedness signedness = compare.condition().isUnsigned() ? NumUtil.Signedness.UNSIGNED : NumUtil.Signedness.SIGNED;
653+
if (fitsIn32Bit(integerBoundStamp, signedness) && fitsIn32Bit(integerIvStamp, signedness)) {
653654
fitsInInt = true;
654655
}
655656
}
@@ -757,8 +758,19 @@ private static boolean optimizedCompareUnconditionalDeopt(GuardNode guard, Optim
757758
return false;
758759
}
759760

760-
private static boolean fitsIn32Bit(IntegerStamp stamp) {
761-
return NumUtil.isUInt(stamp.mayBeSet());
761+
/**
762+
* Returns {@code true} if the given stamp fits into a 32-bit range. If the stamp is larger
763+
* than 32 bits, the bounds must be signed or unsigned according to {@code signedness}.
764+
*/
765+
private static boolean fitsIn32Bit(IntegerStamp stamp, NumUtil.Signedness signedness) {
766+
if (stamp.getBits() <= 32) {
767+
return true;
768+
}
769+
if (signedness == NumUtil.Signedness.SIGNED) {
770+
return NumUtil.isSignedNbit(32, stamp.lowerBound()) && NumUtil.isSignedNbit(32, stamp.upperBound());
771+
} else {
772+
return NumUtil.isUnsignedNbit(32, stamp.lowerBound()) && NumUtil.isUnsignedNbit(32, stamp.upperBound());
773+
}
762774
}
763775

764776
private CFGLoop<HIRBlock> tryOptimizeInstanceOf(GuardNode guard, InstanceOfNode compare) {

0 commit comments

Comments
 (0)