1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use move_ir_types::ast as IR;
use std::collections::{BTreeSet, HashMap};
pub fn code(loop_heads: &BTreeSet<IR::BlockLabel>, blocks: &mut IR::BytecodeBlocks) {
let mut changed = true;
while changed {
let fall_through_removed = remove_fall_through(loop_heads, blocks);
let block_removed = remove_empty_blocks(blocks);
changed = fall_through_removed || block_removed;
}
}
fn remove_fall_through(
loop_heads: &BTreeSet<IR::BlockLabel>,
blocks: &mut IR::BytecodeBlocks,
) -> bool {
use IR::Bytecode_ as B;
let mut changed = false;
for idx in 0..(blocks.len() - 1) {
let next_block = &blocks[idx + 1].0.clone();
let (lbl, block) = &mut blocks[idx];
if loop_heads.contains(lbl) {
continue;
}
let remove_last =
matches!(&block.last().unwrap().value, B::Branch(lbl) if lbl == next_block);
if remove_last {
changed = true;
block.pop();
}
}
changed
}
fn remove_empty_blocks(blocks: &mut IR::BytecodeBlocks) -> bool {
let mut label_map = HashMap::new();
let mut cur_label = None;
let mut removed = false;
let old_blocks = std::mem::take(blocks);
for (label, block) in old_blocks.into_iter().rev() {
if block.is_empty() {
removed = true;
} else {
cur_label = Some(label.clone());
blocks.push((label.clone(), block))
}
label_map.insert(label, cur_label.clone().unwrap());
}
blocks.reverse();
if removed {
super::remap_labels(blocks, &label_map);
}
removed
}