From b0be08c6a673577f8ae1a132528deb0ab3550cd8 Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sun, 4 Feb 2024 20:01:06 +0000 Subject: [PATCH] Use `br` instead of a conditional when switching on a constant boolean --- compiler/rustc_codegen_ssa/src/mir/block.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_codegen_ssa/src/mir/block.rs b/compiler/rustc_codegen_ssa/src/mir/block.rs index e35b4029b4506..526b75796d632 100644 --- a/compiler/rustc_codegen_ssa/src/mir/block.rs +++ b/compiler/rustc_codegen_ssa/src/mir/block.rs @@ -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 {