1 //===- ARMInstructionSelector.cpp ----------------------------*- C++ -*-==//
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
7 //===----------------------------------------------------------------------===//
9 /// This file implements the targeting of the InstructionSelector class for ARM.
10 /// \todo This should be generated by TableGen.
11 //===----------------------------------------------------------------------===//
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"
22 #define DEBUG_TYPE "arm-isel"
28 #define GET_GLOBALISEL_PREDICATE_BITSET
29 #include "ARMGenGlobalISel.inc"
30 #undef GET_GLOBALISEL_PREDICATE_BITSET
32 class ARMInstructionSelector : public InstructionSelector {
34 ARMInstructionSelector(const ARMBaseTargetMachine &TM, const ARMSubtarget &STI,
35 const ARMRegisterBankInfo &RBI);
37 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
38 static const char *getName() { return DEBUG_TYPE; }
41 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
46 bool selectCmp(CmpConstants Helper, MachineInstrBuilder &MIB,
47 MachineRegisterInfo &MRI) const;
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;
56 // Set \p DestReg to \p Constant.
57 void putConstant(InsertInfo I, unsigned DestReg, unsigned Constant) const;
59 bool selectGlobal(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
60 bool selectSelect(MachineInstrBuilder &MIB, MachineRegisterInfo &MRI) const;
61 bool selectShift(unsigned ShiftOpc, MachineInstrBuilder &MIB) const;
63 // Check if the types match and both operands have the expected size and
65 bool validOpRegPair(MachineRegisterInfo &MRI, unsigned LHS, unsigned RHS,
66 unsigned ExpectedSize, unsigned ExpectedRegBankID) const;
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;
72 const ARMBaseInstrInfo &TII;
73 const ARMBaseRegisterInfo &TRI;
74 const ARMBaseTargetMachine &TM;
75 const ARMRegisterBankInfo &RBI;
76 const ARMSubtarget &STI;
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;
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.
92 // Used for implementing ZEXT/SEXT from i1
120 // Used for G_GLOBAL_VALUE
122 unsigned ConstPoolLoad;
123 unsigned MOV_ga_pcrel;
124 unsigned LDRLIT_ga_pcrel;
125 unsigned LDRLIT_ga_abs;
127 OpcodeCache(const ARMSubtarget &STI);
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
134 unsigned selectSimpleExtOpc(unsigned Opc, unsigned Size) const;
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;
141 #define GET_GLOBALISEL_PREDICATES_DECL
142 #include "ARMGenGlobalISel.inc"
143 #undef GET_GLOBALISEL_PREDICATES_DECL
145 // We declare the temporaries used by selectImpl() in the class to minimize the
146 // cost of constructing placeholder values.
147 #define GET_GLOBALISEL_TEMPORARIES_DECL
148 #include "ARMGenGlobalISel.inc"
149 #undef GET_GLOBALISEL_TEMPORARIES_DECL
151 } // end anonymous namespace
154 InstructionSelector *
155 createARMInstructionSelector(const ARMBaseTargetMachine &TM,
156 const ARMSubtarget &STI,
157 const ARMRegisterBankInfo &RBI) {
158 return new ARMInstructionSelector(TM, STI, RBI);
162 const unsigned zero_reg = 0;
164 #define GET_GLOBALISEL_IMPL
165 #include "ARMGenGlobalISel.inc"
166 #undef GET_GLOBALISEL_IMPL
168 ARMInstructionSelector::ARMInstructionSelector(const ARMBaseTargetMachine &TM,
169 const ARMSubtarget &STI,
170 const ARMRegisterBankInfo &RBI)
171 : InstructionSelector(), TII(*STI.getInstrInfo()),
172 TRI(*STI.getRegisterInfo()), TM(TM), RBI(RBI), STI(STI), Opcodes(STI),
173 #define GET_GLOBALISEL_PREDICATES_INIT
174 #include "ARMGenGlobalISel.inc"
175 #undef GET_GLOBALISEL_PREDICATES_INIT
176 #define GET_GLOBALISEL_TEMPORARIES_INIT
177 #include "ARMGenGlobalISel.inc"
178 #undef GET_GLOBALISEL_TEMPORARIES_INIT
182 static const TargetRegisterClass *guessRegClass(unsigned Reg,
183 MachineRegisterInfo &MRI,
184 const TargetRegisterInfo &TRI,
185 const RegisterBankInfo &RBI) {
186 const RegisterBank *RegBank = RBI.getRegBank(Reg, MRI, TRI);
187 assert(RegBank && "Can't get reg bank for virtual register");
189 const unsigned Size = MRI.getType(Reg).getSizeInBits();
190 assert((RegBank->getID() == ARM::GPRRegBankID ||
191 RegBank->getID() == ARM::FPRRegBankID) &&
192 "Unsupported reg bank");
194 if (RegBank->getID() == ARM::FPRRegBankID) {
196 return &ARM::SPRRegClass;
198 return &ARM::DPRRegClass;
199 else if (Size == 128)
200 return &ARM::QPRRegClass;
202 llvm_unreachable("Unsupported destination size");
205 return &ARM::GPRRegClass;
208 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
209 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
210 const RegisterBankInfo &RBI) {
211 unsigned DstReg = I.getOperand(0).getReg();
212 if (TargetRegisterInfo::isPhysicalRegister(DstReg))
215 const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI);
217 // No need to constrain SrcReg. It will get constrained when
218 // we hit another of its uses or its defs.
219 // Copies do not have constraints.
220 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
221 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
228 static bool selectMergeValues(MachineInstrBuilder &MIB,
229 const ARMBaseInstrInfo &TII,
230 MachineRegisterInfo &MRI,
231 const TargetRegisterInfo &TRI,
232 const RegisterBankInfo &RBI) {
233 assert(TII.getSubtarget().hasVFP2() && "Can't select merge without VFP");
235 // We only support G_MERGE_VALUES as a way to stick together two scalar GPRs
237 unsigned VReg0 = MIB->getOperand(0).getReg();
239 assert(MRI.getType(VReg0).getSizeInBits() == 64 &&
240 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::FPRRegBankID &&
241 "Unsupported operand for G_MERGE_VALUES");
242 unsigned VReg1 = MIB->getOperand(1).getReg();
244 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
245 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
246 "Unsupported operand for G_MERGE_VALUES");
247 unsigned VReg2 = MIB->getOperand(2).getReg();
249 assert(MRI.getType(VReg2).getSizeInBits() == 32 &&
250 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::GPRRegBankID &&
251 "Unsupported operand for G_MERGE_VALUES");
253 MIB->setDesc(TII.get(ARM::VMOVDRR));
254 MIB.add(predOps(ARMCC::AL));
259 static bool selectUnmergeValues(MachineInstrBuilder &MIB,
260 const ARMBaseInstrInfo &TII,
261 MachineRegisterInfo &MRI,
262 const TargetRegisterInfo &TRI,
263 const RegisterBankInfo &RBI) {
264 assert(TII.getSubtarget().hasVFP2() && "Can't select unmerge without VFP");
266 // We only support G_UNMERGE_VALUES as a way to break up one DPR into two
268 unsigned VReg0 = MIB->getOperand(0).getReg();
270 assert(MRI.getType(VReg0).getSizeInBits() == 32 &&
271 RBI.getRegBank(VReg0, MRI, TRI)->getID() == ARM::GPRRegBankID &&
272 "Unsupported operand for G_UNMERGE_VALUES");
273 unsigned VReg1 = MIB->getOperand(1).getReg();
275 assert(MRI.getType(VReg1).getSizeInBits() == 32 &&
276 RBI.getRegBank(VReg1, MRI, TRI)->getID() == ARM::GPRRegBankID &&
277 "Unsupported operand for G_UNMERGE_VALUES");
278 unsigned VReg2 = MIB->getOperand(2).getReg();
280 assert(MRI.getType(VReg2).getSizeInBits() == 64 &&
281 RBI.getRegBank(VReg2, MRI, TRI)->getID() == ARM::FPRRegBankID &&
282 "Unsupported operand for G_UNMERGE_VALUES");
284 MIB->setDesc(TII.get(ARM::VMOVRRD));
285 MIB.add(predOps(ARMCC::AL));
290 ARMInstructionSelector::OpcodeCache::OpcodeCache(const ARMSubtarget &STI) {
291 bool isThumb = STI.isThumb();
293 using namespace TargetOpcode;
295 #define STORE_OPCODE(VAR, OPC) VAR = isThumb ? ARM::t2##OPC : ARM::OPC
296 STORE_OPCODE(SEXT16, SXTH);
297 STORE_OPCODE(ZEXT16, UXTH);
299 STORE_OPCODE(SEXT8, SXTB);
300 STORE_OPCODE(ZEXT8, UXTB);
302 STORE_OPCODE(AND, ANDri);
303 STORE_OPCODE(RSB, RSBri);
305 STORE_OPCODE(STORE32, STRi12);
306 STORE_OPCODE(LOAD32, LDRi12);
308 // LDRH/STRH are special...
309 STORE16 = isThumb ? ARM::t2STRHi12 : ARM::STRH;
310 LOAD16 = isThumb ? ARM::t2LDRHi12 : ARM::LDRH;
312 STORE_OPCODE(STORE8, STRBi12);
313 STORE_OPCODE(LOAD8, LDRBi12);
315 STORE_OPCODE(ADDrr, ADDrr);
316 STORE_OPCODE(ADDri, ADDri);
318 STORE_OPCODE(CMPrr, CMPrr);
319 STORE_OPCODE(MOVi, MOVi);
320 STORE_OPCODE(MOVCCi, MOVCCi);
322 STORE_OPCODE(CMPri, CMPri);
323 STORE_OPCODE(MOVCCr, MOVCCr);
325 STORE_OPCODE(TSTri, TSTri);
326 STORE_OPCODE(Bcc, Bcc);
328 STORE_OPCODE(MOVi32imm, MOVi32imm);
329 ConstPoolLoad = isThumb ? ARM::t2LDRpci : ARM::LDRi12;
330 STORE_OPCODE(MOV_ga_pcrel, MOV_ga_pcrel);
331 LDRLIT_ga_pcrel = isThumb ? ARM::tLDRLIT_ga_pcrel : ARM::LDRLIT_ga_pcrel;
332 LDRLIT_ga_abs = isThumb ? ARM::tLDRLIT_ga_abs : ARM::LDRLIT_ga_abs;
336 unsigned ARMInstructionSelector::selectSimpleExtOpc(unsigned Opc,
337 unsigned Size) const {
338 using namespace TargetOpcode;
340 if (Size != 8 && Size != 16)
344 return Size == 8 ? Opcodes.SEXT8 : Opcodes.SEXT16;
347 return Size == 8 ? Opcodes.ZEXT8 : Opcodes.ZEXT16;
352 unsigned ARMInstructionSelector::selectLoadStoreOpCode(unsigned Opc,
354 unsigned Size) const {
355 bool isStore = Opc == TargetOpcode::G_STORE;
357 if (RegBank == ARM::GPRRegBankID) {
361 return isStore ? Opcodes.STORE8 : Opcodes.LOAD8;
363 return isStore ? Opcodes.STORE16 : Opcodes.LOAD16;
365 return isStore ? Opcodes.STORE32 : Opcodes.LOAD32;
371 if (RegBank == ARM::FPRRegBankID) {
374 return isStore ? ARM::VSTRS : ARM::VLDRS;
376 return isStore ? ARM::VSTRD : ARM::VLDRD;
385 // When lowering comparisons, we sometimes need to perform two compares instead
386 // of just one. Get the condition codes for both comparisons. If only one is
387 // needed, the second member of the pair is ARMCC::AL.
388 static std::pair<ARMCC::CondCodes, ARMCC::CondCodes>
389 getComparePreds(CmpInst::Predicate Pred) {
390 std::pair<ARMCC::CondCodes, ARMCC::CondCodes> Preds = {ARMCC::AL, ARMCC::AL};
392 case CmpInst::FCMP_ONE:
393 Preds = {ARMCC::GT, ARMCC::MI};
395 case CmpInst::FCMP_UEQ:
396 Preds = {ARMCC::EQ, ARMCC::VS};
398 case CmpInst::ICMP_EQ:
399 case CmpInst::FCMP_OEQ:
400 Preds.first = ARMCC::EQ;
402 case CmpInst::ICMP_SGT:
403 case CmpInst::FCMP_OGT:
404 Preds.first = ARMCC::GT;
406 case CmpInst::ICMP_SGE:
407 case CmpInst::FCMP_OGE:
408 Preds.first = ARMCC::GE;
410 case CmpInst::ICMP_UGT:
411 case CmpInst::FCMP_UGT:
412 Preds.first = ARMCC::HI;
414 case CmpInst::FCMP_OLT:
415 Preds.first = ARMCC::MI;
417 case CmpInst::ICMP_ULE:
418 case CmpInst::FCMP_OLE:
419 Preds.first = ARMCC::LS;
421 case CmpInst::FCMP_ORD:
422 Preds.first = ARMCC::VC;
424 case CmpInst::FCMP_UNO:
425 Preds.first = ARMCC::VS;
427 case CmpInst::FCMP_UGE:
428 Preds.first = ARMCC::PL;
430 case CmpInst::ICMP_SLT:
431 case CmpInst::FCMP_ULT:
432 Preds.first = ARMCC::LT;
434 case CmpInst::ICMP_SLE:
435 case CmpInst::FCMP_ULE:
436 Preds.first = ARMCC::LE;
438 case CmpInst::FCMP_UNE:
439 case CmpInst::ICMP_NE:
440 Preds.first = ARMCC::NE;
442 case CmpInst::ICMP_UGE:
443 Preds.first = ARMCC::HS;
445 case CmpInst::ICMP_ULT:
446 Preds.first = ARMCC::LO;
451 assert(Preds.first != ARMCC::AL && "No comparisons needed?");
455 struct ARMInstructionSelector::CmpConstants {
456 CmpConstants(unsigned CmpOpcode, unsigned FlagsOpcode, unsigned SelectOpcode,
457 unsigned OpRegBank, unsigned OpSize)
458 : ComparisonOpcode(CmpOpcode), ReadFlagsOpcode(FlagsOpcode),
459 SelectResultOpcode(SelectOpcode), OperandRegBankID(OpRegBank),
460 OperandSize(OpSize) {}
462 // The opcode used for performing the comparison.
463 const unsigned ComparisonOpcode;
465 // The opcode used for reading the flags set by the comparison. May be
466 // ARM::INSTRUCTION_LIST_END if we don't need to read the flags.
467 const unsigned ReadFlagsOpcode;
469 // The opcode used for materializing the result of the comparison.
470 const unsigned SelectResultOpcode;
472 // The assumed register bank ID for the operands.
473 const unsigned OperandRegBankID;
475 // The assumed size in bits for the operands.
476 const unsigned OperandSize;
479 struct ARMInstructionSelector::InsertInfo {
480 InsertInfo(MachineInstrBuilder &MIB)
481 : MBB(*MIB->getParent()), InsertBefore(std::next(MIB->getIterator())),
482 DbgLoc(MIB->getDebugLoc()) {}
484 MachineBasicBlock &MBB;
485 const MachineBasicBlock::instr_iterator InsertBefore;
486 const DebugLoc &DbgLoc;
489 void ARMInstructionSelector::putConstant(InsertInfo I, unsigned DestReg,
490 unsigned Constant) const {
491 (void)BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Opcodes.MOVi))
494 .add(predOps(ARMCC::AL))
498 bool ARMInstructionSelector::validOpRegPair(MachineRegisterInfo &MRI,
499 unsigned LHSReg, unsigned RHSReg,
500 unsigned ExpectedSize,
501 unsigned ExpectedRegBankID) const {
502 return MRI.getType(LHSReg) == MRI.getType(RHSReg) &&
503 validReg(MRI, LHSReg, ExpectedSize, ExpectedRegBankID) &&
504 validReg(MRI, RHSReg, ExpectedSize, ExpectedRegBankID);
507 bool ARMInstructionSelector::validReg(MachineRegisterInfo &MRI, unsigned Reg,
508 unsigned ExpectedSize,
509 unsigned ExpectedRegBankID) const {
510 if (MRI.getType(Reg).getSizeInBits() != ExpectedSize) {
511 LLVM_DEBUG(dbgs() << "Unexpected size for register");
515 if (RBI.getRegBank(Reg, MRI, TRI)->getID() != ExpectedRegBankID) {
516 LLVM_DEBUG(dbgs() << "Unexpected register bank for register");
523 bool ARMInstructionSelector::selectCmp(CmpConstants Helper,
524 MachineInstrBuilder &MIB,
525 MachineRegisterInfo &MRI) const {
526 const InsertInfo I(MIB);
528 auto ResReg = MIB->getOperand(0).getReg();
529 if (!validReg(MRI, ResReg, 1, ARM::GPRRegBankID))
533 static_cast<CmpInst::Predicate>(MIB->getOperand(1).getPredicate());
534 if (Cond == CmpInst::FCMP_TRUE || Cond == CmpInst::FCMP_FALSE) {
535 putConstant(I, ResReg, Cond == CmpInst::FCMP_TRUE ? 1 : 0);
536 MIB->eraseFromParent();
540 auto LHSReg = MIB->getOperand(2).getReg();
541 auto RHSReg = MIB->getOperand(3).getReg();
542 if (!validOpRegPair(MRI, LHSReg, RHSReg, Helper.OperandSize,
543 Helper.OperandRegBankID))
546 auto ARMConds = getComparePreds(Cond);
547 auto ZeroReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
548 putConstant(I, ZeroReg, 0);
550 if (ARMConds.second == ARMCC::AL) {
551 // Simple case, we only need one comparison and we're done.
552 if (!insertComparison(Helper, I, ResReg, ARMConds.first, LHSReg, RHSReg,
556 // Not so simple, we need two successive comparisons.
557 auto IntermediateRes = MRI.createVirtualRegister(&ARM::GPRRegClass);
558 if (!insertComparison(Helper, I, IntermediateRes, ARMConds.first, LHSReg,
561 if (!insertComparison(Helper, I, ResReg, ARMConds.second, LHSReg, RHSReg,
566 MIB->eraseFromParent();
570 bool ARMInstructionSelector::insertComparison(CmpConstants Helper, InsertInfo I,
572 ARMCC::CondCodes Cond,
573 unsigned LHSReg, unsigned RHSReg,
574 unsigned PrevRes) const {
575 // Perform the comparison.
577 BuildMI(I.MBB, I.InsertBefore, I.DbgLoc, TII.get(Helper.ComparisonOpcode))
580 .add(predOps(ARMCC::AL));
581 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
584 // Read the comparison flags (if necessary).
585 if (Helper.ReadFlagsOpcode != ARM::INSTRUCTION_LIST_END) {
586 auto ReadI = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc,
587 TII.get(Helper.ReadFlagsOpcode))
588 .add(predOps(ARMCC::AL));
589 if (!constrainSelectedInstRegOperands(*ReadI, TII, TRI, RBI))
593 // Select either 1 or the previous result based on the value of the flags.
594 auto Mov1I = BuildMI(I.MBB, I.InsertBefore, I.DbgLoc,
595 TII.get(Helper.SelectResultOpcode))
599 .add(predOps(Cond, ARM::CPSR));
600 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
606 bool ARMInstructionSelector::selectGlobal(MachineInstrBuilder &MIB,
607 MachineRegisterInfo &MRI) const {
608 if ((STI.isROPI() || STI.isRWPI()) && !STI.isTargetELF()) {
609 LLVM_DEBUG(dbgs() << "ROPI and RWPI only supported for ELF\n");
613 auto GV = MIB->getOperand(1).getGlobal();
614 if (GV->isThreadLocal()) {
615 LLVM_DEBUG(dbgs() << "TLS variables not supported yet\n");
619 auto &MBB = *MIB->getParent();
620 auto &MF = *MBB.getParent();
622 bool UseMovt = STI.useMovt();
624 unsigned Size = TM.getPointerSize(0);
625 unsigned Alignment = 4;
627 auto addOpsForConstantPoolLoad = [&MF, Alignment,
628 Size](MachineInstrBuilder &MIB,
629 const GlobalValue *GV, bool IsSBREL) {
630 assert((MIB->getOpcode() == ARM::LDRi12 ||
631 MIB->getOpcode() == ARM::t2LDRpci) &&
632 "Unsupported instruction");
633 auto ConstPool = MF.getConstantPool();
635 // For SB relative entries we need a target-specific constant pool.
636 // Otherwise, just use a regular constant pool entry.
638 ? ConstPool->getConstantPoolIndex(
639 ARMConstantPoolConstant::Create(GV, ARMCP::SBREL), Alignment)
640 : ConstPool->getConstantPoolIndex(GV, Alignment);
641 MIB.addConstantPoolIndex(CPIndex, /*Offset*/ 0, /*TargetFlags*/ 0)
642 .addMemOperand(MF.getMachineMemOperand(
643 MachinePointerInfo::getConstantPool(MF), MachineMemOperand::MOLoad,
645 if (MIB->getOpcode() == ARM::LDRi12)
647 MIB.add(predOps(ARMCC::AL));
650 auto addGOTMemOperand = [this, &MF, Alignment](MachineInstrBuilder &MIB) {
651 MIB.addMemOperand(MF.getMachineMemOperand(
652 MachinePointerInfo::getGOT(MF), MachineMemOperand::MOLoad,
653 TM.getProgramPointerSize(), Alignment));
656 if (TM.isPositionIndependent()) {
657 bool Indirect = STI.isGVIndirectSymbol(GV);
659 // For ARM mode, we have different pseudoinstructions for direct accesses
660 // and indirect accesses, and the ones for indirect accesses include the
661 // load from GOT. For Thumb mode, we use the same pseudoinstruction for both
662 // direct and indirect accesses, and we need to manually generate the load
664 bool UseOpcodeThatLoads = Indirect && !STI.isThumb();
666 // FIXME: Taking advantage of MOVT for ELF is pretty involved, so we don't
667 // support it yet. See PR28229.
669 UseMovt && !STI.isTargetELF()
670 ? (UseOpcodeThatLoads ? (unsigned)ARM::MOV_ga_pcrel_ldr
671 : Opcodes.MOV_ga_pcrel)
672 : (UseOpcodeThatLoads ? (unsigned)ARM::LDRLIT_ga_pcrel_ldr
673 : Opcodes.LDRLIT_ga_pcrel);
674 MIB->setDesc(TII.get(Opc));
676 int TargetFlags = ARMII::MO_NO_FLAG;
677 if (STI.isTargetDarwin())
678 TargetFlags |= ARMII::MO_NONLAZY;
679 if (STI.isGVInGOT(GV))
680 TargetFlags |= ARMII::MO_GOT;
681 MIB->getOperand(1).setTargetFlags(TargetFlags);
684 if (!UseOpcodeThatLoads) {
685 auto ResultReg = MIB->getOperand(0).getReg();
686 auto AddressReg = MRI.createVirtualRegister(&ARM::GPRRegClass);
688 MIB->getOperand(0).setReg(AddressReg);
690 auto InsertBefore = std::next(MIB->getIterator());
691 auto MIBLoad = BuildMI(MBB, InsertBefore, MIB->getDebugLoc(),
692 TII.get(Opcodes.LOAD32))
696 .add(predOps(ARMCC::AL));
697 addGOTMemOperand(MIBLoad);
699 if (!constrainSelectedInstRegOperands(*MIBLoad, TII, TRI, RBI))
702 addGOTMemOperand(MIB);
706 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
709 bool isReadOnly = STI.getTargetLowering()->isReadOnly(GV);
710 if (STI.isROPI() && isReadOnly) {
711 unsigned Opc = UseMovt ? Opcodes.MOV_ga_pcrel : Opcodes.LDRLIT_ga_pcrel;
712 MIB->setDesc(TII.get(Opc));
713 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
715 if (STI.isRWPI() && !isReadOnly) {
716 auto Offset = MRI.createVirtualRegister(&ARM::GPRRegClass);
717 MachineInstrBuilder OffsetMIB;
719 OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(),
720 TII.get(Opcodes.MOVi32imm), Offset);
721 OffsetMIB.addGlobalAddress(GV, /*Offset*/ 0, ARMII::MO_SBREL);
723 // Load the offset from the constant pool.
724 OffsetMIB = BuildMI(MBB, *MIB, MIB->getDebugLoc(),
725 TII.get(Opcodes.ConstPoolLoad), Offset);
726 addOpsForConstantPoolLoad(OffsetMIB, GV, /*IsSBREL*/ true);
728 if (!constrainSelectedInstRegOperands(*OffsetMIB, TII, TRI, RBI))
731 // Add the offset to the SB register.
732 MIB->setDesc(TII.get(Opcodes.ADDrr));
733 MIB->RemoveOperand(1);
734 MIB.addReg(ARM::R9) // FIXME: don't hardcode R9
736 .add(predOps(ARMCC::AL))
739 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
742 if (STI.isTargetELF()) {
744 MIB->setDesc(TII.get(Opcodes.MOVi32imm));
746 // Load the global's address from the constant pool.
747 MIB->setDesc(TII.get(Opcodes.ConstPoolLoad));
748 MIB->RemoveOperand(1);
749 addOpsForConstantPoolLoad(MIB, GV, /*IsSBREL*/ false);
751 } else if (STI.isTargetMachO()) {
753 MIB->setDesc(TII.get(Opcodes.MOVi32imm));
755 MIB->setDesc(TII.get(Opcodes.LDRLIT_ga_abs));
757 LLVM_DEBUG(dbgs() << "Object format not supported yet\n");
761 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
764 bool ARMInstructionSelector::selectSelect(MachineInstrBuilder &MIB,
765 MachineRegisterInfo &MRI) const {
766 auto &MBB = *MIB->getParent();
767 auto InsertBefore = std::next(MIB->getIterator());
768 auto &DbgLoc = MIB->getDebugLoc();
770 // Compare the condition to 0.
771 auto CondReg = MIB->getOperand(1).getReg();
772 assert(validReg(MRI, CondReg, 1, ARM::GPRRegBankID) &&
773 "Unsupported types for select operation");
774 auto CmpI = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(Opcodes.CMPri))
777 .add(predOps(ARMCC::AL));
778 if (!constrainSelectedInstRegOperands(*CmpI, TII, TRI, RBI))
781 // Move a value into the result register based on the result of the
783 auto ResReg = MIB->getOperand(0).getReg();
784 auto TrueReg = MIB->getOperand(2).getReg();
785 auto FalseReg = MIB->getOperand(3).getReg();
786 assert(validOpRegPair(MRI, ResReg, TrueReg, 32, ARM::GPRRegBankID) &&
787 validOpRegPair(MRI, TrueReg, FalseReg, 32, ARM::GPRRegBankID) &&
788 "Unsupported types for select operation");
789 auto Mov1I = BuildMI(MBB, InsertBefore, DbgLoc, TII.get(Opcodes.MOVCCr))
793 .add(predOps(ARMCC::EQ, ARM::CPSR));
794 if (!constrainSelectedInstRegOperands(*Mov1I, TII, TRI, RBI))
797 MIB->eraseFromParent();
801 bool ARMInstructionSelector::selectShift(unsigned ShiftOpc,
802 MachineInstrBuilder &MIB) const {
803 MIB->setDesc(TII.get(ARM::MOVsr));
804 MIB.addImm(ShiftOpc);
805 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
806 return constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
809 bool ARMInstructionSelector::select(MachineInstr &I,
810 CodeGenCoverage &CoverageInfo) const {
811 assert(I.getParent() && "Instruction should be in a basic block!");
812 assert(I.getParent()->getParent() && "Instruction should be in a function!");
814 auto &MBB = *I.getParent();
815 auto &MF = *MBB.getParent();
816 auto &MRI = MF.getRegInfo();
818 if (!isPreISelGenericOpcode(I.getOpcode())) {
820 return selectCopy(I, TII, MRI, TRI, RBI);
825 using namespace TargetOpcode;
827 if (selectImpl(I, CoverageInfo))
830 MachineInstrBuilder MIB{MF, I};
833 switch (I.getOpcode()) {
838 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
839 // FIXME: Smaller destination sizes coming soon!
840 if (DstTy.getSizeInBits() != 32) {
841 LLVM_DEBUG(dbgs() << "Unsupported destination size for extension");
845 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
846 unsigned SrcSize = SrcTy.getSizeInBits();
849 // ZExt boils down to & 0x1; for SExt we also subtract that from 0
850 I.setDesc(TII.get(Opcodes.AND));
851 MIB.addImm(1).add(predOps(ARMCC::AL)).add(condCodeOp());
854 unsigned SExtResult = I.getOperand(0).getReg();
856 // Use a new virtual register for the result of the AND
857 unsigned AndResult = MRI.createVirtualRegister(&ARM::GPRRegClass);
858 I.getOperand(0).setReg(AndResult);
860 auto InsertBefore = std::next(I.getIterator());
862 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(Opcodes.RSB))
866 .add(predOps(ARMCC::AL))
868 if (!constrainSelectedInstRegOperands(*SubI, TII, TRI, RBI))
875 unsigned NewOpc = selectSimpleExtOpc(I.getOpcode(), SrcSize);
876 if (NewOpc == I.getOpcode())
878 I.setDesc(TII.get(NewOpc));
879 MIB.addImm(0).add(predOps(ARMCC::AL));
883 LLVM_DEBUG(dbgs() << "Unsupported source size for extension");
890 // The high bits are undefined, so there's nothing special to do, just
891 // treat it as a copy.
892 auto SrcReg = I.getOperand(1).getReg();
893 auto DstReg = I.getOperand(0).getReg();
895 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
896 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
898 if (SrcRegBank.getID() == ARM::FPRRegBankID) {
899 // This should only happen in the obscure case where we have put a 64-bit
900 // integer into a D register. Get it out of there and keep only the
902 assert(I.getOpcode() == G_TRUNC && "Unsupported operand for G_ANYEXT");
903 assert(DstRegBank.getID() == ARM::GPRRegBankID &&
904 "Unsupported combination of register banks");
905 assert(MRI.getType(SrcReg).getSizeInBits() == 64 && "Unsupported size");
906 assert(MRI.getType(DstReg).getSizeInBits() <= 32 && "Unsupported size");
908 unsigned IgnoredBits = MRI.createVirtualRegister(&ARM::GPRRegClass);
909 auto InsertBefore = std::next(I.getIterator());
911 BuildMI(MBB, InsertBefore, I.getDebugLoc(), TII.get(ARM::VMOVRRD))
915 .add(predOps(ARMCC::AL));
916 if (!constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI))
919 MIB->eraseFromParent();
923 if (SrcRegBank.getID() != DstRegBank.getID()) {
925 dbgs() << "G_TRUNC/G_ANYEXT operands on different register banks\n");
929 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
930 LLVM_DEBUG(dbgs() << "G_TRUNC/G_ANYEXT on non-GPR not supported yet\n");
934 I.setDesc(TII.get(COPY));
935 return selectCopy(I, TII, MRI, TRI, RBI);
938 if (!MRI.getType(I.getOperand(0).getReg()).isPointer()) {
939 // Non-pointer constants should be handled by TableGen.
940 LLVM_DEBUG(dbgs() << "Unsupported constant type\n");
944 auto &Val = I.getOperand(1);
946 if (!Val.getCImm()->isZero()) {
947 LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n");
950 Val.ChangeToImmediate(0);
952 assert(Val.isImm() && "Unexpected operand for G_CONSTANT");
953 if (Val.getImm() != 0) {
954 LLVM_DEBUG(dbgs() << "Unsupported pointer constant value\n");
959 I.setDesc(TII.get(ARM::MOVi));
960 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
965 auto SrcReg = I.getOperand(1).getReg();
966 auto DstReg = I.getOperand(0).getReg();
968 const auto &SrcRegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
969 const auto &DstRegBank = *RBI.getRegBank(DstReg, MRI, TRI);
971 if (SrcRegBank.getID() != DstRegBank.getID()) {
974 << "G_INTTOPTR/G_PTRTOINT operands on different register banks\n");
978 if (SrcRegBank.getID() != ARM::GPRRegBankID) {
980 dbgs() << "G_INTTOPTR/G_PTRTOINT on non-GPR not supported yet\n");
984 I.setDesc(TII.get(COPY));
985 return selectCopy(I, TII, MRI, TRI, RBI);
988 return selectSelect(MIB, MRI);
990 CmpConstants Helper(Opcodes.CMPrr, ARM::INSTRUCTION_LIST_END,
991 Opcodes.MOVCCi, ARM::GPRRegBankID, 32);
992 return selectCmp(Helper, MIB, MRI);
995 assert(STI.hasVFP2() && "Can't select fcmp without VFP");
997 unsigned OpReg = I.getOperand(2).getReg();
998 unsigned Size = MRI.getType(OpReg).getSizeInBits();
1000 if (Size == 64 && STI.isFPOnlySP()) {
1001 LLVM_DEBUG(dbgs() << "Subtarget only supports single precision");
1004 if (Size != 32 && Size != 64) {
1005 LLVM_DEBUG(dbgs() << "Unsupported size for G_FCMP operand");
1009 CmpConstants Helper(Size == 32 ? ARM::VCMPS : ARM::VCMPD, ARM::FMSTAT,
1010 Opcodes.MOVCCi, ARM::FPRRegBankID, Size);
1011 return selectCmp(Helper, MIB, MRI);
1014 return selectShift(ARM_AM::ShiftOpc::lsr, MIB);
1016 return selectShift(ARM_AM::ShiftOpc::asr, MIB);
1018 return selectShift(ARM_AM::ShiftOpc::lsl, MIB);
1021 I.setDesc(TII.get(Opcodes.ADDrr));
1022 MIB.add(predOps(ARMCC::AL)).add(condCodeOp());
1025 // Add 0 to the given frame index and hope it will eventually be folded into
1027 I.setDesc(TII.get(Opcodes.ADDri));
1028 MIB.addImm(0).add(predOps(ARMCC::AL)).add(condCodeOp());
1030 case G_GLOBAL_VALUE:
1031 return selectGlobal(MIB, MRI);
1034 const auto &MemOp = **I.memoperands_begin();
1035 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
1036 LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n");
1040 unsigned Reg = I.getOperand(0).getReg();
1041 unsigned RegBank = RBI.getRegBank(Reg, MRI, TRI)->getID();
1043 LLT ValTy = MRI.getType(Reg);
1044 const auto ValSize = ValTy.getSizeInBits();
1046 assert((ValSize != 64 || STI.hasVFP2()) &&
1047 "Don't know how to load/store 64-bit value without VFP");
1049 const auto NewOpc = selectLoadStoreOpCode(I.getOpcode(), RegBank, ValSize);
1050 if (NewOpc == G_LOAD || NewOpc == G_STORE)
1053 I.setDesc(TII.get(NewOpc));
1055 if (NewOpc == ARM::LDRH || NewOpc == ARM::STRH)
1056 // LDRH has a funny addressing mode (there's already a FIXME for it).
1058 MIB.addImm(0).add(predOps(ARMCC::AL));
1061 case G_MERGE_VALUES: {
1062 if (!selectMergeValues(MIB, TII, MRI, TRI, RBI))
1066 case G_UNMERGE_VALUES: {
1067 if (!selectUnmergeValues(MIB, TII, MRI, TRI, RBI))
1072 if (!validReg(MRI, I.getOperand(0).getReg(), 1, ARM::GPRRegBankID)) {
1073 LLVM_DEBUG(dbgs() << "Unsupported condition register for G_BRCOND");
1079 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opcodes.TSTri))
1080 .addReg(I.getOperand(0).getReg())
1082 .add(predOps(ARMCC::AL));
1083 if (!constrainSelectedInstRegOperands(*Test, TII, TRI, RBI))
1086 // Branch conditionally.
1088 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opcodes.Bcc))
1089 .add(I.getOperand(1))
1090 .add(predOps(ARMCC::NE, ARM::CPSR));
1091 if (!constrainSelectedInstRegOperands(*Branch, TII, TRI, RBI))
1093 I.eraseFromParent();
1097 I.setDesc(TII.get(PHI));
1099 unsigned DstReg = I.getOperand(0).getReg();
1100 const TargetRegisterClass *RC = guessRegClass(DstReg, MRI, TRI, RBI);
1101 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
1111 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);