diff options
author | James Iry <jamesiry@gmail.com> | 2013-03-07 15:05:35 -0800 |
---|---|---|
committer | James Iry <jamesiry@gmail.com> | 2013-03-07 16:19:31 -0800 |
commit | 69109c0ace5e3ac831c3b0a5635f25317d3b28bf (patch) | |
tree | 191edbde9b7b0ae80dfc3860ff449598c8df5a60 /test/files/jvm | |
parent | 5967a664ab1129e28687c591bd94c0e482cb305f (diff) | |
download | scala-69109c0ace5e3ac831c3b0a5635f25317d3b28bf.tar.gz scala-69109c0ace5e3ac831c3b0a5635f25317d3b28bf.tar.bz2 scala-69109c0ace5e3ac831c3b0a5635f25317d3b28bf.zip |
Analyze constants to remove unnecessary branches
This commit adds analysis and optimization of constants to remove
unnecessary branches. It uses abstract interpretation to determine
what constant(s) a particular stack slot or variable might or might not
hold at a given spot and uses that knowledge to eliminate branches that
cannot be taken. Its primary goal is null check removal, but it also
works for other constants.
Several tests are modified to include the new optimization phase.
Two new tests are added. One verifies that branching still works as
expected. The other verifies that branches are removed.
Diffstat (limited to 'test/files/jvm')
-rw-r--r-- | test/files/jvm/constant-optimization/Foo_1.flags | 1 | ||||
-rw-r--r-- | test/files/jvm/constant-optimization/Foo_1.scala | 9 | ||||
-rw-r--r-- | test/files/jvm/constant-optimization/Test.scala | 27 |
3 files changed, 37 insertions, 0 deletions
diff --git a/test/files/jvm/constant-optimization/Foo_1.flags b/test/files/jvm/constant-optimization/Foo_1.flags new file mode 100644 index 0000000000..86f52af447 --- /dev/null +++ b/test/files/jvm/constant-optimization/Foo_1.flags @@ -0,0 +1 @@ +-Ynooptimise -Yconst-opt
\ No newline at end of file diff --git a/test/files/jvm/constant-optimization/Foo_1.scala b/test/files/jvm/constant-optimization/Foo_1.scala new file mode 100644 index 0000000000..cb67ad4e90 --- /dev/null +++ b/test/files/jvm/constant-optimization/Foo_1.scala @@ -0,0 +1,9 @@ +class Foo_1 { + def foo() { + // constant optimization should eliminate all branches + val i = 1 + val x = if (i != 1) null else "good" + val y = if (x == null) "good" else x + "" + println(y) + } +}
\ No newline at end of file diff --git a/test/files/jvm/constant-optimization/Test.scala b/test/files/jvm/constant-optimization/Test.scala new file mode 100644 index 0000000000..283aa6f47a --- /dev/null +++ b/test/files/jvm/constant-optimization/Test.scala @@ -0,0 +1,27 @@ + +import scala.tools.partest.BytecodeTest +import scala.tools.asm +import asm.tree.InsnList +import scala.collection.JavaConverters._ + +object Test extends BytecodeTest { + val comparisons = Set(asm.Opcodes.IF_ACMPEQ, asm.Opcodes.IF_ACMPNE, asm.Opcodes.IF_ICMPEQ, asm.Opcodes.IF_ICMPGE, asm.Opcodes.IF_ICMPGT, asm.Opcodes.IF_ICMPLE, + asm.Opcodes.IF_ICMPLT, asm.Opcodes.IF_ICMPNE, asm.Opcodes.IFEQ, asm.Opcodes.IFGE, asm.Opcodes.IFGT, asm.Opcodes.IFLE, asm.Opcodes.IFLT, + asm.Opcodes.IFNE, asm.Opcodes.IFNONNULL, asm.Opcodes.IFNULL) + + def show: Unit = { + val classNode = loadClassNode("Foo_1") + val methodNode = getMethod(classNode, "foo") + // after optimization there should be no comparisons left + val expected = 0 + + val got = countComparisons(methodNode.instructions) + assert(got == expected, s"expected $expected but got $got comparisons") + } + + def countComparisons(insnList: InsnList): Int = { + def isComparison(node: asm.tree.AbstractInsnNode): Boolean = + (comparisons contains node.getOpcode) + insnList.iterator.asScala count isComparison + } +}
\ No newline at end of file |