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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
use anyhow::Result;
use std::collections::HashMap;
use crate::config::Config;
use crate::mir::{Instruction, Mir};
use crate::symbol::Loc;
#[derive(Clone, Debug)]
pub struct Compactor {
config: Config,
code: Vec<Instruction>, // the revised mir
live: HashMap<Loc, usize>, // the map of live Stack slots and their last used position
stack: HashMap<Loc, Loc>, // the map of old Stack slots to new Stack slots
pool: Vec<Loc>, // the pool of Stack slots available to reuse
count_stack: u32, // next stack id to use if the pool is empty
depth: isize, // loop depth
}
impl Compactor {
pub fn new(config: Config) -> Compactor {
Compactor {
config,
code: Vec::new(),
live: HashMap::new(),
stack: HashMap::new(),
pool: Vec::new(),
count_stack: 16,
depth: 0,
}
}
pub fn compact(&mut self, mir: &mut Mir) -> Result<usize> {
self.collect_last(mir); // first pass, collect live info
self.compact_stack(mir); // second pass, rename stack slots
mir.code = std::mem::take(&mut self.code);
Ok(self.count_stack as usize)
}
fn push(&mut self, ins: Instruction) {
self.code.push(ins);
}
fn collect_last(&mut self, mir: &Mir) {
for (ip, ins) in mir.code.iter().enumerate() {
match *ins {
Instruction::Load { loc, .. } | Instruction::IfElse { cond: loc, .. } => {
if let Loc::Stack(_) = loc {
if let Some(x) = self.live.get_mut(&loc) {
*x = ip;
}
}
}
Instruction::Save { loc, .. } => {
if let Loc::Stack(_) = loc {
self.live.insert(loc, ip);
}
}
_ => {}
}
}
}
fn save(&mut self, loc: Loc) -> Loc {
if let Loc::Stack(_) = loc {
// A stack slot can be assigned more than once in loops (ϕ-constructs)
if let Some(Loc::Stack(s)) = self.stack.get(&loc) {
Loc::Stack(*s)
} else {
let l = match self.pool.pop() {
Some(l) => l, // pool is not empty
None => {
let l = Loc::Stack(self.count_stack);
self.count_stack += if self.config.is_complex() { 2 } else { 1 };
l
}
};
self.stack.insert(loc, l);
l
}
} else {
loc
}
}
fn load(&mut self, loc: Loc, ip: usize) -> Loc {
if let Loc::Stack(_) = loc {
let l = self.stack.get(&loc).unwrap();
let last = self.live.get(&loc).unwrap();
// stack slots are not returned to the pool inside loops.
// We can do better by delaying return to the branch at
// the end of the loop, but this would add complexity.
if *last == ip && self.depth == 0 {
self.pool.push(*l);
}
*l
} else {
loc
}
}
fn compact_stack(&mut self, mir: &Mir) {
for (ip, ins) in mir.code.iter().enumerate() {
match ins {
Instruction::Load { dst, loc } => {
let l = self.load(*loc, ip);
self.push(Instruction::Load { dst: *dst, loc: l });
}
Instruction::IfElse {
dst,
true_val,
false_val,
cond,
} => {
let l = self.load(*cond, ip);
self.push(Instruction::IfElse {
dst: *dst,
true_val: *true_val,
false_val: *false_val,
cond: l,
});
}
Instruction::Save { src, loc } => {
let l = self.save(*loc);
self.push(Instruction::Save { src: *src, loc: l });
}
Instruction::Label { label } => {
self.depth += 1;
self.push(Instruction::Label {
label: label.clone(),
});
}
Instruction::Branch { label } => {
self.depth -= 1;
self.push(Instruction::Branch {
label: label.clone(),
});
}
Instruction::BranchIf {
cond,
label,
is_else,
} => {
self.depth -= 1;
self.push(Instruction::BranchIf {
cond: *cond,
label: label.clone(),
is_else: *is_else,
});
}
_ => {
self.push(ins.clone());
}
}
}
}
}