JVM optimize unneeded temporary vals
This commit is contained in:
committed by
TeamCityServer
parent
d8f6d82411
commit
f4a1e27124
@@ -16,17 +16,14 @@
|
||||
|
||||
package org.jetbrains.kotlin.codegen
|
||||
|
||||
import org.jetbrains.kotlin.codegen.inline.nodeText
|
||||
import org.jetbrains.kotlin.codegen.inline.wrapWithMaxLocalCalc
|
||||
import org.jetbrains.org.objectweb.asm.MethodVisitor
|
||||
import org.jetbrains.org.objectweb.asm.Opcodes
|
||||
import org.jetbrains.org.objectweb.asm.tree.MethodNode
|
||||
import org.jetbrains.org.objectweb.asm.util.Textifier
|
||||
import org.jetbrains.org.objectweb.asm.util.TraceMethodVisitor
|
||||
|
||||
import java.util.ArrayList
|
||||
|
||||
import org.jetbrains.kotlin.codegen.inline.nodeText
|
||||
import org.jetbrains.kotlin.codegen.inline.wrapWithMaxLocalCalc
|
||||
|
||||
abstract class TransformationMethodVisitor(
|
||||
private val delegate: MethodVisitor,
|
||||
access: Int,
|
||||
|
||||
+39
-3
@@ -118,9 +118,17 @@ open class FastMethodAnalyzer<V : Value>(
|
||||
}
|
||||
|
||||
} catch (e: AnalyzerException) {
|
||||
throw AnalyzerException(e.node, "Error at instruction #$insn ${insnNode.insnText(method.instructions)}: ${e.message}", e)
|
||||
throw AnalyzerException(
|
||||
e.node,
|
||||
"Error at instruction #$insn ${insnNode.insnText(method.instructions)}: ${e.message}\ncurrent: ${current.dump()}",
|
||||
e
|
||||
)
|
||||
} catch (e: Exception) {
|
||||
throw AnalyzerException(insnNode, "Error at instruction #$insn ${insnNode.insnText(method.instructions)}: ${e.message}", e)
|
||||
throw AnalyzerException(
|
||||
insnNode,
|
||||
"Error at instruction #$insn ${insnNode.insnText(method.instructions)}: ${e.message}\ncurrent: ${current.dump()}",
|
||||
e
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -245,11 +253,39 @@ open class FastMethodAnalyzer<V : Value>(
|
||||
true
|
||||
}
|
||||
else ->
|
||||
oldFrame.merge(frame, interpreter)
|
||||
try {
|
||||
oldFrame.merge(frame, interpreter)
|
||||
} catch (e: AnalyzerException) {
|
||||
throw AnalyzerException(null, "${e.message}\nframe: ${frame.dump()}\noldFrame: ${oldFrame.dump()}")
|
||||
}
|
||||
}
|
||||
if (changes && !queued[dest]) {
|
||||
queued[dest] = true
|
||||
queue[top++] = dest
|
||||
}
|
||||
}
|
||||
|
||||
private fun Frame<V>.dump(): String {
|
||||
return buildString {
|
||||
append("{\n")
|
||||
append(" locals: [\n")
|
||||
for (i in 0 until method.maxLocals) {
|
||||
append(" #$i: ${this@dump.getLocal(i)}\n")
|
||||
}
|
||||
append(" ]\n")
|
||||
val stackSize = this@dump.stackSize
|
||||
append(" stack: size=")
|
||||
append(stackSize)
|
||||
if (stackSize == 0) {
|
||||
append(" []\n")
|
||||
} else {
|
||||
append(" [\n")
|
||||
for (i in 0 until stackSize) {
|
||||
append(" #$i: ${this@dump.getStack(i)}\n")
|
||||
}
|
||||
append(" ]\n")
|
||||
}
|
||||
append("}\n")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+4
-28
@@ -101,22 +101,6 @@ class StoreLoadFrame<V : StoreLoadValue>(val maxLocals: Int) {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
internal val isOpcodeRelevantForStoreLoadAnalysis = BooleanArray(256).also { a ->
|
||||
for (i in Opcodes.ILOAD..Opcodes.ALOAD) a[i] = true
|
||||
for (i in Opcodes.ISTORE..Opcodes.ASTORE) a[i] = true
|
||||
|
||||
// Relevant JumpInsnNode opcodes
|
||||
a[Opcodes.GOTO] = true
|
||||
a[Opcodes.IFNONNULL] = true
|
||||
a[Opcodes.IFNULL] = true
|
||||
for (i in Opcodes.IFEQ..Opcodes.IF_ACMPNE) a[i] = true
|
||||
|
||||
a[Opcodes.LOOKUPSWITCH] = true
|
||||
a[Opcodes.TABLESWITCH] = true
|
||||
}
|
||||
|
||||
|
||||
@Suppress("DuplicatedCode")
|
||||
class FastStoreLoadAnalyzer<V : StoreLoadValue>(
|
||||
private val owner: String,
|
||||
@@ -175,15 +159,11 @@ class FastStoreLoadAnalyzer<V : StoreLoadValue>(
|
||||
}
|
||||
}
|
||||
|
||||
// No need to analyze exception handler code for instructions that just propagate data flow information forward.
|
||||
if (isRelevantNode(insn, insnOpcode)) {
|
||||
handlers[insn]?.forEach { tcb ->
|
||||
val jump = tcb.handler.indexOf()
|
||||
handler.init(f)
|
||||
mergeControlFlowEdge(jump, handler)
|
||||
}
|
||||
handlers[insn]?.forEach { tcb ->
|
||||
val jump = tcb.handler.indexOf()
|
||||
handler.init(f)
|
||||
mergeControlFlowEdge(jump, handler)
|
||||
}
|
||||
|
||||
} catch (e: AnalyzerException) {
|
||||
throw AnalyzerException(e.node, "Error at instruction #$insn ${insnNode.insnText(method.instructions)}: ${e.message}", e)
|
||||
} catch (e: Exception) {
|
||||
@@ -195,10 +175,6 @@ class FastStoreLoadAnalyzer<V : StoreLoadValue>(
|
||||
return frames
|
||||
}
|
||||
|
||||
private fun isRelevantNode(insn: Int, insnOpcode: Int) =
|
||||
isMergeNode[insn] ||
|
||||
insnOpcode >= 0 && isOpcodeRelevantForStoreLoadAnalysis[insnOpcode]
|
||||
|
||||
private fun newFrame(maxLocals: Int) =
|
||||
StoreLoadFrame<V>(maxLocals)
|
||||
|
||||
|
||||
+17
-4
@@ -6,11 +6,15 @@
|
||||
package org.jetbrains.kotlin.codegen.optimization.temporaryVals
|
||||
|
||||
import org.jetbrains.kotlin.codegen.optimization.OptimizationMethodVisitor
|
||||
import org.jetbrains.kotlin.codegen.optimization.common.isMeaningful
|
||||
import org.jetbrains.kotlin.codegen.optimization.common.isStoreOperation
|
||||
import org.jetbrains.kotlin.utils.SmartSet
|
||||
import org.jetbrains.org.objectweb.asm.Opcodes
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
import org.jetbrains.org.objectweb.asm.tree.*
|
||||
import org.jetbrains.org.objectweb.asm.tree.AbstractInsnNode
|
||||
import org.jetbrains.org.objectweb.asm.tree.IincInsnNode
|
||||
import org.jetbrains.org.objectweb.asm.tree.MethodNode
|
||||
import org.jetbrains.org.objectweb.asm.tree.VarInsnNode
|
||||
|
||||
|
||||
// A temporary val is a local variables that is:
|
||||
@@ -61,12 +65,21 @@ class TemporaryValsAnalyzer {
|
||||
}
|
||||
}
|
||||
|
||||
// Some coroutine transformations require exception handler to start from an ASTORE instruction.
|
||||
for (tcb in methodNode.tryCatchBlocks) {
|
||||
val handlerFirstInsn = tcb.handler.next ?: continue
|
||||
if (handlerFirstInsn.opcode == Opcodes.ASTORE) {
|
||||
// Some coroutine transformations require exception handler to start from an ASTORE instruction.
|
||||
var handlerFirstInsn: AbstractInsnNode? = tcb.handler
|
||||
while (handlerFirstInsn != null && !handlerFirstInsn.isMeaningful) {
|
||||
handlerFirstInsn = handlerFirstInsn.next
|
||||
}
|
||||
if (handlerFirstInsn != null && handlerFirstInsn.opcode == Opcodes.ASTORE) {
|
||||
potentiallyTemporaryStores.remove(handlerFirstInsn)
|
||||
}
|
||||
// Don't touch stack spilling at TCB start
|
||||
var insn = tcb.start.previous
|
||||
while (insn != null && insn.isStoreOperation()) {
|
||||
potentiallyTemporaryStores.remove(insn)
|
||||
insn = insn.previous
|
||||
}
|
||||
}
|
||||
|
||||
// If the method is big, and we couldn't eliminate enough temporary variable store candidates,
|
||||
|
||||
+11
-1
@@ -151,7 +151,17 @@ class TemporaryVariablesEliminationTransformer(private val state: GenerationStat
|
||||
val insnList = cfg.methodNode.instructions
|
||||
|
||||
for (tmp in temporaryVals) {
|
||||
if (tmp.loadInsns.size == 1) {
|
||||
if (tmp.loadInsns.isEmpty()) {
|
||||
// Drop unused temporary store
|
||||
val popOpcode = when (tmp.storeInsn.opcode) {
|
||||
Opcodes.ISTORE, Opcodes.FSTORE, Opcodes.ASTORE ->
|
||||
Opcodes.POP
|
||||
else ->
|
||||
Opcodes.POP2
|
||||
}
|
||||
insnList.insertBefore(tmp.storeInsn, InsnNode(popOpcode))
|
||||
insnList.remove(tmp.storeInsn)
|
||||
} else if (tmp.loadInsns.size == 1) {
|
||||
val storeInsn = tmp.storeInsn
|
||||
val loadInsn = tmp.loadInsns[0]
|
||||
|
||||
|
||||
Reference in New Issue
Block a user