ForLoopsLowering: Use last-exclusive for-loops for optimized until

progressions instead of decrementing "last".

#KT-41352 Fixed
This commit is contained in:
Mark Punzalan
2020-10-08 06:20:25 +00:00
committed by Alexander Udalov
parent 1adb130509
commit ccbf7cc2ee
28 changed files with 98 additions and 606 deletions

View File

@@ -2579,21 +2579,6 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
public void testForInUntilLongMinValue() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMinValue.kt");
}
@TestMetadata("forInUntilWithMixedTypeBoundsBoundCheckNeededForIntRangeIR.kt")
public void testForInUntilWithMixedTypeBoundsBoundCheckNeededForIntRangeIR() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilWithMixedTypeBoundsBoundCheckNeededForIntRangeIR.kt");
}
@TestMetadata("forInUntilWithMixedTypeBoundsNoBoundCheckNeededForIntRangeIR.kt")
public void testForInUntilWithMixedTypeBoundsNoBoundCheckNeededForIntRangeIR() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilWithMixedTypeBoundsNoBoundCheckNeededForIntRangeIR.kt");
}
@TestMetadata("forInUntilWithMixedTypeBoundsNoBoundCheckNeededForLongRangeIR.kt")
public void testForInUntilWithMixedTypeBoundsNoBoundCheckNeededForLongRangeIR() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilWithMixedTypeBoundsNoBoundCheckNeededForLongRangeIR.kt");
}
}
@TestMetadata("compiler/testData/codegen/bytecodeText/forLoop/stepped")
@@ -2608,11 +2593,6 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeText/forLoop/stepped"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("emptyUntilProgressionToMinValue.kt")
public void testEmptyUntilProgressionToMinValue() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/emptyUntilProgressionToMinValue.kt");
}
@TestMetadata("illegalStepConst.kt")
public void testIllegalStepConst() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/illegalStepConst.kt");
@@ -2682,11 +2662,6 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
public void testStepThenStepOne() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/stepThenStepOne.kt");
}
@TestMetadata("untilProgressionToNonConst.kt")
public void testUntilProgressionToNonConst() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/untilProgressionToNonConst.kt");
}
}
@TestMetadata("compiler/testData/codegen/bytecodeText/forLoop/unsigned")
@@ -2701,11 +2676,6 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeText/forLoop/unsigned"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("emptyUntilProgressionToMinValue.kt")
public void testEmptyUntilProgressionToMinValue() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/emptyUntilProgressionToMinValue.kt");
}
@TestMetadata("forInDownToUIntMinValue.kt")
public void testForInDownToUIntMinValue() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt");
@@ -2780,11 +2750,6 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
public void testStepThenDifferentStep() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/stepThenDifferentStep.kt");
}
@TestMetadata("untilProgressionToNonConst.kt")
public void testUntilProgressionToNonConst() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/untilProgressionToNonConst.kt");
}
}
}

View File

@@ -124,6 +124,7 @@ private class Transformer(
val lower: IrExpression
val upper: IrExpression
val isUpperInclusive: Boolean
val shouldUpperComeFirst: Boolean
val useCompareTo: Boolean
val isNumericRange: Boolean
@@ -178,12 +179,14 @@ private class Transformer(
// `compareTo` must be used for UInt/ULong; they don't have intrinsic comparison operators.
useCompareTo = headerInfo.progressionType is UnsignedProgressionType
isUpperInclusive = headerInfo.isLastInclusive
isNumericRange = true
additionalNotEmptyCondition = headerInfo.additionalNotEmptyCondition
}
is FloatingPointRangeHeaderInfo -> {
lower = headerInfo.start
upper = headerInfo.endInclusive
isUpperInclusive = true
shouldUpperComeFirst = false
useCompareTo = false
isNumericRange = true
@@ -192,6 +195,7 @@ private class Transformer(
is ComparableRangeInfo -> {
lower = headerInfo.start
upper = headerInfo.endInclusive
isUpperInclusive = true
shouldUpperComeFirst = false
useCompareTo = true
isNumericRange = false
@@ -289,7 +293,12 @@ private class Transformer(
upperExpression = upperExpression.castIfNecessary(comparisonClass)
}
val lessOrEqualFun = builtIns.lessOrEqualFunByOperandType.getValue(if (useCompareTo) builtIns.intClass else comparisonClass.symbol)
val lowerCompFun = builtIns.lessOrEqualFunByOperandType.getValue(if (useCompareTo) builtIns.intClass else comparisonClass.symbol)
val upperCompFun = if (isUpperInclusive) {
builtIns.lessOrEqualFunByOperandType
} else {
builtIns.lessFunByOperandType
}.getValue(if (useCompareTo) builtIns.intClass else comparisonClass.symbol)
val compareToFun = comparisonClass.functions.singleOrNull {
it.name == OperatorNameConventions.COMPARE_TO &&
it.dispatchReceiverParameter != null && it.extensionReceiverParameter == null &&
@@ -301,7 +310,7 @@ private class Transformer(
// for compareTo() may have side effects dependent on which expressions are the receiver and argument
// (see evaluationOrderForComparableRange.kt test).
val lowerClause = if (useCompareTo) {
irCall(lessOrEqualFun).apply {
irCall(lowerCompFun).apply {
putValueArgument(0, irInt(0))
putValueArgument(1, irCall(compareToFun).apply {
dispatchReceiver = argExpression
@@ -309,13 +318,13 @@ private class Transformer(
})
}
} else {
irCall(lessOrEqualFun).apply {
irCall(lowerCompFun).apply {
putValueArgument(0, lowerExpression)
putValueArgument(1, argExpression)
}
}
val upperClause = if (useCompareTo) {
irCall(lessOrEqualFun).apply {
irCall(upperCompFun).apply {
putValueArgument(0, irCall(compareToFun).apply {
dispatchReceiver = argExpression.deepCopyWithSymbols()
putValueArgument(0, upperExpression)
@@ -323,7 +332,7 @@ private class Transformer(
putValueArgument(1, irInt(0))
}
} else {
irCall(lessOrEqualFun).apply {
irCall(upperCompFun).apply {
putValueArgument(0, argExpression.deepCopyWithSymbols())
putValueArgument(1, upperExpression)
}

View File

@@ -46,6 +46,14 @@ internal class StepHandler(
val nestedInfo = expression.extensionReceiver!!.accept(visitor, null) as? ProgressionHeaderInfo
?: return null
if (!nestedInfo.isLastInclusive) {
// To compute the new "last" value for a stepped progression (see call to getProgressionLastElement() below) where the
// underlying progression is last-exclusive, we must decrement the nested "last" by the step. However, this can cause
// underflow if "last" is MIN_VALUE. We will not support fully optimizing this scenario (e.g., `for (i in A until B step C`)
// for now. It will be partly optimized via DefaultProgressionHandler.
return null
}
val stepArg = expression.getValueArgument(0)!!
// We can return the nested info if its step is constant and its absolute value is the same as the step argument. Examples:
//

View File

@@ -10,13 +10,9 @@ import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
import org.jetbrains.kotlin.backend.common.lower.loops.*
import org.jetbrains.kotlin.backend.common.lower.matchers.SimpleCalleeMatcher
import org.jetbrains.kotlin.backend.common.lower.matchers.singleArgumentExtension
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.declarations.IrVariable
import org.jetbrains.kotlin.ir.builders.irInt
import org.jetbrains.kotlin.ir.expressions.IrCall
import org.jetbrains.kotlin.ir.symbols.IrSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.defaultType
import org.jetbrains.kotlin.ir.util.deepCopyWithSymbols
import org.jetbrains.kotlin.name.FqName
/** Builds a [HeaderInfo] for progressions built using the `until` extension function. */
@@ -25,10 +21,6 @@ internal class UntilHandler(private val context: CommonBackendContext) :
private val symbols = context.ir.symbols
private val progressionElementTypes = symbols.progressionElementTypes
private val uByteType = symbols.uByte?.defaultType
private val uShortType = symbols.uShort?.defaultType
private val uIntType = symbols.uInt?.defaultType
private val uLongType = symbols.uLong?.defaultType
override val matcher = SimpleCalleeMatcher {
singleArgumentExtension(FqName("kotlin.ranges.until"), progressionElementTypes)
@@ -38,138 +30,14 @@ internal class UntilHandler(private val context: CommonBackendContext) :
override fun build(expression: IrCall, data: ProgressionType, scopeOwner: IrSymbol): HeaderInfo? =
with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) {
with(data) {
// `A until B` is essentially the same as `A .. (B-1)`. However, B could be MIN_VALUE and hence `(B-1)` could underflow.
// If B is MIN_VALUE, then `A until B` is an empty range. We handle this special case be adding an additional "not empty"
// condition in the lowered for-loop. Therefore the following for-loop:
//
// for (i in A until B) { // Loop body }
//
// is lowered into:
//
// var inductionVar = A
// val last = B - 1
// if (inductionVar <= last && B != MIN_VALUE) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar++
// // Loop body
// } while (inductionVar <= last)
// }
//
// However, `B` may be an expression with side-effects that should only be evaluated once, and `A` may also have
// side-effects. They are evaluated once and in the correct order (`A` then `B`), the final lowered form is:
//
// // Additional variables
// val untilReceiverValue = A
// val untilArg = B
// // Standard form of loop over progression
// var inductionVar = untilReceiverValue
// val last = untilArg - 1
// if (inductionVar <= last && untilArg != MIN_VALUE) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar++
// // Loop body
// } while (inductionVar <= last)
// }
val receiverValue = expression.extensionReceiver!!
val untilArg = expression.getValueArgument(0)!!
// Ensure that the argument conforms to the progression type before we decrement.
val untilArgCasted = untilArg.asElementType()
// To reduce local variable usage, we create and use temporary variables only if necessary.
var receiverValueVar: IrVariable? = null
var untilArgVar: IrVariable? = null
var additionalVariables = emptyList<IrVariable>()
if (untilArg.canHaveSideEffects) {
if (receiverValue.canHaveSideEffects) {
receiverValueVar = scope.createTmpVariable(receiverValue, nameHint = "untilReceiverValue")
}
untilArgVar = scope.createTmpVariable(untilArgCasted, nameHint = "untilArg")
additionalVariables = listOfNotNull(receiverValueVar, untilArgVar)
}
val first = if (receiverValueVar == null) receiverValue else irGet(receiverValueVar)
val untilArgExpression = if (untilArgVar == null) untilArgCasted else irGet(untilArgVar)
val last = untilArgExpression.decrement()
// Type of MIN_VALUE constant is signed even for unsigned progressions since the bounds are signed.
val additionalNotEmptyCondition = untilArg.constLongValue.let {
when {
it == null && isAdditionalNotEmptyConditionNeeded(receiverValue.type, untilArg.type) ->
// Condition is needed and untilArg is non-const.
// Build the additional "not empty" condition: `untilArg != MIN_VALUE`.
// Make sure to copy untilArgExpression as it is also used in `last`.
irNotEquals(untilArgExpression.deepCopyWithSymbols(), minValueExpression())
it == data.minValueAsLong ->
// Hardcode "false" as additional condition so that the progression is considered empty.
// The entire lowered loop becomes a candidate for dead code elimination, depending on backend.
irFalse()
else ->
// We know that untilArg != MIN_VALUE, so the additional condition is not necessary.
null
}
}
ProgressionHeaderInfo(
data,
first = first,
last = last,
step = irInt(1),
canOverflow = false,
additionalStatements = additionalVariables,
additionalNotEmptyCondition = additionalNotEmptyCondition,
direction = ProgressionDirection.INCREASING
)
}
ProgressionHeaderInfo(
data,
first = expression.extensionReceiver!!,
last = expression.getValueArgument(0)!!,
step = irInt(1),
canOverflow = false,
isLastInclusive = false,
direction = ProgressionDirection.INCREASING
)
}
private fun isAdditionalNotEmptyConditionNeeded(receiverType: IrType, argType: IrType): Boolean {
// Here are the available `until` extension functions:
//
// infix fun Char.until(to: Char): CharRange
// infix fun Byte.until(to: Byte): IntRange
// infix fun Byte.until(to: Short): IntRange
// infix fun Byte.until(to: Int): IntRange
// infix fun Byte.until(to: Long): LongRange
// infix fun Short.until(to: Byte): IntRange
// infix fun Short.until(to: Short): IntRange
// infix fun Short.until(to: Int): IntRange
// infix fun Short.until(to: Long): LongRange
// infix fun Int.until(to: Byte): IntRange
// infix fun Int.until(to: Short): IntRange
// infix fun Int.until(to: Int): IntRange
// infix fun Int.until(to: Long): LongRange
// infix fun Long.until(to: Byte): LongRange
// infix fun Long.until(to: Short): LongRange
// infix fun Long.until(to: Int): LongRange
// infix fun Long.until(to: Long): LongRange
// infix fun UByte.until(to: UByte): UIntRange
// infix fun UShort.until(to: UShort): UIntRange
// infix fun UInt.until(to: UInt): UIntRange
// infix fun ULong.until(to: ULong): ULongRange
//
// The additional condition is only needed when the argument casted to the range element type and then decremented can produce
// negative overflow - in other words, if the minimum value of `to` is the same as the minimum value for the progression type.
return with(context.irBuiltIns) {
when (receiverType) {
charType -> true
byteType, shortType, intType -> when (argType) {
byteType, shortType -> false
else -> true
}
longType -> when (argType) {
byteType, shortType, intType -> false
else -> true
}
// 1. All unsigned types regardless of size "bottom out" at 0, so they all need the check.
// 2. In case new types are added to stdlib, conservatively add a check as well.
else -> true
}
}
}
}

View File

@@ -19,26 +19,25 @@ fun box(): String {
return "OK"
}
// JVM non-IR uses while.
// JVM IR uses if + do-while. The surrounding "if" gets optimized in this test (constant condition), except for Long.
// JVM non-IR optimizes out all reversed() calls. However, this has a bug (KT-42533).
// JVM IR optimizes out the 2nd reversed() call.
// 0 reversed
// 0 iterator
// 0 getStart
// 0 getEnd
// JVM_TEMPLATES
// 0 reversed
// 0 getFirst
// 0 getLast
// 0 getStep
// JVM_TEMPLATES
// 2 IF_ICMPGE
// 1 IFGE
// 3 IF
// 1 LCMP
// JVM_IR_TEMPLATES
// 2 IF_ICMPLE
// 1 IFGT
// 1 IFLE
// 4 IF
// 2 LCMP
// 3 reversed
// 3 getFirst
// 3 getLast
// 3 getStep

View File

@@ -19,26 +19,25 @@ fun box(): String {
return "OK"
}
// JVM non-IR uses while.
// JVM IR uses if + do-while. The surrounding "if" gets optimized in this test (constant condition), except for Long.
// JVM non-IR optimizes out all reversed() calls. However, this has a bug (KT-42533).
// JVM IR optimizes out the 2nd reversed() call.
// 0 reversed
// 0 iterator
// 0 getStart
// 0 getEnd
// JVM_TEMPLATES
// 0 reversed
// 0 getFirst
// 0 getLast
// 0 getStep
// JVM_TEMPLATES
// 2 IF_ICMPLT
// 1 IFLT
// 3 IF
// 1 LCMP
// JVM_IR_TEMPLATES
// 2 IF_ICMPLE
// 1 IFGT
// 1 IFLE
// 4 IF
// 2 LCMP
// 3 reversed
// 3 getFirst
// 3 getLast
// 3 getStep

View File

@@ -7,7 +7,7 @@ fun test(a: Char, b: Char): String {
}
// JVM non-IR uses while.
// JVM IR uses if + do-while. In addition, for "until" progressions, there is a check that the range is not empty: upper bound != MIN_VALUE.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
@@ -21,7 +21,6 @@ fun test(a: Char, b: Char): String {
// 1 IF
// JVM_IR_TEMPLATES
// 1 IFEQ
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 3 IF
// 1 IF_ICMPGE
// 1 IF_ICMPLT
// 2 IF

View File

@@ -23,6 +23,6 @@ fun f(a: Char): Int {
// 1 IF
// JVM_IR_TEMPLATES
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 1 IF_ICMPGE
// 1 IF_ICMPLT
// 2 IF

View File

@@ -8,8 +8,8 @@ fun f(a: Char): Int {
return n
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
// JVM non-IR uses while.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
@@ -17,10 +17,9 @@ fun f(a: Char): Int {
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 LINENUMBER 7
// JVM_TEMPLATES
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 2 IF

View File

@@ -7,7 +7,7 @@ fun test(a: Int, b: Int): Int {
}
// JVM non-IR uses while.
// JVM IR uses if + do-while. In addition, for "until" progressions, there is a check that the range is not empty: upper bound != MIN_VALUE.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
@@ -21,8 +21,6 @@ fun test(a: Int, b: Int): Int {
// 1 IF
// JVM_IR_TEMPLATES
// 1 LDC -2147483648
// 1 IF_ICMPEQ
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 3 IF
// 1 IF_ICMPGE
// 1 IF_ICMPLT
// 2 IF

View File

@@ -23,6 +23,6 @@ fun f(a: Int): Int {
// 1 IF
// JVM_IR_TEMPLATES
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 1 IF_ICMPGE
// 1 IF_ICMPLT
// 2 IF

View File

@@ -8,8 +8,8 @@ fun f(a: Int): Int {
return n
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
// JVM non-IR uses while.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
@@ -22,5 +22,4 @@ fun f(a: Int): Int {
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 0 LINENUMBER 6
// 2 IF

View File

@@ -7,7 +7,7 @@ fun test(a: Long, b: Long): Long {
}
// JVM non-IR uses while.
// JVM IR uses if + do-while. In addition, for "until" progressions, there is a check that the range is not empty: upper bound != MIN_VALUE.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
@@ -22,9 +22,7 @@ fun test(a: Long, b: Long): Long {
// 1 IF
// JVM_IR_TEMPLATES
// 1 LDC -9223372036854775808
// 3 LCMP
// 1 IFEQ
// 1 IFGT
// 1 IFLE
// 3 IF
// 2 LCMP
// 1 IFGE
// 1 IFLT
// 2 IF

View File

@@ -25,6 +25,6 @@ fun f(a: Long): Int {
// JVM_IR_TEMPLATES
// 2 LCMP
// 1 IFGT
// 1 IFLE
// 1 IFGE
// 1 IFLT
// 2 IF

View File

@@ -8,8 +8,8 @@ fun f(a: Long): Int {
return n
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
// JVM non-IR uses while.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
@@ -22,5 +22,4 @@ fun f(a: Long): Int {
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 0 LINENUMBER 6
// 2 IF

View File

@@ -1,41 +0,0 @@
// TARGET_BACKEND: JVM_IR
fun testByteUntilInt(a: Byte, b: Int): Int {
var sum = 0
for (i in a until b) {
sum = sum * 10 + i
}
return sum
}
fun testShortUntilInt(a: Short, b: Int): Int {
var sum = 0
for (i in a until b) {
sum = sum * 10 + i
}
return sum
}
// For "until" progressions in JVM IR, there is typically a check that the range is not empty: upper bound != MIN_VALUE.
// However, this check is not needed when the upper bound is smaller than the range element type.
// Here are the available `until` extension functions with mixed bounds that return IntRange:
//
// infix fun Byte.until(to: Byte): IntRange
// infix fun Byte.until(to: Short): IntRange
// infix fun Byte.until(to: Int): IntRange // Bound check needed
// infix fun Short.until(to: Byte): IntRange
// infix fun Short.until(to: Short): IntRange
// infix fun Short.until(to: Int): IntRange // Bound check needed
// infix fun Int.until(to: Byte): IntRange
// infix fun Int.until(to: Short): IntRange
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 2 LDC -2147483648
// 2 IF_ICMPEQ
// 2 IF_ICMPGT
// 2 IF_ICMPLE
// 6 IF

View File

@@ -1,72 +0,0 @@
// TARGET_BACKEND: JVM_IR
fun testByteUntilByte(a: Byte, b: Byte): Int {
var sum = 0
for (i in a until b) {
sum = sum * 10 + i
}
return sum
}
fun testByteUntilShort(a: Byte, b: Short): Int {
var sum = 0
for (i in a until b) {
sum = sum * 10 + i
}
return sum
}
fun testShortUntilByte(a: Short, b: Byte): Int {
var sum = 0
for (i in a until b) {
sum = sum * 10 + i
}
return sum
}
fun testShortUntilShort(a: Short, b: Short): Int {
var sum = 0
for (i in a until b) {
sum = sum * 10 + i
}
return sum
}
fun testIntUntilByte(a: Int, b: Byte): Int {
var sum = 0
for (i in a until b) {
sum = sum * 10 + i
}
return sum
}
fun testIntUntilShort(a: Int, b: Short): Int {
var sum = 0
for (i in a until b) {
sum = sum * 10 + i
}
return sum
}
// For "until" progressions in JVM IR, there is typically a check that the range is not empty: upper bound != MIN_VALUE.
// However, this check is not needed when the upper bound is smaller than the range element type.
// Here are the available `until` extension functions with mixed bounds that return IntRange:
//
// infix fun Byte.until(to: Byte): IntRange // NO bound check needed
// infix fun Byte.until(to: Short): IntRange // NO bound check needed
// infix fun Byte.until(to: Int): IntRange
// infix fun Short.until(to: Byte): IntRange // NO bound check needed
// infix fun Short.until(to: Short): IntRange // NO bound check needed
// infix fun Short.until(to: Int): IntRange
// infix fun Int.until(to: Byte): IntRange // NO bound check needed
// infix fun Int.until(to: Short): IntRange // NO bound check needed
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 LDC -2147483648
// 6 IF_ICMPGT
// 6 IF_ICMPLE
// 12 IF

View File

@@ -1,44 +0,0 @@
// TARGET_BACKEND: JVM_IR
fun testLongUntilByte(a: Long, b: Byte): Long {
var sum = 0L
for (i in a until b) {
sum = sum * 10L + i
}
return sum
}
fun testLongUntilShort(a: Long, b: Short): Long {
var sum = 0L
for (i in a until b) {
sum = sum * 10L + i
}
return sum
}
fun testLongUntilInt(a: Long, b: Int): Long {
var sum = 0L
for (i in a until b) {
sum = sum * 10L + i
}
return sum
}
// For "until" progressions in JVM IR, there is typically a check that the range is not empty: upper bound != MIN_VALUE.
// However, this check is not needed when the upper bound is smaller than the range element type.
// Here are the available `until` extension functions with mixed bounds that return LongRange:
//
// infix fun Long.until(to: Byte): LongRange // NO bound check needed
// infix fun Long.until(to: Short): LongRange // NO bound check needed
// infix fun Long.until(to: Int): LongRange // NO bound check needed
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 LDC -9223372036854775808
// 6 LCMP
// 3 IFGT
// 3 IFLE
// 6 IF

View File

@@ -1,36 +0,0 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in 2 until Int.MIN_VALUE step 2) {
}
return "OK"
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
// Calls to getProgressionLastElement() are still present because these happen before the eliminated loop.
//
// Expected lowered form of loop (before bytecode optimizations):
//
// // Standard form of loop over progression
// var inductionVar = 2
// val last = getProgressionLastElement(2, Int.MIN_VALUE - 1, 2) // `(Int.MIN_VALUE - 1)` underflows to Int.MAX_VALUE
// if (false && inductionVar <= last) { // `false` comes from constant folding of `Int.MIN_VALUE != Int.MIN_VALUE`
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 0 IF

View File

@@ -1,43 +0,0 @@
// TARGET_BACKEND: JVM_IR
fun nine() = 9
fun box(): String {
for (i in 1 until nine() step 2) {
}
return "OK"
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
//
// Expected lowered form of loop (before bytecode optimizations):
//
// // Additional statements:
// val untilArg = nine()
// val nestedLast = untilArg - 1
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, nestedLast, 2)
// if (untilArg != Int.MIN_VALUE && inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 1 LDC -2147483648
// 1 IF_ICMPEQ
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 3 IF

View File

@@ -1,39 +0,0 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun box(): String {
for (i in 2u until UInt.MIN_VALUE step 2) {
}
return "OK"
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
// Calls to getProgressionLastElement() are still present because these happen before the eliminated loop.
//
// Expected lowered form of loop (before bytecode optimizations):
//
// // Standard form of loop over progression
// var inductionVar = 2u
// val last = getProgressionLastElement(2u, UInt.MIN_VALUE - 1, 2) // `(UInt.MIN_VALUE - 1)` underflows to UInt.MAX_VALUE
// if (false && inductionVar <= last) { // `false` comes from constant folding of `Int.MIN_VALUE != Int.MIN_VALUE`
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/internal/UProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 0 IF
// 0 INVOKESTATIC kotlin/UInt.constructor-impl
// 0 INVOKE\w+ kotlin/UInt.(un)?box-impl

View File

@@ -28,6 +28,6 @@ fun f(a: UInt): Int {
// JVM_IR_TEMPLATES
// 2 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGT
// 1 IFLE
// 1 IFGE
// 1 IFLT
// 2 IF

View File

@@ -9,8 +9,8 @@ fun f(a: UInt): Int {
return n
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
// JVM non-IR uses while.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
@@ -25,5 +25,4 @@ fun f(a: UInt): Int {
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 0 LINENUMBER 7
// 2 IF

View File

@@ -28,6 +28,6 @@ fun f(a: ULong): Int {
// JVM_IR_TEMPLATES
// 2 INVOKESTATIC kotlin/UnsignedKt.ulongCompare
// 1 IFGT
// 1 IFLE
// 1 IFGE
// 1 IFLT
// 2 IF

View File

@@ -9,8 +9,8 @@ fun f(a: ULong): Int {
return n
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
// JVM non-IR uses while.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
@@ -25,5 +25,4 @@ fun f(a: ULong): Int {
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 0 LINENUMBER 7
// 2 IF

View File

@@ -1,46 +0,0 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun nine() = 9u
fun box(): String {
for (i in 1u until nine() step 2) {
}
return "OK"
}
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
//
// Expected lowered form of loop (before bytecode optimizations):
//
// // Additional statements:
// val untilArg = nine()
// val nestedLast = untilArg - 1u
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1u, nestedLast, 2)
// if (untilArg != UInt.MIN_VALUE && inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/internal/UProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 1 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFEQ
// 1 IFGT
// 1 IF_ICMPNE
// 3 IF
// 0 INVOKESTATIC kotlin/UInt.constructor-impl
// 0 INVOKE\w+ kotlin/UInt.(un)?box-impl

View File

@@ -2579,21 +2579,6 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
public void testForInUntilLongMinValue() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMinValue.kt");
}
@TestMetadata("forInUntilWithMixedTypeBoundsBoundCheckNeededForIntRangeIR.kt")
public void testForInUntilWithMixedTypeBoundsBoundCheckNeededForIntRangeIR() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilWithMixedTypeBoundsBoundCheckNeededForIntRangeIR.kt");
}
@TestMetadata("forInUntilWithMixedTypeBoundsNoBoundCheckNeededForIntRangeIR.kt")
public void testForInUntilWithMixedTypeBoundsNoBoundCheckNeededForIntRangeIR() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilWithMixedTypeBoundsNoBoundCheckNeededForIntRangeIR.kt");
}
@TestMetadata("forInUntilWithMixedTypeBoundsNoBoundCheckNeededForLongRangeIR.kt")
public void testForInUntilWithMixedTypeBoundsNoBoundCheckNeededForLongRangeIR() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilWithMixedTypeBoundsNoBoundCheckNeededForLongRangeIR.kt");
}
}
@TestMetadata("compiler/testData/codegen/bytecodeText/forLoop/stepped")
@@ -2608,11 +2593,6 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeText/forLoop/stepped"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("emptyUntilProgressionToMinValue.kt")
public void testEmptyUntilProgressionToMinValue() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/emptyUntilProgressionToMinValue.kt");
}
@TestMetadata("illegalStepConst.kt")
public void testIllegalStepConst() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/illegalStepConst.kt");
@@ -2682,11 +2662,6 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
public void testStepThenStepOne() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/stepThenStepOne.kt");
}
@TestMetadata("untilProgressionToNonConst.kt")
public void testUntilProgressionToNonConst() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/untilProgressionToNonConst.kt");
}
}
@TestMetadata("compiler/testData/codegen/bytecodeText/forLoop/unsigned")
@@ -2701,11 +2676,6 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/codegen/bytecodeText/forLoop/unsigned"), Pattern.compile("^(.+)\\.kt$"), null, TargetBackend.JVM_IR, true);
}
@TestMetadata("emptyUntilProgressionToMinValue.kt")
public void testEmptyUntilProgressionToMinValue() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/emptyUntilProgressionToMinValue.kt");
}
@TestMetadata("forInDownToUIntMinValue.kt")
public void testForInDownToUIntMinValue() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt");
@@ -2780,11 +2750,6 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
public void testStepThenDifferentStep() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/stepThenDifferentStep.kt");
}
@TestMetadata("untilProgressionToNonConst.kt")
public void testUntilProgressionToNonConst() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/untilProgressionToNonConst.kt");
}
}
}

View File

@@ -16913,6 +16913,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/primitiveTypes/equalityWithObject/generated/primitiveEqObjectChar.kt");
}
@TestMetadata("primitiveEqObjectInt.kt")
public void testPrimitiveEqObjectInt() throws Exception {
runTest("compiler/testData/codegen/box/primitiveTypes/equalityWithObject/generated/primitiveEqObjectInt.kt");
}
@TestMetadata("primitiveEqObjectLong.kt")
public void testPrimitiveEqObjectLong() throws Exception {
runTest("compiler/testData/codegen/box/primitiveTypes/equalityWithObject/generated/primitiveEqObjectLong.kt");
}
@TestMetadata("primitiveEqObjectShort.kt")
public void testPrimitiveEqObjectShort() throws Exception {
runTest("compiler/testData/codegen/box/primitiveTypes/equalityWithObject/generated/primitiveEqObjectShort.kt");