Skip to content

Commit c7fa844

Browse files
committed
Introduce StatementKind::as_assign
1 parent d8712dc commit c7fa844

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,13 @@ impl<'tcx> StatementKind<'tcx> {
15041504
_ => None,
15051505
}
15061506
}
1507+
1508+
pub fn as_assign(&self) -> Option<&Box<(Place<'tcx>, Rvalue<'tcx>)>> {
1509+
match self {
1510+
StatementKind::Assign(x) => Some(x),
1511+
_ => None,
1512+
}
1513+
}
15071514
}
15081515

15091516
/// Describes what kind of retag is to be performed.

compiler/rustc_mir/src/transform/const_goto.rs

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -59,55 +59,52 @@ impl<'a, 'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'a, 'tcx> {
5959
let last_statement =
6060
self.body.basic_blocks()[location.block].statements.last()?;
6161

62-
match &last_statement.kind {
63-
StatementKind::Assign(box (place, Rvalue::Use(op))) => {
64-
let _const = op.constant()?;
65-
// We found a constant being assigned to `place`.
66-
// Now check that the target of this Goto switches on this place.
67-
let target_bb = &self.body.basic_blocks()[target];
68-
// FIXME(simonvandel): We are conservative here when we don't allow
69-
// any statements in the target basic block.
70-
// This could probably be relaxed to allow `StorageDead`s which could be
71-
// copied to the predecessor of this block.
72-
if !target_bb.statements.is_empty() {
73-
None?
74-
}
62+
if let Some(box (place, Rvalue::Use(op))) = last_statement.kind.as_assign(){
63+
let _const = op.constant()?;
64+
// We found a constant being assigned to `place`.
65+
// Now check that the target of this Goto switches on this place.
66+
let target_bb = &self.body.basic_blocks()[target];
67+
// FIXME(simonvandel): We are conservative here when we don't allow
68+
// any statements in the target basic block.
69+
// This could probably be relaxed to allow `StorageDead`s which could be
70+
// copied to the predecessor of this block.
71+
if !target_bb.statements.is_empty() {
72+
None?
73+
}
7574

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

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

102-
self.optimizations.push(OptimizationToApply {
103-
bb_with_goto: location.block,
104-
target_to_use_in_goto,
105-
});
106-
}
107-
_ => {}
101+
self.optimizations.push(OptimizationToApply {
102+
bb_with_goto: location.block,
103+
target_to_use_in_goto,
104+
});
108105
}
106+
_ => {}
109107
}
110-
_ => {}
111108
}
112109
}
113110
_ => {}

0 commit comments

Comments
 (0)