Skip to content

Commit

Permalink
Use br instead of a conditional when switching on a constant boolean
Browse files Browse the repository at this point in the history
  • Loading branch information
clubby789 committed Feb 4, 2024
1 parent b11fbfb commit b0be08c
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 b0be08c

Please sign in to comment.