1 //===- AArch64InstructionSelector.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
12 /// \todo This should be generated by TableGen.
13 //===----------------------------------------------------------------------===//
15 #include "AArch64InstrInfo.h"
16 #include "AArch64MachineFunctionInfo.h"
17 #include "AArch64RegisterBankInfo.h"
18 #include "AArch64RegisterInfo.h"
19 #include "AArch64Subtarget.h"
20 #include "AArch64TargetMachine.h"
21 #include "MCTargetDesc/AArch64AddressingModes.h"
22 #include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
23 #include "llvm/CodeGen/GlobalISel/InstructionSelectorImpl.h"
24 #include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
25 #include "llvm/CodeGen/GlobalISel/Utils.h"
26 #include "llvm/CodeGen/MachineBasicBlock.h"
27 #include "llvm/CodeGen/MachineFunction.h"
28 #include "llvm/CodeGen/MachineInstr.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/CodeGen/MachineOperand.h"
31 #include "llvm/CodeGen/MachineRegisterInfo.h"
32 #include "llvm/IR/Type.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/raw_ostream.h"
36 #define DEBUG_TYPE "aarch64-isel"
42 #define GET_GLOBALISEL_PREDICATE_BITSET
43 #include "AArch64GenGlobalISel.inc"
44 #undef GET_GLOBALISEL_PREDICATE_BITSET
46 class AArch64InstructionSelector : public InstructionSelector {
48 AArch64InstructionSelector(const AArch64TargetMachine &TM,
49 const AArch64Subtarget &STI,
50 const AArch64RegisterBankInfo &RBI);
52 bool select(MachineInstr &I, CodeGenCoverage &CoverageInfo) const override;
53 static const char *getName() { return DEBUG_TYPE; }
56 /// tblgen-erated 'select' implementation, used as the initial selector for
57 /// the patterns that don't require complex C++.
58 bool selectImpl(MachineInstr &I, CodeGenCoverage &CoverageInfo) const;
60 bool selectVaStartAAPCS(MachineInstr &I, MachineFunction &MF,
61 MachineRegisterInfo &MRI) const;
62 bool selectVaStartDarwin(MachineInstr &I, MachineFunction &MF,
63 MachineRegisterInfo &MRI) const;
65 bool selectCompareBranch(MachineInstr &I, MachineFunction &MF,
66 MachineRegisterInfo &MRI) const;
68 // Helper to generate an equivalent of scalar_to_vector into a new register,
69 // returned via 'Dst'.
70 bool emitScalarToVector(unsigned &Dst, const LLT DstTy,
71 const TargetRegisterClass *DstRC, unsigned Scalar,
72 MachineBasicBlock &MBB,
73 MachineBasicBlock::iterator MBBI,
74 MachineRegisterInfo &MRI) const;
75 bool selectBuildVector(MachineInstr &I, MachineRegisterInfo &MRI) const;
76 bool selectMergeValues(MachineInstr &I, MachineRegisterInfo &MRI) const;
78 ComplexRendererFns selectArithImmed(MachineOperand &Root) const;
80 ComplexRendererFns selectAddrModeUnscaled(MachineOperand &Root,
83 ComplexRendererFns selectAddrModeUnscaled8(MachineOperand &Root) const {
84 return selectAddrModeUnscaled(Root, 1);
86 ComplexRendererFns selectAddrModeUnscaled16(MachineOperand &Root) const {
87 return selectAddrModeUnscaled(Root, 2);
89 ComplexRendererFns selectAddrModeUnscaled32(MachineOperand &Root) const {
90 return selectAddrModeUnscaled(Root, 4);
92 ComplexRendererFns selectAddrModeUnscaled64(MachineOperand &Root) const {
93 return selectAddrModeUnscaled(Root, 8);
95 ComplexRendererFns selectAddrModeUnscaled128(MachineOperand &Root) const {
96 return selectAddrModeUnscaled(Root, 16);
99 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root,
100 unsigned Size) const;
102 ComplexRendererFns selectAddrModeIndexed(MachineOperand &Root) const {
103 return selectAddrModeIndexed(Root, Width / 8);
106 void renderTruncImm(MachineInstrBuilder &MIB, const MachineInstr &MI) const;
108 // Materialize a GlobalValue or BlockAddress using a movz+movk sequence.
109 void materializeLargeCMVal(MachineInstr &I, const Value *V,
110 unsigned char OpFlags) const;
112 const AArch64TargetMachine &TM;
113 const AArch64Subtarget &STI;
114 const AArch64InstrInfo &TII;
115 const AArch64RegisterInfo &TRI;
116 const AArch64RegisterBankInfo &RBI;
118 #define GET_GLOBALISEL_PREDICATES_DECL
119 #include "AArch64GenGlobalISel.inc"
120 #undef GET_GLOBALISEL_PREDICATES_DECL
122 // We declare the temporaries used by selectImpl() in the class to minimize the
123 // cost of constructing placeholder values.
124 #define GET_GLOBALISEL_TEMPORARIES_DECL
125 #include "AArch64GenGlobalISel.inc"
126 #undef GET_GLOBALISEL_TEMPORARIES_DECL
129 } // end anonymous namespace
131 #define GET_GLOBALISEL_IMPL
132 #include "AArch64GenGlobalISel.inc"
133 #undef GET_GLOBALISEL_IMPL
135 AArch64InstructionSelector::AArch64InstructionSelector(
136 const AArch64TargetMachine &TM, const AArch64Subtarget &STI,
137 const AArch64RegisterBankInfo &RBI)
138 : InstructionSelector(), TM(TM), STI(STI), TII(*STI.getInstrInfo()),
139 TRI(*STI.getRegisterInfo()), RBI(RBI),
140 #define GET_GLOBALISEL_PREDICATES_INIT
141 #include "AArch64GenGlobalISel.inc"
142 #undef GET_GLOBALISEL_PREDICATES_INIT
143 #define GET_GLOBALISEL_TEMPORARIES_INIT
144 #include "AArch64GenGlobalISel.inc"
145 #undef GET_GLOBALISEL_TEMPORARIES_INIT
149 // FIXME: This should be target-independent, inferred from the types declared
150 // for each class in the bank.
151 static const TargetRegisterClass *
152 getRegClassForTypeOnBank(LLT Ty, const RegisterBank &RB,
153 const RegisterBankInfo &RBI,
154 bool GetAllRegSet = false) {
155 if (RB.getID() == AArch64::GPRRegBankID) {
156 if (Ty.getSizeInBits() <= 32)
157 return GetAllRegSet ? &AArch64::GPR32allRegClass
158 : &AArch64::GPR32RegClass;
159 if (Ty.getSizeInBits() == 64)
160 return GetAllRegSet ? &AArch64::GPR64allRegClass
161 : &AArch64::GPR64RegClass;
165 if (RB.getID() == AArch64::FPRRegBankID) {
166 if (Ty.getSizeInBits() <= 16)
167 return &AArch64::FPR16RegClass;
168 if (Ty.getSizeInBits() == 32)
169 return &AArch64::FPR32RegClass;
170 if (Ty.getSizeInBits() == 64)
171 return &AArch64::FPR64RegClass;
172 if (Ty.getSizeInBits() == 128)
173 return &AArch64::FPR128RegClass;
180 /// Check whether \p I is a currently unsupported binary operation:
181 /// - it has an unsized type
182 /// - an operand is not a vreg
183 /// - all operands are not in the same bank
184 /// These are checks that should someday live in the verifier, but right now,
185 /// these are mostly limitations of the aarch64 selector.
186 static bool unsupportedBinOp(const MachineInstr &I,
187 const AArch64RegisterBankInfo &RBI,
188 const MachineRegisterInfo &MRI,
189 const AArch64RegisterInfo &TRI) {
190 LLT Ty = MRI.getType(I.getOperand(0).getReg());
192 LLVM_DEBUG(dbgs() << "Generic binop register should be typed\n");
196 const RegisterBank *PrevOpBank = nullptr;
197 for (auto &MO : I.operands()) {
198 // FIXME: Support non-register operands.
200 LLVM_DEBUG(dbgs() << "Generic inst non-reg operands are unsupported\n");
204 // FIXME: Can generic operations have physical registers operands? If
205 // so, this will need to be taught about that, and we'll need to get the
206 // bank out of the minimal class for the register.
207 // Either way, this needs to be documented (and possibly verified).
208 if (!TargetRegisterInfo::isVirtualRegister(MO.getReg())) {
209 LLVM_DEBUG(dbgs() << "Generic inst has physical register operand\n");
213 const RegisterBank *OpBank = RBI.getRegBank(MO.getReg(), MRI, TRI);
215 LLVM_DEBUG(dbgs() << "Generic register has no bank or class\n");
219 if (PrevOpBank && OpBank != PrevOpBank) {
220 LLVM_DEBUG(dbgs() << "Generic inst operands have different banks\n");
228 /// Select the AArch64 opcode for the basic binary operation \p GenericOpc
229 /// (such as G_OR or G_SDIV), appropriate for the register bank \p RegBankID
230 /// and of size \p OpSize.
231 /// \returns \p GenericOpc if the combination is unsupported.
232 static unsigned selectBinaryOp(unsigned GenericOpc, unsigned RegBankID,
235 case AArch64::GPRRegBankID:
237 switch (GenericOpc) {
238 case TargetOpcode::G_SHL:
239 return AArch64::LSLVWr;
240 case TargetOpcode::G_LSHR:
241 return AArch64::LSRVWr;
242 case TargetOpcode::G_ASHR:
243 return AArch64::ASRVWr;
247 } else if (OpSize == 64) {
248 switch (GenericOpc) {
249 case TargetOpcode::G_GEP:
250 return AArch64::ADDXrr;
251 case TargetOpcode::G_SHL:
252 return AArch64::LSLVXr;
253 case TargetOpcode::G_LSHR:
254 return AArch64::LSRVXr;
255 case TargetOpcode::G_ASHR:
256 return AArch64::ASRVXr;
262 case AArch64::FPRRegBankID:
265 switch (GenericOpc) {
266 case TargetOpcode::G_FADD:
267 return AArch64::FADDSrr;
268 case TargetOpcode::G_FSUB:
269 return AArch64::FSUBSrr;
270 case TargetOpcode::G_FMUL:
271 return AArch64::FMULSrr;
272 case TargetOpcode::G_FDIV:
273 return AArch64::FDIVSrr;
278 switch (GenericOpc) {
279 case TargetOpcode::G_FADD:
280 return AArch64::FADDDrr;
281 case TargetOpcode::G_FSUB:
282 return AArch64::FSUBDrr;
283 case TargetOpcode::G_FMUL:
284 return AArch64::FMULDrr;
285 case TargetOpcode::G_FDIV:
286 return AArch64::FDIVDrr;
287 case TargetOpcode::G_OR:
288 return AArch64::ORRv8i8;
298 /// Select the AArch64 opcode for the G_LOAD or G_STORE operation \p GenericOpc,
299 /// appropriate for the (value) register bank \p RegBankID and of memory access
300 /// size \p OpSize. This returns the variant with the base+unsigned-immediate
301 /// addressing mode (e.g., LDRXui).
302 /// \returns \p GenericOpc if the combination is unsupported.
303 static unsigned selectLoadStoreUIOp(unsigned GenericOpc, unsigned RegBankID,
305 const bool isStore = GenericOpc == TargetOpcode::G_STORE;
307 case AArch64::GPRRegBankID:
310 return isStore ? AArch64::STRBBui : AArch64::LDRBBui;
312 return isStore ? AArch64::STRHHui : AArch64::LDRHHui;
314 return isStore ? AArch64::STRWui : AArch64::LDRWui;
316 return isStore ? AArch64::STRXui : AArch64::LDRXui;
319 case AArch64::FPRRegBankID:
322 return isStore ? AArch64::STRBui : AArch64::LDRBui;
324 return isStore ? AArch64::STRHui : AArch64::LDRHui;
326 return isStore ? AArch64::STRSui : AArch64::LDRSui;
328 return isStore ? AArch64::STRDui : AArch64::LDRDui;
335 static bool selectFP16CopyFromGPR32(MachineInstr &I, const TargetInstrInfo &TII,
336 MachineRegisterInfo &MRI, unsigned SrcReg) {
337 // Copies from gpr32 to fpr16 need to use a sub-register copy.
338 unsigned CopyReg = MRI.createVirtualRegister(&AArch64::FPR32RegClass);
339 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::COPY))
342 unsigned SubRegCopy = MRI.createVirtualRegister(&AArch64::FPR16RegClass);
343 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(TargetOpcode::COPY))
345 .addUse(CopyReg, 0, AArch64::hsub);
347 MachineOperand &RegOp = I.getOperand(1);
348 RegOp.setReg(SubRegCopy);
352 static bool selectCopy(MachineInstr &I, const TargetInstrInfo &TII,
353 MachineRegisterInfo &MRI, const TargetRegisterInfo &TRI,
354 const RegisterBankInfo &RBI) {
356 unsigned DstReg = I.getOperand(0).getReg();
357 unsigned SrcReg = I.getOperand(1).getReg();
359 if (TargetRegisterInfo::isPhysicalRegister(DstReg)) {
360 if (TRI.getRegClass(AArch64::FPR16RegClassID)->contains(DstReg) &&
361 !TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
362 const RegisterBank &RegBank = *RBI.getRegBank(SrcReg, MRI, TRI);
363 const TargetRegisterClass *SrcRC = getRegClassForTypeOnBank(
364 MRI.getType(SrcReg), RegBank, RBI, /* GetAllRegSet */ true);
365 if (SrcRC == &AArch64::GPR32allRegClass)
366 return selectFP16CopyFromGPR32(I, TII, MRI, SrcReg);
368 assert(I.isCopy() && "Generic operators do not allow physical registers");
372 const RegisterBank &RegBank = *RBI.getRegBank(DstReg, MRI, TRI);
373 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
375 const unsigned SrcSize = RBI.getSizeInBits(SrcReg, MRI, TRI);
377 assert((!TargetRegisterInfo::isPhysicalRegister(SrcReg) || I.isCopy()) &&
378 "No phys reg on generic operators");
380 (DstSize == SrcSize ||
381 // Copies are a mean to setup initial types, the number of
382 // bits may not exactly match.
383 (TargetRegisterInfo::isPhysicalRegister(SrcReg) &&
384 DstSize <= RBI.getSizeInBits(SrcReg, MRI, TRI)) ||
385 // Copies are a mean to copy bits around, as long as we are
386 // on the same register class, that's fine. Otherwise, that
387 // means we need some SUBREG_TO_REG or AND & co.
388 (((DstSize + 31) / 32 == (SrcSize + 31) / 32) && DstSize > SrcSize)) &&
389 "Copy with different width?!");
390 assert((DstSize <= 64 || RegBank.getID() == AArch64::FPRRegBankID) &&
391 "GPRs cannot get more than 64-bit width values");
393 const TargetRegisterClass *RC = getRegClassForTypeOnBank(
394 MRI.getType(DstReg), RegBank, RBI, /* GetAllRegSet */ true);
396 LLVM_DEBUG(dbgs() << "Unexpected bitcast size " << DstSize << '\n');
400 if (!TargetRegisterInfo::isPhysicalRegister(SrcReg)) {
401 const RegClassOrRegBank &RegClassOrBank = MRI.getRegClassOrRegBank(SrcReg);
402 const TargetRegisterClass *SrcRC =
403 RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
404 const RegisterBank *RB = nullptr;
406 RB = RegClassOrBank.get<const RegisterBank *>();
407 SrcRC = getRegClassForTypeOnBank(MRI.getType(SrcReg), *RB, RBI, true);
409 // Copies from fpr16 to gpr32 need to use SUBREG_TO_REG.
410 if (RC == &AArch64::GPR32allRegClass && SrcRC == &AArch64::FPR16RegClass) {
411 unsigned PromoteReg = MRI.createVirtualRegister(&AArch64::FPR32RegClass);
412 BuildMI(*I.getParent(), I, I.getDebugLoc(),
413 TII.get(AArch64::SUBREG_TO_REG))
417 .addImm(AArch64::hsub);
418 MachineOperand &RegOp = I.getOperand(1);
419 RegOp.setReg(PromoteReg);
420 } else if (RC == &AArch64::FPR16RegClass &&
421 SrcRC == &AArch64::GPR32allRegClass) {
422 selectFP16CopyFromGPR32(I, TII, MRI, SrcReg);
426 // No need to constrain SrcReg. It will get constrained when
427 // we hit another of its use or its defs.
428 // Copies do not have constraints.
429 if (!RBI.constrainGenericRegister(DstReg, *RC, MRI)) {
430 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(I.getOpcode())
434 I.setDesc(TII.get(AArch64::COPY));
438 static unsigned selectFPConvOpc(unsigned GenericOpc, LLT DstTy, LLT SrcTy) {
439 if (!DstTy.isScalar() || !SrcTy.isScalar())
442 const unsigned DstSize = DstTy.getSizeInBits();
443 const unsigned SrcSize = SrcTy.getSizeInBits();
449 switch (GenericOpc) {
450 case TargetOpcode::G_SITOFP:
451 return AArch64::SCVTFUWSri;
452 case TargetOpcode::G_UITOFP:
453 return AArch64::UCVTFUWSri;
454 case TargetOpcode::G_FPTOSI:
455 return AArch64::FCVTZSUWSr;
456 case TargetOpcode::G_FPTOUI:
457 return AArch64::FCVTZUUWSr;
462 switch (GenericOpc) {
463 case TargetOpcode::G_SITOFP:
464 return AArch64::SCVTFUXSri;
465 case TargetOpcode::G_UITOFP:
466 return AArch64::UCVTFUXSri;
467 case TargetOpcode::G_FPTOSI:
468 return AArch64::FCVTZSUWDr;
469 case TargetOpcode::G_FPTOUI:
470 return AArch64::FCVTZUUWDr;
480 switch (GenericOpc) {
481 case TargetOpcode::G_SITOFP:
482 return AArch64::SCVTFUWDri;
483 case TargetOpcode::G_UITOFP:
484 return AArch64::UCVTFUWDri;
485 case TargetOpcode::G_FPTOSI:
486 return AArch64::FCVTZSUXSr;
487 case TargetOpcode::G_FPTOUI:
488 return AArch64::FCVTZUUXSr;
493 switch (GenericOpc) {
494 case TargetOpcode::G_SITOFP:
495 return AArch64::SCVTFUXDri;
496 case TargetOpcode::G_UITOFP:
497 return AArch64::UCVTFUXDri;
498 case TargetOpcode::G_FPTOSI:
499 return AArch64::FCVTZSUXDr;
500 case TargetOpcode::G_FPTOUI:
501 return AArch64::FCVTZUUXDr;
514 static AArch64CC::CondCode changeICMPPredToAArch64CC(CmpInst::Predicate P) {
517 llvm_unreachable("Unknown condition code!");
518 case CmpInst::ICMP_NE:
519 return AArch64CC::NE;
520 case CmpInst::ICMP_EQ:
521 return AArch64CC::EQ;
522 case CmpInst::ICMP_SGT:
523 return AArch64CC::GT;
524 case CmpInst::ICMP_SGE:
525 return AArch64CC::GE;
526 case CmpInst::ICMP_SLT:
527 return AArch64CC::LT;
528 case CmpInst::ICMP_SLE:
529 return AArch64CC::LE;
530 case CmpInst::ICMP_UGT:
531 return AArch64CC::HI;
532 case CmpInst::ICMP_UGE:
533 return AArch64CC::HS;
534 case CmpInst::ICMP_ULT:
535 return AArch64CC::LO;
536 case CmpInst::ICMP_ULE:
537 return AArch64CC::LS;
541 static void changeFCMPPredToAArch64CC(CmpInst::Predicate P,
542 AArch64CC::CondCode &CondCode,
543 AArch64CC::CondCode &CondCode2) {
544 CondCode2 = AArch64CC::AL;
547 llvm_unreachable("Unknown FP condition!");
548 case CmpInst::FCMP_OEQ:
549 CondCode = AArch64CC::EQ;
551 case CmpInst::FCMP_OGT:
552 CondCode = AArch64CC::GT;
554 case CmpInst::FCMP_OGE:
555 CondCode = AArch64CC::GE;
557 case CmpInst::FCMP_OLT:
558 CondCode = AArch64CC::MI;
560 case CmpInst::FCMP_OLE:
561 CondCode = AArch64CC::LS;
563 case CmpInst::FCMP_ONE:
564 CondCode = AArch64CC::MI;
565 CondCode2 = AArch64CC::GT;
567 case CmpInst::FCMP_ORD:
568 CondCode = AArch64CC::VC;
570 case CmpInst::FCMP_UNO:
571 CondCode = AArch64CC::VS;
573 case CmpInst::FCMP_UEQ:
574 CondCode = AArch64CC::EQ;
575 CondCode2 = AArch64CC::VS;
577 case CmpInst::FCMP_UGT:
578 CondCode = AArch64CC::HI;
580 case CmpInst::FCMP_UGE:
581 CondCode = AArch64CC::PL;
583 case CmpInst::FCMP_ULT:
584 CondCode = AArch64CC::LT;
586 case CmpInst::FCMP_ULE:
587 CondCode = AArch64CC::LE;
589 case CmpInst::FCMP_UNE:
590 CondCode = AArch64CC::NE;
595 bool AArch64InstructionSelector::selectCompareBranch(
596 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
598 const unsigned CondReg = I.getOperand(0).getReg();
599 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
600 MachineInstr *CCMI = MRI.getVRegDef(CondReg);
601 if (CCMI->getOpcode() == TargetOpcode::G_TRUNC)
602 CCMI = MRI.getVRegDef(CCMI->getOperand(1).getReg());
603 if (CCMI->getOpcode() != TargetOpcode::G_ICMP)
606 unsigned LHS = CCMI->getOperand(2).getReg();
607 unsigned RHS = CCMI->getOperand(3).getReg();
608 if (!getConstantVRegVal(RHS, MRI))
611 const auto RHSImm = getConstantVRegVal(RHS, MRI);
612 if (!RHSImm || *RHSImm != 0)
615 const RegisterBank &RB = *RBI.getRegBank(LHS, MRI, TRI);
616 if (RB.getID() != AArch64::GPRRegBankID)
619 const auto Pred = (CmpInst::Predicate)CCMI->getOperand(1).getPredicate();
620 if (Pred != CmpInst::ICMP_NE && Pred != CmpInst::ICMP_EQ)
623 const unsigned CmpWidth = MRI.getType(LHS).getSizeInBits();
626 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZW : AArch64::CBNZW);
627 else if (CmpWidth == 64)
628 CBOpc = (Pred == CmpInst::ICMP_EQ ? AArch64::CBZX : AArch64::CBNZX);
632 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(CBOpc))
635 .constrainAllUses(TII, TRI, RBI);
641 bool AArch64InstructionSelector::selectVaStartAAPCS(
642 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
646 bool AArch64InstructionSelector::selectVaStartDarwin(
647 MachineInstr &I, MachineFunction &MF, MachineRegisterInfo &MRI) const {
648 AArch64FunctionInfo *FuncInfo = MF.getInfo<AArch64FunctionInfo>();
649 unsigned ListReg = I.getOperand(0).getReg();
651 unsigned ArgsAddrReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
654 BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::ADDXri))
656 .addFrameIndex(FuncInfo->getVarArgsStackIndex())
660 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
662 MIB = BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::STRXui))
666 .addMemOperand(*I.memoperands_begin());
668 constrainSelectedInstRegOperands(*MIB, TII, TRI, RBI);
673 void AArch64InstructionSelector::materializeLargeCMVal(
674 MachineInstr &I, const Value *V, unsigned char OpFlags) const {
675 MachineBasicBlock &MBB = *I.getParent();
676 MachineFunction &MF = *MBB.getParent();
677 MachineRegisterInfo &MRI = MF.getRegInfo();
678 MachineIRBuilder MIB(I);
680 auto MovZ = MIB.buildInstr(AArch64::MOVZXi, {&AArch64::GPR64RegClass}, {});
681 MovZ->addOperand(MF, I.getOperand(1));
682 MovZ->getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_G0 |
684 MovZ->addOperand(MF, MachineOperand::CreateImm(0));
685 constrainSelectedInstRegOperands(*MovZ, TII, TRI, RBI);
687 auto BuildMovK = [&](unsigned SrcReg, unsigned char Flags, unsigned Offset,
688 unsigned ForceDstReg) {
689 unsigned DstReg = ForceDstReg
691 : MRI.createVirtualRegister(&AArch64::GPR64RegClass);
692 auto MovI = MIB.buildInstr(AArch64::MOVKXi).addDef(DstReg).addUse(SrcReg);
693 if (auto *GV = dyn_cast<GlobalValue>(V)) {
694 MovI->addOperand(MF, MachineOperand::CreateGA(
695 GV, MovZ->getOperand(1).getOffset(), Flags));
698 MF, MachineOperand::CreateBA(cast<BlockAddress>(V),
699 MovZ->getOperand(1).getOffset(), Flags));
701 MovI->addOperand(MF, MachineOperand::CreateImm(Offset));
702 constrainSelectedInstRegOperands(*MovI, TII, TRI, RBI);
705 unsigned DstReg = BuildMovK(MovZ->getOperand(0).getReg(),
706 AArch64II::MO_G1 | AArch64II::MO_NC, 16, 0);
707 DstReg = BuildMovK(DstReg, AArch64II::MO_G2 | AArch64II::MO_NC, 32, 0);
708 BuildMovK(DstReg, AArch64II::MO_G3, 48, I.getOperand(0).getReg());
712 bool AArch64InstructionSelector::select(MachineInstr &I,
713 CodeGenCoverage &CoverageInfo) const {
714 assert(I.getParent() && "Instruction should be in a basic block!");
715 assert(I.getParent()->getParent() && "Instruction should be in a function!");
717 MachineBasicBlock &MBB = *I.getParent();
718 MachineFunction &MF = *MBB.getParent();
719 MachineRegisterInfo &MRI = MF.getRegInfo();
721 unsigned Opcode = I.getOpcode();
722 // G_PHI requires same handling as PHI
723 if (!isPreISelGenericOpcode(Opcode) || Opcode == TargetOpcode::G_PHI) {
724 // Certain non-generic instructions also need some special handling.
726 if (Opcode == TargetOpcode::LOAD_STACK_GUARD)
727 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
729 if (Opcode == TargetOpcode::PHI || Opcode == TargetOpcode::G_PHI) {
730 const unsigned DefReg = I.getOperand(0).getReg();
731 const LLT DefTy = MRI.getType(DefReg);
733 const TargetRegisterClass *DefRC = nullptr;
734 if (TargetRegisterInfo::isPhysicalRegister(DefReg)) {
735 DefRC = TRI.getRegClass(DefReg);
737 const RegClassOrRegBank &RegClassOrBank =
738 MRI.getRegClassOrRegBank(DefReg);
740 DefRC = RegClassOrBank.dyn_cast<const TargetRegisterClass *>();
742 if (!DefTy.isValid()) {
743 LLVM_DEBUG(dbgs() << "PHI operand has no type, not a gvreg?\n");
746 const RegisterBank &RB = *RegClassOrBank.get<const RegisterBank *>();
747 DefRC = getRegClassForTypeOnBank(DefTy, RB, RBI);
749 LLVM_DEBUG(dbgs() << "PHI operand has unexpected size/bank\n");
754 I.setDesc(TII.get(TargetOpcode::PHI));
756 return RBI.constrainGenericRegister(DefReg, *DefRC, MRI);
760 return selectCopy(I, TII, MRI, TRI, RBI);
766 if (I.getNumOperands() != I.getNumExplicitOperands()) {
768 dbgs() << "Generic instruction has unexpected implicit operands\n");
772 if (selectImpl(I, CoverageInfo))
776 I.getOperand(0).isReg() ? MRI.getType(I.getOperand(0).getReg()) : LLT{};
779 case TargetOpcode::G_BRCOND: {
780 if (Ty.getSizeInBits() > 32) {
781 // We shouldn't need this on AArch64, but it would be implemented as an
782 // EXTRACT_SUBREG followed by a TBNZW because TBNZX has no encoding if the
783 // bit being tested is < 32.
784 LLVM_DEBUG(dbgs() << "G_BRCOND has type: " << Ty
785 << ", expected at most 32-bits");
789 const unsigned CondReg = I.getOperand(0).getReg();
790 MachineBasicBlock *DestMBB = I.getOperand(1).getMBB();
792 // Speculation tracking/SLH assumes that optimized TB(N)Z/CB(N)Z
793 // instructions will not be produced, as they are conditional branch
794 // instructions that do not set flags.
795 bool ProduceNonFlagSettingCondBr =
796 !MF.getFunction().hasFnAttribute(Attribute::SpeculativeLoadHardening);
797 if (ProduceNonFlagSettingCondBr && selectCompareBranch(I, MF, MRI))
800 if (ProduceNonFlagSettingCondBr) {
801 auto MIB = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::TBNZW))
803 .addImm(/*bit offset=*/0)
807 return constrainSelectedInstRegOperands(*MIB.getInstr(), TII, TRI, RBI);
809 auto CMP = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
810 .addDef(AArch64::WZR)
813 constrainSelectedInstRegOperands(*CMP.getInstr(), TII, TRI, RBI);
815 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::Bcc))
816 .addImm(AArch64CC::EQ)
820 return constrainSelectedInstRegOperands(*Bcc.getInstr(), TII, TRI, RBI);
824 case TargetOpcode::G_BRINDIRECT: {
825 I.setDesc(TII.get(AArch64::BR));
826 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
829 case TargetOpcode::G_FCONSTANT:
830 case TargetOpcode::G_CONSTANT: {
831 const bool isFP = Opcode == TargetOpcode::G_FCONSTANT;
833 const LLT s32 = LLT::scalar(32);
834 const LLT s64 = LLT::scalar(64);
835 const LLT p0 = LLT::pointer(0, 64);
837 const unsigned DefReg = I.getOperand(0).getReg();
838 const LLT DefTy = MRI.getType(DefReg);
839 const unsigned DefSize = DefTy.getSizeInBits();
840 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
842 // FIXME: Redundant check, but even less readable when factored out.
844 if (Ty != s32 && Ty != s64) {
845 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
846 << " constant, expected: " << s32 << " or " << s64
851 if (RB.getID() != AArch64::FPRRegBankID) {
852 LLVM_DEBUG(dbgs() << "Unable to materialize FP " << Ty
853 << " constant on bank: " << RB
854 << ", expected: FPR\n");
858 // The case when we have 0.0 is covered by tablegen. Reject it here so we
859 // can be sure tablegen works correctly and isn't rescued by this code.
860 if (I.getOperand(1).getFPImm()->getValueAPF().isExactlyValue(0.0))
863 // s32 and s64 are covered by tablegen.
865 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
866 << " constant, expected: " << s32 << ", " << s64
867 << ", or " << p0 << '\n');
871 if (RB.getID() != AArch64::GPRRegBankID) {
872 LLVM_DEBUG(dbgs() << "Unable to materialize integer " << Ty
873 << " constant on bank: " << RB
874 << ", expected: GPR\n");
879 const unsigned MovOpc =
880 DefSize == 32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
882 I.setDesc(TII.get(MovOpc));
885 const TargetRegisterClass &GPRRC =
886 DefSize == 32 ? AArch64::GPR32RegClass : AArch64::GPR64RegClass;
887 const TargetRegisterClass &FPRRC =
888 DefSize == 32 ? AArch64::FPR32RegClass : AArch64::FPR64RegClass;
890 const unsigned DefGPRReg = MRI.createVirtualRegister(&GPRRC);
891 MachineOperand &RegOp = I.getOperand(0);
892 RegOp.setReg(DefGPRReg);
894 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
895 TII.get(AArch64::COPY))
899 if (!RBI.constrainGenericRegister(DefReg, FPRRC, MRI)) {
900 LLVM_DEBUG(dbgs() << "Failed to constrain G_FCONSTANT def operand\n");
904 MachineOperand &ImmOp = I.getOperand(1);
905 // FIXME: Is going through int64_t always correct?
906 ImmOp.ChangeToImmediate(
907 ImmOp.getFPImm()->getValueAPF().bitcastToAPInt().getZExtValue());
908 } else if (I.getOperand(1).isCImm()) {
909 uint64_t Val = I.getOperand(1).getCImm()->getZExtValue();
910 I.getOperand(1).ChangeToImmediate(Val);
911 } else if (I.getOperand(1).isImm()) {
912 uint64_t Val = I.getOperand(1).getImm();
913 I.getOperand(1).ChangeToImmediate(Val);
916 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
919 case TargetOpcode::G_EXTRACT: {
920 LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
921 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
923 unsigned SrcSize = SrcTy.getSizeInBits();
924 // Larger extracts are vectors, same-size extracts should be something else
925 // by now (either split up or simplified to a COPY).
926 if (SrcTy.getSizeInBits() > 64 || Ty.getSizeInBits() > 32)
929 I.setDesc(TII.get(SrcSize == 64 ? AArch64::UBFMXri : AArch64::UBFMWri));
930 MachineInstrBuilder(MF, I).addImm(I.getOperand(2).getImm() +
931 Ty.getSizeInBits() - 1);
934 assert(SrcSize == 32 && DstTy.getSizeInBits() == 16 &&
935 "unexpected G_EXTRACT types");
936 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
939 unsigned DstReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
940 BuildMI(MBB, std::next(I.getIterator()), I.getDebugLoc(),
941 TII.get(AArch64::COPY))
942 .addDef(I.getOperand(0).getReg())
943 .addUse(DstReg, 0, AArch64::sub_32);
944 RBI.constrainGenericRegister(I.getOperand(0).getReg(),
945 AArch64::GPR32RegClass, MRI);
946 I.getOperand(0).setReg(DstReg);
948 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
951 case TargetOpcode::G_INSERT: {
952 LLT SrcTy = MRI.getType(I.getOperand(2).getReg());
953 LLT DstTy = MRI.getType(I.getOperand(0).getReg());
954 unsigned DstSize = DstTy.getSizeInBits();
955 // Larger inserts are vectors, same-size ones should be something else by
956 // now (split up or turned into COPYs).
957 if (Ty.getSizeInBits() > 64 || SrcTy.getSizeInBits() > 32)
960 I.setDesc(TII.get(DstSize == 64 ? AArch64::BFMXri : AArch64::BFMWri));
961 unsigned LSB = I.getOperand(3).getImm();
962 unsigned Width = MRI.getType(I.getOperand(2).getReg()).getSizeInBits();
963 I.getOperand(3).setImm((DstSize - LSB) % DstSize);
964 MachineInstrBuilder(MF, I).addImm(Width - 1);
967 assert(DstSize == 32 && SrcTy.getSizeInBits() == 16 &&
968 "unexpected G_INSERT types");
969 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
972 unsigned SrcReg = MRI.createGenericVirtualRegister(LLT::scalar(64));
973 BuildMI(MBB, I.getIterator(), I.getDebugLoc(),
974 TII.get(AArch64::SUBREG_TO_REG))
977 .addUse(I.getOperand(2).getReg())
978 .addImm(AArch64::sub_32);
979 RBI.constrainGenericRegister(I.getOperand(2).getReg(),
980 AArch64::GPR32RegClass, MRI);
981 I.getOperand(2).setReg(SrcReg);
983 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
985 case TargetOpcode::G_FRAME_INDEX: {
986 // allocas and G_FRAME_INDEX are only supported in addrspace(0).
987 if (Ty != LLT::pointer(0, 64)) {
988 LLVM_DEBUG(dbgs() << "G_FRAME_INDEX pointer has type: " << Ty
989 << ", expected: " << LLT::pointer(0, 64) << '\n');
992 I.setDesc(TII.get(AArch64::ADDXri));
994 // MOs for a #0 shifted immediate.
995 I.addOperand(MachineOperand::CreateImm(0));
996 I.addOperand(MachineOperand::CreateImm(0));
998 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1001 case TargetOpcode::G_GLOBAL_VALUE: {
1002 auto GV = I.getOperand(1).getGlobal();
1003 if (GV->isThreadLocal()) {
1004 // FIXME: we don't support TLS yet.
1007 unsigned char OpFlags = STI.ClassifyGlobalReference(GV, TM);
1008 if (OpFlags & AArch64II::MO_GOT) {
1009 I.setDesc(TII.get(AArch64::LOADgot));
1010 I.getOperand(1).setTargetFlags(OpFlags);
1011 } else if (TM.getCodeModel() == CodeModel::Large) {
1012 // Materialize the global using movz/movk instructions.
1013 materializeLargeCMVal(I, GV, OpFlags);
1014 I.eraseFromParent();
1016 } else if (TM.getCodeModel() == CodeModel::Tiny) {
1017 I.setDesc(TII.get(AArch64::ADR));
1018 I.getOperand(1).setTargetFlags(OpFlags);
1020 I.setDesc(TII.get(AArch64::MOVaddr));
1021 I.getOperand(1).setTargetFlags(OpFlags | AArch64II::MO_PAGE);
1022 MachineInstrBuilder MIB(MF, I);
1023 MIB.addGlobalAddress(GV, I.getOperand(1).getOffset(),
1024 OpFlags | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
1026 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1029 case TargetOpcode::G_LOAD:
1030 case TargetOpcode::G_STORE: {
1031 LLT PtrTy = MRI.getType(I.getOperand(1).getReg());
1033 if (PtrTy != LLT::pointer(0, 64)) {
1034 LLVM_DEBUG(dbgs() << "Load/Store pointer has type: " << PtrTy
1035 << ", expected: " << LLT::pointer(0, 64) << '\n');
1039 auto &MemOp = **I.memoperands_begin();
1040 if (MemOp.getOrdering() != AtomicOrdering::NotAtomic) {
1041 LLVM_DEBUG(dbgs() << "Atomic load/store not supported yet\n");
1044 unsigned MemSizeInBits = MemOp.getSize() * 8;
1046 const unsigned PtrReg = I.getOperand(1).getReg();
1048 const RegisterBank &PtrRB = *RBI.getRegBank(PtrReg, MRI, TRI);
1049 // Sanity-check the pointer register.
1050 assert(PtrRB.getID() == AArch64::GPRRegBankID &&
1051 "Load/Store pointer operand isn't a GPR");
1052 assert(MRI.getType(PtrReg).isPointer() &&
1053 "Load/Store pointer operand isn't a pointer");
1056 const unsigned ValReg = I.getOperand(0).getReg();
1057 const RegisterBank &RB = *RBI.getRegBank(ValReg, MRI, TRI);
1059 const unsigned NewOpc =
1060 selectLoadStoreUIOp(I.getOpcode(), RB.getID(), MemSizeInBits);
1061 if (NewOpc == I.getOpcode())
1064 I.setDesc(TII.get(NewOpc));
1066 uint64_t Offset = 0;
1067 auto *PtrMI = MRI.getVRegDef(PtrReg);
1069 // Try to fold a GEP into our unsigned immediate addressing mode.
1070 if (PtrMI->getOpcode() == TargetOpcode::G_GEP) {
1071 if (auto COff = getConstantVRegVal(PtrMI->getOperand(2).getReg(), MRI)) {
1072 int64_t Imm = *COff;
1073 const unsigned Size = MemSizeInBits / 8;
1074 const unsigned Scale = Log2_32(Size);
1075 if ((Imm & (Size - 1)) == 0 && Imm >= 0 && Imm < (0x1000 << Scale)) {
1076 unsigned Ptr2Reg = PtrMI->getOperand(1).getReg();
1077 I.getOperand(1).setReg(Ptr2Reg);
1078 PtrMI = MRI.getVRegDef(Ptr2Reg);
1079 Offset = Imm / Size;
1084 // If we haven't folded anything into our addressing mode yet, try to fold
1085 // a frame index into the base+offset.
1086 if (!Offset && PtrMI->getOpcode() == TargetOpcode::G_FRAME_INDEX)
1087 I.getOperand(1).ChangeToFrameIndex(PtrMI->getOperand(1).getIndex());
1089 I.addOperand(MachineOperand::CreateImm(Offset));
1091 // If we're storing a 0, use WZR/XZR.
1092 if (auto CVal = getConstantVRegVal(ValReg, MRI)) {
1093 if (*CVal == 0 && Opcode == TargetOpcode::G_STORE) {
1094 if (I.getOpcode() == AArch64::STRWui)
1095 I.getOperand(0).setReg(AArch64::WZR);
1096 else if (I.getOpcode() == AArch64::STRXui)
1097 I.getOperand(0).setReg(AArch64::XZR);
1101 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1104 case TargetOpcode::G_SMULH:
1105 case TargetOpcode::G_UMULH: {
1106 // Reject the various things we don't support yet.
1107 if (unsupportedBinOp(I, RBI, MRI, TRI))
1110 const unsigned DefReg = I.getOperand(0).getReg();
1111 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1113 if (RB.getID() != AArch64::GPRRegBankID) {
1114 LLVM_DEBUG(dbgs() << "G_[SU]MULH on bank: " << RB << ", expected: GPR\n");
1118 if (Ty != LLT::scalar(64)) {
1119 LLVM_DEBUG(dbgs() << "G_[SU]MULH has type: " << Ty
1120 << ", expected: " << LLT::scalar(64) << '\n');
1124 unsigned NewOpc = I.getOpcode() == TargetOpcode::G_SMULH ? AArch64::SMULHrr
1126 I.setDesc(TII.get(NewOpc));
1128 // Now that we selected an opcode, we need to constrain the register
1129 // operands to use appropriate classes.
1130 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1132 case TargetOpcode::G_FADD:
1133 case TargetOpcode::G_FSUB:
1134 case TargetOpcode::G_FMUL:
1135 case TargetOpcode::G_FDIV:
1137 case TargetOpcode::G_OR:
1138 case TargetOpcode::G_SHL:
1139 case TargetOpcode::G_LSHR:
1140 case TargetOpcode::G_ASHR:
1141 case TargetOpcode::G_GEP: {
1142 // Reject the various things we don't support yet.
1143 if (unsupportedBinOp(I, RBI, MRI, TRI))
1146 const unsigned OpSize = Ty.getSizeInBits();
1148 const unsigned DefReg = I.getOperand(0).getReg();
1149 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1151 const unsigned NewOpc = selectBinaryOp(I.getOpcode(), RB.getID(), OpSize);
1152 if (NewOpc == I.getOpcode())
1155 I.setDesc(TII.get(NewOpc));
1156 // FIXME: Should the type be always reset in setDesc?
1158 // Now that we selected an opcode, we need to constrain the register
1159 // operands to use appropriate classes.
1160 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1163 case TargetOpcode::G_PTR_MASK: {
1164 uint64_t Align = I.getOperand(2).getImm();
1165 if (Align >= 64 || Align == 0)
1168 uint64_t Mask = ~((1ULL << Align) - 1);
1169 I.setDesc(TII.get(AArch64::ANDXri));
1170 I.getOperand(2).setImm(AArch64_AM::encodeLogicalImmediate(Mask, 64));
1172 return constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1174 case TargetOpcode::G_PTRTOINT:
1175 case TargetOpcode::G_TRUNC: {
1176 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1177 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1179 const unsigned DstReg = I.getOperand(0).getReg();
1180 const unsigned SrcReg = I.getOperand(1).getReg();
1182 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1183 const RegisterBank &SrcRB = *RBI.getRegBank(SrcReg, MRI, TRI);
1185 if (DstRB.getID() != SrcRB.getID()) {
1187 dbgs() << "G_TRUNC/G_PTRTOINT input/output on different banks\n");
1191 if (DstRB.getID() == AArch64::GPRRegBankID) {
1192 const TargetRegisterClass *DstRC =
1193 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1197 const TargetRegisterClass *SrcRC =
1198 getRegClassForTypeOnBank(SrcTy, SrcRB, RBI);
1202 if (!RBI.constrainGenericRegister(SrcReg, *SrcRC, MRI) ||
1203 !RBI.constrainGenericRegister(DstReg, *DstRC, MRI)) {
1204 LLVM_DEBUG(dbgs() << "Failed to constrain G_TRUNC/G_PTRTOINT\n");
1208 if (DstRC == SrcRC) {
1209 // Nothing to be done
1210 } else if (Opcode == TargetOpcode::G_TRUNC && DstTy == LLT::scalar(32) &&
1211 SrcTy == LLT::scalar(64)) {
1212 llvm_unreachable("TableGen can import this case");
1214 } else if (DstRC == &AArch64::GPR32RegClass &&
1215 SrcRC == &AArch64::GPR64RegClass) {
1216 I.getOperand(1).setSubReg(AArch64::sub_32);
1219 dbgs() << "Unhandled mismatched classes in G_TRUNC/G_PTRTOINT\n");
1223 I.setDesc(TII.get(TargetOpcode::COPY));
1225 } else if (DstRB.getID() == AArch64::FPRRegBankID) {
1226 if (DstTy == LLT::vector(4, 16) && SrcTy == LLT::vector(4, 32)) {
1227 I.setDesc(TII.get(AArch64::XTNv4i16));
1228 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1236 case TargetOpcode::G_ANYEXT: {
1237 const unsigned DstReg = I.getOperand(0).getReg();
1238 const unsigned SrcReg = I.getOperand(1).getReg();
1240 const RegisterBank &RBDst = *RBI.getRegBank(DstReg, MRI, TRI);
1241 if (RBDst.getID() != AArch64::GPRRegBankID) {
1242 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBDst
1243 << ", expected: GPR\n");
1247 const RegisterBank &RBSrc = *RBI.getRegBank(SrcReg, MRI, TRI);
1248 if (RBSrc.getID() != AArch64::GPRRegBankID) {
1249 LLVM_DEBUG(dbgs() << "G_ANYEXT on bank: " << RBSrc
1250 << ", expected: GPR\n");
1254 const unsigned DstSize = MRI.getType(DstReg).getSizeInBits();
1257 LLVM_DEBUG(dbgs() << "G_ANYEXT operand has no size, not a gvreg?\n");
1261 if (DstSize != 64 && DstSize > 32) {
1262 LLVM_DEBUG(dbgs() << "G_ANYEXT to size: " << DstSize
1263 << ", expected: 32 or 64\n");
1266 // At this point G_ANYEXT is just like a plain COPY, but we need
1267 // to explicitly form the 64-bit value if any.
1269 unsigned ExtSrc = MRI.createVirtualRegister(&AArch64::GPR64allRegClass);
1270 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1274 .addImm(AArch64::sub_32);
1275 I.getOperand(1).setReg(ExtSrc);
1277 return selectCopy(I, TII, MRI, TRI, RBI);
1280 case TargetOpcode::G_ZEXT:
1281 case TargetOpcode::G_SEXT: {
1282 unsigned Opcode = I.getOpcode();
1283 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1284 SrcTy = MRI.getType(I.getOperand(1).getReg());
1285 const bool isSigned = Opcode == TargetOpcode::G_SEXT;
1286 const unsigned DefReg = I.getOperand(0).getReg();
1287 const unsigned SrcReg = I.getOperand(1).getReg();
1288 const RegisterBank &RB = *RBI.getRegBank(DefReg, MRI, TRI);
1290 if (RB.getID() != AArch64::GPRRegBankID) {
1291 LLVM_DEBUG(dbgs() << TII.getName(I.getOpcode()) << " on bank: " << RB
1292 << ", expected: GPR\n");
1297 if (DstTy == LLT::scalar(64)) {
1298 // FIXME: Can we avoid manually doing this?
1299 if (!RBI.constrainGenericRegister(SrcReg, AArch64::GPR32RegClass, MRI)) {
1300 LLVM_DEBUG(dbgs() << "Failed to constrain " << TII.getName(Opcode)
1305 const unsigned SrcXReg =
1306 MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1307 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::SUBREG_TO_REG))
1311 .addImm(AArch64::sub_32);
1313 const unsigned NewOpc = isSigned ? AArch64::SBFMXri : AArch64::UBFMXri;
1314 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1318 .addImm(SrcTy.getSizeInBits() - 1);
1319 } else if (DstTy.isScalar() && DstTy.getSizeInBits() <= 32) {
1320 const unsigned NewOpc = isSigned ? AArch64::SBFMWri : AArch64::UBFMWri;
1321 ExtI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(NewOpc))
1325 .addImm(SrcTy.getSizeInBits() - 1);
1330 constrainSelectedInstRegOperands(*ExtI, TII, TRI, RBI);
1332 I.eraseFromParent();
1336 case TargetOpcode::G_SITOFP:
1337 case TargetOpcode::G_UITOFP:
1338 case TargetOpcode::G_FPTOSI:
1339 case TargetOpcode::G_FPTOUI: {
1340 const LLT DstTy = MRI.getType(I.getOperand(0).getReg()),
1341 SrcTy = MRI.getType(I.getOperand(1).getReg());
1342 const unsigned NewOpc = selectFPConvOpc(Opcode, DstTy, SrcTy);
1343 if (NewOpc == Opcode)
1346 I.setDesc(TII.get(NewOpc));
1347 constrainSelectedInstRegOperands(I, TII, TRI, RBI);
1353 case TargetOpcode::G_INTTOPTR:
1354 // The importer is currently unable to import pointer types since they
1355 // didn't exist in SelectionDAG.
1356 return selectCopy(I, TII, MRI, TRI, RBI);
1358 case TargetOpcode::G_BITCAST:
1359 // Imported SelectionDAG rules can handle every bitcast except those that
1360 // bitcast from a type to the same type. Ideally, these shouldn't occur
1361 // but we might not run an optimizer that deletes them.
1362 if (MRI.getType(I.getOperand(0).getReg()) ==
1363 MRI.getType(I.getOperand(1).getReg()))
1364 return selectCopy(I, TII, MRI, TRI, RBI);
1367 case TargetOpcode::G_SELECT: {
1368 if (MRI.getType(I.getOperand(1).getReg()) != LLT::scalar(1)) {
1369 LLVM_DEBUG(dbgs() << "G_SELECT cond has type: " << Ty
1370 << ", expected: " << LLT::scalar(1) << '\n');
1374 const unsigned CondReg = I.getOperand(1).getReg();
1375 const unsigned TReg = I.getOperand(2).getReg();
1376 const unsigned FReg = I.getOperand(3).getReg();
1378 unsigned CSelOpc = 0;
1380 if (Ty == LLT::scalar(32)) {
1381 CSelOpc = AArch64::CSELWr;
1382 } else if (Ty == LLT::scalar(64) || Ty == LLT::pointer(0, 64)) {
1383 CSelOpc = AArch64::CSELXr;
1388 MachineInstr &TstMI =
1389 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ANDSWri))
1390 .addDef(AArch64::WZR)
1392 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1394 MachineInstr &CSelMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CSelOpc))
1395 .addDef(I.getOperand(0).getReg())
1398 .addImm(AArch64CC::NE);
1400 constrainSelectedInstRegOperands(TstMI, TII, TRI, RBI);
1401 constrainSelectedInstRegOperands(CSelMI, TII, TRI, RBI);
1403 I.eraseFromParent();
1406 case TargetOpcode::G_ICMP: {
1407 if (Ty != LLT::scalar(32)) {
1408 LLVM_DEBUG(dbgs() << "G_ICMP result has type: " << Ty
1409 << ", expected: " << LLT::scalar(32) << '\n');
1413 unsigned CmpOpc = 0;
1416 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1417 if (CmpTy == LLT::scalar(32)) {
1418 CmpOpc = AArch64::SUBSWrr;
1419 ZReg = AArch64::WZR;
1420 } else if (CmpTy == LLT::scalar(64) || CmpTy.isPointer()) {
1421 CmpOpc = AArch64::SUBSXrr;
1422 ZReg = AArch64::XZR;
1427 // CSINC increments the result by one when the condition code is false.
1428 // Therefore, we have to invert the predicate to get an increment by 1 when
1429 // the predicate is true.
1430 const AArch64CC::CondCode invCC =
1431 changeICMPPredToAArch64CC(CmpInst::getInversePredicate(
1432 (CmpInst::Predicate)I.getOperand(1).getPredicate()));
1434 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1436 .addUse(I.getOperand(2).getReg())
1437 .addUse(I.getOperand(3).getReg());
1439 MachineInstr &CSetMI =
1440 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1441 .addDef(I.getOperand(0).getReg())
1442 .addUse(AArch64::WZR)
1443 .addUse(AArch64::WZR)
1446 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1447 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1449 I.eraseFromParent();
1453 case TargetOpcode::G_FCMP: {
1454 if (Ty != LLT::scalar(32)) {
1455 LLVM_DEBUG(dbgs() << "G_FCMP result has type: " << Ty
1456 << ", expected: " << LLT::scalar(32) << '\n');
1460 unsigned CmpOpc = 0;
1461 LLT CmpTy = MRI.getType(I.getOperand(2).getReg());
1462 if (CmpTy == LLT::scalar(32)) {
1463 CmpOpc = AArch64::FCMPSrr;
1464 } else if (CmpTy == LLT::scalar(64)) {
1465 CmpOpc = AArch64::FCMPDrr;
1472 AArch64CC::CondCode CC1, CC2;
1473 changeFCMPPredToAArch64CC(
1474 (CmpInst::Predicate)I.getOperand(1).getPredicate(), CC1, CC2);
1476 MachineInstr &CmpMI = *BuildMI(MBB, I, I.getDebugLoc(), TII.get(CmpOpc))
1477 .addUse(I.getOperand(2).getReg())
1478 .addUse(I.getOperand(3).getReg());
1480 const unsigned DefReg = I.getOperand(0).getReg();
1481 unsigned Def1Reg = DefReg;
1482 if (CC2 != AArch64CC::AL)
1483 Def1Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1485 MachineInstr &CSetMI =
1486 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1488 .addUse(AArch64::WZR)
1489 .addUse(AArch64::WZR)
1490 .addImm(getInvertedCondCode(CC1));
1492 if (CC2 != AArch64CC::AL) {
1493 unsigned Def2Reg = MRI.createVirtualRegister(&AArch64::GPR32RegClass);
1494 MachineInstr &CSet2MI =
1495 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::CSINCWr))
1497 .addUse(AArch64::WZR)
1498 .addUse(AArch64::WZR)
1499 .addImm(getInvertedCondCode(CC2));
1500 MachineInstr &OrMI =
1501 *BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::ORRWrr))
1505 constrainSelectedInstRegOperands(OrMI, TII, TRI, RBI);
1506 constrainSelectedInstRegOperands(CSet2MI, TII, TRI, RBI);
1509 constrainSelectedInstRegOperands(CmpMI, TII, TRI, RBI);
1510 constrainSelectedInstRegOperands(CSetMI, TII, TRI, RBI);
1512 I.eraseFromParent();
1515 case TargetOpcode::G_VASTART:
1516 return STI.isTargetDarwin() ? selectVaStartDarwin(I, MF, MRI)
1517 : selectVaStartAAPCS(I, MF, MRI);
1518 case TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS:
1519 if (!I.getOperand(0).isIntrinsicID())
1521 if (I.getOperand(0).getIntrinsicID() != Intrinsic::trap)
1523 BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::BRK))
1525 I.eraseFromParent();
1527 case TargetOpcode::G_IMPLICIT_DEF: {
1528 I.setDesc(TII.get(TargetOpcode::IMPLICIT_DEF));
1529 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1530 const unsigned DstReg = I.getOperand(0).getReg();
1531 const RegisterBank &DstRB = *RBI.getRegBank(DstReg, MRI, TRI);
1532 const TargetRegisterClass *DstRC =
1533 getRegClassForTypeOnBank(DstTy, DstRB, RBI);
1534 RBI.constrainGenericRegister(DstReg, *DstRC, MRI);
1537 case TargetOpcode::G_BLOCK_ADDR: {
1538 if (TM.getCodeModel() == CodeModel::Large) {
1539 materializeLargeCMVal(I, I.getOperand(1).getBlockAddress(), 0);
1540 I.eraseFromParent();
1543 I.setDesc(TII.get(AArch64::MOVaddrBA));
1544 auto MovMI = BuildMI(MBB, I, I.getDebugLoc(), TII.get(AArch64::MOVaddrBA),
1545 I.getOperand(0).getReg())
1546 .addBlockAddress(I.getOperand(1).getBlockAddress(),
1547 /* Offset */ 0, AArch64II::MO_PAGE)
1549 I.getOperand(1).getBlockAddress(), /* Offset */ 0,
1550 AArch64II::MO_NC | AArch64II::MO_PAGEOFF);
1551 I.eraseFromParent();
1552 return constrainSelectedInstRegOperands(*MovMI, TII, TRI, RBI);
1555 case TargetOpcode::G_BUILD_VECTOR:
1556 return selectBuildVector(I, MRI);
1557 case TargetOpcode::G_MERGE_VALUES:
1558 return selectMergeValues(I, MRI);
1564 bool AArch64InstructionSelector::emitScalarToVector(
1565 unsigned &Dst, const LLT DstTy, const TargetRegisterClass *DstRC,
1566 unsigned Scalar, MachineBasicBlock &MBB,
1567 MachineBasicBlock::iterator MBBI, MachineRegisterInfo &MRI) const {
1568 Dst = MRI.createVirtualRegister(DstRC);
1570 unsigned UndefVec = MRI.createVirtualRegister(DstRC);
1571 MachineInstr &UndefMI = *BuildMI(MBB, MBBI, MBBI->getDebugLoc(),
1572 TII.get(TargetOpcode::IMPLICIT_DEF))
1575 auto BuildFn = [&](unsigned SubregIndex) {
1576 MachineInstr &InsMI = *BuildMI(MBB, MBBI, MBBI->getDebugLoc(),
1577 TII.get(TargetOpcode::INSERT_SUBREG))
1581 .addImm(SubregIndex);
1582 constrainSelectedInstRegOperands(UndefMI, TII, TRI, RBI);
1583 return constrainSelectedInstRegOperands(InsMI, TII, TRI, RBI);
1586 switch (DstTy.getElementType().getSizeInBits()) {
1588 return BuildFn(AArch64::ssub);
1590 return BuildFn(AArch64::dsub);
1596 bool AArch64InstructionSelector::selectMergeValues(
1597 MachineInstr &I, MachineRegisterInfo &MRI) const {
1598 assert(I.getOpcode() == TargetOpcode::G_MERGE_VALUES && "unexpected opcode");
1599 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1600 const LLT SrcTy = MRI.getType(I.getOperand(1).getReg());
1601 assert(!DstTy.isVector() && !SrcTy.isVector() && "invalid merge operation");
1603 // At the moment we only support merging two s32s into an s64.
1604 if (I.getNumOperands() != 3)
1606 if (DstTy.getSizeInBits() != 64 || SrcTy.getSizeInBits() != 32)
1608 const RegisterBank &RB = *RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI);
1609 if (RB.getID() != AArch64::GPRRegBankID)
1612 auto *DstRC = &AArch64::GPR64RegClass;
1613 unsigned SubToRegDef = MRI.createVirtualRegister(DstRC);
1614 MachineInstr &SubRegMI = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1615 TII.get(TargetOpcode::SUBREG_TO_REG))
1616 .addDef(SubToRegDef)
1618 .addUse(I.getOperand(1).getReg())
1619 .addImm(AArch64::sub_32);
1620 unsigned SubToRegDef2 = MRI.createVirtualRegister(DstRC);
1621 // Need to anyext the second scalar before we can use bfm
1622 MachineInstr &SubRegMI2 = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1623 TII.get(TargetOpcode::SUBREG_TO_REG))
1624 .addDef(SubToRegDef2)
1626 .addUse(I.getOperand(2).getReg())
1627 .addImm(AArch64::sub_32);
1629 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(AArch64::BFMXri))
1630 .addDef(I.getOperand(0).getReg())
1631 .addUse(SubToRegDef)
1632 .addUse(SubToRegDef2)
1635 constrainSelectedInstRegOperands(SubRegMI, TII, TRI, RBI);
1636 constrainSelectedInstRegOperands(SubRegMI2, TII, TRI, RBI);
1637 constrainSelectedInstRegOperands(BFM, TII, TRI, RBI);
1638 I.eraseFromParent();
1642 bool AArch64InstructionSelector::selectBuildVector(
1643 MachineInstr &I, MachineRegisterInfo &MRI) const {
1644 assert(I.getOpcode() == TargetOpcode::G_BUILD_VECTOR);
1645 // Until we port more of the optimized selections, for now just use a vector
1647 const LLT DstTy = MRI.getType(I.getOperand(0).getReg());
1648 const LLT EltTy = MRI.getType(I.getOperand(1).getReg());
1649 unsigned EltSize = EltTy.getSizeInBits();
1650 if (EltSize < 32 || EltSize > 64)
1651 return false; // Don't support all element types yet.
1652 const RegisterBank &RB = *RBI.getRegBank(I.getOperand(1).getReg(), MRI, TRI);
1655 if (RB.getID() == AArch64::GPRRegBankID) {
1656 if (EltSize == 32) {
1657 Opc = AArch64::INSvi32gpr;
1658 SubregIdx = AArch64::ssub;
1660 Opc = AArch64::INSvi64gpr;
1661 SubregIdx = AArch64::dsub;
1664 if (EltSize == 32) {
1665 Opc = AArch64::INSvi32lane;
1666 SubregIdx = AArch64::ssub;
1668 Opc = AArch64::INSvi64lane;
1669 SubregIdx = AArch64::dsub;
1673 if (EltSize * DstTy.getNumElements() != 128)
1674 return false; // Don't handle unpacked vectors yet.
1676 unsigned DstVec = 0;
1677 const TargetRegisterClass *DstRC = getRegClassForTypeOnBank(
1678 DstTy, RBI.getRegBank(AArch64::FPRRegBankID), RBI);
1679 emitScalarToVector(DstVec, DstTy, DstRC, I.getOperand(1).getReg(),
1680 *I.getParent(), I.getIterator(), MRI);
1681 for (unsigned i = 2, e = DstTy.getSizeInBits() / EltSize + 1; i < e; ++i) {
1683 // For the last insert re-use the dst reg of the G_BUILD_VECTOR.
1685 InsDef = MRI.createVirtualRegister(DstRC);
1687 InsDef = I.getOperand(0).getReg();
1688 unsigned LaneIdx = i - 1;
1689 if (RB.getID() == AArch64::FPRRegBankID) {
1690 unsigned ImpDef = MRI.createVirtualRegister(DstRC);
1691 MachineInstr &ImpDefMI = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1692 TII.get(TargetOpcode::IMPLICIT_DEF))
1694 unsigned InsSubDef = MRI.createVirtualRegister(DstRC);
1695 MachineInstr &InsSubMI = *BuildMI(*I.getParent(), I, I.getDebugLoc(),
1696 TII.get(TargetOpcode::INSERT_SUBREG))
1699 .addUse(I.getOperand(i).getReg())
1701 MachineInstr &InsEltMI =
1702 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opc))
1708 constrainSelectedInstRegOperands(ImpDefMI, TII, TRI, RBI);
1709 constrainSelectedInstRegOperands(InsSubMI, TII, TRI, RBI);
1710 constrainSelectedInstRegOperands(InsEltMI, TII, TRI, RBI);
1713 MachineInstr &InsMI =
1714 *BuildMI(*I.getParent(), I, I.getDebugLoc(), TII.get(Opc))
1718 .addUse(I.getOperand(i).getReg());
1719 constrainSelectedInstRegOperands(InsMI, TII, TRI, RBI);
1723 I.eraseFromParent();
1727 /// SelectArithImmed - Select an immediate value that can be represented as
1728 /// a 12-bit value shifted left by either 0 or 12. If so, return true with
1729 /// Val set to the 12-bit value and Shift set to the shifter operand.
1730 InstructionSelector::ComplexRendererFns
1731 AArch64InstructionSelector::selectArithImmed(MachineOperand &Root) const {
1732 MachineInstr &MI = *Root.getParent();
1733 MachineBasicBlock &MBB = *MI.getParent();
1734 MachineFunction &MF = *MBB.getParent();
1735 MachineRegisterInfo &MRI = MF.getRegInfo();
1737 // This function is called from the addsub_shifted_imm ComplexPattern,
1738 // which lists [imm] as the list of opcode it's interested in, however
1739 // we still need to check whether the operand is actually an immediate
1740 // here because the ComplexPattern opcode list is only used in
1741 // root-level opcode matching.
1744 Immed = Root.getImm();
1745 else if (Root.isCImm())
1746 Immed = Root.getCImm()->getZExtValue();
1747 else if (Root.isReg()) {
1748 MachineInstr *Def = MRI.getVRegDef(Root.getReg());
1749 if (Def->getOpcode() != TargetOpcode::G_CONSTANT)
1751 MachineOperand &Op1 = Def->getOperand(1);
1752 if (!Op1.isCImm() || Op1.getCImm()->getBitWidth() > 64)
1754 Immed = Op1.getCImm()->getZExtValue();
1760 if (Immed >> 12 == 0) {
1762 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
1764 Immed = Immed >> 12;
1768 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
1770 [=](MachineInstrBuilder &MIB) { MIB.addImm(Immed); },
1771 [=](MachineInstrBuilder &MIB) { MIB.addImm(ShVal); },
1775 /// Select a "register plus unscaled signed 9-bit immediate" address. This
1776 /// should only match when there is an offset that is not valid for a scaled
1777 /// immediate addressing mode. The "Size" argument is the size in bytes of the
1778 /// memory reference, which is needed here to know what is valid for a scaled
1780 InstructionSelector::ComplexRendererFns
1781 AArch64InstructionSelector::selectAddrModeUnscaled(MachineOperand &Root,
1782 unsigned Size) const {
1783 MachineRegisterInfo &MRI =
1784 Root.getParent()->getParent()->getParent()->getRegInfo();
1789 if (!isBaseWithConstantOffset(Root, MRI))
1792 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1796 MachineOperand &OffImm = RootDef->getOperand(2);
1797 if (!OffImm.isReg())
1799 MachineInstr *RHS = MRI.getVRegDef(OffImm.getReg());
1800 if (!RHS || RHS->getOpcode() != TargetOpcode::G_CONSTANT)
1803 MachineOperand &RHSOp1 = RHS->getOperand(1);
1804 if (!RHSOp1.isCImm() || RHSOp1.getCImm()->getBitWidth() > 64)
1806 RHSC = RHSOp1.getCImm()->getSExtValue();
1808 // If the offset is valid as a scaled immediate, don't match here.
1809 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Log2_32(Size)))
1811 if (RHSC >= -256 && RHSC < 256) {
1812 MachineOperand &Base = RootDef->getOperand(1);
1814 [=](MachineInstrBuilder &MIB) { MIB.add(Base); },
1815 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC); },
1821 /// Select a "register plus scaled unsigned 12-bit immediate" address. The
1822 /// "Size" argument is the size in bytes of the memory reference, which
1823 /// determines the scale.
1824 InstructionSelector::ComplexRendererFns
1825 AArch64InstructionSelector::selectAddrModeIndexed(MachineOperand &Root,
1826 unsigned Size) const {
1827 MachineRegisterInfo &MRI =
1828 Root.getParent()->getParent()->getParent()->getRegInfo();
1833 MachineInstr *RootDef = MRI.getVRegDef(Root.getReg());
1837 if (RootDef->getOpcode() == TargetOpcode::G_FRAME_INDEX) {
1839 [=](MachineInstrBuilder &MIB) { MIB.add(RootDef->getOperand(1)); },
1840 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1844 if (isBaseWithConstantOffset(Root, MRI)) {
1845 MachineOperand &LHS = RootDef->getOperand(1);
1846 MachineOperand &RHS = RootDef->getOperand(2);
1847 MachineInstr *LHSDef = MRI.getVRegDef(LHS.getReg());
1848 MachineInstr *RHSDef = MRI.getVRegDef(RHS.getReg());
1849 if (LHSDef && RHSDef) {
1850 int64_t RHSC = (int64_t)RHSDef->getOperand(1).getCImm()->getZExtValue();
1851 unsigned Scale = Log2_32(Size);
1852 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
1853 if (LHSDef->getOpcode() == TargetOpcode::G_FRAME_INDEX)
1855 [=](MachineInstrBuilder &MIB) { MIB.add(LHSDef->getOperand(1)); },
1856 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1860 [=](MachineInstrBuilder &MIB) { MIB.add(LHS); },
1861 [=](MachineInstrBuilder &MIB) { MIB.addImm(RHSC >> Scale); },
1867 // Before falling back to our general case, check if the unscaled
1868 // instructions can handle this. If so, that's preferable.
1869 if (selectAddrModeUnscaled(Root, Size).hasValue())
1873 [=](MachineInstrBuilder &MIB) { MIB.add(Root); },
1874 [=](MachineInstrBuilder &MIB) { MIB.addImm(0); },
1878 void AArch64InstructionSelector::renderTruncImm(MachineInstrBuilder &MIB,
1879 const MachineInstr &MI) const {
1880 const MachineRegisterInfo &MRI = MI.getParent()->getParent()->getRegInfo();
1881 assert(MI.getOpcode() == TargetOpcode::G_CONSTANT && "Expected G_CONSTANT");
1882 Optional<int64_t> CstVal = getConstantVRegVal(MI.getOperand(0).getReg(), MRI);
1883 assert(CstVal && "Expected constant value");
1884 MIB.addImm(CstVal.getValue());
1888 InstructionSelector *
1889 createAArch64InstructionSelector(const AArch64TargetMachine &TM,
1890 AArch64Subtarget &Subtarget,
1891 AArch64RegisterBankInfo &RBI) {
1892 return new AArch64InstructionSelector(TM, Subtarget, RBI);