aboutsummaryrefslogtreecommitdiff
path: root/backend/lib/CodeGen/MachineSSAExit.cpp
blob: 76963ea6344a1b2006c75a905c02c506304bdad3 (plain)
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
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
// SPDX-License-Identifier: GPL-3.0-or-later

#include "rcc/CodeGen/MachineSSAExit.h"

#include "rcc/CodeGen/GenericOpcodes.h"
#include "rcc/CodeGen/MachineBasicBlock.h"
#include "rcc/CodeGen/MachineCFGUpdater.h"
#include "rcc/CodeGen/MachineFunction.h"
#include "rcc/CodeGen/MachineFunctionCloner.h"
#include "rcc/CodeGen/MachineIRBuilder.h"
#include "rcc/CodeGen/MachineInstr.h"
#include "rcc/CodeGen/MachineLiveness.h"
#include "rcc/CodeGen/MachineOperand.h"
#include "rcc/CodeGen/MachineRegisterInfo.h"
#include "rcc/CodeGen/TargetDescription.h"

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <optional>
#include <ranges>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>

namespace rcc {
namespace {

struct EdgeKey final {
  const MachineBasicBlock *From = nullptr;
  const MachineBasicBlock *To = nullptr;

  friend bool operator==(const EdgeKey &, const EdgeKey &) noexcept = default;
};

struct EdgeKeyHash final {
  [[nodiscard]] std::size_t operator()(const EdgeKey &Key) const noexcept {
    const std::size_t FromHash = std::hash<const MachineBasicBlock *>{}(Key.From);
    const std::size_t ToHash = std::hash<const MachineBasicBlock *>{}(Key.To);
    return FromHash ^ (ToHash << 1U);
  }
};

struct RegisterValue final {
  Register Reg;
  SubRegIndex SubReg;
  RegisterOperandFlags Flags;
  std::uint32_t TargetFlags = 0;
};

struct PhiIncoming final {
  RegisterValue Value;
  MachineBasicBlock *Predecessor = nullptr;
  bool IsUndef = false;
};

struct PhiPlan final {
  MachineInstr *Instruction = nullptr;
  RegisterValue Destination;
  Register FreshIncoming;
  std::vector<PhiIncoming> IncomingValues;
};

[[nodiscard]] Error prefixedError(const Error &Source, std::string Prefix) {
  Prefix.append(Source.message());
  return Error(Source.code(), std::move(Prefix));
}

[[nodiscard]] Expected<void> requireProperties(const MachineFunction &Function,
                                               const MachinePassContract &Contract) {
  const MachineFunctionProperties Missing = Contract.Requires.missingFrom(Function.properties());
  if (Missing.empty())
    return {};
  return makeUnexpected(ErrorCode::VerificationError,
                        Contract.Name + " requires properties: " + Missing.toString());
}

[[nodiscard]] bool isPhi(const MachineInstr &Instruction, const TargetDescription &Target) {
  const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction.opcode());
  return Descriptor != nullptr && Descriptor->Flags.has(InstructionFlag::Phi);
}

[[nodiscard]] bool isMeta(const MachineInstr &Instruction, const TargetDescription &Target) {
  const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction.opcode());
  return Descriptor != nullptr && Descriptor->Flags.has(InstructionFlag::Meta);
}

[[nodiscard]] MachineInstr *firstTerminator(MachineBasicBlock &Block,
                                            const TargetDescription &Target) {
  for (MachineInstr *Instruction = Block.firstInstruction(); Instruction != nullptr;
       Instruction = Instruction->next()) {
    const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
    if (Descriptor != nullptr && Descriptor->Flags.has(InstructionFlag::Terminator))
      return Instruction;
  }
  return nullptr;
}

[[nodiscard]] MachineInstr *firstCodeAfterPhiRegion(MachineBasicBlock &Block,
                                                    const TargetDescription &Target) {
  for (MachineInstr *Instruction = Block.firstInstruction(); Instruction != nullptr;
       Instruction = Instruction->next()) {
    if (!isPhi(*Instruction, Target) && !isMeta(*Instruction, Target))
      return Instruction;
  }
  return nullptr;
}

[[nodiscard]] RegisterOperandFlags generatedDefFlags(RegisterOperandFlags Original) noexcept {
  RegisterOperandFlags Result;
  if (Original.has(RegisterOperandFlag::Undef))
    Result.set(RegisterOperandFlag::Undef);
  return Result;
}

[[nodiscard]] Expected<void> copyDebugLocation(MachineInstrBuilder &Builder,
                                               const MachineInstr *Source) {
  if (Source == nullptr || !Source->debugLocation())
    return {};
  return Builder.setDebugLocation(*Source->debugLocation());
}

[[nodiscard]] Expected<Register> cloneRegisterConstraints(MachineFunction &Function,
                                                          Register Source) {
  if (!Source.isVirtual()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "SSA deconstruction requires a virtual PHI destination");
  }
  auto SourceInfo = Function.registerInfo().virtualRegisterInfo(Source);
  if (!SourceInfo)
    return std::unexpected(SourceInfo.error());
  const VirtualRegisterInfo Info = SourceInfo->get();

  auto Fresh = Function.registerInfo().createVirtualRegister();
  if (!Fresh)
    return std::unexpected(Fresh.error());
  if (Info.Type) {
    auto SetType = Function.registerInfo().setType(*Fresh, *Info.Type);
    if (!SetType)
      return std::unexpected(SetType.error());
  }
  if (Info.Bank) {
    auto SetBank = Function.registerInfo().setRegisterBank(*Fresh, *Info.Bank);
    if (!SetBank)
      return std::unexpected(SetBank.error());
  }
  if (Info.RegClass) {
    auto SetClass = Function.registerInfo().setRegisterClass(*Fresh, *Info.RegClass);
    if (!SetClass)
      return std::unexpected(SetClass.error());
  }
  for (const PhysicalRegister Hint : Info.AllocationHints) {
    auto AddedHint = Function.registerInfo().addAllocationHint(*Fresh, Hint);
    if (!AddedHint)
      return std::unexpected(AddedHint.error());
  }
  if (Info.TargetFlags != 0) {
    auto SetFlags = Function.registerInfo().setTargetRegisterFlags(*Fresh, Info.TargetFlags);
    if (!SetFlags)
      return std::unexpected(SetFlags.error());
  }
  return *Fresh;
}

[[nodiscard]] Expected<MachineInstr *>
buildCopy(MachineFunction &Function, const TargetDescription &Target,
          const TargetSSAInfo &TargetInfo, MachineBasicBlock &Block, MachineInstr *Before,
          RegisterValue Destination, RegisterValue Source, const MachineInstr *DebugSource) {
  auto Legality = TargetInfo.validateCopy(Destination.Reg, Destination.SubReg, Source.Reg,
                                          Source.SubReg, Function, Target);
  if (!Legality)
    return std::unexpected(prefixedError(Legality.error(), "illegal COPY: "));

  MachineIRBuilder Builder(Function, Target);
  auto Point =
      Before == nullptr ? Builder.setInsertPoint(Block) : Builder.setInsertPointBefore(*Before);
  if (!Point)
    return std::unexpected(Point.error());
  auto Pending = Builder.buildInstr(opcode(GenericOpcode::COPY));
  if (!Pending)
    return std::unexpected(Pending.error());
  auto Def = Pending->addDef(Destination.Reg, generatedDefFlags(Destination.Flags),
                             Destination.SubReg, Destination.TargetFlags);
  if (!Def)
    return std::unexpected(Def.error());
  auto Use = Pending->addUse(Source.Reg, {}, Source.SubReg, Source.TargetFlags);
  if (!Use)
    return std::unexpected(Use.error());
  auto Debug = copyDebugLocation(*Pending, DebugSource);
  if (!Debug)
    return std::unexpected(Debug.error());
  return Pending->build();
}

[[nodiscard]] Expected<MachineInstr *>
buildImplicitDef(MachineFunction &Function, const TargetDescription &Target,
                 MachineBasicBlock &Block, MachineInstr *Before, RegisterValue Destination,
                 const MachineInstr *DebugSource) {
  MachineIRBuilder Builder(Function, Target);
  auto Point =
      Before == nullptr ? Builder.setInsertPoint(Block) : Builder.setInsertPointBefore(*Before);
  if (!Point)
    return std::unexpected(Point.error());
  auto Pending = Builder.buildInstr(opcode(GenericOpcode::IMPLICIT_DEF));
  if (!Pending)
    return std::unexpected(Pending.error());
  auto Def = Pending->addDef(Destination.Reg, generatedDefFlags(Destination.Flags),
                             Destination.SubReg, Destination.TargetFlags);
  if (!Def)
    return std::unexpected(Def.error());
  auto Debug = copyDebugLocation(*Pending, DebugSource);
  if (!Debug)
    return std::unexpected(Debug.error());
  return Pending->build();
}

[[nodiscard]] bool isImplicitlyUndef(const MachineFunction &Function,
                                     const MachineOperand &Operand) {
  if (Operand.registerFlags().has(RegisterOperandFlag::Undef))
    return true;
  const Register *Reg = Operand.getIf<Register>();
  if (Reg == nullptr || Reg->isNoRegister())
    return true;
  if (!Reg->isVirtual())
    return false;
  const MachineOperand *Definition = Function.registerInfo().getUniqueVRegDef(*Reg);
  if (Definition == nullptr || Definition->parent() == nullptr)
    return false;
  if (Definition->subReg().value() != 0 && Definition->subReg() != Operand.subReg())
    return false;
  const Opcode DefinitionOpcode = Definition->parent()->opcode();
  return DefinitionOpcode == opcode(GenericOpcode::IMPLICIT_DEF) ||
         DefinitionOpcode == opcode(GenericOpcode::G_IMPLICIT_DEF) ||
         Definition->registerFlags().has(RegisterOperandFlag::Undef);
}

[[nodiscard]] Expected<void> validatePhiValue(const MachineFunction &Function,
                                              const TargetDescription &Target,
                                              const MachineOperand &Destination,
                                              const MachineOperand &Source) {
  const Register *DestinationReg = Destination.getIf<Register>();
  const Register *SourceReg = Source.getIf<Register>();
  if (DestinationReg == nullptr || SourceReg == nullptr)
    return makeUnexpected(ErrorCode::VerificationError, "PHI operands must name registers");
  if (SourceReg->isNoRegister())
    return {};

  if (DestinationReg->isVirtual() && SourceReg->isVirtual()) {
    auto DestinationInfo = Function.registerInfo().virtualRegisterInfo(*DestinationReg);
    if (!DestinationInfo)
      return std::unexpected(DestinationInfo.error());
    auto SourceInfo = Function.registerInfo().virtualRegisterInfo(*SourceReg);
    if (!SourceInfo)
      return std::unexpected(SourceInfo.error());
    if (DestinationInfo->get().Type && SourceInfo->get().Type &&
        DestinationInfo->get().Type != SourceInfo->get().Type) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "PHI destination and incoming value types do not match");
    }
  }
  return Target.validateTieCompatibility(Destination, Source, Function.registerInfo());
}

[[nodiscard]] Expected<std::vector<PhiPlan>> collectPhiPlans(MachineBasicBlock &Block,
                                                             MachineFunction &Function,
                                                             const TargetDescription &Target) {
  std::vector<PhiPlan> Plans;
  bool SawCode = false;
  for (MachineInstr *Instruction = Block.firstInstruction(); Instruction != nullptr;
       Instruction = Instruction->next()) {
    const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
    if (Descriptor == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "PHI elimination encountered an unknown opcode");
    }
    if (Instruction->opcode() == opcode(GenericOpcode::G_PHI)) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "PHI elimination requires G_PHI to be selected to PHI");
    }
    if (!Descriptor->Flags.has(InstructionFlag::Phi)) {
      SawCode |= !Descriptor->Flags.has(InstructionFlag::Meta);
      continue;
    }
    if (SawCode) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "PHI instruction appears after the leading PHI/meta region");
    }
    if (Instruction->operandCount() < 3 || (Instruction->operandCount() - 1U) % 2U != 0) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "PHI must contain a destination and incoming value/block pairs");
    }

    MachineOperand *DestinationOperand = Instruction->operandAt(0);
    const Register *Destination =
        DestinationOperand == nullptr ? nullptr : DestinationOperand->getIf<Register>();
    if (Destination == nullptr || !Destination->isVirtual() || !DestinationOperand->isDef() ||
        DestinationOperand->isUse() || DestinationOperand->isImplicit()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "PHI destination must be an explicit virtual-register definition");
    }

    PhiPlan Plan;
    Plan.Instruction = Instruction;
    Plan.Destination =
        RegisterValue{*Destination, DestinationOperand->subReg(),
                      DestinationOperand->registerFlags(), DestinationOperand->targetFlags()};
    std::unordered_set<MachineBasicBlock *> SeenPredecessors;
    for (std::size_t Index = 1; Index < Instruction->operandCount(); Index += 2U) {
      MachineOperand *SourceOperand = Instruction->operandAt(Index);
      MachineOperand *BlockOperand = Instruction->operandAt(Index + 1U);
      const Register *Source =
          SourceOperand == nullptr ? nullptr : SourceOperand->getIf<Register>();
      const BasicBlockOperand *IncomingBlock =
          BlockOperand == nullptr ? nullptr : BlockOperand->getIf<BasicBlockOperand>();
      if (Source == nullptr || SourceOperand == nullptr || !SourceOperand->isUse() ||
          SourceOperand->isDef() || SourceOperand->isImplicit() || IncomingBlock == nullptr ||
          IncomingBlock->Block == nullptr || BlockOperand->isImplicit()) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "PHI incoming operands must be explicit register/block pairs");
      }
      if (std::ranges::find(Block.predecessors(), IncomingBlock->Block) ==
          Block.predecessors().end()) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "PHI incoming block is not a CFG predecessor");
      }
      if (!SeenPredecessors.insert(IncomingBlock->Block).second) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "PHI contains duplicate incoming predecessors");
      }
      auto Compatibility = validatePhiValue(Function, Target, *DestinationOperand, *SourceOperand);
      if (!Compatibility)
        return std::unexpected(Compatibility.error());
      Plan.IncomingValues.push_back(
          PhiIncoming{RegisterValue{*Source, SourceOperand->subReg(),
                                    SourceOperand->registerFlags(), SourceOperand->targetFlags()},
                      IncomingBlock->Block, isImplicitlyUndef(Function, *SourceOperand)});
    }
    if (SeenPredecessors.size() != Block.predecessors().size()) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "PHI incoming blocks do not exactly cover CFG predecessors");
    }
    Plans.push_back(std::move(Plan));
  }
  return Plans;
}

[[nodiscard]] Expected<void> copyLiveIns(const MachineBasicBlock &Source,
                                         MachineBasicBlock &Destination) {
  for (const MachineBasicBlockLiveIn &LiveIn : Source.liveIns()) {
    auto Added = Destination.addLiveIn(LiveIn.PhysReg, LiveIn.Lanes);
    if (!Added)
      return std::unexpected(Added.error());
  }
  return {};
}

[[nodiscard]] Expected<void> ensurePhysicalLiveIn(MachineBasicBlock &Block, PhysicalRegister Reg,
                                                  const TargetDescription &Target) {
  const PhysicalRegisterDesc *Description = Target.registers().physicalRegister(Reg);
  if (Description == nullptr) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "PHI incoming value references an unknown physical register");
  }
  LaneMask Required;
  for (const RegisterUnitLane &Unit : Description->Units)
    Required = Required | Unit.Lanes;
  if (Required.empty()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "PHI incoming physical register has no addressable lanes");
  }

  for (const MachineBasicBlockLiveIn &LiveIn : Block.liveIns()) {
    if (LiveIn.PhysReg != Reg)
      continue;
    if (LiveIn.Lanes.contains(Required))
      return {};
    const LaneMask Combined = LiveIn.Lanes | Required;
    auto Removed = Block.removeLiveIn(Reg);
    if (!Removed)
      return std::unexpected(Removed.error());
    return Block.addLiveIn(Reg, Combined);
  }
  return Block.addLiveIn(Reg, Required);
}

[[nodiscard]] Expected<MachineBasicBlock *>
placePhiCopiesOnEdge(MachineFunction &Function, const TargetDescription &Target,
                     const TargetSSAInfo &TargetInfo, MachineBasicBlock &From,
                     MachineBasicBlock &To,
                     std::unordered_map<EdgeKey, MachineBasicBlock *, EdgeKeyHash> &Cache,
                     MachinePassResult &PassResult) {
  if (From.successors().size() <= 1)
    return &From;

  const EdgeKey Key{&From, &To};
  if (const auto Existing = Cache.find(Key); Existing != Cache.end())
    return Existing->second;
  if (std::ranges::none_of(From.successors(), [&To](const MachineSuccessor &Successor) {
        return Successor.Block == &To;
      })) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "PHI incoming predecessor has no edge to its successor");
  }

  auto SourceFallthrough = TargetInfo.isFallthroughEdge(From, To, Function, Target);
  if (!SourceFallthrough)
    return std::unexpected(SourceFallthrough.error());

  MachineBasicBlock *LayoutPredecessor = To.previous();
  if (LayoutPredecessor != nullptr && LayoutPredecessor != &From) {
    auto OtherFallthrough = TargetInfo.isFallthroughEdge(*LayoutPredecessor, To, Function, Target);
    if (!OtherFallthrough)
      return std::unexpected(OtherFallthrough.error());
    if (*OtherFallthrough) {
      auto Branch = TargetInfo.insertUnconditionalBranch(*LayoutPredecessor, To, Function, Target);
      if (!Branch)
        return std::unexpected(Branch.error());
      PassResult.Changed = true;
      PassResult.Changes.add(MachinePassChange::Instructions);
    }
  }

  auto EdgeBlockResult = Function.createBasicBlock();
  if (!EdgeBlockResult)
    return std::unexpected(EdgeBlockResult.error());
  MachineBasicBlock *EdgeBlock = *EdgeBlockResult;
  auto Inserted = Function.insertBlockBefore(To, *EdgeBlock);
  if (!Inserted)
    return std::unexpected(Inserted.error());

  auto Rewritten = TargetInfo.rewriteBranchTarget(From, To, *EdgeBlock, Function, Target);
  if (!Rewritten)
    return std::unexpected(Rewritten.error());
  if (!*Rewritten && !*SourceFallthrough) {
    return makeUnexpected(ErrorCode::Unsupported,
                          "PHI elimination cannot split an indirect or unmodeled CFG edge");
  }

  auto Split = Function.cfgUpdater().splitEdge(From, To, *EdgeBlock);
  if (!Split)
    return std::unexpected(Split.error());
  auto LiveIns = copyLiveIns(To, *EdgeBlock);
  if (!LiveIns)
    return std::unexpected(LiveIns.error());
  auto Branch = TargetInfo.insertUnconditionalBranch(*EdgeBlock, To, Function, Target);
  if (!Branch)
    return std::unexpected(Branch.error());

  Cache.emplace(Key, EdgeBlock);
  PassResult.Changed = true;
  PassResult.Changes.add(MachinePassChange::CFG);
  PassResult.Changes.add(MachinePassChange::BlockLayout);
  PassResult.Changes.add(MachinePassChange::Instructions);
  if (*Rewritten)
    PassResult.Changes.add(MachinePassChange::Operands);
  return EdgeBlock;
}

[[nodiscard]] Expected<void> repairTrackedLiveness(MachineFunction &Function,
                                                   const TargetDescription &Target,
                                                   MachinePassResult &Result) {
  if (!Result.Changed || !Function.properties().has(MachineFunctionProperty::TracksLiveness)) {
    return {};
  }
  auto Rebuilt = rebuildMachineLiveness(Function, Target);
  if (!Rebuilt)
    return std::unexpected(Rebuilt.error());
  if (*Rebuilt)
    Result.Changes.add(MachinePassChange::Operands);
  return {};
}

[[nodiscard]] Expected<SubRegIndex> subRegIndex(const MachineOperand &Operand,
                                                const TargetDescription &Target) {
  std::uint64_t Value = 0;
  if (const auto *Immediate = Operand.getIf<std::int64_t>(); Immediate != nullptr) {
    if (*Immediate <= 0)
      return makeUnexpected(ErrorCode::VerificationError,
                            "subregister pseudo requires a positive subregister index");
    Value = static_cast<std::uint64_t>(*Immediate);
  } else if (const auto *WideImmediate = Operand.getIf<IntegerImmediateOperand>();
             WideImmediate != nullptr) {
    if (WideImmediate->Value.isNegative() || WideImmediate->Value.bitWidth() > 64)
      return makeUnexpected(ErrorCode::VerificationError,
                            "subregister pseudo index does not fit an unsigned integer");
    Value = WideImmediate->Value.words().front();
  } else {
    return makeUnexpected(ErrorCode::VerificationError,
                          "subregister pseudo is missing its immediate index");
  }
  if (Value == 0 || Value > std::numeric_limits<std::uint32_t>::max()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "subregister pseudo index is outside the supported range");
  }
  const SubRegIndex Result = SubRegIndex::fromValue(static_cast<std::uint32_t>(Value));
  if (Target.registers().subRegIndex(Result) == nullptr) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "subregister pseudo references an unknown target index");
  }
  return Result;
}

[[nodiscard]] Expected<SubRegIndex> composeSubReg(SubRegIndex Outer, SubRegIndex Inner,
                                                  const TargetDescription &Target) {
  if (Outer.value() == 0)
    return Inner;
  if (Inner.value() == 0)
    return Outer;
  const auto Composed = Target.registers().composeSubRegIndices(Outer, Inner);
  if (!Composed) {
    return makeUnexpected(ErrorCode::Unsupported,
                          "target cannot compose the requested subregister indices");
  }
  return *Composed;
}

[[nodiscard]] Expected<void> removeInstruction(MachineInstr &Instruction) {
  MachineBasicBlock *Block = Instruction.parent();
  if (Block == nullptr)
    return makeUnexpected(ErrorCode::InternalError, "rewritten instruction is not linked");
  return Block->removeInstruction(Instruction);
}

[[nodiscard]] Expected<void> expandRegSequence(MachineInstr &Instruction, MachineFunction &Function,
                                               const TargetDescription &Target,
                                               const TargetSSAInfo &TargetInfo) {
  if (Instruction.operandCount() < 3 || (Instruction.operandCount() - 1U) % 2U != 0) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "REG_SEQUENCE requires a destination and source/subregister pairs");
  }
  MachineOperand *DestinationOperand = Instruction.operandAt(0);
  const Register *Destination =
      DestinationOperand == nullptr ? nullptr : DestinationOperand->getIf<Register>();
  if (Destination == nullptr || Destination->isNoRegister() || !DestinationOperand->isDef()) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "REG_SEQUENCE requires a register destination definition");
  }
  MachineBasicBlock *Block = Instruction.parent();
  if (Block == nullptr)
    return makeUnexpected(ErrorCode::InternalError, "REG_SEQUENCE is not linked to a block");

  RegisterValue DestinationValue{*Destination, DestinationOperand->subReg(),
                                 DestinationOperand->registerFlags(),
                                 DestinationOperand->targetFlags()};
  auto Initial =
      buildImplicitDef(Function, Target, *Block, &Instruction, DestinationValue, &Instruction);
  if (!Initial)
    return std::unexpected(Initial.error());

  for (std::size_t Index = 1; Index < Instruction.operandCount(); Index += 2U) {
    MachineOperand *SourceOperand = Instruction.operandAt(Index);
    MachineOperand *IndexOperand = Instruction.operandAt(Index + 1U);
    const Register *Source = SourceOperand == nullptr ? nullptr : SourceOperand->getIf<Register>();
    if (Source == nullptr || SourceOperand == nullptr || !SourceOperand->isUse() ||
        IndexOperand == nullptr) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "REG_SEQUENCE contains a malformed source/subregister pair");
    }
    auto InnerSubReg = subRegIndex(*IndexOperand, Target);
    if (!InnerSubReg)
      return std::unexpected(InnerSubReg.error());
    auto DestinationSubReg = composeSubReg(DestinationOperand->subReg(), *InnerSubReg, Target);
    if (!DestinationSubReg)
      return std::unexpected(DestinationSubReg.error());

    RegisterValue LaneDestination{
        *Destination, *DestinationSubReg, {}, DestinationOperand->targetFlags()};
    if (isImplicitlyUndef(Function, *SourceOperand)) {
      auto Undef =
          buildImplicitDef(Function, Target, *Block, &Instruction, LaneDestination, &Instruction);
      if (!Undef)
        return std::unexpected(Undef.error());
      continue;
    }
    RegisterValue SourceValue{*Source, SourceOperand->subReg(), SourceOperand->registerFlags(),
                              SourceOperand->targetFlags()};
    auto Copy = buildCopy(Function, Target, TargetInfo, *Block, &Instruction, LaneDestination,
                          SourceValue, &Instruction);
    if (!Copy)
      return std::unexpected(Copy.error());
  }
  return removeInstruction(Instruction);
}

[[nodiscard]] Expected<void> expandInsertSubreg(MachineInstr &Instruction,
                                                MachineFunction &Function,
                                                const TargetDescription &Target,
                                                const TargetSSAInfo &TargetInfo) {
  if (Instruction.operandCount() != 4) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "INSERT_SUBREG requires destination, base, inserted value, and index");
  }
  MachineOperand *DestinationOperand = Instruction.operandAt(0);
  MachineOperand *BaseOperand = Instruction.operandAt(1);
  MachineOperand *InsertedOperand = Instruction.operandAt(2);
  MachineOperand *IndexOperand = Instruction.operandAt(3);
  const Register *Destination =
      DestinationOperand == nullptr ? nullptr : DestinationOperand->getIf<Register>();
  const Register *Base = BaseOperand == nullptr ? nullptr : BaseOperand->getIf<Register>();
  const Register *Inserted =
      InsertedOperand == nullptr ? nullptr : InsertedOperand->getIf<Register>();
  if (Destination == nullptr || Base == nullptr || Inserted == nullptr ||
      Destination->isNoRegister() || DestinationOperand == nullptr ||
      !DestinationOperand->isDef() || BaseOperand == nullptr || !BaseOperand->isUse() ||
      InsertedOperand == nullptr || !InsertedOperand->isUse() || IndexOperand == nullptr) {
    return makeUnexpected(ErrorCode::VerificationError,
                          "INSERT_SUBREG contains malformed register operands");
  }
  MachineBasicBlock *Block = Instruction.parent();
  if (Block == nullptr)
    return makeUnexpected(ErrorCode::InternalError, "INSERT_SUBREG is not linked to a block");

  RegisterValue DestinationValue{*Destination, DestinationOperand->subReg(),
                                 DestinationOperand->registerFlags(),
                                 DestinationOperand->targetFlags()};
  if (isImplicitlyUndef(Function, *BaseOperand)) {
    auto Undef =
        buildImplicitDef(Function, Target, *Block, &Instruction, DestinationValue, &Instruction);
    if (!Undef)
      return std::unexpected(Undef.error());
  } else if (*Destination != *Base || DestinationOperand->subReg() != BaseOperand->subReg()) {
    RegisterValue BaseValue{*Base, BaseOperand->subReg(), BaseOperand->registerFlags(),
                            BaseOperand->targetFlags()};
    auto Copy = buildCopy(Function, Target, TargetInfo, *Block, &Instruction, DestinationValue,
                          BaseValue, &Instruction);
    if (!Copy)
      return std::unexpected(Copy.error());
  }

  auto InnerSubReg = subRegIndex(*IndexOperand, Target);
  if (!InnerSubReg)
    return std::unexpected(InnerSubReg.error());
  auto DestinationSubReg = composeSubReg(DestinationOperand->subReg(), *InnerSubReg, Target);
  if (!DestinationSubReg)
    return std::unexpected(DestinationSubReg.error());
  RegisterValue LaneDestination{
      *Destination, *DestinationSubReg, {}, DestinationOperand->targetFlags()};
  if (isImplicitlyUndef(Function, *InsertedOperand)) {
    auto Undef =
        buildImplicitDef(Function, Target, *Block, &Instruction, LaneDestination, &Instruction);
    if (!Undef)
      return std::unexpected(Undef.error());
  } else {
    RegisterValue InsertedValue{*Inserted, InsertedOperand->subReg(),
                                InsertedOperand->registerFlags(), InsertedOperand->targetFlags()};
    auto Copy = buildCopy(Function, Target, TargetInfo, *Block, &Instruction, LaneDestination,
                          InsertedValue, &Instruction);
    if (!Copy)
      return std::unexpected(Copy.error());
  }
  return removeInstruction(Instruction);
}

[[nodiscard]] Expected<void> swapRegisterUses(MachineOperand &Left, MachineOperand &Right) {
  const std::optional<Register> LeftReg = Left.registerValue();
  const std::optional<Register> RightReg = Right.registerValue();
  if (!LeftReg || !RightReg || !Left.isUse() || Left.isDef() || !Right.isUse() || Right.isDef()) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "commutation requires two pure register uses");
  }
  const SubRegIndex LeftSubReg = Left.subReg();
  const SubRegIndex RightSubReg = Right.subReg();
  const RegisterOperandFlags LeftFlags = Left.registerFlags();
  const RegisterOperandFlags RightFlags = Right.registerFlags();
  const std::uint32_t LeftTargetFlags = Left.targetFlags();
  const std::uint32_t RightTargetFlags = Right.targetFlags();

  auto SetLeftReg = Left.setRegister(*RightReg);
  if (!SetLeftReg)
    return std::unexpected(SetLeftReg.error());
  auto SetRightReg = Right.setRegister(*LeftReg);
  if (!SetRightReg)
    return std::unexpected(SetRightReg.error());
  auto SetLeftSubReg = Left.setSubReg(RightSubReg);
  if (!SetLeftSubReg)
    return std::unexpected(SetLeftSubReg.error());
  auto SetRightSubReg = Right.setSubReg(LeftSubReg);
  if (!SetRightSubReg)
    return std::unexpected(SetRightSubReg.error());
  auto SetLeftFlags = Left.setRegisterFlags(RightFlags);
  if (!SetLeftFlags)
    return std::unexpected(SetLeftFlags.error());
  auto SetRightFlags = Right.setRegisterFlags(LeftFlags);
  if (!SetRightFlags)
    return std::unexpected(SetRightFlags.error());
  Left.setTargetFlags(RightTargetFlags);
  Right.setTargetFlags(LeftTargetFlags);
  return {};
}

[[nodiscard]] Expected<bool> tryCommute(MachineInstr &Instruction, std::size_t TiedUseIndex,
                                        Register Destination, const TargetDescription &Target,
                                        MachineFunction &Function) {
  const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction.opcode());
  if (Descriptor == nullptr || !Descriptor->Flags.has(InstructionFlag::Commutable))
    return false;
  MachineOperand *TiedUse = Instruction.operandAt(TiedUseIndex);
  if (TiedUse == nullptr)
    return makeUnexpected(ErrorCode::InternalError, "tied use disappeared during commutation");

  for (std::size_t Index = 0; Index < Instruction.operandCount(); ++Index) {
    if (Index == TiedUseIndex)
      continue;
    MachineOperand *Candidate = Instruction.operandAt(Index);
    if (Candidate == nullptr || Candidate->isImplicit() || !Candidate->isUse() ||
        Candidate->isDef() || Candidate->tiedOperandIndex() ||
        Candidate->registerValue() != Destination || isImplicitlyUndef(Function, *Candidate)) {
      continue;
    }
    auto Swapped = swapRegisterUses(*TiedUse, *Candidate);
    if (!Swapped)
      return std::unexpected(Swapped.error());
    auto Validation = Target.validateInstruction(Instruction, Function.registerInfo());
    if (Validation)
      return true;
    auto Restored = swapRegisterUses(*TiedUse, *Candidate);
    if (!Restored)
      return std::unexpected(Restored.error());
  }
  return false;
}

[[nodiscard]] Expected<void> rewriteUndefTie(MachineOperand &Use, const MachineOperand &Def) {
  const std::optional<Register> Destination = Def.registerValue();
  if (!Destination)
    return makeUnexpected(ErrorCode::VerificationError, "tied definition is not a register");
  auto SetRegister = Use.setRegister(*Destination);
  if (!SetRegister)
    return std::unexpected(SetRegister.error());
  auto SetSubReg = Use.setSubReg(Def.subReg());
  if (!SetSubReg)
    return std::unexpected(SetSubReg.error());
  RegisterOperandFlags Flags = Use.registerFlags();
  Flags.set(RegisterOperandFlag::Undef);
  Flags.clear(RegisterOperandFlag::Kill);
  return Use.setRegisterFlags(Flags);
}

[[nodiscard]] Expected<void> verifyNoStageTenPseudos(const MachineFunction &Function,
                                                     const TargetDescription &Target) {
  for (const MachineInstr *Instruction : Function.allInstructions()) {
    if (Instruction->parent() == nullptr)
      continue;
    if (Instruction->opcode() == opcode(GenericOpcode::G_PHI) ||
        Instruction->opcode() == opcode(GenericOpcode::PHI) ||
        Instruction->opcode() == opcode(GenericOpcode::REG_SEQUENCE) ||
        Instruction->opcode() == opcode(GenericOpcode::INSERT_SUBREG)) {
      return makeUnexpected(ErrorCode::VerificationError,
                            "SSA exit left a Stage 10 pseudo instruction in MachineIR");
    }
    const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
    if (Descriptor == nullptr)
      return makeUnexpected(ErrorCode::VerificationError,
                            "SSA exit produced an instruction with no descriptor");
    for (std::size_t Index = 0; Index < Instruction->operandCount(); ++Index) {
      const MachineOperand *Operand = Instruction->operandAt(Index);
      if (Operand == nullptr || !Operand->isUse())
        continue;
      const std::optional<std::size_t> Tie = Operand->tiedOperandIndex();
      if (!Tie)
        continue;
      const MachineOperand *Def = Instruction->operandAt(Tie.value());
      if (Def == nullptr || Def->registerValue() != Operand->registerValue()) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "two-address rewrite left an unequal tied register");
      }
    }
  }
  return {};
}

} // namespace

const MachinePassContract &PHIEliminationPass::contract() const noexcept {
  static const MachinePassContract Contract{
      .Name = "phi-node-elimination",
      .Requires = {MachineFunctionProperty::IsSSA, MachineFunctionProperty::Selected,
                   MachineFunctionProperty::RegBankSelected},
      .Sets = {MachineFunctionProperty::NoPHIs},
      .Clears = {MachineFunctionProperty::IsSSA},
      .RequiredAnalyses = {},
      .VerificationStage = PipelineStage::SSAEliminated,
  };
  return Contract;
}

Expected<MachinePassResult> PHIEliminationPass::run(MachineFunction &Function,
                                                    const TargetDescription &Target,
                                                    MachineAnalysisManager &Analyses) {
  if (Info == nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "PHI elimination has no target SSA behavior interface");
  }
  auto Required = requireProperties(Function, contract());
  if (!Required)
    return std::unexpected(Required.error());

  auto Trial = cloneMachineFunction(
      Function, Target,
      MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
  if (!Trial) {
    return std::unexpected(
        prefixedError(Trial.error(), "PHI-elimination transaction setup failed: "));
  }
  MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
  auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
  if (!TrialResult)
    return std::unexpected(TrialResult.error());

  Actions.beginReplay();
  auto Result = runImplementation(Function, Target, Actions);
  if (!Result) {
    return std::unexpected(prefixedError(
        Result.error(), "PHI elimination diverged after a successful transaction preflight: "));
  }
  auto Replayed = Actions.finishReplay();
  if (!Replayed)
    return std::unexpected(Replayed.error());
  return Result;
}

Expected<MachinePassResult> PHIEliminationPass::runImplementation(MachineFunction &Function,
                                                                  const TargetDescription &Target,
                                                                  MachineActionReplay &Actions) {
  MachinePassResult Result;
  std::unordered_map<EdgeKey, MachineBasicBlock *, EdgeKeyHash> SplitEdges;
  const std::vector<MachineBasicBlock *> Blocks(Function.allBlocks().begin(),
                                                Function.allBlocks().end());

  for (MachineBasicBlock *Block : Blocks) {
    auto Plans = collectPhiPlans(*Block, Function, Target);
    if (!Plans)
      return std::unexpected(Plans.error());
    if (Plans->empty())
      continue;

    for (PhiPlan &Plan : *Plans) {
      auto Accepted = Actions.request("eliminate-phi", Function, Plan.Instruction->stableID());
      if (!Accepted)
        return std::unexpected(Accepted.error());
      if (!*Accepted) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "required PHI elimination action was rejected");
      }
      auto Fresh = cloneRegisterConstraints(Function, Plan.Destination.Reg);
      if (!Fresh)
        return std::unexpected(Fresh.error());
      Plan.FreshIncoming = *Fresh;
    }

    for (PhiPlan &Plan : *Plans) {
      for (const PhiIncoming &Incoming : Plan.IncomingValues) {
        auto EdgeBlock = placePhiCopiesOnEdge(Function, Target, *Info, *Incoming.Predecessor,
                                              *Block, SplitEdges, Result);
        if (!EdgeBlock)
          return std::unexpected(EdgeBlock.error());
        if (*EdgeBlock != Incoming.Predecessor) {
          const std::optional<PhysicalRegister> Physical = Incoming.Value.Reg.physicalRegister();
          if (Physical) {
            auto LiveIn = ensurePhysicalLiveIn(**EdgeBlock, *Physical, Target);
            if (!LiveIn)
              return std::unexpected(LiveIn.error());
          }
        }
        MachineInstr *Terminator = firstTerminator(**EdgeBlock, Target);
        RegisterValue Fresh{Plan.FreshIncoming, Plan.Destination.SubReg, Plan.Destination.Flags,
                            Plan.Destination.TargetFlags};
        Expected<MachineInstr *> Transfer =
            Incoming.IsUndef ? buildImplicitDef(Function, Target, **EdgeBlock, Terminator, Fresh,
                                                Plan.Instruction)
                             : buildCopy(Function, Target, *Info, **EdgeBlock, Terminator, Fresh,
                                         Incoming.Value, Plan.Instruction);
        if (!Transfer)
          return std::unexpected(Transfer.error());
        Result.Changed = true;
        Result.Changes.add(MachinePassChange::Instructions);
      }
    }

    MachineInstr *InsertionPoint = firstCodeAfterPhiRegion(*Block, Target);
    for (PhiPlan &Plan : *Plans) {
      RegisterValue Fresh{
          Plan.FreshIncoming, Plan.Destination.SubReg, {}, Plan.Destination.TargetFlags};
      auto Copy = buildCopy(Function, Target, *Info, *Block, InsertionPoint, Plan.Destination,
                            Fresh, Plan.Instruction);
      if (!Copy)
        return std::unexpected(Copy.error());
      Result.Changed = true;
      Result.Changes.add(MachinePassChange::Instructions);
    }

    for (PhiPlan &Plan : *Plans) {
      auto Removed = Block->removeInstruction(*Plan.Instruction);
      if (!Removed)
        return std::unexpected(Removed.error());
    }
    Result.Changes.add(MachinePassChange::Registers);
  }

  if (Result.Changed && Function.properties().has(MachineFunctionProperty::TracksLiveness)) {
    auto Repaired = repairTrackedLiveness(Function, Target, Result);
    if (!Repaired)
      return std::unexpected(
          prefixedError(Repaired.error(), "PHI-elimination liveness repair failed: "));
  }
  Result.Preserved = Result.Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
  return Result;
}

const MachinePassContract &TwoAddressRewritePass::contract() const noexcept {
  static const MachinePassContract Contract{
      .Name = "twoaddressinstruction",
      .Requires = {MachineFunctionProperty::Selected, MachineFunctionProperty::NoPHIs},
      .Sets = {MachineFunctionProperty::TiedOpsRewritten},
      .Clears = {MachineFunctionProperty::IsSSA},
      .RequiredAnalyses = {},
      .VerificationStage = PipelineStage::TiedOpsRewritten,
  };
  return Contract;
}

Expected<MachinePassResult> TwoAddressRewritePass::run(MachineFunction &Function,
                                                       const TargetDescription &Target,
                                                       MachineAnalysisManager &Analyses) {
  if (Info == nullptr) {
    return makeUnexpected(ErrorCode::InvalidArgument,
                          "two-address rewrite has no target SSA behavior interface");
  }
  auto Required = requireProperties(Function, contract());
  if (!Required)
    return std::unexpected(Required.error());

  auto Trial = cloneMachineFunction(
      Function, Target,
      MachineFunctionCloneOptions{.NewStableID = Function.stableID(), .NewName = std::nullopt});
  if (!Trial) {
    return std::unexpected(prefixedError(Trial.error(), "two-address transaction setup failed: "));
  }
  MachineActionReplay Actions(Analyses, Trial->InstructionStableIDs);
  auto TrialResult = runImplementation(*Trial->ClonedFunction, Target, Actions);
  if (!TrialResult)
    return std::unexpected(TrialResult.error());

  Actions.beginReplay();
  auto Result = runImplementation(Function, Target, Actions);
  if (!Result) {
    return std::unexpected(prefixedError(
        Result.error(), "two-address rewrite diverged after a successful transaction preflight: "));
  }
  auto Replayed = Actions.finishReplay();
  if (!Replayed)
    return std::unexpected(Replayed.error());
  return Result;
}

Expected<MachinePassResult>
TwoAddressRewritePass::runImplementation(MachineFunction &Function, const TargetDescription &Target,
                                         MachineActionReplay &Actions) {
  MachinePassResult Result;
  const std::vector<MachineInstr *> Instructions(Function.allInstructions().begin(),
                                                 Function.allInstructions().end());
  for (MachineInstr *Instruction : Instructions) {
    if (Instruction->parent() == nullptr)
      continue;
    if (Instruction->opcode() == opcode(GenericOpcode::REG_SEQUENCE)) {
      auto Accepted = Actions.request("expand-reg-sequence", Function, Instruction->stableID());
      if (!Accepted)
        return std::unexpected(Accepted.error());
      if (!*Accepted)
        return makeUnexpected(ErrorCode::VerificationError,
                              "required REG_SEQUENCE expansion action was rejected");
      auto Expanded = expandRegSequence(*Instruction, Function, Target, *Info);
      if (!Expanded)
        return std::unexpected(Expanded.error());
      Result.Changed = true;
      Result.Changes.add(MachinePassChange::Instructions);
      continue;
    }
    if (Instruction->opcode() == opcode(GenericOpcode::INSERT_SUBREG)) {
      auto Accepted = Actions.request("expand-insert-subreg", Function, Instruction->stableID());
      if (!Accepted)
        return std::unexpected(Accepted.error());
      if (!*Accepted)
        return makeUnexpected(ErrorCode::VerificationError,
                              "required INSERT_SUBREG expansion action was rejected");
      auto Expanded = expandInsertSubreg(*Instruction, Function, Target, *Info);
      if (!Expanded)
        return std::unexpected(Expanded.error());
      Result.Changed = true;
      Result.Changes.add(MachinePassChange::Instructions);
      continue;
    }

    std::size_t RewriteAttempts = 0;
    bool Restart = true;
    while (Restart && Instruction->parent() != nullptr) {
      Restart = false;
      if (++RewriteAttempts > Instruction->operandCount() * 2U + 8U) {
        return makeUnexpected(ErrorCode::InternalError,
                              "two-address target conversion did not make progress");
      }
      const InstructionDescriptor *Descriptor = Target.instructionDescriptor(Instruction->opcode());
      if (Descriptor == nullptr) {
        return makeUnexpected(ErrorCode::VerificationError,
                              "two-address rewrite encountered an unknown opcode");
      }

      for (std::size_t UseIndex = 0; UseIndex < Instruction->operandCount(); ++UseIndex) {
        MachineOperand *Use = Instruction->operandAt(UseIndex);
        if (Use == nullptr || !Use->isUse())
          continue;
        const std::optional<std::size_t> TiedDefIndex = Use->tiedOperandIndex();
        if (!TiedDefIndex)
          continue;
        const std::size_t DefIndex = TiedDefIndex.value();
        MachineOperand *Def = Instruction->operandAt(DefIndex);
        if (Def == nullptr || !Def->isDef()) {
          return makeUnexpected(ErrorCode::VerificationError,
                                "tied use does not reference a definition");
        }
        const std::optional<Register> DefReg = Def->registerValue();
        const std::optional<Register> UseReg = Use->registerValue();
        if (!DefReg || !UseReg || DefReg->isNoRegister()) {
          return makeUnexpected(ErrorCode::Unsupported,
                                "two-address rewrite requires register ties");
        }
        if (*DefReg == *UseReg)
          continue;

        if (isImplicitlyUndef(Function, *Use)) {
          auto Accepted = Actions.request("rewrite-undef-tie", Function, Instruction->stableID());
          if (!Accepted)
            return std::unexpected(Accepted.error());
          if (!*Accepted)
            return makeUnexpected(ErrorCode::VerificationError,
                                  "required undef tie rewrite action was rejected");
          auto Rewritten = rewriteUndefTie(*Use, *Def);
          if (!Rewritten)
            return std::unexpected(Rewritten.error());
          Result.Changed = true;
          Result.Changes.add(MachinePassChange::Operands);
          continue;
        }

        auto AllowCommute =
            Actions.request("commute-tied-operands", Function, Instruction->stableID());
        if (!AllowCommute)
          return std::unexpected(AllowCommute.error());
        if (*AllowCommute) {
          auto Commuted = tryCommute(*Instruction, UseIndex, *DefReg, Target, Function);
          if (!Commuted)
            return std::unexpected(Commuted.error());
          if (*Commuted) {
            Result.Changed = true;
            Result.Changes.add(MachinePassChange::Operands);
            continue;
          }
        }

        auto AllowConversion =
            Actions.request("convert-three-address", Function, Instruction->stableID());
        if (!AllowConversion)
          return std::unexpected(AllowConversion.error());
        if (*AllowConversion) {
          auto Converted =
              Info->tryConvertToThreeAddress(*Instruction, DefIndex, UseIndex, Function, Target);
          if (!Converted)
            return std::unexpected(Converted.error());
          if (*Converted) {
            Result.Changed = true;
            Result.Changes.add(MachinePassChange::Instructions);
            Result.Changes.add(MachinePassChange::Operands);
            Result.Changes.add(MachinePassChange::Registers);
            Restart = true;
            break;
          }
        }

        auto AllowCopy = Actions.request("insert-tied-copy", Function, Instruction->stableID());
        if (!AllowCopy)
          return std::unexpected(AllowCopy.error());
        if (!*AllowCopy)
          return makeUnexpected(ErrorCode::VerificationError,
                                "required tied COPY insertion action was rejected");
        MachineBasicBlock *Block = Instruction->parent();
        if (Block == nullptr)
          return makeUnexpected(ErrorCode::InternalError,
                                "two-address instruction disappeared before COPY insertion");
        RegisterValue Destination{*DefReg, Def->subReg(), Def->registerFlags(), Def->targetFlags()};
        RegisterValue Source{*UseReg, Use->subReg(), Use->registerFlags(), Use->targetFlags()};
        auto Copy = buildCopy(Function, Target, *Info, *Block, Instruction, Destination, Source,
                              Instruction);
        if (!Copy)
          return std::unexpected(Copy.error());
        auto SetRegister = Use->setRegister(*DefReg);
        if (!SetRegister)
          return std::unexpected(SetRegister.error());
        auto SetSubReg = Use->setSubReg(Def->subReg());
        if (!SetSubReg)
          return std::unexpected(SetSubReg.error());
        RegisterOperandFlags Flags = Use->registerFlags();
        Flags.clear(RegisterOperandFlag::Kill);
        auto SetFlags = Use->setRegisterFlags(Flags);
        if (!SetFlags)
          return std::unexpected(SetFlags.error());
        Result.Changed = true;
        Result.Changes.add(MachinePassChange::Instructions);
        Result.Changes.add(MachinePassChange::Operands);
      }
    }
  }

  auto FinalState = verifyNoStageTenPseudos(Function, Target);
  if (!FinalState)
    return std::unexpected(FinalState.error());
  if (Result.Changed && Function.properties().has(MachineFunctionProperty::TracksLiveness)) {
    auto Repaired = repairTrackedLiveness(Function, Target, Result);
    if (!Repaired)
      return std::unexpected(
          prefixedError(Repaired.error(), "two-address liveness repair failed: "));
  }
  Result.Preserved = Result.Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
  return Result;
}

} // namespace rcc