diff options
| author | Björn Steinbrink <bsteinbr@gmail.com> | 2015-09-25 11:02:51 +0200 |
|---|---|---|
| committer | Björn Steinbrink <bsteinbr@gmail.com> | 2015-09-25 11:09:19 +0200 |
| commit | 91f7c60d2d77423ed8f163beb6f76b92de03a09f (patch) | |
| tree | 995827f4c0a9a95bdc84305a49662f5d5b5ec72d /src/test/codegen | |
| parent | 5ca60d94316bd56f412ef4c13292237e206babf1 (diff) | |
| download | rust-91f7c60d2d77423ed8f163beb6f76b92de03a09f.tar.gz rust-91f7c60d2d77423ed8f163beb6f76b92de03a09f.zip | |
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:
```
Diffstat (limited to 'src/test/codegen')
| -rw-r--r-- | src/test/codegen/match.rs | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/test/codegen/match.rs b/src/test/codegen/match.rs new file mode 100644 index 00000000000..ac47f6082e3 --- /dev/null +++ b/src/test/codegen/match.rs @@ -0,0 +1,30 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// compile-flags: -C no-prepopulate-passes + +#![crate_type = "lib"] + +pub enum E { + A, + B, +} + +// CHECK-LABEL: @exhaustive_match +#[no_mangle] +pub fn exhaustive_match(e: E) { +// CHECK: switch{{.*}}, label %[[DEFAULT:[a-zA-Z0-9_]+]] +// CHECK: [[DEFAULT]]: +// CHECK-NEXT: unreachable + match e { + E::A => (), + E::B => (), + } +} |
