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 5, 2024
2 parents 8c0b4f6 + ee1df1b commit e99adce
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions compiler/rustc_codegen_ssa/src/mir/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
targets: &SwitchTargets,
) {
let discr = self.codegen_operand(bx, discr);
// If our discr is a constant we can branch directly
if let Some(const_discr) = bx.const_to_opt_u128(discr.immediate(), true) {
let target = targets.target_for_value(const_discr);
bx.br(helper.llbb_with_cleanup(self, target));
return;
};

let switch_ty = discr.layout.ty;
let mut target_iter = targets.iter();
if target_iter.len() == 1 {
Expand All @@ -329,6 +336,16 @@ 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.
/*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!(),
}*/
match test_value {
0 => bx.cond_br(discr.immediate(), llfalse, lltrue),
1 => bx.cond_br(discr.immediate(), lltrue, llfalse),
Expand Down

0 comments on commit e99adce

Please sign in to comment.