From 951b2fa770633164dbeedceb9b3c4ec2a2a1ebe1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Steven=20Sch=C3=A4fer?= Date: Wed, 29 Jan 2020 15:43:14 +0100 Subject: [PATCH] JVM IR: Optimize type checks to null checks if possible --- .../kotlin/backend/jvm/lower/TypeOperatorLowering.kt | 2 ++ .../genericParameterBridge/notNullAnyMC.kt | 7 +++++-- .../genericParameterBridge/notNullParamMC.kt | 7 +++++-- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt index 17d774daa6c..c68dec9dc7e 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/lower/TypeOperatorLowering.kt @@ -69,6 +69,8 @@ private class TypeOperatorLowering(private val context: JvmBackendContext) : Fil ) } } + argument.type.isNullable() && !type.isNullable() && argument.type.erasedUpperBound == type.erasedUpperBound -> + irNotEquals(argument, irNull()) else -> irIs(argument, type.makeNotNull()) } } diff --git a/compiler/testData/codegen/bytecodeText/builtinFunctions/genericParameterBridge/notNullAnyMC.kt b/compiler/testData/codegen/bytecodeText/builtinFunctions/genericParameterBridge/notNullAnyMC.kt index 0da6a697c53..4e73e898f7c 100644 --- a/compiler/testData/codegen/bytecodeText/builtinFunctions/genericParameterBridge/notNullAnyMC.kt +++ b/compiler/testData/codegen/bytecodeText/builtinFunctions/genericParameterBridge/notNullAnyMC.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR abstract class A8 : MutableCollection { override fun contains(o: Any): Boolean { throw UnsupportedOperationException() @@ -8,5 +7,9 @@ abstract class A8 : MutableCollection { // 1 bridge // 1 public final bridge size // 0 INSTANCEOF -/* Only 1 IFNULL should be within contains method */ + +/* Only 1 null check should be within the contains method */ +// JVM_TEMPLATES: // 1 IFNULL +// JVM_IR_TEMPLATES: +// 1 IFNONNULL diff --git a/compiler/testData/codegen/bytecodeText/builtinFunctions/genericParameterBridge/notNullParamMC.kt b/compiler/testData/codegen/bytecodeText/builtinFunctions/genericParameterBridge/notNullParamMC.kt index e3491e0b8df..2cde9869764 100644 --- a/compiler/testData/codegen/bytecodeText/builtinFunctions/genericParameterBridge/notNullParamMC.kt +++ b/compiler/testData/codegen/bytecodeText/builtinFunctions/genericParameterBridge/notNullParamMC.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM_IR abstract class A : MutableCollection { override fun contains(o: T): Boolean { throw UnsupportedOperationException() @@ -8,5 +7,9 @@ abstract class A : MutableCollection { // 1 bridge // 1 public final bridge size // 0 INSTANCEOF -/* Only 1 IFNULL should be within contains method (because T is not nullable) */ + +/* Only 1 null check should be within the contains method (because T is not nullable) */ +// JVM_TEMPLATES: // 1 IFNULL +// JVM_IR_TEMPLATES: +// 1 IFNONNULL