KT-55005 Do not generate CHECKCAST before AASTORE

This commit is contained in:
Pavel Mikhailovskii
2022-12-21 15:06:07 +00:00
committed by Space Team
parent d26b96afe1
commit 06d3e1d8bd
9 changed files with 70 additions and 19 deletions
@@ -61,6 +61,7 @@ class OptimizationMethodVisitor(
RedundantGotoMethodTransformer(),
RedundantNopsCleanupMethodTransformer(),
NegatedJumpsMethodTransformer(),
RedundantCheckcastsBeforeAastoreMethodTransformer,
MethodVerifier("AFTER optimizations", generationState)
)
@@ -0,0 +1,30 @@
/*
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.codegen.optimization
import org.jetbrains.kotlin.codegen.inline.ReifiedTypeInliner.Companion.isOperationReifiedMarker
import org.jetbrains.kotlin.codegen.optimization.transformer.MethodTransformer
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.tree.MethodNode
object RedundantCheckcastsBeforeAastoreMethodTransformer : MethodTransformer() {
override fun transform(internalClassName: String, methodNode: MethodNode) {
val iter = methodNode.instructions.iterator()
while (iter.hasNext()) {
val insn = iter.next()
if (insn.opcode == Opcodes.CHECKCAST && insn.next?.opcode == Opcodes.AASTORE) {
val isReified = isOperationReifiedMarker(insn.previous)
iter.remove()
if (isReified) {
for (i in 1..3) {
iter.previous()
iter.remove()
}
}
}
}
}
}
@@ -4853,6 +4853,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/casts/kt54802.kt");
}
@Test
@TestMetadata("kt55005.kt")
public void testKt55005() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt55005.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {
+27
View File
@@ -0,0 +1,27 @@
// TARGET_BACKEND: JVM_IR
// CHECK_BYTECODE_TEXT
// WITH_STDLIB
import kotlin.test.*
inline fun <reified T> foo(x: Any) = arrayOf<T>(x as T)
fun box(): String {
val a: Array<String> = arrayOf("")
assertFailsWith<ArrayStoreException> {
(a as Array<Any>)[0] = Any()
}
assertFailsWith<ArrayStoreException> {
(a as Array<Any>)[0] = 1
}
assertFailsWith<ArrayStoreException> {
(a as Array<CharSequence>)[0] = StringBuilder()
}
assertFailsWith<ArrayStoreException> {
foo<String>(Any())
}
return "OK"
}
// 0 CHECKCAST
@@ -4853,6 +4853,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/casts/kt54802.kt");
}
@Test
@TestMetadata("kt55005.kt")
public void testKt55005() throws Exception {
runTest("compiler/testData/codegen/box/casts/kt55005.kt");
}
@Test
@TestMetadata("lambdaToUnitCast.kt")
public void testLambdaToUnitCast() throws Exception {