Skip to content

Commit b215a32

Browse files
committed
Small mir-opt refactor
Hopefully-non-controversial changes from some not-ready-yet work that I'd figured I'd submit on their own.
1 parent f7c4829 commit b215a32

File tree

4 files changed

+11
-23
lines changed

4 files changed

+11
-23
lines changed

compiler/rustc_middle/src/mir/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,7 @@ impl Statement<'_> {
15041504
}
15051505

15061506
/// Changes a statement to a nop and returns the original statement.
1507+
#[must_use = "If you don't need the statement, use `make_nop` instead"]
15071508
pub fn replace_nop(&mut self) -> Self {
15081509
Statement {
15091510
source_info: self.source_info,

compiler/rustc_middle/src/mir/terminator.rs

+7
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ impl SwitchTargets {
7878
pub fn all_targets_mut(&mut self) -> &mut [BasicBlock] {
7979
&mut self.targets
8080
}
81+
82+
/// Finds the `BasicBlock` to which this `SwitchInt` will branch given the
83+
/// specific value. This cannot fail, as it'll return the `otherwise`
84+
/// branch if there's not a specific match for the value.
85+
pub fn target_for_value(&self, value: u128) -> BasicBlock {
86+
self.iter().find_map(|(v, t)| (v == value).then_some(t)).unwrap_or_else(|| self.otherwise())
87+
}
8188
}
8289

8390
pub struct SwitchTargetsIter<'a> {

compiler/rustc_mir_transform/src/const_goto.rs

+1-14
Original file line numberDiff line numberDiff line change
@@ -82,20 +82,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'a, 'tcx> {
8282
// Now find which value in the Switch matches the const value.
8383
let const_value =
8484
_const.literal.try_eval_bits(self.tcx, self.param_env, switch_ty)?;
85-
let found_value_idx_option = targets
86-
.iter()
87-
.enumerate()
88-
.find(|(_, (value, _))| const_value == *value)
89-
.map(|(idx, _)| idx);
90-
91-
let target_to_use_in_goto =
92-
if let Some(found_value_idx) = found_value_idx_option {
93-
targets.iter().nth(found_value_idx).unwrap().1
94-
} else {
95-
// If we did not find the const value in values, it must be the otherwise case
96-
targets.otherwise()
97-
};
98-
85+
let target_to_use_in_goto = targets.target_for_value(const_value);
9986
self.optimizations.push(OptimizationToApply {
10087
bb_with_goto: location.block,
10188
target_to_use_in_goto,

compiler/rustc_mir_transform/src/simplify_branches.rs

+2-9
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,8 @@ impl<'tcx> MirPass<'tcx> for SimplifyBranches {
3434
} => {
3535
let constant = c.literal.try_eval_bits(tcx, param_env, switch_ty);
3636
if let Some(constant) = constant {
37-
let otherwise = targets.otherwise();
38-
let mut ret = TerminatorKind::Goto { target: otherwise };
39-
for (v, t) in targets.iter() {
40-
if v == constant {
41-
ret = TerminatorKind::Goto { target: t };
42-
break;
43-
}
44-
}
45-
ret
37+
let target = targets.target_for_value(constant);
38+
TerminatorKind::Goto { target }
4639
} else {
4740
continue;
4841
}

0 commit comments

Comments
 (0)