JVM_IR: optimize more if-null chains

1. consider reads of fields from the same file "stable" just like
    functions, i.e. assume their nullability information is correct
 2. apply if-null fusion repeatedly until the subject is no longer a
    nested if-null expression
This commit is contained in:
pyos
2021-04-12 20:10:32 +02:00
committed by TeamCityServer
parent 0b0a6d6ede
commit 85b4668b7c
5 changed files with 56 additions and 15 deletions
@@ -0,0 +1,18 @@
class A(val x: String) {
fun y() = x
// Both `x` and `y()` assumed to respect their nullability information,
// so only `a` and `b` need to be checked.
fun foo1(a: A?, b: A?) = a?.x ?: b?.x ?: x // if (a == null) if (b == null) x else b.x else a.x
fun foo2(a: A?, b: A?) = a?.y() ?: b?.y() ?: y() // if (a == null) if (b == null) y() else b.y() else a.y()
}
// JVM_TEMPLATES
// Optimization not implemented
// 8 IFNULL
// 0 IFNONNULL
// 2 ACONST_NULL
// JVM_IR_TEMPLATES
// 0 IFNULL
// 4 IFNONNULL
// 0 ACONST_NULL