Skip to content

Commit

Permalink
Auto merge of rust-lang#120650 - clubby789:switchint-const, r=<try>
Browse files Browse the repository at this point in the history
Use `br` instead of a conditional when switching on a constant boolean

r? `@ghost`
  • Loading branch information
bors committed Feb 4, 2024
2 parents 671eb38 + b0be08c commit 14c7e36
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
let llfalse = helper.llbb_with_cleanup(self, targets.otherwise());
if switch_ty == bx.tcx().types.bool {
// Don't generate trivial icmps when switching on bool.
match test_value {
0 => bx.cond_br(discr.immediate(), llfalse, lltrue),
1 => bx.cond_br(discr.immediate(), lltrue, llfalse),
let discr = discr.immediate();
match (bx.const_to_opt_uint(discr), test_value) {
(Some(0), 0) => bx.br(lltrue),
(Some(1), 0) => bx.br(llfalse),
(Some(0), 1) => bx.br(llfalse),
(Some(1), 1) => bx.br(lltrue),
(None, 0) => bx.cond_br(discr, llfalse, lltrue),
(None, 1) => bx.cond_br(discr, lltrue, llfalse),
_ => bug!(),
}
} else {
Expand Down

0 comments on commit 14c7e36

Please sign in to comment.