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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
use crate::{
asm_generation::{register_allocator, AllocatedAbstractInstructionSet, RegisterSequencer},
asm_lang::{
allocated_ops::{AllocatedOp, AllocatedOpcode},
AllocatedAbstractOp, Op, OrganizationalOp, RealizedOp, VirtualOp, VirtualRegister,
},
};
use sway_error::error::CompileError;
use sway_types::Span;
use std::{
collections::{BTreeSet, HashSet},
fmt,
};
use either::Either;
#[derive(Clone)]
pub struct AbstractInstructionSet {
pub(crate) ops: Vec<Op>,
}
impl AbstractInstructionSet {
pub(crate) fn optimize(self) -> AbstractInstructionSet {
self.remove_sequential_jumps()
.remove_redundant_moves()
.remove_unused_ops()
}
fn remove_sequential_jumps(mut self) -> AbstractInstructionSet {
let dead_jumps: Vec<_> = self
.ops
.windows(2)
.enumerate()
.filter_map(|(idx, ops)| match (&ops[0].opcode, &ops[1].opcode) {
(
Either::Right(OrganizationalOp::Jump(dst_label)),
Either::Right(OrganizationalOp::Label(label)),
) if dst_label == label => Some(idx),
_otherwise => None,
})
.collect();
for idx in dead_jumps {
self.ops[idx] = Op {
opcode: Either::Left(VirtualOp::NOOP),
comment: "removed redundant JUMP".into(),
owning_span: None,
};
}
self
}
fn remove_redundant_moves(mut self) -> AbstractInstructionSet {
loop {
let uses: HashSet<&VirtualRegister> =
self.ops.iter().fold(HashSet::new(), |mut acc, op| {
for u in &op.use_registers() {
acc.insert(u);
}
acc
});
let mut dead_moves = Vec::new();
for (idx, op) in self.ops.iter().enumerate() {
if let Either::Left(VirtualOp::MOVE(
dst_reg @ VirtualRegister::Virtual(_),
_src_reg,
)) = &op.opcode
{
if !uses.contains(dst_reg) {
dead_moves.push(idx);
}
}
}
if dead_moves.is_empty() {
break;
}
for idx in dead_moves {
self.ops[idx] = Op {
opcode: Either::Left(VirtualOp::NOOP),
comment: "removed redundant MOVE".into(),
owning_span: None,
};
}
}
self
}
fn remove_unused_ops(mut self) -> AbstractInstructionSet {
self.ops.retain(|op| match &op.opcode {
Either::Left(VirtualOp::NOOP) => false,
_otherwise => true,
});
self
}
pub(crate) fn verify(self) -> Result<AbstractInstructionSet, CompileError> {
macro_rules! add_virt_regs {
($regs: expr, $set: expr) => {
let mut regs = $regs;
regs.retain(|®| matches!(reg, VirtualRegister::Virtual(_)));
$set.append(&mut regs);
};
}
let mut use_regs = BTreeSet::new();
let mut def_regs = BTreeSet::new();
for op in &self.ops {
add_virt_regs!(op.use_registers(), use_regs);
add_virt_regs!(op.def_registers(), def_regs);
}
if def_regs.is_superset(&use_regs) {
Ok(self)
} else {
Err(CompileError::Internal(
"Program erroneously uses uninitialized virtual registers.",
Span::dummy(),
))
}
}
pub(crate) fn allocate_registers(
self,
register_sequencer: &mut RegisterSequencer,
) -> AllocatedAbstractInstructionSet {
let live_out = register_allocator::liveness_analysis(&self.ops);
let (mut interference_graph, mut reg_to_node_ix) =
register_allocator::create_interference_graph(&self.ops, &live_out);
let reduced_ops = register_allocator::coalesce_registers(
&self.ops,
&mut interference_graph,
&mut reg_to_node_ix,
register_sequencer,
);
let mut stack = register_allocator::color_interference_graph(&mut interference_graph);
let pool = register_allocator::assign_registers(&mut stack);
let mut buf = vec![];
for op in &reduced_ops {
buf.push(AllocatedAbstractOp {
opcode: op.allocate_registers(&pool),
comment: op.comment.clone(),
owning_span: op.owning_span.clone(),
})
}
AllocatedAbstractInstructionSet { ops: buf }
}
}
impl fmt::Display for AbstractInstructionSet {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
".program:\n{}",
self.ops
.iter()
.map(|x| format!("{}", x))
.collect::<Vec<_>>()
.join("\n")
)
}
}
pub struct RealizedAbstractInstructionSet {
pub(super) ops: Vec<RealizedOp>,
}
impl RealizedAbstractInstructionSet {
pub(crate) fn pad_to_even(self) -> Vec<AllocatedOp> {
let mut ops = self
.ops
.into_iter()
.map(
|RealizedOp {
opcode,
comment,
owning_span,
}| {
AllocatedOp {
opcode,
comment,
owning_span,
}
},
)
.collect::<Vec<_>>();
if ops.len() & 1 != 0 {
ops.push(AllocatedOp {
opcode: AllocatedOpcode::NOOP,
comment: "word-alignment of data section".into(),
owning_span: None,
});
}
ops
}
}