Skip to content

Commit d1e1894

Browse files
committed
compute trip countd with overflow
1 parent 62a8b33 commit d1e1894

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/nodes/loop/CountedLoopInfo.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,7 +696,7 @@ public boolean ivCanNeverOverflow(InductionVariable iv) {
696696
if (iv.isConstantInit() && isConstantMaxTripCount() && iv.isConstantStride()) {
697697
try {
698698
final int bits = IntegerStamp.getBits(iv.valueNode().stamp(NodeView.DEFAULT));
699-
long tripCountMinus1 = LoopUtility.subtractExact(bits, maxTripCountNode().asJavaConstant().asLong(), 1);
699+
long tripCountMinus1 = LoopUtility.subtractExact(bits, LoopUtility.tripCountSignedExact(this), 1);
700700
long stripTimesTripCount = LoopUtility.multiplyExact(bits, iv.constantStride(), tripCountMinus1);
701701
@SuppressWarnings("unused")
702702
long extremum = LoopUtility.addExact(bits, stripTimesTripCount, iv.initNode().asJavaConstant().asLong());

compiler/src/jdk.graal.compiler/src/jdk/graal/compiler/phases/common/util/LoopUtility.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
import jdk.graal.compiler.nodes.cfg.HIRBlock;
5454
import jdk.graal.compiler.nodes.extended.OpaqueValueNode;
5555
import jdk.graal.compiler.nodes.loop.BasicInductionVariable;
56+
import jdk.graal.compiler.nodes.loop.CountedLoopInfo;
5657
import jdk.graal.compiler.nodes.loop.InductionVariable;
5758
import jdk.graal.compiler.nodes.loop.Loop;
5859
import jdk.graal.compiler.nodes.loop.LoopsData;
@@ -61,6 +62,15 @@
6162

6263
public class LoopUtility {
6364

65+
public static long tripCountSignedExact(CountedLoopInfo loop) {
66+
ValueNode maxTripCountNode = loop.maxTripCountNode();
67+
final long maxTripCountAsSigned = maxTripCountNode.asJavaConstant().asLong();
68+
if (maxTripCountAsSigned < 0) {
69+
throw new ArithmeticException("Unsigned value " + maxTripCountAsSigned + " overflows signed range");
70+
}
71+
return maxTripCountAsSigned;
72+
}
73+
6474
public static long addExact(int bits, long a, long b) {
6575
if (bits == 8) {
6676
byte ba = NumUtil.safeToByteAE(a);

0 commit comments

Comments
 (0)