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);
779 case Instruction::SExt:
780 case Instruction::ZExt: {
781 if (!Addr.getReg() || Addr.getOffsetReg())
784 const Value *Src = nullptr;
785 // Fold the zext or sext when it won't become a noop.
786 if (const auto *ZE = dyn_cast<ZExtInst>(U)) {
787 if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
788 Addr.setExtendType(AArch64_AM::UXTW);
789 Src = ZE->getOperand(0);
791 } else if (const auto *SE = dyn_cast<SExtInst>(U)) {
792 if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
793 Addr.setExtendType(AArch64_AM::SXTW);
794 Src = SE->getOperand(0);
802 unsigned Reg = getRegForValue(Src);
805 Addr.setOffsetReg(Reg);
811 if (!Addr.getOffsetReg()) {
812 unsigned Reg = getRegForValue(Obj);
815 Addr.setOffsetReg(Reg);
821 unsigned Reg = getRegForValue(Obj);
828 bool AArch64FastISel::computeCallAddress(const Value *V, Address &Addr) {
829 const User *U = nullptr;
830 unsigned Opcode = Instruction::UserOp1;
833 if (const auto *I = dyn_cast<Instruction>(V)) {
834 Opcode = I->getOpcode();
836 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
837 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
838 Opcode = C->getOpcode();
844 case Instruction::BitCast:
845 // Look past bitcasts if its operand is in the same BB.
847 return computeCallAddress(U->getOperand(0), Addr);
849 case Instruction::IntToPtr:
850 // Look past no-op inttoptrs if its operand is in the same BB.
852 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
853 return computeCallAddress(U->getOperand(0), Addr);
855 case Instruction::PtrToInt:
856 // Look past no-op ptrtoints if its operand is in the same BB.
858 TLI.getValueType(U->getType()) == TLI.getPointerTy())
859 return computeCallAddress(U->getOperand(0), Addr);
863 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
864 Addr.setGlobalValue(GV);
868 // If all else fails, try to materialize the value in a register.
869 if (!Addr.getGlobalValue()) {
870 Addr.setReg(getRegForValue(V));
871 return Addr.getReg() != 0;
878 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
879 EVT evt = TLI.getValueType(Ty, true);
881 // Only handle simple types.
882 if (evt == MVT::Other || !evt.isSimple())
884 VT = evt.getSimpleVT();
886 // This is a legal type, but it's not something we handle in fast-isel.
890 // Handle all other legal types, i.e. a register that will directly hold this
892 return TLI.isTypeLegal(VT);
895 /// \brief Determine if the value type is supported by FastISel.
897 /// FastISel for AArch64 can handle more value types than are legal. This adds
898 /// simple value type such as i1, i8, and i16.
899 bool AArch64FastISel::isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed) {
900 if (Ty->isVectorTy() && !IsVectorAllowed)
903 if (isTypeLegal(Ty, VT))
906 // If this is a type than can be sign or zero-extended to a basic operation
907 // go ahead and accept it now.
908 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
914 bool AArch64FastISel::isValueAvailable(const Value *V) const {
915 if (!isa<Instruction>(V))
918 const auto *I = cast<Instruction>(V);
919 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
925 bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) {
926 unsigned ScaleFactor = getImplicitScaleFactor(VT);
930 bool ImmediateOffsetNeedsLowering = false;
931 bool RegisterOffsetNeedsLowering = false;
932 int64_t Offset = Addr.getOffset();
933 if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
934 ImmediateOffsetNeedsLowering = true;
935 else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
936 !isUInt<12>(Offset / ScaleFactor))
937 ImmediateOffsetNeedsLowering = true;
939 // Cannot encode an offset register and an immediate offset in the same
940 // instruction. Fold the immediate offset into the load/store instruction and
941 // emit an additonal add to take care of the offset register.
942 if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.isRegBase() &&
944 RegisterOffsetNeedsLowering = true;
946 // Cannot encode zero register as base.
947 if (Addr.isRegBase() && Addr.getOffsetReg() && !Addr.getReg())
948 RegisterOffsetNeedsLowering = true;
950 // If this is a stack pointer and the offset needs to be simplified then put
951 // the alloca address into a register, set the base type back to register and
952 // continue. This should almost never happen.
953 if (ImmediateOffsetNeedsLowering && Addr.isFIBase()) {
954 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
955 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
957 .addFrameIndex(Addr.getFI())
960 Addr.setKind(Address::RegBase);
961 Addr.setReg(ResultReg);
964 if (RegisterOffsetNeedsLowering) {
965 unsigned ResultReg = 0;
967 if (Addr.getExtendType() == AArch64_AM::SXTW ||
968 Addr.getExtendType() == AArch64_AM::UXTW )
969 ResultReg = emitAddSub_rx(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
970 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
971 /*TODO:IsKill=*/false, Addr.getExtendType(),
974 ResultReg = emitAddSub_rs(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
975 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
976 /*TODO:IsKill=*/false, AArch64_AM::LSL,
979 if (Addr.getExtendType() == AArch64_AM::UXTW)
980 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
981 /*Op0IsKill=*/false, Addr.getShift(),
983 else if (Addr.getExtendType() == AArch64_AM::SXTW)
984 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
985 /*Op0IsKill=*/false, Addr.getShift(),
988 ResultReg = emitLSL_ri(MVT::i64, MVT::i64, Addr.getOffsetReg(),
989 /*Op0IsKill=*/false, Addr.getShift());
994 Addr.setReg(ResultReg);
995 Addr.setOffsetReg(0);
997 Addr.setExtendType(AArch64_AM::InvalidShiftExtend);
1000 // Since the offset is too large for the load/store instruction get the
1001 // reg+offset into a register.
1002 if (ImmediateOffsetNeedsLowering) {
1004 if (Addr.getReg()) {
1005 // Try to fold the immediate into the add instruction.
1007 ResultReg = emitAddSub_ri(/*UseAdd=*/false, MVT::i64, Addr.getReg(),
1008 /*IsKill=*/false, -Offset);
1010 ResultReg = emitAddSub_ri(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
1011 /*IsKill=*/false, Offset);
1013 unsigned ImmReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
1014 ResultReg = emitAddSub_rr(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
1015 /*IsKill=*/false, ImmReg, /*IsKill=*/true);
1018 ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
1022 Addr.setReg(ResultReg);
1028 void AArch64FastISel::addLoadStoreOperands(Address &Addr,
1029 const MachineInstrBuilder &MIB,
1031 unsigned ScaleFactor,
1032 MachineMemOperand *MMO) {
1033 int64_t Offset = Addr.getOffset() / ScaleFactor;
1034 // Frame base works a bit differently. Handle it separately.
1035 if (Addr.isFIBase()) {
1036 int FI = Addr.getFI();
1037 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
1038 // and alignment should be based on the VT.
1039 MMO = FuncInfo.MF->getMachineMemOperand(
1040 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
1041 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
1042 // Now add the rest of the operands.
1043 MIB.addFrameIndex(FI).addImm(Offset);
1045 assert(Addr.isRegBase() && "Unexpected address kind.");
1046 const MCInstrDesc &II = MIB->getDesc();
1047 unsigned Idx = (Flags & MachineMemOperand::MOStore) ? 1 : 0;
1049 constrainOperandRegClass(II, Addr.getReg(), II.getNumDefs()+Idx));
1051 constrainOperandRegClass(II, Addr.getOffsetReg(), II.getNumDefs()+Idx+1));
1052 if (Addr.getOffsetReg()) {
1053 assert(Addr.getOffset() == 0 && "Unexpected offset");
1054 bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
1055 Addr.getExtendType() == AArch64_AM::SXTX;
1056 MIB.addReg(Addr.getReg());
1057 MIB.addReg(Addr.getOffsetReg());
1058 MIB.addImm(IsSigned);
1059 MIB.addImm(Addr.getShift() != 0);
1061 MIB.addReg(Addr.getReg());
1067 MIB.addMemOperand(MMO);
1070 unsigned AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
1071 const Value *RHS, bool SetFlags,
1072 bool WantResult, bool IsZExt) {
1073 AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
1074 bool NeedExtend = false;
1075 switch (RetVT.SimpleTy) {
1083 ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
1087 ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
1089 case MVT::i32: // fall-through
1094 RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
1096 // Canonicalize immediates to the RHS first.
1097 if (UseAdd && isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1098 std::swap(LHS, RHS);
1100 // Canonicalize mul by power of 2 to the RHS.
1101 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1102 if (isMulPowOf2(LHS))
1103 std::swap(LHS, RHS);
1105 // Canonicalize shift immediate to the RHS.
1106 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1107 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
1108 if (isa<ConstantInt>(SI->getOperand(1)))
1109 if (SI->getOpcode() == Instruction::Shl ||
1110 SI->getOpcode() == Instruction::LShr ||
1111 SI->getOpcode() == Instruction::AShr )
1112 std::swap(LHS, RHS);
1114 unsigned LHSReg = getRegForValue(LHS);
1117 bool LHSIsKill = hasTrivialKill(LHS);
1120 LHSReg = emitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
1122 unsigned ResultReg = 0;
1123 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1124 uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
1125 if (C->isNegative())
1126 ResultReg = emitAddSub_ri(!UseAdd, RetVT, LHSReg, LHSIsKill, -Imm,
1127 SetFlags, WantResult);
1129 ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, Imm, SetFlags,
1135 // Only extend the RHS within the instruction if there is a valid extend type.
1136 if (ExtendType != AArch64_AM::InvalidShiftExtend && RHS->hasOneUse() &&
1137 isValueAvailable(RHS)) {
1138 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
1139 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
1140 if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
1141 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1144 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1145 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1146 RHSIsKill, ExtendType, C->getZExtValue(),
1147 SetFlags, WantResult);
1149 unsigned RHSReg = getRegForValue(RHS);
1152 bool RHSIsKill = hasTrivialKill(RHS);
1153 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1154 ExtendType, 0, SetFlags, WantResult);
1157 // Check if the mul can be folded into the instruction.
1158 if (RHS->hasOneUse() && isValueAvailable(RHS))
1159 if (isMulPowOf2(RHS)) {
1160 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1161 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1163 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1164 if (C->getValue().isPowerOf2())
1165 std::swap(MulLHS, MulRHS);
1167 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1168 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1169 unsigned RHSReg = getRegForValue(MulLHS);
1172 bool RHSIsKill = hasTrivialKill(MulLHS);
1173 return emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1174 AArch64_AM::LSL, ShiftVal, SetFlags, WantResult);
1177 // Check if the shift can be folded into the instruction.
1178 if (RHS->hasOneUse() && isValueAvailable(RHS))
1179 if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
1180 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1181 AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
1182 switch (SI->getOpcode()) {
1184 case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
1185 case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
1186 case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
1188 uint64_t ShiftVal = C->getZExtValue();
1189 if (ShiftType != AArch64_AM::InvalidShiftExtend) {
1190 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1193 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1194 return emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1195 RHSIsKill, ShiftType, ShiftVal, SetFlags,
1201 unsigned RHSReg = getRegForValue(RHS);
1204 bool RHSIsKill = hasTrivialKill(RHS);
1207 RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
1209 return emitAddSub_rr(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1210 SetFlags, WantResult);
1213 unsigned AArch64FastISel::emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
1214 bool LHSIsKill, unsigned RHSReg,
1215 bool RHSIsKill, bool SetFlags,
1217 assert(LHSReg && RHSReg && "Invalid register number.");
1219 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1222 static const unsigned OpcTable[2][2][2] = {
1223 { { AArch64::SUBWrr, AArch64::SUBXrr },
1224 { AArch64::ADDWrr, AArch64::ADDXrr } },
1225 { { AArch64::SUBSWrr, AArch64::SUBSXrr },
1226 { AArch64::ADDSWrr, AArch64::ADDSXrr } }
1228 bool Is64Bit = RetVT == MVT::i64;
1229 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1230 const TargetRegisterClass *RC =
1231 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1234 ResultReg = createResultReg(RC);
1236 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1238 const MCInstrDesc &II = TII.get(Opc);
1239 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1240 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1241 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1242 .addReg(LHSReg, getKillRegState(LHSIsKill))
1243 .addReg(RHSReg, getKillRegState(RHSIsKill));
1247 unsigned AArch64FastISel::emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
1248 bool LHSIsKill, uint64_t Imm,
1249 bool SetFlags, bool WantResult) {
1250 assert(LHSReg && "Invalid register number.");
1252 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1256 if (isUInt<12>(Imm))
1258 else if ((Imm & 0xfff000) == Imm) {
1264 static const unsigned OpcTable[2][2][2] = {
1265 { { AArch64::SUBWri, AArch64::SUBXri },
1266 { AArch64::ADDWri, AArch64::ADDXri } },
1267 { { AArch64::SUBSWri, AArch64::SUBSXri },
1268 { AArch64::ADDSWri, AArch64::ADDSXri } }
1270 bool Is64Bit = RetVT == MVT::i64;
1271 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1272 const TargetRegisterClass *RC;
1274 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1276 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1279 ResultReg = createResultReg(RC);
1281 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1283 const MCInstrDesc &II = TII.get(Opc);
1284 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1285 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1286 .addReg(LHSReg, getKillRegState(LHSIsKill))
1288 .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
1292 unsigned AArch64FastISel::emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
1293 bool LHSIsKill, unsigned RHSReg,
1295 AArch64_AM::ShiftExtendType ShiftType,
1296 uint64_t ShiftImm, bool SetFlags,
1298 assert(LHSReg && RHSReg && "Invalid register number.");
1300 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1303 static const unsigned OpcTable[2][2][2] = {
1304 { { AArch64::SUBWrs, AArch64::SUBXrs },
1305 { AArch64::ADDWrs, AArch64::ADDXrs } },
1306 { { AArch64::SUBSWrs, AArch64::SUBSXrs },
1307 { AArch64::ADDSWrs, AArch64::ADDSXrs } }
1309 bool Is64Bit = RetVT == MVT::i64;
1310 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1311 const TargetRegisterClass *RC =
1312 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1315 ResultReg = createResultReg(RC);
1317 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1319 const MCInstrDesc &II = TII.get(Opc);
1320 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1321 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1322 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1323 .addReg(LHSReg, getKillRegState(LHSIsKill))
1324 .addReg(RHSReg, getKillRegState(RHSIsKill))
1325 .addImm(getShifterImm(ShiftType, ShiftImm));
1329 unsigned AArch64FastISel::emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
1330 bool LHSIsKill, unsigned RHSReg,
1332 AArch64_AM::ShiftExtendType ExtType,
1333 uint64_t ShiftImm, bool SetFlags,
1335 assert(LHSReg && RHSReg && "Invalid register number.");
1337 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1340 static const unsigned OpcTable[2][2][2] = {
1341 { { AArch64::SUBWrx, AArch64::SUBXrx },
1342 { AArch64::ADDWrx, AArch64::ADDXrx } },
1343 { { AArch64::SUBSWrx, AArch64::SUBSXrx },
1344 { AArch64::ADDSWrx, AArch64::ADDSXrx } }
1346 bool Is64Bit = RetVT == MVT::i64;
1347 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1348 const TargetRegisterClass *RC = nullptr;
1350 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1352 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1355 ResultReg = createResultReg(RC);
1357 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1359 const MCInstrDesc &II = TII.get(Opc);
1360 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1361 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1362 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1363 .addReg(LHSReg, getKillRegState(LHSIsKill))
1364 .addReg(RHSReg, getKillRegState(RHSIsKill))
1365 .addImm(getArithExtendImm(ExtType, ShiftImm));
1369 bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1370 Type *Ty = LHS->getType();
1371 EVT EVT = TLI.getValueType(Ty, true);
1372 if (!EVT.isSimple())
1374 MVT VT = EVT.getSimpleVT();
1376 switch (VT.SimpleTy) {
1384 return emitICmp(VT, LHS, RHS, IsZExt);
1387 return emitFCmp(VT, LHS, RHS);
1391 bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1393 return emitSub(RetVT, LHS, RHS, /*SetFlags=*/true, /*WantResult=*/false,
1397 bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1399 return emitAddSub_ri(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, Imm,
1400 /*SetFlags=*/true, /*WantResult=*/false) != 0;
1403 bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1404 if (RetVT != MVT::f32 && RetVT != MVT::f64)
1407 // Check to see if the 2nd operand is a constant that we can encode directly
1409 bool UseImm = false;
1410 if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1411 if (CFP->isZero() && !CFP->isNegative())
1414 unsigned LHSReg = getRegForValue(LHS);
1417 bool LHSIsKill = hasTrivialKill(LHS);
1420 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1422 .addReg(LHSReg, getKillRegState(LHSIsKill));
1426 unsigned RHSReg = getRegForValue(RHS);
1429 bool RHSIsKill = hasTrivialKill(RHS);
1431 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1432 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1433 .addReg(LHSReg, getKillRegState(LHSIsKill))
1434 .addReg(RHSReg, getKillRegState(RHSIsKill));
1438 unsigned AArch64FastISel::emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
1439 bool SetFlags, bool WantResult, bool IsZExt) {
1440 return emitAddSub(/*UseAdd=*/true, RetVT, LHS, RHS, SetFlags, WantResult,
1444 unsigned AArch64FastISel::emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
1445 bool SetFlags, bool WantResult, bool IsZExt) {
1446 return emitAddSub(/*UseAdd=*/false, RetVT, LHS, RHS, SetFlags, WantResult,
1450 unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1451 bool LHSIsKill, unsigned RHSReg,
1452 bool RHSIsKill, bool WantResult) {
1453 return emitAddSub_rr(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1454 RHSIsKill, /*SetFlags=*/true, WantResult);
1457 unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1458 bool LHSIsKill, unsigned RHSReg,
1460 AArch64_AM::ShiftExtendType ShiftType,
1461 uint64_t ShiftImm, bool WantResult) {
1462 return emitAddSub_rs(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1463 RHSIsKill, ShiftType, ShiftImm, /*SetFlags=*/true,
1467 unsigned AArch64FastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
1468 const Value *LHS, const Value *RHS) {
1469 // Canonicalize immediates to the RHS first.
1470 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1471 std::swap(LHS, RHS);
1473 // Canonicalize mul by power-of-2 to the RHS.
1474 if (LHS->hasOneUse() && isValueAvailable(LHS))
1475 if (isMulPowOf2(LHS))
1476 std::swap(LHS, RHS);
1478 // Canonicalize shift immediate to the RHS.
1479 if (LHS->hasOneUse() && isValueAvailable(LHS))
1480 if (const auto *SI = dyn_cast<ShlOperator>(LHS))
1481 if (isa<ConstantInt>(SI->getOperand(1)))
1482 std::swap(LHS, RHS);
1484 unsigned LHSReg = getRegForValue(LHS);
1487 bool LHSIsKill = hasTrivialKill(LHS);
1489 unsigned ResultReg = 0;
1490 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1491 uint64_t Imm = C->getZExtValue();
1492 ResultReg = emitLogicalOp_ri(ISDOpc, RetVT, LHSReg, LHSIsKill, Imm);
1497 // Check if the mul can be folded into the instruction.
1498 if (RHS->hasOneUse() && isValueAvailable(RHS))
1499 if (isMulPowOf2(RHS)) {
1500 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1501 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1503 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1504 if (C->getValue().isPowerOf2())
1505 std::swap(MulLHS, MulRHS);
1507 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1508 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1510 unsigned RHSReg = getRegForValue(MulLHS);
1513 bool RHSIsKill = hasTrivialKill(MulLHS);
1514 return emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1515 RHSIsKill, ShiftVal);
1518 // Check if the shift can be folded into the instruction.
1519 if (RHS->hasOneUse() && isValueAvailable(RHS))
1520 if (const auto *SI = dyn_cast<ShlOperator>(RHS))
1521 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1522 uint64_t ShiftVal = C->getZExtValue();
1523 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1526 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1527 return emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1528 RHSIsKill, ShiftVal);
1531 unsigned RHSReg = getRegForValue(RHS);
1534 bool RHSIsKill = hasTrivialKill(RHS);
1536 MVT VT = std::max(MVT::i32, RetVT.SimpleTy);
1537 ResultReg = fastEmit_rr(VT, VT, ISDOpc, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1538 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1539 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1540 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1545 unsigned AArch64FastISel::emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT,
1546 unsigned LHSReg, bool LHSIsKill,
1548 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1549 "ISD nodes are not consecutive!");
1550 static const unsigned OpcTable[3][2] = {
1551 { AArch64::ANDWri, AArch64::ANDXri },
1552 { AArch64::ORRWri, AArch64::ORRXri },
1553 { AArch64::EORWri, AArch64::EORXri }
1555 const TargetRegisterClass *RC;
1558 switch (RetVT.SimpleTy) {
1565 unsigned Idx = ISDOpc - ISD::AND;
1566 Opc = OpcTable[Idx][0];
1567 RC = &AArch64::GPR32spRegClass;
1572 Opc = OpcTable[ISDOpc - ISD::AND][1];
1573 RC = &AArch64::GPR64spRegClass;
1578 if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
1581 unsigned ResultReg =
1582 fastEmitInst_ri(Opc, RC, LHSReg, LHSIsKill,
1583 AArch64_AM::encodeLogicalImmediate(Imm, RegSize));
1584 if (RetVT >= MVT::i8 && RetVT <= MVT::i16 && ISDOpc != ISD::AND) {
1585 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1586 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1591 unsigned AArch64FastISel::emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT,
1592 unsigned LHSReg, bool LHSIsKill,
1593 unsigned RHSReg, bool RHSIsKill,
1594 uint64_t ShiftImm) {
1595 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1596 "ISD nodes are not consecutive!");
1597 static const unsigned OpcTable[3][2] = {
1598 { AArch64::ANDWrs, AArch64::ANDXrs },
1599 { AArch64::ORRWrs, AArch64::ORRXrs },
1600 { AArch64::EORWrs, AArch64::EORXrs }
1602 const TargetRegisterClass *RC;
1604 switch (RetVT.SimpleTy) {
1611 Opc = OpcTable[ISDOpc - ISD::AND][0];
1612 RC = &AArch64::GPR32RegClass;
1615 Opc = OpcTable[ISDOpc - ISD::AND][1];
1616 RC = &AArch64::GPR64RegClass;
1619 unsigned ResultReg =
1620 fastEmitInst_rri(Opc, RC, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1621 AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftImm));
1622 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1623 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1624 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1629 unsigned AArch64FastISel::emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1631 return emitLogicalOp_ri(ISD::AND, RetVT, LHSReg, LHSIsKill, Imm);
1634 bool AArch64FastISel::emitLoad(MVT VT, MVT RetVT, unsigned &ResultReg,
1635 Address Addr, bool WantZExt,
1636 MachineMemOperand *MMO) {
1637 // Simplify this down to something we can handle.
1638 if (!simplifyAddress(Addr, VT))
1641 unsigned ScaleFactor = getImplicitScaleFactor(VT);
1643 llvm_unreachable("Unexpected value type.");
1645 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1646 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1647 bool UseScaled = true;
1648 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1653 static const unsigned GPOpcTable[2][8][4] = {
1655 { { AArch64::LDURSBWi, AArch64::LDURSHWi, AArch64::LDURWi,
1657 { AArch64::LDURSBXi, AArch64::LDURSHXi, AArch64::LDURSWi,
1659 { AArch64::LDRSBWui, AArch64::LDRSHWui, AArch64::LDRWui,
1661 { AArch64::LDRSBXui, AArch64::LDRSHXui, AArch64::LDRSWui,
1663 { AArch64::LDRSBWroX, AArch64::LDRSHWroX, AArch64::LDRWroX,
1665 { AArch64::LDRSBXroX, AArch64::LDRSHXroX, AArch64::LDRSWroX,
1667 { AArch64::LDRSBWroW, AArch64::LDRSHWroW, AArch64::LDRWroW,
1669 { AArch64::LDRSBXroW, AArch64::LDRSHXroW, AArch64::LDRSWroW,
1673 { { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1675 { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1677 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1679 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1681 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1683 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1685 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1687 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1692 static const unsigned FPOpcTable[4][2] = {
1693 { AArch64::LDURSi, AArch64::LDURDi },
1694 { AArch64::LDRSui, AArch64::LDRDui },
1695 { AArch64::LDRSroX, AArch64::LDRDroX },
1696 { AArch64::LDRSroW, AArch64::LDRDroW }
1700 const TargetRegisterClass *RC;
1701 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1702 Addr.getOffsetReg();
1703 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1704 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1705 Addr.getExtendType() == AArch64_AM::SXTW)
1708 bool IsRet64Bit = RetVT == MVT::i64;
1709 switch (VT.SimpleTy) {
1711 llvm_unreachable("Unexpected value type.");
1712 case MVT::i1: // Intentional fall-through.
1714 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][0];
1715 RC = (IsRet64Bit && !WantZExt) ?
1716 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1719 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][1];
1720 RC = (IsRet64Bit && !WantZExt) ?
1721 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1724 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][2];
1725 RC = (IsRet64Bit && !WantZExt) ?
1726 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1729 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][3];
1730 RC = &AArch64::GPR64RegClass;
1733 Opc = FPOpcTable[Idx][0];
1734 RC = &AArch64::FPR32RegClass;
1737 Opc = FPOpcTable[Idx][1];
1738 RC = &AArch64::FPR64RegClass;
1742 // Create the base instruction, then add the operands.
1743 ResultReg = createResultReg(RC);
1744 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1745 TII.get(Opc), ResultReg);
1746 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
1748 // For zero-extending loads to 64bit we emit a 32bit load and then convert
1749 // the w-reg to an x-reg. In the end this is just an noop and will be removed.
1750 if (WantZExt && RetVT == MVT::i64 && VT <= MVT::i32) {
1751 unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
1752 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1753 TII.get(AArch64::SUBREG_TO_REG), Reg64)
1755 .addReg(ResultReg, getKillRegState(true))
1756 .addImm(AArch64::sub_32);
1760 // Loading an i1 requires special handling.
1761 if (VT == MVT::i1) {
1762 unsigned ANDReg = emitAnd_ri(IsRet64Bit ? MVT::i64 : MVT::i32, ResultReg,
1763 /*IsKill=*/true, 1);
1764 assert(ANDReg && "Unexpected AND instruction emission failure.");
1770 bool AArch64FastISel::selectAddSub(const Instruction *I) {
1772 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1776 return selectOperator(I, I->getOpcode());
1779 switch (I->getOpcode()) {
1781 llvm_unreachable("Unexpected instruction.");
1782 case Instruction::Add:
1783 ResultReg = emitAdd(VT, I->getOperand(0), I->getOperand(1));
1785 case Instruction::Sub:
1786 ResultReg = emitSub(VT, I->getOperand(0), I->getOperand(1));
1792 updateValueMap(I, ResultReg);
1796 bool AArch64FastISel::selectLogicalOp(const Instruction *I) {
1798 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1802 return selectOperator(I, I->getOpcode());
1805 switch (I->getOpcode()) {
1807 llvm_unreachable("Unexpected instruction.");
1808 case Instruction::And:
1809 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
1811 case Instruction::Or:
1812 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
1814 case Instruction::Xor:
1815 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
1821 updateValueMap(I, ResultReg);
1825 bool AArch64FastISel::selectLoad(const Instruction *I) {
1827 // Verify we have a legal type before going any further. Currently, we handle
1828 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1829 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1830 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true) ||
1831 cast<LoadInst>(I)->isAtomic())
1834 // See if we can handle this address.
1836 if (!computeAddress(I->getOperand(0), Addr, I->getType()))
1839 bool WantZExt = true;
1841 if (I->hasOneUse()) {
1842 if (const auto *ZE = dyn_cast<ZExtInst>(I->use_begin()->getUser())) {
1843 if (!isTypeSupported(ZE->getType(), RetVT, /*IsVectorAllowed=*/false))
1845 } else if (const auto *SE = dyn_cast<SExtInst>(I->use_begin()->getUser())) {
1846 if (!isTypeSupported(SE->getType(), RetVT, /*IsVectorAllowed=*/false))
1853 if (!emitLoad(VT, RetVT, ResultReg, Addr, WantZExt,
1854 createMachineMemOperandFor(I)))
1857 updateValueMap(I, ResultReg);
1861 bool AArch64FastISel::emitStore(MVT VT, unsigned SrcReg, Address Addr,
1862 MachineMemOperand *MMO) {
1863 // Simplify this down to something we can handle.
1864 if (!simplifyAddress(Addr, VT))
1867 unsigned ScaleFactor = getImplicitScaleFactor(VT);
1869 llvm_unreachable("Unexpected value type.");
1871 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1872 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1873 bool UseScaled = true;
1874 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1879 static const unsigned OpcTable[4][6] = {
1880 { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
1881 AArch64::STURSi, AArch64::STURDi },
1882 { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
1883 AArch64::STRSui, AArch64::STRDui },
1884 { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
1885 AArch64::STRSroX, AArch64::STRDroX },
1886 { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
1887 AArch64::STRSroW, AArch64::STRDroW }
1891 bool VTIsi1 = false;
1892 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1893 Addr.getOffsetReg();
1894 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1895 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1896 Addr.getExtendType() == AArch64_AM::SXTW)
1899 switch (VT.SimpleTy) {
1900 default: llvm_unreachable("Unexpected value type.");
1901 case MVT::i1: VTIsi1 = true;
1902 case MVT::i8: Opc = OpcTable[Idx][0]; break;
1903 case MVT::i16: Opc = OpcTable[Idx][1]; break;
1904 case MVT::i32: Opc = OpcTable[Idx][2]; break;
1905 case MVT::i64: Opc = OpcTable[Idx][3]; break;
1906 case MVT::f32: Opc = OpcTable[Idx][4]; break;
1907 case MVT::f64: Opc = OpcTable[Idx][5]; break;
1910 // Storing an i1 requires special handling.
1911 if (VTIsi1 && SrcReg != AArch64::WZR) {
1912 unsigned ANDReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
1913 assert(ANDReg && "Unexpected AND instruction emission failure.");
1916 // Create the base instruction, then add the operands.
1917 const MCInstrDesc &II = TII.get(Opc);
1918 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
1919 MachineInstrBuilder MIB =
1920 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(SrcReg);
1921 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
1926 bool AArch64FastISel::selectStore(const Instruction *I) {
1928 const Value *Op0 = I->getOperand(0);
1929 // Verify we have a legal type before going any further. Currently, we handle
1930 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1931 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1932 if (!isTypeSupported(Op0->getType(), VT, /*IsVectorAllowed=*/true) ||
1933 cast<StoreInst>(I)->isAtomic())
1936 // Get the value to be stored into a register. Use the zero register directly
1937 // when possible to avoid an unnecessary copy and a wasted register.
1938 unsigned SrcReg = 0;
1939 if (const auto *CI = dyn_cast<ConstantInt>(Op0)) {
1941 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
1942 } else if (const auto *CF = dyn_cast<ConstantFP>(Op0)) {
1943 if (CF->isZero() && !CF->isNegative()) {
1944 VT = MVT::getIntegerVT(VT.getSizeInBits());
1945 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
1950 SrcReg = getRegForValue(Op0);
1955 // See if we can handle this address.
1957 if (!computeAddress(I->getOperand(1), Addr, I->getOperand(0)->getType()))
1960 if (!emitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
1965 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
1967 case CmpInst::FCMP_ONE:
1968 case CmpInst::FCMP_UEQ:
1970 // AL is our "false" for now. The other two need more compares.
1971 return AArch64CC::AL;
1972 case CmpInst::ICMP_EQ:
1973 case CmpInst::FCMP_OEQ:
1974 return AArch64CC::EQ;
1975 case CmpInst::ICMP_SGT:
1976 case CmpInst::FCMP_OGT:
1977 return AArch64CC::GT;
1978 case CmpInst::ICMP_SGE:
1979 case CmpInst::FCMP_OGE:
1980 return AArch64CC::GE;
1981 case CmpInst::ICMP_UGT:
1982 case CmpInst::FCMP_UGT:
1983 return AArch64CC::HI;
1984 case CmpInst::FCMP_OLT:
1985 return AArch64CC::MI;
1986 case CmpInst::ICMP_ULE:
1987 case CmpInst::FCMP_OLE:
1988 return AArch64CC::LS;
1989 case CmpInst::FCMP_ORD:
1990 return AArch64CC::VC;
1991 case CmpInst::FCMP_UNO:
1992 return AArch64CC::VS;
1993 case CmpInst::FCMP_UGE:
1994 return AArch64CC::PL;
1995 case CmpInst::ICMP_SLT:
1996 case CmpInst::FCMP_ULT:
1997 return AArch64CC::LT;
1998 case CmpInst::ICMP_SLE:
1999 case CmpInst::FCMP_ULE:
2000 return AArch64CC::LE;
2001 case CmpInst::FCMP_UNE:
2002 case CmpInst::ICMP_NE:
2003 return AArch64CC::NE;
2004 case CmpInst::ICMP_UGE:
2005 return AArch64CC::HS;
2006 case CmpInst::ICMP_ULT:
2007 return AArch64CC::LO;
2011 /// \brief Try to emit a combined compare-and-branch instruction.
2012 bool AArch64FastISel::emitCompareAndBranch(const BranchInst *BI) {
2013 assert(isa<CmpInst>(BI->getCondition()) && "Expected cmp instruction");
2014 const CmpInst *CI = cast<CmpInst>(BI->getCondition());
2015 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2017 const Value *LHS = CI->getOperand(0);
2018 const Value *RHS = CI->getOperand(1);
2020 Type *Ty = LHS->getType();
2021 if (!Ty->isIntegerTy())
2024 unsigned BW = cast<IntegerType>(Ty)->getBitWidth();
2025 if (BW != 1 && BW != 8 && BW != 16 && BW != 32 && BW != 64)
2028 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2029 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2031 // Try to take advantage of fallthrough opportunities.
2032 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2033 std::swap(TBB, FBB);
2034 Predicate = CmpInst::getInversePredicate(Predicate);
2039 if ((Predicate == CmpInst::ICMP_EQ) || (Predicate == CmpInst::ICMP_NE)) {
2040 if (const auto *C = dyn_cast<ConstantInt>(LHS))
2041 if (C->isNullValue())
2042 std::swap(LHS, RHS);
2044 if (!isa<ConstantInt>(RHS))
2047 if (!cast<ConstantInt>(RHS)->isNullValue())
2050 if (const auto *AI = dyn_cast<BinaryOperator>(LHS))
2051 if (AI->getOpcode() == Instruction::And) {
2052 const Value *AndLHS = AI->getOperand(0);
2053 const Value *AndRHS = AI->getOperand(1);
2055 if (const auto *C = dyn_cast<ConstantInt>(AndLHS))
2056 if (C->getValue().isPowerOf2())
2057 std::swap(AndLHS, AndRHS);
2059 if (const auto *C = dyn_cast<ConstantInt>(AndRHS))
2060 if (C->getValue().isPowerOf2()) {
2061 TestBit = C->getValue().logBase2();
2065 IsCmpNE = Predicate == CmpInst::ICMP_NE;
2066 } else if (Predicate == CmpInst::ICMP_SLT) {
2067 if (!isa<ConstantInt>(RHS))
2070 if (!cast<ConstantInt>(RHS)->isNullValue())
2075 } else if (Predicate == CmpInst::ICMP_SGT) {
2076 if (!isa<ConstantInt>(RHS))
2079 if (cast<ConstantInt>(RHS)->getValue() != -1)
2087 static const unsigned OpcTable[2][2][2] = {
2088 { {AArch64::CBZW, AArch64::CBZX },
2089 {AArch64::CBNZW, AArch64::CBNZX} },
2090 { {AArch64::TBZW, AArch64::TBZX },
2091 {AArch64::TBNZW, AArch64::TBNZX} }
2094 bool IsBitTest = TestBit != -1;
2095 bool Is64Bit = BW == 64;
2096 if (TestBit < 32 && TestBit >= 0)
2099 unsigned Opc = OpcTable[IsBitTest][IsCmpNE][Is64Bit];
2100 const MCInstrDesc &II = TII.get(Opc);
2102 unsigned SrcReg = getRegForValue(LHS);
2105 bool SrcIsKill = hasTrivialKill(LHS);
2107 if (BW == 64 && !Is64Bit) {
2108 SrcReg = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
2110 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
2113 // Emit the combined compare and branch instruction.
2114 MachineInstrBuilder MIB =
2115 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
2116 .addReg(SrcReg, getKillRegState(SrcIsKill));
2118 MIB.addImm(TestBit);
2121 // Obtain the branch weight and add the TrueBB to the successor list.
2122 uint32_t BranchWeight = 0;
2124 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2125 TBB->getBasicBlock());
2126 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2127 fastEmitBranch(FBB, DbgLoc);
2132 bool AArch64FastISel::selectBranch(const Instruction *I) {
2133 const BranchInst *BI = cast<BranchInst>(I);
2134 if (BI->isUnconditional()) {
2135 MachineBasicBlock *MSucc = FuncInfo.MBBMap[BI->getSuccessor(0)];
2136 fastEmitBranch(MSucc, BI->getDebugLoc());
2140 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2141 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2143 AArch64CC::CondCode CC = AArch64CC::NE;
2144 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
2145 if (CI->hasOneUse() && isValueAvailable(CI)) {
2146 // Try to optimize or fold the cmp.
2147 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2148 switch (Predicate) {
2151 case CmpInst::FCMP_FALSE:
2152 fastEmitBranch(FBB, DbgLoc);
2154 case CmpInst::FCMP_TRUE:
2155 fastEmitBranch(TBB, DbgLoc);
2159 // Try to emit a combined compare-and-branch first.
2160 if (emitCompareAndBranch(BI))
2163 // Try to take advantage of fallthrough opportunities.
2164 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2165 std::swap(TBB, FBB);
2166 Predicate = CmpInst::getInversePredicate(Predicate);
2170 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2173 // FCMP_UEQ and FCMP_ONE cannot be checked with a single branch
2175 CC = getCompareCC(Predicate);
2176 AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2177 switch (Predicate) {
2180 case CmpInst::FCMP_UEQ:
2181 ExtraCC = AArch64CC::EQ;
2184 case CmpInst::FCMP_ONE:
2185 ExtraCC = AArch64CC::MI;
2189 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2191 // Emit the extra branch for FCMP_UEQ and FCMP_ONE.
2192 if (ExtraCC != AArch64CC::AL) {
2193 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2199 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2203 // Obtain the branch weight and add the TrueBB to the successor list.
2204 uint32_t BranchWeight = 0;
2206 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2207 TBB->getBasicBlock());
2208 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2210 fastEmitBranch(FBB, DbgLoc);
2213 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
2215 if (TI->hasOneUse() && isValueAvailable(TI) &&
2216 isTypeSupported(TI->getOperand(0)->getType(), SrcVT)) {
2217 unsigned CondReg = getRegForValue(TI->getOperand(0));
2220 bool CondIsKill = hasTrivialKill(TI->getOperand(0));
2222 // Issue an extract_subreg to get the lower 32-bits.
2223 if (SrcVT == MVT::i64) {
2224 CondReg = fastEmitInst_extractsubreg(MVT::i32, CondReg, CondIsKill,
2229 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
2230 assert(ANDReg && "Unexpected AND instruction emission failure.");
2231 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2233 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2234 std::swap(TBB, FBB);
2237 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2241 // Obtain the branch weight and add the TrueBB to the successor list.
2242 uint32_t BranchWeight = 0;
2244 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2245 TBB->getBasicBlock());
2246 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2248 fastEmitBranch(FBB, DbgLoc);
2251 } else if (const auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
2252 uint64_t Imm = CI->getZExtValue();
2253 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
2254 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
2257 // Obtain the branch weight and add the target to the successor list.
2258 uint32_t BranchWeight = 0;
2260 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2261 Target->getBasicBlock());
2262 FuncInfo.MBB->addSuccessor(Target, BranchWeight);
2264 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
2265 // Fake request the condition, otherwise the intrinsic might be completely
2267 unsigned CondReg = getRegForValue(BI->getCondition());
2272 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2276 // Obtain the branch weight and add the TrueBB to the successor list.
2277 uint32_t BranchWeight = 0;
2279 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2280 TBB->getBasicBlock());
2281 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2283 fastEmitBranch(FBB, DbgLoc);
2287 unsigned CondReg = getRegForValue(BI->getCondition());
2290 bool CondRegIsKill = hasTrivialKill(BI->getCondition());
2292 // We've been divorced from our compare! Our block was split, and
2293 // now our compare lives in a predecessor block. We musn't
2294 // re-compare here, as the children of the compare aren't guaranteed
2295 // live across the block boundary (we *could* check for this).
2296 // Regardless, the compare has been done in the predecessor block,
2297 // and it left a value for us in a virtual register. Ergo, we test
2298 // the one-bit value left in the virtual register.
2299 emitICmp_ri(MVT::i32, CondReg, CondRegIsKill, 0);
2301 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2302 std::swap(TBB, FBB);
2306 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2310 // Obtain the branch weight and add the TrueBB to the successor list.
2311 uint32_t BranchWeight = 0;
2313 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2314 TBB->getBasicBlock());
2315 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2317 fastEmitBranch(FBB, DbgLoc);
2321 bool AArch64FastISel::selectIndirectBr(const Instruction *I) {
2322 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
2323 unsigned AddrReg = getRegForValue(BI->getOperand(0));
2327 // Emit the indirect branch.
2328 const MCInstrDesc &II = TII.get(AArch64::BR);
2329 AddrReg = constrainOperandRegClass(II, AddrReg, II.getNumDefs());
2330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(AddrReg);
2332 // Make sure the CFG is up-to-date.
2333 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
2334 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
2339 bool AArch64FastISel::selectCmp(const Instruction *I) {
2340 const CmpInst *CI = cast<CmpInst>(I);
2342 // Try to optimize or fold the cmp.
2343 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2344 unsigned ResultReg = 0;
2345 switch (Predicate) {
2348 case CmpInst::FCMP_FALSE:
2349 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2350 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2351 TII.get(TargetOpcode::COPY), ResultReg)
2352 .addReg(AArch64::WZR, getKillRegState(true));
2354 case CmpInst::FCMP_TRUE:
2355 ResultReg = fastEmit_i(MVT::i32, MVT::i32, ISD::Constant, 1);
2360 updateValueMap(I, ResultReg);
2365 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2368 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2370 // FCMP_UEQ and FCMP_ONE cannot be checked with a single instruction. These
2371 // condition codes are inverted, because they are used by CSINC.
2372 static unsigned CondCodeTable[2][2] = {
2373 { AArch64CC::NE, AArch64CC::VC },
2374 { AArch64CC::PL, AArch64CC::LE }
2376 unsigned *CondCodes = nullptr;
2377 switch (Predicate) {
2380 case CmpInst::FCMP_UEQ:
2381 CondCodes = &CondCodeTable[0][0];
2383 case CmpInst::FCMP_ONE:
2384 CondCodes = &CondCodeTable[1][0];
2389 unsigned TmpReg1 = createResultReg(&AArch64::GPR32RegClass);
2390 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2392 .addReg(AArch64::WZR, getKillRegState(true))
2393 .addReg(AArch64::WZR, getKillRegState(true))
2394 .addImm(CondCodes[0]);
2395 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2397 .addReg(TmpReg1, getKillRegState(true))
2398 .addReg(AArch64::WZR, getKillRegState(true))
2399 .addImm(CondCodes[1]);
2401 updateValueMap(I, ResultReg);
2405 // Now set a register based on the comparison.
2406 AArch64CC::CondCode CC = getCompareCC(Predicate);
2407 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2408 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
2409 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2411 .addReg(AArch64::WZR, getKillRegState(true))
2412 .addReg(AArch64::WZR, getKillRegState(true))
2413 .addImm(invertedCC);
2415 updateValueMap(I, ResultReg);
2419 bool AArch64FastISel::selectSelect(const Instruction *I) {
2420 const SelectInst *SI = cast<SelectInst>(I);
2422 EVT DestEVT = TLI.getValueType(SI->getType(), true);
2423 if (!DestEVT.isSimple())
2426 MVT DestVT = DestEVT.getSimpleVT();
2427 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
2432 const TargetRegisterClass *RC = nullptr;
2433 switch (DestVT.SimpleTy) {
2434 default: return false;
2436 SelectOpc = AArch64::CSELWr; RC = &AArch64::GPR32RegClass; break;
2438 SelectOpc = AArch64::CSELXr; RC = &AArch64::GPR64RegClass; break;
2440 SelectOpc = AArch64::FCSELSrrr; RC = &AArch64::FPR32RegClass; break;
2442 SelectOpc = AArch64::FCSELDrrr; RC = &AArch64::FPR64RegClass; break;
2445 const Value *Cond = SI->getCondition();
2446 bool NeedTest = true;
2447 AArch64CC::CondCode CC = AArch64CC::NE;
2448 if (foldXALUIntrinsic(CC, I, Cond))
2451 unsigned CondReg = getRegForValue(Cond);
2454 bool CondIsKill = hasTrivialKill(Cond);
2457 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
2458 assert(ANDReg && "Unexpected AND instruction emission failure.");
2459 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2462 unsigned TrueReg = getRegForValue(SI->getTrueValue());
2463 bool TrueIsKill = hasTrivialKill(SI->getTrueValue());
2465 unsigned FalseReg = getRegForValue(SI->getFalseValue());
2466 bool FalseIsKill = hasTrivialKill(SI->getFalseValue());
2468 if (!TrueReg || !FalseReg)
2471 unsigned ResultReg = fastEmitInst_rri(SelectOpc, RC, TrueReg, TrueIsKill,
2472 FalseReg, FalseIsKill, CC);
2473 updateValueMap(I, ResultReg);
2477 bool AArch64FastISel::selectFPExt(const Instruction *I) {
2478 Value *V = I->getOperand(0);
2479 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
2482 unsigned Op = getRegForValue(V);
2486 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
2487 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
2488 ResultReg).addReg(Op);
2489 updateValueMap(I, ResultReg);
2493 bool AArch64FastISel::selectFPTrunc(const Instruction *I) {
2494 Value *V = I->getOperand(0);
2495 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
2498 unsigned Op = getRegForValue(V);
2502 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
2503 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
2504 ResultReg).addReg(Op);
2505 updateValueMap(I, ResultReg);
2509 // FPToUI and FPToSI
2510 bool AArch64FastISel::selectFPToInt(const Instruction *I, bool Signed) {
2512 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2515 unsigned SrcReg = getRegForValue(I->getOperand(0));
2519 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2520 if (SrcVT == MVT::f128)
2524 if (SrcVT == MVT::f64) {
2526 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
2528 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
2531 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
2533 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
2535 unsigned ResultReg = createResultReg(
2536 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
2537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2539 updateValueMap(I, ResultReg);
2543 bool AArch64FastISel::selectIntToFP(const Instruction *I, bool Signed) {
2545 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2547 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
2548 "Unexpected value type.");
2550 unsigned SrcReg = getRegForValue(I->getOperand(0));
2553 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
2555 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2557 // Handle sign-extension.
2558 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
2560 emitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
2567 if (SrcVT == MVT::i64) {
2569 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
2571 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
2574 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
2576 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
2579 unsigned ResultReg = fastEmitInst_r(Opc, TLI.getRegClassFor(DestVT), SrcReg,
2581 updateValueMap(I, ResultReg);
2585 bool AArch64FastISel::fastLowerArguments() {
2586 if (!FuncInfo.CanLowerReturn)
2589 const Function *F = FuncInfo.Fn;
2593 CallingConv::ID CC = F->getCallingConv();
2594 if (CC != CallingConv::C)
2597 // Only handle simple cases of up to 8 GPR and FPR each.
2598 unsigned GPRCnt = 0;
2599 unsigned FPRCnt = 0;
2601 for (auto const &Arg : F->args()) {
2602 // The first argument is at index 1.
2604 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2605 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2606 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2607 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2610 Type *ArgTy = Arg.getType();
2611 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
2614 EVT ArgVT = TLI.getValueType(ArgTy);
2615 if (!ArgVT.isSimple())
2618 MVT VT = ArgVT.getSimpleVT().SimpleTy;
2619 if (VT.isFloatingPoint() && !Subtarget->hasFPARMv8())
2622 if (VT.isVector() &&
2623 (!Subtarget->hasNEON() || !Subtarget->isLittleEndian()))
2626 if (VT >= MVT::i1 && VT <= MVT::i64)
2628 else if ((VT >= MVT::f16 && VT <= MVT::f64) || VT.is64BitVector() ||
2629 VT.is128BitVector())
2634 if (GPRCnt > 8 || FPRCnt > 8)
2638 static const MCPhysReg Registers[6][8] = {
2639 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
2640 AArch64::W5, AArch64::W6, AArch64::W7 },
2641 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
2642 AArch64::X5, AArch64::X6, AArch64::X7 },
2643 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
2644 AArch64::H5, AArch64::H6, AArch64::H7 },
2645 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
2646 AArch64::S5, AArch64::S6, AArch64::S7 },
2647 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
2648 AArch64::D5, AArch64::D6, AArch64::D7 },
2649 { AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, AArch64::Q4,
2650 AArch64::Q5, AArch64::Q6, AArch64::Q7 }
2653 unsigned GPRIdx = 0;
2654 unsigned FPRIdx = 0;
2655 for (auto const &Arg : F->args()) {
2656 MVT VT = TLI.getSimpleValueType(Arg.getType());
2658 const TargetRegisterClass *RC;
2659 if (VT >= MVT::i1 && VT <= MVT::i32) {
2660 SrcReg = Registers[0][GPRIdx++];
2661 RC = &AArch64::GPR32RegClass;
2663 } else if (VT == MVT::i64) {
2664 SrcReg = Registers[1][GPRIdx++];
2665 RC = &AArch64::GPR64RegClass;
2666 } else if (VT == MVT::f16) {
2667 SrcReg = Registers[2][FPRIdx++];
2668 RC = &AArch64::FPR16RegClass;
2669 } else if (VT == MVT::f32) {
2670 SrcReg = Registers[3][FPRIdx++];
2671 RC = &AArch64::FPR32RegClass;
2672 } else if ((VT == MVT::f64) || VT.is64BitVector()) {
2673 SrcReg = Registers[4][FPRIdx++];
2674 RC = &AArch64::FPR64RegClass;
2675 } else if (VT.is128BitVector()) {
2676 SrcReg = Registers[5][FPRIdx++];
2677 RC = &AArch64::FPR128RegClass;
2679 llvm_unreachable("Unexpected value type.");
2681 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2682 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2683 // Without this, EmitLiveInCopies may eliminate the livein if its only
2684 // use is a bitcast (which isn't turned into an instruction).
2685 unsigned ResultReg = createResultReg(RC);
2686 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2687 TII.get(TargetOpcode::COPY), ResultReg)
2688 .addReg(DstReg, getKillRegState(true));
2689 updateValueMap(&Arg, ResultReg);
2694 bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
2695 SmallVectorImpl<MVT> &OutVTs,
2696 unsigned &NumBytes) {
2697 CallingConv::ID CC = CLI.CallConv;
2698 SmallVector<CCValAssign, 16> ArgLocs;
2699 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
2700 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
2702 // Get a count of how many bytes are to be pushed on the stack.
2703 NumBytes = CCInfo.getNextStackOffset();
2705 // Issue CALLSEQ_START
2706 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
2707 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
2710 // Process the args.
2711 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2712 CCValAssign &VA = ArgLocs[i];
2713 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
2714 MVT ArgVT = OutVTs[VA.getValNo()];
2716 unsigned ArgReg = getRegForValue(ArgVal);
2720 // Handle arg promotion: SExt, ZExt, AExt.
2721 switch (VA.getLocInfo()) {
2722 case CCValAssign::Full:
2724 case CCValAssign::SExt: {
2725 MVT DestVT = VA.getLocVT();
2727 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
2732 case CCValAssign::AExt:
2733 // Intentional fall-through.
2734 case CCValAssign::ZExt: {
2735 MVT DestVT = VA.getLocVT();
2737 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
2743 llvm_unreachable("Unknown arg promotion!");
2746 // Now copy/store arg to correct locations.
2747 if (VA.isRegLoc() && !VA.needsCustom()) {
2748 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2749 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
2750 CLI.OutRegs.push_back(VA.getLocReg());
2751 } else if (VA.needsCustom()) {
2752 // FIXME: Handle custom args.
2755 assert(VA.isMemLoc() && "Assuming store on stack.");
2757 // Don't emit stores for undef values.
2758 if (isa<UndefValue>(ArgVal))
2761 // Need to store on the stack.
2762 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
2764 unsigned BEAlign = 0;
2765 if (ArgSize < 8 && !Subtarget->isLittleEndian())
2766 BEAlign = 8 - ArgSize;
2769 Addr.setKind(Address::RegBase);
2770 Addr.setReg(AArch64::SP);
2771 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
2773 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
2774 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2775 MachinePointerInfo::getStack(Addr.getOffset()),
2776 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
2778 if (!emitStore(ArgVT, ArgReg, Addr, MMO))
2785 bool AArch64FastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
2786 unsigned NumBytes) {
2787 CallingConv::ID CC = CLI.CallConv;
2789 // Issue CALLSEQ_END
2790 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2791 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
2792 .addImm(NumBytes).addImm(0);
2794 // Now the return value.
2795 if (RetVT != MVT::isVoid) {
2796 SmallVector<CCValAssign, 16> RVLocs;
2797 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
2798 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
2800 // Only handle a single return value.
2801 if (RVLocs.size() != 1)
2804 // Copy all of the result registers out of their specified physreg.
2805 MVT CopyVT = RVLocs[0].getValVT();
2806 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
2807 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2808 TII.get(TargetOpcode::COPY), ResultReg)
2809 .addReg(RVLocs[0].getLocReg());
2810 CLI.InRegs.push_back(RVLocs[0].getLocReg());
2812 CLI.ResultReg = ResultReg;
2813 CLI.NumResultRegs = 1;
2819 bool AArch64FastISel::fastLowerCall(CallLoweringInfo &CLI) {
2820 CallingConv::ID CC = CLI.CallConv;
2821 bool IsTailCall = CLI.IsTailCall;
2822 bool IsVarArg = CLI.IsVarArg;
2823 const Value *Callee = CLI.Callee;
2824 const char *SymName = CLI.SymName;
2826 if (!Callee && !SymName)
2829 // Allow SelectionDAG isel to handle tail calls.
2833 CodeModel::Model CM = TM.getCodeModel();
2834 // Only support the small and large code model.
2835 if (CM != CodeModel::Small && CM != CodeModel::Large)
2838 // FIXME: Add large code model support for ELF.
2839 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
2842 // Let SDISel handle vararg functions.
2846 // FIXME: Only handle *simple* calls for now.
2848 if (CLI.RetTy->isVoidTy())
2849 RetVT = MVT::isVoid;
2850 else if (!isTypeLegal(CLI.RetTy, RetVT))
2853 for (auto Flag : CLI.OutFlags)
2854 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
2857 // Set up the argument vectors.
2858 SmallVector<MVT, 16> OutVTs;
2859 OutVTs.reserve(CLI.OutVals.size());
2861 for (auto *Val : CLI.OutVals) {
2863 if (!isTypeLegal(Val->getType(), VT) &&
2864 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
2867 // We don't handle vector parameters yet.
2868 if (VT.isVector() || VT.getSizeInBits() > 64)
2871 OutVTs.push_back(VT);
2875 if (Callee && !computeCallAddress(Callee, Addr))
2878 // Handle the arguments now that we've gotten them.
2880 if (!processCallArgs(CLI, OutVTs, NumBytes))
2884 MachineInstrBuilder MIB;
2885 if (CM == CodeModel::Small) {
2886 const MCInstrDesc &II = TII.get(Addr.getReg() ? AArch64::BLR : AArch64::BL);
2887 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II);
2889 MIB.addExternalSymbol(SymName, 0);
2890 else if (Addr.getGlobalValue())
2891 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
2892 else if (Addr.getReg()) {
2893 unsigned Reg = constrainOperandRegClass(II, Addr.getReg(), 0);
2898 unsigned CallReg = 0;
2900 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
2901 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
2903 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGE);
2905 CallReg = createResultReg(&AArch64::GPR64RegClass);
2906 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
2909 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
2911 } else if (Addr.getGlobalValue())
2912 CallReg = materializeGV(Addr.getGlobalValue());
2913 else if (Addr.getReg())
2914 CallReg = Addr.getReg();
2919 const MCInstrDesc &II = TII.get(AArch64::BLR);
2920 CallReg = constrainOperandRegClass(II, CallReg, 0);
2921 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(CallReg);
2924 // Add implicit physical register uses to the call.
2925 for (auto Reg : CLI.OutRegs)
2926 MIB.addReg(Reg, RegState::Implicit);
2928 // Add a register mask with the call-preserved registers.
2929 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2930 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2934 // Finish off the call including any return values.
2935 return finishCall(CLI, RetVT, NumBytes);
2938 bool AArch64FastISel::isMemCpySmall(uint64_t Len, unsigned Alignment) {
2940 return Len / Alignment <= 4;
2945 bool AArch64FastISel::tryEmitSmallMemCpy(Address Dest, Address Src,
2946 uint64_t Len, unsigned Alignment) {
2947 // Make sure we don't bloat code by inlining very large memcpy's.
2948 if (!isMemCpySmall(Len, Alignment))
2951 int64_t UnscaledOffset = 0;
2952 Address OrigDest = Dest;
2953 Address OrigSrc = Src;
2957 if (!Alignment || Alignment >= 8) {
2968 // Bound based on alignment.
2969 if (Len >= 4 && Alignment == 4)
2971 else if (Len >= 2 && Alignment == 2)
2980 RV = emitLoad(VT, VT, ResultReg, Src);
2984 RV = emitStore(VT, ResultReg, Dest);
2988 int64_t Size = VT.getSizeInBits() / 8;
2990 UnscaledOffset += Size;
2992 // We need to recompute the unscaled offset for each iteration.
2993 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
2994 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
3000 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
3001 /// into the user. The condition code will only be updated on success.
3002 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
3003 const Instruction *I,
3004 const Value *Cond) {
3005 if (!isa<ExtractValueInst>(Cond))
3008 const auto *EV = cast<ExtractValueInst>(Cond);
3009 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
3012 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
3014 const Function *Callee = II->getCalledFunction();
3016 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
3017 if (!isTypeLegal(RetTy, RetVT))
3020 if (RetVT != MVT::i32 && RetVT != MVT::i64)
3023 const Value *LHS = II->getArgOperand(0);
3024 const Value *RHS = II->getArgOperand(1);
3026 // Canonicalize immediate to the RHS.
3027 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3028 isCommutativeIntrinsic(II))
3029 std::swap(LHS, RHS);
3031 // Simplify multiplies.
3032 unsigned IID = II->getIntrinsicID();
3036 case Intrinsic::smul_with_overflow:
3037 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3038 if (C->getValue() == 2)
3039 IID = Intrinsic::sadd_with_overflow;
3041 case Intrinsic::umul_with_overflow:
3042 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3043 if (C->getValue() == 2)
3044 IID = Intrinsic::uadd_with_overflow;
3048 AArch64CC::CondCode TmpCC;
3052 case Intrinsic::sadd_with_overflow:
3053 case Intrinsic::ssub_with_overflow:
3054 TmpCC = AArch64CC::VS;
3056 case Intrinsic::uadd_with_overflow:
3057 TmpCC = AArch64CC::HS;
3059 case Intrinsic::usub_with_overflow:
3060 TmpCC = AArch64CC::LO;
3062 case Intrinsic::smul_with_overflow:
3063 case Intrinsic::umul_with_overflow:
3064 TmpCC = AArch64CC::NE;
3068 // Check if both instructions are in the same basic block.
3069 if (!isValueAvailable(II))
3072 // Make sure nothing is in the way
3073 BasicBlock::const_iterator Start = I;
3074 BasicBlock::const_iterator End = II;
3075 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
3076 // We only expect extractvalue instructions between the intrinsic and the
3077 // instruction to be selected.
3078 if (!isa<ExtractValueInst>(Itr))
3081 // Check that the extractvalue operand comes from the intrinsic.
3082 const auto *EVI = cast<ExtractValueInst>(Itr);
3083 if (EVI->getAggregateOperand() != II)
3091 bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
3092 // FIXME: Handle more intrinsics.
3093 switch (II->getIntrinsicID()) {
3094 default: return false;
3095 case Intrinsic::frameaddress: {
3096 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
3097 MFI->setFrameAddressIsTaken(true);
3099 const AArch64RegisterInfo *RegInfo =
3100 static_cast<const AArch64RegisterInfo *>(
3101 TM.getSubtargetImpl()->getRegisterInfo());
3102 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
3103 unsigned SrcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3104 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3105 TII.get(TargetOpcode::COPY), SrcReg).addReg(FramePtr);
3106 // Recursively load frame address
3112 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
3114 DestReg = fastEmitInst_ri(AArch64::LDRXui, &AArch64::GPR64RegClass,
3115 SrcReg, /*IsKill=*/true, 0);
3116 assert(DestReg && "Unexpected LDR instruction emission failure.");
3120 updateValueMap(II, SrcReg);
3123 case Intrinsic::memcpy:
3124 case Intrinsic::memmove: {
3125 const auto *MTI = cast<MemTransferInst>(II);
3126 // Don't handle volatile.
3127 if (MTI->isVolatile())
3130 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
3131 // we would emit dead code because we don't currently handle memmoves.
3132 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
3133 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
3134 // Small memcpy's are common enough that we want to do them without a call
3136 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
3137 unsigned Alignment = MTI->getAlignment();
3138 if (isMemCpySmall(Len, Alignment)) {
3140 if (!computeAddress(MTI->getRawDest(), Dest) ||
3141 !computeAddress(MTI->getRawSource(), Src))
3143 if (tryEmitSmallMemCpy(Dest, Src, Len, Alignment))
3148 if (!MTI->getLength()->getType()->isIntegerTy(64))
3151 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
3152 // Fast instruction selection doesn't support the special
3156 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
3157 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
3159 case Intrinsic::memset: {
3160 const MemSetInst *MSI = cast<MemSetInst>(II);
3161 // Don't handle volatile.
3162 if (MSI->isVolatile())
3165 if (!MSI->getLength()->getType()->isIntegerTy(64))
3168 if (MSI->getDestAddressSpace() > 255)
3169 // Fast instruction selection doesn't support the special
3173 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
3175 case Intrinsic::sin:
3176 case Intrinsic::cos:
3177 case Intrinsic::pow: {
3179 if (!isTypeLegal(II->getType(), RetVT))
3182 if (RetVT != MVT::f32 && RetVT != MVT::f64)
3185 static const RTLIB::Libcall LibCallTable[3][2] = {
3186 { RTLIB::SIN_F32, RTLIB::SIN_F64 },
3187 { RTLIB::COS_F32, RTLIB::COS_F64 },
3188 { RTLIB::POW_F32, RTLIB::POW_F64 }
3191 bool Is64Bit = RetVT == MVT::f64;
3192 switch (II->getIntrinsicID()) {
3194 llvm_unreachable("Unexpected intrinsic.");
3195 case Intrinsic::sin:
3196 LC = LibCallTable[0][Is64Bit];
3198 case Intrinsic::cos:
3199 LC = LibCallTable[1][Is64Bit];
3201 case Intrinsic::pow:
3202 LC = LibCallTable[2][Is64Bit];
3207 Args.reserve(II->getNumArgOperands());
3209 // Populate the argument list.
3210 for (auto &Arg : II->arg_operands()) {
3213 Entry.Ty = Arg->getType();
3214 Args.push_back(Entry);
3217 CallLoweringInfo CLI;
3218 CLI.setCallee(TLI.getLibcallCallingConv(LC), II->getType(),
3219 TLI.getLibcallName(LC), std::move(Args));
3220 if (!lowerCallTo(CLI))
3222 updateValueMap(II, CLI.ResultReg);
3225 case Intrinsic::trap: {
3226 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
3230 case Intrinsic::sqrt: {
3231 Type *RetTy = II->getCalledFunction()->getReturnType();
3234 if (!isTypeLegal(RetTy, VT))
3237 unsigned Op0Reg = getRegForValue(II->getOperand(0));
3240 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
3242 unsigned ResultReg = fastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
3246 updateValueMap(II, ResultReg);
3249 case Intrinsic::sadd_with_overflow:
3250 case Intrinsic::uadd_with_overflow:
3251 case Intrinsic::ssub_with_overflow:
3252 case Intrinsic::usub_with_overflow:
3253 case Intrinsic::smul_with_overflow:
3254 case Intrinsic::umul_with_overflow: {
3255 // This implements the basic lowering of the xalu with overflow intrinsics.
3256 const Function *Callee = II->getCalledFunction();
3257 auto *Ty = cast<StructType>(Callee->getReturnType());
3258 Type *RetTy = Ty->getTypeAtIndex(0U);
3261 if (!isTypeLegal(RetTy, VT))
3264 if (VT != MVT::i32 && VT != MVT::i64)
3267 const Value *LHS = II->getArgOperand(0);
3268 const Value *RHS = II->getArgOperand(1);
3269 // Canonicalize immediate to the RHS.
3270 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3271 isCommutativeIntrinsic(II))
3272 std::swap(LHS, RHS);
3274 // Simplify multiplies.
3275 unsigned IID = II->getIntrinsicID();
3279 case Intrinsic::smul_with_overflow:
3280 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3281 if (C->getValue() == 2) {
3282 IID = Intrinsic::sadd_with_overflow;
3286 case Intrinsic::umul_with_overflow:
3287 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3288 if (C->getValue() == 2) {
3289 IID = Intrinsic::uadd_with_overflow;
3295 unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
3296 AArch64CC::CondCode CC = AArch64CC::Invalid;
3298 default: llvm_unreachable("Unexpected intrinsic!");
3299 case Intrinsic::sadd_with_overflow:
3300 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3303 case Intrinsic::uadd_with_overflow:
3304 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3307 case Intrinsic::ssub_with_overflow:
3308 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3311 case Intrinsic::usub_with_overflow:
3312 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3315 case Intrinsic::smul_with_overflow: {
3317 unsigned LHSReg = getRegForValue(LHS);
3320 bool LHSIsKill = hasTrivialKill(LHS);
3322 unsigned RHSReg = getRegForValue(RHS);
3325 bool RHSIsKill = hasTrivialKill(RHS);
3327 if (VT == MVT::i32) {
3328 MulReg = emitSMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3329 unsigned ShiftReg = emitLSR_ri(MVT::i64, MVT::i64, MulReg,
3330 /*IsKill=*/false, 32);
3331 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3333 ShiftReg = fastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
3335 emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3336 AArch64_AM::ASR, 31, /*WantResult=*/false);
3338 assert(VT == MVT::i64 && "Unexpected value type.");
3339 MulReg = emitMul_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3340 unsigned SMULHReg = fastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
3342 emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3343 AArch64_AM::ASR, 63, /*WantResult=*/false);
3347 case Intrinsic::umul_with_overflow: {
3349 unsigned LHSReg = getRegForValue(LHS);
3352 bool LHSIsKill = hasTrivialKill(LHS);
3354 unsigned RHSReg = getRegForValue(RHS);
3357 bool RHSIsKill = hasTrivialKill(RHS);
3359 if (VT == MVT::i32) {
3360 MulReg = emitUMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3361 emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
3362 /*IsKill=*/false, AArch64_AM::LSR, 32,
3363 /*WantResult=*/false);
3364 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3367 assert(VT == MVT::i64 && "Unexpected value type.");
3368 MulReg = emitMul_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3369 unsigned UMULHReg = fastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
3371 emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
3372 /*IsKill=*/false, /*WantResult=*/false);
3379 ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
3380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3381 TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
3384 ResultReg2 = fastEmitInst_rri(AArch64::CSINCWr, &AArch64::GPR32RegClass,
3385 AArch64::WZR, /*IsKill=*/true, AArch64::WZR,
3386 /*IsKill=*/true, getInvertedCondCode(CC));
3388 assert((ResultReg1 + 1) == ResultReg2 &&
3389 "Nonconsecutive result registers.");
3390 updateValueMap(II, ResultReg1, 2);
3397 bool AArch64FastISel::selectRet(const Instruction *I) {
3398 const ReturnInst *Ret = cast<ReturnInst>(I);
3399 const Function &F = *I->getParent()->getParent();
3401 if (!FuncInfo.CanLowerReturn)
3407 // Build a list of return value registers.
3408 SmallVector<unsigned, 4> RetRegs;
3410 if (Ret->getNumOperands() > 0) {
3411 CallingConv::ID CC = F.getCallingConv();
3412 SmallVector<ISD::OutputArg, 4> Outs;
3413 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
3415 // Analyze operands of the call, assigning locations to each operand.
3416 SmallVector<CCValAssign, 16> ValLocs;
3417 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
3418 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
3419 : RetCC_AArch64_AAPCS;
3420 CCInfo.AnalyzeReturn(Outs, RetCC);
3422 // Only handle a single return value for now.
3423 if (ValLocs.size() != 1)
3426 CCValAssign &VA = ValLocs[0];
3427 const Value *RV = Ret->getOperand(0);
3429 // Don't bother handling odd stuff for now.
3430 if ((VA.getLocInfo() != CCValAssign::Full) &&
3431 (VA.getLocInfo() != CCValAssign::BCvt))
3434 // Only handle register returns for now.
3438 unsigned Reg = getRegForValue(RV);
3442 unsigned SrcReg = Reg + VA.getValNo();
3443 unsigned DestReg = VA.getLocReg();
3444 // Avoid a cross-class copy. This is very unlikely.
3445 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
3448 EVT RVEVT = TLI.getValueType(RV->getType());
3449 if (!RVEVT.isSimple())
3452 // Vectors (of > 1 lane) in big endian need tricky handling.
3453 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1 &&
3454 !Subtarget->isLittleEndian())
3457 MVT RVVT = RVEVT.getSimpleVT();
3458 if (RVVT == MVT::f128)
3461 MVT DestVT = VA.getValVT();
3462 // Special handling for extended integers.
3463 if (RVVT != DestVT) {
3464 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
3467 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
3470 bool IsZExt = Outs[0].Flags.isZExt();
3471 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
3477 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3478 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
3480 // Add register to return instruction.
3481 RetRegs.push_back(VA.getLocReg());
3484 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3485 TII.get(AArch64::RET_ReallyLR));
3486 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
3487 MIB.addReg(RetRegs[i], RegState::Implicit);
3491 bool AArch64FastISel::selectTrunc(const Instruction *I) {
3492 Type *DestTy = I->getType();
3493 Value *Op = I->getOperand(0);
3494 Type *SrcTy = Op->getType();
3496 EVT SrcEVT = TLI.getValueType(SrcTy, true);
3497 EVT DestEVT = TLI.getValueType(DestTy, true);
3498 if (!SrcEVT.isSimple())
3500 if (!DestEVT.isSimple())
3503 MVT SrcVT = SrcEVT.getSimpleVT();
3504 MVT DestVT = DestEVT.getSimpleVT();
3506 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
3509 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
3513 unsigned SrcReg = getRegForValue(Op);
3516 bool SrcIsKill = hasTrivialKill(Op);
3518 // If we're truncating from i64 to a smaller non-legal type then generate an
3519 // AND. Otherwise, we know the high bits are undefined and a truncate only
3520 // generate a COPY. We cannot mark the source register also as result
3521 // register, because this can incorrectly transfer the kill flag onto the
3524 if (SrcVT == MVT::i64) {
3526 switch (DestVT.SimpleTy) {
3528 // Trunc i64 to i32 is handled by the target-independent fast-isel.
3540 // Issue an extract_subreg to get the lower 32-bits.
3541 unsigned Reg32 = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
3543 // Create the AND instruction which performs the actual truncation.
3544 ResultReg = emitAnd_ri(MVT::i32, Reg32, /*IsKill=*/true, Mask);
3545 assert(ResultReg && "Unexpected AND instruction emission failure.");
3547 ResultReg = createResultReg(&AArch64::GPR32RegClass);
3548 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3549 TII.get(TargetOpcode::COPY), ResultReg)
3550 .addReg(SrcReg, getKillRegState(SrcIsKill));
3553 updateValueMap(I, ResultReg);
3557 unsigned AArch64FastISel::emiti1Ext(unsigned SrcReg, MVT DestVT, bool IsZExt) {
3558 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
3559 DestVT == MVT::i64) &&
3560 "Unexpected value type.");
3561 // Handle i8 and i16 as i32.
3562 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3566 unsigned ResultReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
3567 assert(ResultReg && "Unexpected AND instruction emission failure.");
3568 if (DestVT == MVT::i64) {
3569 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
3570 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
3571 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3572 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3573 TII.get(AArch64::SUBREG_TO_REG), Reg64)
3576 .addImm(AArch64::sub_32);
3581 if (DestVT == MVT::i64) {
3582 // FIXME: We're SExt i1 to i64.
3585 return fastEmitInst_rii(AArch64::SBFMWri, &AArch64::GPR32RegClass, SrcReg,
3586 /*TODO:IsKill=*/false, 0, 0);
3590 unsigned AArch64FastISel::emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3591 unsigned Op1, bool Op1IsKill) {
3593 switch (RetVT.SimpleTy) {
3599 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
3601 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
3604 const TargetRegisterClass *RC =
3605 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3606 return fastEmitInst_rrr(Opc, RC, Op0, Op0IsKill, Op1, Op1IsKill,
3607 /*IsKill=*/ZReg, true);
3610 unsigned AArch64FastISel::emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3611 unsigned Op1, bool Op1IsKill) {
3612 if (RetVT != MVT::i64)
3615 return fastEmitInst_rrr(AArch64::SMADDLrrr, &AArch64::GPR64RegClass,
3616 Op0, Op0IsKill, Op1, Op1IsKill,
3617 AArch64::XZR, /*IsKill=*/true);
3620 unsigned AArch64FastISel::emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3621 unsigned Op1, bool Op1IsKill) {
3622 if (RetVT != MVT::i64)
3625 return fastEmitInst_rrr(AArch64::UMADDLrrr, &AArch64::GPR64RegClass,
3626 Op0, Op0IsKill, Op1, Op1IsKill,
3627 AArch64::XZR, /*IsKill=*/true);
3630 unsigned AArch64FastISel::emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3631 unsigned Op1Reg, bool Op1IsKill) {
3633 bool NeedTrunc = false;
3635 switch (RetVT.SimpleTy) {
3637 case MVT::i8: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xff; break;
3638 case MVT::i16: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xffff; break;
3639 case MVT::i32: Opc = AArch64::LSLVWr; break;
3640 case MVT::i64: Opc = AArch64::LSLVXr; break;
3643 const TargetRegisterClass *RC =
3644 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3646 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3649 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3652 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3656 unsigned AArch64FastISel::emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3657 bool Op0IsKill, uint64_t Shift,
3659 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3660 "Unexpected source/return type pair.");
3661 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
3662 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
3663 "Unexpected source value type.");
3664 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3665 RetVT == MVT::i64) && "Unexpected return value type.");
3667 bool Is64Bit = (RetVT == MVT::i64);
3668 unsigned RegSize = Is64Bit ? 64 : 32;
3669 unsigned DstBits = RetVT.getSizeInBits();
3670 unsigned SrcBits = SrcVT.getSizeInBits();
3672 // Don't deal with undefined shifts.
3673 if (Shift >= DstBits)
3676 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3677 // {S|U}BFM Wd, Wn, #r, #s
3678 // Wd<32+s-r,32-r> = Wn<s:0> when r > s
3680 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3681 // %2 = shl i16 %1, 4
3682 // Wd<32+7-28,32-28> = Wn<7:0> <- clamp s to 7
3683 // 0b1111_1111_1111_1111__1111_1010_1010_0000 sext
3684 // 0b0000_0000_0000_0000__0000_0101_0101_0000 sext | zext
3685 // 0b0000_0000_0000_0000__0000_1010_1010_0000 zext
3687 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3688 // %2 = shl i16 %1, 8
3689 // Wd<32+7-24,32-24> = Wn<7:0>
3690 // 0b1111_1111_1111_1111__1010_1010_0000_0000 sext
3691 // 0b0000_0000_0000_0000__0101_0101_0000_0000 sext | zext
3692 // 0b0000_0000_0000_0000__1010_1010_0000_0000 zext
3694 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3695 // %2 = shl i16 %1, 12
3696 // Wd<32+3-20,32-20> = Wn<3:0>
3697 // 0b1111_1111_1111_1111__1010_0000_0000_0000 sext
3698 // 0b0000_0000_0000_0000__0101_0000_0000_0000 sext | zext
3699 // 0b0000_0000_0000_0000__1010_0000_0000_0000 zext
3701 unsigned ImmR = RegSize - Shift;
3702 // Limit the width to the length of the source type.
3703 unsigned ImmS = std::min<unsigned>(SrcBits - 1, DstBits - 1 - Shift);
3704 static const unsigned OpcTable[2][2] = {
3705 {AArch64::SBFMWri, AArch64::SBFMXri},
3706 {AArch64::UBFMWri, AArch64::UBFMXri}
3708 unsigned Opc = OpcTable[IsZext][Is64Bit];
3709 const TargetRegisterClass *RC =
3710 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3711 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3712 unsigned TmpReg = MRI.createVirtualRegister(RC);
3713 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3714 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3716 .addReg(Op0, getKillRegState(Op0IsKill))
3717 .addImm(AArch64::sub_32);
3721 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3724 unsigned AArch64FastISel::emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3725 unsigned Op1Reg, bool Op1IsKill) {
3727 bool NeedTrunc = false;
3729 switch (RetVT.SimpleTy) {
3731 case MVT::i8: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xff; break;
3732 case MVT::i16: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xffff; break;
3733 case MVT::i32: Opc = AArch64::LSRVWr; break;
3734 case MVT::i64: Opc = AArch64::LSRVXr; break;
3737 const TargetRegisterClass *RC =
3738 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3740 Op0Reg = emitAnd_ri(MVT::i32, Op0Reg, Op0IsKill, Mask);
3741 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3742 Op0IsKill = Op1IsKill = true;
3744 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3747 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3751 unsigned AArch64FastISel::emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3752 bool Op0IsKill, uint64_t Shift,
3754 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3755 "Unexpected source/return type pair.");
3756 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3757 SrcVT == MVT::i64) && "Unexpected source value type.");
3758 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3759 RetVT == MVT::i64) && "Unexpected return value type.");
3761 bool Is64Bit = (RetVT == MVT::i64);
3762 unsigned RegSize = Is64Bit ? 64 : 32;
3763 unsigned DstBits = RetVT.getSizeInBits();
3764 unsigned SrcBits = SrcVT.getSizeInBits();
3766 // Don't deal with undefined shifts.
3767 if (Shift >= DstBits)
3770 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3771 // {S|U}BFM Wd, Wn, #r, #s
3772 // Wd<s-r:0> = Wn<s:r> when r <= s
3774 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3775 // %2 = lshr i16 %1, 4
3776 // Wd<7-4:0> = Wn<7:4>
3777 // 0b0000_0000_0000_0000__0000_1111_1111_1010 sext
3778 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
3779 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
3781 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3782 // %2 = lshr i16 %1, 8
3783 // Wd<7-7,0> = Wn<7:7>
3784 // 0b0000_0000_0000_0000__0000_0000_1111_1111 sext
3785 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3786 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3788 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3789 // %2 = lshr i16 %1, 12
3790 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
3791 // 0b0000_0000_0000_0000__0000_0000_0000_1111 sext
3792 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3793 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3795 if (Shift >= SrcBits && IsZExt)
3796 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
3798 // It is not possible to fold a sign-extend into the LShr instruction. In this
3799 // case emit a sign-extend.
3801 Op0 = emitIntExt(SrcVT, Op0, RetVT, IsZExt);
3806 SrcBits = SrcVT.getSizeInBits();
3810 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
3811 unsigned ImmS = SrcBits - 1;
3812 static const unsigned OpcTable[2][2] = {
3813 {AArch64::SBFMWri, AArch64::SBFMXri},
3814 {AArch64::UBFMWri, AArch64::UBFMXri}
3816 unsigned Opc = OpcTable[IsZExt][Is64Bit];
3817 const TargetRegisterClass *RC =
3818 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3819 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3820 unsigned TmpReg = MRI.createVirtualRegister(RC);
3821 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3822 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3824 .addReg(Op0, getKillRegState(Op0IsKill))
3825 .addImm(AArch64::sub_32);
3829 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3832 unsigned AArch64FastISel::emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3833 unsigned Op1Reg, bool Op1IsKill) {
3835 bool NeedTrunc = false;
3837 switch (RetVT.SimpleTy) {
3839 case MVT::i8: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xff; break;
3840 case MVT::i16: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xffff; break;
3841 case MVT::i32: Opc = AArch64::ASRVWr; break;
3842 case MVT::i64: Opc = AArch64::ASRVXr; break;
3845 const TargetRegisterClass *RC =
3846 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3848 Op0Reg = emitIntExt(RetVT, Op0Reg, MVT::i32, /*IsZExt=*/false);
3849 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3850 Op0IsKill = Op1IsKill = true;
3852 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3855 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3859 unsigned AArch64FastISel::emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3860 bool Op0IsKill, uint64_t Shift,
3862 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3863 "Unexpected source/return type pair.");
3864 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3865 SrcVT == MVT::i64) && "Unexpected source value type.");
3866 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3867 RetVT == MVT::i64) && "Unexpected return value type.");
3869 bool Is64Bit = (RetVT == MVT::i64);
3870 unsigned RegSize = Is64Bit ? 64 : 32;
3871 unsigned DstBits = RetVT.getSizeInBits();
3872 unsigned SrcBits = SrcVT.getSizeInBits();
3874 // Don't deal with undefined shifts.
3875 if (Shift >= DstBits)
3878 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3879 // {S|U}BFM Wd, Wn, #r, #s
3880 // Wd<s-r:0> = Wn<s:r> when r <= s
3882 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3883 // %2 = ashr i16 %1, 4
3884 // Wd<7-4:0> = Wn<7:4>
3885 // 0b1111_1111_1111_1111__1111_1111_1111_1010 sext
3886 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
3887 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
3889 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3890 // %2 = ashr i16 %1, 8
3891 // Wd<7-7,0> = Wn<7:7>
3892 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
3893 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3894 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3896 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3897 // %2 = ashr i16 %1, 12
3898 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
3899 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
3900 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3901 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3903 if (Shift >= SrcBits && IsZExt)
3904 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
3906 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
3907 unsigned ImmS = SrcBits - 1;
3908 static const unsigned OpcTable[2][2] = {
3909 {AArch64::SBFMWri, AArch64::SBFMXri},
3910 {AArch64::UBFMWri, AArch64::UBFMXri}
3912 unsigned Opc = OpcTable[IsZExt][Is64Bit];
3913 const TargetRegisterClass *RC =
3914 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3915 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3916 unsigned TmpReg = MRI.createVirtualRegister(RC);
3917 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3918 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3920 .addReg(Op0, getKillRegState(Op0IsKill))
3921 .addImm(AArch64::sub_32);
3925 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3928 unsigned AArch64FastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
3930 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
3932 // FastISel does not have plumbing to deal with extensions where the SrcVT or
3933 // DestVT are odd things, so test to make sure that they are both types we can
3934 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
3935 // bail out to SelectionDAG.
3936 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
3937 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
3938 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
3939 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
3945 switch (SrcVT.SimpleTy) {
3949 return emiti1Ext(SrcReg, DestVT, IsZExt);
3951 if (DestVT == MVT::i64)
3952 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3954 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
3958 if (DestVT == MVT::i64)
3959 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3961 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
3965 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
3966 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3971 // Handle i8 and i16 as i32.
3972 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3974 else if (DestVT == MVT::i64) {
3975 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3976 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3977 TII.get(AArch64::SUBREG_TO_REG), Src64)
3980 .addImm(AArch64::sub_32);
3984 const TargetRegisterClass *RC =
3985 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3986 return fastEmitInst_rii(Opc, RC, SrcReg, /*TODO:IsKill=*/false, 0, Imm);
3989 bool AArch64FastISel::selectIntExt(const Instruction *I) {
3990 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
3991 "Unexpected integer extend instruction.");
3994 if (!isTypeSupported(I->getType(), RetVT))
3997 if (!isTypeSupported(I->getOperand(0)->getType(), SrcVT))
4000 unsigned SrcReg = getRegForValue(I->getOperand(0));
4003 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
4005 // The load instruction selection code handles the sign-/zero-extension.
4006 if (const auto *LI = dyn_cast<LoadInst>(I->getOperand(0))) {
4007 if (LI->hasOneUse()) {
4008 updateValueMap(I, SrcReg);
4013 bool IsZExt = isa<ZExtInst>(I);
4014 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0))) {
4015 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr())) {
4016 if (RetVT == MVT::i64 && SrcVT != MVT::i64) {
4017 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
4018 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4019 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
4021 .addReg(SrcReg, getKillRegState(SrcIsKill))
4022 .addImm(AArch64::sub_32);
4025 updateValueMap(I, SrcReg);
4030 unsigned ResultReg = emitIntExt(SrcVT, SrcReg, RetVT, IsZExt);
4034 updateValueMap(I, ResultReg);
4038 bool AArch64FastISel::selectRem(const Instruction *I, unsigned ISDOpcode) {
4039 EVT DestEVT = TLI.getValueType(I->getType(), true);
4040 if (!DestEVT.isSimple())
4043 MVT DestVT = DestEVT.getSimpleVT();
4044 if (DestVT != MVT::i64 && DestVT != MVT::i32)
4048 bool Is64bit = (DestVT == MVT::i64);
4049 switch (ISDOpcode) {
4053 DivOpc = Is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
4056 DivOpc = Is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
4059 unsigned MSubOpc = Is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
4060 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4063 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4065 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4068 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4070 const TargetRegisterClass *RC =
4071 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4072 unsigned QuotReg = fastEmitInst_rr(DivOpc, RC, Src0Reg, /*IsKill=*/false,
4073 Src1Reg, /*IsKill=*/false);
4074 assert(QuotReg && "Unexpected DIV instruction emission failure.");
4075 // The remainder is computed as numerator - (quotient * denominator) using the
4076 // MSUB instruction.
4077 unsigned ResultReg = fastEmitInst_rrr(MSubOpc, RC, QuotReg, /*IsKill=*/true,
4078 Src1Reg, Src1IsKill, Src0Reg,
4080 updateValueMap(I, ResultReg);
4084 bool AArch64FastISel::selectMul(const Instruction *I) {
4086 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
4090 return selectBinaryOp(I, ISD::MUL);
4092 const Value *Src0 = I->getOperand(0);
4093 const Value *Src1 = I->getOperand(1);
4094 if (const auto *C = dyn_cast<ConstantInt>(Src0))
4095 if (C->getValue().isPowerOf2())
4096 std::swap(Src0, Src1);
4098 // Try to simplify to a shift instruction.
4099 if (const auto *C = dyn_cast<ConstantInt>(Src1))
4100 if (C->getValue().isPowerOf2()) {
4101 uint64_t ShiftVal = C->getValue().logBase2();
4104 if (const auto *ZExt = dyn_cast<ZExtInst>(Src0)) {
4105 if (!isIntExtFree(ZExt)) {
4107 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), VT)) {
4110 Src0 = ZExt->getOperand(0);
4113 } else if (const auto *SExt = dyn_cast<SExtInst>(Src0)) {
4114 if (!isIntExtFree(SExt)) {
4116 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), VT)) {
4119 Src0 = SExt->getOperand(0);
4124 unsigned Src0Reg = getRegForValue(Src0);
4127 bool Src0IsKill = hasTrivialKill(Src0);
4129 unsigned ResultReg =
4130 emitLSL_ri(VT, SrcVT, Src0Reg, Src0IsKill, ShiftVal, IsZExt);
4133 updateValueMap(I, ResultReg);
4138 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4141 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4143 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4146 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4148 unsigned ResultReg = emitMul_rr(VT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
4153 updateValueMap(I, ResultReg);
4157 bool AArch64FastISel::selectShift(const Instruction *I) {
4159 if (!isTypeSupported(I->getType(), RetVT, /*IsVectorAllowed=*/true))
4162 if (RetVT.isVector())
4163 return selectOperator(I, I->getOpcode());
4165 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
4166 unsigned ResultReg = 0;
4167 uint64_t ShiftVal = C->getZExtValue();
4169 bool IsZExt = (I->getOpcode() == Instruction::AShr) ? false : true;
4170 const Value *Op0 = I->getOperand(0);
4171 if (const auto *ZExt = dyn_cast<ZExtInst>(Op0)) {
4172 if (!isIntExtFree(ZExt)) {
4174 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), TmpVT)) {
4177 Op0 = ZExt->getOperand(0);
4180 } else if (const auto *SExt = dyn_cast<SExtInst>(Op0)) {
4181 if (!isIntExtFree(SExt)) {
4183 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), TmpVT)) {
4186 Op0 = SExt->getOperand(0);
4191 unsigned Op0Reg = getRegForValue(Op0);
4194 bool Op0IsKill = hasTrivialKill(Op0);
4196 switch (I->getOpcode()) {
4197 default: llvm_unreachable("Unexpected instruction.");
4198 case Instruction::Shl:
4199 ResultReg = emitLSL_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4201 case Instruction::AShr:
4202 ResultReg = emitASR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4204 case Instruction::LShr:
4205 ResultReg = emitLSR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4211 updateValueMap(I, ResultReg);
4215 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4218 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4220 unsigned Op1Reg = getRegForValue(I->getOperand(1));
4223 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
4225 unsigned ResultReg = 0;
4226 switch (I->getOpcode()) {
4227 default: llvm_unreachable("Unexpected instruction.");
4228 case Instruction::Shl:
4229 ResultReg = emitLSL_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4231 case Instruction::AShr:
4232 ResultReg = emitASR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4234 case Instruction::LShr:
4235 ResultReg = emitLSR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4242 updateValueMap(I, ResultReg);
4246 bool AArch64FastISel::selectBitCast(const Instruction *I) {
4249 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
4251 if (!isTypeLegal(I->getType(), RetVT))
4255 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
4256 Opc = AArch64::FMOVWSr;
4257 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
4258 Opc = AArch64::FMOVXDr;
4259 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
4260 Opc = AArch64::FMOVSWr;
4261 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
4262 Opc = AArch64::FMOVDXr;
4266 const TargetRegisterClass *RC = nullptr;
4267 switch (RetVT.SimpleTy) {
4268 default: llvm_unreachable("Unexpected value type.");
4269 case MVT::i32: RC = &AArch64::GPR32RegClass; break;
4270 case MVT::i64: RC = &AArch64::GPR64RegClass; break;
4271 case MVT::f32: RC = &AArch64::FPR32RegClass; break;
4272 case MVT::f64: RC = &AArch64::FPR64RegClass; break;
4274 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4277 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4278 unsigned ResultReg = fastEmitInst_r(Opc, RC, Op0Reg, Op0IsKill);
4283 updateValueMap(I, ResultReg);
4287 bool AArch64FastISel::selectFRem(const Instruction *I) {
4289 if (!isTypeLegal(I->getType(), RetVT))
4293 switch (RetVT.SimpleTy) {
4297 LC = RTLIB::REM_F32;
4300 LC = RTLIB::REM_F64;
4305 Args.reserve(I->getNumOperands());
4307 // Populate the argument list.
4308 for (auto &Arg : I->operands()) {
4311 Entry.Ty = Arg->getType();
4312 Args.push_back(Entry);
4315 CallLoweringInfo CLI;
4316 CLI.setCallee(TLI.getLibcallCallingConv(LC), I->getType(),
4317 TLI.getLibcallName(LC), std::move(Args));
4318 if (!lowerCallTo(CLI))
4320 updateValueMap(I, CLI.ResultReg);
4324 bool AArch64FastISel::selectSDiv(const Instruction *I) {
4326 if (!isTypeLegal(I->getType(), VT))
4329 if (!isa<ConstantInt>(I->getOperand(1)))
4330 return selectBinaryOp(I, ISD::SDIV);
4332 const APInt &C = cast<ConstantInt>(I->getOperand(1))->getValue();
4333 if ((VT != MVT::i32 && VT != MVT::i64) || !C ||
4334 !(C.isPowerOf2() || (-C).isPowerOf2()))
4335 return selectBinaryOp(I, ISD::SDIV);
4337 unsigned Lg2 = C.countTrailingZeros();
4338 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4341 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4343 if (cast<BinaryOperator>(I)->isExact()) {
4344 unsigned ResultReg = emitASR_ri(VT, VT, Src0Reg, Src0IsKill, Lg2);
4347 updateValueMap(I, ResultReg);
4351 unsigned Pow2MinusOne = (1 << Lg2) - 1;
4352 unsigned AddReg = emitAddSub_ri(/*UseAdd=*/true, VT, Src0Reg,
4353 /*IsKill=*/false, Pow2MinusOne);
4357 // (Src0 < 0) ? Pow2 - 1 : 0;
4358 if (!emitICmp_ri(VT, Src0Reg, /*IsKill=*/false, 0))
4362 const TargetRegisterClass *RC;
4363 if (VT == MVT::i64) {
4364 SelectOpc = AArch64::CSELXr;
4365 RC = &AArch64::GPR64RegClass;
4367 SelectOpc = AArch64::CSELWr;
4368 RC = &AArch64::GPR32RegClass;
4370 unsigned SelectReg =
4371 fastEmitInst_rri(SelectOpc, RC, AddReg, /*IsKill=*/true, Src0Reg,
4372 Src0IsKill, AArch64CC::LT);
4376 // Divide by Pow2 --> ashr. If we're dividing by a negative value we must also
4377 // negate the result.
4378 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
4381 ResultReg = emitAddSub_rs(/*UseAdd=*/false, VT, ZeroReg, /*IsKill=*/true,
4382 SelectReg, /*IsKill=*/true, AArch64_AM::ASR, Lg2);
4384 ResultReg = emitASR_ri(VT, VT, SelectReg, /*IsKill=*/true, Lg2);
4389 updateValueMap(I, ResultReg);
4393 bool AArch64FastISel::fastSelectInstruction(const Instruction *I) {
4394 switch (I->getOpcode()) {
4397 case Instruction::Add:
4398 case Instruction::Sub:
4399 return selectAddSub(I);
4400 case Instruction::Mul:
4401 return selectMul(I);
4402 case Instruction::SDiv:
4403 return selectSDiv(I);
4404 case Instruction::SRem:
4405 if (!selectBinaryOp(I, ISD::SREM))
4406 return selectRem(I, ISD::SREM);
4408 case Instruction::URem:
4409 if (!selectBinaryOp(I, ISD::UREM))
4410 return selectRem(I, ISD::UREM);
4412 case Instruction::Shl:
4413 case Instruction::LShr:
4414 case Instruction::AShr:
4415 return selectShift(I);
4416 case Instruction::And:
4417 case Instruction::Or:
4418 case Instruction::Xor:
4419 return selectLogicalOp(I);
4420 case Instruction::Br:
4421 return selectBranch(I);
4422 case Instruction::IndirectBr:
4423 return selectIndirectBr(I);
4424 case Instruction::BitCast:
4425 if (!FastISel::selectBitCast(I))
4426 return selectBitCast(I);
4428 case Instruction::FPToSI:
4429 if (!selectCast(I, ISD::FP_TO_SINT))
4430 return selectFPToInt(I, /*Signed=*/true);
4432 case Instruction::FPToUI:
4433 return selectFPToInt(I, /*Signed=*/false);
4434 case Instruction::ZExt:
4435 case Instruction::SExt:
4436 return selectIntExt(I);
4437 case Instruction::Trunc:
4438 if (!selectCast(I, ISD::TRUNCATE))
4439 return selectTrunc(I);
4441 case Instruction::FPExt:
4442 return selectFPExt(I);
4443 case Instruction::FPTrunc:
4444 return selectFPTrunc(I);
4445 case Instruction::SIToFP:
4446 if (!selectCast(I, ISD::SINT_TO_FP))
4447 return selectIntToFP(I, /*Signed=*/true);
4449 case Instruction::UIToFP:
4450 return selectIntToFP(I, /*Signed=*/false);
4451 case Instruction::Load:
4452 return selectLoad(I);
4453 case Instruction::Store:
4454 return selectStore(I);
4455 case Instruction::FCmp:
4456 case Instruction::ICmp:
4457 return selectCmp(I);
4458 case Instruction::Select:
4459 return selectSelect(I);
4460 case Instruction::Ret:
4461 return selectRet(I);
4462 case Instruction::FRem:
4463 return selectFRem(I);
4466 // fall-back to target-independent instruction selection.
4467 return selectOperator(I, I->getOpcode());
4468 // Silence warnings.
4469 (void)&CC_AArch64_DarwinPCS_VarArg;
4473 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &FuncInfo,
4474 const TargetLibraryInfo *LibInfo) {
4475 return new AArch64FastISel(FuncInfo, LibInfo);