JVM_IR generate range-based loop closer to Java counter loop

KT-48435 KT-48507
This commit is contained in:
Dmitry Petrov
2021-09-01 15:31:19 +03:00
parent 9ed08438d5
commit b669de1663
137 changed files with 1518 additions and 573 deletions
@@ -1,5 +1,15 @@
// TARGET_BACKEND: JVM_IR
// WITH_RUNTIME
// IMPORTANT!
// Please, when your changes cause failures in bytecodeText tests for 'for' loops,
// examine the resulting bytecode shape carefully.
// Range and progression-based loops generated with Kotlin compiler should be
// as close as possible to Java counter loops ('for (int i = a; i < b; ++i) { ... }').
// Otherwise it may result in performance regression due to missing HotSpot optimizations.
// Run Kotlin compiler benchmarks (https://github.com/Kotlin/kotlin-benchmarks)
// with compiler built from your changes if you are not sure.
fun box(): String {
val uintRange = 1u..7u
for (i in uintRange step 2) {
@@ -13,23 +23,6 @@ fun box(): String {
// However, if the progression is of type *Range (e.g., IntRange) instead of *Progression (e.g., IntProgression), this
// check is not needed since *Range always has step == 1.
//
// Expected lowered form of loop:
//
// // Additional statements:
// val nestedFirst = uintRange.first
// val nestedLast = uintRange.last
//
// // Standard form of loop over progression
// var inductionVar = nestedFirst
// val last = getProgressionLastElement(nestedFirst, nestedLast, 2)
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += 2
// // Loop body
// } while (i != last)
// }
// 0 iterator
// 0 getStart
@@ -42,8 +35,13 @@ fun box(): String {
// 0 ATHROW
// 1 INVOKESTATIC kotlin/UnsignedKt.uintCompare
// 1 IFGT
// 1 IF_ICMPNE
// 1 IF_ICMPEQ
// 2 IF
// 0 INEG
// 0 INVOKESTATIC kotlin/UInt.constructor-impl
// 0 INVOKE\w+ kotlin/UInt.(un)?box-impl
// 12 ILOAD
// 9 ISTORE
// 0 IADD
// 0 ISUB
// 1 IINC