JVM_IR KT-44993 preserve inner expression type when fusing if-null

This commit is contained in:
Dmitry Petrov
2021-03-16 11:15:18 +03:00
committed by TeamCityServer
parent 922419efb8
commit decfaa3ba5
10 changed files with 140 additions and 7 deletions
@@ -36540,6 +36540,12 @@ public class FirBlackBoxCodegenTestGenerated extends AbstractFirBlackBoxCodegenT
runTest("compiler/testData/codegen/box/regressions/kt4281.kt");
}
@Test
@TestMetadata("kt44993.kt")
public void testKt44993() throws Exception {
runTest("compiler/testData/codegen/box/regressions/kt44993.kt");
}
@Test
@TestMetadata("kt5056.kt")
public void testKt5056() throws Exception {
@@ -1358,6 +1358,12 @@ public class Fir2IrTextTestGenerated extends AbstractFir2IrTextTest {
runTest("compiler/testData/ir/irText/expressions/kt42321.kt");
}
@Test
@TestMetadata("kt44993.kt")
public void testKt44993() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt44993.kt");
}
@Test
@TestMetadata("kt45022.kt")
public void testKt45022() throws Exception {
@@ -8,15 +8,19 @@ package org.jetbrains.kotlin.backend.common.lower
import org.jetbrains.kotlin.backend.common.CommonBackendContext
import org.jetbrains.kotlin.backend.common.FileLoweringPass
import org.jetbrains.kotlin.backend.common.phaser.makeIrFilePhase
import org.jetbrains.kotlin.ir.builders.*
import org.jetbrains.kotlin.ir.builders.createTmpVariable
import org.jetbrains.kotlin.ir.builders.irBlock
import org.jetbrains.kotlin.ir.builders.irGet
import org.jetbrains.kotlin.ir.builders.irIfNull
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.expressions.*
import org.jetbrains.kotlin.ir.expressions.impl.IrTypeOperatorCallImpl
import org.jetbrains.kotlin.ir.symbols.IrVariableSymbol
import org.jetbrains.kotlin.ir.types.IrType
import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.visitors.*
import org.jetbrains.kotlin.ir.util.fileOrNull
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
val ifNullExpressionsFusionPhase =
makeIrFilePhase(
@@ -98,17 +102,17 @@ class IfNullExpressionsFusionLowering(val context: CommonBackendContext) : FileL
(!outer.ifNullExpr.isTrivial() && innerKeepsNull != false && innerDiscardsNonNull != false)
) return this
return inner.createIrBuilder().irBlock {
val ifNull = outer.substitute(inner.ifNullExpr, innerKeepsNull)
val ifNotNull = outer.substitute(inner.ifNotNullExpr, innerDiscardsNonNull)
val ifNull = outer.substitute(inner.ifNullExpr, innerKeepsNull, inner.type)
val ifNotNull = outer.substitute(inner.ifNotNullExpr, innerDiscardsNonNull, inner.type)
+inner.subjectVar
+irIfNull(outer.type, irGet(inner.subjectVar), ifNull, ifNotNull)
}
}
private fun IfNullExpr.substitute(subject: IrExpression, knownNullability: Boolean?): IrExpression =
private fun IfNullExpr.substitute(subject: IrExpression, knownNullability: Boolean?, temporaryVarType: IrType): IrExpression =
when (knownNullability) {
null -> createIrBuilder().irBlock {
val tmp = createTmpVariable(subject)
val tmp = createTmpVariable(subject, irType = temporaryVarType)
val ifNull = ifNullExpr.remap(subjectVar, lazy { tmp })
val ifNotNull = ifNotNullExpr.remap(subjectVar, lazy { tmp })
+irIfNull(type, irGet(tmp), ifNull, ifNotNull)
+15
View File
@@ -0,0 +1,15 @@
// TARGET_BACKEND: JVM
// WITH_RUNTIME
// FILE: kt44993.kt
fun box(): String =
f(KotlinBox(JavaBox()))
fun f(r: KotlinBox<JavaBox>): String =
r?.data?.element!!
class KotlinBox<T>(@JvmField val data: T?)
// FILE: JavaBox.java
public class JavaBox {
public final String element = "OK";
}
+13
View File
@@ -0,0 +1,13 @@
// FIR_IDENTICAL
// WITH_RUNTIME
// SKIP_KT_DUMP
// FILE: kt44993.kt
fun f(r: KotlinBox<JavaBox>): String =
r?.data?.element!!
class KotlinBox<T>(@JvmField val data: T?)
// FILE: JavaBox.java
public class JavaBox {
public final String element = "OK";
}
+66
View File
@@ -0,0 +1,66 @@
FILE fqName:<root> fileName:/kt44993.kt
FUN name:f visibility:public modality:FINAL <> (r:<root>.KotlinBox<<root>.JavaBox>) returnType:kotlin.String
VALUE_PARAMETER name:r index:0 type:<root>.KotlinBox<<root>.JavaBox>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun f (r: <root>.KotlinBox<<root>.JavaBox>): kotlin.String declared in <root>'
CALL 'public final fun CHECK_NOT_NULL <T0> (arg0: T0 of kotlin.internal.ir.CHECK_NOT_NULL?): T0 of kotlin.internal.ir.CHECK_NOT_NULL declared in kotlin.internal.ir' type=kotlin.String origin=EXCLEXCL
<T0>: kotlin.String
arg0: BLOCK type=@[FlexibleNullability] kotlin.String? origin=SAFE_CALL
VAR IR_TEMPORARY_VARIABLE name:tmp_0 type:<root>.JavaBox? [val]
BLOCK type=<root>.JavaBox? origin=SAFE_CALL
VAR IR_TEMPORARY_VARIABLE name:tmp_1 type:<root>.KotlinBox<<root>.JavaBox> [val]
GET_VAR 'r: <root>.KotlinBox<<root>.JavaBox> declared in <root>.f' type=<root>.KotlinBox<<root>.JavaBox> origin=null
WHEN type=<root>.JavaBox? origin=null
BRANCH
if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ
arg0: GET_VAR 'val tmp_1: <root>.KotlinBox<<root>.JavaBox> [val] declared in <root>.f' type=<root>.KotlinBox<<root>.JavaBox> origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: CONST Null type=kotlin.Nothing? value=null
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: CALL 'public final fun <get-data> (): T of <root>.KotlinBox? declared in <root>.KotlinBox' type=<root>.JavaBox? origin=GET_PROPERTY
$this: GET_VAR 'val tmp_1: <root>.KotlinBox<<root>.JavaBox> [val] declared in <root>.f' type=<root>.KotlinBox<<root>.JavaBox> origin=null
WHEN type=@[FlexibleNullability] kotlin.String? origin=null
BRANCH
if: CALL 'public final fun EQEQ (arg0: kotlin.Any?, arg1: kotlin.Any?): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=EQEQ
arg0: GET_VAR 'val tmp_0: <root>.JavaBox? [val] declared in <root>.f' type=<root>.JavaBox? origin=null
arg1: CONST Null type=kotlin.Nothing? value=null
then: CONST Null type=kotlin.Nothing? value=null
BRANCH
if: CONST Boolean type=kotlin.Boolean value=true
then: GET_FIELD 'FIELD IR_EXTERNAL_JAVA_DECLARATION_STUB name:element type:@[FlexibleNullability] kotlin.String? visibility:public [final]' type=@[FlexibleNullability] kotlin.String? origin=GET_PROPERTY
receiver: GET_VAR 'val tmp_0: <root>.JavaBox? [val] declared in <root>.f' type=<root>.JavaBox? origin=null
CLASS CLASS name:KotlinBox modality:FINAL visibility:public superTypes:[kotlin.Any]
$this: VALUE_PARAMETER INSTANCE_RECEIVER name:<this> type:<root>.KotlinBox<T of <root>.KotlinBox>
TYPE_PARAMETER name:T index:0 variance: superTypes:[kotlin.Any?]
CONSTRUCTOR visibility:public <> (data:T of <root>.KotlinBox?) returnType:<root>.KotlinBox<T of <root>.KotlinBox> [primary]
VALUE_PARAMETER name:data index:0 type:T of <root>.KotlinBox?
BLOCK_BODY
DELEGATING_CONSTRUCTOR_CALL 'public constructor <init> () [primary] declared in kotlin.Any'
INSTANCE_INITIALIZER_CALL classDescriptor='CLASS CLASS name:KotlinBox modality:FINAL visibility:public superTypes:[kotlin.Any]'
PROPERTY name:data visibility:public modality:FINAL [val]
FIELD PROPERTY_BACKING_FIELD name:data type:T of <root>.KotlinBox? visibility:public [final]
annotations:
JvmField
EXPRESSION_BODY
GET_VAR 'data: T of <root>.KotlinBox? declared in <root>.KotlinBox.<init>' type=T of <root>.KotlinBox? origin=INITIALIZE_PROPERTY_FROM_PARAMETER
FUN DEFAULT_PROPERTY_ACCESSOR name:<get-data> visibility:public modality:FINAL <> ($this:<root>.KotlinBox<T of <root>.KotlinBox>) returnType:T of <root>.KotlinBox?
correspondingProperty: PROPERTY name:data visibility:public modality:FINAL [val]
$this: VALUE_PARAMETER name:<this> type:<root>.KotlinBox<T of <root>.KotlinBox>
BLOCK_BODY
RETURN type=kotlin.Nothing from='public final fun <get-data> (): T of <root>.KotlinBox? declared in <root>.KotlinBox'
GET_FIELD 'FIELD PROPERTY_BACKING_FIELD name:data type:T of <root>.KotlinBox? visibility:public [final]' type=T of <root>.KotlinBox? origin=null
receiver: GET_VAR '<this>: <root>.KotlinBox<T of <root>.KotlinBox> declared in <root>.KotlinBox.<get-data>' type=<root>.KotlinBox<T of <root>.KotlinBox> origin=null
FUN FAKE_OVERRIDE name:equals visibility:public modality:OPEN <> ($this:kotlin.Any, other:kotlin.Any?) returnType:kotlin.Boolean [fake_override,operator]
overridden:
public open fun equals (other: kotlin.Any?): kotlin.Boolean [operator] declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
VALUE_PARAMETER name:other index:0 type:kotlin.Any?
FUN FAKE_OVERRIDE name:hashCode visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.Int [fake_override]
overridden:
public open fun hashCode (): kotlin.Int declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
FUN FAKE_OVERRIDE name:toString visibility:public modality:OPEN <> ($this:kotlin.Any) returnType:kotlin.String [fake_override]
overridden:
public open fun toString (): kotlin.String declared in kotlin.Any
$this: VALUE_PARAMETER name:<this> type:kotlin.Any
@@ -36540,6 +36540,12 @@ public class BlackBoxCodegenTestGenerated extends AbstractBlackBoxCodegenTest {
runTest("compiler/testData/codegen/box/regressions/kt4281.kt");
}
@Test
@TestMetadata("kt44993.kt")
public void testKt44993() throws Exception {
runTest("compiler/testData/codegen/box/regressions/kt44993.kt");
}
@Test
@TestMetadata("kt5056.kt")
public void testKt5056() throws Exception {
@@ -36540,6 +36540,12 @@ public class IrBlackBoxCodegenTestGenerated extends AbstractIrBlackBoxCodegenTes
runTest("compiler/testData/codegen/box/regressions/kt4281.kt");
}
@Test
@TestMetadata("kt44993.kt")
public void testKt44993() throws Exception {
runTest("compiler/testData/codegen/box/regressions/kt44993.kt");
}
@Test
@TestMetadata("kt5056.kt")
public void testKt5056() throws Exception {
@@ -1358,6 +1358,12 @@ public class IrTextTestGenerated extends AbstractIrTextTest {
runTest("compiler/testData/ir/irText/expressions/kt42321.kt");
}
@Test
@TestMetadata("kt44993.kt")
public void testKt44993() throws Exception {
runTest("compiler/testData/ir/irText/expressions/kt44993.kt");
}
@Test
@TestMetadata("kt45022.kt")
public void testKt45022() throws Exception {
@@ -29110,6 +29110,11 @@ public class LightAnalysisModeTestGenerated extends AbstractLightAnalysisModeTes
runTest("compiler/testData/codegen/box/regressions/kt4281.kt");
}
@TestMetadata("kt44993.kt")
public void testKt44993() throws Exception {
runTest("compiler/testData/codegen/box/regressions/kt44993.kt");
}
@TestMetadata("kt5056.kt")
public void testKt5056() throws Exception {
runTest("compiler/testData/codegen/box/regressions/kt5056.kt");