# Air: direct representation of ranges in switch cases · gitcafe/zig

[View on GitCafe](https://git.cafe/gitcafe/zig/commit/1b000b90c9a7abde3aeacf29cef73a877da237e1)

Repository: [gitcafe/zig](https://git.cafe/gitcafe/zig)

Visibility: public

Requested revision: 1b000b90c9a7abde3aeacf29cef73a877da237e1

Requested commit: 1b000b90c9a7abde3aeacf29cef73a877da237e1

Commit: 1b000b90c9a7abde3aeacf29cef73a877da237e1

Tree: 837c7481d97c1bbf8d165f2b74b3ae77fbcd2f09

Author: mlugg

Committer: mlugg

## Message

```
Air: direct representation of ranges in switch cases

This commit modifies the representation of the AIR `switch_br`
instruction to represent ranges in cases. Previously, Sema emitted
different AIR in the case of a range, where the `else` branch of the
`switch_br` contained a simple `cond_br` for each such case which did a
simple range check (`x > a and x < b`). Not only does this add
complexity to Sema, which we would like to minimize, but it also gets in
the way of the implementation of #8220. That proposal turns certain
`switch` statements into a looping construct, and for optimization
purposes, we want to lower this to AIR fairly directly (i.e. without
involving a `loop` instruction). That means we would ideally like a
single instruction to represent the entire `switch` statement, so that
we can dispatch back to it with a different operand as in #8220. This is
not really possible to do correctly under the status quo system.

This commit implements lowering of this new `switch_br` usage in the
LLVM and C backends. The C backend just turns any case containing ranges
entirely into conditionals, as before. The LLVM backend is a little
smarter, and puts scalar items into the `switch` instruction, only using
conditionals for the range cases (which direct to the same bb). All
remaining self-hosted backends are temporarily regressed in the presence
of switch range cases. This functionality will be restored for at least
the x86_64 backend before merge.

```

## Parents

- [49ad51b2feacad394e05d7b5c87c5020c3bc0f5e](https://git.cafe/gitcafe/zig/commit/49ad51b2feacad394e05d7b5c87c5020c3bc0f5e?format=markdown)

[Source at this commit](https://git.cafe/gitcafe/zig/tree/1b000b90c9a7abde3aeacf29cef73a877da237e1?format=markdown)
