Skip to content

Commit 0fb1c37

Browse files
committed
Auto merge of #91279 - scottmcm:small-refactor, r=nagisa
Small mir-opt refactor Hopefully-non-controversial changes from some not-ready-yet work that I'd figured I'd submit on their own.
2 parents bc9326d + b215a32 commit 0fb1c37

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
@@ -1519,6 +1519,7 @@ impl Statement<'_> {
15191519
}
15201520

15211521
/// Changes a statement to a nop and returns the original statement.
1522+
#[must_use = "If you don't need the statement, use `make_nop` instead"]
15221523
pub fn replace_nop(&mut self) -> Self {
15231524
Statement {
15241525
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
@@ -83,20 +83,7 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'a, 'tcx> {
8383
// Now find which value in the Switch matches the const value.
8484
let const_value =
8585
_const.literal.try_eval_bits(self.tcx, self.param_env, switch_ty)?;
86-
let found_value_idx_option = targets
87-
.iter()
88-
.enumerate()
89-
.find(|(_, (value, _))| const_value == *value)
90-
.map(|(idx, _)| idx);
91-
92-
let target_to_use_in_goto =
93-
if let Some(found_value_idx) = found_value_idx_option {
94-
targets.iter().nth(found_value_idx).unwrap().1
95-
} else {
96-
// If we did not find the const value in values, it must be the otherwise case
97-
targets.otherwise()
98-
};
99-
86+
let target_to_use_in_goto = targets.target_for_value(const_value);
10087
self.optimizations.push(OptimizationToApply {
10188
bb_with_goto: location.block,
10289
target_to_use_in_goto,

compiler/rustc_mir_transform/src/simplify_branches.rs

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

0 commit comments

Comments
 (0)