JVM don't run CCE on methods without optimizable conditional jumps

This commit is contained in:
Dmitry Petrov
2021-07-07 20:45:15 +03:00
committed by teamcityserver
parent 25f0beed12
commit 24fcadb869
4 changed files with 19 additions and 29 deletions
@@ -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()
-12
View File
@@ -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
-12
View File
@@ -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
+4 -5
View File
@@ -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