Skip to content

Commit e99adce

Browse files
committed
Auto merge of rust-lang#120650 - clubby789:switchint-const, r=<try>
Use `br` instead of a conditional when switching on a constant boolean r? `@ghost`
2 parents 8c0b4f6 + ee1df1b commit e99adce

File tree

1 file changed

+17
-0
lines changed
  • compiler/rustc_codegen_ssa/src/mir

1 file changed

+17
-0
lines changed

compiler/rustc_codegen_ssa/src/mir/block.rs

+17
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
319319
targets: &SwitchTargets,
320320
) {
321321
let discr = self.codegen_operand(bx, discr);
322+
// If our discr is a constant we can branch directly
323+
if let Some(const_discr) = bx.const_to_opt_u128(discr.immediate(), true) {
324+
let target = targets.target_for_value(const_discr);
325+
bx.br(helper.llbb_with_cleanup(self, target));
326+
return;
327+
};
328+
322329
let switch_ty = discr.layout.ty;
323330
let mut target_iter = targets.iter();
324331
if target_iter.len() == 1 {
@@ -329,6 +336,16 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
329336
let llfalse = helper.llbb_with_cleanup(self, targets.otherwise());
330337
if switch_ty == bx.tcx().types.bool {
331338
// Don't generate trivial icmps when switching on bool.
339+
/*let discr = discr.immediate();
340+
match (bx.const_to_opt_uint(discr), test_value) {
341+
(Some(0), 0) => bx.br(lltrue),
342+
(Some(1), 0) => bx.br(llfalse),
343+
(Some(0), 1) => bx.br(llfalse),
344+
(Some(1), 1) => bx.br(lltrue),
345+
(None, 0) => bx.cond_br(discr, llfalse, lltrue),
346+
(None, 1) => bx.cond_br(discr, lltrue, llfalse),
347+
_ => bug!(),
348+
}*/
332349
match test_value {
333350
0 => bx.cond_br(discr.immediate(), llfalse, lltrue),
334351
1 => bx.cond_br(discr.immediate(), lltrue, llfalse),

0 commit comments

Comments
 (0)