@@ -3,6 +3,7 @@ use std::cmp;
3
3
use rustc_abi:: { BackendRepr , ExternAbi , HasDataLayout , Reg , WrappingRange } ;
4
4
use rustc_ast as ast;
5
5
use rustc_ast:: { InlineAsmOptions , InlineAsmTemplatePiece } ;
6
+ use rustc_data_structures:: packed:: Pu128 ;
6
7
use rustc_hir:: lang_items:: LangItem ;
7
8
use rustc_middle:: mir:: { self , AssertKind , InlineAsmMacro , SwitchTargets , UnwindTerminateReason } ;
8
9
use rustc_middle:: ty:: layout:: { HasTyCtxt , LayoutOf , ValidityRequirement } ;
@@ -426,6 +427,30 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
426
427
let llval = bx. const_uint_big ( switch_llty, test_value1) ;
427
428
let cmp = bx. icmp ( IntPredicate :: IntEQ , discr_value, llval) ;
428
429
bx. cond_br ( cmp, ll1, ll2) ;
430
+ } else if target_iter. len ( ) == 2
431
+ && self . mir [ targets. otherwise ( ) ] . is_empty_unreachable ( )
432
+ && targets. all_values ( ) . contains ( & Pu128 ( 0 ) )
433
+ && targets. all_values ( ) . contains ( & Pu128 ( 1 ) )
434
+ {
435
+ // This is the really common case for `bool`, `Option`, etc.
436
+ // By using `trunc nuw` we communicate that other values are
437
+ // impossible without needing `switch` or `assume`s.
438
+ let true_bb = targets. target_for_value ( 1 ) ;
439
+ let false_bb = targets. target_for_value ( 0 ) ;
440
+ let true_ll = helper. llbb_with_cleanup ( self , true_bb) ;
441
+ let false_ll = helper. llbb_with_cleanup ( self , false_bb) ;
442
+ let true_cold = self . cold_blocks [ true_bb] ;
443
+ let false_cold = self . cold_blocks [ false_bb] ;
444
+ let expect = ( true_cold != false_cold) . then_some ( !true_cold) ;
445
+
446
+ let bool_ty = bx. tcx ( ) . types . bool ;
447
+ let cond = if switch_ty == bool_ty {
448
+ discr_value
449
+ } else {
450
+ let bool_llty = bx. immediate_backend_type ( bx. layout_of ( bool_ty) ) ;
451
+ bx. unchecked_utrunc ( discr_value, bool_llty)
452
+ } ;
453
+ bx. cond_br_with_expect ( cond, true_ll, false_ll, expect) ;
429
454
} else {
430
455
let otherwise = targets. otherwise ( ) ;
431
456
let otherwise_cold = self . cold_blocks [ otherwise] ;
0 commit comments