@@ -37,11 +37,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
37
37
_ => continue ,
38
38
} ;
39
39
40
- if SimplifyToIf . simplify ( tcx, body, bb_idx, param_env) {
40
+ if SimplifyToIf . simplify ( tcx, body, bb_idx, param_env) . is_some ( ) {
41
41
should_cleanup = true ;
42
42
continue ;
43
43
}
44
- if SimplifyToExp :: default ( ) . simplify ( tcx, body, bb_idx, param_env) {
44
+ if SimplifyToExp :: default ( ) . simplify ( tcx, body, bb_idx, param_env) . is_some ( ) {
45
45
should_cleanup = true ;
46
46
continue ;
47
47
}
@@ -62,17 +62,15 @@ trait SimplifyMatch<'tcx> {
62
62
body : & mut Body < ' tcx > ,
63
63
switch_bb_idx : BasicBlock ,
64
64
param_env : ParamEnv < ' tcx > ,
65
- ) -> bool {
65
+ ) -> Option < ( ) > {
66
66
let bbs = & body. basic_blocks ;
67
67
let ( discr, targets) = match bbs[ switch_bb_idx] . terminator ( ) . kind {
68
68
TerminatorKind :: SwitchInt { ref discr, ref targets, .. } => ( discr, targets) ,
69
69
_ => unreachable ! ( ) ,
70
70
} ;
71
71
72
72
let discr_ty = discr. ty ( body. local_decls ( ) , tcx) ;
73
- if !self . can_simplify ( tcx, targets, param_env, bbs, discr_ty) {
74
- return false ;
75
- }
73
+ self . can_simplify ( tcx, targets, param_env, bbs, discr_ty) ?;
76
74
77
75
let mut patch = MirPatch :: new ( body) ;
78
76
@@ -92,7 +90,7 @@ trait SimplifyMatch<'tcx> {
92
90
patch. add_statement ( parent_end, StatementKind :: StorageDead ( discr_local) ) ;
93
91
patch. patch_terminator ( switch_bb_idx, bbs[ first] . terminator ( ) . kind . clone ( ) ) ;
94
92
patch. apply ( body) ;
95
- true
93
+ Some ( ( ) )
96
94
}
97
95
98
96
/// Check that the BBs to be simplified satisfies all distinct and
@@ -105,7 +103,7 @@ trait SimplifyMatch<'tcx> {
105
103
param_env : ParamEnv < ' tcx > ,
106
104
bbs : & IndexSlice < BasicBlock , BasicBlockData < ' tcx > > ,
107
105
discr_ty : Ty < ' tcx > ,
108
- ) -> bool ;
106
+ ) -> Option < ( ) > ;
109
107
110
108
fn new_stmts (
111
109
& self ,
@@ -161,27 +159,27 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
161
159
param_env : ParamEnv < ' tcx > ,
162
160
bbs : & IndexSlice < BasicBlock , BasicBlockData < ' tcx > > ,
163
161
_discr_ty : Ty < ' tcx > ,
164
- ) -> bool {
162
+ ) -> Option < ( ) > {
165
163
if targets. iter ( ) . len ( ) != 1 {
166
- return false ;
164
+ return None ;
167
165
}
168
166
// We require that the possible target blocks all be distinct.
169
167
let ( _, first) = targets. iter ( ) . next ( ) . unwrap ( ) ;
170
168
let second = targets. otherwise ( ) ;
171
169
if first == second {
172
- return false ;
170
+ return None ;
173
171
}
174
172
// Check that destinations are identical, and if not, then don't optimize this block
175
173
if bbs[ first] . terminator ( ) . kind != bbs[ second] . terminator ( ) . kind {
176
- return false ;
174
+ return None ;
177
175
}
178
176
179
177
// Check that blocks are assignments of consts to the same place or same statement,
180
178
// and match up 1-1, if not don't optimize this block.
181
179
let first_stmts = & bbs[ first] . statements ;
182
180
let second_stmts = & bbs[ second] . statements ;
183
181
if first_stmts. len ( ) != second_stmts. len ( ) {
184
- return false ;
182
+ return None ;
185
183
}
186
184
for ( f, s) in iter:: zip ( first_stmts, second_stmts) {
187
185
match ( & f. kind , & s. kind ) {
@@ -199,10 +197,10 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
199
197
&& s_c. const_ . try_eval_bool ( tcx, param_env) . is_some ( ) => { }
200
198
201
199
// Otherwise we cannot optimize. Try another block.
202
- _ => return false ,
200
+ _ => return None ,
203
201
}
204
202
}
205
- true
203
+ Some ( ( ) )
206
204
}
207
205
208
206
fn new_stmts (
@@ -340,16 +338,16 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
340
338
param_env : ParamEnv < ' tcx > ,
341
339
bbs : & IndexSlice < BasicBlock , BasicBlockData < ' tcx > > ,
342
340
discr_ty : Ty < ' tcx > ,
343
- ) -> bool {
341
+ ) -> Option < ( ) > {
344
342
if targets. iter ( ) . len ( ) < 2 || targets. iter ( ) . len ( ) > 64 {
345
- return false ;
343
+ return None ;
346
344
}
347
345
// We require that the possible target blocks all be distinct.
348
346
if !targets. is_distinct ( ) {
349
- return false ;
347
+ return None ;
350
348
}
351
349
if !bbs[ targets. otherwise ( ) ] . is_empty_unreachable ( ) {
352
- return false ;
350
+ return None ;
353
351
}
354
352
let mut target_iter = targets. iter ( ) ;
355
353
let ( first_val, first_target) = target_iter. next ( ) . unwrap ( ) ;
@@ -359,15 +357,15 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
359
357
. iter ( )
360
358
. all ( |( _, other_target) | first_terminator_kind == & bbs[ other_target] . terminator ( ) . kind )
361
359
{
362
- return false ;
360
+ return None ;
363
361
}
364
362
365
363
let discr_size = tcx. layout_of ( param_env. and ( discr_ty) ) . unwrap ( ) . size ;
366
364
let first_stmts = & bbs[ first_target] . statements ;
367
365
let ( second_val, second_target) = target_iter. next ( ) . unwrap ( ) ;
368
366
let second_stmts = & bbs[ second_target] . statements ;
369
367
if first_stmts. len ( ) != second_stmts. len ( ) {
370
- return false ;
368
+ return None ;
371
369
}
372
370
373
371
fn int_equal ( l : ScalarInt , r : impl Into < u128 > , size : Size ) -> bool {
@@ -412,13 +410,13 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
412
410
}
413
411
}
414
412
_ => {
415
- return false ;
413
+ return None ;
416
414
}
417
415
}
418
416
}
419
417
420
418
// Otherwise we cannot optimize. Try another block.
421
- _ => return false ,
419
+ _ => return None ,
422
420
} ;
423
421
compare_types. push ( compare_type) ;
424
422
}
@@ -427,7 +425,7 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
427
425
for ( other_val, other_target) in target_iter {
428
426
let other_stmts = & bbs[ other_target] . statements ;
429
427
if compare_types. len ( ) != other_stmts. len ( ) {
430
- return false ;
428
+ return None ;
431
429
}
432
430
for ( f, s) in iter:: zip ( & compare_types, other_stmts) {
433
431
match ( * f, & s. kind ) {
@@ -443,7 +441,7 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
443
441
StatementKind :: Assign ( box ( lhs_s, Rvalue :: Use ( Operand :: Constant ( s_c) ) ) ) ,
444
442
) if lhs_f == lhs_s && s_c. const_ . ty ( ) == f_ty => {
445
443
let Some ( f) = s_c. const_ . try_eval_scalar_int ( tcx, param_env) else {
446
- return false ;
444
+ return None ;
447
445
} ;
448
446
if is_signed
449
447
&& s_c. const_ . ty ( ) . is_signed ( )
@@ -454,14 +452,14 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
454
452
if Some ( f) == ScalarInt :: try_from_uint ( other_val, f. size ( ) ) {
455
453
continue ;
456
454
}
457
- return false ;
455
+ return None ;
458
456
}
459
- _ => return false ,
457
+ _ => return None ,
460
458
}
461
459
}
462
460
}
463
461
self . transfrom_types = compare_types. into_iter ( ) . map ( |c| c. into ( ) ) . collect ( ) ;
464
- true
462
+ Some ( ( ) )
465
463
}
466
464
467
465
fn new_stmts (
0 commit comments