1 //===-- AArch6464FastISel.cpp - AArch64 FastISel implementation -----------===//
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 defines the AArch64-specific support for the FastISel class. Some
11 // of the target-specific code is generated by tablegen in the file
12 // AArch64GenFastISel.inc, which is #included here.
14 //===----------------------------------------------------------------------===//
17 #include "AArch64Subtarget.h"
18 #include "AArch64TargetMachine.h"
19 #include "MCTargetDesc/AArch64AddressingModes.h"
20 #include "llvm/Analysis/BranchProbabilityInfo.h"
21 #include "llvm/CodeGen/CallingConvLower.h"
22 #include "llvm/CodeGen/FastISel.h"
23 #include "llvm/CodeGen/FunctionLoweringInfo.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/IR/CallingConv.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DerivedTypes.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/GetElementPtrTypeIterator.h"
33 #include "llvm/IR/GlobalAlias.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/IntrinsicInst.h"
37 #include "llvm/IR/Operator.h"
38 #include "llvm/Support/CommandLine.h"
43 class AArch64FastISel final : public FastISel {
53 AArch64_AM::ShiftExtendType ExtType;
61 const GlobalValue *GV;
64 Address() : Kind(RegBase), ExtType(AArch64_AM::InvalidShiftExtend),
65 OffsetReg(0), Shift(0), Offset(0), GV(nullptr) { Base.Reg = 0; }
66 void setKind(BaseKind K) { Kind = K; }
67 BaseKind getKind() const { return Kind; }
68 void setExtendType(AArch64_AM::ShiftExtendType E) { ExtType = E; }
69 AArch64_AM::ShiftExtendType getExtendType() const { return ExtType; }
70 bool isRegBase() const { return Kind == RegBase; }
71 bool isFIBase() const { return Kind == FrameIndexBase; }
72 void setReg(unsigned Reg) {
73 assert(isRegBase() && "Invalid base register access!");
76 unsigned getReg() const {
77 assert(isRegBase() && "Invalid base register access!");
80 void setOffsetReg(unsigned Reg) {
81 assert(isRegBase() && "Invalid offset register access!");
84 unsigned getOffsetReg() const {
85 assert(isRegBase() && "Invalid offset register access!");
88 void setFI(unsigned FI) {
89 assert(isFIBase() && "Invalid base frame index access!");
92 unsigned getFI() const {
93 assert(isFIBase() && "Invalid base frame index access!");
96 void setOffset(int64_t O) { Offset = O; }
97 int64_t getOffset() { return Offset; }
98 void setShift(unsigned S) { Shift = S; }
99 unsigned getShift() { return Shift; }
101 void setGlobalValue(const GlobalValue *G) { GV = G; }
102 const GlobalValue *getGlobalValue() { return GV; }
105 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const AArch64Subtarget *Subtarget;
108 LLVMContext *Context;
110 bool fastLowerArguments() override;
111 bool fastLowerCall(CallLoweringInfo &CLI) override;
112 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
115 // Selection routines.
116 bool selectAddSub(const Instruction *I);
117 bool selectLogicalOp(const Instruction *I);
118 bool selectLoad(const Instruction *I);
119 bool selectStore(const Instruction *I);
120 bool selectBranch(const Instruction *I);
121 bool selectIndirectBr(const Instruction *I);
122 bool selectCmp(const Instruction *I);
123 bool selectSelect(const Instruction *I);
124 bool selectFPExt(const Instruction *I);
125 bool selectFPTrunc(const Instruction *I);
126 bool selectFPToInt(const Instruction *I, bool Signed);
127 bool selectIntToFP(const Instruction *I, bool Signed);
128 bool selectRem(const Instruction *I, unsigned ISDOpcode);
129 bool selectRet(const Instruction *I);
130 bool selectTrunc(const Instruction *I);
131 bool selectIntExt(const Instruction *I);
132 bool selectMul(const Instruction *I);
133 bool selectShift(const Instruction *I);
134 bool selectBitCast(const Instruction *I);
135 bool selectFRem(const Instruction *I);
136 bool selectSDiv(const Instruction *I);
138 // Utility helper routines.
139 bool isTypeLegal(Type *Ty, MVT &VT);
140 bool isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed = false);
141 bool isValueAvailable(const Value *V) const;
142 bool computeAddress(const Value *Obj, Address &Addr, Type *Ty = nullptr);
143 bool computeCallAddress(const Value *V, Address &Addr);
144 bool simplifyAddress(Address &Addr, MVT VT);
145 void addLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
146 unsigned Flags, unsigned ScaleFactor,
147 MachineMemOperand *MMO);
148 bool isMemCpySmall(uint64_t Len, unsigned Alignment);
149 bool tryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
151 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
154 // Emit helper routines.
155 unsigned emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
156 const Value *RHS, bool SetFlags = false,
157 bool WantResult = true, bool IsZExt = false);
158 unsigned emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
159 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
160 bool SetFlags = false, bool WantResult = true);
161 unsigned emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
162 bool LHSIsKill, uint64_t Imm, bool SetFlags = false,
163 bool WantResult = true);
164 unsigned emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
165 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
166 AArch64_AM::ShiftExtendType ShiftType,
167 uint64_t ShiftImm, bool SetFlags = false,
168 bool WantResult = true);
169 unsigned emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
170 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
171 AArch64_AM::ShiftExtendType ExtType,
172 uint64_t ShiftImm, bool SetFlags = false,
173 bool WantResult = true);
176 bool emitCompareAndBranch(const BranchInst *BI);
177 bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
178 bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
179 bool emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
180 bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
181 bool emitLoad(MVT VT, MVT ResultVT, unsigned &ResultReg, Address Addr,
182 bool WantZExt = true, MachineMemOperand *MMO = nullptr);
183 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
184 MachineMemOperand *MMO = nullptr);
185 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
186 unsigned emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
187 unsigned emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
188 bool SetFlags = false, bool WantResult = true,
189 bool IsZExt = false);
190 unsigned emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
191 bool SetFlags = false, bool WantResult = true,
192 bool IsZExt = false);
193 unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
194 unsigned RHSReg, bool RHSIsKill, bool WantResult = true);
195 unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
196 unsigned RHSReg, bool RHSIsKill,
197 AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
198 bool WantResult = true);
199 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
201 unsigned emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
202 bool LHSIsKill, uint64_t Imm);
203 unsigned emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
204 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
206 unsigned emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
207 unsigned emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
208 unsigned Op1, bool Op1IsKill);
209 unsigned emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
210 unsigned Op1, bool Op1IsKill);
211 unsigned emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
212 unsigned Op1, bool Op1IsKill);
213 unsigned emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
214 unsigned Op1Reg, bool Op1IsKill);
215 unsigned emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
216 uint64_t Imm, bool IsZExt = true);
217 unsigned emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
218 unsigned Op1Reg, bool Op1IsKill);
219 unsigned emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
220 uint64_t Imm, bool IsZExt = true);
221 unsigned emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
222 unsigned Op1Reg, bool Op1IsKill);
223 unsigned emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
224 uint64_t Imm, bool IsZExt = false);
226 unsigned materializeInt(const ConstantInt *CI, MVT VT);
227 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
228 unsigned materializeGV(const GlobalValue *GV);
230 // Call handling routines.
232 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
233 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
235 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
238 // Backend specific FastISel code.
239 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
240 unsigned fastMaterializeConstant(const Constant *C) override;
241 unsigned fastMaterializeFloatZero(const ConstantFP* CF) override;
243 explicit AArch64FastISel(FunctionLoweringInfo &FuncInfo,
244 const TargetLibraryInfo *LibInfo)
245 : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
246 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
247 Context = &FuncInfo.Fn->getContext();
250 bool fastSelectInstruction(const Instruction *I) override;
252 #include "AArch64GenFastISel.inc"
255 } // end anonymous namespace
257 #include "AArch64GenCallingConv.inc"
259 /// \brief Check if the sign-/zero-extend will be a noop.
260 static bool isIntExtFree(const Instruction *I) {
261 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
262 "Unexpected integer extend instruction.");
263 assert(!I->getType()->isVectorTy() && I->getType()->isIntegerTy() &&
264 "Unexpected value type.");
265 bool IsZExt = isa<ZExtInst>(I);
267 if (const auto *LI = dyn_cast<LoadInst>(I->getOperand(0)))
271 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0)))
272 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr()))
278 /// \brief Determine the implicit scale factor that is applied by a memory
279 /// operation for a given value type.
280 static unsigned getImplicitScaleFactor(MVT VT) {
281 switch (VT.SimpleTy) {
284 case MVT::i1: // fall-through
289 case MVT::i32: // fall-through
292 case MVT::i64: // fall-through
298 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
299 if (CC == CallingConv::WebKit_JS)
300 return CC_AArch64_WebKit_JS;
301 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
304 unsigned AArch64FastISel::fastMaterializeAlloca(const AllocaInst *AI) {
305 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
306 "Alloca should always return a pointer.");
308 // Don't handle dynamic allocas.
309 if (!FuncInfo.StaticAllocaMap.count(AI))
312 DenseMap<const AllocaInst *, int>::iterator SI =
313 FuncInfo.StaticAllocaMap.find(AI);
315 if (SI != FuncInfo.StaticAllocaMap.end()) {
316 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
317 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
319 .addFrameIndex(SI->second)
328 unsigned AArch64FastISel::materializeInt(const ConstantInt *CI, MVT VT) {
333 return fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
335 // Create a copy from the zero register to materialize a "0" value.
336 const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
337 : &AArch64::GPR32RegClass;
338 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
339 unsigned ResultReg = createResultReg(RC);
340 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
341 ResultReg).addReg(ZeroReg, getKillRegState(true));
345 unsigned AArch64FastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
346 // Positive zero (+0.0) has to be materialized with a fmov from the zero
347 // register, because the immediate version of fmov cannot encode zero.
348 if (CFP->isNullValue())
349 return fastMaterializeFloatZero(CFP);
351 if (VT != MVT::f32 && VT != MVT::f64)
354 const APFloat Val = CFP->getValueAPF();
355 bool Is64Bit = (VT == MVT::f64);
356 // This checks to see if we can use FMOV instructions to materialize
357 // a constant, otherwise we have to materialize via the constant pool.
358 if (TLI.isFPImmLegal(Val, VT)) {
360 Is64Bit ? AArch64_AM::getFP64Imm(Val) : AArch64_AM::getFP32Imm(Val);
361 assert((Imm != -1) && "Cannot encode floating-point constant.");
362 unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
363 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
366 // Materialize via constant pool. MachineConstantPool wants an explicit
368 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
370 Align = DL.getTypeAllocSize(CFP->getType());
372 unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
373 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
374 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
375 ADRPReg).addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
377 unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
378 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
379 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
381 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
385 unsigned AArch64FastISel::materializeGV(const GlobalValue *GV) {
386 // We can't handle thread-local variables quickly yet.
387 if (GV->isThreadLocal())
390 // MachO still uses GOT for large code-model accesses, but ELF requires
391 // movz/movk sequences, which FastISel doesn't handle yet.
392 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
395 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
397 EVT DestEVT = TLI.getValueType(GV->getType(), true);
398 if (!DestEVT.isSimple())
401 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
404 if (OpFlags & AArch64II::MO_GOT) {
406 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
408 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
410 ResultReg = createResultReg(&AArch64::GPR64RegClass);
411 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
414 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
416 } else if (OpFlags & AArch64II::MO_CONSTPOOL) {
417 // We can't handle addresses loaded from a constant pool quickly yet.
421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
423 .addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
425 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
426 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
429 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
435 unsigned AArch64FastISel::fastMaterializeConstant(const Constant *C) {
436 EVT CEVT = TLI.getValueType(C->getType(), true);
438 // Only handle simple types.
439 if (!CEVT.isSimple())
441 MVT VT = CEVT.getSimpleVT();
443 if (const auto *CI = dyn_cast<ConstantInt>(C))
444 return materializeInt(CI, VT);
445 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
446 return materializeFP(CFP, VT);
447 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
448 return materializeGV(GV);
453 unsigned AArch64FastISel::fastMaterializeFloatZero(const ConstantFP* CFP) {
454 assert(CFP->isNullValue() &&
455 "Floating-point constant is not a positive zero.");
457 if (!isTypeLegal(CFP->getType(), VT))
460 if (VT != MVT::f32 && VT != MVT::f64)
463 bool Is64Bit = (VT == MVT::f64);
464 unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
465 unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
466 return fastEmitInst_r(Opc, TLI.getRegClassFor(VT), ZReg, /*IsKill=*/true);
469 /// \brief Check if the multiply is by a power-of-2 constant.
470 static bool isMulPowOf2(const Value *I) {
471 if (const auto *MI = dyn_cast<MulOperator>(I)) {
472 if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(0)))
473 if (C->getValue().isPowerOf2())
475 if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(1)))
476 if (C->getValue().isPowerOf2())
482 // Computes the address to get to an object.
483 bool AArch64FastISel::computeAddress(const Value *Obj, Address &Addr, Type *Ty)
485 const User *U = nullptr;
486 unsigned Opcode = Instruction::UserOp1;
487 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
488 // Don't walk into other basic blocks unless the object is an alloca from
489 // another block, otherwise it may not have a virtual register assigned.
490 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
491 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
492 Opcode = I->getOpcode();
495 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
496 Opcode = C->getOpcode();
500 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
501 if (Ty->getAddressSpace() > 255)
502 // Fast instruction selection doesn't support the special
509 case Instruction::BitCast: {
510 // Look through bitcasts.
511 return computeAddress(U->getOperand(0), Addr, Ty);
513 case Instruction::IntToPtr: {
514 // Look past no-op inttoptrs.
515 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
516 return computeAddress(U->getOperand(0), Addr, Ty);
519 case Instruction::PtrToInt: {
520 // Look past no-op ptrtoints.
521 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
522 return computeAddress(U->getOperand(0), Addr, Ty);
525 case Instruction::GetElementPtr: {
526 Address SavedAddr = Addr;
527 uint64_t TmpOffset = Addr.getOffset();
529 // Iterate through the GEP folding the constants into offsets where
531 gep_type_iterator GTI = gep_type_begin(U);
532 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
534 const Value *Op = *i;
535 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
536 const StructLayout *SL = DL.getStructLayout(STy);
537 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
538 TmpOffset += SL->getElementOffset(Idx);
540 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
542 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
543 // Constant-offset addressing.
544 TmpOffset += CI->getSExtValue() * S;
547 if (canFoldAddIntoGEP(U, Op)) {
548 // A compatible add with a constant operand. Fold the constant.
550 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
551 TmpOffset += CI->getSExtValue() * S;
552 // Iterate on the other operand.
553 Op = cast<AddOperator>(Op)->getOperand(0);
557 goto unsupported_gep;
562 // Try to grab the base operand now.
563 Addr.setOffset(TmpOffset);
564 if (computeAddress(U->getOperand(0), Addr, Ty))
567 // We failed, restore everything and try the other options.
573 case Instruction::Alloca: {
574 const AllocaInst *AI = cast<AllocaInst>(Obj);
575 DenseMap<const AllocaInst *, int>::iterator SI =
576 FuncInfo.StaticAllocaMap.find(AI);
577 if (SI != FuncInfo.StaticAllocaMap.end()) {
578 Addr.setKind(Address::FrameIndexBase);
579 Addr.setFI(SI->second);
584 case Instruction::Add: {
585 // Adds of constants are common and easy enough.
586 const Value *LHS = U->getOperand(0);
587 const Value *RHS = U->getOperand(1);
589 if (isa<ConstantInt>(LHS))
592 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
593 Addr.setOffset(Addr.getOffset() + CI->getSExtValue());
594 return computeAddress(LHS, Addr, Ty);
597 Address Backup = Addr;
598 if (computeAddress(LHS, Addr, Ty) && computeAddress(RHS, Addr, Ty))
604 case Instruction::Sub: {
605 // Subs of constants are common and easy enough.
606 const Value *LHS = U->getOperand(0);
607 const Value *RHS = U->getOperand(1);
609 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
610 Addr.setOffset(Addr.getOffset() - CI->getSExtValue());
611 return computeAddress(LHS, Addr, Ty);
615 case Instruction::Shl: {
616 if (Addr.getOffsetReg())
619 const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1));
623 unsigned Val = CI->getZExtValue();
624 if (Val < 1 || Val > 3)
627 uint64_t NumBytes = 0;
628 if (Ty && Ty->isSized()) {
629 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
630 NumBytes = NumBits / 8;
631 if (!isPowerOf2_64(NumBits))
635 if (NumBytes != (1ULL << Val))
639 Addr.setExtendType(AArch64_AM::LSL);
641 const Value *Src = U->getOperand(0);
642 if (const auto *I = dyn_cast<Instruction>(Src))
643 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
646 // Fold the zext or sext when it won't become a noop.
647 if (const auto *ZE = dyn_cast<ZExtInst>(Src)) {
648 if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
649 Addr.setExtendType(AArch64_AM::UXTW);
650 Src = ZE->getOperand(0);
652 } else if (const auto *SE = dyn_cast<SExtInst>(Src)) {
653 if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
654 Addr.setExtendType(AArch64_AM::SXTW);
655 Src = SE->getOperand(0);
659 if (const auto *AI = dyn_cast<BinaryOperator>(Src))
660 if (AI->getOpcode() == Instruction::And) {
661 const Value *LHS = AI->getOperand(0);
662 const Value *RHS = AI->getOperand(1);
664 if (const auto *C = dyn_cast<ConstantInt>(LHS))
665 if (C->getValue() == 0xffffffff)
668 if (const auto *C = dyn_cast<ConstantInt>(RHS))
669 if (C->getValue() == 0xffffffff) {
670 Addr.setExtendType(AArch64_AM::UXTW);
671 unsigned Reg = getRegForValue(LHS);
674 bool RegIsKill = hasTrivialKill(LHS);
675 Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
677 Addr.setOffsetReg(Reg);
682 unsigned Reg = getRegForValue(Src);
685 Addr.setOffsetReg(Reg);
688 case Instruction::Mul: {
689 if (Addr.getOffsetReg())
695 const Value *LHS = U->getOperand(0);
696 const Value *RHS = U->getOperand(1);
698 // Canonicalize power-of-2 value to the RHS.
699 if (const auto *C = dyn_cast<ConstantInt>(LHS))
700 if (C->getValue().isPowerOf2())
703 assert(isa<ConstantInt>(RHS) && "Expected an ConstantInt.");
704 const auto *C = cast<ConstantInt>(RHS);
705 unsigned Val = C->getValue().logBase2();
706 if (Val < 1 || Val > 3)
709 uint64_t NumBytes = 0;
710 if (Ty && Ty->isSized()) {
711 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
712 NumBytes = NumBits / 8;
713 if (!isPowerOf2_64(NumBits))
717 if (NumBytes != (1ULL << Val))
721 Addr.setExtendType(AArch64_AM::LSL);
723 const Value *Src = LHS;
724 if (const auto *I = dyn_cast<Instruction>(Src))
725 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
729 // Fold the zext or sext when it won't become a noop.
730 if (const auto *ZE = dyn_cast<ZExtInst>(Src)) {
731 if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
732 Addr.setExtendType(AArch64_AM::UXTW);
733 Src = ZE->getOperand(0);
735 } else if (const auto *SE = dyn_cast<SExtInst>(Src)) {
736 if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
737 Addr.setExtendType(AArch64_AM::SXTW);
738 Src = SE->getOperand(0);
742 unsigned Reg = getRegForValue(Src);
745 Addr.setOffsetReg(Reg);
748 case Instruction::And: {
749 if (Addr.getOffsetReg())
752 if (DL.getTypeSizeInBits(Ty) != 8)
755 const Value *LHS = U->getOperand(0);
756 const Value *RHS = U->getOperand(1);
758 if (const auto *C = dyn_cast<ConstantInt>(LHS))
759 if (C->getValue() == 0xffffffff)
762 if (const auto *C = dyn_cast<ConstantInt>(RHS))
763 if (C->getValue() == 0xffffffff) {
765 Addr.setExtendType(AArch64_AM::LSL);
766 Addr.setExtendType(AArch64_AM::UXTW);
768 unsigned Reg = getRegForValue(LHS);
771 bool RegIsKill = hasTrivialKill(LHS);
772 Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
774 Addr.setOffsetReg(Reg);
782 if (!Addr.getOffsetReg()) {
783 unsigned Reg = getRegForValue(Obj);
786 Addr.setOffsetReg(Reg);
792 unsigned Reg = getRegForValue(Obj);
799 bool AArch64FastISel::computeCallAddress(const Value *V, Address &Addr) {
800 const User *U = nullptr;
801 unsigned Opcode = Instruction::UserOp1;
804 if (const auto *I = dyn_cast<Instruction>(V)) {
805 Opcode = I->getOpcode();
807 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
808 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
809 Opcode = C->getOpcode();
815 case Instruction::BitCast:
816 // Look past bitcasts if its operand is in the same BB.
818 return computeCallAddress(U->getOperand(0), Addr);
820 case Instruction::IntToPtr:
821 // Look past no-op inttoptrs if its operand is in the same BB.
823 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
824 return computeCallAddress(U->getOperand(0), Addr);
826 case Instruction::PtrToInt:
827 // Look past no-op ptrtoints if its operand is in the same BB.
829 TLI.getValueType(U->getType()) == TLI.getPointerTy())
830 return computeCallAddress(U->getOperand(0), Addr);
834 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
835 Addr.setGlobalValue(GV);
839 // If all else fails, try to materialize the value in a register.
840 if (!Addr.getGlobalValue()) {
841 Addr.setReg(getRegForValue(V));
842 return Addr.getReg() != 0;
849 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
850 EVT evt = TLI.getValueType(Ty, true);
852 // Only handle simple types.
853 if (evt == MVT::Other || !evt.isSimple())
855 VT = evt.getSimpleVT();
857 // This is a legal type, but it's not something we handle in fast-isel.
861 // Handle all other legal types, i.e. a register that will directly hold this
863 return TLI.isTypeLegal(VT);
866 /// \brief Determine if the value type is supported by FastISel.
868 /// FastISel for AArch64 can handle more value types than are legal. This adds
869 /// simple value type such as i1, i8, and i16.
870 bool AArch64FastISel::isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed) {
871 if (Ty->isVectorTy() && !IsVectorAllowed)
874 if (isTypeLegal(Ty, VT))
877 // If this is a type than can be sign or zero-extended to a basic operation
878 // go ahead and accept it now.
879 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
885 bool AArch64FastISel::isValueAvailable(const Value *V) const {
886 if (!isa<Instruction>(V))
889 const auto *I = cast<Instruction>(V);
890 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
896 bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) {
897 unsigned ScaleFactor = getImplicitScaleFactor(VT);
901 bool ImmediateOffsetNeedsLowering = false;
902 bool RegisterOffsetNeedsLowering = false;
903 int64_t Offset = Addr.getOffset();
904 if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
905 ImmediateOffsetNeedsLowering = true;
906 else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
907 !isUInt<12>(Offset / ScaleFactor))
908 ImmediateOffsetNeedsLowering = true;
910 // Cannot encode an offset register and an immediate offset in the same
911 // instruction. Fold the immediate offset into the load/store instruction and
912 // emit an additonal add to take care of the offset register.
913 if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.isRegBase() &&
915 RegisterOffsetNeedsLowering = true;
917 // Cannot encode zero register as base.
918 if (Addr.isRegBase() && Addr.getOffsetReg() && !Addr.getReg())
919 RegisterOffsetNeedsLowering = true;
921 // If this is a stack pointer and the offset needs to be simplified then put
922 // the alloca address into a register, set the base type back to register and
923 // continue. This should almost never happen.
924 if (ImmediateOffsetNeedsLowering && Addr.isFIBase()) {
925 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
926 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
928 .addFrameIndex(Addr.getFI())
931 Addr.setKind(Address::RegBase);
932 Addr.setReg(ResultReg);
935 if (RegisterOffsetNeedsLowering) {
936 unsigned ResultReg = 0;
938 if (Addr.getExtendType() == AArch64_AM::SXTW ||
939 Addr.getExtendType() == AArch64_AM::UXTW )
940 ResultReg = emitAddSub_rx(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
941 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
942 /*TODO:IsKill=*/false, Addr.getExtendType(),
945 ResultReg = emitAddSub_rs(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
946 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
947 /*TODO:IsKill=*/false, AArch64_AM::LSL,
950 if (Addr.getExtendType() == AArch64_AM::UXTW)
951 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
952 /*Op0IsKill=*/false, Addr.getShift(),
954 else if (Addr.getExtendType() == AArch64_AM::SXTW)
955 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
956 /*Op0IsKill=*/false, Addr.getShift(),
959 ResultReg = emitLSL_ri(MVT::i64, MVT::i64, Addr.getOffsetReg(),
960 /*Op0IsKill=*/false, Addr.getShift());
965 Addr.setReg(ResultReg);
966 Addr.setOffsetReg(0);
968 Addr.setExtendType(AArch64_AM::InvalidShiftExtend);
971 // Since the offset is too large for the load/store instruction get the
972 // reg+offset into a register.
973 if (ImmediateOffsetNeedsLowering) {
976 // Try to fold the immediate into the add instruction.
978 ResultReg = emitAddSub_ri(/*UseAdd=*/false, MVT::i64, Addr.getReg(),
979 /*IsKill=*/false, -Offset);
981 ResultReg = emitAddSub_ri(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
982 /*IsKill=*/false, Offset);
984 unsigned ImmReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
985 ResultReg = emitAddSub_rr(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
986 /*IsKill=*/false, ImmReg, /*IsKill=*/true);
989 ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
993 Addr.setReg(ResultReg);
999 void AArch64FastISel::addLoadStoreOperands(Address &Addr,
1000 const MachineInstrBuilder &MIB,
1002 unsigned ScaleFactor,
1003 MachineMemOperand *MMO) {
1004 int64_t Offset = Addr.getOffset() / ScaleFactor;
1005 // Frame base works a bit differently. Handle it separately.
1006 if (Addr.isFIBase()) {
1007 int FI = Addr.getFI();
1008 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
1009 // and alignment should be based on the VT.
1010 MMO = FuncInfo.MF->getMachineMemOperand(
1011 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
1012 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
1013 // Now add the rest of the operands.
1014 MIB.addFrameIndex(FI).addImm(Offset);
1016 assert(Addr.isRegBase() && "Unexpected address kind.");
1017 const MCInstrDesc &II = MIB->getDesc();
1018 unsigned Idx = (Flags & MachineMemOperand::MOStore) ? 1 : 0;
1020 constrainOperandRegClass(II, Addr.getReg(), II.getNumDefs()+Idx));
1022 constrainOperandRegClass(II, Addr.getOffsetReg(), II.getNumDefs()+Idx+1));
1023 if (Addr.getOffsetReg()) {
1024 assert(Addr.getOffset() == 0 && "Unexpected offset");
1025 bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
1026 Addr.getExtendType() == AArch64_AM::SXTX;
1027 MIB.addReg(Addr.getReg());
1028 MIB.addReg(Addr.getOffsetReg());
1029 MIB.addImm(IsSigned);
1030 MIB.addImm(Addr.getShift() != 0);
1032 MIB.addReg(Addr.getReg());
1038 MIB.addMemOperand(MMO);
1041 unsigned AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
1042 const Value *RHS, bool SetFlags,
1043 bool WantResult, bool IsZExt) {
1044 AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
1045 bool NeedExtend = false;
1046 switch (RetVT.SimpleTy) {
1054 ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
1058 ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
1060 case MVT::i32: // fall-through
1065 RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
1067 // Canonicalize immediates to the RHS first.
1068 if (UseAdd && isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1069 std::swap(LHS, RHS);
1071 // Canonicalize mul by power of 2 to the RHS.
1072 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1073 if (isMulPowOf2(LHS))
1074 std::swap(LHS, RHS);
1076 // Canonicalize shift immediate to the RHS.
1077 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1078 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
1079 if (isa<ConstantInt>(SI->getOperand(1)))
1080 if (SI->getOpcode() == Instruction::Shl ||
1081 SI->getOpcode() == Instruction::LShr ||
1082 SI->getOpcode() == Instruction::AShr )
1083 std::swap(LHS, RHS);
1085 unsigned LHSReg = getRegForValue(LHS);
1088 bool LHSIsKill = hasTrivialKill(LHS);
1091 LHSReg = emitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
1093 unsigned ResultReg = 0;
1094 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1095 uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
1096 if (C->isNegative())
1097 ResultReg = emitAddSub_ri(!UseAdd, RetVT, LHSReg, LHSIsKill, -Imm,
1098 SetFlags, WantResult);
1100 ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, Imm, SetFlags,
1106 // Only extend the RHS within the instruction if there is a valid extend type.
1107 if (ExtendType != AArch64_AM::InvalidShiftExtend && RHS->hasOneUse() &&
1108 isValueAvailable(RHS)) {
1109 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
1110 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
1111 if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
1112 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1115 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1116 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1117 RHSIsKill, ExtendType, C->getZExtValue(),
1118 SetFlags, WantResult);
1120 unsigned RHSReg = getRegForValue(RHS);
1123 bool RHSIsKill = hasTrivialKill(RHS);
1124 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1125 ExtendType, 0, SetFlags, WantResult);
1128 // Check if the mul can be folded into the instruction.
1129 if (RHS->hasOneUse() && isValueAvailable(RHS))
1130 if (isMulPowOf2(RHS)) {
1131 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1132 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1134 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1135 if (C->getValue().isPowerOf2())
1136 std::swap(MulLHS, MulRHS);
1138 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1139 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1140 unsigned RHSReg = getRegForValue(MulLHS);
1143 bool RHSIsKill = hasTrivialKill(MulLHS);
1144 return emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1145 AArch64_AM::LSL, ShiftVal, SetFlags, WantResult);
1148 // Check if the shift can be folded into the instruction.
1149 if (RHS->hasOneUse() && isValueAvailable(RHS))
1150 if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
1151 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1152 AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
1153 switch (SI->getOpcode()) {
1155 case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
1156 case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
1157 case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
1159 uint64_t ShiftVal = C->getZExtValue();
1160 if (ShiftType != AArch64_AM::InvalidShiftExtend) {
1161 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1164 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1165 return emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1166 RHSIsKill, ShiftType, ShiftVal, SetFlags,
1172 unsigned RHSReg = getRegForValue(RHS);
1175 bool RHSIsKill = hasTrivialKill(RHS);
1178 RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
1180 return emitAddSub_rr(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1181 SetFlags, WantResult);
1184 unsigned AArch64FastISel::emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
1185 bool LHSIsKill, unsigned RHSReg,
1186 bool RHSIsKill, bool SetFlags,
1188 assert(LHSReg && RHSReg && "Invalid register number.");
1190 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1193 static const unsigned OpcTable[2][2][2] = {
1194 { { AArch64::SUBWrr, AArch64::SUBXrr },
1195 { AArch64::ADDWrr, AArch64::ADDXrr } },
1196 { { AArch64::SUBSWrr, AArch64::SUBSXrr },
1197 { AArch64::ADDSWrr, AArch64::ADDSXrr } }
1199 bool Is64Bit = RetVT == MVT::i64;
1200 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1201 const TargetRegisterClass *RC =
1202 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1205 ResultReg = createResultReg(RC);
1207 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1209 const MCInstrDesc &II = TII.get(Opc);
1210 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1211 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1212 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1213 .addReg(LHSReg, getKillRegState(LHSIsKill))
1214 .addReg(RHSReg, getKillRegState(RHSIsKill));
1218 unsigned AArch64FastISel::emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
1219 bool LHSIsKill, uint64_t Imm,
1220 bool SetFlags, bool WantResult) {
1221 assert(LHSReg && "Invalid register number.");
1223 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1227 if (isUInt<12>(Imm))
1229 else if ((Imm & 0xfff000) == Imm) {
1235 static const unsigned OpcTable[2][2][2] = {
1236 { { AArch64::SUBWri, AArch64::SUBXri },
1237 { AArch64::ADDWri, AArch64::ADDXri } },
1238 { { AArch64::SUBSWri, AArch64::SUBSXri },
1239 { AArch64::ADDSWri, AArch64::ADDSXri } }
1241 bool Is64Bit = RetVT == MVT::i64;
1242 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1243 const TargetRegisterClass *RC;
1245 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1247 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1250 ResultReg = createResultReg(RC);
1252 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1254 const MCInstrDesc &II = TII.get(Opc);
1255 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1256 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1257 .addReg(LHSReg, getKillRegState(LHSIsKill))
1259 .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
1263 unsigned AArch64FastISel::emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
1264 bool LHSIsKill, unsigned RHSReg,
1266 AArch64_AM::ShiftExtendType ShiftType,
1267 uint64_t ShiftImm, bool SetFlags,
1269 assert(LHSReg && RHSReg && "Invalid register number.");
1271 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1274 static const unsigned OpcTable[2][2][2] = {
1275 { { AArch64::SUBWrs, AArch64::SUBXrs },
1276 { AArch64::ADDWrs, AArch64::ADDXrs } },
1277 { { AArch64::SUBSWrs, AArch64::SUBSXrs },
1278 { AArch64::ADDSWrs, AArch64::ADDSXrs } }
1280 bool Is64Bit = RetVT == MVT::i64;
1281 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1282 const TargetRegisterClass *RC =
1283 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1286 ResultReg = createResultReg(RC);
1288 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1290 const MCInstrDesc &II = TII.get(Opc);
1291 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1292 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1293 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1294 .addReg(LHSReg, getKillRegState(LHSIsKill))
1295 .addReg(RHSReg, getKillRegState(RHSIsKill))
1296 .addImm(getShifterImm(ShiftType, ShiftImm));
1300 unsigned AArch64FastISel::emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
1301 bool LHSIsKill, unsigned RHSReg,
1303 AArch64_AM::ShiftExtendType ExtType,
1304 uint64_t ShiftImm, bool SetFlags,
1306 assert(LHSReg && RHSReg && "Invalid register number.");
1308 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1311 static const unsigned OpcTable[2][2][2] = {
1312 { { AArch64::SUBWrx, AArch64::SUBXrx },
1313 { AArch64::ADDWrx, AArch64::ADDXrx } },
1314 { { AArch64::SUBSWrx, AArch64::SUBSXrx },
1315 { AArch64::ADDSWrx, AArch64::ADDSXrx } }
1317 bool Is64Bit = RetVT == MVT::i64;
1318 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1319 const TargetRegisterClass *RC = nullptr;
1321 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1323 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1326 ResultReg = createResultReg(RC);
1328 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1330 const MCInstrDesc &II = TII.get(Opc);
1331 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1332 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1333 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1334 .addReg(LHSReg, getKillRegState(LHSIsKill))
1335 .addReg(RHSReg, getKillRegState(RHSIsKill))
1336 .addImm(getArithExtendImm(ExtType, ShiftImm));
1340 bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1341 Type *Ty = LHS->getType();
1342 EVT EVT = TLI.getValueType(Ty, true);
1343 if (!EVT.isSimple())
1345 MVT VT = EVT.getSimpleVT();
1347 switch (VT.SimpleTy) {
1355 return emitICmp(VT, LHS, RHS, IsZExt);
1358 return emitFCmp(VT, LHS, RHS);
1362 bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1364 return emitSub(RetVT, LHS, RHS, /*SetFlags=*/true, /*WantResult=*/false,
1368 bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1370 return emitAddSub_ri(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, Imm,
1371 /*SetFlags=*/true, /*WantResult=*/false) != 0;
1374 bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1375 if (RetVT != MVT::f32 && RetVT != MVT::f64)
1378 // Check to see if the 2nd operand is a constant that we can encode directly
1380 bool UseImm = false;
1381 if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1382 if (CFP->isZero() && !CFP->isNegative())
1385 unsigned LHSReg = getRegForValue(LHS);
1388 bool LHSIsKill = hasTrivialKill(LHS);
1391 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1392 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1393 .addReg(LHSReg, getKillRegState(LHSIsKill));
1397 unsigned RHSReg = getRegForValue(RHS);
1400 bool RHSIsKill = hasTrivialKill(RHS);
1402 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1403 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1404 .addReg(LHSReg, getKillRegState(LHSIsKill))
1405 .addReg(RHSReg, getKillRegState(RHSIsKill));
1409 unsigned AArch64FastISel::emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
1410 bool SetFlags, bool WantResult, bool IsZExt) {
1411 return emitAddSub(/*UseAdd=*/true, RetVT, LHS, RHS, SetFlags, WantResult,
1415 unsigned AArch64FastISel::emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
1416 bool SetFlags, bool WantResult, bool IsZExt) {
1417 return emitAddSub(/*UseAdd=*/false, RetVT, LHS, RHS, SetFlags, WantResult,
1421 unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1422 bool LHSIsKill, unsigned RHSReg,
1423 bool RHSIsKill, bool WantResult) {
1424 return emitAddSub_rr(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1425 RHSIsKill, /*SetFlags=*/true, WantResult);
1428 unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1429 bool LHSIsKill, unsigned RHSReg,
1431 AArch64_AM::ShiftExtendType ShiftType,
1432 uint64_t ShiftImm, bool WantResult) {
1433 return emitAddSub_rs(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1434 RHSIsKill, ShiftType, ShiftImm, /*SetFlags=*/true,
1438 unsigned AArch64FastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
1439 const Value *LHS, const Value *RHS) {
1440 // Canonicalize immediates to the RHS first.
1441 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1442 std::swap(LHS, RHS);
1444 // Canonicalize mul by power-of-2 to the RHS.
1445 if (LHS->hasOneUse() && isValueAvailable(LHS))
1446 if (isMulPowOf2(LHS))
1447 std::swap(LHS, RHS);
1449 // Canonicalize shift immediate to the RHS.
1450 if (LHS->hasOneUse() && isValueAvailable(LHS))
1451 if (const auto *SI = dyn_cast<ShlOperator>(LHS))
1452 if (isa<ConstantInt>(SI->getOperand(1)))
1453 std::swap(LHS, RHS);
1455 unsigned LHSReg = getRegForValue(LHS);
1458 bool LHSIsKill = hasTrivialKill(LHS);
1460 unsigned ResultReg = 0;
1461 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1462 uint64_t Imm = C->getZExtValue();
1463 ResultReg = emitLogicalOp_ri(ISDOpc, RetVT, LHSReg, LHSIsKill, Imm);
1468 // Check if the mul can be folded into the instruction.
1469 if (RHS->hasOneUse() && isValueAvailable(RHS))
1470 if (isMulPowOf2(RHS)) {
1471 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1472 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1474 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1475 if (C->getValue().isPowerOf2())
1476 std::swap(MulLHS, MulRHS);
1478 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1479 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1481 unsigned RHSReg = getRegForValue(MulLHS);
1484 bool RHSIsKill = hasTrivialKill(MulLHS);
1485 return emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1486 RHSIsKill, ShiftVal);
1489 // Check if the shift can be folded into the instruction.
1490 if (RHS->hasOneUse() && isValueAvailable(RHS))
1491 if (const auto *SI = dyn_cast<ShlOperator>(RHS))
1492 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1493 uint64_t ShiftVal = C->getZExtValue();
1494 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1497 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1498 return emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1499 RHSIsKill, ShiftVal);
1502 unsigned RHSReg = getRegForValue(RHS);
1505 bool RHSIsKill = hasTrivialKill(RHS);
1507 MVT VT = std::max(MVT::i32, RetVT.SimpleTy);
1508 ResultReg = fastEmit_rr(VT, VT, ISDOpc, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1509 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1510 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1511 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1516 unsigned AArch64FastISel::emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT,
1517 unsigned LHSReg, bool LHSIsKill,
1519 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1520 "ISD nodes are not consecutive!");
1521 static const unsigned OpcTable[3][2] = {
1522 { AArch64::ANDWri, AArch64::ANDXri },
1523 { AArch64::ORRWri, AArch64::ORRXri },
1524 { AArch64::EORWri, AArch64::EORXri }
1526 const TargetRegisterClass *RC;
1529 switch (RetVT.SimpleTy) {
1536 unsigned Idx = ISDOpc - ISD::AND;
1537 Opc = OpcTable[Idx][0];
1538 RC = &AArch64::GPR32spRegClass;
1543 Opc = OpcTable[ISDOpc - ISD::AND][1];
1544 RC = &AArch64::GPR64spRegClass;
1549 if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
1552 unsigned ResultReg =
1553 fastEmitInst_ri(Opc, RC, LHSReg, LHSIsKill,
1554 AArch64_AM::encodeLogicalImmediate(Imm, RegSize));
1555 if (RetVT >= MVT::i8 && RetVT <= MVT::i16 && ISDOpc != ISD::AND) {
1556 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1557 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1562 unsigned AArch64FastISel::emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT,
1563 unsigned LHSReg, bool LHSIsKill,
1564 unsigned RHSReg, bool RHSIsKill,
1565 uint64_t ShiftImm) {
1566 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1567 "ISD nodes are not consecutive!");
1568 static const unsigned OpcTable[3][2] = {
1569 { AArch64::ANDWrs, AArch64::ANDXrs },
1570 { AArch64::ORRWrs, AArch64::ORRXrs },
1571 { AArch64::EORWrs, AArch64::EORXrs }
1573 const TargetRegisterClass *RC;
1575 switch (RetVT.SimpleTy) {
1582 Opc = OpcTable[ISDOpc - ISD::AND][0];
1583 RC = &AArch64::GPR32RegClass;
1586 Opc = OpcTable[ISDOpc - ISD::AND][1];
1587 RC = &AArch64::GPR64RegClass;
1590 unsigned ResultReg =
1591 fastEmitInst_rri(Opc, RC, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1592 AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftImm));
1593 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1594 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1595 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1600 unsigned AArch64FastISel::emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1602 return emitLogicalOp_ri(ISD::AND, RetVT, LHSReg, LHSIsKill, Imm);
1605 bool AArch64FastISel::emitLoad(MVT VT, MVT RetVT, unsigned &ResultReg,
1606 Address Addr, bool WantZExt,
1607 MachineMemOperand *MMO) {
1608 // Simplify this down to something we can handle.
1609 if (!simplifyAddress(Addr, VT))
1612 unsigned ScaleFactor = getImplicitScaleFactor(VT);
1614 llvm_unreachable("Unexpected value type.");
1616 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1617 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1618 bool UseScaled = true;
1619 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1624 static const unsigned GPOpcTable[2][8][4] = {
1626 { { AArch64::LDURSBWi, AArch64::LDURSHWi, AArch64::LDURWi,
1628 { AArch64::LDURSBXi, AArch64::LDURSHXi, AArch64::LDURSWi,
1630 { AArch64::LDRSBWui, AArch64::LDRSHWui, AArch64::LDRWui,
1632 { AArch64::LDRSBXui, AArch64::LDRSHXui, AArch64::LDRSWui,
1634 { AArch64::LDRSBWroX, AArch64::LDRSHWroX, AArch64::LDRWroX,
1636 { AArch64::LDRSBXroX, AArch64::LDRSHXroX, AArch64::LDRSWroX,
1638 { AArch64::LDRSBWroW, AArch64::LDRSHWroW, AArch64::LDRWroW,
1640 { AArch64::LDRSBXroW, AArch64::LDRSHXroW, AArch64::LDRSWroW,
1644 { { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1646 { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1648 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1650 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1652 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1654 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1656 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1658 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1663 static const unsigned FPOpcTable[4][2] = {
1664 { AArch64::LDURSi, AArch64::LDURDi },
1665 { AArch64::LDRSui, AArch64::LDRDui },
1666 { AArch64::LDRSroX, AArch64::LDRDroX },
1667 { AArch64::LDRSroW, AArch64::LDRDroW }
1671 const TargetRegisterClass *RC;
1672 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1673 Addr.getOffsetReg();
1674 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1675 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1676 Addr.getExtendType() == AArch64_AM::SXTW)
1679 bool IsRet64Bit = RetVT == MVT::i64;
1680 switch (VT.SimpleTy) {
1682 llvm_unreachable("Unexpected value type.");
1683 case MVT::i1: // Intentional fall-through.
1685 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][0];
1686 RC = (IsRet64Bit && !WantZExt) ?
1687 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1690 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][1];
1691 RC = (IsRet64Bit && !WantZExt) ?
1692 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1695 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][2];
1696 RC = (IsRet64Bit && !WantZExt) ?
1697 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1700 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][3];
1701 RC = &AArch64::GPR64RegClass;
1704 Opc = FPOpcTable[Idx][0];
1705 RC = &AArch64::FPR32RegClass;
1708 Opc = FPOpcTable[Idx][1];
1709 RC = &AArch64::FPR64RegClass;
1713 // Create the base instruction, then add the operands.
1714 ResultReg = createResultReg(RC);
1715 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1716 TII.get(Opc), ResultReg);
1717 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
1719 // For zero-extending loads to 64bit we emit a 32bit load and then convert
1720 // the w-reg to an x-reg. In the end this is just an noop and will be removed.
1721 if (WantZExt && RetVT == MVT::i64 && VT <= MVT::i32) {
1722 unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
1723 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1724 TII.get(AArch64::SUBREG_TO_REG), Reg64)
1726 .addReg(ResultReg, getKillRegState(true))
1727 .addImm(AArch64::sub_32);
1731 // Loading an i1 requires special handling.
1732 if (VT == MVT::i1) {
1733 unsigned ANDReg = emitAnd_ri(IsRet64Bit ? MVT::i64 : MVT::i32, ResultReg,
1734 /*IsKill=*/true, 1);
1735 assert(ANDReg && "Unexpected AND instruction emission failure.");
1741 bool AArch64FastISel::selectAddSub(const Instruction *I) {
1743 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1747 return selectOperator(I, I->getOpcode());
1750 switch (I->getOpcode()) {
1752 llvm_unreachable("Unexpected instruction.");
1753 case Instruction::Add:
1754 ResultReg = emitAdd(VT, I->getOperand(0), I->getOperand(1));
1756 case Instruction::Sub:
1757 ResultReg = emitSub(VT, I->getOperand(0), I->getOperand(1));
1763 updateValueMap(I, ResultReg);
1767 bool AArch64FastISel::selectLogicalOp(const Instruction *I) {
1769 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1773 return selectOperator(I, I->getOpcode());
1776 switch (I->getOpcode()) {
1778 llvm_unreachable("Unexpected instruction.");
1779 case Instruction::And:
1780 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
1782 case Instruction::Or:
1783 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
1785 case Instruction::Xor:
1786 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
1792 updateValueMap(I, ResultReg);
1796 bool AArch64FastISel::selectLoad(const Instruction *I) {
1798 // Verify we have a legal type before going any further. Currently, we handle
1799 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1800 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1801 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true) ||
1802 cast<LoadInst>(I)->isAtomic())
1805 // See if we can handle this address.
1807 if (!computeAddress(I->getOperand(0), Addr, I->getType()))
1810 bool WantZExt = true;
1812 if (I->hasOneUse()) {
1813 if (const auto *ZE = dyn_cast<ZExtInst>(I->use_begin()->getUser())) {
1814 if (!isTypeSupported(ZE->getType(), RetVT, /*IsVectorAllowed=*/false))
1816 } else if (const auto *SE = dyn_cast<SExtInst>(I->use_begin()->getUser())) {
1817 if (!isTypeSupported(SE->getType(), RetVT, /*IsVectorAllowed=*/false))
1824 if (!emitLoad(VT, RetVT, ResultReg, Addr, WantZExt,
1825 createMachineMemOperandFor(I)))
1828 updateValueMap(I, ResultReg);
1832 bool AArch64FastISel::emitStore(MVT VT, unsigned SrcReg, Address Addr,
1833 MachineMemOperand *MMO) {
1834 // Simplify this down to something we can handle.
1835 if (!simplifyAddress(Addr, VT))
1838 unsigned ScaleFactor = getImplicitScaleFactor(VT);
1840 llvm_unreachable("Unexpected value type.");
1842 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1843 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1844 bool UseScaled = true;
1845 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1850 static const unsigned OpcTable[4][6] = {
1851 { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
1852 AArch64::STURSi, AArch64::STURDi },
1853 { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
1854 AArch64::STRSui, AArch64::STRDui },
1855 { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
1856 AArch64::STRSroX, AArch64::STRDroX },
1857 { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
1858 AArch64::STRSroW, AArch64::STRDroW }
1862 bool VTIsi1 = false;
1863 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1864 Addr.getOffsetReg();
1865 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1866 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1867 Addr.getExtendType() == AArch64_AM::SXTW)
1870 switch (VT.SimpleTy) {
1871 default: llvm_unreachable("Unexpected value type.");
1872 case MVT::i1: VTIsi1 = true;
1873 case MVT::i8: Opc = OpcTable[Idx][0]; break;
1874 case MVT::i16: Opc = OpcTable[Idx][1]; break;
1875 case MVT::i32: Opc = OpcTable[Idx][2]; break;
1876 case MVT::i64: Opc = OpcTable[Idx][3]; break;
1877 case MVT::f32: Opc = OpcTable[Idx][4]; break;
1878 case MVT::f64: Opc = OpcTable[Idx][5]; break;
1881 // Storing an i1 requires special handling.
1882 if (VTIsi1 && SrcReg != AArch64::WZR) {
1883 unsigned ANDReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
1884 assert(ANDReg && "Unexpected AND instruction emission failure.");
1887 // Create the base instruction, then add the operands.
1888 const MCInstrDesc &II = TII.get(Opc);
1889 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
1890 MachineInstrBuilder MIB =
1891 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(SrcReg);
1892 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
1897 bool AArch64FastISel::selectStore(const Instruction *I) {
1899 const Value *Op0 = I->getOperand(0);
1900 // Verify we have a legal type before going any further. Currently, we handle
1901 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1902 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1903 if (!isTypeSupported(Op0->getType(), VT, /*IsVectorAllowed=*/true) ||
1904 cast<StoreInst>(I)->isAtomic())
1907 // Get the value to be stored into a register. Use the zero register directly
1908 // when possible to avoid an unnecessary copy and a wasted register.
1909 unsigned SrcReg = 0;
1910 if (const auto *CI = dyn_cast<ConstantInt>(Op0)) {
1912 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
1913 } else if (const auto *CF = dyn_cast<ConstantFP>(Op0)) {
1914 if (CF->isZero() && !CF->isNegative()) {
1915 VT = MVT::getIntegerVT(VT.getSizeInBits());
1916 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
1921 SrcReg = getRegForValue(Op0);
1926 // See if we can handle this address.
1928 if (!computeAddress(I->getOperand(1), Addr, I->getOperand(0)->getType()))
1931 if (!emitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
1936 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
1938 case CmpInst::FCMP_ONE:
1939 case CmpInst::FCMP_UEQ:
1941 // AL is our "false" for now. The other two need more compares.
1942 return AArch64CC::AL;
1943 case CmpInst::ICMP_EQ:
1944 case CmpInst::FCMP_OEQ:
1945 return AArch64CC::EQ;
1946 case CmpInst::ICMP_SGT:
1947 case CmpInst::FCMP_OGT:
1948 return AArch64CC::GT;
1949 case CmpInst::ICMP_SGE:
1950 case CmpInst::FCMP_OGE:
1951 return AArch64CC::GE;
1952 case CmpInst::ICMP_UGT:
1953 case CmpInst::FCMP_UGT:
1954 return AArch64CC::HI;
1955 case CmpInst::FCMP_OLT:
1956 return AArch64CC::MI;
1957 case CmpInst::ICMP_ULE:
1958 case CmpInst::FCMP_OLE:
1959 return AArch64CC::LS;
1960 case CmpInst::FCMP_ORD:
1961 return AArch64CC::VC;
1962 case CmpInst::FCMP_UNO:
1963 return AArch64CC::VS;
1964 case CmpInst::FCMP_UGE:
1965 return AArch64CC::PL;
1966 case CmpInst::ICMP_SLT:
1967 case CmpInst::FCMP_ULT:
1968 return AArch64CC::LT;
1969 case CmpInst::ICMP_SLE:
1970 case CmpInst::FCMP_ULE:
1971 return AArch64CC::LE;
1972 case CmpInst::FCMP_UNE:
1973 case CmpInst::ICMP_NE:
1974 return AArch64CC::NE;
1975 case CmpInst::ICMP_UGE:
1976 return AArch64CC::HS;
1977 case CmpInst::ICMP_ULT:
1978 return AArch64CC::LO;
1982 /// \brief Try to emit a combined compare-and-branch instruction.
1983 bool AArch64FastISel::emitCompareAndBranch(const BranchInst *BI) {
1984 assert(isa<CmpInst>(BI->getCondition()) && "Expected cmp instruction");
1985 const CmpInst *CI = cast<CmpInst>(BI->getCondition());
1986 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1988 const Value *LHS = CI->getOperand(0);
1989 const Value *RHS = CI->getOperand(1);
1991 Type *Ty = LHS->getType();
1992 if (!Ty->isIntegerTy())
1995 unsigned BW = cast<IntegerType>(Ty)->getBitWidth();
1996 if (BW != 1 && BW != 8 && BW != 16 && BW != 32 && BW != 64)
1999 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2000 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2002 // Try to take advantage of fallthrough opportunities.
2003 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2004 std::swap(TBB, FBB);
2005 Predicate = CmpInst::getInversePredicate(Predicate);
2010 if ((Predicate == CmpInst::ICMP_EQ) || (Predicate == CmpInst::ICMP_NE)) {
2011 if (const auto *C = dyn_cast<ConstantInt>(LHS))
2012 if (C->isNullValue())
2013 std::swap(LHS, RHS);
2015 if (!isa<ConstantInt>(RHS))
2018 if (!cast<ConstantInt>(RHS)->isNullValue())
2021 if (const auto *AI = dyn_cast<BinaryOperator>(LHS))
2022 if (AI->getOpcode() == Instruction::And) {
2023 const Value *AndLHS = AI->getOperand(0);
2024 const Value *AndRHS = AI->getOperand(1);
2026 if (const auto *C = dyn_cast<ConstantInt>(AndLHS))
2027 if (C->getValue().isPowerOf2())
2028 std::swap(AndLHS, AndRHS);
2030 if (const auto *C = dyn_cast<ConstantInt>(AndRHS))
2031 if (C->getValue().isPowerOf2()) {
2032 TestBit = C->getValue().logBase2();
2036 IsCmpNE = Predicate == CmpInst::ICMP_NE;
2037 } else if (Predicate == CmpInst::ICMP_SLT) {
2038 if (!isa<ConstantInt>(RHS))
2041 if (!cast<ConstantInt>(RHS)->isNullValue())
2046 } else if (Predicate == CmpInst::ICMP_SGT) {
2047 if (!isa<ConstantInt>(RHS))
2050 if (cast<ConstantInt>(RHS)->getValue() != -1)
2058 static const unsigned OpcTable[2][2][2] = {
2059 { {AArch64::CBZW, AArch64::CBZX },
2060 {AArch64::CBNZW, AArch64::CBNZX} },
2061 { {AArch64::TBZW, AArch64::TBZX },
2062 {AArch64::TBNZW, AArch64::TBNZX} }
2065 bool IsBitTest = TestBit != -1;
2066 bool Is64Bit = BW == 64;
2067 if (TestBit < 32 && TestBit >= 0)
2070 unsigned Opc = OpcTable[IsBitTest][IsCmpNE][Is64Bit];
2071 const MCInstrDesc &II = TII.get(Opc);
2073 unsigned SrcReg = getRegForValue(LHS);
2076 bool SrcIsKill = hasTrivialKill(LHS);
2078 if (BW == 64 && !Is64Bit) {
2079 SrcReg = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
2081 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
2084 // Emit the combined compare and branch instruction.
2085 MachineInstrBuilder MIB =
2086 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
2087 .addReg(SrcReg, getKillRegState(SrcIsKill));
2089 MIB.addImm(TestBit);
2092 // Obtain the branch weight and add the TrueBB to the successor list.
2093 uint32_t BranchWeight = 0;
2095 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2096 TBB->getBasicBlock());
2097 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2098 fastEmitBranch(FBB, DbgLoc);
2103 bool AArch64FastISel::selectBranch(const Instruction *I) {
2104 const BranchInst *BI = cast<BranchInst>(I);
2105 if (BI->isUnconditional()) {
2106 MachineBasicBlock *MSucc = FuncInfo.MBBMap[BI->getSuccessor(0)];
2107 fastEmitBranch(MSucc, BI->getDebugLoc());
2111 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2112 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2114 AArch64CC::CondCode CC = AArch64CC::NE;
2115 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
2116 if (CI->hasOneUse() && isValueAvailable(CI)) {
2117 // Try to optimize or fold the cmp.
2118 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2119 switch (Predicate) {
2122 case CmpInst::FCMP_FALSE:
2123 fastEmitBranch(FBB, DbgLoc);
2125 case CmpInst::FCMP_TRUE:
2126 fastEmitBranch(TBB, DbgLoc);
2130 // Try to emit a combined compare-and-branch first.
2131 if (emitCompareAndBranch(BI))
2134 // Try to take advantage of fallthrough opportunities.
2135 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2136 std::swap(TBB, FBB);
2137 Predicate = CmpInst::getInversePredicate(Predicate);
2141 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2144 // FCMP_UEQ and FCMP_ONE cannot be checked with a single branch
2146 CC = getCompareCC(Predicate);
2147 AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2148 switch (Predicate) {
2151 case CmpInst::FCMP_UEQ:
2152 ExtraCC = AArch64CC::EQ;
2155 case CmpInst::FCMP_ONE:
2156 ExtraCC = AArch64CC::MI;
2160 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2162 // Emit the extra branch for FCMP_UEQ and FCMP_ONE.
2163 if (ExtraCC != AArch64CC::AL) {
2164 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2170 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2174 // Obtain the branch weight and add the TrueBB to the successor list.
2175 uint32_t BranchWeight = 0;
2177 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2178 TBB->getBasicBlock());
2179 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2181 fastEmitBranch(FBB, DbgLoc);
2184 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
2186 if (TI->hasOneUse() && isValueAvailable(TI) &&
2187 isTypeSupported(TI->getOperand(0)->getType(), SrcVT)) {
2188 unsigned CondReg = getRegForValue(TI->getOperand(0));
2191 bool CondIsKill = hasTrivialKill(TI->getOperand(0));
2193 // Issue an extract_subreg to get the lower 32-bits.
2194 if (SrcVT == MVT::i64) {
2195 CondReg = fastEmitInst_extractsubreg(MVT::i32, CondReg, CondIsKill,
2200 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
2201 assert(ANDReg && "Unexpected AND instruction emission failure.");
2202 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2204 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2205 std::swap(TBB, FBB);
2208 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2212 // Obtain the branch weight and add the TrueBB to the successor list.
2213 uint32_t BranchWeight = 0;
2215 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2216 TBB->getBasicBlock());
2217 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2219 fastEmitBranch(FBB, DbgLoc);
2222 } else if (const auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
2223 uint64_t Imm = CI->getZExtValue();
2224 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
2225 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
2228 // Obtain the branch weight and add the target to the successor list.
2229 uint32_t BranchWeight = 0;
2231 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2232 Target->getBasicBlock());
2233 FuncInfo.MBB->addSuccessor(Target, BranchWeight);
2235 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
2236 // Fake request the condition, otherwise the intrinsic might be completely
2238 unsigned CondReg = getRegForValue(BI->getCondition());
2243 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2247 // Obtain the branch weight and add the TrueBB to the successor list.
2248 uint32_t BranchWeight = 0;
2250 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2251 TBB->getBasicBlock());
2252 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2254 fastEmitBranch(FBB, DbgLoc);
2258 unsigned CondReg = getRegForValue(BI->getCondition());
2261 bool CondRegIsKill = hasTrivialKill(BI->getCondition());
2263 // We've been divorced from our compare! Our block was split, and
2264 // now our compare lives in a predecessor block. We musn't
2265 // re-compare here, as the children of the compare aren't guaranteed
2266 // live across the block boundary (we *could* check for this).
2267 // Regardless, the compare has been done in the predecessor block,
2268 // and it left a value for us in a virtual register. Ergo, we test
2269 // the one-bit value left in the virtual register.
2270 emitICmp_ri(MVT::i32, CondReg, CondRegIsKill, 0);
2272 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2273 std::swap(TBB, FBB);
2277 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2281 // Obtain the branch weight and add the TrueBB to the successor list.
2282 uint32_t BranchWeight = 0;
2284 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2285 TBB->getBasicBlock());
2286 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2288 fastEmitBranch(FBB, DbgLoc);
2292 bool AArch64FastISel::selectIndirectBr(const Instruction *I) {
2293 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
2294 unsigned AddrReg = getRegForValue(BI->getOperand(0));
2298 // Emit the indirect branch.
2299 const MCInstrDesc &II = TII.get(AArch64::BR);
2300 AddrReg = constrainOperandRegClass(II, AddrReg, II.getNumDefs());
2301 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(AddrReg);
2303 // Make sure the CFG is up-to-date.
2304 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
2305 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
2310 bool AArch64FastISel::selectCmp(const Instruction *I) {
2311 const CmpInst *CI = cast<CmpInst>(I);
2313 // Try to optimize or fold the cmp.
2314 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2315 unsigned ResultReg = 0;
2316 switch (Predicate) {
2319 case CmpInst::FCMP_FALSE:
2320 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2321 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2322 TII.get(TargetOpcode::COPY), ResultReg)
2323 .addReg(AArch64::WZR, getKillRegState(true));
2325 case CmpInst::FCMP_TRUE:
2326 ResultReg = fastEmit_i(MVT::i32, MVT::i32, ISD::Constant, 1);
2331 updateValueMap(I, ResultReg);
2336 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2339 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2341 // FCMP_UEQ and FCMP_ONE cannot be checked with a single instruction. These
2342 // condition codes are inverted, because they are used by CSINC.
2343 static unsigned CondCodeTable[2][2] = {
2344 { AArch64CC::NE, AArch64CC::VC },
2345 { AArch64CC::PL, AArch64CC::LE }
2347 unsigned *CondCodes = nullptr;
2348 switch (Predicate) {
2351 case CmpInst::FCMP_UEQ:
2352 CondCodes = &CondCodeTable[0][0];
2354 case CmpInst::FCMP_ONE:
2355 CondCodes = &CondCodeTable[1][0];
2360 unsigned TmpReg1 = createResultReg(&AArch64::GPR32RegClass);
2361 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2363 .addReg(AArch64::WZR, getKillRegState(true))
2364 .addReg(AArch64::WZR, getKillRegState(true))
2365 .addImm(CondCodes[0]);
2366 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2368 .addReg(TmpReg1, getKillRegState(true))
2369 .addReg(AArch64::WZR, getKillRegState(true))
2370 .addImm(CondCodes[1]);
2372 updateValueMap(I, ResultReg);
2376 // Now set a register based on the comparison.
2377 AArch64CC::CondCode CC = getCompareCC(Predicate);
2378 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2379 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
2380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2382 .addReg(AArch64::WZR, getKillRegState(true))
2383 .addReg(AArch64::WZR, getKillRegState(true))
2384 .addImm(invertedCC);
2386 updateValueMap(I, ResultReg);
2390 bool AArch64FastISel::selectSelect(const Instruction *I) {
2391 const SelectInst *SI = cast<SelectInst>(I);
2393 EVT DestEVT = TLI.getValueType(SI->getType(), true);
2394 if (!DestEVT.isSimple())
2397 MVT DestVT = DestEVT.getSimpleVT();
2398 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
2403 const TargetRegisterClass *RC = nullptr;
2404 switch (DestVT.SimpleTy) {
2405 default: return false;
2407 SelectOpc = AArch64::CSELWr; RC = &AArch64::GPR32RegClass; break;
2409 SelectOpc = AArch64::CSELXr; RC = &AArch64::GPR64RegClass; break;
2411 SelectOpc = AArch64::FCSELSrrr; RC = &AArch64::FPR32RegClass; break;
2413 SelectOpc = AArch64::FCSELDrrr; RC = &AArch64::FPR64RegClass; break;
2416 const Value *Cond = SI->getCondition();
2417 bool NeedTest = true;
2418 AArch64CC::CondCode CC = AArch64CC::NE;
2419 if (foldXALUIntrinsic(CC, I, Cond))
2422 unsigned CondReg = getRegForValue(Cond);
2425 bool CondIsKill = hasTrivialKill(Cond);
2428 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
2429 assert(ANDReg && "Unexpected AND instruction emission failure.");
2430 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2433 unsigned TrueReg = getRegForValue(SI->getTrueValue());
2434 bool TrueIsKill = hasTrivialKill(SI->getTrueValue());
2436 unsigned FalseReg = getRegForValue(SI->getFalseValue());
2437 bool FalseIsKill = hasTrivialKill(SI->getFalseValue());
2439 if (!TrueReg || !FalseReg)
2442 unsigned ResultReg = fastEmitInst_rri(SelectOpc, RC, TrueReg, TrueIsKill,
2443 FalseReg, FalseIsKill, CC);
2444 updateValueMap(I, ResultReg);
2448 bool AArch64FastISel::selectFPExt(const Instruction *I) {
2449 Value *V = I->getOperand(0);
2450 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
2453 unsigned Op = getRegForValue(V);
2457 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
2458 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
2459 ResultReg).addReg(Op);
2460 updateValueMap(I, ResultReg);
2464 bool AArch64FastISel::selectFPTrunc(const Instruction *I) {
2465 Value *V = I->getOperand(0);
2466 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
2469 unsigned Op = getRegForValue(V);
2473 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
2474 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
2475 ResultReg).addReg(Op);
2476 updateValueMap(I, ResultReg);
2480 // FPToUI and FPToSI
2481 bool AArch64FastISel::selectFPToInt(const Instruction *I, bool Signed) {
2483 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2486 unsigned SrcReg = getRegForValue(I->getOperand(0));
2490 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2491 if (SrcVT == MVT::f128)
2495 if (SrcVT == MVT::f64) {
2497 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
2499 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
2502 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
2504 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
2506 unsigned ResultReg = createResultReg(
2507 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
2508 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2510 updateValueMap(I, ResultReg);
2514 bool AArch64FastISel::selectIntToFP(const Instruction *I, bool Signed) {
2516 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2518 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
2519 "Unexpected value type.");
2521 unsigned SrcReg = getRegForValue(I->getOperand(0));
2524 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
2526 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2528 // Handle sign-extension.
2529 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
2531 emitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
2538 if (SrcVT == MVT::i64) {
2540 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
2542 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
2545 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
2547 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
2550 unsigned ResultReg = fastEmitInst_r(Opc, TLI.getRegClassFor(DestVT), SrcReg,
2552 updateValueMap(I, ResultReg);
2556 bool AArch64FastISel::fastLowerArguments() {
2557 if (!FuncInfo.CanLowerReturn)
2560 const Function *F = FuncInfo.Fn;
2564 CallingConv::ID CC = F->getCallingConv();
2565 if (CC != CallingConv::C)
2568 // Only handle simple cases of up to 8 GPR and FPR each.
2569 unsigned GPRCnt = 0;
2570 unsigned FPRCnt = 0;
2572 for (auto const &Arg : F->args()) {
2573 // The first argument is at index 1.
2575 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2576 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2577 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2578 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2581 Type *ArgTy = Arg.getType();
2582 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
2585 EVT ArgVT = TLI.getValueType(ArgTy);
2586 if (!ArgVT.isSimple())
2589 MVT VT = ArgVT.getSimpleVT().SimpleTy;
2590 if (VT.isFloatingPoint() && !Subtarget->hasFPARMv8())
2593 if (VT.isVector() &&
2594 (!Subtarget->hasNEON() || !Subtarget->isLittleEndian()))
2597 if (VT >= MVT::i1 && VT <= MVT::i64)
2599 else if ((VT >= MVT::f16 && VT <= MVT::f64) || VT.is64BitVector() ||
2600 VT.is128BitVector())
2605 if (GPRCnt > 8 || FPRCnt > 8)
2609 static const MCPhysReg Registers[6][8] = {
2610 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
2611 AArch64::W5, AArch64::W6, AArch64::W7 },
2612 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
2613 AArch64::X5, AArch64::X6, AArch64::X7 },
2614 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
2615 AArch64::H5, AArch64::H6, AArch64::H7 },
2616 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
2617 AArch64::S5, AArch64::S6, AArch64::S7 },
2618 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
2619 AArch64::D5, AArch64::D6, AArch64::D7 },
2620 { AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, AArch64::Q4,
2621 AArch64::Q5, AArch64::Q6, AArch64::Q7 }
2624 unsigned GPRIdx = 0;
2625 unsigned FPRIdx = 0;
2626 for (auto const &Arg : F->args()) {
2627 MVT VT = TLI.getSimpleValueType(Arg.getType());
2629 const TargetRegisterClass *RC;
2630 if (VT >= MVT::i1 && VT <= MVT::i32) {
2631 SrcReg = Registers[0][GPRIdx++];
2632 RC = &AArch64::GPR32RegClass;
2634 } else if (VT == MVT::i64) {
2635 SrcReg = Registers[1][GPRIdx++];
2636 RC = &AArch64::GPR64RegClass;
2637 } else if (VT == MVT::f16) {
2638 SrcReg = Registers[2][FPRIdx++];
2639 RC = &AArch64::FPR16RegClass;
2640 } else if (VT == MVT::f32) {
2641 SrcReg = Registers[3][FPRIdx++];
2642 RC = &AArch64::FPR32RegClass;
2643 } else if ((VT == MVT::f64) || VT.is64BitVector()) {
2644 SrcReg = Registers[4][FPRIdx++];
2645 RC = &AArch64::FPR64RegClass;
2646 } else if (VT.is128BitVector()) {
2647 SrcReg = Registers[5][FPRIdx++];
2648 RC = &AArch64::FPR128RegClass;
2650 llvm_unreachable("Unexpected value type.");
2652 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2653 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2654 // Without this, EmitLiveInCopies may eliminate the livein if its only
2655 // use is a bitcast (which isn't turned into an instruction).
2656 unsigned ResultReg = createResultReg(RC);
2657 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2658 TII.get(TargetOpcode::COPY), ResultReg)
2659 .addReg(DstReg, getKillRegState(true));
2660 updateValueMap(&Arg, ResultReg);
2665 bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
2666 SmallVectorImpl<MVT> &OutVTs,
2667 unsigned &NumBytes) {
2668 CallingConv::ID CC = CLI.CallConv;
2669 SmallVector<CCValAssign, 16> ArgLocs;
2670 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
2671 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
2673 // Get a count of how many bytes are to be pushed on the stack.
2674 NumBytes = CCInfo.getNextStackOffset();
2676 // Issue CALLSEQ_START
2677 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
2678 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
2681 // Process the args.
2682 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2683 CCValAssign &VA = ArgLocs[i];
2684 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
2685 MVT ArgVT = OutVTs[VA.getValNo()];
2687 unsigned ArgReg = getRegForValue(ArgVal);
2691 // Handle arg promotion: SExt, ZExt, AExt.
2692 switch (VA.getLocInfo()) {
2693 case CCValAssign::Full:
2695 case CCValAssign::SExt: {
2696 MVT DestVT = VA.getLocVT();
2698 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
2703 case CCValAssign::AExt:
2704 // Intentional fall-through.
2705 case CCValAssign::ZExt: {
2706 MVT DestVT = VA.getLocVT();
2708 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
2714 llvm_unreachable("Unknown arg promotion!");
2717 // Now copy/store arg to correct locations.
2718 if (VA.isRegLoc() && !VA.needsCustom()) {
2719 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2720 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
2721 CLI.OutRegs.push_back(VA.getLocReg());
2722 } else if (VA.needsCustom()) {
2723 // FIXME: Handle custom args.
2726 assert(VA.isMemLoc() && "Assuming store on stack.");
2728 // Don't emit stores for undef values.
2729 if (isa<UndefValue>(ArgVal))
2732 // Need to store on the stack.
2733 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
2735 unsigned BEAlign = 0;
2736 if (ArgSize < 8 && !Subtarget->isLittleEndian())
2737 BEAlign = 8 - ArgSize;
2740 Addr.setKind(Address::RegBase);
2741 Addr.setReg(AArch64::SP);
2742 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
2744 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
2745 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2746 MachinePointerInfo::getStack(Addr.getOffset()),
2747 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
2749 if (!emitStore(ArgVT, ArgReg, Addr, MMO))
2756 bool AArch64FastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
2757 unsigned NumBytes) {
2758 CallingConv::ID CC = CLI.CallConv;
2760 // Issue CALLSEQ_END
2761 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2762 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
2763 .addImm(NumBytes).addImm(0);
2765 // Now the return value.
2766 if (RetVT != MVT::isVoid) {
2767 SmallVector<CCValAssign, 16> RVLocs;
2768 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
2769 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
2771 // Only handle a single return value.
2772 if (RVLocs.size() != 1)
2775 // Copy all of the result registers out of their specified physreg.
2776 MVT CopyVT = RVLocs[0].getValVT();
2777 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
2778 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2779 TII.get(TargetOpcode::COPY), ResultReg)
2780 .addReg(RVLocs[0].getLocReg());
2781 CLI.InRegs.push_back(RVLocs[0].getLocReg());
2783 CLI.ResultReg = ResultReg;
2784 CLI.NumResultRegs = 1;
2790 bool AArch64FastISel::fastLowerCall(CallLoweringInfo &CLI) {
2791 CallingConv::ID CC = CLI.CallConv;
2792 bool IsTailCall = CLI.IsTailCall;
2793 bool IsVarArg = CLI.IsVarArg;
2794 const Value *Callee = CLI.Callee;
2795 const char *SymName = CLI.SymName;
2797 if (!Callee && !SymName)
2800 // Allow SelectionDAG isel to handle tail calls.
2804 CodeModel::Model CM = TM.getCodeModel();
2805 // Only support the small and large code model.
2806 if (CM != CodeModel::Small && CM != CodeModel::Large)
2809 // FIXME: Add large code model support for ELF.
2810 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
2813 // Let SDISel handle vararg functions.
2817 // FIXME: Only handle *simple* calls for now.
2819 if (CLI.RetTy->isVoidTy())
2820 RetVT = MVT::isVoid;
2821 else if (!isTypeLegal(CLI.RetTy, RetVT))
2824 for (auto Flag : CLI.OutFlags)
2825 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
2828 // Set up the argument vectors.
2829 SmallVector<MVT, 16> OutVTs;
2830 OutVTs.reserve(CLI.OutVals.size());
2832 for (auto *Val : CLI.OutVals) {
2834 if (!isTypeLegal(Val->getType(), VT) &&
2835 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
2838 // We don't handle vector parameters yet.
2839 if (VT.isVector() || VT.getSizeInBits() > 64)
2842 OutVTs.push_back(VT);
2846 if (Callee && !computeCallAddress(Callee, Addr))
2849 // Handle the arguments now that we've gotten them.
2851 if (!processCallArgs(CLI, OutVTs, NumBytes))
2855 MachineInstrBuilder MIB;
2856 if (CM == CodeModel::Small) {
2857 const MCInstrDesc &II = TII.get(Addr.getReg() ? AArch64::BLR : AArch64::BL);
2858 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II);
2860 MIB.addExternalSymbol(SymName, 0);
2861 else if (Addr.getGlobalValue())
2862 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
2863 else if (Addr.getReg()) {
2864 unsigned Reg = constrainOperandRegClass(II, Addr.getReg(), 0);
2869 unsigned CallReg = 0;
2871 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
2872 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
2874 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGE);
2876 CallReg = createResultReg(&AArch64::GPR64RegClass);
2877 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
2880 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
2882 } else if (Addr.getGlobalValue())
2883 CallReg = materializeGV(Addr.getGlobalValue());
2884 else if (Addr.getReg())
2885 CallReg = Addr.getReg();
2890 const MCInstrDesc &II = TII.get(AArch64::BLR);
2891 CallReg = constrainOperandRegClass(II, CallReg, 0);
2892 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(CallReg);
2895 // Add implicit physical register uses to the call.
2896 for (auto Reg : CLI.OutRegs)
2897 MIB.addReg(Reg, RegState::Implicit);
2899 // Add a register mask with the call-preserved registers.
2900 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2901 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2905 // Finish off the call including any return values.
2906 return finishCall(CLI, RetVT, NumBytes);
2909 bool AArch64FastISel::isMemCpySmall(uint64_t Len, unsigned Alignment) {
2911 return Len / Alignment <= 4;
2916 bool AArch64FastISel::tryEmitSmallMemCpy(Address Dest, Address Src,
2917 uint64_t Len, unsigned Alignment) {
2918 // Make sure we don't bloat code by inlining very large memcpy's.
2919 if (!isMemCpySmall(Len, Alignment))
2922 int64_t UnscaledOffset = 0;
2923 Address OrigDest = Dest;
2924 Address OrigSrc = Src;
2928 if (!Alignment || Alignment >= 8) {
2939 // Bound based on alignment.
2940 if (Len >= 4 && Alignment == 4)
2942 else if (Len >= 2 && Alignment == 2)
2951 RV = emitLoad(VT, VT, ResultReg, Src);
2955 RV = emitStore(VT, ResultReg, Dest);
2959 int64_t Size = VT.getSizeInBits() / 8;
2961 UnscaledOffset += Size;
2963 // We need to recompute the unscaled offset for each iteration.
2964 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
2965 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
2971 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
2972 /// into the user. The condition code will only be updated on success.
2973 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
2974 const Instruction *I,
2975 const Value *Cond) {
2976 if (!isa<ExtractValueInst>(Cond))
2979 const auto *EV = cast<ExtractValueInst>(Cond);
2980 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
2983 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
2985 const Function *Callee = II->getCalledFunction();
2987 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
2988 if (!isTypeLegal(RetTy, RetVT))
2991 if (RetVT != MVT::i32 && RetVT != MVT::i64)
2994 const Value *LHS = II->getArgOperand(0);
2995 const Value *RHS = II->getArgOperand(1);
2997 // Canonicalize immediate to the RHS.
2998 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2999 isCommutativeIntrinsic(II))
3000 std::swap(LHS, RHS);
3002 // Simplify multiplies.
3003 unsigned IID = II->getIntrinsicID();
3007 case Intrinsic::smul_with_overflow:
3008 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3009 if (C->getValue() == 2)
3010 IID = Intrinsic::sadd_with_overflow;
3012 case Intrinsic::umul_with_overflow:
3013 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3014 if (C->getValue() == 2)
3015 IID = Intrinsic::uadd_with_overflow;
3019 AArch64CC::CondCode TmpCC;
3023 case Intrinsic::sadd_with_overflow:
3024 case Intrinsic::ssub_with_overflow:
3025 TmpCC = AArch64CC::VS;
3027 case Intrinsic::uadd_with_overflow:
3028 TmpCC = AArch64CC::HS;
3030 case Intrinsic::usub_with_overflow:
3031 TmpCC = AArch64CC::LO;
3033 case Intrinsic::smul_with_overflow:
3034 case Intrinsic::umul_with_overflow:
3035 TmpCC = AArch64CC::NE;
3039 // Check if both instructions are in the same basic block.
3040 if (!isValueAvailable(II))
3043 // Make sure nothing is in the way
3044 BasicBlock::const_iterator Start = I;
3045 BasicBlock::const_iterator End = II;
3046 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
3047 // We only expect extractvalue instructions between the intrinsic and the
3048 // instruction to be selected.
3049 if (!isa<ExtractValueInst>(Itr))
3052 // Check that the extractvalue operand comes from the intrinsic.
3053 const auto *EVI = cast<ExtractValueInst>(Itr);
3054 if (EVI->getAggregateOperand() != II)
3062 bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
3063 // FIXME: Handle more intrinsics.
3064 switch (II->getIntrinsicID()) {
3065 default: return false;
3066 case Intrinsic::frameaddress: {
3067 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
3068 MFI->setFrameAddressIsTaken(true);
3070 const AArch64RegisterInfo *RegInfo =
3071 static_cast<const AArch64RegisterInfo *>(
3072 TM.getSubtargetImpl()->getRegisterInfo());
3073 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
3074 unsigned SrcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3076 TII.get(TargetOpcode::COPY), SrcReg).addReg(FramePtr);
3077 // Recursively load frame address
3083 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
3085 DestReg = fastEmitInst_ri(AArch64::LDRXui, &AArch64::GPR64RegClass,
3086 SrcReg, /*IsKill=*/true, 0);
3087 assert(DestReg && "Unexpected LDR instruction emission failure.");
3091 updateValueMap(II, SrcReg);
3094 case Intrinsic::memcpy:
3095 case Intrinsic::memmove: {
3096 const auto *MTI = cast<MemTransferInst>(II);
3097 // Don't handle volatile.
3098 if (MTI->isVolatile())
3101 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
3102 // we would emit dead code because we don't currently handle memmoves.
3103 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
3104 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
3105 // Small memcpy's are common enough that we want to do them without a call
3107 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
3108 unsigned Alignment = MTI->getAlignment();
3109 if (isMemCpySmall(Len, Alignment)) {
3111 if (!computeAddress(MTI->getRawDest(), Dest) ||
3112 !computeAddress(MTI->getRawSource(), Src))
3114 if (tryEmitSmallMemCpy(Dest, Src, Len, Alignment))
3119 if (!MTI->getLength()->getType()->isIntegerTy(64))
3122 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
3123 // Fast instruction selection doesn't support the special
3127 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
3128 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
3130 case Intrinsic::memset: {
3131 const MemSetInst *MSI = cast<MemSetInst>(II);
3132 // Don't handle volatile.
3133 if (MSI->isVolatile())
3136 if (!MSI->getLength()->getType()->isIntegerTy(64))
3139 if (MSI->getDestAddressSpace() > 255)
3140 // Fast instruction selection doesn't support the special
3144 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
3146 case Intrinsic::sin:
3147 case Intrinsic::cos:
3148 case Intrinsic::pow: {
3150 if (!isTypeLegal(II->getType(), RetVT))
3153 if (RetVT != MVT::f32 && RetVT != MVT::f64)
3156 static const RTLIB::Libcall LibCallTable[3][2] = {
3157 { RTLIB::SIN_F32, RTLIB::SIN_F64 },
3158 { RTLIB::COS_F32, RTLIB::COS_F64 },
3159 { RTLIB::POW_F32, RTLIB::POW_F64 }
3162 bool Is64Bit = RetVT == MVT::f64;
3163 switch (II->getIntrinsicID()) {
3165 llvm_unreachable("Unexpected intrinsic.");
3166 case Intrinsic::sin:
3167 LC = LibCallTable[0][Is64Bit];
3169 case Intrinsic::cos:
3170 LC = LibCallTable[1][Is64Bit];
3172 case Intrinsic::pow:
3173 LC = LibCallTable[2][Is64Bit];
3178 Args.reserve(II->getNumArgOperands());
3180 // Populate the argument list.
3181 for (auto &Arg : II->arg_operands()) {
3184 Entry.Ty = Arg->getType();
3185 Args.push_back(Entry);
3188 CallLoweringInfo CLI;
3189 CLI.setCallee(TLI.getLibcallCallingConv(LC), II->getType(),
3190 TLI.getLibcallName(LC), std::move(Args));
3191 if (!lowerCallTo(CLI))
3193 updateValueMap(II, CLI.ResultReg);
3196 case Intrinsic::trap: {
3197 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
3201 case Intrinsic::sqrt: {
3202 Type *RetTy = II->getCalledFunction()->getReturnType();
3205 if (!isTypeLegal(RetTy, VT))
3208 unsigned Op0Reg = getRegForValue(II->getOperand(0));
3211 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
3213 unsigned ResultReg = fastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
3217 updateValueMap(II, ResultReg);
3220 case Intrinsic::sadd_with_overflow:
3221 case Intrinsic::uadd_with_overflow:
3222 case Intrinsic::ssub_with_overflow:
3223 case Intrinsic::usub_with_overflow:
3224 case Intrinsic::smul_with_overflow:
3225 case Intrinsic::umul_with_overflow: {
3226 // This implements the basic lowering of the xalu with overflow intrinsics.
3227 const Function *Callee = II->getCalledFunction();
3228 auto *Ty = cast<StructType>(Callee->getReturnType());
3229 Type *RetTy = Ty->getTypeAtIndex(0U);
3232 if (!isTypeLegal(RetTy, VT))
3235 if (VT != MVT::i32 && VT != MVT::i64)
3238 const Value *LHS = II->getArgOperand(0);
3239 const Value *RHS = II->getArgOperand(1);
3240 // Canonicalize immediate to the RHS.
3241 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3242 isCommutativeIntrinsic(II))
3243 std::swap(LHS, RHS);
3245 // Simplify multiplies.
3246 unsigned IID = II->getIntrinsicID();
3250 case Intrinsic::smul_with_overflow:
3251 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3252 if (C->getValue() == 2) {
3253 IID = Intrinsic::sadd_with_overflow;
3257 case Intrinsic::umul_with_overflow:
3258 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3259 if (C->getValue() == 2) {
3260 IID = Intrinsic::uadd_with_overflow;
3266 unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
3267 AArch64CC::CondCode CC = AArch64CC::Invalid;
3269 default: llvm_unreachable("Unexpected intrinsic!");
3270 case Intrinsic::sadd_with_overflow:
3271 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3274 case Intrinsic::uadd_with_overflow:
3275 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3278 case Intrinsic::ssub_with_overflow:
3279 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3282 case Intrinsic::usub_with_overflow:
3283 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3286 case Intrinsic::smul_with_overflow: {
3288 unsigned LHSReg = getRegForValue(LHS);
3291 bool LHSIsKill = hasTrivialKill(LHS);
3293 unsigned RHSReg = getRegForValue(RHS);
3296 bool RHSIsKill = hasTrivialKill(RHS);
3298 if (VT == MVT::i32) {
3299 MulReg = emitSMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3300 unsigned ShiftReg = emitLSR_ri(MVT::i64, MVT::i64, MulReg,
3301 /*IsKill=*/false, 32);
3302 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3304 ShiftReg = fastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
3306 emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3307 AArch64_AM::ASR, 31, /*WantResult=*/false);
3309 assert(VT == MVT::i64 && "Unexpected value type.");
3310 MulReg = emitMul_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3311 unsigned SMULHReg = fastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
3313 emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3314 AArch64_AM::ASR, 63, /*WantResult=*/false);
3318 case Intrinsic::umul_with_overflow: {
3320 unsigned LHSReg = getRegForValue(LHS);
3323 bool LHSIsKill = hasTrivialKill(LHS);
3325 unsigned RHSReg = getRegForValue(RHS);
3328 bool RHSIsKill = hasTrivialKill(RHS);
3330 if (VT == MVT::i32) {
3331 MulReg = emitUMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3332 emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
3333 /*IsKill=*/false, AArch64_AM::LSR, 32,
3334 /*WantResult=*/false);
3335 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3338 assert(VT == MVT::i64 && "Unexpected value type.");
3339 MulReg = emitMul_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3340 unsigned UMULHReg = fastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
3342 emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
3343 /*IsKill=*/false, /*WantResult=*/false);
3350 ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
3351 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3352 TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
3355 ResultReg2 = fastEmitInst_rri(AArch64::CSINCWr, &AArch64::GPR32RegClass,
3356 AArch64::WZR, /*IsKill=*/true, AArch64::WZR,
3357 /*IsKill=*/true, getInvertedCondCode(CC));
3359 assert((ResultReg1 + 1) == ResultReg2 &&
3360 "Nonconsecutive result registers.");
3361 updateValueMap(II, ResultReg1, 2);
3368 bool AArch64FastISel::selectRet(const Instruction *I) {
3369 const ReturnInst *Ret = cast<ReturnInst>(I);
3370 const Function &F = *I->getParent()->getParent();
3372 if (!FuncInfo.CanLowerReturn)
3378 // Build a list of return value registers.
3379 SmallVector<unsigned, 4> RetRegs;
3381 if (Ret->getNumOperands() > 0) {
3382 CallingConv::ID CC = F.getCallingConv();
3383 SmallVector<ISD::OutputArg, 4> Outs;
3384 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
3386 // Analyze operands of the call, assigning locations to each operand.
3387 SmallVector<CCValAssign, 16> ValLocs;
3388 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
3389 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
3390 : RetCC_AArch64_AAPCS;
3391 CCInfo.AnalyzeReturn(Outs, RetCC);
3393 // Only handle a single return value for now.
3394 if (ValLocs.size() != 1)
3397 CCValAssign &VA = ValLocs[0];
3398 const Value *RV = Ret->getOperand(0);
3400 // Don't bother handling odd stuff for now.
3401 if ((VA.getLocInfo() != CCValAssign::Full) &&
3402 (VA.getLocInfo() != CCValAssign::BCvt))
3405 // Only handle register returns for now.
3409 unsigned Reg = getRegForValue(RV);
3413 unsigned SrcReg = Reg + VA.getValNo();
3414 unsigned DestReg = VA.getLocReg();
3415 // Avoid a cross-class copy. This is very unlikely.
3416 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
3419 EVT RVEVT = TLI.getValueType(RV->getType());
3420 if (!RVEVT.isSimple())
3423 // Vectors (of > 1 lane) in big endian need tricky handling.
3424 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1 &&
3425 !Subtarget->isLittleEndian())
3428 MVT RVVT = RVEVT.getSimpleVT();
3429 if (RVVT == MVT::f128)
3432 MVT DestVT = VA.getValVT();
3433 // Special handling for extended integers.
3434 if (RVVT != DestVT) {
3435 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
3438 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
3441 bool IsZExt = Outs[0].Flags.isZExt();
3442 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
3448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3449 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
3451 // Add register to return instruction.
3452 RetRegs.push_back(VA.getLocReg());
3455 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3456 TII.get(AArch64::RET_ReallyLR));
3457 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
3458 MIB.addReg(RetRegs[i], RegState::Implicit);
3462 bool AArch64FastISel::selectTrunc(const Instruction *I) {
3463 Type *DestTy = I->getType();
3464 Value *Op = I->getOperand(0);
3465 Type *SrcTy = Op->getType();
3467 EVT SrcEVT = TLI.getValueType(SrcTy, true);
3468 EVT DestEVT = TLI.getValueType(DestTy, true);
3469 if (!SrcEVT.isSimple())
3471 if (!DestEVT.isSimple())
3474 MVT SrcVT = SrcEVT.getSimpleVT();
3475 MVT DestVT = DestEVT.getSimpleVT();
3477 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
3480 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
3484 unsigned SrcReg = getRegForValue(Op);
3487 bool SrcIsKill = hasTrivialKill(Op);
3489 // If we're truncating from i64 to a smaller non-legal type then generate an
3490 // AND. Otherwise, we know the high bits are undefined and a truncate only
3491 // generate a COPY. We cannot mark the source register also as result
3492 // register, because this can incorrectly transfer the kill flag onto the
3495 if (SrcVT == MVT::i64) {
3497 switch (DestVT.SimpleTy) {
3499 // Trunc i64 to i32 is handled by the target-independent fast-isel.
3511 // Issue an extract_subreg to get the lower 32-bits.
3512 unsigned Reg32 = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
3514 // Create the AND instruction which performs the actual truncation.
3515 ResultReg = emitAnd_ri(MVT::i32, Reg32, /*IsKill=*/true, Mask);
3516 assert(ResultReg && "Unexpected AND instruction emission failure.");
3518 ResultReg = createResultReg(&AArch64::GPR32RegClass);
3519 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3520 TII.get(TargetOpcode::COPY), ResultReg)
3521 .addReg(SrcReg, getKillRegState(SrcIsKill));
3524 updateValueMap(I, ResultReg);
3528 unsigned AArch64FastISel::emiti1Ext(unsigned SrcReg, MVT DestVT, bool IsZExt) {
3529 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
3530 DestVT == MVT::i64) &&
3531 "Unexpected value type.");
3532 // Handle i8 and i16 as i32.
3533 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3537 unsigned ResultReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
3538 assert(ResultReg && "Unexpected AND instruction emission failure.");
3539 if (DestVT == MVT::i64) {
3540 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
3541 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
3542 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3543 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3544 TII.get(AArch64::SUBREG_TO_REG), Reg64)
3547 .addImm(AArch64::sub_32);
3552 if (DestVT == MVT::i64) {
3553 // FIXME: We're SExt i1 to i64.
3556 return fastEmitInst_rii(AArch64::SBFMWri, &AArch64::GPR32RegClass, SrcReg,
3557 /*TODO:IsKill=*/false, 0, 0);
3561 unsigned AArch64FastISel::emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3562 unsigned Op1, bool Op1IsKill) {
3564 switch (RetVT.SimpleTy) {
3570 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
3572 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
3575 const TargetRegisterClass *RC =
3576 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3577 return fastEmitInst_rrr(Opc, RC, Op0, Op0IsKill, Op1, Op1IsKill,
3578 /*IsKill=*/ZReg, true);
3581 unsigned AArch64FastISel::emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3582 unsigned Op1, bool Op1IsKill) {
3583 if (RetVT != MVT::i64)
3586 return fastEmitInst_rrr(AArch64::SMADDLrrr, &AArch64::GPR64RegClass,
3587 Op0, Op0IsKill, Op1, Op1IsKill,
3588 AArch64::XZR, /*IsKill=*/true);
3591 unsigned AArch64FastISel::emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3592 unsigned Op1, bool Op1IsKill) {
3593 if (RetVT != MVT::i64)
3596 return fastEmitInst_rrr(AArch64::UMADDLrrr, &AArch64::GPR64RegClass,
3597 Op0, Op0IsKill, Op1, Op1IsKill,
3598 AArch64::XZR, /*IsKill=*/true);
3601 unsigned AArch64FastISel::emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3602 unsigned Op1Reg, bool Op1IsKill) {
3604 bool NeedTrunc = false;
3606 switch (RetVT.SimpleTy) {
3608 case MVT::i8: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xff; break;
3609 case MVT::i16: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xffff; break;
3610 case MVT::i32: Opc = AArch64::LSLVWr; break;
3611 case MVT::i64: Opc = AArch64::LSLVXr; break;
3614 const TargetRegisterClass *RC =
3615 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3617 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3620 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3623 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3627 unsigned AArch64FastISel::emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3628 bool Op0IsKill, uint64_t Shift,
3630 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3631 "Unexpected source/return type pair.");
3632 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
3633 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
3634 "Unexpected source value type.");
3635 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3636 RetVT == MVT::i64) && "Unexpected return value type.");
3638 bool Is64Bit = (RetVT == MVT::i64);
3639 unsigned RegSize = Is64Bit ? 64 : 32;
3640 unsigned DstBits = RetVT.getSizeInBits();
3641 unsigned SrcBits = SrcVT.getSizeInBits();
3643 // Don't deal with undefined shifts.
3644 if (Shift >= DstBits)
3647 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3648 // {S|U}BFM Wd, Wn, #r, #s
3649 // Wd<32+s-r,32-r> = Wn<s:0> when r > s
3651 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3652 // %2 = shl i16 %1, 4
3653 // Wd<32+7-28,32-28> = Wn<7:0> <- clamp s to 7
3654 // 0b1111_1111_1111_1111__1111_1010_1010_0000 sext
3655 // 0b0000_0000_0000_0000__0000_0101_0101_0000 sext | zext
3656 // 0b0000_0000_0000_0000__0000_1010_1010_0000 zext
3658 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3659 // %2 = shl i16 %1, 8
3660 // Wd<32+7-24,32-24> = Wn<7:0>
3661 // 0b1111_1111_1111_1111__1010_1010_0000_0000 sext
3662 // 0b0000_0000_0000_0000__0101_0101_0000_0000 sext | zext
3663 // 0b0000_0000_0000_0000__1010_1010_0000_0000 zext
3665 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3666 // %2 = shl i16 %1, 12
3667 // Wd<32+3-20,32-20> = Wn<3:0>
3668 // 0b1111_1111_1111_1111__1010_0000_0000_0000 sext
3669 // 0b0000_0000_0000_0000__0101_0000_0000_0000 sext | zext
3670 // 0b0000_0000_0000_0000__1010_0000_0000_0000 zext
3672 unsigned ImmR = RegSize - Shift;
3673 // Limit the width to the length of the source type.
3674 unsigned ImmS = std::min<unsigned>(SrcBits - 1, DstBits - 1 - Shift);
3675 static const unsigned OpcTable[2][2] = {
3676 {AArch64::SBFMWri, AArch64::SBFMXri},
3677 {AArch64::UBFMWri, AArch64::UBFMXri}
3679 unsigned Opc = OpcTable[IsZext][Is64Bit];
3680 const TargetRegisterClass *RC =
3681 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3682 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3683 unsigned TmpReg = MRI.createVirtualRegister(RC);
3684 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3685 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3687 .addReg(Op0, getKillRegState(Op0IsKill))
3688 .addImm(AArch64::sub_32);
3692 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3695 unsigned AArch64FastISel::emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3696 unsigned Op1Reg, bool Op1IsKill) {
3698 bool NeedTrunc = false;
3700 switch (RetVT.SimpleTy) {
3702 case MVT::i8: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xff; break;
3703 case MVT::i16: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xffff; break;
3704 case MVT::i32: Opc = AArch64::LSRVWr; break;
3705 case MVT::i64: Opc = AArch64::LSRVXr; break;
3708 const TargetRegisterClass *RC =
3709 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3711 Op0Reg = emitAnd_ri(MVT::i32, Op0Reg, Op0IsKill, Mask);
3712 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3713 Op0IsKill = Op1IsKill = true;
3715 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3718 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3722 unsigned AArch64FastISel::emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3723 bool Op0IsKill, uint64_t Shift,
3725 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3726 "Unexpected source/return type pair.");
3727 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3728 SrcVT == MVT::i64) && "Unexpected source value type.");
3729 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3730 RetVT == MVT::i64) && "Unexpected return value type.");
3732 bool Is64Bit = (RetVT == MVT::i64);
3733 unsigned RegSize = Is64Bit ? 64 : 32;
3734 unsigned DstBits = RetVT.getSizeInBits();
3735 unsigned SrcBits = SrcVT.getSizeInBits();
3737 // Don't deal with undefined shifts.
3738 if (Shift >= DstBits)
3741 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3742 // {S|U}BFM Wd, Wn, #r, #s
3743 // Wd<s-r:0> = Wn<s:r> when r <= s
3745 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3746 // %2 = lshr i16 %1, 4
3747 // Wd<7-4:0> = Wn<7:4>
3748 // 0b0000_0000_0000_0000__0000_1111_1111_1010 sext
3749 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
3750 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
3752 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3753 // %2 = lshr i16 %1, 8
3754 // Wd<7-7,0> = Wn<7:7>
3755 // 0b0000_0000_0000_0000__0000_0000_1111_1111 sext
3756 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3757 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3759 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3760 // %2 = lshr i16 %1, 12
3761 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
3762 // 0b0000_0000_0000_0000__0000_0000_0000_1111 sext
3763 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3764 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3766 if (Shift >= SrcBits && IsZExt)
3767 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
3769 // It is not possible to fold a sign-extend into the LShr instruction. In this
3770 // case emit a sign-extend.
3772 Op0 = emitIntExt(SrcVT, Op0, RetVT, IsZExt);
3777 SrcBits = SrcVT.getSizeInBits();
3781 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
3782 unsigned ImmS = SrcBits - 1;
3783 static const unsigned OpcTable[2][2] = {
3784 {AArch64::SBFMWri, AArch64::SBFMXri},
3785 {AArch64::UBFMWri, AArch64::UBFMXri}
3787 unsigned Opc = OpcTable[IsZExt][Is64Bit];
3788 const TargetRegisterClass *RC =
3789 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3790 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3791 unsigned TmpReg = MRI.createVirtualRegister(RC);
3792 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3793 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3795 .addReg(Op0, getKillRegState(Op0IsKill))
3796 .addImm(AArch64::sub_32);
3800 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3803 unsigned AArch64FastISel::emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3804 unsigned Op1Reg, bool Op1IsKill) {
3806 bool NeedTrunc = false;
3808 switch (RetVT.SimpleTy) {
3810 case MVT::i8: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xff; break;
3811 case MVT::i16: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xffff; break;
3812 case MVT::i32: Opc = AArch64::ASRVWr; break;
3813 case MVT::i64: Opc = AArch64::ASRVXr; break;
3816 const TargetRegisterClass *RC =
3817 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3819 Op0Reg = emitIntExt(RetVT, Op0Reg, MVT::i32, /*IsZExt=*/false);
3820 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3821 Op0IsKill = Op1IsKill = true;
3823 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3826 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3830 unsigned AArch64FastISel::emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3831 bool Op0IsKill, uint64_t Shift,
3833 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3834 "Unexpected source/return type pair.");
3835 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3836 SrcVT == MVT::i64) && "Unexpected source value type.");
3837 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3838 RetVT == MVT::i64) && "Unexpected return value type.");
3840 bool Is64Bit = (RetVT == MVT::i64);
3841 unsigned RegSize = Is64Bit ? 64 : 32;
3842 unsigned DstBits = RetVT.getSizeInBits();
3843 unsigned SrcBits = SrcVT.getSizeInBits();
3845 // Don't deal with undefined shifts.
3846 if (Shift >= DstBits)
3849 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3850 // {S|U}BFM Wd, Wn, #r, #s
3851 // Wd<s-r:0> = Wn<s:r> when r <= s
3853 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3854 // %2 = ashr i16 %1, 4
3855 // Wd<7-4:0> = Wn<7:4>
3856 // 0b1111_1111_1111_1111__1111_1111_1111_1010 sext
3857 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
3858 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
3860 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3861 // %2 = ashr i16 %1, 8
3862 // Wd<7-7,0> = Wn<7:7>
3863 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
3864 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3865 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3867 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3868 // %2 = ashr i16 %1, 12
3869 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
3870 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
3871 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3872 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3874 if (Shift >= SrcBits && IsZExt)
3875 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
3877 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
3878 unsigned ImmS = SrcBits - 1;
3879 static const unsigned OpcTable[2][2] = {
3880 {AArch64::SBFMWri, AArch64::SBFMXri},
3881 {AArch64::UBFMWri, AArch64::UBFMXri}
3883 unsigned Opc = OpcTable[IsZExt][Is64Bit];
3884 const TargetRegisterClass *RC =
3885 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3886 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3887 unsigned TmpReg = MRI.createVirtualRegister(RC);
3888 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3889 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3891 .addReg(Op0, getKillRegState(Op0IsKill))
3892 .addImm(AArch64::sub_32);
3896 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3899 unsigned AArch64FastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
3901 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
3903 // FastISel does not have plumbing to deal with extensions where the SrcVT or
3904 // DestVT are odd things, so test to make sure that they are both types we can
3905 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
3906 // bail out to SelectionDAG.
3907 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
3908 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
3909 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
3910 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
3916 switch (SrcVT.SimpleTy) {
3920 return emiti1Ext(SrcReg, DestVT, IsZExt);
3922 if (DestVT == MVT::i64)
3923 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3925 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
3929 if (DestVT == MVT::i64)
3930 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3932 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
3936 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
3937 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3942 // Handle i8 and i16 as i32.
3943 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3945 else if (DestVT == MVT::i64) {
3946 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3947 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3948 TII.get(AArch64::SUBREG_TO_REG), Src64)
3951 .addImm(AArch64::sub_32);
3955 const TargetRegisterClass *RC =
3956 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3957 return fastEmitInst_rii(Opc, RC, SrcReg, /*TODO:IsKill=*/false, 0, Imm);
3960 bool AArch64FastISel::selectIntExt(const Instruction *I) {
3961 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
3962 "Unexpected integer extend instruction.");
3965 if (!isTypeSupported(I->getType(), RetVT))
3968 if (!isTypeSupported(I->getOperand(0)->getType(), SrcVT))
3971 unsigned SrcReg = getRegForValue(I->getOperand(0));
3974 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
3976 // The load instruction selection code handles the sign-/zero-extension.
3977 if (const auto *LI = dyn_cast<LoadInst>(I->getOperand(0))) {
3978 if (LI->hasOneUse()) {
3979 updateValueMap(I, SrcReg);
3984 bool IsZExt = isa<ZExtInst>(I);
3985 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0))) {
3986 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr())) {
3987 if (RetVT == MVT::i64 && SrcVT != MVT::i64) {
3988 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
3989 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3990 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
3992 .addReg(SrcReg, getKillRegState(SrcIsKill))
3993 .addImm(AArch64::sub_32);
3996 updateValueMap(I, SrcReg);
4001 unsigned ResultReg = emitIntExt(SrcVT, SrcReg, RetVT, IsZExt);
4005 updateValueMap(I, ResultReg);
4009 bool AArch64FastISel::selectRem(const Instruction *I, unsigned ISDOpcode) {
4010 EVT DestEVT = TLI.getValueType(I->getType(), true);
4011 if (!DestEVT.isSimple())
4014 MVT DestVT = DestEVT.getSimpleVT();
4015 if (DestVT != MVT::i64 && DestVT != MVT::i32)
4019 bool Is64bit = (DestVT == MVT::i64);
4020 switch (ISDOpcode) {
4024 DivOpc = Is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
4027 DivOpc = Is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
4030 unsigned MSubOpc = Is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
4031 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4034 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4036 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4039 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4041 const TargetRegisterClass *RC =
4042 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4043 unsigned QuotReg = fastEmitInst_rr(DivOpc, RC, Src0Reg, /*IsKill=*/false,
4044 Src1Reg, /*IsKill=*/false);
4045 assert(QuotReg && "Unexpected DIV instruction emission failure.");
4046 // The remainder is computed as numerator - (quotient * denominator) using the
4047 // MSUB instruction.
4048 unsigned ResultReg = fastEmitInst_rrr(MSubOpc, RC, QuotReg, /*IsKill=*/true,
4049 Src1Reg, Src1IsKill, Src0Reg,
4051 updateValueMap(I, ResultReg);
4055 bool AArch64FastISel::selectMul(const Instruction *I) {
4057 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
4061 return selectBinaryOp(I, ISD::MUL);
4063 const Value *Src0 = I->getOperand(0);
4064 const Value *Src1 = I->getOperand(1);
4065 if (const auto *C = dyn_cast<ConstantInt>(Src0))
4066 if (C->getValue().isPowerOf2())
4067 std::swap(Src0, Src1);
4069 // Try to simplify to a shift instruction.
4070 if (const auto *C = dyn_cast<ConstantInt>(Src1))
4071 if (C->getValue().isPowerOf2()) {
4072 uint64_t ShiftVal = C->getValue().logBase2();
4075 if (const auto *ZExt = dyn_cast<ZExtInst>(Src0)) {
4076 if (!isIntExtFree(ZExt)) {
4078 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), VT)) {
4081 Src0 = ZExt->getOperand(0);
4084 } else if (const auto *SExt = dyn_cast<SExtInst>(Src0)) {
4085 if (!isIntExtFree(SExt)) {
4087 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), VT)) {
4090 Src0 = SExt->getOperand(0);
4095 unsigned Src0Reg = getRegForValue(Src0);
4098 bool Src0IsKill = hasTrivialKill(Src0);
4100 unsigned ResultReg =
4101 emitLSL_ri(VT, SrcVT, Src0Reg, Src0IsKill, ShiftVal, IsZExt);
4104 updateValueMap(I, ResultReg);
4109 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4112 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4114 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4117 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4119 unsigned ResultReg = emitMul_rr(VT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
4124 updateValueMap(I, ResultReg);
4128 bool AArch64FastISel::selectShift(const Instruction *I) {
4130 if (!isTypeSupported(I->getType(), RetVT, /*IsVectorAllowed=*/true))
4133 if (RetVT.isVector())
4134 return selectOperator(I, I->getOpcode());
4136 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
4137 unsigned ResultReg = 0;
4138 uint64_t ShiftVal = C->getZExtValue();
4140 bool IsZExt = (I->getOpcode() == Instruction::AShr) ? false : true;
4141 const Value *Op0 = I->getOperand(0);
4142 if (const auto *ZExt = dyn_cast<ZExtInst>(Op0)) {
4143 if (!isIntExtFree(ZExt)) {
4145 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), TmpVT)) {
4148 Op0 = ZExt->getOperand(0);
4151 } else if (const auto *SExt = dyn_cast<SExtInst>(Op0)) {
4152 if (!isIntExtFree(SExt)) {
4154 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), TmpVT)) {
4157 Op0 = SExt->getOperand(0);
4162 unsigned Op0Reg = getRegForValue(Op0);
4165 bool Op0IsKill = hasTrivialKill(Op0);
4167 switch (I->getOpcode()) {
4168 default: llvm_unreachable("Unexpected instruction.");
4169 case Instruction::Shl:
4170 ResultReg = emitLSL_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4172 case Instruction::AShr:
4173 ResultReg = emitASR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4175 case Instruction::LShr:
4176 ResultReg = emitLSR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4182 updateValueMap(I, ResultReg);
4186 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4189 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4191 unsigned Op1Reg = getRegForValue(I->getOperand(1));
4194 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
4196 unsigned ResultReg = 0;
4197 switch (I->getOpcode()) {
4198 default: llvm_unreachable("Unexpected instruction.");
4199 case Instruction::Shl:
4200 ResultReg = emitLSL_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4202 case Instruction::AShr:
4203 ResultReg = emitASR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4205 case Instruction::LShr:
4206 ResultReg = emitLSR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4213 updateValueMap(I, ResultReg);
4217 bool AArch64FastISel::selectBitCast(const Instruction *I) {
4220 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
4222 if (!isTypeLegal(I->getType(), RetVT))
4226 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
4227 Opc = AArch64::FMOVWSr;
4228 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
4229 Opc = AArch64::FMOVXDr;
4230 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
4231 Opc = AArch64::FMOVSWr;
4232 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
4233 Opc = AArch64::FMOVDXr;
4237 const TargetRegisterClass *RC = nullptr;
4238 switch (RetVT.SimpleTy) {
4239 default: llvm_unreachable("Unexpected value type.");
4240 case MVT::i32: RC = &AArch64::GPR32RegClass; break;
4241 case MVT::i64: RC = &AArch64::GPR64RegClass; break;
4242 case MVT::f32: RC = &AArch64::FPR32RegClass; break;
4243 case MVT::f64: RC = &AArch64::FPR64RegClass; break;
4245 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4248 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4249 unsigned ResultReg = fastEmitInst_r(Opc, RC, Op0Reg, Op0IsKill);
4254 updateValueMap(I, ResultReg);
4258 bool AArch64FastISel::selectFRem(const Instruction *I) {
4260 if (!isTypeLegal(I->getType(), RetVT))
4264 switch (RetVT.SimpleTy) {
4268 LC = RTLIB::REM_F32;
4271 LC = RTLIB::REM_F64;
4276 Args.reserve(I->getNumOperands());
4278 // Populate the argument list.
4279 for (auto &Arg : I->operands()) {
4282 Entry.Ty = Arg->getType();
4283 Args.push_back(Entry);
4286 CallLoweringInfo CLI;
4287 CLI.setCallee(TLI.getLibcallCallingConv(LC), I->getType(),
4288 TLI.getLibcallName(LC), std::move(Args));
4289 if (!lowerCallTo(CLI))
4291 updateValueMap(I, CLI.ResultReg);
4295 bool AArch64FastISel::selectSDiv(const Instruction *I) {
4297 if (!isTypeLegal(I->getType(), VT))
4300 if (!isa<ConstantInt>(I->getOperand(1)))
4301 return selectBinaryOp(I, ISD::SDIV);
4303 const APInt &C = cast<ConstantInt>(I->getOperand(1))->getValue();
4304 if ((VT != MVT::i32 && VT != MVT::i64) || !C ||
4305 !(C.isPowerOf2() || (-C).isPowerOf2()))
4306 return selectBinaryOp(I, ISD::SDIV);
4308 unsigned Lg2 = C.countTrailingZeros();
4309 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4312 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4314 if (cast<BinaryOperator>(I)->isExact()) {
4315 unsigned ResultReg = emitASR_ri(VT, VT, Src0Reg, Src0IsKill, Lg2);
4318 updateValueMap(I, ResultReg);
4322 unsigned Pow2MinusOne = (1 << Lg2) - 1;
4323 unsigned AddReg = emitAddSub_ri(/*UseAdd=*/true, VT, Src0Reg,
4324 /*IsKill=*/false, Pow2MinusOne);
4328 // (Src0 < 0) ? Pow2 - 1 : 0;
4329 if (!emitICmp_ri(VT, Src0Reg, /*IsKill=*/false, 0))
4333 const TargetRegisterClass *RC;
4334 if (VT == MVT::i64) {
4335 SelectOpc = AArch64::CSELXr;
4336 RC = &AArch64::GPR64RegClass;
4338 SelectOpc = AArch64::CSELWr;
4339 RC = &AArch64::GPR32RegClass;
4341 unsigned SelectReg =
4342 fastEmitInst_rri(SelectOpc, RC, AddReg, /*IsKill=*/true, Src0Reg,
4343 Src0IsKill, AArch64CC::LT);
4347 // Divide by Pow2 --> ashr. If we're dividing by a negative value we must also
4348 // negate the result.
4349 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
4352 ResultReg = emitAddSub_rs(/*UseAdd=*/false, VT, ZeroReg, /*IsKill=*/true,
4353 SelectReg, /*IsKill=*/true, AArch64_AM::ASR, Lg2);
4355 ResultReg = emitASR_ri(VT, VT, SelectReg, /*IsKill=*/true, Lg2);
4360 updateValueMap(I, ResultReg);
4364 bool AArch64FastISel::fastSelectInstruction(const Instruction *I) {
4365 switch (I->getOpcode()) {
4368 case Instruction::Add:
4369 case Instruction::Sub:
4370 return selectAddSub(I);
4371 case Instruction::Mul:
4372 return selectMul(I);
4373 case Instruction::SDiv:
4374 return selectSDiv(I);
4375 case Instruction::SRem:
4376 if (!selectBinaryOp(I, ISD::SREM))
4377 return selectRem(I, ISD::SREM);
4379 case Instruction::URem:
4380 if (!selectBinaryOp(I, ISD::UREM))
4381 return selectRem(I, ISD::UREM);
4383 case Instruction::Shl:
4384 case Instruction::LShr:
4385 case Instruction::AShr:
4386 return selectShift(I);
4387 case Instruction::And:
4388 case Instruction::Or:
4389 case Instruction::Xor:
4390 return selectLogicalOp(I);
4391 case Instruction::Br:
4392 return selectBranch(I);
4393 case Instruction::IndirectBr:
4394 return selectIndirectBr(I);
4395 case Instruction::BitCast:
4396 if (!FastISel::selectBitCast(I))
4397 return selectBitCast(I);
4399 case Instruction::FPToSI:
4400 if (!selectCast(I, ISD::FP_TO_SINT))
4401 return selectFPToInt(I, /*Signed=*/true);
4403 case Instruction::FPToUI:
4404 return selectFPToInt(I, /*Signed=*/false);
4405 case Instruction::ZExt:
4406 case Instruction::SExt:
4407 return selectIntExt(I);
4408 case Instruction::Trunc:
4409 if (!selectCast(I, ISD::TRUNCATE))
4410 return selectTrunc(I);
4412 case Instruction::FPExt:
4413 return selectFPExt(I);
4414 case Instruction::FPTrunc:
4415 return selectFPTrunc(I);
4416 case Instruction::SIToFP:
4417 if (!selectCast(I, ISD::SINT_TO_FP))
4418 return selectIntToFP(I, /*Signed=*/true);
4420 case Instruction::UIToFP:
4421 return selectIntToFP(I, /*Signed=*/false);
4422 case Instruction::Load:
4423 return selectLoad(I);
4424 case Instruction::Store:
4425 return selectStore(I);
4426 case Instruction::FCmp:
4427 case Instruction::ICmp:
4428 return selectCmp(I);
4429 case Instruction::Select:
4430 return selectSelect(I);
4431 case Instruction::Ret:
4432 return selectRet(I);
4433 case Instruction::FRem:
4434 return selectFRem(I);
4437 // fall-back to target-independent instruction selection.
4438 return selectOperator(I, I->getOpcode());
4439 // Silence warnings.
4440 (void)&CC_AArch64_DarwinPCS_VarArg;
4444 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &FuncInfo,
4445 const TargetLibraryInfo *LibInfo) {
4446 return new AArch64FastISel(FuncInfo, LibInfo);