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
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
#![allow(clippy::mutable_key_type)]
use std::collections::{BTreeMap, VecDeque};

use midenc_hir::{
    self as hir,
    adt::{SmallMap, SmallSet},
    pass::{AnalysisManager, RewritePass, RewriteResult},
    *,
};
use midenc_hir_analysis::{
    spill::Placement, ControlFlowGraph, DominanceFrontier, DominatorTree, SpillAnalysis, Use, User,
};
use midenc_session::{diagnostics::IntoDiagnostic, Emit, Session};
use rustc_hash::FxHashSet;

/// This pass places spills of SSA values to temporaries to cap the depth of the operand stack.
///
/// Internally it handles orchestrating the [InsertSpills]  and [RewriteSpills] passes, and should
/// be preferred over using those two passes directly. See their respective documentation to better
/// understand what this pass does as a whole.
///
/// In addition to running the two passes, and maintaining the [AnalysisManager] state between them,
/// this pass also handles applying an additional run of [crate::InlineBlocks] if spills were
/// introduced, so as to ensure that the output of the spills transformation is cleaned up. As
/// applying a pass conditionally like that is a bit tricky, we handle that here to ensure that is
/// a detail downstream users do not have to deal with.
#[derive(Default, PassInfo, ModuleRewritePassAdapter)]
pub struct ApplySpills;
impl RewritePass for ApplySpills {
    type Entity = hir::Function;

    fn apply(
        &mut self,
        function: &mut Self::Entity,
        analyses: &mut AnalysisManager,
        session: &Session,
    ) -> RewriteResult {
        use midenc_hir::pass::{RewriteFn, RewriteSet};

        let mut rewrites = RewriteSet::pair(InsertSpills, RewriteSpills);

        // If the spills transformation is run, we want to run the block inliner again to
        // clean up the output, but _only_ if there were actually spills, otherwise running
        // the inliner again will have no effect. To avoid that case, we wrap the second run
        // in a closure which will only apply the pass if there were spills
        let maybe_rerun_block_inliner: Box<RewriteFn<hir::Function>> = Box::new(
            |function: &mut hir::Function,
             analyses: &mut AnalysisManager,
             session: &Session|
             -> RewriteResult {
                let has_spills = analyses
                    .get::<SpillAnalysis>(&function.id)
                    .map(|spills| spills.has_spills())
                    .unwrap_or(false);
                if has_spills {
                    let mut inliner = crate::InlineBlocks;
                    inliner.apply(function, analyses, session)
                } else {
                    Ok(())
                }
            },
        );
        rewrites.push(maybe_rerun_block_inliner);

        // Apply the above collectively
        rewrites.apply(function, analyses, session)?;

        session.print(&function, Self::FLAG).into_diagnostic()?;
        if session.should_print_cfg(Self::FLAG) {
            use std::io::Write;
            let cfg = function.cfg_printer();
            let mut stdout = std::io::stdout().lock();
            write!(&mut stdout, "{cfg}").into_diagnostic()?;
        }
        Ok(())
    }
}

/// This pass inserts spills and reloads as computed by running a [SpillAnalysis] on the given
/// function, recording materialized splits, spills, and reloads in the analysis results.
///
/// **IMPORTANT:** This pass is intended to be combined with the [RewriteSpills] pass when used
/// as part of a compilation pipeline - it performs the first phase of a two-phase transformation,
/// and compilation _will_ fail if you forget to apply [RewriteSpills] after this pass when the
/// [SpillAnalysis] directed spills to be injected.
///
/// ## Design
///
/// The full spills transformation is split into an analysis and two rewrite passes, corresponding
/// to the three phases of the transformation:
///
/// 1. Analyze the function to determine if and when to spill values, which values to spill, and
///    where to place reloads.
/// 2. Insert the computed spills and reloads, temporarily breaking the SSA form of the program
/// 3. Reconstruct SSA form, by rewriting uses of spilled values to use the nearest dominating
///    definition, inserting block parameters as needed to ensure that all uses are strictly
///    dominated by the corresponding definitions.
///
/// Additionally, splitting it up this way makes each phase independently testable and verifiable,
/// essential due to the complexity of the overall transformation.
///
/// This pass corresponds to Phase 2 above, application of computed spills and reloads. It is very
/// simple, and can be seen as essentially materializing the analysis results in the IR. In addition
/// to setting the stage for Phase 3, this pass can also be used to validate that the scheduling of
/// spills and reloads is correct, matching the order in which we expect those operations to occur.
///
/// ## Notes About The Validity of Emitted IR
///
/// It is implied in my earlier notice, but I want to make it explicit here - this pass may produce
/// IR that is semantically invalid. Such IR is technically valid, and self-consistent, but cannot
/// be compiled to Miden Assembly. First, the spill and reload pseudo-instructions are expected to
/// only ever exist in the IR during application of the [InsertSpills] and  [RewriteSpills] passes;
/// later passes do not know how to handle them, and may panic if encountered, particularly the code
/// generation pass, which will raise an error on any unhandled instructions. Second, the semantics
/// of spills and reloads dictates that when a spill occurs, the live range of the spilled value is
/// terminated; and may only be resurrected by an explicit reload of that value. However, because
/// the new definition produced by a reload instruction is not actually used in the IR until after
/// the [RewriteSpills] pass is applied, the IR immediately after the [InsertSpills] pass is
/// semantically invalid - values will be dropped from the operand stack by a spill, yet there will
/// be code later in the same function which expects them to still be live (and thus on the operand
/// stack), which will fail to compile.
///
/// **TL;DR:** Unless testing or debugging, always apply [InsertSpills] and [RewriteSpills]
/// consecutively!
#[derive(Default)]
pub struct InsertSpills;
impl RewritePass for InsertSpills {
    type Entity = hir::Function;

    fn apply(
        &mut self,
        function: &mut Self::Entity,
        analyses: &mut AnalysisManager,
        session: &Session,
    ) -> RewriteResult {
        let mut spills = {
            // Compute the spills
            let spills = analyses.get_or_compute::<SpillAnalysis>(function, session)?;
            // If there are no spills to process, we're done
            if !spills.has_spills() {
                analyses.mark_all_preserved::<Function>(&function.id);
                return Ok(());
            }
            // Drop the reference to the analysis so that we can take ownership of it
            drop(spills);
            analyses.take::<SpillAnalysis>(&function.id).unwrap()
        };

        // Apply all splits
        for split_info in spills.splits_mut() {
            let mut builder = FunctionBuilder::at(
                function,
                InsertionPoint::after(split_info.predecessor.block.into()),
            );

            // Create the split
            let split = builder.create_block();
            builder.switch_to_block(split);

            // Record the block we created for this split
            split_info.split = Some(split);

            // Rewrite the terminator in the predecessor so that it transfers control to the
            // original successor via `split`, moving any block arguments into the
            // unconditional branch that terminates `split`.
            let span = builder.func.dfg.inst_span(split_info.predecessor.inst);
            let ix = builder.func.dfg.inst_mut(split_info.predecessor.inst);
            let args = match ix {
                Instruction::Br(Br {
                    ref mut successor, ..
                }) => {
                    assert_eq!(successor.destination, split_info.block);
                    successor.destination = split;
                    successor.args.take()
                }
                Instruction::CondBr(CondBr {
                    ref mut then_dest,
                    ref mut else_dest,
                    ..
                }) => {
                    if then_dest.destination == split_info.block {
                        then_dest.destination = split;
                        then_dest.args.take()
                    } else {
                        assert_eq!(else_dest.destination, split_info.block);
                        else_dest.destination = split;
                        else_dest.args.take()
                    }
                }
                Instruction::Switch(_) => {
                    panic!("expected switch instructions to have been rewritten prior to this pass")
                }
                ix => unimplemented!("unhandled branch instruction: {}", ix.opcode()),
            };
            builder.ins().Br(Opcode::Br, Type::Unit, split_info.block, args, span);
        }

        // Insert all spills
        for spill_info in spills.spills.iter_mut() {
            let ip = match spill_info.place {
                Placement::Split(split) => {
                    let split_block = spills.splits[split.as_u32() as usize]
                        .split
                        .expect("expected split to have been materialized");
                    let terminator = function.dfg.last_inst(split_block).unwrap();
                    InsertionPoint::before(terminator.into())
                }
                Placement::At(ip) => ip,
            };
            let mut builder = FunctionBuilder::at(function, ip);
            let mut args = ValueList::default();
            args.push(spill_info.value, &mut builder.func.dfg.value_lists);
            let inst = builder.ins().PrimOp(Opcode::Spill, Type::Unit, args, spill_info.span).0;
            spill_info.inst = Some(inst);
        }

        // Insert all reloads
        for reload in spills.reloads.iter_mut() {
            let ip = match reload.place {
                Placement::Split(split) => {
                    let split_block = spills.splits[split.as_u32() as usize]
                        .split
                        .expect("expected split to have been materialized");
                    let terminator = function.dfg.last_inst(split_block).unwrap();
                    InsertionPoint::before(terminator.into())
                }
                Placement::At(ip) => ip,
            };

            let ty = function.dfg.value_type(reload.value).clone();
            let mut builder = FunctionBuilder::at(function, ip);
            let mut args = ValueList::default();
            args.push(reload.value, &mut builder.func.dfg.value_lists);
            let inst = builder.ins().PrimOp(Opcode::Reload, ty, args, reload.span).0;
            reload.inst = Some(inst);
        }

        // Save the updated analysis results, and mark it preserved for later passes
        analyses.insert(function.id, spills);
        analyses.mark_preserved::<SpillAnalysis>(&function.id);

        if session.options.print_ir_after_all {
            function.write_to_stdout(session).into_diagnostic()?;
        }

        Ok(())
    }
}

/// This pass rewrites a function annotated by the [InsertSpills] pass, by means of the spill
/// and reload pseudo-instructions, such that the resulting function is semantically equivalent
/// to the original function, but with the additional property that the function will keep the
/// operand stack depth <= 16 at all times.
///
/// This rewrite consists of the following main objectives:
///
/// * Match all uses of spilled values with the nearest dominating definition, modifying the IR as
///   required to ensure that all uses are strictly dominated by their definitions.
/// * Allocate sufficient procedure locals to store concurrently-active spills
/// * Rewrite all `spill` instructions to primitive `local.store` instructions
/// * Rewrite used `reload` instructions to primitive `local.load` instructions
/// * Remove unused `reload` instructions as dead code
///
/// **NOTE:** This pass is intended to be combined with the [InsertSpills] pass. If run on its own,
/// it is effectively a no-op, so it is safe to do, but nonsensical. In a normal compilation
/// pipeline, this pass is run immediately after [InsertSpills]. It is _not_ safe to run other
/// passes between [InsertSpills] and [RewriteSpills], unless that pass specifically is designed to
/// preserve the results of the [SpillAnalysis] computed and used by [InsertSpills] to place spills
/// and reloads. Conversely, you can't just run [InsertSpills] without this pass, or the resulting
/// IR will fail to codegen.
///
/// ## Design
///
/// See [SpillAnalysis] and [InsertSpills] for more context and details.
///
/// The primary purpose of this pass is twofold: reconstruct SSA form after insertion of spills and
/// reloads by [InsertSpills], and lowering of the spill and reload pseudo-instructions to primitive
/// stores and loads from procedure-local variables. It is the final, and most important phase of
/// the spills transformation.
///
/// Unlike [InsertSpills], which mainly just materializes the results of the [SpillAnalysis], this
/// pass must do a tricky combo of dataflow analysis and rewrite in a single postorder traversal of
/// the CFG (i.e. bottom-up):
///
/// * We need to find uses of spilled values as we encounter them, and keep track of them until
/// we find an appropriate definition for each use.
/// * We need to propagate uses up the dominance tree until all uses are matched with definitions
/// * We need to rewrite uses when we find a definition
/// * We need to identify whether a block we are about to leave (on our way up the CFG), is in
/// the iterated dominance frontier for the set of spilled values we've found uses for. If it is,
/// we must append a new block parameter, rewrite the terminator of any predecessor blocks, and
/// rewrite all uses found so far by using the new block parameter as the dominating definition.
///
/// Technically, this pass could be generalized a step further, such that it fixes up invalid
/// def-use relationships in general, rather than just the narrow case of spills/reloads - but it is
/// more efficient to keep it specialized for now, we can always generalize later.
///
/// This pass guarantees that:
///
/// 1. No `spill` or `reload` instructions remain in the IR
/// 2. The semantics of the original IR on which [InsertSpills] was run, will be preserved, if:
///   * The original IR was valid
///   * No modification to the IR was made between [InsertSpills] and [RewriteSpills]
/// 3. The resulting function, once compiled to Miden Assembly, will keep the operand stack depth <=
///    16 elements, so long as the schedule produced by the backend preserves the scheduling
///    semantics. For example, spills/reloads are computed based on an implied scheduling of
///    operations, given by following the control flow graph, and visiting instructions in a block
///    top-down. If the backend reschedules operations for more optimal placement of operands on the
///    operand stack, it is possible that this rescheduling could result in the operand stack depth
///    exceeding 16 elements. However, at this point, it is not expected that this will be a
///    practical issue, even if it does occur, since the introduction of spills and reloads, not
///    only place greater constraints on backend scheduling, but also ensure that more live ranges
///    are split, and thus operands will spend less time on the operand stack overall. Time will
///    tell whether this holds true or not.
#[derive(Default)]
pub struct RewriteSpills;
impl RewritePass for RewriteSpills {
    type Entity = hir::Function;

    fn apply(
        &mut self,
        function: &mut Self::Entity,
        analyses: &mut AnalysisManager,
        session: &Session,
    ) -> RewriteResult {
        // At this point, we've potentially emitted spills/reloads, but these are not yet being
        // used to split the live ranges of the SSA values to which they apply. Our job now, is
        // to walk the CFG bottom-up, finding uses of values for which we have issued reloads,
        // and then looking for the dominating definition (either original, or reload) that controls
        // that use, rewriting the use with the SSA value corresponding to the reloaded value.
        //
        // This has the effect of "reconstructing" the SSA form - although in our case it is more
        // precise to say that we are fixing up the original program to reflect the live-range
        // splits that we have computed (and inserted pseudo-instructions for). In the original
        // paper, they actually had multiple definitions of reloaded SSA values, which is why
        // this phase is referred to as "reconstructing", as it is intended to recover the SSA
        // property that was lost once multiple definitions are introduced.
        //
        //   * For each original definition of a spilled value `v`, get the new definitions of `v`
        //     (reloads) and the uses of `v`.
        //   * For each use of `v`, walk the dominance tree upwards until a definition of `v` is
        //     found that is responsible for that use. If an iterated dominance frontier is passed,
        //     a block argument is inserted such that appropriate definitions from each predecessor
        //     are wired up to that block argument, which is then the definition of `v` responsible
        //     for subsequent uses. The predecessor instructions which branch to it are new uses
        //     which we visit in the same manner as described above. After visiting all uses, any
        //     definitions (reloads) which are dead will have no uses of the reloaded value, and can
        //     thus be eliminated.

        // We consume the spill analysis in this pass, as it will no longer be valid after this
        let spills = match analyses.get::<SpillAnalysis>(&function.id) {
            Some(spills) if spills.has_spills() => spills,
            _ => {
                analyses.mark_all_preserved::<Function>(&function.id);
                return Ok(());
            }
        };
        let cfg = analyses.get_or_compute::<ControlFlowGraph>(function, session).unwrap();
        let domtree = analyses.get_or_compute::<DominatorTree>(function, session).unwrap();
        let domf = DominanceFrontier::compute(&domtree, &cfg, function);

        // Make sure that any block in the iterated dominance frontier of a spilled value, has
        // a new phi (block argument) inserted, if one is not already present. These must be in
        // the CFG before we search for dominating definitions.
        let inserted_phis = insert_required_phis(&spills, function, &cfg, &domf);

        // Traverse the CFG bottom-up, doing the following along the way:
        //
        // 0. Merge the "used" sets of each successor of the current block (see remaining steps for
        //    how the "used" set is computed for a block). NOTE: We elaborate in step 4 on how to
        //    handle computing the "used" set for a successor, from the "used" set at the start of
        //    the successor block.
        // 1. If we encounter a use of a spilled value, record the location of that use in the set
        // of uses we're seeking a dominating definition for, i.e. the "used" set
        // 2. If we reach a definition for a value with uses in the "used" set:
        //   * If the definition is the original definition of the value, no action is needed, so we
        //     remove all uses of that value from the "used" set.
        //   * If the definition is a reload, rewrite all of the uses in the "used" set to use the
        //     reload instead, removing them from the "used" set. Mark the reload used.
        // 3. When we reach the start of the block, the state of the "used" set is associated with
        //    the current block. This will be used as the starting state of the "used" set in each
        //    predecessor of the block
        // 4. When computing the "used" set in the predecessor (i.e. step 0), we also check whether
        //    a given successor is in the iterated dominance frontier for any values in the "used"
        //    set of that successor. If so, we need to insert a block parameter for each such value,
        //    rewrite all uses of that value to use the new block parameter, and add the "used"
        //    value as an additional argument to that successor. The resulting "used" set will thus
        //    retain a single entry for each of the values for which uses were rewritten
        //    (corresponding to the block arguments for the successor), but all of the uses
        //    dominated by the introduced block parameter are no longer in the set, as their
        //    dominating definition has been found. Any values in the "used" set for which the
        //    successor is not in the iterated dominance frontier for that value, are retained in
        //    the "used" set without any changes.
        let mut used_sets = BTreeMap::<Block, BTreeMap<Value, FxHashSet<User>>>::default();
        let mut block_q = VecDeque::from_iter(domtree.cfg_postorder().iter().copied());
        while let Some(block_id) = block_q.pop_front() {
            // Compute the initial "used" set for this block
            let mut used = BTreeMap::<Value, FxHashSet<User>>::default();
            for succ in cfg.succ_iter(block_id) {
                if let Some(succ_used) = used_sets.get_mut(&succ) {
                    // Union the used set from this successor with the others
                    for (value, users) in succ_used.iter() {
                        used.entry(*value).or_default().extend(users.iter().cloned());
                    }
                }
            }

            // Traverse the block bottom-up, recording uses of spilled values while looking for
            // definitions
            let mut insts = function.dfg.block(block_id).insts().collect::<Vec<_>>();
            while let Some(current_inst) = insts.pop() {
                find_inst_uses(current_inst, &mut used, function, &spills);
            }

            // At the top of the block, if any of the block parameters are in the "used" set, remove
            // those uses, as the block parameters are the original definitions for
            // those values, and thus no rewrite is needed.
            for arg in function.dfg.block_args(block_id) {
                used.remove(arg);
            }

            rewrite_inserted_phi_uses(&inserted_phis, block_id, &mut used, function);

            // What remains are the unsatisfied uses of spilled values for this block and its
            // successors
            used_sets.insert(block_id, used);
        }

        rewrite_spill_pseudo_instructions(function, &domtree, &spills);

        Ok(())
    }
}

// Insert additional phi nodes as follows:
//
// 1. For each spilled value V
// 2. Obtain the set of blocks, R, containing a reload of V
// 3. For each block B in the iterated dominance frontier of R, insert a phi in B for V
// 4. For every predecessor of B, append a new block argument to B, passing V initially
// 5. Traverse the CFG bottom-up, finding uses of V, until we reach an inserted phi, a reload, or
//    the original definition. Rewrite all found uses of V up to that point, to use this definition.
fn insert_required_phis(
    spills: &SpillAnalysis,
    function: &mut hir::Function,
    cfg: &ControlFlowGraph,
    domf: &DominanceFrontier,
) -> BTreeMap<Block, SmallMap<Value, Value, 2>> {
    let mut required_phis = BTreeMap::<Value, SmallSet<Block, 2>>::default();
    for info in spills.reloads() {
        let r_block = function.dfg.inst_block(info.inst.unwrap()).unwrap();
        let r = required_phis.entry(info.value).or_default();
        r.insert(r_block);
    }

    let mut inserted_phis = BTreeMap::<Block, SmallMap<Value, Value, 2>>::default();
    for (value, domf_r) in required_phis {
        // Compute the iterated dominance frontier, DF+(R)
        let idf_r = domf.iterate_all(domf_r);
        // Add phi to each B in DF+(R)
        let data = function.dfg.value_data(value);
        let ty = data.ty().clone();
        let span = data.span();
        for b in idf_r {
            // Allocate new block parameter/phi, if one is not already present
            let phis = inserted_phis.entry(b).or_default();
            if let adt::smallmap::Entry::Vacant(entry) = phis.entry(value) {
                let phi = function.dfg.append_block_param(b, ty.clone(), span);
                entry.insert(phi);

                // Append `value` as new argument to every predecessor to satisfy new parameter
                for pred in cfg.pred_iter(b) {
                    function.dfg.append_branch_destination_argument(pred.inst, b, value);
                }
            }
        }
    }

    inserted_phis
}

fn find_inst_uses(
    current_inst: Inst,
    used: &mut BTreeMap<Value, FxHashSet<User>>,
    function: &mut hir::Function,
    spills: &SpillAnalysis,
) {
    // If `current_inst` is a branch or terminator, it cannot define a value, so
    // we simply record any uses, and move on.
    match function.dfg.analyze_branch(current_inst) {
        BranchInfo::SingleDest(SuccessorInfo { args, .. }) => {
            for (index, arg) in args.iter().enumerate() {
                if spills.is_spilled(arg) {
                    used.entry(*arg).or_default().insert(User::new(
                        current_inst,
                        *arg,
                        Use::BlockArgument {
                            succ: 0,
                            index: index as u16,
                        },
                    ));
                }
            }
        }
        BranchInfo::MultiDest(infos) => {
            for (succ_index, info) in infos.into_iter().enumerate() {
                for (index, arg) in info.args.iter().enumerate() {
                    if spills.is_spilled(arg) {
                        used.entry(*arg).or_default().insert(User::new(
                            current_inst,
                            *arg,
                            Use::BlockArgument {
                                succ: succ_index as u16,
                                index: index as u16,
                            },
                        ));
                    }
                }
            }
        }
        BranchInfo::NotABranch => {
            // Does this instruction provide a definition for any spilled values?
            let ix = function.dfg.inst(current_inst);

            let is_reload = matches!(ix.opcode(), Opcode::Reload);
            if is_reload {
                // We have found a new definition for a spilled value, we must rewrite
                // all uses of the spilled value found so
                // far, with the reloaded value
                let spilled = ix.arguments(&function.dfg.value_lists)[0];
                let reloaded = function.dfg.first_result(current_inst);

                if let Some(to_rewrite) = used.remove(&spilled) {
                    debug_assert!(!to_rewrite.is_empty(), "expected empty use sets to be removed");

                    for user in to_rewrite.iter() {
                        match user.ty {
                            Use::BlockArgument {
                                succ: succ_succ,
                                index,
                            } => {
                                function.dfg.replace_successor_argument(
                                    user.inst,
                                    succ_succ as usize,
                                    index as usize,
                                    reloaded,
                                );
                            }
                            Use::Operand { index } => {
                                function.dfg.replace_argument(user.inst, index as usize, reloaded);
                            }
                        }
                    }
                } else {
                    // This reload is unused, so remove it entirely, and move to the
                    // next instruction
                    return;
                }
            }

            for spilled in function
                .dfg
                .inst_results(current_inst)
                .iter()
                .filter(|result| spills.is_spilled(result))
            {
                // This op is the original definition for `spilled`, so remove any uses
                // of it we've accumulated so far, as they
                // do not need to be rewritten
                used.remove(spilled);
            }
        }
    }

    // Record any uses of spilled values in the argument list for `current_inst` (except
    // reloads)
    let ignored = matches!(function.dfg.inst(current_inst).opcode(), Opcode::Reload);
    if !ignored {
        for (index, arg) in function.dfg.inst_args(current_inst).iter().enumerate() {
            if spills.is_spilled(arg) {
                used.entry(*arg).or_default().insert(User::new(
                    current_inst,
                    *arg,
                    Use::Operand {
                        index: index as u16,
                    },
                ));
            }
        }
    }
}

fn rewrite_inserted_phi_uses(
    inserted_phis: &BTreeMap<Block, SmallMap<Value, Value, 2>>,
    block_id: Block,
    used: &mut BTreeMap<Value, FxHashSet<User>>,
    function: &mut hir::Function,
) {
    // If we have inserted any phis in this block, rewrite uses of the spilled values they
    // represent.
    if let Some(phis) = inserted_phis.get(&block_id) {
        for (spilled, phi) in phis.iter() {
            if let Some(to_rewrite) = used.remove(spilled) {
                debug_assert!(!to_rewrite.is_empty(), "expected empty use sets to be removed");

                for user in to_rewrite.iter() {
                    match user.ty {
                        Use::BlockArgument {
                            succ: succ_succ,
                            index,
                        } => {
                            function.dfg.replace_successor_argument(
                                user.inst,
                                succ_succ as usize,
                                index as usize,
                                *phi,
                            );
                        }
                        Use::Operand { index } => {
                            function.dfg.replace_argument(user.inst, index as usize, *phi);
                        }
                    }
                }
            } else {
                // TODO(pauls): This phi is unused, we should be able to remove it
                continue;
            }
        }
    }
}

/// For each spilled value, allocate a procedure local, rewrite the spill instruction as a
/// `local.store`, unless the spill is dead, in which case we remove the spill entirely.
///
/// Dead spills can occur because the spills analysis must conservatively place them to
/// ensure that all paths to a block where a value has been spilled along at least one
/// of those paths, gets spilled on all of them, by inserting extra spills along those
/// edges where a spill hasn't occurred yet.
///
/// However, this produces dead spills on some paths through the function, which are not
/// needed once rewrites have been performed. So we eliminate dead spills by identifying
/// those spills which do not dominate any reloads - if a store to a spill slot can never
/// be read, then the store can be elided.
fn rewrite_spill_pseudo_instructions(
    function: &mut hir::Function,
    domtree: &DominatorTree,
    spills: &SpillAnalysis,
) {
    let mut locals = BTreeMap::<Value, LocalId>::default();
    for spill_info in spills.spills() {
        let spill = spill_info.inst.expect("expected spill to have been materialized");
        let spilled = spill_info.value;
        let stored = function.dfg.inst_args(spill)[0];
        let is_used = spills.reloads().iter().any(|info| {
            if info.value == spilled {
                let reload = info.inst.unwrap();
                domtree.dominates(spill, reload, &function.dfg)
            } else {
                false
            }
        });
        if is_used {
            let local = *locals
                .entry(spilled)
                .or_insert_with(|| function.dfg.alloc_local(spill_info.ty.clone()));
            let builder = ReplaceBuilder::new(&mut function.dfg, spill);
            builder.store_local(local, stored, spill_info.span);
        } else {
            let spill_block = function.dfg.inst_block(spill).unwrap();
            let block = function.dfg.block_mut(spill_block);
            block.cursor_mut_at_inst(spill).remove();
        }
    }

    // Rewrite all used reload instructions as `local.load` instructions from the corresponding
    // procedure local
    for reload_info in spills.reloads() {
        let inst = reload_info.inst.expect("expected reload to have been materialized");
        let spilled = function.dfg.inst_args(inst)[0];
        let builder = ReplaceBuilder::new(&mut function.dfg, inst);
        builder.load_local(locals[&spilled], reload_info.span);
    }
}

#[cfg(test)]
mod tests {
    use midenc_hir::testing::TestContext;
    use pretty_assertions::{assert_ne, assert_str_eq};

    use super::*;

    #[test]
    fn spills_intra_block() {
        let context = TestContext::default();
        let id = "test::spill".parse().unwrap();
        let mut function = Function::new(
            id,
            Signature::new(
                [AbiParam::new(Type::Ptr(Box::new(Type::U8)))],
                [AbiParam::new(Type::U32)],
            ),
        );

        {
            let mut builder = FunctionBuilder::new(&mut function);
            let example = builder
                .import_function(
                    "foo",
                    "example",
                    Signature::new(
                        [
                            AbiParam::new(Type::Ptr(Box::new(Type::U128))),
                            AbiParam::new(Type::U128),
                            AbiParam::new(Type::U128),
                            AbiParam::new(Type::U128),
                            AbiParam::new(Type::U64),
                        ],
                        [AbiParam::new(Type::U32)],
                    ),
                    SourceSpan::UNKNOWN,
                )
                .unwrap();
            let entry = builder.current_block();
            let v0 = {
                let args = builder.block_params(entry);
                args[0]
            };

            // entry
            let v1 = builder.ins().ptrtoint(v0, Type::U32, SourceSpan::UNKNOWN);
            let v2 = builder.ins().add_imm_unchecked(v1, Immediate::U32(32), SourceSpan::UNKNOWN);
            let v3 =
                builder.ins().inttoptr(v2, Type::Ptr(Box::new(Type::U128)), SourceSpan::UNKNOWN);
            let v4 = builder.ins().load(v3, SourceSpan::UNKNOWN);
            let v5 = builder.ins().add_imm_unchecked(v1, Immediate::U32(64), SourceSpan::UNKNOWN);
            let v6 =
                builder.ins().inttoptr(v5, Type::Ptr(Box::new(Type::U128)), SourceSpan::UNKNOWN);
            let v7 = builder.ins().load(v6, SourceSpan::UNKNOWN);
            let v8 = builder.ins().u64(1, SourceSpan::UNKNOWN);
            builder.ins().call(example, &[v6, v4, v7, v7, v8], SourceSpan::UNKNOWN);
            let v10 = builder.ins().add_imm_unchecked(v1, Immediate::U32(72), SourceSpan::UNKNOWN);
            builder.ins().store(v3, v7, SourceSpan::UNKNOWN);
            let v11 =
                builder.ins().inttoptr(v10, Type::Ptr(Box::new(Type::U64)), SourceSpan::UNKNOWN);
            let _v12 = builder.ins().load(v11, SourceSpan::UNKNOWN);
            builder.ins().ret(Some(v2), SourceSpan::UNKNOWN);
        }

        let original = function.to_string();
        let mut analyses = AnalysisManager::default();
        let mut rewrite = InsertSpills;
        rewrite
            .apply(&mut function, &mut analyses, &context.session)
            .expect("spill insertion failed");

        analyses.invalidate::<Function>(&function.id);

        let mut rewrite = RewriteSpills;
        rewrite
            .apply(&mut function, &mut analyses, &context.session)
            .expect("spill cleanup failed");

        let expected = "\
(func (export #spill) (param (ptr u8)) (result u32)
    (block 0 (param v0 (ptr u8))
        (let (v1 u32) (ptrtoint v0))
        (let (v2 u32) (add.unchecked v1 32))
        (let (v3 (ptr u128)) (inttoptr v2))
        (let (v4 u128) (load v3))
        (let (v5 u32) (add.unchecked v1 64))
        (let (v6 (ptr u128)) (inttoptr v5))
        (let (v7 u128) (load v6))
        (let (v8 u64) (const.u64 1))
        (store.local local0 v2)
        (store.local local1 v3)
        (let (v9 u32) (call (#foo #example) v6 v4 v7 v7 v8))
        (let (v10 u32) (add.unchecked v1 72))
        (let (v13 (ptr u128)) (load.local local1))
        (store v13 v7)
        (let (v11 (ptr u64)) (inttoptr v10))
        (let (v12 u64) (load v11))
        (let (v14 u32) (load.local local0))
        (ret v14))
)";

        let transformed = function.to_string();
        assert_ne!(transformed, original);
        assert_str_eq!(transformed.as_str(), expected);
    }

    #[test]
    fn spills_branching_control_flow() {
        let context = TestContext::default();
        let id = "test::spill".parse().unwrap();
        let mut function = Function::new(
            id,
            Signature::new(
                [AbiParam::new(Type::Ptr(Box::new(Type::U8)))],
                [AbiParam::new(Type::U32)],
            ),
        );

        {
            let mut builder = FunctionBuilder::new(&mut function);
            let example = builder
                .import_function(
                    "foo",
                    "example",
                    Signature::new(
                        [
                            AbiParam::new(Type::Ptr(Box::new(Type::U128))),
                            AbiParam::new(Type::U128),
                            AbiParam::new(Type::U128),
                            AbiParam::new(Type::U128),
                            AbiParam::new(Type::U64),
                        ],
                        [AbiParam::new(Type::U32)],
                    ),
                    SourceSpan::UNKNOWN,
                )
                .unwrap();
            let entry = builder.current_block();
            let block1 = builder.create_block();
            let block2 = builder.create_block();
            let block3 = builder.create_block();
            let v0 = {
                let args = builder.block_params(entry);
                args[0]
            };

            // entry
            let v1 = builder.ins().ptrtoint(v0, Type::U32, SourceSpan::UNKNOWN);
            let v2 = builder.ins().add_imm_unchecked(v1, Immediate::U32(32), SourceSpan::UNKNOWN);
            let v3 =
                builder.ins().inttoptr(v2, Type::Ptr(Box::new(Type::U128)), SourceSpan::UNKNOWN);
            let v4 = builder.ins().load(v3, SourceSpan::UNKNOWN);
            let v5 = builder.ins().add_imm_unchecked(v1, Immediate::U32(64), SourceSpan::UNKNOWN);
            let v6 =
                builder.ins().inttoptr(v5, Type::Ptr(Box::new(Type::U128)), SourceSpan::UNKNOWN);
            let v7 = builder.ins().load(v6, SourceSpan::UNKNOWN);
            let v8 = builder.ins().eq_imm(v1, Immediate::U32(0), SourceSpan::UNKNOWN);
            builder.ins().cond_br(v8, block1, &[], block2, &[], SourceSpan::UNKNOWN);

            // block1
            builder.switch_to_block(block1);
            let v9 = builder.ins().u64(1, SourceSpan::UNKNOWN);
            let call = builder.ins().call(example, &[v6, v4, v7, v7, v9], SourceSpan::UNKNOWN);
            let v10 = builder.func.dfg.first_result(call);
            builder.ins().br(block3, &[v10], SourceSpan::UNKNOWN);

            // block2
            builder.switch_to_block(block2);
            let v11 = builder.ins().add_imm_unchecked(v1, Immediate::U32(8), SourceSpan::UNKNOWN);
            builder.ins().br(block3, &[v11], SourceSpan::UNKNOWN);

            // block3
            let v12 = builder.append_block_param(block3, Type::U32, SourceSpan::UNKNOWN);
            builder.switch_to_block(block3);
            let v13 = builder.ins().add_imm_unchecked(v1, Immediate::U32(72), SourceSpan::UNKNOWN);
            let v14 = builder.ins().add_unchecked(v13, v12, SourceSpan::UNKNOWN);
            let v15 =
                builder.ins().inttoptr(v14, Type::Ptr(Box::new(Type::U64)), SourceSpan::UNKNOWN);
            builder.ins().store(v3, v7, SourceSpan::UNKNOWN);
            let _v16 = builder.ins().load(v15, SourceSpan::UNKNOWN);
            builder.ins().ret(Some(v2), SourceSpan::UNKNOWN);
        }

        let original = function.to_string();
        let mut analyses = AnalysisManager::default();
        let mut rewrite = InsertSpills;
        rewrite
            .apply(&mut function, &mut analyses, &context.session)
            .expect("spill insertion failed");

        analyses.invalidate::<Function>(&function.id);

        let mut rewrite = RewriteSpills;
        rewrite
            .apply(&mut function, &mut analyses, &context.session)
            .expect("spill cleanup failed");

        let expected = "\
(func (export #spill) (param (ptr u8)) (result u32)
    (block 0 (param v0 (ptr u8))
        (let (v1 u32) (ptrtoint v0))
        (let (v2 u32) (add.unchecked v1 32))
        (let (v3 (ptr u128)) (inttoptr v2))
        (let (v4 u128) (load v3))
        (let (v5 u32) (add.unchecked v1 64))
        (let (v6 (ptr u128)) (inttoptr v5))
        (let (v7 u128) (load v6))
        (let (v8 i1) (eq v1 0))
        (condbr v8 (block 1) (block 2)))

    (block 1
        (let (v9 u64) (const.u64 1))
        (store.local local0 v2)
        (store.local local1 v3)
        (let (v10 u32) (call (#foo #example) v6 v4 v7 v7 v9))
        (br (block 4)))

    (block 2
        (let (v11 u32) (add.unchecked v1 8))
        (br (block 5)))

    (block 3 (param v12 u32) (param v19 u32) (param v20 (ptr u128))
        (let (v13 u32) (add.unchecked v1 72))
        (let (v14 u32) (add.unchecked v13 v12))
        (let (v15 (ptr u64)) (inttoptr v14))
        (store v20 v7)
        (let (v16 u64) (load v15))
        (ret v19))

    (block 4
        (let (v17 (ptr u128)) (load.local local1))
        (let (v18 u32) (load.local local0))
        (br (block 3 v10 v18 v17)))

    (block 5
        (br (block 3 v11 v2 v3)))
)";

        let transformed = function.to_string();
        assert_ne!(transformed, original);
        assert_str_eq!(transformed.as_str(), expected);
    }

    #[test]
    fn spills_loop_nest() {
        let context = TestContext::default();
        let id = "test::spill".parse().unwrap();
        let mut function = Function::new(
            id,
            Signature::new(
                [
                    AbiParam::new(Type::Ptr(Box::new(Type::U64))),
                    AbiParam::new(Type::U64),
                    AbiParam::new(Type::U64),
                ],
                [AbiParam::new(Type::U64)],
            ),
        );

        {
            let mut builder = FunctionBuilder::new(&mut function);
            let entry = builder.current_block();
            let (v0, v1, v2) = {
                let args = builder.block_params(entry);
                (args[0], args[1], args[2])
            };

            let block1 = builder.create_block();
            let block2 = builder.create_block();
            let block3 = builder.create_block();
            let block4 = builder.create_block();
            let block5 = builder.create_block();
            let block6 = builder.create_block();

            // entry
            let v3 = builder.ins().u64(0, SourceSpan::UNKNOWN);
            let v4 = builder.ins().u64(0, SourceSpan::UNKNOWN);
            let v5 = builder.ins().u64(0, SourceSpan::UNKNOWN);
            builder.ins().br(block1, &[v3, v4, v5], SourceSpan::UNKNOWN);

            // block1 - outer loop header
            let v6 = builder.append_block_param(block1, Type::U64, SourceSpan::UNKNOWN);
            let v7 = builder.append_block_param(block1, Type::U64, SourceSpan::UNKNOWN);
            let v8 = builder.append_block_param(block1, Type::U64, SourceSpan::UNKNOWN);
            builder.switch_to_block(block1);
            let v9 = builder.ins().eq(v6, v1, SourceSpan::UNKNOWN);
            builder.ins().cond_br(v9, block2, &[], block3, &[], SourceSpan::UNKNOWN);

            // block2 - outer exit
            builder.switch_to_block(block2);
            builder.ins().ret(Some(v8), SourceSpan::UNKNOWN);

            // block3 - split edge
            builder.switch_to_block(block3);
            builder.ins().br(block4, &[v7, v8], SourceSpan::UNKNOWN);

            // block4 - inner loop
            let v10 = builder.append_block_param(block4, Type::U64, SourceSpan::UNKNOWN);
            let v11 = builder.append_block_param(block4, Type::U64, SourceSpan::UNKNOWN);
            builder.switch_to_block(block4);
            let v12 = builder.ins().eq(v10, v2, SourceSpan::UNKNOWN);
            builder.ins().cond_br(v12, block5, &[], block6, &[], SourceSpan::UNKNOWN);

            // block5 - inner latch
            builder.switch_to_block(block5);
            let v13 = builder.ins().add_imm_unchecked(v6, Immediate::U64(1), SourceSpan::UNKNOWN);
            builder.ins().br(block1, &[v13, v10, v11], SourceSpan::UNKNOWN);

            // block6 - inner body
            builder.switch_to_block(block6);
            let v14 = builder.ins().add_imm_unchecked(v6, Immediate::U64(1), SourceSpan::UNKNOWN);
            let v15 = builder.ins().mul_unchecked(v14, v2, SourceSpan::UNKNOWN);
            let v16 = builder.ins().add_unchecked(v10, v15, SourceSpan::UNKNOWN);
            let v17 = builder.ins().ptrtoint(v0, Type::U64, SourceSpan::UNKNOWN);
            let v18 = builder.ins().add_unchecked(v17, v16, SourceSpan::UNKNOWN);
            let v19 =
                builder.ins().inttoptr(v18, Type::Ptr(Box::new(Type::U64)), SourceSpan::UNKNOWN);
            let v20 = builder.ins().load(v19, SourceSpan::UNKNOWN);
            let v21 = builder.ins().add_unchecked(v11, v20, SourceSpan::UNKNOWN);
            let v22 = builder.ins().add_imm_unchecked(v10, Immediate::U64(1), SourceSpan::UNKNOWN);
            builder.ins().br(block4, &[v22, v21], SourceSpan::UNKNOWN);
        }

        let original = function.to_string();
        let mut analyses = AnalysisManager::default();
        let mut rewrite = InsertSpills;
        rewrite
            .apply(&mut function, &mut analyses, &context.session)
            .expect("spill insertion failed");

        analyses.invalidate::<Function>(&function.id);

        let mut rewrite = RewriteSpills;
        rewrite
            .apply(&mut function, &mut analyses, &context.session)
            .expect("spill cleanup failed");

        let expected = "\
(func (export #spill) (param (ptr u64)) (param u64) (param u64) (result u64)
    (block 0 (param v0 (ptr u64)) (param v1 u64) (param v2 u64)
        (let (v3 u64) (const.u64 0))
        (let (v4 u64) (const.u64 0))
        (let (v5 u64) (const.u64 0))
        (br (block 1 v3 v4 v5 v1)))

    (block 1 (param v6 u64) (param v7 u64) (param v8 u64) (param v24 u64)
        (let (v9 i1) (eq v6 v24))
        (condbr v9 (block 2) (block 3)))

    (block 2
        (ret v8))

    (block 3
        (br (block 7)))

    (block 4 (param v10 u64) (param v11 u64)
        (let (v12 i1) (eq v10 v2))
        (condbr v12 (block 5) (block 6)))

    (block 5
        (let (v13 u64) (add.unchecked v6 1))
        (br (block 8)))

    (block 6
        (let (v14 u64) (add.unchecked v6 1))
        (let (v15 u64) (mul.unchecked v14 v2))
        (let (v16 u64) (add.unchecked v10 v15))
        (let (v17 u64) (ptrtoint v0))
        (let (v18 u64) (add.unchecked v17 v16))
        (let (v19 (ptr u64)) (inttoptr v18))
        (let (v20 u64) (load v19))
        (let (v21 u64) (add.unchecked v11 v20))
        (let (v22 u64) (add.unchecked v10 1))
        (br (block 4 v22 v21)))

    (block 7
        (store.local local0 v24)
        (br (block 4 v7 v8)))

    (block 8
        (let (v23 u64) (load.local local0))
        (br (block 1 v13 v10 v11 v23)))
)";

        let transformed = function.to_string();
        assert_ne!(transformed, original);
        assert_str_eq!(transformed.as_str(), expected);
    }
}