somni 0.2.0

Somni scripting language and VM
Documentation
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
use std::collections::{HashMap, HashSet};

use indexmap::IndexMap;

use crate::{
    error::CompileError,
    ir::{self, BlockIndex, VariableIndex},
    string_interner::{self, StringIndex},
    variable_tracker::{LocalVariableIndex, ScopeData},
};
use somni_parser::Location;

#[derive(Debug, Clone, Copy, PartialEq)]
enum ConstraintKind {
    Equals {
        left: VariableIndex,
        right: VariableIndex,
    },
    Requires {
        variable: VariableIndex,
        required_type: ir::Variable,
    },
    Reference {
        left: VariableIndex,
        right: VariableIndex,
    },
}

#[derive(Debug, Clone, Copy, PartialEq)]
struct Constraint {
    source_location: Location,
    kind: ConstraintKind,
}

struct FunctionSignature {
    return_type: ir::Variable,
    arguments: Vec<ir::Variable>,
}

struct TypeResolver<'a, 's> {
    source: &'s str,
    constraints: Vec<Constraint>,
    globals: &'a IndexMap<StringIndex, ir::GlobalVariableInfo>,
    locals: &'a mut ScopeData,
}

impl<'a, 's> TypeResolver<'a, 's> {
    fn new(
        source: &'s str,
        globals: &'a IndexMap<StringIndex, ir::GlobalVariableInfo>,
        locals: &'a mut ScopeData,
    ) -> Self {
        Self {
            source,
            constraints: vec![],
            globals,
            locals,
        }
    }

    /// Require that a variable has a specific type at a given source location.
    fn require(
        &mut self,
        left: VariableIndex,
        ty: ir::Variable,
        source_location: Location,
    ) -> Result<(), CompileError<'s>> {
        self.constraints.push(Constraint {
            source_location,
            kind: ConstraintKind::Requires {
                variable: left,
                required_type: ty,
            },
        });
        self.step_resolve()?;

        Ok(())
    }

    /// Add a constraint that two variables must be equal, recording the source location where
    /// this requirement was made.
    fn equate(
        &mut self,
        left: VariableIndex,
        right: VariableIndex,
        source_location: Location,
    ) -> Result<(), CompileError<'s>> {
        self.constraints.push(Constraint {
            source_location,
            kind: ConstraintKind::Equals { left, right },
        });
        self.step_resolve()?;

        Ok(())
    }

    fn reference(
        &mut self,
        left: VariableIndex,
        right: VariableIndex,
        source_location: Location,
    ) -> Result<(), CompileError<'s>> {
        self.constraints.push(Constraint {
            source_location,
            kind: ConstraintKind::Reference { left, right },
        });
        self.step_resolve()?;

        Ok(())
    }

    fn current_type_of(&self, variable: VariableIndex) -> Option<ir::Variable> {
        match variable {
            VariableIndex::Local(local) | VariableIndex::Temporary(local) => {
                self.locals.variable(local).unwrap().ty
            }
            VariableIndex::Global(global) => {
                let Some((_name, global_info)) = self.globals.get_index(global.0) else {
                    unreachable!();
                };

                Some(ir::Variable::Value(global_info.ty))
            }
        }
    }

    fn step_resolve(&mut self) -> Result<bool, CompileError<'s>> {
        let mut i = 0;
        let mut any_changed = false;
        while i < self.constraints.len() {
            let constraint = &self.constraints[i];
            match constraint.kind {
                ConstraintKind::Equals { left, right } => {
                    let left_type = self.current_type_of(left);
                    let right_type = self.current_type_of(right);

                    let made_progress = match (left_type, right_type) {
                        (Some(left_ty), Some(right_ty)) if left_ty == right_ty => {
                            !left_ty.maybe_signed_integer() && !right_ty.maybe_signed_integer()
                        }
                        (Some(left_ty), Some(right_ty)) => {
                            if left_ty.maybe_signed_integer() && right_ty.is_integer() {
                                self.locals
                                    .variable_mut(left.local_index().unwrap())
                                    .unwrap()
                                    .ty = Some(right_ty);
                                any_changed = true;
                            } else if right_ty.maybe_signed_integer() && left_ty.is_integer() {
                                self.locals
                                    .variable_mut(right.local_index().unwrap())
                                    .unwrap()
                                    .ty = Some(left_ty);
                                any_changed = true;
                            } else {
                                return Err(CompileError {
                                    source: self.source,
                                    location: constraint.source_location,
                                    error: format!(
                                        "Type mismatch: expected {left_ty}, found {right_ty}",
                                    )
                                    .into_boxed_str(),
                                });
                            }

                            !left_ty.maybe_signed_integer() && !right_ty.maybe_signed_integer()
                        }
                        (Some(ty), None) => {
                            self.locals
                                .variable_mut(right.local_index().unwrap())
                                .unwrap()
                                .ty = Some(ty);
                            any_changed = true;
                            !ty.maybe_signed_integer()
                        }
                        (None, Some(ty)) => {
                            self.locals
                                .variable_mut(left.local_index().unwrap())
                                .unwrap()
                                .ty = Some(ty);
                            any_changed = true;
                            !ty.maybe_signed_integer()
                        }
                        (None, None) => {
                            // Cannot make progress with this constraint yet.
                            false
                        }
                    };

                    if made_progress {
                        // If we made progress, we can remove this constraint.
                        any_changed = true;
                        self.constraints.swap_remove(i);
                    } else {
                        // If we didn't make progress, just move to the next constraint.
                        i += 1;
                    }
                }
                ConstraintKind::Requires {
                    variable,
                    required_type: required_ty,
                } => {
                    let made_progress = match variable {
                        VariableIndex::Local(local) | VariableIndex::Temporary(local) => {
                            let local_info = self.locals.variable_mut(local).unwrap();
                            match local_info.ty.as_mut() {
                                Some(ty) => {
                                    if *ty != required_ty {
                                        any_changed = true;
                                    }

                                    // If any of the types is a maybe integer, the other needs to be some kind of integer.
                                    let is_compatible = (required_ty.maybe_signed_integer()
                                        || ty.maybe_signed_integer())
                                        && (ty.is_integer() && required_ty.is_integer());

                                    if is_compatible {
                                        true
                                    } else if *ty == required_ty {
                                        *ty = required_ty;
                                        !ty.maybe_signed_integer()
                                    } else {
                                        return Err(CompileError {
                                            source: self.source,
                                            location: constraint.source_location,
                                            error: format!(
                                                "Type mismatch: expected {required_ty}, found {}",
                                                *ty
                                            )
                                            .into_boxed_str(),
                                        });
                                    }
                                }
                                None => {
                                    // If the type is not set, we can set it now.
                                    local_info.ty = Some(required_ty);
                                    any_changed = true;
                                    true
                                }
                            }
                        }
                        VariableIndex::Global(global) => {
                            let Some((_name, _global_info)) = self.globals.get_index(global.0)
                            else {
                                unreachable!();
                            };

                            // TODO
                            true
                        }
                    };

                    if made_progress {
                        any_changed = true;
                        self.constraints.swap_remove(i);
                    } else {
                        // If we didn't make progress, just move to the next constraint.
                        i += 1;
                    }
                }

                ConstraintKind::Reference { left, right } => {
                    let left_type = match left {
                        VariableIndex::Global(_) => {
                            return Err(CompileError {
                                source: self.source,
                                location: constraint.source_location,
                                error: "Globals cannot be references".to_string().into_boxed_str(),
                            });
                        }
                        _ => self.current_type_of(left),
                    };
                    let right_type = self.current_type_of(right);

                    let made_progress = match (left_type, right_type) {
                        (Some(left_ty), Some(right_ty)) if left_ty.reference() == right_ty => {
                            !left_ty.maybe_signed_integer()
                        }
                        (Some(left_ty), Some(right_ty))
                            if left_ty.maybe_signed_integer() && right_ty.is_integer() =>
                        {
                            if !right_ty.maybe_signed_integer() {
                                self.locals
                                    .variable_mut(left.local_index().unwrap())
                                    .unwrap()
                                    .ty = Some(right_ty.reference());

                                any_changed = true;
                            }
                            !right_ty.maybe_signed_integer()
                        }
                        (Some(left_ty), Some(right_ty))
                            if right_ty.maybe_signed_integer() && left_ty.is_integer() =>
                        {
                            if !left_ty.maybe_signed_integer() {
                                let left_ty =
                                    left_ty.dereference().ok_or_else(|| CompileError {
                                        source: self.source,
                                        location: constraint.source_location,
                                        error: format!("Cannot dereference {left_ty}")
                                            .into_boxed_str(),
                                    })?;
                                self.locals
                                    .variable_mut(right.local_index().unwrap())
                                    .unwrap()
                                    .ty = Some(left_ty);

                                any_changed = true;
                            }
                            !left_ty.maybe_signed_integer()
                        }

                        (Some(left_ty), Some(right_ty)) => {
                            return Err(CompileError {
                                source: self.source,
                                location: constraint.source_location,
                                error: format!(
                                    "Type mismatch: expected {left_ty}, found {right_ty}"
                                )
                                .into_boxed_str(),
                            });
                        }
                        (Some(ty), None) => {
                            let ty = ty.dereference().ok_or_else(|| CompileError {
                                source: self.source,
                                location: constraint.source_location,
                                error: format!("Cannot dereference {ty}").into_boxed_str(),
                            })?;
                            self.locals
                                .variable_mut(right.local_index().unwrap())
                                .unwrap()
                                .ty = Some(ty);

                            any_changed = true;

                            !ty.maybe_signed_integer()
                        }
                        (None, Some(ty)) => {
                            self.locals
                                .variable_mut(left.local_index().unwrap())
                                .unwrap()
                                .ty = Some(ty.reference());

                            any_changed = true;

                            !ty.maybe_signed_integer()
                        }
                        (None, None) => false,
                    };

                    if made_progress {
                        // If we made progress, we can remove this constraint.
                        any_changed = true;
                        self.constraints.swap_remove(i);
                    } else {
                        // If we didn't make progress, just move to the next constraint.
                        i += 1;
                    }
                }
            }
        }

        Ok(any_changed)
    }
}

pub fn transform_ir<'s>(source: &'s str, ir: &mut ir::Program) -> Result<(), CompileError<'s>> {
    for func in ir.functions.values_mut() {
        bypass_redundant_jump_blocks(func);
        merge_identical_blocks(func);
        remove_unreachable_blocks(func);
    }
    propagate_variable_types(source, ir)?;

    // Remove unnecessary assignments (assignments without reads after them).
    for func in ir.functions.values_mut() {
        let Some(implem) = func.implementation.as_mut() else {
            continue;
        };

        // For now we only optimize variables that are declared AND freed in the same block.
        // This is way too conservative, but relaxing it would require a figuring out loops.
        let mut relevant_variables_in_block = HashMap::new();
        for (block_idx, block) in implem.blocks.iter().enumerate() {
            let mut declared = HashMap::new();
            let mut freed = HashSet::new();
            for instruction in &block.instructions {
                match instruction.instruction {
                    ir::Ir::Declare(var, _) => {
                        // We also don't allow optimizing TopOfStack variables for now.
                        if var.allocation_method == ir::AllocationMethod::FirstFit {
                            declared.insert(var.index, var.is_argument);
                        }
                    }
                    ir::Ir::FreeVariable(var) => {
                        freed.insert(var);
                    }
                    _ => {}
                }
            }

            let mut relevant = HashMap::new();
            for freed in freed {
                if let Some((key, value)) = declared.remove_entry(&freed) {
                    relevant.insert(key, value);
                }
            }

            relevant_variables_in_block.insert(block_idx, relevant);
        }

        for (block_idx, block) in implem.blocks.iter_mut().enumerate() {
            let variables_in_block = &relevant_variables_in_block[&block_idx];
            propagate_destination(block, variables_in_block);
            propagate_value(block, variables_in_block);
            remove_unused_assignments(block, variables_in_block);
            remove_unused_variables(block, variables_in_block);
        }
    }
    // Remove unused variables.

    Ok(())
}

/// This function removes blocks that only contain a jump to another block,
/// effectively bypassing them. The bypassed blocks will be removed by `remove_unreachable_blocks`.
fn bypass_redundant_jump_blocks(func: &mut ir::Function) {
    // We will keep track of replacements for blocks that are bypassed.
    let mut replacements = HashMap::new();
    let Some(implem) = func.implementation.as_mut() else {
        return;
    };

    for (index, block) in implem.blocks.iter().enumerate() {
        if let ir::Termination::Jump { to, .. } = block.terminator {
            // If the block only contains a jump, we can bypass it.
            if block.instructions.is_empty() {
                // If the target block is already replaced, we use that replacement.
                let new_target = replacements.get(&to).copied().unwrap_or(to);
                // If this block is a target of another replacement, update that replacement.
                if let Some(to_update) =
                    replacements.values_mut().find(|v| **v == BlockIndex(index))
                {
                    *to_update = new_target;
                }

                replacements.insert(BlockIndex(index), new_target);
            }
        }
    }

    let remap = |idx: &BlockIndex| replacements.get(idx).copied().unwrap_or(*idx);

    remap_block_idxs(implem, remap);
}

fn remap_block_idxs<F>(implem: &mut ir::FunctionImplementation, remap: F)
where
    F: Fn(&BlockIndex) -> BlockIndex,
{
    for block in &mut implem.blocks {
        match &mut block.terminator {
            ir::Termination::Jump { to, .. } => *to = remap(to),
            ir::Termination::If {
                then_block,
                else_block,
                ..
            } => {
                *then_block = remap(then_block);
                *else_block = remap(else_block);
            }
            ir::Termination::Return { .. } => {}
        }
    }
}

fn remove_unreachable_blocks(func: &mut ir::Function) {
    let Some(implem) = func.implementation.as_mut() else {
        return;
    };
    let mut reachable = vec![false; implem.blocks.len()];
    let mut stack = vec![BlockIndex(0)]; // Start with the first block.

    while let Some(BlockIndex(block_index)) = stack.pop() {
        if reachable[block_index] {
            continue; // Already visited this block.
        }
        reachable[block_index] = true;

        let block = &implem.blocks[block_index];

        match block.terminator {
            ir::Termination::Return { .. } => {}
            ir::Termination::Jump { to, .. } => {
                stack.push(to);
            }
            ir::Termination::If {
                then_block,
                else_block,
                ..
            } => {
                stack.push(then_block);
                stack.push(else_block);
            }
        }
    }

    // Renumber reachable blocks.
    let remap = |idx: &BlockIndex| {
        let new_idx = reachable.iter().take(idx.0 + 1).filter(|&&r| r).count() - 1;

        BlockIndex(new_idx)
    };

    remap_block_idxs(implem, remap);

    // Remove unreachable blocks.
    let mut current = 0;
    implem.blocks.retain(|_| {
        let retain = reachable[current];
        current += 1;
        retain
    });
}

fn merge_identical_blocks(func: &mut ir::Function) {
    let Some(implem) = func.implementation.as_mut() else {
        return;
    };
    let mut identical_to = (0..).take(implem.blocks.len()).collect::<Vec<_>>();

    for (idx, block) in implem.blocks.iter().enumerate() {
        // Check if this block is identical to any previous block.
        for (prev_idx, prev_block) in implem.blocks.iter().enumerate().take(idx) {
            if block == prev_block {
                // Mark this block as identical to the previous one.
                identical_to[idx] = prev_idx;
                break;
            }
        }
    }

    let remap = |idx: &BlockIndex| {
        // If the block is identical to another, use the first occurrence.
        BlockIndex(identical_to[idx.0])
    };

    remap_block_idxs(implem, remap);

    // remove_unreachable_blocks will trim the blocks
}

fn propagate_variable_types<'s>(
    source: &'s str,
    ir: &mut ir::Program,
) -> Result<(), CompileError<'s>> {
    let mut func_signatures = HashMap::new();

    // Collect function signatures first
    for (name, function) in &ir.functions {
        let signature = FunctionSignature {
            return_type: ir::Variable::Value(function.return_type),
            arguments: function.arguments.clone(),
        };
        func_signatures.insert(*name, signature);
    }

    for (name, function) in &mut ir.functions {
        propagate_variable_types_inner(
            source,
            *name,
            function,
            &ir.strings,
            &ir.globals,
            &func_signatures,
        )?;
    }

    Ok(())
}

fn propagate_variable_types_inner<'s>(
    source: &'s str,
    _name: StringIndex,
    function: &mut ir::Function,
    strings: &string_interner::StringInterner,
    globals: &IndexMap<StringIndex, ir::GlobalVariableInfo>,
    signatures: &HashMap<StringIndex, FunctionSignature>,
) -> Result<(), CompileError<'s>> {
    let Some(implem) = function.implementation.as_mut() else {
        return Ok(());
    };
    let mut resolver = TypeResolver::new(source, globals, &mut implem.variables);

    for block in &mut implem.blocks {
        for (idx, instruction) in block.instructions.iter().enumerate() {
            match &instruction.instruction {
                ir::Ir::Declare(dst, init_value) => {
                    if let Some(init_value) = init_value {
                        // If there's an initial value, we can infer the type from it.
                        resolver.require(
                            VariableIndex::Local(dst.index),
                            ir::Variable::Value(init_value.type_of()),
                            instruction.source_location,
                        )?;
                    }
                }
                ir::Ir::Assign(dst, src) => {
                    resolver.equate(*dst, *src, instruction.source_location)?;
                }
                ir::Ir::DerefAssign(dst, src) => {
                    resolver.reference(*src, *dst, instruction.source_location)?;
                }
                ir::Ir::Call(func, return_value, args) => {
                    if let Some(signature) = signatures.get(func) {
                        // If the function has a signature, we can require the return type.
                        resolver.require(
                            *return_value,
                            signature.return_type,
                            instruction.source_location,
                        )?;

                        if args.len() != signature.arguments.len() {
                            return Err(CompileError {
                                source,
                                location: instruction.source_location,
                                error: format!(
                                    "Function {} expected {} arguments, found {}",
                                    strings.lookup(*func),
                                    signature.arguments.len(),
                                    args.len()
                                )
                                .into_boxed_str(),
                            });
                        }

                        for (arg, arg_type) in args.iter().zip(signature.arguments.iter()) {
                            let mut last_assigned_at = Location::dummy();
                            for instr in block.instructions[..idx].iter().rev() {
                                if let ir::IrWithLocation {
                                    instruction: ir::Ir::Assign(dst, _),
                                    source_location,
                                } = instr
                                {
                                    if dst == arg {
                                        last_assigned_at = *source_location;
                                        break;
                                    }
                                }
                            }
                            resolver.require(*arg, *arg_type, last_assigned_at)?;
                        }
                    }
                }
                ir::Ir::BinaryOperator(op, dst, lhs, rhs) => {
                    match strings.lookup(*op) {
                        "<=" | "<" | ">=" | ">" | "==" | "!=" => {
                            // For comparison operators, the result is a boolean.
                            resolver.require(
                                *dst,
                                ir::Variable::Value(ir::Type::Bool),
                                instruction.source_location,
                            )?;
                            // The left and right hand sides must be of the same type.
                            resolver.equate(*lhs, *rhs, instruction.source_location)?;
                        }
                        _ => {
                            // For now, all other operators require the
                            // three variables to be of the same type.
                            resolver.equate(*dst, *lhs, instruction.source_location)?;
                            resolver.equate(*dst, *rhs, instruction.source_location)?;
                        }
                    }
                }
                ir::Ir::UnaryOperator(op, dst, src) => {
                    match strings.lookup(*op) {
                        "*" => {
                            resolver.reference(*src, *dst, instruction.source_location)?;
                        }
                        "&" => {
                            resolver.reference(*dst, *src, instruction.source_location)?;
                        }
                        _ => {
                            // For now, the destination and source must be of the same type.
                            resolver.equate(*dst, *src, instruction.source_location)?;
                        }
                    }
                }
                ir::Ir::FreeVariable(_) => {}
            }
        }

        if let ir::Termination::If {
            source_location,
            condition,
            ..
        } = block.terminator
        {
            // Condition needs to be a boolean
            resolver.require(
                condition,
                ir::Variable::Value(ir::Type::Bool),
                source_location,
            )?;
        }
    }

    while !resolver.constraints.is_empty() {
        if !resolver.step_resolve()? {
            // No more progress can be made.
            break;
        }
    }

    Ok(())
}

fn propagate_destination(
    block: &mut ir::Block,
    variables_in_block: &HashMap<LocalVariableIndex, bool>,
) {
    for idx in (1..block.instructions.len()).rev() {
        if let ir::Ir::Assign(dst, src) = block.instructions[idx].instruction {
            // If src is not defined in this block, we can't avoid writing to it.
            if let Some(src) = src.local_index() {
                // If the source is not in the block, we can skip it.
                if !variables_in_block.contains_key(&src) {
                    continue;
                }
            } else {
                continue;
            }

            // If we find an assignment, let's try to remove it. We can remove it if we find the
            // src written previously in the block.
            for prev_idx in (0..idx).rev() {
                let read = match &block.instructions[prev_idx].instruction {
                    ir::Ir::Declare(info, _) => Some(info.index) == dst.local_index(),
                    ir::Ir::Assign(_, assign_source) => *assign_source == src,
                    ir::Ir::Call(_, _, args) => args.contains(&src),
                    ir::Ir::BinaryOperator(_, _, lhs, rhs) => *lhs == src || *rhs == src,
                    ir::Ir::UnaryOperator(_, _, operand) => *operand == src,
                    _ => false,
                };
                if read {
                    break;
                }
                let written = match &mut block.instructions[prev_idx].instruction {
                    ir::Ir::Assign(dst, _) => dst,
                    ir::Ir::Call(_, dst, _) => dst,
                    ir::Ir::BinaryOperator(_, dst, _, _) => dst,
                    ir::Ir::UnaryOperator(_, dst, _) => dst,
                    _ => continue,
                };

                if *written == src {
                    *written = dst; // Replace the written variable with the destination.
                    block.instructions.remove(idx); // Remove the assignment.
                    break;
                }
            }
        }
    }
}

fn propagate_value(block: &mut ir::Block, _: &HashMap<LocalVariableIndex, bool>) {
    let mut candidates = HashMap::new();
    let mut values = HashMap::new();
    let mut to_delete = vec![];
    for idx in 0..block.instructions.len() {
        match block.instructions[idx].instruction {
            ir::Ir::Declare(new, None) => {
                candidates.insert(new.index, (idx, new));
            }
            ir::Ir::Declare(new, Some(value)) => {
                values.insert(new.index, value);
            }
            ir::Ir::Assign(dst, src) => {
                let Some(dst) = dst.local_index() else {
                    continue;
                };
                if let Some((instruction_idx, decl)) = candidates.remove(&dst) {
                    let Some(src) = src.local_index() else {
                        continue;
                    };
                    if let Some(value) = values.get(&src) {
                        block.instructions[instruction_idx].instruction =
                            ir::Ir::Declare(decl, Some(*value));
                        to_delete.push(idx);
                    }
                }
            }
            _ => {}
        }
    }

    // Remove instructions
    let mut current = 0;
    block.instructions.retain(|_| {
        let delete = to_delete.contains(&current);
        current += 1;
        !delete
    });
}

fn remove_unused_assignments(
    block: &mut ir::Block,
    variables_in_block: &HashMap<LocalVariableIndex, bool>,
) {
    let mut can_remove = variables_in_block
        .iter()
        .filter_map(|(v, is_arg)| if *is_arg { None } else { Some((*v, *is_arg)) })
        .collect::<HashMap<_, _>>();
    for idx in (0..block.instructions.len()).rev() {
        // First, update `can_remove` with the read variables
        match &block.instructions[idx].instruction {
            ir::Ir::Assign(_, src)
            | ir::Ir::DerefAssign(_, src)
            | ir::Ir::UnaryOperator(_, _, src) => {
                if let Some(src) = src.local_index() {
                    can_remove.remove(&src);
                }
            }
            ir::Ir::BinaryOperator(_, _, lhs, rhs) => {
                if let Some(lhs) = lhs.local_index() {
                    can_remove.remove(&lhs);
                }
                if let Some(rhs) = rhs.local_index() {
                    can_remove.remove(&rhs);
                }
            }
            ir::Ir::Call(_, _, args) => {
                for src in args {
                    if let Some(src) = src.local_index() {
                        can_remove.remove(&src);
                    }
                }
            }
            _ => {}
        };

        // Next, try to remove the operation
        match &block.instructions[idx].instruction {
            ir::Ir::Assign(dst, _)
            | ir::Ir::UnaryOperator(_, dst, _)
            | ir::Ir::BinaryOperator(_, dst, _, _) => {
                if let Some(dst) = dst.local_index() {
                    if can_remove.contains_key(&dst) {
                        block.instructions.remove(idx); // Remove the assignment.
                    }
                }
            }
            _ => {}
        };
    }
}

fn remove_unused_variables(
    block: &mut ir::Block,
    variables_in_block: &HashMap<LocalVariableIndex, bool>,
) {
    let mut can_remove = variables_in_block
        .iter()
        .filter_map(|(v, is_arg)| if *is_arg { None } else { Some((*v, *is_arg)) })
        .collect::<HashMap<_, _>>();
    for idx in (0..block.instructions.len()).rev() {
        match &block.instructions[idx].instruction {
            ir::Ir::Assign(_, src) | ir::Ir::DerefAssign(_, src) => {
                if let Some(src) = src.local_index() {
                    can_remove.remove(&src);
                }
            }
            ir::Ir::UnaryOperator(_, dst, src) => {
                if let Some(src) = src.local_index() {
                    can_remove.remove(&src);
                }
                if let Some(dst) = dst.local_index() {
                    can_remove.remove(&dst);
                }
            }
            ir::Ir::BinaryOperator(_, dst, lhs, rhs) => {
                if let Some(lhs) = lhs.local_index() {
                    can_remove.remove(&lhs);
                }
                if let Some(rhs) = rhs.local_index() {
                    can_remove.remove(&rhs);
                }
                if let Some(dst) = dst.local_index() {
                    can_remove.remove(&dst);
                }
            }
            ir::Ir::Call(_, rtv, args) => {
                for src in args {
                    if let Some(src) = src.local_index() {
                        can_remove.remove(&src);
                    }
                }
                if let Some(rtv) = rtv.local_index() {
                    can_remove.remove(&rtv);
                }
            }
            _ => {}
        };
    }
    for idx in (0..block.instructions.len()).rev() {
        match &block.instructions[idx].instruction {
            ir::Ir::Declare(v, _) => {
                if can_remove.contains_key(&v.index) {
                    block.instructions.remove(idx); // Remove the assignment.
                }
            }
            ir::Ir::FreeVariable(v) => {
                if can_remove.contains_key(v) {
                    block.instructions.remove(idx); // Remove the assignment.
                }
            }
            _ => {}
        };
    }
}