Don't generate jump for last when condition

This commit is contained in:
Mikhael Bogdanov
2019-11-15 12:03:45 +01:00
parent 9a07063d8b
commit e409387078
5 changed files with 83 additions and 1 deletions
@@ -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)
@@ -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
@@ -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
@@ -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)
@@ -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)