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,
153 bool optimizeIntExtLoad(const Instruction *I, MVT RetVT, MVT SrcVT);
155 // Emit helper routines.
156 unsigned emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
157 const Value *RHS, bool SetFlags = false,
158 bool WantResult = true, bool IsZExt = false);
159 unsigned emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
160 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
161 bool SetFlags = false, bool WantResult = true);
162 unsigned emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
163 bool LHSIsKill, uint64_t Imm, bool SetFlags = false,
164 bool WantResult = true);
165 unsigned emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
166 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
167 AArch64_AM::ShiftExtendType ShiftType,
168 uint64_t ShiftImm, bool SetFlags = false,
169 bool WantResult = true);
170 unsigned emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
171 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
172 AArch64_AM::ShiftExtendType ExtType,
173 uint64_t ShiftImm, bool SetFlags = false,
174 bool WantResult = true);
177 bool emitCompareAndBranch(const BranchInst *BI);
178 bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
179 bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
180 bool emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
181 bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
182 unsigned emitLoad(MVT VT, MVT ResultVT, Address Addr, bool WantZExt = true,
183 MachineMemOperand *MMO = nullptr);
184 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
185 MachineMemOperand *MMO = nullptr);
186 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
187 unsigned emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
188 unsigned emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
189 bool SetFlags = false, bool WantResult = true,
190 bool IsZExt = false);
191 unsigned emitAdd_ri_(MVT VT, unsigned Op0, bool Op0IsKill, int64_t Imm);
192 unsigned emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
193 bool SetFlags = false, bool WantResult = true,
194 bool IsZExt = false);
195 unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
196 unsigned RHSReg, bool RHSIsKill, bool WantResult = true);
197 unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
198 unsigned RHSReg, bool RHSIsKill,
199 AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
200 bool WantResult = true);
201 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
203 unsigned emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
204 bool LHSIsKill, uint64_t Imm);
205 unsigned emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
206 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
208 unsigned emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
209 unsigned emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
210 unsigned Op1, bool Op1IsKill);
211 unsigned emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
212 unsigned Op1, bool Op1IsKill);
213 unsigned emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
214 unsigned Op1, bool Op1IsKill);
215 unsigned emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
216 unsigned Op1Reg, bool Op1IsKill);
217 unsigned emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
218 uint64_t Imm, bool IsZExt = true);
219 unsigned emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
220 unsigned Op1Reg, bool Op1IsKill);
221 unsigned emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
222 uint64_t Imm, bool IsZExt = true);
223 unsigned emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
224 unsigned Op1Reg, bool Op1IsKill);
225 unsigned emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
226 uint64_t Imm, bool IsZExt = false);
228 unsigned materializeInt(const ConstantInt *CI, MVT VT);
229 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
230 unsigned materializeGV(const GlobalValue *GV);
232 // Call handling routines.
234 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
235 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
237 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
240 // Backend specific FastISel code.
241 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
242 unsigned fastMaterializeConstant(const Constant *C) override;
243 unsigned fastMaterializeFloatZero(const ConstantFP* CF) override;
245 explicit AArch64FastISel(FunctionLoweringInfo &FuncInfo,
246 const TargetLibraryInfo *LibInfo)
247 : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
248 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
249 Context = &FuncInfo.Fn->getContext();
252 bool fastSelectInstruction(const Instruction *I) override;
254 #include "AArch64GenFastISel.inc"
257 } // end anonymous namespace
259 #include "AArch64GenCallingConv.inc"
261 /// \brief Check if the sign-/zero-extend will be a noop.
262 static bool isIntExtFree(const Instruction *I) {
263 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
264 "Unexpected integer extend instruction.");
265 assert(!I->getType()->isVectorTy() && I->getType()->isIntegerTy() &&
266 "Unexpected value type.");
267 bool IsZExt = isa<ZExtInst>(I);
269 if (const auto *LI = dyn_cast<LoadInst>(I->getOperand(0)))
273 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0)))
274 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr()))
280 /// \brief Determine the implicit scale factor that is applied by a memory
281 /// operation for a given value type.
282 static unsigned getImplicitScaleFactor(MVT VT) {
283 switch (VT.SimpleTy) {
286 case MVT::i1: // fall-through
291 case MVT::i32: // fall-through
294 case MVT::i64: // fall-through
300 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
301 if (CC == CallingConv::WebKit_JS)
302 return CC_AArch64_WebKit_JS;
303 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
306 unsigned AArch64FastISel::fastMaterializeAlloca(const AllocaInst *AI) {
307 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
308 "Alloca should always return a pointer.");
310 // Don't handle dynamic allocas.
311 if (!FuncInfo.StaticAllocaMap.count(AI))
314 DenseMap<const AllocaInst *, int>::iterator SI =
315 FuncInfo.StaticAllocaMap.find(AI);
317 if (SI != FuncInfo.StaticAllocaMap.end()) {
318 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
319 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
321 .addFrameIndex(SI->second)
330 unsigned AArch64FastISel::materializeInt(const ConstantInt *CI, MVT VT) {
335 return fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
337 // Create a copy from the zero register to materialize a "0" value.
338 const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
339 : &AArch64::GPR32RegClass;
340 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
341 unsigned ResultReg = createResultReg(RC);
342 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
343 ResultReg).addReg(ZeroReg, getKillRegState(true));
347 unsigned AArch64FastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
348 // Positive zero (+0.0) has to be materialized with a fmov from the zero
349 // register, because the immediate version of fmov cannot encode zero.
350 if (CFP->isNullValue())
351 return fastMaterializeFloatZero(CFP);
353 if (VT != MVT::f32 && VT != MVT::f64)
356 const APFloat Val = CFP->getValueAPF();
357 bool Is64Bit = (VT == MVT::f64);
358 // This checks to see if we can use FMOV instructions to materialize
359 // a constant, otherwise we have to materialize via the constant pool.
360 if (TLI.isFPImmLegal(Val, VT)) {
362 Is64Bit ? AArch64_AM::getFP64Imm(Val) : AArch64_AM::getFP32Imm(Val);
363 assert((Imm != -1) && "Cannot encode floating-point constant.");
364 unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
365 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
368 // Materialize via constant pool. MachineConstantPool wants an explicit
370 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
372 Align = DL.getTypeAllocSize(CFP->getType());
374 unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
375 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
376 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
377 ADRPReg).addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
379 unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
380 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
383 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
387 unsigned AArch64FastISel::materializeGV(const GlobalValue *GV) {
388 // We can't handle thread-local variables quickly yet.
389 if (GV->isThreadLocal())
392 // MachO still uses GOT for large code-model accesses, but ELF requires
393 // movz/movk sequences, which FastISel doesn't handle yet.
394 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
397 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
399 EVT DestEVT = TLI.getValueType(GV->getType(), true);
400 if (!DestEVT.isSimple())
403 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
406 if (OpFlags & AArch64II::MO_GOT) {
408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
410 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
412 ResultReg = createResultReg(&AArch64::GPR64RegClass);
413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
416 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
418 } else if (OpFlags & AArch64II::MO_CONSTPOOL) {
419 // We can't handle addresses loaded from a constant pool quickly yet.
423 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
425 .addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
427 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
431 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
437 unsigned AArch64FastISel::fastMaterializeConstant(const Constant *C) {
438 EVT CEVT = TLI.getValueType(C->getType(), true);
440 // Only handle simple types.
441 if (!CEVT.isSimple())
443 MVT VT = CEVT.getSimpleVT();
445 if (const auto *CI = dyn_cast<ConstantInt>(C))
446 return materializeInt(CI, VT);
447 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
448 return materializeFP(CFP, VT);
449 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
450 return materializeGV(GV);
455 unsigned AArch64FastISel::fastMaterializeFloatZero(const ConstantFP* CFP) {
456 assert(CFP->isNullValue() &&
457 "Floating-point constant is not a positive zero.");
459 if (!isTypeLegal(CFP->getType(), VT))
462 if (VT != MVT::f32 && VT != MVT::f64)
465 bool Is64Bit = (VT == MVT::f64);
466 unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
467 unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
468 return fastEmitInst_r(Opc, TLI.getRegClassFor(VT), ZReg, /*IsKill=*/true);
471 /// \brief Check if the multiply is by a power-of-2 constant.
472 static bool isMulPowOf2(const Value *I) {
473 if (const auto *MI = dyn_cast<MulOperator>(I)) {
474 if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(0)))
475 if (C->getValue().isPowerOf2())
477 if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(1)))
478 if (C->getValue().isPowerOf2())
484 // Computes the address to get to an object.
485 bool AArch64FastISel::computeAddress(const Value *Obj, Address &Addr, Type *Ty)
487 const User *U = nullptr;
488 unsigned Opcode = Instruction::UserOp1;
489 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
490 // Don't walk into other basic blocks unless the object is an alloca from
491 // another block, otherwise it may not have a virtual register assigned.
492 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
493 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
494 Opcode = I->getOpcode();
497 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
498 Opcode = C->getOpcode();
502 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
503 if (Ty->getAddressSpace() > 255)
504 // Fast instruction selection doesn't support the special
511 case Instruction::BitCast: {
512 // Look through bitcasts.
513 return computeAddress(U->getOperand(0), Addr, Ty);
515 case Instruction::IntToPtr: {
516 // Look past no-op inttoptrs.
517 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
518 return computeAddress(U->getOperand(0), Addr, Ty);
521 case Instruction::PtrToInt: {
522 // Look past no-op ptrtoints.
523 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
524 return computeAddress(U->getOperand(0), Addr, Ty);
527 case Instruction::GetElementPtr: {
528 Address SavedAddr = Addr;
529 uint64_t TmpOffset = Addr.getOffset();
531 // Iterate through the GEP folding the constants into offsets where
533 gep_type_iterator GTI = gep_type_begin(U);
534 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
536 const Value *Op = *i;
537 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
538 const StructLayout *SL = DL.getStructLayout(STy);
539 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
540 TmpOffset += SL->getElementOffset(Idx);
542 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
544 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
545 // Constant-offset addressing.
546 TmpOffset += CI->getSExtValue() * S;
549 if (canFoldAddIntoGEP(U, Op)) {
550 // A compatible add with a constant operand. Fold the constant.
552 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
553 TmpOffset += CI->getSExtValue() * S;
554 // Iterate on the other operand.
555 Op = cast<AddOperator>(Op)->getOperand(0);
559 goto unsupported_gep;
564 // Try to grab the base operand now.
565 Addr.setOffset(TmpOffset);
566 if (computeAddress(U->getOperand(0), Addr, Ty))
569 // We failed, restore everything and try the other options.
575 case Instruction::Alloca: {
576 const AllocaInst *AI = cast<AllocaInst>(Obj);
577 DenseMap<const AllocaInst *, int>::iterator SI =
578 FuncInfo.StaticAllocaMap.find(AI);
579 if (SI != FuncInfo.StaticAllocaMap.end()) {
580 Addr.setKind(Address::FrameIndexBase);
581 Addr.setFI(SI->second);
586 case Instruction::Add: {
587 // Adds of constants are common and easy enough.
588 const Value *LHS = U->getOperand(0);
589 const Value *RHS = U->getOperand(1);
591 if (isa<ConstantInt>(LHS))
594 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
595 Addr.setOffset(Addr.getOffset() + CI->getSExtValue());
596 return computeAddress(LHS, Addr, Ty);
599 Address Backup = Addr;
600 if (computeAddress(LHS, Addr, Ty) && computeAddress(RHS, Addr, Ty))
606 case Instruction::Sub: {
607 // Subs of constants are common and easy enough.
608 const Value *LHS = U->getOperand(0);
609 const Value *RHS = U->getOperand(1);
611 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
612 Addr.setOffset(Addr.getOffset() - CI->getSExtValue());
613 return computeAddress(LHS, Addr, Ty);
617 case Instruction::Shl: {
618 if (Addr.getOffsetReg())
621 const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1));
625 unsigned Val = CI->getZExtValue();
626 if (Val < 1 || Val > 3)
629 uint64_t NumBytes = 0;
630 if (Ty && Ty->isSized()) {
631 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
632 NumBytes = NumBits / 8;
633 if (!isPowerOf2_64(NumBits))
637 if (NumBytes != (1ULL << Val))
641 Addr.setExtendType(AArch64_AM::LSL);
643 const Value *Src = U->getOperand(0);
644 if (const auto *I = dyn_cast<Instruction>(Src))
645 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
648 // Fold the zext or sext when it won't become a noop.
649 if (const auto *ZE = dyn_cast<ZExtInst>(Src)) {
650 if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
651 Addr.setExtendType(AArch64_AM::UXTW);
652 Src = ZE->getOperand(0);
654 } else if (const auto *SE = dyn_cast<SExtInst>(Src)) {
655 if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
656 Addr.setExtendType(AArch64_AM::SXTW);
657 Src = SE->getOperand(0);
661 if (const auto *AI = dyn_cast<BinaryOperator>(Src))
662 if (AI->getOpcode() == Instruction::And) {
663 const Value *LHS = AI->getOperand(0);
664 const Value *RHS = AI->getOperand(1);
666 if (const auto *C = dyn_cast<ConstantInt>(LHS))
667 if (C->getValue() == 0xffffffff)
670 if (const auto *C = dyn_cast<ConstantInt>(RHS))
671 if (C->getValue() == 0xffffffff) {
672 Addr.setExtendType(AArch64_AM::UXTW);
673 unsigned Reg = getRegForValue(LHS);
676 bool RegIsKill = hasTrivialKill(LHS);
677 Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
679 Addr.setOffsetReg(Reg);
684 unsigned Reg = getRegForValue(Src);
687 Addr.setOffsetReg(Reg);
690 case Instruction::Mul: {
691 if (Addr.getOffsetReg())
697 const Value *LHS = U->getOperand(0);
698 const Value *RHS = U->getOperand(1);
700 // Canonicalize power-of-2 value to the RHS.
701 if (const auto *C = dyn_cast<ConstantInt>(LHS))
702 if (C->getValue().isPowerOf2())
705 assert(isa<ConstantInt>(RHS) && "Expected an ConstantInt.");
706 const auto *C = cast<ConstantInt>(RHS);
707 unsigned Val = C->getValue().logBase2();
708 if (Val < 1 || Val > 3)
711 uint64_t NumBytes = 0;
712 if (Ty && Ty->isSized()) {
713 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
714 NumBytes = NumBits / 8;
715 if (!isPowerOf2_64(NumBits))
719 if (NumBytes != (1ULL << Val))
723 Addr.setExtendType(AArch64_AM::LSL);
725 const Value *Src = LHS;
726 if (const auto *I = dyn_cast<Instruction>(Src))
727 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
731 // Fold the zext or sext when it won't become a noop.
732 if (const auto *ZE = dyn_cast<ZExtInst>(Src)) {
733 if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
734 Addr.setExtendType(AArch64_AM::UXTW);
735 Src = ZE->getOperand(0);
737 } else if (const auto *SE = dyn_cast<SExtInst>(Src)) {
738 if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
739 Addr.setExtendType(AArch64_AM::SXTW);
740 Src = SE->getOperand(0);
744 unsigned Reg = getRegForValue(Src);
747 Addr.setOffsetReg(Reg);
750 case Instruction::And: {
751 if (Addr.getOffsetReg())
754 if (DL.getTypeSizeInBits(Ty) != 8)
757 const Value *LHS = U->getOperand(0);
758 const Value *RHS = U->getOperand(1);
760 if (const auto *C = dyn_cast<ConstantInt>(LHS))
761 if (C->getValue() == 0xffffffff)
764 if (const auto *C = dyn_cast<ConstantInt>(RHS))
765 if (C->getValue() == 0xffffffff) {
767 Addr.setExtendType(AArch64_AM::LSL);
768 Addr.setExtendType(AArch64_AM::UXTW);
770 unsigned Reg = getRegForValue(LHS);
773 bool RegIsKill = hasTrivialKill(LHS);
774 Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
776 Addr.setOffsetReg(Reg);
781 case Instruction::SExt:
782 case Instruction::ZExt: {
783 if (!Addr.getReg() || Addr.getOffsetReg())
786 const Value *Src = nullptr;
787 // Fold the zext or sext when it won't become a noop.
788 if (const auto *ZE = dyn_cast<ZExtInst>(U)) {
789 if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
790 Addr.setExtendType(AArch64_AM::UXTW);
791 Src = ZE->getOperand(0);
793 } else if (const auto *SE = dyn_cast<SExtInst>(U)) {
794 if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
795 Addr.setExtendType(AArch64_AM::SXTW);
796 Src = SE->getOperand(0);
804 unsigned Reg = getRegForValue(Src);
807 Addr.setOffsetReg(Reg);
813 if (!Addr.getOffsetReg()) {
814 unsigned Reg = getRegForValue(Obj);
817 Addr.setOffsetReg(Reg);
823 unsigned Reg = getRegForValue(Obj);
830 bool AArch64FastISel::computeCallAddress(const Value *V, Address &Addr) {
831 const User *U = nullptr;
832 unsigned Opcode = Instruction::UserOp1;
835 if (const auto *I = dyn_cast<Instruction>(V)) {
836 Opcode = I->getOpcode();
838 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
839 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
840 Opcode = C->getOpcode();
846 case Instruction::BitCast:
847 // Look past bitcasts if its operand is in the same BB.
849 return computeCallAddress(U->getOperand(0), Addr);
851 case Instruction::IntToPtr:
852 // Look past no-op inttoptrs if its operand is in the same BB.
854 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
855 return computeCallAddress(U->getOperand(0), Addr);
857 case Instruction::PtrToInt:
858 // Look past no-op ptrtoints if its operand is in the same BB.
860 TLI.getValueType(U->getType()) == TLI.getPointerTy())
861 return computeCallAddress(U->getOperand(0), Addr);
865 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
866 Addr.setGlobalValue(GV);
870 // If all else fails, try to materialize the value in a register.
871 if (!Addr.getGlobalValue()) {
872 Addr.setReg(getRegForValue(V));
873 return Addr.getReg() != 0;
880 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
881 EVT evt = TLI.getValueType(Ty, true);
883 // Only handle simple types.
884 if (evt == MVT::Other || !evt.isSimple())
886 VT = evt.getSimpleVT();
888 // This is a legal type, but it's not something we handle in fast-isel.
892 // Handle all other legal types, i.e. a register that will directly hold this
894 return TLI.isTypeLegal(VT);
897 /// \brief Determine if the value type is supported by FastISel.
899 /// FastISel for AArch64 can handle more value types than are legal. This adds
900 /// simple value type such as i1, i8, and i16.
901 bool AArch64FastISel::isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed) {
902 if (Ty->isVectorTy() && !IsVectorAllowed)
905 if (isTypeLegal(Ty, VT))
908 // If this is a type than can be sign or zero-extended to a basic operation
909 // go ahead and accept it now.
910 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
916 bool AArch64FastISel::isValueAvailable(const Value *V) const {
917 if (!isa<Instruction>(V))
920 const auto *I = cast<Instruction>(V);
921 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
927 bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) {
928 unsigned ScaleFactor = getImplicitScaleFactor(VT);
932 bool ImmediateOffsetNeedsLowering = false;
933 bool RegisterOffsetNeedsLowering = false;
934 int64_t Offset = Addr.getOffset();
935 if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
936 ImmediateOffsetNeedsLowering = true;
937 else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
938 !isUInt<12>(Offset / ScaleFactor))
939 ImmediateOffsetNeedsLowering = true;
941 // Cannot encode an offset register and an immediate offset in the same
942 // instruction. Fold the immediate offset into the load/store instruction and
943 // emit an additonal add to take care of the offset register.
944 if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.isRegBase() &&
946 RegisterOffsetNeedsLowering = true;
948 // Cannot encode zero register as base.
949 if (Addr.isRegBase() && Addr.getOffsetReg() && !Addr.getReg())
950 RegisterOffsetNeedsLowering = true;
952 // If this is a stack pointer and the offset needs to be simplified then put
953 // the alloca address into a register, set the base type back to register and
954 // continue. This should almost never happen.
955 if (ImmediateOffsetNeedsLowering && Addr.isFIBase()) {
956 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
957 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
959 .addFrameIndex(Addr.getFI())
962 Addr.setKind(Address::RegBase);
963 Addr.setReg(ResultReg);
966 if (RegisterOffsetNeedsLowering) {
967 unsigned ResultReg = 0;
969 if (Addr.getExtendType() == AArch64_AM::SXTW ||
970 Addr.getExtendType() == AArch64_AM::UXTW )
971 ResultReg = emitAddSub_rx(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
972 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
973 /*TODO:IsKill=*/false, Addr.getExtendType(),
976 ResultReg = emitAddSub_rs(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
977 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
978 /*TODO:IsKill=*/false, AArch64_AM::LSL,
981 if (Addr.getExtendType() == AArch64_AM::UXTW)
982 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
983 /*Op0IsKill=*/false, Addr.getShift(),
985 else if (Addr.getExtendType() == AArch64_AM::SXTW)
986 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
987 /*Op0IsKill=*/false, Addr.getShift(),
990 ResultReg = emitLSL_ri(MVT::i64, MVT::i64, Addr.getOffsetReg(),
991 /*Op0IsKill=*/false, Addr.getShift());
996 Addr.setReg(ResultReg);
997 Addr.setOffsetReg(0);
999 Addr.setExtendType(AArch64_AM::InvalidShiftExtend);
1002 // Since the offset is too large for the load/store instruction get the
1003 // reg+offset into a register.
1004 if (ImmediateOffsetNeedsLowering) {
1007 // Try to fold the immediate into the add instruction.
1008 ResultReg = emitAdd_ri_(MVT::i64, Addr.getReg(), /*IsKill=*/false, Offset);
1010 ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
1014 Addr.setReg(ResultReg);
1020 void AArch64FastISel::addLoadStoreOperands(Address &Addr,
1021 const MachineInstrBuilder &MIB,
1023 unsigned ScaleFactor,
1024 MachineMemOperand *MMO) {
1025 int64_t Offset = Addr.getOffset() / ScaleFactor;
1026 // Frame base works a bit differently. Handle it separately.
1027 if (Addr.isFIBase()) {
1028 int FI = Addr.getFI();
1029 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
1030 // and alignment should be based on the VT.
1031 MMO = FuncInfo.MF->getMachineMemOperand(
1032 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
1033 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
1034 // Now add the rest of the operands.
1035 MIB.addFrameIndex(FI).addImm(Offset);
1037 assert(Addr.isRegBase() && "Unexpected address kind.");
1038 const MCInstrDesc &II = MIB->getDesc();
1039 unsigned Idx = (Flags & MachineMemOperand::MOStore) ? 1 : 0;
1041 constrainOperandRegClass(II, Addr.getReg(), II.getNumDefs()+Idx));
1043 constrainOperandRegClass(II, Addr.getOffsetReg(), II.getNumDefs()+Idx+1));
1044 if (Addr.getOffsetReg()) {
1045 assert(Addr.getOffset() == 0 && "Unexpected offset");
1046 bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
1047 Addr.getExtendType() == AArch64_AM::SXTX;
1048 MIB.addReg(Addr.getReg());
1049 MIB.addReg(Addr.getOffsetReg());
1050 MIB.addImm(IsSigned);
1051 MIB.addImm(Addr.getShift() != 0);
1053 MIB.addReg(Addr.getReg());
1059 MIB.addMemOperand(MMO);
1062 unsigned AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
1063 const Value *RHS, bool SetFlags,
1064 bool WantResult, bool IsZExt) {
1065 AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
1066 bool NeedExtend = false;
1067 switch (RetVT.SimpleTy) {
1075 ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
1079 ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
1081 case MVT::i32: // fall-through
1086 RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
1088 // Canonicalize immediates to the RHS first.
1089 if (UseAdd && isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1090 std::swap(LHS, RHS);
1092 // Canonicalize mul by power of 2 to the RHS.
1093 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1094 if (isMulPowOf2(LHS))
1095 std::swap(LHS, RHS);
1097 // Canonicalize shift immediate to the RHS.
1098 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1099 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
1100 if (isa<ConstantInt>(SI->getOperand(1)))
1101 if (SI->getOpcode() == Instruction::Shl ||
1102 SI->getOpcode() == Instruction::LShr ||
1103 SI->getOpcode() == Instruction::AShr )
1104 std::swap(LHS, RHS);
1106 unsigned LHSReg = getRegForValue(LHS);
1109 bool LHSIsKill = hasTrivialKill(LHS);
1112 LHSReg = emitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
1114 unsigned ResultReg = 0;
1115 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1116 uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
1117 if (C->isNegative())
1118 ResultReg = emitAddSub_ri(!UseAdd, RetVT, LHSReg, LHSIsKill, -Imm,
1119 SetFlags, WantResult);
1121 ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, Imm, SetFlags,
1127 // Only extend the RHS within the instruction if there is a valid extend type.
1128 if (ExtendType != AArch64_AM::InvalidShiftExtend && RHS->hasOneUse() &&
1129 isValueAvailable(RHS)) {
1130 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
1131 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
1132 if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
1133 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1136 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1137 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1138 RHSIsKill, ExtendType, C->getZExtValue(),
1139 SetFlags, WantResult);
1141 unsigned RHSReg = getRegForValue(RHS);
1144 bool RHSIsKill = hasTrivialKill(RHS);
1145 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1146 ExtendType, 0, SetFlags, WantResult);
1149 // Check if the mul can be folded into the instruction.
1150 if (RHS->hasOneUse() && isValueAvailable(RHS))
1151 if (isMulPowOf2(RHS)) {
1152 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1153 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1155 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1156 if (C->getValue().isPowerOf2())
1157 std::swap(MulLHS, MulRHS);
1159 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1160 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1161 unsigned RHSReg = getRegForValue(MulLHS);
1164 bool RHSIsKill = hasTrivialKill(MulLHS);
1165 return emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1166 AArch64_AM::LSL, ShiftVal, SetFlags, WantResult);
1169 // Check if the shift can be folded into the instruction.
1170 if (RHS->hasOneUse() && isValueAvailable(RHS))
1171 if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
1172 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1173 AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
1174 switch (SI->getOpcode()) {
1176 case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
1177 case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
1178 case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
1180 uint64_t ShiftVal = C->getZExtValue();
1181 if (ShiftType != AArch64_AM::InvalidShiftExtend) {
1182 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1185 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1186 return emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1187 RHSIsKill, ShiftType, ShiftVal, SetFlags,
1193 unsigned RHSReg = getRegForValue(RHS);
1196 bool RHSIsKill = hasTrivialKill(RHS);
1199 RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
1201 return emitAddSub_rr(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1202 SetFlags, WantResult);
1205 unsigned AArch64FastISel::emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
1206 bool LHSIsKill, unsigned RHSReg,
1207 bool RHSIsKill, bool SetFlags,
1209 assert(LHSReg && RHSReg && "Invalid register number.");
1211 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1214 static const unsigned OpcTable[2][2][2] = {
1215 { { AArch64::SUBWrr, AArch64::SUBXrr },
1216 { AArch64::ADDWrr, AArch64::ADDXrr } },
1217 { { AArch64::SUBSWrr, AArch64::SUBSXrr },
1218 { AArch64::ADDSWrr, AArch64::ADDSXrr } }
1220 bool Is64Bit = RetVT == MVT::i64;
1221 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1222 const TargetRegisterClass *RC =
1223 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1226 ResultReg = createResultReg(RC);
1228 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1230 const MCInstrDesc &II = TII.get(Opc);
1231 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1232 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1233 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1234 .addReg(LHSReg, getKillRegState(LHSIsKill))
1235 .addReg(RHSReg, getKillRegState(RHSIsKill));
1239 unsigned AArch64FastISel::emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
1240 bool LHSIsKill, uint64_t Imm,
1241 bool SetFlags, bool WantResult) {
1242 assert(LHSReg && "Invalid register number.");
1244 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1248 if (isUInt<12>(Imm))
1250 else if ((Imm & 0xfff000) == Imm) {
1256 static const unsigned OpcTable[2][2][2] = {
1257 { { AArch64::SUBWri, AArch64::SUBXri },
1258 { AArch64::ADDWri, AArch64::ADDXri } },
1259 { { AArch64::SUBSWri, AArch64::SUBSXri },
1260 { AArch64::ADDSWri, AArch64::ADDSXri } }
1262 bool Is64Bit = RetVT == MVT::i64;
1263 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1264 const TargetRegisterClass *RC;
1266 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1268 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1271 ResultReg = createResultReg(RC);
1273 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1275 const MCInstrDesc &II = TII.get(Opc);
1276 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1277 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1278 .addReg(LHSReg, getKillRegState(LHSIsKill))
1280 .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
1284 unsigned AArch64FastISel::emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
1285 bool LHSIsKill, unsigned RHSReg,
1287 AArch64_AM::ShiftExtendType ShiftType,
1288 uint64_t ShiftImm, bool SetFlags,
1290 assert(LHSReg && RHSReg && "Invalid register number.");
1292 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1295 static const unsigned OpcTable[2][2][2] = {
1296 { { AArch64::SUBWrs, AArch64::SUBXrs },
1297 { AArch64::ADDWrs, AArch64::ADDXrs } },
1298 { { AArch64::SUBSWrs, AArch64::SUBSXrs },
1299 { AArch64::ADDSWrs, AArch64::ADDSXrs } }
1301 bool Is64Bit = RetVT == MVT::i64;
1302 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1303 const TargetRegisterClass *RC =
1304 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1307 ResultReg = createResultReg(RC);
1309 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1311 const MCInstrDesc &II = TII.get(Opc);
1312 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1313 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1314 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1315 .addReg(LHSReg, getKillRegState(LHSIsKill))
1316 .addReg(RHSReg, getKillRegState(RHSIsKill))
1317 .addImm(getShifterImm(ShiftType, ShiftImm));
1321 unsigned AArch64FastISel::emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
1322 bool LHSIsKill, unsigned RHSReg,
1324 AArch64_AM::ShiftExtendType ExtType,
1325 uint64_t ShiftImm, bool SetFlags,
1327 assert(LHSReg && RHSReg && "Invalid register number.");
1329 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1332 static const unsigned OpcTable[2][2][2] = {
1333 { { AArch64::SUBWrx, AArch64::SUBXrx },
1334 { AArch64::ADDWrx, AArch64::ADDXrx } },
1335 { { AArch64::SUBSWrx, AArch64::SUBSXrx },
1336 { AArch64::ADDSWrx, AArch64::ADDSXrx } }
1338 bool Is64Bit = RetVT == MVT::i64;
1339 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1340 const TargetRegisterClass *RC = nullptr;
1342 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1344 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1347 ResultReg = createResultReg(RC);
1349 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1351 const MCInstrDesc &II = TII.get(Opc);
1352 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1353 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1354 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1355 .addReg(LHSReg, getKillRegState(LHSIsKill))
1356 .addReg(RHSReg, getKillRegState(RHSIsKill))
1357 .addImm(getArithExtendImm(ExtType, ShiftImm));
1361 bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1362 Type *Ty = LHS->getType();
1363 EVT EVT = TLI.getValueType(Ty, true);
1364 if (!EVT.isSimple())
1366 MVT VT = EVT.getSimpleVT();
1368 switch (VT.SimpleTy) {
1376 return emitICmp(VT, LHS, RHS, IsZExt);
1379 return emitFCmp(VT, LHS, RHS);
1383 bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1385 return emitSub(RetVT, LHS, RHS, /*SetFlags=*/true, /*WantResult=*/false,
1389 bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1391 return emitAddSub_ri(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, Imm,
1392 /*SetFlags=*/true, /*WantResult=*/false) != 0;
1395 bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1396 if (RetVT != MVT::f32 && RetVT != MVT::f64)
1399 // Check to see if the 2nd operand is a constant that we can encode directly
1401 bool UseImm = false;
1402 if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1403 if (CFP->isZero() && !CFP->isNegative())
1406 unsigned LHSReg = getRegForValue(LHS);
1409 bool LHSIsKill = hasTrivialKill(LHS);
1412 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1413 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1414 .addReg(LHSReg, getKillRegState(LHSIsKill));
1418 unsigned RHSReg = getRegForValue(RHS);
1421 bool RHSIsKill = hasTrivialKill(RHS);
1423 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1424 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1425 .addReg(LHSReg, getKillRegState(LHSIsKill))
1426 .addReg(RHSReg, getKillRegState(RHSIsKill));
1430 unsigned AArch64FastISel::emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
1431 bool SetFlags, bool WantResult, bool IsZExt) {
1432 return emitAddSub(/*UseAdd=*/true, RetVT, LHS, RHS, SetFlags, WantResult,
1436 /// \brief This method is a wrapper to simplify add emission.
1438 /// First try to emit an add with an immediate operand using emitAddSub_ri. If
1439 /// that fails, then try to materialize the immediate into a register and use
1440 /// emitAddSub_rr instead.
1441 unsigned AArch64FastISel::emitAdd_ri_(MVT VT, unsigned Op0, bool Op0IsKill,
1445 ResultReg = emitAddSub_ri(false, VT, Op0, Op0IsKill, -Imm);
1447 ResultReg = emitAddSub_ri(true, VT, Op0, Op0IsKill, Imm);
1452 unsigned CReg = fastEmit_i(VT, VT, ISD::Constant, Imm);
1456 ResultReg = emitAddSub_rr(true, VT, Op0, Op0IsKill, CReg, true);
1460 unsigned AArch64FastISel::emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
1461 bool SetFlags, bool WantResult, bool IsZExt) {
1462 return emitAddSub(/*UseAdd=*/false, RetVT, LHS, RHS, SetFlags, WantResult,
1466 unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1467 bool LHSIsKill, unsigned RHSReg,
1468 bool RHSIsKill, bool WantResult) {
1469 return emitAddSub_rr(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1470 RHSIsKill, /*SetFlags=*/true, WantResult);
1473 unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1474 bool LHSIsKill, unsigned RHSReg,
1476 AArch64_AM::ShiftExtendType ShiftType,
1477 uint64_t ShiftImm, bool WantResult) {
1478 return emitAddSub_rs(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1479 RHSIsKill, ShiftType, ShiftImm, /*SetFlags=*/true,
1483 unsigned AArch64FastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
1484 const Value *LHS, const Value *RHS) {
1485 // Canonicalize immediates to the RHS first.
1486 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1487 std::swap(LHS, RHS);
1489 // Canonicalize mul by power-of-2 to the RHS.
1490 if (LHS->hasOneUse() && isValueAvailable(LHS))
1491 if (isMulPowOf2(LHS))
1492 std::swap(LHS, RHS);
1494 // Canonicalize shift immediate to the RHS.
1495 if (LHS->hasOneUse() && isValueAvailable(LHS))
1496 if (const auto *SI = dyn_cast<ShlOperator>(LHS))
1497 if (isa<ConstantInt>(SI->getOperand(1)))
1498 std::swap(LHS, RHS);
1500 unsigned LHSReg = getRegForValue(LHS);
1503 bool LHSIsKill = hasTrivialKill(LHS);
1505 unsigned ResultReg = 0;
1506 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1507 uint64_t Imm = C->getZExtValue();
1508 ResultReg = emitLogicalOp_ri(ISDOpc, RetVT, LHSReg, LHSIsKill, Imm);
1513 // Check if the mul can be folded into the instruction.
1514 if (RHS->hasOneUse() && isValueAvailable(RHS))
1515 if (isMulPowOf2(RHS)) {
1516 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1517 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1519 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1520 if (C->getValue().isPowerOf2())
1521 std::swap(MulLHS, MulRHS);
1523 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1524 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1526 unsigned RHSReg = getRegForValue(MulLHS);
1529 bool RHSIsKill = hasTrivialKill(MulLHS);
1530 return emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1531 RHSIsKill, ShiftVal);
1534 // Check if the shift can be folded into the instruction.
1535 if (RHS->hasOneUse() && isValueAvailable(RHS))
1536 if (const auto *SI = dyn_cast<ShlOperator>(RHS))
1537 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1538 uint64_t ShiftVal = C->getZExtValue();
1539 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1542 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1543 return emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1544 RHSIsKill, ShiftVal);
1547 unsigned RHSReg = getRegForValue(RHS);
1550 bool RHSIsKill = hasTrivialKill(RHS);
1552 MVT VT = std::max(MVT::i32, RetVT.SimpleTy);
1553 ResultReg = fastEmit_rr(VT, VT, ISDOpc, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1554 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1555 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1556 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1561 unsigned AArch64FastISel::emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT,
1562 unsigned LHSReg, bool LHSIsKill,
1564 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1565 "ISD nodes are not consecutive!");
1566 static const unsigned OpcTable[3][2] = {
1567 { AArch64::ANDWri, AArch64::ANDXri },
1568 { AArch64::ORRWri, AArch64::ORRXri },
1569 { AArch64::EORWri, AArch64::EORXri }
1571 const TargetRegisterClass *RC;
1574 switch (RetVT.SimpleTy) {
1581 unsigned Idx = ISDOpc - ISD::AND;
1582 Opc = OpcTable[Idx][0];
1583 RC = &AArch64::GPR32spRegClass;
1588 Opc = OpcTable[ISDOpc - ISD::AND][1];
1589 RC = &AArch64::GPR64spRegClass;
1594 if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
1597 unsigned ResultReg =
1598 fastEmitInst_ri(Opc, RC, LHSReg, LHSIsKill,
1599 AArch64_AM::encodeLogicalImmediate(Imm, RegSize));
1600 if (RetVT >= MVT::i8 && RetVT <= MVT::i16 && ISDOpc != ISD::AND) {
1601 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1602 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1607 unsigned AArch64FastISel::emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT,
1608 unsigned LHSReg, bool LHSIsKill,
1609 unsigned RHSReg, bool RHSIsKill,
1610 uint64_t ShiftImm) {
1611 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1612 "ISD nodes are not consecutive!");
1613 static const unsigned OpcTable[3][2] = {
1614 { AArch64::ANDWrs, AArch64::ANDXrs },
1615 { AArch64::ORRWrs, AArch64::ORRXrs },
1616 { AArch64::EORWrs, AArch64::EORXrs }
1618 const TargetRegisterClass *RC;
1620 switch (RetVT.SimpleTy) {
1627 Opc = OpcTable[ISDOpc - ISD::AND][0];
1628 RC = &AArch64::GPR32RegClass;
1631 Opc = OpcTable[ISDOpc - ISD::AND][1];
1632 RC = &AArch64::GPR64RegClass;
1635 unsigned ResultReg =
1636 fastEmitInst_rri(Opc, RC, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1637 AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftImm));
1638 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1639 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1640 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1645 unsigned AArch64FastISel::emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1647 return emitLogicalOp_ri(ISD::AND, RetVT, LHSReg, LHSIsKill, Imm);
1650 unsigned AArch64FastISel::emitLoad(MVT VT, MVT RetVT, Address Addr,
1651 bool WantZExt, MachineMemOperand *MMO) {
1652 // Simplify this down to something we can handle.
1653 if (!simplifyAddress(Addr, VT))
1656 unsigned ScaleFactor = getImplicitScaleFactor(VT);
1658 llvm_unreachable("Unexpected value type.");
1660 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1661 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1662 bool UseScaled = true;
1663 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1668 static const unsigned GPOpcTable[2][8][4] = {
1670 { { AArch64::LDURSBWi, AArch64::LDURSHWi, AArch64::LDURWi,
1672 { AArch64::LDURSBXi, AArch64::LDURSHXi, AArch64::LDURSWi,
1674 { AArch64::LDRSBWui, AArch64::LDRSHWui, AArch64::LDRWui,
1676 { AArch64::LDRSBXui, AArch64::LDRSHXui, AArch64::LDRSWui,
1678 { AArch64::LDRSBWroX, AArch64::LDRSHWroX, AArch64::LDRWroX,
1680 { AArch64::LDRSBXroX, AArch64::LDRSHXroX, AArch64::LDRSWroX,
1682 { AArch64::LDRSBWroW, AArch64::LDRSHWroW, AArch64::LDRWroW,
1684 { AArch64::LDRSBXroW, AArch64::LDRSHXroW, AArch64::LDRSWroW,
1688 { { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1690 { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1692 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1694 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1696 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1698 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1700 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1702 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1707 static const unsigned FPOpcTable[4][2] = {
1708 { AArch64::LDURSi, AArch64::LDURDi },
1709 { AArch64::LDRSui, AArch64::LDRDui },
1710 { AArch64::LDRSroX, AArch64::LDRDroX },
1711 { AArch64::LDRSroW, AArch64::LDRDroW }
1715 const TargetRegisterClass *RC;
1716 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1717 Addr.getOffsetReg();
1718 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1719 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1720 Addr.getExtendType() == AArch64_AM::SXTW)
1723 bool IsRet64Bit = RetVT == MVT::i64;
1724 switch (VT.SimpleTy) {
1726 llvm_unreachable("Unexpected value type.");
1727 case MVT::i1: // Intentional fall-through.
1729 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][0];
1730 RC = (IsRet64Bit && !WantZExt) ?
1731 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1734 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][1];
1735 RC = (IsRet64Bit && !WantZExt) ?
1736 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1739 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][2];
1740 RC = (IsRet64Bit && !WantZExt) ?
1741 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1744 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][3];
1745 RC = &AArch64::GPR64RegClass;
1748 Opc = FPOpcTable[Idx][0];
1749 RC = &AArch64::FPR32RegClass;
1752 Opc = FPOpcTable[Idx][1];
1753 RC = &AArch64::FPR64RegClass;
1757 // Create the base instruction, then add the operands.
1758 unsigned ResultReg = createResultReg(RC);
1759 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1760 TII.get(Opc), ResultReg);
1761 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
1763 // Loading an i1 requires special handling.
1764 if (VT == MVT::i1) {
1765 unsigned ANDReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, 1);
1766 assert(ANDReg && "Unexpected AND instruction emission failure.");
1770 // For zero-extending loads to 64bit we emit a 32bit load and then convert
1771 // the 32bit reg to a 64bit reg.
1772 if (WantZExt && RetVT == MVT::i64 && VT <= MVT::i32) {
1773 unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
1774 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1775 TII.get(AArch64::SUBREG_TO_REG), Reg64)
1777 .addReg(ResultReg, getKillRegState(true))
1778 .addImm(AArch64::sub_32);
1784 bool AArch64FastISel::selectAddSub(const Instruction *I) {
1786 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1790 return selectOperator(I, I->getOpcode());
1793 switch (I->getOpcode()) {
1795 llvm_unreachable("Unexpected instruction.");
1796 case Instruction::Add:
1797 ResultReg = emitAdd(VT, I->getOperand(0), I->getOperand(1));
1799 case Instruction::Sub:
1800 ResultReg = emitSub(VT, I->getOperand(0), I->getOperand(1));
1806 updateValueMap(I, ResultReg);
1810 bool AArch64FastISel::selectLogicalOp(const Instruction *I) {
1812 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1816 return selectOperator(I, I->getOpcode());
1819 switch (I->getOpcode()) {
1821 llvm_unreachable("Unexpected instruction.");
1822 case Instruction::And:
1823 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
1825 case Instruction::Or:
1826 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
1828 case Instruction::Xor:
1829 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
1835 updateValueMap(I, ResultReg);
1839 bool AArch64FastISel::selectLoad(const Instruction *I) {
1841 // Verify we have a legal type before going any further. Currently, we handle
1842 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1843 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1844 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true) ||
1845 cast<LoadInst>(I)->isAtomic())
1848 // See if we can handle this address.
1850 if (!computeAddress(I->getOperand(0), Addr, I->getType()))
1853 // Fold the following sign-/zero-extend into the load instruction.
1854 bool WantZExt = true;
1856 const Value *IntExtVal = nullptr;
1857 if (I->hasOneUse()) {
1858 if (const auto *ZE = dyn_cast<ZExtInst>(I->use_begin()->getUser())) {
1859 if (isTypeSupported(ZE->getType(), RetVT))
1863 } else if (const auto *SE = dyn_cast<SExtInst>(I->use_begin()->getUser())) {
1864 if (isTypeSupported(SE->getType(), RetVT))
1872 unsigned ResultReg =
1873 emitLoad(VT, RetVT, Addr, WantZExt, createMachineMemOperandFor(I));
1877 // There are a few different cases we have to handle, because the load or the
1878 // sign-/zero-extend might not be selected by FastISel if we fall-back to
1879 // SelectionDAG. There is also an ordering issue when both instructions are in
1880 // different basic blocks.
1881 // 1.) The load instruction is selected by FastISel, but the integer extend
1882 // not. This usually happens when the integer extend is in a different
1883 // basic block and SelectionDAG took over for that basic block.
1884 // 2.) The load instruction is selected before the integer extend. This only
1885 // happens when the integer extend is in a different basic block.
1886 // 3.) The load instruction is selected by SelectionDAG and the integer extend
1887 // by FastISel. This happens if there are instructions between the load
1888 // and the integer extend that couldn't be selected by FastISel.
1890 // The integer extend hasn't been emitted yet. FastISel or SelectionDAG
1891 // could select it. Emit a copy to subreg if necessary. FastISel will remove
1892 // it when it selects the integer extend.
1893 unsigned Reg = lookUpRegForValue(IntExtVal);
1895 if (RetVT == MVT::i64 && VT <= MVT::i32) {
1897 // Delete the last emitted instruction from emitLoad (SUBREG_TO_REG).
1898 std::prev(FuncInfo.InsertPt)->eraseFromParent();
1899 ResultReg = std::prev(FuncInfo.InsertPt)->getOperand(0).getReg();
1901 ResultReg = fastEmitInst_extractsubreg(MVT::i32, ResultReg,
1905 updateValueMap(I, ResultReg);
1909 // The integer extend has already been emitted - delete all the instructions
1910 // that have been emitted by the integer extend lowering code and use the
1911 // result from the load instruction directly.
1913 auto *MI = MRI.getUniqueVRegDef(Reg);
1917 for (auto &Opnd : MI->uses()) {
1919 Reg = Opnd.getReg();
1923 MI->eraseFromParent();
1925 updateValueMap(IntExtVal, ResultReg);
1929 updateValueMap(I, ResultReg);
1933 bool AArch64FastISel::emitStore(MVT VT, unsigned SrcReg, Address Addr,
1934 MachineMemOperand *MMO) {
1935 // Simplify this down to something we can handle.
1936 if (!simplifyAddress(Addr, VT))
1939 unsigned ScaleFactor = getImplicitScaleFactor(VT);
1941 llvm_unreachable("Unexpected value type.");
1943 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1944 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1945 bool UseScaled = true;
1946 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1951 static const unsigned OpcTable[4][6] = {
1952 { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
1953 AArch64::STURSi, AArch64::STURDi },
1954 { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
1955 AArch64::STRSui, AArch64::STRDui },
1956 { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
1957 AArch64::STRSroX, AArch64::STRDroX },
1958 { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
1959 AArch64::STRSroW, AArch64::STRDroW }
1963 bool VTIsi1 = false;
1964 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1965 Addr.getOffsetReg();
1966 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1967 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1968 Addr.getExtendType() == AArch64_AM::SXTW)
1971 switch (VT.SimpleTy) {
1972 default: llvm_unreachable("Unexpected value type.");
1973 case MVT::i1: VTIsi1 = true;
1974 case MVT::i8: Opc = OpcTable[Idx][0]; break;
1975 case MVT::i16: Opc = OpcTable[Idx][1]; break;
1976 case MVT::i32: Opc = OpcTable[Idx][2]; break;
1977 case MVT::i64: Opc = OpcTable[Idx][3]; break;
1978 case MVT::f32: Opc = OpcTable[Idx][4]; break;
1979 case MVT::f64: Opc = OpcTable[Idx][5]; break;
1982 // Storing an i1 requires special handling.
1983 if (VTIsi1 && SrcReg != AArch64::WZR) {
1984 unsigned ANDReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
1985 assert(ANDReg && "Unexpected AND instruction emission failure.");
1988 // Create the base instruction, then add the operands.
1989 const MCInstrDesc &II = TII.get(Opc);
1990 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
1991 MachineInstrBuilder MIB =
1992 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(SrcReg);
1993 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
1998 bool AArch64FastISel::selectStore(const Instruction *I) {
2000 const Value *Op0 = I->getOperand(0);
2001 // Verify we have a legal type before going any further. Currently, we handle
2002 // simple types that will directly fit in a register (i32/f32/i64/f64) or
2003 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
2004 if (!isTypeSupported(Op0->getType(), VT, /*IsVectorAllowed=*/true) ||
2005 cast<StoreInst>(I)->isAtomic())
2008 // Get the value to be stored into a register. Use the zero register directly
2009 // when possible to avoid an unnecessary copy and a wasted register.
2010 unsigned SrcReg = 0;
2011 if (const auto *CI = dyn_cast<ConstantInt>(Op0)) {
2013 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
2014 } else if (const auto *CF = dyn_cast<ConstantFP>(Op0)) {
2015 if (CF->isZero() && !CF->isNegative()) {
2016 VT = MVT::getIntegerVT(VT.getSizeInBits());
2017 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
2022 SrcReg = getRegForValue(Op0);
2027 // See if we can handle this address.
2029 if (!computeAddress(I->getOperand(1), Addr, I->getOperand(0)->getType()))
2032 if (!emitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
2037 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
2039 case CmpInst::FCMP_ONE:
2040 case CmpInst::FCMP_UEQ:
2042 // AL is our "false" for now. The other two need more compares.
2043 return AArch64CC::AL;
2044 case CmpInst::ICMP_EQ:
2045 case CmpInst::FCMP_OEQ:
2046 return AArch64CC::EQ;
2047 case CmpInst::ICMP_SGT:
2048 case CmpInst::FCMP_OGT:
2049 return AArch64CC::GT;
2050 case CmpInst::ICMP_SGE:
2051 case CmpInst::FCMP_OGE:
2052 return AArch64CC::GE;
2053 case CmpInst::ICMP_UGT:
2054 case CmpInst::FCMP_UGT:
2055 return AArch64CC::HI;
2056 case CmpInst::FCMP_OLT:
2057 return AArch64CC::MI;
2058 case CmpInst::ICMP_ULE:
2059 case CmpInst::FCMP_OLE:
2060 return AArch64CC::LS;
2061 case CmpInst::FCMP_ORD:
2062 return AArch64CC::VC;
2063 case CmpInst::FCMP_UNO:
2064 return AArch64CC::VS;
2065 case CmpInst::FCMP_UGE:
2066 return AArch64CC::PL;
2067 case CmpInst::ICMP_SLT:
2068 case CmpInst::FCMP_ULT:
2069 return AArch64CC::LT;
2070 case CmpInst::ICMP_SLE:
2071 case CmpInst::FCMP_ULE:
2072 return AArch64CC::LE;
2073 case CmpInst::FCMP_UNE:
2074 case CmpInst::ICMP_NE:
2075 return AArch64CC::NE;
2076 case CmpInst::ICMP_UGE:
2077 return AArch64CC::HS;
2078 case CmpInst::ICMP_ULT:
2079 return AArch64CC::LO;
2083 /// \brief Try to emit a combined compare-and-branch instruction.
2084 bool AArch64FastISel::emitCompareAndBranch(const BranchInst *BI) {
2085 assert(isa<CmpInst>(BI->getCondition()) && "Expected cmp instruction");
2086 const CmpInst *CI = cast<CmpInst>(BI->getCondition());
2087 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2089 const Value *LHS = CI->getOperand(0);
2090 const Value *RHS = CI->getOperand(1);
2092 Type *Ty = LHS->getType();
2093 if (!Ty->isIntegerTy())
2096 unsigned BW = cast<IntegerType>(Ty)->getBitWidth();
2097 if (BW != 1 && BW != 8 && BW != 16 && BW != 32 && BW != 64)
2100 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2101 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2103 // Try to take advantage of fallthrough opportunities.
2104 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2105 std::swap(TBB, FBB);
2106 Predicate = CmpInst::getInversePredicate(Predicate);
2111 if ((Predicate == CmpInst::ICMP_EQ) || (Predicate == CmpInst::ICMP_NE)) {
2112 if (const auto *C = dyn_cast<ConstantInt>(LHS))
2113 if (C->isNullValue())
2114 std::swap(LHS, RHS);
2116 if (!isa<ConstantInt>(RHS))
2119 if (!cast<ConstantInt>(RHS)->isNullValue())
2122 if (const auto *AI = dyn_cast<BinaryOperator>(LHS))
2123 if (AI->getOpcode() == Instruction::And) {
2124 const Value *AndLHS = AI->getOperand(0);
2125 const Value *AndRHS = AI->getOperand(1);
2127 if (const auto *C = dyn_cast<ConstantInt>(AndLHS))
2128 if (C->getValue().isPowerOf2())
2129 std::swap(AndLHS, AndRHS);
2131 if (const auto *C = dyn_cast<ConstantInt>(AndRHS))
2132 if (C->getValue().isPowerOf2()) {
2133 TestBit = C->getValue().logBase2();
2137 IsCmpNE = Predicate == CmpInst::ICMP_NE;
2138 } else if (Predicate == CmpInst::ICMP_SLT) {
2139 if (!isa<ConstantInt>(RHS))
2142 if (!cast<ConstantInt>(RHS)->isNullValue())
2147 } else if (Predicate == CmpInst::ICMP_SGT) {
2148 if (!isa<ConstantInt>(RHS))
2151 if (cast<ConstantInt>(RHS)->getValue() != -1)
2159 static const unsigned OpcTable[2][2][2] = {
2160 { {AArch64::CBZW, AArch64::CBZX },
2161 {AArch64::CBNZW, AArch64::CBNZX} },
2162 { {AArch64::TBZW, AArch64::TBZX },
2163 {AArch64::TBNZW, AArch64::TBNZX} }
2166 bool IsBitTest = TestBit != -1;
2167 bool Is64Bit = BW == 64;
2168 if (TestBit < 32 && TestBit >= 0)
2171 unsigned Opc = OpcTable[IsBitTest][IsCmpNE][Is64Bit];
2172 const MCInstrDesc &II = TII.get(Opc);
2174 unsigned SrcReg = getRegForValue(LHS);
2177 bool SrcIsKill = hasTrivialKill(LHS);
2179 if (BW == 64 && !Is64Bit)
2180 SrcReg = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
2183 // Emit the combined compare and branch instruction.
2184 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
2185 MachineInstrBuilder MIB =
2186 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
2187 .addReg(SrcReg, getKillRegState(SrcIsKill));
2189 MIB.addImm(TestBit);
2192 // Obtain the branch weight and add the TrueBB to the successor list.
2193 uint32_t BranchWeight = 0;
2195 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2196 TBB->getBasicBlock());
2197 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2198 fastEmitBranch(FBB, DbgLoc);
2203 bool AArch64FastISel::selectBranch(const Instruction *I) {
2204 const BranchInst *BI = cast<BranchInst>(I);
2205 if (BI->isUnconditional()) {
2206 MachineBasicBlock *MSucc = FuncInfo.MBBMap[BI->getSuccessor(0)];
2207 fastEmitBranch(MSucc, BI->getDebugLoc());
2211 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2212 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2214 AArch64CC::CondCode CC = AArch64CC::NE;
2215 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
2216 if (CI->hasOneUse() && isValueAvailable(CI)) {
2217 // Try to optimize or fold the cmp.
2218 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2219 switch (Predicate) {
2222 case CmpInst::FCMP_FALSE:
2223 fastEmitBranch(FBB, DbgLoc);
2225 case CmpInst::FCMP_TRUE:
2226 fastEmitBranch(TBB, DbgLoc);
2230 // Try to emit a combined compare-and-branch first.
2231 if (emitCompareAndBranch(BI))
2234 // Try to take advantage of fallthrough opportunities.
2235 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2236 std::swap(TBB, FBB);
2237 Predicate = CmpInst::getInversePredicate(Predicate);
2241 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2244 // FCMP_UEQ and FCMP_ONE cannot be checked with a single branch
2246 CC = getCompareCC(Predicate);
2247 AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2248 switch (Predicate) {
2251 case CmpInst::FCMP_UEQ:
2252 ExtraCC = AArch64CC::EQ;
2255 case CmpInst::FCMP_ONE:
2256 ExtraCC = AArch64CC::MI;
2260 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2262 // Emit the extra branch for FCMP_UEQ and FCMP_ONE.
2263 if (ExtraCC != AArch64CC::AL) {
2264 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2270 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2274 // Obtain the branch weight and add the TrueBB to the successor list.
2275 uint32_t BranchWeight = 0;
2277 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2278 TBB->getBasicBlock());
2279 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2281 fastEmitBranch(FBB, DbgLoc);
2284 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
2286 if (TI->hasOneUse() && isValueAvailable(TI) &&
2287 isTypeSupported(TI->getOperand(0)->getType(), SrcVT)) {
2288 unsigned CondReg = getRegForValue(TI->getOperand(0));
2291 bool CondIsKill = hasTrivialKill(TI->getOperand(0));
2293 // Issue an extract_subreg to get the lower 32-bits.
2294 if (SrcVT == MVT::i64) {
2295 CondReg = fastEmitInst_extractsubreg(MVT::i32, CondReg, CondIsKill,
2300 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
2301 assert(ANDReg && "Unexpected AND instruction emission failure.");
2302 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2304 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2305 std::swap(TBB, FBB);
2308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2312 // Obtain the branch weight and add the TrueBB to the successor list.
2313 uint32_t BranchWeight = 0;
2315 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2316 TBB->getBasicBlock());
2317 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2319 fastEmitBranch(FBB, DbgLoc);
2322 } else if (const auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
2323 uint64_t Imm = CI->getZExtValue();
2324 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
2325 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
2328 // Obtain the branch weight and add the target to the successor list.
2329 uint32_t BranchWeight = 0;
2331 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2332 Target->getBasicBlock());
2333 FuncInfo.MBB->addSuccessor(Target, BranchWeight);
2335 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
2336 // Fake request the condition, otherwise the intrinsic might be completely
2338 unsigned CondReg = getRegForValue(BI->getCondition());
2343 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2347 // Obtain the branch weight and add the TrueBB to the successor list.
2348 uint32_t BranchWeight = 0;
2350 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2351 TBB->getBasicBlock());
2352 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2354 fastEmitBranch(FBB, DbgLoc);
2358 unsigned CondReg = getRegForValue(BI->getCondition());
2361 bool CondRegIsKill = hasTrivialKill(BI->getCondition());
2363 // We've been divorced from our compare! Our block was split, and
2364 // now our compare lives in a predecessor block. We musn't
2365 // re-compare here, as the children of the compare aren't guaranteed
2366 // live across the block boundary (we *could* check for this).
2367 // Regardless, the compare has been done in the predecessor block,
2368 // and it left a value for us in a virtual register. Ergo, we test
2369 // the one-bit value left in the virtual register.
2370 emitICmp_ri(MVT::i32, CondReg, CondRegIsKill, 0);
2372 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2373 std::swap(TBB, FBB);
2377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2381 // Obtain the branch weight and add the TrueBB to the successor list.
2382 uint32_t BranchWeight = 0;
2384 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2385 TBB->getBasicBlock());
2386 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2388 fastEmitBranch(FBB, DbgLoc);
2392 bool AArch64FastISel::selectIndirectBr(const Instruction *I) {
2393 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
2394 unsigned AddrReg = getRegForValue(BI->getOperand(0));
2398 // Emit the indirect branch.
2399 const MCInstrDesc &II = TII.get(AArch64::BR);
2400 AddrReg = constrainOperandRegClass(II, AddrReg, II.getNumDefs());
2401 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(AddrReg);
2403 // Make sure the CFG is up-to-date.
2404 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
2405 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
2410 bool AArch64FastISel::selectCmp(const Instruction *I) {
2411 const CmpInst *CI = cast<CmpInst>(I);
2413 // Try to optimize or fold the cmp.
2414 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2415 unsigned ResultReg = 0;
2416 switch (Predicate) {
2419 case CmpInst::FCMP_FALSE:
2420 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2421 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2422 TII.get(TargetOpcode::COPY), ResultReg)
2423 .addReg(AArch64::WZR, getKillRegState(true));
2425 case CmpInst::FCMP_TRUE:
2426 ResultReg = fastEmit_i(MVT::i32, MVT::i32, ISD::Constant, 1);
2431 updateValueMap(I, ResultReg);
2436 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2439 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2441 // FCMP_UEQ and FCMP_ONE cannot be checked with a single instruction. These
2442 // condition codes are inverted, because they are used by CSINC.
2443 static unsigned CondCodeTable[2][2] = {
2444 { AArch64CC::NE, AArch64CC::VC },
2445 { AArch64CC::PL, AArch64CC::LE }
2447 unsigned *CondCodes = nullptr;
2448 switch (Predicate) {
2451 case CmpInst::FCMP_UEQ:
2452 CondCodes = &CondCodeTable[0][0];
2454 case CmpInst::FCMP_ONE:
2455 CondCodes = &CondCodeTable[1][0];
2460 unsigned TmpReg1 = createResultReg(&AArch64::GPR32RegClass);
2461 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2463 .addReg(AArch64::WZR, getKillRegState(true))
2464 .addReg(AArch64::WZR, getKillRegState(true))
2465 .addImm(CondCodes[0]);
2466 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2468 .addReg(TmpReg1, getKillRegState(true))
2469 .addReg(AArch64::WZR, getKillRegState(true))
2470 .addImm(CondCodes[1]);
2472 updateValueMap(I, ResultReg);
2476 // Now set a register based on the comparison.
2477 AArch64CC::CondCode CC = getCompareCC(Predicate);
2478 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2479 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
2480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2482 .addReg(AArch64::WZR, getKillRegState(true))
2483 .addReg(AArch64::WZR, getKillRegState(true))
2484 .addImm(invertedCC);
2486 updateValueMap(I, ResultReg);
2490 bool AArch64FastISel::selectSelect(const Instruction *I) {
2491 const SelectInst *SI = cast<SelectInst>(I);
2493 EVT DestEVT = TLI.getValueType(SI->getType(), true);
2494 if (!DestEVT.isSimple())
2497 MVT DestVT = DestEVT.getSimpleVT();
2498 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
2503 const TargetRegisterClass *RC = nullptr;
2504 switch (DestVT.SimpleTy) {
2505 default: return false;
2507 SelectOpc = AArch64::CSELWr; RC = &AArch64::GPR32RegClass; break;
2509 SelectOpc = AArch64::CSELXr; RC = &AArch64::GPR64RegClass; break;
2511 SelectOpc = AArch64::FCSELSrrr; RC = &AArch64::FPR32RegClass; break;
2513 SelectOpc = AArch64::FCSELDrrr; RC = &AArch64::FPR64RegClass; break;
2516 const Value *Cond = SI->getCondition();
2517 bool NeedTest = true;
2518 AArch64CC::CondCode CC = AArch64CC::NE;
2519 if (foldXALUIntrinsic(CC, I, Cond))
2522 unsigned CondReg = getRegForValue(Cond);
2525 bool CondIsKill = hasTrivialKill(Cond);
2528 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
2529 assert(ANDReg && "Unexpected AND instruction emission failure.");
2530 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2533 unsigned TrueReg = getRegForValue(SI->getTrueValue());
2534 bool TrueIsKill = hasTrivialKill(SI->getTrueValue());
2536 unsigned FalseReg = getRegForValue(SI->getFalseValue());
2537 bool FalseIsKill = hasTrivialKill(SI->getFalseValue());
2539 if (!TrueReg || !FalseReg)
2542 unsigned ResultReg = fastEmitInst_rri(SelectOpc, RC, TrueReg, TrueIsKill,
2543 FalseReg, FalseIsKill, CC);
2544 updateValueMap(I, ResultReg);
2548 bool AArch64FastISel::selectFPExt(const Instruction *I) {
2549 Value *V = I->getOperand(0);
2550 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
2553 unsigned Op = getRegForValue(V);
2557 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
2558 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
2559 ResultReg).addReg(Op);
2560 updateValueMap(I, ResultReg);
2564 bool AArch64FastISel::selectFPTrunc(const Instruction *I) {
2565 Value *V = I->getOperand(0);
2566 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
2569 unsigned Op = getRegForValue(V);
2573 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
2574 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
2575 ResultReg).addReg(Op);
2576 updateValueMap(I, ResultReg);
2580 // FPToUI and FPToSI
2581 bool AArch64FastISel::selectFPToInt(const Instruction *I, bool Signed) {
2583 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2586 unsigned SrcReg = getRegForValue(I->getOperand(0));
2590 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2591 if (SrcVT == MVT::f128)
2595 if (SrcVT == MVT::f64) {
2597 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
2599 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
2602 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
2604 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
2606 unsigned ResultReg = createResultReg(
2607 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
2608 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2610 updateValueMap(I, ResultReg);
2614 bool AArch64FastISel::selectIntToFP(const Instruction *I, bool Signed) {
2616 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2618 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
2619 "Unexpected value type.");
2621 unsigned SrcReg = getRegForValue(I->getOperand(0));
2624 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
2626 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2628 // Handle sign-extension.
2629 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
2631 emitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
2638 if (SrcVT == MVT::i64) {
2640 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
2642 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
2645 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
2647 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
2650 unsigned ResultReg = fastEmitInst_r(Opc, TLI.getRegClassFor(DestVT), SrcReg,
2652 updateValueMap(I, ResultReg);
2656 bool AArch64FastISel::fastLowerArguments() {
2657 if (!FuncInfo.CanLowerReturn)
2660 const Function *F = FuncInfo.Fn;
2664 CallingConv::ID CC = F->getCallingConv();
2665 if (CC != CallingConv::C)
2668 // Only handle simple cases of up to 8 GPR and FPR each.
2669 unsigned GPRCnt = 0;
2670 unsigned FPRCnt = 0;
2672 for (auto const &Arg : F->args()) {
2673 // The first argument is at index 1.
2675 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2676 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2677 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2678 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2681 Type *ArgTy = Arg.getType();
2682 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
2685 EVT ArgVT = TLI.getValueType(ArgTy);
2686 if (!ArgVT.isSimple())
2689 MVT VT = ArgVT.getSimpleVT().SimpleTy;
2690 if (VT.isFloatingPoint() && !Subtarget->hasFPARMv8())
2693 if (VT.isVector() &&
2694 (!Subtarget->hasNEON() || !Subtarget->isLittleEndian()))
2697 if (VT >= MVT::i1 && VT <= MVT::i64)
2699 else if ((VT >= MVT::f16 && VT <= MVT::f64) || VT.is64BitVector() ||
2700 VT.is128BitVector())
2705 if (GPRCnt > 8 || FPRCnt > 8)
2709 static const MCPhysReg Registers[6][8] = {
2710 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
2711 AArch64::W5, AArch64::W6, AArch64::W7 },
2712 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
2713 AArch64::X5, AArch64::X6, AArch64::X7 },
2714 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
2715 AArch64::H5, AArch64::H6, AArch64::H7 },
2716 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
2717 AArch64::S5, AArch64::S6, AArch64::S7 },
2718 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
2719 AArch64::D5, AArch64::D6, AArch64::D7 },
2720 { AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, AArch64::Q4,
2721 AArch64::Q5, AArch64::Q6, AArch64::Q7 }
2724 unsigned GPRIdx = 0;
2725 unsigned FPRIdx = 0;
2726 for (auto const &Arg : F->args()) {
2727 MVT VT = TLI.getSimpleValueType(Arg.getType());
2729 const TargetRegisterClass *RC;
2730 if (VT >= MVT::i1 && VT <= MVT::i32) {
2731 SrcReg = Registers[0][GPRIdx++];
2732 RC = &AArch64::GPR32RegClass;
2734 } else if (VT == MVT::i64) {
2735 SrcReg = Registers[1][GPRIdx++];
2736 RC = &AArch64::GPR64RegClass;
2737 } else if (VT == MVT::f16) {
2738 SrcReg = Registers[2][FPRIdx++];
2739 RC = &AArch64::FPR16RegClass;
2740 } else if (VT == MVT::f32) {
2741 SrcReg = Registers[3][FPRIdx++];
2742 RC = &AArch64::FPR32RegClass;
2743 } else if ((VT == MVT::f64) || VT.is64BitVector()) {
2744 SrcReg = Registers[4][FPRIdx++];
2745 RC = &AArch64::FPR64RegClass;
2746 } else if (VT.is128BitVector()) {
2747 SrcReg = Registers[5][FPRIdx++];
2748 RC = &AArch64::FPR128RegClass;
2750 llvm_unreachable("Unexpected value type.");
2752 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2753 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2754 // Without this, EmitLiveInCopies may eliminate the livein if its only
2755 // use is a bitcast (which isn't turned into an instruction).
2756 unsigned ResultReg = createResultReg(RC);
2757 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2758 TII.get(TargetOpcode::COPY), ResultReg)
2759 .addReg(DstReg, getKillRegState(true));
2760 updateValueMap(&Arg, ResultReg);
2765 bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
2766 SmallVectorImpl<MVT> &OutVTs,
2767 unsigned &NumBytes) {
2768 CallingConv::ID CC = CLI.CallConv;
2769 SmallVector<CCValAssign, 16> ArgLocs;
2770 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
2771 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
2773 // Get a count of how many bytes are to be pushed on the stack.
2774 NumBytes = CCInfo.getNextStackOffset();
2776 // Issue CALLSEQ_START
2777 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
2778 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
2781 // Process the args.
2782 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2783 CCValAssign &VA = ArgLocs[i];
2784 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
2785 MVT ArgVT = OutVTs[VA.getValNo()];
2787 unsigned ArgReg = getRegForValue(ArgVal);
2791 // Handle arg promotion: SExt, ZExt, AExt.
2792 switch (VA.getLocInfo()) {
2793 case CCValAssign::Full:
2795 case CCValAssign::SExt: {
2796 MVT DestVT = VA.getLocVT();
2798 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
2803 case CCValAssign::AExt:
2804 // Intentional fall-through.
2805 case CCValAssign::ZExt: {
2806 MVT DestVT = VA.getLocVT();
2808 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
2814 llvm_unreachable("Unknown arg promotion!");
2817 // Now copy/store arg to correct locations.
2818 if (VA.isRegLoc() && !VA.needsCustom()) {
2819 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2820 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
2821 CLI.OutRegs.push_back(VA.getLocReg());
2822 } else if (VA.needsCustom()) {
2823 // FIXME: Handle custom args.
2826 assert(VA.isMemLoc() && "Assuming store on stack.");
2828 // Don't emit stores for undef values.
2829 if (isa<UndefValue>(ArgVal))
2832 // Need to store on the stack.
2833 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
2835 unsigned BEAlign = 0;
2836 if (ArgSize < 8 && !Subtarget->isLittleEndian())
2837 BEAlign = 8 - ArgSize;
2840 Addr.setKind(Address::RegBase);
2841 Addr.setReg(AArch64::SP);
2842 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
2844 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
2845 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2846 MachinePointerInfo::getStack(Addr.getOffset()),
2847 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
2849 if (!emitStore(ArgVT, ArgReg, Addr, MMO))
2856 bool AArch64FastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
2857 unsigned NumBytes) {
2858 CallingConv::ID CC = CLI.CallConv;
2860 // Issue CALLSEQ_END
2861 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2862 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
2863 .addImm(NumBytes).addImm(0);
2865 // Now the return value.
2866 if (RetVT != MVT::isVoid) {
2867 SmallVector<CCValAssign, 16> RVLocs;
2868 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
2869 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
2871 // Only handle a single return value.
2872 if (RVLocs.size() != 1)
2875 // Copy all of the result registers out of their specified physreg.
2876 MVT CopyVT = RVLocs[0].getValVT();
2877 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
2878 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2879 TII.get(TargetOpcode::COPY), ResultReg)
2880 .addReg(RVLocs[0].getLocReg());
2881 CLI.InRegs.push_back(RVLocs[0].getLocReg());
2883 CLI.ResultReg = ResultReg;
2884 CLI.NumResultRegs = 1;
2890 bool AArch64FastISel::fastLowerCall(CallLoweringInfo &CLI) {
2891 CallingConv::ID CC = CLI.CallConv;
2892 bool IsTailCall = CLI.IsTailCall;
2893 bool IsVarArg = CLI.IsVarArg;
2894 const Value *Callee = CLI.Callee;
2895 const char *SymName = CLI.SymName;
2897 if (!Callee && !SymName)
2900 // Allow SelectionDAG isel to handle tail calls.
2904 CodeModel::Model CM = TM.getCodeModel();
2905 // Only support the small and large code model.
2906 if (CM != CodeModel::Small && CM != CodeModel::Large)
2909 // FIXME: Add large code model support for ELF.
2910 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
2913 // Let SDISel handle vararg functions.
2917 // FIXME: Only handle *simple* calls for now.
2919 if (CLI.RetTy->isVoidTy())
2920 RetVT = MVT::isVoid;
2921 else if (!isTypeLegal(CLI.RetTy, RetVT))
2924 for (auto Flag : CLI.OutFlags)
2925 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
2928 // Set up the argument vectors.
2929 SmallVector<MVT, 16> OutVTs;
2930 OutVTs.reserve(CLI.OutVals.size());
2932 for (auto *Val : CLI.OutVals) {
2934 if (!isTypeLegal(Val->getType(), VT) &&
2935 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
2938 // We don't handle vector parameters yet.
2939 if (VT.isVector() || VT.getSizeInBits() > 64)
2942 OutVTs.push_back(VT);
2946 if (Callee && !computeCallAddress(Callee, Addr))
2949 // Handle the arguments now that we've gotten them.
2951 if (!processCallArgs(CLI, OutVTs, NumBytes))
2955 MachineInstrBuilder MIB;
2956 if (CM == CodeModel::Small) {
2957 const MCInstrDesc &II = TII.get(Addr.getReg() ? AArch64::BLR : AArch64::BL);
2958 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II);
2960 MIB.addExternalSymbol(SymName, 0);
2961 else if (Addr.getGlobalValue())
2962 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
2963 else if (Addr.getReg()) {
2964 unsigned Reg = constrainOperandRegClass(II, Addr.getReg(), 0);
2969 unsigned CallReg = 0;
2971 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
2972 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
2974 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGE);
2976 CallReg = createResultReg(&AArch64::GPR64RegClass);
2977 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
2980 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
2982 } else if (Addr.getGlobalValue())
2983 CallReg = materializeGV(Addr.getGlobalValue());
2984 else if (Addr.getReg())
2985 CallReg = Addr.getReg();
2990 const MCInstrDesc &II = TII.get(AArch64::BLR);
2991 CallReg = constrainOperandRegClass(II, CallReg, 0);
2992 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(CallReg);
2995 // Add implicit physical register uses to the call.
2996 for (auto Reg : CLI.OutRegs)
2997 MIB.addReg(Reg, RegState::Implicit);
2999 // Add a register mask with the call-preserved registers.
3000 // Proper defs for return values will be added by setPhysRegsDeadExcept().
3001 MIB.addRegMask(TRI.getCallPreservedMask(CC));
3005 // Finish off the call including any return values.
3006 return finishCall(CLI, RetVT, NumBytes);
3009 bool AArch64FastISel::isMemCpySmall(uint64_t Len, unsigned Alignment) {
3011 return Len / Alignment <= 4;
3016 bool AArch64FastISel::tryEmitSmallMemCpy(Address Dest, Address Src,
3017 uint64_t Len, unsigned Alignment) {
3018 // Make sure we don't bloat code by inlining very large memcpy's.
3019 if (!isMemCpySmall(Len, Alignment))
3022 int64_t UnscaledOffset = 0;
3023 Address OrigDest = Dest;
3024 Address OrigSrc = Src;
3028 if (!Alignment || Alignment >= 8) {
3039 // Bound based on alignment.
3040 if (Len >= 4 && Alignment == 4)
3042 else if (Len >= 2 && Alignment == 2)
3049 unsigned ResultReg = emitLoad(VT, VT, Src);
3053 if (!emitStore(VT, ResultReg, Dest))
3056 int64_t Size = VT.getSizeInBits() / 8;
3058 UnscaledOffset += Size;
3060 // We need to recompute the unscaled offset for each iteration.
3061 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
3062 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
3068 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
3069 /// into the user. The condition code will only be updated on success.
3070 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
3071 const Instruction *I,
3072 const Value *Cond) {
3073 if (!isa<ExtractValueInst>(Cond))
3076 const auto *EV = cast<ExtractValueInst>(Cond);
3077 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
3080 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
3082 const Function *Callee = II->getCalledFunction();
3084 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
3085 if (!isTypeLegal(RetTy, RetVT))
3088 if (RetVT != MVT::i32 && RetVT != MVT::i64)
3091 const Value *LHS = II->getArgOperand(0);
3092 const Value *RHS = II->getArgOperand(1);
3094 // Canonicalize immediate to the RHS.
3095 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3096 isCommutativeIntrinsic(II))
3097 std::swap(LHS, RHS);
3099 // Simplify multiplies.
3100 unsigned IID = II->getIntrinsicID();
3104 case Intrinsic::smul_with_overflow:
3105 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3106 if (C->getValue() == 2)
3107 IID = Intrinsic::sadd_with_overflow;
3109 case Intrinsic::umul_with_overflow:
3110 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3111 if (C->getValue() == 2)
3112 IID = Intrinsic::uadd_with_overflow;
3116 AArch64CC::CondCode TmpCC;
3120 case Intrinsic::sadd_with_overflow:
3121 case Intrinsic::ssub_with_overflow:
3122 TmpCC = AArch64CC::VS;
3124 case Intrinsic::uadd_with_overflow:
3125 TmpCC = AArch64CC::HS;
3127 case Intrinsic::usub_with_overflow:
3128 TmpCC = AArch64CC::LO;
3130 case Intrinsic::smul_with_overflow:
3131 case Intrinsic::umul_with_overflow:
3132 TmpCC = AArch64CC::NE;
3136 // Check if both instructions are in the same basic block.
3137 if (!isValueAvailable(II))
3140 // Make sure nothing is in the way
3141 BasicBlock::const_iterator Start = I;
3142 BasicBlock::const_iterator End = II;
3143 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
3144 // We only expect extractvalue instructions between the intrinsic and the
3145 // instruction to be selected.
3146 if (!isa<ExtractValueInst>(Itr))
3149 // Check that the extractvalue operand comes from the intrinsic.
3150 const auto *EVI = cast<ExtractValueInst>(Itr);
3151 if (EVI->getAggregateOperand() != II)
3159 bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
3160 // FIXME: Handle more intrinsics.
3161 switch (II->getIntrinsicID()) {
3162 default: return false;
3163 case Intrinsic::frameaddress: {
3164 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
3165 MFI->setFrameAddressIsTaken(true);
3167 const AArch64RegisterInfo *RegInfo =
3168 static_cast<const AArch64RegisterInfo *>(
3169 TM.getSubtargetImpl()->getRegisterInfo());
3170 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
3171 unsigned SrcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3173 TII.get(TargetOpcode::COPY), SrcReg).addReg(FramePtr);
3174 // Recursively load frame address
3180 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
3182 DestReg = fastEmitInst_ri(AArch64::LDRXui, &AArch64::GPR64RegClass,
3183 SrcReg, /*IsKill=*/true, 0);
3184 assert(DestReg && "Unexpected LDR instruction emission failure.");
3188 updateValueMap(II, SrcReg);
3191 case Intrinsic::memcpy:
3192 case Intrinsic::memmove: {
3193 const auto *MTI = cast<MemTransferInst>(II);
3194 // Don't handle volatile.
3195 if (MTI->isVolatile())
3198 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
3199 // we would emit dead code because we don't currently handle memmoves.
3200 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
3201 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
3202 // Small memcpy's are common enough that we want to do them without a call
3204 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
3205 unsigned Alignment = MTI->getAlignment();
3206 if (isMemCpySmall(Len, Alignment)) {
3208 if (!computeAddress(MTI->getRawDest(), Dest) ||
3209 !computeAddress(MTI->getRawSource(), Src))
3211 if (tryEmitSmallMemCpy(Dest, Src, Len, Alignment))
3216 if (!MTI->getLength()->getType()->isIntegerTy(64))
3219 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
3220 // Fast instruction selection doesn't support the special
3224 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
3225 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
3227 case Intrinsic::memset: {
3228 const MemSetInst *MSI = cast<MemSetInst>(II);
3229 // Don't handle volatile.
3230 if (MSI->isVolatile())
3233 if (!MSI->getLength()->getType()->isIntegerTy(64))
3236 if (MSI->getDestAddressSpace() > 255)
3237 // Fast instruction selection doesn't support the special
3241 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
3243 case Intrinsic::sin:
3244 case Intrinsic::cos:
3245 case Intrinsic::pow: {
3247 if (!isTypeLegal(II->getType(), RetVT))
3250 if (RetVT != MVT::f32 && RetVT != MVT::f64)
3253 static const RTLIB::Libcall LibCallTable[3][2] = {
3254 { RTLIB::SIN_F32, RTLIB::SIN_F64 },
3255 { RTLIB::COS_F32, RTLIB::COS_F64 },
3256 { RTLIB::POW_F32, RTLIB::POW_F64 }
3259 bool Is64Bit = RetVT == MVT::f64;
3260 switch (II->getIntrinsicID()) {
3262 llvm_unreachable("Unexpected intrinsic.");
3263 case Intrinsic::sin:
3264 LC = LibCallTable[0][Is64Bit];
3266 case Intrinsic::cos:
3267 LC = LibCallTable[1][Is64Bit];
3269 case Intrinsic::pow:
3270 LC = LibCallTable[2][Is64Bit];
3275 Args.reserve(II->getNumArgOperands());
3277 // Populate the argument list.
3278 for (auto &Arg : II->arg_operands()) {
3281 Entry.Ty = Arg->getType();
3282 Args.push_back(Entry);
3285 CallLoweringInfo CLI;
3286 CLI.setCallee(TLI.getLibcallCallingConv(LC), II->getType(),
3287 TLI.getLibcallName(LC), std::move(Args));
3288 if (!lowerCallTo(CLI))
3290 updateValueMap(II, CLI.ResultReg);
3293 case Intrinsic::trap: {
3294 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
3298 case Intrinsic::sqrt: {
3299 Type *RetTy = II->getCalledFunction()->getReturnType();
3302 if (!isTypeLegal(RetTy, VT))
3305 unsigned Op0Reg = getRegForValue(II->getOperand(0));
3308 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
3310 unsigned ResultReg = fastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
3314 updateValueMap(II, ResultReg);
3317 case Intrinsic::sadd_with_overflow:
3318 case Intrinsic::uadd_with_overflow:
3319 case Intrinsic::ssub_with_overflow:
3320 case Intrinsic::usub_with_overflow:
3321 case Intrinsic::smul_with_overflow:
3322 case Intrinsic::umul_with_overflow: {
3323 // This implements the basic lowering of the xalu with overflow intrinsics.
3324 const Function *Callee = II->getCalledFunction();
3325 auto *Ty = cast<StructType>(Callee->getReturnType());
3326 Type *RetTy = Ty->getTypeAtIndex(0U);
3329 if (!isTypeLegal(RetTy, VT))
3332 if (VT != MVT::i32 && VT != MVT::i64)
3335 const Value *LHS = II->getArgOperand(0);
3336 const Value *RHS = II->getArgOperand(1);
3337 // Canonicalize immediate to the RHS.
3338 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3339 isCommutativeIntrinsic(II))
3340 std::swap(LHS, RHS);
3342 // Simplify multiplies.
3343 unsigned IID = II->getIntrinsicID();
3347 case Intrinsic::smul_with_overflow:
3348 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3349 if (C->getValue() == 2) {
3350 IID = Intrinsic::sadd_with_overflow;
3354 case Intrinsic::umul_with_overflow:
3355 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3356 if (C->getValue() == 2) {
3357 IID = Intrinsic::uadd_with_overflow;
3363 unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
3364 AArch64CC::CondCode CC = AArch64CC::Invalid;
3366 default: llvm_unreachable("Unexpected intrinsic!");
3367 case Intrinsic::sadd_with_overflow:
3368 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3371 case Intrinsic::uadd_with_overflow:
3372 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3375 case Intrinsic::ssub_with_overflow:
3376 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3379 case Intrinsic::usub_with_overflow:
3380 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3383 case Intrinsic::smul_with_overflow: {
3385 unsigned LHSReg = getRegForValue(LHS);
3388 bool LHSIsKill = hasTrivialKill(LHS);
3390 unsigned RHSReg = getRegForValue(RHS);
3393 bool RHSIsKill = hasTrivialKill(RHS);
3395 if (VT == MVT::i32) {
3396 MulReg = emitSMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3397 unsigned ShiftReg = emitLSR_ri(MVT::i64, MVT::i64, MulReg,
3398 /*IsKill=*/false, 32);
3399 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3401 ShiftReg = fastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
3403 emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3404 AArch64_AM::ASR, 31, /*WantResult=*/false);
3406 assert(VT == MVT::i64 && "Unexpected value type.");
3407 MulReg = emitMul_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3408 unsigned SMULHReg = fastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
3410 emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3411 AArch64_AM::ASR, 63, /*WantResult=*/false);
3415 case Intrinsic::umul_with_overflow: {
3417 unsigned LHSReg = getRegForValue(LHS);
3420 bool LHSIsKill = hasTrivialKill(LHS);
3422 unsigned RHSReg = getRegForValue(RHS);
3425 bool RHSIsKill = hasTrivialKill(RHS);
3427 if (VT == MVT::i32) {
3428 MulReg = emitUMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3429 emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
3430 /*IsKill=*/false, AArch64_AM::LSR, 32,
3431 /*WantResult=*/false);
3432 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3435 assert(VT == MVT::i64 && "Unexpected value type.");
3436 MulReg = emitMul_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3437 unsigned UMULHReg = fastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
3439 emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
3440 /*IsKill=*/false, /*WantResult=*/false);
3447 ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
3448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3449 TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
3452 ResultReg2 = fastEmitInst_rri(AArch64::CSINCWr, &AArch64::GPR32RegClass,
3453 AArch64::WZR, /*IsKill=*/true, AArch64::WZR,
3454 /*IsKill=*/true, getInvertedCondCode(CC));
3456 assert((ResultReg1 + 1) == ResultReg2 &&
3457 "Nonconsecutive result registers.");
3458 updateValueMap(II, ResultReg1, 2);
3465 bool AArch64FastISel::selectRet(const Instruction *I) {
3466 const ReturnInst *Ret = cast<ReturnInst>(I);
3467 const Function &F = *I->getParent()->getParent();
3469 if (!FuncInfo.CanLowerReturn)
3475 // Build a list of return value registers.
3476 SmallVector<unsigned, 4> RetRegs;
3478 if (Ret->getNumOperands() > 0) {
3479 CallingConv::ID CC = F.getCallingConv();
3480 SmallVector<ISD::OutputArg, 4> Outs;
3481 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
3483 // Analyze operands of the call, assigning locations to each operand.
3484 SmallVector<CCValAssign, 16> ValLocs;
3485 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
3486 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
3487 : RetCC_AArch64_AAPCS;
3488 CCInfo.AnalyzeReturn(Outs, RetCC);
3490 // Only handle a single return value for now.
3491 if (ValLocs.size() != 1)
3494 CCValAssign &VA = ValLocs[0];
3495 const Value *RV = Ret->getOperand(0);
3497 // Don't bother handling odd stuff for now.
3498 if ((VA.getLocInfo() != CCValAssign::Full) &&
3499 (VA.getLocInfo() != CCValAssign::BCvt))
3502 // Only handle register returns for now.
3506 unsigned Reg = getRegForValue(RV);
3510 unsigned SrcReg = Reg + VA.getValNo();
3511 unsigned DestReg = VA.getLocReg();
3512 // Avoid a cross-class copy. This is very unlikely.
3513 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
3516 EVT RVEVT = TLI.getValueType(RV->getType());
3517 if (!RVEVT.isSimple())
3520 // Vectors (of > 1 lane) in big endian need tricky handling.
3521 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1 &&
3522 !Subtarget->isLittleEndian())
3525 MVT RVVT = RVEVT.getSimpleVT();
3526 if (RVVT == MVT::f128)
3529 MVT DestVT = VA.getValVT();
3530 // Special handling for extended integers.
3531 if (RVVT != DestVT) {
3532 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
3535 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
3538 bool IsZExt = Outs[0].Flags.isZExt();
3539 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
3545 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3546 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
3548 // Add register to return instruction.
3549 RetRegs.push_back(VA.getLocReg());
3552 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3553 TII.get(AArch64::RET_ReallyLR));
3554 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
3555 MIB.addReg(RetRegs[i], RegState::Implicit);
3559 bool AArch64FastISel::selectTrunc(const Instruction *I) {
3560 Type *DestTy = I->getType();
3561 Value *Op = I->getOperand(0);
3562 Type *SrcTy = Op->getType();
3564 EVT SrcEVT = TLI.getValueType(SrcTy, true);
3565 EVT DestEVT = TLI.getValueType(DestTy, true);
3566 if (!SrcEVT.isSimple())
3568 if (!DestEVT.isSimple())
3571 MVT SrcVT = SrcEVT.getSimpleVT();
3572 MVT DestVT = DestEVT.getSimpleVT();
3574 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
3577 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
3581 unsigned SrcReg = getRegForValue(Op);
3584 bool SrcIsKill = hasTrivialKill(Op);
3586 // If we're truncating from i64 to a smaller non-legal type then generate an
3587 // AND. Otherwise, we know the high bits are undefined and a truncate only
3588 // generate a COPY. We cannot mark the source register also as result
3589 // register, because this can incorrectly transfer the kill flag onto the
3592 if (SrcVT == MVT::i64) {
3594 switch (DestVT.SimpleTy) {
3596 // Trunc i64 to i32 is handled by the target-independent fast-isel.
3608 // Issue an extract_subreg to get the lower 32-bits.
3609 unsigned Reg32 = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
3611 // Create the AND instruction which performs the actual truncation.
3612 ResultReg = emitAnd_ri(MVT::i32, Reg32, /*IsKill=*/true, Mask);
3613 assert(ResultReg && "Unexpected AND instruction emission failure.");
3615 ResultReg = createResultReg(&AArch64::GPR32RegClass);
3616 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3617 TII.get(TargetOpcode::COPY), ResultReg)
3618 .addReg(SrcReg, getKillRegState(SrcIsKill));
3621 updateValueMap(I, ResultReg);
3625 unsigned AArch64FastISel::emiti1Ext(unsigned SrcReg, MVT DestVT, bool IsZExt) {
3626 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
3627 DestVT == MVT::i64) &&
3628 "Unexpected value type.");
3629 // Handle i8 and i16 as i32.
3630 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3634 unsigned ResultReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
3635 assert(ResultReg && "Unexpected AND instruction emission failure.");
3636 if (DestVT == MVT::i64) {
3637 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
3638 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
3639 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3640 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3641 TII.get(AArch64::SUBREG_TO_REG), Reg64)
3644 .addImm(AArch64::sub_32);
3649 if (DestVT == MVT::i64) {
3650 // FIXME: We're SExt i1 to i64.
3653 return fastEmitInst_rii(AArch64::SBFMWri, &AArch64::GPR32RegClass, SrcReg,
3654 /*TODO:IsKill=*/false, 0, 0);
3658 unsigned AArch64FastISel::emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3659 unsigned Op1, bool Op1IsKill) {
3661 switch (RetVT.SimpleTy) {
3667 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
3669 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
3672 const TargetRegisterClass *RC =
3673 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3674 return fastEmitInst_rrr(Opc, RC, Op0, Op0IsKill, Op1, Op1IsKill,
3675 /*IsKill=*/ZReg, true);
3678 unsigned AArch64FastISel::emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3679 unsigned Op1, bool Op1IsKill) {
3680 if (RetVT != MVT::i64)
3683 return fastEmitInst_rrr(AArch64::SMADDLrrr, &AArch64::GPR64RegClass,
3684 Op0, Op0IsKill, Op1, Op1IsKill,
3685 AArch64::XZR, /*IsKill=*/true);
3688 unsigned AArch64FastISel::emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3689 unsigned Op1, bool Op1IsKill) {
3690 if (RetVT != MVT::i64)
3693 return fastEmitInst_rrr(AArch64::UMADDLrrr, &AArch64::GPR64RegClass,
3694 Op0, Op0IsKill, Op1, Op1IsKill,
3695 AArch64::XZR, /*IsKill=*/true);
3698 unsigned AArch64FastISel::emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3699 unsigned Op1Reg, bool Op1IsKill) {
3701 bool NeedTrunc = false;
3703 switch (RetVT.SimpleTy) {
3705 case MVT::i8: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xff; break;
3706 case MVT::i16: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xffff; break;
3707 case MVT::i32: Opc = AArch64::LSLVWr; break;
3708 case MVT::i64: Opc = AArch64::LSLVXr; break;
3711 const TargetRegisterClass *RC =
3712 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3714 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3717 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3720 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3724 unsigned AArch64FastISel::emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3725 bool Op0IsKill, uint64_t Shift,
3727 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3728 "Unexpected source/return type pair.");
3729 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
3730 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
3731 "Unexpected source value type.");
3732 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3733 RetVT == MVT::i64) && "Unexpected return value type.");
3735 bool Is64Bit = (RetVT == MVT::i64);
3736 unsigned RegSize = Is64Bit ? 64 : 32;
3737 unsigned DstBits = RetVT.getSizeInBits();
3738 unsigned SrcBits = SrcVT.getSizeInBits();
3740 // Don't deal with undefined shifts.
3741 if (Shift >= DstBits)
3744 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3745 // {S|U}BFM Wd, Wn, #r, #s
3746 // Wd<32+s-r,32-r> = Wn<s:0> when r > s
3748 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3749 // %2 = shl i16 %1, 4
3750 // Wd<32+7-28,32-28> = Wn<7:0> <- clamp s to 7
3751 // 0b1111_1111_1111_1111__1111_1010_1010_0000 sext
3752 // 0b0000_0000_0000_0000__0000_0101_0101_0000 sext | zext
3753 // 0b0000_0000_0000_0000__0000_1010_1010_0000 zext
3755 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3756 // %2 = shl i16 %1, 8
3757 // Wd<32+7-24,32-24> = Wn<7:0>
3758 // 0b1111_1111_1111_1111__1010_1010_0000_0000 sext
3759 // 0b0000_0000_0000_0000__0101_0101_0000_0000 sext | zext
3760 // 0b0000_0000_0000_0000__1010_1010_0000_0000 zext
3762 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3763 // %2 = shl i16 %1, 12
3764 // Wd<32+3-20,32-20> = Wn<3:0>
3765 // 0b1111_1111_1111_1111__1010_0000_0000_0000 sext
3766 // 0b0000_0000_0000_0000__0101_0000_0000_0000 sext | zext
3767 // 0b0000_0000_0000_0000__1010_0000_0000_0000 zext
3769 unsigned ImmR = RegSize - Shift;
3770 // Limit the width to the length of the source type.
3771 unsigned ImmS = std::min<unsigned>(SrcBits - 1, DstBits - 1 - Shift);
3772 static const unsigned OpcTable[2][2] = {
3773 {AArch64::SBFMWri, AArch64::SBFMXri},
3774 {AArch64::UBFMWri, AArch64::UBFMXri}
3776 unsigned Opc = OpcTable[IsZext][Is64Bit];
3777 const TargetRegisterClass *RC =
3778 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3779 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3780 unsigned TmpReg = MRI.createVirtualRegister(RC);
3781 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3782 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3784 .addReg(Op0, getKillRegState(Op0IsKill))
3785 .addImm(AArch64::sub_32);
3789 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3792 unsigned AArch64FastISel::emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3793 unsigned Op1Reg, bool Op1IsKill) {
3795 bool NeedTrunc = false;
3797 switch (RetVT.SimpleTy) {
3799 case MVT::i8: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xff; break;
3800 case MVT::i16: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xffff; break;
3801 case MVT::i32: Opc = AArch64::LSRVWr; break;
3802 case MVT::i64: Opc = AArch64::LSRVXr; break;
3805 const TargetRegisterClass *RC =
3806 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3808 Op0Reg = emitAnd_ri(MVT::i32, Op0Reg, Op0IsKill, Mask);
3809 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3810 Op0IsKill = Op1IsKill = true;
3812 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3815 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3819 unsigned AArch64FastISel::emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3820 bool Op0IsKill, uint64_t Shift,
3822 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3823 "Unexpected source/return type pair.");
3824 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3825 SrcVT == MVT::i64) && "Unexpected source value type.");
3826 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3827 RetVT == MVT::i64) && "Unexpected return value type.");
3829 bool Is64Bit = (RetVT == MVT::i64);
3830 unsigned RegSize = Is64Bit ? 64 : 32;
3831 unsigned DstBits = RetVT.getSizeInBits();
3832 unsigned SrcBits = SrcVT.getSizeInBits();
3834 // Don't deal with undefined shifts.
3835 if (Shift >= DstBits)
3838 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3839 // {S|U}BFM Wd, Wn, #r, #s
3840 // Wd<s-r:0> = Wn<s:r> when r <= s
3842 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3843 // %2 = lshr i16 %1, 4
3844 // Wd<7-4:0> = Wn<7:4>
3845 // 0b0000_0000_0000_0000__0000_1111_1111_1010 sext
3846 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
3847 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
3849 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3850 // %2 = lshr i16 %1, 8
3851 // Wd<7-7,0> = Wn<7:7>
3852 // 0b0000_0000_0000_0000__0000_0000_1111_1111 sext
3853 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3854 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3856 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3857 // %2 = lshr i16 %1, 12
3858 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
3859 // 0b0000_0000_0000_0000__0000_0000_0000_1111 sext
3860 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3861 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3863 if (Shift >= SrcBits && IsZExt)
3864 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
3866 // It is not possible to fold a sign-extend into the LShr instruction. In this
3867 // case emit a sign-extend.
3869 Op0 = emitIntExt(SrcVT, Op0, RetVT, IsZExt);
3874 SrcBits = SrcVT.getSizeInBits();
3878 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
3879 unsigned ImmS = SrcBits - 1;
3880 static const unsigned OpcTable[2][2] = {
3881 {AArch64::SBFMWri, AArch64::SBFMXri},
3882 {AArch64::UBFMWri, AArch64::UBFMXri}
3884 unsigned Opc = OpcTable[IsZExt][Is64Bit];
3885 const TargetRegisterClass *RC =
3886 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3887 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3888 unsigned TmpReg = MRI.createVirtualRegister(RC);
3889 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3890 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3892 .addReg(Op0, getKillRegState(Op0IsKill))
3893 .addImm(AArch64::sub_32);
3897 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3900 unsigned AArch64FastISel::emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3901 unsigned Op1Reg, bool Op1IsKill) {
3903 bool NeedTrunc = false;
3905 switch (RetVT.SimpleTy) {
3907 case MVT::i8: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xff; break;
3908 case MVT::i16: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xffff; break;
3909 case MVT::i32: Opc = AArch64::ASRVWr; break;
3910 case MVT::i64: Opc = AArch64::ASRVXr; break;
3913 const TargetRegisterClass *RC =
3914 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3916 Op0Reg = emitIntExt(RetVT, Op0Reg, MVT::i32, /*IsZExt=*/false);
3917 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3918 Op0IsKill = Op1IsKill = true;
3920 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3923 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3927 unsigned AArch64FastISel::emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3928 bool Op0IsKill, uint64_t Shift,
3930 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3931 "Unexpected source/return type pair.");
3932 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3933 SrcVT == MVT::i64) && "Unexpected source value type.");
3934 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3935 RetVT == MVT::i64) && "Unexpected return value type.");
3937 bool Is64Bit = (RetVT == MVT::i64);
3938 unsigned RegSize = Is64Bit ? 64 : 32;
3939 unsigned DstBits = RetVT.getSizeInBits();
3940 unsigned SrcBits = SrcVT.getSizeInBits();
3942 // Don't deal with undefined shifts.
3943 if (Shift >= DstBits)
3946 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3947 // {S|U}BFM Wd, Wn, #r, #s
3948 // Wd<s-r:0> = Wn<s:r> when r <= s
3950 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3951 // %2 = ashr i16 %1, 4
3952 // Wd<7-4:0> = Wn<7:4>
3953 // 0b1111_1111_1111_1111__1111_1111_1111_1010 sext
3954 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
3955 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
3957 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3958 // %2 = ashr i16 %1, 8
3959 // Wd<7-7,0> = Wn<7:7>
3960 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
3961 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3962 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3964 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3965 // %2 = ashr i16 %1, 12
3966 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
3967 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
3968 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3969 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3971 if (Shift >= SrcBits && IsZExt)
3972 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
3974 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
3975 unsigned ImmS = SrcBits - 1;
3976 static const unsigned OpcTable[2][2] = {
3977 {AArch64::SBFMWri, AArch64::SBFMXri},
3978 {AArch64::UBFMWri, AArch64::UBFMXri}
3980 unsigned Opc = OpcTable[IsZExt][Is64Bit];
3981 const TargetRegisterClass *RC =
3982 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3983 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3984 unsigned TmpReg = MRI.createVirtualRegister(RC);
3985 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3986 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3988 .addReg(Op0, getKillRegState(Op0IsKill))
3989 .addImm(AArch64::sub_32);
3993 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3996 unsigned AArch64FastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
3998 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
4000 // FastISel does not have plumbing to deal with extensions where the SrcVT or
4001 // DestVT are odd things, so test to make sure that they are both types we can
4002 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
4003 // bail out to SelectionDAG.
4004 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
4005 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
4006 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
4007 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
4013 switch (SrcVT.SimpleTy) {
4017 return emiti1Ext(SrcReg, DestVT, IsZExt);
4019 if (DestVT == MVT::i64)
4020 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4022 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
4026 if (DestVT == MVT::i64)
4027 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4029 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
4033 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
4034 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4039 // Handle i8 and i16 as i32.
4040 if (DestVT == MVT::i8 || DestVT == MVT::i16)
4042 else if (DestVT == MVT::i64) {
4043 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
4044 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4045 TII.get(AArch64::SUBREG_TO_REG), Src64)
4048 .addImm(AArch64::sub_32);
4052 const TargetRegisterClass *RC =
4053 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4054 return fastEmitInst_rii(Opc, RC, SrcReg, /*TODO:IsKill=*/false, 0, Imm);
4057 static bool isZExtLoad(const MachineInstr *LI) {
4058 switch (LI->getOpcode()) {
4061 case AArch64::LDURBBi:
4062 case AArch64::LDURHHi:
4063 case AArch64::LDURWi:
4064 case AArch64::LDRBBui:
4065 case AArch64::LDRHHui:
4066 case AArch64::LDRWui:
4067 case AArch64::LDRBBroX:
4068 case AArch64::LDRHHroX:
4069 case AArch64::LDRWroX:
4070 case AArch64::LDRBBroW:
4071 case AArch64::LDRHHroW:
4072 case AArch64::LDRWroW:
4077 static bool isSExtLoad(const MachineInstr *LI) {
4078 switch (LI->getOpcode()) {
4081 case AArch64::LDURSBWi:
4082 case AArch64::LDURSHWi:
4083 case AArch64::LDURSBXi:
4084 case AArch64::LDURSHXi:
4085 case AArch64::LDURSWi:
4086 case AArch64::LDRSBWui:
4087 case AArch64::LDRSHWui:
4088 case AArch64::LDRSBXui:
4089 case AArch64::LDRSHXui:
4090 case AArch64::LDRSWui:
4091 case AArch64::LDRSBWroX:
4092 case AArch64::LDRSHWroX:
4093 case AArch64::LDRSBXroX:
4094 case AArch64::LDRSHXroX:
4095 case AArch64::LDRSWroX:
4096 case AArch64::LDRSBWroW:
4097 case AArch64::LDRSHWroW:
4098 case AArch64::LDRSBXroW:
4099 case AArch64::LDRSHXroW:
4100 case AArch64::LDRSWroW:
4105 bool AArch64FastISel::optimizeIntExtLoad(const Instruction *I, MVT RetVT,
4107 const auto *LI = dyn_cast<LoadInst>(I->getOperand(0));
4108 if (!LI || !LI->hasOneUse())
4111 // Check if the load instruction has already been selected.
4112 unsigned Reg = lookUpRegForValue(LI);
4116 MachineInstr *MI = MRI.getUniqueVRegDef(Reg);
4120 // Check if the correct load instruction has been emitted - SelectionDAG might
4121 // have emitted a zero-extending load, but we need a sign-extending load.
4122 bool IsZExt = isa<ZExtInst>(I);
4123 const auto *LoadMI = MI;
4124 if (LoadMI->getOpcode() == TargetOpcode::COPY &&
4125 LoadMI->getOperand(1).getSubReg() == AArch64::sub_32) {
4126 unsigned LoadReg = MI->getOperand(1).getReg();
4127 LoadMI = MRI.getUniqueVRegDef(LoadReg);
4128 assert(LoadMI && "Expected valid instruction");
4130 if (!(IsZExt && isZExtLoad(LoadMI)) && !(!IsZExt && isSExtLoad(LoadMI)))
4133 // Nothing to be done.
4134 if (RetVT != MVT::i64 || SrcVT > MVT::i32) {
4135 updateValueMap(I, Reg);
4140 unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
4141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4142 TII.get(AArch64::SUBREG_TO_REG), Reg64)
4144 .addReg(Reg, getKillRegState(true))
4145 .addImm(AArch64::sub_32);
4148 assert((MI->getOpcode() == TargetOpcode::COPY &&
4149 MI->getOperand(1).getSubReg() == AArch64::sub_32) &&
4150 "Expected copy instruction");
4151 Reg = MI->getOperand(1).getReg();
4152 MI->eraseFromParent();
4154 updateValueMap(I, Reg);
4158 bool AArch64FastISel::selectIntExt(const Instruction *I) {
4159 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
4160 "Unexpected integer extend instruction.");
4163 if (!isTypeSupported(I->getType(), RetVT))
4166 if (!isTypeSupported(I->getOperand(0)->getType(), SrcVT))
4169 // Try to optimize already sign-/zero-extended values from load instructions.
4170 if (optimizeIntExtLoad(I, RetVT, SrcVT))
4173 unsigned SrcReg = getRegForValue(I->getOperand(0));
4176 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
4178 // Try to optimize already sign-/zero-extended values from function arguments.
4179 bool IsZExt = isa<ZExtInst>(I);
4180 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0))) {
4181 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr())) {
4182 if (RetVT == MVT::i64 && SrcVT != MVT::i64) {
4183 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
4184 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4185 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
4187 .addReg(SrcReg, getKillRegState(SrcIsKill))
4188 .addImm(AArch64::sub_32);
4191 updateValueMap(I, SrcReg);
4196 unsigned ResultReg = emitIntExt(SrcVT, SrcReg, RetVT, IsZExt);
4200 updateValueMap(I, ResultReg);
4204 bool AArch64FastISel::selectRem(const Instruction *I, unsigned ISDOpcode) {
4205 EVT DestEVT = TLI.getValueType(I->getType(), true);
4206 if (!DestEVT.isSimple())
4209 MVT DestVT = DestEVT.getSimpleVT();
4210 if (DestVT != MVT::i64 && DestVT != MVT::i32)
4214 bool Is64bit = (DestVT == MVT::i64);
4215 switch (ISDOpcode) {
4219 DivOpc = Is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
4222 DivOpc = Is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
4225 unsigned MSubOpc = Is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
4226 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4229 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4231 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4234 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4236 const TargetRegisterClass *RC =
4237 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4238 unsigned QuotReg = fastEmitInst_rr(DivOpc, RC, Src0Reg, /*IsKill=*/false,
4239 Src1Reg, /*IsKill=*/false);
4240 assert(QuotReg && "Unexpected DIV instruction emission failure.");
4241 // The remainder is computed as numerator - (quotient * denominator) using the
4242 // MSUB instruction.
4243 unsigned ResultReg = fastEmitInst_rrr(MSubOpc, RC, QuotReg, /*IsKill=*/true,
4244 Src1Reg, Src1IsKill, Src0Reg,
4246 updateValueMap(I, ResultReg);
4250 bool AArch64FastISel::selectMul(const Instruction *I) {
4252 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
4256 return selectBinaryOp(I, ISD::MUL);
4258 const Value *Src0 = I->getOperand(0);
4259 const Value *Src1 = I->getOperand(1);
4260 if (const auto *C = dyn_cast<ConstantInt>(Src0))
4261 if (C->getValue().isPowerOf2())
4262 std::swap(Src0, Src1);
4264 // Try to simplify to a shift instruction.
4265 if (const auto *C = dyn_cast<ConstantInt>(Src1))
4266 if (C->getValue().isPowerOf2()) {
4267 uint64_t ShiftVal = C->getValue().logBase2();
4270 if (const auto *ZExt = dyn_cast<ZExtInst>(Src0)) {
4271 if (!isIntExtFree(ZExt)) {
4273 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), VT)) {
4276 Src0 = ZExt->getOperand(0);
4279 } else if (const auto *SExt = dyn_cast<SExtInst>(Src0)) {
4280 if (!isIntExtFree(SExt)) {
4282 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), VT)) {
4285 Src0 = SExt->getOperand(0);
4290 unsigned Src0Reg = getRegForValue(Src0);
4293 bool Src0IsKill = hasTrivialKill(Src0);
4295 unsigned ResultReg =
4296 emitLSL_ri(VT, SrcVT, Src0Reg, Src0IsKill, ShiftVal, IsZExt);
4299 updateValueMap(I, ResultReg);
4304 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4307 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4309 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4312 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4314 unsigned ResultReg = emitMul_rr(VT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
4319 updateValueMap(I, ResultReg);
4323 bool AArch64FastISel::selectShift(const Instruction *I) {
4325 if (!isTypeSupported(I->getType(), RetVT, /*IsVectorAllowed=*/true))
4328 if (RetVT.isVector())
4329 return selectOperator(I, I->getOpcode());
4331 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
4332 unsigned ResultReg = 0;
4333 uint64_t ShiftVal = C->getZExtValue();
4335 bool IsZExt = (I->getOpcode() == Instruction::AShr) ? false : true;
4336 const Value *Op0 = I->getOperand(0);
4337 if (const auto *ZExt = dyn_cast<ZExtInst>(Op0)) {
4338 if (!isIntExtFree(ZExt)) {
4340 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), TmpVT)) {
4343 Op0 = ZExt->getOperand(0);
4346 } else if (const auto *SExt = dyn_cast<SExtInst>(Op0)) {
4347 if (!isIntExtFree(SExt)) {
4349 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), TmpVT)) {
4352 Op0 = SExt->getOperand(0);
4357 unsigned Op0Reg = getRegForValue(Op0);
4360 bool Op0IsKill = hasTrivialKill(Op0);
4362 switch (I->getOpcode()) {
4363 default: llvm_unreachable("Unexpected instruction.");
4364 case Instruction::Shl:
4365 ResultReg = emitLSL_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4367 case Instruction::AShr:
4368 ResultReg = emitASR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4370 case Instruction::LShr:
4371 ResultReg = emitLSR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4377 updateValueMap(I, ResultReg);
4381 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4384 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4386 unsigned Op1Reg = getRegForValue(I->getOperand(1));
4389 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
4391 unsigned ResultReg = 0;
4392 switch (I->getOpcode()) {
4393 default: llvm_unreachable("Unexpected instruction.");
4394 case Instruction::Shl:
4395 ResultReg = emitLSL_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4397 case Instruction::AShr:
4398 ResultReg = emitASR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4400 case Instruction::LShr:
4401 ResultReg = emitLSR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4408 updateValueMap(I, ResultReg);
4412 bool AArch64FastISel::selectBitCast(const Instruction *I) {
4415 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
4417 if (!isTypeLegal(I->getType(), RetVT))
4421 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
4422 Opc = AArch64::FMOVWSr;
4423 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
4424 Opc = AArch64::FMOVXDr;
4425 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
4426 Opc = AArch64::FMOVSWr;
4427 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
4428 Opc = AArch64::FMOVDXr;
4432 const TargetRegisterClass *RC = nullptr;
4433 switch (RetVT.SimpleTy) {
4434 default: llvm_unreachable("Unexpected value type.");
4435 case MVT::i32: RC = &AArch64::GPR32RegClass; break;
4436 case MVT::i64: RC = &AArch64::GPR64RegClass; break;
4437 case MVT::f32: RC = &AArch64::FPR32RegClass; break;
4438 case MVT::f64: RC = &AArch64::FPR64RegClass; break;
4440 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4443 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4444 unsigned ResultReg = fastEmitInst_r(Opc, RC, Op0Reg, Op0IsKill);
4449 updateValueMap(I, ResultReg);
4453 bool AArch64FastISel::selectFRem(const Instruction *I) {
4455 if (!isTypeLegal(I->getType(), RetVT))
4459 switch (RetVT.SimpleTy) {
4463 LC = RTLIB::REM_F32;
4466 LC = RTLIB::REM_F64;
4471 Args.reserve(I->getNumOperands());
4473 // Populate the argument list.
4474 for (auto &Arg : I->operands()) {
4477 Entry.Ty = Arg->getType();
4478 Args.push_back(Entry);
4481 CallLoweringInfo CLI;
4482 CLI.setCallee(TLI.getLibcallCallingConv(LC), I->getType(),
4483 TLI.getLibcallName(LC), std::move(Args));
4484 if (!lowerCallTo(CLI))
4486 updateValueMap(I, CLI.ResultReg);
4490 bool AArch64FastISel::selectSDiv(const Instruction *I) {
4492 if (!isTypeLegal(I->getType(), VT))
4495 if (!isa<ConstantInt>(I->getOperand(1)))
4496 return selectBinaryOp(I, ISD::SDIV);
4498 const APInt &C = cast<ConstantInt>(I->getOperand(1))->getValue();
4499 if ((VT != MVT::i32 && VT != MVT::i64) || !C ||
4500 !(C.isPowerOf2() || (-C).isPowerOf2()))
4501 return selectBinaryOp(I, ISD::SDIV);
4503 unsigned Lg2 = C.countTrailingZeros();
4504 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4507 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4509 if (cast<BinaryOperator>(I)->isExact()) {
4510 unsigned ResultReg = emitASR_ri(VT, VT, Src0Reg, Src0IsKill, Lg2);
4513 updateValueMap(I, ResultReg);
4517 unsigned Pow2MinusOne = (1 << Lg2) - 1;
4518 unsigned AddReg = emitAddSub_ri(/*UseAdd=*/true, VT, Src0Reg,
4519 /*IsKill=*/false, Pow2MinusOne);
4523 // (Src0 < 0) ? Pow2 - 1 : 0;
4524 if (!emitICmp_ri(VT, Src0Reg, /*IsKill=*/false, 0))
4528 const TargetRegisterClass *RC;
4529 if (VT == MVT::i64) {
4530 SelectOpc = AArch64::CSELXr;
4531 RC = &AArch64::GPR64RegClass;
4533 SelectOpc = AArch64::CSELWr;
4534 RC = &AArch64::GPR32RegClass;
4536 unsigned SelectReg =
4537 fastEmitInst_rri(SelectOpc, RC, AddReg, /*IsKill=*/true, Src0Reg,
4538 Src0IsKill, AArch64CC::LT);
4542 // Divide by Pow2 --> ashr. If we're dividing by a negative value we must also
4543 // negate the result.
4544 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
4547 ResultReg = emitAddSub_rs(/*UseAdd=*/false, VT, ZeroReg, /*IsKill=*/true,
4548 SelectReg, /*IsKill=*/true, AArch64_AM::ASR, Lg2);
4550 ResultReg = emitASR_ri(VT, VT, SelectReg, /*IsKill=*/true, Lg2);
4555 updateValueMap(I, ResultReg);
4559 bool AArch64FastISel::fastSelectInstruction(const Instruction *I) {
4560 switch (I->getOpcode()) {
4563 case Instruction::Add:
4564 case Instruction::Sub:
4565 return selectAddSub(I);
4566 case Instruction::Mul:
4567 return selectMul(I);
4568 case Instruction::SDiv:
4569 return selectSDiv(I);
4570 case Instruction::SRem:
4571 if (!selectBinaryOp(I, ISD::SREM))
4572 return selectRem(I, ISD::SREM);
4574 case Instruction::URem:
4575 if (!selectBinaryOp(I, ISD::UREM))
4576 return selectRem(I, ISD::UREM);
4578 case Instruction::Shl:
4579 case Instruction::LShr:
4580 case Instruction::AShr:
4581 return selectShift(I);
4582 case Instruction::And:
4583 case Instruction::Or:
4584 case Instruction::Xor:
4585 return selectLogicalOp(I);
4586 case Instruction::Br:
4587 return selectBranch(I);
4588 case Instruction::IndirectBr:
4589 return selectIndirectBr(I);
4590 case Instruction::BitCast:
4591 if (!FastISel::selectBitCast(I))
4592 return selectBitCast(I);
4594 case Instruction::FPToSI:
4595 if (!selectCast(I, ISD::FP_TO_SINT))
4596 return selectFPToInt(I, /*Signed=*/true);
4598 case Instruction::FPToUI:
4599 return selectFPToInt(I, /*Signed=*/false);
4600 case Instruction::ZExt:
4601 case Instruction::SExt:
4602 return selectIntExt(I);
4603 case Instruction::Trunc:
4604 if (!selectCast(I, ISD::TRUNCATE))
4605 return selectTrunc(I);
4607 case Instruction::FPExt:
4608 return selectFPExt(I);
4609 case Instruction::FPTrunc:
4610 return selectFPTrunc(I);
4611 case Instruction::SIToFP:
4612 if (!selectCast(I, ISD::SINT_TO_FP))
4613 return selectIntToFP(I, /*Signed=*/true);
4615 case Instruction::UIToFP:
4616 return selectIntToFP(I, /*Signed=*/false);
4617 case Instruction::Load:
4618 return selectLoad(I);
4619 case Instruction::Store:
4620 return selectStore(I);
4621 case Instruction::FCmp:
4622 case Instruction::ICmp:
4623 return selectCmp(I);
4624 case Instruction::Select:
4625 return selectSelect(I);
4626 case Instruction::Ret:
4627 return selectRet(I);
4628 case Instruction::FRem:
4629 return selectFRem(I);
4632 // fall-back to target-independent instruction selection.
4633 return selectOperator(I, I->getOpcode());
4634 // Silence warnings.
4635 (void)&CC_AArch64_DarwinPCS_VarArg;
4639 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &FuncInfo,
4640 const TargetLibraryInfo *LibInfo) {
4641 return new AArch64FastISel(FuncInfo, LibInfo);