[JVM_IR] Avoid safe-call conversions from Byte? and Short? to Int? for

comparisons.

Having those conversions leads to unnecesary boxing and null checks.

This change does it only for JVM in the optimization lowering. It
is unclear to me if the other backends can get away with something
similar.
This commit is contained in:
Mads Ager
2020-05-07 15:09:25 +02:00
committed by Dmitry Petrov
parent 3d5003d476
commit 1f4a3b0d1c
8 changed files with 66 additions and 19 deletions
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
fun testBoolean1(a: Boolean?, b: Boolean) = a == b
fun testBoolean2(a: Boolean?, b: Boolean) = a != b
@@ -1,5 +1,3 @@
// IGNORE_BACKEND: JVM_IR
fun testBoolean1(a: Boolean, b: Boolean?) = a == b
fun testBoolean2(a: Boolean, b: Boolean?) = a != b
@@ -1,6 +1,4 @@
// !LANGUAGE: +ProperIeee754Comparisons
// IGNORE_BACKEND: JVM_IR
fun equals3(a: Byte?, b: Byte?) = a != null && b != null && a == b
fun equals4(a: Byte?, b: Byte?) = if (a is Byte && b is Byte) a == b else null!!
@@ -13,9 +11,19 @@ fun less4(a: Byte?, b: Byte?) = if (a is Byte && b is Byte) a < b else true
fun less5(a: Any?, b: Any?) = if (a is Byte && b is Byte) a < b else true
// JVM_TEMPLATES
// 3 Intrinsics\.areEqual
// 0 Intrinsics\.compare
// 4 INVOKEVIRTUAL java/lang/Byte\.byteValue \(\)B
// 2 INVOKEVIRTUAL java/lang/Number\.intValue \(\)I
// 0 IFGE
// 3 IF_ICMPGE
// JVM_IR_TEMPLATES
// 2 Intrinsics\.areEqual
// 0 Intrinsics\.compare
// 4 INVOKEVIRTUAL java/lang/Byte\.byteValue \(\)B
// 4 INVOKEVIRTUAL java/lang/Number\.byteValue \(\)B
// 0 IFGE
// 3 IF_ICMPGE
@@ -1,5 +1,4 @@
// !LANGUAGE: -ProperIeee754Comparisons
// IGNORE_BACKEND: JVM_IR
fun equals3(a: Byte?, b: Byte?) = a != null && b != null && a == b
@@ -21,10 +20,9 @@ fun less5(a: Any?, b: Any?) = if (a is Byte && b is Byte) a < b else true
// 0 IF_ICMPGE
// JVM_IR_TEMPLATES
// 3 Intrinsics\.areEqual
// 2 Intrinsics\.areEqual
// 0 Intrinsics\.compare
// 4 INVOKEVIRTUAL java/lang/Byte\.byteValue \(\)B
// 2 INVOKEVIRTUAL java/lang/Number\.intValue \(\)I
// 4 INVOKEVIRTUAL java/lang/Number\.byteValue \(\)B
// 0 IFGE
// 3 IF_ICMPGE
// 0 IF_ICMPNE
// 3 IF_ICMPGE
@@ -13,6 +13,7 @@ fun less4(a: Short?, b: Short?) = if (a is Short && b is Short) a < b else true
fun less5(a: Any?, b: Any?) = if (a is Short && b is Short) a < b else true
// JVM_TEMPLATES
// 3 Intrinsics\.areEqual
// 0 Intrinsics\.compare
// 4 INVOKEVIRTUAL java/lang/Short\.shortValue \(\)S
@@ -20,3 +21,13 @@ fun less5(a: Any?, b: Any?) = if (a is Short && b is Short) a < b else true
// 0 IFGE
// 3 IF_ICMPGE
// 0 IF_ICMPNE
// JVM_IR_TEMPLATES
// 2 Intrinsics\.areEqual
// 0 Intrinsics\.compare
// 4 INVOKEVIRTUAL java/lang/Short\.shortValue \(\)S
// 4 INVOKEVIRTUAL java/lang/Number\.shortValue \(\)S
// 0 IFGE
// 3 IF_ICMPGE
// 0 IF_ICMPNE
@@ -20,10 +20,10 @@ fun less5(a: Any?, b: Any?) = if (a is Short && b is Short) a < b else true
// 0 IF_ICMPGE
// JVM_IR_TEMPLATES
// 3 Intrinsics\.areEqual
// 2 Intrinsics\.areEqual
// 0 Intrinsics\.compare
// 4 INVOKEVIRTUAL java/lang/Short\.shortValue \(\)S
// 2 INVOKEVIRTUAL java/lang/Number\.intValue \(\)I
// 4 INVOKEVIRTUAL java/lang/Number\.shortValue \(\)S
// 0 IFGE
// 3 IF_ICMPGE
// 0 IF_ICMPNE