Skip to content

Commit 58db206

Browse files
committed
Make the PredecessorMap only keep track of a count of predecessors
1 parent 261895b commit 58db206

File tree

2 files changed

+14
-21
lines changed

2 files changed

+14
-21
lines changed

src/librustc_mir/transform/simplify_cfg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ impl SimplifyCfg {
7373
// See if we can merge the target block into this one
7474
match terminator {
7575
Terminator::Goto { target } if target.index() > DIVERGE_BLOCK.index() &&
76-
predecessor_map.predecessors(target).len() == 1 => {
76+
predecessor_map.num_predecessors(target) == 1 => {
7777
changed = true;
7878
let mut other_data = BasicBlockData {
7979
statements: Vec::new(),

src/librustc_mir/transform/util.rs

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -51,41 +51,34 @@ pub fn retain_basic_blocks(mir: &mut Mir, keep: &[bool]) {
5151
update_basic_block_ids(mir, &replacements);
5252
}
5353

54-
// A simple map to perform quick lookups of the predecessors of a BasicBlock.
55-
// Since BasicBlocks usually only have a small number of predecessors, we use a
56-
// simple vector. Also, if a block has the same target more than once, for
57-
// example in a switch, it will appear in the target's predecessor list multiple
58-
// times. This allows to update the map more easily when modifying the graph.
54+
// A simple map to perform quick lookups of the number of predecessors of a
55+
// BasicBlock. If a block has the same target more than once, for
56+
// example in a switch, it will appear in the target's predecessor list
57+
// multiple times. This makes updating the map easier when modifying the graph.
5958
pub struct PredecessorMap {
60-
map: Vec<Vec<BasicBlock>>,
59+
map: Vec<usize>,
6160
}
6261

6362
impl PredecessorMap {
6463
pub fn new(num_blocks: usize) -> PredecessorMap {
6564
PredecessorMap {
66-
map: vec![Vec::new(); num_blocks],
65+
map: vec![0; num_blocks],
6766
}
6867
}
6968

70-
pub fn predecessors(&self, block: BasicBlock) -> &[BasicBlock] {
71-
&self.map[block.index()]
69+
pub fn num_predecessors(&self, block: BasicBlock) -> usize {
70+
self.map[block.index()]
7271
}
7372

74-
pub fn add_predecessor(&mut self, block: BasicBlock, predecessor: BasicBlock) {
75-
self.map[block.index()].push(predecessor);
73+
pub fn add_predecessor(&mut self, block: BasicBlock, _predecessor: BasicBlock) {
74+
self.map[block.index()] += 1;
7675
}
7776

78-
pub fn remove_predecessor(&mut self, block: BasicBlock, predecessor: BasicBlock) {
79-
let pos = self.map[block.index()].iter().position(|&p| p == predecessor).expect(
80-
&format!("{:?} is not registered as a predecessor of {:?}", predecessor, block));
81-
82-
self.map[block.index()].swap_remove(pos);
77+
pub fn remove_predecessor(&mut self, block: BasicBlock, _predecessor: BasicBlock) {
78+
self.map[block.index()] -= 1;
8379
}
8480

85-
pub fn replace_predecessor(&mut self, block: BasicBlock, old: BasicBlock, new: BasicBlock) {
86-
self.remove_predecessor(block, old);
87-
self.add_predecessor(block, new);
88-
}
81+
pub fn replace_predecessor(&mut self, _block: BasicBlock, _old: BasicBlock, _new: BasicBlock) { }
8982
}
9083

9184
struct PredecessorVisitor {

0 commit comments

Comments
 (0)