Optimize constant conditions

Using basic constant propagation (only integer constants, no arithmetic
calculations), rewrite conditional jump instructions with constant
arguments.

This covers problem description in KT-17007.
Note that it also works transparently with inline functions.
Partial evaluation is required to cover more "advanced" cases.

As a side effect, this also covers KT-3098:
rewrite IF_ICMP<cmp_op>(x, 0) to IF<cmp0_op>(x).
This commit is contained in:
Dmitry Petrov
2017-05-12 15:22:52 +03:00
parent 495fba43c0
commit 2051355d6a
8 changed files with 372 additions and 6 deletions
@@ -0,0 +1,25 @@
// FILE: util.kt
inline fun ieq(x: Int, y: Int) = x == y
inline fun ine(x: Int, y: Int) = x != y
inline fun ilt(x: Int, y: Int) = x < y
inline fun ile(x: Int, y: Int) = x <= y
inline fun igt(x: Int, y: Int) = x > y
inline fun ige(x: Int, y: Int) = x >= y
// FILE: test.kt
fun testeq(x: Int) = ieq(x, 0)
fun testne(x: Int) = ine(x, 0)
fun testlt(x: Int) = ilt(x, 0)
fun testle(x: Int) = ile(x, 0)
fun testgt(x: Int) = igt(x, 0)
fun testge(x: Int) = ige(x, 0)
// @TestKt.class:
// 0 IF_ICMPEQ
// 0 IF_ICMPNE
// 0 IF_ICMPGE
// 0 IF_ICMPGT
// 0 IF_ICMPLE
// 0 IF_ICMPLT