1 //===- ARMInstructionSelector.cpp ----------------------------*- C++ -*-==//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 /// \file
9 /// This file implements the targeting of the InstructionSelector class for ARM.
10 /// \todo This should be generated by TableGen.
11 //===----------------------------------------------------------------------===//
12 
13 #include "ARMRegisterBankInfo.h"
14 #include "ARMSubtarget.h"
15 #include "ARMTargetMachine.h"
16 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
17 #include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
18 #include "llvm/CodeGen/MachineConstantPool.h"
19 #include "llvm/CodeGen/MachineRegisterInfo.h"
20 #include "llvm/Support/Debug.h"
21 
22 #define DEBUG_TYPE "arm-isel"
23 
24 using namespace llvm;
25 
26 namespace {
27 
28 #define GET_GLOBALISEL_PREDICATE_BITSET
29 #include "ARMGenGlobalISel.inc"
30 #undef GET_GLOBALISEL_PREDICATE_BITSET
31 
32 class ARMInstructionSelector : public InstructionSelector {
33 public:
34   ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI,
35                          const ARMRegisterBankInfo &RBI);
36 
37   bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
38   static const char *getName() { return DEBUG_TYPE; }
39 
40 private:
41   bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
42 
43   struct CmpConstants;
44   struct InsertInfo;
45 
46   bool selectCmp(CmpConstants Helper, MachineInstrBuilder &MIB,
47                  MachineRegisterInfo &MRI) const;
48 
49   // Helper for inserting a comparison sequence that sets \p ResReg to either 1
50   // if \p LHSReg and \p RHSReg are in the relationship defined by \p Cond, or
51   // \p PrevRes otherwise. In essence, it computes PrevRes OR (LHS Cond RHS).
52   bool insertComparison(CmpConstants Helper, InsertInfo I, unsigned ResReg,
53                         ARMCC::CondCodes Cond, unsigned LHSReg, unsigned RHSReg,
54                         unsigned PrevRes) const;
55 
56   // Set \p DestReg to \p Constant.
57   void putConstant(InsertInfo I, unsigned DestReg, unsigned Constant) const;
58 
59   bool selectGlobal(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
60   bool selectSelect(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
61   bool selectShift(unsigned ShiftOpc, MachineInstrBuilder &MIB) const;
62 
63   // Check if the types match and both operands have the expected size and
64   // register bank.
65   bool validOpRegPair(MachineRegisterInfo &MRI, unsigned LHS, unsigned RHS,
66                       unsigned ExpectedSize, unsigned ExpectedRegBankID) const;
67 
68   // Check if the register has the expected size and register bank.
69   bool validReg(MachineRegisterInfo &MRI, unsigned Reg, unsigned ExpectedSize,
70                 unsigned ExpectedRegBankID) const;
71 
72   const ARMBaseInstrInfo &TII;
73   const ARMBaseRegisterInfo &TRI;
74   const ARMBaseTargetMachine &TM;
75   const ARMRegisterBankInfo &RBI;
76   const ARMSubtarget &STI;
77 
78   // FIXME: This is necessary because DAGISel uses "Subtarget->" and GlobalISel
79   // uses "STI." in the code generated by TableGen. If we want to reuse some of
80   // the custom C++ predicates written for DAGISel, we need to have both around.
81   const ARMSubtarget *Subtarget = &STI;
82 
83   // Store the opcodes that we might need, so we don't have to check what kind
84   // of subtarget (ARM vs Thumb) we have all the time.
85   struct OpcodeCache {
86     unsigned ZEXT16;
87     unsigned SEXT16;
88 
89     unsigned ZEXT8;
90     unsigned SEXT8;
91 
92     // Used for implementing ZEXT/SEXT from i1
93     unsigned AND;
94     unsigned RSB;
95 
96     unsigned STORE32;
97     unsigned LOAD32;
98 
99     unsigned STORE16;
100     unsigned LOAD16;
101 
102     unsigned STORE8;
103     unsigned LOAD8;
104 
105     unsigned ADDrr;
106     unsigned ADDri;
107 
108     // Used for G_ICMP
109     unsigned CMPrr;
110     unsigned MOVi;
111     unsigned MOVCCi;
112 
113     // Used for G_SELECT
114     unsigned MOVCCr;
115 
116     unsigned TSTri;
117     unsigned Bcc;
118 
119     // Used for G_GLOBAL_VALUE
120     unsigned MOVi32imm;
121     unsigned ConstPoolLoad;
122     unsigned MOV_ga_pcrel;
123     unsigned LDRLIT_ga_pcrel;
124     unsigned LDRLIT_ga_abs;
125 
126     OpcodeCache(const ARMSubtarget &STI);
127   } const Opcodes;
128 
129   // Select the opcode for simple extensions (that translate to a single SXT/UXT
130   // instruction). Extension operations more complicated than that should not
131   // invoke this. Returns the original opcode if it doesn't know how to select a
132   // better one.
133   unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) const;
134 
135   // Select the opcode for simple loads and stores. Returns the original opcode
136   // if it doesn't know how to select a better one.
137   unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
138                                  unsigned Size) const;
139 
140 #define GET_GLOBALISEL_PREDICATES_DECL
141 #include "ARMGenGlobalISel.inc"
142 #undef GET_GLOBALISEL_PREDICATES_DECL
143 
144 // We declare the temporaries used by selectImpl() in the class to minimize the
145 // cost of constructing placeholder values.
146 #define GET_GLOBALISEL_TEMPORARIES_DECL
147 #include "ARMGenGlobalISel.inc"
148 #undef GET_GLOBALISEL_TEMPORARIES_DECL
149 };
150 } // end anonymous namespace
151 
152 namespace llvm {
153 InstructionSelector *
154 createARMInstructionSelector(const ARMBaseTargetMachine &TM,
155                              const ARMSubtarget &STI,
156                              const ARMRegisterBankInfo &RBI) {
157   return new ARMInstructionSelector(TM, STI, RBI);
158 }
159 }
160 
161 const unsigned zero_reg = 0;
162 
163 #define GET_GLOBALISEL_IMPL
164 #include "ARMGenGlobalISel.inc"
165 #undef GET_GLOBALISEL_IMPL
166 
167 ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM,
168                                                const ARMSubtarget &STI,
169                                                const ARMRegisterBankInfo &RBI)
170     : InstructionSelector(), TII(*STI.getInstrInfo()),
171       TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), Opcodes(STI),
172 #define GET_GLOBALISEL_PREDICATES_INIT
173 #include "ARMGenGlobalISel.inc"
174 #undef GET_GLOBALISEL_PREDICATES_INIT
175 #define GET_GLOBALISEL_TEMPORARIES_INIT
176 #include "ARMGenGlobalISel.inc"
177 #undef GET_GLOBALISEL_TEMPORARIES_INIT
178 {
179 }
180 
181 static const TargetRegisterClass *guessRegClass(unsigned Reg,
182                                                 MachineRegisterInfo &MRI,
183                                                 const TargetRegisterInfo &TRI,
184                                                 const RegisterBankInfo &RBI) {
185   const RegisterBank *RegBank = RBI.getRegBank(Reg, MRI, TRI);
186   assert(RegBank && "Can't get reg bank for virtual register");
187 
188   const unsigned Size = MRI.getType(Reg).getSizeInBits();
189   assert((RegBank->getID() == ARM::GPRRegBankID ||
190           RegBank->getID() == ARM::FPRRegBankID) &&
191          "Unsupported reg bank");
192 
193   if (RegBank->getID() == ARM::FPRRegBankID) {
194     if (Size == 32)
195       return &ARM::SPRRegClass;
196     else if (Size == 64)
197       return &ARM::DPRRegClass;
198     else if (Size == 128)
199       return &ARM::QPRRegClass;
200     else
201       llvm_unreachable("Unsupported destination size");
202   }
203 
204   return &ARM::GPRRegClass;
205 }
206 
207 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
208                        MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
209                        const RegisterBankInfo &RBI) {
210   unsigned DstReg = I.getOperand(0).getReg();
211   if (TargetRegisterInfo::isPhysicalRegister(DstReg))
212     return true;
213 
214   const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI);
215 
216   // No need to constrain SrcReg. It will get constrained when
217   // we hit another of its uses or its defs.
218   // Copies do not have constraints.
219   if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
220     LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
221                       << " operand\n");
222     return false;
223   }
224   return true;
225 }
226 
227 static bool selectMergeValues(MachineInstrBuilder &MIB,
228                               const ARMBaseInstrInfo &TII,
229                               MachineRegisterInfo &MRI,
230                               const TargetRegisterInfo &TRI,
231                               const RegisterBankInfo &RBI) {
232   assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP");
233 
234   // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs
235   // into one DPR.
236   unsigned VReg0 = MIB->getOperand(0).getReg();
237   (void)VReg0;
238   assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
239          RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
240          "Unsupported operand for G_MERGE_VALUES");
241   unsigned VReg1 = MIB->getOperand(1).getReg();
242   (void)VReg1;
243   assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
244          RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
245          "Unsupported operand for G_MERGE_VALUES");
246   unsigned VReg2 = MIB->getOperand(2).getReg();
247   (void)VReg2;
248   assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
249          RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
250          "Unsupported operand for G_MERGE_VALUES");
251 
252   MIB->setDesc(TII.get(ARM::VMOVDRR));
253   MIB.add(predOps(ARMCC::AL));
254 
255   return true;
256 }
257 
258 static bool selectUnmergeValues(MachineInstrBuilder &MIB,
259                                 const ARMBaseInstrInfo &TII,
260                                 MachineRegisterInfo &MRI,
261                                 const TargetRegisterInfo &TRI,
262                                 const RegisterBankInfo &RBI) {
263   assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP");
264 
265   // We only support G_UNMERGE_VALUES as a way to break up one DPR into two
266   // GPRs.
267   unsigned VReg0 = MIB->getOperand(0).getReg();
268   (void)VReg0;
269   assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
270          RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
271          "Unsupported operand for G_UNMERGE_VALUES");
272   unsigned VReg1 = MIB->getOperand(1).getReg();
273   (void)VReg1;
274   assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
275          RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
276          "Unsupported operand for G_UNMERGE_VALUES");
277   unsigned VReg2 = MIB->getOperand(2).getReg();
278   (void)VReg2;
279   assert(MRI.getType(VReg2).getSizeInBits() == 64 &&
280          RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID &&
281          "Unsupported operand for G_UNMERGE_VALUES");
282 
283   MIB->setDesc(TII.get(ARM::VMOVRRD));
284   MIB.add(predOps(ARMCC::AL));
285 
286   return true;
287 }
288 
289 ARMInstructionSelector::OpcodeCache::OpcodeCache(const ARMSubtarget &STI) {
290   bool isThumb = STI.isThumb();
291 
292   using namespace TargetOpcode;
293 
294 #define STORE_OPCODE(VAR, OPC) VAR = isThumb ? ARM::t2##OPC : ARM::OPC
295   STORE_OPCODE(SEXT16, SXTH);
296   STORE_OPCODE(ZEXT16, UXTH);
297 
298   STORE_OPCODE(SEXT8, SXTB);
299   STORE_OPCODE(ZEXT8, UXTB);
300 
301   STORE_OPCODE(AND, ANDri);
302   STORE_OPCODE(RSB, RSBri);
303 
304   STORE_OPCODE(STORE32, STRi12);
305   STORE_OPCODE(LOAD32, LDRi12);
306 
307   // LDRH/STRH are special...
308   STORE16 = isThumb ? ARM::t2STRHi12 : ARM::STRH;
309   LOAD16 = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
310 
311   STORE_OPCODE(STORE8, STRBi12);
312   STORE_OPCODE(LOAD8, LDRBi12);
313 
314   STORE_OPCODE(ADDrr, ADDrr);
315   STORE_OPCODE(ADDri, ADDri);
316 
317   STORE_OPCODE(CMPrr, CMPrr);
318   STORE_OPCODE(MOVi, MOVi);
319   STORE_OPCODE(MOVCCi, MOVCCi);
320 
321   STORE_OPCODE(MOVCCr, MOVCCr);
322 
323   STORE_OPCODE(TSTri, TSTri);
324   STORE_OPCODE(Bcc, Bcc);
325 
326   STORE_OPCODE(MOVi32imm, MOVi32imm);
327   ConstPoolLoad = isThumb ? ARM::t2LDRpci : ARM::LDRi12;
328   STORE_OPCODE(MOV_ga_pcrel, MOV_ga_pcrel);
329   LDRLIT_ga_pcrel = isThumb ? ARM::tLDRLIT_ga_pcrel : ARM::LDRLIT_ga_pcrel;
330   LDRLIT_ga_abs = isThumb ? ARM::tLDRLIT_ga_abs : ARM::LDRLIT_ga_abs;
331 #undef MAP_OPCODE
332 }
333 
334 unsigned ARMInstructionSelector::selectSimpleExtOpc(unsigned Opc,
335                                                     unsigned Size) const {
336   using namespace TargetOpcode;
337 
338   if (Size != 8 && Size != 16)
339     return Opc;
340 
341   if (Opc == G_SEXT)
342     return Size == 8 ? Opcodes.SEXT8 : Opcodes.SEXT16;
343 
344   if (Opc == G_ZEXT)
345     return Size == 8 ? Opcodes.ZEXT8 : Opcodes.ZEXT16;
346 
347   return Opc;
348 }
349 
350 unsigned ARMInstructionSelector::selectLoadStoreOpCode(unsigned Opc,
351                                                        unsigned RegBank,
352                                                        unsigned Size) const {
353   bool isStore = Opc == TargetOpcode::G_STORE;
354 
355   if (RegBank == ARM::GPRRegBankID) {
356     switch (Size) {
357     case 1:
358     case 8:
359       return isStore ? Opcodes.STORE8 : Opcodes.LOAD8;
360     case 16:
361       return isStore ? Opcodes.STORE16 : Opcodes.LOAD16;
362     case 32:
363       return isStore ? Opcodes.STORE32 : Opcodes.LOAD32;
364     default:
365       return Opc;
366     }
367   }
368 
369   if (RegBank == ARM::FPRRegBankID) {
370     switch (Size) {
371     case 32:
372       return isStore ? ARM::VSTRS : ARM::VLDRS;
373     case 64:
374       return isStore ? ARM::VSTRD : ARM::VLDRD;
375     default:
376       return Opc;
377     }
378   }
379 
380   return Opc;
381 }
382 
383 // When lowering comparisons, we sometimes need to perform two compares instead
384 // of just one. Get the condition codes for both comparisons. If only one is
385 // needed, the second member of the pair is ARMCC::AL.
386 static std::pair<ARMCC::CondCodes, ARMCC::CondCodes>
387 getComparePreds(CmpInst::Predicate Pred) {
388   std::pair<ARMCC::CondCodes, ARMCC::CondCodes> Preds = {ARMCC::AL, ARMCC::AL};
389   switch (Pred) {
390   case CmpInst::FCMP_ONE:
391     Preds = {ARMCC::GT, ARMCC::MI};
392     break;
393   case CmpInst::FCMP_UEQ:
394     Preds = {ARMCC::EQ, ARMCC::VS};
395     break;
396   case CmpInst::ICMP_EQ:
397   case CmpInst::FCMP_OEQ:
398     Preds.first = ARMCC::EQ;
399     break;
400   case CmpInst::ICMP_SGT:
401   case CmpInst::FCMP_OGT:
402     Preds.first = ARMCC::GT;
403     break;
404   case CmpInst::ICMP_SGE:
405   case CmpInst::FCMP_OGE:
406     Preds.first = ARMCC::GE;
407     break;
408   case CmpInst::ICMP_UGT:
409   case CmpInst::FCMP_UGT:
410     Preds.first = ARMCC::HI;
411     break;
412   case CmpInst::FCMP_OLT:
413     Preds.first = ARMCC::MI;
414     break;
415   case CmpInst::ICMP_ULE:
416   case CmpInst::FCMP_OLE:
417     Preds.first = ARMCC::LS;
418     break;
419   case CmpInst::FCMP_ORD:
420     Preds.first = ARMCC::VC;
421     break;
422   case CmpInst::FCMP_UNO:
423     Preds.first = ARMCC::VS;
424     break;
425   case CmpInst::FCMP_UGE:
426     Preds.first = ARMCC::PL;
427     break;
428   case CmpInst::ICMP_SLT:
429   case CmpInst::FCMP_ULT:
430     Preds.first = ARMCC::LT;
431     break;
432   case CmpInst::ICMP_SLE:
433   case CmpInst::FCMP_ULE:
434     Preds.first = ARMCC::LE;
435     break;
436   case CmpInst::FCMP_UNE:
437   case CmpInst::ICMP_NE:
438     Preds.first = ARMCC::NE;
439     break;
440   case CmpInst::ICMP_UGE:
441     Preds.first = ARMCC::HS;
442     break;
443   case CmpInst::ICMP_ULT:
444     Preds.first = ARMCC::LO;
445     break;
446   default:
447     break;
448   }
449   assert(Preds.first != ARMCC::AL && "No comparisons needed?");
450   return Preds;
451 }
452 
453 struct ARMInstructionSelector::CmpConstants {
454   CmpConstants(unsigned CmpOpcode, unsigned FlagsOpcode, unsigned SelectOpcode,
455                unsigned OpRegBank, unsigned OpSize)
456       : ComparisonOpcode(CmpOpcode), ReadFlagsOpcode(FlagsOpcode),
457         SelectResultOpcode(SelectOpcode), OperandRegBankID(OpRegBank),
458         OperandSize(OpSize) {}
459 
460   // The opcode used for performing the comparison.
461   const unsigned ComparisonOpcode;
462 
463   // The opcode used for reading the flags set by the comparison. May be
464   // ARM::INSTRUCTION_LIST_END if we don't need to read the flags.
465   const unsigned ReadFlagsOpcode;
466 
467   // The opcode used for materializing the result of the comparison.
468   const unsigned SelectResultOpcode;
469 
470   // The assumed register bank ID for the operands.
471   const unsigned OperandRegBankID;
472 
473   // The assumed size in bits for the operands.
474   const unsigned OperandSize;
475 };
476 
477 struct ARMInstructionSelector::InsertInfo {
478   InsertInfo(MachineInstrBuilder &MIB)
479       : MBB(*MIB->getParent()), InsertBefore(std::next(MIB->getIterator())),
480         DbgLoc(MIB->getDebugLoc()) {}
481 
482   MachineBasicBlock &MBB;
483   const MachineBasicBlock::instr_iterator InsertBefore;
484   const DebugLoc &DbgLoc;
485 };
486 
487 void ARMInstructionSelector::putConstant(InsertInfo I, unsigned DestReg,
488                                          unsigned Constant) const {
489   (void)BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Opcodes.MOVi))
490       .addDef(DestReg)
491       .addImm(Constant)
492       .add(predOps(ARMCC::AL))
493       .add(condCodeOp());
494 }
495 
496 bool ARMInstructionSelector::validOpRegPair(MachineRegisterInfo &MRI,
497                                             unsigned LHSReg, unsigned RHSReg,
498                                             unsigned ExpectedSize,
499                                             unsigned ExpectedRegBankID) const {
500   return MRI.getType(LHSReg) == MRI.getType(RHSReg) &&
501          validReg(MRI, LHSReg, ExpectedSize, ExpectedRegBankID) &&
502          validReg(MRI, RHSReg, ExpectedSize, ExpectedRegBankID);
503 }
504 
505 bool ARMInstructionSelector::validReg(MachineRegisterInfo &MRI, unsigned Reg,
506                                       unsigned ExpectedSize,
507                                       unsigned ExpectedRegBankID) const {
508   if (MRI.getType(Reg).getSizeInBits() != ExpectedSize) {
509     LLVM_DEBUG(dbgs() << "Unexpected size for register");
510     return false;
511   }
512 
513   if (RBI.getRegBank(Reg, MRI, TRI)->getID() != ExpectedRegBankID) {
514     LLVM_DEBUG(dbgs() << "Unexpected register bank for register");
515     return false;
516   }
517 
518   return true;
519 }
520 
521 bool ARMInstructionSelector::selectCmp(CmpConstants Helper,
522                                        MachineInstrBuilder &MIB,
523                                        MachineRegisterInfo &MRI) const {
524   const InsertInfo I(MIB);
525 
526   auto ResReg = MIB->getOperand(0).getReg();
527   if (!validReg(MRI, ResReg, 1, ARM::GPRRegBankID))
528     return false;
529 
530   auto Cond =
531       static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate());
532   if (Cond == CmpInst::FCMP_TRUE || Cond == CmpInst::FCMP_FALSE) {
533     putConstant(I, ResReg, Cond == CmpInst::FCMP_TRUE ? 1 : 0);
534     MIB->eraseFromParent();
535     return true;
536   }
537 
538   auto LHSReg = MIB->getOperand(2).getReg();
539   auto RHSReg = MIB->getOperand(3).getReg();
540   if (!validOpRegPair(MRI, LHSReg, RHSReg, Helper.OperandSize,
541                       Helper.OperandRegBankID))
542     return false;
543 
544   auto ARMConds = getComparePreds(Cond);
545   auto ZeroReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
546   putConstant(I, ZeroReg, 0);
547 
548   if (ARMConds.second == ARMCC::AL) {
549     // Simple case, we only need one comparison and we're done.
550     if (!insertComparison(Helper, I, ResReg, ARMConds.first, LHSReg, RHSReg,
551                           ZeroReg))
552       return false;
553   } else {
554     // Not so simple, we need two successive comparisons.
555     auto IntermediateRes = MRI.createVirtualRegister(&ARM::GPRRegClass);
556     if (!insertComparison(Helper, I, IntermediateRes, ARMConds.first, LHSReg,
557                           RHSReg, ZeroReg))
558       return false;
559     if (!insertComparison(Helper, I, ResReg, ARMConds.second, LHSReg, RHSReg,
560                           IntermediateRes))
561       return false;
562   }
563 
564   MIB->eraseFromParent();
565   return true;
566 }
567 
568 bool ARMInstructionSelector::insertComparison(CmpConstants Helper, InsertInfo I,
569                                               unsigned ResReg,
570                                               ARMCC::CondCodes Cond,
571                                               unsigned LHSReg, unsigned RHSReg,
572                                               unsigned PrevRes) const {
573   // Perform the comparison.
574   auto CmpI =
575       BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Helper.ComparisonOpcode))
576           .addUse(LHSReg)
577           .addUse(RHSReg)
578           .add(predOps(ARMCC::AL));
579   if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
580     return false;
581 
582   // Read the comparison flags (if necessary).
583   if (Helper.ReadFlagsOpcode != ARM::INSTRUCTION_LIST_END) {
584     auto ReadI = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc,
585                          TII.get(Helper.ReadFlagsOpcode))
586                      .add(predOps(ARMCC::AL));
587     if (!constrainSelectedInstRegOperands(*ReadI, TII, TRI, RBI))
588       return false;
589   }
590 
591   // Select either 1 or the previous result based on the value of the flags.
592   auto Mov1I = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc,
593                        TII.get(Helper.SelectResultOpcode))
594                    .addDef(ResReg)
595                    .addUse(PrevRes)
596                    .addImm(1)
597                    .add(predOps(Cond, ARM::CPSR));
598   if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
599     return false;
600 
601   return true;
602 }
603 
604 bool ARMInstructionSelector::selectGlobal(MachineInstrBuilder &MIB,
605                                           MachineRegisterInfo &MRI) const {
606   if ((STI.isROPI() || STI.isRWPI()) && !STI.isTargetELF()) {
607     LLVM_DEBUG(dbgs() << "ROPI and RWPI only supported for ELF\n");
608     return false;
609   }
610 
611   auto GV = MIB->getOperand(1).getGlobal();
612   if (GV->isThreadLocal()) {
613     LLVM_DEBUG(dbgs() << "TLS variables not supported yet\n");
614     return false;
615   }
616 
617   auto &MBB = *MIB->getParent();
618   auto &MF = *MBB.getParent();
619 
620   bool UseMovt = STI.useMovt();
621 
622   unsigned Size = TM.getPointerSize(0);
623   unsigned Alignment = 4;
624 
625   auto addOpsForConstantPoolLoad = [&MF, Alignment,
626                                     Size](MachineInstrBuilder &MIB,
627                                           const GlobalValue *GV, bool IsSBREL) {
628     assert((MIB->getOpcode() == ARM::LDRi12 ||
629             MIB->getOpcode() == ARM::t2LDRpci) &&
630            "Unsupported instruction");
631     auto ConstPool = MF.getConstantPool();
632     auto CPIndex =
633         // For SB relative entries we need a target-specific constant pool.
634         // Otherwise, just use a regular constant pool entry.
635         IsSBREL
636             ? ConstPool->getConstantPoolIndex(
637                   ARMConstantPoolConstant::Create(GV, ARMCP::SBREL), Alignment)
638             : ConstPool->getConstantPoolIndex(GV, Alignment);
639     MIB.addConstantPoolIndex(CPIndex, /*Offset*/ 0, /*TargetFlags*/ 0)
640         .addMemOperand(MF.getMachineMemOperand(
641             MachinePointerInfo::getConstantPool(MF), MachineMemOperand::MOLoad,
642             Size, Alignment));
643     if (MIB->getOpcode() == ARM::LDRi12)
644       MIB.addImm(0);
645     MIB.add(predOps(ARMCC::AL));
646   };
647 
648   auto addGOTMemOperand = [this, &MF, Alignment](MachineInstrBuilder &MIB) {
649     MIB.addMemOperand(MF.getMachineMemOperand(
650         MachinePointerInfo::getGOT(MF), MachineMemOperand::MOLoad,
651         TM.getProgramPointerSize(), Alignment));
652   };
653 
654   if (TM.isPositionIndependent()) {
655     bool Indirect = STI.isGVIndirectSymbol(GV);
656 
657     // For ARM mode, we have different pseudoinstructions for direct accesses
658     // and indirect accesses, and the ones for indirect accesses include the
659     // load from GOT. For Thumb mode, we use the same pseudoinstruction for both
660     // direct and indirect accesses, and we need to manually generate the load
661     // from GOT.
662     bool UseOpcodeThatLoads = Indirect && !STI.isThumb();
663 
664     // FIXME: Taking advantage of MOVT for ELF is pretty involved, so we don't
665     // support it yet. See PR28229.
666     unsigned Opc =
667         UseMovt && !STI.isTargetELF()
668             ? (UseOpcodeThatLoads ? (unsigned)ARM::MOV_ga_pcrel_ldr
669                                   : Opcodes.MOV_ga_pcrel)
670             : (UseOpcodeThatLoads ? (unsigned)ARM::LDRLIT_ga_pcrel_ldr
671                                   : Opcodes.LDRLIT_ga_pcrel);
672     MIB->setDesc(TII.get(Opc));
673 
674     int TargetFlags = ARMII::MO_NO_FLAG;
675     if (STI.isTargetDarwin())
676       TargetFlags |= ARMII::MO_NONLAZY;
677     if (STI.isGVInGOT(GV))
678       TargetFlags |= ARMII::MO_GOT;
679     MIB->getOperand(1).setTargetFlags(TargetFlags);
680 
681     if (Indirect) {
682       if (!UseOpcodeThatLoads) {
683         auto ResultReg = MIB->getOperand(0).getReg();
684         auto AddressReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
685 
686         MIB->getOperand(0).setReg(AddressReg);
687 
688         auto InsertBefore = std::next(MIB->getIterator());
689         auto MIBLoad = BuildMI(MBB, InsertBefore, MIB->getDebugLoc(),
690                                TII.get(Opcodes.LOAD32))
691                            .addDef(ResultReg)
692                            .addReg(AddressReg)
693                            .addImm(0)
694                            .add(predOps(ARMCC::AL));
695         addGOTMemOperand(MIBLoad);
696 
697         if (!constrainSelectedInstRegOperands(*MIBLoad, TII, TRI, RBI))
698           return false;
699       } else {
700         addGOTMemOperand(MIB);
701       }
702     }
703 
704     return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
705   }
706 
707   bool isReadOnly = STI.getTargetLowering()->isReadOnly(GV);
708   if (STI.isROPI() && isReadOnly) {
709     unsigned Opc = UseMovt ? Opcodes.MOV_ga_pcrel : Opcodes.LDRLIT_ga_pcrel;
710     MIB->setDesc(TII.get(Opc));
711     return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
712   }
713   if (STI.isRWPI() && !isReadOnly) {
714     auto Offset = MRI.createVirtualRegister(&ARM::GPRRegClass);
715     MachineInstrBuilder OffsetMIB;
716     if (UseMovt) {
717       OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(),
718                           TII.get(Opcodes.MOVi32imm), Offset);
719       OffsetMIB.addGlobalAddress(GV, /*Offset*/ 0, ARMII::MO_SBREL);
720     } else {
721       // Load the offset from the constant pool.
722       OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(),
723                           TII.get(Opcodes.ConstPoolLoad), Offset);
724       addOpsForConstantPoolLoad(OffsetMIB, GV, /*IsSBREL*/ true);
725     }
726     if (!constrainSelectedInstRegOperands(*OffsetMIB, TII, TRI, RBI))
727       return false;
728 
729     // Add the offset to the SB register.
730     MIB->setDesc(TII.get(Opcodes.ADDrr));
731     MIB->RemoveOperand(1);
732     MIB.addReg(ARM::R9) // FIXME: don't hardcode R9
733         .addReg(Offset)
734         .add(predOps(ARMCC::AL))
735         .add(condCodeOp());
736 
737     return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
738   }
739 
740   if (STI.isTargetELF()) {
741     if (UseMovt) {
742       MIB->setDesc(TII.get(Opcodes.MOVi32imm));
743     } else {
744       // Load the global's address from the constant pool.
745       MIB->setDesc(TII.get(Opcodes.ConstPoolLoad));
746       MIB->RemoveOperand(1);
747       addOpsForConstantPoolLoad(MIB, GV, /*IsSBREL*/ false);
748     }
749   } else if (STI.isTargetMachO()) {
750     if (UseMovt)
751       MIB->setDesc(TII.get(Opcodes.MOVi32imm));
752     else
753       MIB->setDesc(TII.get(Opcodes.LDRLIT_ga_abs));
754   } else {
755     LLVM_DEBUG(dbgs() << "Object format not supported yet\n");
756     return false;
757   }
758 
759   return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
760 }
761 
762 bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB,
763                                           MachineRegisterInfo &MRI) const {
764   auto &MBB = *MIB->getParent();
765   auto InsertBefore = std::next(MIB->getIterator());
766   auto &DbgLoc = MIB->getDebugLoc();
767 
768   // Compare the condition to 1.
769   auto CondReg = MIB->getOperand(1).getReg();
770   assert(validReg(MRI, CondReg, 1, ARM::GPRRegBankID) &&
771          "Unsupported types for select operation");
772   auto CmpI = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(Opcodes.TSTri))
773                   .addUse(CondReg)
774                   .addImm(1)
775                   .add(predOps(ARMCC::AL));
776   if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
777     return false;
778 
779   // Move a value into the result register based on the result of the
780   // comparison.
781   auto ResReg = MIB->getOperand(0).getReg();
782   auto TrueReg = MIB->getOperand(2).getReg();
783   auto FalseReg = MIB->getOperand(3).getReg();
784   assert(validOpRegPair(MRI, ResReg, TrueReg, 32, ARM::GPRRegBankID) &&
785          validOpRegPair(MRI, TrueReg, FalseReg, 32, ARM::GPRRegBankID) &&
786          "Unsupported types for select operation");
787   auto Mov1I = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(Opcodes.MOVCCr))
788                    .addDef(ResReg)
789                    .addUse(TrueReg)
790                    .addUse(FalseReg)
791                    .add(predOps(ARMCC::EQ, ARM::CPSR));
792   if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
793     return false;
794 
795   MIB->eraseFromParent();
796   return true;
797 }
798 
799 bool ARMInstructionSelector::selectShift(unsigned ShiftOpc,
800                                          MachineInstrBuilder &MIB) const {
801   MIB->setDesc(TII.get(ARM::MOVsr));
802   MIB.addImm(ShiftOpc);
803   MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
804   return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
805 }
806 
807 bool ARMInstructionSelector::select(MachineInstr &I,
808                                     CodeGenCoverage &CoverageInfo) const {
809   assert(I.getParent() && "Instruction should be in a basic block!");
810   assert(I.getParent()->getParent() && "Instruction should be in a function!");
811 
812   auto &MBB = *I.getParent();
813   auto &MF = *MBB.getParent();
814   auto &MRI = MF.getRegInfo();
815 
816   if (!isPreISelGenericOpcode(I.getOpcode())) {
817     if (I.isCopy())
818       return selectCopy(I, TII, MRI, TRI, RBI);
819 
820     return true;
821   }
822 
823   using namespace TargetOpcode;
824 
825   if (selectImpl(I, CoverageInfo))
826     return true;
827 
828   MachineInstrBuilder MIB{MF, I};
829   bool isSExt = false;
830 
831   switch (I.getOpcode()) {
832   case G_SEXT:
833     isSExt = true;
834     LLVM_FALLTHROUGH;
835   case G_ZEXT: {
836     LLT DstTy = MRI.getType(I.getOperand(0).getReg());
837     // FIXME: Smaller destination sizes coming soon!
838     if (DstTy.getSizeInBits() != 32) {
839       LLVM_DEBUG(dbgs() << "Unsupported destination size for extension");
840       return false;
841     }
842 
843     LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
844     unsigned SrcSize = SrcTy.getSizeInBits();
845     switch (SrcSize) {
846     case 1: {
847       // ZExt boils down to & 0x1; for SExt we also subtract that from 0
848       I.setDesc(TII.get(Opcodes.AND));
849       MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
850 
851       if (isSExt) {
852         unsigned SExtResult = I.getOperand(0).getReg();
853 
854         // Use a new virtual register for the result of the AND
855         unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
856         I.getOperand(0).setReg(AndResult);
857 
858         auto InsertBefore = std::next(I.getIterator());
859         auto SubI =
860             BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(Opcodes.RSB))
861                 .addDef(SExtResult)
862                 .addUse(AndResult)
863                 .addImm(0)
864                 .add(predOps(ARMCC::AL))
865                 .add(condCodeOp());
866         if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
867           return false;
868       }
869       break;
870     }
871     case 8:
872     case 16: {
873       unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
874       if (NewOpc == I.getOpcode())
875         return false;
876       I.setDesc(TII.get(NewOpc));
877       MIB.addImm(0).add(predOps(ARMCC::AL));
878       break;
879     }
880     default:
881       LLVM_DEBUG(dbgs() << "Unsupported source size for extension");
882       return false;
883     }
884     break;
885   }
886   case G_ANYEXT:
887   case G_TRUNC: {
888     // The high bits are undefined, so there's nothing special to do, just
889     // treat it as a copy.
890     auto SrcReg = I.getOperand(1).getReg();
891     auto DstReg = I.getOperand(0).getReg();
892 
893     const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
894     const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
895 
896     if (SrcRegBank.getID() == ARM::FPRRegBankID) {
897       // This should only happen in the obscure case where we have put a 64-bit
898       // integer into a D register. Get it out of there and keep only the
899       // interesting part.
900       assert(I.getOpcode() == G_TRUNC && "Unsupported operand for G_ANYEXT");
901       assert(DstRegBank.getID() == ARM::GPRRegBankID &&
902              "Unsupported combination of register banks");
903       assert(MRI.getType(SrcReg).getSizeInBits() == 64 && "Unsupported size");
904       assert(MRI.getType(DstReg).getSizeInBits() <= 32 && "Unsupported size");
905 
906       unsigned IgnoredBits = MRI.createVirtualRegister(&ARM::GPRRegClass);
907       auto InsertBefore = std::next(I.getIterator());
908       auto MovI =
909           BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::VMOVRRD))
910               .addDef(DstReg)
911               .addDef(IgnoredBits)
912               .addUse(SrcReg)
913               .add(predOps(ARMCC::AL));
914       if (!constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI))
915         return false;
916 
917       MIB->eraseFromParent();
918       return true;
919     }
920 
921     if (SrcRegBank.getID() != DstRegBank.getID()) {
922       LLVM_DEBUG(
923           dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n");
924       return false;
925     }
926 
927     if (SrcRegBank.getID() != ARM::GPRRegBankID) {
928       LLVM_DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n");
929       return false;
930     }
931 
932     I.setDesc(TII.get(COPY));
933     return selectCopy(I, TII, MRI, TRI, RBI);
934   }
935   case G_CONSTANT: {
936     if (!MRI.getType(I.getOperand(0).getReg()).isPointer()) {
937       // Non-pointer constants should be handled by TableGen.
938       LLVM_DEBUG(dbgs() << "Unsupported constant type\n");
939       return false;
940     }
941 
942     auto &Val = I.getOperand(1);
943     if (Val.isCImm()) {
944       if (!Val.getCImm()->isZero()) {
945         LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n");
946         return false;
947       }
948       Val.ChangeToImmediate(0);
949     } else {
950       assert(Val.isImm() && "Unexpected operand for G_CONSTANT");
951       if (Val.getImm() != 0) {
952         LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n");
953         return false;
954       }
955     }
956 
957     I.setDesc(TII.get(ARM::MOVi));
958     MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
959     break;
960   }
961   case G_INTTOPTR:
962   case G_PTRTOINT: {
963     auto SrcReg = I.getOperand(1).getReg();
964     auto DstReg = I.getOperand(0).getReg();
965 
966     const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
967     const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
968 
969     if (SrcRegBank.getID() != DstRegBank.getID()) {
970       LLVM_DEBUG(
971           dbgs()
972           << "G_INTTOPTR/G_PTRTOINT operands on different register banks\n");
973       return false;
974     }
975 
976     if (SrcRegBank.getID() != ARM::GPRRegBankID) {
977       LLVM_DEBUG(
978           dbgs() << "G_INTTOPTR/G_PTRTOINT on non-GPR not supported yet\n");
979       return false;
980     }
981 
982     I.setDesc(TII.get(COPY));
983     return selectCopy(I, TII, MRI, TRI, RBI);
984   }
985   case G_SELECT:
986     return selectSelect(MIB, MRI);
987   case G_ICMP: {
988     CmpConstants Helper(Opcodes.CMPrr, ARM::INSTRUCTION_LIST_END,
989                         Opcodes.MOVCCi, ARM::GPRRegBankID, 32);
990     return selectCmp(Helper, MIB, MRI);
991   }
992   case G_FCMP: {
993     assert(STI.hasVFP2() && "Can't select fcmp without VFP");
994 
995     unsigned OpReg = I.getOperand(2).getReg();
996     unsigned Size = MRI.getType(OpReg).getSizeInBits();
997 
998     if (Size == 64 && STI.isFPOnlySP()) {
999       LLVM_DEBUG(dbgs() << "Subtarget only supports single precision");
1000       return false;
1001     }
1002     if (Size != 32 && Size != 64) {
1003       LLVM_DEBUG(dbgs() << "Unsupported size for G_FCMP operand");
1004       return false;
1005     }
1006 
1007     CmpConstants Helper(Size == 32 ? ARM::VCMPS : ARM::VCMPD, ARM::FMSTAT,
1008                         Opcodes.MOVCCi, ARM::FPRRegBankID, Size);
1009     return selectCmp(Helper, MIB, MRI);
1010   }
1011   case G_LSHR:
1012     return selectShift(ARM_AM::ShiftOpc::lsr, MIB);
1013   case G_ASHR:
1014     return selectShift(ARM_AM::ShiftOpc::asr, MIB);
1015   case G_SHL: {
1016     return selectShift(ARM_AM::ShiftOpc::lsl, MIB);
1017   }
1018   case G_GEP:
1019     I.setDesc(TII.get(Opcodes.ADDrr));
1020     MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
1021     break;
1022   case G_FRAME_INDEX:
1023     // Add 0 to the given frame index and hope it will eventually be folded into
1024     // the user(s).
1025     I.setDesc(TII.get(Opcodes.ADDri));
1026     MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
1027     break;
1028   case G_GLOBAL_VALUE:
1029     return selectGlobal(MIB, MRI);
1030   case G_STORE:
1031   case G_LOAD: {
1032     const auto &MemOp = **I.memoperands_begin();
1033     if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
1034       LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n");
1035       return false;
1036     }
1037 
1038     unsigned Reg = I.getOperand(0).getReg();
1039     unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
1040 
1041     LLT ValTy = MRI.getType(Reg);
1042     const auto ValSize = ValTy.getSizeInBits();
1043 
1044     assert((ValSize != 64 || STI.hasVFP2()) &&
1045            "Don't know how to load/store 64-bit value without VFP");
1046 
1047     const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
1048     if (NewOpc == G_LOAD || NewOpc == G_STORE)
1049       return false;
1050 
1051     if (ValSize == 1 && NewOpc == Opcodes.STORE8) {
1052       // Before storing a 1-bit value, make sure to clear out any unneeded bits.
1053       unsigned OriginalValue = I.getOperand(0).getReg();
1054 
1055       unsigned ValueToStore = MRI.createVirtualRegister(&ARM::GPRRegClass);
1056       I.getOperand(0).setReg(ValueToStore);
1057 
1058       auto InsertBefore = I.getIterator();
1059       auto AndI = BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(Opcodes.AND))
1060         .addDef(ValueToStore)
1061         .addUse(OriginalValue)
1062         .addImm(1)
1063         .add(predOps(ARMCC::AL))
1064         .add(condCodeOp());
1065       if (!constrainSelectedInstRegOperands(*AndI, TII, TRI, RBI))
1066         return false;
1067     }
1068 
1069     I.setDesc(TII.get(NewOpc));
1070 
1071     if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
1072       // LDRH has a funny addressing mode (there's already a FIXME for it).
1073       MIB.addReg(0);
1074     MIB.addImm(0).add(predOps(ARMCC::AL));
1075     break;
1076   }
1077   case G_MERGE_VALUES: {
1078     if (!selectMergeValues(MIB, TII, MRI, TRI, RBI))
1079       return false;
1080     break;
1081   }
1082   case G_UNMERGE_VALUES: {
1083     if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI))
1084       return false;
1085     break;
1086   }
1087   case G_BRCOND: {
1088     if (!validReg(MRI, I.getOperand(0).getReg(), 1, ARM::GPRRegBankID)) {
1089       LLVM_DEBUG(dbgs() << "Unsupported condition register for G_BRCOND");
1090       return false;
1091     }
1092 
1093     // Set the flags.
1094     auto Test =
1095         BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opcodes.TSTri))
1096             .addReg(I.getOperand(0).getReg())
1097             .addImm(1)
1098             .add(predOps(ARMCC::AL));
1099     if (!constrainSelectedInstRegOperands(*Test, TII, TRI, RBI))
1100       return false;
1101 
1102     // Branch conditionally.
1103     auto Branch =
1104         BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opcodes.Bcc))
1105             .add(I.getOperand(1))
1106             .add(predOps(ARMCC::NE, ARM::CPSR));
1107     if (!constrainSelectedInstRegOperands(*Branch, TII, TRI, RBI))
1108       return false;
1109     I.eraseFromParent();
1110     return true;
1111   }
1112   case G_PHI: {
1113     I.setDesc(TII.get(PHI));
1114 
1115     unsigned DstReg = I.getOperand(0).getReg();
1116     const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI);
1117     if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
1118       break;
1119     }
1120 
1121     return true;
1122   }
1123   default:
1124     return false;
1125   }
1126 
1127   return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1128 }
1129