Skip to content

Commit fbc3cc1

Browse files
committed
Avoid constructing switch sources unless necessary
Switch sources are used by backward analysis with a custom switch int edge effects, but are otherwise unnecessarily computed. Delay the computation until we know that switch sources are indeed required and avoid the computation otherwise.
1 parent 83322c5 commit fbc3cc1

File tree

2 files changed

+6
-3
lines changed

2 files changed

+6
-3
lines changed

compiler/rustc_middle/src/mir/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,8 @@ impl<'tcx> Body<'tcx> {
580580
self.predecessor_cache.compute(&self.basic_blocks)
581581
}
582582

583+
/// `body.switch_sources()[target][switch]` returns a list of switch values
584+
/// that lead to a `target` block from a `switch` block.
583585
#[inline]
584586
pub fn switch_sources(&self) -> &SwitchSources {
585587
self.switch_source_cache.compute(&self.basic_blocks)

compiler/rustc_mir_dataflow/src/framework/direction.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -269,9 +269,9 @@ impl Direction for Backward {
269269

270270
mir::TerminatorKind::SwitchInt { targets: _, ref discr, switch_ty: _ } => {
271271
let mut applier = BackwardSwitchIntEdgeEffectsApplier {
272+
body,
272273
pred,
273274
exit_state,
274-
values: &body.switch_sources()[bb][pred],
275275
bb,
276276
propagate: &mut propagate,
277277
effects_applied: false,
@@ -305,9 +305,9 @@ impl Direction for Backward {
305305
}
306306

307307
struct BackwardSwitchIntEdgeEffectsApplier<'a, D, F> {
308+
body: &'a mir::Body<'a>,
308309
pred: BasicBlock,
309310
exit_state: &'a mut D,
310-
values: &'a [Option<u128>],
311311
bb: BasicBlock,
312312
propagate: &'a mut F,
313313

@@ -322,7 +322,8 @@ where
322322
fn apply(&mut self, mut apply_edge_effect: impl FnMut(&mut D, SwitchIntTarget)) {
323323
assert!(!self.effects_applied);
324324

325-
let targets = self.values.iter().map(|&value| SwitchIntTarget { value, target: self.bb });
325+
let values = &self.body.switch_sources()[self.bb][self.pred];
326+
let targets = values.iter().map(|&value| SwitchIntTarget { value, target: self.bb });
326327

327328
let mut tmp = None;
328329
for target in targets {

0 commit comments

Comments
 (0)