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 &&
16696252407SPetar Avramovic         !TargetRegisterInfo::isPhysicalRegister(
16796252407SPetar Avramovic             NonCopyInstr->getOperand(0).getReg()))
16896252407SPetar Avramovic       addDefUses(NonCopyInstr->getOperand(0).getReg(), MRI);
16996252407SPetar Avramovic     else
17096252407SPetar Avramovic       DefUses.push_back(skipCopiesOutgoing(&UseMI));
171aa699b20SPetar Avramovic   }
172aa699b20SPetar Avramovic }
173aa699b20SPetar Avramovic 
174ec575f6eSPetar Avramovic void MipsRegisterBankInfo::AmbiguousRegDefUseContainer::addUseDef(
175ec575f6eSPetar Avramovic     Register Reg, const MachineRegisterInfo &MRI) {
176ec575f6eSPetar Avramovic   assert(!MRI.getType(Reg).isPointer() &&
177ec575f6eSPetar Avramovic          "Pointers are gprb, they should not be considered as ambiguous.\n");
178ec575f6eSPetar Avramovic   MachineInstr *DefMI = MRI.getVRegDef(Reg);
17996252407SPetar Avramovic   UseDefs.push_back(skipCopiesIncoming(DefMI));
18096252407SPetar Avramovic }
181ec575f6eSPetar Avramovic 
18296252407SPetar Avramovic MachineInstr *
18396252407SPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::skipCopiesOutgoing(
18496252407SPetar Avramovic     MachineInstr *MI) const {
18596252407SPetar Avramovic   const MachineFunction &MF = *MI->getParent()->getParent();
18696252407SPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
18796252407SPetar Avramovic   MachineInstr *Ret = MI;
18896252407SPetar Avramovic   while (Ret->getOpcode() == TargetOpcode::COPY &&
18996252407SPetar Avramovic          !TargetRegisterInfo::isPhysicalRegister(Ret->getOperand(0).getReg()) &&
19096252407SPetar Avramovic          MRI.hasOneUse(Ret->getOperand(0).getReg())) {
19196252407SPetar Avramovic     Ret = &(*MRI.use_instr_begin(Ret->getOperand(0).getReg()));
19296252407SPetar Avramovic   }
19396252407SPetar Avramovic   return Ret;
19496252407SPetar Avramovic }
19596252407SPetar Avramovic 
19696252407SPetar Avramovic MachineInstr *
19796252407SPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::skipCopiesIncoming(
19896252407SPetar Avramovic     MachineInstr *MI) const {
19996252407SPetar Avramovic   const MachineFunction &MF = *MI->getParent()->getParent();
20096252407SPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
20196252407SPetar Avramovic   MachineInstr *Ret = MI;
20296252407SPetar Avramovic   while (Ret->getOpcode() == TargetOpcode::COPY &&
20396252407SPetar Avramovic          !TargetRegisterInfo::isPhysicalRegister(Ret->getOperand(1).getReg()))
20496252407SPetar Avramovic     Ret = MRI.getVRegDef(Ret->getOperand(1).getReg());
20596252407SPetar Avramovic   return Ret;
206ec575f6eSPetar Avramovic }
207ec575f6eSPetar Avramovic 
208ec575f6eSPetar Avramovic MipsRegisterBankInfo::AmbiguousRegDefUseContainer::AmbiguousRegDefUseContainer(
209ec575f6eSPetar Avramovic     const MachineInstr *MI) {
210ec575f6eSPetar Avramovic   assert(isAmbiguous(MI->getOpcode()) &&
211ec575f6eSPetar Avramovic          "Not implemented for non Ambiguous opcode.\n");
212ec575f6eSPetar Avramovic 
213ec575f6eSPetar Avramovic   const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
214ec575f6eSPetar Avramovic 
215aa699b20SPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_LOAD)
216aa699b20SPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
217aa699b20SPetar Avramovic 
218ec575f6eSPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_STORE)
219ec575f6eSPetar Avramovic     addUseDef(MI->getOperand(0).getReg(), MRI);
220dbb6d01dSPetar Avramovic 
221be20e361SPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_PHI) {
222be20e361SPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
223be20e361SPetar Avramovic 
224be20e361SPetar Avramovic     for (unsigned i = 1; i < MI->getNumOperands(); i += 2)
225be20e361SPetar Avramovic       addUseDef(MI->getOperand(i).getReg(), MRI);
226be20e361SPetar Avramovic   }
227be20e361SPetar Avramovic 
228dbb6d01dSPetar Avramovic   if (MI->getOpcode() == TargetOpcode::G_SELECT) {
229dbb6d01dSPetar Avramovic     addDefUses(MI->getOperand(0).getReg(), MRI);
230dbb6d01dSPetar Avramovic 
231dbb6d01dSPetar Avramovic     addUseDef(MI->getOperand(2).getReg(), MRI);
232dbb6d01dSPetar Avramovic     addUseDef(MI->getOperand(3).getReg(), MRI);
233dbb6d01dSPetar Avramovic   }
234ec575f6eSPetar Avramovic }
235ec575f6eSPetar Avramovic 
236e3bb0a72SPetar Avramovic bool MipsRegisterBankInfo::TypeInfoForMF::visit(
237e3bb0a72SPetar Avramovic     const MachineInstr *MI, const MachineInstr *WaitingForTypeOfMI) {
238ec575f6eSPetar Avramovic   assert(isAmbiguous(MI->getOpcode()) && "Visiting non-Ambiguous opcode.\n");
239e3bb0a72SPetar Avramovic   if (wasVisited(MI))
240e3bb0a72SPetar Avramovic     return true; // InstType has already been determined for MI.
241ec575f6eSPetar Avramovic 
242ec575f6eSPetar Avramovic   startVisit(MI);
243ec575f6eSPetar Avramovic   AmbiguousRegDefUseContainer DefUseContainer(MI);
244ec575f6eSPetar Avramovic 
245aa699b20SPetar Avramovic   // Visit instructions where MI's DEF operands are USED.
246aa699b20SPetar Avramovic   if (visitAdjacentInstrs(MI, DefUseContainer.getDefUses(), true))
247aa699b20SPetar Avramovic     return true;
248aa699b20SPetar Avramovic 
249ec575f6eSPetar Avramovic   // Visit instructions that DEFINE MI's USE operands.
250aa699b20SPetar Avramovic   if (visitAdjacentInstrs(MI, DefUseContainer.getUseDefs(), false))
251ec575f6eSPetar Avramovic     return true;
252ec575f6eSPetar Avramovic 
253e3bb0a72SPetar Avramovic   // All MI's adjacent instructions, are ambiguous.
254e3bb0a72SPetar Avramovic   if (!WaitingForTypeOfMI) {
255e3bb0a72SPetar Avramovic     // This is chain of ambiguous instructions.
256e3bb0a72SPetar Avramovic     setTypes(MI, InstType::Ambiguous);
257e3bb0a72SPetar Avramovic     return true;
258e3bb0a72SPetar Avramovic   }
259e3bb0a72SPetar Avramovic   // Excluding WaitingForTypeOfMI, MI is either connected to chains of ambiguous
260e3bb0a72SPetar Avramovic   // instructions or has no other adjacent instructions. Anyway InstType could
261e3bb0a72SPetar Avramovic   // not be determined. There could be unexplored path from some of
262e3bb0a72SPetar Avramovic   // WaitingForTypeOfMI's adjacent instructions to an instruction with only one
263e3bb0a72SPetar Avramovic   // mapping available.
264e3bb0a72SPetar Avramovic   // We are done with this branch, add MI to WaitingForTypeOfMI's WaitingQueue,
265e3bb0a72SPetar Avramovic   // this way when WaitingForTypeOfMI figures out its InstType same InstType
266e3bb0a72SPetar Avramovic   // will be assigned to all instructions in this branch.
267e3bb0a72SPetar Avramovic   addToWaitingQueue(WaitingForTypeOfMI, MI);
268ec575f6eSPetar Avramovic   return false;
269ec575f6eSPetar Avramovic }
270ec575f6eSPetar Avramovic 
271ec575f6eSPetar Avramovic bool MipsRegisterBankInfo::TypeInfoForMF::visitAdjacentInstrs(
272aa699b20SPetar Avramovic     const MachineInstr *MI, SmallVectorImpl<MachineInstr *> &AdjacentInstrs,
273aa699b20SPetar Avramovic     bool isDefUse) {
274ec575f6eSPetar Avramovic   while (!AdjacentInstrs.empty()) {
275ec575f6eSPetar Avramovic     MachineInstr *AdjMI = AdjacentInstrs.pop_back_val();
276ec575f6eSPetar Avramovic 
277aa699b20SPetar Avramovic     if (isDefUse ? isFloatingPointOpcodeUse(AdjMI->getOpcode())
278aa699b20SPetar Avramovic                  : isFloatingPointOpcodeDef(AdjMI->getOpcode())) {
279ec575f6eSPetar Avramovic       setTypes(MI, InstType::FloatingPoint);
280ec575f6eSPetar Avramovic       return true;
281ec575f6eSPetar Avramovic     }
282ec575f6eSPetar Avramovic 
283ec575f6eSPetar Avramovic     // Determine InstType from register bank of phys register that is
284aa699b20SPetar Avramovic     // 'isDefUse ? def : use' of this copy.
285ec575f6eSPetar Avramovic     if (AdjMI->getOpcode() == TargetOpcode::COPY) {
286aa699b20SPetar Avramovic       setTypesAccordingToPhysicalRegister(MI, AdjMI, isDefUse ? 0 : 1);
287ec575f6eSPetar Avramovic       return true;
288ec575f6eSPetar Avramovic     }
289ec575f6eSPetar Avramovic 
290ec575f6eSPetar Avramovic     // Defaults to integer instruction. Includes G_MERGE_VALUES and
291ec575f6eSPetar Avramovic     // G_UNMERGE_VALUES.
292e3bb0a72SPetar Avramovic     if (!isAmbiguous(AdjMI->getOpcode())) {
293ec575f6eSPetar Avramovic       setTypes(MI, InstType::Integer);
294ec575f6eSPetar Avramovic       return true;
295ec575f6eSPetar Avramovic     }
296e3bb0a72SPetar Avramovic 
297e3bb0a72SPetar Avramovic     // When AdjMI was visited first, MI has to continue to explore remaining
298e3bb0a72SPetar Avramovic     // adjacent instructions and determine InstType without visiting AdjMI.
299e3bb0a72SPetar Avramovic     if (!wasVisited(AdjMI) ||
300e3bb0a72SPetar Avramovic         getRecordedTypeForInstr(AdjMI) != InstType::NotDetermined) {
301e3bb0a72SPetar Avramovic       if (visit(AdjMI, MI)) {
302e3bb0a72SPetar Avramovic         // InstType is successfully determined and is same as for AdjMI.
303e3bb0a72SPetar Avramovic         setTypes(MI, getRecordedTypeForInstr(AdjMI));
304e3bb0a72SPetar Avramovic         return true;
305e3bb0a72SPetar Avramovic       }
306e3bb0a72SPetar Avramovic     }
307e3bb0a72SPetar Avramovic   }
308ec575f6eSPetar Avramovic   return false;
309ec575f6eSPetar Avramovic }
310ec575f6eSPetar Avramovic 
311ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::setTypes(const MachineInstr *MI,
312ec575f6eSPetar Avramovic                                                    InstType InstTy) {
313ec575f6eSPetar Avramovic   changeRecordedTypeForInstr(MI, InstTy);
314e3bb0a72SPetar Avramovic   for (const MachineInstr *WaitingInstr : getWaitingQueueFor(MI)) {
315e3bb0a72SPetar Avramovic     setTypes(WaitingInstr, InstTy);
316e3bb0a72SPetar Avramovic   }
317ec575f6eSPetar Avramovic }
318ec575f6eSPetar Avramovic 
319ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::setTypesAccordingToPhysicalRegister(
320ec575f6eSPetar Avramovic     const MachineInstr *MI, const MachineInstr *CopyInst, unsigned Op) {
321ec575f6eSPetar Avramovic   assert((TargetRegisterInfo::isPhysicalRegister(
322ec575f6eSPetar Avramovic              CopyInst->getOperand(Op).getReg())) &&
323ec575f6eSPetar Avramovic          "Copies of non physical registers should not be considered here.\n");
324ec575f6eSPetar Avramovic 
325ec575f6eSPetar Avramovic   const MachineFunction &MF = *CopyInst->getMF();
326ec575f6eSPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
327ec575f6eSPetar Avramovic   const TargetRegisterInfo &TRI = *MF.getSubtarget().getRegisterInfo();
328ec575f6eSPetar Avramovic   const RegisterBankInfo &RBI =
329ec575f6eSPetar Avramovic       *CopyInst->getMF()->getSubtarget().getRegBankInfo();
330ec575f6eSPetar Avramovic   const RegisterBank *Bank =
331ec575f6eSPetar Avramovic       RBI.getRegBank(CopyInst->getOperand(Op).getReg(), MRI, TRI);
332ec575f6eSPetar Avramovic 
333ec575f6eSPetar Avramovic   if (Bank == &Mips::FPRBRegBank)
334ec575f6eSPetar Avramovic     setTypes(MI, InstType::FloatingPoint);
335ec575f6eSPetar Avramovic   else if (Bank == &Mips::GPRBRegBank)
336ec575f6eSPetar Avramovic     setTypes(MI, InstType::Integer);
337ec575f6eSPetar Avramovic   else
338ec575f6eSPetar Avramovic     llvm_unreachable("Unsupported register bank.\n");
339ec575f6eSPetar Avramovic }
340ec575f6eSPetar Avramovic 
341ec575f6eSPetar Avramovic MipsRegisterBankInfo::InstType
342ec575f6eSPetar Avramovic MipsRegisterBankInfo::TypeInfoForMF::determineInstType(const MachineInstr *MI) {
343e3bb0a72SPetar Avramovic   visit(MI, nullptr);
344ec575f6eSPetar Avramovic   return getRecordedTypeForInstr(MI);
345ec575f6eSPetar Avramovic }
346ec575f6eSPetar Avramovic 
347ec575f6eSPetar Avramovic void MipsRegisterBankInfo::TypeInfoForMF::cleanupIfNewFunction(
348ec575f6eSPetar Avramovic     llvm::StringRef FunctionName) {
349ec575f6eSPetar Avramovic   if (MFName != FunctionName) {
350ec575f6eSPetar Avramovic     MFName = FunctionName;
351e3bb0a72SPetar Avramovic     WaitingQueues.clear();
352ec575f6eSPetar Avramovic     Types.clear();
353ec575f6eSPetar Avramovic   }
354ec575f6eSPetar Avramovic }
355ec575f6eSPetar Avramovic 
356366857a2SPetar Jovanovic const RegisterBankInfo::InstructionMapping &
357366857a2SPetar Jovanovic MipsRegisterBankInfo::getInstrMapping(const MachineInstr &MI) const {
358366857a2SPetar Jovanovic 
359ec575f6eSPetar Avramovic   static TypeInfoForMF TI;
360ec575f6eSPetar Avramovic 
361ec575f6eSPetar Avramovic   // Reset TI internal data when MF changes.
362ec575f6eSPetar Avramovic   TI.cleanupIfNewFunction(MI.getMF()->getName());
363ec575f6eSPetar Avramovic 
364366857a2SPetar Jovanovic   unsigned Opc = MI.getOpcode();
3651af05df3SPetar Avramovic   const MachineFunction &MF = *MI.getParent()->getParent();
3661af05df3SPetar Avramovic   const MachineRegisterInfo &MRI = MF.getRegInfo();
367366857a2SPetar Jovanovic 
368be20e361SPetar Avramovic   if (MI.getOpcode() != TargetOpcode::G_PHI) {
369be20e361SPetar Avramovic     const RegisterBankInfo::InstructionMapping &Mapping =
370be20e361SPetar Avramovic         getInstrMappingImpl(MI);
371366857a2SPetar Jovanovic     if (Mapping.isValid())
372366857a2SPetar Jovanovic       return Mapping;
373be20e361SPetar Avramovic   }
374366857a2SPetar Jovanovic 
375366857a2SPetar Jovanovic   using namespace TargetOpcode;
376366857a2SPetar Jovanovic 
377366857a2SPetar Jovanovic   unsigned NumOperands = MI.getNumOperands();
378366857a2SPetar Jovanovic   const ValueMapping *OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx];
379ec575f6eSPetar Avramovic   unsigned MappingID = DefaultMappingID;
380ec575f6eSPetar Avramovic   const unsigned CustomMappingID = 1;
381366857a2SPetar Jovanovic 
382366857a2SPetar Jovanovic   switch (Opc) {
3837cecadb9SPetar Avramovic   case G_TRUNC:
384366857a2SPetar Jovanovic   case G_ADD:
3857cecadb9SPetar Avramovic   case G_SUB:
38656dc218dSPetar Avramovic   case G_MUL:
387a48285a1SPetar Avramovic   case G_UMULH:
38879df8596SPetar Avramovic   case G_ZEXTLOAD:
38979df8596SPetar Avramovic   case G_SEXTLOAD:
390021e4c82SPetar Jovanovic   case G_GEP:
391*b1fc6f61SPetar Avramovic   case G_INTTOPTR:
392*b1fc6f61SPetar Avramovic   case G_PTRTOINT:
3933b953c37SPetar Jovanovic   case G_AND:
3943b953c37SPetar Jovanovic   case G_OR:
3953b953c37SPetar Jovanovic   case G_XOR:
3963b953c37SPetar Jovanovic   case G_SHL:
3973b953c37SPetar Jovanovic   case G_ASHR:
3983b953c37SPetar Jovanovic   case G_LSHR:
3990a5e4eb7SPetar Avramovic   case G_SDIV:
4000a5e4eb7SPetar Avramovic   case G_UDIV:
4010a5e4eb7SPetar Avramovic   case G_SREM:
4020a5e4eb7SPetar Avramovic   case G_UREM:
403366857a2SPetar Jovanovic     OperandsMapping = &Mips::ValueMappings[Mips::GPRIdx];
404366857a2SPetar Jovanovic     break;
405aa699b20SPetar Avramovic   case G_LOAD: {
406aa699b20SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
407aa699b20SPetar Avramovic     InstType InstTy = InstType::Integer;
408aa699b20SPetar Avramovic     if (!MRI.getType(MI.getOperand(0).getReg()).isPointer()) {
409aa699b20SPetar Avramovic       InstTy = TI.determineInstType(&MI);
410aa699b20SPetar Avramovic     }
411aa699b20SPetar Avramovic 
412e3bb0a72SPetar Avramovic     if (InstTy == InstType::FloatingPoint ||
413e3bb0a72SPetar Avramovic         (Size == 64 && InstTy == InstType::Ambiguous)) { // fprb
414aa699b20SPetar Avramovic       OperandsMapping =
415aa699b20SPetar Avramovic           getOperandsMapping({Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
416aa699b20SPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
417aa699b20SPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
418aa699b20SPetar Avramovic       break;
419aa699b20SPetar Avramovic     } else { // gprb
420aa699b20SPetar Avramovic       OperandsMapping =
421aa699b20SPetar Avramovic           getOperandsMapping({Size <= 32 ? &Mips::ValueMappings[Mips::GPRIdx]
422aa699b20SPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
423aa699b20SPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
424aa699b20SPetar Avramovic       if (Size == 64)
425aa699b20SPetar Avramovic         MappingID = CustomMappingID;
426aa699b20SPetar Avramovic     }
427aa699b20SPetar Avramovic 
428aa699b20SPetar Avramovic     break;
429aa699b20SPetar Avramovic   }
430ec575f6eSPetar Avramovic   case G_STORE: {
431ec575f6eSPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
432ec575f6eSPetar Avramovic     InstType InstTy = InstType::Integer;
433ec575f6eSPetar Avramovic     if (!MRI.getType(MI.getOperand(0).getReg()).isPointer()) {
434ec575f6eSPetar Avramovic       InstTy = TI.determineInstType(&MI);
435ec575f6eSPetar Avramovic     }
436ec575f6eSPetar Avramovic 
437e3bb0a72SPetar Avramovic     if (InstTy == InstType::FloatingPoint ||
438e3bb0a72SPetar Avramovic         (Size == 64 && InstTy == InstType::Ambiguous)) { // fprb
439ec575f6eSPetar Avramovic       OperandsMapping =
440ec575f6eSPetar Avramovic           getOperandsMapping({Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
441ec575f6eSPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
442ec575f6eSPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
443ec575f6eSPetar Avramovic       break;
444ec575f6eSPetar Avramovic     } else { // gprb
445ec575f6eSPetar Avramovic       OperandsMapping =
446ec575f6eSPetar Avramovic           getOperandsMapping({Size <= 32 ? &Mips::ValueMappings[Mips::GPRIdx]
447ec575f6eSPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
448ec575f6eSPetar Avramovic                               &Mips::ValueMappings[Mips::GPRIdx]});
449ec575f6eSPetar Avramovic       if (Size == 64)
450ec575f6eSPetar Avramovic         MappingID = CustomMappingID;
451ec575f6eSPetar Avramovic     }
452ec575f6eSPetar Avramovic     break;
453ec575f6eSPetar Avramovic   }
454be20e361SPetar Avramovic   case G_PHI: {
455be20e361SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
456be20e361SPetar Avramovic     InstType InstTy = InstType::Integer;
457be20e361SPetar Avramovic     if (!MRI.getType(MI.getOperand(0).getReg()).isPointer()) {
458be20e361SPetar Avramovic       InstTy = TI.determineInstType(&MI);
459be20e361SPetar Avramovic     }
460be20e361SPetar Avramovic 
461be20e361SPetar Avramovic     // PHI is copylike and should have one regbank in mapping for def register.
462be20e361SPetar Avramovic     if (InstTy == InstType::Integer && Size == 64) { // fprb
463be20e361SPetar Avramovic       OperandsMapping =
464be20e361SPetar Avramovic           getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx]});
465be20e361SPetar Avramovic       return getInstructionMapping(CustomMappingID, /*Cost=*/1, OperandsMapping,
466be20e361SPetar Avramovic                                    /*NumOperands=*/1);
467be20e361SPetar Avramovic     }
468be20e361SPetar Avramovic     // Use default handling for PHI, i.e. set reg bank of def operand to match
469be20e361SPetar Avramovic     // register banks of use operands.
470be20e361SPetar Avramovic     const RegisterBankInfo::InstructionMapping &Mapping =
471be20e361SPetar Avramovic         getInstrMappingImpl(MI);
472be20e361SPetar Avramovic     return Mapping;
473be20e361SPetar Avramovic   }
474dbb6d01dSPetar Avramovic   case G_SELECT: {
475dbb6d01dSPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
476dbb6d01dSPetar Avramovic     InstType InstTy = InstType::Integer;
477dbb6d01dSPetar Avramovic     if (!MRI.getType(MI.getOperand(0).getReg()).isPointer()) {
478dbb6d01dSPetar Avramovic       InstTy = TI.determineInstType(&MI);
479dbb6d01dSPetar Avramovic     }
480dbb6d01dSPetar Avramovic 
481e3bb0a72SPetar Avramovic     if (InstTy == InstType::FloatingPoint ||
482e3bb0a72SPetar Avramovic         (Size == 64 && InstTy == InstType::Ambiguous)) { // fprb
483dbb6d01dSPetar Avramovic       const RegisterBankInfo::ValueMapping *Bank =
484dbb6d01dSPetar Avramovic           Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
485dbb6d01dSPetar Avramovic                      : &Mips::ValueMappings[Mips::DPRIdx];
486dbb6d01dSPetar Avramovic       OperandsMapping = getOperandsMapping(
487dbb6d01dSPetar Avramovic           {Bank, &Mips::ValueMappings[Mips::GPRIdx], Bank, Bank});
488dbb6d01dSPetar Avramovic       break;
489dbb6d01dSPetar Avramovic     } else { // gprb
490dbb6d01dSPetar Avramovic       const RegisterBankInfo::ValueMapping *Bank =
491dbb6d01dSPetar Avramovic           Size <= 32 ? &Mips::ValueMappings[Mips::GPRIdx]
492dbb6d01dSPetar Avramovic                      : &Mips::ValueMappings[Mips::DPRIdx];
493dbb6d01dSPetar Avramovic       OperandsMapping = getOperandsMapping(
494dbb6d01dSPetar Avramovic           {Bank, &Mips::ValueMappings[Mips::GPRIdx], Bank, Bank});
495dbb6d01dSPetar Avramovic       if (Size == 64)
496dbb6d01dSPetar Avramovic         MappingID = CustomMappingID;
497dbb6d01dSPetar Avramovic     }
498dbb6d01dSPetar Avramovic     break;
499dbb6d01dSPetar Avramovic   }
500aa699b20SPetar Avramovic   case G_UNMERGE_VALUES: {
501aa699b20SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx],
502aa699b20SPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx],
503aa699b20SPetar Avramovic                                           &Mips::ValueMappings[Mips::DPRIdx]});
504aa699b20SPetar Avramovic     MappingID = CustomMappingID;
505aa699b20SPetar Avramovic     break;
506aa699b20SPetar Avramovic   }
507ec575f6eSPetar Avramovic   case G_MERGE_VALUES: {
508ec575f6eSPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx],
509ec575f6eSPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx],
510ec575f6eSPetar Avramovic                                           &Mips::ValueMappings[Mips::GPRIdx]});
511ec575f6eSPetar Avramovic     MappingID = CustomMappingID;
512ec575f6eSPetar Avramovic     break;
513ec575f6eSPetar Avramovic   }
514afa3afa3SPetar Avramovic   case G_FADD:
515afa3afa3SPetar Avramovic   case G_FSUB:
516afa3afa3SPetar Avramovic   case G_FMUL:
5170a1fd355SPetar Avramovic   case G_FDIV:
51881132ce0SPetar Avramovic   case G_FABS:
51981132ce0SPetar Avramovic   case G_FSQRT:{
520afa3afa3SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
521afa3afa3SPetar Avramovic     assert((Size == 32 || Size == 64) && "Unsupported floating point size");
522afa3afa3SPetar Avramovic     OperandsMapping = Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
523afa3afa3SPetar Avramovic                                  : &Mips::ValueMappings[Mips::DPRIdx];
524afa3afa3SPetar Avramovic     break;
525afa3afa3SPetar Avramovic   }
5261af05df3SPetar Avramovic   case G_FCONSTANT: {
527afa3afa3SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
528afa3afa3SPetar Avramovic     assert((Size == 32 || Size == 64) && "Unsupported floating point size");
5291af05df3SPetar Avramovic     const RegisterBankInfo::ValueMapping *FPRValueMapping =
5301af05df3SPetar Avramovic         Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
5311af05df3SPetar Avramovic                    : &Mips::ValueMappings[Mips::DPRIdx];
5321af05df3SPetar Avramovic     OperandsMapping = getOperandsMapping({FPRValueMapping, nullptr});
5331af05df3SPetar Avramovic     break;
5341af05df3SPetar Avramovic   }
53522e99c43SPetar Avramovic   case G_FCMP: {
53622e99c43SPetar Avramovic     unsigned Size = MRI.getType(MI.getOperand(2).getReg()).getSizeInBits();
53722e99c43SPetar Avramovic     assert((Size == 32 || Size == 64) && "Unsupported floating point size");
53822e99c43SPetar Avramovic     const RegisterBankInfo::ValueMapping *FPRValueMapping =
53922e99c43SPetar Avramovic         Size == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
54022e99c43SPetar Avramovic                    : &Mips::ValueMappings[Mips::DPRIdx];
54122e99c43SPetar Avramovic     OperandsMapping =
54222e99c43SPetar Avramovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr,
54322e99c43SPetar Avramovic                             FPRValueMapping, FPRValueMapping});
54422e99c43SPetar Avramovic     break;
54522e99c43SPetar Avramovic   }
546a7d00064SPetar Avramovic   case G_FPEXT:
547a7d00064SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::DPRIdx],
548a7d00064SPetar Avramovic                                           &Mips::ValueMappings[Mips::SPRIdx]});
549a7d00064SPetar Avramovic     break;
550a7d00064SPetar Avramovic   case G_FPTRUNC:
551a7d00064SPetar Avramovic     OperandsMapping = getOperandsMapping({&Mips::ValueMappings[Mips::SPRIdx],
552a7d00064SPetar Avramovic                                           &Mips::ValueMappings[Mips::DPRIdx]});
553a7d00064SPetar Avramovic     break;
5544b4dae1cSPetar Avramovic   case G_FPTOSI: {
5554b4dae1cSPetar Avramovic     unsigned SizeFP = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
556ddd056c9SFangrui Song     assert((MRI.getType(MI.getOperand(0).getReg()).getSizeInBits() == 32) &&
557ddd056c9SFangrui Song            "Unsupported integer size");
5584b4dae1cSPetar Avramovic     assert((SizeFP == 32 || SizeFP == 64) && "Unsupported floating point size");
5594b4dae1cSPetar Avramovic     OperandsMapping = getOperandsMapping({
5604b4dae1cSPetar Avramovic         &Mips::ValueMappings[Mips::GPRIdx],
5614b4dae1cSPetar Avramovic         SizeFP == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
5624b4dae1cSPetar Avramovic                      : &Mips::ValueMappings[Mips::DPRIdx],
5634b4dae1cSPetar Avramovic     });
5644b4dae1cSPetar Avramovic     break;
5654b4dae1cSPetar Avramovic   }
566153bd24eSPetar Avramovic   case G_SITOFP: {
567153bd24eSPetar Avramovic     unsigned SizeInt = MRI.getType(MI.getOperand(1).getReg()).getSizeInBits();
568153bd24eSPetar Avramovic     unsigned SizeFP = MRI.getType(MI.getOperand(0).getReg()).getSizeInBits();
569ddd056c9SFangrui Song     (void)SizeInt;
570153bd24eSPetar Avramovic     assert((SizeInt == 32) && "Unsupported integer size");
571153bd24eSPetar Avramovic     assert((SizeFP == 32 || SizeFP == 64) && "Unsupported floating point size");
572153bd24eSPetar Avramovic     OperandsMapping =
573153bd24eSPetar Avramovic         getOperandsMapping({SizeFP == 32 ? &Mips::ValueMappings[Mips::SPRIdx]
574153bd24eSPetar Avramovic                                          : &Mips::ValueMappings[Mips::DPRIdx],
575153bd24eSPetar Avramovic                             &Mips::ValueMappings[Mips::GPRIdx]});
576153bd24eSPetar Avramovic     break;
577153bd24eSPetar Avramovic   }
578021e4c82SPetar Jovanovic   case G_CONSTANT:
579021e4c82SPetar Jovanovic   case G_FRAME_INDEX:
58064c10ba8SPetar Jovanovic   case G_GLOBAL_VALUE:
5815d9b8eedSPetar Avramovic   case G_BRCOND:
582021e4c82SPetar Jovanovic     OperandsMapping =
583021e4c82SPetar Jovanovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr});
584021e4c82SPetar Jovanovic     break;
585ce4dd0aeSPetar Jovanovic   case G_ICMP:
586ce4dd0aeSPetar Jovanovic     OperandsMapping =
587ce4dd0aeSPetar Jovanovic         getOperandsMapping({&Mips::ValueMappings[Mips::GPRIdx], nullptr,
588ce4dd0aeSPetar Jovanovic                             &Mips::ValueMappings[Mips::GPRIdx],
589ce4dd0aeSPetar Jovanovic                             &Mips::ValueMappings[Mips::GPRIdx]});
590ce4dd0aeSPetar Jovanovic     break;
591366857a2SPetar Jovanovic   default:
592366857a2SPetar Jovanovic     return getInvalidInstructionMapping();
593366857a2SPetar Jovanovic   }
594366857a2SPetar Jovanovic 
595ec575f6eSPetar Avramovic   return getInstructionMapping(MappingID, /*Cost=*/1, OperandsMapping,
596366857a2SPetar Jovanovic                                NumOperands);
597366857a2SPetar Jovanovic }
598ec575f6eSPetar Avramovic 
599ec575f6eSPetar Avramovic using InstListTy = GISelWorkList<4>;
600ec575f6eSPetar Avramovic namespace {
601ec575f6eSPetar Avramovic class InstManager : public GISelChangeObserver {
602ec575f6eSPetar Avramovic   InstListTy &InstList;
603ec575f6eSPetar Avramovic 
604ec575f6eSPetar Avramovic public:
605ec575f6eSPetar Avramovic   InstManager(InstListTy &Insts) : InstList(Insts) {}
606ec575f6eSPetar Avramovic 
607ec575f6eSPetar Avramovic   void createdInstr(MachineInstr &MI) override { InstList.insert(&MI); }
608ec575f6eSPetar Avramovic   void erasingInstr(MachineInstr &MI) override {}
609ec575f6eSPetar Avramovic   void changingInstr(MachineInstr &MI) override {}
610ec575f6eSPetar Avramovic   void changedInstr(MachineInstr &MI) override {}
611ec575f6eSPetar Avramovic };
612ec575f6eSPetar Avramovic } // end anonymous namespace
613ec575f6eSPetar Avramovic 
614ec575f6eSPetar Avramovic /// Here we have to narrowScalar s64 operands to s32, combine away
615ec575f6eSPetar Avramovic /// G_MERGE/G_UNMERGE and erase instructions that became dead in the process.
616ec575f6eSPetar Avramovic /// We manually assign 32 bit gprb to register operands of all new instructions
617ec575f6eSPetar Avramovic /// that got created in the process since they will not end up in RegBankSelect
618ec575f6eSPetar Avramovic /// loop. Careful not to delete instruction after MI i.e. MI.getIterator()++.
619ec575f6eSPetar Avramovic void MipsRegisterBankInfo::applyMappingImpl(
620ec575f6eSPetar Avramovic     const OperandsMapper &OpdMapper) const {
621ec575f6eSPetar Avramovic   MachineInstr &MI = OpdMapper.getMI();
622ec575f6eSPetar Avramovic   InstListTy NewInstrs;
623ec575f6eSPetar Avramovic   MachineIRBuilder B(MI);
624ec575f6eSPetar Avramovic   MachineFunction *MF = MI.getMF();
625ec575f6eSPetar Avramovic   MachineRegisterInfo &MRI = OpdMapper.getMRI();
626ec575f6eSPetar Avramovic 
627ec575f6eSPetar Avramovic   InstManager NewInstrObserver(NewInstrs);
628ec575f6eSPetar Avramovic   GISelObserverWrapper WrapperObserver(&NewInstrObserver);
629ec575f6eSPetar Avramovic   LegalizerHelper Helper(*MF, WrapperObserver, B);
630ec575f6eSPetar Avramovic   LegalizationArtifactCombiner ArtCombiner(
631ec575f6eSPetar Avramovic       B, MF->getRegInfo(), *MF->getSubtarget().getLegalizerInfo());
632ec575f6eSPetar Avramovic 
633ec575f6eSPetar Avramovic   switch (MI.getOpcode()) {
634aa699b20SPetar Avramovic   case TargetOpcode::G_LOAD:
635dbb6d01dSPetar Avramovic   case TargetOpcode::G_STORE:
636be20e361SPetar Avramovic   case TargetOpcode::G_PHI:
637dbb6d01dSPetar Avramovic   case TargetOpcode::G_SELECT: {
638ec575f6eSPetar Avramovic     Helper.narrowScalar(MI, 0, LLT::scalar(32));
639ec575f6eSPetar Avramovic     // Handle new instructions.
640ec575f6eSPetar Avramovic     while (!NewInstrs.empty()) {
641ec575f6eSPetar Avramovic       MachineInstr *NewMI = NewInstrs.pop_back_val();
642ec575f6eSPetar Avramovic       // This is new G_UNMERGE that was created during narrowScalar and will
643ec575f6eSPetar Avramovic       // not be considered for regbank selection. RegBankSelect for mips
644ec575f6eSPetar Avramovic       // visits/makes corresponding G_MERGE first. Combine them here.
645ec575f6eSPetar Avramovic       if (NewMI->getOpcode() == TargetOpcode::G_UNMERGE_VALUES) {
646ec575f6eSPetar Avramovic         SmallVector<MachineInstr *, 2> DeadInstrs;
647ec575f6eSPetar Avramovic         ArtCombiner.tryCombineMerges(*NewMI, DeadInstrs);
648ec575f6eSPetar Avramovic         for (MachineInstr *DeadMI : DeadInstrs)
649ec575f6eSPetar Avramovic           DeadMI->eraseFromParent();
650aa699b20SPetar Avramovic       }
651aa699b20SPetar Avramovic       // This G_MERGE will be combined away when its corresponding G_UNMERGE
652aa699b20SPetar Avramovic       // gets regBankSelected.
653aa699b20SPetar Avramovic       else if (NewMI->getOpcode() == TargetOpcode::G_MERGE_VALUES)
654aa699b20SPetar Avramovic         continue;
655aa699b20SPetar Avramovic       else
656ec575f6eSPetar Avramovic         // Manually set register banks for all register operands to 32 bit gprb.
657ec575f6eSPetar Avramovic         for (auto Op : NewMI->operands()) {
658ec575f6eSPetar Avramovic           if (Op.isReg()) {
659ec575f6eSPetar Avramovic             assert(MRI.getType(Op.getReg()).getSizeInBits() == 32 &&
660ec575f6eSPetar Avramovic                    "Only 32 bit gprb is handled here.\n");
661ec575f6eSPetar Avramovic             MRI.setRegBank(Op.getReg(), getRegBank(Mips::GPRBRegBankID));
662ec575f6eSPetar Avramovic           }
663ec575f6eSPetar Avramovic         }
664ec575f6eSPetar Avramovic     }
665ec575f6eSPetar Avramovic     return;
666ec575f6eSPetar Avramovic   }
667aa699b20SPetar Avramovic   case TargetOpcode::G_UNMERGE_VALUES: {
668aa699b20SPetar Avramovic     SmallVector<MachineInstr *, 2> DeadInstrs;
669aa699b20SPetar Avramovic     ArtCombiner.tryCombineMerges(MI, DeadInstrs);
670aa699b20SPetar Avramovic     for (MachineInstr *DeadMI : DeadInstrs)
671aa699b20SPetar Avramovic       DeadMI->eraseFromParent();
672aa699b20SPetar Avramovic     return;
673aa699b20SPetar Avramovic   }
674ec575f6eSPetar Avramovic   default:
675ec575f6eSPetar Avramovic     break;
676ec575f6eSPetar Avramovic   }
677ec575f6eSPetar Avramovic 
678ec575f6eSPetar Avramovic   return applyDefaultMapping(OpdMapper);
679ec575f6eSPetar Avramovic }
680