KT53100: Optimization: <T-unbox>(CONSTANT_PRIMITIVE(x: T?)) => x
Merge-request: KT-MR-6684 Merged-by: Vladimir Sukharev <Vladimir.Sukharev@jetbrains.com>
This commit is contained in:
+11
-2
@@ -7,11 +7,9 @@ package org.jetbrains.kotlin.backend.konan.lower
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
||||
import org.jetbrains.kotlin.backend.konan.Context
|
||||
import org.jetbrains.kotlin.backend.konan.DECLARATION_ORIGIN_INLINE_CLASS_SPECIAL_FUNCTION
|
||||
import org.jetbrains.kotlin.backend.konan.getInlinedClassNative
|
||||
import org.jetbrains.kotlin.backend.konan.ir.isBoxOrUnboxCall
|
||||
import org.jetbrains.kotlin.ir.IrElement
|
||||
import org.jetbrains.kotlin.ir.IrStatement
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
@@ -19,6 +17,7 @@ import org.jetbrains.kotlin.ir.expressions.*
|
||||
import org.jetbrains.kotlin.ir.expressions.impl.*
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.IrReturnableBlockSymbolImpl
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrFail
|
||||
import org.jetbrains.kotlin.ir.types.classifierOrNull
|
||||
import org.jetbrains.kotlin.ir.util.dump
|
||||
import org.jetbrains.kotlin.ir.util.getArgumentsWithIr
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
|
||||
@@ -266,6 +265,16 @@ internal class RedundantCoercionsCleaner(val context: Context) : FileLoweringPas
|
||||
}
|
||||
}
|
||||
|
||||
is IrConstantPrimitive ->
|
||||
if (expression.value.type == coercion.type) {
|
||||
// In case the initial and final types are equal, then
|
||||
// the whole sequence of transformations is no-op, and we can remove them all
|
||||
PossiblyFoldedExpression(expression.value, true)
|
||||
} else {
|
||||
// Types are not equal for ex. in kt53100_casts.kt
|
||||
PossiblyFoldedExpression(expression.transformIfAsked(), false)
|
||||
}
|
||||
|
||||
else -> PossiblyFoldedExpression(expression.transformIfAsked(), false)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2581,6 +2581,11 @@ task boxing15(type: KonanLocalTest) {
|
||||
source = "codegen/boxing/boxing15.kt"
|
||||
}
|
||||
|
||||
task kt53100_casts(type: KonanLocalTest) {
|
||||
useGoldenData = true
|
||||
source = "codegen/boxing/kt53100_casts.kt"
|
||||
}
|
||||
|
||||
task boxCache0(type: KonanLocalTest) {
|
||||
useGoldenData = true
|
||||
source = "codegen/boxing/box_cache0.kt"
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
package codegen.boxing.kt53100_casts
|
||||
|
||||
import kotlin.test.*
|
||||
|
||||
// Reproducer is copied from FloatingPointParser.unaryMinus()
|
||||
inline fun <reified T> unaryMinus(value: T): T {
|
||||
return when (value) {
|
||||
is Float -> -value as T
|
||||
is Double -> -value as T
|
||||
else -> throw NumberFormatException()
|
||||
}
|
||||
}
|
||||
|
||||
@Test fun runTest(){
|
||||
println(unaryMinus(0.0))
|
||||
println(unaryMinus(0.0f))
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
-0.0
|
||||
-0.0
|
||||
@@ -9,8 +9,6 @@ class C {
|
||||
|
||||
// CHECK-LABEL: define void @"kfun:#main(){}"()
|
||||
// CHECK-NOT: Int-box
|
||||
// TODO Remove next check after fix of https://youtrack.jetbrains.com/issue/KT-53100/Optimization-needed-T-unboxCONSTANTPRIMITIVEx-T-x
|
||||
// CHECK: Int-unbox
|
||||
// CHECK-NOT: Int-unbox
|
||||
// CHECK: ret void
|
||||
fun main() {
|
||||
|
||||
@@ -9,8 +9,6 @@ class C<T> {
|
||||
|
||||
// CHECK-LABEL: define void @"kfun:#main(){}"()
|
||||
// CHECK-NOT: Int-box
|
||||
// TODO Remove next check after fix of https://youtrack.jetbrains.com/issue/KT-53100/Optimization-needed-T-unboxCONSTANTPRIMITIVEx-T-x
|
||||
// CHECK: Int-unbox
|
||||
// CHECK-NOT: Int-unbox
|
||||
// CHECK: ret void
|
||||
fun main() {
|
||||
|
||||
Reference in New Issue
Block a user