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

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