[JVM] Move computeExceptionHandlers into base FastAnalyzer

This commit is contained in:
Ivan Kylchik
2023-09-05 16:44:58 +02:00
committed by Space Team
parent 98cc2c839f
commit bf53268453
4 changed files with 39 additions and 59 deletions
@@ -5,6 +5,7 @@
package org.jetbrains.kotlin.codegen.optimization.common
import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.tree.*
import org.jetbrains.org.objectweb.asm.tree.analysis.Frame
@@ -47,6 +48,40 @@ abstract class FastAnalyzer<V : Value, I : Interpreter<V>, F: Frame<V>>(
protected fun AbstractInsnNode.indexOf() =
method.instructions.indexOf(this)
protected fun computeExceptionHandlers(m: MethodNode, forEachInsn: Boolean = true) {
for (tcb in m.tryCatchBlocks) {
if (forEachInsn) computeExceptionHandlersForEachInsn(tcb) else computeExceptionHandlerFast(tcb)
}
}
private fun computeExceptionHandlersForEachInsn(tcb: TryCatchBlockNode) {
var current: AbstractInsnNode = tcb.start
val end = tcb.end
while (current != end) {
if (current.isMeaningful) {
val currentIndex = current.indexOf()
var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[currentIndex]
if (insnHandlers == null) {
insnHandlers = SmartList()
handlers[currentIndex] = insnHandlers
}
insnHandlers.add(tcb)
}
current = current.next
}
}
private fun computeExceptionHandlerFast(tcb: TryCatchBlockNode) {
val start = tcb.start.indexOf()
var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[start]
if (insnHandlers == null) {
insnHandlers = ArrayList()
handlers[start] = insnHandlers
}
insnHandlers.add(tcb)
}
protected fun F.dump(): String {
return buildString {
append("{\n")
@@ -34,7 +34,6 @@
package org.jetbrains.kotlin.codegen.optimization.common
import org.jetbrains.kotlin.codegen.inline.insnText
import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Type
import org.jetbrains.org.objectweb.asm.tree.*
@@ -67,7 +66,7 @@ open class FastMethodAnalyzer<V : Value>
if (nInsns == 0) return frames
checkAssertions()
computeExceptionHandlersForEachInsn(method)
computeExceptionHandlers(method)
val isTcbStart = BooleanArray(nInsns)
for (tcb in method.tryCatchBlocks) {
@@ -195,26 +194,6 @@ open class FastMethodAnalyzer<V : Value>
}
}
private fun computeExceptionHandlersForEachInsn(m: MethodNode) {
for (tcb in m.tryCatchBlocks) {
var current: AbstractInsnNode = tcb.start
val end = tcb.end
while (current != end) {
if (current.isMeaningful) {
val currentIndex = current.indexOf()
var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[currentIndex]
if (insnHandlers == null) {
insnHandlers = SmartList()
handlers[currentIndex] = insnHandlers
}
insnHandlers.add(tcb)
}
current = current.next
}
}
}
/**
* Updates frame at the index [dest] with its old value if provided and previous control flow node frame [frame].
* Reuses old frame when possible and when [canReuse] is true.
@@ -71,7 +71,8 @@ internal open class FastStackAnalyzer<V : Value>(
checkAssertions()
computeExceptionEdges()
// Don't have to visit same exception handler multiple times - we care only about stack state at TCB start.
computeExceptionHandlers(method, forEachInsn = false)
val current = newFrame(method.maxLocals, method.maxStack)
val handler = newFrame(method.maxLocals, method.maxStack)
@@ -188,19 +189,6 @@ internal open class FastStackAnalyzer<V : Value>(
mergeControlFlowEdge(0, current)
}
private fun computeExceptionEdges() {
for (tcb in method.tryCatchBlocks) {
// Don't have to visit same exception handler multiple times - we care only about stack state at TCB start.
val start = tcb.start.indexOf()
var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[start]
if (insnHandlers == null) {
insnHandlers = ArrayList()
handlers[start] = insnHandlers
}
insnHandlers.add(tcb)
}
}
private fun mergeControlFlowEdge(dest: Int, frame: Frame<V>) {
val destFrame = frames[dest]
if (destFrame == null) {
@@ -35,8 +35,6 @@ package org.jetbrains.kotlin.codegen.optimization.temporaryVals
import org.jetbrains.kotlin.codegen.inline.insnText
import org.jetbrains.kotlin.codegen.optimization.common.FastAnalyzer
import org.jetbrains.kotlin.codegen.optimization.common.isMeaningful
import org.jetbrains.kotlin.utils.SmartList
import org.jetbrains.org.objectweb.asm.Opcodes
import org.jetbrains.org.objectweb.asm.Opcodes.API_VERSION
import org.jetbrains.org.objectweb.asm.Type
@@ -119,7 +117,7 @@ class FastStoreLoadAnalyzer<V : StoreLoadValue>(
if (nInsns == 0) return frames
checkAssertions()
computeExceptionHandlersForEachInsn(method)
computeExceptionHandlers(method)
initMergeNodes()
val current = newFrame(method.maxLocals)
@@ -188,26 +186,6 @@ class FastStoreLoadAnalyzer<V : StoreLoadValue>(
mergeControlFlowEdge(insnNode.label.indexOf(), current)
}
private fun computeExceptionHandlersForEachInsn(m: MethodNode) {
for (tcb in m.tryCatchBlocks) {
var current: AbstractInsnNode = tcb.start
val end = tcb.end
while (current != end) {
if (current.isMeaningful) {
val currentIndex = current.indexOf()
var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[currentIndex]
if (insnHandlers == null) {
insnHandlers = SmartList()
handlers[currentIndex] = insnHandlers
}
insnHandlers.add(tcb)
}
current = current.next
}
}
}
private fun initMergeNodes() {
for (insn in method.instructions) {
when (insn.type) {