summaryrefslogtreecommitdiff
path: root/test/files/run/constant-optimization.scala
diff options
context:
space:
mode:
authorJames Iry <jamesiry@gmail.com>2013-03-07 15:05:35 -0800
committerJames Iry <jamesiry@gmail.com>2013-03-07 16:19:31 -0800
commit69109c0ace5e3ac831c3b0a5635f25317d3b28bf (patch)
tree191edbde9b7b0ae80dfc3860ff449598c8df5a60 /test/files/run/constant-optimization.scala
parent5967a664ab1129e28687c591bd94c0e482cb305f (diff)
downloadscala-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/run/constant-optimization.scala')
-rw-r--r--test/files/run/constant-optimization.scala18
1 files changed, 18 insertions, 0 deletions
diff --git a/test/files/run/constant-optimization.scala b/test/files/run/constant-optimization.scala
new file mode 100644
index 0000000000..86f981e13f
--- /dev/null
+++ b/test/files/run/constant-optimization.scala
@@ -0,0 +1,18 @@
+object Test extends App {
+ def testBothReachable() {
+ val i = util.Random.nextInt
+ val x = if (i % 2 == 0) null else "good"
+ val y = if (x == null) "good" else x + ""
+ println(s"testBothReachable: $y")
+ }
+
+ def testOneReachable() {
+ val i = 1
+ val x = if (i != 1) null else "good"
+ val y = if (x == null) "good" else x + ""
+ println(s"testOneReachable: $y")
+ }
+
+ testBothReachable()
+ testOneReachable()
+}