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.
This commit is contained in:
Tomasz Miąsko 2022-05-08 00:00:00 +00:00
parent 83322c557f
commit fbc3cc18be
2 changed files with 6 additions and 3 deletions

View file

@ -580,6 +580,8 @@ impl<'tcx> Body<'tcx> {
self.predecessor_cache.compute(&self.basic_blocks) self.predecessor_cache.compute(&self.basic_blocks)
} }
/// `body.switch_sources()[target][switch]` returns a list of switch values
/// that lead to a `target` block from a `switch` block.
#[inline] #[inline]
pub fn switch_sources(&self) -> &SwitchSources { pub fn switch_sources(&self) -> &SwitchSources {
self.switch_source_cache.compute(&self.basic_blocks) self.switch_source_cache.compute(&self.basic_blocks)

View file

@ -269,9 +269,9 @@ impl Direction for Backward {
mir::TerminatorKind::SwitchInt { targets: _, ref discr, switch_ty: _ } => { mir::TerminatorKind::SwitchInt { targets: _, ref discr, switch_ty: _ } => {
let mut applier = BackwardSwitchIntEdgeEffectsApplier { let mut applier = BackwardSwitchIntEdgeEffectsApplier {
body,
pred, pred,
exit_state, exit_state,
values: &body.switch_sources()[bb][pred],
bb, bb,
propagate: &mut propagate, propagate: &mut propagate,
effects_applied: false, effects_applied: false,
@ -305,9 +305,9 @@ impl Direction for Backward {
} }
struct BackwardSwitchIntEdgeEffectsApplier<'a, D, F> { struct BackwardSwitchIntEdgeEffectsApplier<'a, D, F> {
body: &'a mir::Body<'a>,
pred: BasicBlock, pred: BasicBlock,
exit_state: &'a mut D, exit_state: &'a mut D,
values: &'a [Option<u128>],
bb: BasicBlock, bb: BasicBlock,
propagate: &'a mut F, propagate: &'a mut F,
@ -322,7 +322,8 @@ where
fn apply(&mut self, mut apply_edge_effect: impl FnMut(&mut D, SwitchIntTarget)) { fn apply(&mut self, mut apply_edge_effect: impl FnMut(&mut D, SwitchIntTarget)) {
assert!(!self.effects_applied); assert!(!self.effects_applied);
let targets = self.values.iter().map(|&value| SwitchIntTarget { value, target: self.bb }); let values = &self.body.switch_sources()[self.bb][self.pred];
let targets = values.iter().map(|&value| SwitchIntTarget { value, target: self.bb });
let mut tmp = None; let mut tmp = None;
for target in targets { for target in targets {