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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
//! Constant-offset "remat" pass: rewrite x+k to local additions off
//! of one base, to minimize live value / register pressure. Also push
//! these offsets into loads/stores where possible.
use fxhash::{FxHashMap, FxHashSet};
use std::collections::{BTreeMap, VecDeque};
use waffle::{
cfg::CFGInfo, entity::PerEntity, pool::ListRef, Block, FunctionBody, Operator, Terminator,
Type, Value, ValueDef,
};
/// Dataflow analysis lattice: a value is either some original SSA
/// value plus an offset, or else an arbitrary runtime value.
///
/// Constants and additions deal only in 32-bit space.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Default)]
enum AbsValue {
/// Not yet computed.
#[default]
Top,
/// A constant value alone.
Constant(u32),
/// A constant plus some other SSA value.
Offset(Value, u32),
/// Bottom value: value is "itself", no other description.
Bottom,
}
impl AbsValue {
fn meet(a: AbsValue, b: AbsValue) -> AbsValue {
use AbsValue::*;
match (a, b) {
(a, b) if a == b => a,
(x, Top) | (Top, x) => x,
_ => Bottom,
}
}
}
pub fn run(func: &mut FunctionBody, cfg: &CFGInfo) {
waffle::passes::resolve_aliases::run(func);
log::trace!(
"constant_offsets pass running on:\n{}",
func.display_verbose("| ", None)
);
// Compute a fixpoint analysis: which values are some original SSA
// value plus an offset?
let mut values: PerEntity<Value, AbsValue> = PerEntity::default();
let mut workqueue: VecDeque<Block> = VecDeque::new();
let mut workqueue_set: FxHashSet<Block> = FxHashSet::default();
let mut visited: FxHashSet<Block> = FxHashSet::default();
workqueue.push_back(func.entry);
workqueue_set.insert(func.entry);
for &(_, param) in &func.blocks[func.entry].params {
values[param] = AbsValue::Bottom;
}
while let Some(block) = workqueue.pop_front() {
log::trace!("processing {block}");
workqueue_set.remove(&block);
for &inst in &func.blocks[block].insts {
log::trace!("block {} value {}: {:?}", block, inst, func.values[inst]);
match &func.values[inst] {
ValueDef::BlockParam(..) => {
unreachable!();
}
ValueDef::Alias(orig) => {
values[inst] = values[*orig];
}
ValueDef::Operator(op, args, tys) if tys.len() == 1 => {
let args = &func.arg_pool[*args];
log::trace!(" -> args = {args:?}");
match op {
Operator::I32Const { value } => {
values[inst] = AbsValue::Constant(*value);
}
Operator::I32Add => {
let x = args[0];
let y = args[1];
values[inst] = match (values[x], values[y]) {
(AbsValue::Top, _) | (_, AbsValue::Top) => AbsValue::Top,
(AbsValue::Constant(k1), AbsValue::Constant(k2)) => {
AbsValue::Constant(k1.wrapping_add(k2))
}
(AbsValue::Offset(base, k1), AbsValue::Constant(k2))
| (AbsValue::Constant(k2), AbsValue::Offset(base, k1)) => {
AbsValue::Offset(base, k1.wrapping_add(k2))
}
(_, AbsValue::Constant(k)) => AbsValue::Offset(x, k),
(AbsValue::Constant(k), _) => AbsValue::Offset(y, k),
_ => AbsValue::Bottom,
};
}
Operator::I32Sub => {
// Like the addition case, but no commutativity.
let x = args[0];
let y = args[1];
values[inst] = match (values[x], values[y]) {
(AbsValue::Top, _) | (_, AbsValue::Top) => AbsValue::Top,
(AbsValue::Constant(k1), AbsValue::Constant(k2)) => {
AbsValue::Constant(k1.wrapping_sub(k2))
}
(AbsValue::Offset(base, k1), AbsValue::Constant(k2)) => {
AbsValue::Offset(base, k1.wrapping_sub(k2))
}
(_, AbsValue::Constant(k)) => {
AbsValue::Offset(x, 0u32.wrapping_sub(k))
}
(AbsValue::Offset(base1, k1), AbsValue::Offset(base2, k2))
if base1 == base2 =>
{
AbsValue::Constant(k1.wrapping_sub(k2))
}
(_, AbsValue::Offset(base, k)) if base == x => {
AbsValue::Constant(0u32.wrapping_sub(k))
}
_ => AbsValue::Bottom,
};
}
_ => {
values[inst] = AbsValue::Bottom;
}
}
}
_ => {
values[inst] = AbsValue::Bottom;
}
}
log::trace!(" -> values[{}] = {:?}", inst, values[inst]);
}
func.blocks[block].terminator.visit_targets(|target| {
let mut changed = false;
let succ_params = &func.blocks[target.block].params;
for (&arg, &(_, blockparam)) in target.args.iter().zip(succ_params.iter()) {
let new = AbsValue::meet(values[arg], values[blockparam]);
log::trace!(" -> block {} target {}: arg {} to blockparam {}: value {:?} -> {:?}",
block, target.block, arg, blockparam, values[blockparam], new);
changed |= new != values[blockparam];
values[blockparam] = new;
}
if changed ||
visited.insert(target.block) ||
(block != target.block && cfg.dominates(block, target.block)) {
log::trace!(" -> at least one blockparam changed, or we dominate target block; enqueuing {}",
target.block);
if workqueue_set.insert(target.block) {
workqueue.push_back(target.block);
}
}
});
}
// Constant-fold conditional branches.
for &block in cfg.rpo.values() {
// If the terminator is a CondBr and has a constant input now,
// const-fold that.
if let Terminator::CondBr {
cond,
if_true,
if_false,
} = &func.blocks[block].terminator
{
if let AbsValue::Constant(k) = values[*cond] {
let target = if k != 0 {
if_true.clone()
} else {
if_false.clone()
};
func.blocks[block].terminator = Terminator::Br { target };
}
}
}
// Find the set of all values used as addresses to loads/stores.
let mut used_as_addr = FxHashSet::default();
for (_, def) in func.values.entries() {
if let ValueDef::Operator(op, args, _) = def {
if op.is_load() || op.is_store() {
used_as_addr.insert(func.arg_pool[*args][0]);
}
}
}
// For any value that's a base, compute the minimum offset (when
// interpreted as an i32).
let mut min_offset_from: BTreeMap<Value, i32> = BTreeMap::new();
for value in func.values.iter() {
if used_as_addr.contains(&value) {
if let AbsValue::Offset(base, off) = values[value] {
let signed = off as i32;
let min = min_offset_from.entry(base).or_insert(0);
*min = std::cmp::min(*min, signed);
}
}
}
// Create an i32add or i32sub computing one shared base (with only
// positive offsets needed) for each address-related offset-from
// value in `min_offset_from`. For a value `x`, with `y =
// offset_base[x]`, we have `y + min_offset_from[x] == x`.
//
// Note that we don't insert these values into any blocks yet; we
// do that when we see the original defs below (i.e., insert `y`
// just after `x` is defined). `offset_base_const` facilitates
// this (we need to insert the i32const first).
let mut offset_base: BTreeMap<Value, Value> = BTreeMap::new();
let mut offset_base_const: BTreeMap<Value, Value> = BTreeMap::new();
let i32_ty = func.single_type_list(Type::I32);
for (&value, &offset) in &min_offset_from {
assert!(offset <= 0);
if offset != 0 {
let k = func.add_value(ValueDef::Operator(
Operator::I32Const {
value: (-offset) as u32,
},
ListRef::default(),
i32_ty,
));
let args = func.arg_pool.double(value, k);
let add = func.add_value(ValueDef::Operator(Operator::I32Sub, args, i32_ty));
offset_base_const.insert(value, k);
offset_base.insert(value, add);
log::trace!(
"created common base {k} (and const {add}) associated with offset-from value {value}"
);
} else {
offset_base.insert(value, value);
}
}
// Now, for each value that's an Offset, rewrite it to an add
// instruction.
for (block, block_def) in func.blocks.entries_mut() {
log::trace!("rewriting in block {block}");
let mut computed_offsets: FxHashMap<AbsValue, Value> = FxHashMap::default();
let mut new_insts = vec![];
for (_, param) in &block_def.params {
// Insert the common-base computations where needed.
if let Some(common_base_const) = offset_base_const.get(¶m) {
new_insts.push(*common_base_const);
new_insts.push(*offset_base.get(¶m).unwrap());
}
}
for inst in std::mem::take(&mut block_def.insts) {
log::trace!("visiting inst {}: {:?}", inst, values[inst]);
// Handle loads/stores.
if let ValueDef::Operator(op, args, tys) = &func.values[inst] {
if op.is_load() || op.is_store() {
let args = &func.arg_pool[*args];
let tys = *tys;
let addr = args[0];
log::trace!("load/store with addr {addr}");
if let AbsValue::Offset(base, this_offset) = values[addr] {
log::trace!("inst {inst} is a load/store with addr that is offset from base {base}; pushing offset into instruction");
// Update the offset embedded in the Operator
// and use the `base` value instead as the
// address arg.
let mut op = *op;
let mut args = args.iter().cloned().collect::<Vec<_>>();
let common_base = *offset_base.get(&base).unwrap();
let offset = *min_offset_from.get(&base).unwrap();
assert!(offset <= 0);
let addend = (-offset) as u32;
op.update_memory_arg(|memory| {
memory.offset =
memory.offset.wrapping_add(addend).wrapping_add(this_offset)
});
args[0] = common_base;
let args = func.arg_pool.from_iter(args.into_iter());
func.values[inst] = ValueDef::Operator(op, args, tys);
}
}
}
// If this value is a constant according to analysis above
// (perhaps because it is the difference between x+k1 and
// x+k2) but not an I32Const then make it so.
if let AbsValue::Constant(k) = values[inst] {
if !matches!(
func.values[inst],
ValueDef::Operator(Operator::I32Const { .. }, _, _)
) {
func.values[inst] = ValueDef::Operator(
Operator::I32Const { value: k },
ListRef::default(),
i32_ty,
);
}
}
// Recompute this particular value if appropriate.
if let AbsValue::Offset(base, offset) = values[inst] {
let computed_offset = *computed_offsets.entry(values[inst]).or_insert_with(|| {
if offset == 0 {
base
} else {
let offset = offset as i32;
let (op, value) = if offset > 0 {
(Operator::I32Add, offset as u32)
} else {
(Operator::I32Sub, (-offset) as u32)
};
let k = func.values.push(ValueDef::Operator(
Operator::I32Const { value },
ListRef::default(),
i32_ty,
));
new_insts.push(k);
let args = func.arg_pool.double(base, k);
let add = func.values.push(ValueDef::Operator(op, args, i32_ty));
func.source_locs[k] = func.source_locs[inst];
func.source_locs[add] = func.source_locs[inst];
log::trace!(" -> recomputed as {add}");
new_insts.push(add);
add
}
});
log::trace!(" -> rewrite to {computed_offset}");
func.values[inst] = ValueDef::Alias(computed_offset);
} else {
new_insts.push(inst);
}
// Insert the common-base computations where needed.
if let Some(common_base_const) = offset_base_const.get(&inst) {
new_insts.push(*common_base_const);
new_insts.push(*offset_base.get(&inst).unwrap());
}
}
block_def.insts = new_insts;
}
}