ForLoopsLowering: Add additional bytecode text tests for optimization of

unsigned progressions.
This commit is contained in:
Mark Punzalan
2020-04-07 22:52:17 -07:00
committed by Alexander Udalov
parent 0e6af517d7
commit f249e7f5e9
22 changed files with 713 additions and 15 deletions
@@ -17,10 +17,10 @@ fun f(a: Int): Int {
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 LINENUMBER 7
// JVM_TEMPLATES
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 0 IF
// 0 LINENUMBER 6
@@ -17,10 +17,10 @@ fun f(a: Long): Int {
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 LINENUMBER 7
// JVM_TEMPLATES
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 0 IF
// 0 LINENUMBER 6
@@ -14,7 +14,7 @@ fun box(): String {
//
// // Standard form of loop over progression
// var inductionVar = 2
// val last = getProgressionLastElement(1, Int.MIN_VALUE - 1, 2) // `(Int.MIN_VALUE - 1)` underflows to 2147483647
// val last = getProgressionLastElement(2, Int.MIN_VALUE - 1, 2) // `(Int.MIN_VALUE - 1)` underflows to Int.MAX_VALUE
// val step = 2
// if (false && inductionVar <= last) { // `false` comes from constant folding of `Int.MIN_VALUE != Int.MIN_VALUE`
// // Loop is not empty
@@ -0,0 +1,38 @@
// 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
// val step = 2
// 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 += step
// // 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,0 +1,19 @@
// WITH_RUNTIME
const val M = UInt.MIN_VALUE
fun f(a: UInt): Int {
var n = 0
for (i in a downTo M) {
n++
}
return n
}
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 2 IF
@@ -0,0 +1,19 @@
// WITH_RUNTIME
const val M = ULong.MIN_VALUE
fun f(a: ULong): Int {
var n = 0
for (i in a downTo M) {
n++
}
return n
}
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/UnsignedKt.ulongCompare
// 2 IF
@@ -51,3 +51,8 @@ fun testULongDownTo(a: ULong, b: ULong): Int {
// 0 iterator
// 0 hasNext
// 0 next
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
@@ -0,0 +1,18 @@
const val M = UInt.MAX_VALUE
fun f(a: UInt): Int {
var n = 0
for (i in a..M) {
n++
}
return n
}
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 2 IF
@@ -0,0 +1,19 @@
// WITH_RUNTIME
const val M = ULong.MAX_VALUE
fun f(a: ULong): Int {
var n = 0
for (i in a..M) {
n++
}
return n
}
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 1 INVOKESTATIC kotlin/UnsignedKt.ulongCompare
// 2 IF
@@ -0,0 +1,31 @@
// WITH_RUNTIME
const val M = UInt.MAX_VALUE
fun f(a: UInt): Int {
var n = 0
for (i in a until M) {
n++
}
return n
}
// JVM non-IR uses while.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// JVM_TEMPLATES
// 1 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGE
// 1 IF
// JVM_IR_TEMPLATES
// 2 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGT
// 1 IFLE
// 2 IF
@@ -0,0 +1,27 @@
// WITH_RUNTIME
const val M = UInt.MIN_VALUE
fun f(a: UInt): Int {
var n = 0
for (i in a until M) {
n++
}
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.
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// JVM_TEMPLATES
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 0 LINENUMBER 7
@@ -0,0 +1,31 @@
// WITH_RUNTIME
const val M = ULong.MAX_VALUE
fun f(a: ULong): Int {
var n = 0
for (i in a until M) {
n++
}
return n
}
// JVM non-IR uses while.
// JVM IR uses if + do-while.
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// JVM_TEMPLATES
// 1 INVOKESTATIC kotlin/UnsignedKt.ulongCompare
// 1 IFGE
// 1 IF
// JVM_IR_TEMPLATES
// 2 INVOKESTATIC kotlin/UnsignedKt.ulongCompare
// 1 IFGT
// 1 IFLE
// 2 IF
@@ -0,0 +1,27 @@
// WITH_RUNTIME
const val M = ULong.MIN_VALUE
fun f(a: ULong): Int {
var n = 0
for (i in a until M) {
n++
}
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.
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// JVM_TEMPLATES
// 1 IF
// JVM_IR_TEMPLATES
// 0 IF
// 0 LINENUMBER 7
@@ -0,0 +1,34 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun testUByteUntilUByte(a: UByte, b: UByte): UInt {
var sum = 0u
for (i in a until b) {
sum = sum * 10u + i
}
return sum
}
fun testUShortUntilUShort(a: UShort, b: UShort): UInt {
var sum = 0u
for (i in a until b) {
sum = sum * 10u + 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 UIntRange:
//
// infix fun UByte.until(to: UByte): UIntRange // NO bound check needed
// infix fun UShort.until(to: UShort): UIntRange // NO bound check needed
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 2 IFGT
// 2 IFLE
// 4 IF
@@ -0,0 +1,41 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun box(): String {
for (i in 1u..6u step 0) {
}
return "OK"
}
// For "step" progressions in JVM IR, if the step is constant and <= 0, the expression for step is replaced with an
// IllegalArgumentException. The backend can then eliminate the entire loop and the rest of the function as dead code.
//
// Expected lowered form of loop (before bytecode optimization):
//
// // Additional variables:
// val newStep = throw IllegalArgumentException("Step must be positive, was: 0.")
//
// // Standard form of loop over progression
// var inductionVar = 1u
// val last = getProgressionLastElement(1u, 6u, newStep)
// val step = newStep
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 1 NEW java/lang/IllegalArgumentException
// 1 ATHROW
// 0 IF
// 0 ARETURN
@@ -0,0 +1,43 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun box(): String {
for (i in (1u..8u).reversed() step 2) {
}
return "OK"
}
// For "step" progressions in JVM IR, a call to getProgressionLastElement() is made to compute the "last" value.
// If the step is non-constant, there is a check that it is > 0, and if not, an IllegalArgumentException is thrown. However, when the
// step is constant and > 0, this check does not need to be added.
//
// Expected lowered form of loop:
//
// // Standard form of loop over progression
// val last = getProgressionLastElement(8u, 1u, -2)
// var inductionVar = 8u
// val step = -2
// if (last <= inductionVar) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (i != last)
// }
//
// We can't use "0 reversed" in the regex below because "reversed" is in the test filename.
// 0 INVOKE.*reversed
// 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
// 1 IFGT
// 1 IF_ICMPNE
// 2 IF
@@ -0,0 +1,53 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun box(): String {
val uintProgression = 1u..7u
for (i in uintProgression 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.
// If the step is non-constant, there is a check that it is > 0, and if not, an IllegalArgumentException is thrown. However, when the
// step is constant and > 0, this check does not need to be added.
//
// Expected lowered form of loop:
//
// // Additional variables:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val newStep = if (nestedStep > 0) 2 else -2
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, newStep)
// val step = newStep
// if ((step > 0 && inductionVar <= last) || (step < 0 && last <= inductionVar)) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 1 getFirst
// 1 getLast
// 1 getStep
// 1 INVOKESTATIC kotlin/internal/UProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 2 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGT
// 1 IF_ICMPNE
// 3 IFLE
// 1 IFGE
// 6 IF
// 0 INEG
@@ -0,0 +1,57 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun one() = 1
fun box(): String {
val uintProgression = 1u..7u
for (i in uintProgression step one()) {
}
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.
// If the step is non-constant, there is a check that it is > 0, and if not, an IllegalArgumentException is thrown.
//
// Expected lowered form of loop:
//
// // Additional variables:
// val progression = intProgression
// val nestedFirst = progression.first
// val nestedLast = progression.last
// val nestedStep = progression.step
// val stepArg = one()
// val checkedStep = if (stepArg > 0) stepArg
// else throw IllegalArgumentException("Step must be positive, was: $stepArg.")
// val newStep = if (nestedStep > 0) checkedStep else -checkedStep
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, newStep)
// val step = newStep
// if ((step > 0 && inductionVar <= last) || (step < 0 && last <= inductionVar)) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 1 getFirst
// 1 getLast
// 1 getStep
// 1 INVOKESTATIC kotlin/internal/UProgressionUtilKt.getProgressionLastElement
// 1 NEW java/lang/IllegalArgumentException
// 1 ATHROW
// 2 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGT
// 1 IF_ICMPNE
// 4 IFLE
// 1 IFGE
// 7 IF
// 1 INEG
@@ -0,0 +1,45 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
fun box(): String {
for (i in 1u..7u step 3 step 2) {
}
return "OK"
}
// For "step" progressions in JVM IR, a call to getProgressionLastElement() is made to compute the "last" value. This is done for each
// nested call to "step".
// If the step is non-constant, there is a check that it is > 0, and if not, an IllegalArgumentException is thrown. However, when the
// step is constant and > 0, this check does not need to be added.
//
// Expected lowered form of loop:
//
// // Additional variables:
// val outerNestedLast = getProgressionLastElement(1u, 7u, 3)
//
// // Standard form of loop over progression
// var inductionVar = 1u
// val last = getProgressionLastElement(1u, outerNestedLast, 2)
// val step = 2
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 2 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,0 +1,45 @@
// 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 variables:
// val untilArg = nine()
// val nestedLast = untilArg - 1u
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1u, nestedLast, 2)
// val step = 2
// if (untilArg != UInt.MIN_VALUE && inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // 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