[llvm-branch-commits] [llvm] [AArch64] Use generated instruction mappings (PR #241159)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Nov 10 23:16:41 PST 2026


https://github.com/paperchalice created https://github.com/llvm/llvm-project/pull/241159

<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>

>From 3d296e00fba2820408aaf24df48388135dd26e70 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Sat, 7 Nov 2026 09:26:58 +0800
Subject: [PATCH 1/2] [TableGen] Generate instruction mappings for
 `RegisterBankInfo`

---
 .../llvm/Target/GlobalISel/RegisterBank.td    |  59 ++
 .../TableGen/RegisterBankEmitter-Check.td     |  17 +
 llvm/test/TableGen/RegisterBankEmitter.td     |  16 +
 llvm/utils/TableGen/RegisterBankEmitter.cpp   | 606 +++++++++++++++++-
 4 files changed, 695 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/TableGen/RegisterBankEmitter-Check.td

diff --git a/llvm/include/llvm/Target/GlobalISel/RegisterBank.td b/llvm/include/llvm/Target/GlobalISel/RegisterBank.td
index 51578b66b160bb..48d548b1be39dc 100644
--- a/llvm/include/llvm/Target/GlobalISel/RegisterBank.td
+++ b/llvm/include/llvm/Target/GlobalISel/RegisterBank.td
@@ -9,7 +9,66 @@
 //
 //===----------------------------------------------------------------------===//
 
+/// Same as RegisterBankInfo::PartialMapping
+class PartialMapping<int Len, int startIdx = 0> {
+  int Length = Len;
+  int StartIdx = startIdx;
+}
+
+class RegBankMappingID<string name, int id> {
+  string Name = name;
+  int ID = id;
+}
+def RegBankDefaultMappingID : RegBankMappingID<"DefaultMappingID", -1{31...0}>;
+def RegBankInvalidMappingID : RegBankMappingID<"InvalidMappingID", -2{31...0}>;
+
 class RegisterBank<string name, list<RegisterClass> classes> {
   string Name = name;
   list<RegisterClass> RegisterClasses = classes;
+
+  // If some partial mappings can't be inferred from register class
+  // add them to this list.
+  list<PartialMapping> ExtraPartialMappings = [];
+
+  // Exclude register sizes that are not used by the mapping policy.
+  list<PartialMapping> IgnoredPartialMappings = [];
+
+  // The fixed PartialMapping used for scalable values; zero disables them.
+  int ScalableMappingSize = 0;
+}
+
+class RegBankMapping<RegisterBank bank> {
+  RegisterBank Bank = bank;
+}
+class RegBankTypeMapping<RegisterBank bank, TypedOperand type> : RegBankMapping<bank> {
+  TypedOperand Type = type;
+}
+
+// Define mapping rule for given instruction shape, each mapping in `Mappings` must
+// cover all operands or TypedOperand types.
+class RegBankMappingRule<list<RegBankMapping> mappings,
+  int cost = 1, int priority = 0, RegBankMappingID id = RegBankDefaultMappingID, code predicate = [{}]> {
+  list<RegBankMapping> Mappings = mappings;
+  assert !foldl(1, mappings, cond, m,
+    !and(cond, !isa<RegBankTypeMapping>(m))),
+    "Mappings must not be plan RegBankMapping";
+  int Priority = priority;
+  RegBankMappingID ID = id;
+  int Cost = cost;
+  code Predicate = predicate;
+  bit IsAlternative = false;
+}
+class RegBankAltMappingRule<list<RegBankMapping> mappings,
+  int cost = 1, int priority = 0, RegBankMappingID id = RegBankDefaultMappingID, code predicate = [{}]>
+    : RegBankMappingRule<mappings, cost, priority, id, predicate> {
+  let IsAlternative = true;
+}
+
+/// Assign register bank mapping to instructions in `Opcodes`.
+class RegBankInstrMapping<list<GenericInstruction> opcodes,
+                          list<RegBankMappingRule> rules> {
+  assert !not(!empty(opcodes)), "no opcodes";
+  assert !not(!empty(rules)), "no rules";
+  list<GenericInstruction> Opcodes = opcodes;
+  list<RegBankMappingRule> Rules = rules;
 }
diff --git a/llvm/test/TableGen/RegisterBankEmitter-Check.td b/llvm/test/TableGen/RegisterBankEmitter-Check.td
new file mode 100644
index 00000000000000..3cb64a1508ea1c
--- /dev/null
+++ b/llvm/test/TableGen/RegisterBankEmitter-Check.td
@@ -0,0 +1,17 @@
+// RUN: not llvm-tblgen -gen-register-bank -I %p/../../include %s 2>&1 | FileCheck %s
+
+include "llvm/Target/Target.td"
+
+def MyTarget : Target;
+def R0 : Register<"r0">;
+let Size = 32 in {
+  def ClassA : RegisterClass<"MyTarget", [i32], 32, (add R0)>;
+  def ClassB : RegisterClass<"MyTarget", [i1], 32, (add ClassA)>;
+}
+
+def GPRRegBank : RegisterBank<"GPR", [ClassA]>;
+
+// CHECK: type1 is not mapped in rules
+def : RegBankInstrMapping<[G_SHL], [
+  RegBankMappingRule<[RegBankTypeMapping<GPRRegBank, type0>]>
+]>;
diff --git a/llvm/test/TableGen/RegisterBankEmitter.td b/llvm/test/TableGen/RegisterBankEmitter.td
index 88c7ec1f791516..611a17044966ac 100644
--- a/llvm/test/TableGen/RegisterBankEmitter.td
+++ b/llvm/test/TableGen/RegisterBankEmitter.td
@@ -13,3 +13,19 @@ let Size = 32 in {
 // CHECK: MyTarget::ClassARegClassID
 // CHECK: MyTarget::ClassBRegClassID
 def GPRRegBank : RegisterBank<"GPR", [ClassA]>;
+
+// CHECK: {0, 8, BreakDownBank},
+// CHECK: {8, 8, BreakDownBank},
+// CHECK: {16, 8, BreakDownBank},
+// CHECK: {24, 8, BreakDownBank},
+// CHECK: {0, 16, BreakDownBank},
+// CHECK: {16, 16, BreakDownBank},
+// CHECK: {0, 32, BreakDownBank},
+def BreakDownBank : RegisterBank<"GPR", [ClassA]> {
+  let ExtraPartialMappings=[
+    PartialMapping<16>,
+    PartialMapping<8>,PartialMapping<8,8>,
+    PartialMapping<16, 16>,
+    PartialMapping<8,16>,PartialMapping<8,24>
+  ];
+}
diff --git a/llvm/utils/TableGen/RegisterBankEmitter.cpp b/llvm/utils/TableGen/RegisterBankEmitter.cpp
index 7c5eed10485951..daa30cee16d5c2 100644
--- a/llvm/utils/TableGen/RegisterBankEmitter.cpp
+++ b/llvm/utils/TableGen/RegisterBankEmitter.cpp
@@ -27,6 +27,46 @@
 using namespace llvm;
 
 namespace {
+
+struct PartialMapping {
+  size_t Length;
+  size_t StartIdx;
+
+  // the index in final table
+  size_t Index = 0;
+
+  PartialMapping(const Record &Def)
+      : Length(Def.getValueAsInt("Length")),
+        StartIdx(Def.getValueAsInt("StartIdx")) {}
+
+  PartialMapping(size_t Length, size_t StartIdx = 0)
+      : Length(Length), StartIdx(StartIdx) {}
+
+  bool operator<(const PartialMapping &RHS) const {
+    return std::tie(StartIdx, Length) < std::tie(RHS.StartIdx, RHS.Length);
+  }
+
+  bool operator==(const PartialMapping &RHS) const {
+    return std::tie(StartIdx, Length) == std::tie(RHS.StartIdx, RHS.Length);
+  }
+
+  bool isSimple() const { return StartIdx == 0; }
+};
+
+struct ValueMapping {
+  std::vector<PartialMapping> Parts;
+
+  size_t getNumBreakDowns() const { return Parts.size(); }
+  size_t getSize() const {
+    assert(getNumBreakDowns() != 0 && "invalid value mapping");
+    return Parts.size() * Parts[0].Length;
+  }
+  size_t getPartialMappingIdx() const {
+    assert(getNumBreakDowns() != 0 && "invalid value mapping");
+    return Parts[0].Index;
+  }
+};
+
 class RegisterBank {
 
   /// A vector of register classes that are included in the register bank.
@@ -41,9 +81,19 @@ class RegisterBank {
   /// The register class with the largest register size.
   std::vector<const CodeGenRegisterClass *> RCsWithLargestRegSize;
 
+  /// Record all possible partial mappings of this register bank,
+  /// currently only mappings with start index 0 are supported.
+  std::set<PartialMapping> PartialMappings;
+  std::vector<PartialMapping> PartialMappingTable;
+  std::vector<ValueMapping> ValueMappings;
+
 public:
   RegisterBank(const Record &TheDef, unsigned NumModeIds)
-      : TheDef(TheDef), RCsWithLargestRegSize(NumModeIds) {}
+      : TheDef(TheDef), RCsWithLargestRegSize(NumModeIds) {
+    for (const auto *ExtraDef :
+         TheDef.getValueAsListOfDefs("ExtraPartialMappings"))
+      PartialMappings.emplace(*ExtraDef);
+  }
 
   /// Get the human-readable name for the bank.
   StringRef getName() const { return TheDef.getValueAsString("Name"); }
@@ -61,8 +111,75 @@ class RegisterBank {
   /// Get the name of the global instance variable.
   StringRef getInstanceVarName() const { return TheDef.getName(); }
 
+  size_t getScalableMappingSize() const {
+    return TheDef.getValueAsInt("ScalableMappingSize");
+  }
+
   const Record &getDef() const { return TheDef; }
 
+  const std::set<PartialMapping> &getPartialMappings() const {
+    return PartialMappings;
+  }
+  const std::vector<PartialMapping> &getPartialMappingTable() const {
+    return PartialMappingTable;
+  }
+  const std::vector<ValueMapping> &getValueMappings() const {
+    return ValueMappings;
+  }
+
+  void collectMappings() {
+    for (const auto *RC : register_classes())
+      for (const auto &[Mode, RSI] : RC->RSI)
+        PartialMappings.emplace(RSI.RegSize);
+
+    for (const auto *IgnoreDef :
+         TheDef.getValueAsListOfDefs("IgnoredPartialMappings"))
+      PartialMappings.erase(*IgnoreDef);
+
+    // construct possible value mappings
+    // partial mappings in PartialMappings
+    size_t PartialMappingIdx = 0;
+    for (const auto &PM : PartialMappings) {
+      if (!PM.isSimple())
+        break;
+
+      ValueMapping VMs;
+      VMs.Parts.push_back(PM);
+      // find all possible break downs
+      size_t NextStartIdx = PM.Length;
+      size_t PartSize = PM.Length;
+      while (true) {
+        auto It = llvm::find_if(
+            PartialMappings, [NextStartIdx, PartSize](const PartialMapping PM) {
+              return PM.StartIdx == NextStartIdx && PM.Length == PartSize;
+            });
+        if (It == PartialMappings.end())
+          break;
+        VMs.Parts.push_back(*It);
+        NextStartIdx += PartSize;
+      }
+
+      for (auto &PM : VMs.Parts)
+        PM.Index = PartialMappingIdx++;
+      llvm::append_range(PartialMappingTable, VMs.Parts);
+      for (size_t I : llvm::seq(VMs.Parts.size())) {
+        ArrayRef<PartialMapping> PMs = VMs.Parts;
+        ValueMapping VM;
+        VM.Parts.reserve(I + 1);
+        llvm::append_range(VM.Parts, PMs.take_front(I + 1));
+        ValueMappings.push_back(std::move(VM));
+      }
+    }
+  }
+
+  void setPartialMappingIdxStart(size_t IdxStart) {
+    for (auto &PM : PartialMappingTable)
+      PM.Index += IdxStart;
+    for (auto &VM : ValueMappings)
+      for (auto &PM : VM.Parts)
+        PM.Index += IdxStart;
+  }
+
   /// Get the register classes listed in the RegisterBank.RegisterClasses field.
   std::vector<const CodeGenRegisterClass *>
   getExplicitlySpecifiedRegisterClasses(
@@ -106,17 +223,137 @@ class RegisterBank {
   }
 };
 
+struct TypedOperandInfo {
+  const Record *Def = nullptr;
+  unsigned Index;
+  TypedOperandInfo() = default;
+  TypedOperandInfo(const Record &Def) : Def(&Def) {
+    Def.getValueAsString("OperandType")
+        .drop_until(llvm::isDigit)
+        .getAsInteger(10, Index);
+  }
+  bool isPtr() const { return Def->getValueAsBit("IsPointer"); }
+  bool isImm() const { return Def->getValueAsBit("IsImmediate"); }
+
+  std::string getLLTName() const {
+    assert(!isImm() && "not a reg");
+    return (isPtr() ? "PType" : "Type") + llvm::itostr(Index);
+  }
+};
+
+using TypedOperandInfoMap = DenseMap<const Record *, TypedOperandInfo>;
+
+struct InstrShape {
+  // map operand num to type index, -1 means a non TypedOperand or imm
+  // TypedOperand
+  std::vector<int> Shape;
+  std::vector<const Record *> OperandTypes;
+  std::vector<int> RepresentativeOperands;
+  InstrShape(const CodeGenInstruction &CGI, const TypedOperandInfoMap &TOI) {
+    OperandTypes.resize(CGI.Operands.size());
+    RepresentativeOperands.resize(CGI.Operands.size(), -1);
+    for (const auto &[OperandIdx, Operand] : llvm::enumerate(CGI.Operands)) {
+      const Record *OpRec = Operand.Rec;
+      auto It = TOI.find(OpRec);
+      if (It != TOI.end()) {
+        const TypedOperandInfo Info = It->second;
+        if (!Info.isImm()) {
+          unsigned TypeIdx = Info.Index;
+          Shape.push_back(TypeIdx);
+          OperandTypes[TypeIdx] = OpRec;
+          if (RepresentativeOperands[TypeIdx] == -1)
+            RepresentativeOperands[TypeIdx] = OperandIdx;
+          continue;
+        }
+      }
+      Shape.push_back(-1);
+    }
+
+    while (!OperandTypes.empty() && OperandTypes.back() == nullptr)
+      OperandTypes.pop_back();
+    while (!RepresentativeOperands.empty() &&
+           RepresentativeOperands.back() == -1)
+      RepresentativeOperands.pop_back();
+  }
+
+  size_t getNumOperands() const { return Shape.size(); }
+  size_t getTypeTupleSize() const { return OperandTypes.size(); }
+
+  bool operator==(const InstrShape &RHS) const {
+    return std::tie(OperandTypes, Shape) ==
+           std::tie(RHS.OperandTypes, RHS.Shape);
+  }
+  bool operator<(const InstrShape &RHS) const {
+    return std::tie(OperandTypes, Shape) <
+           std::tie(RHS.OperandTypes, RHS.Shape);
+  }
+};
+
+template <typename T>
+static void getCartesianProductImpl(
+    std::vector<std::vector<T>> &Result, std::vector<T> &Current,
+    typename std::vector<std::vector<T>>::const_iterator It,
+    typename std::vector<std::vector<T>>::const_iterator End) {
+  if (It == End) {
+    Result.push_back(Current);
+    return;
+  }
+
+  // need an easy name for my vector-of-ints
+  const auto &ItVec = *It;
+  for (auto I = ItVec.begin(), E = ItVec.end(); I != E; ++I) {
+    Current.push_back(*I);
+    getCartesianProductImpl(Result, Current, It + 1, End);
+    Current.pop_back();
+  }
+}
+
+template <typename T>
+static std::vector<std::vector<T>>
+getCartesianProduct(const std::vector<std::vector<T>> &Components) {
+  std::vector<std::vector<T>> Result;
+  std::vector<T> ResultTemp;
+  getCartesianProductImpl(Result, ResultTemp, Components.begin(),
+                          Components.end());
+  return Result;
+}
+
 class RegisterBankEmitter {
 private:
   const CodeGenTarget Target;
   const RecordKeeper &Records;
 
+  std::vector<RegisterBank> Banks;
+
+  TypedOperandInfoMap TypedOperands;
+  std::map<InstrShape, std::set<const CodeGenInstruction *>> Shapes;
+  DenseMap<const InstrShape *, std::set<std::vector<const RegisterBank *>>>
+      BankAssignments;
+  DenseMap<const CodeGenInstruction *, const InstrShape *> CGIToShapeMap;
+  DenseMap<const CodeGenInstruction *, std::vector<const Record *>>
+      InstrMappingRules;
+  DenseMap<const InstrShape *,
+           std::map<std::vector<const RegisterBank *>, size_t>>
+      ShapeToTupleStart;
+
+  void collectValueMappings();
+  void validateMappingRules();
+  void collectInstrMappings();
+
   void emitHeader(raw_ostream &OS, StringRef TargetName,
                   ArrayRef<RegisterBank> Banks);
   void emitBaseClassDefinition(raw_ostream &OS, StringRef TargetName,
                                ArrayRef<RegisterBank> Banks);
   void emitBaseClassImplementation(raw_ostream &OS, StringRef TargetName,
                                    ArrayRef<RegisterBank> Banks);
+  std::vector<const RegisterBank *> getBankAssignment(const Record &RuleRec,
+                                                      const InstrShape &Shape);
+  void emitOperandsMappingRules(raw_ostream &OS, const CodeGenInstruction &CGI,
+                                const InstrShape &Shape,
+                                const std::vector<const Record *> Rules);
+  void emitOperandMappingForShape(raw_ostream &OS, size_t ShapeIdx,
+                                  const InstrShape &Shape);
+  void emitSearchMethod(raw_ostream &OS, StringRef TargetName);
 
 public:
   RegisterBankEmitter(const RecordKeeper &R) : Target(R), Records(R) {}
@@ -157,7 +394,17 @@ void RegisterBankEmitter::emitBaseClassDefinition(
         "TargetRegisterClass &RC, LLT Ty) const override;\n"
      << "protected:\n"
      << "  " << TargetName << "GenRegisterBankInfo(unsigned HwMode = 0);\n"
-     << "\n";
+     << "\n"
+     << "  const InstructionMapping &getGeneratedInstrMapping(const "
+        "MachineInstr &MI) const;\n";
+  for (const auto &MappingIDDef :
+       Records.getAllDerivedDefinitions("RegBankMappingID")) {
+    StringRef Name = MappingIDDef->getValueAsString("Name");
+    if (Name == "DefaultMappingID" || Name == "InvalidMappingID")
+      continue;
+    OS << "  static inline constexpr unsigned " << Name << " = "
+       << MappingIDDef->getValueAsInt("ID") << ";\n";
+  }
 }
 
 /// Visit each register class belonging to the given register bank.
@@ -218,6 +465,94 @@ static void visitRegisterBankClasses(
   }
 }
 
+void RegisterBankEmitter::collectValueMappings() {
+  size_t PartialMappingIdxStart = 0;
+  for (auto &Bank : Banks) {
+    Bank.collectMappings();
+    Bank.setPartialMappingIdxStart(PartialMappingIdxStart);
+    PartialMappingIdxStart += Bank.getPartialMappingTable().size();
+  }
+}
+
+void RegisterBankEmitter::validateMappingRules() {
+  for (const auto *Rec :
+       Records.getAllDerivedDefinitions("RegBankInstrMapping")) {
+    std::vector<const Record *> Opcodes = Rec->getValueAsListOfDefs("Opcodes");
+    assert(Opcodes.size() > 0 && "size assertion in RegBank.td failed");
+    const CodeGenInstruction &CGI = Target.getInstruction(Opcodes[0]);
+    if (!CGIToShapeMap.contains(&CGI))
+      PrintFatalError("Unsupported instruction " + CGI.getName());
+    InstrShape Shape(CGI, TypedOperands);
+    if (!llvm::all_of(Opcodes, [&](const Record *OpDef) {
+          return Shape ==
+                 InstrShape(Target.getInstruction(OpDef), TypedOperands);
+        }))
+      PrintFatalError(Rec, "Not all instructions have the same shape.");
+
+    for (const auto *RuleRec : Rec->getValueAsListOfDefs("Rules")) {
+      std::vector<const Record *> MappingRecs =
+          RuleRec->getValueAsListOfDefs("Mappings");
+      SetVector<const Record *> MappedTypes;
+      for (const auto *MappingRec : MappingRecs) {
+        if (!MappingRec->isSubClassOf("RegBankTypeMapping"))
+          continue;
+        const Record *TypedOperandRec = MappingRec->getValueAsDef("Type");
+        if (llvm::find(Shape.OperandTypes, TypedOperandRec) ==
+            Shape.OperandTypes.end())
+          PrintFatalError(MappingRec,
+                          "unexpected TypedOperand for Instruction " +
+                              CGI.getName());
+        MappedTypes.insert(TypedOperandRec);
+      }
+      for (const auto *Rec : Shape.OperandTypes)
+        if (!MappedTypes.contains(Rec))
+          PrintFatalError(RuleRec, Rec->getName() + " is not mapped in rules");
+    }
+  }
+}
+
+void RegisterBankEmitter::collectInstrMappings() {
+  // Firstly, collect instruction shapes
+  // we are only interested in generic instructions with fixed num of operands.
+  for (const auto &CGI : llvm::make_pointee_range(Target.getInstructions())) {
+    if (!CGI.TheDef->isSubClassOf("GenericInstruction") ||
+        CGI.Operands.empty() || CGI.Operands.isVariadic)
+      continue;
+    if (llvm::all_of(CGI.Operands, [](const CGIOperandList::OperandInfo &Op) {
+          return !Op.Rec->isSubClassOf("TypedOperand");
+        }))
+      continue;
+    InstrShape Shape(CGI, TypedOperands);
+    auto It = Shapes.try_emplace(Shape).first;
+    It->second.insert(&CGI);
+    CGIToShapeMap[&CGI] = &It->first;
+  }
+
+  // validate all rules
+  validateMappingRules();
+
+  for (const auto *Rec :
+       Records.getAllDerivedDefinitions("RegBankInstrMapping")) {
+    std::vector<const Record *> Opcodes = Rec->getValueAsListOfDefs("Opcodes");
+    const CodeGenInstruction &CGI = Target.getInstruction(Opcodes[0]);
+    const InstrShape *Shape = CGIToShapeMap[&CGI];
+    for (const auto *Opc : Opcodes) {
+      std::vector<const Record *> &Rules =
+          InstrMappingRules[&Target.getInstruction(Opc)];
+      for (const auto *RuleDef : Rec->getValueAsListOfDefs("Rules"))
+        Rules.push_back(RuleDef);
+    }
+    for (const auto *RuleDef : Rec->getValueAsListOfDefs("Rules"))
+      BankAssignments[Shape].insert(getBankAssignment(*RuleDef, *Shape));
+  }
+
+  for (auto &KV : InstrMappingRules) {
+    llvm::stable_sort(KV.second, [](const Record *LHS, const Record *RHS) {
+      return LHS->getValueAsInt("Priority") > RHS->getValueAsInt("Priority");
+    });
+  }
+}
+
 void RegisterBankEmitter::emitBaseClassImplementation(
     raw_ostream &OS, StringRef TargetName, ArrayRef<RegisterBank> Banks) {
   const CodeGenRegBank &RegisterClassHierarchy = Target.getRegBank();
@@ -260,6 +595,86 @@ void RegisterBankEmitter::emitBaseClassImplementation(
          << Bank.getCoverageArrayName() << ", /* NumRegClasses */ "
          << RegisterClassHierarchy.getRegClasses().size() << ");\n";
     }
+
+    // operands mappings
+    OS << "\nstatic constexpr RegisterBankInfo::PartialMapping\n"
+          "GeneratedPartialMappings[] = {\n"
+          "  // StartIdx, Length, RegBank\n";
+    for (const auto &Bank : Banks) {
+      OS << '\n';
+      for (const auto &Mapping : Bank.getPartialMappingTable())
+        OS << "  {" << Mapping.StartIdx << ", " << Mapping.Length << ", "
+           << Bank.getInstanceVarName() << "}, // " << Mapping.Index << '\n';
+    }
+    OS << "};\n\n";
+
+    for (const auto &Bank : Banks) {
+      OS << "static constexpr RegisterBankInfo::ValueMapping\n"
+         << "Generated" << Bank.getName() << "ValueMappings[] = {\n";
+      for (const auto &Mapping : Bank.getValueMappings())
+        OS << "  {&GeneratedPartialMappings[" << Mapping.getPartialMappingIdx()
+           << "], " << Mapping.getNumBreakDowns() << "},\n";
+      OS << "};\n\n";
+    }
+
+    for (const auto &[ShapeIdx, KV] : llvm::enumerate(Shapes)) {
+      const auto &[Shape, CGIs] = KV;
+
+      emitOperandMappingForShape(OS, ShapeIdx, Shape);
+    }
+
+    OS << "static size_t getValueMappingIdx(unsigned BankID, const TypeSize "
+          "&Size, unsigned NumBreakDowns = 1) {\n";
+    OS << "  switch (BankID) {\n";
+    for (auto &Bank : Banks) {
+      std::vector<ValueMapping> ValueMappings = Bank.getValueMappings();
+      OS << "    case " << Bank.getEnumeratorName() << ": {\n";
+      size_t ScalableMappingSize = Bank.getScalableMappingSize();
+      OS << "      uint64_t S = Size";
+      if (ScalableMappingSize == 0) {
+        OS << ";\n";
+      } else {
+        OS << ".getKnownMinValue();\n";
+        OS << "      if (Size.isScalable()) \n"
+              "        return ";
+        auto It = llvm::find_if(ValueMappings,
+                                [ScalableMappingSize](const ValueMapping &VM) {
+                                  return VM.getSize() == ScalableMappingSize &&
+                                         VM.getNumBreakDowns() == 1;
+                                });
+        OS << std::distance(ValueMappings.begin(), It) << ";\n";
+      }
+      for (const auto &[Idx, VM] : llvm::enumerate(ValueMappings)) {
+        OS << "      if (S <= " << VM.getSize()
+           << " && NumBreakDowns == " << VM.getNumBreakDowns() << ") \n"
+           << "        return " << Idx << ";\n";
+      }
+
+      OS << "      break;\n"
+            "    }\n";
+    }
+    OS << "  }\n"
+          "  return -1;\n"
+          "}\n\n";
+
+    OS << "[[maybe_unused]] static const RegisterBankInfo::ValueMapping *\n"
+          "getGeneratedValueMapping(unsigned BankID, const TypeSize &Size, "
+          "unsigned NumBreakDowns = 1) {\n"
+          "  static const RegisterBankInfo::ValueMapping InvalidMapping;\n"
+          "  size_t Idx = getValueMappingIdx(BankID, Size, NumBreakDowns);\n"
+          "  if (Idx == (size_t)-1) \n"
+          "    return &InvalidMapping;\n\n"
+          "  switch (BankID) {\n"
+          "    default:\n"
+          "      return &InvalidMapping;\n";
+    for (const auto &Bank : Banks) {
+      OS << "    case " << Bank.getEnumeratorName() << ":\n"
+         << "      return &Generated" << Bank.getName()
+         << "ValueMappings[Idx];\n";
+    }
+    OS << "  }\n";
+
+    OS << "}\n\n";
   } // End target namespace.
 
   OS << "\nconst RegisterBank *" << TargetName
@@ -377,6 +792,186 @@ void RegisterBankEmitter::emitBaseClassImplementation(
         "class ID "
         "0x\").concat(llvm::Twine::utohexstr(RegClassID)).str().c_str());\n"
         "}\n";
+
+  emitSearchMethod(OS, TargetName);
+}
+
+std::vector<const RegisterBank *>
+RegisterBankEmitter::getBankAssignment(const Record &RuleRec,
+                                       const InstrShape &Shape) {
+  std::vector<const RegisterBank *> Assignment(Shape.getTypeTupleSize());
+  for (const auto *MappingDef : RuleRec.getValueAsListOfDefs("Mappings")) {
+    const Record *BankDef = MappingDef->getValueAsDef("Bank");
+    const RegisterBank *Bank =
+        &*llvm::find_if(Banks, [BankDef](const RegisterBank &Bank) {
+          return &Bank.getDef() == BankDef;
+        });
+
+    Assignment[TypedOperands[MappingDef->getValueAsDef("Type")].Index] = Bank;
+  }
+  return Assignment;
+}
+
+void RegisterBankEmitter::emitOperandMappingForShape(raw_ostream &OS,
+                                                     size_t ShapeIdx,
+                                                     const InstrShape &Shape) {
+  OS << "static constexpr RegisterBankInfo::ValueMapping\n"
+        "GeneratedOperandsMappingsShape"
+     << ShapeIdx << "[] = {\n";
+  std::set<std::vector<const RegisterBank *>> &Assignments =
+      BankAssignments[&Shape];
+  if (Assignments.empty()) {
+    for (size_t I : llvm::seq(Shape.Shape.size()))
+      OS << "  {nullptr, 0}, " << "// " << I << '\n';
+    OS << "};\n\n";
+    return;
+  }
+
+  size_t Idx = 0;
+  std::map<std::vector<const RegisterBank *>, size_t> OffsetMap;
+  for (const auto &Assignment : Assignments) {
+    std::vector<std::vector<ValueMapping>> Components;
+    ListSeparator LS(" x ");
+    OS << "  // ";
+    for (const auto *Bank : Assignment)
+      OS << LS << Bank->getName();
+    OS << '\n';
+
+    for (const auto *Bank : Assignment)
+      Components.push_back(Bank->getValueMappings());
+
+    std::vector<std::vector<ValueMapping>> Product =
+        getCartesianProduct(Components);
+    OffsetMap[Assignment] = Idx;
+    for (const auto &Tup : Product) {
+      for (int I : Shape.Shape) {
+        if (I >= 0) {
+          const ValueMapping &VM = Tup[I];
+          OS << "  {&GeneratedPartialMappings[" << VM.getPartialMappingIdx()
+             << "], " << VM.getNumBreakDowns() << "}, // " << Idx++ << '\n';
+        } else {
+          OS << "  {nullptr, 0}, " << "// " << Idx++ << '\n';
+        }
+      }
+      OS << '\n';
+    }
+  }
+  OS << "};\n";
+  ShapeToTupleStart[&Shape] = OffsetMap;
+}
+
+void RegisterBankEmitter::emitOperandsMappingRules(
+    raw_ostream &OS, const CodeGenInstruction &CGI, const InstrShape &Shape,
+    const std::vector<const Record *> Rules) {
+  std::vector<std::string> LLTSizeNames;
+  for (int I : Shape.RepresentativeOperands) {
+    std::string RegName = CGI.Operands[I].Name.str() + "Reg";
+    RegName[0] = std::toupper(RegName[0]);
+    OS << "      Register " << RegName << " = MI.getOperand(" << I
+       << ").getReg();\n";
+    const TypedOperandInfo &TyOpInfo = TypedOperands[CGI.Operands[I].Rec];
+    std::string LLTName = TyOpInfo.getLLTName();
+    OS << "      LLT " << LLTName << " = MRI.getType(" << RegName << ");\n";
+    std::string LLTSizeName = LLTName + "Size";
+    OS << "      TypeSize " << LLTSizeName << " = " << LLTName
+       << ".getSizeInBits();\n";
+    LLTSizeNames.push_back(LLTSizeName);
+  }
+  OS << '\n';
+
+  for (const auto *RuleDef : Rules) {
+    if (StringRef Pred = RuleDef->getValueAsString("Predicate"); !Pred.empty())
+      OS << "      if (" << Pred << ") {\n";
+    else
+      OS << "      {\n";
+
+    std::vector<const RegisterBank *> BankAssignment =
+        getBankAssignment(*RuleDef, Shape);
+    OS << "        ArrayRef<ValueMapping> OperandsMappings = "
+          "OperandsMappingStart.drop_front("
+       << ShapeToTupleStart[&Shape][BankAssignment] << ");\n";
+    OS << "        std::array Coord = {";
+    StringRef TargetName = Target.getName();
+    for (const auto &[Idx, Bank] : llvm::enumerate(BankAssignment)) {
+      OS << TargetName << "::getValueMappingIdx(" << TargetName
+         << "::" << Bank->getEnumeratorName() << ", " << LLTSizeNames[Idx]
+         << "),";
+    }
+    OS << "};\n";
+    OS << "        if (llvm::any_of(Coord, [](size_t S) { return S == "
+          "(size_t)-1; }))\n"
+          "          break;\n";
+    // decode instruction mapping position
+    ListSeparator Plus(" + ");
+    OS << "        size_t Idx = (";
+    for (const auto &[Idx, Bank] : llvm::enumerate(BankAssignment)) {
+      OS << Plus << "Coord[" << Idx << "] * "
+         << llvm::accumulate(ArrayRef(BankAssignment).drop_front(Idx + 1), 1,
+                             [](size_t LHS, const RegisterBank *RHS) {
+                               return LHS * RHS->getValueMappings().size();
+                             });
+    }
+    OS << ") * " << Shape.getNumOperands() << ";\n";
+    StringRef MapID = RuleDef->getValueAsDef("ID")->getValueAsString("Name");
+    unsigned Cost = RuleDef->getValueAsInt("Cost");
+    OS << "        return getInstructionMapping(" << MapID << ", " << Cost
+       << ", "
+          "&OperandsMappings[Idx], "
+       << Shape.getNumOperands() << ");\n";
+
+    OS << "      }\n\n";
+  }
+}
+
+void RegisterBankEmitter::emitSearchMethod(raw_ostream &OS,
+                                           StringRef TargetName) {
+  OS << '\n';
+  OS << "const RegisterBankInfo::InstructionMapping &\n"
+     << TargetName
+     << "GenRegisterBankInfo::getGeneratedInstrMapping(const "
+        "MachineInstr &MI) const {\n";
+  OS << "  const MachineFunction &MF = *MI.getParent()->getParent();\n"
+        "  [[maybe_unused]] const MachineRegisterInfo &MRI = "
+        "MF.getRegInfo();\n";
+  OS << "  const TargetSubtargetInfo &STI = MF.getSubtarget();\n"
+        "  [[maybe_unused]] const TargetRegisterInfo &TRI = "
+        "*STI.getRegisterInfo();\n\n";
+  OS << "  const unsigned Opc = MI.getOpcode();\n\n";
+
+  OS << "  switch(Opc) {\n"
+        "    default:\n"
+        "      break;\n";
+
+  for (const auto &[ShapeIdx, ShapeCGIs] : llvm::enumerate(Shapes)) {
+    const auto &[Shape, CGIs] = ShapeCGIs;
+    // instruction with same rules, cluster them in the same case body
+    std::map<std::vector<const Record *>, std::set<const CodeGenInstruction *>>
+        GroupedInstrMappingRules;
+    for (auto *CGI : CGIs) {
+      auto It = InstrMappingRules.find(CGI);
+      if (It == InstrMappingRules.end())
+        continue;
+      std::vector<const Record *> &Rules = It->second;
+      GroupedInstrMappingRules[Rules].insert(CGI);
+    }
+
+    for (const auto &[Rules, CGIs] : GroupedInstrMappingRules) {
+      for (const auto *CGI : CGIs)
+        OS << "\n    case " << TargetName << "::" << CGI->getName() << ":";
+      OS << " {\n"
+            "      ArrayRef<ValueMapping> OperandsMappingStart = "
+         << TargetName << "::GeneratedOperandsMappingsShape" << ShapeIdx
+         << ";\n";
+      emitOperandsMappingRules(OS, **CGIs.begin(), Shape, Rules);
+      OS << "      break;\n"
+            "    }\n";
+    }
+  }
+
+  OS << "  }\n"
+        "  return getInvalidInstructionMapping();\n";
+
+  OS << "}\n";
 }
 
 void RegisterBankEmitter::run(raw_ostream &OS) {
@@ -386,7 +981,6 @@ void RegisterBankEmitter::run(raw_ostream &OS) {
 
   TGTimer &Timer = Records.getTimer();
   Timer.startTimer("Analyze records");
-  std::vector<RegisterBank> Banks;
   for (const auto &V : Records.getAllDerivedDefinitions("RegisterBank")) {
     DenseSet<const CodeGenRegisterClass *> VisitedRCs;
     RegisterBank Bank(*V, CGH.getNumModeIds());
@@ -409,6 +1003,12 @@ void RegisterBankEmitter::run(raw_ostream &OS) {
   if (Banks.empty())
     PrintFatalError("No register banks defined");
 
+  for (const auto *Op : Records.getAllDerivedDefinitions("TypedOperand"))
+    TypedOperands[Op] = TypedOperandInfo(*Op);
+
+  collectValueMappings();
+  collectInstrMappings();
+
   // Warn about ambiguous MIR caused by register bank/class name clashes.
   Timer.startTimer("Warn ambiguous");
   for (const auto &Class : RegisterClassHierarchy.getRegClasses()) {

>From 0cabb2da0453c30e5f6e05b91a1f2ca365a76a6b Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Wed, 11 Nov 2026 14:51:21 +0800
Subject: [PATCH 2/2] [AArch64] Use generated instruction mappings

---
 .../AArch64/AArch64GenRegisterBankInfo.def    | 278 ----------
 .../Target/AArch64/AArch64RegisterBanks.td    | 137 ++++-
 .../AArch64/GISel/AArch64RegisterBankInfo.cpp | 495 ++++--------------
 .../AArch64/GISel/AArch64RegisterBankInfo.h   |  81 +--
 4 files changed, 226 insertions(+), 765 deletions(-)
 delete mode 100644 llvm/lib/Target/AArch64/AArch64GenRegisterBankInfo.def

diff --git a/llvm/lib/Target/AArch64/AArch64GenRegisterBankInfo.def b/llvm/lib/Target/AArch64/AArch64GenRegisterBankInfo.def
deleted file mode 100644
index bc15805d08626b..00000000000000
--- a/llvm/lib/Target/AArch64/AArch64GenRegisterBankInfo.def
+++ /dev/null
@@ -1,278 +0,0 @@
-//===- AArch64GenRegisterBankInfo.def ----------------------------*- C++ -*-==//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-/// \file
-/// This file defines all the static objects used by AArch64RegisterBankInfo.
-/// \todo This should be generated by TableGen.
-//===----------------------------------------------------------------------===//
-
-namespace llvm {
-const RegisterBankInfo::PartialMapping AArch64GenRegisterBankInfo::PartMappings[]{
-    /* StartIdx, Length, RegBank */
-    // 0: FPR 16-bit value.
-    {0, 16, AArch64::FPRRegBank},
-    // 1: FPR 32-bit value.
-    {0, 32, AArch64::FPRRegBank},
-    // 2: FPR 64-bit value.
-    {0, 64, AArch64::FPRRegBank},
-    // 3: FPR 128-bit value.
-    {0, 128, AArch64::FPRRegBank},
-    // 4: FPR 256-bit value.
-    {0, 256, AArch64::FPRRegBank},
-    // 5: FPR 512-bit value.
-    {0, 512, AArch64::FPRRegBank},
-    // 6: GPR 32-bit value.
-    {0, 32, AArch64::GPRRegBank},
-    // 7: GPR 64-bit value.
-    {0, 64, AArch64::GPRRegBank},
-    // 8: GPR 128-bit value.
-    {0, 128, AArch64::GPRRegBank},
-};
-
-// ValueMappings.
-const RegisterBankInfo::ValueMapping AArch64GenRegisterBankInfo::ValMappings[]{
-    /* BreakDown, NumBreakDowns */
-    // 0: invalid
-    {nullptr, 0},
-    // 3-operands instructions (all binary operations should end up with one of
-    // those mapping).
-    // 1: FPR 16-bit value. <-- This must match First3OpsIdx.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR16 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR16 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR16 - PMI_Min], 1},
-    // 4: FPR 32-bit value. <-- This must match First3OpsIdx.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR32 - PMI_Min], 1},
-    // 7: FPR 64-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR64 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR64 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR64 - PMI_Min], 1},
-    // 10: FPR 128-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR128 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR128 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR128 - PMI_Min], 1},
-    // 13: FPR 256-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR256 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR256 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR256 - PMI_Min], 1},
-    // 16: FPR 512-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR512 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR512 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR512 - PMI_Min], 1},
-    // 19: GPR 32-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR32 - PMI_Min], 1},
-    // 22: GPR 64-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR64 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR64 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR64 - PMI_Min], 1},
-    // 25: GPR 128-bit value. <-- This must match Last3OpsIdx.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR128 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR128 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR128 - PMI_Min], 1},
-    // Cross register bank copies.
-    // 28: FPR 16-bit value to GPR 16-bit. <-- This must match
-    //                                         FirstCrossRegCpyIdx.
-    // Note: This is the kind of copy we see with physical registers.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR16 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR32 - PMI_Min], 1},
-    // 30: FPR 32-bit value to GPR 32-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR32 - PMI_Min], 1},
-    // 32: FPR 64-bit value to GPR 64-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR64 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR64 - PMI_Min], 1},
-    // 34: FPR 128-bit value to GPR 128-bit value (invalid)
-    {nullptr, 1},
-    {nullptr, 1},
-    // 36: FPR 256-bit value to GPR 256-bit value (invalid)
-    {nullptr, 1},
-    {nullptr, 1},
-    // 38: FPR 512-bit value to GPR 512-bit value (invalid)
-    {nullptr, 1},
-    {nullptr, 1},
-    // 40: GPR 32-bit value to FPR 32-bit value.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR32 - PMI_Min], 1},
-    // 42: GPR 64-bit value to FPR 64-bit value. <-- This must match
-    //                                               LastCrossRegCpyIdx.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR64 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR64 - PMI_Min], 1},
-    // 44: FPExt: 16 to 32. <-- This must match FPExt16To32Idx.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR16 - PMI_Min], 1},
-    // 46: FPExt: 16 to 32. <-- This must match FPExt16To64Idx.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR64 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR16 - PMI_Min], 1},
-    // 48: FPExt: 32 to 64. <-- This must match FPExt32To64Idx.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR64 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR32 - PMI_Min], 1},
-    // 50: FPExt vector: 64 to 128. <-- This must match FPExt64To128Idx.
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR128 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_FPR64 - PMI_Min], 1},
-    // 52: Shift scalar with 64 bit shift imm
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR32 - PMI_Min], 1},
-    {&AArch64GenRegisterBankInfo::PartMappings[PMI_GPR64 - PMI_Min], 1},
-};
-
-bool AArch64GenRegisterBankInfo::checkPartialMap(unsigned Idx,
-                                                 unsigned ValStartIdx,
-                                                 unsigned ValLength,
-                                                 const RegisterBank &RB) {
-  const PartialMapping &Map = PartMappings[Idx - PartialMappingIdx::PMI_Min];
-  return Map.StartIdx == ValStartIdx && Map.Length == ValLength &&
-         Map.RegBank == &RB;
-}
-
-bool AArch64GenRegisterBankInfo::checkValueMapImpl(unsigned Idx,
-                                                   unsigned FirstInBank,
-                                                   unsigned Size,
-                                                   unsigned Offset) {
-  unsigned PartialMapBaseIdx = Idx - PartialMappingIdx::PMI_Min;
-  const ValueMapping &Map =
-      AArch64GenRegisterBankInfo::getValueMapping(
-        (PartialMappingIdx)FirstInBank,
-        TypeSize::getFixed(Size))[Offset];
-  return Map.BreakDown == &PartMappings[PartialMapBaseIdx] &&
-         Map.NumBreakDowns == 1;
-}
-
-bool AArch64GenRegisterBankInfo::checkPartialMappingIdx(
-    PartialMappingIdx FirstAlias, PartialMappingIdx LastAlias,
-    ArrayRef<PartialMappingIdx> Order) {
-  if (Order.front() != FirstAlias)
-    return false;
-  if (Order.back() != LastAlias)
-    return false;
-  if (Order.front() > Order.back())
-    return false;
-
-  PartialMappingIdx Previous = Order.front();
-  for (const auto &Current : Order.drop_front()) {
-    if (Previous + 1 != Current)
-      return false;
-    Previous = Current;
-  }
-  return true;
-}
-
-unsigned AArch64GenRegisterBankInfo::getRegBankBaseIdxOffset(unsigned RBIdx,
-                                                             TypeSize Size) {
-  if (RBIdx == PMI_FirstGPR) {
-    if (Size <= 32)
-      return 0;
-    if (Size <= 64)
-      return 1;
-    if (Size <= 128)
-      return 2;
-    return -1;
-  }
-  if (RBIdx == PMI_FirstFPR) {
-    const unsigned MinSize = Size.getKnownMinValue();
-    assert((!Size.isScalable() || MinSize >= 128) &&
-           "Scalable vector types should have size of at least 128 bits");
-    if (Size.isScalable())
-      return 3;
-    if (MinSize <= 16)
-      return 0;
-    if (MinSize <= 32)
-      return 1;
-    if (MinSize <= 64)
-      return 2;
-    if (MinSize <= 128)
-      return 3;
-    if (MinSize <= 256)
-      return 4;
-    if (MinSize <= 512)
-      return 5;
-    return -1;
-  }
-  return -1;
-}
-
-const RegisterBankInfo::ValueMapping *
-AArch64GenRegisterBankInfo::getValueMapping(PartialMappingIdx RBIdx,
-                                            const TypeSize Size) {
-  assert(RBIdx != PartialMappingIdx::PMI_None && "No mapping needed for that");
-  unsigned BaseIdxOffset = getRegBankBaseIdxOffset(RBIdx, Size);
-  if (BaseIdxOffset == -1u)
-    return &ValMappings[InvalidIdx];
-
-  unsigned ValMappingIdx =
-      First3OpsIdx + (RBIdx - PartialMappingIdx::PMI_Min + BaseIdxOffset) *
-                         ValueMappingIdx::DistanceBetweenRegBanks;
-  assert(ValMappingIdx >= First3OpsIdx && ValMappingIdx <= Last3OpsIdx &&
-         "Mapping out of bound");
-
-  return &ValMappings[ValMappingIdx];
-}
-
-const AArch64GenRegisterBankInfo::PartialMappingIdx
-    AArch64GenRegisterBankInfo::BankIDToCopyMapIdx[]{
-        PMI_None,     // CCR
-        PMI_FirstFPR, // FPR
-        PMI_FirstGPR, // GPR
-    };
-
-const RegisterBankInfo::ValueMapping *
-AArch64GenRegisterBankInfo::getCopyMapping(unsigned DstBankID,
-                                           unsigned SrcBankID,
-                                           const TypeSize Size) {
-  assert(DstBankID < AArch64::NumRegisterBanks && "Invalid bank ID");
-  assert(SrcBankID < AArch64::NumRegisterBanks && "Invalid bank ID");
-  PartialMappingIdx DstRBIdx = BankIDToCopyMapIdx[DstBankID];
-  PartialMappingIdx SrcRBIdx = BankIDToCopyMapIdx[SrcBankID];
-  assert(DstRBIdx != PMI_None && "No such mapping");
-  assert(SrcRBIdx != PMI_None && "No such mapping");
-
-  if (DstRBIdx == SrcRBIdx)
-    return getValueMapping(DstRBIdx, Size);
-
-  assert(Size <= 64 && "GPR cannot handle that size");
-  unsigned ValMappingIdx =
-      FirstCrossRegCpyIdx +
-      (DstRBIdx - PMI_Min + getRegBankBaseIdxOffset(DstRBIdx, Size)) *
-          ValueMappingIdx::DistanceBetweenCrossRegCpy;
-  assert(ValMappingIdx >= FirstCrossRegCpyIdx &&
-         ValMappingIdx <= LastCrossRegCpyIdx && "Mapping out of bound");
-  return &ValMappings[ValMappingIdx];
-}
-
-const RegisterBankInfo::ValueMapping *
-AArch64GenRegisterBankInfo::getFPExtMapping(unsigned DstSize,
-                                         unsigned SrcSize) {
-  // We support:
-  // - For Scalar:
-  //   - 16 to 32.
-  //   - 16 to 64.
-  //   - 32 to 64.
-  // => FPR 16 to FPR 32|64
-  // => FPR 32 to FPR 64
-  // - For vectors:
-  //   - v4f16 to v4f32
-  //   - v2f32 to v2f64
-  // => FPR 64 to FPR 128
-
-  // Check that we have been asked sensible sizes.
-  if (SrcSize == 16) {
-    assert((DstSize == 32 || DstSize == 64) && "Unexpected half extension");
-    if (DstSize == 32)
-      return &ValMappings[FPExt16To32Idx];
-    return &ValMappings[FPExt16To64Idx];
-  }
-
-  if (SrcSize == 32) {
-    assert(DstSize == 64 && "Unexpected float extension");
-    return &ValMappings[FPExt32To64Idx];
-  }
-  assert((SrcSize == 64 || DstSize == 128) && "Unexpected vector extension");
-  return &ValMappings[FPExt64To128Idx];
-}
-} // End llvm namespace.
diff --git a/llvm/lib/Target/AArch64/AArch64RegisterBanks.td b/llvm/lib/Target/AArch64/AArch64RegisterBanks.td
index 2b597b8606921e..e342df5fa46278 100644
--- a/llvm/lib/Target/AArch64/AArch64RegisterBanks.td
+++ b/llvm/lib/Target/AArch64/AArch64RegisterBanks.td
@@ -13,7 +13,142 @@
 def GPRRegBank : RegisterBank<"GPR", [XSeqPairsClass]>;
 
 /// Floating Point, Vector, Scalable Vector Registers: B, H, S, D, Q, Z.
-def FPRRegBank : RegisterBank<"FPR", [QQQQ, ZPR]>;
+def FPRRegBank : RegisterBank<"FPR", [QQQQ, ZPR]> {
+  let ScalableMappingSize = 128;
+  let IgnoredPartialMappings=[PartialMapping<8>, PartialMapping<192>, PartialMapping<384>];
+}
 
 /// Conditional register: NZCV.
 def CCRegBank : RegisterBank<"CC", [CCR]>;
+
+def RegBankCustomMappingID : RegBankMappingID<"CustomMappingID", 1>;
+
+def : RegBankInstrMapping<[
+    // G_{F|S|U}REM are not listed because they are not legal.
+    // Arithmetic ops.
+    G_ADD, G_SUB, G_MUL, G_SDIV, G_UDIV,
+    // Bitwise ops.
+    G_AND, G_OR, G_XOR,
+    // Floating point ops.
+    G_FADD, G_FSUB, G_FMUL, G_FDIV,
+    G_FMAXIMUM, G_FMINIMUM
+  ], [
+  RegBankMappingRule<[RegBankTypeMapping<FPRRegBank, type0>],
+    predicate=[{Type0.isVector() || isPreISelGenericFloatingPointOpcode(Opc)}]>,
+  RegBankMappingRule<[RegBankTypeMapping<GPRRegBank, type0>]>
+]>;
+
+def : RegBankInstrMapping<[G_PTR_ADD], [
+  RegBankMappingRule<[RegBankTypeMapping<FPRRegBank, ptype0>, RegBankTypeMapping<GPRRegBank, type1>],
+    predicate=[{PType0.isVector()}]>,
+  RegBankMappingRule<[RegBankTypeMapping<GPRRegBank, ptype0>, RegBankTypeMapping<GPRRegBank, type1>]>
+]>;
+
+def : RegBankInstrMapping<[G_FPEXT], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<FPRRegBank, type0>,
+    RegBankTypeMapping<FPRRegBank, type1>
+  ]>
+]>;
+
+def : RegBankInstrMapping<[
+    // Shifts.
+    G_SHL, G_LSHR, G_ASHR
+  ], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, type0>,
+    RegBankTypeMapping<GPRRegBank, type1>
+  ], predicate=[{Type0Size == 64 && Type1Size == 32}]>,
+  RegBankMappingRule<[
+      RegBankTypeMapping<FPRRegBank, type0>,
+      RegBankTypeMapping<FPRRegBank, type1>
+    ],
+    predicate=[{Type0.isVector()}]>,
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, type0>,
+    RegBankTypeMapping<GPRRegBank, type1>
+  ]>
+]>;
+
+def : RegBankInstrMapping<[G_CONSTANT], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, type0>
+  ], predicate=[{Type0.isPointer() || (Type0.isScalar() && Type0Size >= 32 && Type0Size <= 64)}]>,
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, type0>
+  ], id=RegBankCustomMappingID, predicate=[{Type0.isScalar() && Type0Size < 32}]>,
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, type0>
+  ]>
+]>;
+
+def : RegBankInstrMapping<[G_BRCOND], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, type0>
+  ]>
+]>;
+
+def : RegBankInstrMapping<[G_FRAME_INDEX], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, ptype0>
+  ]>
+]>;
+
+def : RegBankInstrMapping<[G_FCONSTANT], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, type0>,
+  ], id=RegBankCustomMappingID,
+      predicate=[{preferGPRForFPImm(MI, MRI, static_cast<const AArch64Subtarget&>(STI))}]>,
+  RegBankMappingRule<[
+    RegBankTypeMapping<FPRRegBank, type0>
+  ]>
+]>;
+
+def : RegBankInstrMapping<[G_FCMP], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<FPRRegBank, type0>,
+    RegBankTypeMapping<FPRRegBank, type1>,
+  ], predicate=[{Type0.isVector()}]>,
+  RegBankMappingRule<[
+    RegBankTypeMapping<GPRRegBank, type0>,
+    RegBankTypeMapping<FPRRegBank, type1>,
+  ]>
+]>;
+
+def : RegBankInstrMapping<[
+    G_VECREDUCE_FADD, G_VECREDUCE_FMUL, G_VECREDUCE_FMAX,
+    G_VECREDUCE_FMIN, G_VECREDUCE_FMAXIMUM, G_VECREDUCE_FMINIMUM, G_VECREDUCE_ADD,
+    G_VECREDUCE_MUL, G_VECREDUCE_AND, G_VECREDUCE_OR, G_VECREDUCE_XOR, G_VECREDUCE_SMAX,
+    G_VECREDUCE_SMIN, G_VECREDUCE_UMAX, G_VECREDUCE_UMIN
+  ], [
+  // Reductions produce a scalar value from a vector, the scalar should be on
+  // FPR bank.
+  RegBankMappingRule<[
+    RegBankTypeMapping<FPRRegBank, type0>,
+    RegBankTypeMapping<FPRRegBank, type1>
+  ]>
+]>;
+
+def : RegBankInstrMapping<[G_INSERT_VECTOR_ELT], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<FPRRegBank, type0>,
+    RegBankTypeMapping<FPRRegBank, type1>,
+    RegBankTypeMapping<GPRRegBank, type2>
+  ],
+    // The element may be either a GPR or FPR. Preserve that behaviour.
+    predicate=[{getRegBank(EltReg, MRI, TRI) == &AArch64::FPRRegBank}]>,
+  RegBankMappingRule<[
+    RegBankTypeMapping<FPRRegBank, type0>,
+    RegBankTypeMapping<GPRRegBank, type1>,
+    RegBankTypeMapping<GPRRegBank, type2>
+  ]>
+]>;
+
+def : RegBankInstrMapping<[G_VECREDUCE_SEQ_FADD, G_VECREDUCE_SEQ_FMUL], [
+  RegBankMappingRule<[
+    RegBankTypeMapping<FPRRegBank, type0>,
+    RegBankTypeMapping<FPRRegBank, type1>,
+    RegBankTypeMapping<FPRRegBank, type2>
+  ]>
+]>;
+
diff --git a/llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.cpp b/llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.cpp
index cf62075dfc6a00..365b4d63ce5368 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.cpp
+++ b/llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.cpp
@@ -40,190 +40,35 @@
 #include "llvm/Support/Threading.h"
 #include <cassert>
 
+static bool preferGPRForFPImm(const llvm::MachineInstr &MI,
+                              const llvm::MachineRegisterInfo &MRI,
+                              const llvm::AArch64Subtarget &STI);
+static bool preferGPRForFPImm(const llvm::MachineInstr &MI,
+                              const llvm::MachineRegisterInfo &MRI,
+                              const llvm::AArch64Subtarget &STI);
+
 #define GET_TARGET_REGBANK_IMPL
 #include "AArch64GenRegisterBank.inc"
 
-// This file will be TableGen'ed at some point.
-#include "AArch64GenRegisterBankInfo.def"
-
 using namespace llvm;
 using namespace MIPatternMatch;
 static const unsigned CustomMappingID = 1;
 
-AArch64RegisterBankInfo::AArch64RegisterBankInfo(
-    const TargetRegisterInfo &TRI) {
-  static llvm::once_flag InitializeRegisterBankFlag;
-
-  static auto InitializeRegisterBankOnce = [&]() {
-    // We have only one set of register banks, whatever the subtarget
-    // is. Therefore, the initialization of the RegBanks table should be
-    // done only once. Indeed the table of all register banks
-    // (AArch64::RegBanks) is unique in the compiler. At some point, it
-    // will get tablegen'ed and the whole constructor becomes empty.
-
-    const RegisterBank &RBGPR = getRegBank(AArch64::GPRRegBankID);
-    (void)RBGPR;
-    assert(&AArch64::GPRRegBank == &RBGPR &&
-           "The order in RegBanks is messed up");
-
-    const RegisterBank &RBFPR = getRegBank(AArch64::FPRRegBankID);
-    (void)RBFPR;
-    assert(&AArch64::FPRRegBank == &RBFPR &&
-           "The order in RegBanks is messed up");
-
-    const RegisterBank &RBCCR = getRegBank(AArch64::CCRegBankID);
-    (void)RBCCR;
-    assert(&AArch64::CCRegBank == &RBCCR &&
-           "The order in RegBanks is messed up");
-
-    // The GPR register bank is fully defined by all the registers in
-    // GR64all + its subclasses.
-    assert(RBGPR.covers(*TRI.getRegClass(AArch64::GPR32RegClassID)) &&
-           "Subclass not added?");
-    assert(getMaximumSize(RBGPR.getID()) == 128 &&
-           "GPRs should hold up to 128-bit");
-
-    // The FPR register bank is fully defined by all the registers in
-    // GR64all + its subclasses.
-    assert(RBFPR.covers(*TRI.getRegClass(AArch64::QQRegClassID)) &&
-           "Subclass not added?");
-    assert(RBFPR.covers(*TRI.getRegClass(AArch64::FPR64RegClassID)) &&
-           "Subclass not added?");
-    assert(getMaximumSize(RBFPR.getID()) == 512 &&
-           "FPRs should hold up to 512-bit via QQQQ sequence");
-
-    assert(RBCCR.covers(*TRI.getRegClass(AArch64::CCRRegClassID)) &&
-           "Class not added?");
-    assert(getMaximumSize(RBCCR.getID()) == 32 &&
-           "CCR should hold up to 32-bit");
-
-    // Check that the TableGen'ed like file is in sync we our expectations.
-    // First, the Idx.
-    assert(checkPartialMappingIdx(PMI_FirstGPR, PMI_LastGPR,
-                                  {PMI_GPR32, PMI_GPR64, PMI_GPR128}) &&
-           "PartialMappingIdx's are incorrectly ordered");
-    assert(checkPartialMappingIdx(PMI_FirstFPR, PMI_LastFPR,
-                                  {PMI_FPR16, PMI_FPR32, PMI_FPR64, PMI_FPR128,
-                                   PMI_FPR256, PMI_FPR512}) &&
-           "PartialMappingIdx's are incorrectly ordered");
-// Now, the content.
-// Check partial mapping.
-#define CHECK_PARTIALMAP(Idx, ValStartIdx, ValLength, RB)                      \
-  do {                                                                         \
-    assert(                                                                    \
-        checkPartialMap(PartialMappingIdx::Idx, ValStartIdx, ValLength, RB) && \
-        #Idx " is incorrectly initialized");                                   \
-  } while (false)
-
-    CHECK_PARTIALMAP(PMI_GPR32, 0, 32, RBGPR);
-    CHECK_PARTIALMAP(PMI_GPR64, 0, 64, RBGPR);
-    CHECK_PARTIALMAP(PMI_GPR128, 0, 128, RBGPR);
-    CHECK_PARTIALMAP(PMI_FPR16, 0, 16, RBFPR);
-    CHECK_PARTIALMAP(PMI_FPR32, 0, 32, RBFPR);
-    CHECK_PARTIALMAP(PMI_FPR64, 0, 64, RBFPR);
-    CHECK_PARTIALMAP(PMI_FPR128, 0, 128, RBFPR);
-    CHECK_PARTIALMAP(PMI_FPR256, 0, 256, RBFPR);
-    CHECK_PARTIALMAP(PMI_FPR512, 0, 512, RBFPR);
-
-// Check value mapping.
-#define CHECK_VALUEMAP_IMPL(RBName, Size, Offset)                              \
-  do {                                                                         \
-    assert(checkValueMapImpl(PartialMappingIdx::PMI_##RBName##Size,            \
-                             PartialMappingIdx::PMI_First##RBName, Size,       \
-                             Offset) &&                                        \
-           #RBName #Size " " #Offset " is incorrectly initialized");           \
-  } while (false)
-
-#define CHECK_VALUEMAP(RBName, Size) CHECK_VALUEMAP_IMPL(RBName, Size, 0)
-
-    CHECK_VALUEMAP(GPR, 32);
-    CHECK_VALUEMAP(GPR, 64);
-    CHECK_VALUEMAP(GPR, 128);
-    CHECK_VALUEMAP(FPR, 16);
-    CHECK_VALUEMAP(FPR, 32);
-    CHECK_VALUEMAP(FPR, 64);
-    CHECK_VALUEMAP(FPR, 128);
-    CHECK_VALUEMAP(FPR, 256);
-    CHECK_VALUEMAP(FPR, 512);
-
-// Check the value mapping for 3-operands instructions where all the operands
-// map to the same value mapping.
-#define CHECK_VALUEMAP_3OPS(RBName, Size)                                      \
-  do {                                                                         \
-    CHECK_VALUEMAP_IMPL(RBName, Size, 0);                                      \
-    CHECK_VALUEMAP_IMPL(RBName, Size, 1);                                      \
-    CHECK_VALUEMAP_IMPL(RBName, Size, 2);                                      \
-  } while (false)
-
-    CHECK_VALUEMAP_3OPS(GPR, 32);
-    CHECK_VALUEMAP_3OPS(GPR, 64);
-    CHECK_VALUEMAP_3OPS(GPR, 128);
-    CHECK_VALUEMAP_3OPS(FPR, 32);
-    CHECK_VALUEMAP_3OPS(FPR, 64);
-    CHECK_VALUEMAP_3OPS(FPR, 128);
-    CHECK_VALUEMAP_3OPS(FPR, 256);
-    CHECK_VALUEMAP_3OPS(FPR, 512);
-
-#define CHECK_VALUEMAP_CROSSREGCPY(RBNameDst, RBNameSrc, Size)                 \
-  do {                                                                         \
-    unsigned PartialMapDstIdx = PMI_##RBNameDst##Size - PMI_Min;               \
-    unsigned PartialMapSrcIdx = PMI_##RBNameSrc##Size - PMI_Min;               \
-    (void)PartialMapDstIdx;                                                    \
-    (void)PartialMapSrcIdx;                                                    \
-    const ValueMapping *Map = getCopyMapping(AArch64::RBNameDst##RegBankID,    \
-                                             AArch64::RBNameSrc##RegBankID,    \
-                                             TypeSize::getFixed(Size));        \
-    (void)Map;                                                                 \
-    assert(Map[0].BreakDown ==                                                 \
-               &AArch64GenRegisterBankInfo::PartMappings[PartialMapDstIdx] &&  \
-           Map[0].NumBreakDowns == 1 &&                                        \
-           #RBNameDst #Size " Dst is incorrectly initialized");                \
-    assert(Map[1].BreakDown ==                                                 \
-               &AArch64GenRegisterBankInfo::PartMappings[PartialMapSrcIdx] &&  \
-           Map[1].NumBreakDowns == 1 &&                                        \
-           #RBNameSrc #Size " Src is incorrectly initialized");                \
-                                                                               \
-  } while (false)
-
-    CHECK_VALUEMAP_CROSSREGCPY(GPR, GPR, 32);
-    CHECK_VALUEMAP_CROSSREGCPY(GPR, FPR, 32);
-    CHECK_VALUEMAP_CROSSREGCPY(GPR, GPR, 64);
-    CHECK_VALUEMAP_CROSSREGCPY(GPR, FPR, 64);
-    CHECK_VALUEMAP_CROSSREGCPY(FPR, FPR, 32);
-    CHECK_VALUEMAP_CROSSREGCPY(FPR, GPR, 32);
-    CHECK_VALUEMAP_CROSSREGCPY(FPR, FPR, 64);
-    CHECK_VALUEMAP_CROSSREGCPY(FPR, GPR, 64);
-
-#define CHECK_VALUEMAP_FPEXT(DstSize, SrcSize)                                 \
-  do {                                                                         \
-    unsigned PartialMapDstIdx = PMI_FPR##DstSize - PMI_Min;                    \
-    unsigned PartialMapSrcIdx = PMI_FPR##SrcSize - PMI_Min;                    \
-    (void)PartialMapDstIdx;                                                    \
-    (void)PartialMapSrcIdx;                                                    \
-    const ValueMapping *Map = getFPExtMapping(DstSize, SrcSize);               \
-    (void)Map;                                                                 \
-    assert(Map[0].BreakDown ==                                                 \
-               &AArch64GenRegisterBankInfo::PartMappings[PartialMapDstIdx] &&  \
-           Map[0].NumBreakDowns == 1 && "FPR" #DstSize                         \
-                                        " Dst is incorrectly initialized");    \
-    assert(Map[1].BreakDown ==                                                 \
-               &AArch64GenRegisterBankInfo::PartMappings[PartialMapSrcIdx] &&  \
-           Map[1].NumBreakDowns == 1 && "FPR" #SrcSize                         \
-                                        " Src is incorrectly initialized");    \
-                                                                               \
-  } while (false)
-
-    CHECK_VALUEMAP_FPEXT(32, 16);
-    CHECK_VALUEMAP_FPEXT(64, 16);
-    CHECK_VALUEMAP_FPEXT(64, 32);
-    CHECK_VALUEMAP_FPEXT(128, 64);
-
-    assert(verify(TRI) && "Invalid register bank information");
-  };
-
-  llvm::call_once(InitializeRegisterBankFlag, InitializeRegisterBankOnce);
+const RegisterBankInfo::ValueMapping *
+AArch64GenRegisterBankInfo::getCopyMapping(unsigned DstBankID,
+                                           unsigned SrcBankID,
+                                           const TypeSize Size) const {
+
+  const ValueMapping *DstMapping =
+      AArch64::getGeneratedValueMapping(DstBankID, Size);
+  const ValueMapping *SrcMapping =
+      AArch64::getGeneratedValueMapping(SrcBankID, Size);
+  return getOperandsMapping({DstMapping, SrcMapping});
 }
 
+AArch64RegisterBankInfo::AArch64RegisterBankInfo(
+    const TargetRegisterInfo &TRI) {}
+
 unsigned AArch64RegisterBankInfo::copyCost(const RegisterBank &A,
                                            const RegisterBank &B,
                                            const TypeSize Size) const {
@@ -277,11 +122,15 @@ AArch64RegisterBankInfo::getInstrAlternativeMappings(
     if (MI.getNumOperands() != 3)
       break;
     InstructionMappings AltMappings;
+    const ValueMapping *GPRVM =
+        AArch64::getGeneratedValueMapping(AArch64::GPRRegBankID, Size);
+    const ValueMapping *FPRVM =
+        AArch64::getGeneratedValueMapping(AArch64::FPRRegBankID, Size);
     const InstructionMapping &GPRMapping = getInstructionMapping(
-        /*ID*/ 1, /*Cost*/ 1, getValueMapping(PMI_FirstGPR, Size),
+        /*ID*/ 1, /*Cost*/ 1, getOperandsMapping({GPRVM, GPRVM, GPRVM}),
         /*NumOperands*/ 3);
     const InstructionMapping &FPRMapping = getInstructionMapping(
-        /*ID*/ 2, /*Cost*/ 1, getValueMapping(PMI_FirstFPR, Size),
+        /*ID*/ 2, /*Cost*/ 1, getOperandsMapping({FPRVM, FPRVM, FPRVM}),
         /*NumOperands*/ 3);
 
     AltMappings.push_back(&GPRMapping);
@@ -342,16 +191,18 @@ AArch64RegisterBankInfo::getInstrAlternativeMappings(
     const InstructionMapping &GPRMapping = getInstructionMapping(
         /*ID*/ 1, /*Cost*/ 1,
         getOperandsMapping(
-            {getValueMapping(PMI_FirstGPR, Size),
+            {AArch64::getGeneratedValueMapping(AArch64::GPRRegBankID, Size),
              // Addresses are GPR 64-bit.
-             getValueMapping(PMI_FirstGPR, TypeSize::getFixed(64))}),
+             AArch64::getGeneratedValueMapping(AArch64::GPRRegBankID,
+                                               TypeSize::getFixed(64))}),
         /*NumOperands*/ 2);
     const InstructionMapping &FPRMapping = getInstructionMapping(
         /*ID*/ 2, /*Cost*/ 1,
         getOperandsMapping(
-            {getValueMapping(PMI_FirstFPR, Size),
+            {AArch64::getGeneratedValueMapping(AArch64::FPRRegBankID, Size),
              // Addresses are GPR 64-bit.
-             getValueMapping(PMI_FirstGPR, TypeSize::getFixed(64))}),
+             AArch64::getGeneratedValueMapping(AArch64::GPRRegBankID,
+                                               TypeSize::getFixed(64))}),
         /*NumOperands*/ 2);
 
     AltMappings.push_back(&GPRMapping);
@@ -542,49 +393,6 @@ void AArch64RegisterBankInfo::applyMappingImpl(
   }
 }
 
-const RegisterBankInfo::InstructionMapping &
-AArch64RegisterBankInfo::getSameKindOfOperandsMapping(
-    const MachineInstr &MI) const {
-  const unsigned Opc = MI.getOpcode();
-  const MachineFunction &MF = *MI.getParent()->getParent();
-  const MachineRegisterInfo &MRI = MF.getRegInfo();
-
-  unsigned NumOperands = MI.getNumOperands();
-  assert(NumOperands <= 3 &&
-         "This code is for instructions with 3 or less operands");
-
-  LLT Ty = MRI.getType(MI.getOperand(0).getReg());
-  TypeSize Size = Ty.getSizeInBits();
-  bool IsFPR = Ty.isVector() || isPreISelGenericFloatingPointOpcode(Opc);
-
-  PartialMappingIdx RBIdx = IsFPR ? PMI_FirstFPR : PMI_FirstGPR;
-
-#ifndef NDEBUG
-  // Make sure all the operands are using similar size and type.
-  // Should probably be checked by the machine verifier.
-  // This code won't catch cases where the number of lanes is
-  // different between the operands.
-  // If we want to go to that level of details, it is probably
-  // best to check that the types are the same, period.
-  // Currently, we just check that the register banks are the same
-  // for each types.
-  for (unsigned Idx = 1; Idx != NumOperands; ++Idx) {
-    LLT OpTy = MRI.getType(MI.getOperand(Idx).getReg());
-    assert(
-        AArch64GenRegisterBankInfo::getRegBankBaseIdxOffset(
-            RBIdx, OpTy.getSizeInBits()) ==
-            AArch64GenRegisterBankInfo::getRegBankBaseIdxOffset(RBIdx, Size) &&
-        "Operand has incompatible size");
-    bool OpIsFPR = OpTy.isVector() || isPreISelGenericFloatingPointOpcode(Opc);
-    (void)OpIsFPR;
-    assert(IsFPR == OpIsFPR && "Operand has incompatible type");
-  }
-#endif // End NDEBUG.
-
-  return getInstructionMapping(DefaultMappingID, 1,
-                               getValueMapping(RBIdx, Size), NumOperands);
-}
-
 /// \returns true if a given intrinsic only uses and defines FPRs.
 static bool isFPIntrinsic(const MachineRegisterInfo &MRI,
                           const MachineInstr &MI) {
@@ -903,51 +711,16 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
       return Mapping;
   }
 
+  if (const InstructionMapping &Mapping = getGeneratedInstrMapping(MI);
+      Mapping.isValid())
+    return Mapping;
+
   const MachineFunction &MF = *MI.getParent()->getParent();
   const MachineRegisterInfo &MRI = MF.getRegInfo();
   const AArch64Subtarget &STI = MF.getSubtarget<AArch64Subtarget>();
   const AArch64RegisterInfo &TRI = *STI.getRegisterInfo();
 
   switch (Opc) {
-    // G_{F|S|U}REM are not listed because they are not legal.
-    // Arithmetic ops.
-  case TargetOpcode::G_ADD:
-  case TargetOpcode::G_SUB:
-  case TargetOpcode::G_PTR_ADD:
-  case TargetOpcode::G_MUL:
-  case TargetOpcode::G_SDIV:
-  case TargetOpcode::G_UDIV:
-    // Bitwise ops.
-  case TargetOpcode::G_AND:
-  case TargetOpcode::G_OR:
-  case TargetOpcode::G_XOR:
-    // Floating point ops.
-  case TargetOpcode::G_FADD:
-  case TargetOpcode::G_FSUB:
-  case TargetOpcode::G_FMUL:
-  case TargetOpcode::G_FDIV:
-  case TargetOpcode::G_FMAXIMUM:
-  case TargetOpcode::G_FMINIMUM:
-    return getSameKindOfOperandsMapping(MI);
-  case TargetOpcode::G_FPEXT: {
-    LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
-    LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
-    return getInstructionMapping(
-        DefaultMappingID, /*Cost*/ 1,
-        getFPExtMapping(DstTy.getSizeInBits(), SrcTy.getSizeInBits()),
-        /*NumOperands*/ 2);
-  }
-    // Shifts.
-  case TargetOpcode::G_SHL:
-  case TargetOpcode::G_LSHR:
-  case TargetOpcode::G_ASHR: {
-    LLT ShiftAmtTy = MRI.getType(MI.getOperand(2).getReg());
-    LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
-    if (ShiftAmtTy.getSizeInBits() == 64 && SrcTy.getSizeInBits() == 32)
-      return getInstructionMapping(DefaultMappingID, 1,
-                                   &ValMappings[Shift64Imm], 3);
-    return getSameKindOfOperandsMapping(MI);
-  }
   case TargetOpcode::G_BITCAST: {
     Register SrcReg = MI.getOperand(1).getReg();
     const RegisterBank *SrcRB = getRegBank(SrcReg, MRI, TRI);
@@ -999,26 +772,6 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
         // We only care about the mapping of the destination for COPY.
         /*NumOperands*/ Opc == TargetOpcode::G_BITCAST ? 2 : 1);
   }
-  case TargetOpcode::G_CONSTANT: {
-    LLT DstTy = MRI.getType(MI.getOperand(0).getReg());
-    TypeSize Size = DstTy.getSizeInBits();
-    if (!DstTy.isPointer() && (!DstTy.isScalar() || Size < 32 || Size > 64))
-      break;
-    // Scalar constants materialize in GPRs.
-    [[fallthrough]];
-  }
-  case TargetOpcode::G_BRCOND:
-  case TargetOpcode::G_FRAME_INDEX: {
-    // Operand 0 is the only banked operand and is mapped to GPR.
-    return getInstructionMapping(
-        DefaultMappingID, /*Cost=*/1,
-        getOperandsMapping(
-            {getValueMapping(
-                 PMI_FirstGPR,
-                 MRI.getType(MI.getOperand(0).getReg()).getSizeInBits()),
-             nullptr}),
-        /*NumOperands=*/2);
-  }
   default:
     break;
   }
@@ -1028,7 +781,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
 
   // Track the size and bank of each register.  We don't do partial mappings.
   SmallVector<unsigned, 4> OpSize(NumOperands);
-  SmallVector<PartialMappingIdx, 4> OpRegBankIdx(NumOperands);
+  SmallVector<unsigned, 4> OpRegBankIdx(NumOperands);
   for (unsigned Idx = 0; Idx < NumOperands; ++Idx) {
     auto &MO = MI.getOperand(Idx);
     if (!MO.isReg() || !MO.getReg())
@@ -1043,59 +796,44 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
     // ones go in FPRs, scalars and pointers in GPRs.
     // For floating-point instructions, scalars go in FPRs.
     if (Ty.isVector())
-      OpRegBankIdx[Idx] = PMI_FirstFPR;
+      OpRegBankIdx[Idx] = AArch64::FPRRegBankID;
     else if (isPreISelGenericFloatingPointOpcode(Opc) ||
              (MO.isDef() && onlyDefinesFP(MI, MRI, TRI)) ||
              (MO.isUse() && onlyUsesFP(MI, MRI, TRI)) ||
              Ty.getSizeInBits() > 64)
-      OpRegBankIdx[Idx] = PMI_FirstFPR;
+      OpRegBankIdx[Idx] = AArch64::FPRRegBankID;
     else
-      OpRegBankIdx[Idx] = PMI_FirstGPR;
+      OpRegBankIdx[Idx] = AArch64::GPRRegBankID;
   }
 
   unsigned Cost = 1;
   // Some of the floating-point instructions have mixed GPR and FPR operands:
   // fine-tune the computed mapping.
   switch (Opc) {
-  case TargetOpcode::G_CONSTANT: {
-    Register Dst = MI.getOperand(0).getReg();
-    LLT DstTy = MRI.getType(Dst);
-    if (DstTy.isScalar() && DstTy.getSizeInBits() < 32)
-      MappingID = CustomMappingID;
-    break;
-  }
-  case TargetOpcode::G_FCONSTANT: {
-    if (preferGPRForFPImm(MI, MRI, STI)) {
-      // Materialize in GPR and rely on later bank copies for FP uses.
-      MappingID = CustomMappingID;
-      OpRegBankIdx = {PMI_FirstGPR};
-    }
-    break;
-  }
   case TargetOpcode::G_SPLAT_VECTOR: {
     Register ScalarReg = MI.getOperand(1).getReg();
     LLT ScalarTy = MRI.getType(ScalarReg);
     auto ScalarDef = MRI.getVRegDef(ScalarReg);
     // We want to select dup(load) into LD1R.
     if (ScalarDef->getOpcode() == TargetOpcode::G_LOAD)
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::FPRRegBankID};
     else if (getRegBank(ScalarReg, MRI, TRI) == &AArch64::FPRRegBank ||
              onlyDefinesFP(*ScalarDef, MRI, TRI))
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::FPRRegBankID};
     else {
       if (ScalarTy.getSizeInBits() < 32 &&
           getRegBank(ScalarReg, MRI, TRI) == &AArch64::GPRRegBank) {
         // Calls applyMappingImpl()
         MappingID = CustomMappingID;
       }
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstGPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::GPRRegBankID};
     }
     break;
   }
   case TargetOpcode::G_TRUNC: {
     LLT SrcTy = MRI.getType(MI.getOperand(1).getReg());
     if (!SrcTy.isVector() && SrcTy.getSizeInBits() == 128)
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::FPRRegBankID};
     break;
   }
   case TargetOpcode::G_SITOFP:
@@ -1108,9 +846,9 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
     if (getRegBank(SrcReg, MRI, TRI) == &AArch64::FPRRegBank &&
         MRI.getType(SrcReg).getSizeInBits() ==
             MRI.getType(MI.getOperand(0).getReg()).getSizeInBits())
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::FPRRegBankID};
     else
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstGPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::GPRRegBankID};
     break;
   }
   case TargetOpcode::G_FPTOSI_SAT:
@@ -1125,7 +863,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
     if (DstType.isVector())
       break;
     if (DstType == LLT::scalar(16)) {
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::FPRRegBankID};
       break;
     }
     TypeSize DstSize = TRI.getRegSizeInBits(MI.getOperand(0).getReg(), MRI);
@@ -1136,27 +874,16 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
                  return onlyUsesFP(UseMI, MRI, TRI) ||
                         prefersFPUse(UseMI, MRI, TRI);
                }))
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::FPRRegBankID};
     else
-      OpRegBankIdx = {PMI_FirstGPR, PMI_FirstFPR};
-    break;
-  }
-  case TargetOpcode::G_FCMP: {
-    // If the result is a vector, it must use a FPR.
-    AArch64GenRegisterBankInfo::PartialMappingIdx Idx0 =
-        MRI.getType(MI.getOperand(0).getReg()).isVector() ? PMI_FirstFPR
-                                                          : PMI_FirstGPR;
-    OpRegBankIdx = {Idx0,
-                    /* Predicate */ PMI_None, PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::GPRRegBankID, AArch64::FPRRegBankID};
     break;
   }
   case TargetOpcode::G_BITCAST:
     // This is going to be a cross register bank copy and this is expensive.
     if (OpRegBankIdx[0] != OpRegBankIdx[1])
-      Cost = copyCost(
-          *AArch64GenRegisterBankInfo::PartMappings[OpRegBankIdx[0]].RegBank,
-          *AArch64GenRegisterBankInfo::PartMappings[OpRegBankIdx[1]].RegBank,
-          TypeSize::getFixed(OpSize[0]));
+      Cost = copyCost(getRegBank(OpRegBankIdx[0]), getRegBank(OpRegBankIdx[1]),
+                      TypeSize::getFixed(OpSize[0]));
     break;
   case TargetOpcode::G_LOAD: {
     // Loading in vector unit is slightly more expensive.
@@ -1165,14 +892,14 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
     // for the greedy mode the cost of the cross bank copy will
     // offset this number.
     // FIXME: Should be derived from the scheduling model.
-    if (OpRegBankIdx[0] != PMI_FirstGPR) {
+    if (OpRegBankIdx[0] != AArch64::GPRRegBankID) {
       Cost = 2;
       break;
     }
 
     if (cast<GLoad>(MI).isAtomic()) {
       // Atomics always use GPR destinations. Don't refine any further.
-      OpRegBankIdx[0] = PMI_FirstGPR;
+      OpRegBankIdx[0] = AArch64::GPRRegBankID;
       if (MRI.getType(MI.getOperand(0).getReg()).getSizeInBits() < 32)
         MappingID = CustomMappingID;
       break;
@@ -1180,7 +907,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
 
     // Try to guess the type of the load from the MMO.
     if (isLoadFromFPType(MI)) {
-      OpRegBankIdx[0] = PMI_FirstFPR;
+      OpRegBankIdx[0] = AArch64::FPRRegBankID;
       break;
     }
 
@@ -1204,7 +931,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
                  return onlyUsesFP(UseMI, MRI, TRI) ||
                         prefersFPUse(UseMI, MRI, TRI);
                }))
-      OpRegBankIdx[0] = PMI_FirstFPR;
+      OpRegBankIdx[0] = AArch64::FPRRegBankID;
 
     // On GPR, extend any load < 32bits to 32bit.
     LLT Ty = MRI.getType(MI.getOperand(0).getReg());
@@ -1214,12 +941,12 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
   }
   case TargetOpcode::G_STORE:
     // Check if that store is fed by fp instructions.
-    if (OpRegBankIdx[0] == PMI_FirstGPR) {
+    if (OpRegBankIdx[0] == AArch64::GPRRegBankID) {
       Register VReg = MI.getOperand(0).getReg();
       if (VReg) {
         MachineInstr *DefMI = MRI.getVRegDef(VReg);
         if (onlyDefinesFP(*DefMI, MRI, TRI)) {
-          OpRegBankIdx[0] = PMI_FirstFPR;
+          OpRegBankIdx[0] = AArch64::FPRRegBankID;
           break;
         }
       }
@@ -1231,36 +958,37 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
     }
     break;
   case TargetOpcode::G_INDEXED_STORE:
-    if (OpRegBankIdx[1] == PMI_FirstGPR) {
+    if (OpRegBankIdx[1] == AArch64::GPRRegBankID) {
       Register VReg = MI.getOperand(1).getReg();
       if (!VReg)
         break;
       MachineInstr *DefMI = MRI.getVRegDef(VReg);
       if (onlyDefinesFP(*DefMI, MRI, TRI))
-        OpRegBankIdx[1] = PMI_FirstFPR;
+        OpRegBankIdx[1] = AArch64::FPRRegBankID;
       break;
     }
     break;
   case TargetOpcode::G_INDEXED_SEXTLOAD:
   case TargetOpcode::G_INDEXED_ZEXTLOAD:
     // These should always be GPR.
-    OpRegBankIdx[0] = PMI_FirstGPR;
+    OpRegBankIdx[0] = AArch64::GPRRegBankID;
     break;
   case TargetOpcode::G_INDEXED_LOAD: {
     if (isLoadFromFPType(MI))
-      OpRegBankIdx[0] = PMI_FirstFPR;
+      OpRegBankIdx[0] = AArch64::FPRRegBankID;
     break;
   }
   case TargetOpcode::G_SELECT: {
     // If the destination is FPR, preserve that.
-    if (OpRegBankIdx[0] != PMI_FirstGPR)
+    if (OpRegBankIdx[0] != AArch64::GPRRegBankID)
       break;
 
     // If we're taking in vectors, we have no choice but to put everything on
     // FPRs, except for the condition. The condition must always be on a GPR.
     LLT SrcTy = MRI.getType(MI.getOperand(2).getReg());
     if (SrcTy.isVector()) {
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstGPR, PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::GPRRegBankID,
+                      AArch64::FPRRegBankID, AArch64::FPRRegBankID};
       break;
     }
 
@@ -1303,14 +1031,15 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
     // If we have more FP constraints than not, then move everything over to
     // FPR.
     if (NumFP >= 2)
-      OpRegBankIdx = {PMI_FirstFPR, PMI_FirstGPR, PMI_FirstFPR, PMI_FirstFPR};
+      OpRegBankIdx = {AArch64::FPRRegBankID, AArch64::GPRRegBankID,
+                      AArch64::FPRRegBankID, AArch64::FPRRegBankID};
 
     break;
   }
   case TargetOpcode::G_UNMERGE_VALUES: {
     // If the first operand belongs to a FPR register bank, then make sure that
     // we preserve that.
-    if (OpRegBankIdx[0] != PMI_FirstGPR)
+    if (OpRegBankIdx[0] != AArch64::GPRRegBankID)
       break;
 
     LLT SrcTy = MRI.getType(MI.getOperand(MI.getNumOperands()-1).getReg());
@@ -1322,40 +1051,17 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
       // Set the register bank of every operand to FPR.
       for (unsigned Idx = 0, NumOperands = MI.getNumOperands();
            Idx < NumOperands; ++Idx)
-        OpRegBankIdx[Idx] = PMI_FirstFPR;
+        OpRegBankIdx[Idx] = AArch64::FPRRegBankID;
     }
     break;
   }
-  case TargetOpcode::G_EXTRACT_VECTOR_ELT:
-    // Destination and source need to be FPRs.
-    OpRegBankIdx[0] = PMI_FirstFPR;
-    OpRegBankIdx[1] = PMI_FirstFPR;
-
-    // Index needs to be a GPR.
-    OpRegBankIdx[2] = PMI_FirstGPR;
-    break;
   case AArch64::G_SQSHLU_I:
     // Destination and source need to be FPRs.
-    OpRegBankIdx[0] = PMI_FirstFPR;
-    OpRegBankIdx[1] = PMI_FirstFPR;
+    OpRegBankIdx[0] = AArch64::FPRRegBankID;
+    OpRegBankIdx[1] = AArch64::FPRRegBankID;
 
     // Shift Index needs to be a GPR.
-    OpRegBankIdx[2] = PMI_FirstGPR;
-    break;
-
-  case TargetOpcode::G_INSERT_VECTOR_ELT:
-    OpRegBankIdx[0] = PMI_FirstFPR;
-    OpRegBankIdx[1] = PMI_FirstFPR;
-
-    // The element may be either a GPR or FPR. Preserve that behaviour.
-    if (getRegBank(MI.getOperand(2).getReg(), MRI, TRI) == &AArch64::FPRRegBank)
-      OpRegBankIdx[2] = PMI_FirstFPR;
-    else {
-      OpRegBankIdx[2] = PMI_FirstGPR;
-    }
-
-    // Index needs to be a GPR.
-    OpRegBankIdx[3] = PMI_FirstGPR;
+    OpRegBankIdx[2] = AArch64::GPRRegBankID;
     break;
   case TargetOpcode::G_EXTRACT: {
     // For s128 sources we have to use fpr unless we know otherwise.
@@ -1364,8 +1070,8 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
     if (SrcTy.getSizeInBits() != 128)
       break;
     auto Idx = MRI.getRegClassOrNull(Src) == &AArch64::XSeqPairsClassRegClass
-                   ? PMI_FirstGPR
-                   : PMI_FirstFPR;
+                   ? AArch64::GPRRegBankID
+                   : AArch64::FPRRegBankID;
     OpRegBankIdx[0] = Idx;
     OpRegBankIdx[1] = Idx;
     break;
@@ -1373,7 +1079,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
   case TargetOpcode::G_BUILD_VECTOR: {
     // If the first source operand belongs to a FPR register bank, then make
     // sure that we preserve that.
-    if (OpRegBankIdx[1] != PMI_FirstGPR)
+    if (OpRegBankIdx[1] != AArch64::GPRRegBankID)
       break;
     Register VReg = MI.getOperand(1).getReg();
     if (!VReg)
@@ -1399,35 +1105,10 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
       // Make sure every operand gets mapped to a FPR register class.
       unsigned NumOperands = MI.getNumOperands();
       for (unsigned Idx = 0; Idx < NumOperands; ++Idx)
-        OpRegBankIdx[Idx] = PMI_FirstFPR;
+        OpRegBankIdx[Idx] = AArch64::FPRRegBankID;
     }
     break;
   }
-  case TargetOpcode::G_VECREDUCE_FADD:
-  case TargetOpcode::G_VECREDUCE_FMUL:
-  case TargetOpcode::G_VECREDUCE_FMAX:
-  case TargetOpcode::G_VECREDUCE_FMIN:
-  case TargetOpcode::G_VECREDUCE_FMAXIMUM:
-  case TargetOpcode::G_VECREDUCE_FMINIMUM:
-  case TargetOpcode::G_VECREDUCE_ADD:
-  case TargetOpcode::G_VECREDUCE_MUL:
-  case TargetOpcode::G_VECREDUCE_AND:
-  case TargetOpcode::G_VECREDUCE_OR:
-  case TargetOpcode::G_VECREDUCE_XOR:
-  case TargetOpcode::G_VECREDUCE_SMAX:
-  case TargetOpcode::G_VECREDUCE_SMIN:
-  case TargetOpcode::G_VECREDUCE_UMAX:
-  case TargetOpcode::G_VECREDUCE_UMIN:
-    // Reductions produce a scalar value from a vector, the scalar should be on
-    // FPR bank.
-    OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR};
-    break;
-  case TargetOpcode::G_VECREDUCE_SEQ_FADD:
-  case TargetOpcode::G_VECREDUCE_SEQ_FMUL:
-    // These reductions also take a scalar accumulator input.
-    // Assign them FPR for now.
-    OpRegBankIdx = {PMI_FirstFPR, PMI_FirstFPR, PMI_FirstFPR};
-    break;
   case TargetOpcode::G_INTRINSIC:
   case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS: {
     switch (cast<GIntrinsic>(MI).getIntrinsicID()) {
@@ -1441,41 +1122,41 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
     case Intrinsic::aarch64_neon_fcvtnu:
     case Intrinsic::aarch64_neon_fcvtps:
     case Intrinsic::aarch64_neon_fcvtpu: {
-      OpRegBankIdx[2] = PMI_FirstFPR;
+      OpRegBankIdx[2] = AArch64::FPRRegBankID;
       if (shouldUseFPRForCvtOperand(MI, /*BankedOpIsDef=*/true,
                                     /*ForceFPRForBankedOp16=*/true,
                                     /*ForceFPRForOtherOp16=*/false,
                                     /*AllowFPRCVT=*/STI.hasFPRCVT(),
                                     /*CheckBankedOpUses=*/true))
-        OpRegBankIdx[0] = PMI_FirstFPR;
+        OpRegBankIdx[0] = AArch64::FPRRegBankID;
       else
-        OpRegBankIdx[0] = PMI_FirstGPR;
+        OpRegBankIdx[0] = AArch64::GPRRegBankID;
       break;
     }
     case Intrinsic::aarch64_neon_vcvtfxs2fp:
     case Intrinsic::aarch64_neon_vcvtfxu2fp: {
-      OpRegBankIdx[0] = PMI_FirstFPR;
+      OpRegBankIdx[0] = AArch64::FPRRegBankID;
       if (shouldUseFPRForCvtOperand(MI, /*BankedOpIsDef=*/false,
                                     /*ForceFPRForBankedOp16=*/false,
                                     /*ForceFPRForOtherOp16=*/true,
                                     /*AllowFPRCVT=*/false,
                                     /*CheckBankedOpUses=*/false))
-        OpRegBankIdx[2] = PMI_FirstFPR;
+        OpRegBankIdx[2] = AArch64::FPRRegBankID;
       else
-        OpRegBankIdx[2] = PMI_FirstGPR;
+        OpRegBankIdx[2] = AArch64::GPRRegBankID;
       break;
     }
     case Intrinsic::aarch64_neon_vcvtfp2fxs:
     case Intrinsic::aarch64_neon_vcvtfp2fxu: {
-      OpRegBankIdx[2] = PMI_FirstFPR;
+      OpRegBankIdx[2] = AArch64::FPRRegBankID;
       if (shouldUseFPRForCvtOperand(MI, /*BankedOpIsDef=*/true,
                                     /*ForceFPRForBankedOp16=*/false,
                                     /*ForceFPRForOtherOp16=*/true,
                                     /*AllowFPRCVT=*/false,
                                     /*CheckBankedOpUses=*/true))
-        OpRegBankIdx[0] = PMI_FirstFPR;
+        OpRegBankIdx[0] = AArch64::FPRRegBankID;
       else
-        OpRegBankIdx[0] = PMI_FirstGPR;
+        OpRegBankIdx[0] = AArch64::GPRRegBankID;
       break;
     }
     default: {
@@ -1485,7 +1166,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
       if (onlyDefinesFP(MI, MRI, TRI))
         for (const auto &Op : MI.defs()) {
           if (Op.isReg())
-            OpRegBankIdx[Idx] = PMI_FirstFPR;
+            OpRegBankIdx[Idx] = AArch64::FPRRegBankID;
           ++Idx;
         }
       else
@@ -1494,7 +1175,7 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
       if (onlyUsesFP(MI, MRI, TRI))
         for (const auto &Op : MI.explicit_uses()) {
           if (Op.isReg())
-            OpRegBankIdx[Idx] = PMI_FirstFPR;
+            OpRegBankIdx[Idx] = AArch64::FPRRegBankID;
           ++Idx;
         }
       break;
@@ -1511,8 +1192,8 @@ AArch64RegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
       LLT Ty = MRI.getType(MI.getOperand(Idx).getReg());
       if (!Ty.isValid())
         continue;
-      auto Mapping =
-          getValueMapping(OpRegBankIdx[Idx], TypeSize::getFixed(OpSize[Idx]));
+      auto Mapping = AArch64::getGeneratedValueMapping(
+          OpRegBankIdx[Idx], TypeSize::getFixed(OpSize[Idx]));
       if (!Mapping->isValid())
         return getInvalidInstructionMapping();
 
diff --git a/llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.h b/llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.h
index 53bf6146ad437b..5fcc135a6d7a8a 100644
--- a/llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.h
+++ b/llvm/lib/Target/AArch64/GISel/AArch64RegisterBankInfo.h
@@ -26,78 +26,11 @@ class AArch64RegisterInfo;
 
 class AArch64GenRegisterBankInfo : public RegisterBankInfo {
 protected:
-  enum PartialMappingIdx {
-    PMI_None = -1,
-    PMI_FPR16 = 1,
-    PMI_FPR32,
-    PMI_FPR64,
-    PMI_FPR128,
-    PMI_FPR256,
-    PMI_FPR512,
-    PMI_GPR32,
-    PMI_GPR64,
-    PMI_GPR128,
-    PMI_FirstGPR = PMI_GPR32,
-    PMI_LastGPR = PMI_GPR128,
-    PMI_FirstFPR = PMI_FPR16,
-    PMI_LastFPR = PMI_FPR512,
-    PMI_Min = PMI_FirstFPR,
-  };
-
-  static const RegisterBankInfo::PartialMapping PartMappings[];
-  static const RegisterBankInfo::ValueMapping ValMappings[];
-  static const PartialMappingIdx BankIDToCopyMapIdx[];
-
-  enum ValueMappingIdx {
-    InvalidIdx = 0,
-    First3OpsIdx = 1,
-    Last3OpsIdx = 25,
-    DistanceBetweenRegBanks = 3,
-    FirstCrossRegCpyIdx = 28,
-    LastCrossRegCpyIdx = 42,
-    DistanceBetweenCrossRegCpy = 2,
-    FPExt16To32Idx = 44,
-    FPExt16To64Idx = 46,
-    FPExt32To64Idx = 48,
-    FPExt64To128Idx = 50,
-    Shift64Imm = 52,
-  };
-
-  static bool checkPartialMap(unsigned Idx, unsigned ValStartIdx,
-                              unsigned ValLength, const RegisterBank &RB);
-  static bool checkValueMapImpl(unsigned Idx, unsigned FirstInBank,
-                                unsigned Size, unsigned Offset);
-  static bool checkPartialMappingIdx(PartialMappingIdx FirstAlias,
-                                     PartialMappingIdx LastAlias,
-                                     ArrayRef<PartialMappingIdx> Order);
-
-  static unsigned getRegBankBaseIdxOffset(unsigned RBIdx, TypeSize Size);
-
-  /// Get the pointer to the ValueMapping representing the RegisterBank
-  /// at \p RBIdx with a size of \p Size.
-  ///
-  /// The returned mapping works for instructions with the same kind of
-  /// operands for up to 3 operands.
-  ///
-  /// \pre \p RBIdx != PartialMappingIdx::None
-  static const RegisterBankInfo::ValueMapping *
-  getValueMapping(PartialMappingIdx RBIdx, TypeSize Size);
-
   /// Get the pointer to the ValueMapping of the operands of a copy
   /// instruction from the \p SrcBankID register bank to the \p DstBankID
   /// register bank with a size of \p Size.
-  static const RegisterBankInfo::ValueMapping *
-  getCopyMapping(unsigned DstBankID, unsigned SrcBankID, TypeSize Size);
-
-  /// Get the instruction mapping for G_FPEXT.
-  ///
-  /// \pre (DstSize, SrcSize) pair is one of the following:
-  ///      (32, 16), (64, 16), (64, 32), (128, 64)
-  ///
-  /// \return An InstructionMapping with statically allocated OperandsMapping.
-  static const RegisterBankInfo::ValueMapping *
-  getFPExtMapping(unsigned DstSize, unsigned SrcSize);
-
+  const RegisterBankInfo::ValueMapping *
+  getCopyMapping(unsigned DstBankID, unsigned SrcBankID, TypeSize Size) const;
 #define GET_TARGET_REGBANK_CLASS
 #include "AArch64GenRegisterBank.inc"
 };
@@ -108,16 +41,6 @@ class AArch64RegisterBankInfo final : public AArch64GenRegisterBankInfo {
   void applyMappingImpl(MachineIRBuilder &Builder,
                         const OperandsMapper &OpdMapper) const override;
 
-  /// Get an instruction mapping where all the operands map to
-  /// the same register bank and have similar size.
-  ///
-  /// \pre MI.getNumOperands() <= 3
-  ///
-  /// \return An InstructionMappings with a statically allocated
-  /// OperandsMapping.
-  const InstructionMapping &
-  getSameKindOfOperandsMapping(const MachineInstr &MI) const;
-
   /// Maximum recursion depth for hasFPConstraints.
   const unsigned MaxFPRSearchDepth = 2;
 



More information about the llvm-branch-commits mailing list