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