From 3210a2a9508803b908788881d061f0b7812ddeaf Mon Sep 17 00:00:00 2001 From: Ivan Kochurkin Date: Thu, 14 Oct 2021 15:00:42 +0300 Subject: [PATCH] [FIR2IR] Recognize postfix inc/dec pattern from block statements --- .../kotlin/fir/backend/Fir2IrVisitor.kt | 34 +++++++++---------- .../forLoop/forInDownToCharMinValue.kt | 2 -- .../forLoop/forInDownToIntMinValue.kt | 2 -- .../forInDownToLongConstNoUnderflow.kt | 2 -- .../forLoop/forInDownToLongMinValue.kt | 2 -- .../bytecodeText/forLoop/forInObjectArray.kt | 2 -- .../forLoop/forInPrimitiveArray.kt | 2 -- .../forLoop/forInRangeToCharMaxValue.kt | 2 -- .../forLoop/forInRangeToIntMaxValue.kt | 2 -- .../forLoop/forInRangeToLongMaxValue.kt | 2 -- .../forInUntil/forInUntilCharMaxValue.kt | 2 -- .../forInUntil/forInUntilCharMinValue.kt | 2 -- .../forInUntil/forInUntilIntMaxValue.kt | 2 -- .../forInUntil/forInUntilIntMinValue.kt | 2 -- .../forInUntil/forInUntilLongMaxValue.kt | 2 -- .../forInUntil/forInUntilLongMinValue.kt | 2 -- .../unsigned/forInDownToUIntMinValue.kt | 1 - .../unsigned/forInDownToULongMinValue.kt | 1 - .../unsigned/forInRangeToUIntMaxValue.kt | 1 - .../unsigned/forInRangeToULongMaxValue.kt | 1 - .../unsigned/forInUntilUIntMaxValue.kt | 1 - .../unsigned/forInUntilUIntMinValue.kt | 1 - .../unsigned/forInUntilULongMaxValue.kt | 1 - .../unsigned/forInUntilULongMinValue.kt | 1 - .../expressions/incrementDecrement.fir.ir.txt | 4 +-- .../expressions/whileDoWhile.fir.ir.txt | 2 +- 26 files changed, 19 insertions(+), 59 deletions(-) diff --git a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt index b4ab8a8860a..018b15c3dad 100644 --- a/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt +++ b/compiler/fir/fir2ir/src/org/jetbrains/kotlin/fir/backend/Fir2IrVisitor.kt @@ -46,6 +46,7 @@ import org.jetbrains.kotlin.ir.util.defaultType import org.jetbrains.kotlin.ir.util.parentClassOrNull import org.jetbrains.kotlin.lexer.KtTokens import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.name.SpecialNames import org.jetbrains.kotlin.psi.KtBinaryExpression import org.jetbrains.kotlin.psi.KtForExpression import org.jetbrains.kotlin.util.OperatorNameConventions @@ -590,28 +591,25 @@ class Fir2IrVisitor( } private fun FirBlock.getBlockOrigin(): IrStatementOrigin? { - if (source?.kind !is FirFakeSourceElementKind.DesugaredIncrementOrDecrement) return null + if (statements.size != 3) return null - val isPrefix: Boolean - val incrementStatement = when (statements.size) { - 2 -> { - isPrefix = true - statements[0] - } - 3 -> { - isPrefix = false - statements[1] - } - else -> return null - } + val statement0 = statements[0] + if (statement0 !is FirProperty || !statement0.isLocal) return null + val unarySymbol = statement0.symbol + if (unarySymbol.callableId.callableName != SpecialNames.UNARY) return null + val statement2 = statements[2] + if (statement2 !is FirPropertyAccessExpression) return null + if (statement2.calleeReference.toResolvedCallableSymbol() != unarySymbol) return null + + val incrementStatement = statements[1] if (incrementStatement !is FirVariableAssignment) return null - val origin = incrementStatement.getIrAssignmentOrigin(isPrefix) - return if (origin == IrStatementOrigin.EQ) - null - else - origin + val variable = statement0.initializer?.toResolvedCallableSymbol() ?: return null + if (incrementStatement.lValue.toResolvedCallableSymbol() != variable) return null + + val origin = incrementStatement.getIrAssignmentOrigin() + return if (origin == IrStatementOrigin.EQ) null else origin } override fun visitErrorExpression(errorExpression: FirErrorExpression, data: Any?): IrElement { diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInDownToCharMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInDownToCharMinValue.kt index 7bfaf3da7a1..a3bc37dac67 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInDownToCharMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInDownToCharMinValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInDownToIntMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInDownToIntMinValue.kt index f63c3a7256b..28d399629c2 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInDownToIntMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInDownToIntMinValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInDownToLongConstNoUnderflow.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInDownToLongConstNoUnderflow.kt index d6834e478b5..bd02f78b742 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInDownToLongConstNoUnderflow.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInDownToLongConstNoUnderflow.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInDownToLongMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInDownToLongMinValue.kt index 43371eb568f..e5325216028 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInDownToLongMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInDownToLongMinValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInObjectArray.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInObjectArray.kt index b44307c5fb0..479119371ca 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInObjectArray.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInObjectArray.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInPrimitiveArray.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInPrimitiveArray.kt index a5e89f91a74..f46a495bb4b 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInPrimitiveArray.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInPrimitiveArray.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToCharMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToCharMaxValue.kt index 07c2026e3bc..f1f6e4455c2 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToCharMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToCharMaxValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToIntMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToIntMaxValue.kt index f9d4e641796..83f7cf28d05 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToIntMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToIntMaxValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToLongMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToLongMaxValue.kt index 88e5aae7969..135c9ff364a 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToLongMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInRangeToLongMaxValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilCharMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilCharMaxValue.kt index 79423530c94..7242613b943 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilCharMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilCharMaxValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilCharMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilCharMinValue.kt index 15bae235dfb..e3654416178 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilCharMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilCharMinValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilIntMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilIntMaxValue.kt index 6394346080f..0d51650cf05 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilIntMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilIntMaxValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilIntMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilIntMinValue.kt index 962da644a37..85e0cb87ff6 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilIntMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilIntMinValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMaxValue.kt index 29e2a14bd27..b71f659f072 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMaxValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMinValue.kt index 5c8ccd7b8e6..234e0e4a4ce 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/forInUntil/forInUntilLongMinValue.kt @@ -1,5 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR - // IMPORTANT! // Please, when your changes cause failures in bytecodeText tests for 'for' loops, // examine the resulting bytecode shape carefully. diff --git a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt index 7d1b03e7c93..7ba985ab4f8 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToUIntMinValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // IMPORTANT! diff --git a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToULongMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToULongMinValue.kt index 53bfb08e03e..5e6ecfedc52 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToULongMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInDownToULongMinValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // IMPORTANT! diff --git a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInRangeToUIntMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInRangeToUIntMaxValue.kt index 96f9dd17099..a4113d4c508 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInRangeToUIntMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInRangeToUIntMaxValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // IMPORTANT! diff --git a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInRangeToULongMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInRangeToULongMaxValue.kt index bf00ec29b91..51350602939 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInRangeToULongMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInRangeToULongMaxValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // IMPORTANT! diff --git a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilUIntMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilUIntMaxValue.kt index 46fc00c3233..f91969c7bba 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilUIntMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilUIntMaxValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // IMPORTANT! diff --git a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilUIntMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilUIntMinValue.kt index e7d50b82a4c..ae8bcbfe30a 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilUIntMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilUIntMinValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // IMPORTANT! diff --git a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilULongMaxValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilULongMaxValue.kt index f52bb4df903..b301a41dc43 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilULongMaxValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilULongMaxValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // IMPORTANT! diff --git a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilULongMinValue.kt b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilULongMinValue.kt index 086ed5482ab..069f255aa4d 100644 --- a/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilULongMinValue.kt +++ b/compiler/testData/codegen/bytecodeText/forLoop/unsigned/forInUntilULongMinValue.kt @@ -1,4 +1,3 @@ -// IGNORE_BACKEND_FIR: JVM_IR // WITH_RUNTIME // IMPORTANT! diff --git a/compiler/testData/ir/irText/expressions/incrementDecrement.fir.ir.txt b/compiler/testData/ir/irText/expressions/incrementDecrement.fir.ir.txt index cac8139d58d..c37266d58ef 100644 --- a/compiler/testData/ir/irText/expressions/incrementDecrement.fir.ir.txt +++ b/compiler/testData/ir/irText/expressions/incrementDecrement.fir.ir.txt @@ -32,13 +32,13 @@ FILE fqName: fileName:/incrementDecrement.kt VAR name:x type:kotlin.Int [var] CONST Int type=kotlin.Int value=0 VAR name:x1 type:kotlin.Int [val] - BLOCK type=kotlin.Int origin=PREFIX_INCR + BLOCK type=kotlin.Int origin=null SET_VAR 'var x: kotlin.Int [var] declared in .testVarPrefix' type=kotlin.Unit origin=PREFIX_INCR CALL 'public final fun inc (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null $this: GET_VAR 'var x: kotlin.Int [var] declared in .testVarPrefix' type=kotlin.Int origin=null GET_VAR 'var x: kotlin.Int [var] declared in .testVarPrefix' type=kotlin.Int origin=null VAR name:x2 type:kotlin.Int [val] - BLOCK type=kotlin.Int origin=PREFIX_DECR + BLOCK type=kotlin.Int origin=null SET_VAR 'var x: kotlin.Int [var] declared in .testVarPrefix' type=kotlin.Unit origin=PREFIX_DECR CALL 'public final fun dec (): kotlin.Int [operator] declared in kotlin.Int' type=kotlin.Int origin=null $this: GET_VAR 'var x: kotlin.Int [var] declared in .testVarPrefix' type=kotlin.Int origin=null diff --git a/compiler/testData/ir/irText/expressions/whileDoWhile.fir.ir.txt b/compiler/testData/ir/irText/expressions/whileDoWhile.fir.ir.txt index 8aeb6a9081f..2eff6adc31a 100644 --- a/compiler/testData/ir/irText/expressions/whileDoWhile.fir.ir.txt +++ b/compiler/testData/ir/irText/expressions/whileDoWhile.fir.ir.txt @@ -23,7 +23,7 @@ FILE fqName: fileName:/whileDoWhile.kt condition: CALL 'public final fun less (arg0: kotlin.Int, arg1: kotlin.Int): kotlin.Boolean declared in kotlin.internal.ir' type=kotlin.Boolean origin=LT arg0: GET_VAR 'var x: kotlin.Int [var] declared in .test' type=kotlin.Int origin=null arg1: CONST Int type=kotlin.Int value=10 - body: BLOCK type=kotlin.Int origin=null + body: BLOCK type=kotlin.Int origin=POSTFIX_INCR VAR IR_TEMPORARY_VARIABLE name:tmp_1 type:kotlin.Int [val] GET_VAR 'var x: kotlin.Int [var] declared in .test' type=kotlin.Int origin=null SET_VAR 'var x: kotlin.Int [var] declared in .test' type=kotlin.Unit origin=POSTFIX_INCR