Files
kotlin/compiler/testData/codegen/bytecodeText/constantConditions/constantFlag.kt
Dmitry Petrov 2051355d6a 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).
2017-05-16 17:28:43 +03:00

36 lines
632 B
Kotlin
Vendored

// FILE: util.kt
const val FLAG = true
const val OTHER_FLAG = false
fun doStuff1() {}
fun doStuff2() {}
fun doStuff3() {}
inline fun doStuff1IfTrue(flag: Boolean) {
if (flag) doStuff1()
}
inline fun doStuff2IfFalse(flag: Boolean) {
if (!flag) doStuff2()
}
inline fun doStuff3IfComplex(flag: Boolean) {
if (flag && !OTHER_FLAG) doStuff3()
}
// FILE: test.kt
fun test() {
doStuff1IfTrue(FLAG)
doStuff2IfFalse(FLAG)
doStuff3IfComplex(FLAG)
}
// @TestKt.class:
// 0 FLAG
// 1 INVOKESTATIC UtilKt.doStuff1
// 0 INVOKESTATIC UtilKt.doStuff2
// 1 INVOKESTATIC UtilKt.doStuff3
// 0 ILOAD 0
// 0 IFEQ
// 0 IFNE