Use Intrinsics#compare when specializing compareTo for ints

This commit is contained in:
Dmitry Petrov
2017-05-19 16:00:13 +03:00
parent a1ae40a617
commit 39aa97eebf
3 changed files with 19 additions and 13 deletions
@@ -453,19 +453,8 @@ class RedundantBoxingMethodTransformer : MethodTransformer() {
}
else -> {
// Can't fuse with branching instruction.
// Trick: convert I, I on stack to L, L and use LCMP.
// This is more compact than explicit branching.
// TODO Generate 'java.lang.Integer#compare(int, int)' in targets >= JVM 1.7
// Initial stack: I1 I2
insertBefore(insn, InsnNode(Opcodes.SWAP)) // I2 I1
insertBefore(insn, InsnNode(Opcodes.I2L)) // L2 I1
insertBefore(insn, InsnNode(Opcodes.DUP2_X1)) // L2 I1 L2
insertBefore(insn, InsnNode(Opcodes.POP2)) // I1 L2
insertBefore(insn, InsnNode(Opcodes.I2L)) // L1 L2
insertBefore(insn, InsnNode(Opcodes.LCMP)) // compare(L1, L2)
remove(insn)
// Can't fuse with branching instruction. Use Intrinsics#compare(int, int).
set(insn, MethodInsnNode(Opcodes.INVOKESTATIC, "kotlin/jvm/internal/Intrinsics", "compare", "(II)I", false))
}
}
}
@@ -0,0 +1,11 @@
fun box(): String {
val a: Any = 1
val b: Any = 42
val test = (a as Comparable<Any>).compareTo(b)
if (test != -1) return "Fail: $test"
return "OK"
}
// 0 compareTo
// 1 Intrinsics.compare
@@ -479,6 +479,12 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest {
doTest(fileName);
}
@TestMetadata("intCompareTo.kt")
public void testIntCompareTo() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/boxingOptimization/intCompareTo.kt");
doTest(fileName);
}
@TestMetadata("kClassInAnnotation.kt")
public void testKClassInAnnotation() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/bytecodeText/boxingOptimization/kClassInAnnotation.kt");