Add bytecode text tests for ForLoopsLowering.

This commit is contained in:
Mark Punzalan
2019-06-21 23:06:26 -07:00
committed by max-kammerer
parent 277cb39e3b
commit 9bb9ab67a7
18 changed files with 750 additions and 3 deletions
@@ -0,0 +1,37 @@
// 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(1, Int.MIN_VALUE - 1, 2) // `(Int.MIN_VALUE - 1)` underflows to 2147483647
// 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/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 0 IF
@@ -0,0 +1,40 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in 1..6 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 = 1
// val last = getProgressionLastElement(1, 6, 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,42 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in (1..8).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(8, 1, -2)
// var inductionVar = 8
// 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/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 2 IF
@@ -0,0 +1,39 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in 1..7 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
// var inductionVar = 1
// val last = getProgressionLastElement(1, 7, 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
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 2 IF
@@ -0,0 +1,52 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
val intProgression = 1..7
for (i in intProgression 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/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 1 IF_ICMPNE
// 2 IFLE
// 1 IFGE
// 6 IF
// 0 INEG
@@ -0,0 +1,46 @@
// TARGET_BACKEND: JVM_IR
fun one() = 1
fun box(): String {
for (i in 1..7 step one()) {
}
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.
//
// Expected lowered form of loop:
//
// // Additional variables:
// val stepArg = one()
// val newStep = if (stepArg > 0) stepArg
// else throw IllegalArgumentException("Step must be positive, was: $stepArg.")
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, 7, 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
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 1 NEW java/lang/IllegalArgumentException
// 1 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 1 IFLE
// 3 IF
@@ -0,0 +1,56 @@
// TARGET_BACKEND: JVM_IR
fun one() = 1
fun box(): String {
val intProgression = 1..7
for (i in intProgression 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/ProgressionUtilKt.getProgressionLastElement
// 1 NEW java/lang/IllegalArgumentException
// 1 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 1 IF_ICMPNE
// 3 IFLE
// 1 IFGE
// 7 IF
// 1 INEG
@@ -0,0 +1,37 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in 1..4 step 1) {
}
return "OK"
}
// For "step" progressions in JVM IR, when the step is constant and == 1, and "step" is called on a literal progression which we already
// know to have a step whose absolute value is 1, we can essentially ignore the "step" call.
//
// Expected lowered form of loop:
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = 4
// val step = 1
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (inductionVar <= last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPLE
// 1 IF
@@ -0,0 +1,37 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in 1..4 step 1 step 1) {
}
return "OK"
}
// For "step" progressions in JVM IR, when the step is constant and == 1, and "step" is called on a literal progression which we already
// know to have a step whose absolute value is 1, we can essentially ignore the "step" call.
//
// Expected lowered form of loop:
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = 4
// val step = 1
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (inductionVar <= last)
// }
// 0 iterator
// 0 getStart
// 0 getEnd
// 0 getFirst
// 0 getLast
// 0 getStep
// 0 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPLE
// 1 IF
@@ -0,0 +1,43 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in 1..7 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(1, 7, 3)
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, 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/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 2 IF
@@ -0,0 +1,40 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in (1..8 step 2).reversed()) {
}
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 = 1
// var inductionVar = getProgressionLastElement(1, 8, 2)
// val step = -2
// if (last <= inductionVar) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (last <= inductionVar)
// }
// 0 reversed
// 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
// 1 IF_ICMPGT
// 1 IF_ICMPLE
// 2 IF
@@ -0,0 +1,40 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in 1..8 step 2 step 2) {
}
return "OK"
}
// For "step" progressions in JVM IR, a call to getProgressionLastElement() is made to compute the "last" value. This is normally done for
// each nested call to "step", except when the step value is the same as the preceding step 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
// var inductionVar = 1
// val last = getProgressionLastElement(1, 8, 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
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 2 IF
@@ -0,0 +1,50 @@
// TARGET_BACKEND: JVM_IR
fun one() = 1
fun box(): String {
for (i in 1..6 step one() step one()) {
}
return "OK"
}
// For "step" progressions in JVM IR, 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 innerStepArg = one()
// val innerNewStep = if (innerStepArg > 0) innerStepArg
// else throw IllegalArgumentException("Step must be positive, was: $innerStepArg.")
// val outerNestedLast = getProgressionLastElement(1, 6, innerNewStep)
// val outerStepArg = one()
// val outerNewStep = if (outerStepArg > 0) outerStepArg
// else throw IllegalArgumentException("Step must be positive, was: $outerStepArg.")
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, outerNestedLast, outerNewStep)
// val step = outerNewStep
// 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/ProgressionUtilKt.getProgressionLastElement
// 2 NEW java/lang/IllegalArgumentException
// 2 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 2 IFLE
// 4 IF
@@ -0,0 +1,43 @@
// TARGET_BACKEND: JVM_IR
fun box(): String {
for (i in 1..5 step 2 step 1) {
}
return "OK"
}
// For "step" progressions in JVM IR, a call to getProgressionLastElement() is made to compute the "last" value. This is normally done for
// each nested call to "step", except when the step value is constant and == 1.
// 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 innerNestedLast = getProgressionLastElement(1, 5, 2)
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = innerNestedLast
// val step = 1
// 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
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 0 ATHROW
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 2 IF
@@ -0,0 +1,44 @@
// 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 variables:
// val untilArg = nine()
// val nestedLast = untilArg - 1
//
// // Standard form of loop over progression
// var inductionVar = 1
// val last = getProgressionLastElement(1, nestedLast, 2)
// val step = 2
// if (untilArg != Int.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/ProgressionUtilKt.getProgressionLastElement
// 0 NEW java/lang/IllegalArgumentException
// 1 LDC -2147483648
// 1 IF_ICMPEQ
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 3 IF