diff options
author | Stefan Zeiger <szeiger@novocode.com> | 2016-02-17 18:06:24 +0100 |
---|---|---|
committer | Stefan Zeiger <szeiger@novocode.com> | 2016-02-17 18:06:24 +0100 |
commit | be3f0e3db699aef764b1f61f95be0871ba7a715a (patch) | |
tree | 1f90f9412f7c377f528169eec06a0b7c2f5b3cdb | |
parent | 4e39a79e726eb2a499dd4948ef03e14b73f2e4e3 (diff) | |
parent | 60bcaf19f90f76722658e2b5b2bb3d8a3009375c (diff) | |
download | scala-be3f0e3db699aef764b1f61f95be0871ba7a715a.tar.gz scala-be3f0e3db699aef764b1f61f95be0871ba7a715a.tar.bz2 scala-be3f0e3db699aef764b1f61f95be0871ba7a715a.zip |
Merge pull request #4969 from lrytz/valPatternOpt
Tests for optimizing val patterns
9 files changed, 201 insertions, 186 deletions
diff --git a/src/partest-extras/scala/tools/partest/ASMConverters.scala b/src/partest-extras/scala/tools/partest/ASMConverters.scala index d990160ce8..a3d849a9c1 100644 --- a/src/partest-extras/scala/tools/partest/ASMConverters.scala +++ b/src/partest-extras/scala/tools/partest/ASMConverters.scala @@ -43,6 +43,23 @@ object ASMConverters { case i: Invoke => i.name case i => i.opcode } + + def summaryText: String = { + def comment(i: Instruction) = i match { + case j: Jump => s" /*${j.label.offset}*/" + case l: Label => s" /*${l.offset}*/" + case _ => "" + } + dropNonOp.map({ + case i: Invoke => s""""${i.name}"""" + case ins => opcodeToString(ins.opcode, ins.opcode) + comment(ins) + }).mkString("List(", ", ", ")") + } + } + + def opcodeToString(op: Int, default: Any = "?"): String = { + import scala.tools.asm.util.Printer.OPCODES + if (OPCODES.isDefinedAt(op)) OPCODES(op) else default.toString } sealed abstract class Instruction extends Product { @@ -50,12 +67,9 @@ object ASMConverters { // toString such that the first field, "opcode: Int", is printed textually. final override def toString() = { - import scala.tools.asm.util.Printer.OPCODES - def opString(op: Int) = if (OPCODES.isDefinedAt(op)) OPCODES(op) else "?" val printOpcode = opcode != -1 - productPrefix + ( - if (printOpcode) Iterator(opString(opcode)) ++ productIterator.drop(1) + if (printOpcode) Iterator(opcodeToString(opcode)) ++ productIterator.drop(1) else productIterator ).mkString("(", ", ", ")") } diff --git a/test/junit/scala/issues/BytecodeTest.scala b/test/junit/scala/issues/BytecodeTest.scala index ca1b33ed20..18f8b44391 100644 --- a/test/junit/scala/issues/BytecodeTest.scala +++ b/test/junit/scala/issues/BytecodeTest.scala @@ -150,21 +150,21 @@ class BytecodeTest extends ClearAfterClass { val List(c) = compileClasses(compiler)(code) // t1: no unnecessary GOTOs - assertSameCode(getSingleMethod(c, "t1").instructions.dropNonOp, List( + assertSameCode(getSingleMethod(c, "t1"), List( VarOp(ILOAD, 1), Jump(IFEQ, Label(6)), Op(ICONST_1), Jump(GOTO, Label(9)), Label(6), Op(ICONST_2), Label(9), Op(IRETURN))) // t2: no unnecessary GOTOs - assertSameCode(getSingleMethod(c, "t2").instructions.dropNonOp, List( + assertSameCode(getSingleMethod(c, "t2"), List( VarOp(ILOAD, 1), IntOp(SIPUSH, 393), Jump(IF_ICMPNE, Label(7)), Op(ICONST_1), Jump(GOTO, Label(10)), Label(7), Op(ICONST_2), Label(10), Op(IRETURN))) // t3: Array == is translated to reference equality, AnyRef == to null checks and equals - assertSameCode(getSingleMethod(c, "t3").instructions.dropNonOp, List( + assertSameCode(getSingleMethod(c, "t3"), List( // Array == VarOp(ALOAD, 1), VarOp(ALOAD, 2), Jump(IF_ACMPEQ, Label(23)), // AnyRef == @@ -183,13 +183,13 @@ class BytecodeTest extends ClearAfterClass { Label(13), Op(IRETURN)) // t4: one side is known null, so just a null check on the other - assertSameCode(getSingleMethod(c, "t4").instructions.dropNonOp, t4t5) + assertSameCode(getSingleMethod(c, "t4"), t4t5) // t5: one side known null, so just a null check on the other - assertSameCode(getSingleMethod(c, "t5").instructions.dropNonOp, t4t5) + assertSameCode(getSingleMethod(c, "t5"), t4t5) // t6: no unnecessary GOTOs - assertSameCode(getSingleMethod(c, "t6").instructions.dropNonOp, List( + assertSameCode(getSingleMethod(c, "t6"), List( VarOp(ILOAD, 1), IntOp(BIPUSH, 10), Jump(IF_ICMPNE, Label(7)), VarOp(ILOAD, 2), Jump(IFNE, Label(12)), Label(7), VarOp(ILOAD, 1), Op(ICONST_1), Jump(IF_ICMPEQ, Label(16)), @@ -201,7 +201,7 @@ class BytecodeTest extends ClearAfterClass { assertInvoke(getSingleMethod(c, "t7"), "scala/runtime/BoxesRunTime", "equals") // t8: no null checks invoking equals on modules and constants - assertSameCode(getSingleMethod(c, "t8").instructions.dropNonOp, List( + assertSameCode(getSingleMethod(c, "t8"), List( Field(GETSTATIC, "scala/collection/immutable/Nil$", "MODULE$", "Lscala/collection/immutable/Nil$;"), VarOp(ALOAD, 1), Invoke(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false), Jump(IFNE, Label(10)), Ldc(LDC, ""), VarOp(ALOAD, 1), Invoke(INVOKEVIRTUAL, "java/lang/Object", "equals", "(Ljava/lang/Object;)Z", false), Jump(IFNE, Label(14)), Label(10), Op(ICONST_1), Jump(GOTO, Label(17)), diff --git a/test/junit/scala/issues/OptimizedBytecodeTest.scala b/test/junit/scala/issues/OptimizedBytecodeTest.scala index 3c6f1ff25e..03c0c64442 100644 --- a/test/junit/scala/issues/OptimizedBytecodeTest.scala +++ b/test/junit/scala/issues/OptimizedBytecodeTest.scala @@ -36,7 +36,7 @@ class OptimizedBytecodeTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(compiler)(code) - assertSameCode(getSingleMethod(c, "t").instructions.dropNonOp, List(Label(0), Jump(GOTO, Label(0)))) + assertSameCode(getSingleMethod(c, "t"), List(Label(0), Jump(GOTO, Label(0)))) } @Test @@ -55,17 +55,10 @@ class OptimizedBytecodeTest extends ClearAfterClass { """.stripMargin val List(c) = compileClasses(compiler)(code) - assertEquals( - getSingleMethod(c, "t").instructions.summary, - List(LDC, ASTORE, ALOAD /*0*/, ALOAD /*1*/, "C$$$anonfun$1", IRETURN)) - - assertEquals( - getSingleMethod(c, "C$$$anonfun$1").instructions.summary, - List(LDC, "C$$$anonfun$2", IRETURN)) - - assertEquals( - getSingleMethod(c, "C$$$anonfun$2").instructions.summary, - List(-1 /*A*/, GOTO /*A*/)) + assertSameSummary(getSingleMethod(c, "t"), List( + LDC, ASTORE, ALOAD /*0*/, ALOAD /*1*/, "C$$$anonfun$1", IRETURN)) + assertSameSummary(getSingleMethod(c, "C$$$anonfun$1"), List(LDC, "C$$$anonfun$2", IRETURN)) + assertSameSummary(getSingleMethod(c, "C$$$anonfun$2"), List(-1 /*A*/, GOTO /*A*/)) } @Test @@ -87,9 +80,7 @@ class OptimizedBytecodeTest extends ClearAfterClass { |} """.stripMargin val List(c, t, tMod) = compileClasses(compiler)(code, allowMessage = _.msg.contains("not be exhaustive")) - assertEquals( - getSingleMethod(c, "t").instructions.summary, - List(GETSTATIC, "$qmark$qmark$qmark", ATHROW)) + assertSameSummary(getSingleMethod(c, "t"), List(GETSTATIC, "$qmark$qmark$qmark", ATHROW)) } @Test @@ -235,9 +226,7 @@ class OptimizedBytecodeTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(compiler)(code) - assertEquals( - getSingleMethod(c, "t").instructions.summary, - List( + assertSameSummary(getSingleMethod(c, "t"), List( ALOAD /*1*/, INSTANCEOF /*Some*/, IFNE /*A*/, ALOAD /*0*/, "getInt", POP, -1 /*A*/, BIPUSH, IRETURN)) @@ -256,13 +245,11 @@ class OptimizedBytecodeTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(compiler)(code) - assertEquals( - getSingleMethod(c, "t").instructions.summary, - List( - -1 /*A*/, ILOAD /*1*/, TABLESWITCH, - -1, ALOAD, "pr", RETURN, - -1, ALOAD, "pr", RETURN, - -1, ILOAD, ICONST_2, ISUB, ISTORE, GOTO /*A*/)) + assertSameSummary(getSingleMethod(c, "t"), List( + -1 /*A*/, ILOAD /*1*/, TABLESWITCH, + -1, ALOAD, "pr", RETURN, + -1, ALOAD, "pr", RETURN, + -1, ILOAD, ICONST_2, ISUB, ISTORE, GOTO /*A*/)) } @Test @@ -283,12 +270,10 @@ class OptimizedBytecodeTest extends ClearAfterClass { val cls = compileClassesSeparately(List(c1, c2), extraArgs = OptimizedBytecodeTest.args) val c = cls.find(_.name == "C").get - assertEquals( - getSingleMethod(c, "t").instructions.summary, - List( - GETSTATIC, IFNONNULL, ACONST_NULL, ATHROW, // module load and null checks not yet eliminated - -1, ICONST_1, GETSTATIC, IFNONNULL, ACONST_NULL, ATHROW, - -1, ICONST_2, IADD, IRETURN)) + assertSameSummary(getSingleMethod(c, "t"), List( + GETSTATIC, IFNONNULL, ACONST_NULL, ATHROW, // module load and null checks not yet eliminated + -1, ICONST_1, GETSTATIC, IFNONNULL, ACONST_NULL, ATHROW, + -1, ICONST_2, IADD, IRETURN)) } @Test diff --git a/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala b/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala index 0d353e930e..fe43ed2f6a 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/CodeGenTools.scala @@ -164,8 +164,18 @@ object CodeGenTools { convertMethod(m) } + def assertSameCode(method: Method, expected: List[Instruction]): Unit = assertSameCode(method.instructions.dropNonOp, expected) def assertSameCode(actual: List[Instruction], expected: List[Instruction]): Unit = { - assertTrue(s"\nExpected: $expected\nActual : $actual", actual === expected) + assert(actual === expected, s"\nExpected: $expected\nActual : $actual") + } + + def assertSameSummary(method: Method, expected: List[Any]): Unit = assertSameSummary(method.instructions, expected) + def assertSameSummary(actual: List[Instruction], expected: List[Any]): Unit = { + def expectedString = expected.map({ + case s: String => s""""$s"""" + case i: Int => opcodeToString(i, i) + }).mkString("List(", ", ", ")") + assert(actual.summary == expected, s"\nFound : ${actual.summaryText}\nExpected: $expectedString") } def assertNoInvoke(m: Method): Unit = assertNoInvoke(m.instructions) @@ -181,6 +191,21 @@ object CodeGenTools { }, l.stringLines) } + def assertDoesNotInvoke(m: Method, method: String): Unit = assertDoesNotInvoke(m.instructions, method) + def assertDoesNotInvoke(l: List[Instruction], method: String): Unit = { + assert(!l.exists { + case i: Invoke => i.name == method + case _ => false + }, l.stringLines) + } + + def assertInvokedMethods(m: Method, expected: List[String]): Unit = assertInvokedMethods(m.instructions, expected) + def assertInvokedMethods(l: List[Instruction], expected: List[String]): Unit = { + def quote(l: List[String]) = l.map(s => s""""$s"""").mkString("List(", ", ", ")") + val actual = l collect { case i: Invoke => i.owner + "." + i.name } + assert(actual == expected, s"\nFound : ${quote(actual)}\nExpected: ${quote(expected)}") + } + def getSingleMethod(classNode: ClassNode, name: String): Method = convertMethod(classNode.methods.asScala.toList.find(_.name == name).get) diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/ClosureOptimizerTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/ClosureOptimizerTest.scala index b314643fb1..a0b9d6b4ed 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/ClosureOptimizerTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/ClosureOptimizerTest.scala @@ -82,7 +82,7 @@ class ClosureOptimizerTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(compiler)(code) - assertSameCode(getSingleMethod(c, "t").instructions.dropNonOp, + assertSameCode(getSingleMethod(c, "t"), List(VarOp(ALOAD, 1), Invoke(INVOKEVIRTUAL, "scala/collection/immutable/List", "head", "()Ljava/lang/Object;", false), TypeOp(CHECKCAST, "java/lang/String"), Invoke(INVOKESTATIC, "C", "C$$$anonfun$1", "(Ljava/lang/String;)Ljava/lang/String;", false), Op(ARETURN))) @@ -103,7 +103,6 @@ class ClosureOptimizerTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(compiler)(code) - assertEquals(getSingleMethod(c, "t").instructions.summary, - List(NEW, DUP, LDC, "<init>", ATHROW)) + assertSameSummary(getSingleMethod(c, "t"), List(NEW, DUP, LDC, "<init>", ATHROW)) } } diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala index 54eddc868d..2c8f5e794e 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/InlinerTest.scala @@ -100,7 +100,7 @@ class InlinerTest extends ClearAfterClass { val g = inlineTest(code) val gConv = convertMethod(g) - assertSameCode(gConv.instructions.dropNonOp, + assertSameCode(gConv, List( VarOp(ALOAD, 0), VarOp(ASTORE, 1), // store this Op(ICONST_1), VarOp(ISTORE, 2), Jump(GOTO, Label(10)), // store return value @@ -144,10 +144,10 @@ class InlinerTest extends ClearAfterClass { VarOp(ALOAD, 2), Op(ATHROW)) - assertSameCode(convertMethod(g).instructions.dropNonOp, gBeforeLocalOpt) + assertSameCode(convertMethod(g), gBeforeLocalOpt) compiler.genBCode.bTypes.localOpt.methodOptimizations(g, "C") - assertSameCode(convertMethod(g).instructions.dropNonOp, invokeQQQ :+ Op(ATHROW)) + assertSameCode(convertMethod(g), invokeQQQ :+ Op(ATHROW)) } @Test @@ -1313,60 +1313,40 @@ class InlinerTest extends ClearAfterClass { """.stripMargin val List(c, _, _) = compile(code) - assertEquals(getSingleMethod(c, "t1").instructions.summary, - List(BIPUSH, "C$$$anonfun$1", IRETURN)) - - assertEquals(getSingleMethod(c, "t1a").instructions.summary, - List(LCONST_1, "C$$$anonfun$2", IRETURN)) - - assertEquals(getSingleMethod(c, "t2").instructions.summary, List( - ICONST_1, ICONST_2, "C$$$anonfun$3",IRETURN)) + assertSameSummary(getSingleMethod(c, "t1"), List(BIPUSH, "C$$$anonfun$1", IRETURN)) + assertSameSummary(getSingleMethod(c, "t1a"), List(LCONST_1, "C$$$anonfun$2", IRETURN)) + assertSameSummary(getSingleMethod(c, "t2"), List(ICONST_1, ICONST_2, "C$$$anonfun$3",IRETURN)) // val a = new ValKl(n); new ValKl(anonfun(a.x)).x // value class instantiation-extraction should be optimized by boxing elim - assertEquals(getSingleMethod(c, "t3").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t3"), List( NEW, DUP, ICONST_1, "<init>", ASTORE, NEW, DUP, ALOAD, "x", "C$$$anonfun$4", "<init>", "x", IRETURN)) - assertEquals(getSingleMethod(c, "t4").instructions.summary, List( - BIPUSH, "C$$$anonfun$5", "boxToInteger", ARETURN)) - - assertEquals(getSingleMethod(c, "t4a").instructions.summary, List( - ICONST_1, LDC, "C$$$anonfun$6", LRETURN)) - - assertEquals(getSingleMethod(c, "t5").instructions.summary, List( - BIPUSH, ICONST_3, "C$$$anonfun$7", "boxToInteger", ARETURN)) - - assertEquals(getSingleMethod(c, "t5a").instructions.summary, List( - BIPUSH, BIPUSH, I2B, "C$$$anonfun$8", IRETURN)) - - assertEquals(getSingleMethod(c, "t6").instructions.summary, List( - BIPUSH, "C$$$anonfun$9", RETURN)) - - assertEquals(getSingleMethod(c, "t7").instructions.summary, List( - ICONST_1, "C$$$anonfun$10", RETURN)) - - assertEquals(getSingleMethod(c, "t8").instructions.summary, List( - ICONST_1, LDC, "C$$$anonfun$11", LRETURN)) - - assertEquals(getSingleMethod(c, "t9").instructions.summary, List( - ICONST_1, "boxToInteger", "C$$$anonfun$12", RETURN)) + assertSameSummary(getSingleMethod(c, "t4"), List(BIPUSH, "C$$$anonfun$5", "boxToInteger", ARETURN)) + assertSameSummary(getSingleMethod(c, "t4a"), List(ICONST_1, LDC, "C$$$anonfun$6", LRETURN)) + assertSameSummary(getSingleMethod(c, "t5"), List(BIPUSH, ICONST_3, "C$$$anonfun$7", "boxToInteger", ARETURN)) + assertSameSummary(getSingleMethod(c, "t5a"), List(BIPUSH, BIPUSH, I2B, "C$$$anonfun$8", IRETURN)) + assertSameSummary(getSingleMethod(c, "t6"), List(BIPUSH, "C$$$anonfun$9", RETURN)) + assertSameSummary(getSingleMethod(c, "t7"), List(ICONST_1, "C$$$anonfun$10", RETURN)) + assertSameSummary(getSingleMethod(c, "t8"), List(ICONST_1, LDC, "C$$$anonfun$11", LRETURN)) + assertSameSummary(getSingleMethod(c, "t9"), List(ICONST_1, "boxToInteger", "C$$$anonfun$12", RETURN)) // t9a inlines Range.foreach, which is quite a bit of code, so just testing the core assertInvoke(getSingleMethod(c, "t9a"), "C", "C$$$anonfun$13") - assert(getSingleMethod(c, "t9a").instructions.summary.contains("boxToInteger")) + assertInvoke(getSingleMethod(c, "t9a"), "scala/runtime/BoxesRunTime", "boxToInteger") - assertEquals(getSingleMethod(c, "t10").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t10"), List( ICONST_1, ISTORE, ALOAD, ILOAD, "C$$$anonfun$14", RETURN)) // t10a inlines Range.foreach assertInvoke(getSingleMethod(c, "t10a"), "C", "C$$$anonfun$15") - assert(!getSingleMethod(c, "t10a").instructions.summary.contains("boxToInteger")) + assertDoesNotInvoke(getSingleMethod(c, "t10a"), "boxToInteger") } @Test @@ -1389,7 +1369,7 @@ class InlinerTest extends ClearAfterClass { |} """.stripMargin val List(c) = compile(code) - assertSameCode(getSingleMethod(c, "t1").instructions.dropNonOp, List(Op(ICONST_0), Op(ICONST_1), Op(IADD), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t1"), List(Op(ICONST_0), Op(ICONST_1), Op(IADD), Op(IRETURN))) assertEquals(getSingleMethod(c, "t2").instructions collect { case i: Invoke => i.owner +"."+ i.name }, List( "scala/runtime/IntRef.create", "C.C$$$anonfun$1")) } @@ -1430,9 +1410,9 @@ class InlinerTest extends ClearAfterClass { |} """.stripMargin val List(c) = compile(code) - assertSameCode(getSingleMethod(c, "t1").instructions.dropNonOp, List(Op(ICONST_3), Op(ICONST_4), Op(IADD), Op(IRETURN))) - assertSameCode(getSingleMethod(c, "t2").instructions.dropNonOp, List(Op(ICONST_1), Op(ICONST_2), Op(IADD), Op(IRETURN))) - assertSameCode(getSingleMethod(c, "t3").instructions.dropNonOp, List(Op(ICONST_1), Op(ICONST_3), Op(ISUB), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t1"), List(Op(ICONST_3), Op(ICONST_4), Op(IADD), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t2"), List(Op(ICONST_1), Op(ICONST_2), Op(IADD), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t3"), List(Op(ICONST_1), Op(ICONST_3), Op(ISUB), Op(IRETURN))) assertNoInvoke(getSingleMethod(c, "t4")) assertNoInvoke(getSingleMethod(c, "t5")) } @@ -1461,9 +1441,9 @@ class InlinerTest extends ClearAfterClass { """.stripMargin val List(c, _) = compile(code) def casts(m: String) = getSingleMethod(c, m).instructions collect { case TypeOp(CHECKCAST, tp) => tp } - assertSameCode(getSingleMethod(c, "t1").instructions.dropNonOp, List(VarOp(ALOAD, 1), Op(ARETURN))) - assertSameCode(getSingleMethod(c, "t2").instructions.dropNonOp, List(VarOp(ALOAD, 1), Op(ARETURN))) - assertSameCode(getSingleMethod(c, "t3").instructions.dropNonOp, List(VarOp(ALOAD, 1), TypeOp(CHECKCAST, "C"), Op(ARETURN))) + assertSameCode(getSingleMethod(c, "t1"), List(VarOp(ALOAD, 1), Op(ARETURN))) + assertSameCode(getSingleMethod(c, "t2"), List(VarOp(ALOAD, 1), Op(ARETURN))) + assertSameCode(getSingleMethod(c, "t3"), List(VarOp(ALOAD, 1), TypeOp(CHECKCAST, "C"), Op(ARETURN))) assertEquals(casts("t4"), List("C")) assertEquals(casts("t5"), Nil) assertEquals(casts("t6"), Nil) @@ -1489,12 +1469,11 @@ class InlinerTest extends ClearAfterClass { val cls = compile(code) val test = cls.find(_.name == "Test$").get - assertEquals( - getSingleMethod(test, "f").instructions.summary, - List(GETSTATIC, "mkFoo", - BIPUSH, ISTORE, - IFNONNULL, ACONST_NULL, ATHROW, -1 /*label*/, - ILOAD, ICONST_1, IADD, IRETURN)) + assertSameSummary(getSingleMethod(test, "f"), List( + GETSTATIC, "mkFoo", + BIPUSH, ISTORE, + IFNONNULL, ACONST_NULL, ATHROW, -1 /*label*/, + ILOAD, ICONST_1, IADD, IRETURN)) } @Test // a test taken from the test suite for the 2.11 inliner @@ -1509,11 +1488,10 @@ class InlinerTest extends ClearAfterClass { val List(c) = compile(code) // box-unbox will clean it up - assertEquals(getSingleMethod(c, "t").instructions.summary, - List( - ALOAD, "C$$$anonfun$1", IFEQ /*A*/, - "C$$$anonfun$2", IRETURN, - -1 /*A*/, "C$$$anonfun$3", IRETURN)) + assertSameSummary(getSingleMethod(c, "t"), List( + ALOAD, "C$$$anonfun$1", IFEQ /*A*/, + "C$$$anonfun$2", IRETURN, + -1 /*A*/, "C$$$anonfun$3", IRETURN)) } @Test diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala index 8f18a84a1c..0a9a26cda7 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/MethodLevelOptsTest.scala @@ -46,8 +46,7 @@ class MethodLevelOptsTest extends ClearAfterClass { val code = "def f = { try {} catch { case _: Throwable => 0 }; 1 }" val m = singleMethod(methodOptCompiler)(code) assertTrue(m.handlers.length == 0) - assertSameCode(m.instructions.dropNonOp, - List(Op(ICONST_1), Op(IRETURN))) + assertSameCode(m, List(Op(ICONST_1), Op(IRETURN))) } @Test @@ -89,7 +88,7 @@ class MethodLevelOptsTest extends ClearAfterClass { """.stripMargin val m = singleMethod(methodOptCompiler)(code) assertTrue(m.handlers.isEmpty) - assertSameCode(m.instructions.dropNonOp, List(Op(ICONST_3), Op(IRETURN))) + assertSameCode(m, List(Op(ICONST_3), Op(IRETURN))) } @Test @@ -108,8 +107,8 @@ class MethodLevelOptsTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertSameCode(getSingleMethod(c, "t").instructions.dropNonOp, - List(Op(ACONST_NULL), Invoke(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false), Op(ARETURN))) + assertSameCode(getSingleMethod(c, "t"), List( + Op(ACONST_NULL), Invoke(INVOKEVIRTUAL, "java/lang/Object", "toString", "()Ljava/lang/String;", false), Op(ARETURN))) } @Test @@ -126,8 +125,7 @@ class MethodLevelOptsTest extends ClearAfterClass { """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) assertSameCode( - getSingleMethod(c, "t").instructions.dropNonOp, - List(Ldc(LDC, "c"), Op(ARETURN))) + getSingleMethod(c, "t"), List(Ldc(LDC, "c"), Op(ARETURN))) } @Test @@ -147,13 +145,11 @@ class MethodLevelOptsTest extends ClearAfterClass { """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertEquals( - getSingleMethod(c, "t").instructions.dropNonOp, - List( - Ldc(LDC, "el"), VarOp(ASTORE, 1), - Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"), VarOp(ALOAD, 1), Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false), - Op(ACONST_NULL), VarOp(ASTORE, 1), - Ldc(LDC, "zit"), VarOp(ASTORE, 1), VarOp(ALOAD, 1), Op(ARETURN))) + assertSameCode(getSingleMethod(c, "t"), List( + Ldc(LDC, "el"), VarOp(ASTORE, 1), + Field(GETSTATIC, "scala/Predef$", "MODULE$", "Lscala/Predef$;"), VarOp(ALOAD, 1), Invoke(INVOKEVIRTUAL, "scala/Predef$", "println", "(Ljava/lang/Object;)V", false), + Op(ACONST_NULL), VarOp(ASTORE, 1), + Ldc(LDC, "zit"), VarOp(ASTORE, 1), VarOp(ALOAD, 1), Op(ARETURN))) } @Test @@ -172,8 +168,8 @@ class MethodLevelOptsTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertEquals(getSingleMethod(c, "t").instructions.dropNonOp, - List(IntOp(BIPUSH, 23), IntOp(NEWARRAY, 5), Op(POP), VarOp(ILOAD, 1), VarOp(ILOAD, 2), Op(IADD), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t"), List( + IntOp(BIPUSH, 23), IntOp(NEWARRAY, 5), Op(POP), VarOp(ILOAD, 1), VarOp(ILOAD, 2), Op(IADD), Op(IRETURN))) } @Test @@ -187,9 +183,9 @@ class MethodLevelOptsTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertEquals(getSingleMethod(c, "t").instructions.dropNonOp, - List(TypeOp(NEW, "java/lang/Integer"), Ldc(LDC, "nono"), Invoke(INVOKESPECIAL, "java/lang/Integer", "<init>", "(Ljava/lang/String;)V", false), - VarOp(ILOAD, 1), VarOp(ILOAD, 2), Op(IADD), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t"), List( + TypeOp(NEW, "java/lang/Integer"), Ldc(LDC, "nono"), Invoke(INVOKESPECIAL, "java/lang/Integer", "<init>", "(Ljava/lang/String;)V", false), + VarOp(ILOAD, 1), VarOp(ILOAD, 2), Op(IADD), Op(IRETURN))) } @Test @@ -213,8 +209,7 @@ class MethodLevelOptsTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertEquals(getSingleMethod(c, "t").instructions.dropNonOp, - List(Op(ICONST_0), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t"), List(Op(ICONST_0), Op(IRETURN))) } @Test @@ -230,14 +225,12 @@ class MethodLevelOptsTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertEquals( - getSingleMethod(c, "t").instructions.dropNonOp, - List( - IntOp(BIPUSH, 30), VarOp(ISTORE, 3), // no constant propagation, so we keep the store (and load below) of a const - VarOp(ILOAD, 1), - VarOp(ILOAD, 2), - VarOp(ILOAD, 3), - Invoke(INVOKESTATIC, "C", "C$$$anonfun$1", "(III)I", false), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t"), List( + IntOp(BIPUSH, 30), VarOp(ISTORE, 3), // no constant propagation, so we keep the store (and load below) of a const + VarOp(ILOAD, 1), + VarOp(ILOAD, 2), + VarOp(ILOAD, 3), + Invoke(INVOKESTATIC, "C", "C$$$anonfun$1", "(III)I", false), Op(IRETURN))) } @Test @@ -342,14 +335,14 @@ class MethodLevelOptsTest extends ClearAfterClass { assertNoInvoke(getSingleMethod(c, "t5")) assertNoInvoke(getSingleMethod(c, "t6")) assertNoInvoke(getSingleMethod(c, "t7")) - assertEquals(getSingleMethod(c, "t8").instructions.summary, List(ICONST_0, IRETURN)) + assertSameSummary(getSingleMethod(c, "t8"), List(ICONST_0, IRETURN)) assertNoInvoke(getSingleMethod(c, "t9")) // t10: no invocation of unbox assertEquals(getSingleMethod(c, "t10").instructions collect { case Invoke(_, owner, name, _, _) => (owner, name) }, List( ("java/lang/Integer", "valueOf"), ("C", "escape"))) - assertEquals(getSingleMethod(c, "t11").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t11"), List( BIPUSH, "valueOf", ASTORE /*2*/, BIPUSH, "valueOf", ASTORE /*3*/, ALOAD /*0*/, ALOAD /*2*/, "escape", @@ -410,9 +403,9 @@ class MethodLevelOptsTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertEquals(getSingleMethod(c, "t1").instructions.summary, List(ICONST_0, IRETURN)) + assertSameSummary(getSingleMethod(c, "t1"), List(ICONST_0, IRETURN)) assertNoInvoke(getSingleMethod(c, "t2")) - assertEquals(getSingleMethod(c, "t3").instructions.summary, List(LDC, LDC, LADD, LRETURN)) + assertSameSummary(getSingleMethod(c, "t3"), List(LDC, LDC, LADD, LRETURN)) assertNoInvoke(getSingleMethod(c, "t4")) assertEquals(getSingleMethod(c, "t5").instructions collect { case Field(_, owner, name, _) => s"$owner.$name" }, List("scala/runtime/IntRef.elem")) @@ -475,16 +468,16 @@ class MethodLevelOptsTest extends ClearAfterClass { """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) assertNoInvoke(getSingleMethod(c, "t1")) - assertEquals(getSingleMethod(c, "t2").instructions.summary, List(ICONST_1, ICONST_3, IADD, IRETURN)) - assertEquals(getSingleMethod(c, "t3").instructions.summary, List(ICONST_3, ICONST_4, IADD, IRETURN)) - assertEquals(getSingleMethod(c, "t4").instructions.summary, List(ICONST_3, "boxToInteger", ARETURN)) + assertSameSummary(getSingleMethod(c, "t2"), List(ICONST_1, ICONST_3, IADD, IRETURN)) + assertSameSummary(getSingleMethod(c, "t3"), List(ICONST_3, ICONST_4, IADD, IRETURN)) + assertSameSummary(getSingleMethod(c, "t4"), List(ICONST_3, "boxToInteger", ARETURN)) assertEquals(getSingleMethod(c, "t5").instructions collect { case Invoke(_, owner, name, _, _) => (owner, name) }, List( ("scala/runtime/BoxesRunTime", "boxToInteger"), ("scala/runtime/BoxesRunTime", "boxToInteger"), ("C", "tpl"), ("scala/Tuple2", "_1$mcI$sp"))) - assertEquals(getSingleMethod(c, "t6").instructions.summary, List(ICONST_1, ICONST_2, ISUB, IRETURN)) - assertEquals(getSingleMethod(c, "t7").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t6"), List(ICONST_1, ICONST_2, ISUB, IRETURN)) + assertSameSummary(getSingleMethod(c, "t7"), List( ICONST_1, ICONST_2, ISTORE, ISTORE, ICONST_3, ISTORE, ILOAD, ILOAD, IADD, ILOAD, IADD, IRETURN)) @@ -539,13 +532,13 @@ class MethodLevelOptsTest extends ClearAfterClass { |} """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertEquals(getSingleMethod(c, "t1").instructions.summary, List(NEW, DUP, "<init>", ARETURN)) - assertSameCode(getSingleMethod(c, "t2").instructions.dropNonOp, List(Op(LCONST_0), Op(LRETURN))) - assertSameCode(getSingleMethod(c, "t3").instructions.dropNonOp, List(Op(ICONST_1), Op(IRETURN))) - assertSameCode(getSingleMethod(c, "t4").instructions.dropNonOp, List(Op(ICONST_1), Op(IRETURN))) - assertSameCode(getSingleMethod(c, "t5").instructions.dropNonOp, List(Op(DCONST_0), Op(DRETURN))) - assertSameCode(getSingleMethod(c, "t6").instructions.dropNonOp, List(Op(ACONST_NULL), Op(ARETURN))) - assertSameCode(getSingleMethod(c, "t7").instructions.dropNonOp, List(Op(ICONST_0), Op(IRETURN))) + assertSameSummary(getSingleMethod(c, "t1"), List(NEW, DUP, "<init>", ARETURN)) + assertSameCode(getSingleMethod(c, "t2"), List(Op(LCONST_0), Op(LRETURN))) + assertSameCode(getSingleMethod(c, "t3"), List(Op(ICONST_1), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t4"), List(Op(ICONST_1), Op(IRETURN))) + assertSameCode(getSingleMethod(c, "t5"), List(Op(DCONST_0), Op(DRETURN))) + assertSameCode(getSingleMethod(c, "t6"), List(Op(ACONST_NULL), Op(ARETURN))) + assertSameCode(getSingleMethod(c, "t7"), List(Op(ICONST_0), Op(IRETURN))) } @Test @@ -560,8 +553,8 @@ class MethodLevelOptsTest extends ClearAfterClass { """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) assertSameCode( - getSingleMethod(c, "t").instructions.dropNonOp, - List(VarOp(ALOAD, 1), Jump(IFNULL, Label(6)), Op(ICONST_1), Op(IRETURN), Label(6), Op(ICONST_0), Op(IRETURN))) + getSingleMethod(c, "t"), List( + VarOp(ALOAD, 1), Jump(IFNULL, Label(6)), Op(ICONST_1), Op(IRETURN), Label(6), Op(ICONST_0), Op(IRETURN))) } @Test @@ -703,15 +696,8 @@ class MethodLevelOptsTest extends ClearAfterClass { assertEquals(locals(c, "t2"), List(("this", 0), ("x", 1))) // we don't have constant propagation (yet). // the local var can't be optimized as a store;laod sequence, there's a GETSTATIC between the two - assertEquals( - textify(findAsmMethod(c, "t2")), - getSingleMethod(c, "t2").instructions.dropNonOp.map(_.opcode), - List( - ICONST_2, ISTORE, - GETSTATIC, // Predef.MODULE$ - ILOAD, INVOKESTATIC, // boxToInteger - INVOKEVIRTUAL, // println - RETURN)) + assertSameSummary(getSingleMethod(c, "t2"), List( + ICONST_2, ISTORE, GETSTATIC, ILOAD, "boxToInteger", "println", RETURN)) assertEquals(locals(c, "t3"), List(("this", 0))) assertEquals(locals(c, "t4"), List(("this", 0), ("x", 1))) @@ -736,10 +722,7 @@ class MethodLevelOptsTest extends ClearAfterClass { val t = getSingleMethod(c, "t") assertEquals(t.handlers, Nil) assertEquals(locals(c, "t"), List(("this", 0))) - assertEquals(t.instructions.summary, - List( - GETSTATIC, LDC, "print", - -1, GOTO)) + assertSameSummary(t, List(GETSTATIC, LDC, "print", -1, GOTO)) } @Test @@ -769,7 +752,7 @@ class MethodLevelOptsTest extends ClearAfterClass { """.stripMargin val List(c) = compileClasses(methodOptCompiler)(code) - assertEquals(getSingleMethod(c, "t").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t"), List( BIPUSH, ILOAD, IF_ICMPNE, BIPUSH, ILOAD, IF_ICMPNE, LDC, ASTORE, GOTO, diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/UnreachableCodeTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/UnreachableCodeTest.scala index 62ee09e9de..9634517e28 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/UnreachableCodeTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/UnreachableCodeTest.scala @@ -232,35 +232,33 @@ class UnreachableCodeTest extends ClearAfterClass { """.stripMargin val List(c) = compileClasses(noOptCompiler)(code) - assertEquals(getSingleMethod(c, "nl").instructions.summary, List(ACONST_NULL, ARETURN)) + assertSameSummary(getSingleMethod(c, "nl"), List(ACONST_NULL, ARETURN)) - assertEquals(getSingleMethod(c, "nt").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "nt"), List( NEW, DUP, LDC, "<init>", ATHROW)) - assertEquals(getSingleMethod(c, "t1").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t1"), List( ALOAD, ACONST_NULL, "cons", RETURN)) // GenBCode introduces POP; ACONST_NULL after loading an expression of type scala.runtime.Null$, // see comment in BCodeBodyBuilder.adapt - assertEquals(getSingleMethod(c, "t2").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t2"), List( ALOAD, ALOAD, "nl", POP, ACONST_NULL, "cons", RETURN)) // the bytecode generated by GenBCode is ... ATHROW; INVOKEVIRTUAL C.cons; RETURN // the ASM classfile writer creates a new basic block (creates a label) right after the ATHROW // and replaces all instructions by NOP*; ATHROW, see comment in BCodeBodyBuilder.adapt // NOTE: DCE is enabled by default and gets rid of the redundant code (tested below) - assertEquals(getSingleMethod(c, "t3").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t3"), List( ALOAD, NEW, DUP, LDC, "<init>", ATHROW, NOP, NOP, NOP, ATHROW)) // GenBCode introduces an ATHROW after the invocation of C.nt, see BCodeBodyBuilder.adapt // NOTE: DCE is enabled by default and gets rid of the redundant code (tested below) - assertEquals(getSingleMethod(c, "t4").instructions.summary, List( + assertSameSummary(getSingleMethod(c, "t4"), List( ALOAD, ALOAD, "nt", ATHROW, NOP, NOP, NOP, ATHROW)) val List(cDCE) = compileClasses(dceCompiler)(code) - assertEquals(getSingleMethod(cDCE, "t3").instructions.summary, List( - ALOAD, NEW, DUP, LDC, "<init>", ATHROW)) - assertEquals(getSingleMethod(cDCE, "t4").instructions.summary, List( - ALOAD, ALOAD, "nt", ATHROW)) + assertSameSummary(getSingleMethod(cDCE, "t3"), List(ALOAD, NEW, DUP, LDC, "<init>", ATHROW)) + assertSameSummary(getSingleMethod(cDCE, "t4"), List(ALOAD, ALOAD, "nt", ATHROW)) } } diff --git a/test/junit/scala/tools/nsc/transform/patmat/PatmatBytecodeTest.scala b/test/junit/scala/tools/nsc/transform/patmat/PatmatBytecodeTest.scala index 3fc3144eb2..ac558e2e21 100644 --- a/test/junit/scala/tools/nsc/transform/patmat/PatmatBytecodeTest.scala +++ b/test/junit/scala/tools/nsc/transform/patmat/PatmatBytecodeTest.scala @@ -18,7 +18,7 @@ import scala.tools.testing.ClearAfterClass object PatmatBytecodeTest extends ClearAfterClass.Clearable { var compiler = newCompiler() - var optCompiler = newCompiler(extraArgs = "-Yopt:l:method") + var optCompiler = newCompiler(extraArgs = "-Yopt:l:project") def clear(): Unit = { compiler = null; optCompiler = null } } @@ -96,10 +96,9 @@ class PatmatBytecodeTest extends ClearAfterClass { """.stripMargin val c = compileClasses(optCompiler)(code).head - assertEquals(textify(findAsmMethod(c, "a")), getSingleMethod(c, "a").instructions.summary, - List( - NEW, DUP, ICONST_1, LDC, "<init>", - "y", ARETURN)) + assertSameSummary(getSingleMethod(c, "a"), List( + NEW, DUP, ICONST_1, LDC, "<init>", + "y", ARETURN)) } @Test @@ -127,12 +126,12 @@ class PatmatBytecodeTest extends ClearAfterClass { |} """.stripMargin val c = compileClasses(optCompiler)(code).head - assertEquals(textify(findAsmMethod(c, "a")), getSingleMethod(c, "a").instructions.summary, - List(NEW, DUP, ICONST_1, "boxToInteger", LDC, "<init>", ASTORE /*1*/, - ALOAD /*1*/, "y", ASTORE /*2*/, - ALOAD /*1*/, "x", INSTANCEOF, IFNE /*R*/, - NEW, DUP, ALOAD /*1*/, "<init>", ATHROW, - /*R*/ -1, ALOAD /*2*/, ARETURN)) + assertSameSummary(getSingleMethod(c, "a"), List( + NEW, DUP, ICONST_1, "boxToInteger", LDC, "<init>", ASTORE /*1*/, + ALOAD /*1*/, "y", ASTORE /*2*/, + ALOAD /*1*/, "x", INSTANCEOF, IFNE /*R*/, + NEW, DUP, ALOAD /*1*/, "<init>", ATHROW, + /*R*/ -1, ALOAD /*2*/, ARETURN)) } @Test @@ -156,7 +155,41 @@ class PatmatBytecodeTest extends ClearAfterClass { -1 /*A*/ , NEW /*MatchError*/ , DUP, ALOAD /*1*/ , "<init>", ATHROW, -1 /*B*/ , ILOAD, IRETURN) - assertEquals(textify(findAsmMethod(c, "a")), getSingleMethod(c, "a").instructions.summary, expected) - assertEquals(textify(findAsmMethod(c, "b")), getSingleMethod(c, "b").instructions.summary, expected) + assertSameSummary(getSingleMethod(c, "a"), expected) + assertSameSummary(getSingleMethod(c, "b"), expected) + } + + @Test + def valPatterns(): Unit = { + val code = + """case class C(a: Any, b: Int) { + | def tplCall = ("hi", 3) + | @inline final def tplInline = (true, 'z') + | + | def t1 = { val (a, b) = (1, 2); a + b } + | def t2 = { val (a, _) = (1, 3); a } + | def t3 = { val (s, i) = tplCall; s.length + i } + | def t4 = { val (_, i) = tplCall; i } + | def t5 = { val (b, c) = tplInline; b || c == 'e' } + | def t6 = { val (_, c) = tplInline; c } + | + | def t7 = { val C(s: String, b) = this; s.length + b } + | def t8 = { val C(_, b) = this; b } + | def t9 = { val C(a, _) = C("hi", 23); a.toString } + |} + """.stripMargin + val List(c, cMod) = compileClasses(optCompiler)(code) + assertSameSummary(getSingleMethod(c, "t1"), List(ICONST_1, ICONST_2, IADD, IRETURN)) + assertSameSummary(getSingleMethod(c, "t2"), List(ICONST_1, IRETURN)) + assertInvokedMethods(getSingleMethod(c, "t3"), List("C.tplCall", "scala/Tuple2._1", "scala/Tuple2._2$mcI$sp", "scala/MatchError.<init>", "java/lang/String.length")) + assertInvokedMethods(getSingleMethod(c, "t4"), List("C.tplCall", "scala/Tuple2._2$mcI$sp", "scala/MatchError.<init>")) + assertNoInvoke(getSingleMethod(c, "t5")) + assertSameSummary(getSingleMethod(c, "t6"), List(BIPUSH, IRETURN)) + + // MatchError reachable because of the type pattern `s: String` + assertInvokedMethods(getSingleMethod(c, "t7"), List("C.a", "C.b", "scala/MatchError.<init>", "java/lang/String.length")) + assertSameSummary(getSingleMethod(c, "t8"), List(ALOAD, "b", IRETURN)) + // C allocation not eliminated - constructor may have side-effects. + assertSameSummary(getSingleMethod(c, "t9"), List(NEW, DUP, LDC, BIPUSH, "<init>", "a", "toString", ARETURN)) } } |