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,3 +1,12 @@
// 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 f() {
for (i in 0..5 step 2) {
}
@@ -11,36 +20,6 @@ fun f() {
// JVM non-IR does NOT specifically handle "step" progressions. The stepped progressions in the above code are constructed and their
// first/last/step properties are retrieved.
// JVM IR has an optimized handler for "step" progressions and elides the construction of the stepped progressions.
//
// Expected lowered form of `for (i in 0..5 step 2)`:
//
// // Standard form of loop over progression
// var inductionVar = 0
// val last = getProgressionLastElement(0, 5, 2)
// val step = 2
// if (inductionVar <= last) {
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (i != last)
// }
//
// Expected lowered form of `for (i in 5 downTo 1 step 1)`:
//
// // Standard form of loop over progression
// var inductionVar = 5
// val last = 1
// val step = -1
// if (last <= inductionVar) { // Optimized out in bytecode
// // Loop is not empty
// do {
// val i = inductionVar
// inductionVar += step
// // Loop body
// } while (last <= inductionVar)
// }
// 0 iterator
@@ -56,7 +35,12 @@ fun f() {
// 0 getLast
// 0 getStep
// 1 IF_ICMPGT
// 1 IF_ICMPNE
// 1 IF_ICMPEQ
// 1 IF_ICMPLE
// 3 IF
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement \(III\)I
// 1 INVOKESTATIC kotlin/internal/ProgressionUtilKt.getProgressionLastElement \(III\)I
// 6 ILOAD
// 4 ISTORE
// 2 IINC
// 0 IADD
// 0 ISUB