[JS IR] Optimize pattern-matching of enums into comparing their ordinals

For this code:
```
enum class Season {
    WINTER,
    SPRING,
    SUMMER,
    AUTUMN
}

fun bar1(x : Season) : String {
    return when (x) {
        Season.WINTER, Season.SPRING -> "winter_spring"
        Season.SUMMER -> "summer"
        else -> "autumn"
    }
}
```

previously we generated this:
```
function foo(x) {
    var tmp0_subject = x;
    return (tmp0_subject.equals(Season_WINTER_getInstance())
        ? true
        : tmp0_subject.equals(Season_SPRING_getInstance()))
            ? 'winter_spring'
            : tmp0_subject.equals(Season_SUMMER_getInstance())
                ? 'summer'
                : 'autumn';
}
```

And now we generated this:
```
function bar2(x) {
  var tmp0_subject = x;
  var tmp0 = tmp0_subject._get_ordinal__0_k$();
  var tmp;
  switch (tmp0) {
    case 0:
    case 1:
      tmp = 'winter_spring';
      break;
    case 2:
      tmp = 'summer';
      break;
    case 3:
      tmp = 'autumn';
      break;
    default:
      noWhenBranchMatchedException();
      break;
  }
  return tmp;
}
```
This commit is contained in:
Sergej Jaskiewicz
2021-10-12 15:50:49 +03:00
committed by Space
parent 533eb589cb
commit 55ae6d1f3e
12 changed files with 45 additions and 44 deletions
@@ -18,10 +18,7 @@ import org.jetbrains.kotlin.ir.expressions.impl.IrGetValueImpl
import org.jetbrains.kotlin.ir.types.classifierOrNull import org.jetbrains.kotlin.ir.types.classifierOrNull
import org.jetbrains.kotlin.ir.types.getClass import org.jetbrains.kotlin.ir.types.getClass
import org.jetbrains.kotlin.ir.types.isNullable import org.jetbrains.kotlin.ir.types.isNullable
import org.jetbrains.kotlin.ir.util.dump import org.jetbrains.kotlin.ir.util.*
import org.jetbrains.kotlin.ir.util.getPropertyGetter
import org.jetbrains.kotlin.ir.util.isNullConst
import org.jetbrains.kotlin.ir.util.parentAsClass
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
/** Look for when-constructs where subject is enum entry. /** Look for when-constructs where subject is enum entry.
@@ -53,9 +50,14 @@ open class EnumWhenLowering(protected val context: CommonBackendContext) : IrEle
return super.visitBlock(expression) return super.visitBlock(expression)
} }
val subject = expression.statements[0] val subject = expression.statements[0]
if (subject !is IrVariable || subject.type.getClass()?.kind != ClassKind.ENUM_CLASS) { if (subject !is IrVariable) {
return super.visitBlock(expression) return super.visitBlock(expression)
} }
val subjectClass = subject.type.getClass()
if (subjectClass == null || subjectClass.kind != ClassKind.ENUM_CLASS || subjectClass.isEffectivelyExternal()) {
return super.visitBlock(expression)
}
// Will be initialized only when we found a branch that compares // Will be initialized only when we found a branch that compares
// subject with compile-time known enum entry. // subject with compile-time known enum entry.
val subjectOrdinalProvider = lazy { val subjectOrdinalProvider = lazy {
@@ -385,6 +385,12 @@ private val forLoopsLoweringPhase = makeBodyLoweringPhase(
description = "[Optimization] For loops lowering" description = "[Optimization] For loops lowering"
) )
private val enumWhenPhase = makeJsModulePhase(
::EnumWhenLowering,
name = "EnumWhenLowering",
description = "Replace `when` subjects of enum types with their ordinals"
).toModuleLowering()
private val propertyLazyInitLoweringPhase = makeBodyLoweringPhase( private val propertyLazyInitLoweringPhase = makeBodyLoweringPhase(
::PropertyLazyInitLowering, ::PropertyLazyInitLowering,
name = "PropertyLazyInitLowering", name = "PropertyLazyInitLowering",
@@ -832,6 +838,7 @@ private val loweringList = listOf<Lowering>(
initializersCleanupLoweringPhase, initializersCleanupLoweringPhase,
kotlinNothingValueExceptionPhase, kotlinNothingValueExceptionPhase,
// Common prefix ends // Common prefix ends
enumWhenPhase,
enumEntryInstancesLoweringPhase, enumEntryInstancesLoweringPhase,
enumEntryInstancesBodyLoweringPhase, enumEntryInstancesBodyLoweringPhase,
enumClassCreateInitializerLoweringPhase, enumClassCreateInitializerLoweringPhase,
@@ -1,12 +1,8 @@
// WITH_RUNTIME // WITH_RUNTIME
// CHECK_CASES_COUNT: function=bar1 count=6 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=bar1 count=6
// CHECK_CASES_COUNT: function=bar1 count=0 IGNORED_BACKENDS=JS // CHECK_IF_COUNT: function=bar1 count=0
// CHECK_IF_COUNT: function=bar1 count=0 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=bar2 count=6
// CHECK_IF_COUNT: function=bar1 count=2 IGNORED_BACKENDS=JS // CHECK_IF_COUNT: function=bar2 count=0
// CHECK_CASES_COUNT: function=bar2 count=6 TARGET_BACKENDS=JS
// CHECK_CASES_COUNT: function=bar2 count=0 IGNORED_BACKENDS=JS
// CHECK_IF_COUNT: function=bar2 count=0 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=bar2 count=3 IGNORED_BACKENDS=JS
import kotlin.test.assertEquals import kotlin.test.assertEquals
@@ -1,8 +1,7 @@
// WITH_RUNTIME // WITH_RUNTIME
// CHECK_CASES_COUNT: function=bar count=3 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=bar count=3 TARGET_BACKENDS=JS
// CHECK_CASES_COUNT: function=bar count=0 IGNORED_BACKENDS=JS // CHECK_CASES_COUNT: function=bar count=4 IGNORED_BACKENDS=JS
// CHECK_IF_COUNT: function=bar count=0 TARGET_BACKENDS=JS // CHECK_IF_COUNT: function=bar count=0
// CHECK_IF_COUNT: function=bar count=2 IGNORED_BACKENDS=JS
import kotlin.test.assertEquals import kotlin.test.assertEquals
@@ -1,6 +1,5 @@
// WITH_RUNTIME // WITH_RUNTIME
// CHECK_CASES_COUNT: function=foo count=3 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=foo count=3
// CHECK_CASES_COUNT: function=foo count=0 IGNORED_BACKENDS=JS
// CHECK_IF_COUNT: function=foo count=0 // CHECK_IF_COUNT: function=foo count=0
import kotlin.test.assertEquals import kotlin.test.assertEquals
@@ -1,11 +1,8 @@
// WITH_RUNTIME // WITH_RUNTIME
// CHECK_CASES_COUNT: function=bar1 count=3 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=bar1 count=3
// CHECK_CASES_COUNT: function=bar1 count=0 IGNORED_BACKENDS=JS
// CHECK_IF_COUNT: function=bar1 count=0 // CHECK_IF_COUNT: function=bar1 count=0
// CHECK_CASES_COUNT: function=bar2 count=4 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=bar2 count=4
// CHECK_CASES_COUNT: function=bar2 count=0 IGNORED_BACKENDS=JS // CHECK_IF_COUNT: function=bar2 count=0
// CHECK_IF_COUNT: function=bar2 count=0 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=bar2 count=3 IGNORED_BACKENDS=JS
import kotlin.test.assertEquals import kotlin.test.assertEquals
@@ -1,7 +1,5 @@
// CHECK_CASES_COUNT: function=box count=6 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=box count=6
// CHECK_CASES_COUNT: function=box count=0 IGNORED_BACKENDS=JS // CHECK_IF_COUNT: function=box count=1
// CHECK_IF_COUNT: function=box count=1 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=box count=7 IGNORED_BACKENDS=JS
enum class En { A, B, C } enum class En { A, B, C }
@@ -1,7 +1,5 @@
// CHECK_CASES_COUNT: function=box count=18 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=box count=18
// CHECK_CASES_COUNT: function=box count=0 IGNORED_BACKENDS=JS // CHECK_IF_COUNT: function=box count=3
// CHECK_IF_COUNT: function=box count=3 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=box count=21 IGNORED_BACKENDS=JS
enum class En { A, B, C } enum class En { A, B, C }
@@ -1,8 +1,12 @@
// WITH_RUNTIME // WITH_RUNTIME
// CHECK_CASES_COUNT: function=foo1 count=0 // CHECK_CASES_COUNT: function=foo1 count=0 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=foo1 count=2 // CHECK_CASES_COUNT: function=foo1 count=4 IGNORED_BACKENDS=JS
// CHECK_CASES_COUNT: function=foo2 count=0 // CHECK_IF_COUNT: function=foo1 count=2 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=foo2 count=2 // CHECK_IF_COUNT: function=foo1 count=0 IGNORED_BACKENDS=JS
// CHECK_CASES_COUNT: function=foo2 count=0 TARGET_BACKENDS=JS
// CHECK_CASES_COUNT: function=foo2 count=3 IGNORED_BACKENDS=JS
// CHECK_IF_COUNT: function=foo2 count=2 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=foo2 count=0 IGNORED_BACKENDS=JS
import kotlin.test.assertEquals import kotlin.test.assertEquals
@@ -1,5 +1,7 @@
// CHECK_CASES_COUNT: function=test count=0 // CHECK_CASES_COUNT: function=test count=0 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=test count=3 // CHECK_CASES_COUNT: function=test count=3 IGNORED_BACKENDS=JS
// CHECK_IF_COUNT: function=test count=3 TARGET_BACKENDS=JS
// CHECK_IF_COUNT: function=test count=0 IGNORED_BACKENDS=JS
enum class E { enum class E {
A, A,
@@ -1,12 +1,10 @@
// WITH_RUNTIME // WITH_RUNTIME
// CHECK_CASES_COUNT: function=bar1 count=3 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=bar1 count=3 TARGET_BACKENDS=JS
// CHECK_CASES_COUNT: function=bar1 count=0 IGNORED_BACKENDS=JS // CHECK_CASES_COUNT: function=bar1 count=4 IGNORED_BACKENDS=JS
// CHECK_IF_COUNT: function=bar1 count=0 TARGET_BACKENDS=JS // CHECK_IF_COUNT: function=bar1 count=0
// CHECK_IF_COUNT: function=bar1 count=3 IGNORED_BACKENDS=JS
// CHECK_CASES_COUNT: function=bar2 count=4 TARGET_BACKENDS=JS // CHECK_CASES_COUNT: function=bar2 count=4 TARGET_BACKENDS=JS
// CHECK_CASES_COUNT: function=bar2 count=0 IGNORED_BACKENDS=JS // CHECK_CASES_COUNT: function=bar2 count=5 IGNORED_BACKENDS=JS
// CHECK_IF_COUNT: function=bar2 count=0 TARGET_BACKENDS=JS // CHECK_IF_COUNT: function=bar2 count=0
// CHECK_IF_COUNT: function=bar2 count=4 IGNORED_BACKENDS=JS
import kotlin.test.assertEquals import kotlin.test.assertEquals
@@ -1,4 +1,5 @@
// EXPECTED_REACHABLE_NODES: 1291 // EXPECTED_REACHABLE_NODES: 1291
// CHECK_IF_COUNT: function=foo count=2
external enum class X { external enum class X {
A, B A, B
} }