Skip to content

Commit 14c7e36

Browse files
committed
Auto merge of #120650 - clubby789:switchint-const, r=<try>
Use `br` instead of a conditional when switching on a constant boolean r? `@ghost`
2 parents 671eb38 + b0be08c commit 14c7e36

File tree

1 file changed

+8
-3
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+8
-3
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+8-3
Original file line numberDiff line numberDiff line change
@@ -329,9 +329,14 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
329329
let llfalse = helper.llbb_with_cleanup(self, targets.otherwise());
330330
if switch_ty == bx.tcx().types.bool {
331331
// Don't generate trivial icmps when switching on bool.
332-
match test_value {
333-
0 => bx.cond_br(discr.immediate(), llfalse, lltrue),
334-
1 => bx.cond_br(discr.immediate(), lltrue, llfalse),
332+
let discr = discr.immediate();
333+
match (bx.const_to_opt_uint(discr), test_value) {
334+
(Some(0), 0) => bx.br(lltrue),
335+
(Some(1), 0) => bx.br(llfalse),
336+
(Some(0), 1) => bx.br(llfalse),
337+
(Some(1), 1) => bx.br(lltrue),
338+
(None, 0) => bx.cond_br(discr, llfalse, lltrue),
339+
(None, 1) => bx.cond_br(discr, lltrue, llfalse),
335340
_ => bug!(),
336341
}
337342
} else {

0 commit comments

Comments
 (0)