backend: Don't create a Progression objects in 'for' loops

This patch optimizes the following pattern:

for (i in first..last step st) { ... }

In this case we need to create a Progression object and then call its
iterator() method causing at least 2 allocation per loop. This change
replaces such loops with the following constuction:

var inductionVar = first
checkProgressionStep(step)  // check if step > 0
last = getProgressionLastElement(first, last, step)
if (first <= last) {
    do {
        i = inductionVar
        inductionVar += step
        ...
    } while(i != last)
}
This commit is contained in:
Ilya Matveev
2017-06-29 13:25:48 +07:00
committed by ilmat192
parent 06e31939dd
commit d474f207e3
10 changed files with 668 additions and 3 deletions
+23
View File
@@ -307,6 +307,29 @@ task sum_3const(type: RunKonanTest) {
source = "codegen/function/sum_3const.kt"
}
task codegen_basics_for_loops(type: RunKonanTest) {
source = "codegen/basics/for_loops.kt"
goldValue = "01234\n0123\n43210\n\n024\n02\n420\n\n036\n03\n630\n\n"
}
task codegen_basics_for_loops_types(type: RunKonanTest) {
source = "codegen/basics/for_loops_types.kt"
goldValue = "01234\n01234\n01234\n01234\n01234\n01234\n01234\n01234\n01234\n01234\n01234\n01234\n01234\n01234\n" +
"01234\n01234\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n0123\n" +
"0123\n43210\n43210\n43210\n43210\n43210\n43210\n43210\n43210\n43210\n43210\n43210\n43210\n43210\n43210\n" +
"43210\n43210\nabcd\nabc\ndcba\n"
}
task codegen_basics_for_loops_overflow(type: RunKonanTest) {
source = "codegen/basics/for_loops_overflow.kt"
goldValue = "2147483646 2147483647 \n2147483646 \n-2147483647 -2147483648 \n1073741827 \n"
}
task codegen_basics_for_loops_errors(type: RunKonanTest) {
source = "codegen/basics/for_loops_errors.kt"
goldValue = "OK\n"
}
task local_variable(type: RunKonanTest) {
source = "codegen/basics/local_variable.kt"
}