diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt index dfc207eb225..c173542f2a9 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/codegen/ExpressionCodegen.kt @@ -603,6 +603,7 @@ class ExpressionCodegen( assert(exhaustive || expression.type.isUnit()) { "non-exhaustive conditional should return Unit: ${expression.dump()}" } + val lastBranch = expression.branches.lastOrNull() for (branch in expression.branches) { val elseLabel = Label() if (branch.condition.isFalseConst() || branch.condition.isTrueConst()) { @@ -628,7 +629,10 @@ class ExpressionCodegen( return materializedResult } } - mv.goTo(endLabel) + + if (branch != lastBranch) { + mv.goTo(endLabel) + } mv.mark(elseLabel) } mv.mark(endLabel) diff --git a/compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInLastBranch.kt b/compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInLastBranch.kt new file mode 100644 index 00000000000..61f22d5876d --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInLastBranch.kt @@ -0,0 +1,17 @@ +// KOTLIN_CONFIGURATION_FLAGS: +JVM.DISABLE_OPTIMIZATION + +fun consume(i: Int) {} + +fun foo(a: String) { + var b = 1 + if (a[1] == 'b') { + b = 2 + } else if (a[0] == 'a') { + b = 3 + } + + consume(b) +} + +// 1 GOTO +// 2 IF \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInSingleBranch.kt b/compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInSingleBranch.kt new file mode 100644 index 00000000000..fdf2360b47d --- /dev/null +++ b/compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInSingleBranch.kt @@ -0,0 +1,15 @@ +// KOTLIN_CONFIGURATION_FLAGS: +JVM.DISABLE_OPTIMIZATION + +fun consume(i: Int) {} + +fun foo(a: Boolean) { + var b = 1 + if (a) { + b = 2 + } + + consume(b) +} + +// 0 GOTO +// 1 IF \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index 0b6ac8a02ce..20c3ce91e5d 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -1614,6 +1614,29 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { } } + @TestMetadata("compiler/testData/codegen/bytecodeText/disabledOptimizations") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class DisabledOptimizations extends AbstractBytecodeTextTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM, testDataFilePath); + } + + public void testAllFilesPresentInDisabledOptimizations() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/bytecodeText/disabledOptimizations"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); + } + + @TestMetadata("noJumpInLastBranch.kt") + public void testNoJumpInLastBranch() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInLastBranch.kt"); + } + + @TestMetadata("noJumpInSingleBranch.kt") + public void testNoJumpInSingleBranch() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInSingleBranch.kt"); + } + } + @TestMetadata("compiler/testData/codegen/bytecodeText/enum") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class) diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java index 1b2bb8f1052..260e2eb7be4 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java @@ -1569,6 +1569,29 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest { } } + @TestMetadata("compiler/testData/codegen/bytecodeText/disabledOptimizations") + @TestDataPath("$PROJECT_ROOT") + @RunWith(JUnit3RunnerWithInners.class) + public static class DisabledOptimizations extends AbstractIrBytecodeTextTest { + private void runTest(String testDataFilePath) throws Exception { + KotlinTestUtils.runTest(this::doTest, TargetBackend.JVM_IR, testDataFilePath); + } + + public void testAllFilesPresentInDisabledOptimizations() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/bytecodeText/disabledOptimizations"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM_IR, true); + } + + @TestMetadata("noJumpInLastBranch.kt") + public void testNoJumpInLastBranch() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInLastBranch.kt"); + } + + @TestMetadata("noJumpInSingleBranch.kt") + public void testNoJumpInSingleBranch() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/disabledOptimizations/noJumpInSingleBranch.kt"); + } + } + @TestMetadata("compiler/testData/codegen/bytecodeText/enum") @TestDataPath("$PROJECT_ROOT") @RunWith(JUnit3RunnerWithInners.class)