forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#120650 - clubby789:switchint-const, r=saethlin
Use `br` instead of a conditional when switching on a constant boolean r? `@ghost`
- Loading branch information
Showing
2 changed files
with
72 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
//@ compile-flags: -Zmir-opt-level=0 -C no-prepopulate-passes -Copt-level=0 | ||
// make sure that branching on a constant does not emit a conditional | ||
// branch or a switch | ||
|
||
#![crate_type = "lib"] | ||
|
||
// CHECK-LABEL: @if_bool | ||
#[no_mangle] | ||
pub fn if_bool() { | ||
// CHECK: br label %{{.+}} | ||
_ = if true { | ||
0 | ||
} else { | ||
1 | ||
}; | ||
|
||
// CHECK: br label %{{.+}} | ||
_ = if false { | ||
0 | ||
} else { | ||
1 | ||
}; | ||
} | ||
|
||
// CHECK-LABEL: @if_constant_int_eq | ||
#[no_mangle] | ||
pub fn if_constant_int_eq() { | ||
let val = 0; | ||
// CHECK: br label %{{.+}} | ||
_ = if val == 0 { | ||
0 | ||
} else { | ||
1 | ||
}; | ||
|
||
// CHECK: br label %{{.+}} | ||
_ = if val == 1 { | ||
0 | ||
} else { | ||
1 | ||
}; | ||
} | ||
|
||
// CHECK-LABEL: @if_constant_match | ||
#[no_mangle] | ||
pub fn if_constant_match() { | ||
// CHECK: br label %{{.+}} | ||
_ = match 1 { | ||
1 => 2, | ||
2 => 3, | ||
_ => 4 | ||
}; | ||
|
||
// CHECK: br label %{{.+}} | ||
_ = match 1 { | ||
2 => 3, | ||
_ => 4 | ||
}; | ||
} |