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"
15ec575f6eSPetar Avramovic #include "llvm/CodeGen/GlobalISel/GISelChangeObserver.h"
16ec575f6eSPetar Avramovic #include "llvm/CodeGen/GlobalISel/LegalizationArtifactCombiner.h"
17ec575f6eSPetar Avramovic #include "llvm/CodeGen/GlobalISel/LegalizerHelper.h"
18fac93e28SPetar Jovanovic #include "llvm/CodeGen/MachineRegisterInfo.h"
19fac93e28SPetar Jovanovic 
20366857a2SPetar Jovanovic #define GET_TARGET_REGBANK_IMPL
21fac93e28SPetar Jovanovic 
22366857a2SPetar Jovanovic #include "MipsGenRegisterBank.inc"
23366857a2SPetar Jovanovic 
24366857a2SPetar Jovanovic namespace llvm {
25366857a2SPetar Jovanovic namespace Mips {
26366857a2SPetar Jovanovic enum PartialMappingIdx {
27366857a2SPetar Jovanovic   PMI_GPR,
281af05df3SPetar Avramovic   PMI_SPR,
291af05df3SPetar Avramovic   PMI_DPR,
30366857a2SPetar Jovanovic   PMI_Min = PMI_GPR,
31366857a2SPetar Jovanovic };
32366857a2SPetar Jovanovic 
33366857a2SPetar Jovanovic RegisterBankInfo::PartialMapping PartMappings[]{
341af05df3SPetar Avramovic     {0, 32, GPRBRegBank},
351af05df3SPetar Avramovic     {0, 32, FPRBRegBank},
361af05df3SPetar Avramovic     {0, 64, FPRBRegBank}
37366857a2SPetar Jovanovic };
38366857a2SPetar Jovanovic 
391af05df3SPetar Avramovic enum ValueMappingIdx {
401af05df3SPetar Avramovic     InvalidIdx = 0,
411af05df3SPetar Avramovic     GPRIdx = 1,
421af05df3SPetar Avramovic     SPRIdx = 4,
431af05df3SPetar Avramovic     DPRIdx = 7
441af05df3SPetar Avramovic };
45366857a2SPetar Jovanovic 
46366857a2SPetar Jovanovic RegisterBankInfo::ValueMapping ValueMappings[] = {
47366857a2SPetar Jovanovic     // invalid
48366857a2SPetar Jovanovic     {nullptr, 0},
491af05df3SPetar Avramovic     // up to 3 operands in GPRs
50366857a2SPetar Jovanovic     {&PartMappings[PMI_GPR - PMI_Min], 1},
51366857a2SPetar Jovanovic     {&PartMappings[PMI_GPR - PMI_Min], 1},
521af05df3SPetar Avramovic     {&PartMappings[PMI_GPR - PMI_Min], 1},
531af05df3SPetar Avramovic     // up to 3 ops operands FPRs - single precission
541af05df3SPetar Avramovic     {&PartMappings[PMI_SPR - PMI_Min], 1},
551af05df3SPetar Avramovic     {&PartMappings[PMI_SPR - PMI_Min], 1},
561af05df3SPetar Avramovic     {&PartMappings[PMI_SPR - PMI_Min], 1},
571af05df3SPetar Avramovic     // up to 3 ops operands FPRs - double precission
581af05df3SPetar Avramovic     {&PartMappings[PMI_DPR - PMI_Min], 1},
591af05df3SPetar Avramovic     {&PartMappings[PMI_DPR - PMI_Min], 1},
601af05df3SPetar Avramovic     {&PartMappings[PMI_DPR - PMI_Min], 1}
611af05df3SPetar Avramovic };
62366857a2SPetar Jovanovic 
63366857a2SPetar Jovanovic } // end namespace Mips
64366857a2SPetar Jovanovic } // end namespace llvm
65366857a2SPetar Jovanovic 
66366857a2SPetar Jovanovic using namespace llvm;
67fac93e28SPetar Jovanovic 
68fac93e28SPetar Jovanovic MipsRegisterBankInfo::MipsRegisterBankInfo(const TargetRegisterInfo &TRI)
69fac93e28SPetar Jovanovic     : MipsGenRegisterBankInfo() {}
70366857a2SPetar Jovanovic 
71366857a2SPetar Jovanovic const RegisterBank &MipsRegisterBankInfo::getRegBankFromRegClass(
72366857a2SPetar Jovanovic     const TargetRegisterClass &RC) const {
73366857a2SPetar Jovanovic   using namespace Mips;
74366857a2SPetar Jovanovic 
75366857a2SPetar Jovanovic   switch (RC.getID()) {
76366857a2SPetar Jovanovic   case Mips::GPR32RegClassID:
77366857a2SPetar Jovanovic   case Mips::CPU16Regs_and_GPRMM16ZeroRegClassID:
78852dd83bSSimon Atanasyan   case Mips::GPRMM16MovePPairFirstRegClassID:
79852dd83bSSimon Atanasyan   case Mips::CPU16Regs_and_GPRMM16MovePPairSecondRegClassID:
80366857a2SPetar Jovanovic   case Mips::GPRMM16MoveP_and_CPU16Regs_and_GPRMM16ZeroRegClassID:
81852dd83bSSimon Atanasyan   case Mips::GPRMM16MovePPairFirst_and_GPRMM16MovePPairSecondRegClassID:
82021e4c82SPetar Jovanovic   case Mips::SP32RegClassID:
83efcd3c00SPetar Avramovic   case Mips::GP32RegClassID:
84366857a2SPetar Jovanovic     return getRegBank(Mips::GPRBRegBankID);
853dfa368dSPetar Avramovic   case Mips::FGRCCRegClassID:
86afa3afa3SPetar Avramovic   case Mips::FGR32RegClassID:
873dfa368dSPetar Avramovic   case Mips::FGR64RegClassID:
883dfa368dSPetar Avramovic   case Mips::AFGR64RegClassID:
893dfa368dSPetar Avramovic     return getRegBank(Mips::FPRBRegBankID);
90366857a2SPetar Jovanovic   default:
91366857a2SPetar Jovanovic     llvm_unreachable("Register class not supported");
92366857a2SPetar Jovanovic   }
93366857a2SPetar Jovanovic }
94366857a2SPetar Jovanovic 
95ec575f6eSPetar Avramovic // Instructions where all register operands are floating point.
96ec575f6eSPetar Avramovic static bool isFloatingPointOpcode(unsigned Opc) {
97ec575f6eSPetar Avramovic   switch (Opc) {
98ec575f6eSPetar Avramovic   case TargetOpcode::G_FCONSTANT:
99ec575f6eSPetar Avramovic   case TargetOpcode::G_FADD:
100ec575f6eSPetar Avramovic   case TargetOpcode::G_FSUB:
101ec575f6eSPetar Avramovic   case TargetOpcode::G_FMUL:
102ec575f6eSPetar Avramovic   case TargetOpcode::G_FDIV:
103ec575f6eSPetar Avramovic   case TargetOpcode::G_FABS:
104ec575f6eSPetar Avramovic   case TargetOpcode::G_FSQRT:
105ec575f6eSPetar Avramovic   case TargetOpcode::G_FCEIL:
106ec575f6eSPetar Avramovic   case TargetOpcode::G_FFLOOR:
107ec575f6eSPetar Avramovic   case TargetOpcode::G_FPEXT:
108ec575f6eSPetar Avramovic   case TargetOpcode::G_FPTRUNC:
109ec575f6eSPetar Avramovic     return true;
110ec575f6eSPetar Avramovic   default:
111ec575f6eSPetar Avramovic     return false;
112ec575f6eSPetar Avramovic   }
113ec575f6eSPetar Avramovic }
114ec575f6eSPetar Avramovic 
115aa699b20SPetar Avramovic // Instructions where use operands are floating point registers.
116aa699b20SPetar Avramovic // Def operands are general purpose.
117aa699b20SPetar Avramovic static bool isFloatingPointOpcodeUse(unsigned Opc) {
118aa699b20SPetar Avramovic   switch (Opc) {
119aa699b20SPetar Avramovic   case TargetOpcode::G_FPTOSI:
120aa699b20SPetar Avramovic   case TargetOpcode::G_FPTOUI:
121aa699b20SPetar Avramovic   case TargetOpcode::G_FCMP:
122aa699b20SPetar Avramovic   case Mips::MFC1:
123aa699b20SPetar Avramovic   case Mips::ExtractElementF64:
124aa699b20SPetar Avramovic   case Mips::ExtractElementF64_64:
125aa699b20SPetar Avramovic     return true;
126aa699b20SPetar Avramovic   default:
127aa699b20SPetar Avramovic     return isFloatingPointOpcode(Opc);
128aa699b20SPetar Avramovic   }
129aa699b20SPetar Avramovic }
130aa699b20SPetar Avramovic 
131ec575f6eSPetar Avramovic // Instructions where def operands are floating point registers.
132ec575f6eSPetar Avramovic // Use operands are general purpose.
133ec575f6eSPetar Avramovic static bool isFloatingPointOpcodeDef(unsigned Opc) {
134ec575f6eSPetar Avramovic   switch (Opc) {
135ec575f6eSPetar Avramovic   case TargetOpcode::G_SITOFP:
136ec575f6eSPetar Avramovic   case TargetOpcode::G_UITOFP:
137ec575f6eSPetar Avramovic   case Mips::MTC1:
138ec575f6eSPetar Avramovic   case Mips::BuildPairF64:
139ec575f6eSPetar Avramovic   case Mips::BuildPairF64_64:
140ec575f6eSPetar Avramovic     return true;
141ec575f6eSPetar Avramovic   default:
142ec575f6eSPetar Avramovic     return isFloatingPointOpcode(Opc);
143ec575f6eSPetar Avramovic   }
144ec575f6eSPetar Avramovic }
145ec575f6eSPetar Avramovic 
146ec575f6eSPetar Avramovic static bool isAmbiguous(unsigned Opc) {
147ec575f6eSPetar Avramovic   switch (Opc) {
148ec575f6eSPetar Avramovic   case TargetOpcode::G_LOAD:
149ec575f6eSPetar Avramovic   case TargetOpcode::G_STORE:
150ec575f6eSPetar Avramovic   case TargetOpcode::G_PHI:
151ec575f6eSPetar Avramovic   case TargetOpcode::G_SELECT:
152ec575f6eSPetar Avramovic     return true;
153ec575f6eSPetar Avramovic   default:
154ec575f6eSPetar Avramovic     return false;
155ec575f6eSPetar Avramovic   }
156ec575f6eSPetar Avramovic }
157ec575f6eSPetar Avramovic 
158aa699b20SPetar Avramovic void MipsRegisterBankInfo::AmbiguousRegDefUseContainer::addDefUses(
159aa699b20SPetar Avramovic     Register Reg, const MachineRegisterInfo &MRI) {
160aa699b20SPetar Avramovic   assert(!MRI.getType(Reg).isPointer() &&
161aa699b20SPetar Avramovic          "Pointers are gprb, they should not be considered as ambiguous.\n");
162aa699b20SPetar Avramovic   for (MachineInstr &UseMI : MRI.use_instructions(Reg)) {
16396252407SPetar Avramovic     MachineInstr *NonCopyInstr = skipCopiesOutgoing(&UseMI);
16496252407SPetar Avramovic     // Copy with many uses.
16596252407SPetar Avramovic     if (NonCopyInstr->getOpcode() == TargetOpcode::COPY &&
1662bea69bfSDaniel Sanders         !Register::isPhysicalRegister(NonCopyInstr->getOperand(0).getReg()))
16796252407SPetar Avramovic       addDefUses(NonCopyInstr->getOperand(0).getReg(), MRI);
16896252407SPetar Avramovic     else
16996252407SPetar Avramovic       DefUses.push_back(skipCopiesOutgoing(&UseMI));
170aa699b20SPetar Avramovic   }
171aa699b20SPetar Avramovic }
172aa699b20SPetar Avramovic 
173ec575f6eSPetar Avramovic void MipsRegisterBankInfo::AmbiguousRegDefUseContainer::addUseDef(
174ec575f6eSPetar Avramovic     Register Reg, const MachineRegisterInfo &MRI) {
175ec575f6eSPetar Avramovic   assert(!MRI.getType(Reg).isPointer() &&
176ec575f6eSPetar Avramovic          "Pointers are gprb, they should not be considered as ambiguous.\n");
177ec575f6eSPetar Avramovic   MachineInstr *DefMI = MRI.getVRegDef(Reg);
17896252407SPetar Avramovic   UseDefs.push_back(skipCopiesIncoming(DefMI));
17996252407SPetar Avramovic }
180ec575f6eSPetar Avramovic 
18196252407SPetar Avramovic MachineInstr *
18296252407SPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::skipCopiesOutgoing(
18396252407SPetar Avramovic     MachineInstr *MI) const {
18496252407SPetar Avramovic   const MachineFunction &MF = *MI->getParent()->getParent();
18596252407SPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
18696252407SPetar Avramovic   MachineInstr *Ret = MI;
18796252407SPetar Avramovic   while (Ret->getOpcode() == TargetOpcode::COPY &&
1882bea69bfSDaniel Sanders          !Register::isPhysicalRegister(Ret->getOperand(0).getReg()) &&
18996252407SPetar Avramovic          MRI.hasOneUse(Ret->getOperand(0).getReg())) {
19096252407SPetar Avramovic     Ret = &(*MRI.use_instr_begin(Ret->getOperand(0).getReg()));
19196252407SPetar Avramovic   }
19296252407SPetar Avramovic   return Ret;
19396252407SPetar Avramovic }
19496252407SPetar Avramovic 
19596252407SPetar Avramovic MachineInstr *
19696252407SPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::skipCopiesIncoming(
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(1).getReg()))
20396252407SPetar Avramovic     Ret = MRI.getVRegDef(Ret->getOperand(1).getReg());
20496252407SPetar Avramovic   return Ret;
205ec575f6eSPetar Avramovic }
206ec575f6eSPetar Avramovic 
207ec575f6eSPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::AmbiguousRegDefUseContainer(
208ec575f6eSPetar Avramovic     const MachineInstr *MI) {
209ec575f6eSPetar Avramovic   assert(isAmbiguous(MI->getOpcode()) &&
210ec575f6eSPetar Avramovic          "Not implemented for non Ambiguous opcode.\n");
211ec575f6eSPetar Avramovic 
212ec575f6eSPetar Avramovic   const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
213ec575f6eSPetar Avramovic 
214aa699b20SPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_LOAD)
215aa699b20SPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
216aa699b20SPetar Avramovic 
217ec575f6eSPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_STORE)
218ec575f6eSPetar Avramovic     addUseDef(MI->getOperand(0).getReg(), MRI);
219dbb6d01dSPetar Avramovic 
220be20e361SPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_PHI) {
221be20e361SPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
222be20e361SPetar Avramovic 
223be20e361SPetar Avramovic     for (unsigned i = 1; i < MI->getNumOperands(); i += 2)
224be20e361SPetar Avramovic       addUseDef(MI->getOperand(i).getReg(), MRI);
225be20e361SPetar Avramovic   }
226be20e361SPetar Avramovic 
227dbb6d01dSPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_SELECT) {
228dbb6d01dSPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
229dbb6d01dSPetar Avramovic 
230dbb6d01dSPetar Avramovic     addUseDef(MI->getOperand(2).getReg(), MRI);
231dbb6d01dSPetar Avramovic     addUseDef(MI->getOperand(3).getReg(), MRI);
232dbb6d01dSPetar Avramovic   }
233ec575f6eSPetar Avramovic }
234ec575f6eSPetar Avramovic 
235e3bb0a72SPetar Avramovic bool MipsRegisterBankInfo::TypeInfoForMF::visit(
236e3bb0a72SPetar Avramovic     const MachineInstr *MI, const MachineInstr *WaitingForTypeOfMI) {
237ec575f6eSPetar Avramovic   assert(isAmbiguous(MI->getOpcode()) && "Visiting non-Ambiguous opcode.\n");
238e3bb0a72SPetar Avramovic   if (wasVisited(MI))
239e3bb0a72SPetar Avramovic     return true; // InstType has already been determined for MI.
240ec575f6eSPetar Avramovic 
241ec575f6eSPetar Avramovic   startVisit(MI);
242ec575f6eSPetar Avramovic   AmbiguousRegDefUseContainer DefUseContainer(MI);
243ec575f6eSPetar Avramovic 
244aa699b20SPetar Avramovic   // Visit instructions where MI's DEF operands are USED.
245aa699b20SPetar Avramovic   if (visitAdjacentInstrs(MI, DefUseContainer.getDefUses(), true))
246aa699b20SPetar Avramovic     return true;
247aa699b20SPetar Avramovic 
248ec575f6eSPetar Avramovic   // Visit instructions that DEFINE MI's USE operands.
249aa699b20SPetar Avramovic   if (visitAdjacentInstrs(MI, DefUseContainer.getUseDefs(), false))
250ec575f6eSPetar Avramovic     return true;
251ec575f6eSPetar Avramovic 
252e3bb0a72SPetar Avramovic   // All MI's adjacent instructions, are ambiguous.
253e3bb0a72SPetar Avramovic   if (!WaitingForTypeOfMI) {
254e3bb0a72SPetar Avramovic     // This is chain of ambiguous instructions.
255e3bb0a72SPetar Avramovic     setTypes(MI, InstType::Ambiguous);
256e3bb0a72SPetar Avramovic     return true;
257e3bb0a72SPetar Avramovic   }
258e3bb0a72SPetar Avramovic   // Excluding WaitingForTypeOfMI, MI is either connected to chains of ambiguous
259e3bb0a72SPetar Avramovic   // instructions or has no other adjacent instructions. Anyway InstType could
260e3bb0a72SPetar Avramovic   // not be determined. There could be unexplored path from some of
261e3bb0a72SPetar Avramovic   // WaitingForTypeOfMI's adjacent instructions to an instruction with only one
262e3bb0a72SPetar Avramovic   // mapping available.
263e3bb0a72SPetar Avramovic   // We are done with this branch, add MI to WaitingForTypeOfMI's WaitingQueue,
264e3bb0a72SPetar Avramovic   // this way when WaitingForTypeOfMI figures out its InstType same InstType
265e3bb0a72SPetar Avramovic   // will be assigned to all instructions in this branch.
266e3bb0a72SPetar Avramovic   addToWaitingQueue(WaitingForTypeOfMI, MI);
267ec575f6eSPetar Avramovic   return false;
268ec575f6eSPetar Avramovic }
269ec575f6eSPetar Avramovic 
270ec575f6eSPetar Avramovic bool MipsRegisterBankInfo::TypeInfoForMF::visitAdjacentInstrs(
271aa699b20SPetar Avramovic     const MachineInstr *MI, SmallVectorImpl<MachineInstr *> &AdjacentInstrs,
272aa699b20SPetar Avramovic     bool isDefUse) {
273ec575f6eSPetar Avramovic   while (!AdjacentInstrs.empty()) {
274ec575f6eSPetar Avramovic     MachineInstr *AdjMI = AdjacentInstrs.pop_back_val();
275ec575f6eSPetar Avramovic 
276aa699b20SPetar Avramovic     if (isDefUse ? isFloatingPointOpcodeUse(AdjMI->getOpcode())
277aa699b20SPetar Avramovic                  : isFloatingPointOpcodeDef(AdjMI->getOpcode())) {
278ec575f6eSPetar Avramovic       setTypes(MI, InstType::FloatingPoint);
279ec575f6eSPetar Avramovic       return true;
280ec575f6eSPetar Avramovic     }
281ec575f6eSPetar Avramovic 
282ec575f6eSPetar Avramovic     // Determine InstType from register bank of phys register that is
283aa699b20SPetar Avramovic     // 'isDefUse ? def : use' of this copy.
284ec575f6eSPetar Avramovic     if (AdjMI->getOpcode() == TargetOpcode::COPY) {
285aa699b20SPetar Avramovic       setTypesAccordingToPhysicalRegister(MI, AdjMI, isDefUse ? 0 : 1);
286ec575f6eSPetar Avramovic       return true;
287ec575f6eSPetar Avramovic     }
288ec575f6eSPetar Avramovic 
289ec575f6eSPetar Avramovic     // Defaults to integer instruction. Includes G_MERGE_VALUES and
290ec575f6eSPetar Avramovic     // G_UNMERGE_VALUES.
291e3bb0a72SPetar Avramovic     if (!isAmbiguous(AdjMI->getOpcode())) {
292ec575f6eSPetar Avramovic       setTypes(MI, InstType::Integer);
293ec575f6eSPetar Avramovic       return true;
294ec575f6eSPetar Avramovic     }
295e3bb0a72SPetar Avramovic 
296e3bb0a72SPetar Avramovic     // When AdjMI was visited first, MI has to continue to explore remaining
297e3bb0a72SPetar Avramovic     // adjacent instructions and determine InstType without visiting AdjMI.
298e3bb0a72SPetar Avramovic     if (!wasVisited(AdjMI) ||
299e3bb0a72SPetar Avramovic         getRecordedTypeForInstr(AdjMI) != InstType::NotDetermined) {
300e3bb0a72SPetar Avramovic       if (visit(AdjMI, MI)) {
301e3bb0a72SPetar Avramovic         // InstType is successfully determined and is same as for AdjMI.
302e3bb0a72SPetar Avramovic         setTypes(MI, getRecordedTypeForInstr(AdjMI));
303e3bb0a72SPetar Avramovic         return true;
304e3bb0a72SPetar Avramovic       }
305e3bb0a72SPetar Avramovic     }
306e3bb0a72SPetar Avramovic   }
307ec575f6eSPetar Avramovic   return false;
308ec575f6eSPetar Avramovic }
309ec575f6eSPetar Avramovic 
310ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::setTypes(const MachineInstr *MI,
311ec575f6eSPetar Avramovic                                                    InstType InstTy) {
312ec575f6eSPetar Avramovic   changeRecordedTypeForInstr(MI, InstTy);
313e3bb0a72SPetar Avramovic   for (const MachineInstr *WaitingInstr : getWaitingQueueFor(MI)) {
314e3bb0a72SPetar Avramovic     setTypes(WaitingInstr, InstTy);
315e3bb0a72SPetar Avramovic   }
316ec575f6eSPetar Avramovic }
317ec575f6eSPetar Avramovic 
318ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::setTypesAccordingToPhysicalRegister(
319ec575f6eSPetar Avramovic     const MachineInstr *MI, const MachineInstr *CopyInst, unsigned Op) {
3202bea69bfSDaniel Sanders   assert((Register::isPhysicalRegister(CopyInst->getOperand(Op).getReg())) &&
321ec575f6eSPetar Avramovic          "Copies of non physical registers should not be considered here.\n");
322ec575f6eSPetar Avramovic 
323ec575f6eSPetar Avramovic   const MachineFunction &MF = *CopyInst->getMF();
324ec575f6eSPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
325ec575f6eSPetar Avramovic   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
326ec575f6eSPetar Avramovic   const RegisterBankInfo &RBI =
327ec575f6eSPetar Avramovic       *CopyInst->getMF()->getSubtarget().getRegBankInfo();
328ec575f6eSPetar Avramovic   const RegisterBank *Bank =
329ec575f6eSPetar Avramovic       RBI.getRegBank(CopyInst->getOperand(Op).getReg(), MRI, TRI);
330ec575f6eSPetar Avramovic 
331ec575f6eSPetar Avramovic   if (Bank == &Mips::FPRBRegBank)
332ec575f6eSPetar Avramovic     setTypes(MI, InstType::FloatingPoint);
333ec575f6eSPetar Avramovic   else if (Bank == &Mips::GPRBRegBank)
334ec575f6eSPetar Avramovic     setTypes(MI, InstType::Integer);
335ec575f6eSPetar Avramovic   else
336ec575f6eSPetar Avramovic     llvm_unreachable("Unsupported register bank.\n");
337ec575f6eSPetar Avramovic }
338ec575f6eSPetar Avramovic 
339ec575f6eSPetar Avramovic MipsRegisterBankInfo::InstType
340ec575f6eSPetar Avramovic MipsRegisterBankInfo::TypeInfoForMF::determineInstType(const MachineInstr *MI) {
341e3bb0a72SPetar Avramovic   visit(MI, nullptr);
342ec575f6eSPetar Avramovic   return getRecordedTypeForInstr(MI);
343ec575f6eSPetar Avramovic }
344ec575f6eSPetar Avramovic 
345ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::cleanupIfNewFunction(
346ec575f6eSPetar Avramovic     llvm::StringRef FunctionName) {
347ec575f6eSPetar Avramovic   if (MFName != FunctionName) {
348ec575f6eSPetar Avramovic     MFName = FunctionName;
349e3bb0a72SPetar Avramovic     WaitingQueues.clear();
350ec575f6eSPetar Avramovic     Types.clear();
351ec575f6eSPetar Avramovic   }
352ec575f6eSPetar Avramovic }
353ec575f6eSPetar Avramovic 
354366857a2SPetar Jovanovic const RegisterBankInfo::InstructionMapping &
355366857a2SPetar Jovanovic MipsRegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
356366857a2SPetar Jovanovic 
357ec575f6eSPetar Avramovic   static TypeInfoForMF TI;
358ec575f6eSPetar Avramovic 
359ec575f6eSPetar Avramovic   // Reset TI internal data when MF changes.
360ec575f6eSPetar Avramovic   TI.cleanupIfNewFunction(MI.getMF()->getName());
361ec575f6eSPetar Avramovic 
362366857a2SPetar Jovanovic   unsigned Opc = MI.getOpcode();
3631af05df3SPetar Avramovic   const MachineFunction &MF = *MI.getParent()->getParent();
3641af05df3SPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
365366857a2SPetar Jovanovic 
366be20e361SPetar Avramovic   if (MI.getOpcode() != TargetOpcode::G_PHI) {
367be20e361SPetar Avramovic     const RegisterBankInfo::InstructionMapping &Mapping =
368be20e361SPetar Avramovic         getInstrMappingImpl(MI);
369366857a2SPetar Jovanovic     if (Mapping.isValid())
370366857a2SPetar Jovanovic       return Mapping;
371be20e361SPetar Avramovic   }
372366857a2SPetar Jovanovic 
373366857a2SPetar Jovanovic   using namespace TargetOpcode;
374366857a2SPetar Jovanovic 
375366857a2SPetar Jovanovic   unsigned NumOperands = MI.getNumOperands();
376366857a2SPetar Jovanovic   const ValueMapping *OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx];
377ec575f6eSPetar Avramovic   unsigned MappingID = DefaultMappingID;
378ec575f6eSPetar Avramovic   const unsigned CustomMappingID = 1;
379366857a2SPetar Jovanovic 
380366857a2SPetar Jovanovic   switch (Opc) {
3817cecadb9SPetar Avramovic   case G_TRUNC:
382366857a2SPetar Jovanovic   case G_ADD:
3837cecadb9SPetar Avramovic   case G_SUB:
38456dc218dSPetar Avramovic   case G_MUL:
385a48285a1SPetar Avramovic   case G_UMULH:
38679df8596SPetar Avramovic   case G_ZEXTLOAD:
38779df8596SPetar Avramovic   case G_SEXTLOAD:
388021e4c82SPetar Jovanovic   case G_GEP:
389b1fc6f61SPetar Avramovic   case G_INTTOPTR:
390b1fc6f61SPetar Avramovic   case G_PTRTOINT:
3913b953c37SPetar Jovanovic   case G_AND:
3923b953c37SPetar Jovanovic   case G_OR:
3933b953c37SPetar Jovanovic   case G_XOR:
3943b953c37SPetar Jovanovic   case G_SHL:
3953b953c37SPetar Jovanovic   case G_ASHR:
3963b953c37SPetar Jovanovic   case G_LSHR:
3970a5e4eb7SPetar Avramovic   case G_SDIV:
3980a5e4eb7SPetar Avramovic   case G_UDIV:
3990a5e4eb7SPetar Avramovic   case G_SREM:
4000a5e4eb7SPetar Avramovic   case G_UREM:
401366857a2SPetar Jovanovic     OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx];
402366857a2SPetar Jovanovic     break;
403aa699b20SPetar Avramovic   case G_LOAD: {
404aa699b20SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
405aa699b20SPetar Avramovic     InstType InstTy = InstType::Integer;
406aa699b20SPetar Avramovic     if (!MRI.getType(MI.getOperand(0).getReg()).isPointer()) {
407aa699b20SPetar Avramovic       InstTy = TI.determineInstType(&MI);
408aa699b20SPetar Avramovic     }
409aa699b20SPetar Avramovic 
410e3bb0a72SPetar Avramovic     if (InstTy == InstType::FloatingPoint ||
411e3bb0a72SPetar Avramovic         (Size == 64 && InstTy == InstType::Ambiguous)) { // fprb
412aa699b20SPetar Avramovic       OperandsMapping =
413aa699b20SPetar Avramovic           getOperandsMapping({Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
414aa699b20SPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
415aa699b20SPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
416aa699b20SPetar Avramovic       break;
417aa699b20SPetar Avramovic     } else { // gprb
418aa699b20SPetar Avramovic       OperandsMapping =
419aa699b20SPetar Avramovic           getOperandsMapping({Size <= 32 ? &Mips::ValueMappings[Mips::GPRIdx]
420aa699b20SPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
421aa699b20SPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
422aa699b20SPetar Avramovic       if (Size == 64)
423aa699b20SPetar Avramovic         MappingID = CustomMappingID;
424aa699b20SPetar Avramovic     }
425aa699b20SPetar Avramovic 
426aa699b20SPetar Avramovic     break;
427aa699b20SPetar Avramovic   }
428ec575f6eSPetar Avramovic   case G_STORE: {
429ec575f6eSPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
430ec575f6eSPetar Avramovic     InstType InstTy = InstType::Integer;
431ec575f6eSPetar Avramovic     if (!MRI.getType(MI.getOperand(0).getReg()).isPointer()) {
432ec575f6eSPetar Avramovic       InstTy = TI.determineInstType(&MI);
433ec575f6eSPetar Avramovic     }
434ec575f6eSPetar Avramovic 
435e3bb0a72SPetar Avramovic     if (InstTy == InstType::FloatingPoint ||
436e3bb0a72SPetar Avramovic         (Size == 64 && InstTy == InstType::Ambiguous)) { // fprb
437ec575f6eSPetar Avramovic       OperandsMapping =
438ec575f6eSPetar Avramovic           getOperandsMapping({Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
439ec575f6eSPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
440ec575f6eSPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
441ec575f6eSPetar Avramovic       break;
442ec575f6eSPetar Avramovic     } else { // gprb
443ec575f6eSPetar Avramovic       OperandsMapping =
444ec575f6eSPetar Avramovic           getOperandsMapping({Size <= 32 ? &Mips::ValueMappings[Mips::GPRIdx]
445ec575f6eSPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
446ec575f6eSPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
447ec575f6eSPetar Avramovic       if (Size == 64)
448ec575f6eSPetar Avramovic         MappingID = CustomMappingID;
449ec575f6eSPetar Avramovic     }
450ec575f6eSPetar Avramovic     break;
451ec575f6eSPetar Avramovic   }
452be20e361SPetar Avramovic   case G_PHI: {
453be20e361SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
454be20e361SPetar Avramovic     InstType InstTy = InstType::Integer;
455be20e361SPetar Avramovic     if (!MRI.getType(MI.getOperand(0).getReg()).isPointer()) {
456be20e361SPetar Avramovic       InstTy = TI.determineInstType(&MI);
457be20e361SPetar Avramovic     }
458be20e361SPetar Avramovic 
459be20e361SPetar Avramovic     // PHI is copylike and should have one regbank in mapping for def register.
460be20e361SPetar Avramovic     if (InstTy == InstType::Integer && Size == 64) { // fprb
461be20e361SPetar Avramovic       OperandsMapping =
462be20e361SPetar Avramovic           getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx]});
463be20e361SPetar Avramovic       return getInstructionMapping(CustomMappingID, /*Cost=*/1, OperandsMapping,
464be20e361SPetar Avramovic                                    /*NumOperands=*/1);
465be20e361SPetar Avramovic     }
466be20e361SPetar Avramovic     // Use default handling for PHI, i.e. set reg bank of def operand to match
467be20e361SPetar Avramovic     // register banks of use operands.
468be20e361SPetar Avramovic     const RegisterBankInfo::InstructionMapping &Mapping =
469be20e361SPetar Avramovic         getInstrMappingImpl(MI);
470be20e361SPetar Avramovic     return Mapping;
471be20e361SPetar Avramovic   }
472dbb6d01dSPetar Avramovic   case G_SELECT: {
473dbb6d01dSPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
474dbb6d01dSPetar Avramovic     InstType InstTy = InstType::Integer;
475dbb6d01dSPetar Avramovic     if (!MRI.getType(MI.getOperand(0).getReg()).isPointer()) {
476dbb6d01dSPetar Avramovic       InstTy = TI.determineInstType(&MI);
477dbb6d01dSPetar Avramovic     }
478dbb6d01dSPetar Avramovic 
479e3bb0a72SPetar Avramovic     if (InstTy == InstType::FloatingPoint ||
480e3bb0a72SPetar Avramovic         (Size == 64 && InstTy == InstType::Ambiguous)) { // fprb
481dbb6d01dSPetar Avramovic       const RegisterBankInfo::ValueMapping *Bank =
482dbb6d01dSPetar Avramovic           Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
483dbb6d01dSPetar Avramovic                      : &Mips::ValueMappings[Mips::DPRIdx];
484dbb6d01dSPetar Avramovic       OperandsMapping = getOperandsMapping(
485dbb6d01dSPetar Avramovic           {Bank, &Mips::ValueMappings[Mips::GPRIdx], Bank, Bank});
486dbb6d01dSPetar Avramovic       break;
487dbb6d01dSPetar Avramovic     } else { // gprb
488dbb6d01dSPetar Avramovic       const RegisterBankInfo::ValueMapping *Bank =
489dbb6d01dSPetar Avramovic           Size <= 32 ? &Mips::ValueMappings[Mips::GPRIdx]
490dbb6d01dSPetar Avramovic                      : &Mips::ValueMappings[Mips::DPRIdx];
491dbb6d01dSPetar Avramovic       OperandsMapping = getOperandsMapping(
492dbb6d01dSPetar Avramovic           {Bank, &Mips::ValueMappings[Mips::GPRIdx], Bank, Bank});
493dbb6d01dSPetar Avramovic       if (Size == 64)
494dbb6d01dSPetar Avramovic         MappingID = CustomMappingID;
495dbb6d01dSPetar Avramovic     }
496dbb6d01dSPetar Avramovic     break;
497dbb6d01dSPetar Avramovic   }
498aa699b20SPetar Avramovic   case G_UNMERGE_VALUES: {
499aa699b20SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx],
500aa699b20SPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx],
501aa699b20SPetar Avramovic                                           &Mips::ValueMappings[Mips::DPRIdx]});
502aa699b20SPetar Avramovic     MappingID = CustomMappingID;
503aa699b20SPetar Avramovic     break;
504aa699b20SPetar Avramovic   }
505ec575f6eSPetar Avramovic   case G_MERGE_VALUES: {
506ec575f6eSPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx],
507ec575f6eSPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx],
508ec575f6eSPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx]});
509ec575f6eSPetar Avramovic     MappingID = CustomMappingID;
510ec575f6eSPetar Avramovic     break;
511ec575f6eSPetar Avramovic   }
512afa3afa3SPetar Avramovic   case G_FADD:
513afa3afa3SPetar Avramovic   case G_FSUB:
514afa3afa3SPetar Avramovic   case G_FMUL:
5150a1fd355SPetar Avramovic   case G_FDIV:
51681132ce0SPetar Avramovic   case G_FABS:
51781132ce0SPetar Avramovic   case G_FSQRT:{
518afa3afa3SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
519afa3afa3SPetar Avramovic     assert((Size == 32 || Size == 64) && "Unsupported floating point size");
520afa3afa3SPetar Avramovic     OperandsMapping = Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
521afa3afa3SPetar Avramovic                                  : &Mips::ValueMappings[Mips::DPRIdx];
522afa3afa3SPetar Avramovic     break;
523afa3afa3SPetar Avramovic   }
5241af05df3SPetar Avramovic   case G_FCONSTANT: {
525afa3afa3SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
526afa3afa3SPetar Avramovic     assert((Size == 32 || Size == 64) && "Unsupported floating point size");
5271af05df3SPetar Avramovic     const RegisterBankInfo::ValueMapping *FPRValueMapping =
5281af05df3SPetar Avramovic         Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
5291af05df3SPetar Avramovic                    : &Mips::ValueMappings[Mips::DPRIdx];
5301af05df3SPetar Avramovic     OperandsMapping = getOperandsMapping({FPRValueMapping, nullptr});
5311af05df3SPetar Avramovic     break;
5321af05df3SPetar Avramovic   }
53322e99c43SPetar Avramovic   case G_FCMP: {
53422e99c43SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(2).getReg()).getSizeInBits();
53522e99c43SPetar Avramovic     assert((Size == 32 || Size == 64) && "Unsupported floating point size");
53622e99c43SPetar Avramovic     const RegisterBankInfo::ValueMapping *FPRValueMapping =
53722e99c43SPetar Avramovic         Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
53822e99c43SPetar Avramovic                    : &Mips::ValueMappings[Mips::DPRIdx];
53922e99c43SPetar Avramovic     OperandsMapping =
54022e99c43SPetar Avramovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr,
54122e99c43SPetar Avramovic                             FPRValueMapping, FPRValueMapping});
54222e99c43SPetar Avramovic     break;
54322e99c43SPetar Avramovic   }
544a7d00064SPetar Avramovic   case G_FPEXT:
545a7d00064SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx],
546a7d00064SPetar Avramovic                                           &Mips::ValueMappings[Mips::SPRIdx]});
547a7d00064SPetar Avramovic     break;
548a7d00064SPetar Avramovic   case G_FPTRUNC:
549a7d00064SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::SPRIdx],
550a7d00064SPetar Avramovic                                           &Mips::ValueMappings[Mips::DPRIdx]});
551a7d00064SPetar Avramovic     break;
5524b4dae1cSPetar Avramovic   case G_FPTOSI: {
5534b4dae1cSPetar Avramovic     unsigned SizeFP = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
554ddd056c9SFangrui Song     assert((MRI.getType(MI.getOperand(0).getReg()).getSizeInBits() == 32) &&
555ddd056c9SFangrui Song            "Unsupported integer size");
5564b4dae1cSPetar Avramovic     assert((SizeFP == 32 || SizeFP == 64) && "Unsupported floating point size");
5574b4dae1cSPetar Avramovic     OperandsMapping = getOperandsMapping({
5584b4dae1cSPetar Avramovic         &Mips::ValueMappings[Mips::GPRIdx],
5594b4dae1cSPetar Avramovic         SizeFP == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
5604b4dae1cSPetar Avramovic                      : &Mips::ValueMappings[Mips::DPRIdx],
5614b4dae1cSPetar Avramovic     });
5624b4dae1cSPetar Avramovic     break;
5634b4dae1cSPetar Avramovic   }
564153bd24eSPetar Avramovic   case G_SITOFP: {
565153bd24eSPetar Avramovic     unsigned SizeInt = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
566153bd24eSPetar Avramovic     unsigned SizeFP = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
567ddd056c9SFangrui Song     (void)SizeInt;
568153bd24eSPetar Avramovic     assert((SizeInt == 32) && "Unsupported integer size");
569153bd24eSPetar Avramovic     assert((SizeFP == 32 || SizeFP == 64) && "Unsupported floating point size");
570153bd24eSPetar Avramovic     OperandsMapping =
571153bd24eSPetar Avramovic         getOperandsMapping({SizeFP == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
572153bd24eSPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
573153bd24eSPetar Avramovic                             &Mips::ValueMappings[Mips::GPRIdx]});
574153bd24eSPetar Avramovic     break;
575153bd24eSPetar Avramovic   }
576021e4c82SPetar Jovanovic   case G_CONSTANT:
577021e4c82SPetar Jovanovic   case G_FRAME_INDEX:
57864c10ba8SPetar Jovanovic   case G_GLOBAL_VALUE:
579*caef9306SPetar Avramovic   case G_JUMP_TABLE:
5805d9b8eedSPetar Avramovic   case G_BRCOND:
581021e4c82SPetar Jovanovic     OperandsMapping =
582021e4c82SPetar Jovanovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr});
583021e4c82SPetar Jovanovic     break;
584*caef9306SPetar Avramovic   case G_BRJT:
585*caef9306SPetar Avramovic     OperandsMapping =
586*caef9306SPetar Avramovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr,
587*caef9306SPetar Avramovic                             &Mips::ValueMappings[Mips::GPRIdx]});
588*caef9306SPetar Avramovic     break;
589ce4dd0aeSPetar Jovanovic   case G_ICMP:
590ce4dd0aeSPetar Jovanovic     OperandsMapping =
591ce4dd0aeSPetar Jovanovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr,
592ce4dd0aeSPetar Jovanovic                             &Mips::ValueMappings[Mips::GPRIdx],
593ce4dd0aeSPetar Jovanovic                             &Mips::ValueMappings[Mips::GPRIdx]});
594ce4dd0aeSPetar Jovanovic     break;
595366857a2SPetar Jovanovic   default:
596366857a2SPetar Jovanovic     return getInvalidInstructionMapping();
597366857a2SPetar Jovanovic   }
598366857a2SPetar Jovanovic 
599ec575f6eSPetar Avramovic   return getInstructionMapping(MappingID, /*Cost=*/1, OperandsMapping,
600366857a2SPetar Jovanovic                                NumOperands);
601366857a2SPetar Jovanovic }
602ec575f6eSPetar Avramovic 
603ec575f6eSPetar Avramovic using InstListTy = GISelWorkList<4>;
604ec575f6eSPetar Avramovic namespace {
605ec575f6eSPetar Avramovic class InstManager : public GISelChangeObserver {
606ec575f6eSPetar Avramovic   InstListTy &InstList;
607ec575f6eSPetar Avramovic 
608ec575f6eSPetar Avramovic public:
609ec575f6eSPetar Avramovic   InstManager(InstListTy &Insts) : InstList(Insts) {}
610ec575f6eSPetar Avramovic 
611ec575f6eSPetar Avramovic   void createdInstr(MachineInstr &MI) override { InstList.insert(&MI); }
612ec575f6eSPetar Avramovic   void erasingInstr(MachineInstr &MI) override {}
613ec575f6eSPetar Avramovic   void changingInstr(MachineInstr &MI) override {}
614ec575f6eSPetar Avramovic   void changedInstr(MachineInstr &MI) override {}
615ec575f6eSPetar Avramovic };
616ec575f6eSPetar Avramovic } // end anonymous namespace
617ec575f6eSPetar Avramovic 
618ec575f6eSPetar Avramovic /// Here we have to narrowScalar s64 operands to s32, combine away
619ec575f6eSPetar Avramovic /// G_MERGE/G_UNMERGE and erase instructions that became dead in the process.
620ec575f6eSPetar Avramovic /// We manually assign 32 bit gprb to register operands of all new instructions
621ec575f6eSPetar Avramovic /// that got created in the process since they will not end up in RegBankSelect
622ec575f6eSPetar Avramovic /// loop. Careful not to delete instruction after MI i.e. MI.getIterator()++.
623ec575f6eSPetar Avramovic void MipsRegisterBankInfo::applyMappingImpl(
624ec575f6eSPetar Avramovic     const OperandsMapper &OpdMapper) const {
625ec575f6eSPetar Avramovic   MachineInstr &MI = OpdMapper.getMI();
626ec575f6eSPetar Avramovic   InstListTy NewInstrs;
627ec575f6eSPetar Avramovic   MachineIRBuilder B(MI);
628ec575f6eSPetar Avramovic   MachineFunction *MF = MI.getMF();
629ec575f6eSPetar Avramovic   MachineRegisterInfo &MRI = OpdMapper.getMRI();
630ec575f6eSPetar Avramovic 
631ec575f6eSPetar Avramovic   InstManager NewInstrObserver(NewInstrs);
632ec575f6eSPetar Avramovic   GISelObserverWrapper WrapperObserver(&NewInstrObserver);
633ec575f6eSPetar Avramovic   LegalizerHelper Helper(*MF, WrapperObserver, B);
634ec575f6eSPetar Avramovic   LegalizationArtifactCombiner ArtCombiner(
635ec575f6eSPetar Avramovic       B, MF->getRegInfo(), *MF->getSubtarget().getLegalizerInfo());
636ec575f6eSPetar Avramovic 
637ec575f6eSPetar Avramovic   switch (MI.getOpcode()) {
638aa699b20SPetar Avramovic   case TargetOpcode::G_LOAD:
639dbb6d01dSPetar Avramovic   case TargetOpcode::G_STORE:
640be20e361SPetar Avramovic   case TargetOpcode::G_PHI:
641dbb6d01dSPetar Avramovic   case TargetOpcode::G_SELECT: {
642ec575f6eSPetar Avramovic     Helper.narrowScalar(MI, 0, LLT::scalar(32));
643ec575f6eSPetar Avramovic     // Handle new instructions.
644ec575f6eSPetar Avramovic     while (!NewInstrs.empty()) {
645ec575f6eSPetar Avramovic       MachineInstr *NewMI = NewInstrs.pop_back_val();
646ec575f6eSPetar Avramovic       // This is new G_UNMERGE that was created during narrowScalar and will
647ec575f6eSPetar Avramovic       // not be considered for regbank selection. RegBankSelect for mips
648ec575f6eSPetar Avramovic       // visits/makes corresponding G_MERGE first. Combine them here.
649ec575f6eSPetar Avramovic       if (NewMI->getOpcode() == TargetOpcode::G_UNMERGE_VALUES) {
650ec575f6eSPetar Avramovic         SmallVector<MachineInstr *, 2> DeadInstrs;
651ec575f6eSPetar Avramovic         ArtCombiner.tryCombineMerges(*NewMI, DeadInstrs);
652ec575f6eSPetar Avramovic         for (MachineInstr *DeadMI : DeadInstrs)
653ec575f6eSPetar Avramovic           DeadMI->eraseFromParent();
654aa699b20SPetar Avramovic       }
655aa699b20SPetar Avramovic       // This G_MERGE will be combined away when its corresponding G_UNMERGE
656aa699b20SPetar Avramovic       // gets regBankSelected.
657aa699b20SPetar Avramovic       else if (NewMI->getOpcode() == TargetOpcode::G_MERGE_VALUES)
658aa699b20SPetar Avramovic         continue;
659aa699b20SPetar Avramovic       else
660ec575f6eSPetar Avramovic         // Manually set register banks for all register operands to 32 bit gprb.
661ec575f6eSPetar Avramovic         for (auto Op : NewMI->operands()) {
662ec575f6eSPetar Avramovic           if (Op.isReg()) {
663ec575f6eSPetar Avramovic             assert(MRI.getType(Op.getReg()).getSizeInBits() == 32 &&
664ec575f6eSPetar Avramovic                    "Only 32 bit gprb is handled here.\n");
665ec575f6eSPetar Avramovic             MRI.setRegBank(Op.getReg(), getRegBank(Mips::GPRBRegBankID));
666ec575f6eSPetar Avramovic           }
667ec575f6eSPetar Avramovic         }
668ec575f6eSPetar Avramovic     }
669ec575f6eSPetar Avramovic     return;
670ec575f6eSPetar Avramovic   }
671aa699b20SPetar Avramovic   case TargetOpcode::G_UNMERGE_VALUES: {
672aa699b20SPetar Avramovic     SmallVector<MachineInstr *, 2> DeadInstrs;
673aa699b20SPetar Avramovic     ArtCombiner.tryCombineMerges(MI, DeadInstrs);
674aa699b20SPetar Avramovic     for (MachineInstr *DeadMI : DeadInstrs)
675aa699b20SPetar Avramovic       DeadMI->eraseFromParent();
676aa699b20SPetar Avramovic     return;
677aa699b20SPetar Avramovic   }
678ec575f6eSPetar Avramovic   default:
679ec575f6eSPetar Avramovic     break;
680ec575f6eSPetar Avramovic   }
681ec575f6eSPetar Avramovic 
682ec575f6eSPetar Avramovic   return applyDefaultMapping(OpdMapper);
683ec575f6eSPetar Avramovic }
684