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);
137 // Utility helper routines.
138 bool isTypeLegal(Type *Ty, MVT &VT);
139 bool isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed = false);
140 bool isValueAvailable(const Value *V) const;
141 bool computeAddress(const Value *Obj, Address &Addr, Type *Ty = nullptr);
142 bool computeCallAddress(const Value *V, Address &Addr);
143 bool simplifyAddress(Address &Addr, MVT VT);
144 void addLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
145 unsigned Flags, unsigned ScaleFactor,
146 MachineMemOperand *MMO);
147 bool isMemCpySmall(uint64_t Len, unsigned Alignment);
148 bool tryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
150 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
153 // Emit helper routines.
154 unsigned emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
155 const Value *RHS, bool SetFlags = false,
156 bool WantResult = true, bool IsZExt = false);
157 unsigned emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
158 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
159 bool SetFlags = false, bool WantResult = true);
160 unsigned emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
161 bool LHSIsKill, uint64_t Imm, bool SetFlags = false,
162 bool WantResult = true);
163 unsigned emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
164 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
165 AArch64_AM::ShiftExtendType ShiftType,
166 uint64_t ShiftImm, bool SetFlags = false,
167 bool WantResult = true);
168 unsigned emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
169 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
170 AArch64_AM::ShiftExtendType ExtType,
171 uint64_t ShiftImm, bool SetFlags = false,
172 bool WantResult = true);
175 bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
176 bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
177 bool emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
178 bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
179 bool emitLoad(MVT VT, unsigned &ResultReg, Address Addr,
180 MachineMemOperand *MMO = nullptr);
181 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
182 MachineMemOperand *MMO = nullptr);
183 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
184 unsigned emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
185 unsigned emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
186 bool SetFlags = false, bool WantResult = true,
187 bool IsZExt = false);
188 unsigned emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
189 bool SetFlags = false, bool WantResult = true,
190 bool IsZExt = false);
191 unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
192 unsigned RHSReg, bool RHSIsKill, bool WantResult = true);
193 unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
194 unsigned RHSReg, bool RHSIsKill,
195 AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
196 bool WantResult = true);
197 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
199 unsigned emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
200 bool LHSIsKill, uint64_t Imm);
201 unsigned emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
202 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
204 unsigned emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
205 unsigned emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
206 unsigned Op1, bool Op1IsKill);
207 unsigned emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
208 unsigned Op1, bool Op1IsKill);
209 unsigned emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
210 unsigned Op1, bool Op1IsKill);
211 unsigned emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
212 unsigned Op1Reg, bool Op1IsKill);
213 unsigned emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
214 uint64_t Imm, bool IsZExt = true);
215 unsigned emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
216 unsigned Op1Reg, bool Op1IsKill);
217 unsigned emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
218 uint64_t Imm, bool IsZExt = true);
219 unsigned emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
220 unsigned Op1Reg, bool Op1IsKill);
221 unsigned emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
222 uint64_t Imm, bool IsZExt = false);
224 unsigned materializeInt(const ConstantInt *CI, MVT VT);
225 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
226 unsigned materializeGV(const GlobalValue *GV);
228 // Call handling routines.
230 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
231 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
233 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
236 // Backend specific FastISel code.
237 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
238 unsigned fastMaterializeConstant(const Constant *C) override;
239 unsigned fastMaterializeFloatZero(const ConstantFP* CF) override;
241 explicit AArch64FastISel(FunctionLoweringInfo &FuncInfo,
242 const TargetLibraryInfo *LibInfo)
243 : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
244 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
245 Context = &FuncInfo.Fn->getContext();
248 bool fastSelectInstruction(const Instruction *I) override;
250 #include "AArch64GenFastISel.inc"
253 } // end anonymous namespace
255 #include "AArch64GenCallingConv.inc"
257 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
258 if (CC == CallingConv::WebKit_JS)
259 return CC_AArch64_WebKit_JS;
260 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
263 unsigned AArch64FastISel::fastMaterializeAlloca(const AllocaInst *AI) {
264 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
265 "Alloca should always return a pointer.");
267 // Don't handle dynamic allocas.
268 if (!FuncInfo.StaticAllocaMap.count(AI))
271 DenseMap<const AllocaInst *, int>::iterator SI =
272 FuncInfo.StaticAllocaMap.find(AI);
274 if (SI != FuncInfo.StaticAllocaMap.end()) {
275 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
276 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
278 .addFrameIndex(SI->second)
287 unsigned AArch64FastISel::materializeInt(const ConstantInt *CI, MVT VT) {
292 return fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
294 // Create a copy from the zero register to materialize a "0" value.
295 const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
296 : &AArch64::GPR32RegClass;
297 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
298 unsigned ResultReg = createResultReg(RC);
299 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
300 ResultReg).addReg(ZeroReg, getKillRegState(true));
304 unsigned AArch64FastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
305 // Positive zero (+0.0) has to be materialized with a fmov from the zero
306 // register, because the immediate version of fmov cannot encode zero.
307 if (CFP->isNullValue())
308 return fastMaterializeFloatZero(CFP);
310 if (VT != MVT::f32 && VT != MVT::f64)
313 const APFloat Val = CFP->getValueAPF();
314 bool Is64Bit = (VT == MVT::f64);
315 // This checks to see if we can use FMOV instructions to materialize
316 // a constant, otherwise we have to materialize via the constant pool.
317 if (TLI.isFPImmLegal(Val, VT)) {
319 Is64Bit ? AArch64_AM::getFP64Imm(Val) : AArch64_AM::getFP32Imm(Val);
320 assert((Imm != -1) && "Cannot encode floating-point constant.");
321 unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
322 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
325 // Materialize via constant pool. MachineConstantPool wants an explicit
327 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
329 Align = DL.getTypeAllocSize(CFP->getType());
331 unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
332 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
333 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
334 ADRPReg).addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
336 unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
337 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
338 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
340 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
344 unsigned AArch64FastISel::materializeGV(const GlobalValue *GV) {
345 // We can't handle thread-local variables quickly yet.
346 if (GV->isThreadLocal())
349 // MachO still uses GOT for large code-model accesses, but ELF requires
350 // movz/movk sequences, which FastISel doesn't handle yet.
351 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
354 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
356 EVT DestEVT = TLI.getValueType(GV->getType(), true);
357 if (!DestEVT.isSimple())
360 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
363 if (OpFlags & AArch64II::MO_GOT) {
365 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
367 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
369 ResultReg = createResultReg(&AArch64::GPR64RegClass);
370 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
373 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
375 } else if (OpFlags & AArch64II::MO_CONSTPOOL) {
376 // We can't handle addresses loaded from a constant pool quickly yet.
380 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
382 .addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
384 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
385 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
388 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
394 unsigned AArch64FastISel::fastMaterializeConstant(const Constant *C) {
395 EVT CEVT = TLI.getValueType(C->getType(), true);
397 // Only handle simple types.
398 if (!CEVT.isSimple())
400 MVT VT = CEVT.getSimpleVT();
402 if (const auto *CI = dyn_cast<ConstantInt>(C))
403 return materializeInt(CI, VT);
404 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
405 return materializeFP(CFP, VT);
406 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
407 return materializeGV(GV);
412 unsigned AArch64FastISel::fastMaterializeFloatZero(const ConstantFP* CFP) {
413 assert(CFP->isNullValue() &&
414 "Floating-point constant is not a positive zero.");
416 if (!isTypeLegal(CFP->getType(), VT))
419 if (VT != MVT::f32 && VT != MVT::f64)
422 bool Is64Bit = (VT == MVT::f64);
423 unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
424 unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
425 return fastEmitInst_r(Opc, TLI.getRegClassFor(VT), ZReg, /*IsKill=*/true);
428 // Computes the address to get to an object.
429 bool AArch64FastISel::computeAddress(const Value *Obj, Address &Addr, Type *Ty)
431 const User *U = nullptr;
432 unsigned Opcode = Instruction::UserOp1;
433 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
434 // Don't walk into other basic blocks unless the object is an alloca from
435 // another block, otherwise it may not have a virtual register assigned.
436 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
437 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
438 Opcode = I->getOpcode();
441 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
442 Opcode = C->getOpcode();
446 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
447 if (Ty->getAddressSpace() > 255)
448 // Fast instruction selection doesn't support the special
455 case Instruction::BitCast: {
456 // Look through bitcasts.
457 return computeAddress(U->getOperand(0), Addr, Ty);
459 case Instruction::IntToPtr: {
460 // Look past no-op inttoptrs.
461 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
462 return computeAddress(U->getOperand(0), Addr, Ty);
465 case Instruction::PtrToInt: {
466 // Look past no-op ptrtoints.
467 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
468 return computeAddress(U->getOperand(0), Addr, Ty);
471 case Instruction::GetElementPtr: {
472 Address SavedAddr = Addr;
473 uint64_t TmpOffset = Addr.getOffset();
475 // Iterate through the GEP folding the constants into offsets where
477 gep_type_iterator GTI = gep_type_begin(U);
478 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
480 const Value *Op = *i;
481 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
482 const StructLayout *SL = DL.getStructLayout(STy);
483 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
484 TmpOffset += SL->getElementOffset(Idx);
486 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
488 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
489 // Constant-offset addressing.
490 TmpOffset += CI->getSExtValue() * S;
493 if (canFoldAddIntoGEP(U, Op)) {
494 // A compatible add with a constant operand. Fold the constant.
496 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
497 TmpOffset += CI->getSExtValue() * S;
498 // Iterate on the other operand.
499 Op = cast<AddOperator>(Op)->getOperand(0);
503 goto unsupported_gep;
508 // Try to grab the base operand now.
509 Addr.setOffset(TmpOffset);
510 if (computeAddress(U->getOperand(0), Addr, Ty))
513 // We failed, restore everything and try the other options.
519 case Instruction::Alloca: {
520 const AllocaInst *AI = cast<AllocaInst>(Obj);
521 DenseMap<const AllocaInst *, int>::iterator SI =
522 FuncInfo.StaticAllocaMap.find(AI);
523 if (SI != FuncInfo.StaticAllocaMap.end()) {
524 Addr.setKind(Address::FrameIndexBase);
525 Addr.setFI(SI->second);
530 case Instruction::Add: {
531 // Adds of constants are common and easy enough.
532 const Value *LHS = U->getOperand(0);
533 const Value *RHS = U->getOperand(1);
535 if (isa<ConstantInt>(LHS))
538 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
539 Addr.setOffset(Addr.getOffset() + (uint64_t)CI->getSExtValue());
540 return computeAddress(LHS, Addr, Ty);
543 Address Backup = Addr;
544 if (computeAddress(LHS, Addr, Ty) && computeAddress(RHS, Addr, Ty))
550 case Instruction::Shl:
551 if (Addr.getOffsetReg())
554 if (const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
555 unsigned Val = CI->getZExtValue();
556 if (Val < 1 || Val > 3)
559 uint64_t NumBytes = 0;
560 if (Ty && Ty->isSized()) {
561 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
562 NumBytes = NumBits / 8;
563 if (!isPowerOf2_64(NumBits))
567 if (NumBytes != (1ULL << Val))
571 Addr.setExtendType(AArch64_AM::LSL);
573 if (const auto *I = dyn_cast<Instruction>(U->getOperand(0)))
574 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
577 if (const auto *ZE = dyn_cast<ZExtInst>(U))
578 if (ZE->getOperand(0)->getType()->isIntegerTy(32))
579 Addr.setExtendType(AArch64_AM::UXTW);
581 if (const auto *SE = dyn_cast<SExtInst>(U))
582 if (SE->getOperand(0)->getType()->isIntegerTy(32))
583 Addr.setExtendType(AArch64_AM::SXTW);
585 unsigned Reg = getRegForValue(U->getOperand(0));
588 Addr.setOffsetReg(Reg);
595 if (!Addr.getOffsetReg()) {
596 unsigned Reg = getRegForValue(Obj);
599 Addr.setOffsetReg(Reg);
605 unsigned Reg = getRegForValue(Obj);
612 bool AArch64FastISel::computeCallAddress(const Value *V, Address &Addr) {
613 const User *U = nullptr;
614 unsigned Opcode = Instruction::UserOp1;
617 if (const auto *I = dyn_cast<Instruction>(V)) {
618 Opcode = I->getOpcode();
620 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
621 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
622 Opcode = C->getOpcode();
628 case Instruction::BitCast:
629 // Look past bitcasts if its operand is in the same BB.
631 return computeCallAddress(U->getOperand(0), Addr);
633 case Instruction::IntToPtr:
634 // Look past no-op inttoptrs if its operand is in the same BB.
636 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
637 return computeCallAddress(U->getOperand(0), Addr);
639 case Instruction::PtrToInt:
640 // Look past no-op ptrtoints if its operand is in the same BB.
642 TLI.getValueType(U->getType()) == TLI.getPointerTy())
643 return computeCallAddress(U->getOperand(0), Addr);
647 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
648 Addr.setGlobalValue(GV);
652 // If all else fails, try to materialize the value in a register.
653 if (!Addr.getGlobalValue()) {
654 Addr.setReg(getRegForValue(V));
655 return Addr.getReg() != 0;
662 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
663 EVT evt = TLI.getValueType(Ty, true);
665 // Only handle simple types.
666 if (evt == MVT::Other || !evt.isSimple())
668 VT = evt.getSimpleVT();
670 // This is a legal type, but it's not something we handle in fast-isel.
674 // Handle all other legal types, i.e. a register that will directly hold this
676 return TLI.isTypeLegal(VT);
679 /// \brief Determine if the value type is supported by FastISel.
681 /// FastISel for AArch64 can handle more value types than are legal. This adds
682 /// simple value type such as i1, i8, and i16.
683 bool AArch64FastISel::isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed) {
684 if (Ty->isVectorTy() && !IsVectorAllowed)
687 if (isTypeLegal(Ty, VT))
690 // If this is a type than can be sign or zero-extended to a basic operation
691 // go ahead and accept it now.
692 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
698 bool AArch64FastISel::isValueAvailable(const Value *V) const {
699 if (!isa<Instruction>(V))
702 const auto *I = cast<Instruction>(V);
703 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
709 bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) {
710 unsigned ScaleFactor;
711 switch (VT.SimpleTy) {
712 default: return false;
713 case MVT::i1: // fall-through
714 case MVT::i8: ScaleFactor = 1; break;
715 case MVT::i16: ScaleFactor = 2; break;
716 case MVT::i32: // fall-through
717 case MVT::f32: ScaleFactor = 4; break;
718 case MVT::i64: // fall-through
719 case MVT::f64: ScaleFactor = 8; break;
722 bool ImmediateOffsetNeedsLowering = false;
723 bool RegisterOffsetNeedsLowering = false;
724 int64_t Offset = Addr.getOffset();
725 if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
726 ImmediateOffsetNeedsLowering = true;
727 else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
728 !isUInt<12>(Offset / ScaleFactor))
729 ImmediateOffsetNeedsLowering = true;
731 // Cannot encode an offset register and an immediate offset in the same
732 // instruction. Fold the immediate offset into the load/store instruction and
733 // emit an additonal add to take care of the offset register.
734 if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.isRegBase() &&
736 RegisterOffsetNeedsLowering = true;
738 // Cannot encode zero register as base.
739 if (Addr.isRegBase() && Addr.getOffsetReg() && !Addr.getReg())
740 RegisterOffsetNeedsLowering = true;
742 // If this is a stack pointer and the offset needs to be simplified then put
743 // the alloca address into a register, set the base type back to register and
744 // continue. This should almost never happen.
745 if (ImmediateOffsetNeedsLowering && Addr.isFIBase()) {
746 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
747 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
749 .addFrameIndex(Addr.getFI())
752 Addr.setKind(Address::RegBase);
753 Addr.setReg(ResultReg);
756 if (RegisterOffsetNeedsLowering) {
757 unsigned ResultReg = 0;
759 if (Addr.getExtendType() == AArch64_AM::SXTW ||
760 Addr.getExtendType() == AArch64_AM::UXTW )
761 ResultReg = emitAddSub_rx(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
762 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
763 /*TODO:IsKill=*/false, Addr.getExtendType(),
766 ResultReg = emitAddSub_rs(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
767 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
768 /*TODO:IsKill=*/false, AArch64_AM::LSL,
771 if (Addr.getExtendType() == AArch64_AM::UXTW)
772 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
773 /*Op0IsKill=*/false, Addr.getShift(),
775 else if (Addr.getExtendType() == AArch64_AM::SXTW)
776 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
777 /*Op0IsKill=*/false, Addr.getShift(),
780 ResultReg = emitLSL_ri(MVT::i64, MVT::i64, Addr.getOffsetReg(),
781 /*Op0IsKill=*/false, Addr.getShift());
786 Addr.setReg(ResultReg);
787 Addr.setOffsetReg(0);
789 Addr.setExtendType(AArch64_AM::InvalidShiftExtend);
792 // Since the offset is too large for the load/store instruction get the
793 // reg+offset into a register.
794 if (ImmediateOffsetNeedsLowering) {
795 unsigned ResultReg = 0;
797 ResultReg = fastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(),
798 /*IsKill=*/false, Offset, MVT::i64);
800 ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
804 Addr.setReg(ResultReg);
810 void AArch64FastISel::addLoadStoreOperands(Address &Addr,
811 const MachineInstrBuilder &MIB,
813 unsigned ScaleFactor,
814 MachineMemOperand *MMO) {
815 int64_t Offset = Addr.getOffset() / ScaleFactor;
816 // Frame base works a bit differently. Handle it separately.
817 if (Addr.isFIBase()) {
818 int FI = Addr.getFI();
819 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
820 // and alignment should be based on the VT.
821 MMO = FuncInfo.MF->getMachineMemOperand(
822 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
823 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
824 // Now add the rest of the operands.
825 MIB.addFrameIndex(FI).addImm(Offset);
827 assert(Addr.isRegBase() && "Unexpected address kind.");
828 const MCInstrDesc &II = MIB->getDesc();
829 unsigned Idx = (Flags & MachineMemOperand::MOStore) ? 1 : 0;
831 constrainOperandRegClass(II, Addr.getReg(), II.getNumDefs()+Idx));
833 constrainOperandRegClass(II, Addr.getOffsetReg(), II.getNumDefs()+Idx+1));
834 if (Addr.getOffsetReg()) {
835 assert(Addr.getOffset() == 0 && "Unexpected offset");
836 bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
837 Addr.getExtendType() == AArch64_AM::SXTX;
838 MIB.addReg(Addr.getReg());
839 MIB.addReg(Addr.getOffsetReg());
840 MIB.addImm(IsSigned);
841 MIB.addImm(Addr.getShift() != 0);
843 MIB.addReg(Addr.getReg());
849 MIB.addMemOperand(MMO);
852 unsigned AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
853 const Value *RHS, bool SetFlags,
854 bool WantResult, bool IsZExt) {
855 AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
856 bool NeedExtend = false;
857 switch (RetVT.SimpleTy) {
865 ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
869 ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
871 case MVT::i32: // fall-through
876 RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
878 // Canonicalize immediates to the RHS first.
879 if (UseAdd && isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
882 // Canonicalize shift immediate to the RHS.
883 if (UseAdd && isValueAvailable(LHS))
884 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
885 if (isa<ConstantInt>(SI->getOperand(1)))
886 if (SI->getOpcode() == Instruction::Shl ||
887 SI->getOpcode() == Instruction::LShr ||
888 SI->getOpcode() == Instruction::AShr )
891 unsigned LHSReg = getRegForValue(LHS);
894 bool LHSIsKill = hasTrivialKill(LHS);
897 LHSReg = emitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
899 unsigned ResultReg = 0;
900 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
901 uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
903 ResultReg = emitAddSub_ri(!UseAdd, RetVT, LHSReg, LHSIsKill, -Imm,
904 SetFlags, WantResult);
906 ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, Imm, SetFlags,
912 // Only extend the RHS within the instruction if there is a valid extend type.
913 if (ExtendType != AArch64_AM::InvalidShiftExtend && isValueAvailable(RHS)) {
914 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
915 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
916 if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
917 unsigned RHSReg = getRegForValue(SI->getOperand(0));
920 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
921 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
922 RHSIsKill, ExtendType, C->getZExtValue(),
923 SetFlags, WantResult);
925 unsigned RHSReg = getRegForValue(RHS);
928 bool RHSIsKill = hasTrivialKill(RHS);
929 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
930 ExtendType, 0, SetFlags, WantResult);
933 // Check if the shift can be folded into the instruction.
934 if (isValueAvailable(RHS))
935 if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
936 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
937 AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
938 switch (SI->getOpcode()) {
940 case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
941 case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
942 case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
944 uint64_t ShiftVal = C->getZExtValue();
945 if (ShiftType != AArch64_AM::InvalidShiftExtend) {
946 unsigned RHSReg = getRegForValue(SI->getOperand(0));
949 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
950 return emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
951 RHSIsKill, ShiftType, ShiftVal, SetFlags,
957 unsigned RHSReg = getRegForValue(RHS);
960 bool RHSIsKill = hasTrivialKill(RHS);
963 RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
965 return emitAddSub_rr(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
966 SetFlags, WantResult);
969 unsigned AArch64FastISel::emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
970 bool LHSIsKill, unsigned RHSReg,
971 bool RHSIsKill, bool SetFlags,
973 assert(LHSReg && RHSReg && "Invalid register number.");
975 if (RetVT != MVT::i32 && RetVT != MVT::i64)
978 static const unsigned OpcTable[2][2][2] = {
979 { { AArch64::SUBWrr, AArch64::SUBXrr },
980 { AArch64::ADDWrr, AArch64::ADDXrr } },
981 { { AArch64::SUBSWrr, AArch64::SUBSXrr },
982 { AArch64::ADDSWrr, AArch64::ADDSXrr } }
984 bool Is64Bit = RetVT == MVT::i64;
985 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
986 const TargetRegisterClass *RC =
987 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
990 ResultReg = createResultReg(RC);
992 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
994 const MCInstrDesc &II = TII.get(Opc);
995 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
996 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
997 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
998 .addReg(LHSReg, getKillRegState(LHSIsKill))
999 .addReg(RHSReg, getKillRegState(RHSIsKill));
1003 unsigned AArch64FastISel::emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
1004 bool LHSIsKill, uint64_t Imm,
1005 bool SetFlags, bool WantResult) {
1006 assert(LHSReg && "Invalid register number.");
1008 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1012 if (isUInt<12>(Imm))
1014 else if ((Imm & 0xfff000) == Imm) {
1020 static const unsigned OpcTable[2][2][2] = {
1021 { { AArch64::SUBWri, AArch64::SUBXri },
1022 { AArch64::ADDWri, AArch64::ADDXri } },
1023 { { AArch64::SUBSWri, AArch64::SUBSXri },
1024 { AArch64::ADDSWri, AArch64::ADDSXri } }
1026 bool Is64Bit = RetVT == MVT::i64;
1027 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1028 const TargetRegisterClass *RC;
1030 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1032 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1035 ResultReg = createResultReg(RC);
1037 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1039 const MCInstrDesc &II = TII.get(Opc);
1040 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1041 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1042 .addReg(LHSReg, getKillRegState(LHSIsKill))
1044 .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
1048 unsigned AArch64FastISel::emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
1049 bool LHSIsKill, unsigned RHSReg,
1051 AArch64_AM::ShiftExtendType ShiftType,
1052 uint64_t ShiftImm, bool SetFlags,
1054 assert(LHSReg && RHSReg && "Invalid register number.");
1056 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1059 static const unsigned OpcTable[2][2][2] = {
1060 { { AArch64::SUBWrs, AArch64::SUBXrs },
1061 { AArch64::ADDWrs, AArch64::ADDXrs } },
1062 { { AArch64::SUBSWrs, AArch64::SUBSXrs },
1063 { AArch64::ADDSWrs, AArch64::ADDSXrs } }
1065 bool Is64Bit = RetVT == MVT::i64;
1066 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1067 const TargetRegisterClass *RC =
1068 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1071 ResultReg = createResultReg(RC);
1073 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1075 const MCInstrDesc &II = TII.get(Opc);
1076 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1077 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1078 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1079 .addReg(LHSReg, getKillRegState(LHSIsKill))
1080 .addReg(RHSReg, getKillRegState(RHSIsKill))
1081 .addImm(getShifterImm(ShiftType, ShiftImm));
1085 unsigned AArch64FastISel::emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
1086 bool LHSIsKill, unsigned RHSReg,
1088 AArch64_AM::ShiftExtendType ExtType,
1089 uint64_t ShiftImm, bool SetFlags,
1091 assert(LHSReg && RHSReg && "Invalid register number.");
1093 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1096 static const unsigned OpcTable[2][2][2] = {
1097 { { AArch64::SUBWrx, AArch64::SUBXrx },
1098 { AArch64::ADDWrx, AArch64::ADDXrx } },
1099 { { AArch64::SUBSWrx, AArch64::SUBSXrx },
1100 { AArch64::ADDSWrx, AArch64::ADDSXrx } }
1102 bool Is64Bit = RetVT == MVT::i64;
1103 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1104 const TargetRegisterClass *RC = nullptr;
1106 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1108 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1111 ResultReg = createResultReg(RC);
1113 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1115 const MCInstrDesc &II = TII.get(Opc);
1116 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1117 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1118 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1119 .addReg(LHSReg, getKillRegState(LHSIsKill))
1120 .addReg(RHSReg, getKillRegState(RHSIsKill))
1121 .addImm(getArithExtendImm(ExtType, ShiftImm));
1125 bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1126 Type *Ty = LHS->getType();
1127 EVT EVT = TLI.getValueType(Ty, true);
1128 if (!EVT.isSimple())
1130 MVT VT = EVT.getSimpleVT();
1132 switch (VT.SimpleTy) {
1140 return emitICmp(VT, LHS, RHS, IsZExt);
1143 return emitFCmp(VT, LHS, RHS);
1147 bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1149 return emitSub(RetVT, LHS, RHS, /*SetFlags=*/true, /*WantResult=*/false,
1153 bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1155 return emitAddSub_ri(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, Imm,
1156 /*SetFlags=*/true, /*WantResult=*/false) != 0;
1159 bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1160 if (RetVT != MVT::f32 && RetVT != MVT::f64)
1163 // Check to see if the 2nd operand is a constant that we can encode directly
1165 bool UseImm = false;
1166 if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1167 if (CFP->isZero() && !CFP->isNegative())
1170 unsigned LHSReg = getRegForValue(LHS);
1173 bool LHSIsKill = hasTrivialKill(LHS);
1176 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1177 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1178 .addReg(LHSReg, getKillRegState(LHSIsKill));
1182 unsigned RHSReg = getRegForValue(RHS);
1185 bool RHSIsKill = hasTrivialKill(RHS);
1187 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1188 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1189 .addReg(LHSReg, getKillRegState(LHSIsKill))
1190 .addReg(RHSReg, getKillRegState(RHSIsKill));
1194 unsigned AArch64FastISel::emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
1195 bool SetFlags, bool WantResult, bool IsZExt) {
1196 return emitAddSub(/*UseAdd=*/true, RetVT, LHS, RHS, SetFlags, WantResult,
1200 unsigned AArch64FastISel::emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
1201 bool SetFlags, bool WantResult, bool IsZExt) {
1202 return emitAddSub(/*UseAdd=*/false, RetVT, LHS, RHS, SetFlags, WantResult,
1206 unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1207 bool LHSIsKill, unsigned RHSReg,
1208 bool RHSIsKill, bool WantResult) {
1209 return emitAddSub_rr(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1210 RHSIsKill, /*SetFlags=*/true, WantResult);
1213 unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1214 bool LHSIsKill, unsigned RHSReg,
1216 AArch64_AM::ShiftExtendType ShiftType,
1217 uint64_t ShiftImm, bool WantResult) {
1218 return emitAddSub_rs(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1219 RHSIsKill, ShiftType, ShiftImm, /*SetFlags=*/true,
1223 unsigned AArch64FastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
1224 const Value *LHS, const Value *RHS) {
1225 // Canonicalize immediates to the RHS first.
1226 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1227 std::swap(LHS, RHS);
1229 // Canonicalize shift immediate to the RHS.
1230 if (isValueAvailable(LHS))
1231 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
1232 if (isa<ConstantInt>(SI->getOperand(1)))
1233 if (SI->getOpcode() == Instruction::Shl)
1234 std::swap(LHS, RHS);
1236 unsigned LHSReg = getRegForValue(LHS);
1239 bool LHSIsKill = hasTrivialKill(LHS);
1241 unsigned ResultReg = 0;
1242 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1243 uint64_t Imm = C->getZExtValue();
1244 ResultReg = emitLogicalOp_ri(ISDOpc, RetVT, LHSReg, LHSIsKill, Imm);
1249 // Check if the shift can be folded into the instruction.
1250 if (isValueAvailable(RHS))
1251 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
1252 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
1253 if (SI->getOpcode() == Instruction::Shl) {
1254 uint64_t ShiftVal = C->getZExtValue();
1255 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1258 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1259 return emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1260 RHSIsKill, ShiftVal);
1263 unsigned RHSReg = getRegForValue(RHS);
1266 bool RHSIsKill = hasTrivialKill(RHS);
1268 MVT VT = std::max(MVT::i32, RetVT.SimpleTy);
1269 ResultReg = fastEmit_rr(VT, VT, ISDOpc, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1270 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1271 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1272 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1277 unsigned AArch64FastISel::emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT,
1278 unsigned LHSReg, bool LHSIsKill,
1280 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1281 "ISD nodes are not consecutive!");
1282 static const unsigned OpcTable[3][2] = {
1283 { AArch64::ANDWri, AArch64::ANDXri },
1284 { AArch64::ORRWri, AArch64::ORRXri },
1285 { AArch64::EORWri, AArch64::EORXri }
1287 const TargetRegisterClass *RC;
1290 switch (RetVT.SimpleTy) {
1297 unsigned Idx = ISDOpc - ISD::AND;
1298 Opc = OpcTable[Idx][0];
1299 RC = &AArch64::GPR32spRegClass;
1304 Opc = OpcTable[ISDOpc - ISD::AND][1];
1305 RC = &AArch64::GPR64spRegClass;
1310 if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
1313 unsigned ResultReg =
1314 fastEmitInst_ri(Opc, RC, LHSReg, LHSIsKill,
1315 AArch64_AM::encodeLogicalImmediate(Imm, RegSize));
1316 if (RetVT >= MVT::i8 && RetVT <= MVT::i16 && ISDOpc != ISD::AND) {
1317 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1318 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1323 unsigned AArch64FastISel::emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT,
1324 unsigned LHSReg, bool LHSIsKill,
1325 unsigned RHSReg, bool RHSIsKill,
1326 uint64_t ShiftImm) {
1327 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1328 "ISD nodes are not consecutive!");
1329 static const unsigned OpcTable[3][2] = {
1330 { AArch64::ANDWrs, AArch64::ANDXrs },
1331 { AArch64::ORRWrs, AArch64::ORRXrs },
1332 { AArch64::EORWrs, AArch64::EORXrs }
1334 const TargetRegisterClass *RC;
1336 switch (RetVT.SimpleTy) {
1343 Opc = OpcTable[ISDOpc - ISD::AND][0];
1344 RC = &AArch64::GPR32RegClass;
1347 Opc = OpcTable[ISDOpc - ISD::AND][1];
1348 RC = &AArch64::GPR64RegClass;
1351 unsigned ResultReg =
1352 fastEmitInst_rri(Opc, RC, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1353 AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftImm));
1354 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1355 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1356 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1361 unsigned AArch64FastISel::emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1363 return emitLogicalOp_ri(ISD::AND, RetVT, LHSReg, LHSIsKill, Imm);
1366 bool AArch64FastISel::emitLoad(MVT VT, unsigned &ResultReg, Address Addr,
1367 MachineMemOperand *MMO) {
1368 // Simplify this down to something we can handle.
1369 if (!simplifyAddress(Addr, VT))
1372 unsigned ScaleFactor;
1373 switch (VT.SimpleTy) {
1374 default: llvm_unreachable("Unexpected value type.");
1375 case MVT::i1: // fall-through
1376 case MVT::i8: ScaleFactor = 1; break;
1377 case MVT::i16: ScaleFactor = 2; break;
1378 case MVT::i32: // fall-through
1379 case MVT::f32: ScaleFactor = 4; break;
1380 case MVT::i64: // fall-through
1381 case MVT::f64: ScaleFactor = 8; break;
1384 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1385 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1386 bool UseScaled = true;
1387 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1392 static const unsigned OpcTable[4][6] = {
1393 { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi, AArch64::LDURXi,
1394 AArch64::LDURSi, AArch64::LDURDi },
1395 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui, AArch64::LDRXui,
1396 AArch64::LDRSui, AArch64::LDRDui },
1397 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX, AArch64::LDRXroX,
1398 AArch64::LDRSroX, AArch64::LDRDroX },
1399 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW, AArch64::LDRXroW,
1400 AArch64::LDRSroW, AArch64::LDRDroW }
1404 const TargetRegisterClass *RC;
1405 bool VTIsi1 = false;
1406 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1407 Addr.getOffsetReg();
1408 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1409 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1410 Addr.getExtendType() == AArch64_AM::SXTW)
1413 switch (VT.SimpleTy) {
1414 default: llvm_unreachable("Unexpected value type.");
1415 case MVT::i1: VTIsi1 = true; // Intentional fall-through.
1416 case MVT::i8: Opc = OpcTable[Idx][0]; RC = &AArch64::GPR32RegClass; break;
1417 case MVT::i16: Opc = OpcTable[Idx][1]; RC = &AArch64::GPR32RegClass; break;
1418 case MVT::i32: Opc = OpcTable[Idx][2]; RC = &AArch64::GPR32RegClass; break;
1419 case MVT::i64: Opc = OpcTable[Idx][3]; RC = &AArch64::GPR64RegClass; break;
1420 case MVT::f32: Opc = OpcTable[Idx][4]; RC = &AArch64::FPR32RegClass; break;
1421 case MVT::f64: Opc = OpcTable[Idx][5]; RC = &AArch64::FPR64RegClass; break;
1424 // Create the base instruction, then add the operands.
1425 ResultReg = createResultReg(RC);
1426 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1427 TII.get(Opc), ResultReg);
1428 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
1430 // Loading an i1 requires special handling.
1432 unsigned ANDReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, 1);
1433 assert(ANDReg && "Unexpected AND instruction emission failure.");
1439 bool AArch64FastISel::selectAddSub(const Instruction *I) {
1441 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1445 return selectOperator(I, I->getOpcode());
1448 switch (I->getOpcode()) {
1450 llvm_unreachable("Unexpected instruction.");
1451 case Instruction::Add:
1452 ResultReg = emitAdd(VT, I->getOperand(0), I->getOperand(1));
1454 case Instruction::Sub:
1455 ResultReg = emitSub(VT, I->getOperand(0), I->getOperand(1));
1461 updateValueMap(I, ResultReg);
1465 bool AArch64FastISel::selectLogicalOp(const Instruction *I) {
1467 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1471 return selectOperator(I, I->getOpcode());
1474 switch (I->getOpcode()) {
1476 llvm_unreachable("Unexpected instruction.");
1477 case Instruction::And:
1478 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
1480 case Instruction::Or:
1481 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
1483 case Instruction::Xor:
1484 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
1490 updateValueMap(I, ResultReg);
1494 bool AArch64FastISel::selectLoad(const Instruction *I) {
1496 // Verify we have a legal type before going any further. Currently, we handle
1497 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1498 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1499 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true) ||
1500 cast<LoadInst>(I)->isAtomic())
1503 // See if we can handle this address.
1505 if (!computeAddress(I->getOperand(0), Addr, I->getType()))
1509 if (!emitLoad(VT, ResultReg, Addr, createMachineMemOperandFor(I)))
1512 updateValueMap(I, ResultReg);
1516 bool AArch64FastISel::emitStore(MVT VT, unsigned SrcReg, Address Addr,
1517 MachineMemOperand *MMO) {
1518 // Simplify this down to something we can handle.
1519 if (!simplifyAddress(Addr, VT))
1522 unsigned ScaleFactor;
1523 switch (VT.SimpleTy) {
1524 default: llvm_unreachable("Unexpected value type.");
1525 case MVT::i1: // fall-through
1526 case MVT::i8: ScaleFactor = 1; break;
1527 case MVT::i16: ScaleFactor = 2; break;
1528 case MVT::i32: // fall-through
1529 case MVT::f32: ScaleFactor = 4; break;
1530 case MVT::i64: // fall-through
1531 case MVT::f64: ScaleFactor = 8; break;
1534 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1535 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1536 bool UseScaled = true;
1537 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1543 static const unsigned OpcTable[4][6] = {
1544 { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
1545 AArch64::STURSi, AArch64::STURDi },
1546 { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
1547 AArch64::STRSui, AArch64::STRDui },
1548 { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
1549 AArch64::STRSroX, AArch64::STRDroX },
1550 { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
1551 AArch64::STRSroW, AArch64::STRDroW }
1556 bool VTIsi1 = false;
1557 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1558 Addr.getOffsetReg();
1559 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1560 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1561 Addr.getExtendType() == AArch64_AM::SXTW)
1564 switch (VT.SimpleTy) {
1565 default: llvm_unreachable("Unexpected value type.");
1566 case MVT::i1: VTIsi1 = true;
1567 case MVT::i8: Opc = OpcTable[Idx][0]; break;
1568 case MVT::i16: Opc = OpcTable[Idx][1]; break;
1569 case MVT::i32: Opc = OpcTable[Idx][2]; break;
1570 case MVT::i64: Opc = OpcTable[Idx][3]; break;
1571 case MVT::f32: Opc = OpcTable[Idx][4]; break;
1572 case MVT::f64: Opc = OpcTable[Idx][5]; break;
1575 // Storing an i1 requires special handling.
1576 if (VTIsi1 && SrcReg != AArch64::WZR) {
1577 unsigned ANDReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
1578 assert(ANDReg && "Unexpected AND instruction emission failure.");
1581 // Create the base instruction, then add the operands.
1582 const MCInstrDesc &II = TII.get(Opc);
1583 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
1584 MachineInstrBuilder MIB =
1585 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(SrcReg);
1586 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
1591 bool AArch64FastISel::selectStore(const Instruction *I) {
1593 const Value *Op0 = I->getOperand(0);
1594 // Verify we have a legal type before going any further. Currently, we handle
1595 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1596 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1597 if (!isTypeSupported(Op0->getType(), VT, /*IsVectorAllowed=*/true) ||
1598 cast<StoreInst>(I)->isAtomic())
1601 // Get the value to be stored into a register. Use the zero register directly
1602 // when possible to avoid an unnecessary copy and a wasted register.
1603 unsigned SrcReg = 0;
1604 if (const auto *CI = dyn_cast<ConstantInt>(Op0)) {
1606 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
1607 } else if (const auto *CF = dyn_cast<ConstantFP>(Op0)) {
1608 if (CF->isZero() && !CF->isNegative()) {
1609 VT = MVT::getIntegerVT(VT.getSizeInBits());
1610 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
1615 SrcReg = getRegForValue(Op0);
1620 // See if we can handle this address.
1622 if (!computeAddress(I->getOperand(1), Addr, I->getOperand(0)->getType()))
1625 if (!emitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
1630 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
1632 case CmpInst::FCMP_ONE:
1633 case CmpInst::FCMP_UEQ:
1635 // AL is our "false" for now. The other two need more compares.
1636 return AArch64CC::AL;
1637 case CmpInst::ICMP_EQ:
1638 case CmpInst::FCMP_OEQ:
1639 return AArch64CC::EQ;
1640 case CmpInst::ICMP_SGT:
1641 case CmpInst::FCMP_OGT:
1642 return AArch64CC::GT;
1643 case CmpInst::ICMP_SGE:
1644 case CmpInst::FCMP_OGE:
1645 return AArch64CC::GE;
1646 case CmpInst::ICMP_UGT:
1647 case CmpInst::FCMP_UGT:
1648 return AArch64CC::HI;
1649 case CmpInst::FCMP_OLT:
1650 return AArch64CC::MI;
1651 case CmpInst::ICMP_ULE:
1652 case CmpInst::FCMP_OLE:
1653 return AArch64CC::LS;
1654 case CmpInst::FCMP_ORD:
1655 return AArch64CC::VC;
1656 case CmpInst::FCMP_UNO:
1657 return AArch64CC::VS;
1658 case CmpInst::FCMP_UGE:
1659 return AArch64CC::PL;
1660 case CmpInst::ICMP_SLT:
1661 case CmpInst::FCMP_ULT:
1662 return AArch64CC::LT;
1663 case CmpInst::ICMP_SLE:
1664 case CmpInst::FCMP_ULE:
1665 return AArch64CC::LE;
1666 case CmpInst::FCMP_UNE:
1667 case CmpInst::ICMP_NE:
1668 return AArch64CC::NE;
1669 case CmpInst::ICMP_UGE:
1670 return AArch64CC::HS;
1671 case CmpInst::ICMP_ULT:
1672 return AArch64CC::LO;
1676 bool AArch64FastISel::selectBranch(const Instruction *I) {
1677 const BranchInst *BI = cast<BranchInst>(I);
1678 if (BI->isUnconditional()) {
1679 MachineBasicBlock *MSucc = FuncInfo.MBBMap[BI->getSuccessor(0)];
1680 fastEmitBranch(MSucc, BI->getDebugLoc());
1684 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1685 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1687 AArch64CC::CondCode CC = AArch64CC::NE;
1688 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1689 if (CI->hasOneUse() && isValueAvailable(CI)) {
1690 // Try to optimize or fold the cmp.
1691 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1692 switch (Predicate) {
1695 case CmpInst::FCMP_FALSE:
1696 fastEmitBranch(FBB, DbgLoc);
1698 case CmpInst::FCMP_TRUE:
1699 fastEmitBranch(TBB, DbgLoc);
1703 // Try to take advantage of fallthrough opportunities.
1704 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1705 std::swap(TBB, FBB);
1706 Predicate = CmpInst::getInversePredicate(Predicate);
1710 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1713 // FCMP_UEQ and FCMP_ONE cannot be checked with a single branch
1715 CC = getCompareCC(Predicate);
1716 AArch64CC::CondCode ExtraCC = AArch64CC::AL;
1717 switch (Predicate) {
1720 case CmpInst::FCMP_UEQ:
1721 ExtraCC = AArch64CC::EQ;
1724 case CmpInst::FCMP_ONE:
1725 ExtraCC = AArch64CC::MI;
1729 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
1731 // Emit the extra branch for FCMP_UEQ and FCMP_ONE.
1732 if (ExtraCC != AArch64CC::AL) {
1733 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1739 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1743 // Obtain the branch weight and add the TrueBB to the successor list.
1744 uint32_t BranchWeight = 0;
1746 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1747 TBB->getBasicBlock());
1748 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1750 fastEmitBranch(FBB, DbgLoc);
1753 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1755 if (TI->hasOneUse() && isValueAvailable(TI) &&
1756 isTypeSupported(TI->getOperand(0)->getType(), SrcVT)) {
1757 unsigned CondReg = getRegForValue(TI->getOperand(0));
1760 bool CondIsKill = hasTrivialKill(TI->getOperand(0));
1762 // Issue an extract_subreg to get the lower 32-bits.
1763 if (SrcVT == MVT::i64) {
1764 CondReg = fastEmitInst_extractsubreg(MVT::i32, CondReg, CondIsKill,
1769 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
1770 assert(ANDReg && "Unexpected AND instruction emission failure.");
1771 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
1773 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1774 std::swap(TBB, FBB);
1777 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1781 // Obtain the branch weight and add the TrueBB to the successor list.
1782 uint32_t BranchWeight = 0;
1784 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1785 TBB->getBasicBlock());
1786 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1788 fastEmitBranch(FBB, DbgLoc);
1791 } else if (const auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
1792 uint64_t Imm = CI->getZExtValue();
1793 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1794 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
1797 // Obtain the branch weight and add the target to the successor list.
1798 uint32_t BranchWeight = 0;
1800 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1801 Target->getBasicBlock());
1802 FuncInfo.MBB->addSuccessor(Target, BranchWeight);
1804 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
1805 // Fake request the condition, otherwise the intrinsic might be completely
1807 unsigned CondReg = getRegForValue(BI->getCondition());
1812 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1816 // Obtain the branch weight and add the TrueBB to the successor list.
1817 uint32_t BranchWeight = 0;
1819 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1820 TBB->getBasicBlock());
1821 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1823 fastEmitBranch(FBB, DbgLoc);
1827 unsigned CondReg = getRegForValue(BI->getCondition());
1830 bool CondRegIsKill = hasTrivialKill(BI->getCondition());
1832 // We've been divorced from our compare! Our block was split, and
1833 // now our compare lives in a predecessor block. We musn't
1834 // re-compare here, as the children of the compare aren't guaranteed
1835 // live across the block boundary (we *could* check for this).
1836 // Regardless, the compare has been done in the predecessor block,
1837 // and it left a value for us in a virtual register. Ergo, we test
1838 // the one-bit value left in the virtual register.
1839 emitICmp_ri(MVT::i32, CondReg, CondRegIsKill, 0);
1841 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1842 std::swap(TBB, FBB);
1846 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1850 // Obtain the branch weight and add the TrueBB to the successor list.
1851 uint32_t BranchWeight = 0;
1853 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1854 TBB->getBasicBlock());
1855 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1857 fastEmitBranch(FBB, DbgLoc);
1861 bool AArch64FastISel::selectIndirectBr(const Instruction *I) {
1862 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
1863 unsigned AddrReg = getRegForValue(BI->getOperand(0));
1867 // Emit the indirect branch.
1868 const MCInstrDesc &II = TII.get(AArch64::BR);
1869 AddrReg = constrainOperandRegClass(II, AddrReg, II.getNumDefs());
1870 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(AddrReg);
1872 // Make sure the CFG is up-to-date.
1873 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
1874 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
1879 bool AArch64FastISel::selectCmp(const Instruction *I) {
1880 const CmpInst *CI = cast<CmpInst>(I);
1882 // Try to optimize or fold the cmp.
1883 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
1884 unsigned ResultReg = 0;
1885 switch (Predicate) {
1888 case CmpInst::FCMP_FALSE:
1889 ResultReg = createResultReg(&AArch64::GPR32RegClass);
1890 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1891 TII.get(TargetOpcode::COPY), ResultReg)
1892 .addReg(AArch64::WZR, getKillRegState(true));
1894 case CmpInst::FCMP_TRUE:
1895 ResultReg = fastEmit_i(MVT::i32, MVT::i32, ISD::Constant, 1);
1900 updateValueMap(I, ResultReg);
1905 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1908 ResultReg = createResultReg(&AArch64::GPR32RegClass);
1910 // FCMP_UEQ and FCMP_ONE cannot be checked with a single instruction. These
1911 // condition codes are inverted, because they are used by CSINC.
1912 static unsigned CondCodeTable[2][2] = {
1913 { AArch64CC::NE, AArch64CC::VC },
1914 { AArch64CC::PL, AArch64CC::LE }
1916 unsigned *CondCodes = nullptr;
1917 switch (Predicate) {
1920 case CmpInst::FCMP_UEQ:
1921 CondCodes = &CondCodeTable[0][0];
1923 case CmpInst::FCMP_ONE:
1924 CondCodes = &CondCodeTable[1][0];
1929 unsigned TmpReg1 = createResultReg(&AArch64::GPR32RegClass);
1930 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1932 .addReg(AArch64::WZR, getKillRegState(true))
1933 .addReg(AArch64::WZR, getKillRegState(true))
1934 .addImm(CondCodes[0]);
1935 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1937 .addReg(TmpReg1, getKillRegState(true))
1938 .addReg(AArch64::WZR, getKillRegState(true))
1939 .addImm(CondCodes[1]);
1941 updateValueMap(I, ResultReg);
1945 // Now set a register based on the comparison.
1946 AArch64CC::CondCode CC = getCompareCC(Predicate);
1947 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
1948 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
1949 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1951 .addReg(AArch64::WZR, getKillRegState(true))
1952 .addReg(AArch64::WZR, getKillRegState(true))
1953 .addImm(invertedCC);
1955 updateValueMap(I, ResultReg);
1959 bool AArch64FastISel::selectSelect(const Instruction *I) {
1960 const SelectInst *SI = cast<SelectInst>(I);
1962 EVT DestEVT = TLI.getValueType(SI->getType(), true);
1963 if (!DestEVT.isSimple())
1966 MVT DestVT = DestEVT.getSimpleVT();
1967 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
1972 const TargetRegisterClass *RC = nullptr;
1973 switch (DestVT.SimpleTy) {
1974 default: return false;
1976 SelectOpc = AArch64::CSELWr; RC = &AArch64::GPR32RegClass; break;
1978 SelectOpc = AArch64::CSELXr; RC = &AArch64::GPR64RegClass; break;
1980 SelectOpc = AArch64::FCSELSrrr; RC = &AArch64::FPR32RegClass; break;
1982 SelectOpc = AArch64::FCSELDrrr; RC = &AArch64::FPR64RegClass; break;
1985 const Value *Cond = SI->getCondition();
1986 bool NeedTest = true;
1987 AArch64CC::CondCode CC = AArch64CC::NE;
1988 if (foldXALUIntrinsic(CC, I, Cond))
1991 unsigned CondReg = getRegForValue(Cond);
1994 bool CondIsKill = hasTrivialKill(Cond);
1997 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
1998 assert(ANDReg && "Unexpected AND instruction emission failure.");
1999 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2002 unsigned TrueReg = getRegForValue(SI->getTrueValue());
2003 bool TrueIsKill = hasTrivialKill(SI->getTrueValue());
2005 unsigned FalseReg = getRegForValue(SI->getFalseValue());
2006 bool FalseIsKill = hasTrivialKill(SI->getFalseValue());
2008 if (!TrueReg || !FalseReg)
2011 unsigned ResultReg = fastEmitInst_rri(SelectOpc, RC, TrueReg, TrueIsKill,
2012 FalseReg, FalseIsKill, CC);
2013 updateValueMap(I, ResultReg);
2017 bool AArch64FastISel::selectFPExt(const Instruction *I) {
2018 Value *V = I->getOperand(0);
2019 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
2022 unsigned Op = getRegForValue(V);
2026 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
2027 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
2028 ResultReg).addReg(Op);
2029 updateValueMap(I, ResultReg);
2033 bool AArch64FastISel::selectFPTrunc(const Instruction *I) {
2034 Value *V = I->getOperand(0);
2035 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
2038 unsigned Op = getRegForValue(V);
2042 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
2043 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
2044 ResultReg).addReg(Op);
2045 updateValueMap(I, ResultReg);
2049 // FPToUI and FPToSI
2050 bool AArch64FastISel::selectFPToInt(const Instruction *I, bool Signed) {
2052 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2055 unsigned SrcReg = getRegForValue(I->getOperand(0));
2059 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2060 if (SrcVT == MVT::f128)
2064 if (SrcVT == MVT::f64) {
2066 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
2068 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
2071 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
2073 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
2075 unsigned ResultReg = createResultReg(
2076 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
2077 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2079 updateValueMap(I, ResultReg);
2083 bool AArch64FastISel::selectIntToFP(const Instruction *I, bool Signed) {
2085 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2087 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
2088 "Unexpected value type.");
2090 unsigned SrcReg = getRegForValue(I->getOperand(0));
2093 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
2095 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2097 // Handle sign-extension.
2098 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
2100 emitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
2107 if (SrcVT == MVT::i64) {
2109 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
2111 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
2114 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
2116 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
2119 unsigned ResultReg = fastEmitInst_r(Opc, TLI.getRegClassFor(DestVT), SrcReg,
2121 updateValueMap(I, ResultReg);
2125 bool AArch64FastISel::fastLowerArguments() {
2126 if (!FuncInfo.CanLowerReturn)
2129 const Function *F = FuncInfo.Fn;
2133 CallingConv::ID CC = F->getCallingConv();
2134 if (CC != CallingConv::C)
2137 // Only handle simple cases of up to 8 GPR and FPR each.
2138 unsigned GPRCnt = 0;
2139 unsigned FPRCnt = 0;
2141 for (auto const &Arg : F->args()) {
2142 // The first argument is at index 1.
2144 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2145 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2146 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2147 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2150 Type *ArgTy = Arg.getType();
2151 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
2154 EVT ArgVT = TLI.getValueType(ArgTy);
2155 if (!ArgVT.isSimple())
2158 MVT VT = ArgVT.getSimpleVT().SimpleTy;
2159 if (VT.isFloatingPoint() && !Subtarget->hasFPARMv8())
2162 if (VT.isVector() &&
2163 (!Subtarget->hasNEON() || !Subtarget->isLittleEndian()))
2166 if (VT >= MVT::i1 && VT <= MVT::i64)
2168 else if ((VT >= MVT::f16 && VT <= MVT::f64) || VT.is64BitVector() ||
2169 VT.is128BitVector())
2174 if (GPRCnt > 8 || FPRCnt > 8)
2178 static const MCPhysReg Registers[6][8] = {
2179 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
2180 AArch64::W5, AArch64::W6, AArch64::W7 },
2181 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
2182 AArch64::X5, AArch64::X6, AArch64::X7 },
2183 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
2184 AArch64::H5, AArch64::H6, AArch64::H7 },
2185 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
2186 AArch64::S5, AArch64::S6, AArch64::S7 },
2187 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
2188 AArch64::D5, AArch64::D6, AArch64::D7 },
2189 { AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, AArch64::Q4,
2190 AArch64::Q5, AArch64::Q6, AArch64::Q7 }
2193 unsigned GPRIdx = 0;
2194 unsigned FPRIdx = 0;
2195 for (auto const &Arg : F->args()) {
2196 MVT VT = TLI.getSimpleValueType(Arg.getType());
2198 const TargetRegisterClass *RC;
2199 if (VT >= MVT::i1 && VT <= MVT::i32) {
2200 SrcReg = Registers[0][GPRIdx++];
2201 RC = &AArch64::GPR32RegClass;
2203 } else if (VT == MVT::i64) {
2204 SrcReg = Registers[1][GPRIdx++];
2205 RC = &AArch64::GPR64RegClass;
2206 } else if (VT == MVT::f16) {
2207 SrcReg = Registers[2][FPRIdx++];
2208 RC = &AArch64::FPR16RegClass;
2209 } else if (VT == MVT::f32) {
2210 SrcReg = Registers[3][FPRIdx++];
2211 RC = &AArch64::FPR32RegClass;
2212 } else if ((VT == MVT::f64) || VT.is64BitVector()) {
2213 SrcReg = Registers[4][FPRIdx++];
2214 RC = &AArch64::FPR64RegClass;
2215 } else if (VT.is128BitVector()) {
2216 SrcReg = Registers[5][FPRIdx++];
2217 RC = &AArch64::FPR128RegClass;
2219 llvm_unreachable("Unexpected value type.");
2221 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2222 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2223 // Without this, EmitLiveInCopies may eliminate the livein if its only
2224 // use is a bitcast (which isn't turned into an instruction).
2225 unsigned ResultReg = createResultReg(RC);
2226 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2227 TII.get(TargetOpcode::COPY), ResultReg)
2228 .addReg(DstReg, getKillRegState(true));
2229 updateValueMap(&Arg, ResultReg);
2234 bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
2235 SmallVectorImpl<MVT> &OutVTs,
2236 unsigned &NumBytes) {
2237 CallingConv::ID CC = CLI.CallConv;
2238 SmallVector<CCValAssign, 16> ArgLocs;
2239 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
2240 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
2242 // Get a count of how many bytes are to be pushed on the stack.
2243 NumBytes = CCInfo.getNextStackOffset();
2245 // Issue CALLSEQ_START
2246 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
2247 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
2250 // Process the args.
2251 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
2252 CCValAssign &VA = ArgLocs[i];
2253 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
2254 MVT ArgVT = OutVTs[VA.getValNo()];
2256 unsigned ArgReg = getRegForValue(ArgVal);
2260 // Handle arg promotion: SExt, ZExt, AExt.
2261 switch (VA.getLocInfo()) {
2262 case CCValAssign::Full:
2264 case CCValAssign::SExt: {
2265 MVT DestVT = VA.getLocVT();
2267 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
2272 case CCValAssign::AExt:
2273 // Intentional fall-through.
2274 case CCValAssign::ZExt: {
2275 MVT DestVT = VA.getLocVT();
2277 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
2283 llvm_unreachable("Unknown arg promotion!");
2286 // Now copy/store arg to correct locations.
2287 if (VA.isRegLoc() && !VA.needsCustom()) {
2288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2289 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
2290 CLI.OutRegs.push_back(VA.getLocReg());
2291 } else if (VA.needsCustom()) {
2292 // FIXME: Handle custom args.
2295 assert(VA.isMemLoc() && "Assuming store on stack.");
2297 // Don't emit stores for undef values.
2298 if (isa<UndefValue>(ArgVal))
2301 // Need to store on the stack.
2302 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
2304 unsigned BEAlign = 0;
2305 if (ArgSize < 8 && !Subtarget->isLittleEndian())
2306 BEAlign = 8 - ArgSize;
2309 Addr.setKind(Address::RegBase);
2310 Addr.setReg(AArch64::SP);
2311 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
2313 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
2314 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
2315 MachinePointerInfo::getStack(Addr.getOffset()),
2316 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
2318 if (!emitStore(ArgVT, ArgReg, Addr, MMO))
2325 bool AArch64FastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
2326 unsigned NumBytes) {
2327 CallingConv::ID CC = CLI.CallConv;
2329 // Issue CALLSEQ_END
2330 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
2331 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
2332 .addImm(NumBytes).addImm(0);
2334 // Now the return value.
2335 if (RetVT != MVT::isVoid) {
2336 SmallVector<CCValAssign, 16> RVLocs;
2337 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
2338 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
2340 // Only handle a single return value.
2341 if (RVLocs.size() != 1)
2344 // Copy all of the result registers out of their specified physreg.
2345 MVT CopyVT = RVLocs[0].getValVT();
2346 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
2347 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2348 TII.get(TargetOpcode::COPY), ResultReg)
2349 .addReg(RVLocs[0].getLocReg());
2350 CLI.InRegs.push_back(RVLocs[0].getLocReg());
2352 CLI.ResultReg = ResultReg;
2353 CLI.NumResultRegs = 1;
2359 bool AArch64FastISel::fastLowerCall(CallLoweringInfo &CLI) {
2360 CallingConv::ID CC = CLI.CallConv;
2361 bool IsTailCall = CLI.IsTailCall;
2362 bool IsVarArg = CLI.IsVarArg;
2363 const Value *Callee = CLI.Callee;
2364 const char *SymName = CLI.SymName;
2366 if (!Callee && !SymName)
2369 // Allow SelectionDAG isel to handle tail calls.
2373 CodeModel::Model CM = TM.getCodeModel();
2374 // Only support the small and large code model.
2375 if (CM != CodeModel::Small && CM != CodeModel::Large)
2378 // FIXME: Add large code model support for ELF.
2379 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
2382 // Let SDISel handle vararg functions.
2386 // FIXME: Only handle *simple* calls for now.
2388 if (CLI.RetTy->isVoidTy())
2389 RetVT = MVT::isVoid;
2390 else if (!isTypeLegal(CLI.RetTy, RetVT))
2393 for (auto Flag : CLI.OutFlags)
2394 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
2397 // Set up the argument vectors.
2398 SmallVector<MVT, 16> OutVTs;
2399 OutVTs.reserve(CLI.OutVals.size());
2401 for (auto *Val : CLI.OutVals) {
2403 if (!isTypeLegal(Val->getType(), VT) &&
2404 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
2407 // We don't handle vector parameters yet.
2408 if (VT.isVector() || VT.getSizeInBits() > 64)
2411 OutVTs.push_back(VT);
2415 if (Callee && !computeCallAddress(Callee, Addr))
2418 // Handle the arguments now that we've gotten them.
2420 if (!processCallArgs(CLI, OutVTs, NumBytes))
2424 MachineInstrBuilder MIB;
2425 if (CM == CodeModel::Small) {
2426 const MCInstrDesc &II = TII.get(Addr.getReg() ? AArch64::BLR : AArch64::BL);
2427 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II);
2429 MIB.addExternalSymbol(SymName, 0);
2430 else if (Addr.getGlobalValue())
2431 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
2432 else if (Addr.getReg()) {
2433 unsigned Reg = constrainOperandRegClass(II, Addr.getReg(), 0);
2438 unsigned CallReg = 0;
2440 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
2441 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
2443 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGE);
2445 CallReg = createResultReg(&AArch64::GPR64RegClass);
2446 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
2449 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
2451 } else if (Addr.getGlobalValue())
2452 CallReg = materializeGV(Addr.getGlobalValue());
2453 else if (Addr.getReg())
2454 CallReg = Addr.getReg();
2459 const MCInstrDesc &II = TII.get(AArch64::BLR);
2460 CallReg = constrainOperandRegClass(II, CallReg, 0);
2461 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(CallReg);
2464 // Add implicit physical register uses to the call.
2465 for (auto Reg : CLI.OutRegs)
2466 MIB.addReg(Reg, RegState::Implicit);
2468 // Add a register mask with the call-preserved registers.
2469 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2470 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2474 // Finish off the call including any return values.
2475 return finishCall(CLI, RetVT, NumBytes);
2478 bool AArch64FastISel::isMemCpySmall(uint64_t Len, unsigned Alignment) {
2480 return Len / Alignment <= 4;
2485 bool AArch64FastISel::tryEmitSmallMemCpy(Address Dest, Address Src,
2486 uint64_t Len, unsigned Alignment) {
2487 // Make sure we don't bloat code by inlining very large memcpy's.
2488 if (!isMemCpySmall(Len, Alignment))
2491 int64_t UnscaledOffset = 0;
2492 Address OrigDest = Dest;
2493 Address OrigSrc = Src;
2497 if (!Alignment || Alignment >= 8) {
2508 // Bound based on alignment.
2509 if (Len >= 4 && Alignment == 4)
2511 else if (Len >= 2 && Alignment == 2)
2520 RV = emitLoad(VT, ResultReg, Src);
2524 RV = emitStore(VT, ResultReg, Dest);
2528 int64_t Size = VT.getSizeInBits() / 8;
2530 UnscaledOffset += Size;
2532 // We need to recompute the unscaled offset for each iteration.
2533 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
2534 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
2540 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
2541 /// into the user. The condition code will only be updated on success.
2542 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
2543 const Instruction *I,
2544 const Value *Cond) {
2545 if (!isa<ExtractValueInst>(Cond))
2548 const auto *EV = cast<ExtractValueInst>(Cond);
2549 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
2552 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
2554 const Function *Callee = II->getCalledFunction();
2556 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
2557 if (!isTypeLegal(RetTy, RetVT))
2560 if (RetVT != MVT::i32 && RetVT != MVT::i64)
2563 AArch64CC::CondCode TmpCC;
2564 switch (II->getIntrinsicID()) {
2565 default: return false;
2566 case Intrinsic::sadd_with_overflow:
2567 case Intrinsic::ssub_with_overflow: TmpCC = AArch64CC::VS; break;
2568 case Intrinsic::uadd_with_overflow: TmpCC = AArch64CC::HS; break;
2569 case Intrinsic::usub_with_overflow: TmpCC = AArch64CC::LO; break;
2570 case Intrinsic::smul_with_overflow:
2571 case Intrinsic::umul_with_overflow: TmpCC = AArch64CC::NE; break;
2574 // Check if both instructions are in the same basic block.
2575 if (!isValueAvailable(II))
2578 // Make sure nothing is in the way
2579 BasicBlock::const_iterator Start = I;
2580 BasicBlock::const_iterator End = II;
2581 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
2582 // We only expect extractvalue instructions between the intrinsic and the
2583 // instruction to be selected.
2584 if (!isa<ExtractValueInst>(Itr))
2587 // Check that the extractvalue operand comes from the intrinsic.
2588 const auto *EVI = cast<ExtractValueInst>(Itr);
2589 if (EVI->getAggregateOperand() != II)
2597 bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
2598 // FIXME: Handle more intrinsics.
2599 switch (II->getIntrinsicID()) {
2600 default: return false;
2601 case Intrinsic::frameaddress: {
2602 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2603 MFI->setFrameAddressIsTaken(true);
2605 const AArch64RegisterInfo *RegInfo =
2606 static_cast<const AArch64RegisterInfo *>(
2607 TM.getSubtargetImpl()->getRegisterInfo());
2608 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2609 unsigned SrcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2610 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2611 TII.get(TargetOpcode::COPY), SrcReg).addReg(FramePtr);
2612 // Recursively load frame address
2618 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
2620 DestReg = fastEmitInst_ri(AArch64::LDRXui, &AArch64::GPR64RegClass,
2621 SrcReg, /*IsKill=*/true, 0);
2622 assert(DestReg && "Unexpected LDR instruction emission failure.");
2626 updateValueMap(II, SrcReg);
2629 case Intrinsic::memcpy:
2630 case Intrinsic::memmove: {
2631 const auto *MTI = cast<MemTransferInst>(II);
2632 // Don't handle volatile.
2633 if (MTI->isVolatile())
2636 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2637 // we would emit dead code because we don't currently handle memmoves.
2638 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
2639 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
2640 // Small memcpy's are common enough that we want to do them without a call
2642 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
2643 unsigned Alignment = MTI->getAlignment();
2644 if (isMemCpySmall(Len, Alignment)) {
2646 if (!computeAddress(MTI->getRawDest(), Dest) ||
2647 !computeAddress(MTI->getRawSource(), Src))
2649 if (tryEmitSmallMemCpy(Dest, Src, Len, Alignment))
2654 if (!MTI->getLength()->getType()->isIntegerTy(64))
2657 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
2658 // Fast instruction selection doesn't support the special
2662 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
2663 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
2665 case Intrinsic::memset: {
2666 const MemSetInst *MSI = cast<MemSetInst>(II);
2667 // Don't handle volatile.
2668 if (MSI->isVolatile())
2671 if (!MSI->getLength()->getType()->isIntegerTy(64))
2674 if (MSI->getDestAddressSpace() > 255)
2675 // Fast instruction selection doesn't support the special
2679 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
2681 case Intrinsic::sin:
2682 case Intrinsic::cos:
2683 case Intrinsic::pow: {
2685 if (!isTypeLegal(II->getType(), RetVT))
2688 if (RetVT != MVT::f32 && RetVT != MVT::f64)
2691 static const RTLIB::Libcall LibCallTable[3][2] = {
2692 { RTLIB::SIN_F32, RTLIB::SIN_F64 },
2693 { RTLIB::COS_F32, RTLIB::COS_F64 },
2694 { RTLIB::POW_F32, RTLIB::POW_F64 }
2697 bool Is64Bit = RetVT == MVT::f64;
2698 switch (II->getIntrinsicID()) {
2700 llvm_unreachable("Unexpected intrinsic.");
2701 case Intrinsic::sin:
2702 LC = LibCallTable[0][Is64Bit];
2704 case Intrinsic::cos:
2705 LC = LibCallTable[1][Is64Bit];
2707 case Intrinsic::pow:
2708 LC = LibCallTable[2][Is64Bit];
2713 Args.reserve(II->getNumArgOperands());
2715 // Populate the argument list.
2716 for (auto &Arg : II->arg_operands()) {
2719 Entry.Ty = Arg->getType();
2720 Args.push_back(Entry);
2723 CallLoweringInfo CLI;
2724 CLI.setCallee(TLI.getLibcallCallingConv(LC), II->getType(),
2725 TLI.getLibcallName(LC), std::move(Args));
2726 if (!lowerCallTo(CLI))
2728 updateValueMap(II, CLI.ResultReg);
2731 case Intrinsic::trap: {
2732 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
2736 case Intrinsic::sqrt: {
2737 Type *RetTy = II->getCalledFunction()->getReturnType();
2740 if (!isTypeLegal(RetTy, VT))
2743 unsigned Op0Reg = getRegForValue(II->getOperand(0));
2746 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
2748 unsigned ResultReg = fastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
2752 updateValueMap(II, ResultReg);
2755 case Intrinsic::sadd_with_overflow:
2756 case Intrinsic::uadd_with_overflow:
2757 case Intrinsic::ssub_with_overflow:
2758 case Intrinsic::usub_with_overflow:
2759 case Intrinsic::smul_with_overflow:
2760 case Intrinsic::umul_with_overflow: {
2761 // This implements the basic lowering of the xalu with overflow intrinsics.
2762 const Function *Callee = II->getCalledFunction();
2763 auto *Ty = cast<StructType>(Callee->getReturnType());
2764 Type *RetTy = Ty->getTypeAtIndex(0U);
2767 if (!isTypeLegal(RetTy, VT))
2770 if (VT != MVT::i32 && VT != MVT::i64)
2773 const Value *LHS = II->getArgOperand(0);
2774 const Value *RHS = II->getArgOperand(1);
2775 // Canonicalize immediate to the RHS.
2776 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2777 isCommutativeIntrinsic(II))
2778 std::swap(LHS, RHS);
2780 unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
2781 AArch64CC::CondCode CC = AArch64CC::Invalid;
2782 switch (II->getIntrinsicID()) {
2783 default: llvm_unreachable("Unexpected intrinsic!");
2784 case Intrinsic::sadd_with_overflow:
2785 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
2788 case Intrinsic::uadd_with_overflow:
2789 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
2792 case Intrinsic::ssub_with_overflow:
2793 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
2796 case Intrinsic::usub_with_overflow:
2797 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
2800 case Intrinsic::smul_with_overflow: {
2802 unsigned LHSReg = getRegForValue(LHS);
2805 bool LHSIsKill = hasTrivialKill(LHS);
2807 unsigned RHSReg = getRegForValue(RHS);
2810 bool RHSIsKill = hasTrivialKill(RHS);
2812 if (VT == MVT::i32) {
2813 MulReg = emitSMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2814 unsigned ShiftReg = emitLSR_ri(MVT::i64, MVT::i64, MulReg,
2815 /*IsKill=*/false, 32);
2816 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
2818 ShiftReg = fastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
2820 emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
2821 AArch64_AM::ASR, 31, /*WantResult=*/false);
2823 assert(VT == MVT::i64 && "Unexpected value type.");
2824 MulReg = emitMul_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2825 unsigned SMULHReg = fastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
2827 emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
2828 AArch64_AM::ASR, 63, /*WantResult=*/false);
2832 case Intrinsic::umul_with_overflow: {
2834 unsigned LHSReg = getRegForValue(LHS);
2837 bool LHSIsKill = hasTrivialKill(LHS);
2839 unsigned RHSReg = getRegForValue(RHS);
2842 bool RHSIsKill = hasTrivialKill(RHS);
2844 if (VT == MVT::i32) {
2845 MulReg = emitUMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2846 emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
2847 /*IsKill=*/false, AArch64_AM::LSR, 32,
2848 /*WantResult=*/false);
2849 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
2852 assert(VT == MVT::i64 && "Unexpected value type.");
2853 MulReg = emitMul_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2854 unsigned UMULHReg = fastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
2856 emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
2857 /*IsKill=*/false, /*WantResult=*/false);
2864 ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
2865 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2866 TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
2869 ResultReg2 = fastEmitInst_rri(AArch64::CSINCWr, &AArch64::GPR32RegClass,
2870 AArch64::WZR, /*IsKill=*/true, AArch64::WZR,
2871 /*IsKill=*/true, getInvertedCondCode(CC));
2872 assert((ResultReg1 + 1) == ResultReg2 &&
2873 "Nonconsecutive result registers.");
2874 updateValueMap(II, ResultReg1, 2);
2881 bool AArch64FastISel::selectRet(const Instruction *I) {
2882 const ReturnInst *Ret = cast<ReturnInst>(I);
2883 const Function &F = *I->getParent()->getParent();
2885 if (!FuncInfo.CanLowerReturn)
2891 // Build a list of return value registers.
2892 SmallVector<unsigned, 4> RetRegs;
2894 if (Ret->getNumOperands() > 0) {
2895 CallingConv::ID CC = F.getCallingConv();
2896 SmallVector<ISD::OutputArg, 4> Outs;
2897 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
2899 // Analyze operands of the call, assigning locations to each operand.
2900 SmallVector<CCValAssign, 16> ValLocs;
2901 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
2902 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
2903 : RetCC_AArch64_AAPCS;
2904 CCInfo.AnalyzeReturn(Outs, RetCC);
2906 // Only handle a single return value for now.
2907 if (ValLocs.size() != 1)
2910 CCValAssign &VA = ValLocs[0];
2911 const Value *RV = Ret->getOperand(0);
2913 // Don't bother handling odd stuff for now.
2914 if ((VA.getLocInfo() != CCValAssign::Full) &&
2915 (VA.getLocInfo() != CCValAssign::BCvt))
2918 // Only handle register returns for now.
2922 unsigned Reg = getRegForValue(RV);
2926 unsigned SrcReg = Reg + VA.getValNo();
2927 unsigned DestReg = VA.getLocReg();
2928 // Avoid a cross-class copy. This is very unlikely.
2929 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
2932 EVT RVEVT = TLI.getValueType(RV->getType());
2933 if (!RVEVT.isSimple())
2936 // Vectors (of > 1 lane) in big endian need tricky handling.
2937 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1 &&
2938 !Subtarget->isLittleEndian())
2941 MVT RVVT = RVEVT.getSimpleVT();
2942 if (RVVT == MVT::f128)
2945 MVT DestVT = VA.getValVT();
2946 // Special handling for extended integers.
2947 if (RVVT != DestVT) {
2948 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2951 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
2954 bool IsZExt = Outs[0].Flags.isZExt();
2955 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
2961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2962 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
2964 // Add register to return instruction.
2965 RetRegs.push_back(VA.getLocReg());
2968 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2969 TII.get(AArch64::RET_ReallyLR));
2970 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2971 MIB.addReg(RetRegs[i], RegState::Implicit);
2975 bool AArch64FastISel::selectTrunc(const Instruction *I) {
2976 Type *DestTy = I->getType();
2977 Value *Op = I->getOperand(0);
2978 Type *SrcTy = Op->getType();
2980 EVT SrcEVT = TLI.getValueType(SrcTy, true);
2981 EVT DestEVT = TLI.getValueType(DestTy, true);
2982 if (!SrcEVT.isSimple())
2984 if (!DestEVT.isSimple())
2987 MVT SrcVT = SrcEVT.getSimpleVT();
2988 MVT DestVT = DestEVT.getSimpleVT();
2990 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
2993 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
2997 unsigned SrcReg = getRegForValue(Op);
3000 bool SrcIsKill = hasTrivialKill(Op);
3002 // If we're truncating from i64 to a smaller non-legal type then generate an
3003 // AND. Otherwise, we know the high bits are undefined and a truncate only
3004 // generate a COPY. We cannot mark the source register also as result
3005 // register, because this can incorrectly transfer the kill flag onto the
3008 if (SrcVT == MVT::i64) {
3010 switch (DestVT.SimpleTy) {
3012 // Trunc i64 to i32 is handled by the target-independent fast-isel.
3024 // Issue an extract_subreg to get the lower 32-bits.
3025 unsigned Reg32 = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
3027 // Create the AND instruction which performs the actual truncation.
3028 ResultReg = emitAnd_ri(MVT::i32, Reg32, /*IsKill=*/true, Mask);
3029 assert(ResultReg && "Unexpected AND instruction emission failure.");
3031 ResultReg = createResultReg(&AArch64::GPR32RegClass);
3032 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3033 TII.get(TargetOpcode::COPY), ResultReg)
3034 .addReg(SrcReg, getKillRegState(SrcIsKill));
3037 updateValueMap(I, ResultReg);
3041 unsigned AArch64FastISel::emiti1Ext(unsigned SrcReg, MVT DestVT, bool IsZExt) {
3042 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
3043 DestVT == MVT::i64) &&
3044 "Unexpected value type.");
3045 // Handle i8 and i16 as i32.
3046 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3050 unsigned ResultReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
3051 assert(ResultReg && "Unexpected AND instruction emission failure.");
3052 if (DestVT == MVT::i64) {
3053 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
3054 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
3055 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3056 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3057 TII.get(AArch64::SUBREG_TO_REG), Reg64)
3060 .addImm(AArch64::sub_32);
3065 if (DestVT == MVT::i64) {
3066 // FIXME: We're SExt i1 to i64.
3069 return fastEmitInst_rii(AArch64::SBFMWri, &AArch64::GPR32RegClass, SrcReg,
3070 /*TODO:IsKill=*/false, 0, 0);
3074 unsigned AArch64FastISel::emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3075 unsigned Op1, bool Op1IsKill) {
3077 switch (RetVT.SimpleTy) {
3083 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
3085 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
3088 const TargetRegisterClass *RC =
3089 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3090 return fastEmitInst_rrr(Opc, RC, Op0, Op0IsKill, Op1, Op1IsKill,
3091 /*IsKill=*/ZReg, true);
3094 unsigned AArch64FastISel::emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3095 unsigned Op1, bool Op1IsKill) {
3096 if (RetVT != MVT::i64)
3099 return fastEmitInst_rrr(AArch64::SMADDLrrr, &AArch64::GPR64RegClass,
3100 Op0, Op0IsKill, Op1, Op1IsKill,
3101 AArch64::XZR, /*IsKill=*/true);
3104 unsigned AArch64FastISel::emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3105 unsigned Op1, bool Op1IsKill) {
3106 if (RetVT != MVT::i64)
3109 return fastEmitInst_rrr(AArch64::UMADDLrrr, &AArch64::GPR64RegClass,
3110 Op0, Op0IsKill, Op1, Op1IsKill,
3111 AArch64::XZR, /*IsKill=*/true);
3114 unsigned AArch64FastISel::emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3115 unsigned Op1Reg, bool Op1IsKill) {
3117 bool NeedTrunc = false;
3119 switch (RetVT.SimpleTy) {
3121 case MVT::i8: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xff; break;
3122 case MVT::i16: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xffff; break;
3123 case MVT::i32: Opc = AArch64::LSLVWr; break;
3124 case MVT::i64: Opc = AArch64::LSLVXr; break;
3127 const TargetRegisterClass *RC =
3128 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3130 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3133 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3136 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3140 unsigned AArch64FastISel::emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3141 bool Op0IsKill, uint64_t Shift,
3143 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3144 "Unexpected source/return type pair.");
3145 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3146 SrcVT == MVT::i64) && "Unexpected source value type.");
3147 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3148 RetVT == MVT::i64) && "Unexpected return value type.");
3150 bool Is64Bit = (RetVT == MVT::i64);
3151 unsigned RegSize = Is64Bit ? 64 : 32;
3152 unsigned DstBits = RetVT.getSizeInBits();
3153 unsigned SrcBits = SrcVT.getSizeInBits();
3155 // Don't deal with undefined shifts.
3156 if (Shift >= DstBits)
3159 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3160 // {S|U}BFM Wd, Wn, #r, #s
3161 // Wd<32+s-r,32-r> = Wn<s:0> when r > s
3163 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3164 // %2 = shl i16 %1, 4
3165 // Wd<32+7-28,32-28> = Wn<7:0> <- clamp s to 7
3166 // 0b1111_1111_1111_1111__1111_1010_1010_0000 sext
3167 // 0b0000_0000_0000_0000__0000_0101_0101_0000 sext | zext
3168 // 0b0000_0000_0000_0000__0000_1010_1010_0000 zext
3170 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3171 // %2 = shl i16 %1, 8
3172 // Wd<32+7-24,32-24> = Wn<7:0>
3173 // 0b1111_1111_1111_1111__1010_1010_0000_0000 sext
3174 // 0b0000_0000_0000_0000__0101_0101_0000_0000 sext | zext
3175 // 0b0000_0000_0000_0000__1010_1010_0000_0000 zext
3177 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3178 // %2 = shl i16 %1, 12
3179 // Wd<32+3-20,32-20> = Wn<3:0>
3180 // 0b1111_1111_1111_1111__1010_0000_0000_0000 sext
3181 // 0b0000_0000_0000_0000__0101_0000_0000_0000 sext | zext
3182 // 0b0000_0000_0000_0000__1010_0000_0000_0000 zext
3184 unsigned ImmR = RegSize - Shift;
3185 // Limit the width to the length of the source type.
3186 unsigned ImmS = std::min<unsigned>(SrcBits - 1, DstBits - 1 - Shift);
3187 static const unsigned OpcTable[2][2] = {
3188 {AArch64::SBFMWri, AArch64::SBFMXri},
3189 {AArch64::UBFMWri, AArch64::UBFMXri}
3191 unsigned Opc = OpcTable[IsZext][Is64Bit];
3192 const TargetRegisterClass *RC =
3193 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3194 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3195 unsigned TmpReg = MRI.createVirtualRegister(RC);
3196 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3197 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3199 .addReg(Op0, getKillRegState(Op0IsKill))
3200 .addImm(AArch64::sub_32);
3204 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3207 unsigned AArch64FastISel::emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3208 unsigned Op1Reg, bool Op1IsKill) {
3210 bool NeedTrunc = false;
3212 switch (RetVT.SimpleTy) {
3214 case MVT::i8: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xff; break;
3215 case MVT::i16: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xffff; break;
3216 case MVT::i32: Opc = AArch64::LSRVWr; break;
3217 case MVT::i64: Opc = AArch64::LSRVXr; break;
3220 const TargetRegisterClass *RC =
3221 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3223 Op0Reg = emitAnd_ri(MVT::i32, Op0Reg, Op0IsKill, Mask);
3224 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3225 Op0IsKill = Op1IsKill = true;
3227 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3230 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3234 unsigned AArch64FastISel::emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3235 bool Op0IsKill, uint64_t Shift,
3237 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3238 "Unexpected source/return type pair.");
3239 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3240 SrcVT == MVT::i64) && "Unexpected source value type.");
3241 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3242 RetVT == MVT::i64) && "Unexpected return value type.");
3244 bool Is64Bit = (RetVT == MVT::i64);
3245 unsigned RegSize = Is64Bit ? 64 : 32;
3246 unsigned DstBits = RetVT.getSizeInBits();
3247 unsigned SrcBits = SrcVT.getSizeInBits();
3249 // Don't deal with undefined shifts.
3250 if (Shift >= DstBits)
3253 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3254 // {S|U}BFM Wd, Wn, #r, #s
3255 // Wd<s-r:0> = Wn<s:r> when r <= s
3257 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3258 // %2 = lshr i16 %1, 4
3259 // Wd<7-4:0> = Wn<7:4>
3260 // 0b0000_0000_0000_0000__0000_1111_1111_1010 sext
3261 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
3262 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
3264 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3265 // %2 = lshr i16 %1, 8
3266 // Wd<7-7,0> = Wn<7:7>
3267 // 0b0000_0000_0000_0000__0000_0000_1111_1111 sext
3268 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3269 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3271 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3272 // %2 = lshr i16 %1, 12
3273 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
3274 // 0b0000_0000_0000_0000__0000_0000_0000_1111 sext
3275 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3276 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3278 if (Shift >= SrcBits && IsZExt)
3279 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
3281 // It is not possible to fold a sign-extend into the LShr instruction. In this
3282 // case emit a sign-extend.
3284 Op0 = emitIntExt(SrcVT, Op0, RetVT, IsZExt);
3289 SrcBits = SrcVT.getSizeInBits();
3293 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
3294 unsigned ImmS = SrcBits - 1;
3295 static const unsigned OpcTable[2][2] = {
3296 {AArch64::SBFMWri, AArch64::SBFMXri},
3297 {AArch64::UBFMWri, AArch64::UBFMXri}
3299 unsigned Opc = OpcTable[IsZExt][Is64Bit];
3300 const TargetRegisterClass *RC =
3301 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3302 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3303 unsigned TmpReg = MRI.createVirtualRegister(RC);
3304 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3305 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3307 .addReg(Op0, getKillRegState(Op0IsKill))
3308 .addImm(AArch64::sub_32);
3312 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3315 unsigned AArch64FastISel::emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3316 unsigned Op1Reg, bool Op1IsKill) {
3318 bool NeedTrunc = false;
3320 switch (RetVT.SimpleTy) {
3322 case MVT::i8: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xff; break;
3323 case MVT::i16: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xffff; break;
3324 case MVT::i32: Opc = AArch64::ASRVWr; break;
3325 case MVT::i64: Opc = AArch64::ASRVXr; break;
3328 const TargetRegisterClass *RC =
3329 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3331 Op0Reg = emitIntExt(RetVT, Op0Reg, MVT::i32, /*IsZExt=*/false);
3332 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3333 Op0IsKill = Op1IsKill = true;
3335 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3338 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3342 unsigned AArch64FastISel::emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3343 bool Op0IsKill, uint64_t Shift,
3345 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3346 "Unexpected source/return type pair.");
3347 assert((SrcVT == MVT::i8 || SrcVT == MVT::i16 || SrcVT == MVT::i32 ||
3348 SrcVT == MVT::i64) && "Unexpected source value type.");
3349 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3350 RetVT == MVT::i64) && "Unexpected return value type.");
3352 bool Is64Bit = (RetVT == MVT::i64);
3353 unsigned RegSize = Is64Bit ? 64 : 32;
3354 unsigned DstBits = RetVT.getSizeInBits();
3355 unsigned SrcBits = SrcVT.getSizeInBits();
3357 // Don't deal with undefined shifts.
3358 if (Shift >= DstBits)
3361 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3362 // {S|U}BFM Wd, Wn, #r, #s
3363 // Wd<s-r:0> = Wn<s:r> when r <= s
3365 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3366 // %2 = ashr i16 %1, 4
3367 // Wd<7-4:0> = Wn<7:4>
3368 // 0b1111_1111_1111_1111__1111_1111_1111_1010 sext
3369 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
3370 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
3372 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3373 // %2 = ashr i16 %1, 8
3374 // Wd<7-7,0> = Wn<7:7>
3375 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
3376 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3377 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3379 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3380 // %2 = ashr i16 %1, 12
3381 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
3382 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
3383 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
3384 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
3386 if (Shift >= SrcBits && IsZExt)
3387 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
3389 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
3390 unsigned ImmS = SrcBits - 1;
3391 static const unsigned OpcTable[2][2] = {
3392 {AArch64::SBFMWri, AArch64::SBFMXri},
3393 {AArch64::UBFMWri, AArch64::UBFMXri}
3395 unsigned Opc = OpcTable[IsZExt][Is64Bit];
3396 const TargetRegisterClass *RC =
3397 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3398 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3399 unsigned TmpReg = MRI.createVirtualRegister(RC);
3400 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3401 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
3403 .addReg(Op0, getKillRegState(Op0IsKill))
3404 .addImm(AArch64::sub_32);
3408 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
3411 unsigned AArch64FastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
3413 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
3415 // FastISel does not have plumbing to deal with extensions where the SrcVT or
3416 // DestVT are odd things, so test to make sure that they are both types we can
3417 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
3418 // bail out to SelectionDAG.
3419 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
3420 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
3421 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
3422 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
3428 switch (SrcVT.SimpleTy) {
3432 return emiti1Ext(SrcReg, DestVT, IsZExt);
3434 if (DestVT == MVT::i64)
3435 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3437 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
3441 if (DestVT == MVT::i64)
3442 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3444 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
3448 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
3449 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
3454 // Handle i8 and i16 as i32.
3455 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3457 else if (DestVT == MVT::i64) {
3458 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3459 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3460 TII.get(AArch64::SUBREG_TO_REG), Src64)
3463 .addImm(AArch64::sub_32);
3467 const TargetRegisterClass *RC =
3468 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3469 return fastEmitInst_rii(Opc, RC, SrcReg, /*TODO:IsKill=*/false, 0, Imm);
3472 bool AArch64FastISel::selectIntExt(const Instruction *I) {
3473 // On ARM, in general, integer casts don't involve legal types; this code
3474 // handles promotable integers. The high bits for a type smaller than
3475 // the register size are assumed to be undefined.
3476 Type *DestTy = I->getType();
3477 Value *Src = I->getOperand(0);
3478 Type *SrcTy = Src->getType();
3480 unsigned SrcReg = getRegForValue(Src);
3484 EVT SrcEVT = TLI.getValueType(SrcTy, true);
3485 EVT DestEVT = TLI.getValueType(DestTy, true);
3486 if (!SrcEVT.isSimple())
3488 if (!DestEVT.isSimple())
3491 MVT SrcVT = SrcEVT.getSimpleVT();
3492 MVT DestVT = DestEVT.getSimpleVT();
3493 unsigned ResultReg = 0;
3495 bool IsZExt = isa<ZExtInst>(I);
3496 // Check if it is an argument and if it is already zero/sign-extended.
3497 if (const auto *Arg = dyn_cast<Argument>(Src)) {
3498 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr())) {
3499 if (DestVT == MVT::i64) {
3500 ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
3501 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3502 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
3505 .addImm(AArch64::sub_32);
3512 ResultReg = emitIntExt(SrcVT, SrcReg, DestVT, IsZExt);
3517 updateValueMap(I, ResultReg);
3521 bool AArch64FastISel::selectRem(const Instruction *I, unsigned ISDOpcode) {
3522 EVT DestEVT = TLI.getValueType(I->getType(), true);
3523 if (!DestEVT.isSimple())
3526 MVT DestVT = DestEVT.getSimpleVT();
3527 if (DestVT != MVT::i64 && DestVT != MVT::i32)
3531 bool Is64bit = (DestVT == MVT::i64);
3532 switch (ISDOpcode) {
3536 DivOpc = Is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
3539 DivOpc = Is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
3542 unsigned MSubOpc = Is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
3543 unsigned Src0Reg = getRegForValue(I->getOperand(0));
3546 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
3548 unsigned Src1Reg = getRegForValue(I->getOperand(1));
3551 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
3553 const TargetRegisterClass *RC =
3554 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3555 unsigned QuotReg = fastEmitInst_rr(DivOpc, RC, Src0Reg, /*IsKill=*/false,
3556 Src1Reg, /*IsKill=*/false);
3557 assert(QuotReg && "Unexpected DIV instruction emission failure.");
3558 // The remainder is computed as numerator - (quotient * denominator) using the
3559 // MSUB instruction.
3560 unsigned ResultReg = fastEmitInst_rrr(MSubOpc, RC, QuotReg, /*IsKill=*/true,
3561 Src1Reg, Src1IsKill, Src0Reg,
3563 updateValueMap(I, ResultReg);
3567 bool AArch64FastISel::selectMul(const Instruction *I) {
3568 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType(), true);
3569 if (!SrcEVT.isSimple())
3571 MVT SrcVT = SrcEVT.getSimpleVT();
3573 // Must be simple value type. Don't handle vectors.
3574 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
3578 unsigned Src0Reg = getRegForValue(I->getOperand(0));
3581 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
3583 unsigned Src1Reg = getRegForValue(I->getOperand(1));
3586 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
3588 unsigned ResultReg =
3589 emitMul_rr(SrcVT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
3594 updateValueMap(I, ResultReg);
3598 bool AArch64FastISel::selectShift(const Instruction *I) {
3600 if (!isTypeSupported(I->getType(), RetVT, /*IsVectorAllowed=*/true))
3603 if (RetVT.isVector())
3604 return selectOperator(I, I->getOpcode());
3606 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
3607 unsigned ResultReg = 0;
3608 uint64_t ShiftVal = C->getZExtValue();
3610 bool IsZExt = (I->getOpcode() == Instruction::AShr) ? false : true;
3611 const Value *Op0 = I->getOperand(0);
3612 if (const auto *ZExt = dyn_cast<ZExtInst>(Op0)) {
3614 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), TmpVT)) {
3617 Op0 = ZExt->getOperand(0);
3619 } else if (const auto *SExt = dyn_cast<SExtInst>(Op0)) {
3621 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), TmpVT)) {
3624 Op0 = SExt->getOperand(0);
3628 unsigned Op0Reg = getRegForValue(Op0);
3631 bool Op0IsKill = hasTrivialKill(Op0);
3633 switch (I->getOpcode()) {
3634 default: llvm_unreachable("Unexpected instruction.");
3635 case Instruction::Shl:
3636 ResultReg = emitLSL_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
3638 case Instruction::AShr:
3639 ResultReg = emitASR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
3641 case Instruction::LShr:
3642 ResultReg = emitLSR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
3648 updateValueMap(I, ResultReg);
3652 unsigned Op0Reg = getRegForValue(I->getOperand(0));
3655 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
3657 unsigned Op1Reg = getRegForValue(I->getOperand(1));
3660 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
3662 unsigned ResultReg = 0;
3663 switch (I->getOpcode()) {
3664 default: llvm_unreachable("Unexpected instruction.");
3665 case Instruction::Shl:
3666 ResultReg = emitLSL_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
3668 case Instruction::AShr:
3669 ResultReg = emitASR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
3671 case Instruction::LShr:
3672 ResultReg = emitLSR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
3679 updateValueMap(I, ResultReg);
3683 bool AArch64FastISel::selectBitCast(const Instruction *I) {
3686 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
3688 if (!isTypeLegal(I->getType(), RetVT))
3692 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
3693 Opc = AArch64::FMOVWSr;
3694 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
3695 Opc = AArch64::FMOVXDr;
3696 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
3697 Opc = AArch64::FMOVSWr;
3698 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
3699 Opc = AArch64::FMOVDXr;
3703 const TargetRegisterClass *RC = nullptr;
3704 switch (RetVT.SimpleTy) {
3705 default: llvm_unreachable("Unexpected value type.");
3706 case MVT::i32: RC = &AArch64::GPR32RegClass; break;
3707 case MVT::i64: RC = &AArch64::GPR64RegClass; break;
3708 case MVT::f32: RC = &AArch64::FPR32RegClass; break;
3709 case MVT::f64: RC = &AArch64::FPR64RegClass; break;
3711 unsigned Op0Reg = getRegForValue(I->getOperand(0));
3714 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
3715 unsigned ResultReg = fastEmitInst_r(Opc, RC, Op0Reg, Op0IsKill);
3720 updateValueMap(I, ResultReg);
3724 bool AArch64FastISel::selectFRem(const Instruction *I) {
3726 if (!isTypeLegal(I->getType(), RetVT))
3730 switch (RetVT.SimpleTy) {
3734 LC = RTLIB::REM_F32;
3737 LC = RTLIB::REM_F64;
3742 Args.reserve(I->getNumOperands());
3744 // Populate the argument list.
3745 for (auto &Arg : I->operands()) {
3748 Entry.Ty = Arg->getType();
3749 Args.push_back(Entry);
3752 CallLoweringInfo CLI;
3753 CLI.setCallee(TLI.getLibcallCallingConv(LC), I->getType(),
3754 TLI.getLibcallName(LC), std::move(Args));
3755 if (!lowerCallTo(CLI))
3757 updateValueMap(I, CLI.ResultReg);
3761 bool AArch64FastISel::fastSelectInstruction(const Instruction *I) {
3762 switch (I->getOpcode()) {
3765 case Instruction::Add:
3766 case Instruction::Sub:
3767 return selectAddSub(I);
3768 case Instruction::Mul:
3769 if (!selectBinaryOp(I, ISD::MUL))
3770 return selectMul(I);
3772 case Instruction::SRem:
3773 if (!selectBinaryOp(I, ISD::SREM))
3774 return selectRem(I, ISD::SREM);
3776 case Instruction::URem:
3777 if (!selectBinaryOp(I, ISD::UREM))
3778 return selectRem(I, ISD::UREM);
3780 case Instruction::Shl:
3781 case Instruction::LShr:
3782 case Instruction::AShr:
3783 return selectShift(I);
3784 case Instruction::And:
3785 case Instruction::Or:
3786 case Instruction::Xor:
3787 return selectLogicalOp(I);
3788 case Instruction::Br:
3789 return selectBranch(I);
3790 case Instruction::IndirectBr:
3791 return selectIndirectBr(I);
3792 case Instruction::BitCast:
3793 if (!FastISel::selectBitCast(I))
3794 return selectBitCast(I);
3796 case Instruction::FPToSI:
3797 if (!selectCast(I, ISD::FP_TO_SINT))
3798 return selectFPToInt(I, /*Signed=*/true);
3800 case Instruction::FPToUI:
3801 return selectFPToInt(I, /*Signed=*/false);
3802 case Instruction::ZExt:
3803 if (!selectCast(I, ISD::ZERO_EXTEND))
3804 return selectIntExt(I);
3806 case Instruction::SExt:
3807 if (!selectCast(I, ISD::SIGN_EXTEND))
3808 return selectIntExt(I);
3810 case Instruction::Trunc:
3811 if (!selectCast(I, ISD::TRUNCATE))
3812 return selectTrunc(I);
3814 case Instruction::FPExt:
3815 return selectFPExt(I);
3816 case Instruction::FPTrunc:
3817 return selectFPTrunc(I);
3818 case Instruction::SIToFP:
3819 if (!selectCast(I, ISD::SINT_TO_FP))
3820 return selectIntToFP(I, /*Signed=*/true);
3822 case Instruction::UIToFP:
3823 return selectIntToFP(I, /*Signed=*/false);
3824 case Instruction::Load:
3825 return selectLoad(I);
3826 case Instruction::Store:
3827 return selectStore(I);
3828 case Instruction::FCmp:
3829 case Instruction::ICmp:
3830 return selectCmp(I);
3831 case Instruction::Select:
3832 return selectSelect(I);
3833 case Instruction::Ret:
3834 return selectRet(I);
3835 case Instruction::FRem:
3836 return selectFRem(I);
3839 // fall-back to target-independent instruction selection.
3840 return selectOperator(I, I->getOpcode());
3841 // Silence warnings.
3842 (void)&CC_AArch64_DarwinPCS_VarArg;
3846 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &FuncInfo,
3847 const TargetLibraryInfo *LibInfo) {
3848 return new AArch64FastISel(FuncInfo, LibInfo);