summaryrefslogtreecommitdiff
path: root/test/junit/scala/tools/nsc/backend/jvm/opt/CompactLocalVariablesTest.scala
blob: 76492cfa2335c94c13cd89e4eb1da5551e5b3536 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package scala.tools.nsc
package backend.jvm
package opt

import org.junit.runner.RunWith
import org.junit.runners.JUnit4
import org.junit.Test
import scala.tools.asm.Opcodes._
import org.junit.Assert._

import CodeGenTools._
import scala.tools.partest.ASMConverters
import ASMConverters._

@RunWith(classOf[JUnit4])
class CompactLocalVariablesTest {

  // recurse-unreachable-jumps is required for eliminating catch blocks, in the first dce round they
  // are still live.only after eliminating the empty handler the catch blocks become unreachable.
  val methodOptCompiler     = newCompiler(extraArgs = "-target:jvm-1.6 -Ybackend:GenBCode -Yopt:unreachable-code,compact-locals")
  val noCompactVarsCompiler = newCompiler(extraArgs = "-target:jvm-1.6 -Ybackend:GenBCode -Yopt:unreachable-code")

  @Test
  def compactUnused(): Unit = {
    val code =
      """def f: Double = {
        |  try { }
        |  catch {
        |    case _: Throwable =>
        |      // eliminated by dce
        |      val i = 1
        |      val d = 1d
        |      val f = 1f
        |      val l = 1l
        |  }
        |
        |  val i = 1      // variable index 1 (it's an instance method, so at index 0 we have `this`)
        |  val d = 1d     // 2,3
        |  val f = 1f     // 4
        |  val l = 1l     // 5,6
        |
        |  try { }
        |  catch {
        |    case _: Throwable =>
        |      // eliminated by dce
        |      val i = 1
        |      val d = 1d
        |      val f = 1f
        |      val l = 1l
        |  }
        |
        |  val ii = 1     // 7
        |  val dd = 1d    // 8,9
        |  val ff = 1f    // 10
        |  val ll = 1l    // 11,12
        |
        |  i + ii + d + dd + f + ff + l + ll
        |}
        |""".stripMargin

    val List(noCompact)   = compileMethods(noCompactVarsCompiler)(code)
    val List(withCompact) = compileMethods(methodOptCompiler)(code)

    // code is the same, except for local var indices
    assertTrue(noCompact.instructions.size == withCompact.instructions.size)

    val varOpSlots = convertMethod(withCompact).instructions collect {
      case VarOp(_, v) => v
    }
    assertTrue(varOpSlots.toString, varOpSlots == List(1, 2, 4, 5, 7, 8, 10, 11,  // stores
                                                       1, 7, 2, 8, 4, 10, 5, 11)) // loads

    // the local variables descriptor table is cleaned up to remove stale entries after dce,
    // also when the slots are not compacted
    assertTrue(noCompact.localVariables.size == withCompact.localVariables.size)

    assertTrue(noCompact.maxLocals == 25)
    assertTrue(withCompact.maxLocals == 13)
  }
}