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