[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
@@ -1,6 +1,5 @@
// WITH_RUNTIME
// CHECK_CASES_COUNT: function=foo count=3 TARGET_BACKENDS=JS
// CHECK_CASES_COUNT: function=foo count=0 IGNORED_BACKENDS=JS
// CHECK_CASES_COUNT: function=foo count=3
// CHECK_IF_COUNT: function=foo count=0
import kotlin.test.assertEquals