Skip to content

Commit 3afe02c

Browse files
vsukharevSpace Team
authored andcommitted
[IR][Native] Remove redundant implicit casts
^KT-80953
1 parent 46d9321 commit 3afe02c

File tree

3 files changed

+42
-1
lines changed

3 files changed

+42
-1
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 2010-2025 JetBrains s.r.o. and Kotlin Programming Language contributors.
3+
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
4+
*/
5+
6+
package org.jetbrains.kotlin.backend.common.lower
7+
8+
import org.jetbrains.kotlin.backend.common.FileLoweringPass
9+
import org.jetbrains.kotlin.backend.common.LoweringContext
10+
import org.jetbrains.kotlin.ir.declarations.IrFile
11+
import org.jetbrains.kotlin.ir.expressions.IrExpression
12+
import org.jetbrains.kotlin.ir.expressions.IrTypeOperator
13+
import org.jetbrains.kotlin.ir.expressions.IrTypeOperatorCall
14+
import org.jetbrains.kotlin.ir.visitors.IrElementTransformerVoid
15+
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
16+
17+
/**
18+
* Removes redundant casts like
19+
* - `TYPE_OP type=kotlin.Int origin=IMPLICIT_CAST typeOperand=kotlin.Int`
20+
*/
21+
class RedundantCastsRemoverLowering(val context: LoweringContext) : FileLoweringPass {
22+
override fun lower(irFile: IrFile) {
23+
irFile.transformChildrenVoid(Transformer())
24+
}
25+
26+
private class Transformer : IrElementTransformerVoid() {
27+
override fun visitTypeOperator(expression: IrTypeOperatorCall): IrExpression {
28+
if (expression.operator == IrTypeOperator.IMPLICIT_CAST && expression.type == expression.argument.type) {
29+
// Replace a redundant implicit cast with its argument
30+
return super.visitExpression(expression.argument)
31+
}
32+
return super.visitTypeOperator(expression)
33+
}
34+
}
35+
}

kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/NativeLoweringPhases.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,12 @@ private val assertionRemoverPhase = createFileLoweringPhase(
557557
prerequisite = setOf(assertionWrapperPhase),
558558
)
559559

560+
internal val redundantCastsRemoverPhase = createFileLoweringPhase(
561+
lowering = ::RedundantCastsRemoverLowering,
562+
name = "RedundantCastsRemoverLowering",
563+
prerequisite = setOf(inlineAllFunctionsPhase),
564+
)
565+
560566
internal val constEvaluationPhase = createFileLoweringPhase(
561567
lowering = { context: Context ->
562568
val configuration = IrInterpreterConfiguration(printOnlyExceptionMessage = true)

kotlin-native/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/driver/phases/TopLevelPhases.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ internal fun <C : NativeBackendPhaseContext> PhaseEngine<C>.runBackend(backendCo
163163
fragmentWithState.forEach { (fragment, state) -> state.runSpecifiedLowerings(fragment, getLoweringsUpToAndIncludingSyntheticAccessors()) }
164164
fragmentWithState.forEach { (fragment, state) -> state.runSpecifiedLowerings(fragment, validateIrAfterInliningOnlyPrivateFunctions) }
165165
fragmentWithState.forEach { (fragment, state) -> state.runSpecifiedLowerings(fragment, listOf(inlineAllFunctionsPhase)) }
166-
fragmentWithState.forEach { (fragment, state) -> state.runSpecifiedLowerings(fragment, listOf(specialObjCValidationPhase)) }
166+
fragmentWithState.forEach { (fragment, state) -> state.runSpecifiedLowerings(fragment, listOf(specialObjCValidationPhase, redundantCastsRemoverPhase)) }
167167
}
168168

169169
fragmentWithState.forEach { (fragment, state) -> state.runSpecifiedLowerings(fragment, validateIrAfterInliningAllFunctions) }

0 commit comments

Comments
 (0)