]> git.lizzy.rs Git - rust.git/commit
Tell LLVM when a match is exhaustive
authorBjörn Steinbrink <bsteinbr@gmail.com>
Fri, 25 Sep 2015 09:02:51 +0000 (11:02 +0200)
committerBjörn Steinbrink <bsteinbr@gmail.com>
Fri, 25 Sep 2015 09:09:19 +0000 (11:09 +0200)
commit91f7c60d2d77423ed8f163beb6f76b92de03a09f
tree995827f4c0a9a95bdc84305a49662f5d5b5ec72d
parent5ca60d94316bd56f412ef4c13292237e206babf1
Tell LLVM when a match is exhaustive

By putting an "unreachable" instruction into the default arm of a switch
instruction we can let LLVM know that the match is exhaustive, allowing
for better optimizations.

For example, this match:
```rust
pub enum Enum {
    One,
    Two,
    Three,
}

impl Enum {
    pub fn get_disc(self) -> u8 {
        match self {
            Enum::One => 0,
            Enum::Two => 1,
            Enum::Three => 2,
        }
    }
}
```

Currently compiles to this on x86_64:
```asm
  .cfi_startproc
  movzbl  %dil, %ecx
  cmpl  $1, %ecx
  setne %al
  testb %cl, %cl
  je  .LBB0_2
  incb  %al
  movb  %al, %dil
.LBB0_2:
  movb  %dil, %al
  retq
.Lfunc_end0:
```

But with this change we get:
```asm
  .cfi_startproc
  movb  %dil, %al
  retq
.Lfunc_end0:
```
src/librustc_trans/trans/_match.rs
src/test/codegen/match.rs [new file with mode: 0644]