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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use anyhow::Result;
use std::collections::HashMap;
use crate::config::{Config, SLICE_CAP, SPILL_AREA};
use crate::mir::{Instruction, Mir};
use crate::serializer::MirWriter;
use crate::symbol::Loc;
// #[derive(Debug)]
pub struct Compactor {
config: Config,
code: MirWriter, // 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
fixed: u32,
}
impl Compactor {
pub fn new(config: Config) -> Compactor {
let fixed = if config.is_complex() {
(2 * SLICE_CAP + SPILL_AREA) as u32
} else {
(SLICE_CAP + SPILL_AREA) as u32
};
Compactor {
config,
code: MirWriter::new(),
live: HashMap::new(),
stack: HashMap::new(),
pool: Vec::new(),
count_stack: fixed,
depth: 0,
fixed,
}
}
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.fixed + 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, .. }
| Instruction::LoadMath { loc, .. }
| Instruction::LoadComplex { loc, .. } => {
if let Loc::Stack(idx) = loc {
if idx >= self.fixed {
if let Some(x) = self.live.get_mut(&loc) {
*x = ip;
}
}
}
}
Instruction::Save { loc, .. } | Instruction::SaveComplex { loc, .. } => {
if let Loc::Stack(idx) = loc {
if idx >= self.fixed {
self.live.insert(loc, ip);
}
}
}
_ => {}
}
}
}
fn save(&mut self, loc: Loc) -> Loc {
if let Loc::Stack(idx) = loc {
if idx < self.fixed {
loc
} else if let Some(Loc::Stack(s)) = self.stack.get(&loc) {
// A stack slot can be assigned more than once in loops (ϕ-constructs)
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(idx) = loc {
if idx < self.fixed {
loc
} else {
let l = self
.stack
.get(&loc)
.unwrap_or_else(|| panic!("cannot find {:?}", loc));
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::LoadComplex { xd, yd, loc } => {
let l = self.load(*loc, ip);
self.push(Instruction::LoadComplex {
xd: *xd,
yd: *yd,
loc: l,
});
}
Instruction::LoadMath { op, dst, s1, loc } => {
let l = self.load(*loc, ip);
self.push(Instruction::LoadMath {
op: *op,
dst: *dst,
s1: *s1,
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::SaveComplex { xs, ys, loc } => {
let l = self.save(*loc);
self.push(Instruction::SaveComplex {
xs: *xs,
ys: *ys,
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());
}
}
}
}
}