From 24fcadb869fb38a638de40acad281d242090a28c Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Wed, 7 Jul 2021 20:45:15 +0300 Subject: [PATCH] JVM don't run CCE on methods without optimizable conditional jumps --- ...nstantConditionEliminationMethodTransformer.kt | 15 +++++++++++++++ .../testData/debug/stepping/tryCatchExpression.kt | 12 ------------ .../testData/debug/stepping/tryCatchFinally.kt | 12 ------------ compiler/testData/debug/stepping/tryFinally.kt | 9 ++++----- 4 files changed, 19 insertions(+), 29 deletions(-) diff --git a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/ConstantConditionEliminationMethodTransformer.kt b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/ConstantConditionEliminationMethodTransformer.kt index 92724b54ba8..20eb4f0f199 100644 --- a/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/ConstantConditionEliminationMethodTransformer.kt +++ b/compiler/backend/src/org/jetbrains/kotlin/codegen/optimization/ConstantConditionEliminationMethodTransformer.kt @@ -34,11 +34,26 @@ import org.jetbrains.org.objectweb.asm.tree.analysis.Frame class ConstantConditionEliminationMethodTransformer : MethodTransformer() { override fun transform(internalClassName: String, methodNode: MethodNode) { + if (!methodNode.hasOptimizableConditions()) { + return + } do { val changes = ConstantConditionsOptimization(internalClassName, methodNode).run() } while (changes) } + private fun MethodNode.hasOptimizableConditions(): Boolean { + val insns = instructions.toArray() + return insns.any { it.isIntJump() } && insns.any { it.isIntConst() } + } + + private fun AbstractInsnNode.isIntConst() = + opcode in Opcodes.ICONST_M1..Opcodes.ICONST_5 || opcode == Opcodes.BIPUSH || opcode == Opcodes.SIPUSH || + (opcode == Opcodes.LDC && this is LdcInsnNode && cst is Int) + + private fun AbstractInsnNode.isIntJump() = + opcode in Opcodes.IFEQ..Opcodes.IFLE || opcode in Opcodes.IF_ICMPEQ..Opcodes.IF_ICMPLE + private class ConstantConditionsOptimization(val internalClassName: String, val methodNode: MethodNode) { fun run(): Boolean { val actions = collectRewriteActions() diff --git a/compiler/testData/debug/stepping/tryCatchExpression.kt b/compiler/testData/debug/stepping/tryCatchExpression.kt index caab3022a6a..ef39533b0f2 100644 --- a/compiler/testData/debug/stepping/tryCatchExpression.kt +++ b/compiler/testData/debug/stepping/tryCatchExpression.kt @@ -59,21 +59,13 @@ fun box() { // test.kt:32 mightThrow2 // test.kt:33 mightThrow2 // test.kt:11 foo -// LINENUMBERS JVM_IR // test.kt:10 foo -// LINENUMBERS JVM -// test.kt:13 foo -// LINENUMBERS // test.kt:16 foo // test.kt:17 foo // test.kt:36 mightThrow3 // test.kt:37 mightThrow3 // test.kt:17 foo -// LINENUMBERS JVM_IR // test.kt:16 foo -// LINENUMBERS JVM -// test.kt:19 foo -// LINENUMBERS // test.kt:21 foo // test.kt:42 box // test.kt:43 box @@ -87,11 +79,7 @@ fun box() { // test.kt:32 mightThrow2 // test.kt:33 mightThrow2 // test.kt:11 foo -// LINENUMBERS JVM -// test.kt:13 foo -// LINENUMBERS JVM_IR // test.kt:10 foo -// LINENUMBERS // test.kt:16 foo // test.kt:17 foo // test.kt:36 mightThrow3 diff --git a/compiler/testData/debug/stepping/tryCatchFinally.kt b/compiler/testData/debug/stepping/tryCatchFinally.kt index 21ae4d081ff..f71591017df 100644 --- a/compiler/testData/debug/stepping/tryCatchFinally.kt +++ b/compiler/testData/debug/stepping/tryCatchFinally.kt @@ -52,11 +52,7 @@ fun box() { // test.kt:13 foo // test.kt:17 foo // test.kt:18 foo -// LINENUMBERS JVM -// test.kt:17 foo -// LINENUMBERS JVM_IR // test.kt:12 foo -// LINENUMBERS // test.kt:19 foo // test.kt:34 box // test.kt:35 box @@ -73,11 +69,7 @@ fun box() { // test.kt:15 foo // test.kt:17 foo // test.kt:18 foo -// LINENUMBERS JVM -// test.kt:17 foo -// LINENUMBERS JVM_IR // test.kt:12 foo -// LINENUMBERS // test.kt:19 foo // test.kt:36 box // test.kt:37 box @@ -95,10 +87,6 @@ fun box() { // test.kt:15 foo // test.kt:17 foo // test.kt:18 foo -// LINENUMBERS JVM -// test.kt:17 foo -// LINENUMBERS JVM_IR // test.kt:12 foo -// LINENUMBERS // test.kt:19 foo // test.kt:38 box \ No newline at end of file diff --git a/compiler/testData/debug/stepping/tryFinally.kt b/compiler/testData/debug/stepping/tryFinally.kt index 49a939cfdda..8f1137243bf 100644 --- a/compiler/testData/debug/stepping/tryFinally.kt +++ b/compiler/testData/debug/stepping/tryFinally.kt @@ -56,11 +56,7 @@ fun box() { // LINENUMBERS // test.kt:13 foo // test.kt:14 foo -// LINENUMBERS JVM -// test.kt:13 foo -// LINENUMBERS JVM_IR // test.kt:10 foo -// LINENUMBERS // test.kt:15 foo // test.kt:30 box // test.kt:31 box @@ -74,4 +70,7 @@ fun box() { // test.kt:11 foo // test.kt:25 mightThrow2 // test.kt:14 foo -// test.kt:13 foo +// LINENUMBERS JVM +// test.kt:10 foo +// LINENUMBERS JVM_IR +// test.kt:13 foo \ No newline at end of file