Skip to content

Commit f440737

Browse files
committed
Change the return type of can_simplify to Option<()>
1 parent 032bb74 commit f440737

File tree

1 file changed

+26
-28
lines changed

1 file changed

+26
-28
lines changed

compiler/rustc_mir_transform/src/match_branches.rs

+26-28
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ impl<'tcx> MirPass<'tcx> for MatchBranchSimplification {
3737
_ => continue,
3838
};
3939

40-
if SimplifyToIf.simplify(tcx, body, bb_idx, param_env) {
40+
if SimplifyToIf.simplify(tcx, body, bb_idx, param_env).is_some() {
4141
should_cleanup = true;
4242
continue;
4343
}
44-
if SimplifyToExp::default().simplify(tcx, body, bb_idx, param_env) {
44+
if SimplifyToExp::default().simplify(tcx, body, bb_idx, param_env).is_some() {
4545
should_cleanup = true;
4646
continue;
4747
}
@@ -62,17 +62,15 @@ trait SimplifyMatch<'tcx> {
6262
body: &mut Body<'tcx>,
6363
switch_bb_idx: BasicBlock,
6464
param_env: ParamEnv<'tcx>,
65-
) -> bool {
65+
) -> Option<()> {
6666
let bbs = &body.basic_blocks;
6767
let (discr, targets) = match bbs[switch_bb_idx].terminator().kind {
6868
TerminatorKind::SwitchInt { ref discr, ref targets, .. } => (discr, targets),
6969
_ => unreachable!(),
7070
};
7171

7272
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)?;
7674

7775
let mut patch = MirPatch::new(body);
7876

@@ -92,7 +90,7 @@ trait SimplifyMatch<'tcx> {
9290
patch.add_statement(parent_end, StatementKind::StorageDead(discr_local));
9391
patch.patch_terminator(switch_bb_idx, bbs[first].terminator().kind.clone());
9492
patch.apply(body);
95-
true
93+
Some(())
9694
}
9795

9896
/// Check that the BBs to be simplified satisfies all distinct and
@@ -105,7 +103,7 @@ trait SimplifyMatch<'tcx> {
105103
param_env: ParamEnv<'tcx>,
106104
bbs: &IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
107105
discr_ty: Ty<'tcx>,
108-
) -> bool;
106+
) -> Option<()>;
109107

110108
fn new_stmts(
111109
&self,
@@ -161,27 +159,27 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
161159
param_env: ParamEnv<'tcx>,
162160
bbs: &IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
163161
_discr_ty: Ty<'tcx>,
164-
) -> bool {
162+
) -> Option<()> {
165163
if targets.iter().len() != 1 {
166-
return false;
164+
return None;
167165
}
168166
// We require that the possible target blocks all be distinct.
169167
let (_, first) = targets.iter().next().unwrap();
170168
let second = targets.otherwise();
171169
if first == second {
172-
return false;
170+
return None;
173171
}
174172
// Check that destinations are identical, and if not, then don't optimize this block
175173
if bbs[first].terminator().kind != bbs[second].terminator().kind {
176-
return false;
174+
return None;
177175
}
178176

179177
// Check that blocks are assignments of consts to the same place or same statement,
180178
// and match up 1-1, if not don't optimize this block.
181179
let first_stmts = &bbs[first].statements;
182180
let second_stmts = &bbs[second].statements;
183181
if first_stmts.len() != second_stmts.len() {
184-
return false;
182+
return None;
185183
}
186184
for (f, s) in iter::zip(first_stmts, second_stmts) {
187185
match (&f.kind, &s.kind) {
@@ -199,10 +197,10 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToIf {
199197
&& s_c.const_.try_eval_bool(tcx, param_env).is_some() => {}
200198

201199
// Otherwise we cannot optimize. Try another block.
202-
_ => return false,
200+
_ => return None,
203201
}
204202
}
205-
true
203+
Some(())
206204
}
207205

208206
fn new_stmts(
@@ -340,16 +338,16 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
340338
param_env: ParamEnv<'tcx>,
341339
bbs: &IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
342340
discr_ty: Ty<'tcx>,
343-
) -> bool {
341+
) -> Option<()> {
344342
if targets.iter().len() < 2 || targets.iter().len() > 64 {
345-
return false;
343+
return None;
346344
}
347345
// We require that the possible target blocks all be distinct.
348346
if !targets.is_distinct() {
349-
return false;
347+
return None;
350348
}
351349
if !bbs[targets.otherwise()].is_empty_unreachable() {
352-
return false;
350+
return None;
353351
}
354352
let mut target_iter = targets.iter();
355353
let (first_val, first_target) = target_iter.next().unwrap();
@@ -359,15 +357,15 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
359357
.iter()
360358
.all(|(_, other_target)| first_terminator_kind == &bbs[other_target].terminator().kind)
361359
{
362-
return false;
360+
return None;
363361
}
364362

365363
let discr_size = tcx.layout_of(param_env.and(discr_ty)).unwrap().size;
366364
let first_stmts = &bbs[first_target].statements;
367365
let (second_val, second_target) = target_iter.next().unwrap();
368366
let second_stmts = &bbs[second_target].statements;
369367
if first_stmts.len() != second_stmts.len() {
370-
return false;
368+
return None;
371369
}
372370

373371
fn int_equal(l: ScalarInt, r: impl Into<u128>, size: Size) -> bool {
@@ -412,13 +410,13 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
412410
}
413411
}
414412
_ => {
415-
return false;
413+
return None;
416414
}
417415
}
418416
}
419417

420418
// Otherwise we cannot optimize. Try another block.
421-
_ => return false,
419+
_ => return None,
422420
};
423421
compare_types.push(compare_type);
424422
}
@@ -427,7 +425,7 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
427425
for (other_val, other_target) in target_iter {
428426
let other_stmts = &bbs[other_target].statements;
429427
if compare_types.len() != other_stmts.len() {
430-
return false;
428+
return None;
431429
}
432430
for (f, s) in iter::zip(&compare_types, other_stmts) {
433431
match (*f, &s.kind) {
@@ -443,7 +441,7 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
443441
StatementKind::Assign(box (lhs_s, Rvalue::Use(Operand::Constant(s_c)))),
444442
) if lhs_f == lhs_s && s_c.const_.ty() == f_ty => {
445443
let Some(f) = s_c.const_.try_eval_scalar_int(tcx, param_env) else {
446-
return false;
444+
return None;
447445
};
448446
if is_signed
449447
&& s_c.const_.ty().is_signed()
@@ -454,14 +452,14 @@ impl<'tcx> SimplifyMatch<'tcx> for SimplifyToExp {
454452
if Some(f) == ScalarInt::try_from_uint(other_val, f.size()) {
455453
continue;
456454
}
457-
return false;
455+
return None;
458456
}
459-
_ => return false,
457+
_ => return None,
460458
}
461459
}
462460
}
463461
self.transfrom_types = compare_types.into_iter().map(|c| c.into()).collect();
464-
true
462+
Some(())
465463
}
466464

467465
fn new_stmts(

0 commit comments

Comments
 (0)