polycomp 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,602 @@
1
+ use crate::ir::*;
2
+ use rustc_hash::{FxHashMap, FxHashSet};
3
+
4
+ pub fn optimize_function(func: &mut IrFunction) {
5
+ fuse_call_lazy_casts(func);
6
+ for _ in 0..3 {
7
+ let mut changed = false;
8
+ changed |= fold_constants_local(func);
9
+ changed |= prune_unreachable_blocks(func);
10
+ changed |= eliminate_dead_instructions(func);
11
+ if !changed {
12
+ break;
13
+ }
14
+ }
15
+ infer_types(func);
16
+ }
17
+
18
+ /// Fuses `CallLazy(tmp, site, args); Cast(dst, target_ty, tmp)` when `tmp` has a single use,
19
+ /// preserving exact typed return expectations for JIT compilation.
20
+ fn fuse_call_lazy_casts(func: &mut IrFunction) {
21
+ let mut use_counts: FxHashMap<Register, usize> = FxHashMap::default();
22
+ for bb in &func.blocks {
23
+ for instr in &bb.instructions {
24
+ for r in read_regs(instr) {
25
+ *use_counts.entry(r).or_insert(0) += 1;
26
+ }
27
+ }
28
+ }
29
+
30
+ for bb in &mut func.blocks {
31
+ let mut i = 0;
32
+ while i + 1 < bb.instructions.len() {
33
+ let fuse = match (&bb.instructions[i], &bb.instructions[i + 1]) {
34
+ (IrInstruction::CallLazy(tmp, site, args), IrInstruction::Cast(dst, ty, src))
35
+ if tmp == src && use_counts.get(tmp).copied().unwrap_or(0) == 1 =>
36
+ {
37
+ Some((*dst, *ty, *site, args.clone()))
38
+ }
39
+ (IrInstruction::CallLazy(tmp, site, args), IrInstruction::Move(dst, src))
40
+ if tmp == src
41
+ && use_counts.get(tmp).copied().unwrap_or(0) == 1
42
+ && func.register_types[dst.0] != IrType::Dynamic =>
43
+ {
44
+ Some((*dst, func.register_types[dst.0], *site, args.clone()))
45
+ }
46
+ _ => None,
47
+ };
48
+
49
+ if let Some((dst, ty, site, args)) = fuse {
50
+ func.register_types[dst.0] = ty;
51
+ bb.instructions[i] = IrInstruction::CallLazy(dst, site, args);
52
+ bb.instructions.remove(i + 1);
53
+ } else {
54
+ i += 1;
55
+ }
56
+ }
57
+ }
58
+ }
59
+
60
+ fn fold_constants_local(func: &mut IrFunction) -> bool {
61
+ let mut changed = false;
62
+
63
+ for bb in &mut func.blocks {
64
+ let mut env: FxHashMap<Register, Constant> = FxHashMap::default();
65
+ let mut copies: FxHashMap<Register, Register> = FxHashMap::default();
66
+
67
+ for instr in &mut bb.instructions {
68
+ match instr {
69
+ IrInstruction::LoadConst(dst, c) => {
70
+ copies.remove(dst);
71
+ env.insert(*dst, c.clone());
72
+ }
73
+ IrInstruction::Move(dst, src) => {
74
+ let resolved = resolve_copy(*src, &copies);
75
+ if resolved != *src {
76
+ *src = resolved;
77
+ changed = true;
78
+ }
79
+ if let Some(c) = env.get(&resolved).cloned() {
80
+ env.insert(*dst, c.clone());
81
+ copies.remove(dst);
82
+ *instr = IrInstruction::LoadConst(*dst, c);
83
+ changed = true;
84
+ } else {
85
+ env.remove(dst);
86
+ if dst != &resolved {
87
+ copies.insert(*dst, resolved);
88
+ }
89
+ }
90
+ }
91
+ IrInstruction::UnaryOp(op, dst, src) => {
92
+ let resolved = resolve_copy(*src, &copies);
93
+ *src = resolved;
94
+ if let Some(c) = env.get(&resolved).cloned()
95
+ && let Some(folded) = eval_const_unary(*op, &c)
96
+ {
97
+ env.insert(*dst, folded.clone());
98
+ copies.remove(dst);
99
+ *instr = IrInstruction::LoadConst(*dst, folded);
100
+ changed = true;
101
+ continue;
102
+ }
103
+ env.remove(dst);
104
+ copies.remove(dst);
105
+ }
106
+ IrInstruction::BinOp(op, dst, l, r) => {
107
+ let rl = resolve_copy(*l, &copies);
108
+ let rr = resolve_copy(*r, &copies);
109
+ *l = rl;
110
+ *r = rr;
111
+ if let (Some(cl), Some(cr)) = (env.get(&rl), env.get(&rr))
112
+ && let Some(folded) = eval_const_binop(*op, cl, cr)
113
+ {
114
+ env.insert(*dst, folded.clone());
115
+ copies.remove(dst);
116
+ *instr = IrInstruction::LoadConst(*dst, folded);
117
+ changed = true;
118
+ continue;
119
+ }
120
+ env.remove(dst);
121
+ copies.remove(dst);
122
+ }
123
+ IrInstruction::Cast(dst, ty, src) => {
124
+ let resolved = resolve_copy(*src, &copies);
125
+ *src = resolved;
126
+ if let Some(c) = env.get(&resolved).cloned()
127
+ && let Some(folded) = eval_const_cast(*ty, &c)
128
+ {
129
+ env.insert(*dst, folded.clone());
130
+ copies.remove(dst);
131
+ *instr = IrInstruction::LoadConst(*dst, folded);
132
+ changed = true;
133
+ continue;
134
+ }
135
+ env.remove(dst);
136
+ copies.remove(dst);
137
+ }
138
+ IrInstruction::BranchIf(cond, t_bb, f_bb) => {
139
+ let rc = resolve_copy(*cond, &copies);
140
+ *cond = rc;
141
+ if let Some(c) = env.get(&rc)
142
+ && let Some(b) = const_truthy(c)
143
+ {
144
+ let target = if b { *t_bb } else { *f_bb };
145
+ *instr = IrInstruction::Jump(target);
146
+ changed = true;
147
+ }
148
+ }
149
+ other => {
150
+ for r in mut_read_regs(other) {
151
+ let resolved = resolve_copy(*r, &copies);
152
+ if resolved != *r {
153
+ *r = resolved;
154
+ changed = true;
155
+ }
156
+ }
157
+ if let Some(dst) = write_reg(other) {
158
+ env.remove(&dst);
159
+ copies.remove(&dst);
160
+ }
161
+ }
162
+ }
163
+ }
164
+ }
165
+
166
+ changed
167
+ }
168
+
169
+ fn resolve_copy(mut r: Register, copies: &FxHashMap<Register, Register>) -> Register {
170
+ let mut hops = 0;
171
+ while let Some(&prev) = copies.get(&r) {
172
+ if prev == r || hops > 16 {
173
+ break;
174
+ }
175
+ r = prev;
176
+ hops += 1;
177
+ }
178
+ r
179
+ }
180
+
181
+ fn eval_const_unary(op: IrUnaryOp, c: &Constant) -> Option<Constant> {
182
+ match (op, c) {
183
+ (IrUnaryOp::Neg, Constant::Int(i)) => Some(Constant::Int(i.wrapping_neg())),
184
+ (IrUnaryOp::Neg, Constant::Float(f)) => Some(Constant::Float(-f)),
185
+ (IrUnaryOp::Not, c) => const_truthy(c).map(|b| Constant::Bool(!b)),
186
+ (IrUnaryOp::BitNot, Constant::Int(i)) => Some(Constant::Int(!i)),
187
+ (IrUnaryOp::BitsToF64, Constant::Int(i)) => Some(Constant::Float(f64::from_bits(*i as u64))),
188
+ _ => None,
189
+ }
190
+ }
191
+
192
+ fn eval_const_cast(ty: IrType, c: &Constant) -> Option<Constant> {
193
+ match (ty, c) {
194
+ (IrType::I64, Constant::Int(i)) => Some(Constant::Int(*i)),
195
+ (IrType::I64, Constant::Float(f)) => Some(Constant::Int(*f as i64)),
196
+ (IrType::I64, Constant::Bool(b)) => Some(Constant::Int(i64::from(*b))),
197
+ (IrType::F64, Constant::Float(f)) => Some(Constant::Float(*f)),
198
+ (IrType::F64, Constant::Int(i)) => Some(Constant::Float(*i as f64)),
199
+ (IrType::Bool, c) => const_truthy(c).map(Constant::Bool),
200
+ _ => None,
201
+ }
202
+ }
203
+
204
+ fn const_truthy(c: &Constant) -> Option<bool> {
205
+ match c {
206
+ Constant::Nil => Some(false),
207
+ Constant::Bool(b) => Some(*b),
208
+ Constant::Int(i) => Some(*i != 0),
209
+ Constant::Float(f) => Some(*f != 0.0),
210
+ Constant::String(s) => Some(!s.is_empty()),
211
+ }
212
+ }
213
+
214
+ fn eval_const_binop(op: IrBinOp, a: &Constant, b: &Constant) -> Option<Constant> {
215
+ match (a, b) {
216
+ (Constant::Int(x), Constant::Int(y)) => match op {
217
+ IrBinOp::Add => Some(Constant::Int(x.wrapping_add(*y))),
218
+ IrBinOp::Sub => Some(Constant::Int(x.wrapping_sub(*y))),
219
+ IrBinOp::Mul => Some(Constant::Int(x.wrapping_mul(*y))),
220
+ IrBinOp::Div if *y != 0 => Some(Constant::Int(x.wrapping_div(*y))),
221
+ IrBinOp::FloorDiv if *y != 0 => Some(Constant::Int(x.div_euclid(*y))),
222
+ IrBinOp::Mod if *y != 0 => Some(Constant::Int(x.wrapping_rem(*y))),
223
+ IrBinOp::Eq => Some(Constant::Bool(x == y)),
224
+ IrBinOp::Ne => Some(Constant::Bool(x != y)),
225
+ IrBinOp::Lt => Some(Constant::Bool(x < y)),
226
+ IrBinOp::Le => Some(Constant::Bool(x <= y)),
227
+ IrBinOp::Gt => Some(Constant::Bool(x > y)),
228
+ IrBinOp::Ge => Some(Constant::Bool(x >= y)),
229
+ IrBinOp::BitAnd => Some(Constant::Int(x & y)),
230
+ IrBinOp::BitOr => Some(Constant::Int(x | y)),
231
+ IrBinOp::BitXor => Some(Constant::Int(x ^ y)),
232
+ IrBinOp::Shl => Some(Constant::Int(x.wrapping_shl(*y as u32))),
233
+ IrBinOp::Shr => Some(Constant::Int(x.wrapping_shr(*y as u32))),
234
+ _ => None,
235
+ },
236
+ (Constant::Float(x), Constant::Float(y)) => eval_float_binop(op, *x, *y),
237
+ (Constant::Int(x), Constant::Float(y)) => eval_float_binop(op, *x as f64, *y),
238
+ (Constant::Float(x), Constant::Int(y)) => eval_float_binop(op, *x, *y as f64),
239
+ (Constant::Bool(x), Constant::Bool(y)) => match op {
240
+ IrBinOp::Eq => Some(Constant::Bool(x == y)),
241
+ IrBinOp::Ne => Some(Constant::Bool(x != y)),
242
+ IrBinOp::And => Some(Constant::Bool(*x && *y)),
243
+ IrBinOp::Or => Some(Constant::Bool(*x || *y)),
244
+ _ => None,
245
+ },
246
+ (Constant::String(x), Constant::String(y)) if op == IrBinOp::Add => {
247
+ Some(Constant::String(format!("{x}{y}")))
248
+ }
249
+ _ => None,
250
+ }
251
+ }
252
+
253
+ fn eval_float_binop(op: IrBinOp, x: f64, y: f64) -> Option<Constant> {
254
+ match op {
255
+ IrBinOp::Add => Some(Constant::Float(x + y)),
256
+ IrBinOp::Sub => Some(Constant::Float(x - y)),
257
+ IrBinOp::Mul => Some(Constant::Float(x * y)),
258
+ IrBinOp::Div if y != 0.0 => Some(Constant::Float(x / y)),
259
+ IrBinOp::FloorDiv if y != 0.0 => Some(Constant::Float((x / y).floor())),
260
+ IrBinOp::Mod if y != 0.0 => Some(Constant::Float(x % y)),
261
+ IrBinOp::Eq => Some(Constant::Bool(x == y)),
262
+ IrBinOp::Ne => Some(Constant::Bool(x != y)),
263
+ IrBinOp::Lt => Some(Constant::Bool(x < y)),
264
+ IrBinOp::Le => Some(Constant::Bool(x <= y)),
265
+ IrBinOp::Gt => Some(Constant::Bool(x > y)),
266
+ IrBinOp::Ge => Some(Constant::Bool(x >= y)),
267
+ _ => None,
268
+ }
269
+ }
270
+
271
+ fn prune_unreachable_blocks(func: &mut IrFunction) -> bool {
272
+ if func.blocks.is_empty() {
273
+ return false;
274
+ }
275
+ let mut visited = FxHashSet::default();
276
+ let mut stack = vec![0usize];
277
+ while let Some(idx) = stack.pop() {
278
+ if !visited.insert(idx) {
279
+ continue;
280
+ }
281
+ if let Some(bb) = func.blocks.get(idx) {
282
+ for instr in &bb.instructions {
283
+ match instr {
284
+ IrInstruction::Jump(t) => stack.push(t.0),
285
+ IrInstruction::BranchIf(_, a, b) => {
286
+ stack.push(a.0);
287
+ stack.push(b.0);
288
+ }
289
+ _ => {}
290
+ }
291
+ }
292
+ }
293
+ }
294
+
295
+ if visited.len() == func.blocks.len() {
296
+ return false;
297
+ }
298
+
299
+ let mut old_to_new = FxHashMap::default();
300
+ let mut new_blocks = Vec::with_capacity(visited.len());
301
+ for (old_idx, bb) in func.blocks.iter().enumerate() {
302
+ if visited.contains(&old_idx) {
303
+ let new_id = BasicBlockId(new_blocks.len());
304
+ old_to_new.insert(bb.id, new_id);
305
+ let mut cloned = bb.clone();
306
+ cloned.id = new_id;
307
+ new_blocks.push(cloned);
308
+ }
309
+ }
310
+
311
+ for bb in &mut new_blocks {
312
+ for instr in &mut bb.instructions {
313
+ match instr {
314
+ IrInstruction::Jump(t) => {
315
+ *t = old_to_new[t];
316
+ }
317
+ IrInstruction::BranchIf(_, a, b) => {
318
+ *a = old_to_new[a];
319
+ *b = old_to_new[b];
320
+ }
321
+ _ => {}
322
+ }
323
+ }
324
+ }
325
+
326
+ func.blocks = new_blocks;
327
+ true
328
+ }
329
+
330
+ fn eliminate_dead_instructions(func: &mut IrFunction) -> bool {
331
+ let mut used: FxHashSet<Register> = FxHashSet::default();
332
+ for bb in &func.blocks {
333
+ for instr in &bb.instructions {
334
+ for r in read_regs(instr) {
335
+ used.insert(r);
336
+ }
337
+ }
338
+ }
339
+
340
+ let mut changed = false;
341
+ for bb in &mut func.blocks {
342
+ let before = bb.instructions.len();
343
+ bb.instructions.retain(|instr| match instr {
344
+ IrInstruction::LoadConst(d, _)
345
+ | IrInstruction::Move(d, _)
346
+ | IrInstruction::Cast(d, _, _)
347
+ | IrInstruction::UnaryOp(_, d, _)
348
+ | IrInstruction::CreateArray(d, _)
349
+ | IrInstruction::CreateObject(d, _)
350
+ | IrInstruction::ResolveFn(d, _) => used.contains(d),
351
+ IrInstruction::BinOp(op, d, _, _) => {
352
+ // Preserve division/mod in case of divide-by-zero trap semantics
353
+ matches!(op, IrBinOp::Div | IrBinOp::FloorDiv | IrBinOp::Mod) || used.contains(d)
354
+ }
355
+ _ => true,
356
+ });
357
+ if bb.instructions.len() != before {
358
+ changed = true;
359
+ }
360
+ }
361
+ changed
362
+ }
363
+
364
+ fn infer_types(func: &mut IrFunction) {
365
+ let mut types = func.register_types.clone();
366
+ for &(reg, ty) in &func.params {
367
+ if ty != IrType::Dynamic {
368
+ types[reg.0] = ty;
369
+ }
370
+ }
371
+
372
+ for _ in 0..8 {
373
+ let mut changed = false;
374
+ for bb in &func.blocks {
375
+ for instr in &bb.instructions {
376
+ match instr {
377
+ IrInstruction::LoadConst(dst, c) => {
378
+ let cty = match c {
379
+ Constant::Nil => IrType::Dynamic,
380
+ Constant::Bool(_) => IrType::Bool,
381
+ Constant::Int(_) => IrType::I64,
382
+ Constant::Float(_) => IrType::F64,
383
+ Constant::String(_) => IrType::Str,
384
+ };
385
+ changed |= update_reg_type(&mut types, *dst, cty);
386
+ }
387
+ IrInstruction::Move(dst, src) => {
388
+ let sty = types[src.0];
389
+ changed |= update_reg_type(&mut types, *dst, sty);
390
+ }
391
+ IrInstruction::Cast(dst, target, _) => {
392
+ changed |= update_reg_type(&mut types, *dst, *target);
393
+ }
394
+ IrInstruction::UnaryOp(op, dst, src) => {
395
+ let rty = match op {
396
+ IrUnaryOp::Not => IrType::Bool,
397
+ IrUnaryOp::BitNot => IrType::I64,
398
+ IrUnaryOp::BitsToF64 => IrType::F64,
399
+ IrUnaryOp::Neg => types[src.0],
400
+ };
401
+ changed |= update_reg_type(&mut types, *dst, rty);
402
+ }
403
+ IrInstruction::BinOp(op, dst, l, r) => {
404
+ let lt = types[l.0];
405
+ let rt = types[r.0];
406
+ let res_ty = match op {
407
+ IrBinOp::Eq
408
+ | IrBinOp::Ne
409
+ | IrBinOp::Lt
410
+ | IrBinOp::Le
411
+ | IrBinOp::Gt
412
+ | IrBinOp::Ge
413
+ | IrBinOp::And
414
+ | IrBinOp::Or => IrType::Bool,
415
+ IrBinOp::BitAnd
416
+ | IrBinOp::BitOr
417
+ | IrBinOp::BitXor
418
+ | IrBinOp::Shl
419
+ | IrBinOp::Shr => IrType::I64,
420
+ IrBinOp::Add => match (lt, rt) {
421
+ (IrType::I64, IrType::I64) => IrType::I64,
422
+ (IrType::F64, IrType::F64)
423
+ | (IrType::I64, IrType::F64)
424
+ | (IrType::F64, IrType::I64) => IrType::F64,
425
+ (IrType::Str, IrType::Str) => IrType::Str,
426
+ _ => IrType::Dynamic,
427
+ },
428
+ IrBinOp::Sub | IrBinOp::Mul | IrBinOp::Div | IrBinOp::FloorDiv | IrBinOp::Mod => {
429
+ match (lt, rt) {
430
+ (IrType::I64, IrType::I64) => IrType::I64,
431
+ (IrType::F64, IrType::F64)
432
+ | (IrType::I64, IrType::F64)
433
+ | (IrType::F64, IrType::I64) => IrType::F64,
434
+ _ => IrType::Dynamic,
435
+ }
436
+ }
437
+ };
438
+ changed |= update_reg_type(&mut types, *dst, res_ty);
439
+ }
440
+ IrInstruction::CallBuiltin(dst, b, _) => {
441
+ let bty = match b {
442
+ BuiltinId::Len | BuiltinId::Int | BuiltinId::BufLen | BuiltinId::ClockMs | BuiltinId::Printf => IrType::I64,
443
+ BuiltinId::Float | BuiltinId::Sqrt | BuiltinId::Pow => IrType::F64,
444
+ BuiltinId::Bool => IrType::Bool,
445
+ BuiltinId::Str | BuiltinId::TypeOf => IrType::Str,
446
+ BuiltinId::Print | BuiltinId::Println | BuiltinId::Assert => {
447
+ IrType::Void
448
+ }
449
+ _ => IrType::Dynamic,
450
+ };
451
+ changed |= update_reg_type(&mut types, *dst, bty);
452
+ }
453
+ IrInstruction::CallFn(dst, id, _) if *id == func.id => {
454
+ let rty = func.return_type;
455
+ changed |= update_reg_type(&mut types, *dst, rty);
456
+ }
457
+ IrInstruction::LoadByte(dst, _, _) => {
458
+ changed |= update_reg_type(&mut types, *dst, IrType::I64);
459
+ }
460
+ _ => {}
461
+ }
462
+ }
463
+ }
464
+ if !changed {
465
+ break;
466
+ }
467
+ }
468
+
469
+ if func.return_type == IrType::Dynamic {
470
+ let mut inferred_ret: Option<IrType> = None;
471
+ let mut mixed_void = false;
472
+ for bb in &func.blocks {
473
+ for instr in &bb.instructions {
474
+ if let IrInstruction::Return(opt_r) = instr {
475
+ match opt_r {
476
+ None => {
477
+ if inferred_ret.is_some() {
478
+ mixed_void = true;
479
+ }
480
+ inferred_ret = Some(IrType::Void);
481
+ }
482
+ Some(r) => {
483
+ let rty = types[r.0];
484
+ inferred_ret = Some(match inferred_ret {
485
+ None => rty,
486
+ Some(IrType::Void) => {
487
+ mixed_void = true;
488
+ IrType::Dynamic
489
+ }
490
+ Some(prev) => prev.join(rty),
491
+ });
492
+ }
493
+ }
494
+ }
495
+ }
496
+ }
497
+ if !mixed_void && let Some(rt) = inferred_ret {
498
+ func.return_type = rt;
499
+ }
500
+ }
501
+
502
+ func.register_types = types;
503
+ }
504
+
505
+ fn update_reg_type(types: &mut [IrType], reg: Register, cand: IrType) -> bool {
506
+ if cand == IrType::Dynamic {
507
+ return false;
508
+ }
509
+ if cand == IrType::Void {
510
+ if types[reg.0] == IrType::Dynamic {
511
+ types[reg.0] = IrType::Void;
512
+ return true;
513
+ }
514
+ return false;
515
+ }
516
+ let old = types[reg.0];
517
+ let next = if old == IrType::Dynamic {
518
+ cand
519
+ } else {
520
+ old.join(cand)
521
+ };
522
+ if next != old {
523
+ types[reg.0] = next;
524
+ true
525
+ } else {
526
+ false
527
+ }
528
+ }
529
+
530
+ fn write_reg(instr: &IrInstruction) -> Option<Register> {
531
+ match instr {
532
+ IrInstruction::LoadConst(d, _)
533
+ | IrInstruction::Move(d, _)
534
+ | IrInstruction::Cast(d, _, _)
535
+ | IrInstruction::BinOp(_, d, _, _)
536
+ | IrInstruction::UnaryOp(_, d, _)
537
+ | IrInstruction::CallLazy(d, _, _)
538
+ | IrInstruction::CallFn(d, _, _)
539
+ | IrInstruction::CallIndirect(d, _, _)
540
+ | IrInstruction::CallBuiltin(d, _, _)
541
+ | IrInstruction::ResolveFn(d, _)
542
+ | IrInstruction::CreateArray(d, _)
543
+ | IrInstruction::GetIndex(d, _, _)
544
+ | IrInstruction::CreateObject(d, _)
545
+ | IrInstruction::GetField(d, _, _)
546
+ | IrInstruction::AllocBuffer(d, _)
547
+ | IrInstruction::LoadByte(d, _, _) => Some(*d),
548
+ _ => None,
549
+ }
550
+ }
551
+
552
+ fn read_regs(instr: &IrInstruction) -> Vec<Register> {
553
+ match instr {
554
+ IrInstruction::Move(_, s)
555
+ | IrInstruction::Cast(_, _, s)
556
+ | IrInstruction::UnaryOp(_, _, s)
557
+ | IrInstruction::AllocBuffer(_, s) => vec![*s],
558
+ IrInstruction::BinOp(_, _, l, r)
559
+ | IrInstruction::GetIndex(_, l, r)
560
+ | IrInstruction::LoadByte(_, l, r) => vec![*l, *r],
561
+ IrInstruction::CallLazy(_, _, args)
562
+ | IrInstruction::CallFn(_, _, args)
563
+ | IrInstruction::CallBuiltin(_, _, args)
564
+ | IrInstruction::CreateArray(_, args) => args.clone(),
565
+ IrInstruction::CallIndirect(_, f, args) => {
566
+ let mut v = vec![*f];
567
+ v.extend(args);
568
+ v
569
+ }
570
+ IrInstruction::Return(Some(r)) | IrInstruction::BranchIf(r, _, _) => vec![*r],
571
+ IrInstruction::SetField(o, _, v) => vec![*o, *v],
572
+ IrInstruction::GetField(_, o, _) => vec![*o],
573
+ IrInstruction::SetIndex(o, i, v) | IrInstruction::StoreByte(o, i, v) => vec![*o, *i, *v],
574
+ _ => Vec::new(),
575
+ }
576
+ }
577
+
578
+ fn mut_read_regs(instr: &mut IrInstruction) -> Vec<&mut Register> {
579
+ match instr {
580
+ IrInstruction::Move(_, s)
581
+ | IrInstruction::Cast(_, _, s)
582
+ | IrInstruction::UnaryOp(_, _, s)
583
+ | IrInstruction::AllocBuffer(_, s) => vec![s],
584
+ IrInstruction::BinOp(_, _, l, r)
585
+ | IrInstruction::GetIndex(_, l, r)
586
+ | IrInstruction::LoadByte(_, l, r) => vec![l, r],
587
+ IrInstruction::CallLazy(_, _, args)
588
+ | IrInstruction::CallFn(_, _, args)
589
+ | IrInstruction::CallBuiltin(_, _, args)
590
+ | IrInstruction::CreateArray(_, args) => args.iter_mut().collect(),
591
+ IrInstruction::CallIndirect(_, f, args) => {
592
+ let mut v = vec![f];
593
+ v.extend(args.iter_mut());
594
+ v
595
+ }
596
+ IrInstruction::Return(Some(r)) | IrInstruction::BranchIf(r, _, _) => vec![r],
597
+ IrInstruction::SetField(o, _, v) => vec![o, v],
598
+ IrInstruction::GetField(_, o, _) => vec![o],
599
+ IrInstruction::SetIndex(o, i, v) | IrInstruction::StoreByte(o, i, v) => vec![o, i, v],
600
+ _ => Vec::new(),
601
+ }
602
+ }