JVM_IR: fix primitive comparison optimizations
1. the `primitive == object?.something` fusion should not apply to
`primitive.equals(object?.something)` because it can't;
2. coercions to Int are there for a reason - don't remove them;
3. better optimize `primitive == object?.something` -- the result
should be subject to if-null fusion, so it needs to have a specific
pattern that resembles safe calls.
#KT-47597 Fixed
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
class C(val x: Short)
|
||||
|
||||
fun box(): String {
|
||||
val a: C = C(1)
|
||||
val b: C? = C(1)
|
||||
return if (b?.x == a.x) "OK" else "fail"
|
||||
}
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
// IGNORE_BACKEND: JS, JS_IR, WASM, NATIVE
|
||||
var result = ""
|
||||
|
||||
fun sideEffecting(): Int {
|
||||
result += "OK"
|
||||
return 123
|
||||
}
|
||||
|
||||
class C(val x: Int)
|
||||
|
||||
val a: C? = C(123)
|
||||
val b: C? = null
|
||||
|
||||
fun box(): String {
|
||||
if (a?.x != sideEffecting()) return "fail cmp 1"
|
||||
// RHS not evaluated because `b` is null, might be a bug:
|
||||
if (b?.x == sideEffecting()) return "fail cmp 2"
|
||||
return result
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
class C(val x: Short)
|
||||
|
||||
fun box(): String {
|
||||
val a: Short = 1
|
||||
val b: C? = C(1)
|
||||
return if (a.equals(b?.x)) "OK" else "fail"
|
||||
}
|
||||
+23
-3
@@ -1,10 +1,25 @@
|
||||
// FILE: J.java
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public interface J {
|
||||
@NotNull
|
||||
public Integer foo();
|
||||
}
|
||||
|
||||
// FILE: test.kt
|
||||
fun Long.id() = this
|
||||
|
||||
fun Short.id() = this
|
||||
|
||||
fun String.drop2() = if (length >= 2) subSequence(2, length) else null
|
||||
|
||||
fun doSimple1(s: String?) = s?.length == 3
|
||||
|
||||
fun doLongReceiver1(x: Long) = x?.id() == 3L
|
||||
fun doJava1(s: String?, j: J) = s?.length == j.foo()
|
||||
|
||||
fun doLongReceiver1(x: Long?) = x?.id() == 3L
|
||||
|
||||
fun doShortReceiver1(x: Short?, y: Short) = x?.id() == y
|
||||
|
||||
fun doChain1(s: String?) = s?.drop2()?.length == 1
|
||||
|
||||
@@ -13,11 +28,16 @@ fun doIf1(s: String?) =
|
||||
|
||||
fun doSimple2(s: String?) = 3 == s?.length
|
||||
|
||||
fun doLongReceiver2(x: Long) = 3L == x?.id()
|
||||
fun doJava2(s: String?, j: J) = j.foo() == s?.length
|
||||
|
||||
fun doLongReceiver2(x: Long?) = 3L == x?.id()
|
||||
|
||||
fun doShortReceiver2(x: Short?, y: Short) = y == x?.id()
|
||||
|
||||
fun doChain2(s: String?) = 1 == s?.drop2()?.length
|
||||
|
||||
fun doIf2(s: String?) =
|
||||
if (1 == s?.length) "A" else "B"
|
||||
|
||||
// 0 valueOf
|
||||
// `doJava1`/`doJava2` box `s?.length` instead of unboxing `j.foo()`:
|
||||
// 2 valueOf
|
||||
|
||||
+2
-4
@@ -1,5 +1,4 @@
|
||||
// !LANGUAGE: +ProperIeee754Comparisons
|
||||
// IGNORE_BACKEND_FIR: 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!!
|
||||
@@ -21,10 +20,9 @@ fun less5(a: Any?, b: Any?) = if (a is Byte && b is Byte) a < b else true
|
||||
// 3 IF_ICMPGE
|
||||
|
||||
// JVM_IR_TEMPLATES
|
||||
// 2 Intrinsics\.areEqual
|
||||
// 0 Intrinsics\.areEqual
|
||||
// 0 Intrinsics\.compare
|
||||
// 4 INVOKEVIRTUAL java/lang/Byte\.byteValue \(\)B
|
||||
// 8 INVOKEVIRTUAL java/lang/Byte\.byteValue \(\)B
|
||||
// 4 INVOKEVIRTUAL java/lang/Number\.byteValue \(\)B
|
||||
// 0 IFGE
|
||||
// 3 IF_ICMPGE
|
||||
|
||||
|
||||
+3
-5
@@ -1,6 +1,4 @@
|
||||
// !LANGUAGE: -ProperIeee754Comparisons
|
||||
// IGNORE_BACKEND_FIR: 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!!
|
||||
@@ -21,9 +19,9 @@ fun less5(a: Any?, b: Any?) = if (a is Byte && b is Byte) a < b else true
|
||||
// 0 IF_ICMPGE
|
||||
|
||||
// JVM_IR_TEMPLATES
|
||||
// 2 Intrinsics\.areEqual
|
||||
// 0 Intrinsics\.areEqual
|
||||
// 0 Intrinsics\.compare
|
||||
// 4 INVOKEVIRTUAL java/lang/Byte\.byteValue \(\)B
|
||||
// 8 INVOKEVIRTUAL java/lang/Byte\.byteValue \(\)B
|
||||
// 4 INVOKEVIRTUAL java/lang/Number\.byteValue \(\)B
|
||||
// 0 IFGE
|
||||
// 3 IF_ICMPGE
|
||||
// 3 IF_ICMPGE
|
||||
|
||||
Reference in New Issue
Block a user