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 "AArch64CallingConvention.h"
18 #include "AArch64Subtarget.h"
19 #include "AArch64TargetMachine.h"
20 #include "MCTargetDesc/AArch64AddressingModes.h"
21 #include "llvm/Analysis/BranchProbabilityInfo.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/FastISel.h"
24 #include "llvm/CodeGen/FunctionLoweringInfo.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/IR/CallingConv.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/GetElementPtrTypeIterator.h"
34 #include "llvm/IR/GlobalAlias.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Instructions.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/Operator.h"
39 #include "llvm/MC/MCSymbol.h"
40 #include "llvm/Support/CommandLine.h"
45 class AArch64FastISel final : public FastISel {
55 AArch64_AM::ShiftExtendType ExtType;
63 const GlobalValue *GV;
66 Address() : Kind(RegBase), ExtType(AArch64_AM::InvalidShiftExtend),
67 OffsetReg(0), Shift(0), Offset(0), GV(nullptr) { Base.Reg = 0; }
68 void setKind(BaseKind K) { Kind = K; }
69 BaseKind getKind() const { return Kind; }
70 void setExtendType(AArch64_AM::ShiftExtendType E) { ExtType = E; }
71 AArch64_AM::ShiftExtendType getExtendType() const { return ExtType; }
72 bool isRegBase() const { return Kind == RegBase; }
73 bool isFIBase() const { return Kind == FrameIndexBase; }
74 void setReg(unsigned Reg) {
75 assert(isRegBase() && "Invalid base register access!");
78 unsigned getReg() const {
79 assert(isRegBase() && "Invalid base register access!");
82 void setOffsetReg(unsigned Reg) {
85 unsigned getOffsetReg() const {
88 void setFI(unsigned FI) {
89 assert(isFIBase() && "Invalid base frame index access!");
92 unsigned getFI() const {
93 assert(isFIBase() && "Invalid base frame index access!");
96 void setOffset(int64_t O) { Offset = O; }
97 int64_t getOffset() { return Offset; }
98 void setShift(unsigned S) { Shift = S; }
99 unsigned getShift() { return Shift; }
101 void setGlobalValue(const GlobalValue *G) { GV = G; }
102 const GlobalValue *getGlobalValue() { return GV; }
105 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const AArch64Subtarget *Subtarget;
108 LLVMContext *Context;
110 bool fastLowerArguments() override;
111 bool fastLowerCall(CallLoweringInfo &CLI) override;
112 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
115 // Selection routines.
116 bool selectAddSub(const Instruction *I);
117 bool selectLogicalOp(const Instruction *I);
118 bool selectLoad(const Instruction *I);
119 bool selectStore(const Instruction *I);
120 bool selectBranch(const Instruction *I);
121 bool selectIndirectBr(const Instruction *I);
122 bool selectCmp(const Instruction *I);
123 bool selectSelect(const Instruction *I);
124 bool selectFPExt(const Instruction *I);
125 bool selectFPTrunc(const Instruction *I);
126 bool selectFPToInt(const Instruction *I, bool Signed);
127 bool selectIntToFP(const Instruction *I, bool Signed);
128 bool selectRem(const Instruction *I, unsigned ISDOpcode);
129 bool selectRet(const Instruction *I);
130 bool selectTrunc(const Instruction *I);
131 bool selectIntExt(const Instruction *I);
132 bool selectMul(const Instruction *I);
133 bool selectShift(const Instruction *I);
134 bool selectBitCast(const Instruction *I);
135 bool selectFRem(const Instruction *I);
136 bool selectSDiv(const Instruction *I);
137 bool selectGetElementPtr(const Instruction *I);
139 // Utility helper routines.
140 bool isTypeLegal(Type *Ty, MVT &VT);
141 bool isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed = false);
142 bool isValueAvailable(const Value *V) const;
143 bool computeAddress(const Value *Obj, Address &Addr, Type *Ty = nullptr);
144 bool computeCallAddress(const Value *V, Address &Addr);
145 bool simplifyAddress(Address &Addr, MVT VT);
146 void addLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
147 unsigned Flags, unsigned ScaleFactor,
148 MachineMemOperand *MMO);
149 bool isMemCpySmall(uint64_t Len, unsigned Alignment);
150 bool tryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
152 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
154 bool optimizeIntExtLoad(const Instruction *I, MVT RetVT, MVT SrcVT);
155 bool optimizeSelect(const SelectInst *SI);
156 std::pair<unsigned, bool> getRegForGEPIndex(const Value *Idx);
158 // Emit helper routines.
159 unsigned emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
160 const Value *RHS, bool SetFlags = false,
161 bool WantResult = true, bool IsZExt = false);
162 unsigned emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
163 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
164 bool SetFlags = false, bool WantResult = true);
165 unsigned emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
166 bool LHSIsKill, uint64_t Imm, bool SetFlags = false,
167 bool WantResult = true);
168 unsigned emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
169 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
170 AArch64_AM::ShiftExtendType ShiftType,
171 uint64_t ShiftImm, bool SetFlags = false,
172 bool WantResult = true);
173 unsigned emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
174 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
175 AArch64_AM::ShiftExtendType ExtType,
176 uint64_t ShiftImm, bool SetFlags = false,
177 bool WantResult = true);
180 bool emitCompareAndBranch(const BranchInst *BI);
181 bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
182 bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
183 bool emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
184 bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
185 unsigned emitLoad(MVT VT, MVT ResultVT, Address Addr, bool WantZExt = true,
186 MachineMemOperand *MMO = nullptr);
187 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
188 MachineMemOperand *MMO = nullptr);
189 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
190 unsigned emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
191 unsigned emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
192 bool SetFlags = false, bool WantResult = true,
193 bool IsZExt = false);
194 unsigned emitAdd_ri_(MVT VT, unsigned Op0, bool Op0IsKill, int64_t Imm);
195 unsigned emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
196 bool SetFlags = false, bool WantResult = true,
197 bool IsZExt = false);
198 unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
199 unsigned RHSReg, bool RHSIsKill, bool WantResult = true);
200 unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
201 unsigned RHSReg, bool RHSIsKill,
202 AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
203 bool WantResult = true);
204 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
206 unsigned emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
207 bool LHSIsKill, uint64_t Imm);
208 unsigned emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
209 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
211 unsigned emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
212 unsigned emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
213 unsigned Op1, bool Op1IsKill);
214 unsigned emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
215 unsigned Op1, bool Op1IsKill);
216 unsigned emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
217 unsigned Op1, bool Op1IsKill);
218 unsigned emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
219 unsigned Op1Reg, bool Op1IsKill);
220 unsigned emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
221 uint64_t Imm, bool IsZExt = true);
222 unsigned emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
223 unsigned Op1Reg, bool Op1IsKill);
224 unsigned emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
225 uint64_t Imm, bool IsZExt = true);
226 unsigned emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
227 unsigned Op1Reg, bool Op1IsKill);
228 unsigned emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
229 uint64_t Imm, bool IsZExt = false);
231 unsigned materializeInt(const ConstantInt *CI, MVT VT);
232 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
233 unsigned materializeGV(const GlobalValue *GV);
235 // Call handling routines.
237 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
238 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
240 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
243 // Backend specific FastISel code.
244 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
245 unsigned fastMaterializeConstant(const Constant *C) override;
246 unsigned fastMaterializeFloatZero(const ConstantFP* CF) override;
248 explicit AArch64FastISel(FunctionLoweringInfo &FuncInfo,
249 const TargetLibraryInfo *LibInfo)
250 : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
252 &static_cast<const AArch64Subtarget &>(FuncInfo.MF->getSubtarget());
253 Context = &FuncInfo.Fn->getContext();
256 bool fastSelectInstruction(const Instruction *I) override;
258 #include "AArch64GenFastISel.inc"
261 } // end anonymous namespace
263 #include "AArch64GenCallingConv.inc"
265 /// \brief Check if the sign-/zero-extend will be a noop.
266 static bool isIntExtFree(const Instruction *I) {
267 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
268 "Unexpected integer extend instruction.");
269 assert(!I->getType()->isVectorTy() && I->getType()->isIntegerTy() &&
270 "Unexpected value type.");
271 bool IsZExt = isa<ZExtInst>(I);
273 if (const auto *LI = dyn_cast<LoadInst>(I->getOperand(0)))
277 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0)))
278 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr()))
284 /// \brief Determine the implicit scale factor that is applied by a memory
285 /// operation for a given value type.
286 static unsigned getImplicitScaleFactor(MVT VT) {
287 switch (VT.SimpleTy) {
290 case MVT::i1: // fall-through
295 case MVT::i32: // fall-through
298 case MVT::i64: // fall-through
304 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
305 if (CC == CallingConv::WebKit_JS)
306 return CC_AArch64_WebKit_JS;
307 if (CC == CallingConv::GHC)
308 return CC_AArch64_GHC;
309 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
312 unsigned AArch64FastISel::fastMaterializeAlloca(const AllocaInst *AI) {
313 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i64 &&
314 "Alloca should always return a pointer.");
316 // Don't handle dynamic allocas.
317 if (!FuncInfo.StaticAllocaMap.count(AI))
320 DenseMap<const AllocaInst *, int>::iterator SI =
321 FuncInfo.StaticAllocaMap.find(AI);
323 if (SI != FuncInfo.StaticAllocaMap.end()) {
324 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
325 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
327 .addFrameIndex(SI->second)
336 unsigned AArch64FastISel::materializeInt(const ConstantInt *CI, MVT VT) {
341 return fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
343 // Create a copy from the zero register to materialize a "0" value.
344 const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
345 : &AArch64::GPR32RegClass;
346 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
347 unsigned ResultReg = createResultReg(RC);
348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
349 ResultReg).addReg(ZeroReg, getKillRegState(true));
353 unsigned AArch64FastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
354 // Positive zero (+0.0) has to be materialized with a fmov from the zero
355 // register, because the immediate version of fmov cannot encode zero.
356 if (CFP->isNullValue())
357 return fastMaterializeFloatZero(CFP);
359 if (VT != MVT::f32 && VT != MVT::f64)
362 const APFloat Val = CFP->getValueAPF();
363 bool Is64Bit = (VT == MVT::f64);
364 // This checks to see if we can use FMOV instructions to materialize
365 // a constant, otherwise we have to materialize via the constant pool.
366 if (TLI.isFPImmLegal(Val, VT)) {
368 Is64Bit ? AArch64_AM::getFP64Imm(Val) : AArch64_AM::getFP32Imm(Val);
369 assert((Imm != -1) && "Cannot encode floating-point constant.");
370 unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
371 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
374 // For the MachO large code model materialize the FP constant in code.
375 if (Subtarget->isTargetMachO() && TM.getCodeModel() == CodeModel::Large) {
376 unsigned Opc1 = Is64Bit ? AArch64::MOVi64imm : AArch64::MOVi32imm;
377 const TargetRegisterClass *RC = Is64Bit ?
378 &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
380 unsigned TmpReg = createResultReg(RC);
381 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc1), TmpReg)
382 .addImm(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
384 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
385 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
386 TII.get(TargetOpcode::COPY), ResultReg)
387 .addReg(TmpReg, getKillRegState(true));
392 // Materialize via constant pool. MachineConstantPool wants an explicit
394 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
396 Align = DL.getTypeAllocSize(CFP->getType());
398 unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
399 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
400 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
401 ADRPReg).addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
403 unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
404 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
405 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
407 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
411 unsigned AArch64FastISel::materializeGV(const GlobalValue *GV) {
412 // We can't handle thread-local variables quickly yet.
413 if (GV->isThreadLocal())
416 // MachO still uses GOT for large code-model accesses, but ELF requires
417 // movz/movk sequences, which FastISel doesn't handle yet.
418 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
421 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
423 EVT DestEVT = TLI.getValueType(DL, GV->getType(), true);
424 if (!DestEVT.isSimple())
427 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
430 if (OpFlags & AArch64II::MO_GOT) {
432 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
434 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
436 ResultReg = createResultReg(&AArch64::GPR64RegClass);
437 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
440 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
442 } else if (OpFlags & AArch64II::MO_CONSTPOOL) {
443 // We can't handle addresses loaded from a constant pool quickly yet.
447 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
449 .addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
451 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
452 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
455 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
461 unsigned AArch64FastISel::fastMaterializeConstant(const Constant *C) {
462 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
464 // Only handle simple types.
465 if (!CEVT.isSimple())
467 MVT VT = CEVT.getSimpleVT();
469 if (const auto *CI = dyn_cast<ConstantInt>(C))
470 return materializeInt(CI, VT);
471 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
472 return materializeFP(CFP, VT);
473 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
474 return materializeGV(GV);
479 unsigned AArch64FastISel::fastMaterializeFloatZero(const ConstantFP* CFP) {
480 assert(CFP->isNullValue() &&
481 "Floating-point constant is not a positive zero.");
483 if (!isTypeLegal(CFP->getType(), VT))
486 if (VT != MVT::f32 && VT != MVT::f64)
489 bool Is64Bit = (VT == MVT::f64);
490 unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
491 unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
492 return fastEmitInst_r(Opc, TLI.getRegClassFor(VT), ZReg, /*IsKill=*/true);
495 /// \brief Check if the multiply is by a power-of-2 constant.
496 static bool isMulPowOf2(const Value *I) {
497 if (const auto *MI = dyn_cast<MulOperator>(I)) {
498 if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(0)))
499 if (C->getValue().isPowerOf2())
501 if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(1)))
502 if (C->getValue().isPowerOf2())
508 // Computes the address to get to an object.
509 bool AArch64FastISel::computeAddress(const Value *Obj, Address &Addr, Type *Ty)
511 const User *U = nullptr;
512 unsigned Opcode = Instruction::UserOp1;
513 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
514 // Don't walk into other basic blocks unless the object is an alloca from
515 // another block, otherwise it may not have a virtual register assigned.
516 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
517 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
518 Opcode = I->getOpcode();
521 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
522 Opcode = C->getOpcode();
526 if (auto *Ty = dyn_cast<PointerType>(Obj->getType()))
527 if (Ty->getAddressSpace() > 255)
528 // Fast instruction selection doesn't support the special
535 case Instruction::BitCast: {
536 // Look through bitcasts.
537 return computeAddress(U->getOperand(0), Addr, Ty);
539 case Instruction::IntToPtr: {
540 // Look past no-op inttoptrs.
541 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
542 TLI.getPointerTy(DL))
543 return computeAddress(U->getOperand(0), Addr, Ty);
546 case Instruction::PtrToInt: {
547 // Look past no-op ptrtoints.
548 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
549 return computeAddress(U->getOperand(0), Addr, Ty);
552 case Instruction::GetElementPtr: {
553 Address SavedAddr = Addr;
554 uint64_t TmpOffset = Addr.getOffset();
556 // Iterate through the GEP folding the constants into offsets where
558 gep_type_iterator GTI = gep_type_begin(U);
559 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
561 const Value *Op = *i;
562 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
563 const StructLayout *SL = DL.getStructLayout(STy);
564 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
565 TmpOffset += SL->getElementOffset(Idx);
567 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
569 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
570 // Constant-offset addressing.
571 TmpOffset += CI->getSExtValue() * S;
574 if (canFoldAddIntoGEP(U, Op)) {
575 // A compatible add with a constant operand. Fold the constant.
577 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
578 TmpOffset += CI->getSExtValue() * S;
579 // Iterate on the other operand.
580 Op = cast<AddOperator>(Op)->getOperand(0);
584 goto unsupported_gep;
589 // Try to grab the base operand now.
590 Addr.setOffset(TmpOffset);
591 if (computeAddress(U->getOperand(0), Addr, Ty))
594 // We failed, restore everything and try the other options.
600 case Instruction::Alloca: {
601 const AllocaInst *AI = cast<AllocaInst>(Obj);
602 DenseMap<const AllocaInst *, int>::iterator SI =
603 FuncInfo.StaticAllocaMap.find(AI);
604 if (SI != FuncInfo.StaticAllocaMap.end()) {
605 Addr.setKind(Address::FrameIndexBase);
606 Addr.setFI(SI->second);
611 case Instruction::Add: {
612 // Adds of constants are common and easy enough.
613 const Value *LHS = U->getOperand(0);
614 const Value *RHS = U->getOperand(1);
616 if (isa<ConstantInt>(LHS))
619 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
620 Addr.setOffset(Addr.getOffset() + CI->getSExtValue());
621 return computeAddress(LHS, Addr, Ty);
624 Address Backup = Addr;
625 if (computeAddress(LHS, Addr, Ty) && computeAddress(RHS, Addr, Ty))
631 case Instruction::Sub: {
632 // Subs of constants are common and easy enough.
633 const Value *LHS = U->getOperand(0);
634 const Value *RHS = U->getOperand(1);
636 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
637 Addr.setOffset(Addr.getOffset() - CI->getSExtValue());
638 return computeAddress(LHS, Addr, Ty);
642 case Instruction::Shl: {
643 if (Addr.getOffsetReg())
646 const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1));
650 unsigned Val = CI->getZExtValue();
651 if (Val < 1 || Val > 3)
654 uint64_t NumBytes = 0;
655 if (Ty && Ty->isSized()) {
656 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
657 NumBytes = NumBits / 8;
658 if (!isPowerOf2_64(NumBits))
662 if (NumBytes != (1ULL << Val))
666 Addr.setExtendType(AArch64_AM::LSL);
668 const Value *Src = U->getOperand(0);
669 if (const auto *I = dyn_cast<Instruction>(Src)) {
670 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
671 // Fold the zext or sext when it won't become a noop.
672 if (const auto *ZE = dyn_cast<ZExtInst>(I)) {
673 if (!isIntExtFree(ZE) &&
674 ZE->getOperand(0)->getType()->isIntegerTy(32)) {
675 Addr.setExtendType(AArch64_AM::UXTW);
676 Src = ZE->getOperand(0);
678 } else if (const auto *SE = dyn_cast<SExtInst>(I)) {
679 if (!isIntExtFree(SE) &&
680 SE->getOperand(0)->getType()->isIntegerTy(32)) {
681 Addr.setExtendType(AArch64_AM::SXTW);
682 Src = SE->getOperand(0);
688 if (const auto *AI = dyn_cast<BinaryOperator>(Src))
689 if (AI->getOpcode() == Instruction::And) {
690 const Value *LHS = AI->getOperand(0);
691 const Value *RHS = AI->getOperand(1);
693 if (const auto *C = dyn_cast<ConstantInt>(LHS))
694 if (C->getValue() == 0xffffffff)
697 if (const auto *C = dyn_cast<ConstantInt>(RHS))
698 if (C->getValue() == 0xffffffff) {
699 Addr.setExtendType(AArch64_AM::UXTW);
700 unsigned Reg = getRegForValue(LHS);
703 bool RegIsKill = hasTrivialKill(LHS);
704 Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
706 Addr.setOffsetReg(Reg);
711 unsigned Reg = getRegForValue(Src);
714 Addr.setOffsetReg(Reg);
717 case Instruction::Mul: {
718 if (Addr.getOffsetReg())
724 const Value *LHS = U->getOperand(0);
725 const Value *RHS = U->getOperand(1);
727 // Canonicalize power-of-2 value to the RHS.
728 if (const auto *C = dyn_cast<ConstantInt>(LHS))
729 if (C->getValue().isPowerOf2())
732 assert(isa<ConstantInt>(RHS) && "Expected an ConstantInt.");
733 const auto *C = cast<ConstantInt>(RHS);
734 unsigned Val = C->getValue().logBase2();
735 if (Val < 1 || Val > 3)
738 uint64_t NumBytes = 0;
739 if (Ty && Ty->isSized()) {
740 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
741 NumBytes = NumBits / 8;
742 if (!isPowerOf2_64(NumBits))
746 if (NumBytes != (1ULL << Val))
750 Addr.setExtendType(AArch64_AM::LSL);
752 const Value *Src = LHS;
753 if (const auto *I = dyn_cast<Instruction>(Src)) {
754 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
755 // Fold the zext or sext when it won't become a noop.
756 if (const auto *ZE = dyn_cast<ZExtInst>(I)) {
757 if (!isIntExtFree(ZE) &&
758 ZE->getOperand(0)->getType()->isIntegerTy(32)) {
759 Addr.setExtendType(AArch64_AM::UXTW);
760 Src = ZE->getOperand(0);
762 } else if (const auto *SE = dyn_cast<SExtInst>(I)) {
763 if (!isIntExtFree(SE) &&
764 SE->getOperand(0)->getType()->isIntegerTy(32)) {
765 Addr.setExtendType(AArch64_AM::SXTW);
766 Src = SE->getOperand(0);
772 unsigned Reg = getRegForValue(Src);
775 Addr.setOffsetReg(Reg);
778 case Instruction::And: {
779 if (Addr.getOffsetReg())
782 if (!Ty || DL.getTypeSizeInBits(Ty) != 8)
785 const Value *LHS = U->getOperand(0);
786 const Value *RHS = U->getOperand(1);
788 if (const auto *C = dyn_cast<ConstantInt>(LHS))
789 if (C->getValue() == 0xffffffff)
792 if (const auto *C = dyn_cast<ConstantInt>(RHS))
793 if (C->getValue() == 0xffffffff) {
795 Addr.setExtendType(AArch64_AM::LSL);
796 Addr.setExtendType(AArch64_AM::UXTW);
798 unsigned Reg = getRegForValue(LHS);
801 bool RegIsKill = hasTrivialKill(LHS);
802 Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
804 Addr.setOffsetReg(Reg);
809 case Instruction::SExt:
810 case Instruction::ZExt: {
811 if (!Addr.getReg() || Addr.getOffsetReg())
814 const Value *Src = nullptr;
815 // Fold the zext or sext when it won't become a noop.
816 if (const auto *ZE = dyn_cast<ZExtInst>(U)) {
817 if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
818 Addr.setExtendType(AArch64_AM::UXTW);
819 Src = ZE->getOperand(0);
821 } else if (const auto *SE = dyn_cast<SExtInst>(U)) {
822 if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
823 Addr.setExtendType(AArch64_AM::SXTW);
824 Src = SE->getOperand(0);
832 unsigned Reg = getRegForValue(Src);
835 Addr.setOffsetReg(Reg);
840 if (Addr.isRegBase() && !Addr.getReg()) {
841 unsigned Reg = getRegForValue(Obj);
848 if (!Addr.getOffsetReg()) {
849 unsigned Reg = getRegForValue(Obj);
852 Addr.setOffsetReg(Reg);
859 bool AArch64FastISel::computeCallAddress(const Value *V, Address &Addr) {
860 const User *U = nullptr;
861 unsigned Opcode = Instruction::UserOp1;
864 if (const auto *I = dyn_cast<Instruction>(V)) {
865 Opcode = I->getOpcode();
867 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
868 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
869 Opcode = C->getOpcode();
875 case Instruction::BitCast:
876 // Look past bitcasts if its operand is in the same BB.
878 return computeCallAddress(U->getOperand(0), Addr);
880 case Instruction::IntToPtr:
881 // Look past no-op inttoptrs if its operand is in the same BB.
883 TLI.getValueType(DL, U->getOperand(0)->getType()) ==
884 TLI.getPointerTy(DL))
885 return computeCallAddress(U->getOperand(0), Addr);
887 case Instruction::PtrToInt:
888 // Look past no-op ptrtoints if its operand is in the same BB.
889 if (InMBB && TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
890 return computeCallAddress(U->getOperand(0), Addr);
894 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
895 Addr.setGlobalValue(GV);
899 // If all else fails, try to materialize the value in a register.
900 if (!Addr.getGlobalValue()) {
901 Addr.setReg(getRegForValue(V));
902 return Addr.getReg() != 0;
909 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
910 EVT evt = TLI.getValueType(DL, Ty, true);
912 // Only handle simple types.
913 if (evt == MVT::Other || !evt.isSimple())
915 VT = evt.getSimpleVT();
917 // This is a legal type, but it's not something we handle in fast-isel.
921 // Handle all other legal types, i.e. a register that will directly hold this
923 return TLI.isTypeLegal(VT);
926 /// \brief Determine if the value type is supported by FastISel.
928 /// FastISel for AArch64 can handle more value types than are legal. This adds
929 /// simple value type such as i1, i8, and i16.
930 bool AArch64FastISel::isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed) {
931 if (Ty->isVectorTy() && !IsVectorAllowed)
934 if (isTypeLegal(Ty, VT))
937 // If this is a type than can be sign or zero-extended to a basic operation
938 // go ahead and accept it now.
939 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
945 bool AArch64FastISel::isValueAvailable(const Value *V) const {
946 if (!isa<Instruction>(V))
949 const auto *I = cast<Instruction>(V);
950 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
956 bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) {
957 unsigned ScaleFactor = getImplicitScaleFactor(VT);
961 bool ImmediateOffsetNeedsLowering = false;
962 bool RegisterOffsetNeedsLowering = false;
963 int64_t Offset = Addr.getOffset();
964 if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
965 ImmediateOffsetNeedsLowering = true;
966 else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
967 !isUInt<12>(Offset / ScaleFactor))
968 ImmediateOffsetNeedsLowering = true;
970 // Cannot encode an offset register and an immediate offset in the same
971 // instruction. Fold the immediate offset into the load/store instruction and
972 // emit an additional add to take care of the offset register.
973 if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.getOffsetReg())
974 RegisterOffsetNeedsLowering = true;
976 // Cannot encode zero register as base.
977 if (Addr.isRegBase() && Addr.getOffsetReg() && !Addr.getReg())
978 RegisterOffsetNeedsLowering = true;
980 // If this is a stack pointer and the offset needs to be simplified then put
981 // the alloca address into a register, set the base type back to register and
982 // continue. This should almost never happen.
983 if ((ImmediateOffsetNeedsLowering || Addr.getOffsetReg()) && Addr.isFIBase())
985 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
986 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
988 .addFrameIndex(Addr.getFI())
991 Addr.setKind(Address::RegBase);
992 Addr.setReg(ResultReg);
995 if (RegisterOffsetNeedsLowering) {
996 unsigned ResultReg = 0;
998 if (Addr.getExtendType() == AArch64_AM::SXTW ||
999 Addr.getExtendType() == AArch64_AM::UXTW )
1000 ResultReg = emitAddSub_rx(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
1001 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
1002 /*TODO:IsKill=*/false, Addr.getExtendType(),
1005 ResultReg = emitAddSub_rs(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
1006 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
1007 /*TODO:IsKill=*/false, AArch64_AM::LSL,
1010 if (Addr.getExtendType() == AArch64_AM::UXTW)
1011 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
1012 /*Op0IsKill=*/false, Addr.getShift(),
1014 else if (Addr.getExtendType() == AArch64_AM::SXTW)
1015 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
1016 /*Op0IsKill=*/false, Addr.getShift(),
1019 ResultReg = emitLSL_ri(MVT::i64, MVT::i64, Addr.getOffsetReg(),
1020 /*Op0IsKill=*/false, Addr.getShift());
1025 Addr.setReg(ResultReg);
1026 Addr.setOffsetReg(0);
1028 Addr.setExtendType(AArch64_AM::InvalidShiftExtend);
1031 // Since the offset is too large for the load/store instruction get the
1032 // reg+offset into a register.
1033 if (ImmediateOffsetNeedsLowering) {
1036 // Try to fold the immediate into the add instruction.
1037 ResultReg = emitAdd_ri_(MVT::i64, Addr.getReg(), /*IsKill=*/false, Offset);
1039 ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
1043 Addr.setReg(ResultReg);
1049 void AArch64FastISel::addLoadStoreOperands(Address &Addr,
1050 const MachineInstrBuilder &MIB,
1052 unsigned ScaleFactor,
1053 MachineMemOperand *MMO) {
1054 int64_t Offset = Addr.getOffset() / ScaleFactor;
1055 // Frame base works a bit differently. Handle it separately.
1056 if (Addr.isFIBase()) {
1057 int FI = Addr.getFI();
1058 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
1059 // and alignment should be based on the VT.
1060 MMO = FuncInfo.MF->getMachineMemOperand(
1061 MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags,
1062 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
1063 // Now add the rest of the operands.
1064 MIB.addFrameIndex(FI).addImm(Offset);
1066 assert(Addr.isRegBase() && "Unexpected address kind.");
1067 const MCInstrDesc &II = MIB->getDesc();
1068 unsigned Idx = (Flags & MachineMemOperand::MOStore) ? 1 : 0;
1070 constrainOperandRegClass(II, Addr.getReg(), II.getNumDefs()+Idx));
1072 constrainOperandRegClass(II, Addr.getOffsetReg(), II.getNumDefs()+Idx+1));
1073 if (Addr.getOffsetReg()) {
1074 assert(Addr.getOffset() == 0 && "Unexpected offset");
1075 bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
1076 Addr.getExtendType() == AArch64_AM::SXTX;
1077 MIB.addReg(Addr.getReg());
1078 MIB.addReg(Addr.getOffsetReg());
1079 MIB.addImm(IsSigned);
1080 MIB.addImm(Addr.getShift() != 0);
1082 MIB.addReg(Addr.getReg()).addImm(Offset);
1086 MIB.addMemOperand(MMO);
1089 unsigned AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
1090 const Value *RHS, bool SetFlags,
1091 bool WantResult, bool IsZExt) {
1092 AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
1093 bool NeedExtend = false;
1094 switch (RetVT.SimpleTy) {
1102 ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
1106 ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
1108 case MVT::i32: // fall-through
1113 RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
1115 // Canonicalize immediates to the RHS first.
1116 if (UseAdd && isa<Constant>(LHS) && !isa<Constant>(RHS))
1117 std::swap(LHS, RHS);
1119 // Canonicalize mul by power of 2 to the RHS.
1120 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1121 if (isMulPowOf2(LHS))
1122 std::swap(LHS, RHS);
1124 // Canonicalize shift immediate to the RHS.
1125 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1126 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
1127 if (isa<ConstantInt>(SI->getOperand(1)))
1128 if (SI->getOpcode() == Instruction::Shl ||
1129 SI->getOpcode() == Instruction::LShr ||
1130 SI->getOpcode() == Instruction::AShr )
1131 std::swap(LHS, RHS);
1133 unsigned LHSReg = getRegForValue(LHS);
1136 bool LHSIsKill = hasTrivialKill(LHS);
1139 LHSReg = emitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
1141 unsigned ResultReg = 0;
1142 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1143 uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
1144 if (C->isNegative())
1145 ResultReg = emitAddSub_ri(!UseAdd, RetVT, LHSReg, LHSIsKill, -Imm,
1146 SetFlags, WantResult);
1148 ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, Imm, SetFlags,
1150 } else if (const auto *C = dyn_cast<Constant>(RHS))
1151 if (C->isNullValue())
1152 ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, 0, SetFlags,
1158 // Only extend the RHS within the instruction if there is a valid extend type.
1159 if (ExtendType != AArch64_AM::InvalidShiftExtend && RHS->hasOneUse() &&
1160 isValueAvailable(RHS)) {
1161 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
1162 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
1163 if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
1164 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1167 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1168 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1169 RHSIsKill, ExtendType, C->getZExtValue(),
1170 SetFlags, WantResult);
1172 unsigned RHSReg = getRegForValue(RHS);
1175 bool RHSIsKill = hasTrivialKill(RHS);
1176 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1177 ExtendType, 0, SetFlags, WantResult);
1180 // Check if the mul can be folded into the instruction.
1181 if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1182 if (isMulPowOf2(RHS)) {
1183 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1184 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1186 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1187 if (C->getValue().isPowerOf2())
1188 std::swap(MulLHS, MulRHS);
1190 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1191 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1192 unsigned RHSReg = getRegForValue(MulLHS);
1195 bool RHSIsKill = hasTrivialKill(MulLHS);
1196 ResultReg = emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1197 RHSIsKill, AArch64_AM::LSL, ShiftVal, SetFlags,
1204 // Check if the shift can be folded into the instruction.
1205 if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1206 if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
1207 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1208 AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
1209 switch (SI->getOpcode()) {
1211 case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
1212 case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
1213 case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
1215 uint64_t ShiftVal = C->getZExtValue();
1216 if (ShiftType != AArch64_AM::InvalidShiftExtend) {
1217 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1220 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1221 ResultReg = emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1222 RHSIsKill, ShiftType, ShiftVal, SetFlags,
1231 unsigned RHSReg = getRegForValue(RHS);
1234 bool RHSIsKill = hasTrivialKill(RHS);
1237 RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
1239 return emitAddSub_rr(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1240 SetFlags, WantResult);
1243 unsigned AArch64FastISel::emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
1244 bool LHSIsKill, unsigned RHSReg,
1245 bool RHSIsKill, bool SetFlags,
1247 assert(LHSReg && RHSReg && "Invalid register number.");
1249 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1252 static const unsigned OpcTable[2][2][2] = {
1253 { { AArch64::SUBWrr, AArch64::SUBXrr },
1254 { AArch64::ADDWrr, AArch64::ADDXrr } },
1255 { { AArch64::SUBSWrr, AArch64::SUBSXrr },
1256 { AArch64::ADDSWrr, AArch64::ADDSXrr } }
1258 bool Is64Bit = RetVT == MVT::i64;
1259 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1260 const TargetRegisterClass *RC =
1261 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1264 ResultReg = createResultReg(RC);
1266 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1268 const MCInstrDesc &II = TII.get(Opc);
1269 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1270 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1271 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1272 .addReg(LHSReg, getKillRegState(LHSIsKill))
1273 .addReg(RHSReg, getKillRegState(RHSIsKill));
1277 unsigned AArch64FastISel::emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
1278 bool LHSIsKill, uint64_t Imm,
1279 bool SetFlags, bool WantResult) {
1280 assert(LHSReg && "Invalid register number.");
1282 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1286 if (isUInt<12>(Imm))
1288 else if ((Imm & 0xfff000) == Imm) {
1294 static const unsigned OpcTable[2][2][2] = {
1295 { { AArch64::SUBWri, AArch64::SUBXri },
1296 { AArch64::ADDWri, AArch64::ADDXri } },
1297 { { AArch64::SUBSWri, AArch64::SUBSXri },
1298 { AArch64::ADDSWri, AArch64::ADDSXri } }
1300 bool Is64Bit = RetVT == MVT::i64;
1301 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1302 const TargetRegisterClass *RC;
1304 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1306 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1309 ResultReg = createResultReg(RC);
1311 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1313 const MCInstrDesc &II = TII.get(Opc);
1314 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1315 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1316 .addReg(LHSReg, getKillRegState(LHSIsKill))
1318 .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
1322 unsigned AArch64FastISel::emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
1323 bool LHSIsKill, unsigned RHSReg,
1325 AArch64_AM::ShiftExtendType ShiftType,
1326 uint64_t ShiftImm, bool SetFlags,
1328 assert(LHSReg && RHSReg && "Invalid register number.");
1330 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1333 // Don't deal with undefined shifts.
1334 if (ShiftImm >= RetVT.getSizeInBits())
1337 static const unsigned OpcTable[2][2][2] = {
1338 { { AArch64::SUBWrs, AArch64::SUBXrs },
1339 { AArch64::ADDWrs, AArch64::ADDXrs } },
1340 { { AArch64::SUBSWrs, AArch64::SUBSXrs },
1341 { AArch64::ADDSWrs, AArch64::ADDSXrs } }
1343 bool Is64Bit = RetVT == MVT::i64;
1344 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1345 const TargetRegisterClass *RC =
1346 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1349 ResultReg = createResultReg(RC);
1351 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1353 const MCInstrDesc &II = TII.get(Opc);
1354 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1355 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1356 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1357 .addReg(LHSReg, getKillRegState(LHSIsKill))
1358 .addReg(RHSReg, getKillRegState(RHSIsKill))
1359 .addImm(getShifterImm(ShiftType, ShiftImm));
1363 unsigned AArch64FastISel::emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
1364 bool LHSIsKill, unsigned RHSReg,
1366 AArch64_AM::ShiftExtendType ExtType,
1367 uint64_t ShiftImm, bool SetFlags,
1369 assert(LHSReg && RHSReg && "Invalid register number.");
1371 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1377 static const unsigned OpcTable[2][2][2] = {
1378 { { AArch64::SUBWrx, AArch64::SUBXrx },
1379 { AArch64::ADDWrx, AArch64::ADDXrx } },
1380 { { AArch64::SUBSWrx, AArch64::SUBSXrx },
1381 { AArch64::ADDSWrx, AArch64::ADDSXrx } }
1383 bool Is64Bit = RetVT == MVT::i64;
1384 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1385 const TargetRegisterClass *RC = nullptr;
1387 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1389 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1392 ResultReg = createResultReg(RC);
1394 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1396 const MCInstrDesc &II = TII.get(Opc);
1397 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1398 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1399 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1400 .addReg(LHSReg, getKillRegState(LHSIsKill))
1401 .addReg(RHSReg, getKillRegState(RHSIsKill))
1402 .addImm(getArithExtendImm(ExtType, ShiftImm));
1406 bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1407 Type *Ty = LHS->getType();
1408 EVT EVT = TLI.getValueType(DL, Ty, true);
1409 if (!EVT.isSimple())
1411 MVT VT = EVT.getSimpleVT();
1413 switch (VT.SimpleTy) {
1421 return emitICmp(VT, LHS, RHS, IsZExt);
1424 return emitFCmp(VT, LHS, RHS);
1428 bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1430 return emitSub(RetVT, LHS, RHS, /*SetFlags=*/true, /*WantResult=*/false,
1434 bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1436 return emitAddSub_ri(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, Imm,
1437 /*SetFlags=*/true, /*WantResult=*/false) != 0;
1440 bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1441 if (RetVT != MVT::f32 && RetVT != MVT::f64)
1444 // Check to see if the 2nd operand is a constant that we can encode directly
1446 bool UseImm = false;
1447 if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1448 if (CFP->isZero() && !CFP->isNegative())
1451 unsigned LHSReg = getRegForValue(LHS);
1454 bool LHSIsKill = hasTrivialKill(LHS);
1457 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1458 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1459 .addReg(LHSReg, getKillRegState(LHSIsKill));
1463 unsigned RHSReg = getRegForValue(RHS);
1466 bool RHSIsKill = hasTrivialKill(RHS);
1468 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1469 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1470 .addReg(LHSReg, getKillRegState(LHSIsKill))
1471 .addReg(RHSReg, getKillRegState(RHSIsKill));
1475 unsigned AArch64FastISel::emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
1476 bool SetFlags, bool WantResult, bool IsZExt) {
1477 return emitAddSub(/*UseAdd=*/true, RetVT, LHS, RHS, SetFlags, WantResult,
1481 /// \brief This method is a wrapper to simplify add emission.
1483 /// First try to emit an add with an immediate operand using emitAddSub_ri. If
1484 /// that fails, then try to materialize the immediate into a register and use
1485 /// emitAddSub_rr instead.
1486 unsigned AArch64FastISel::emitAdd_ri_(MVT VT, unsigned Op0, bool Op0IsKill,
1490 ResultReg = emitAddSub_ri(false, VT, Op0, Op0IsKill, -Imm);
1492 ResultReg = emitAddSub_ri(true, VT, Op0, Op0IsKill, Imm);
1497 unsigned CReg = fastEmit_i(VT, VT, ISD::Constant, Imm);
1501 ResultReg = emitAddSub_rr(true, VT, Op0, Op0IsKill, CReg, true);
1505 unsigned AArch64FastISel::emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
1506 bool SetFlags, bool WantResult, bool IsZExt) {
1507 return emitAddSub(/*UseAdd=*/false, RetVT, LHS, RHS, SetFlags, WantResult,
1511 unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1512 bool LHSIsKill, unsigned RHSReg,
1513 bool RHSIsKill, bool WantResult) {
1514 return emitAddSub_rr(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1515 RHSIsKill, /*SetFlags=*/true, WantResult);
1518 unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1519 bool LHSIsKill, unsigned RHSReg,
1521 AArch64_AM::ShiftExtendType ShiftType,
1522 uint64_t ShiftImm, bool WantResult) {
1523 return emitAddSub_rs(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1524 RHSIsKill, ShiftType, ShiftImm, /*SetFlags=*/true,
1528 unsigned AArch64FastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
1529 const Value *LHS, const Value *RHS) {
1530 // Canonicalize immediates to the RHS first.
1531 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1532 std::swap(LHS, RHS);
1534 // Canonicalize mul by power-of-2 to the RHS.
1535 if (LHS->hasOneUse() && isValueAvailable(LHS))
1536 if (isMulPowOf2(LHS))
1537 std::swap(LHS, RHS);
1539 // Canonicalize shift immediate to the RHS.
1540 if (LHS->hasOneUse() && isValueAvailable(LHS))
1541 if (const auto *SI = dyn_cast<ShlOperator>(LHS))
1542 if (isa<ConstantInt>(SI->getOperand(1)))
1543 std::swap(LHS, RHS);
1545 unsigned LHSReg = getRegForValue(LHS);
1548 bool LHSIsKill = hasTrivialKill(LHS);
1550 unsigned ResultReg = 0;
1551 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1552 uint64_t Imm = C->getZExtValue();
1553 ResultReg = emitLogicalOp_ri(ISDOpc, RetVT, LHSReg, LHSIsKill, Imm);
1558 // Check if the mul can be folded into the instruction.
1559 if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1560 if (isMulPowOf2(RHS)) {
1561 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1562 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1564 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1565 if (C->getValue().isPowerOf2())
1566 std::swap(MulLHS, MulRHS);
1568 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1569 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1571 unsigned RHSReg = getRegForValue(MulLHS);
1574 bool RHSIsKill = hasTrivialKill(MulLHS);
1575 ResultReg = emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1576 RHSIsKill, ShiftVal);
1582 // Check if the shift can be folded into the instruction.
1583 if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1584 if (const auto *SI = dyn_cast<ShlOperator>(RHS))
1585 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1586 uint64_t ShiftVal = C->getZExtValue();
1587 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1590 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1591 ResultReg = emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1592 RHSIsKill, ShiftVal);
1598 unsigned RHSReg = getRegForValue(RHS);
1601 bool RHSIsKill = hasTrivialKill(RHS);
1603 MVT VT = std::max(MVT::i32, RetVT.SimpleTy);
1604 ResultReg = fastEmit_rr(VT, VT, ISDOpc, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1605 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1606 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1607 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1612 unsigned AArch64FastISel::emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT,
1613 unsigned LHSReg, bool LHSIsKill,
1615 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1616 "ISD nodes are not consecutive!");
1617 static const unsigned OpcTable[3][2] = {
1618 { AArch64::ANDWri, AArch64::ANDXri },
1619 { AArch64::ORRWri, AArch64::ORRXri },
1620 { AArch64::EORWri, AArch64::EORXri }
1622 const TargetRegisterClass *RC;
1625 switch (RetVT.SimpleTy) {
1632 unsigned Idx = ISDOpc - ISD::AND;
1633 Opc = OpcTable[Idx][0];
1634 RC = &AArch64::GPR32spRegClass;
1639 Opc = OpcTable[ISDOpc - ISD::AND][1];
1640 RC = &AArch64::GPR64spRegClass;
1645 if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
1648 unsigned ResultReg =
1649 fastEmitInst_ri(Opc, RC, LHSReg, LHSIsKill,
1650 AArch64_AM::encodeLogicalImmediate(Imm, RegSize));
1651 if (RetVT >= MVT::i8 && RetVT <= MVT::i16 && ISDOpc != ISD::AND) {
1652 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1653 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1658 unsigned AArch64FastISel::emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT,
1659 unsigned LHSReg, bool LHSIsKill,
1660 unsigned RHSReg, bool RHSIsKill,
1661 uint64_t ShiftImm) {
1662 assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR) &&
1663 "ISD nodes are not consecutive!");
1664 static const unsigned OpcTable[3][2] = {
1665 { AArch64::ANDWrs, AArch64::ANDXrs },
1666 { AArch64::ORRWrs, AArch64::ORRXrs },
1667 { AArch64::EORWrs, AArch64::EORXrs }
1670 // Don't deal with undefined shifts.
1671 if (ShiftImm >= RetVT.getSizeInBits())
1674 const TargetRegisterClass *RC;
1676 switch (RetVT.SimpleTy) {
1683 Opc = OpcTable[ISDOpc - ISD::AND][0];
1684 RC = &AArch64::GPR32RegClass;
1687 Opc = OpcTable[ISDOpc - ISD::AND][1];
1688 RC = &AArch64::GPR64RegClass;
1691 unsigned ResultReg =
1692 fastEmitInst_rri(Opc, RC, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1693 AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftImm));
1694 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1695 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1696 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1701 unsigned AArch64FastISel::emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1703 return emitLogicalOp_ri(ISD::AND, RetVT, LHSReg, LHSIsKill, Imm);
1706 unsigned AArch64FastISel::emitLoad(MVT VT, MVT RetVT, Address Addr,
1707 bool WantZExt, MachineMemOperand *MMO) {
1708 if (!TLI.allowsMisalignedMemoryAccesses(VT))
1711 // Simplify this down to something we can handle.
1712 if (!simplifyAddress(Addr, VT))
1715 unsigned ScaleFactor = getImplicitScaleFactor(VT);
1717 llvm_unreachable("Unexpected value type.");
1719 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1720 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1721 bool UseScaled = true;
1722 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1727 static const unsigned GPOpcTable[2][8][4] = {
1729 { { AArch64::LDURSBWi, AArch64::LDURSHWi, AArch64::LDURWi,
1731 { AArch64::LDURSBXi, AArch64::LDURSHXi, AArch64::LDURSWi,
1733 { AArch64::LDRSBWui, AArch64::LDRSHWui, AArch64::LDRWui,
1735 { AArch64::LDRSBXui, AArch64::LDRSHXui, AArch64::LDRSWui,
1737 { AArch64::LDRSBWroX, AArch64::LDRSHWroX, AArch64::LDRWroX,
1739 { AArch64::LDRSBXroX, AArch64::LDRSHXroX, AArch64::LDRSWroX,
1741 { AArch64::LDRSBWroW, AArch64::LDRSHWroW, AArch64::LDRWroW,
1743 { AArch64::LDRSBXroW, AArch64::LDRSHXroW, AArch64::LDRSWroW,
1747 { { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1749 { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1751 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1753 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1755 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1757 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1759 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1761 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1766 static const unsigned FPOpcTable[4][2] = {
1767 { AArch64::LDURSi, AArch64::LDURDi },
1768 { AArch64::LDRSui, AArch64::LDRDui },
1769 { AArch64::LDRSroX, AArch64::LDRDroX },
1770 { AArch64::LDRSroW, AArch64::LDRDroW }
1774 const TargetRegisterClass *RC;
1775 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1776 Addr.getOffsetReg();
1777 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1778 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1779 Addr.getExtendType() == AArch64_AM::SXTW)
1782 bool IsRet64Bit = RetVT == MVT::i64;
1783 switch (VT.SimpleTy) {
1785 llvm_unreachable("Unexpected value type.");
1786 case MVT::i1: // Intentional fall-through.
1788 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][0];
1789 RC = (IsRet64Bit && !WantZExt) ?
1790 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1793 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][1];
1794 RC = (IsRet64Bit && !WantZExt) ?
1795 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1798 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][2];
1799 RC = (IsRet64Bit && !WantZExt) ?
1800 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1803 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][3];
1804 RC = &AArch64::GPR64RegClass;
1807 Opc = FPOpcTable[Idx][0];
1808 RC = &AArch64::FPR32RegClass;
1811 Opc = FPOpcTable[Idx][1];
1812 RC = &AArch64::FPR64RegClass;
1816 // Create the base instruction, then add the operands.
1817 unsigned ResultReg = createResultReg(RC);
1818 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1819 TII.get(Opc), ResultReg);
1820 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
1822 // Loading an i1 requires special handling.
1823 if (VT == MVT::i1) {
1824 unsigned ANDReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, 1);
1825 assert(ANDReg && "Unexpected AND instruction emission failure.");
1829 // For zero-extending loads to 64bit we emit a 32bit load and then convert
1830 // the 32bit reg to a 64bit reg.
1831 if (WantZExt && RetVT == MVT::i64 && VT <= MVT::i32) {
1832 unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
1833 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1834 TII.get(AArch64::SUBREG_TO_REG), Reg64)
1836 .addReg(ResultReg, getKillRegState(true))
1837 .addImm(AArch64::sub_32);
1843 bool AArch64FastISel::selectAddSub(const Instruction *I) {
1845 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1849 return selectOperator(I, I->getOpcode());
1852 switch (I->getOpcode()) {
1854 llvm_unreachable("Unexpected instruction.");
1855 case Instruction::Add:
1856 ResultReg = emitAdd(VT, I->getOperand(0), I->getOperand(1));
1858 case Instruction::Sub:
1859 ResultReg = emitSub(VT, I->getOperand(0), I->getOperand(1));
1865 updateValueMap(I, ResultReg);
1869 bool AArch64FastISel::selectLogicalOp(const Instruction *I) {
1871 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1875 return selectOperator(I, I->getOpcode());
1878 switch (I->getOpcode()) {
1880 llvm_unreachable("Unexpected instruction.");
1881 case Instruction::And:
1882 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
1884 case Instruction::Or:
1885 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
1887 case Instruction::Xor:
1888 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
1894 updateValueMap(I, ResultReg);
1898 bool AArch64FastISel::selectLoad(const Instruction *I) {
1900 // Verify we have a legal type before going any further. Currently, we handle
1901 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1902 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1903 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true) ||
1904 cast<LoadInst>(I)->isAtomic())
1907 // See if we can handle this address.
1909 if (!computeAddress(I->getOperand(0), Addr, I->getType()))
1912 // Fold the following sign-/zero-extend into the load instruction.
1913 bool WantZExt = true;
1915 const Value *IntExtVal = nullptr;
1916 if (I->hasOneUse()) {
1917 if (const auto *ZE = dyn_cast<ZExtInst>(I->use_begin()->getUser())) {
1918 if (isTypeSupported(ZE->getType(), RetVT))
1922 } else if (const auto *SE = dyn_cast<SExtInst>(I->use_begin()->getUser())) {
1923 if (isTypeSupported(SE->getType(), RetVT))
1931 unsigned ResultReg =
1932 emitLoad(VT, RetVT, Addr, WantZExt, createMachineMemOperandFor(I));
1936 // There are a few different cases we have to handle, because the load or the
1937 // sign-/zero-extend might not be selected by FastISel if we fall-back to
1938 // SelectionDAG. There is also an ordering issue when both instructions are in
1939 // different basic blocks.
1940 // 1.) The load instruction is selected by FastISel, but the integer extend
1941 // not. This usually happens when the integer extend is in a different
1942 // basic block and SelectionDAG took over for that basic block.
1943 // 2.) The load instruction is selected before the integer extend. This only
1944 // happens when the integer extend is in a different basic block.
1945 // 3.) The load instruction is selected by SelectionDAG and the integer extend
1946 // by FastISel. This happens if there are instructions between the load
1947 // and the integer extend that couldn't be selected by FastISel.
1949 // The integer extend hasn't been emitted yet. FastISel or SelectionDAG
1950 // could select it. Emit a copy to subreg if necessary. FastISel will remove
1951 // it when it selects the integer extend.
1952 unsigned Reg = lookUpRegForValue(IntExtVal);
1953 auto *MI = MRI.getUniqueVRegDef(Reg);
1955 if (RetVT == MVT::i64 && VT <= MVT::i32) {
1957 // Delete the last emitted instruction from emitLoad (SUBREG_TO_REG).
1958 std::prev(FuncInfo.InsertPt)->eraseFromParent();
1959 ResultReg = std::prev(FuncInfo.InsertPt)->getOperand(0).getReg();
1961 ResultReg = fastEmitInst_extractsubreg(MVT::i32, ResultReg,
1965 updateValueMap(I, ResultReg);
1969 // The integer extend has already been emitted - delete all the instructions
1970 // that have been emitted by the integer extend lowering code and use the
1971 // result from the load instruction directly.
1974 for (auto &Opnd : MI->uses()) {
1976 Reg = Opnd.getReg();
1980 MI->eraseFromParent();
1983 MI = MRI.getUniqueVRegDef(Reg);
1985 updateValueMap(IntExtVal, ResultReg);
1989 updateValueMap(I, ResultReg);
1993 bool AArch64FastISel::emitStore(MVT VT, unsigned SrcReg, Address Addr,
1994 MachineMemOperand *MMO) {
1995 if (!TLI.allowsMisalignedMemoryAccesses(VT))
1998 // Simplify this down to something we can handle.
1999 if (!simplifyAddress(Addr, VT))
2002 unsigned ScaleFactor = getImplicitScaleFactor(VT);
2004 llvm_unreachable("Unexpected value type.");
2006 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
2007 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
2008 bool UseScaled = true;
2009 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
2014 static const unsigned OpcTable[4][6] = {
2015 { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
2016 AArch64::STURSi, AArch64::STURDi },
2017 { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
2018 AArch64::STRSui, AArch64::STRDui },
2019 { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
2020 AArch64::STRSroX, AArch64::STRDroX },
2021 { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
2022 AArch64::STRSroW, AArch64::STRDroW }
2026 bool VTIsi1 = false;
2027 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
2028 Addr.getOffsetReg();
2029 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
2030 if (Addr.getExtendType() == AArch64_AM::UXTW ||
2031 Addr.getExtendType() == AArch64_AM::SXTW)
2034 switch (VT.SimpleTy) {
2035 default: llvm_unreachable("Unexpected value type.");
2036 case MVT::i1: VTIsi1 = true;
2037 case MVT::i8: Opc = OpcTable[Idx][0]; break;
2038 case MVT::i16: Opc = OpcTable[Idx][1]; break;
2039 case MVT::i32: Opc = OpcTable[Idx][2]; break;
2040 case MVT::i64: Opc = OpcTable[Idx][3]; break;
2041 case MVT::f32: Opc = OpcTable[Idx][4]; break;
2042 case MVT::f64: Opc = OpcTable[Idx][5]; break;
2045 // Storing an i1 requires special handling.
2046 if (VTIsi1 && SrcReg != AArch64::WZR) {
2047 unsigned ANDReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
2048 assert(ANDReg && "Unexpected AND instruction emission failure.");
2051 // Create the base instruction, then add the operands.
2052 const MCInstrDesc &II = TII.get(Opc);
2053 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
2054 MachineInstrBuilder MIB =
2055 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(SrcReg);
2056 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
2061 bool AArch64FastISel::selectStore(const Instruction *I) {
2063 const Value *Op0 = I->getOperand(0);
2064 // Verify we have a legal type before going any further. Currently, we handle
2065 // simple types that will directly fit in a register (i32/f32/i64/f64) or
2066 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
2067 if (!isTypeSupported(Op0->getType(), VT, /*IsVectorAllowed=*/true) ||
2068 cast<StoreInst>(I)->isAtomic())
2071 // Get the value to be stored into a register. Use the zero register directly
2072 // when possible to avoid an unnecessary copy and a wasted register.
2073 unsigned SrcReg = 0;
2074 if (const auto *CI = dyn_cast<ConstantInt>(Op0)) {
2076 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
2077 } else if (const auto *CF = dyn_cast<ConstantFP>(Op0)) {
2078 if (CF->isZero() && !CF->isNegative()) {
2079 VT = MVT::getIntegerVT(VT.getSizeInBits());
2080 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
2085 SrcReg = getRegForValue(Op0);
2090 // See if we can handle this address.
2092 if (!computeAddress(I->getOperand(1), Addr, I->getOperand(0)->getType()))
2095 if (!emitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
2100 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
2102 case CmpInst::FCMP_ONE:
2103 case CmpInst::FCMP_UEQ:
2105 // AL is our "false" for now. The other two need more compares.
2106 return AArch64CC::AL;
2107 case CmpInst::ICMP_EQ:
2108 case CmpInst::FCMP_OEQ:
2109 return AArch64CC::EQ;
2110 case CmpInst::ICMP_SGT:
2111 case CmpInst::FCMP_OGT:
2112 return AArch64CC::GT;
2113 case CmpInst::ICMP_SGE:
2114 case CmpInst::FCMP_OGE:
2115 return AArch64CC::GE;
2116 case CmpInst::ICMP_UGT:
2117 case CmpInst::FCMP_UGT:
2118 return AArch64CC::HI;
2119 case CmpInst::FCMP_OLT:
2120 return AArch64CC::MI;
2121 case CmpInst::ICMP_ULE:
2122 case CmpInst::FCMP_OLE:
2123 return AArch64CC::LS;
2124 case CmpInst::FCMP_ORD:
2125 return AArch64CC::VC;
2126 case CmpInst::FCMP_UNO:
2127 return AArch64CC::VS;
2128 case CmpInst::FCMP_UGE:
2129 return AArch64CC::PL;
2130 case CmpInst::ICMP_SLT:
2131 case CmpInst::FCMP_ULT:
2132 return AArch64CC::LT;
2133 case CmpInst::ICMP_SLE:
2134 case CmpInst::FCMP_ULE:
2135 return AArch64CC::LE;
2136 case CmpInst::FCMP_UNE:
2137 case CmpInst::ICMP_NE:
2138 return AArch64CC::NE;
2139 case CmpInst::ICMP_UGE:
2140 return AArch64CC::HS;
2141 case CmpInst::ICMP_ULT:
2142 return AArch64CC::LO;
2146 /// \brief Try to emit a combined compare-and-branch instruction.
2147 bool AArch64FastISel::emitCompareAndBranch(const BranchInst *BI) {
2148 assert(isa<CmpInst>(BI->getCondition()) && "Expected cmp instruction");
2149 const CmpInst *CI = cast<CmpInst>(BI->getCondition());
2150 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2152 const Value *LHS = CI->getOperand(0);
2153 const Value *RHS = CI->getOperand(1);
2156 if (!isTypeSupported(LHS->getType(), VT))
2159 unsigned BW = VT.getSizeInBits();
2163 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2164 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2166 // Try to take advantage of fallthrough opportunities.
2167 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2168 std::swap(TBB, FBB);
2169 Predicate = CmpInst::getInversePredicate(Predicate);
2174 switch (Predicate) {
2177 case CmpInst::ICMP_EQ:
2178 case CmpInst::ICMP_NE:
2179 if (isa<Constant>(LHS) && cast<Constant>(LHS)->isNullValue())
2180 std::swap(LHS, RHS);
2182 if (!isa<Constant>(RHS) || !cast<Constant>(RHS)->isNullValue())
2185 if (const auto *AI = dyn_cast<BinaryOperator>(LHS))
2186 if (AI->getOpcode() == Instruction::And && isValueAvailable(AI)) {
2187 const Value *AndLHS = AI->getOperand(0);
2188 const Value *AndRHS = AI->getOperand(1);
2190 if (const auto *C = dyn_cast<ConstantInt>(AndLHS))
2191 if (C->getValue().isPowerOf2())
2192 std::swap(AndLHS, AndRHS);
2194 if (const auto *C = dyn_cast<ConstantInt>(AndRHS))
2195 if (C->getValue().isPowerOf2()) {
2196 TestBit = C->getValue().logBase2();
2204 IsCmpNE = Predicate == CmpInst::ICMP_NE;
2206 case CmpInst::ICMP_SLT:
2207 case CmpInst::ICMP_SGE:
2208 if (!isa<Constant>(RHS) || !cast<Constant>(RHS)->isNullValue())
2212 IsCmpNE = Predicate == CmpInst::ICMP_SLT;
2214 case CmpInst::ICMP_SGT:
2215 case CmpInst::ICMP_SLE:
2216 if (!isa<ConstantInt>(RHS))
2219 if (cast<ConstantInt>(RHS)->getValue() != APInt(BW, -1, true))
2223 IsCmpNE = Predicate == CmpInst::ICMP_SLE;
2227 static const unsigned OpcTable[2][2][2] = {
2228 { {AArch64::CBZW, AArch64::CBZX },
2229 {AArch64::CBNZW, AArch64::CBNZX} },
2230 { {AArch64::TBZW, AArch64::TBZX },
2231 {AArch64::TBNZW, AArch64::TBNZX} }
2234 bool IsBitTest = TestBit != -1;
2235 bool Is64Bit = BW == 64;
2236 if (TestBit < 32 && TestBit >= 0)
2239 unsigned Opc = OpcTable[IsBitTest][IsCmpNE][Is64Bit];
2240 const MCInstrDesc &II = TII.get(Opc);
2242 unsigned SrcReg = getRegForValue(LHS);
2245 bool SrcIsKill = hasTrivialKill(LHS);
2247 if (BW == 64 && !Is64Bit)
2248 SrcReg = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
2251 if ((BW < 32) && !IsBitTest)
2252 SrcReg = emitIntExt(VT, SrcReg, MVT::i32, /*IsZExt=*/true);
2254 // Emit the combined compare and branch instruction.
2255 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
2256 MachineInstrBuilder MIB =
2257 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
2258 .addReg(SrcReg, getKillRegState(SrcIsKill));
2260 MIB.addImm(TestBit);
2263 // Obtain the branch weight and add the TrueBB to the successor list.
2264 uint32_t BranchWeight = 0;
2266 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2267 TBB->getBasicBlock());
2268 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2269 fastEmitBranch(FBB, DbgLoc);
2274 bool AArch64FastISel::selectBranch(const Instruction *I) {
2275 const BranchInst *BI = cast<BranchInst>(I);
2276 if (BI->isUnconditional()) {
2277 MachineBasicBlock *MSucc = FuncInfo.MBBMap[BI->getSuccessor(0)];
2278 fastEmitBranch(MSucc, BI->getDebugLoc());
2282 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2283 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2285 AArch64CC::CondCode CC = AArch64CC::NE;
2286 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
2287 if (CI->hasOneUse() && isValueAvailable(CI)) {
2288 // Try to optimize or fold the cmp.
2289 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2290 switch (Predicate) {
2293 case CmpInst::FCMP_FALSE:
2294 fastEmitBranch(FBB, DbgLoc);
2296 case CmpInst::FCMP_TRUE:
2297 fastEmitBranch(TBB, DbgLoc);
2301 // Try to emit a combined compare-and-branch first.
2302 if (emitCompareAndBranch(BI))
2305 // Try to take advantage of fallthrough opportunities.
2306 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2307 std::swap(TBB, FBB);
2308 Predicate = CmpInst::getInversePredicate(Predicate);
2312 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2315 // FCMP_UEQ and FCMP_ONE cannot be checked with a single branch
2317 CC = getCompareCC(Predicate);
2318 AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2319 switch (Predicate) {
2322 case CmpInst::FCMP_UEQ:
2323 ExtraCC = AArch64CC::EQ;
2326 case CmpInst::FCMP_ONE:
2327 ExtraCC = AArch64CC::MI;
2331 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2333 // Emit the extra branch for FCMP_UEQ and FCMP_ONE.
2334 if (ExtraCC != AArch64CC::AL) {
2335 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2341 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2345 // Obtain the branch weight and add the TrueBB to the successor list.
2346 uint32_t BranchWeight = 0;
2348 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2349 TBB->getBasicBlock());
2350 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2352 fastEmitBranch(FBB, DbgLoc);
2355 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
2357 if (TI->hasOneUse() && isValueAvailable(TI) &&
2358 isTypeSupported(TI->getOperand(0)->getType(), SrcVT)) {
2359 unsigned CondReg = getRegForValue(TI->getOperand(0));
2362 bool CondIsKill = hasTrivialKill(TI->getOperand(0));
2364 // Issue an extract_subreg to get the lower 32-bits.
2365 if (SrcVT == MVT::i64) {
2366 CondReg = fastEmitInst_extractsubreg(MVT::i32, CondReg, CondIsKill,
2371 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondIsKill, 1);
2372 assert(ANDReg && "Unexpected AND instruction emission failure.");
2373 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2375 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2376 std::swap(TBB, FBB);
2379 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2383 // Obtain the branch weight and add the TrueBB to the successor list.
2384 uint32_t BranchWeight = 0;
2386 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2387 TBB->getBasicBlock());
2388 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2390 fastEmitBranch(FBB, DbgLoc);
2393 } else if (const auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
2394 uint64_t Imm = CI->getZExtValue();
2395 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
2396 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
2399 // Obtain the branch weight and add the target to the successor list.
2400 uint32_t BranchWeight = 0;
2402 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2403 Target->getBasicBlock());
2404 FuncInfo.MBB->addSuccessor(Target, BranchWeight);
2406 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
2407 // Fake request the condition, otherwise the intrinsic might be completely
2409 unsigned CondReg = getRegForValue(BI->getCondition());
2414 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2418 // Obtain the branch weight and add the TrueBB to the successor list.
2419 uint32_t BranchWeight = 0;
2421 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2422 TBB->getBasicBlock());
2423 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2425 fastEmitBranch(FBB, DbgLoc);
2429 unsigned CondReg = getRegForValue(BI->getCondition());
2432 bool CondRegIsKill = hasTrivialKill(BI->getCondition());
2434 // We've been divorced from our compare! Our block was split, and
2435 // now our compare lives in a predecessor block. We musn't
2436 // re-compare here, as the children of the compare aren't guaranteed
2437 // live across the block boundary (we *could* check for this).
2438 // Regardless, the compare has been done in the predecessor block,
2439 // and it left a value for us in a virtual register. Ergo, we test
2440 // the one-bit value left in the virtual register.
2442 // FIXME: Optimize this with TBZW/TBZNW.
2443 unsigned ANDReg = emitAnd_ri(MVT::i32, CondReg, CondRegIsKill, 1);
2444 assert(ANDReg && "Unexpected AND instruction emission failure.");
2445 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
2447 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2448 std::swap(TBB, FBB);
2452 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2456 // Obtain the branch weight and add the TrueBB to the successor list.
2457 uint32_t BranchWeight = 0;
2459 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
2460 TBB->getBasicBlock());
2461 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
2463 fastEmitBranch(FBB, DbgLoc);
2467 bool AArch64FastISel::selectIndirectBr(const Instruction *I) {
2468 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
2469 unsigned AddrReg = getRegForValue(BI->getOperand(0));
2473 // Emit the indirect branch.
2474 const MCInstrDesc &II = TII.get(AArch64::BR);
2475 AddrReg = constrainOperandRegClass(II, AddrReg, II.getNumDefs());
2476 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(AddrReg);
2478 // Make sure the CFG is up-to-date.
2479 for (auto *Succ : BI->successors())
2480 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[Succ]);
2485 bool AArch64FastISel::selectCmp(const Instruction *I) {
2486 const CmpInst *CI = cast<CmpInst>(I);
2488 // Try to optimize or fold the cmp.
2489 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2490 unsigned ResultReg = 0;
2491 switch (Predicate) {
2494 case CmpInst::FCMP_FALSE:
2495 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2496 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2497 TII.get(TargetOpcode::COPY), ResultReg)
2498 .addReg(AArch64::WZR, getKillRegState(true));
2500 case CmpInst::FCMP_TRUE:
2501 ResultReg = fastEmit_i(MVT::i32, MVT::i32, ISD::Constant, 1);
2506 updateValueMap(I, ResultReg);
2511 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2514 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2516 // FCMP_UEQ and FCMP_ONE cannot be checked with a single instruction. These
2517 // condition codes are inverted, because they are used by CSINC.
2518 static unsigned CondCodeTable[2][2] = {
2519 { AArch64CC::NE, AArch64CC::VC },
2520 { AArch64CC::PL, AArch64CC::LE }
2522 unsigned *CondCodes = nullptr;
2523 switch (Predicate) {
2526 case CmpInst::FCMP_UEQ:
2527 CondCodes = &CondCodeTable[0][0];
2529 case CmpInst::FCMP_ONE:
2530 CondCodes = &CondCodeTable[1][0];
2535 unsigned TmpReg1 = createResultReg(&AArch64::GPR32RegClass);
2536 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2538 .addReg(AArch64::WZR, getKillRegState(true))
2539 .addReg(AArch64::WZR, getKillRegState(true))
2540 .addImm(CondCodes[0]);
2541 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2543 .addReg(TmpReg1, getKillRegState(true))
2544 .addReg(AArch64::WZR, getKillRegState(true))
2545 .addImm(CondCodes[1]);
2547 updateValueMap(I, ResultReg);
2551 // Now set a register based on the comparison.
2552 AArch64CC::CondCode CC = getCompareCC(Predicate);
2553 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2554 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
2555 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2557 .addReg(AArch64::WZR, getKillRegState(true))
2558 .addReg(AArch64::WZR, getKillRegState(true))
2559 .addImm(invertedCC);
2561 updateValueMap(I, ResultReg);
2565 /// \brief Optimize selects of i1 if one of the operands has a 'true' or 'false'
2567 bool AArch64FastISel::optimizeSelect(const SelectInst *SI) {
2568 if (!SI->getType()->isIntegerTy(1))
2571 const Value *Src1Val, *Src2Val;
2573 bool NeedExtraOp = false;
2574 if (auto *CI = dyn_cast<ConstantInt>(SI->getTrueValue())) {
2576 Src1Val = SI->getCondition();
2577 Src2Val = SI->getFalseValue();
2578 Opc = AArch64::ORRWrr;
2580 assert(CI->isZero());
2581 Src1Val = SI->getFalseValue();
2582 Src2Val = SI->getCondition();
2583 Opc = AArch64::BICWrr;
2585 } else if (auto *CI = dyn_cast<ConstantInt>(SI->getFalseValue())) {
2587 Src1Val = SI->getCondition();
2588 Src2Val = SI->getTrueValue();
2589 Opc = AArch64::ORRWrr;
2592 assert(CI->isZero());
2593 Src1Val = SI->getCondition();
2594 Src2Val = SI->getTrueValue();
2595 Opc = AArch64::ANDWrr;
2602 unsigned Src1Reg = getRegForValue(Src1Val);
2605 bool Src1IsKill = hasTrivialKill(Src1Val);
2607 unsigned Src2Reg = getRegForValue(Src2Val);
2610 bool Src2IsKill = hasTrivialKill(Src2Val);
2613 Src1Reg = emitLogicalOp_ri(ISD::XOR, MVT::i32, Src1Reg, Src1IsKill, 1);
2616 unsigned ResultReg = fastEmitInst_rr(Opc, &AArch64::GPR32RegClass, Src1Reg,
2617 Src1IsKill, Src2Reg, Src2IsKill);
2618 updateValueMap(SI, ResultReg);
2622 bool AArch64FastISel::selectSelect(const Instruction *I) {
2623 assert(isa<SelectInst>(I) && "Expected a select instruction.");
2625 if (!isTypeSupported(I->getType(), VT))
2629 const TargetRegisterClass *RC;
2630 switch (VT.SimpleTy) {
2637 Opc = AArch64::CSELWr;
2638 RC = &AArch64::GPR32RegClass;
2641 Opc = AArch64::CSELXr;
2642 RC = &AArch64::GPR64RegClass;
2645 Opc = AArch64::FCSELSrrr;
2646 RC = &AArch64::FPR32RegClass;
2649 Opc = AArch64::FCSELDrrr;
2650 RC = &AArch64::FPR64RegClass;
2654 const SelectInst *SI = cast<SelectInst>(I);
2655 const Value *Cond = SI->getCondition();
2656 AArch64CC::CondCode CC = AArch64CC::NE;
2657 AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2659 if (optimizeSelect(SI))
2662 // Try to pickup the flags, so we don't have to emit another compare.
2663 if (foldXALUIntrinsic(CC, I, Cond)) {
2664 // Fake request the condition to force emission of the XALU intrinsic.
2665 unsigned CondReg = getRegForValue(Cond);
2668 } else if (isa<CmpInst>(Cond) && cast<CmpInst>(Cond)->hasOneUse() &&
2669 isValueAvailable(Cond)) {
2670 const auto *Cmp = cast<CmpInst>(Cond);
2671 // Try to optimize or fold the cmp.
2672 CmpInst::Predicate Predicate = optimizeCmpPredicate(Cmp);
2673 const Value *FoldSelect = nullptr;
2674 switch (Predicate) {
2677 case CmpInst::FCMP_FALSE:
2678 FoldSelect = SI->getFalseValue();
2680 case CmpInst::FCMP_TRUE:
2681 FoldSelect = SI->getTrueValue();
2686 unsigned SrcReg = getRegForValue(FoldSelect);
2689 unsigned UseReg = lookUpRegForValue(SI);
2691 MRI.clearKillFlags(UseReg);
2693 updateValueMap(I, SrcReg);
2698 if (!emitCmp(Cmp->getOperand(0), Cmp->getOperand(1), Cmp->isUnsigned()))
2701 // FCMP_UEQ and FCMP_ONE cannot be checked with a single select instruction.
2702 CC = getCompareCC(Predicate);
2703 switch (Predicate) {
2706 case CmpInst::FCMP_UEQ:
2707 ExtraCC = AArch64CC::EQ;
2710 case CmpInst::FCMP_ONE:
2711 ExtraCC = AArch64CC::MI;
2715 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2717 unsigned CondReg = getRegForValue(Cond);
2720 bool CondIsKill = hasTrivialKill(Cond);
2722 const MCInstrDesc &II = TII.get(AArch64::ANDSWri);
2723 CondReg = constrainOperandRegClass(II, CondReg, 1);
2725 // Emit a TST instruction (ANDS wzr, reg, #imm).
2726 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
2728 .addReg(CondReg, getKillRegState(CondIsKill))
2729 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
2732 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
2733 bool Src1IsKill = hasTrivialKill(SI->getTrueValue());
2735 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
2736 bool Src2IsKill = hasTrivialKill(SI->getFalseValue());
2738 if (!Src1Reg || !Src2Reg)
2741 if (ExtraCC != AArch64CC::AL) {
2742 Src2Reg = fastEmitInst_rri(Opc, RC, Src1Reg, Src1IsKill, Src2Reg,
2743 Src2IsKill, ExtraCC);
2746 unsigned ResultReg = fastEmitInst_rri(Opc, RC, Src1Reg, Src1IsKill, Src2Reg,
2748 updateValueMap(I, ResultReg);
2752 bool AArch64FastISel::selectFPExt(const Instruction *I) {
2753 Value *V = I->getOperand(0);
2754 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
2757 unsigned Op = getRegForValue(V);
2761 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
2762 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
2763 ResultReg).addReg(Op);
2764 updateValueMap(I, ResultReg);
2768 bool AArch64FastISel::selectFPTrunc(const Instruction *I) {
2769 Value *V = I->getOperand(0);
2770 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
2773 unsigned Op = getRegForValue(V);
2777 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
2778 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
2779 ResultReg).addReg(Op);
2780 updateValueMap(I, ResultReg);
2784 // FPToUI and FPToSI
2785 bool AArch64FastISel::selectFPToInt(const Instruction *I, bool Signed) {
2787 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2790 unsigned SrcReg = getRegForValue(I->getOperand(0));
2794 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType(), true);
2795 if (SrcVT == MVT::f128)
2799 if (SrcVT == MVT::f64) {
2801 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
2803 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
2806 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
2808 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
2810 unsigned ResultReg = createResultReg(
2811 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
2812 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2814 updateValueMap(I, ResultReg);
2818 bool AArch64FastISel::selectIntToFP(const Instruction *I, bool Signed) {
2820 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2822 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
2823 "Unexpected value type.");
2825 unsigned SrcReg = getRegForValue(I->getOperand(0));
2828 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
2830 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType(), true);
2832 // Handle sign-extension.
2833 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
2835 emitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
2842 if (SrcVT == MVT::i64) {
2844 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
2846 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
2849 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
2851 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
2854 unsigned ResultReg = fastEmitInst_r(Opc, TLI.getRegClassFor(DestVT), SrcReg,
2856 updateValueMap(I, ResultReg);
2860 bool AArch64FastISel::fastLowerArguments() {
2861 if (!FuncInfo.CanLowerReturn)
2864 const Function *F = FuncInfo.Fn;
2868 CallingConv::ID CC = F->getCallingConv();
2869 if (CC != CallingConv::C)
2872 // Only handle simple cases of up to 8 GPR and FPR each.
2873 unsigned GPRCnt = 0;
2874 unsigned FPRCnt = 0;
2876 for (auto const &Arg : F->args()) {
2877 // The first argument is at index 1.
2879 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2880 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2881 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2882 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2885 Type *ArgTy = Arg.getType();
2886 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
2889 EVT ArgVT = TLI.getValueType(DL, ArgTy);
2890 if (!ArgVT.isSimple())
2893 MVT VT = ArgVT.getSimpleVT().SimpleTy;
2894 if (VT.isFloatingPoint() && !Subtarget->hasFPARMv8())
2897 if (VT.isVector() &&
2898 (!Subtarget->hasNEON() || !Subtarget->isLittleEndian()))
2901 if (VT >= MVT::i1 && VT <= MVT::i64)
2903 else if ((VT >= MVT::f16 && VT <= MVT::f64) || VT.is64BitVector() ||
2904 VT.is128BitVector())
2909 if (GPRCnt > 8 || FPRCnt > 8)
2913 static const MCPhysReg Registers[6][8] = {
2914 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
2915 AArch64::W5, AArch64::W6, AArch64::W7 },
2916 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
2917 AArch64::X5, AArch64::X6, AArch64::X7 },
2918 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
2919 AArch64::H5, AArch64::H6, AArch64::H7 },
2920 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
2921 AArch64::S5, AArch64::S6, AArch64::S7 },
2922 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
2923 AArch64::D5, AArch64::D6, AArch64::D7 },
2924 { AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, AArch64::Q4,
2925 AArch64::Q5, AArch64::Q6, AArch64::Q7 }
2928 unsigned GPRIdx = 0;
2929 unsigned FPRIdx = 0;
2930 for (auto const &Arg : F->args()) {
2931 MVT VT = TLI.getSimpleValueType(DL, Arg.getType());
2933 const TargetRegisterClass *RC;
2934 if (VT >= MVT::i1 && VT <= MVT::i32) {
2935 SrcReg = Registers[0][GPRIdx++];
2936 RC = &AArch64::GPR32RegClass;
2938 } else if (VT == MVT::i64) {
2939 SrcReg = Registers[1][GPRIdx++];
2940 RC = &AArch64::GPR64RegClass;
2941 } else if (VT == MVT::f16) {
2942 SrcReg = Registers[2][FPRIdx++];
2943 RC = &AArch64::FPR16RegClass;
2944 } else if (VT == MVT::f32) {
2945 SrcReg = Registers[3][FPRIdx++];
2946 RC = &AArch64::FPR32RegClass;
2947 } else if ((VT == MVT::f64) || VT.is64BitVector()) {
2948 SrcReg = Registers[4][FPRIdx++];
2949 RC = &AArch64::FPR64RegClass;
2950 } else if (VT.is128BitVector()) {
2951 SrcReg = Registers[5][FPRIdx++];
2952 RC = &AArch64::FPR128RegClass;
2954 llvm_unreachable("Unexpected value type.");
2956 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2957 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2958 // Without this, EmitLiveInCopies may eliminate the livein if its only
2959 // use is a bitcast (which isn't turned into an instruction).
2960 unsigned ResultReg = createResultReg(RC);
2961 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2962 TII.get(TargetOpcode::COPY), ResultReg)
2963 .addReg(DstReg, getKillRegState(true));
2964 updateValueMap(&Arg, ResultReg);
2969 bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
2970 SmallVectorImpl<MVT> &OutVTs,
2971 unsigned &NumBytes) {
2972 CallingConv::ID CC = CLI.CallConv;
2973 SmallVector<CCValAssign, 16> ArgLocs;
2974 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
2975 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
2977 // Get a count of how many bytes are to be pushed on the stack.
2978 NumBytes = CCInfo.getNextStackOffset();
2980 // Issue CALLSEQ_START
2981 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
2982 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
2985 // Process the args.
2986 for (CCValAssign &VA : ArgLocs) {
2987 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
2988 MVT ArgVT = OutVTs[VA.getValNo()];
2990 unsigned ArgReg = getRegForValue(ArgVal);
2994 // Handle arg promotion: SExt, ZExt, AExt.
2995 switch (VA.getLocInfo()) {
2996 case CCValAssign::Full:
2998 case CCValAssign::SExt: {
2999 MVT DestVT = VA.getLocVT();
3001 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
3006 case CCValAssign::AExt:
3007 // Intentional fall-through.
3008 case CCValAssign::ZExt: {
3009 MVT DestVT = VA.getLocVT();
3011 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
3017 llvm_unreachable("Unknown arg promotion!");
3020 // Now copy/store arg to correct locations.
3021 if (VA.isRegLoc() && !VA.needsCustom()) {
3022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3023 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
3024 CLI.OutRegs.push_back(VA.getLocReg());
3025 } else if (VA.needsCustom()) {
3026 // FIXME: Handle custom args.
3029 assert(VA.isMemLoc() && "Assuming store on stack.");
3031 // Don't emit stores for undef values.
3032 if (isa<UndefValue>(ArgVal))
3035 // Need to store on the stack.
3036 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
3038 unsigned BEAlign = 0;
3039 if (ArgSize < 8 && !Subtarget->isLittleEndian())
3040 BEAlign = 8 - ArgSize;
3043 Addr.setKind(Address::RegBase);
3044 Addr.setReg(AArch64::SP);
3045 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
3047 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
3048 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
3049 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
3050 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
3052 if (!emitStore(ArgVT, ArgReg, Addr, MMO))
3059 bool AArch64FastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
3060 unsigned NumBytes) {
3061 CallingConv::ID CC = CLI.CallConv;
3063 // Issue CALLSEQ_END
3064 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
3065 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
3066 .addImm(NumBytes).addImm(0);
3068 // Now the return value.
3069 if (RetVT != MVT::isVoid) {
3070 SmallVector<CCValAssign, 16> RVLocs;
3071 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
3072 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
3074 // Only handle a single return value.
3075 if (RVLocs.size() != 1)
3078 // Copy all of the result registers out of their specified physreg.
3079 MVT CopyVT = RVLocs[0].getValVT();
3081 // TODO: Handle big-endian results
3082 if (CopyVT.isVector() && !Subtarget->isLittleEndian())
3085 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
3086 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3087 TII.get(TargetOpcode::COPY), ResultReg)
3088 .addReg(RVLocs[0].getLocReg());
3089 CLI.InRegs.push_back(RVLocs[0].getLocReg());
3091 CLI.ResultReg = ResultReg;
3092 CLI.NumResultRegs = 1;
3098 bool AArch64FastISel::fastLowerCall(CallLoweringInfo &CLI) {
3099 CallingConv::ID CC = CLI.CallConv;
3100 bool IsTailCall = CLI.IsTailCall;
3101 bool IsVarArg = CLI.IsVarArg;
3102 const Value *Callee = CLI.Callee;
3103 MCSymbol *Symbol = CLI.Symbol;
3105 if (!Callee && !Symbol)
3108 // Allow SelectionDAG isel to handle tail calls.
3112 CodeModel::Model CM = TM.getCodeModel();
3113 // Only support the small and large code model.
3114 if (CM != CodeModel::Small && CM != CodeModel::Large)
3117 // FIXME: Add large code model support for ELF.
3118 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
3121 // Let SDISel handle vararg functions.
3125 // FIXME: Only handle *simple* calls for now.
3127 if (CLI.RetTy->isVoidTy())
3128 RetVT = MVT::isVoid;
3129 else if (!isTypeLegal(CLI.RetTy, RetVT))
3132 for (auto Flag : CLI.OutFlags)
3133 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
3136 // Set up the argument vectors.
3137 SmallVector<MVT, 16> OutVTs;
3138 OutVTs.reserve(CLI.OutVals.size());
3140 for (auto *Val : CLI.OutVals) {
3142 if (!isTypeLegal(Val->getType(), VT) &&
3143 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
3146 // We don't handle vector parameters yet.
3147 if (VT.isVector() || VT.getSizeInBits() > 64)
3150 OutVTs.push_back(VT);
3154 if (Callee && !computeCallAddress(Callee, Addr))
3157 // Handle the arguments now that we've gotten them.
3159 if (!processCallArgs(CLI, OutVTs, NumBytes))
3163 MachineInstrBuilder MIB;
3164 if (CM == CodeModel::Small) {
3165 const MCInstrDesc &II = TII.get(Addr.getReg() ? AArch64::BLR : AArch64::BL);
3166 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II);
3168 MIB.addSym(Symbol, 0);
3169 else if (Addr.getGlobalValue())
3170 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
3171 else if (Addr.getReg()) {
3172 unsigned Reg = constrainOperandRegClass(II, Addr.getReg(), 0);
3177 unsigned CallReg = 0;
3179 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
3180 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
3182 .addSym(Symbol, AArch64II::MO_GOT | AArch64II::MO_PAGE);
3184 CallReg = createResultReg(&AArch64::GPR64RegClass);
3185 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3186 TII.get(AArch64::LDRXui), CallReg)
3189 AArch64II::MO_GOT | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
3190 } else if (Addr.getGlobalValue())
3191 CallReg = materializeGV(Addr.getGlobalValue());
3192 else if (Addr.getReg())
3193 CallReg = Addr.getReg();
3198 const MCInstrDesc &II = TII.get(AArch64::BLR);
3199 CallReg = constrainOperandRegClass(II, CallReg, 0);
3200 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(CallReg);
3203 // Add implicit physical register uses to the call.
3204 for (auto Reg : CLI.OutRegs)
3205 MIB.addReg(Reg, RegState::Implicit);
3207 // Add a register mask with the call-preserved registers.
3208 // Proper defs for return values will be added by setPhysRegsDeadExcept().
3209 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
3213 // Finish off the call including any return values.
3214 return finishCall(CLI, RetVT, NumBytes);
3217 bool AArch64FastISel::isMemCpySmall(uint64_t Len, unsigned Alignment) {
3219 return Len / Alignment <= 4;
3224 bool AArch64FastISel::tryEmitSmallMemCpy(Address Dest, Address Src,
3225 uint64_t Len, unsigned Alignment) {
3226 // Make sure we don't bloat code by inlining very large memcpy's.
3227 if (!isMemCpySmall(Len, Alignment))
3230 int64_t UnscaledOffset = 0;
3231 Address OrigDest = Dest;
3232 Address OrigSrc = Src;
3236 if (!Alignment || Alignment >= 8) {
3247 // Bound based on alignment.
3248 if (Len >= 4 && Alignment == 4)
3250 else if (Len >= 2 && Alignment == 2)
3257 unsigned ResultReg = emitLoad(VT, VT, Src);
3261 if (!emitStore(VT, ResultReg, Dest))
3264 int64_t Size = VT.getSizeInBits() / 8;
3266 UnscaledOffset += Size;
3268 // We need to recompute the unscaled offset for each iteration.
3269 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
3270 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
3276 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
3277 /// into the user. The condition code will only be updated on success.
3278 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
3279 const Instruction *I,
3280 const Value *Cond) {
3281 if (!isa<ExtractValueInst>(Cond))
3284 const auto *EV = cast<ExtractValueInst>(Cond);
3285 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
3288 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
3290 const Function *Callee = II->getCalledFunction();
3292 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
3293 if (!isTypeLegal(RetTy, RetVT))
3296 if (RetVT != MVT::i32 && RetVT != MVT::i64)
3299 const Value *LHS = II->getArgOperand(0);
3300 const Value *RHS = II->getArgOperand(1);
3302 // Canonicalize immediate to the RHS.
3303 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3304 isCommutativeIntrinsic(II))
3305 std::swap(LHS, RHS);
3307 // Simplify multiplies.
3308 Intrinsic::ID IID = II->getIntrinsicID();
3312 case Intrinsic::smul_with_overflow:
3313 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3314 if (C->getValue() == 2)
3315 IID = Intrinsic::sadd_with_overflow;
3317 case Intrinsic::umul_with_overflow:
3318 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3319 if (C->getValue() == 2)
3320 IID = Intrinsic::uadd_with_overflow;
3324 AArch64CC::CondCode TmpCC;
3328 case Intrinsic::sadd_with_overflow:
3329 case Intrinsic::ssub_with_overflow:
3330 TmpCC = AArch64CC::VS;
3332 case Intrinsic::uadd_with_overflow:
3333 TmpCC = AArch64CC::HS;
3335 case Intrinsic::usub_with_overflow:
3336 TmpCC = AArch64CC::LO;
3338 case Intrinsic::smul_with_overflow:
3339 case Intrinsic::umul_with_overflow:
3340 TmpCC = AArch64CC::NE;
3344 // Check if both instructions are in the same basic block.
3345 if (!isValueAvailable(II))
3348 // Make sure nothing is in the way
3349 BasicBlock::const_iterator Start = I;
3350 BasicBlock::const_iterator End = II;
3351 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
3352 // We only expect extractvalue instructions between the intrinsic and the
3353 // instruction to be selected.
3354 if (!isa<ExtractValueInst>(Itr))
3357 // Check that the extractvalue operand comes from the intrinsic.
3358 const auto *EVI = cast<ExtractValueInst>(Itr);
3359 if (EVI->getAggregateOperand() != II)
3367 bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
3368 // FIXME: Handle more intrinsics.
3369 switch (II->getIntrinsicID()) {
3370 default: return false;
3371 case Intrinsic::frameaddress: {
3372 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
3373 MFI->setFrameAddressIsTaken(true);
3375 const AArch64RegisterInfo *RegInfo =
3376 static_cast<const AArch64RegisterInfo *>(Subtarget->getRegisterInfo());
3377 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
3378 unsigned SrcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3379 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3380 TII.get(TargetOpcode::COPY), SrcReg).addReg(FramePtr);
3381 // Recursively load frame address
3387 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
3389 DestReg = fastEmitInst_ri(AArch64::LDRXui, &AArch64::GPR64RegClass,
3390 SrcReg, /*IsKill=*/true, 0);
3391 assert(DestReg && "Unexpected LDR instruction emission failure.");
3395 updateValueMap(II, SrcReg);
3398 case Intrinsic::memcpy:
3399 case Intrinsic::memmove: {
3400 const auto *MTI = cast<MemTransferInst>(II);
3401 // Don't handle volatile.
3402 if (MTI->isVolatile())
3405 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
3406 // we would emit dead code because we don't currently handle memmoves.
3407 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
3408 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
3409 // Small memcpy's are common enough that we want to do them without a call
3411 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
3412 unsigned Alignment = MTI->getAlignment();
3413 if (isMemCpySmall(Len, Alignment)) {
3415 if (!computeAddress(MTI->getRawDest(), Dest) ||
3416 !computeAddress(MTI->getRawSource(), Src))
3418 if (tryEmitSmallMemCpy(Dest, Src, Len, Alignment))
3423 if (!MTI->getLength()->getType()->isIntegerTy(64))
3426 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
3427 // Fast instruction selection doesn't support the special
3431 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
3432 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
3434 case Intrinsic::memset: {
3435 const MemSetInst *MSI = cast<MemSetInst>(II);
3436 // Don't handle volatile.
3437 if (MSI->isVolatile())
3440 if (!MSI->getLength()->getType()->isIntegerTy(64))
3443 if (MSI->getDestAddressSpace() > 255)
3444 // Fast instruction selection doesn't support the special
3448 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
3450 case Intrinsic::sin:
3451 case Intrinsic::cos:
3452 case Intrinsic::pow: {
3454 if (!isTypeLegal(II->getType(), RetVT))
3457 if (RetVT != MVT::f32 && RetVT != MVT::f64)
3460 static const RTLIB::Libcall LibCallTable[3][2] = {
3461 { RTLIB::SIN_F32, RTLIB::SIN_F64 },
3462 { RTLIB::COS_F32, RTLIB::COS_F64 },
3463 { RTLIB::POW_F32, RTLIB::POW_F64 }
3466 bool Is64Bit = RetVT == MVT::f64;
3467 switch (II->getIntrinsicID()) {
3469 llvm_unreachable("Unexpected intrinsic.");
3470 case Intrinsic::sin:
3471 LC = LibCallTable[0][Is64Bit];
3473 case Intrinsic::cos:
3474 LC = LibCallTable[1][Is64Bit];
3476 case Intrinsic::pow:
3477 LC = LibCallTable[2][Is64Bit];
3482 Args.reserve(II->getNumArgOperands());
3484 // Populate the argument list.
3485 for (auto &Arg : II->arg_operands()) {
3488 Entry.Ty = Arg->getType();
3489 Args.push_back(Entry);
3492 CallLoweringInfo CLI;
3493 MCContext &Ctx = MF->getContext();
3494 CLI.setCallee(DL, Ctx, TLI.getLibcallCallingConv(LC), II->getType(),
3495 TLI.getLibcallName(LC), std::move(Args));
3496 if (!lowerCallTo(CLI))
3498 updateValueMap(II, CLI.ResultReg);
3501 case Intrinsic::fabs: {
3503 if (!isTypeLegal(II->getType(), VT))
3507 switch (VT.SimpleTy) {
3511 Opc = AArch64::FABSSr;
3514 Opc = AArch64::FABSDr;
3517 unsigned SrcReg = getRegForValue(II->getOperand(0));
3520 bool SrcRegIsKill = hasTrivialKill(II->getOperand(0));
3521 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3522 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
3523 .addReg(SrcReg, getKillRegState(SrcRegIsKill));
3524 updateValueMap(II, ResultReg);
3527 case Intrinsic::trap: {
3528 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
3532 case Intrinsic::sqrt: {
3533 Type *RetTy = II->getCalledFunction()->getReturnType();
3536 if (!isTypeLegal(RetTy, VT))
3539 unsigned Op0Reg = getRegForValue(II->getOperand(0));
3542 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
3544 unsigned ResultReg = fastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
3548 updateValueMap(II, ResultReg);
3551 case Intrinsic::sadd_with_overflow:
3552 case Intrinsic::uadd_with_overflow:
3553 case Intrinsic::ssub_with_overflow:
3554 case Intrinsic::usub_with_overflow:
3555 case Intrinsic::smul_with_overflow:
3556 case Intrinsic::umul_with_overflow: {
3557 // This implements the basic lowering of the xalu with overflow intrinsics.
3558 const Function *Callee = II->getCalledFunction();
3559 auto *Ty = cast<StructType>(Callee->getReturnType());
3560 Type *RetTy = Ty->getTypeAtIndex(0U);
3563 if (!isTypeLegal(RetTy, VT))
3566 if (VT != MVT::i32 && VT != MVT::i64)
3569 const Value *LHS = II->getArgOperand(0);
3570 const Value *RHS = II->getArgOperand(1);
3571 // Canonicalize immediate to the RHS.
3572 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3573 isCommutativeIntrinsic(II))
3574 std::swap(LHS, RHS);
3576 // Simplify multiplies.
3577 Intrinsic::ID IID = II->getIntrinsicID();
3581 case Intrinsic::smul_with_overflow:
3582 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3583 if (C->getValue() == 2) {
3584 IID = Intrinsic::sadd_with_overflow;
3588 case Intrinsic::umul_with_overflow:
3589 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3590 if (C->getValue() == 2) {
3591 IID = Intrinsic::uadd_with_overflow;
3597 unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
3598 AArch64CC::CondCode CC = AArch64CC::Invalid;
3600 default: llvm_unreachable("Unexpected intrinsic!");
3601 case Intrinsic::sadd_with_overflow:
3602 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3605 case Intrinsic::uadd_with_overflow:
3606 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3609 case Intrinsic::ssub_with_overflow:
3610 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3613 case Intrinsic::usub_with_overflow:
3614 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3617 case Intrinsic::smul_with_overflow: {
3619 unsigned LHSReg = getRegForValue(LHS);
3622 bool LHSIsKill = hasTrivialKill(LHS);
3624 unsigned RHSReg = getRegForValue(RHS);
3627 bool RHSIsKill = hasTrivialKill(RHS);
3629 if (VT == MVT::i32) {
3630 MulReg = emitSMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3631 unsigned ShiftReg = emitLSR_ri(MVT::i64, MVT::i64, MulReg,
3632 /*IsKill=*/false, 32);
3633 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3635 ShiftReg = fastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
3637 emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3638 AArch64_AM::ASR, 31, /*WantResult=*/false);
3640 assert(VT == MVT::i64 && "Unexpected value type.");
3641 // LHSReg and RHSReg cannot be killed by this Mul, since they are
3642 // reused in the next instruction.
3643 MulReg = emitMul_rr(VT, LHSReg, /*IsKill=*/false, RHSReg,
3645 unsigned SMULHReg = fastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
3647 emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3648 AArch64_AM::ASR, 63, /*WantResult=*/false);
3652 case Intrinsic::umul_with_overflow: {
3654 unsigned LHSReg = getRegForValue(LHS);
3657 bool LHSIsKill = hasTrivialKill(LHS);
3659 unsigned RHSReg = getRegForValue(RHS);
3662 bool RHSIsKill = hasTrivialKill(RHS);
3664 if (VT == MVT::i32) {
3665 MulReg = emitUMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3666 emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
3667 /*IsKill=*/false, AArch64_AM::LSR, 32,
3668 /*WantResult=*/false);
3669 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3672 assert(VT == MVT::i64 && "Unexpected value type.");
3673 // LHSReg and RHSReg cannot be killed by this Mul, since they are
3674 // reused in the next instruction.
3675 MulReg = emitMul_rr(VT, LHSReg, /*IsKill=*/false, RHSReg,
3677 unsigned UMULHReg = fastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
3679 emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
3680 /*IsKill=*/false, /*WantResult=*/false);
3687 ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
3688 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3689 TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
3692 ResultReg2 = fastEmitInst_rri(AArch64::CSINCWr, &AArch64::GPR32RegClass,
3693 AArch64::WZR, /*IsKill=*/true, AArch64::WZR,
3694 /*IsKill=*/true, getInvertedCondCode(CC));
3696 assert((ResultReg1 + 1) == ResultReg2 &&
3697 "Nonconsecutive result registers.");
3698 updateValueMap(II, ResultReg1, 2);
3705 bool AArch64FastISel::selectRet(const Instruction *I) {
3706 const ReturnInst *Ret = cast<ReturnInst>(I);
3707 const Function &F = *I->getParent()->getParent();
3709 if (!FuncInfo.CanLowerReturn)
3715 // Build a list of return value registers.
3716 SmallVector<unsigned, 4> RetRegs;
3718 if (Ret->getNumOperands() > 0) {
3719 CallingConv::ID CC = F.getCallingConv();
3720 SmallVector<ISD::OutputArg, 4> Outs;
3721 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
3723 // Analyze operands of the call, assigning locations to each operand.
3724 SmallVector<CCValAssign, 16> ValLocs;
3725 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
3726 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
3727 : RetCC_AArch64_AAPCS;
3728 CCInfo.AnalyzeReturn(Outs, RetCC);
3730 // Only handle a single return value for now.
3731 if (ValLocs.size() != 1)
3734 CCValAssign &VA = ValLocs[0];
3735 const Value *RV = Ret->getOperand(0);
3737 // Don't bother handling odd stuff for now.
3738 if ((VA.getLocInfo() != CCValAssign::Full) &&
3739 (VA.getLocInfo() != CCValAssign::BCvt))
3742 // Only handle register returns for now.
3746 unsigned Reg = getRegForValue(RV);
3750 unsigned SrcReg = Reg + VA.getValNo();
3751 unsigned DestReg = VA.getLocReg();
3752 // Avoid a cross-class copy. This is very unlikely.
3753 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
3756 EVT RVEVT = TLI.getValueType(DL, RV->getType());
3757 if (!RVEVT.isSimple())
3760 // Vectors (of > 1 lane) in big endian need tricky handling.
3761 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1 &&
3762 !Subtarget->isLittleEndian())
3765 MVT RVVT = RVEVT.getSimpleVT();
3766 if (RVVT == MVT::f128)
3769 MVT DestVT = VA.getValVT();
3770 // Special handling for extended integers.
3771 if (RVVT != DestVT) {
3772 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
3775 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
3778 bool IsZExt = Outs[0].Flags.isZExt();
3779 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
3785 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3786 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
3788 // Add register to return instruction.
3789 RetRegs.push_back(VA.getLocReg());
3792 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3793 TII.get(AArch64::RET_ReallyLR));
3794 for (unsigned RetReg : RetRegs)
3795 MIB.addReg(RetReg, RegState::Implicit);
3799 bool AArch64FastISel::selectTrunc(const Instruction *I) {
3800 Type *DestTy = I->getType();
3801 Value *Op = I->getOperand(0);
3802 Type *SrcTy = Op->getType();
3804 EVT SrcEVT = TLI.getValueType(DL, SrcTy, true);
3805 EVT DestEVT = TLI.getValueType(DL, DestTy, true);
3806 if (!SrcEVT.isSimple())
3808 if (!DestEVT.isSimple())
3811 MVT SrcVT = SrcEVT.getSimpleVT();
3812 MVT DestVT = DestEVT.getSimpleVT();
3814 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
3817 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
3821 unsigned SrcReg = getRegForValue(Op);
3824 bool SrcIsKill = hasTrivialKill(Op);
3826 // If we're truncating from i64 to a smaller non-legal type then generate an
3827 // AND. Otherwise, we know the high bits are undefined and a truncate only
3828 // generate a COPY. We cannot mark the source register also as result
3829 // register, because this can incorrectly transfer the kill flag onto the
3832 if (SrcVT == MVT::i64) {
3834 switch (DestVT.SimpleTy) {
3836 // Trunc i64 to i32 is handled by the target-independent fast-isel.
3848 // Issue an extract_subreg to get the lower 32-bits.
3849 unsigned Reg32 = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
3851 // Create the AND instruction which performs the actual truncation.
3852 ResultReg = emitAnd_ri(MVT::i32, Reg32, /*IsKill=*/true, Mask);
3853 assert(ResultReg && "Unexpected AND instruction emission failure.");
3855 ResultReg = createResultReg(&AArch64::GPR32RegClass);
3856 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3857 TII.get(TargetOpcode::COPY), ResultReg)
3858 .addReg(SrcReg, getKillRegState(SrcIsKill));
3861 updateValueMap(I, ResultReg);
3865 unsigned AArch64FastISel::emiti1Ext(unsigned SrcReg, MVT DestVT, bool IsZExt) {
3866 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
3867 DestVT == MVT::i64) &&
3868 "Unexpected value type.");
3869 // Handle i8 and i16 as i32.
3870 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3874 unsigned ResultReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
3875 assert(ResultReg && "Unexpected AND instruction emission failure.");
3876 if (DestVT == MVT::i64) {
3877 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
3878 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
3879 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3880 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3881 TII.get(AArch64::SUBREG_TO_REG), Reg64)
3884 .addImm(AArch64::sub_32);
3889 if (DestVT == MVT::i64) {
3890 // FIXME: We're SExt i1 to i64.
3893 return fastEmitInst_rii(AArch64::SBFMWri, &AArch64::GPR32RegClass, SrcReg,
3894 /*TODO:IsKill=*/false, 0, 0);
3898 unsigned AArch64FastISel::emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3899 unsigned Op1, bool Op1IsKill) {
3901 switch (RetVT.SimpleTy) {
3907 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
3909 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
3912 const TargetRegisterClass *RC =
3913 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3914 return fastEmitInst_rrr(Opc, RC, Op0, Op0IsKill, Op1, Op1IsKill,
3915 /*IsKill=*/ZReg, true);
3918 unsigned AArch64FastISel::emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3919 unsigned Op1, bool Op1IsKill) {
3920 if (RetVT != MVT::i64)
3923 return fastEmitInst_rrr(AArch64::SMADDLrrr, &AArch64::GPR64RegClass,
3924 Op0, Op0IsKill, Op1, Op1IsKill,
3925 AArch64::XZR, /*IsKill=*/true);
3928 unsigned AArch64FastISel::emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3929 unsigned Op1, bool Op1IsKill) {
3930 if (RetVT != MVT::i64)
3933 return fastEmitInst_rrr(AArch64::UMADDLrrr, &AArch64::GPR64RegClass,
3934 Op0, Op0IsKill, Op1, Op1IsKill,
3935 AArch64::XZR, /*IsKill=*/true);
3938 unsigned AArch64FastISel::emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3939 unsigned Op1Reg, bool Op1IsKill) {
3941 bool NeedTrunc = false;
3943 switch (RetVT.SimpleTy) {
3945 case MVT::i8: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xff; break;
3946 case MVT::i16: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xffff; break;
3947 case MVT::i32: Opc = AArch64::LSLVWr; break;
3948 case MVT::i64: Opc = AArch64::LSLVXr; break;
3951 const TargetRegisterClass *RC =
3952 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3954 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3957 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3960 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3964 unsigned AArch64FastISel::emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3965 bool Op0IsKill, uint64_t Shift,
3967 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3968 "Unexpected source/return type pair.");
3969 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
3970 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
3971 "Unexpected source value type.");
3972 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3973 RetVT == MVT::i64) && "Unexpected return value type.");
3975 bool Is64Bit = (RetVT == MVT::i64);
3976 unsigned RegSize = Is64Bit ? 64 : 32;
3977 unsigned DstBits = RetVT.getSizeInBits();
3978 unsigned SrcBits = SrcVT.getSizeInBits();
3979 const TargetRegisterClass *RC =
3980 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3982 // Just emit a copy for "zero" shifts.
3984 if (RetVT == SrcVT) {
3985 unsigned ResultReg = createResultReg(RC);
3986 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3987 TII.get(TargetOpcode::COPY), ResultReg)
3988 .addReg(Op0, getKillRegState(Op0IsKill));
3991 return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
3994 // Don't deal with undefined shifts.
3995 if (Shift >= DstBits)
3998 // For immediate shifts we can fold the zero-/sign-extension into the shift.
3999 // {S|U}BFM Wd, Wn, #r, #s
4000 // Wd<32+s-r,32-r> = Wn<s:0> when r > s
4002 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4003 // %2 = shl i16 %1, 4
4004 // Wd<32+7-28,32-28> = Wn<7:0> <- clamp s to 7
4005 // 0b1111_1111_1111_1111__1111_1010_1010_0000 sext
4006 // 0b0000_0000_0000_0000__0000_0101_0101_0000 sext | zext
4007 // 0b0000_0000_0000_0000__0000_1010_1010_0000 zext
4009 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4010 // %2 = shl i16 %1, 8
4011 // Wd<32+7-24,32-24> = Wn<7:0>
4012 // 0b1111_1111_1111_1111__1010_1010_0000_0000 sext
4013 // 0b0000_0000_0000_0000__0101_0101_0000_0000 sext | zext
4014 // 0b0000_0000_0000_0000__1010_1010_0000_0000 zext
4016 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4017 // %2 = shl i16 %1, 12
4018 // Wd<32+3-20,32-20> = Wn<3:0>
4019 // 0b1111_1111_1111_1111__1010_0000_0000_0000 sext
4020 // 0b0000_0000_0000_0000__0101_0000_0000_0000 sext | zext
4021 // 0b0000_0000_0000_0000__1010_0000_0000_0000 zext
4023 unsigned ImmR = RegSize - Shift;
4024 // Limit the width to the length of the source type.
4025 unsigned ImmS = std::min<unsigned>(SrcBits - 1, DstBits - 1 - Shift);
4026 static const unsigned OpcTable[2][2] = {
4027 {AArch64::SBFMWri, AArch64::SBFMXri},
4028 {AArch64::UBFMWri, AArch64::UBFMXri}
4030 unsigned Opc = OpcTable[IsZExt][Is64Bit];
4031 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
4032 unsigned TmpReg = MRI.createVirtualRegister(RC);
4033 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4034 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4036 .addReg(Op0, getKillRegState(Op0IsKill))
4037 .addImm(AArch64::sub_32);
4041 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4044 unsigned AArch64FastISel::emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
4045 unsigned Op1Reg, bool Op1IsKill) {
4047 bool NeedTrunc = false;
4049 switch (RetVT.SimpleTy) {
4051 case MVT::i8: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xff; break;
4052 case MVT::i16: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xffff; break;
4053 case MVT::i32: Opc = AArch64::LSRVWr; break;
4054 case MVT::i64: Opc = AArch64::LSRVXr; break;
4057 const TargetRegisterClass *RC =
4058 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4060 Op0Reg = emitAnd_ri(MVT::i32, Op0Reg, Op0IsKill, Mask);
4061 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
4062 Op0IsKill = Op1IsKill = true;
4064 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
4067 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
4071 unsigned AArch64FastISel::emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
4072 bool Op0IsKill, uint64_t Shift,
4074 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
4075 "Unexpected source/return type pair.");
4076 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
4077 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
4078 "Unexpected source value type.");
4079 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
4080 RetVT == MVT::i64) && "Unexpected return value type.");
4082 bool Is64Bit = (RetVT == MVT::i64);
4083 unsigned RegSize = Is64Bit ? 64 : 32;
4084 unsigned DstBits = RetVT.getSizeInBits();
4085 unsigned SrcBits = SrcVT.getSizeInBits();
4086 const TargetRegisterClass *RC =
4087 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4089 // Just emit a copy for "zero" shifts.
4091 if (RetVT == SrcVT) {
4092 unsigned ResultReg = createResultReg(RC);
4093 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4094 TII.get(TargetOpcode::COPY), ResultReg)
4095 .addReg(Op0, getKillRegState(Op0IsKill));
4098 return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4101 // Don't deal with undefined shifts.
4102 if (Shift >= DstBits)
4105 // For immediate shifts we can fold the zero-/sign-extension into the shift.
4106 // {S|U}BFM Wd, Wn, #r, #s
4107 // Wd<s-r:0> = Wn<s:r> when r <= s
4109 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4110 // %2 = lshr i16 %1, 4
4111 // Wd<7-4:0> = Wn<7:4>
4112 // 0b0000_0000_0000_0000__0000_1111_1111_1010 sext
4113 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
4114 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
4116 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4117 // %2 = lshr i16 %1, 8
4118 // Wd<7-7,0> = Wn<7:7>
4119 // 0b0000_0000_0000_0000__0000_0000_1111_1111 sext
4120 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4121 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4123 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4124 // %2 = lshr i16 %1, 12
4125 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
4126 // 0b0000_0000_0000_0000__0000_0000_0000_1111 sext
4127 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4128 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4130 if (Shift >= SrcBits && IsZExt)
4131 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
4133 // It is not possible to fold a sign-extend into the LShr instruction. In this
4134 // case emit a sign-extend.
4136 Op0 = emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4141 SrcBits = SrcVT.getSizeInBits();
4145 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
4146 unsigned ImmS = SrcBits - 1;
4147 static const unsigned OpcTable[2][2] = {
4148 {AArch64::SBFMWri, AArch64::SBFMXri},
4149 {AArch64::UBFMWri, AArch64::UBFMXri}
4151 unsigned Opc = OpcTable[IsZExt][Is64Bit];
4152 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
4153 unsigned TmpReg = MRI.createVirtualRegister(RC);
4154 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4155 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4157 .addReg(Op0, getKillRegState(Op0IsKill))
4158 .addImm(AArch64::sub_32);
4162 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4165 unsigned AArch64FastISel::emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
4166 unsigned Op1Reg, bool Op1IsKill) {
4168 bool NeedTrunc = false;
4170 switch (RetVT.SimpleTy) {
4172 case MVT::i8: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xff; break;
4173 case MVT::i16: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xffff; break;
4174 case MVT::i32: Opc = AArch64::ASRVWr; break;
4175 case MVT::i64: Opc = AArch64::ASRVXr; break;
4178 const TargetRegisterClass *RC =
4179 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4181 Op0Reg = emitIntExt(RetVT, Op0Reg, MVT::i32, /*IsZExt=*/false);
4182 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
4183 Op0IsKill = Op1IsKill = true;
4185 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
4188 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
4192 unsigned AArch64FastISel::emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
4193 bool Op0IsKill, uint64_t Shift,
4195 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
4196 "Unexpected source/return type pair.");
4197 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
4198 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
4199 "Unexpected source value type.");
4200 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
4201 RetVT == MVT::i64) && "Unexpected return value type.");
4203 bool Is64Bit = (RetVT == MVT::i64);
4204 unsigned RegSize = Is64Bit ? 64 : 32;
4205 unsigned DstBits = RetVT.getSizeInBits();
4206 unsigned SrcBits = SrcVT.getSizeInBits();
4207 const TargetRegisterClass *RC =
4208 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4210 // Just emit a copy for "zero" shifts.
4212 if (RetVT == SrcVT) {
4213 unsigned ResultReg = createResultReg(RC);
4214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4215 TII.get(TargetOpcode::COPY), ResultReg)
4216 .addReg(Op0, getKillRegState(Op0IsKill));
4219 return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4222 // Don't deal with undefined shifts.
4223 if (Shift >= DstBits)
4226 // For immediate shifts we can fold the zero-/sign-extension into the shift.
4227 // {S|U}BFM Wd, Wn, #r, #s
4228 // Wd<s-r:0> = Wn<s:r> when r <= s
4230 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4231 // %2 = ashr i16 %1, 4
4232 // Wd<7-4:0> = Wn<7:4>
4233 // 0b1111_1111_1111_1111__1111_1111_1111_1010 sext
4234 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
4235 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
4237 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4238 // %2 = ashr i16 %1, 8
4239 // Wd<7-7,0> = Wn<7:7>
4240 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
4241 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4242 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4244 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4245 // %2 = ashr i16 %1, 12
4246 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
4247 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
4248 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4249 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4251 if (Shift >= SrcBits && IsZExt)
4252 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
4254 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
4255 unsigned ImmS = SrcBits - 1;
4256 static const unsigned OpcTable[2][2] = {
4257 {AArch64::SBFMWri, AArch64::SBFMXri},
4258 {AArch64::UBFMWri, AArch64::UBFMXri}
4260 unsigned Opc = OpcTable[IsZExt][Is64Bit];
4261 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
4262 unsigned TmpReg = MRI.createVirtualRegister(RC);
4263 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4264 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4266 .addReg(Op0, getKillRegState(Op0IsKill))
4267 .addImm(AArch64::sub_32);
4271 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4274 unsigned AArch64FastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
4276 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
4278 // FastISel does not have plumbing to deal with extensions where the SrcVT or
4279 // DestVT are odd things, so test to make sure that they are both types we can
4280 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
4281 // bail out to SelectionDAG.
4282 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
4283 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
4284 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
4285 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
4291 switch (SrcVT.SimpleTy) {
4295 return emiti1Ext(SrcReg, DestVT, IsZExt);
4297 if (DestVT == MVT::i64)
4298 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4300 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
4304 if (DestVT == MVT::i64)
4305 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4307 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
4311 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
4312 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4317 // Handle i8 and i16 as i32.
4318 if (DestVT == MVT::i8 || DestVT == MVT::i16)
4320 else if (DestVT == MVT::i64) {
4321 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
4322 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4323 TII.get(AArch64::SUBREG_TO_REG), Src64)
4326 .addImm(AArch64::sub_32);
4330 const TargetRegisterClass *RC =
4331 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4332 return fastEmitInst_rii(Opc, RC, SrcReg, /*TODO:IsKill=*/false, 0, Imm);
4335 static bool isZExtLoad(const MachineInstr *LI) {
4336 switch (LI->getOpcode()) {
4339 case AArch64::LDURBBi:
4340 case AArch64::LDURHHi:
4341 case AArch64::LDURWi:
4342 case AArch64::LDRBBui:
4343 case AArch64::LDRHHui:
4344 case AArch64::LDRWui:
4345 case AArch64::LDRBBroX:
4346 case AArch64::LDRHHroX:
4347 case AArch64::LDRWroX:
4348 case AArch64::LDRBBroW:
4349 case AArch64::LDRHHroW:
4350 case AArch64::LDRWroW:
4355 static bool isSExtLoad(const MachineInstr *LI) {
4356 switch (LI->getOpcode()) {
4359 case AArch64::LDURSBWi:
4360 case AArch64::LDURSHWi:
4361 case AArch64::LDURSBXi:
4362 case AArch64::LDURSHXi:
4363 case AArch64::LDURSWi:
4364 case AArch64::LDRSBWui:
4365 case AArch64::LDRSHWui:
4366 case AArch64::LDRSBXui:
4367 case AArch64::LDRSHXui:
4368 case AArch64::LDRSWui:
4369 case AArch64::LDRSBWroX:
4370 case AArch64::LDRSHWroX:
4371 case AArch64::LDRSBXroX:
4372 case AArch64::LDRSHXroX:
4373 case AArch64::LDRSWroX:
4374 case AArch64::LDRSBWroW:
4375 case AArch64::LDRSHWroW:
4376 case AArch64::LDRSBXroW:
4377 case AArch64::LDRSHXroW:
4378 case AArch64::LDRSWroW:
4383 bool AArch64FastISel::optimizeIntExtLoad(const Instruction *I, MVT RetVT,
4385 const auto *LI = dyn_cast<LoadInst>(I->getOperand(0));
4386 if (!LI || !LI->hasOneUse())
4389 // Check if the load instruction has already been selected.
4390 unsigned Reg = lookUpRegForValue(LI);
4394 MachineInstr *MI = MRI.getUniqueVRegDef(Reg);
4398 // Check if the correct load instruction has been emitted - SelectionDAG might
4399 // have emitted a zero-extending load, but we need a sign-extending load.
4400 bool IsZExt = isa<ZExtInst>(I);
4401 const auto *LoadMI = MI;
4402 if (LoadMI->getOpcode() == TargetOpcode::COPY &&
4403 LoadMI->getOperand(1).getSubReg() == AArch64::sub_32) {
4404 unsigned LoadReg = MI->getOperand(1).getReg();
4405 LoadMI = MRI.getUniqueVRegDef(LoadReg);
4406 assert(LoadMI && "Expected valid instruction");
4408 if (!(IsZExt && isZExtLoad(LoadMI)) && !(!IsZExt && isSExtLoad(LoadMI)))
4411 // Nothing to be done.
4412 if (RetVT != MVT::i64 || SrcVT > MVT::i32) {
4413 updateValueMap(I, Reg);
4418 unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
4419 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4420 TII.get(AArch64::SUBREG_TO_REG), Reg64)
4422 .addReg(Reg, getKillRegState(true))
4423 .addImm(AArch64::sub_32);
4426 assert((MI->getOpcode() == TargetOpcode::COPY &&
4427 MI->getOperand(1).getSubReg() == AArch64::sub_32) &&
4428 "Expected copy instruction");
4429 Reg = MI->getOperand(1).getReg();
4430 MI->eraseFromParent();
4432 updateValueMap(I, Reg);
4436 bool AArch64FastISel::selectIntExt(const Instruction *I) {
4437 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
4438 "Unexpected integer extend instruction.");
4441 if (!isTypeSupported(I->getType(), RetVT))
4444 if (!isTypeSupported(I->getOperand(0)->getType(), SrcVT))
4447 // Try to optimize already sign-/zero-extended values from load instructions.
4448 if (optimizeIntExtLoad(I, RetVT, SrcVT))
4451 unsigned SrcReg = getRegForValue(I->getOperand(0));
4454 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
4456 // Try to optimize already sign-/zero-extended values from function arguments.
4457 bool IsZExt = isa<ZExtInst>(I);
4458 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0))) {
4459 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr())) {
4460 if (RetVT == MVT::i64 && SrcVT != MVT::i64) {
4461 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
4462 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4463 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
4465 .addReg(SrcReg, getKillRegState(SrcIsKill))
4466 .addImm(AArch64::sub_32);
4469 // Conservatively clear all kill flags from all uses, because we are
4470 // replacing a sign-/zero-extend instruction at IR level with a nop at MI
4471 // level. The result of the instruction at IR level might have been
4472 // trivially dead, which is now not longer true.
4473 unsigned UseReg = lookUpRegForValue(I);
4475 MRI.clearKillFlags(UseReg);
4477 updateValueMap(I, SrcReg);
4482 unsigned ResultReg = emitIntExt(SrcVT, SrcReg, RetVT, IsZExt);
4486 updateValueMap(I, ResultReg);
4490 bool AArch64FastISel::selectRem(const Instruction *I, unsigned ISDOpcode) {
4491 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
4492 if (!DestEVT.isSimple())
4495 MVT DestVT = DestEVT.getSimpleVT();
4496 if (DestVT != MVT::i64 && DestVT != MVT::i32)
4500 bool Is64bit = (DestVT == MVT::i64);
4501 switch (ISDOpcode) {
4505 DivOpc = Is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
4508 DivOpc = Is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
4511 unsigned MSubOpc = Is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
4512 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4515 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4517 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4520 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4522 const TargetRegisterClass *RC =
4523 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4524 unsigned QuotReg = fastEmitInst_rr(DivOpc, RC, Src0Reg, /*IsKill=*/false,
4525 Src1Reg, /*IsKill=*/false);
4526 assert(QuotReg && "Unexpected DIV instruction emission failure.");
4527 // The remainder is computed as numerator - (quotient * denominator) using the
4528 // MSUB instruction.
4529 unsigned ResultReg = fastEmitInst_rrr(MSubOpc, RC, QuotReg, /*IsKill=*/true,
4530 Src1Reg, Src1IsKill, Src0Reg,
4532 updateValueMap(I, ResultReg);
4536 bool AArch64FastISel::selectMul(const Instruction *I) {
4538 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
4542 return selectBinaryOp(I, ISD::MUL);
4544 const Value *Src0 = I->getOperand(0);
4545 const Value *Src1 = I->getOperand(1);
4546 if (const auto *C = dyn_cast<ConstantInt>(Src0))
4547 if (C->getValue().isPowerOf2())
4548 std::swap(Src0, Src1);
4550 // Try to simplify to a shift instruction.
4551 if (const auto *C = dyn_cast<ConstantInt>(Src1))
4552 if (C->getValue().isPowerOf2()) {
4553 uint64_t ShiftVal = C->getValue().logBase2();
4556 if (const auto *ZExt = dyn_cast<ZExtInst>(Src0)) {
4557 if (!isIntExtFree(ZExt)) {
4559 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), VT)) {
4562 Src0 = ZExt->getOperand(0);
4565 } else if (const auto *SExt = dyn_cast<SExtInst>(Src0)) {
4566 if (!isIntExtFree(SExt)) {
4568 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), VT)) {
4571 Src0 = SExt->getOperand(0);
4576 unsigned Src0Reg = getRegForValue(Src0);
4579 bool Src0IsKill = hasTrivialKill(Src0);
4581 unsigned ResultReg =
4582 emitLSL_ri(VT, SrcVT, Src0Reg, Src0IsKill, ShiftVal, IsZExt);
4585 updateValueMap(I, ResultReg);
4590 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4593 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4595 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4598 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4600 unsigned ResultReg = emitMul_rr(VT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
4605 updateValueMap(I, ResultReg);
4609 bool AArch64FastISel::selectShift(const Instruction *I) {
4611 if (!isTypeSupported(I->getType(), RetVT, /*IsVectorAllowed=*/true))
4614 if (RetVT.isVector())
4615 return selectOperator(I, I->getOpcode());
4617 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
4618 unsigned ResultReg = 0;
4619 uint64_t ShiftVal = C->getZExtValue();
4621 bool IsZExt = I->getOpcode() != Instruction::AShr;
4622 const Value *Op0 = I->getOperand(0);
4623 if (const auto *ZExt = dyn_cast<ZExtInst>(Op0)) {
4624 if (!isIntExtFree(ZExt)) {
4626 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), TmpVT)) {
4629 Op0 = ZExt->getOperand(0);
4632 } else if (const auto *SExt = dyn_cast<SExtInst>(Op0)) {
4633 if (!isIntExtFree(SExt)) {
4635 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), TmpVT)) {
4638 Op0 = SExt->getOperand(0);
4643 unsigned Op0Reg = getRegForValue(Op0);
4646 bool Op0IsKill = hasTrivialKill(Op0);
4648 switch (I->getOpcode()) {
4649 default: llvm_unreachable("Unexpected instruction.");
4650 case Instruction::Shl:
4651 ResultReg = emitLSL_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4653 case Instruction::AShr:
4654 ResultReg = emitASR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4656 case Instruction::LShr:
4657 ResultReg = emitLSR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4663 updateValueMap(I, ResultReg);
4667 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4670 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4672 unsigned Op1Reg = getRegForValue(I->getOperand(1));
4675 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
4677 unsigned ResultReg = 0;
4678 switch (I->getOpcode()) {
4679 default: llvm_unreachable("Unexpected instruction.");
4680 case Instruction::Shl:
4681 ResultReg = emitLSL_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4683 case Instruction::AShr:
4684 ResultReg = emitASR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4686 case Instruction::LShr:
4687 ResultReg = emitLSR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4694 updateValueMap(I, ResultReg);
4698 bool AArch64FastISel::selectBitCast(const Instruction *I) {
4701 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
4703 if (!isTypeLegal(I->getType(), RetVT))
4707 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
4708 Opc = AArch64::FMOVWSr;
4709 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
4710 Opc = AArch64::FMOVXDr;
4711 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
4712 Opc = AArch64::FMOVSWr;
4713 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
4714 Opc = AArch64::FMOVDXr;
4718 const TargetRegisterClass *RC = nullptr;
4719 switch (RetVT.SimpleTy) {
4720 default: llvm_unreachable("Unexpected value type.");
4721 case MVT::i32: RC = &AArch64::GPR32RegClass; break;
4722 case MVT::i64: RC = &AArch64::GPR64RegClass; break;
4723 case MVT::f32: RC = &AArch64::FPR32RegClass; break;
4724 case MVT::f64: RC = &AArch64::FPR64RegClass; break;
4726 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4729 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4730 unsigned ResultReg = fastEmitInst_r(Opc, RC, Op0Reg, Op0IsKill);
4735 updateValueMap(I, ResultReg);
4739 bool AArch64FastISel::selectFRem(const Instruction *I) {
4741 if (!isTypeLegal(I->getType(), RetVT))
4745 switch (RetVT.SimpleTy) {
4749 LC = RTLIB::REM_F32;
4752 LC = RTLIB::REM_F64;
4757 Args.reserve(I->getNumOperands());
4759 // Populate the argument list.
4760 for (auto &Arg : I->operands()) {
4763 Entry.Ty = Arg->getType();
4764 Args.push_back(Entry);
4767 CallLoweringInfo CLI;
4768 MCContext &Ctx = MF->getContext();
4769 CLI.setCallee(DL, Ctx, TLI.getLibcallCallingConv(LC), I->getType(),
4770 TLI.getLibcallName(LC), std::move(Args));
4771 if (!lowerCallTo(CLI))
4773 updateValueMap(I, CLI.ResultReg);
4777 bool AArch64FastISel::selectSDiv(const Instruction *I) {
4779 if (!isTypeLegal(I->getType(), VT))
4782 if (!isa<ConstantInt>(I->getOperand(1)))
4783 return selectBinaryOp(I, ISD::SDIV);
4785 const APInt &C = cast<ConstantInt>(I->getOperand(1))->getValue();
4786 if ((VT != MVT::i32 && VT != MVT::i64) || !C ||
4787 !(C.isPowerOf2() || (-C).isPowerOf2()))
4788 return selectBinaryOp(I, ISD::SDIV);
4790 unsigned Lg2 = C.countTrailingZeros();
4791 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4794 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4796 if (cast<BinaryOperator>(I)->isExact()) {
4797 unsigned ResultReg = emitASR_ri(VT, VT, Src0Reg, Src0IsKill, Lg2);
4800 updateValueMap(I, ResultReg);
4804 int64_t Pow2MinusOne = (1ULL << Lg2) - 1;
4805 unsigned AddReg = emitAdd_ri_(VT, Src0Reg, /*IsKill=*/false, Pow2MinusOne);
4809 // (Src0 < 0) ? Pow2 - 1 : 0;
4810 if (!emitICmp_ri(VT, Src0Reg, /*IsKill=*/false, 0))
4814 const TargetRegisterClass *RC;
4815 if (VT == MVT::i64) {
4816 SelectOpc = AArch64::CSELXr;
4817 RC = &AArch64::GPR64RegClass;
4819 SelectOpc = AArch64::CSELWr;
4820 RC = &AArch64::GPR32RegClass;
4822 unsigned SelectReg =
4823 fastEmitInst_rri(SelectOpc, RC, AddReg, /*IsKill=*/true, Src0Reg,
4824 Src0IsKill, AArch64CC::LT);
4828 // Divide by Pow2 --> ashr. If we're dividing by a negative value we must also
4829 // negate the result.
4830 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
4833 ResultReg = emitAddSub_rs(/*UseAdd=*/false, VT, ZeroReg, /*IsKill=*/true,
4834 SelectReg, /*IsKill=*/true, AArch64_AM::ASR, Lg2);
4836 ResultReg = emitASR_ri(VT, VT, SelectReg, /*IsKill=*/true, Lg2);
4841 updateValueMap(I, ResultReg);
4845 /// This is mostly a copy of the existing FastISel getRegForGEPIndex code. We
4846 /// have to duplicate it for AArch64, because otherwise we would fail during the
4847 /// sign-extend emission.
4848 std::pair<unsigned, bool> AArch64FastISel::getRegForGEPIndex(const Value *Idx) {
4849 unsigned IdxN = getRegForValue(Idx);
4851 // Unhandled operand. Halt "fast" selection and bail.
4852 return std::pair<unsigned, bool>(0, false);
4854 bool IdxNIsKill = hasTrivialKill(Idx);
4856 // If the index is smaller or larger than intptr_t, truncate or extend it.
4857 MVT PtrVT = TLI.getPointerTy(DL);
4858 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
4859 if (IdxVT.bitsLT(PtrVT)) {
4860 IdxN = emitIntExt(IdxVT.getSimpleVT(), IdxN, PtrVT, /*IsZExt=*/false);
4862 } else if (IdxVT.bitsGT(PtrVT))
4863 llvm_unreachable("AArch64 FastISel doesn't support types larger than i64");
4864 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
4867 /// This is mostly a copy of the existing FastISel GEP code, but we have to
4868 /// duplicate it for AArch64, because otherwise we would bail out even for
4869 /// simple cases. This is because the standard fastEmit functions don't cover
4870 /// MUL at all and ADD is lowered very inefficientily.
4871 bool AArch64FastISel::selectGetElementPtr(const Instruction *I) {
4872 unsigned N = getRegForValue(I->getOperand(0));
4875 bool NIsKill = hasTrivialKill(I->getOperand(0));
4877 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
4878 // into a single N = N + TotalOffset.
4879 uint64_t TotalOffs = 0;
4880 Type *Ty = I->getOperand(0)->getType();
4881 MVT VT = TLI.getPointerTy(DL);
4882 for (auto OI = std::next(I->op_begin()), E = I->op_end(); OI != E; ++OI) {
4883 const Value *Idx = *OI;
4884 if (auto *StTy = dyn_cast<StructType>(Ty)) {
4885 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
4888 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
4889 Ty = StTy->getElementType(Field);
4891 Ty = cast<SequentialType>(Ty)->getElementType();
4892 // If this is a constant subscript, handle it quickly.
4893 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
4898 DL.getTypeAllocSize(Ty) * cast<ConstantInt>(CI)->getSExtValue();
4902 N = emitAdd_ri_(VT, N, NIsKill, TotalOffs);
4909 // N = N + Idx * ElementSize;
4910 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
4911 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
4912 unsigned IdxN = Pair.first;
4913 bool IdxNIsKill = Pair.second;
4917 if (ElementSize != 1) {
4918 unsigned C = fastEmit_i(VT, VT, ISD::Constant, ElementSize);
4921 IdxN = emitMul_rr(VT, IdxN, IdxNIsKill, C, true);
4926 N = fastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
4932 N = emitAdd_ri_(VT, N, NIsKill, TotalOffs);
4936 updateValueMap(I, N);
4940 bool AArch64FastISel::fastSelectInstruction(const Instruction *I) {
4941 switch (I->getOpcode()) {
4944 case Instruction::Add:
4945 case Instruction::Sub:
4946 return selectAddSub(I);
4947 case Instruction::Mul:
4948 return selectMul(I);
4949 case Instruction::SDiv:
4950 return selectSDiv(I);
4951 case Instruction::SRem:
4952 if (!selectBinaryOp(I, ISD::SREM))
4953 return selectRem(I, ISD::SREM);
4955 case Instruction::URem:
4956 if (!selectBinaryOp(I, ISD::UREM))
4957 return selectRem(I, ISD::UREM);
4959 case Instruction::Shl:
4960 case Instruction::LShr:
4961 case Instruction::AShr:
4962 return selectShift(I);
4963 case Instruction::And:
4964 case Instruction::Or:
4965 case Instruction::Xor:
4966 return selectLogicalOp(I);
4967 case Instruction::Br:
4968 return selectBranch(I);
4969 case Instruction::IndirectBr:
4970 return selectIndirectBr(I);
4971 case Instruction::BitCast:
4972 if (!FastISel::selectBitCast(I))
4973 return selectBitCast(I);
4975 case Instruction::FPToSI:
4976 if (!selectCast(I, ISD::FP_TO_SINT))
4977 return selectFPToInt(I, /*Signed=*/true);
4979 case Instruction::FPToUI:
4980 return selectFPToInt(I, /*Signed=*/false);
4981 case Instruction::ZExt:
4982 case Instruction::SExt:
4983 return selectIntExt(I);
4984 case Instruction::Trunc:
4985 if (!selectCast(I, ISD::TRUNCATE))
4986 return selectTrunc(I);
4988 case Instruction::FPExt:
4989 return selectFPExt(I);
4990 case Instruction::FPTrunc:
4991 return selectFPTrunc(I);
4992 case Instruction::SIToFP:
4993 if (!selectCast(I, ISD::SINT_TO_FP))
4994 return selectIntToFP(I, /*Signed=*/true);
4996 case Instruction::UIToFP:
4997 return selectIntToFP(I, /*Signed=*/false);
4998 case Instruction::Load:
4999 return selectLoad(I);
5000 case Instruction::Store:
5001 return selectStore(I);
5002 case Instruction::FCmp:
5003 case Instruction::ICmp:
5004 return selectCmp(I);
5005 case Instruction::Select:
5006 return selectSelect(I);
5007 case Instruction::Ret:
5008 return selectRet(I);
5009 case Instruction::FRem:
5010 return selectFRem(I);
5011 case Instruction::GetElementPtr:
5012 return selectGetElementPtr(I);
5015 // fall-back to target-independent instruction selection.
5016 return selectOperator(I, I->getOpcode());
5017 // Silence warnings.
5018 (void)&CC_AArch64_DarwinPCS_VarArg;
5022 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &FuncInfo,
5023 const TargetLibraryInfo *LibInfo) {
5024 return new AArch64FastISel(FuncInfo, LibInfo);