[JVM] Drop insnsArray from Fast...Analyzer classes

We are using useless `toArray` can that we can avoid.
This commit is contained in:
Ivan Kylchik
2023-08-23 11:36:39 +02:00
committed by Space Team
parent cf01d2970f
commit d8e682ab30
4 changed files with 53 additions and 49 deletions
@@ -54,7 +54,6 @@ open class FastMethodAnalyzer<V : Value>
private val interpreter: Interpreter<V>, private val interpreter: Interpreter<V>,
private val pruneExceptionEdges: Boolean = false private val pruneExceptionEdges: Boolean = false
) { ) {
private val insnsArray = method.instructions.toArray()
private val nInsns = method.instructions.size() private val nInsns = method.instructions.size()
private val isMergeNode = findMergeNodes(method) private val isMergeNode = findMergeNodes(method)
@@ -186,7 +185,7 @@ open class FastMethodAnalyzer<V : Value>
frames[insn.indexOf()] frames[insn.indexOf()]
private fun checkAssertions() { private fun checkAssertions() {
if (insnsArray.any { it.opcode == Opcodes.JSR || it.opcode == Opcodes.RET }) if (method.instructions.any { it.opcode == Opcodes.JSR || it.opcode == Opcodes.RET })
throw AssertionError("Subroutines are deprecated since Java 6") throw AssertionError("Subroutines are deprecated since Java 6")
} }
@@ -222,16 +221,20 @@ open class FastMethodAnalyzer<V : Value>
private fun computeExceptionHandlersForEachInsn(m: MethodNode) { private fun computeExceptionHandlersForEachInsn(m: MethodNode) {
for (tcb in m.tryCatchBlocks) { for (tcb in m.tryCatchBlocks) {
val begin = tcb.start.indexOf() var current: AbstractInsnNode = tcb.start
val end = tcb.end.indexOf() val end = tcb.end
for (j in begin until end) {
if (!insnsArray[j].isMeaningful) continue while (current != end) {
var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[j] if (current.isMeaningful) {
if (insnHandlers == null) { val currentIndex = current.indexOf()
insnHandlers = SmartList() var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[currentIndex]
handlers[j] = insnHandlers if (insnHandlers == null) {
insnHandlers = SmartList()
handlers[currentIndex] = insnHandlers
}
insnHandlers.add(tcb)
} }
insnHandlers.add(tcb) current = current.next
} }
} }
} }
@@ -51,8 +51,7 @@ internal open class FastStackAnalyzer<V : Value>(
val method: MethodNode, val method: MethodNode,
protected val interpreter: Interpreter<V> protected val interpreter: Interpreter<V>
) { ) {
protected val insnsArray: Array<AbstractInsnNode> = method.instructions.toArray() private val nInsns = method.instructions.size()
private val nInsns = insnsArray.size
private val frames: Array<Frame<V>?> = arrayOfNulls(nInsns) private val frames: Array<Frame<V>?> = arrayOfNulls(nInsns)
@@ -63,7 +62,7 @@ internal open class FastStackAnalyzer<V : Value>(
protected open fun newFrame(nLocals: Int, nStack: Int): Frame<V> = Frame(nLocals, nStack) protected open fun newFrame(nLocals: Int, nStack: Int): Frame<V> = Frame(nLocals, nStack)
protected open fun visitControlFlowEdge(insn: Int, successor: Int): Boolean = true protected open fun visitControlFlowEdge(insnNode: AbstractInsnNode, successor: Int): Boolean = true
protected open fun visitControlFlowExceptionEdge(insn: Int, successor: Int): Boolean = true protected open fun visitControlFlowExceptionEdge(insn: Int, successor: Int): Boolean = true
@@ -92,7 +91,7 @@ internal open class FastStackAnalyzer<V : Value>(
try { try {
if (insnType == AbstractInsnNode.LABEL || insnType == AbstractInsnNode.LINE || insnType == AbstractInsnNode.FRAME) { if (insnType == AbstractInsnNode.LABEL || insnType == AbstractInsnNode.LINE || insnType == AbstractInsnNode.FRAME) {
visitNopInsn(f, insn) visitNopInsn(insnNode, f, insn)
} else { } else {
current.init(f) current.init(f)
if (insnOpcode != Opcodes.RETURN) { if (insnOpcode != Opcodes.RETURN) {
@@ -104,11 +103,11 @@ internal open class FastStackAnalyzer<V : Value>(
insnType == AbstractInsnNode.JUMP_INSN -> insnType == AbstractInsnNode.JUMP_INSN ->
visitJumpInsnNode(insnNode as JumpInsnNode, current, insn, insnOpcode) visitJumpInsnNode(insnNode as JumpInsnNode, current, insn, insnOpcode)
insnType == AbstractInsnNode.LOOKUPSWITCH_INSN -> insnType == AbstractInsnNode.LOOKUPSWITCH_INSN ->
visitLookupSwitchInsnNode(insnNode as LookupSwitchInsnNode, current, insn) visitLookupSwitchInsnNode(insnNode as LookupSwitchInsnNode, current)
insnType == AbstractInsnNode.TABLESWITCH_INSN -> insnType == AbstractInsnNode.TABLESWITCH_INSN ->
visitTableSwitchInsnNode(insnNode as TableSwitchInsnNode, current, insn) visitTableSwitchInsnNode(insnNode as TableSwitchInsnNode, current)
insnOpcode != Opcodes.ATHROW && (insnOpcode < Opcodes.IRETURN || insnOpcode > Opcodes.RETURN) -> insnOpcode != Opcodes.ATHROW && (insnOpcode < Opcodes.IRETURN || insnOpcode > Opcodes.RETURN) ->
visitOpInsn(current, insn) visitOpInsn(insnNode, current, insn)
else -> { else -> {
} }
} }
@@ -142,17 +141,21 @@ internal open class FastStackAnalyzer<V : Value>(
frames[insn.indexOf()] frames[insn.indexOf()]
private fun checkAssertions() { private fun checkAssertions() {
if (insnsArray.any { it.opcode == Opcodes.JSR || it.opcode == Opcodes.RET }) if (method.instructions.any { it.opcode == Opcodes.JSR || it.opcode == Opcodes.RET })
throw AssertionError("Subroutines are deprecated since Java 6") throw AssertionError("Subroutines are deprecated since Java 6")
} }
private fun visitOpInsn(current: Frame<V>, insn: Int) { private fun visitOpInsn(insnNode: AbstractInsnNode, current: Frame<V>, insn: Int) {
processControlFlowEdge(current, insn, insn + 1) processControlFlowEdge(current, insnNode, insn + 1)
} }
private fun visitTableSwitchInsnNode(insnNode: TableSwitchInsnNode, current: Frame<V>, insn: Int) { private fun visitNopInsn(insnNode: AbstractInsnNode, f: Frame<V>, insn: Int) {
processControlFlowEdge(f, insnNode, insn + 1)
}
private fun visitTableSwitchInsnNode(insnNode: TableSwitchInsnNode, current: Frame<V>) {
var jump = insnNode.dflt.indexOf() var jump = insnNode.dflt.indexOf()
processControlFlowEdge(current, insn, jump) processControlFlowEdge(current, insnNode, jump)
// In most cases order of visiting switch labels should not matter // In most cases order of visiting switch labels should not matter
// The only one is a tableswitch being added in the beginning of coroutine method, these switch' labels may lead // The only one is a tableswitch being added in the beginning of coroutine method, these switch' labels may lead
// in the middle of try/catch block, and FixStackAnalyzer is not ready for this (trying to restore stack before it was saved) // in the middle of try/catch block, and FixStackAnalyzer is not ready for this (trying to restore stack before it was saved)
@@ -160,33 +163,29 @@ internal open class FastStackAnalyzer<V : Value>(
// Using 'reversed' is because nodes are processed in LIFO order // Using 'reversed' is because nodes are processed in LIFO order
for (label in insnNode.labels.reversed()) { for (label in insnNode.labels.reversed()) {
jump = label.indexOf() jump = label.indexOf()
processControlFlowEdge(current, insn, jump) processControlFlowEdge(current, insnNode, jump)
} }
} }
private fun visitLookupSwitchInsnNode(insnNode: LookupSwitchInsnNode, current: Frame<V>, insn: Int) { private fun visitLookupSwitchInsnNode(insnNode: LookupSwitchInsnNode, current: Frame<V>) {
var jump = insnNode.dflt.indexOf() var jump = insnNode.dflt.indexOf()
processControlFlowEdge(current, insn, jump) processControlFlowEdge(current, insnNode, jump)
for (label in insnNode.labels) { for (label in insnNode.labels) {
jump = label.indexOf() jump = label.indexOf()
processControlFlowEdge(current, insn, jump) processControlFlowEdge(current, insnNode, jump)
} }
} }
private fun visitJumpInsnNode(insnNode: JumpInsnNode, current: Frame<V>, insn: Int, insnOpcode: Int) { private fun visitJumpInsnNode(insnNode: JumpInsnNode, current: Frame<V>, insn: Int, insnOpcode: Int) {
if (insnOpcode != Opcodes.GOTO) { if (insnOpcode != Opcodes.GOTO) {
processControlFlowEdge(current, insn, insn + 1) processControlFlowEdge(current, insnNode, insn + 1)
} }
val jump = insnNode.label.indexOf() val jump = insnNode.label.indexOf()
processControlFlowEdge(current, insn, jump) processControlFlowEdge(current, insnNode, jump)
} }
private fun visitNopInsn(f: Frame<V>, insn: Int) { private fun processControlFlowEdge(current: Frame<V>, insnNode: AbstractInsnNode, jump: Int) {
processControlFlowEdge(f, insn, insn + 1) if (visitControlFlowEdge(insnNode, jump)) {
}
private fun processControlFlowEdge(current: Frame<V>, insn: Int, jump: Int) {
if (visitControlFlowEdge(insn, jump)) {
mergeControlFlowEdge(jump, current) mergeControlFlowEdge(jump, current)
} }
} }
@@ -95,9 +95,8 @@ internal class FixStackAnalyzer(
val spilledStacks = hashMapOf<AbstractInsnNode, List<FixStackValue>>() val spilledStacks = hashMapOf<AbstractInsnNode, List<FixStackValue>>()
var maxExtraStackSize = 0; private set var maxExtraStackSize = 0; private set
override fun visitControlFlowEdge(insn: Int, successor: Int): Boolean { override fun visitControlFlowEdge(insnNode: AbstractInsnNode, successor: Int): Boolean {
if (!skipBreakContinueGotoEdges) return true if (!skipBreakContinueGotoEdges) return true
val insnNode = insnsArray[insn]
return !(insnNode is JumpInsnNode && context.breakContinueGotoNodes.contains(insnNode)) return !(insnNode is JumpInsnNode && context.breakContinueGotoNodes.contains(insnNode))
} }
@@ -107,7 +107,6 @@ class FastStoreLoadAnalyzer<V : StoreLoadValue>(
private val method: MethodNode, private val method: MethodNode,
private val interpreter: StoreLoadInterpreter<V> private val interpreter: StoreLoadInterpreter<V>
) { ) {
private val insnsArray = method.instructions.toArray()
private val nInsns = method.instructions.size() private val nInsns = method.instructions.size()
private val isMergeNode = BooleanArray(nInsns) private val isMergeNode = BooleanArray(nInsns)
@@ -182,7 +181,7 @@ class FastStoreLoadAnalyzer<V : StoreLoadValue>(
method.instructions.indexOf(this) method.instructions.indexOf(this)
private fun checkAssertions() { private fun checkAssertions() {
if (insnsArray.any { it.opcode == Opcodes.JSR || it.opcode == Opcodes.RET }) if (method.instructions.any { it.opcode == Opcodes.JSR || it.opcode == Opcodes.RET })
throw AssertionError("Subroutines are deprecated since Java 6") throw AssertionError("Subroutines are deprecated since Java 6")
} }
@@ -209,22 +208,26 @@ class FastStoreLoadAnalyzer<V : StoreLoadValue>(
private fun computeExceptionHandlersForEachInsn(m: MethodNode) { private fun computeExceptionHandlersForEachInsn(m: MethodNode) {
for (tcb in m.tryCatchBlocks) { for (tcb in m.tryCatchBlocks) {
val begin = tcb.start.indexOf() var current: AbstractInsnNode = tcb.start
val end = tcb.end.indexOf() val end = tcb.end
for (j in begin until end) {
if (!insnsArray[j].isMeaningful) continue while (current != end) {
var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[j] if (current.isMeaningful) {
if (insnHandlers == null) { val currentIndex = current.indexOf()
insnHandlers = SmartList() var insnHandlers: MutableList<TryCatchBlockNode>? = handlers[currentIndex]
handlers[j] = insnHandlers if (insnHandlers == null) {
insnHandlers = SmartList()
handlers[currentIndex] = insnHandlers
}
insnHandlers.add(tcb)
} }
insnHandlers.add(tcb) current = current.next
} }
} }
} }
private fun initMergeNodes() { private fun initMergeNodes() {
for (insn in insnsArray) { for (insn in method.instructions) {
when (insn.type) { when (insn.type) {
AbstractInsnNode.JUMP_INSN -> { AbstractInsnNode.JUMP_INSN -> {
val jumpInsn = insn as JumpInsnNode val jumpInsn = insn as JumpInsnNode