From c706673de92c3a3f2603fde4031d1fd7760dd383 Mon Sep 17 00:00:00 2001 From: Alexander Udalov Date: Fri, 28 Aug 2020 20:22:24 +0200 Subject: [PATCH] Minor, refactor bytecode text tests on when-over-string optimization - Merge `duplicatingItemsSameHashCode.kt` and `duplicatingItemsSameHashCode2.kt` into one test enabled on both backends, and rename it to `duplicatingItemsSameHashCodeFewBranches.kt`. - Rename `duplicatingItemsSameHashCode3.kt` to `duplicatingItemsSameHashCodeMoreBranches.kt`, and also enable it for both backends. - Use JVM_TEMPLATES/JVM_IR_TEMPLATES to check backend-specific behavior: 1) JVM IR does not optimize less than 2 branches by design 2) JVM IR does not generate duplicate branches, also by design Related to KT-36846. --- .../duplicatingItemsSameHashCode.kt | 17 ----------------- ... duplicatingItemsSameHashCodeFewBranches.kt} | 10 +++++----- ...duplicatingItemsSameHashCodeMoreBranches.kt} | 3 ++- .../codegen/BytecodeTextTestGenerated.java | 17 ++++++----------- .../codegen/ir/IrBytecodeTextTestGenerated.java | 17 ++++++----------- 5 files changed, 19 insertions(+), 45 deletions(-) delete mode 100644 compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode.kt rename compiler/testData/codegen/bytecodeText/whenStringOptimization/{duplicatingItemsSameHashCode2.kt => duplicatingItemsSameHashCodeFewBranches.kt} (77%) rename compiler/testData/codegen/bytecodeText/whenStringOptimization/{duplicatingItemsSameHashCode3.kt => duplicatingItemsSameHashCodeMoreBranches.kt} (93%) diff --git a/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode.kt b/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode.kt deleted file mode 100644 index a235c1e3e22..00000000000 --- a/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode.kt +++ /dev/null @@ -1,17 +0,0 @@ -// IGNORE_BACKEND: JVM_IR -// TODO KT-36846 Generate TABLESWITCH/LOOKUPSWITCH in case of matching hashCodes in JVM_IR - -fun foo(x : String) : String { - assert("abz]".hashCode() == "aby|".hashCode()) - - when (x) { - "abz]" -> return "abz" - "ghi" -> return "ghi" - "aby|" -> return "aby" - "abz]" -> return "fail" - } - - return "other" -} - -// 1 LOOKUPSWITCH diff --git a/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode2.kt b/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeFewBranches.kt similarity index 77% rename from compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode2.kt rename to compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeFewBranches.kt index 147fea74443..5c6d7a24e61 100644 --- a/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode2.kt +++ b/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeFewBranches.kt @@ -1,6 +1,3 @@ -// IGNORE_BACKEND: JVM -import kotlin.test.assertEquals - fun foo(x : String) : String { assert("abz]".hashCode() == "aby|".hashCode()) @@ -14,6 +11,9 @@ fun foo(x : String) : String { return "other" } -// Expecting 0 LOOKUPSWITCH as there is only 2 different hash codes. -// An IF casecade is more efficient. +// JVM_TEMPLATES: +// 1 LOOKUPSWITCH + +// JVM_IR_TEMPLATES: +// Expecting 0 LOOKUPSWITCH as there is only 2 different hash codes. An IF cascade is more efficient. // 0 LOOKUPSWITCH diff --git a/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode3.kt b/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeMoreBranches.kt similarity index 93% rename from compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode3.kt rename to compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeMoreBranches.kt index f2800eab8b5..44958966402 100644 --- a/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode3.kt +++ b/compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeMoreBranches.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND: JVM import kotlin.test.assertEquals fun foo(x : String) : String { @@ -16,4 +15,6 @@ fun foo(x : String) : String { } // 1 LOOKUPSWITCH + +// JVM_IR_TEMPLATES: // 0 LDC "fail" diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java index e57e7c59e3a..b832243ea32 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BytecodeTextTestGenerated.java @@ -4931,19 +4931,14 @@ public class BytecodeTextTestGenerated extends AbstractBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItems.kt"); } - @TestMetadata("duplicatingItemsSameHashCode.kt") - public void testDuplicatingItemsSameHashCode() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode.kt"); + @TestMetadata("duplicatingItemsSameHashCodeFewBranches.kt") + public void testDuplicatingItemsSameHashCodeFewBranches() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeFewBranches.kt"); } - @TestMetadata("duplicatingItemsSameHashCode2.kt") - public void testDuplicatingItemsSameHashCode2() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode2.kt"); - } - - @TestMetadata("duplicatingItemsSameHashCode3.kt") - public void testDuplicatingItemsSameHashCode3() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode3.kt"); + @TestMetadata("duplicatingItemsSameHashCodeMoreBranches.kt") + public void testDuplicatingItemsSameHashCodeMoreBranches() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeMoreBranches.kt"); } @TestMetadata("expression.kt") diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java index 91733f5f01c..239929bd763 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/ir/IrBytecodeTextTestGenerated.java @@ -4899,19 +4899,14 @@ public class IrBytecodeTextTestGenerated extends AbstractIrBytecodeTextTest { runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItems.kt"); } - @TestMetadata("duplicatingItemsSameHashCode.kt") - public void testDuplicatingItemsSameHashCode() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode.kt"); + @TestMetadata("duplicatingItemsSameHashCodeFewBranches.kt") + public void testDuplicatingItemsSameHashCodeFewBranches() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeFewBranches.kt"); } - @TestMetadata("duplicatingItemsSameHashCode2.kt") - public void testDuplicatingItemsSameHashCode2() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode2.kt"); - } - - @TestMetadata("duplicatingItemsSameHashCode3.kt") - public void testDuplicatingItemsSameHashCode3() throws Exception { - runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCode3.kt"); + @TestMetadata("duplicatingItemsSameHashCodeMoreBranches.kt") + public void testDuplicatingItemsSameHashCodeMoreBranches() throws Exception { + runTest("compiler/testData/codegen/bytecodeText/whenStringOptimization/duplicatingItemsSameHashCodeMoreBranches.kt"); } @TestMetadata("expression.kt")