1fac93e28SPetar Jovanovic //===- MipsRegisterBankInfo.cpp ---------------------------------*- C++ -*-===//
2fac93e28SPetar Jovanovic //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6fac93e28SPetar Jovanovic //
7fac93e28SPetar Jovanovic //===----------------------------------------------------------------------===//
8fac93e28SPetar Jovanovic /// \file
9fac93e28SPetar Jovanovic /// This file implements the targeting of the RegisterBankInfo class for Mips.
10fac93e28SPetar Jovanovic /// \todo This should be generated by TableGen.
11fac93e28SPetar Jovanovic //===----------------------------------------------------------------------===//
12fac93e28SPetar Jovanovic 
13fac93e28SPetar Jovanovic #include "MipsRegisterBankInfo.h"
14ec575f6eSPetar Avramovic #include "MipsInstrInfo.h"
15599591f3SPetar Avramovic #include "MipsTargetMachine.h"
16ec575f6eSPetar Avramovic #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
17ec575f6eSPetar Avramovic #include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h"
18ec575f6eSPetar Avramovic #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
19fac93e28SPetar Jovanovic #include "llvm/CodeGen/MachineRegisterInfo.h"
20fac93e28SPetar Jovanovic 
21366857a2SPetar Jovanovic #define GET_TARGET_REGBANK_IMPL
22fac93e28SPetar Jovanovic 
23366857a2SPetar Jovanovic #include "MipsGenRegisterBank.inc"
24366857a2SPetar Jovanovic 
25366857a2SPetar Jovanovic namespace llvm {
26366857a2SPetar Jovanovic namespace Mips {
27366857a2SPetar Jovanovic enum PartialMappingIdx {
28366857a2SPetar Jovanovic   PMI_GPR,
291af05df3SPetar Avramovic   PMI_SPR,
301af05df3SPetar Avramovic   PMI_DPR,
31599591f3SPetar Avramovic   PMI_MSA,
32366857a2SPetar Jovanovic   PMI_Min = PMI_GPR,
33366857a2SPetar Jovanovic };
34366857a2SPetar Jovanovic 
35366857a2SPetar Jovanovic RegisterBankInfo::PartialMapping PartMappings[]{
361af05df3SPetar Avramovic     {0, 32, GPRBRegBank},
371af05df3SPetar Avramovic     {0, 32, FPRBRegBank},
38599591f3SPetar Avramovic     {0, 64, FPRBRegBank},
39599591f3SPetar Avramovic     {0, 128, FPRBRegBank}
40366857a2SPetar Jovanovic };
41366857a2SPetar Jovanovic 
421af05df3SPetar Avramovic enum ValueMappingIdx {
431af05df3SPetar Avramovic     InvalidIdx = 0,
441af05df3SPetar Avramovic     GPRIdx = 1,
451af05df3SPetar Avramovic     SPRIdx = 4,
46599591f3SPetar Avramovic     DPRIdx = 7,
47599591f3SPetar Avramovic     MSAIdx = 10
481af05df3SPetar Avramovic };
49366857a2SPetar Jovanovic 
50366857a2SPetar Jovanovic RegisterBankInfo::ValueMapping ValueMappings[] = {
51366857a2SPetar Jovanovic     // invalid
52366857a2SPetar Jovanovic     {nullptr, 0},
531af05df3SPetar Avramovic     // up to 3 operands in GPRs
54366857a2SPetar Jovanovic     {&PartMappings[PMI_GPR - PMI_Min], 1},
55366857a2SPetar Jovanovic     {&PartMappings[PMI_GPR - PMI_Min], 1},
561af05df3SPetar Avramovic     {&PartMappings[PMI_GPR - PMI_Min], 1},
57f7c213c9SPetar Avramovic     // up to 3 operands in FPRs - single precission
581af05df3SPetar Avramovic     {&PartMappings[PMI_SPR - PMI_Min], 1},
591af05df3SPetar Avramovic     {&PartMappings[PMI_SPR - PMI_Min], 1},
601af05df3SPetar Avramovic     {&PartMappings[PMI_SPR - PMI_Min], 1},
61f7c213c9SPetar Avramovic     // up to 3 operands in FPRs - double precission
621af05df3SPetar Avramovic     {&PartMappings[PMI_DPR - PMI_Min], 1},
631af05df3SPetar Avramovic     {&PartMappings[PMI_DPR - PMI_Min], 1},
64599591f3SPetar Avramovic     {&PartMappings[PMI_DPR - PMI_Min], 1},
65599591f3SPetar Avramovic     // up to 3 operands in FPRs - MSA
66599591f3SPetar Avramovic     {&PartMappings[PMI_MSA - PMI_Min], 1},
67599591f3SPetar Avramovic     {&PartMappings[PMI_MSA - PMI_Min], 1},
68599591f3SPetar Avramovic     {&PartMappings[PMI_MSA - PMI_Min], 1}
691af05df3SPetar Avramovic };
70366857a2SPetar Jovanovic 
71366857a2SPetar Jovanovic } // end namespace Mips
72366857a2SPetar Jovanovic } // end namespace llvm
73366857a2SPetar Jovanovic 
74366857a2SPetar Jovanovic using namespace llvm;
75fac93e28SPetar Jovanovic 
76fac93e28SPetar Jovanovic MipsRegisterBankInfo::MipsRegisterBankInfo(const TargetRegisterInfo &TRI)
77fac93e28SPetar Jovanovic     : MipsGenRegisterBankInfo() {}
78366857a2SPetar Jovanovic 
7921309eafSMatt Arsenault const RegisterBank &
8021309eafSMatt Arsenault MipsRegisterBankInfo::getRegBankFromRegClass(const TargetRegisterClass &RC,
8121309eafSMatt Arsenault                                              LLT) const {
82366857a2SPetar Jovanovic   using namespace Mips;
83366857a2SPetar Jovanovic 
84366857a2SPetar Jovanovic   switch (RC.getID()) {
85366857a2SPetar Jovanovic   case Mips::GPR32RegClassID:
86366857a2SPetar Jovanovic   case Mips::CPU16Regs_and_GPRMM16ZeroRegClassID:
87852dd83bSSimon Atanasyan   case Mips::GPRMM16MovePPairFirstRegClassID:
88852dd83bSSimon Atanasyan   case Mips::CPU16Regs_and_GPRMM16MovePPairSecondRegClassID:
89366857a2SPetar Jovanovic   case Mips::GPRMM16MoveP_and_CPU16Regs_and_GPRMM16ZeroRegClassID:
90852dd83bSSimon Atanasyan   case Mips::GPRMM16MovePPairFirst_and_GPRMM16MovePPairSecondRegClassID:
91021e4c82SPetar Jovanovic   case Mips::SP32RegClassID:
92efcd3c00SPetar Avramovic   case Mips::GP32RegClassID:
93366857a2SPetar Jovanovic     return getRegBank(Mips::GPRBRegBankID);
943dfa368dSPetar Avramovic   case Mips::FGRCCRegClassID:
95afa3afa3SPetar Avramovic   case Mips::FGR32RegClassID:
963dfa368dSPetar Avramovic   case Mips::FGR64RegClassID:
973dfa368dSPetar Avramovic   case Mips::AFGR64RegClassID:
98599591f3SPetar Avramovic   case Mips::MSA128BRegClassID:
99599591f3SPetar Avramovic   case Mips::MSA128HRegClassID:
100599591f3SPetar Avramovic   case Mips::MSA128WRegClassID:
101599591f3SPetar Avramovic   case Mips::MSA128DRegClassID:
1023dfa368dSPetar Avramovic     return getRegBank(Mips::FPRBRegBankID);
103366857a2SPetar Jovanovic   default:
104366857a2SPetar Jovanovic     llvm_unreachable("Register class not supported");
105366857a2SPetar Jovanovic   }
106366857a2SPetar Jovanovic }
107366857a2SPetar Jovanovic 
108ec575f6eSPetar Avramovic // Instructions where all register operands are floating point.
109ec575f6eSPetar Avramovic static bool isFloatingPointOpcode(unsigned Opc) {
110ec575f6eSPetar Avramovic   switch (Opc) {
111ec575f6eSPetar Avramovic   case TargetOpcode::G_FCONSTANT:
112ec575f6eSPetar Avramovic   case TargetOpcode::G_FADD:
113ec575f6eSPetar Avramovic   case TargetOpcode::G_FSUB:
114ec575f6eSPetar Avramovic   case TargetOpcode::G_FMUL:
115ec575f6eSPetar Avramovic   case TargetOpcode::G_FDIV:
116ec575f6eSPetar Avramovic   case TargetOpcode::G_FABS:
117ec575f6eSPetar Avramovic   case TargetOpcode::G_FSQRT:
118ec575f6eSPetar Avramovic   case TargetOpcode::G_FCEIL:
119ec575f6eSPetar Avramovic   case TargetOpcode::G_FFLOOR:
120ec575f6eSPetar Avramovic   case TargetOpcode::G_FPEXT:
121ec575f6eSPetar Avramovic   case TargetOpcode::G_FPTRUNC:
122ec575f6eSPetar Avramovic     return true;
123ec575f6eSPetar Avramovic   default:
124ec575f6eSPetar Avramovic     return false;
125ec575f6eSPetar Avramovic   }
126ec575f6eSPetar Avramovic }
127ec575f6eSPetar Avramovic 
128aa699b20SPetar Avramovic // Instructions where use operands are floating point registers.
129aa699b20SPetar Avramovic // Def operands are general purpose.
130aa699b20SPetar Avramovic static bool isFloatingPointOpcodeUse(unsigned Opc) {
131aa699b20SPetar Avramovic   switch (Opc) {
132aa699b20SPetar Avramovic   case TargetOpcode::G_FPTOSI:
133aa699b20SPetar Avramovic   case TargetOpcode::G_FPTOUI:
134aa699b20SPetar Avramovic   case TargetOpcode::G_FCMP:
135aa699b20SPetar Avramovic   case Mips::MFC1:
136aa699b20SPetar Avramovic   case Mips::ExtractElementF64:
137aa699b20SPetar Avramovic   case Mips::ExtractElementF64_64:
138aa699b20SPetar Avramovic     return true;
139aa699b20SPetar Avramovic   default:
140aa699b20SPetar Avramovic     return isFloatingPointOpcode(Opc);
141aa699b20SPetar Avramovic   }
142aa699b20SPetar Avramovic }
143aa699b20SPetar Avramovic 
144ec575f6eSPetar Avramovic // Instructions where def operands are floating point registers.
145ec575f6eSPetar Avramovic // Use operands are general purpose.
146ec575f6eSPetar Avramovic static bool isFloatingPointOpcodeDef(unsigned Opc) {
147ec575f6eSPetar Avramovic   switch (Opc) {
148ec575f6eSPetar Avramovic   case TargetOpcode::G_SITOFP:
149ec575f6eSPetar Avramovic   case TargetOpcode::G_UITOFP:
150ec575f6eSPetar Avramovic   case Mips::MTC1:
151ec575f6eSPetar Avramovic   case Mips::BuildPairF64:
152ec575f6eSPetar Avramovic   case Mips::BuildPairF64_64:
153ec575f6eSPetar Avramovic     return true;
154ec575f6eSPetar Avramovic   default:
155ec575f6eSPetar Avramovic     return isFloatingPointOpcode(Opc);
156ec575f6eSPetar Avramovic   }
157ec575f6eSPetar Avramovic }
158ec575f6eSPetar Avramovic 
159ec575f6eSPetar Avramovic static bool isAmbiguous(unsigned Opc) {
160ec575f6eSPetar Avramovic   switch (Opc) {
161ec575f6eSPetar Avramovic   case TargetOpcode::G_LOAD:
162ec575f6eSPetar Avramovic   case TargetOpcode::G_STORE:
163ec575f6eSPetar Avramovic   case TargetOpcode::G_PHI:
164ec575f6eSPetar Avramovic   case TargetOpcode::G_SELECT:
16575e43a60SPetar Avramovic   case TargetOpcode::G_IMPLICIT_DEF:
166ec575f6eSPetar Avramovic     return true;
167ec575f6eSPetar Avramovic   default:
168ec575f6eSPetar Avramovic     return false;
169ec575f6eSPetar Avramovic   }
170ec575f6eSPetar Avramovic }
171ec575f6eSPetar Avramovic 
172aa699b20SPetar Avramovic void MipsRegisterBankInfo::AmbiguousRegDefUseContainer::addDefUses(
173aa699b20SPetar Avramovic     Register Reg, const MachineRegisterInfo &MRI) {
174aa699b20SPetar Avramovic   assert(!MRI.getType(Reg).isPointer() &&
175aa699b20SPetar Avramovic          "Pointers are gprb, they should not be considered as ambiguous.\n");
176aa699b20SPetar Avramovic   for (MachineInstr &UseMI : MRI.use_instructions(Reg)) {
17796252407SPetar Avramovic     MachineInstr *NonCopyInstr = skipCopiesOutgoing(&UseMI);
17896252407SPetar Avramovic     // Copy with many uses.
17996252407SPetar Avramovic     if (NonCopyInstr->getOpcode() == TargetOpcode::COPY &&
1802bea69bfSDaniel Sanders         !Register::isPhysicalRegister(NonCopyInstr->getOperand(0).getReg()))
18196252407SPetar Avramovic       addDefUses(NonCopyInstr->getOperand(0).getReg(), MRI);
18296252407SPetar Avramovic     else
18396252407SPetar Avramovic       DefUses.push_back(skipCopiesOutgoing(&UseMI));
184aa699b20SPetar Avramovic   }
185aa699b20SPetar Avramovic }
186aa699b20SPetar Avramovic 
187ec575f6eSPetar Avramovic void MipsRegisterBankInfo::AmbiguousRegDefUseContainer::addUseDef(
188ec575f6eSPetar Avramovic     Register Reg, const MachineRegisterInfo &MRI) {
189ec575f6eSPetar Avramovic   assert(!MRI.getType(Reg).isPointer() &&
190ec575f6eSPetar Avramovic          "Pointers are gprb, they should not be considered as ambiguous.\n");
191ec575f6eSPetar Avramovic   MachineInstr *DefMI = MRI.getVRegDef(Reg);
19296252407SPetar Avramovic   UseDefs.push_back(skipCopiesIncoming(DefMI));
19396252407SPetar Avramovic }
194ec575f6eSPetar Avramovic 
19596252407SPetar Avramovic MachineInstr *
19696252407SPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::skipCopiesOutgoing(
19796252407SPetar Avramovic     MachineInstr *MI) const {
19896252407SPetar Avramovic   const MachineFunction &MF = *MI->getParent()->getParent();
19996252407SPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
20096252407SPetar Avramovic   MachineInstr *Ret = MI;
20196252407SPetar Avramovic   while (Ret->getOpcode() == TargetOpcode::COPY &&
2022bea69bfSDaniel Sanders          !Register::isPhysicalRegister(Ret->getOperand(0).getReg()) &&
20396252407SPetar Avramovic          MRI.hasOneUse(Ret->getOperand(0).getReg())) {
20496252407SPetar Avramovic     Ret = &(*MRI.use_instr_begin(Ret->getOperand(0).getReg()));
20596252407SPetar Avramovic   }
20696252407SPetar Avramovic   return Ret;
20796252407SPetar Avramovic }
20896252407SPetar Avramovic 
20996252407SPetar Avramovic MachineInstr *
21096252407SPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::skipCopiesIncoming(
21196252407SPetar Avramovic     MachineInstr *MI) const {
21296252407SPetar Avramovic   const MachineFunction &MF = *MI->getParent()->getParent();
21396252407SPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
21496252407SPetar Avramovic   MachineInstr *Ret = MI;
21596252407SPetar Avramovic   while (Ret->getOpcode() == TargetOpcode::COPY &&
2162bea69bfSDaniel Sanders          !Register::isPhysicalRegister(Ret->getOperand(1).getReg()))
21796252407SPetar Avramovic     Ret = MRI.getVRegDef(Ret->getOperand(1).getReg());
21896252407SPetar Avramovic   return Ret;
219ec575f6eSPetar Avramovic }
220ec575f6eSPetar Avramovic 
221ec575f6eSPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::AmbiguousRegDefUseContainer(
222ec575f6eSPetar Avramovic     const MachineInstr *MI) {
223ec575f6eSPetar Avramovic   assert(isAmbiguous(MI->getOpcode()) &&
224ec575f6eSPetar Avramovic          "Not implemented for non Ambiguous opcode.\n");
225ec575f6eSPetar Avramovic 
226ec575f6eSPetar Avramovic   const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
227ec575f6eSPetar Avramovic 
228aa699b20SPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_LOAD)
229aa699b20SPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
230aa699b20SPetar Avramovic 
231ec575f6eSPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_STORE)
232ec575f6eSPetar Avramovic     addUseDef(MI->getOperand(0).getReg(), MRI);
233dbb6d01dSPetar Avramovic 
234be20e361SPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_PHI) {
235be20e361SPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
236be20e361SPetar Avramovic 
237be20e361SPetar Avramovic     for (unsigned i = 1; i < MI->getNumOperands(); i += 2)
238be20e361SPetar Avramovic       addUseDef(MI->getOperand(i).getReg(), MRI);
239be20e361SPetar Avramovic   }
240be20e361SPetar Avramovic 
241dbb6d01dSPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_SELECT) {
242dbb6d01dSPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
243dbb6d01dSPetar Avramovic 
244dbb6d01dSPetar Avramovic     addUseDef(MI->getOperand(2).getReg(), MRI);
245dbb6d01dSPetar Avramovic     addUseDef(MI->getOperand(3).getReg(), MRI);
246dbb6d01dSPetar Avramovic   }
24775e43a60SPetar Avramovic 
24875e43a60SPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_IMPLICIT_DEF)
24975e43a60SPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
250ec575f6eSPetar Avramovic }
251ec575f6eSPetar Avramovic 
252e3bb0a72SPetar Avramovic bool MipsRegisterBankInfo::TypeInfoForMF::visit(
253e3bb0a72SPetar Avramovic     const MachineInstr *MI, const MachineInstr *WaitingForTypeOfMI) {
254ec575f6eSPetar Avramovic   assert(isAmbiguous(MI->getOpcode()) && "Visiting non-Ambiguous opcode.\n");
255e3bb0a72SPetar Avramovic   if (wasVisited(MI))
256e3bb0a72SPetar Avramovic     return true; // InstType has already been determined for MI.
257ec575f6eSPetar Avramovic 
258ec575f6eSPetar Avramovic   startVisit(MI);
259ec575f6eSPetar Avramovic   AmbiguousRegDefUseContainer DefUseContainer(MI);
260ec575f6eSPetar Avramovic 
261aa699b20SPetar Avramovic   // Visit instructions where MI's DEF operands are USED.
262aa699b20SPetar Avramovic   if (visitAdjacentInstrs(MI, DefUseContainer.getDefUses(), true))
263aa699b20SPetar Avramovic     return true;
264aa699b20SPetar Avramovic 
265ec575f6eSPetar Avramovic   // Visit instructions that DEFINE MI's USE operands.
266aa699b20SPetar Avramovic   if (visitAdjacentInstrs(MI, DefUseContainer.getUseDefs(), false))
267ec575f6eSPetar Avramovic     return true;
268ec575f6eSPetar Avramovic 
269e3bb0a72SPetar Avramovic   // All MI's adjacent instructions, are ambiguous.
270e3bb0a72SPetar Avramovic   if (!WaitingForTypeOfMI) {
271e3bb0a72SPetar Avramovic     // This is chain of ambiguous instructions.
272e3bb0a72SPetar Avramovic     setTypes(MI, InstType::Ambiguous);
273e3bb0a72SPetar Avramovic     return true;
274e3bb0a72SPetar Avramovic   }
275e3bb0a72SPetar Avramovic   // Excluding WaitingForTypeOfMI, MI is either connected to chains of ambiguous
276e3bb0a72SPetar Avramovic   // instructions or has no other adjacent instructions. Anyway InstType could
277e3bb0a72SPetar Avramovic   // not be determined. There could be unexplored path from some of
278e3bb0a72SPetar Avramovic   // WaitingForTypeOfMI's adjacent instructions to an instruction with only one
279e3bb0a72SPetar Avramovic   // mapping available.
280e3bb0a72SPetar Avramovic   // We are done with this branch, add MI to WaitingForTypeOfMI's WaitingQueue,
281e3bb0a72SPetar Avramovic   // this way when WaitingForTypeOfMI figures out its InstType same InstType
282e3bb0a72SPetar Avramovic   // will be assigned to all instructions in this branch.
283e3bb0a72SPetar Avramovic   addToWaitingQueue(WaitingForTypeOfMI, MI);
284ec575f6eSPetar Avramovic   return false;
285ec575f6eSPetar Avramovic }
286ec575f6eSPetar Avramovic 
287ec575f6eSPetar Avramovic bool MipsRegisterBankInfo::TypeInfoForMF::visitAdjacentInstrs(
288aa699b20SPetar Avramovic     const MachineInstr *MI, SmallVectorImpl<MachineInstr *> &AdjacentInstrs,
289aa699b20SPetar Avramovic     bool isDefUse) {
290ec575f6eSPetar Avramovic   while (!AdjacentInstrs.empty()) {
291ec575f6eSPetar Avramovic     MachineInstr *AdjMI = AdjacentInstrs.pop_back_val();
292ec575f6eSPetar Avramovic 
293aa699b20SPetar Avramovic     if (isDefUse ? isFloatingPointOpcodeUse(AdjMI->getOpcode())
294aa699b20SPetar Avramovic                  : isFloatingPointOpcodeDef(AdjMI->getOpcode())) {
295ec575f6eSPetar Avramovic       setTypes(MI, InstType::FloatingPoint);
296ec575f6eSPetar Avramovic       return true;
297ec575f6eSPetar Avramovic     }
298ec575f6eSPetar Avramovic 
299ec575f6eSPetar Avramovic     // Determine InstType from register bank of phys register that is
300aa699b20SPetar Avramovic     // 'isDefUse ? def : use' of this copy.
301ec575f6eSPetar Avramovic     if (AdjMI->getOpcode() == TargetOpcode::COPY) {
302aa699b20SPetar Avramovic       setTypesAccordingToPhysicalRegister(MI, AdjMI, isDefUse ? 0 : 1);
303ec575f6eSPetar Avramovic       return true;
304ec575f6eSPetar Avramovic     }
305ec575f6eSPetar Avramovic 
306ec575f6eSPetar Avramovic     // Defaults to integer instruction. Includes G_MERGE_VALUES and
307ec575f6eSPetar Avramovic     // G_UNMERGE_VALUES.
308e3bb0a72SPetar Avramovic     if (!isAmbiguous(AdjMI->getOpcode())) {
309ec575f6eSPetar Avramovic       setTypes(MI, InstType::Integer);
310ec575f6eSPetar Avramovic       return true;
311ec575f6eSPetar Avramovic     }
312e3bb0a72SPetar Avramovic 
313e3bb0a72SPetar Avramovic     // When AdjMI was visited first, MI has to continue to explore remaining
314e3bb0a72SPetar Avramovic     // adjacent instructions and determine InstType without visiting AdjMI.
315e3bb0a72SPetar Avramovic     if (!wasVisited(AdjMI) ||
316e3bb0a72SPetar Avramovic         getRecordedTypeForInstr(AdjMI) != InstType::NotDetermined) {
317e3bb0a72SPetar Avramovic       if (visit(AdjMI, MI)) {
318e3bb0a72SPetar Avramovic         // InstType is successfully determined and is same as for AdjMI.
319e3bb0a72SPetar Avramovic         setTypes(MI, getRecordedTypeForInstr(AdjMI));
320e3bb0a72SPetar Avramovic         return true;
321e3bb0a72SPetar Avramovic       }
322e3bb0a72SPetar Avramovic     }
323e3bb0a72SPetar Avramovic   }
324ec575f6eSPetar Avramovic   return false;
325ec575f6eSPetar Avramovic }
326ec575f6eSPetar Avramovic 
327ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::setTypes(const MachineInstr *MI,
328ec575f6eSPetar Avramovic                                                    InstType InstTy) {
329ec575f6eSPetar Avramovic   changeRecordedTypeForInstr(MI, InstTy);
330e3bb0a72SPetar Avramovic   for (const MachineInstr *WaitingInstr : getWaitingQueueFor(MI)) {
331e3bb0a72SPetar Avramovic     setTypes(WaitingInstr, InstTy);
332e3bb0a72SPetar Avramovic   }
333ec575f6eSPetar Avramovic }
334ec575f6eSPetar Avramovic 
335ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::setTypesAccordingToPhysicalRegister(
336ec575f6eSPetar Avramovic     const MachineInstr *MI, const MachineInstr *CopyInst, unsigned Op) {
3372bea69bfSDaniel Sanders   assert((Register::isPhysicalRegister(CopyInst->getOperand(Op).getReg())) &&
338ec575f6eSPetar Avramovic          "Copies of non physical registers should not be considered here.\n");
339ec575f6eSPetar Avramovic 
340ec575f6eSPetar Avramovic   const MachineFunction &MF = *CopyInst->getMF();
341ec575f6eSPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
342ec575f6eSPetar Avramovic   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
343ec575f6eSPetar Avramovic   const RegisterBankInfo &RBI =
344ec575f6eSPetar Avramovic       *CopyInst->getMF()->getSubtarget().getRegBankInfo();
345ec575f6eSPetar Avramovic   const RegisterBank *Bank =
346ec575f6eSPetar Avramovic       RBI.getRegBank(CopyInst->getOperand(Op).getReg(), MRI, TRI);
347ec575f6eSPetar Avramovic 
348ec575f6eSPetar Avramovic   if (Bank == &Mips::FPRBRegBank)
349ec575f6eSPetar Avramovic     setTypes(MI, InstType::FloatingPoint);
350ec575f6eSPetar Avramovic   else if (Bank == &Mips::GPRBRegBank)
351ec575f6eSPetar Avramovic     setTypes(MI, InstType::Integer);
352ec575f6eSPetar Avramovic   else
353ec575f6eSPetar Avramovic     llvm_unreachable("Unsupported register bank.\n");
354ec575f6eSPetar Avramovic }
355ec575f6eSPetar Avramovic 
356ec575f6eSPetar Avramovic MipsRegisterBankInfo::InstType
357ec575f6eSPetar Avramovic MipsRegisterBankInfo::TypeInfoForMF::determineInstType(const MachineInstr *MI) {
358e3bb0a72SPetar Avramovic   visit(MI, nullptr);
359ec575f6eSPetar Avramovic   return getRecordedTypeForInstr(MI);
360ec575f6eSPetar Avramovic }
361ec575f6eSPetar Avramovic 
362ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::cleanupIfNewFunction(
363ec575f6eSPetar Avramovic     llvm::StringRef FunctionName) {
364ec575f6eSPetar Avramovic   if (MFName != FunctionName) {
365ec575f6eSPetar Avramovic     MFName = FunctionName;
366e3bb0a72SPetar Avramovic     WaitingQueues.clear();
367ec575f6eSPetar Avramovic     Types.clear();
368ec575f6eSPetar Avramovic   }
369ec575f6eSPetar Avramovic }
370ec575f6eSPetar Avramovic 
371599591f3SPetar Avramovic static const MipsRegisterBankInfo::ValueMapping *
372599591f3SPetar Avramovic getMSAMapping(const MachineFunction &MF) {
373599591f3SPetar Avramovic   assert(static_cast<const MipsSubtarget &>(MF.getSubtarget()).hasMSA() &&
374599591f3SPetar Avramovic          "MSA mapping not available on target without MSA.");
375599591f3SPetar Avramovic   return &Mips::ValueMappings[Mips::MSAIdx];
376599591f3SPetar Avramovic }
377599591f3SPetar Avramovic 
378f7c213c9SPetar Avramovic static const MipsRegisterBankInfo::ValueMapping *getFprbMapping(unsigned Size) {
379f7c213c9SPetar Avramovic   return Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
380f7c213c9SPetar Avramovic                     : &Mips::ValueMappings[Mips::DPRIdx];
381f7c213c9SPetar Avramovic }
382f7c213c9SPetar Avramovic 
383f7c213c9SPetar Avramovic static const unsigned CustomMappingID = 1;
384f7c213c9SPetar Avramovic 
385f7c213c9SPetar Avramovic // Only 64 bit mapping is available in fprb and will be marked as custom, i.e.
386f7c213c9SPetar Avramovic // will be split into two 32 bit registers in gprb.
387f7c213c9SPetar Avramovic static const MipsRegisterBankInfo::ValueMapping *
388f7c213c9SPetar Avramovic getGprbOrCustomMapping(unsigned Size, unsigned &MappingID) {
389f7c213c9SPetar Avramovic   if (Size == 32)
390f7c213c9SPetar Avramovic     return &Mips::ValueMappings[Mips::GPRIdx];
391f7c213c9SPetar Avramovic 
392f7c213c9SPetar Avramovic   MappingID = CustomMappingID;
393f7c213c9SPetar Avramovic   return &Mips::ValueMappings[Mips::DPRIdx];
394f7c213c9SPetar Avramovic }
395f7c213c9SPetar Avramovic 
396366857a2SPetar Jovanovic const RegisterBankInfo::InstructionMapping &
397366857a2SPetar Jovanovic MipsRegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
398366857a2SPetar Jovanovic 
399ec575f6eSPetar Avramovic   static TypeInfoForMF TI;
400ec575f6eSPetar Avramovic 
401ec575f6eSPetar Avramovic   // Reset TI internal data when MF changes.
402ec575f6eSPetar Avramovic   TI.cleanupIfNewFunction(MI.getMF()->getName());
403ec575f6eSPetar Avramovic 
404366857a2SPetar Jovanovic   unsigned Opc = MI.getOpcode();
4051af05df3SPetar Avramovic   const MachineFunction &MF = *MI.getParent()->getParent();
4061af05df3SPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
407366857a2SPetar Jovanovic 
408be20e361SPetar Avramovic   if (MI.getOpcode() != TargetOpcode::G_PHI) {
409be20e361SPetar Avramovic     const RegisterBankInfo::InstructionMapping &Mapping =
410be20e361SPetar Avramovic         getInstrMappingImpl(MI);
411366857a2SPetar Jovanovic     if (Mapping.isValid())
412366857a2SPetar Jovanovic       return Mapping;
413be20e361SPetar Avramovic   }
414366857a2SPetar Jovanovic 
415366857a2SPetar Jovanovic   using namespace TargetOpcode;
416366857a2SPetar Jovanovic 
417366857a2SPetar Jovanovic   unsigned NumOperands = MI.getNumOperands();
418366857a2SPetar Jovanovic   const ValueMapping *OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx];
419ec575f6eSPetar Avramovic   unsigned MappingID = DefaultMappingID;
420f7c213c9SPetar Avramovic 
421f7c213c9SPetar Avramovic   // Check if LLT sizes match sizes of available register banks.
422f7c213c9SPetar Avramovic   for (const MachineOperand &Op : MI.operands()) {
423f7c213c9SPetar Avramovic     if (Op.isReg()) {
424f7c213c9SPetar Avramovic       LLT RegTy = MRI.getType(Op.getReg());
425f7c213c9SPetar Avramovic 
426f7c213c9SPetar Avramovic       if (RegTy.isScalar() &&
427f7c213c9SPetar Avramovic           (RegTy.getSizeInBits() != 32 && RegTy.getSizeInBits() != 64))
428f7c213c9SPetar Avramovic         return getInvalidInstructionMapping();
429599591f3SPetar Avramovic 
430599591f3SPetar Avramovic       if (RegTy.isVector() && RegTy.getSizeInBits() != 128)
431599591f3SPetar Avramovic         return getInvalidInstructionMapping();
432f7c213c9SPetar Avramovic     }
433f7c213c9SPetar Avramovic   }
434f7c213c9SPetar Avramovic 
435f7c213c9SPetar Avramovic   const LLT Op0Ty = MRI.getType(MI.getOperand(0).getReg());
436f7c213c9SPetar Avramovic   unsigned Op0Size = Op0Ty.getSizeInBits();
437f7c213c9SPetar Avramovic   InstType InstTy = InstType::Integer;
438366857a2SPetar Jovanovic 
439366857a2SPetar Jovanovic   switch (Opc) {
4407cecadb9SPetar Avramovic   case G_TRUNC:
441a48285a1SPetar Avramovic   case G_UMULH:
44279df8596SPetar Avramovic   case G_ZEXTLOAD:
44379df8596SPetar Avramovic   case G_SEXTLOAD:
444e74c5b96SDaniel Sanders   case G_PTR_ADD:
445b1fc6f61SPetar Avramovic   case G_INTTOPTR:
446b1fc6f61SPetar Avramovic   case G_PTRTOINT:
4473b953c37SPetar Jovanovic   case G_AND:
4483b953c37SPetar Jovanovic   case G_OR:
4493b953c37SPetar Jovanovic   case G_XOR:
4503b953c37SPetar Jovanovic   case G_SHL:
4513b953c37SPetar Jovanovic   case G_ASHR:
4523b953c37SPetar Jovanovic   case G_LSHR:
453ff6ac1ebSPetar Avramovic   case G_BRINDIRECT:
454c063b0b0SPetar Avramovic   case G_VASTART:
45594a24e7aSPetar Avramovic   case G_BSWAP:
456*2b66d32fSPetar Avramovic   case G_CTLZ:
457366857a2SPetar Jovanovic     OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx];
458366857a2SPetar Jovanovic     break;
459e4af9de3SPetar Avramovic   case G_ADD:
460c46d24f5SPetar Avramovic   case G_SUB:
461d1815dacSPetar Avramovic   case G_MUL:
4621d7f79c0SPetar Avramovic   case G_SDIV:
4631d7f79c0SPetar Avramovic   case G_SREM:
4641d7f79c0SPetar Avramovic   case G_UDIV:
4651d7f79c0SPetar Avramovic   case G_UREM:
466e4af9de3SPetar Avramovic     OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx];
467e4af9de3SPetar Avramovic     if (Op0Size == 128)
468e4af9de3SPetar Avramovic       OperandsMapping = getMSAMapping(MF);
469e4af9de3SPetar Avramovic     break;
470f7c213c9SPetar Avramovic   case G_STORE:
471f7c213c9SPetar Avramovic   case G_LOAD:
472599591f3SPetar Avramovic     if (Op0Size == 128) {
473599591f3SPetar Avramovic       OperandsMapping = getOperandsMapping(
474599591f3SPetar Avramovic           {getMSAMapping(MF), &Mips::ValueMappings[Mips::GPRIdx]});
475599591f3SPetar Avramovic       break;
476599591f3SPetar Avramovic     }
477599591f3SPetar Avramovic 
478f7c213c9SPetar Avramovic     if (!Op0Ty.isPointer())
479aa699b20SPetar Avramovic       InstTy = TI.determineInstType(&MI);
480aa699b20SPetar Avramovic 
481e3bb0a72SPetar Avramovic     if (InstTy == InstType::FloatingPoint ||
482f7c213c9SPetar Avramovic         (Op0Size == 64 && InstTy == InstType::Ambiguous))
483f7c213c9SPetar Avramovic       OperandsMapping = getOperandsMapping(
484f7c213c9SPetar Avramovic           {getFprbMapping(Op0Size), &Mips::ValueMappings[Mips::GPRIdx]});
485f7c213c9SPetar Avramovic     else
486aa699b20SPetar Avramovic       OperandsMapping =
487f7c213c9SPetar Avramovic           getOperandsMapping({getGprbOrCustomMapping(Op0Size, MappingID),
488aa699b20SPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
489aa699b20SPetar Avramovic 
490aa699b20SPetar Avramovic     break;
491f7c213c9SPetar Avramovic   case G_PHI:
492f7c213c9SPetar Avramovic     if (!Op0Ty.isPointer())
493ec575f6eSPetar Avramovic       InstTy = TI.determineInstType(&MI);
494be20e361SPetar Avramovic 
495be20e361SPetar Avramovic     // PHI is copylike and should have one regbank in mapping for def register.
496f7c213c9SPetar Avramovic     if (InstTy == InstType::Integer && Op0Size == 64) {
497be20e361SPetar Avramovic       OperandsMapping =
498be20e361SPetar Avramovic           getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx]});
499be20e361SPetar Avramovic       return getInstructionMapping(CustomMappingID, /*Cost=*/1, OperandsMapping,
500be20e361SPetar Avramovic                                    /*NumOperands=*/1);
501be20e361SPetar Avramovic     }
502be20e361SPetar Avramovic     // Use default handling for PHI, i.e. set reg bank of def operand to match
503be20e361SPetar Avramovic     // register banks of use operands.
504f7c213c9SPetar Avramovic     return getInstrMappingImpl(MI);
505dbb6d01dSPetar Avramovic   case G_SELECT: {
506f7c213c9SPetar Avramovic     if (!Op0Ty.isPointer())
507dbb6d01dSPetar Avramovic       InstTy = TI.determineInstType(&MI);
508dbb6d01dSPetar Avramovic 
509e3bb0a72SPetar Avramovic     if (InstTy == InstType::FloatingPoint ||
510f7c213c9SPetar Avramovic         (Op0Size == 64 && InstTy == InstType::Ambiguous)) {
511f7c213c9SPetar Avramovic       const RegisterBankInfo::ValueMapping *Bank = getFprbMapping(Op0Size);
512dbb6d01dSPetar Avramovic       OperandsMapping = getOperandsMapping(
513dbb6d01dSPetar Avramovic           {Bank, &Mips::ValueMappings[Mips::GPRIdx], Bank, Bank});
514dbb6d01dSPetar Avramovic       break;
515f7c213c9SPetar Avramovic     } else {
516dbb6d01dSPetar Avramovic       const RegisterBankInfo::ValueMapping *Bank =
517f7c213c9SPetar Avramovic           getGprbOrCustomMapping(Op0Size, MappingID);
518dbb6d01dSPetar Avramovic       OperandsMapping = getOperandsMapping(
519dbb6d01dSPetar Avramovic           {Bank, &Mips::ValueMappings[Mips::GPRIdx], Bank, Bank});
520dbb6d01dSPetar Avramovic     }
521dbb6d01dSPetar Avramovic     break;
522dbb6d01dSPetar Avramovic   }
523f7c213c9SPetar Avramovic   case G_IMPLICIT_DEF:
524f7c213c9SPetar Avramovic     if (!Op0Ty.isPointer())
52575e43a60SPetar Avramovic       InstTy = TI.determineInstType(&MI);
52675e43a60SPetar Avramovic 
527f7c213c9SPetar Avramovic     if (InstTy == InstType::FloatingPoint)
528f7c213c9SPetar Avramovic       OperandsMapping = getFprbMapping(Op0Size);
529f7c213c9SPetar Avramovic     else
530f7c213c9SPetar Avramovic       OperandsMapping = getGprbOrCustomMapping(Op0Size, MappingID);
531f7c213c9SPetar Avramovic 
53275e43a60SPetar Avramovic     break;
533f7c213c9SPetar Avramovic   case G_UNMERGE_VALUES:
534aa699b20SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx],
535aa699b20SPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx],
536aa699b20SPetar Avramovic                                           &Mips::ValueMappings[Mips::DPRIdx]});
537aa699b20SPetar Avramovic     MappingID = CustomMappingID;
538aa699b20SPetar Avramovic     break;
539f7c213c9SPetar Avramovic   case G_MERGE_VALUES:
540ec575f6eSPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx],
541ec575f6eSPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx],
542ec575f6eSPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx]});
543ec575f6eSPetar Avramovic     MappingID = CustomMappingID;
544ec575f6eSPetar Avramovic     break;
545afa3afa3SPetar Avramovic   case G_FADD:
546afa3afa3SPetar Avramovic   case G_FSUB:
547afa3afa3SPetar Avramovic   case G_FMUL:
5480a1fd355SPetar Avramovic   case G_FDIV:
549e3b49df5SPetar Avramovic   case G_FABS:
550417dd678SPetar Avramovic   case G_FSQRT:
551f7c213c9SPetar Avramovic     OperandsMapping = getFprbMapping(Op0Size);
552914ce664SPetar Avramovic     if (Op0Size == 128)
553914ce664SPetar Avramovic       OperandsMapping = getMSAMapping(MF);
554afa3afa3SPetar Avramovic     break;
555f7c213c9SPetar Avramovic   case G_FCONSTANT:
556f7c213c9SPetar Avramovic     OperandsMapping = getOperandsMapping({getFprbMapping(Op0Size), nullptr});
5571af05df3SPetar Avramovic     break;
55822e99c43SPetar Avramovic   case G_FCMP: {
559f7c213c9SPetar Avramovic     unsigned Op2Size = MRI.getType(MI.getOperand(2).getReg()).getSizeInBits();
56022e99c43SPetar Avramovic     OperandsMapping =
56122e99c43SPetar Avramovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr,
562f7c213c9SPetar Avramovic                             getFprbMapping(Op2Size), getFprbMapping(Op2Size)});
56322e99c43SPetar Avramovic     break;
56422e99c43SPetar Avramovic   }
565a7d00064SPetar Avramovic   case G_FPEXT:
566a7d00064SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx],
567a7d00064SPetar Avramovic                                           &Mips::ValueMappings[Mips::SPRIdx]});
568a7d00064SPetar Avramovic     break;
569a7d00064SPetar Avramovic   case G_FPTRUNC:
570a7d00064SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::SPRIdx],
571a7d00064SPetar Avramovic                                           &Mips::ValueMappings[Mips::DPRIdx]});
572a7d00064SPetar Avramovic     break;
5734b4dae1cSPetar Avramovic   case G_FPTOSI: {
574f7c213c9SPetar Avramovic     assert((Op0Size == 32) && "Unsupported integer size");
5754b4dae1cSPetar Avramovic     unsigned SizeFP = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
576f7c213c9SPetar Avramovic     OperandsMapping = getOperandsMapping(
577f7c213c9SPetar Avramovic         {&Mips::ValueMappings[Mips::GPRIdx], getFprbMapping(SizeFP)});
578f7c213c9SPetar Avramovic     break;
579f7c213c9SPetar Avramovic   }
580f7c213c9SPetar Avramovic   case G_SITOFP:
581f7c213c9SPetar Avramovic     assert((MRI.getType(MI.getOperand(1).getReg()).getSizeInBits() == 32) &&
582ddd056c9SFangrui Song            "Unsupported integer size");
583f7c213c9SPetar Avramovic     OperandsMapping = getOperandsMapping(
584f7c213c9SPetar Avramovic         {getFprbMapping(Op0Size), &Mips::ValueMappings[Mips::GPRIdx]});
5854b4dae1cSPetar Avramovic     break;
586021e4c82SPetar Jovanovic   case G_CONSTANT:
587021e4c82SPetar Jovanovic   case G_FRAME_INDEX:
58864c10ba8SPetar Jovanovic   case G_GLOBAL_VALUE:
589caef9306SPetar Avramovic   case G_JUMP_TABLE:
5905d9b8eedSPetar Avramovic   case G_BRCOND:
591021e4c82SPetar Jovanovic     OperandsMapping =
592021e4c82SPetar Jovanovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr});
593021e4c82SPetar Jovanovic     break;
594caef9306SPetar Avramovic   case G_BRJT:
595caef9306SPetar Avramovic     OperandsMapping =
596caef9306SPetar Avramovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr,
597caef9306SPetar Avramovic                             &Mips::ValueMappings[Mips::GPRIdx]});
598caef9306SPetar Avramovic     break;
599ce4dd0aeSPetar Jovanovic   case G_ICMP:
600ce4dd0aeSPetar Jovanovic     OperandsMapping =
601ce4dd0aeSPetar Jovanovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr,
602ce4dd0aeSPetar Jovanovic                             &Mips::ValueMappings[Mips::GPRIdx],
603ce4dd0aeSPetar Jovanovic                             &Mips::ValueMappings[Mips::GPRIdx]});
604ce4dd0aeSPetar Jovanovic     break;
605366857a2SPetar Jovanovic   default:
606366857a2SPetar Jovanovic     return getInvalidInstructionMapping();
607366857a2SPetar Jovanovic   }
608366857a2SPetar Jovanovic 
609ec575f6eSPetar Avramovic   return getInstructionMapping(MappingID, /*Cost=*/1, OperandsMapping,
610366857a2SPetar Jovanovic                                NumOperands);
611366857a2SPetar Jovanovic }
612ec575f6eSPetar Avramovic 
613ec575f6eSPetar Avramovic using InstListTy = GISelWorkList<4>;
614ec575f6eSPetar Avramovic namespace {
615ec575f6eSPetar Avramovic class InstManager : public GISelChangeObserver {
616ec575f6eSPetar Avramovic   InstListTy &InstList;
617ec575f6eSPetar Avramovic 
618ec575f6eSPetar Avramovic public:
619ec575f6eSPetar Avramovic   InstManager(InstListTy &Insts) : InstList(Insts) {}
620ec575f6eSPetar Avramovic 
621ec575f6eSPetar Avramovic   void createdInstr(MachineInstr &MI) override { InstList.insert(&MI); }
622ec575f6eSPetar Avramovic   void erasingInstr(MachineInstr &MI) override {}
623ec575f6eSPetar Avramovic   void changingInstr(MachineInstr &MI) override {}
624ec575f6eSPetar Avramovic   void changedInstr(MachineInstr &MI) override {}
625ec575f6eSPetar Avramovic };
626ec575f6eSPetar Avramovic } // end anonymous namespace
627ec575f6eSPetar Avramovic 
628f7c213c9SPetar Avramovic void MipsRegisterBankInfo::setRegBank(MachineInstr &MI,
629f7c213c9SPetar Avramovic                                       MachineRegisterInfo &MRI) const {
630f7c213c9SPetar Avramovic   Register Dest = MI.getOperand(0).getReg();
631f7c213c9SPetar Avramovic   switch (MI.getOpcode()) {
632f7c213c9SPetar Avramovic   case TargetOpcode::G_STORE:
633f7c213c9SPetar Avramovic     // No def operands, skip this instruction.
634f7c213c9SPetar Avramovic     break;
635f7c213c9SPetar Avramovic   case TargetOpcode::G_CONSTANT:
636f7c213c9SPetar Avramovic   case TargetOpcode::G_LOAD:
637f7c213c9SPetar Avramovic   case TargetOpcode::G_SELECT:
638f7c213c9SPetar Avramovic   case TargetOpcode::G_PHI:
639f7c213c9SPetar Avramovic   case TargetOpcode::G_IMPLICIT_DEF: {
640f7c213c9SPetar Avramovic     assert(MRI.getType(Dest) == LLT::scalar(32) && "Unexpected operand type.");
641f7c213c9SPetar Avramovic     MRI.setRegBank(Dest, getRegBank(Mips::GPRBRegBankID));
642f7c213c9SPetar Avramovic     break;
643f7c213c9SPetar Avramovic   }
644e74c5b96SDaniel Sanders   case TargetOpcode::G_PTR_ADD: {
645f7c213c9SPetar Avramovic     assert(MRI.getType(Dest).isPointer() && "Unexpected operand type.");
646f7c213c9SPetar Avramovic     MRI.setRegBank(Dest, getRegBank(Mips::GPRBRegBankID));
647f7c213c9SPetar Avramovic     break;
648f7c213c9SPetar Avramovic   }
649f7c213c9SPetar Avramovic   default:
650f7c213c9SPetar Avramovic     llvm_unreachable("Unexpected opcode.");
651f7c213c9SPetar Avramovic   }
652f7c213c9SPetar Avramovic }
653f7c213c9SPetar Avramovic 
654f7c213c9SPetar Avramovic static void
655f7c213c9SPetar Avramovic combineAwayG_UNMERGE_VALUES(LegalizationArtifactCombiner &ArtCombiner,
656f7c213c9SPetar Avramovic                             MachineInstr &MI) {
6578731799fSRoman Tereshin   SmallVector<Register, 4> UpdatedDefs;
658f7c213c9SPetar Avramovic   SmallVector<MachineInstr *, 2> DeadInstrs;
6598731799fSRoman Tereshin   ArtCombiner.tryCombineMerges(MI, DeadInstrs, UpdatedDefs);
660f7c213c9SPetar Avramovic   for (MachineInstr *DeadMI : DeadInstrs)
661f7c213c9SPetar Avramovic     DeadMI->eraseFromParent();
662f7c213c9SPetar Avramovic }
663f7c213c9SPetar Avramovic 
664ec575f6eSPetar Avramovic void MipsRegisterBankInfo::applyMappingImpl(
665ec575f6eSPetar Avramovic     const OperandsMapper &OpdMapper) const {
666ec575f6eSPetar Avramovic   MachineInstr &MI = OpdMapper.getMI();
667ec575f6eSPetar Avramovic   InstListTy NewInstrs;
668ec575f6eSPetar Avramovic   MachineIRBuilder B(MI);
669ec575f6eSPetar Avramovic   MachineFunction *MF = MI.getMF();
670ec575f6eSPetar Avramovic   MachineRegisterInfo &MRI = OpdMapper.getMRI();
671f7c213c9SPetar Avramovic   const LegalizerInfo &LegInfo = *MF->getSubtarget().getLegalizerInfo();
672ec575f6eSPetar Avramovic 
673ec575f6eSPetar Avramovic   InstManager NewInstrObserver(NewInstrs);
674ec575f6eSPetar Avramovic   GISelObserverWrapper WrapperObserver(&NewInstrObserver);
675ec575f6eSPetar Avramovic   LegalizerHelper Helper(*MF, WrapperObserver, B);
676f7c213c9SPetar Avramovic   LegalizationArtifactCombiner ArtCombiner(B, MF->getRegInfo(), LegInfo);
677ec575f6eSPetar Avramovic 
678ec575f6eSPetar Avramovic   switch (MI.getOpcode()) {
679aa699b20SPetar Avramovic   case TargetOpcode::G_LOAD:
680dbb6d01dSPetar Avramovic   case TargetOpcode::G_STORE:
681be20e361SPetar Avramovic   case TargetOpcode::G_PHI:
68275e43a60SPetar Avramovic   case TargetOpcode::G_SELECT:
68375e43a60SPetar Avramovic   case TargetOpcode::G_IMPLICIT_DEF: {
684ec575f6eSPetar Avramovic     Helper.narrowScalar(MI, 0, LLT::scalar(32));
685ec575f6eSPetar Avramovic     // Handle new instructions.
686ec575f6eSPetar Avramovic     while (!NewInstrs.empty()) {
687ec575f6eSPetar Avramovic       MachineInstr *NewMI = NewInstrs.pop_back_val();
688ec575f6eSPetar Avramovic       // This is new G_UNMERGE that was created during narrowScalar and will
689ec575f6eSPetar Avramovic       // not be considered for regbank selection. RegBankSelect for mips
690ec575f6eSPetar Avramovic       // visits/makes corresponding G_MERGE first. Combine them here.
691f7c213c9SPetar Avramovic       if (NewMI->getOpcode() == TargetOpcode::G_UNMERGE_VALUES)
692f7c213c9SPetar Avramovic         combineAwayG_UNMERGE_VALUES(ArtCombiner, *NewMI);
693aa699b20SPetar Avramovic       // This G_MERGE will be combined away when its corresponding G_UNMERGE
694aa699b20SPetar Avramovic       // gets regBankSelected.
695aa699b20SPetar Avramovic       else if (NewMI->getOpcode() == TargetOpcode::G_MERGE_VALUES)
696aa699b20SPetar Avramovic         continue;
697aa699b20SPetar Avramovic       else
698f7c213c9SPetar Avramovic         // Manually set register banks for def operands to 32 bit gprb.
699f7c213c9SPetar Avramovic         setRegBank(*NewMI, MRI);
700ec575f6eSPetar Avramovic     }
701ec575f6eSPetar Avramovic     return;
702ec575f6eSPetar Avramovic   }
703f7c213c9SPetar Avramovic   case TargetOpcode::G_UNMERGE_VALUES:
704f7c213c9SPetar Avramovic     combineAwayG_UNMERGE_VALUES(ArtCombiner, MI);
705aa699b20SPetar Avramovic     return;
706ec575f6eSPetar Avramovic   default:
707ec575f6eSPetar Avramovic     break;
708ec575f6eSPetar Avramovic   }
709ec575f6eSPetar Avramovic 
710ec575f6eSPetar Avramovic   return applyDefaultMapping(OpdMapper);
711ec575f6eSPetar Avramovic }
712