Implement constant folding in the IR backend for JVM

The newly added pass folds the set of constant functions of the
current backend, plus IrBuiltIns.
This commit is contained in:
Ting-Yuan Huang
2019-02-07 16:12:28 -08:00
committed by max-kammerer
parent 7e4d33be24
commit 79fcaae991
28 changed files with 576 additions and 7 deletions
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val a: Byte = 1 + 10
// 1 BIPUSH 11
@@ -0,0 +1,22 @@
// TARGET_BACKEND: JVM_IR
fun foo(): Array<Boolean> {
return arrayOf(
0.0 / 0 == 0.0 / 0,
0.0F > -0.0F,
0.0.equals(-0.0),
(0.0 / 0.0).equals(1.0 / 0.0)
)
}
// Disabled: current backend doesn't fold them.
// 4 INVOKESTATIC
// 4 INVOKESTATIC java/lang/Boolean.valueOf
// 1 ICONST_1
// 5 ICONST_0
// 0 IFEQ
// 0 IFNE
// 0 IFGE
// 0 IFGT
// 0 IFLE
// 0 IFLT
@@ -0,0 +1,29 @@
// TARGET_BACKEND: JVM_IR
fun foo(): Array<Boolean> {
return arrayOf(
19 < 20.0,
12 > 11,
3.0F <= 4.0,
4.0F >= 4,
0.0 / 0 != 0.0 / 0,
0.0 == -0.0,
123 == 123,
123L == 123L,
0.0F == -0.0F,
0.0.compareTo(-0.0) > 0,
(0.0 / 0.0).compareTo(1.0 / 0.0) > 0
)
}
// Disabled because the current backend doesn't fold them.
// 11 INVOKESTATIC
// 11 INVOKESTATIC java/lang/Boolean.valueOf
// 1 ICONST_0
// 12 ICONST_1
// 0 IFEQ
// 0 IFNE
// 0 IFGE
// 0 IFGT
// 0 IFLE
// 0 IFLT
@@ -0,0 +1,16 @@
val a = 1.0 + 10
val b = 2 + 10.0
val c = 3.0F + 10
val d = 4 + 10.0F
val e = 1.0 / 0
val f = 1 / 0.0
val g = 0.0 / 0
val h = 0 / 0.0
val i = 0.0 / 0.0
// 1 LDC 11.0
// 1 LDC 12.0
// 1 LDC 13.0
// 1 LDC 14.0
// 2 LDC Infinity
// 3 LDC NaN
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
fun box(): String {
val z = 1;
return "O" + "K".toString() + z
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
val a: Short = 1 + 255
// 1 SIPUSH 256
@@ -1,4 +1,3 @@
// IGNORE_BACKEND: JVM_IR
fun box(): String {
return "O" + "K".toString() + 1.toLong()
}
@@ -1,3 +1,4 @@
// IGNORE_BACKEND: JVM_IR
class A
fun foo(x: Any?) {}
@@ -0,0 +1,21 @@
class A
fun foo(x: Any?) {}
fun box(u: Int) {
val x: Int? = 1
x!!
val z: Int? = if (u == 1) x else null
z!!
foo(1 as java.lang.Integer)
val y: Any? = if (u == 1) x else A()
y!!
}
// 0 IFNULL
// 1 IFNONNULL
// 1 throwNpe
// 0 ATHROW
@@ -1,3 +1,4 @@
// IGNORE_BACKEND: JVM_IR
class A
fun box() {
val x: A? = A()
@@ -0,0 +1,18 @@
class A
fun box(u: Int) {
val x: A? = A()
val y: A?
if (u == 0) {
y = x
}
else {
y = null
}
y!!
}
// 0 IFNULL
// 1 IFNONNULL
// 1 throwNpe
// 0 ATHROW
@@ -6,9 +6,9 @@ class A() {
}
fun box() : String {
fun box(a: String, b: String) : String {
val s = "1" + "2" + 3 + 4L + 5.0 + 6F + '7' + A()
val s = a + "1" + "2" + 3 + 4L + b + 5.0 + 6F + '7' + A()
return "OK"
}
@@ -1,3 +1,4 @@
// IGNORE_BACKEND: JVM_IR
fun main() {
false.toString()
1.toByte().toString()
@@ -0,0 +1,14 @@
fun main(a: Boolean, b:Byte, c: Short, d: Int, e: Long, f: Float, g: Double, h: Char) {
a.toString()
b.toString()
c.toString()
d.toString()
e.toString()
f.toString()
g.toString()
h.toString()
}
/*Check that all "valueOf" are String ones and there is no boxing*/
// 8 valueOf
// 8 INVOKESTATIC java/lang/String.valueOf