From 81609e4c6f8e0a19d01d3b9d6771461dc11fda83 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 14 Jul 2017 09:20:28 +0300 Subject: [PATCH] Generate complex equality comparison only when RHS can have side effects In "short" version, LHS is always evaluated before RHS, and RHS may be not evaluated if LHS is null. So, it makes sense to use "short" version in cases when RHS can't have side effects (because of more compact bytecode and more opportunities for code elimination). --- .../kotlin/codegen/BoxedVsPrimitiveBranchedValues.kt | 4 ++-- .../boxedEqPrimitiveEvaluationOrder.kt | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/BoxedVsPrimitiveBranchedValues.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/BoxedVsPrimitiveBranchedValues.kt index c2d8087039d..439e252b144 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/BoxedVsPrimitiveBranchedValues.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/BoxedVsPrimitiveBranchedValues.kt @@ -134,7 +134,7 @@ class BoxedToPrimitiveEquality private constructor( } private fun jumpIfTrue(v: InstructionAdapter, jumpLabel: Label) { - if (arg1.canHaveSideEffects() || arg2!!.canHaveSideEffects()) { + if (arg2!!.canHaveSideEffects()) { jumpIfTrueWithPossibleSideEffects(v, jumpLabel) return } @@ -179,7 +179,7 @@ class BoxedToPrimitiveEquality private constructor( } private fun jumpIfFalse(v: InstructionAdapter, jumpLabel: Label) { - if (arg1.canHaveSideEffects() || arg2!!.canHaveSideEffects()) { + if (arg2!!.canHaveSideEffects()) { jumpIfFalseWithPossibleSideEffects(v, jumpLabel) return } diff --git a/compiler/testData/codegen/box/primitiveTypes/equalityWithNullable/boxedEqPrimitiveEvaluationOrder.kt b/compiler/testData/codegen/box/primitiveTypes/equalityWithNullable/boxedEqPrimitiveEvaluationOrder.kt index c8d43de6623..0a8975e6127 100644 --- a/compiler/testData/codegen/box/primitiveTypes/equalityWithNullable/boxedEqPrimitiveEvaluationOrder.kt +++ b/compiler/testData/codegen/box/primitiveTypes/equalityWithNullable/boxedEqPrimitiveEvaluationOrder.kt @@ -17,10 +17,18 @@ inline fun evaluateAndCheckOrder(marker: String, expectedValue: Boolean, expecte if (order != expectedOrder) throw AssertionError("$marker, order: Expected: '$expectedOrder', actual: '$order'") } +val nn: Int? = null + fun box(): String { evaluateAndCheckOrder("1", true, "ab") { a(1) == b(1) } evaluateAndCheckOrder("2", true, "ab") { a(1) != b(2) } evaluateAndCheckOrder("3", true, "ab") { !(a(1) == b(2)) } evaluateAndCheckOrder("4", true, "ab") { !(a(1) != b(1)) } + + evaluateAndCheckOrder("null == 1", false, "a") { nn == a(1) } + evaluateAndCheckOrder("null != 1", true, "a") { nn != a(1) } + evaluateAndCheckOrder("!(null == 1)", true, "a") { !(nn == a(1)) } + evaluateAndCheckOrder("!(null != 1)", false, "a") { !(nn != a(1)) } + return "OK" } \ No newline at end of file