[JVM] Never treat arguments to methods as locals that can be removed.

Fixes KT-44347
This commit is contained in:
Mads Ager
2021-01-14 13:38:19 +01:00
committed by Dmitry Petrov
parent ada51509c4
commit 250cc1dc92
10 changed files with 79 additions and 0 deletions
@@ -119,6 +119,20 @@ fun MethodNode.removeEmptyCatchBlocks() {
fun MethodNode.removeUnusedLocalVariables() {
val used = BooleanArray(maxLocals) { false }
// Arguments are always used whether or not they are in the local variable table
// or used by instructions.
var argumentIndex = 0
val isStatic = (access and Opcodes.ACC_STATIC) != 0
if (!isStatic) {
used[argumentIndex++] = true
}
for (argumentType in Type.getArgumentTypes(desc)) {
for (i in 0 until argumentType.size) {
used[argumentIndex++] = true
}
}
for (insn in instructions) {
when (insn) {
is VarInsnNode -> {
@@ -5591,6 +5591,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt");
}
@Test
@TestMetadata("kt44347.kt")
public void testKt44347() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt");
}
@Test
@TestMetadata("sharedSlotsWithCapturedVars.kt")
public void testSharedSlotsWithCapturedVars() throws Exception {
@@ -0,0 +1,22 @@
// WITH_RUNTIME
import kotlin.coroutines.*
fun launch(block: suspend (Long) -> String): String {
var result = ""
block.startCoroutine(0L, Continuation(EmptyCoroutineContext) { result = it.getOrThrow() })
return result
}
suspend fun g() {}
class C {
suspend fun f(i: Long): String {
var x = 0
listOf<Int>().map { x }
g()
return "OK"
}
}
fun box(): String = launch(C()::f)
@@ -5591,6 +5591,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt");
}
@Test
@TestMetadata("kt44347.kt")
public void testKt44347() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt");
}
@Test
@TestMetadata("sharedSlotsWithCapturedVars.kt")
public void testSharedSlotsWithCapturedVars() throws Exception {
@@ -5591,6 +5591,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt");
}
@Test
@TestMetadata("kt44347.kt")
public void testKt44347() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt");
}
@Test
@TestMetadata("sharedSlotsWithCapturedVars.kt")
public void testSharedSlotsWithCapturedVars() throws Exception {
@@ -4878,6 +4878,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt");
}
@TestMetadata("kt44347.kt")
public void testKt44347() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt");
}
@TestMetadata("sharedSlotsWithCapturedVars.kt")
public void testSharedSlotsWithCapturedVars() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt");
@@ -3993,6 +3993,11 @@ public class IrJsCodegenBoxES6TestGenerated extends AbstractIrJsCodegenBoxES6Tes
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt");
}
@TestMetadata("kt44347.kt")
public void testKt44347() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt");
}
@TestMetadata("sharedSlotsWithCapturedVars.kt")
public void testSharedSlotsWithCapturedVars() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt");
@@ -3993,6 +3993,11 @@ public class IrJsCodegenBoxTestGenerated extends AbstractIrJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt");
}
@TestMetadata("kt44347.kt")
public void testKt44347() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt");
}
@TestMetadata("sharedSlotsWithCapturedVars.kt")
public void testSharedSlotsWithCapturedVars() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt");
@@ -3993,6 +3993,11 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17588.kt");
}
@TestMetadata("kt44347.kt")
public void testKt44347() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt");
}
@TestMetadata("sharedSlotsWithCapturedVars.kt")
public void testSharedSlotsWithCapturedVars() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt");
@@ -2864,6 +2864,11 @@ public class IrCodegenBoxWasmTestGenerated extends AbstractIrCodegenBoxWasmTest
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt17200.kt");
}
@TestMetadata("kt44347.kt")
public void testKt44347() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/kt44347.kt");
}
@TestMetadata("sharedSlotsWithCapturedVars.kt")
public void testSharedSlotsWithCapturedVars() throws Exception {
runTest("compiler/testData/codegen/box/closures/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt");