@@ -307,7 +307,9 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) {
307307 } => {
308308 let discr = codegen_operand ( fx, discr) . load_scalar ( fx) ;
309309
310- if switch_ty. kind ( ) == fx. tcx . types . bool . kind ( ) {
310+ let use_bool_opt = switch_ty. kind ( ) == fx. tcx . types . bool . kind ( )
311+ || ( targets. iter ( ) . count ( ) == 1 && targets. iter ( ) . next ( ) . unwrap ( ) . 0 == 0 ) ;
312+ if use_bool_opt {
311313 assert_eq ! ( targets. iter( ) . count( ) , 1 ) ;
312314 let ( then_value, then_block) = targets. iter ( ) . next ( ) . unwrap ( ) ;
313315 let then_block = fx. get_block ( then_block) ;
@@ -325,12 +327,22 @@ fn codegen_fn_content(fx: &mut FunctionCx<'_, '_, impl Module>) {
325327 let discr = crate :: optimize:: peephole:: maybe_unwrap_bint ( & mut fx. bcx , discr) ;
326328 let discr =
327329 crate :: optimize:: peephole:: make_branchable_value ( & mut fx. bcx , discr) ;
328- if test_zero {
329- fx. bcx . ins ( ) . brz ( discr, then_block, & [ ] ) ;
330- fx. bcx . ins ( ) . jump ( else_block, & [ ] ) ;
330+ if let Some ( taken) = crate :: optimize:: peephole:: maybe_known_branch_taken (
331+ & fx. bcx , discr, test_zero,
332+ ) {
333+ if taken {
334+ fx. bcx . ins ( ) . jump ( then_block, & [ ] ) ;
335+ } else {
336+ fx. bcx . ins ( ) . jump ( else_block, & [ ] ) ;
337+ }
331338 } else {
332- fx. bcx . ins ( ) . brnz ( discr, then_block, & [ ] ) ;
333- fx. bcx . ins ( ) . jump ( else_block, & [ ] ) ;
339+ if test_zero {
340+ fx. bcx . ins ( ) . brz ( discr, then_block, & [ ] ) ;
341+ fx. bcx . ins ( ) . jump ( else_block, & [ ] ) ;
342+ } else {
343+ fx. bcx . ins ( ) . brnz ( discr, then_block, & [ ] ) ;
344+ fx. bcx . ins ( ) . jump ( else_block, & [ ] ) ;
345+ }
334346 }
335347 } else {
336348 let mut switch = :: cranelift_frontend:: Switch :: new ( ) ;
0 commit comments