JVM_IR: frame optimization for cast to IntProgression

#KT-29199
This commit is contained in:
Xin Wang
2022-01-25 23:06:49 +08:00
committed by TeamCityServer
parent 15e08893aa
commit fdbcb64d39
4 changed files with 34 additions and 1 deletions
@@ -35,6 +35,7 @@ import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode
import org.jetbrains.org.objectweb.asm.tree.InsnList
import org.jetbrains.org.objectweb.asm.tree.MethodInsnNode
import org.jetbrains.org.objectweb.asm.tree.TypeInsnNode
import org.jetbrains.org.objectweb.asm.tree.analysis.BasicValue
open class BoxingInterpreter(
@@ -136,7 +137,10 @@ open class BoxingInterpreter(
override fun unaryOperation(insn: AbstractInsnNode, value: BasicValue): BasicValue? {
checkUsedValue(value)
return if (insn.opcode == Opcodes.CHECKCAST && isExactValue(value))
return if (insn.opcode == Opcodes.CHECKCAST
&& isExactValue(value)
&& !isCastToProgression(insn) // operations such as cast kotlin/ranges/IntRange to kotlin/ranges/IntProgression, should be allowed
)
value
else
super.unaryOperation(insn, value)
@@ -147,6 +151,16 @@ open class BoxingInterpreter(
value is CleanBoxedValue ||
value.type != null && isProgressionClass(value.type)
private fun isCastToProgression(insn: AbstractInsnNode): Boolean {
assert(insn.opcode == Opcodes.CHECKCAST) { "Expected opcode Opcodes.CHECKCAST, but ${insn.opcode} found" }
val desc = (insn as TypeInsnNode).desc
return desc in setOf(
"kotlin/ranges/CharProgression",
"kotlin/ranges/IntProgression",
"kotlin/ranges/LongProgression"
)
}
override fun merge(v: BasicValue, w: BasicValue) =
mergeStackValues(v, w)
@@ -367,6 +367,12 @@ public class FirBytecodeTextTestGenerated extends AbstractFirBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/maxStackAfterOptimizations.kt");
}
@Test
@TestMetadata("mergedProgression.kt")
public void testMergedProgression() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/mergedProgression.kt");
}
@Test
@TestMetadata("noAccessorForProtectedInSamePackageCrossinline.kt")
public void testNoAccessorForProtectedInSamePackageCrossinline() throws Exception {
@@ -0,0 +1,7 @@
// TARGET_BACKEND: JVM_IR
fun test(a: Int, b: Int, flag: Boolean) =
(if (flag) a..b else a downTo b).map { it + 1 }
// 0 java/util/Iterator.next \(\)Ljava/lang/Object;
// 1 kotlin/collections/IntIterator.nextInt \(\)I
@@ -367,6 +367,12 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest {
runTest("compiler/testData/codegen/bytecodeText/maxStackAfterOptimizations.kt");
}
@Test
@TestMetadata("mergedProgression.kt")
public void testMergedProgression() throws Exception {
runTest("compiler/testData/codegen/bytecodeText/mergedProgression.kt");
}
@Test
@TestMetadata("noAccessorForProtectedInSamePackageCrossinline.kt")
public void testNoAccessorForProtectedInSamePackageCrossinline() throws Exception {