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

@@ -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