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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -25,6 +25,6 @@ fun f(a: Long): Int {
// JVM_IR_TEMPLATES
// 2 LCMP
// 1 IFGT
// 1 IFLE
// 1 IFGE
// 1 IFLT
// 2 IF
@@ -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
@@ -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
@@ -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
@@ -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