From 1378b0cf053a13843be88bcf2b0f17f66a90e171 Mon Sep 17 00:00:00 2001 From: Dmitry Petrov Date: Fri, 12 May 2017 17:27:32 +0300 Subject: [PATCH] Fix bytecode tests after new optimizations - Turn some const conditions into non-const conditions - Make sure inlined const values are used where required (otherwise they are eliminated by POP backward propagation) --- .../boxingOptimization/unsafeRemoving.kt | 6 ++- .../sharedSlotsWithCapturedVars.kt | 4 +- .../bytecodeText/conditions/conjuction.kt | 9 ++-- .../bytecodeText/conditions/disjunction.kt | 9 ++-- .../conditions/negatedConjuction.kt | 9 ++-- .../conditions/negatedDisjunction.kt | 9 ++-- .../conditions/negatedZeroCompare.kt | 3 +- .../bytecodeText/conditions/zeroCompare.kt | 3 +- .../coroutines/varValueConflictsWithTable.kt | 6 ++- ...ForNoParametersArgumentCallInExpression.kt | 2 +- .../bytecodeText/inlineJavaStaticFields.kt | 41 ++++++++++--------- .../intrinsicsCompare/byteSmartCast.kt | 3 +- .../intrinsicsCompare/charSmartCast.kt | 3 +- .../intrinsicsCompare/intSmartCast.kt | 3 +- .../intrinsicsCompare/longSmartCast.kt | 3 +- .../intrinsicsCompare/shortSmartCast.kt | 3 +- .../bytecodeText/lazyCodegen/negateVar.kt | 4 +- .../lazyCodegen/negateVarChain.kt | 4 +- .../ifNullEqualsNullInline.kt | 4 +- .../ifUnitEqualsNullInline.kt | 6 +-- .../preEvaluateInlineJavaStaticFields.kt | 6 ++- .../bytecodeText/redundantGotoRemoving.kt | 4 +- 22 files changed, 84 insertions(+), 60 deletions(-) diff --git a/compiler/testData/codegen/bytecodeText/boxingOptimization/unsafeRemoving.kt b/compiler/testData/codegen/bytecodeText/boxingOptimization/unsafeRemoving.kt index d6e64d0a2ef..0c211227755 100644 --- a/compiler/testData/codegen/bytecodeText/boxingOptimization/unsafeRemoving.kt +++ b/compiler/testData/codegen/bytecodeText/boxingOptimization/unsafeRemoving.kt @@ -3,6 +3,8 @@ fun acceptingBoxed(x : Int?) : Int ? = x class A(var x : Int? = null) +val one = 1 + fun foo() { val rb = returningBoxed() acceptingBoxed(2) @@ -20,8 +22,8 @@ fun foo() { val z: Int? = 8 val res = y === z - val c1: Any = if (1 == 1) 0 else "abc" - val c2: Any = if (1 != 1) 0 else "abc" + val c1: Any = if (1 == one) 0 else "abc" + val c2: Any = if (1 != one) 0 else "abc" } // 9 java/lang/Integer.valueOf diff --git a/compiler/testData/codegen/bytecodeText/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt b/compiler/testData/codegen/bytecodeText/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt index acbfaf5acc8..009a7fc8452 100644 --- a/compiler/testData/codegen/bytecodeText/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt +++ b/compiler/testData/codegen/bytecodeText/capturedVarsOptimization/sharedSlotsWithCapturedVars.kt @@ -20,11 +20,11 @@ fun box(): String { // Shared variable slots (x1, x2): -// 5 ILOAD 0 +// 4 ILOAD 0 // 4 ISTORE 0 // Temporary variable slots for 'x2++': -// 1 ILOAD 1 +// 0 ILOAD 1 // 1 ISTORE 1 // 0 NEW diff --git a/compiler/testData/codegen/bytecodeText/conditions/conjuction.kt b/compiler/testData/codegen/bytecodeText/conditions/conjuction.kt index c610a57813b..c9310c66162 100644 --- a/compiler/testData/codegen/bytecodeText/conditions/conjuction.kt +++ b/compiler/testData/codegen/bytecodeText/conditions/conjuction.kt @@ -1,7 +1,8 @@ +val a = false +val b = false +val c = false + fun main() { - val a = false - val b = false - val c = false if (a && b && c) { "then" } else { @@ -9,7 +10,7 @@ fun main() { } } -// 3 ICONST_0 +// 0 ICONST_0 // 0 ICONST_1 // 3 IFEQ // 0 IFNE diff --git a/compiler/testData/codegen/bytecodeText/conditions/disjunction.kt b/compiler/testData/codegen/bytecodeText/conditions/disjunction.kt index 2fa0aa89f9b..e0f873a74d4 100644 --- a/compiler/testData/codegen/bytecodeText/conditions/disjunction.kt +++ b/compiler/testData/codegen/bytecodeText/conditions/disjunction.kt @@ -1,7 +1,8 @@ +val a = false +val b = false +val c = false + fun main() { - val a = false - val b = false - val c = false if (a || b || c) { "then" } else { @@ -9,7 +10,7 @@ fun main() { } } -// 3 ICONST_0 +// 0 ICONST_0 // 0 ICONST_1 // 1 IFEQ // 2 IFNE diff --git a/compiler/testData/codegen/bytecodeText/conditions/negatedConjuction.kt b/compiler/testData/codegen/bytecodeText/conditions/negatedConjuction.kt index 92a2d84505e..522c2185004 100644 --- a/compiler/testData/codegen/bytecodeText/conditions/negatedConjuction.kt +++ b/compiler/testData/codegen/bytecodeText/conditions/negatedConjuction.kt @@ -1,7 +1,8 @@ +val a = false +val b = false +val c = false + fun main() { - val a = false - val b = false - val c = false if (!(a && b && c)) { "then" } else { @@ -9,7 +10,7 @@ fun main() { } } -// 3 ICONST_0 +// 0 ICONST_0 // 0 ICONST_1 // 2 IFEQ // 1 IFNE diff --git a/compiler/testData/codegen/bytecodeText/conditions/negatedDisjunction.kt b/compiler/testData/codegen/bytecodeText/conditions/negatedDisjunction.kt index e21882e88cf..0278b2e0dfb 100644 --- a/compiler/testData/codegen/bytecodeText/conditions/negatedDisjunction.kt +++ b/compiler/testData/codegen/bytecodeText/conditions/negatedDisjunction.kt @@ -1,7 +1,8 @@ +val a = false +val b = false +val c = false + fun main() { - val a = false - val b = false - val c = false if (!(a || b || c)) { "then" } else { @@ -9,7 +10,7 @@ fun main() { } } -// 3 ICONST_0 +// 0 ICONST_0 // 0 ICONST_1 // 0 IFEQ // 3 IFNE diff --git a/compiler/testData/codegen/bytecodeText/conditions/negatedZeroCompare.kt b/compiler/testData/codegen/bytecodeText/conditions/negatedZeroCompare.kt index 81afc75facd..a00f8492543 100644 --- a/compiler/testData/codegen/bytecodeText/conditions/negatedZeroCompare.kt +++ b/compiler/testData/codegen/bytecodeText/conditions/negatedZeroCompare.kt @@ -1,5 +1,6 @@ +val a = 1 + fun main() { - val a = 1 if (!(a == 0)) { "then" } else { diff --git a/compiler/testData/codegen/bytecodeText/conditions/zeroCompare.kt b/compiler/testData/codegen/bytecodeText/conditions/zeroCompare.kt index ad5c54be239..9010e305d5d 100644 --- a/compiler/testData/codegen/bytecodeText/conditions/zeroCompare.kt +++ b/compiler/testData/codegen/bytecodeText/conditions/zeroCompare.kt @@ -1,5 +1,6 @@ +val a = 1 + fun main() { - val a = 1 if (a == 0) { "then" } else { diff --git a/compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTable.kt b/compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTable.kt index 9228283062a..d59a6ecf239 100644 --- a/compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTable.kt +++ b/compiler/testData/codegen/bytecodeText/coroutines/varValueConflictsWithTable.kt @@ -12,11 +12,13 @@ fun builder(c: suspend () -> Unit) { c.startCoroutine(EmptyContinuation) } +val nonConstOne = 1 + fun box(): String { var result = "fail 1" builder { // Initialize var with Int value - for (i in 1..1) { + for (i in 1..nonConstOne) { if ("".length > 0) continue } @@ -43,4 +45,4 @@ fun box(): String { // 1 LOCALVARIABLE s Ljava/lang/String; L.* 3 // 0 PUTFIELD VarValueConflictsWithTableKt\$box\$1.I\$0 : I /* 2 loads in cycle */ -// 2 ILOAD 3 +// 2 ILOAD 3 \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/inline/linenumberForNoParametersArgumentCallInExpression.kt b/compiler/testData/codegen/bytecodeText/inline/linenumberForNoParametersArgumentCallInExpression.kt index 9cac096c941..5506819e5e4 100644 --- a/compiler/testData/codegen/bytecodeText/inline/linenumberForNoParametersArgumentCallInExpression.kt +++ b/compiler/testData/codegen/bytecodeText/inline/linenumberForNoParametersArgumentCallInExpression.kt @@ -11,4 +11,4 @@ inline fun lookAtMe(f: () -> Int): Int { } // TODO: Less NOPs is better -// 1 NOP \ No newline at end of file +// 2 NOP \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt b/compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt index 4d864d0b62a..f6ae773a90d 100644 --- a/compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt +++ b/compiler/testData/codegen/bytecodeText/inlineJavaStaticFields.kt @@ -35,29 +35,31 @@ object KoKobject { val JvmStaticString: String? = "123" } +fun Any?.use() {} + fun test() { - Integer.MIN_VALUE - java.lang.Long.MAX_VALUE + Integer.MIN_VALUE.use() + java.lang.Long.MAX_VALUE.use() - JClass.PrimitiveInt - JClass.BigPrimitiveInt - JClass.PrimitiveByte - JClass.PrimitiveChar - JClass.PrimitiveLong - JClass.PrimitiveShort - JClass.PrimitiveBool - JClass.PrimitiveFloat - JClass.PrimitiveDouble - JClass.Str - JClass.StrNullable + JClass.PrimitiveInt.use() + JClass.BigPrimitiveInt.use() + JClass.PrimitiveByte.use() + JClass.PrimitiveChar.use() + JClass.PrimitiveLong.use() + JClass.PrimitiveShort.use() + JClass.PrimitiveBool.use() + JClass.PrimitiveFloat.use() + JClass.PrimitiveDouble.use() + JClass.Str.use() + JClass.StrNullable.use() - JClass.BoxedInt - JClass.NonFinal + JClass.BoxedInt.use() + JClass.NonFinal.use() - JClass().NonStatic + JClass().NonStatic.use() - KoKobject.JvmStatic - KoKobject.JvmStaticString + KoKobject.JvmStatic.use() + KoKobject.JvmStaticString.use() } // 1 LDC -2147483648 @@ -78,5 +80,4 @@ fun test() { // 1 GETFIELD JClass.NonStatic : I // 1 GETSTATIC KoKobject.JvmStatic : I // 1 GETSTATIC KoKobject.JvmStaticString : Ljava/lang/String -// 3 POP2 -// 18 POP + diff --git a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/byteSmartCast.kt b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/byteSmartCast.kt index 2d6a5b3c59d..9eade4d1c79 100644 --- a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/byteSmartCast.kt +++ b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/byteSmartCast.kt @@ -13,4 +13,5 @@ fun less5(a: Any?, b: Any?) = if (a is Byte && b is Byte) a < b else true // 3 Intrinsics\.areEqual // 3 Intrinsics\.compare // for compare: -// 3 IF_ICMPGE +// 3 IFGE +// 0 IF_ICMPGE diff --git a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/charSmartCast.kt b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/charSmartCast.kt index 7b7dea65e66..53b7a1b412b 100644 --- a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/charSmartCast.kt +++ b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/charSmartCast.kt @@ -13,4 +13,5 @@ fun less5(a: Any?, b: Any?) = if (a is Char && b is Char) a < b else true // 3 Intrinsics\.areEqual // 3 Intrinsics\.compare // for compare: -// 3 IF_ICMPGE +// 3 IFGE +// 0 IF_ICMPGE diff --git a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/intSmartCast.kt b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/intSmartCast.kt index fb6171fd899..be00d2f681d 100644 --- a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/intSmartCast.kt +++ b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/intSmartCast.kt @@ -13,4 +13,5 @@ fun less5(a: Any?, b: Any?) = if (a is Int && b is Int) a < b else true // 3 Intrinsics\.areEqual // 3 Intrinsics\.compare // for compare: -// 3 IF_ICMPGE +// 3 IFGE +// 0 IF_ICMPGE diff --git a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/longSmartCast.kt b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/longSmartCast.kt index 815ea69d576..98020eb0871 100644 --- a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/longSmartCast.kt +++ b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/longSmartCast.kt @@ -13,4 +13,5 @@ fun less5(a: Any?, b: Any?) = if (a is Long && b is Long) a < b else true // 3 Intrinsics\.areEqual // 3 Intrinsics\.compare // for compare: -// 3 IF_ICMPGE +// 3 IFGE +// 0 IF_ICMPGE diff --git a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/shortSmartCast.kt b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/shortSmartCast.kt index b6c36c8e754..151acdd6fe2 100644 --- a/compiler/testData/codegen/bytecodeText/intrinsicsCompare/shortSmartCast.kt +++ b/compiler/testData/codegen/bytecodeText/intrinsicsCompare/shortSmartCast.kt @@ -13,4 +13,5 @@ fun less5(a: Any?, b: Any?) = if (a is Short && b is Short) a < b else true // 3 Intrinsics\.areEqual // 3 Intrinsics\.compare // for compare: -// 3 IF_ICMPGE +// 3 IFGE +// 0 IF_ICMPGE diff --git a/compiler/testData/codegen/bytecodeText/lazyCodegen/negateVar.kt b/compiler/testData/codegen/bytecodeText/lazyCodegen/negateVar.kt index 2c2c757fd7f..67b152b2274 100644 --- a/compiler/testData/codegen/bytecodeText/lazyCodegen/negateVar.kt +++ b/compiler/testData/codegen/bytecodeText/lazyCodegen/negateVar.kt @@ -1,5 +1,7 @@ +val two = 2 + fun test2() { - val p = 1 < 2; + val p = 1 < two if (!p) { val p = 1 } diff --git a/compiler/testData/codegen/bytecodeText/lazyCodegen/negateVarChain.kt b/compiler/testData/codegen/bytecodeText/lazyCodegen/negateVarChain.kt index 67566965af8..a618b0b1b23 100644 --- a/compiler/testData/codegen/bytecodeText/lazyCodegen/negateVarChain.kt +++ b/compiler/testData/codegen/bytecodeText/lazyCodegen/negateVarChain.kt @@ -1,5 +1,7 @@ +val two = 2 + fun test2() { - val p = 1 < 2; + val p = 1 < two if (!!!!!!p) { val p = 1 } diff --git a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/ifNullEqualsNullInline.kt b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/ifNullEqualsNullInline.kt index cd4478fc0c0..2c13b047cf3 100644 --- a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/ifNullEqualsNullInline.kt +++ b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/ifNullEqualsNullInline.kt @@ -4,8 +4,8 @@ fun test1() { val n = null - n.elvis { "X1" } - "X2".elvis { "X3" } + val u1 = n.elvis { "X1" } + val u2 = "X2".elvis { "X3" } } // @TestKt.class: diff --git a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/ifUnitEqualsNullInline.kt b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/ifUnitEqualsNullInline.kt index 25a60c4f71a..d1e1bd1e18f 100644 --- a/compiler/testData/codegen/bytecodeText/nullCheckOptimization/ifUnitEqualsNullInline.kt +++ b/compiler/testData/codegen/bytecodeText/nullCheckOptimization/ifUnitEqualsNullInline.kt @@ -2,9 +2,9 @@ // FILE: test.kt -fun test1() { +fun test1(): String { val u = Unit - u.mapNullable({ "X1" }, { "X2" }) + return u.mapNullable({ "X1" }, { "X2" }) } // @TestKt.class: @@ -14,5 +14,5 @@ fun test1() { // 0 X2 // FILE: inline.kt -inline fun T?.mapNullable(ifNotNull: (T) -> R, ifNull: () -> R) = +inline fun T?.mapNullable(ifNotNull: (T) -> R, ifNull: () -> R): R = if (this == null) ifNull() else ifNotNull(this) \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/preEvaluateInlineJavaStaticFields.kt b/compiler/testData/codegen/bytecodeText/preEvaluateInlineJavaStaticFields.kt index d3365831869..a1acf41d438 100644 --- a/compiler/testData/codegen/bytecodeText/preEvaluateInlineJavaStaticFields.kt +++ b/compiler/testData/codegen/bytecodeText/preEvaluateInlineJavaStaticFields.kt @@ -38,8 +38,10 @@ object KoKobject { val JvmStaticString: String? = "123" } +fun Any?.use() {} + fun test() { - "res1: " + + ("res1: " + Integer.MIN_VALUE + " " + java.lang.Long.MAX_VALUE + " " + JClass.PrimitiveInt + " " + @@ -52,7 +54,7 @@ fun test() { JClass.PrimitiveFloat + " " + JClass.PrimitiveDouble + " " + JClass.Str + " " + - JClass.StrNullable + JClass.StrNullable).use() "res2: " + JClass.BoxedInt "res3: " + JClass.NonFinal diff --git a/compiler/testData/codegen/bytecodeText/redundantGotoRemoving.kt b/compiler/testData/codegen/bytecodeText/redundantGotoRemoving.kt index a2c2727cfdb..90dfdfc8f2c 100644 --- a/compiler/testData/codegen/bytecodeText/redundantGotoRemoving.kt +++ b/compiler/testData/codegen/bytecodeText/redundantGotoRemoving.kt @@ -1,7 +1,9 @@ +val nonConstFlag = true inline fun calc(value : T, fn: (T) -> R) : R = fn(value) + inline fun identity(value : T) : T = calc(value) { - if (1 == 1) return it + if (nonConstFlag) return it it }