Revert "JVM IR: Implement CHECK_NOT_NULL as a lowering"
This reverts commit dcd72b06d8.
Using a temporary variable has an effect on casts and GC.
This commit is contained in:
@@ -326,7 +326,6 @@ private val jvmFilePhases = listOf(
|
||||
polymorphicSignaturePhase,
|
||||
varargPhase,
|
||||
arrayConstructorPhase,
|
||||
checkNotNullPhase,
|
||||
|
||||
lateinitNullableFieldsPhase,
|
||||
lateinitDeclarationLoweringPhase,
|
||||
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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 org.jetbrains.kotlin.backend.jvm.intrinsics
|
||||
|
||||
import org.jetbrains.kotlin.backend.jvm.codegen.*
|
||||
import org.jetbrains.kotlin.codegen.AsmUtil
|
||||
import org.jetbrains.kotlin.codegen.intrinsics.IntrinsicMethods
|
||||
import org.jetbrains.kotlin.ir.expressions.IrFunctionAccessExpression
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.org.objectweb.asm.Label
|
||||
import org.jetbrains.org.objectweb.asm.Type
|
||||
|
||||
private fun ExpressionCodegen.checkTopValueForNull() {
|
||||
mv.dup()
|
||||
if (state.unifiedNullChecks) {
|
||||
mv.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "checkNotNull", "(Ljava/lang/Object;)V", false)
|
||||
} else {
|
||||
val ifNonNullLabel = Label()
|
||||
mv.ifnonnull(ifNonNullLabel)
|
||||
mv.invokestatic(IntrinsicMethods.INTRINSICS_CLASS_NAME, "throwNpe", "()V", false)
|
||||
mv.mark(ifNonNullLabel)
|
||||
}
|
||||
}
|
||||
|
||||
object IrCheckNotNull : IntrinsicMethod() {
|
||||
override fun invoke(expression: IrFunctionAccessExpression, codegen: ExpressionCodegen, data: BlockInfo): PromisedValue? {
|
||||
val arg0 = expression.getValueArgument(0)!!.accept(codegen, data)
|
||||
if (AsmUtil.isPrimitive(arg0.type)) return arg0
|
||||
return object : PromisedValue(codegen, arg0.type, expression.type) {
|
||||
override fun materializeAt(target: Type, irTarget: IrType, castForReified: Boolean) =
|
||||
arg0.materialized().also { codegen.checkTopValueForNull() }.materializeAt(target, irTarget, castForReified)
|
||||
|
||||
override fun discard() =
|
||||
arg0.materialized().also { codegen.checkTopValueForNull() }.discard()
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
@@ -110,6 +110,7 @@ class IrIntrinsicMethods(val irBuiltIns: IrBuiltIns, val symbols: JvmSymbols) {
|
||||
irBuiltIns.booleanNotSymbol.toKey()!! to Not,
|
||||
irBuiltIns.noWhenBranchMatchedExceptionSymbol.toKey()!! to IrNoWhenBranchMatchedException,
|
||||
irBuiltIns.illegalArgumentExceptionSymbol.toKey()!! to IrIllegalArgumentException,
|
||||
irBuiltIns.checkNotNullSymbol.toKey()!! to IrCheckNotNull,
|
||||
irBuiltIns.andandSymbol.toKey()!! to AndAnd,
|
||||
irBuiltIns.ororSymbol.toKey()!! to OrOr,
|
||||
irBuiltIns.dataClassArrayMemberHashCodeSymbol.toKey()!! to IrDataClassArrayMemberHashCode,
|
||||
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* Copyright 2010-2019 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 org.jetbrains.kotlin.backend.jvm.lower
|
||||
|
||||
import org.jetbrains.kotlin.backend.common.FileLoweringPass
|
||||
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
|
||||
import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
|
||||
import org.jetbrains.kotlin.backend.common.lower.irIfThen
|
||||
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
|
||||
import org.jetbrains.kotlin.backend.jvm.JvmBackendContext
|
||||
import org.jetbrains.kotlin.ir.builders.*
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.expressions.IrGetValue
|
||||
|
||||
internal val checkNotNullPhase = makeIrFilePhase(
|
||||
::CheckNotNullLowering,
|
||||
name = "CheckNotNullLowering",
|
||||
description = "Lower calls to the CHECK_NOT_NULL intrinsic, which are generated by psi2ir for \"!!\" expressions."
|
||||
)
|
||||
|
||||
private class CheckNotNullLowering(private val backendContext: JvmBackendContext) : FileLoweringPass, IrElementTransformerVoidWithContext() {
|
||||
override fun lower(irFile: IrFile) = irFile.transformChildrenVoid()
|
||||
|
||||
override fun visitCall(expression: IrCall): IrExpression {
|
||||
if (expression.symbol != backendContext.irBuiltIns.checkNotNullSymbol)
|
||||
return super.visitCall(expression)
|
||||
|
||||
expression.transformChildrenVoid()
|
||||
return backendContext.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol, expression.startOffset, expression.endOffset).irBlock {
|
||||
val valueArgument = expression.getValueArgument(0)!!
|
||||
|
||||
// For a null-check on a variable we should not introduce a temporary, since null checks are
|
||||
// optimized by RedundantNullCheckMethodTransformer, which remembers that the argument variable
|
||||
// is non-nullable if this call succeeds.
|
||||
val argument = if (valueArgument is IrGetValue) valueArgument.symbol.owner else irTemporary(valueArgument)
|
||||
|
||||
// Starting with Kotlin 1.4 null-checks are lowered to calls to the "checkNotNull" intrinsic, which
|
||||
// throws a NullPointerException on failure. Prior to Kotlin 1.4 we instead inline the null-check and
|
||||
// call the "throwNpe" intrinsic on failure which throws a KotlinNullPointerException.
|
||||
if (backendContext.state.unifiedNullChecks) {
|
||||
+irCall(backendContext.ir.symbols.checkNotNull).apply {
|
||||
putValueArgument(0, irGet(argument))
|
||||
}
|
||||
} else {
|
||||
+irIfThen(irEqualsNull(irGet(argument)), irCall(backendContext.ir.symbols.throwNpe))
|
||||
}
|
||||
|
||||
+irImplicitCast(irGet(argument), expression.type)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user