ForLoopsLowering: Assume step == 1 for *Range (e.g., IntRange) and

handle accordingly (e.g., do not read `step` property).
This commit is contained in:
Mark Punzalan
2020-07-23 15:21:03 -07:00
committed by Alexander Udalov
parent 09e47fff7b
commit 2cfd776092
10 changed files with 143 additions and 24 deletions

View File

@@ -7,4 +7,5 @@ fun f(r: IntRange) {
// 0 getStart
// 0 getEnd
// 1 getFirst
// 1 getLast
// 1 getLast
// 0 getStep

View File

@@ -1,6 +1,6 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
val intProgression = 1..7
val intProgression = 1..7 step 3 // `step` ensures type is IntProgression, NOT IntRange
for (i in intProgression step 2) {
}
@@ -15,10 +15,9 @@ fun box(): String {
// Expected lowered form of loop:
//
// // Additional statements:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val nestedFirst = intProgression.first
// val nestedLast = intProgression.last
// val nestedStep = intProgression.step
// val maybeNegatedStep = if (nestedStep <= 0) -2 else 2
//
// // Standard form of loop over progression

View File

@@ -2,7 +2,7 @@
fun one() = 1
fun box(): String {
val intProgression = 1..7
val intProgression = 1..7 step 3 // `step` ensures type is IntProgression, NOT IntRange
for (i in intProgression step one()) {
}
@@ -16,10 +16,9 @@ fun box(): String {
// Expected lowered form of loop:
//
// // Additional statements:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val nestedFirst = intProgression.first
// val nestedLast = intProgression.last
// val nestedStep = intProgression.step
// var stepArg = one()
// if (stepArg <= 0) throw IllegalArgumentException("Step must be positive, was: $stepArg.")
// if (nestedStep <= 0) stepArg = -stepArg

View File

@@ -0,0 +1,45 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
val intRange = 1..7
for (i in intRange step 2) {
}
return "OK"
}
// For "step" progressions in JVM IR, a call to getProgressionLastElement() is made to compute the "last" value.
// If "step" is called on a non-literal progression, there is a check to see if that progression's step value is < 0.
// However, if the progression is of type *Range (e.g., IntRange) instead of *Progression (e.g., IntProgression), this
// check is not needed since *Range always has step == 1.
//
// Expected lowered form of loop:
//
// // Additional statements:
// val nestedFirst = intRange.first
// val nestedLast = intRange.last
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, 2)
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 1 getFirst
// 1 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 2 IF
// 0 INEG

View File

@@ -1,7 +1,7 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun box(): String {
val uintProgression = 1u..7u
val uintProgression = 1u..7u step 3 // `step` ensures type is UIntProgression, NOT UIntRange
for (i in uintProgression step 2) {
}
@@ -16,10 +16,9 @@ fun box(): String {
// Expected lowered form of loop:
//
// // Additional statements:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val nestedFirst = uintProgression.first
// val nestedLast = uintProgression.last
// val nestedStep = uintProgression.step
// val maybeNegatedStep = if (nestedStep <= 0) -2 else 2
//
// // Standard form of loop over progression

View File

@@ -3,7 +3,7 @@
fun one() = 1
fun box(): String {
val uintProgression = 1u..7u
val uintProgression = 1u..7u step 3 // `step` ensures type is UIntProgression, NOT UIntRange
for (i in uintProgression step one()) {
}
@@ -17,10 +17,9 @@ fun box(): String {
// Expected lowered form of loop:
//
// // Additional statements:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val nestedFirst = uintProgression.first
// val nestedLast = uintProgression.last
// val nestedStep = uintProgression.step
// var stepArg = one()
// if (stepArg <= 0) throw IllegalArgumentException("Step must be positive, was: $stepArg.")
// if (nestedStep <= 0) stepArg = -stepArg

View File

@@ -0,0 +1,49 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun box(): String {
val uintRange = 1u..7u
for (i in uintRange step 2) {
}
return "OK"
}
// For "step" progressions in JVM IR, a call to getProgressionLastElement() is made to compute the "last" value.
// If "step" is called on a non-literal progression, there is a check to see if that progression's step value is < 0.
// However, if the progression is of type *Range (e.g., IntRange) instead of *Progression (e.g., IntProgression), this
// check is not needed since *Range always has step == 1.
//
// Expected lowered form of loop:
//
// // Additional statements:
// val nestedFirst = uintRange.first
// val nestedLast = uintRange.last
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, 2)
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 1 getFirst
// 1 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/internal/UProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGT
// 1 IF_ICMPNE
// 2 IF
// 0 INEG
// 0 INVOKESTATIC kotlin/UInt.constructor-impl
// 0 INVOKE\w+ kotlin/UInt.(un)?box-impl