From f23dfdc0ac79f0117e058bcdf8c6dcfa84fea7cf Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Tue, 24 Oct 2017 11:46:43 +0300 Subject: [PATCH] Mark operands of POP2 as don't touch in unused expression elimination Fixes KT-20879. --- .../PopBackwardPropagationTransformer.kt | 8 +++++-- .../comparisonWithRangeBoundEliminated.kt | 23 +++++++++++++++++++ .../ir/IrBlackBoxCodegenTestGenerated.java | 6 +++++ .../codegen/BlackBoxCodegenTestGenerated.java | 6 +++++ .../LightAnalysisModeTestGenerated.java | 6 +++++ .../semantics/JsCodegenBoxTestGenerated.java | 6 +++++ 6 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 compiler/testData/codegen/box/ranges/contains/comparisonWithRangeBoundEliminated.kt diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/PopBackwardPropagationTransformer.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/PopBackwardPropagationTransformer.kt index 8d29aa3c632..1c67d33e114 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/PopBackwardPropagationTransformer.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/boxing/PopBackwardPropagationTransformer.kt @@ -79,18 +79,22 @@ class PopBackwardPropagationTransformer : MethodTransformer() { private fun analyzeMethodBody(): Array?> { val frames = Analyzer(HazardsTrackingInterpreter()).analyze("fake", methodNode) - postprocessDupNxM(frames) + postprocessStackHazards(frames) return frames } - private fun postprocessDupNxM(frames: Array?>) { + private fun postprocessStackHazards(frames: Array?>) { val insns = methodNode.instructions.toArray() for (i in frames.indices) { val frame = frames[i] ?: continue val insn = insns[i] when (insn.opcode) { + Opcodes.POP2 -> { + val top2 = frame.peekWords(2) ?: throwIncorrectBytecode(insn, frame) + top2.forEach { it.insns.markAsDontTouch() } + } Opcodes.DUP_X1 -> { val top2 = frame.peekWords(1, 1) ?: throwIncorrectBytecode(insn, frame) top2.forEach { it.insns.markAsDontTouch() } diff --git a/compiler/testData/codegen/box/ranges/contains/comparisonWithRangeBoundEliminated.kt b/compiler/testData/codegen/box/ranges/contains/comparisonWithRangeBoundEliminated.kt new file mode 100644 index 00000000000..949136655a1 --- /dev/null +++ b/compiler/testData/codegen/box/ranges/contains/comparisonWithRangeBoundEliminated.kt @@ -0,0 +1,23 @@ +fun abs(x: Int) = if (x < 0) -x else x +fun abs(x: Long) = if (x < 0) -x else x + +fun test1() = + 5 in abs(-1) .. 10 + +fun test2() = + 5 in 1 .. abs(-10) + +fun test3() = + 5L in abs(-1L) .. 10L + +fun test4() = + 5L in 1L .. abs(-10L) + +fun box(): String { + if (!test1()) return "Fail 1" + if (!test2()) return "Fail 2" + if (!test3()) return "Fail 3" + if (!test4()) return "Fail 4" + + return "OK" +} \ No newline at end of file diff --git a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java index a49e6a25702..de8829b43cc 100644 --- a/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java +++ b/compiler/tests-ir-jvm/tests/org/jetbrains/kotlin/codegen/ir/IrBlackBoxCodegenTestGenerated.java @@ -14116,6 +14116,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/ranges/contains"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("comparisonWithRangeBoundEliminated.kt") + public void testComparisonWithRangeBoundEliminated() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/comparisonWithRangeBoundEliminated.kt"); + doTest(fileName); + } + @TestMetadata("evaluationOrderForCollection.kt") public void testEvaluationOrderForCollection() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/evaluationOrderForCollection.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java index ff3aba544da..be3d2985ee3 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/BlackBoxCodegenTestGenerated.java @@ -14116,6 +14116,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/ranges/contains"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("comparisonWithRangeBoundEliminated.kt") + public void testComparisonWithRangeBoundEliminated() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/comparisonWithRangeBoundEliminated.kt"); + doTest(fileName); + } + @TestMetadata("evaluationOrderForCollection.kt") public void testEvaluationOrderForCollection() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/evaluationOrderForCollection.kt"); diff --git a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java index 5a304495658..96832c02488 100644 --- a/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java +++ b/compiler/tests/org/jetbrains/kotlin/codegen/LightAnalysisModeTestGenerated.java @@ -14116,6 +14116,12 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/ranges/contains"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JVM, true); } + @TestMetadata("comparisonWithRangeBoundEliminated.kt") + public void testComparisonWithRangeBoundEliminated() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/comparisonWithRangeBoundEliminated.kt"); + doTest(fileName); + } + @TestMetadata("evaluationOrderForCollection.kt") public void testEvaluationOrderForCollection() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/evaluationOrderForCollection.kt"); diff --git a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java index 7539523a6b7..49b4f98469b 100644 --- a/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java +++ b/js/js.tests/test/org/jetbrains/kotlin/js/test/semantics/JsCodegenBoxTestGenerated.java @@ -15454,6 +15454,12 @@ public class JsCodegenBoxTestGenerated extends AbstractJsCodegenBoxTest { KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/codegen/box/ranges/contains"), Pattern.compile("^(.+)\\.kt$"), TargetBackend.JS, true); } + @TestMetadata("comparisonWithRangeBoundEliminated.kt") + public void testComparisonWithRangeBoundEliminated() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/comparisonWithRangeBoundEliminated.kt"); + doTest(fileName); + } + @TestMetadata("evaluationOrderForCollection.kt") public void testEvaluationOrderForCollection() throws Exception { String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/codegen/box/ranges/contains/evaluationOrderForCollection.kt");