ForLoopsLowering: Use last-exclusive for-loops for optimized until
progressions instead of decrementing "last". #KT-41352 Fixed
This commit is contained in:
committed by
Alexander Udalov
parent
1adb130509
commit
ccbf7cc2ee
Generated
-35
@@ -2579,21 +2579,6 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
|
|||||||
public void testForInUntilLongMinValue() throws Exception {
|
public void testForInUntilLongMinValue() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMinValue.kt");
|
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")
|
@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);
|
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")
|
@TestMetadata("illegalStepConst.kt")
|
||||||
public void testIllegalStepConst() throws Exception {
|
public void testIllegalStepConst() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/illegalStepConst.kt");
|
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/illegalStepConst.kt");
|
||||||
@@ -2682,11 +2662,6 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
|
|||||||
public void testStepThenStepOne() throws Exception {
|
public void testStepThenStepOne() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/stepThenStepOne.kt");
|
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")
|
@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);
|
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")
|
@TestMetadata("forInDownToUIntMinValue.kt")
|
||||||
public void testForInDownToUIntMinValue() throws Exception {
|
public void testForInDownToUIntMinValue() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt");
|
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt");
|
||||||
@@ -2780,11 +2750,6 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
|
|||||||
public void testStepThenDifferentStep() throws Exception {
|
public void testStepThenDifferentStep() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/stepThenDifferentStep.kt");
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+14
-5
@@ -124,6 +124,7 @@ private class Transformer(
|
|||||||
|
|
||||||
val lower: IrExpression
|
val lower: IrExpression
|
||||||
val upper: IrExpression
|
val upper: IrExpression
|
||||||
|
val isUpperInclusive: Boolean
|
||||||
val shouldUpperComeFirst: Boolean
|
val shouldUpperComeFirst: Boolean
|
||||||
val useCompareTo: Boolean
|
val useCompareTo: Boolean
|
||||||
val isNumericRange: 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.
|
// `compareTo` must be used for UInt/ULong; they don't have intrinsic comparison operators.
|
||||||
useCompareTo = headerInfo.progressionType is UnsignedProgressionType
|
useCompareTo = headerInfo.progressionType is UnsignedProgressionType
|
||||||
|
isUpperInclusive = headerInfo.isLastInclusive
|
||||||
isNumericRange = true
|
isNumericRange = true
|
||||||
additionalNotEmptyCondition = headerInfo.additionalNotEmptyCondition
|
additionalNotEmptyCondition = headerInfo.additionalNotEmptyCondition
|
||||||
}
|
}
|
||||||
is FloatingPointRangeHeaderInfo -> {
|
is FloatingPointRangeHeaderInfo -> {
|
||||||
lower = headerInfo.start
|
lower = headerInfo.start
|
||||||
upper = headerInfo.endInclusive
|
upper = headerInfo.endInclusive
|
||||||
|
isUpperInclusive = true
|
||||||
shouldUpperComeFirst = false
|
shouldUpperComeFirst = false
|
||||||
useCompareTo = false
|
useCompareTo = false
|
||||||
isNumericRange = true
|
isNumericRange = true
|
||||||
@@ -192,6 +195,7 @@ private class Transformer(
|
|||||||
is ComparableRangeInfo -> {
|
is ComparableRangeInfo -> {
|
||||||
lower = headerInfo.start
|
lower = headerInfo.start
|
||||||
upper = headerInfo.endInclusive
|
upper = headerInfo.endInclusive
|
||||||
|
isUpperInclusive = true
|
||||||
shouldUpperComeFirst = false
|
shouldUpperComeFirst = false
|
||||||
useCompareTo = true
|
useCompareTo = true
|
||||||
isNumericRange = false
|
isNumericRange = false
|
||||||
@@ -289,7 +293,12 @@ private class Transformer(
|
|||||||
upperExpression = upperExpression.castIfNecessary(comparisonClass)
|
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 {
|
val compareToFun = comparisonClass.functions.singleOrNull {
|
||||||
it.name == OperatorNameConventions.COMPARE_TO &&
|
it.name == OperatorNameConventions.COMPARE_TO &&
|
||||||
it.dispatchReceiverParameter != null && it.extensionReceiverParameter == null &&
|
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
|
// for compareTo() may have side effects dependent on which expressions are the receiver and argument
|
||||||
// (see evaluationOrderForComparableRange.kt test).
|
// (see evaluationOrderForComparableRange.kt test).
|
||||||
val lowerClause = if (useCompareTo) {
|
val lowerClause = if (useCompareTo) {
|
||||||
irCall(lessOrEqualFun).apply {
|
irCall(lowerCompFun).apply {
|
||||||
putValueArgument(0, irInt(0))
|
putValueArgument(0, irInt(0))
|
||||||
putValueArgument(1, irCall(compareToFun).apply {
|
putValueArgument(1, irCall(compareToFun).apply {
|
||||||
dispatchReceiver = argExpression
|
dispatchReceiver = argExpression
|
||||||
@@ -309,13 +318,13 @@ private class Transformer(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
irCall(lessOrEqualFun).apply {
|
irCall(lowerCompFun).apply {
|
||||||
putValueArgument(0, lowerExpression)
|
putValueArgument(0, lowerExpression)
|
||||||
putValueArgument(1, argExpression)
|
putValueArgument(1, argExpression)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
val upperClause = if (useCompareTo) {
|
val upperClause = if (useCompareTo) {
|
||||||
irCall(lessOrEqualFun).apply {
|
irCall(upperCompFun).apply {
|
||||||
putValueArgument(0, irCall(compareToFun).apply {
|
putValueArgument(0, irCall(compareToFun).apply {
|
||||||
dispatchReceiver = argExpression.deepCopyWithSymbols()
|
dispatchReceiver = argExpression.deepCopyWithSymbols()
|
||||||
putValueArgument(0, upperExpression)
|
putValueArgument(0, upperExpression)
|
||||||
@@ -323,7 +332,7 @@ private class Transformer(
|
|||||||
putValueArgument(1, irInt(0))
|
putValueArgument(1, irInt(0))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
irCall(lessOrEqualFun).apply {
|
irCall(upperCompFun).apply {
|
||||||
putValueArgument(0, argExpression.deepCopyWithSymbols())
|
putValueArgument(0, argExpression.deepCopyWithSymbols())
|
||||||
putValueArgument(1, upperExpression)
|
putValueArgument(1, upperExpression)
|
||||||
}
|
}
|
||||||
|
|||||||
+8
@@ -46,6 +46,14 @@ internal class StepHandler(
|
|||||||
val nestedInfo = expression.extensionReceiver!!.accept(visitor, null) as? ProgressionHeaderInfo
|
val nestedInfo = expression.extensionReceiver!!.accept(visitor, null) as? ProgressionHeaderInfo
|
||||||
?: return null
|
?: 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)!!
|
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:
|
// We can return the nested info if its step is constant and its absolute value is the same as the step argument. Examples:
|
||||||
//
|
//
|
||||||
|
|||||||
+10
-142
@@ -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.loops.*
|
||||||
import org.jetbrains.kotlin.backend.common.lower.matchers.SimpleCalleeMatcher
|
import org.jetbrains.kotlin.backend.common.lower.matchers.SimpleCalleeMatcher
|
||||||
import org.jetbrains.kotlin.backend.common.lower.matchers.singleArgumentExtension
|
import org.jetbrains.kotlin.backend.common.lower.matchers.singleArgumentExtension
|
||||||
import org.jetbrains.kotlin.ir.builders.*
|
import org.jetbrains.kotlin.ir.builders.irInt
|
||||||
import org.jetbrains.kotlin.ir.declarations.IrVariable
|
|
||||||
import org.jetbrains.kotlin.ir.expressions.IrCall
|
import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||||
import org.jetbrains.kotlin.ir.symbols.IrSymbol
|
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
|
import org.jetbrains.kotlin.name.FqName
|
||||||
|
|
||||||
/** Builds a [HeaderInfo] for progressions built using the `until` extension function. */
|
/** 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 symbols = context.ir.symbols
|
||||||
private val progressionElementTypes = symbols.progressionElementTypes
|
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 {
|
override val matcher = SimpleCalleeMatcher {
|
||||||
singleArgumentExtension(FqName("kotlin.ranges.until"), progressionElementTypes)
|
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? =
|
override fun build(expression: IrCall, data: ProgressionType, scopeOwner: IrSymbol): HeaderInfo? =
|
||||||
with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) {
|
with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) {
|
||||||
with(data) {
|
ProgressionHeaderInfo(
|
||||||
// `A until B` is essentially the same as `A .. (B-1)`. However, B could be MIN_VALUE and hence `(B-1)` could underflow.
|
data,
|
||||||
// If B is MIN_VALUE, then `A until B` is an empty range. We handle this special case be adding an additional "not empty"
|
first = expression.extensionReceiver!!,
|
||||||
// condition in the lowered for-loop. Therefore the following for-loop:
|
last = expression.getValueArgument(0)!!,
|
||||||
//
|
step = irInt(1),
|
||||||
// for (i in A until B) { // Loop body }
|
canOverflow = false,
|
||||||
//
|
isLastInclusive = false,
|
||||||
// is lowered into:
|
direction = ProgressionDirection.INCREASING
|
||||||
//
|
)
|
||||||
// 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
|
|
||||||
)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
+9
-10
@@ -19,26 +19,25 @@ fun box(): String {
|
|||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
|
|
||||||
// JVM non-IR uses while.
|
// JVM non-IR optimizes out all reversed() calls. However, this has a bug (KT-42533).
|
||||||
// JVM IR uses if + do-while. The surrounding "if" gets optimized in this test (constant condition), except for Long.
|
// JVM IR optimizes out the 2nd reversed() call.
|
||||||
|
|
||||||
// 0 reversed
|
|
||||||
// 0 iterator
|
// 0 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
// 0 getEnd
|
// 0 getEnd
|
||||||
|
|
||||||
|
// JVM_TEMPLATES
|
||||||
|
// 0 reversed
|
||||||
// 0 getFirst
|
// 0 getFirst
|
||||||
// 0 getLast
|
// 0 getLast
|
||||||
// 0 getStep
|
// 0 getStep
|
||||||
|
|
||||||
// JVM_TEMPLATES
|
|
||||||
// 2 IF_ICMPGE
|
// 2 IF_ICMPGE
|
||||||
// 1 IFGE
|
// 1 IFGE
|
||||||
// 3 IF
|
// 3 IF
|
||||||
// 1 LCMP
|
// 1 LCMP
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 2 IF_ICMPLE
|
// 3 reversed
|
||||||
// 1 IFGT
|
// 3 getFirst
|
||||||
// 1 IFLE
|
// 3 getLast
|
||||||
// 4 IF
|
// 3 getStep
|
||||||
// 2 LCMP
|
|
||||||
+9
-10
@@ -19,26 +19,25 @@ fun box(): String {
|
|||||||
return "OK"
|
return "OK"
|
||||||
}
|
}
|
||||||
|
|
||||||
// JVM non-IR uses while.
|
// JVM non-IR optimizes out all reversed() calls. However, this has a bug (KT-42533).
|
||||||
// JVM IR uses if + do-while. The surrounding "if" gets optimized in this test (constant condition), except for Long.
|
// JVM IR optimizes out the 2nd reversed() call.
|
||||||
|
|
||||||
// 0 reversed
|
|
||||||
// 0 iterator
|
// 0 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
// 0 getEnd
|
// 0 getEnd
|
||||||
|
|
||||||
|
// JVM_TEMPLATES
|
||||||
|
// 0 reversed
|
||||||
// 0 getFirst
|
// 0 getFirst
|
||||||
// 0 getLast
|
// 0 getLast
|
||||||
// 0 getStep
|
// 0 getStep
|
||||||
|
|
||||||
// JVM_TEMPLATES
|
|
||||||
// 2 IF_ICMPLT
|
// 2 IF_ICMPLT
|
||||||
// 1 IFLT
|
// 1 IFLT
|
||||||
// 3 IF
|
// 3 IF
|
||||||
// 1 LCMP
|
// 1 LCMP
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 2 IF_ICMPLE
|
// 3 reversed
|
||||||
// 1 IFGT
|
// 3 getFirst
|
||||||
// 1 IFLE
|
// 3 getLast
|
||||||
// 4 IF
|
// 3 getStep
|
||||||
// 2 LCMP
|
|
||||||
+4
-5
@@ -7,7 +7,7 @@ fun test(a: Char, b: Char): String {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JVM non-IR uses while.
|
// 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 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
@@ -21,7 +21,6 @@ fun test(a: Char, b: Char): String {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 1 IFEQ
|
// 1 IF_ICMPGE
|
||||||
// 1 IF_ICMPGT
|
// 1 IF_ICMPLT
|
||||||
// 1 IF_ICMPLE
|
// 2 IF
|
||||||
// 3 IF
|
|
||||||
+2
-2
@@ -23,6 +23,6 @@ fun f(a: Char): Int {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 1 IF_ICMPGT
|
// 1 IF_ICMPGE
|
||||||
// 1 IF_ICMPLE
|
// 1 IF_ICMPLT
|
||||||
// 2 IF
|
// 2 IF
|
||||||
+3
-4
@@ -8,8 +8,8 @@ fun f(a: Char): Int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
|
// JVM non-IR uses while.
|
||||||
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
|
// JVM IR uses if + do-while.
|
||||||
|
|
||||||
// 0 iterator
|
// 0 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
@@ -17,10 +17,9 @@ fun f(a: Char): Int {
|
|||||||
// 0 getFirst
|
// 0 getFirst
|
||||||
// 0 getLast
|
// 0 getLast
|
||||||
// 0 getStep
|
// 0 getStep
|
||||||
// 0 LINENUMBER 7
|
|
||||||
|
|
||||||
// JVM_TEMPLATES
|
// JVM_TEMPLATES
|
||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 0 IF
|
// 2 IF
|
||||||
@@ -7,7 +7,7 @@ fun test(a: Int, b: Int): Int {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JVM non-IR uses while.
|
// 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 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
@@ -21,8 +21,6 @@ fun test(a: Int, b: Int): Int {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 1 LDC -2147483648
|
// 1 IF_ICMPGE
|
||||||
// 1 IF_ICMPEQ
|
// 1 IF_ICMPLT
|
||||||
// 1 IF_ICMPGT
|
// 2 IF
|
||||||
// 1 IF_ICMPLE
|
|
||||||
// 3 IF
|
|
||||||
+2
-2
@@ -23,6 +23,6 @@ fun f(a: Int): Int {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 1 IF_ICMPGT
|
// 1 IF_ICMPGE
|
||||||
// 1 IF_ICMPLE
|
// 1 IF_ICMPLT
|
||||||
// 2 IF
|
// 2 IF
|
||||||
+3
-4
@@ -8,8 +8,8 @@ fun f(a: Int): Int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
|
// JVM non-IR uses while.
|
||||||
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
|
// JVM IR uses if + do-while.
|
||||||
|
|
||||||
// 0 iterator
|
// 0 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
@@ -22,5 +22,4 @@ fun f(a: Int): Int {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 0 IF
|
// 2 IF
|
||||||
// 0 LINENUMBER 6
|
|
||||||
|
|||||||
+5
-7
@@ -7,7 +7,7 @@ fun test(a: Long, b: Long): Long {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JVM non-IR uses while.
|
// 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 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
@@ -22,9 +22,7 @@ fun test(a: Long, b: Long): Long {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 1 LDC -9223372036854775808
|
// 2 LCMP
|
||||||
// 3 LCMP
|
// 1 IFGE
|
||||||
// 1 IFEQ
|
// 1 IFLT
|
||||||
// 1 IFGT
|
// 2 IF
|
||||||
// 1 IFLE
|
|
||||||
// 3 IF
|
|
||||||
+2
-2
@@ -25,6 +25,6 @@ fun f(a: Long): Int {
|
|||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 2 LCMP
|
// 2 LCMP
|
||||||
// 1 IFGT
|
// 1 IFGE
|
||||||
// 1 IFLE
|
// 1 IFLT
|
||||||
// 2 IF
|
// 2 IF
|
||||||
+3
-4
@@ -8,8 +8,8 @@ fun f(a: Long): Int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
|
// JVM non-IR uses while.
|
||||||
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
|
// JVM IR uses if + do-while.
|
||||||
|
|
||||||
// 0 iterator
|
// 0 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
@@ -22,5 +22,4 @@ fun f(a: Long): Int {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 0 IF
|
// 2 IF
|
||||||
// 0 LINENUMBER 6
|
|
||||||
|
|||||||
-41
@@ -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
|
|
||||||
-72
@@ -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
|
|
||||||
-44
@@ -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
|
|
||||||
-36
@@ -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
|
|
||||||
-43
@@ -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
|
|
||||||
-39
@@ -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
|
|
||||||
+2
-2
@@ -28,6 +28,6 @@ fun f(a: UInt): Int {
|
|||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 2 INVOKESTATIC kotlin/UnsignedKt.uintCompare
|
// 2 INVOKESTATIC kotlin/UnsignedKt.uintCompare
|
||||||
// 1 IFGT
|
// 1 IFGE
|
||||||
// 1 IFLE
|
// 1 IFLT
|
||||||
// 2 IF
|
// 2 IF
|
||||||
+3
-4
@@ -9,8 +9,8 @@ fun f(a: UInt): Int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
|
// JVM non-IR uses while.
|
||||||
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
|
// JVM IR uses if + do-while.
|
||||||
|
|
||||||
// 0 iterator
|
// 0 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
@@ -25,5 +25,4 @@ fun f(a: UInt): Int {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 0 IF
|
// 2 IF
|
||||||
// 0 LINENUMBER 7
|
|
||||||
|
|||||||
+2
-2
@@ -28,6 +28,6 @@ fun f(a: ULong): Int {
|
|||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 2 INVOKESTATIC kotlin/UnsignedKt.ulongCompare
|
// 2 INVOKESTATIC kotlin/UnsignedKt.ulongCompare
|
||||||
// 1 IFGT
|
// 1 IFGE
|
||||||
// 1 IFLE
|
// 1 IFLT
|
||||||
// 2 IF
|
// 2 IF
|
||||||
+3
-4
@@ -9,8 +9,8 @@ fun f(a: ULong): Int {
|
|||||||
return n
|
return n
|
||||||
}
|
}
|
||||||
|
|
||||||
// For "until" progressions in JVM IR, there is a check that the range is not empty: upper bound != MIN_VALUE.
|
// JVM non-IR uses while.
|
||||||
// When the upper bound == const MIN_VALUE, the backend can eliminate the entire loop as dead code.
|
// JVM IR uses if + do-while.
|
||||||
|
|
||||||
// 0 iterator
|
// 0 iterator
|
||||||
// 0 getStart
|
// 0 getStart
|
||||||
@@ -25,5 +25,4 @@ fun f(a: ULong): Int {
|
|||||||
// 1 IF
|
// 1 IF
|
||||||
|
|
||||||
// JVM_IR_TEMPLATES
|
// JVM_IR_TEMPLATES
|
||||||
// 0 IF
|
// 2 IF
|
||||||
// 0 LINENUMBER 7
|
|
||||||
|
|||||||
-46
@@ -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
|
|
||||||
-35
@@ -2579,21 +2579,6 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
|
|||||||
public void testForInUntilLongMinValue() throws Exception {
|
public void testForInUntilLongMinValue() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMinValue.kt");
|
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")
|
@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);
|
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")
|
@TestMetadata("illegalStepConst.kt")
|
||||||
public void testIllegalStepConst() throws Exception {
|
public void testIllegalStepConst() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/illegalStepConst.kt");
|
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/illegalStepConst.kt");
|
||||||
@@ -2682,11 +2662,6 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
|
|||||||
public void testStepThenStepOne() throws Exception {
|
public void testStepThenStepOne() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/stepped/stepThenStepOne.kt");
|
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")
|
@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);
|
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")
|
@TestMetadata("forInDownToUIntMinValue.kt")
|
||||||
public void testForInDownToUIntMinValue() throws Exception {
|
public void testForInDownToUIntMinValue() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt");
|
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt");
|
||||||
@@ -2780,11 +2750,6 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
|
|||||||
public void testStepThenDifferentStep() throws Exception {
|
public void testStepThenDifferentStep() throws Exception {
|
||||||
runTest("compiler/testData/codegen/bytecodeText/forLoop/unsigned/stepThenDifferentStep.kt");
|
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");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Generated
+10
@@ -16913,6 +16913,16 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
|
|||||||
runTest("compiler/testData/codegen/box/primitiveTypes/equalityWithObject/generated/primitiveEqObjectChar.kt");
|
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")
|
@TestMetadata("primitiveEqObjectShort.kt")
|
||||||
public void testPrimitiveEqObjectShort() throws Exception {
|
public void testPrimitiveEqObjectShort() throws Exception {
|
||||||
runTest("compiler/testData/codegen/box/primitiveTypes/equalityWithObject/generated/primitiveEqObjectShort.kt");
|
runTest("compiler/testData/codegen/box/primitiveTypes/equalityWithObject/generated/primitiveEqObjectShort.kt");
|
||||||
|
|||||||
Reference in New Issue
Block a user