Skip to content

Commit e3b8d5c

Browse files
committed
Introduce TerminatorKind::as_switch
1 parent c7fa844 commit e3b8d5c

File tree

2 files changed

+34
-29
lines changed

2 files changed

+34
-29
lines changed

compiler/rustc_middle/src/mir/terminator.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,15 @@ impl<'tcx> TerminatorKind<'tcx> {
407407
| TerminatorKind::FalseUnwind { ref mut unwind, .. } => Some(unwind),
408408
}
409409
}
410+
411+
pub fn as_switch(&self) -> Option<(&Operand<'tcx>, Ty<'tcx>, &SwitchTargets)> {
412+
match self {
413+
TerminatorKind::SwitchInt { discr, switch_ty, targets } => {
414+
Some((discr, switch_ty, targets))
415+
}
416+
_ => None,
417+
}
418+
}
410419
}
411420

412421
impl<'tcx> Debug for TerminatorKind<'tcx> {

compiler/rustc_mir/src/transform/const_goto.rs

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -73,37 +73,33 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'a, 'tcx> {
7373
}
7474

7575
let target_bb_terminator = target_bb.terminator();
76-
match &target_bb_terminator.kind {
77-
TerminatorKind::SwitchInt { discr, switch_ty, targets }
78-
if discr.place() == Some(*place) =>
79-
{
80-
// We now know that the Switch matches on the const place, and it is statementless
81-
// Now find which value in the Switch matches the const value.
82-
let const_value = _const.literal.try_eval_bits(
83-
self.tcx,
84-
self.param_env,
85-
switch_ty,
86-
)?;
87-
let found_value_idx_option = targets
88-
.iter()
89-
.enumerate()
90-
.find(|(_, x)| const_value == x.0)
91-
.map(|(idx, _)| idx);
76+
let (discr, switch_ty, targets) = target_bb_terminator.kind.as_switch()?;
77+
if discr.place() == Some(*place) {
78+
// We now know that the Switch matches on the const place, and it is statementless
79+
// Now find which value in the Switch matches the const value.
80+
let const_value = _const.literal.try_eval_bits(
81+
self.tcx,
82+
self.param_env,
83+
switch_ty,
84+
)?;
85+
let found_value_idx_option = targets
86+
.iter()
87+
.enumerate()
88+
.find(|(_, x)| const_value == x.0)
89+
.map(|(idx, _)| idx);
9290

93-
let target_to_use_in_goto =
94-
if let Some(found_value_idx) = found_value_idx_option {
95-
targets.iter().nth(found_value_idx).unwrap().1
96-
} else {
97-
// If we did not find the const value in values, it must be the otherwise case
98-
targets.otherwise()
99-
};
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+
};
10098

101-
self.optimizations.push(OptimizationToApply {
102-
bb_with_goto: location.block,
103-
target_to_use_in_goto,
104-
});
105-
}
106-
_ => {}
99+
self.optimizations.push(OptimizationToApply {
100+
bb_with_goto: location.block,
101+
target_to_use_in_goto,
102+
});
107103
}
108104
}
109105
}

0 commit comments

Comments
 (0)