Skip to content

Commit 8f2ab7f

Browse files
committed
Simplify SwitchSources.
`SwitchSources` is a hashmap containing `SmallVec<[Option<u128>; 1]>` elements. Each `SmallVec` has a predictable form: N `Some` elements followed by a single `None` element. This commit removes the trailing `None` element, changes the `Option<u128>` to `u128`, and adjusts the single `switch_sources` callsite to account for this (by adding a single unrolled loop iteration for the elided `None` element, which simplifies down to a single `propagate` call).
1 parent be137a2 commit 8f2ab7f

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

compiler/rustc_middle/src/mir/basic_blocks.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ pub struct BasicBlocks<'tcx> {
2020
// Typically 95%+ of basic blocks have 4 or fewer predecessors.
2121
type Predecessors = IndexVec<BasicBlock, SmallVec<[BasicBlock; 4]>>;
2222

23-
type SwitchSources = FxHashMap<(BasicBlock, BasicBlock), SmallVec<[Option<u128>; 1]>>;
23+
type SwitchSources = FxHashMap<(BasicBlock, BasicBlock), SmallVec<[u128; 1]>>;
2424

2525
#[derive(Clone, Default, Debug)]
2626
struct Cache {
@@ -71,7 +71,8 @@ impl<'tcx> BasicBlocks<'tcx> {
7171
}
7272

7373
/// `switch_sources()[&(target, switch)]` returns a list of switch
74-
/// values that lead to a `target` block from a `switch` block.
74+
/// values that lead to a `target` block from a `switch` block. Excludes
75+
/// the "otherwise" target.
7576
#[inline]
7677
pub fn switch_sources(&self) -> &SwitchSources {
7778
self.cache.switch_sources.get_or_init(|| {
@@ -82,9 +83,8 @@ impl<'tcx> BasicBlocks<'tcx> {
8283
}) = &data.terminator
8384
{
8485
for (value, target) in targets.iter() {
85-
switch_sources.entry((target, bb)).or_default().push(Some(value));
86+
switch_sources.entry((target, bb)).or_default().push(value);
8687
}
87-
switch_sources.entry((targets.otherwise(), bb)).or_default().push(None);
8888
}
8989
}
9090
switch_sources

compiler/rustc_mir_dataflow/src/framework/direction.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,13 +115,11 @@ impl Direction for Backward {
115115
let mut tmp = analysis.bottom_value(body);
116116
for &value in &body.basic_blocks.switch_sources()[&(block, pred)] {
117117
tmp.clone_from(exit_state);
118-
if let Some(value) = value {
119-
let si_target = SwitchIntTarget { value, target: block };
120-
analysis
121-
.apply_switch_int_edge_effect(&mut data, &mut tmp, si_target);
122-
}
118+
let si_target = SwitchIntTarget { value, target: block };
119+
analysis.apply_switch_int_edge_effect(&mut data, &mut tmp, si_target);
123120
propagate(pred, &tmp);
124121
}
122+
propagate(pred, exit_state);
125123
} else {
126124
propagate(pred, exit_state)
127125
}

0 commit comments

Comments
 (0)