daniellansun commented on code in PR #2707:
URL: https://github.com/apache/groovy/pull/2707#discussion_r3604084417
##########
src/test/groovy/org/codehaus/groovy/classgen/asm/PeepholeOptimizingMethodVisitorTest.groovy:
##########
@@ -329,16 +329,91 @@ final class PeepholeOptimizingMethodVisitorTest {
assert opcodeLines(bytecode) == ['IINC 0 1', 'RETURN']
}
+ @Test
+ void removesLoadAndAttachedCheckcastBeforePop() {
+ def bytecode = sequenceFor('(Ljava/lang/Object;)V') {
+ visitVarInsn(Opcodes.ALOAD, 0)
+ visitTypeInsn(Opcodes.CHECKCAST, 'java/lang/String')
+ visitInsn(Opcodes.POP)
+ visitInsn(RETURN)
+ }
+
+ // ALOAD + CHECKCAST are both dead once the value is discarded.
+ assert opcodeLines(bytecode) == ['RETURN']
+ }
+
@Test
void removesStandaloneCheckcastBeforePop() {
def bytecode = sequenceFor('(Ljava/lang/Object;)V') {
visitVarInsn(Opcodes.ALOAD, 0)
+ visitInsn(Opcodes.NOP) // force the load to flush so CHECKCAST is
standalone
visitTypeInsn(Opcodes.CHECKCAST, 'java/lang/String')
visitInsn(Opcodes.POP)
visitInsn(RETURN)
}
- assert opcodeLines(bytecode) == ['ALOAD 0', 'POP', 'RETURN']
+ assert opcodeLines(bytecode) == ['ALOAD 0', 'NOP', 'POP', 'RETURN']
+ }
+
+ @Test
+ void rewritesNullComparisonsAgainstAconstNull() {
+ def nullComparisons = [
+ (Opcodes.IF_ACMPEQ): 'IFNULL',
+ (Opcodes.IF_ACMPNE): 'IFNONNULL',
+ ]
+
+ nullComparisons.each { opcode, expected ->
+ def label = new Label()
+ def bytecode = sequenceFor('(Ljava/lang/Object;)V') {
+ visitVarInsn(Opcodes.ALOAD, 0)
+ visitInsn(Opcodes.ACONST_NULL)
+ visitJumpInsn(opcode, label)
+ visitInsn(RETURN)
+ visitLabel(label)
+ visitInsn(RETURN)
+ }
+ def lines = opcodeLines(bytecode)
+
+ assert lines.size() == 4
+ assert lines[0] == 'ALOAD 0'
+ assert lines[1].startsWith(expected)
+ assert lines[2..3] == ['RETURN', 'RETURN']
+ assert !lines[1].startsWith(Printer.OPCODES[opcode])
+ }
+ }
+
+ @Test
+ void removesBareDupBeforeMatchingPop() {
+ def bytecode = sequenceFor {
+ visitInsn(Opcodes.ICONST_1)
+ visitInsn(Opcodes.DUP)
+ visitInsn(Opcodes.POP)
+ visitLdcInsn(2L)
+ visitInsn(Opcodes.DUP2)
+ visitInsn(Opcodes.POP2)
+ visitInsn(RETURN)
+ }
+
+ assert opcodeLines(bytecode) == [
+ 'ICONST_1',
+ 'LDC 2L',
+ 'RETURN',
Review Comment:
Agreed — once DUP/POP is gone the constants are already flushed out of the
single load window, so a second pass / wider window would be needed to drop
them before void RETURN. Happy as a follow-up; not this PR.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]