diff --git a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/SwitchGenerator.kt b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/SwitchGenerator.kt index 35d4a97f5cb93..c76491b6fe279 100644 --- a/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/SwitchGenerator.kt +++ b/compiler/ir/backend.jvm/codegen/src/org/jetbrains/kotlin/backend/jvm/codegen/SwitchGenerator.kt @@ -131,6 +131,15 @@ class SwitchGenerator(private val expression: IrWhen, private val data: BlockInf cases ) } + subject is IrGetValue && areConstCharComparisons(conditions) -> { + val cases = extractSwitchCasesAndFilterUnreachableLabels(callToLabels, expressionToLabels) + IntSwitch( + subject, + elseExpression, + expressionToLabels, + cases.map { ValueToLabel((it.value as Char).code, it.label) } + ) + } areConstStringComparisons(conditions) -> { val cases = extractSwitchCasesAndFilterUnreachableLabels(callToLabels, expressionToLabels) StringSwitch( @@ -193,6 +202,10 @@ class SwitchGenerator(private val expression: IrWhen, private val data: BlockInf return checkTypeSpecifics(conditions, { it.isInt() }, { it.kind == IrConstKind.Int }) } + private fun areConstCharComparisons(conditions: List): Boolean { + return checkTypeSpecifics(conditions, { it.isChar() }, { it.kind == IrConstKind.Char }) + } + private fun areConstStringComparisons(conditions: List): Boolean { return checkTypeSpecifics( conditions, diff --git a/compiler/testData/codegen/bytecodeText/when/lookupSwitch.kt b/compiler/testData/codegen/bytecodeText/when/lookupSwitch.kt index d0d154ce0b541..a4b3f21adce2e 100644 --- a/compiler/testData/codegen/bytecodeText/when/lookupSwitch.kt +++ b/compiler/testData/codegen/bytecodeText/when/lookupSwitch.kt @@ -7,5 +7,14 @@ fun foo(x: Int): String { } } -// 1 LOOKUPSWITCH +fun foo(p: Char): String { + return when (p) { + 'a' -> "a" + 'g' -> "g" + 'h' -> "h" + else -> "else" + } +} + +// 2 LOOKUPSWITCH // 0 TABLESWITCH \ No newline at end of file diff --git a/compiler/testData/codegen/bytecodeText/when/tableSwitch.kt b/compiler/testData/codegen/bytecodeText/when/tableSwitch.kt index 7b6e77b90af25..7f67b56a5b664 100644 --- a/compiler/testData/codegen/bytecodeText/when/tableSwitch.kt +++ b/compiler/testData/codegen/bytecodeText/when/tableSwitch.kt @@ -7,5 +7,19 @@ fun foo(x: Int): String { } } +fun foo(p: Char): String { + return when (p) { + 'a' -> "a" + 'b' -> "b" + 'c' -> "c" + 'd' -> "d" + 'e' -> "e" + 'f' -> "f" + 'g' -> "g" + 'h' -> "h" + else -> "else" + } +} + // 0 LOOKUPSWITCH -// 1 TABLESWITCH \ No newline at end of file +// 2 TABLESWITCH \ No newline at end of file