1 //===- ARMInstructionSelector.cpp ----------------------------*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// This file implements the targeting of the InstructionSelector class for ARM.
11 /// \todo This should be generated by TableGen.
12 //===----------------------------------------------------------------------===//
14 #include "ARMRegisterBankInfo.h"
15 #include "ARMSubtarget.h"
16 #include "ARMTargetMachine.h"
17 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
18 #include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
19 #include "llvm/CodeGen/MachineConstantPool.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/Support/Debug.h"
23 #define DEBUG_TYPE "arm-isel"
29 #define GET_GLOBALISEL_PREDICATE_BITSET
30 #include "ARMGenGlobalISel.inc"
31 #undef GET_GLOBALISEL_PREDICATE_BITSET
33 class ARMInstructionSelector : public InstructionSelector {
35 ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI,
36 const ARMRegisterBankInfo &RBI);
38 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
39 static const char *getName() { return DEBUG_TYPE; }
42 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
47 bool selectCmp(CmpConstants Helper, MachineInstrBuilder &MIB,
48 MachineRegisterInfo &MRI) const;
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;
57 // Set \p DestReg to \p Constant.
58 void putConstant(InsertInfo I, unsigned DestReg, unsigned Constant) const;
60 bool selectGlobal(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
61 bool selectSelect(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
62 bool selectShift(unsigned ShiftOpc, MachineInstrBuilder &MIB) const;
64 // Check if the types match and both operands have the expected size and
66 bool validOpRegPair(MachineRegisterInfo &MRI, unsigned LHS, unsigned RHS,
67 unsigned ExpectedSize, unsigned ExpectedRegBankID) const;
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;
73 const ARMBaseInstrInfo &TII;
74 const ARMBaseRegisterInfo &TRI;
75 const ARMBaseTargetMachine &TM;
76 const ARMRegisterBankInfo &RBI;
77 const ARMSubtarget &STI;
79 // Store the opcodes that we might need, so we don't have to check what kind
80 // of subtarget (ARM vs Thumb) we have all the time.
88 // Used for implementing ZEXT/SEXT from i1
101 OpcodeCache(const ARMSubtarget &STI);
104 // Select the opcode for simple extensions (that translate to a single SXT/UXT
105 // instruction). Extension operations more complicated than that should not
106 // invoke this. Returns the original opcode if it doesn't know how to select a
108 unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) const;
110 // Select the opcode for simple loads and stores. Returns the original opcode
111 // if it doesn't know how to select a better one.
112 unsigned selectLoadStoreOpCode(unsigned Opc, unsigned RegBank,
113 unsigned Size) const;
115 #define GET_GLOBALISEL_PREDICATES_DECL
116 #include "ARMGenGlobalISel.inc"
117 #undef GET_GLOBALISEL_PREDICATES_DECL
119 // We declare the temporaries used by selectImpl() in the class to minimize the
120 // cost of constructing placeholder values.
121 #define GET_GLOBALISEL_TEMPORARIES_DECL
122 #include "ARMGenGlobalISel.inc"
123 #undef GET_GLOBALISEL_TEMPORARIES_DECL
125 } // end anonymous namespace
128 InstructionSelector *
129 createARMInstructionSelector(const ARMBaseTargetMachine &TM,
130 const ARMSubtarget &STI,
131 const ARMRegisterBankInfo &RBI) {
132 return new ARMInstructionSelector(TM, STI, RBI);
136 const unsigned zero_reg = 0;
138 #define GET_GLOBALISEL_IMPL
139 #include "ARMGenGlobalISel.inc"
140 #undef GET_GLOBALISEL_IMPL
142 ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM,
143 const ARMSubtarget &STI,
144 const ARMRegisterBankInfo &RBI)
145 : InstructionSelector(), TII(*STI.getInstrInfo()),
146 TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), Opcodes(STI),
147 #define GET_GLOBALISEL_PREDICATES_INIT
148 #include "ARMGenGlobalISel.inc"
149 #undef GET_GLOBALISEL_PREDICATES_INIT
150 #define GET_GLOBALISEL_TEMPORARIES_INIT
151 #include "ARMGenGlobalISel.inc"
152 #undef GET_GLOBALISEL_TEMPORARIES_INIT
156 static const TargetRegisterClass *guessRegClass(unsigned Reg,
157 MachineRegisterInfo &MRI,
158 const TargetRegisterInfo &TRI,
159 const RegisterBankInfo &RBI) {
160 const RegisterBank *RegBank = RBI.getRegBank(Reg, MRI, TRI);
161 assert(RegBank && "Can't get reg bank for virtual register");
163 const unsigned Size = MRI.getType(Reg).getSizeInBits();
164 assert((RegBank->getID() == ARM::GPRRegBankID ||
165 RegBank->getID() == ARM::FPRRegBankID) &&
166 "Unsupported reg bank");
168 if (RegBank->getID() == ARM::FPRRegBankID) {
170 return &ARM::SPRRegClass;
172 return &ARM::DPRRegClass;
173 else if (Size == 128)
174 return &ARM::QPRRegClass;
176 llvm_unreachable("Unsupported destination size");
179 return &ARM::GPRRegClass;
182 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
183 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
184 const RegisterBankInfo &RBI) {
185 unsigned DstReg = I.getOperand(0).getReg();
186 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
189 const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI);
191 // No need to constrain SrcReg. It will get constrained when
192 // we hit another of its uses or its defs.
193 // Copies do not have constraints.
194 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
195 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
202 static bool selectMergeValues(MachineInstrBuilder &MIB,
203 const ARMBaseInstrInfo &TII,
204 MachineRegisterInfo &MRI,
205 const TargetRegisterInfo &TRI,
206 const RegisterBankInfo &RBI) {
207 assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP");
209 // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs
211 unsigned VReg0 = MIB->getOperand(0).getReg();
213 assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
214 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
215 "Unsupported operand for G_MERGE_VALUES");
216 unsigned VReg1 = MIB->getOperand(1).getReg();
218 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
219 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
220 "Unsupported operand for G_MERGE_VALUES");
221 unsigned VReg2 = MIB->getOperand(2).getReg();
223 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
224 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
225 "Unsupported operand for G_MERGE_VALUES");
227 MIB->setDesc(TII.get(ARM::VMOVDRR));
228 MIB.add(predOps(ARMCC::AL));
233 static bool selectUnmergeValues(MachineInstrBuilder &MIB,
234 const ARMBaseInstrInfo &TII,
235 MachineRegisterInfo &MRI,
236 const TargetRegisterInfo &TRI,
237 const RegisterBankInfo &RBI) {
238 assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP");
240 // We only support G_UNMERGE_VALUES as a way to break up one DPR into two
242 unsigned VReg0 = MIB->getOperand(0).getReg();
244 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
245 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
246 "Unsupported operand for G_UNMERGE_VALUES");
247 unsigned VReg1 = MIB->getOperand(1).getReg();
249 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
250 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
251 "Unsupported operand for G_UNMERGE_VALUES");
252 unsigned VReg2 = MIB->getOperand(2).getReg();
254 assert(MRI.getType(VReg2).getSizeInBits() == 64 &&
255 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID &&
256 "Unsupported operand for G_UNMERGE_VALUES");
258 MIB->setDesc(TII.get(ARM::VMOVRRD));
259 MIB.add(predOps(ARMCC::AL));
264 ARMInstructionSelector::OpcodeCache::OpcodeCache(const ARMSubtarget &STI) {
265 bool isThumb = STI.isThumb();
267 using namespace TargetOpcode;
269 #define STORE_OPCODE(VAR, OPC) VAR = isThumb ? ARM::t2##OPC : ARM::OPC
270 STORE_OPCODE(SEXT16, SXTH);
271 STORE_OPCODE(ZEXT16, UXTH);
273 STORE_OPCODE(SEXT8, SXTB);
274 STORE_OPCODE(ZEXT8, UXTB);
276 STORE_OPCODE(AND, ANDri);
277 STORE_OPCODE(RSB, RSBri);
279 STORE_OPCODE(STORE32, STRi12);
280 STORE_OPCODE(LOAD32, LDRi12);
282 // LDRH/STRH are special...
283 STORE16 = isThumb ? ARM::t2STRHi12 : ARM::STRH;
284 LOAD16 = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
286 STORE_OPCODE(STORE8, STRBi12);
287 STORE_OPCODE(LOAD8, LDRBi12);
291 unsigned ARMInstructionSelector::selectSimpleExtOpc(unsigned Opc,
292 unsigned Size) const {
293 using namespace TargetOpcode;
295 if (Size != 8 && Size != 16)
299 return Size == 8 ? Opcodes.SEXT8 : Opcodes.SEXT16;
302 return Size == 8 ? Opcodes.ZEXT8 : Opcodes.ZEXT16;
307 unsigned ARMInstructionSelector::selectLoadStoreOpCode(unsigned Opc,
309 unsigned Size) const {
310 bool isStore = Opc == TargetOpcode::G_STORE;
312 if (RegBank == ARM::GPRRegBankID) {
316 return isStore ? Opcodes.STORE8 : Opcodes.LOAD8;
318 return isStore ? Opcodes.STORE16 : Opcodes.LOAD16;
320 return isStore ? Opcodes.STORE32 : Opcodes.LOAD32;
326 if (RegBank == ARM::FPRRegBankID) {
329 return isStore ? ARM::VSTRS : ARM::VLDRS;
331 return isStore ? ARM::VSTRD : ARM::VLDRD;
340 // When lowering comparisons, we sometimes need to perform two compares instead
341 // of just one. Get the condition codes for both comparisons. If only one is
342 // needed, the second member of the pair is ARMCC::AL.
343 static std::pair<ARMCC::CondCodes, ARMCC::CondCodes>
344 getComparePreds(CmpInst::Predicate Pred) {
345 std::pair<ARMCC::CondCodes, ARMCC::CondCodes> Preds = {ARMCC::AL, ARMCC::AL};
347 case CmpInst::FCMP_ONE:
348 Preds = {ARMCC::GT, ARMCC::MI};
350 case CmpInst::FCMP_UEQ:
351 Preds = {ARMCC::EQ, ARMCC::VS};
353 case CmpInst::ICMP_EQ:
354 case CmpInst::FCMP_OEQ:
355 Preds.first = ARMCC::EQ;
357 case CmpInst::ICMP_SGT:
358 case CmpInst::FCMP_OGT:
359 Preds.first = ARMCC::GT;
361 case CmpInst::ICMP_SGE:
362 case CmpInst::FCMP_OGE:
363 Preds.first = ARMCC::GE;
365 case CmpInst::ICMP_UGT:
366 case CmpInst::FCMP_UGT:
367 Preds.first = ARMCC::HI;
369 case CmpInst::FCMP_OLT:
370 Preds.first = ARMCC::MI;
372 case CmpInst::ICMP_ULE:
373 case CmpInst::FCMP_OLE:
374 Preds.first = ARMCC::LS;
376 case CmpInst::FCMP_ORD:
377 Preds.first = ARMCC::VC;
379 case CmpInst::FCMP_UNO:
380 Preds.first = ARMCC::VS;
382 case CmpInst::FCMP_UGE:
383 Preds.first = ARMCC::PL;
385 case CmpInst::ICMP_SLT:
386 case CmpInst::FCMP_ULT:
387 Preds.first = ARMCC::LT;
389 case CmpInst::ICMP_SLE:
390 case CmpInst::FCMP_ULE:
391 Preds.first = ARMCC::LE;
393 case CmpInst::FCMP_UNE:
394 case CmpInst::ICMP_NE:
395 Preds.first = ARMCC::NE;
397 case CmpInst::ICMP_UGE:
398 Preds.first = ARMCC::HS;
400 case CmpInst::ICMP_ULT:
401 Preds.first = ARMCC::LO;
406 assert(Preds.first != ARMCC::AL && "No comparisons needed?");
410 struct ARMInstructionSelector::CmpConstants {
411 CmpConstants(unsigned CmpOpcode, unsigned FlagsOpcode, unsigned OpRegBank,
413 : ComparisonOpcode(CmpOpcode), ReadFlagsOpcode(FlagsOpcode),
414 OperandRegBankID(OpRegBank), OperandSize(OpSize) {}
416 // The opcode used for performing the comparison.
417 const unsigned ComparisonOpcode;
419 // The opcode used for reading the flags set by the comparison. May be
420 // ARM::INSTRUCTION_LIST_END if we don't need to read the flags.
421 const unsigned ReadFlagsOpcode;
423 // The assumed register bank ID for the operands.
424 const unsigned OperandRegBankID;
426 // The assumed size in bits for the operands.
427 const unsigned OperandSize;
430 struct ARMInstructionSelector::InsertInfo {
431 InsertInfo(MachineInstrBuilder &MIB)
432 : MBB(*MIB->getParent()), InsertBefore(std::next(MIB->getIterator())),
433 DbgLoc(MIB->getDebugLoc()) {}
435 MachineBasicBlock &MBB;
436 const MachineBasicBlock::instr_iterator InsertBefore;
437 const DebugLoc &DbgLoc;
440 void ARMInstructionSelector::putConstant(InsertInfo I, unsigned DestReg,
441 unsigned Constant) const {
442 (void)BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(ARM::MOVi))
445 .add(predOps(ARMCC::AL))
449 bool ARMInstructionSelector::validOpRegPair(MachineRegisterInfo &MRI,
450 unsigned LHSReg, unsigned RHSReg,
451 unsigned ExpectedSize,
452 unsigned ExpectedRegBankID) const {
453 return MRI.getType(LHSReg) == MRI.getType(RHSReg) &&
454 validReg(MRI, LHSReg, ExpectedSize, ExpectedRegBankID) &&
455 validReg(MRI, RHSReg, ExpectedSize, ExpectedRegBankID);
458 bool ARMInstructionSelector::validReg(MachineRegisterInfo &MRI, unsigned Reg,
459 unsigned ExpectedSize,
460 unsigned ExpectedRegBankID) const {
461 if (MRI.getType(Reg).getSizeInBits() != ExpectedSize) {
462 LLVM_DEBUG(dbgs() << "Unexpected size for register");
466 if (RBI.getRegBank(Reg, MRI, TRI)->getID() != ExpectedRegBankID) {
467 LLVM_DEBUG(dbgs() << "Unexpected register bank for register");
474 bool ARMInstructionSelector::selectCmp(CmpConstants Helper,
475 MachineInstrBuilder &MIB,
476 MachineRegisterInfo &MRI) const {
477 const InsertInfo I(MIB);
479 auto ResReg = MIB->getOperand(0).getReg();
480 if (!validReg(MRI, ResReg, 1, ARM::GPRRegBankID))
484 static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate());
485 if (Cond == CmpInst::FCMP_TRUE || Cond == CmpInst::FCMP_FALSE) {
486 putConstant(I, ResReg, Cond == CmpInst::FCMP_TRUE ? 1 : 0);
487 MIB->eraseFromParent();
491 auto LHSReg = MIB->getOperand(2).getReg();
492 auto RHSReg = MIB->getOperand(3).getReg();
493 if (!validOpRegPair(MRI, LHSReg, RHSReg, Helper.OperandSize,
494 Helper.OperandRegBankID))
497 auto ARMConds = getComparePreds(Cond);
498 auto ZeroReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
499 putConstant(I, ZeroReg, 0);
501 if (ARMConds.second == ARMCC::AL) {
502 // Simple case, we only need one comparison and we're done.
503 if (!insertComparison(Helper, I, ResReg, ARMConds.first, LHSReg, RHSReg,
507 // Not so simple, we need two successive comparisons.
508 auto IntermediateRes = MRI.createVirtualRegister(&ARM::GPRRegClass);
509 if (!insertComparison(Helper, I, IntermediateRes, ARMConds.first, LHSReg,
512 if (!insertComparison(Helper, I, ResReg, ARMConds.second, LHSReg, RHSReg,
517 MIB->eraseFromParent();
521 bool ARMInstructionSelector::insertComparison(CmpConstants Helper, InsertInfo I,
523 ARMCC::CondCodes Cond,
524 unsigned LHSReg, unsigned RHSReg,
525 unsigned PrevRes) const {
526 // Perform the comparison.
528 BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Helper.ComparisonOpcode))
531 .add(predOps(ARMCC::AL));
532 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
535 // Read the comparison flags (if necessary).
536 if (Helper.ReadFlagsOpcode != ARM::INSTRUCTION_LIST_END) {
537 auto ReadI = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc,
538 TII.get(Helper.ReadFlagsOpcode))
539 .add(predOps(ARMCC::AL));
540 if (!constrainSelectedInstRegOperands(*ReadI, TII, TRI, RBI))
544 // Select either 1 or the previous result based on the value of the flags.
545 auto Mov1I = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(ARM::MOVCCi))
549 .add(predOps(Cond, ARM::CPSR));
550 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
556 bool ARMInstructionSelector::selectGlobal(MachineInstrBuilder &MIB,
557 MachineRegisterInfo &MRI) const {
558 if ((STI.isROPI() || STI.isRWPI()) && !STI.isTargetELF()) {
559 LLVM_DEBUG(dbgs() << "ROPI and RWPI only supported for ELF\n");
563 auto GV = MIB->getOperand(1).getGlobal();
564 if (GV->isThreadLocal()) {
565 LLVM_DEBUG(dbgs() << "TLS variables not supported yet\n");
569 auto &MBB = *MIB->getParent();
570 auto &MF = *MBB.getParent();
572 bool UseMovt = STI.useMovt(MF);
574 unsigned Size = TM.getPointerSize(0);
575 unsigned Alignment = 4;
577 auto addOpsForConstantPoolLoad = [&MF, Alignment,
578 Size](MachineInstrBuilder &MIB,
579 const GlobalValue *GV, bool IsSBREL) {
580 assert(MIB->getOpcode() == ARM::LDRi12 && "Unsupported instruction");
581 auto ConstPool = MF.getConstantPool();
583 // For SB relative entries we need a target-specific constant pool.
584 // Otherwise, just use a regular constant pool entry.
586 ? ConstPool->getConstantPoolIndex(
587 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL), Alignment)
588 : ConstPool->getConstantPoolIndex(GV, Alignment);
589 MIB.addConstantPoolIndex(CPIndex, /*Offset*/ 0, /*TargetFlags*/ 0)
591 MF.getMachineMemOperand(MachinePointerInfo::getConstantPool(MF),
592 MachineMemOperand::MOLoad, Size, Alignment))
594 .add(predOps(ARMCC::AL));
597 if (TM.isPositionIndependent()) {
598 bool Indirect = STI.isGVIndirectSymbol(GV);
599 // FIXME: Taking advantage of MOVT for ELF is pretty involved, so we don't
600 // support it yet. See PR28229.
602 UseMovt && !STI.isTargetELF()
603 ? (Indirect ? ARM::MOV_ga_pcrel_ldr : ARM::MOV_ga_pcrel)
604 : (Indirect ? ARM::LDRLIT_ga_pcrel_ldr : ARM::LDRLIT_ga_pcrel);
605 MIB->setDesc(TII.get(Opc));
607 int TargetFlags = ARMII::MO_NO_FLAG;
608 if (STI.isTargetDarwin())
609 TargetFlags |= ARMII::MO_NONLAZY;
610 if (STI.isGVInGOT(GV))
611 TargetFlags |= ARMII::MO_GOT;
612 MIB->getOperand(1).setTargetFlags(TargetFlags);
615 MIB.addMemOperand(MF.getMachineMemOperand(
616 MachinePointerInfo::getGOT(MF), MachineMemOperand::MOLoad,
617 TM.getProgramPointerSize(), Alignment));
619 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
622 bool isReadOnly = STI.getTargetLowering()->isReadOnly(GV);
623 if (STI.isROPI() && isReadOnly) {
624 unsigned Opc = UseMovt ? ARM::MOV_ga_pcrel : ARM::LDRLIT_ga_pcrel;
625 MIB->setDesc(TII.get(Opc));
626 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
628 if (STI.isRWPI() && !isReadOnly) {
629 auto Offset = MRI.createVirtualRegister(&ARM::GPRRegClass);
630 MachineInstrBuilder OffsetMIB;
632 OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(),
633 TII.get(ARM::MOVi32imm), Offset);
634 OffsetMIB.addGlobalAddress(GV, /*Offset*/ 0, ARMII::MO_SBREL);
636 // Load the offset from the constant pool.
638 BuildMI(MBB, *MIB, MIB->getDebugLoc(), TII.get(ARM::LDRi12), Offset);
639 addOpsForConstantPoolLoad(OffsetMIB, GV, /*IsSBREL*/ true);
641 if (!constrainSelectedInstRegOperands(*OffsetMIB, TII, TRI, RBI))
644 // Add the offset to the SB register.
645 MIB->setDesc(TII.get(ARM::ADDrr));
646 MIB->RemoveOperand(1);
647 MIB.addReg(ARM::R9) // FIXME: don't hardcode R9
649 .add(predOps(ARMCC::AL))
652 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
655 if (STI.isTargetELF()) {
657 MIB->setDesc(TII.get(ARM::MOVi32imm));
659 // Load the global's address from the constant pool.
660 MIB->setDesc(TII.get(ARM::LDRi12));
661 MIB->RemoveOperand(1);
662 addOpsForConstantPoolLoad(MIB, GV, /*IsSBREL*/ false);
664 } else if (STI.isTargetMachO()) {
666 MIB->setDesc(TII.get(ARM::MOVi32imm));
668 MIB->setDesc(TII.get(ARM::LDRLIT_ga_abs));
670 LLVM_DEBUG(dbgs() << "Object format not supported yet\n");
674 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
677 bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB,
678 MachineRegisterInfo &MRI) const {
679 auto &MBB = *MIB->getParent();
680 auto InsertBefore = std::next(MIB->getIterator());
681 auto &DbgLoc = MIB->getDebugLoc();
683 // Compare the condition to 0.
684 auto CondReg = MIB->getOperand(1).getReg();
685 assert(validReg(MRI, CondReg, 1, ARM::GPRRegBankID) &&
686 "Unsupported types for select operation");
687 auto CmpI = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::CMPri))
690 .add(predOps(ARMCC::AL));
691 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
694 // Move a value into the result register based on the result of the
696 auto ResReg = MIB->getOperand(0).getReg();
697 auto TrueReg = MIB->getOperand(2).getReg();
698 auto FalseReg = MIB->getOperand(3).getReg();
699 assert(validOpRegPair(MRI, ResReg, TrueReg, 32, ARM::GPRRegBankID) &&
700 validOpRegPair(MRI, TrueReg, FalseReg, 32, ARM::GPRRegBankID) &&
701 "Unsupported types for select operation");
702 auto Mov1I = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(ARM::MOVCCr))
706 .add(predOps(ARMCC::EQ, ARM::CPSR));
707 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
710 MIB->eraseFromParent();
714 bool ARMInstructionSelector::selectShift(unsigned ShiftOpc,
715 MachineInstrBuilder &MIB) const {
716 MIB->setDesc(TII.get(ARM::MOVsr));
717 MIB.addImm(ShiftOpc);
718 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
719 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
722 bool ARMInstructionSelector::select(MachineInstr &I,
723 CodeGenCoverage &CoverageInfo) const {
724 assert(I.getParent() && "Instruction should be in a basic block!");
725 assert(I.getParent()->getParent() && "Instruction should be in a function!");
727 auto &MBB = *I.getParent();
728 auto &MF = *MBB.getParent();
729 auto &MRI = MF.getRegInfo();
731 if (!isPreISelGenericOpcode(I.getOpcode())) {
733 return selectCopy(I, TII, MRI, TRI, RBI);
738 using namespace TargetOpcode;
740 if (selectImpl(I, CoverageInfo))
743 MachineInstrBuilder MIB{MF, I};
746 switch (I.getOpcode()) {
751 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
752 // FIXME: Smaller destination sizes coming soon!
753 if (DstTy.getSizeInBits() != 32) {
754 LLVM_DEBUG(dbgs() << "Unsupported destination size for extension");
758 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
759 unsigned SrcSize = SrcTy.getSizeInBits();
762 // ZExt boils down to & 0x1; for SExt we also subtract that from 0
763 I.setDesc(TII.get(Opcodes.AND));
764 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
767 unsigned SExtResult = I.getOperand(0).getReg();
769 // Use a new virtual register for the result of the AND
770 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
771 I.getOperand(0).setReg(AndResult);
773 auto InsertBefore = std::next(I.getIterator());
775 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(Opcodes.RSB))
779 .add(predOps(ARMCC::AL))
781 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
788 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
789 if (NewOpc == I.getOpcode())
791 I.setDesc(TII.get(NewOpc));
792 MIB.addImm(0).add(predOps(ARMCC::AL));
796 LLVM_DEBUG(dbgs() << "Unsupported source size for extension");
803 // The high bits are undefined, so there's nothing special to do, just
804 // treat it as a copy.
805 auto SrcReg = I.getOperand(1).getReg();
806 auto DstReg = I.getOperand(0).getReg();
808 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
809 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
811 if (SrcRegBank.getID() == ARM::FPRRegBankID) {
812 // This should only happen in the obscure case where we have put a 64-bit
813 // integer into a D register. Get it out of there and keep only the
815 assert(I.getOpcode() == G_TRUNC && "Unsupported operand for G_ANYEXT");
816 assert(DstRegBank.getID() == ARM::GPRRegBankID &&
817 "Unsupported combination of register banks");
818 assert(MRI.getType(SrcReg).getSizeInBits() == 64 && "Unsupported size");
819 assert(MRI.getType(DstReg).getSizeInBits() <= 32 && "Unsupported size");
821 unsigned IgnoredBits = MRI.createVirtualRegister(&ARM::GPRRegClass);
822 auto InsertBefore = std::next(I.getIterator());
824 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::VMOVRRD))
828 .add(predOps(ARMCC::AL));
829 if (!constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI))
832 MIB->eraseFromParent();
836 if (SrcRegBank.getID() != DstRegBank.getID()) {
838 dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n");
842 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
843 LLVM_DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n");
847 I.setDesc(TII.get(COPY));
848 return selectCopy(I, TII, MRI, TRI, RBI);
851 if (!MRI.getType(I.getOperand(0).getReg()).isPointer()) {
852 // Non-pointer constants should be handled by TableGen.
853 LLVM_DEBUG(dbgs() << "Unsupported constant type\n");
857 auto &Val = I.getOperand(1);
859 if (!Val.getCImm()->isZero()) {
860 LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n");
863 Val.ChangeToImmediate(0);
865 assert(Val.isImm() && "Unexpected operand for G_CONSTANT");
866 if (Val.getImm() != 0) {
867 LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n");
872 I.setDesc(TII.get(ARM::MOVi));
873 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
878 auto SrcReg = I.getOperand(1).getReg();
879 auto DstReg = I.getOperand(0).getReg();
881 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
882 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
884 if (SrcRegBank.getID() != DstRegBank.getID()) {
887 << "G_INTTOPTR/G_PTRTOINT operands on different register banks\n");
891 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
893 dbgs() << "G_INTTOPTR/G_PTRTOINT on non-GPR not supported yet\n");
897 I.setDesc(TII.get(COPY));
898 return selectCopy(I, TII, MRI, TRI, RBI);
901 return selectSelect(MIB, MRI);
903 CmpConstants Helper(ARM::CMPrr, ARM::INSTRUCTION_LIST_END,
904 ARM::GPRRegBankID, 32);
905 return selectCmp(Helper, MIB, MRI);
908 assert(STI.hasVFP2() && "Can't select fcmp without VFP");
910 unsigned OpReg = I.getOperand(2).getReg();
911 unsigned Size = MRI.getType(OpReg).getSizeInBits();
913 if (Size == 64 && STI.isFPOnlySP()) {
914 LLVM_DEBUG(dbgs() << "Subtarget only supports single precision");
917 if (Size != 32 && Size != 64) {
918 LLVM_DEBUG(dbgs() << "Unsupported size for G_FCMP operand");
922 CmpConstants Helper(Size == 32 ? ARM::VCMPS : ARM::VCMPD, ARM::FMSTAT,
923 ARM::FPRRegBankID, Size);
924 return selectCmp(Helper, MIB, MRI);
927 return selectShift(ARM_AM::ShiftOpc::lsr, MIB);
929 return selectShift(ARM_AM::ShiftOpc::asr, MIB);
931 return selectShift(ARM_AM::ShiftOpc::lsl, MIB);
934 I.setDesc(TII.get(ARM::ADDrr));
935 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
938 // Add 0 to the given frame index and hope it will eventually be folded into
940 I.setDesc(TII.get(ARM::ADDri));
941 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
944 return selectGlobal(MIB, MRI);
947 const auto &MemOp = **I.memoperands_begin();
948 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
949 LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n");
953 unsigned Reg = I.getOperand(0).getReg();
954 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
956 LLT ValTy = MRI.getType(Reg);
957 const auto ValSize = ValTy.getSizeInBits();
959 assert((ValSize != 64 || STI.hasVFP2()) &&
960 "Don't know how to load/store 64-bit value without VFP");
962 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
963 if (NewOpc == G_LOAD || NewOpc == G_STORE)
966 I.setDesc(TII.get(NewOpc));
968 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
969 // LDRH has a funny addressing mode (there's already a FIXME for it).
971 MIB.addImm(0).add(predOps(ARMCC::AL));
974 case G_MERGE_VALUES: {
975 if (!selectMergeValues(MIB, TII, MRI, TRI, RBI))
979 case G_UNMERGE_VALUES: {
980 if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI))
985 if (!validReg(MRI, I.getOperand(0).getReg(), 1, ARM::GPRRegBankID)) {
986 LLVM_DEBUG(dbgs() << "Unsupported condition register for G_BRCOND");
991 auto Test = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(ARM::TSTri))
992 .addReg(I.getOperand(0).getReg())
994 .add(predOps(ARMCC::AL));
995 if (!constrainSelectedInstRegOperands(*Test, TII, TRI, RBI))
998 // Branch conditionally.
999 auto Branch = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(ARM::Bcc))
1000 .add(I.getOperand(1))
1001 .add(predOps(ARMCC::NE, ARM::CPSR));
1002 if (!constrainSelectedInstRegOperands(*Branch, TII, TRI, RBI))
1004 I.eraseFromParent();
1008 I.setDesc(TII.get(PHI));
1010 unsigned DstReg = I.getOperand(0).getReg();
1011 const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI);
1012 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
1022 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);