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 "AArch64RegisterInfo.h"
19 #include "AArch64Subtarget.h"
20 #include "MCTargetDesc/AArch64AddressingModes.h"
21 #include "Utils/AArch64BaseInfo.h"
22 #include "llvm/ADT/APFloat.h"
23 #include "llvm/ADT/APInt.h"
24 #include "llvm/ADT/DenseMap.h"
25 #include "llvm/ADT/SmallVector.h"
26 #include "llvm/Analysis/BranchProbabilityInfo.h"
27 #include "llvm/CodeGen/CallingConvLower.h"
28 #include "llvm/CodeGen/FastISel.h"
29 #include "llvm/CodeGen/FunctionLoweringInfo.h"
30 #include "llvm/CodeGen/ISDOpcodes.h"
31 #include "llvm/CodeGen/MachineBasicBlock.h"
32 #include "llvm/CodeGen/MachineConstantPool.h"
33 #include "llvm/CodeGen/MachineFrameInfo.h"
34 #include "llvm/CodeGen/MachineInstr.h"
35 #include "llvm/CodeGen/MachineInstrBuilder.h"
36 #include "llvm/CodeGen/MachineMemOperand.h"
37 #include "llvm/CodeGen/MachineRegisterInfo.h"
38 #include "llvm/CodeGen/MachineValueType.h"
39 #include "llvm/CodeGen/RuntimeLibcalls.h"
40 #include "llvm/CodeGen/ValueTypes.h"
41 #include "llvm/IR/Argument.h"
42 #include "llvm/IR/Attributes.h"
43 #include "llvm/IR/BasicBlock.h"
44 #include "llvm/IR/CallingConv.h"
45 #include "llvm/IR/Constant.h"
46 #include "llvm/IR/Constants.h"
47 #include "llvm/IR/DataLayout.h"
48 #include "llvm/IR/DerivedTypes.h"
49 #include "llvm/IR/Function.h"
50 #include "llvm/IR/GetElementPtrTypeIterator.h"
51 #include "llvm/IR/GlobalValue.h"
52 #include "llvm/IR/InstrTypes.h"
53 #include "llvm/IR/Instruction.h"
54 #include "llvm/IR/Instructions.h"
55 #include "llvm/IR/IntrinsicInst.h"
56 #include "llvm/IR/Operator.h"
57 #include "llvm/IR/Type.h"
58 #include "llvm/IR/User.h"
59 #include "llvm/IR/Value.h"
60 #include "llvm/MC/MCInstrDesc.h"
61 #include "llvm/MC/MCRegisterInfo.h"
62 #include "llvm/MC/MCSymbol.h"
63 #include "llvm/Support/AtomicOrdering.h"
64 #include "llvm/Support/Casting.h"
65 #include "llvm/Support/CodeGen.h"
66 #include "llvm/Support/ErrorHandling.h"
67 #include "llvm/Support/MathExtras.h"
78 class AArch64FastISel final : public FastISel {
87 BaseKind Kind = RegBase;
88 AArch64_AM::ShiftExtendType ExtType = AArch64_AM::InvalidShiftExtend;
93 unsigned OffsetReg = 0;
96 const GlobalValue *GV = nullptr;
99 Address() { Base.Reg = 0; }
101 void setKind(BaseKind K) { Kind = K; }
102 BaseKind getKind() const { return Kind; }
103 void setExtendType(AArch64_AM::ShiftExtendType E) { ExtType = E; }
104 AArch64_AM::ShiftExtendType getExtendType() const { return ExtType; }
105 bool isRegBase() const { return Kind == RegBase; }
106 bool isFIBase() const { return Kind == FrameIndexBase; }
108 void setReg(unsigned Reg) {
109 assert(isRegBase() && "Invalid base register access!");
113 unsigned getReg() const {
114 assert(isRegBase() && "Invalid base register access!");
118 void setOffsetReg(unsigned Reg) {
122 unsigned getOffsetReg() const {
126 void setFI(unsigned FI) {
127 assert(isFIBase() && "Invalid base frame index access!");
131 unsigned getFI() const {
132 assert(isFIBase() && "Invalid base frame index access!");
136 void setOffset(int64_t O) { Offset = O; }
137 int64_t getOffset() { return Offset; }
138 void setShift(unsigned S) { Shift = S; }
139 unsigned getShift() { return Shift; }
141 void setGlobalValue(const GlobalValue *G) { GV = G; }
142 const GlobalValue *getGlobalValue() { return GV; }
145 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
146 /// make the right decision when generating code for different targets.
147 const AArch64Subtarget *Subtarget;
148 LLVMContext *Context;
150 bool fastLowerArguments() override;
151 bool fastLowerCall(CallLoweringInfo &CLI) override;
152 bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
155 // Selection routines.
156 bool selectAddSub(const Instruction *I);
157 bool selectLogicalOp(const Instruction *I);
158 bool selectLoad(const Instruction *I);
159 bool selectStore(const Instruction *I);
160 bool selectBranch(const Instruction *I);
161 bool selectIndirectBr(const Instruction *I);
162 bool selectCmp(const Instruction *I);
163 bool selectSelect(const Instruction *I);
164 bool selectFPExt(const Instruction *I);
165 bool selectFPTrunc(const Instruction *I);
166 bool selectFPToInt(const Instruction *I, bool Signed);
167 bool selectIntToFP(const Instruction *I, bool Signed);
168 bool selectRem(const Instruction *I, unsigned ISDOpcode);
169 bool selectRet(const Instruction *I);
170 bool selectTrunc(const Instruction *I);
171 bool selectIntExt(const Instruction *I);
172 bool selectMul(const Instruction *I);
173 bool selectShift(const Instruction *I);
174 bool selectBitCast(const Instruction *I);
175 bool selectFRem(const Instruction *I);
176 bool selectSDiv(const Instruction *I);
177 bool selectGetElementPtr(const Instruction *I);
178 bool selectAtomicCmpXchg(const AtomicCmpXchgInst *I);
180 // Utility helper routines.
181 bool isTypeLegal(Type *Ty, MVT &VT);
182 bool isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed = false);
183 bool isValueAvailable(const Value *V) const;
184 bool computeAddress(const Value *Obj, Address &Addr, Type *Ty = nullptr);
185 bool computeCallAddress(const Value *V, Address &Addr);
186 bool simplifyAddress(Address &Addr, MVT VT);
187 void addLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
188 MachineMemOperand::Flags Flags,
189 unsigned ScaleFactor, MachineMemOperand *MMO);
190 bool isMemCpySmall(uint64_t Len, unsigned Alignment);
191 bool tryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
193 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
195 bool optimizeIntExtLoad(const Instruction *I, MVT RetVT, MVT SrcVT);
196 bool optimizeSelect(const SelectInst *SI);
197 std::pair<unsigned, bool> getRegForGEPIndex(const Value *Idx);
199 // Emit helper routines.
200 unsigned emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
201 const Value *RHS, bool SetFlags = false,
202 bool WantResult = true, bool IsZExt = false);
203 unsigned emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
204 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
205 bool SetFlags = false, bool WantResult = true);
206 unsigned emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
207 bool LHSIsKill, uint64_t Imm, bool SetFlags = false,
208 bool WantResult = true);
209 unsigned emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
210 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
211 AArch64_AM::ShiftExtendType ShiftType,
212 uint64_t ShiftImm, bool SetFlags = false,
213 bool WantResult = true);
214 unsigned emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
215 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
216 AArch64_AM::ShiftExtendType ExtType,
217 uint64_t ShiftImm, bool SetFlags = false,
218 bool WantResult = true);
221 bool emitCompareAndBranch(const BranchInst *BI);
222 bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
223 bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
224 bool emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
225 bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
226 unsigned emitLoad(MVT VT, MVT ResultVT, Address Addr, bool WantZExt = true,
227 MachineMemOperand *MMO = nullptr);
228 bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
229 MachineMemOperand *MMO = nullptr);
230 bool emitStoreRelease(MVT VT, unsigned SrcReg, unsigned AddrReg,
231 MachineMemOperand *MMO = nullptr);
232 unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
233 unsigned emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
234 unsigned emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
235 bool SetFlags = false, bool WantResult = true,
236 bool IsZExt = false);
237 unsigned emitAdd_ri_(MVT VT, unsigned Op0, bool Op0IsKill, int64_t Imm);
238 unsigned emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
239 bool SetFlags = false, bool WantResult = true,
240 bool IsZExt = false);
241 unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
242 unsigned RHSReg, bool RHSIsKill, bool WantResult = true);
243 unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
244 unsigned RHSReg, bool RHSIsKill,
245 AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
246 bool WantResult = true);
247 unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
249 unsigned emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
250 bool LHSIsKill, uint64_t Imm);
251 unsigned emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
252 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
254 unsigned emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
255 unsigned emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
256 unsigned Op1, bool Op1IsKill);
257 unsigned emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
258 unsigned Op1, bool Op1IsKill);
259 unsigned emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
260 unsigned Op1, bool Op1IsKill);
261 unsigned emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
262 unsigned Op1Reg, bool Op1IsKill);
263 unsigned emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
264 uint64_t Imm, bool IsZExt = true);
265 unsigned emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
266 unsigned Op1Reg, bool Op1IsKill);
267 unsigned emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
268 uint64_t Imm, bool IsZExt = true);
269 unsigned emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
270 unsigned Op1Reg, bool Op1IsKill);
271 unsigned emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
272 uint64_t Imm, bool IsZExt = false);
274 unsigned materializeInt(const ConstantInt *CI, MVT VT);
275 unsigned materializeFP(const ConstantFP *CFP, MVT VT);
276 unsigned materializeGV(const GlobalValue *GV);
278 // Call handling routines.
280 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
281 bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
283 bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
286 // Backend specific FastISel code.
287 unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
288 unsigned fastMaterializeConstant(const Constant *C) override;
289 unsigned fastMaterializeFloatZero(const ConstantFP* CF) override;
291 explicit AArch64FastISel(FunctionLoweringInfo &FuncInfo,
292 const TargetLibraryInfo *LibInfo)
293 : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
295 &static_cast<const AArch64Subtarget &>(FuncInfo.MF->getSubtarget());
296 Context = &FuncInfo.Fn->getContext();
299 bool fastSelectInstruction(const Instruction *I) override;
301 #include "AArch64GenFastISel.inc"
304 } // end anonymous namespace
306 #include "AArch64GenCallingConv.inc"
308 /// \brief Check if the sign-/zero-extend will be a noop.
309 static bool isIntExtFree(const Instruction *I) {
310 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
311 "Unexpected integer extend instruction.");
312 assert(!I->getType()->isVectorTy() && I->getType()->isIntegerTy() &&
313 "Unexpected value type.");
314 bool IsZExt = isa<ZExtInst>(I);
316 if (const auto *LI = dyn_cast<LoadInst>(I->getOperand(0)))
320 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0)))
321 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr()))
327 /// \brief Determine the implicit scale factor that is applied by a memory
328 /// operation for a given value type.
329 static unsigned getImplicitScaleFactor(MVT VT) {
330 switch (VT.SimpleTy) {
333 case MVT::i1: // fall-through
338 case MVT::i32: // fall-through
341 case MVT::i64: // fall-through
347 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
348 if (CC == CallingConv::WebKit_JS)
349 return CC_AArch64_WebKit_JS;
350 if (CC == CallingConv::GHC)
351 return CC_AArch64_GHC;
352 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
355 unsigned AArch64FastISel::fastMaterializeAlloca(const AllocaInst *AI) {
356 assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i64 &&
357 "Alloca should always return a pointer.");
359 // Don't handle dynamic allocas.
360 if (!FuncInfo.StaticAllocaMap.count(AI))
363 DenseMap<const AllocaInst *, int>::iterator SI =
364 FuncInfo.StaticAllocaMap.find(AI);
366 if (SI != FuncInfo.StaticAllocaMap.end()) {
367 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
368 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
370 .addFrameIndex(SI->second)
379 unsigned AArch64FastISel::materializeInt(const ConstantInt *CI, MVT VT) {
384 return fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
386 // Create a copy from the zero register to materialize a "0" value.
387 const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
388 : &AArch64::GPR32RegClass;
389 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
390 unsigned ResultReg = createResultReg(RC);
391 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
392 ResultReg).addReg(ZeroReg, getKillRegState(true));
396 unsigned AArch64FastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
397 // Positive zero (+0.0) has to be materialized with a fmov from the zero
398 // register, because the immediate version of fmov cannot encode zero.
399 if (CFP->isNullValue())
400 return fastMaterializeFloatZero(CFP);
402 if (VT != MVT::f32 && VT != MVT::f64)
405 const APFloat Val = CFP->getValueAPF();
406 bool Is64Bit = (VT == MVT::f64);
407 // This checks to see if we can use FMOV instructions to materialize
408 // a constant, otherwise we have to materialize via the constant pool.
409 if (TLI.isFPImmLegal(Val, VT)) {
411 Is64Bit ? AArch64_AM::getFP64Imm(Val) : AArch64_AM::getFP32Imm(Val);
412 assert((Imm != -1) && "Cannot encode floating-point constant.");
413 unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
414 return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
417 // For the MachO large code model materialize the FP constant in code.
418 if (Subtarget->isTargetMachO() && TM.getCodeModel() == CodeModel::Large) {
419 unsigned Opc1 = Is64Bit ? AArch64::MOVi64imm : AArch64::MOVi32imm;
420 const TargetRegisterClass *RC = Is64Bit ?
421 &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
423 unsigned TmpReg = createResultReg(RC);
424 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc1), TmpReg)
425 .addImm(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
427 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
428 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
429 TII.get(TargetOpcode::COPY), ResultReg)
430 .addReg(TmpReg, getKillRegState(true));
435 // Materialize via constant pool. MachineConstantPool wants an explicit
437 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
439 Align = DL.getTypeAllocSize(CFP->getType());
441 unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
442 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
443 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
444 ADRPReg).addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
446 unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
447 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
448 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
450 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
454 unsigned AArch64FastISel::materializeGV(const GlobalValue *GV) {
455 // We can't handle thread-local variables quickly yet.
456 if (GV->isThreadLocal())
459 // MachO still uses GOT for large code-model accesses, but ELF requires
460 // movz/movk sequences, which FastISel doesn't handle yet.
461 if (!Subtarget->useSmallAddressing() && !Subtarget->isTargetMachO())
464 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
466 EVT DestEVT = TLI.getValueType(DL, GV->getType(), true);
467 if (!DestEVT.isSimple())
470 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
473 if (OpFlags & AArch64II::MO_GOT) {
475 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
477 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
479 ResultReg = createResultReg(&AArch64::GPR64RegClass);
480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
483 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
487 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
489 .addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
491 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
492 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
495 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
501 unsigned AArch64FastISel::fastMaterializeConstant(const Constant *C) {
502 EVT CEVT = TLI.getValueType(DL, C->getType(), true);
504 // Only handle simple types.
505 if (!CEVT.isSimple())
507 MVT VT = CEVT.getSimpleVT();
509 if (const auto *CI = dyn_cast<ConstantInt>(C))
510 return materializeInt(CI, VT);
511 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
512 return materializeFP(CFP, VT);
513 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
514 return materializeGV(GV);
519 unsigned AArch64FastISel::fastMaterializeFloatZero(const ConstantFP* CFP) {
520 assert(CFP->isNullValue() &&
521 "Floating-point constant is not a positive zero.");
523 if (!isTypeLegal(CFP->getType(), VT))
526 if (VT != MVT::f32 && VT != MVT::f64)
529 bool Is64Bit = (VT == MVT::f64);
530 unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
531 unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
532 return fastEmitInst_r(Opc, TLI.getRegClassFor(VT), ZReg, /*IsKill=*/true);
535 /// \brief Check if the multiply is by a power-of-2 constant.
536 static bool isMulPowOf2(const Value *I) {
537 if (const auto *MI = dyn_cast<MulOperator>(I)) {
538 if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(0)))
539 if (C->getValue().isPowerOf2())
541 if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(1)))
542 if (C->getValue().isPowerOf2())
548 // Computes the address to get to an object.
549 bool AArch64FastISel::computeAddress(const Value *Obj, Address &Addr, Type *Ty)
551 const User *U = nullptr;
552 unsigned Opcode = Instruction::UserOp1;
553 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
554 // Don't walk into other basic blocks unless the object is an alloca from
555 // another block, otherwise it may not have a virtual register assigned.
556 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
557 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
558 Opcode = I->getOpcode();
561 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
562 Opcode = C->getOpcode();
566 if (auto *Ty = dyn_cast<PointerType>(Obj->getType()))
567 if (Ty->getAddressSpace() > 255)
568 // Fast instruction selection doesn't support the special
575 case Instruction::BitCast:
576 // Look through bitcasts.
577 return computeAddress(U->getOperand(0), Addr, Ty);
579 case Instruction::IntToPtr:
580 // Look past no-op inttoptrs.
581 if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
582 TLI.getPointerTy(DL))
583 return computeAddress(U->getOperand(0), Addr, Ty);
586 case Instruction::PtrToInt:
587 // Look past no-op ptrtoints.
588 if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
589 return computeAddress(U->getOperand(0), Addr, Ty);
592 case Instruction::GetElementPtr: {
593 Address SavedAddr = Addr;
594 uint64_t TmpOffset = Addr.getOffset();
596 // Iterate through the GEP folding the constants into offsets where
598 for (gep_type_iterator GTI = gep_type_begin(U), E = gep_type_end(U);
600 const Value *Op = GTI.getOperand();
601 if (StructType *STy = GTI.getStructTypeOrNull()) {
602 const StructLayout *SL = DL.getStructLayout(STy);
603 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
604 TmpOffset += SL->getElementOffset(Idx);
606 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
608 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
609 // Constant-offset addressing.
610 TmpOffset += CI->getSExtValue() * S;
613 if (canFoldAddIntoGEP(U, Op)) {
614 // A compatible add with a constant operand. Fold the constant.
616 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
617 TmpOffset += CI->getSExtValue() * S;
618 // Iterate on the other operand.
619 Op = cast<AddOperator>(Op)->getOperand(0);
623 goto unsupported_gep;
628 // Try to grab the base operand now.
629 Addr.setOffset(TmpOffset);
630 if (computeAddress(U->getOperand(0), Addr, Ty))
633 // We failed, restore everything and try the other options.
639 case Instruction::Alloca: {
640 const AllocaInst *AI = cast<AllocaInst>(Obj);
641 DenseMap<const AllocaInst *, int>::iterator SI =
642 FuncInfo.StaticAllocaMap.find(AI);
643 if (SI != FuncInfo.StaticAllocaMap.end()) {
644 Addr.setKind(Address::FrameIndexBase);
645 Addr.setFI(SI->second);
650 case Instruction::Add: {
651 // Adds of constants are common and easy enough.
652 const Value *LHS = U->getOperand(0);
653 const Value *RHS = U->getOperand(1);
655 if (isa<ConstantInt>(LHS))
658 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
659 Addr.setOffset(Addr.getOffset() + CI->getSExtValue());
660 return computeAddress(LHS, Addr, Ty);
663 Address Backup = Addr;
664 if (computeAddress(LHS, Addr, Ty) && computeAddress(RHS, Addr, Ty))
670 case Instruction::Sub: {
671 // Subs of constants are common and easy enough.
672 const Value *LHS = U->getOperand(0);
673 const Value *RHS = U->getOperand(1);
675 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
676 Addr.setOffset(Addr.getOffset() - CI->getSExtValue());
677 return computeAddress(LHS, Addr, Ty);
681 case Instruction::Shl: {
682 if (Addr.getOffsetReg())
685 const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1));
689 unsigned Val = CI->getZExtValue();
690 if (Val < 1 || Val > 3)
693 uint64_t NumBytes = 0;
694 if (Ty && Ty->isSized()) {
695 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
696 NumBytes = NumBits / 8;
697 if (!isPowerOf2_64(NumBits))
701 if (NumBytes != (1ULL << Val))
705 Addr.setExtendType(AArch64_AM::LSL);
707 const Value *Src = U->getOperand(0);
708 if (const auto *I = dyn_cast<Instruction>(Src)) {
709 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
710 // Fold the zext or sext when it won't become a noop.
711 if (const auto *ZE = dyn_cast<ZExtInst>(I)) {
712 if (!isIntExtFree(ZE) &&
713 ZE->getOperand(0)->getType()->isIntegerTy(32)) {
714 Addr.setExtendType(AArch64_AM::UXTW);
715 Src = ZE->getOperand(0);
717 } else if (const auto *SE = dyn_cast<SExtInst>(I)) {
718 if (!isIntExtFree(SE) &&
719 SE->getOperand(0)->getType()->isIntegerTy(32)) {
720 Addr.setExtendType(AArch64_AM::SXTW);
721 Src = SE->getOperand(0);
727 if (const auto *AI = dyn_cast<BinaryOperator>(Src))
728 if (AI->getOpcode() == Instruction::And) {
729 const Value *LHS = AI->getOperand(0);
730 const Value *RHS = AI->getOperand(1);
732 if (const auto *C = dyn_cast<ConstantInt>(LHS))
733 if (C->getValue() == 0xffffffff)
736 if (const auto *C = dyn_cast<ConstantInt>(RHS))
737 if (C->getValue() == 0xffffffff) {
738 Addr.setExtendType(AArch64_AM::UXTW);
739 unsigned Reg = getRegForValue(LHS);
742 bool RegIsKill = hasTrivialKill(LHS);
743 Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
745 Addr.setOffsetReg(Reg);
750 unsigned Reg = getRegForValue(Src);
753 Addr.setOffsetReg(Reg);
756 case Instruction::Mul: {
757 if (Addr.getOffsetReg())
763 const Value *LHS = U->getOperand(0);
764 const Value *RHS = U->getOperand(1);
766 // Canonicalize power-of-2 value to the RHS.
767 if (const auto *C = dyn_cast<ConstantInt>(LHS))
768 if (C->getValue().isPowerOf2())
771 assert(isa<ConstantInt>(RHS) && "Expected an ConstantInt.");
772 const auto *C = cast<ConstantInt>(RHS);
773 unsigned Val = C->getValue().logBase2();
774 if (Val < 1 || Val > 3)
777 uint64_t NumBytes = 0;
778 if (Ty && Ty->isSized()) {
779 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
780 NumBytes = NumBits / 8;
781 if (!isPowerOf2_64(NumBits))
785 if (NumBytes != (1ULL << Val))
789 Addr.setExtendType(AArch64_AM::LSL);
791 const Value *Src = LHS;
792 if (const auto *I = dyn_cast<Instruction>(Src)) {
793 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
794 // Fold the zext or sext when it won't become a noop.
795 if (const auto *ZE = dyn_cast<ZExtInst>(I)) {
796 if (!isIntExtFree(ZE) &&
797 ZE->getOperand(0)->getType()->isIntegerTy(32)) {
798 Addr.setExtendType(AArch64_AM::UXTW);
799 Src = ZE->getOperand(0);
801 } else if (const auto *SE = dyn_cast<SExtInst>(I)) {
802 if (!isIntExtFree(SE) &&
803 SE->getOperand(0)->getType()->isIntegerTy(32)) {
804 Addr.setExtendType(AArch64_AM::SXTW);
805 Src = SE->getOperand(0);
811 unsigned Reg = getRegForValue(Src);
814 Addr.setOffsetReg(Reg);
817 case Instruction::And: {
818 if (Addr.getOffsetReg())
821 if (!Ty || DL.getTypeSizeInBits(Ty) != 8)
824 const Value *LHS = U->getOperand(0);
825 const Value *RHS = U->getOperand(1);
827 if (const auto *C = dyn_cast<ConstantInt>(LHS))
828 if (C->getValue() == 0xffffffff)
831 if (const auto *C = dyn_cast<ConstantInt>(RHS))
832 if (C->getValue() == 0xffffffff) {
834 Addr.setExtendType(AArch64_AM::LSL);
835 Addr.setExtendType(AArch64_AM::UXTW);
837 unsigned Reg = getRegForValue(LHS);
840 bool RegIsKill = hasTrivialKill(LHS);
841 Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
843 Addr.setOffsetReg(Reg);
848 case Instruction::SExt:
849 case Instruction::ZExt: {
850 if (!Addr.getReg() || Addr.getOffsetReg())
853 const Value *Src = nullptr;
854 // Fold the zext or sext when it won't become a noop.
855 if (const auto *ZE = dyn_cast<ZExtInst>(U)) {
856 if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
857 Addr.setExtendType(AArch64_AM::UXTW);
858 Src = ZE->getOperand(0);
860 } else if (const auto *SE = dyn_cast<SExtInst>(U)) {
861 if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
862 Addr.setExtendType(AArch64_AM::SXTW);
863 Src = SE->getOperand(0);
871 unsigned Reg = getRegForValue(Src);
874 Addr.setOffsetReg(Reg);
879 if (Addr.isRegBase() && !Addr.getReg()) {
880 unsigned Reg = getRegForValue(Obj);
887 if (!Addr.getOffsetReg()) {
888 unsigned Reg = getRegForValue(Obj);
891 Addr.setOffsetReg(Reg);
898 bool AArch64FastISel::computeCallAddress(const Value *V, Address &Addr) {
899 const User *U = nullptr;
900 unsigned Opcode = Instruction::UserOp1;
903 if (const auto *I = dyn_cast<Instruction>(V)) {
904 Opcode = I->getOpcode();
906 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
907 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
908 Opcode = C->getOpcode();
914 case Instruction::BitCast:
915 // Look past bitcasts if its operand is in the same BB.
917 return computeCallAddress(U->getOperand(0), Addr);
919 case Instruction::IntToPtr:
920 // Look past no-op inttoptrs if its operand is in the same BB.
922 TLI.getValueType(DL, U->getOperand(0)->getType()) ==
923 TLI.getPointerTy(DL))
924 return computeCallAddress(U->getOperand(0), Addr);
926 case Instruction::PtrToInt:
927 // Look past no-op ptrtoints if its operand is in the same BB.
928 if (InMBB && TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
929 return computeCallAddress(U->getOperand(0), Addr);
933 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
934 Addr.setGlobalValue(GV);
938 // If all else fails, try to materialize the value in a register.
939 if (!Addr.getGlobalValue()) {
940 Addr.setReg(getRegForValue(V));
941 return Addr.getReg() != 0;
948 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
949 EVT evt = TLI.getValueType(DL, Ty, true);
951 // Only handle simple types.
952 if (evt == MVT::Other || !evt.isSimple())
954 VT = evt.getSimpleVT();
956 // This is a legal type, but it's not something we handle in fast-isel.
960 // Handle all other legal types, i.e. a register that will directly hold this
962 return TLI.isTypeLegal(VT);
965 /// \brief Determine if the value type is supported by FastISel.
967 /// FastISel for AArch64 can handle more value types than are legal. This adds
968 /// simple value type such as i1, i8, and i16.
969 bool AArch64FastISel::isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed) {
970 if (Ty->isVectorTy() && !IsVectorAllowed)
973 if (isTypeLegal(Ty, VT))
976 // If this is a type than can be sign or zero-extended to a basic operation
977 // go ahead and accept it now.
978 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
984 bool AArch64FastISel::isValueAvailable(const Value *V) const {
985 if (!isa<Instruction>(V))
988 const auto *I = cast<Instruction>(V);
989 return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
992 bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) {
993 unsigned ScaleFactor = getImplicitScaleFactor(VT);
997 bool ImmediateOffsetNeedsLowering = false;
998 bool RegisterOffsetNeedsLowering = false;
999 int64_t Offset = Addr.getOffset();
1000 if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
1001 ImmediateOffsetNeedsLowering = true;
1002 else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
1003 !isUInt<12>(Offset / ScaleFactor))
1004 ImmediateOffsetNeedsLowering = true;
1006 // Cannot encode an offset register and an immediate offset in the same
1007 // instruction. Fold the immediate offset into the load/store instruction and
1008 // emit an additional add to take care of the offset register.
1009 if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.getOffsetReg())
1010 RegisterOffsetNeedsLowering = true;
1012 // Cannot encode zero register as base.
1013 if (Addr.isRegBase() && Addr.getOffsetReg() && !Addr.getReg())
1014 RegisterOffsetNeedsLowering = true;
1016 // If this is a stack pointer and the offset needs to be simplified then put
1017 // the alloca address into a register, set the base type back to register and
1018 // continue. This should almost never happen.
1019 if ((ImmediateOffsetNeedsLowering || Addr.getOffsetReg()) && Addr.isFIBase())
1021 unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
1022 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
1024 .addFrameIndex(Addr.getFI())
1027 Addr.setKind(Address::RegBase);
1028 Addr.setReg(ResultReg);
1031 if (RegisterOffsetNeedsLowering) {
1032 unsigned ResultReg = 0;
1033 if (Addr.getReg()) {
1034 if (Addr.getExtendType() == AArch64_AM::SXTW ||
1035 Addr.getExtendType() == AArch64_AM::UXTW )
1036 ResultReg = emitAddSub_rx(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
1037 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
1038 /*TODO:IsKill=*/false, Addr.getExtendType(),
1041 ResultReg = emitAddSub_rs(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
1042 /*TODO:IsKill=*/false, Addr.getOffsetReg(),
1043 /*TODO:IsKill=*/false, AArch64_AM::LSL,
1046 if (Addr.getExtendType() == AArch64_AM::UXTW)
1047 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
1048 /*Op0IsKill=*/false, Addr.getShift(),
1050 else if (Addr.getExtendType() == AArch64_AM::SXTW)
1051 ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
1052 /*Op0IsKill=*/false, Addr.getShift(),
1055 ResultReg = emitLSL_ri(MVT::i64, MVT::i64, Addr.getOffsetReg(),
1056 /*Op0IsKill=*/false, Addr.getShift());
1061 Addr.setReg(ResultReg);
1062 Addr.setOffsetReg(0);
1064 Addr.setExtendType(AArch64_AM::InvalidShiftExtend);
1067 // Since the offset is too large for the load/store instruction get the
1068 // reg+offset into a register.
1069 if (ImmediateOffsetNeedsLowering) {
1072 // Try to fold the immediate into the add instruction.
1073 ResultReg = emitAdd_ri_(MVT::i64, Addr.getReg(), /*IsKill=*/false, Offset);
1075 ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
1079 Addr.setReg(ResultReg);
1085 void AArch64FastISel::addLoadStoreOperands(Address &Addr,
1086 const MachineInstrBuilder &MIB,
1087 MachineMemOperand::Flags Flags,
1088 unsigned ScaleFactor,
1089 MachineMemOperand *MMO) {
1090 int64_t Offset = Addr.getOffset() / ScaleFactor;
1091 // Frame base works a bit differently. Handle it separately.
1092 if (Addr.isFIBase()) {
1093 int FI = Addr.getFI();
1094 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
1095 // and alignment should be based on the VT.
1096 MMO = FuncInfo.MF->getMachineMemOperand(
1097 MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags,
1098 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
1099 // Now add the rest of the operands.
1100 MIB.addFrameIndex(FI).addImm(Offset);
1102 assert(Addr.isRegBase() && "Unexpected address kind.");
1103 const MCInstrDesc &II = MIB->getDesc();
1104 unsigned Idx = (Flags & MachineMemOperand::MOStore) ? 1 : 0;
1106 constrainOperandRegClass(II, Addr.getReg(), II.getNumDefs()+Idx));
1108 constrainOperandRegClass(II, Addr.getOffsetReg(), II.getNumDefs()+Idx+1));
1109 if (Addr.getOffsetReg()) {
1110 assert(Addr.getOffset() == 0 && "Unexpected offset");
1111 bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
1112 Addr.getExtendType() == AArch64_AM::SXTX;
1113 MIB.addReg(Addr.getReg());
1114 MIB.addReg(Addr.getOffsetReg());
1115 MIB.addImm(IsSigned);
1116 MIB.addImm(Addr.getShift() != 0);
1118 MIB.addReg(Addr.getReg()).addImm(Offset);
1122 MIB.addMemOperand(MMO);
1125 unsigned AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
1126 const Value *RHS, bool SetFlags,
1127 bool WantResult, bool IsZExt) {
1128 AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
1129 bool NeedExtend = false;
1130 switch (RetVT.SimpleTy) {
1138 ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
1142 ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
1144 case MVT::i32: // fall-through
1149 RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
1151 // Canonicalize immediates to the RHS first.
1152 if (UseAdd && isa<Constant>(LHS) && !isa<Constant>(RHS))
1153 std::swap(LHS, RHS);
1155 // Canonicalize mul by power of 2 to the RHS.
1156 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1157 if (isMulPowOf2(LHS))
1158 std::swap(LHS, RHS);
1160 // Canonicalize shift immediate to the RHS.
1161 if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1162 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
1163 if (isa<ConstantInt>(SI->getOperand(1)))
1164 if (SI->getOpcode() == Instruction::Shl ||
1165 SI->getOpcode() == Instruction::LShr ||
1166 SI->getOpcode() == Instruction::AShr )
1167 std::swap(LHS, RHS);
1169 unsigned LHSReg = getRegForValue(LHS);
1172 bool LHSIsKill = hasTrivialKill(LHS);
1175 LHSReg = emitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
1177 unsigned ResultReg = 0;
1178 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1179 uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
1180 if (C->isNegative())
1181 ResultReg = emitAddSub_ri(!UseAdd, RetVT, LHSReg, LHSIsKill, -Imm,
1182 SetFlags, WantResult);
1184 ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, Imm, SetFlags,
1186 } else if (const auto *C = dyn_cast<Constant>(RHS))
1187 if (C->isNullValue())
1188 ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, 0, SetFlags,
1194 // Only extend the RHS within the instruction if there is a valid extend type.
1195 if (ExtendType != AArch64_AM::InvalidShiftExtend && RHS->hasOneUse() &&
1196 isValueAvailable(RHS)) {
1197 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
1198 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
1199 if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
1200 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1203 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1204 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1205 RHSIsKill, ExtendType, C->getZExtValue(),
1206 SetFlags, WantResult);
1208 unsigned RHSReg = getRegForValue(RHS);
1211 bool RHSIsKill = hasTrivialKill(RHS);
1212 return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1213 ExtendType, 0, SetFlags, WantResult);
1216 // Check if the mul can be folded into the instruction.
1217 if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1218 if (isMulPowOf2(RHS)) {
1219 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1220 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1222 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1223 if (C->getValue().isPowerOf2())
1224 std::swap(MulLHS, MulRHS);
1226 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1227 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1228 unsigned RHSReg = getRegForValue(MulLHS);
1231 bool RHSIsKill = hasTrivialKill(MulLHS);
1232 ResultReg = emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1233 RHSIsKill, AArch64_AM::LSL, ShiftVal, SetFlags,
1240 // Check if the shift can be folded into the instruction.
1241 if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1242 if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
1243 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1244 AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
1245 switch (SI->getOpcode()) {
1247 case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
1248 case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
1249 case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
1251 uint64_t ShiftVal = C->getZExtValue();
1252 if (ShiftType != AArch64_AM::InvalidShiftExtend) {
1253 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1256 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1257 ResultReg = emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1258 RHSIsKill, ShiftType, ShiftVal, SetFlags,
1267 unsigned RHSReg = getRegForValue(RHS);
1270 bool RHSIsKill = hasTrivialKill(RHS);
1273 RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
1275 return emitAddSub_rr(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1276 SetFlags, WantResult);
1279 unsigned AArch64FastISel::emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
1280 bool LHSIsKill, unsigned RHSReg,
1281 bool RHSIsKill, bool SetFlags,
1283 assert(LHSReg && RHSReg && "Invalid register number.");
1285 if (LHSReg == AArch64::SP || LHSReg == AArch64::WSP ||
1286 RHSReg == AArch64::SP || RHSReg == AArch64::WSP)
1289 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1292 static const unsigned OpcTable[2][2][2] = {
1293 { { AArch64::SUBWrr, AArch64::SUBXrr },
1294 { AArch64::ADDWrr, AArch64::ADDXrr } },
1295 { { AArch64::SUBSWrr, AArch64::SUBSXrr },
1296 { AArch64::ADDSWrr, AArch64::ADDSXrr } }
1298 bool Is64Bit = RetVT == MVT::i64;
1299 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1300 const TargetRegisterClass *RC =
1301 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1304 ResultReg = createResultReg(RC);
1306 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1308 const MCInstrDesc &II = TII.get(Opc);
1309 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1310 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1311 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1312 .addReg(LHSReg, getKillRegState(LHSIsKill))
1313 .addReg(RHSReg, getKillRegState(RHSIsKill));
1317 unsigned AArch64FastISel::emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
1318 bool LHSIsKill, uint64_t Imm,
1319 bool SetFlags, bool WantResult) {
1320 assert(LHSReg && "Invalid register number.");
1322 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1326 if (isUInt<12>(Imm))
1328 else if ((Imm & 0xfff000) == Imm) {
1334 static const unsigned OpcTable[2][2][2] = {
1335 { { AArch64::SUBWri, AArch64::SUBXri },
1336 { AArch64::ADDWri, AArch64::ADDXri } },
1337 { { AArch64::SUBSWri, AArch64::SUBSXri },
1338 { AArch64::ADDSWri, AArch64::ADDSXri } }
1340 bool Is64Bit = RetVT == MVT::i64;
1341 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1342 const TargetRegisterClass *RC;
1344 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1346 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
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 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1356 .addReg(LHSReg, getKillRegState(LHSIsKill))
1358 .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
1362 unsigned AArch64FastISel::emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
1363 bool LHSIsKill, unsigned RHSReg,
1365 AArch64_AM::ShiftExtendType ShiftType,
1366 uint64_t ShiftImm, bool SetFlags,
1368 assert(LHSReg && RHSReg && "Invalid register number.");
1369 assert(LHSReg != AArch64::SP && LHSReg != AArch64::WSP &&
1370 RHSReg != AArch64::SP && RHSReg != AArch64::WSP);
1372 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1375 // Don't deal with undefined shifts.
1376 if (ShiftImm >= RetVT.getSizeInBits())
1379 static const unsigned OpcTable[2][2][2] = {
1380 { { AArch64::SUBWrs, AArch64::SUBXrs },
1381 { AArch64::ADDWrs, AArch64::ADDXrs } },
1382 { { AArch64::SUBSWrs, AArch64::SUBSXrs },
1383 { AArch64::ADDSWrs, AArch64::ADDSXrs } }
1385 bool Is64Bit = RetVT == MVT::i64;
1386 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1387 const TargetRegisterClass *RC =
1388 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1391 ResultReg = createResultReg(RC);
1393 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1395 const MCInstrDesc &II = TII.get(Opc);
1396 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1397 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1398 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1399 .addReg(LHSReg, getKillRegState(LHSIsKill))
1400 .addReg(RHSReg, getKillRegState(RHSIsKill))
1401 .addImm(getShifterImm(ShiftType, ShiftImm));
1405 unsigned AArch64FastISel::emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
1406 bool LHSIsKill, unsigned RHSReg,
1408 AArch64_AM::ShiftExtendType ExtType,
1409 uint64_t ShiftImm, bool SetFlags,
1411 assert(LHSReg && RHSReg && "Invalid register number.");
1412 assert(LHSReg != AArch64::XZR && LHSReg != AArch64::WZR &&
1413 RHSReg != AArch64::XZR && RHSReg != AArch64::WZR);
1415 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1421 static const unsigned OpcTable[2][2][2] = {
1422 { { AArch64::SUBWrx, AArch64::SUBXrx },
1423 { AArch64::ADDWrx, AArch64::ADDXrx } },
1424 { { AArch64::SUBSWrx, AArch64::SUBSXrx },
1425 { AArch64::ADDSWrx, AArch64::ADDSXrx } }
1427 bool Is64Bit = RetVT == MVT::i64;
1428 unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1429 const TargetRegisterClass *RC = nullptr;
1431 RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1433 RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1436 ResultReg = createResultReg(RC);
1438 ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1440 const MCInstrDesc &II = TII.get(Opc);
1441 LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1442 RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1443 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1444 .addReg(LHSReg, getKillRegState(LHSIsKill))
1445 .addReg(RHSReg, getKillRegState(RHSIsKill))
1446 .addImm(getArithExtendImm(ExtType, ShiftImm));
1450 bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1451 Type *Ty = LHS->getType();
1452 EVT EVT = TLI.getValueType(DL, Ty, true);
1453 if (!EVT.isSimple())
1455 MVT VT = EVT.getSimpleVT();
1457 switch (VT.SimpleTy) {
1465 return emitICmp(VT, LHS, RHS, IsZExt);
1468 return emitFCmp(VT, LHS, RHS);
1472 bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1474 return emitSub(RetVT, LHS, RHS, /*SetFlags=*/true, /*WantResult=*/false,
1478 bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1480 return emitAddSub_ri(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, Imm,
1481 /*SetFlags=*/true, /*WantResult=*/false) != 0;
1484 bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1485 if (RetVT != MVT::f32 && RetVT != MVT::f64)
1488 // Check to see if the 2nd operand is a constant that we can encode directly
1490 bool UseImm = false;
1491 if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1492 if (CFP->isZero() && !CFP->isNegative())
1495 unsigned LHSReg = getRegForValue(LHS);
1498 bool LHSIsKill = hasTrivialKill(LHS);
1501 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1502 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1503 .addReg(LHSReg, getKillRegState(LHSIsKill));
1507 unsigned RHSReg = getRegForValue(RHS);
1510 bool RHSIsKill = hasTrivialKill(RHS);
1512 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1513 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1514 .addReg(LHSReg, getKillRegState(LHSIsKill))
1515 .addReg(RHSReg, getKillRegState(RHSIsKill));
1519 unsigned AArch64FastISel::emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
1520 bool SetFlags, bool WantResult, bool IsZExt) {
1521 return emitAddSub(/*UseAdd=*/true, RetVT, LHS, RHS, SetFlags, WantResult,
1525 /// \brief This method is a wrapper to simplify add emission.
1527 /// First try to emit an add with an immediate operand using emitAddSub_ri. If
1528 /// that fails, then try to materialize the immediate into a register and use
1529 /// emitAddSub_rr instead.
1530 unsigned AArch64FastISel::emitAdd_ri_(MVT VT, unsigned Op0, bool Op0IsKill,
1534 ResultReg = emitAddSub_ri(false, VT, Op0, Op0IsKill, -Imm);
1536 ResultReg = emitAddSub_ri(true, VT, Op0, Op0IsKill, Imm);
1541 unsigned CReg = fastEmit_i(VT, VT, ISD::Constant, Imm);
1545 ResultReg = emitAddSub_rr(true, VT, Op0, Op0IsKill, CReg, true);
1549 unsigned AArch64FastISel::emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
1550 bool SetFlags, bool WantResult, bool IsZExt) {
1551 return emitAddSub(/*UseAdd=*/false, RetVT, LHS, RHS, SetFlags, WantResult,
1555 unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1556 bool LHSIsKill, unsigned RHSReg,
1557 bool RHSIsKill, bool WantResult) {
1558 return emitAddSub_rr(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1559 RHSIsKill, /*SetFlags=*/true, WantResult);
1562 unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1563 bool LHSIsKill, unsigned RHSReg,
1565 AArch64_AM::ShiftExtendType ShiftType,
1566 uint64_t ShiftImm, bool WantResult) {
1567 return emitAddSub_rs(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1568 RHSIsKill, ShiftType, ShiftImm, /*SetFlags=*/true,
1572 unsigned AArch64FastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
1573 const Value *LHS, const Value *RHS) {
1574 // Canonicalize immediates to the RHS first.
1575 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1576 std::swap(LHS, RHS);
1578 // Canonicalize mul by power-of-2 to the RHS.
1579 if (LHS->hasOneUse() && isValueAvailable(LHS))
1580 if (isMulPowOf2(LHS))
1581 std::swap(LHS, RHS);
1583 // Canonicalize shift immediate to the RHS.
1584 if (LHS->hasOneUse() && isValueAvailable(LHS))
1585 if (const auto *SI = dyn_cast<ShlOperator>(LHS))
1586 if (isa<ConstantInt>(SI->getOperand(1)))
1587 std::swap(LHS, RHS);
1589 unsigned LHSReg = getRegForValue(LHS);
1592 bool LHSIsKill = hasTrivialKill(LHS);
1594 unsigned ResultReg = 0;
1595 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1596 uint64_t Imm = C->getZExtValue();
1597 ResultReg = emitLogicalOp_ri(ISDOpc, RetVT, LHSReg, LHSIsKill, Imm);
1602 // Check if the mul can be folded into the instruction.
1603 if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1604 if (isMulPowOf2(RHS)) {
1605 const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1606 const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1608 if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1609 if (C->getValue().isPowerOf2())
1610 std::swap(MulLHS, MulRHS);
1612 assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1613 uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1615 unsigned RHSReg = getRegForValue(MulLHS);
1618 bool RHSIsKill = hasTrivialKill(MulLHS);
1619 ResultReg = emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1620 RHSIsKill, ShiftVal);
1626 // Check if the shift can be folded into the instruction.
1627 if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1628 if (const auto *SI = dyn_cast<ShlOperator>(RHS))
1629 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1630 uint64_t ShiftVal = C->getZExtValue();
1631 unsigned RHSReg = getRegForValue(SI->getOperand(0));
1634 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1635 ResultReg = emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1636 RHSIsKill, ShiftVal);
1642 unsigned RHSReg = getRegForValue(RHS);
1645 bool RHSIsKill = hasTrivialKill(RHS);
1647 MVT VT = std::max(MVT::i32, RetVT.SimpleTy);
1648 ResultReg = fastEmit_rr(VT, VT, ISDOpc, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1649 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1650 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1651 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1656 unsigned AArch64FastISel::emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT,
1657 unsigned LHSReg, bool LHSIsKill,
1659 static_assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR),
1660 "ISD nodes are not consecutive!");
1661 static const unsigned OpcTable[3][2] = {
1662 { AArch64::ANDWri, AArch64::ANDXri },
1663 { AArch64::ORRWri, AArch64::ORRXri },
1664 { AArch64::EORWri, AArch64::EORXri }
1666 const TargetRegisterClass *RC;
1669 switch (RetVT.SimpleTy) {
1676 unsigned Idx = ISDOpc - ISD::AND;
1677 Opc = OpcTable[Idx][0];
1678 RC = &AArch64::GPR32spRegClass;
1683 Opc = OpcTable[ISDOpc - ISD::AND][1];
1684 RC = &AArch64::GPR64spRegClass;
1689 if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
1692 unsigned ResultReg =
1693 fastEmitInst_ri(Opc, RC, LHSReg, LHSIsKill,
1694 AArch64_AM::encodeLogicalImmediate(Imm, RegSize));
1695 if (RetVT >= MVT::i8 && RetVT <= MVT::i16 && ISDOpc != ISD::AND) {
1696 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1697 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1702 unsigned AArch64FastISel::emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT,
1703 unsigned LHSReg, bool LHSIsKill,
1704 unsigned RHSReg, bool RHSIsKill,
1705 uint64_t ShiftImm) {
1706 static_assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR),
1707 "ISD nodes are not consecutive!");
1708 static const unsigned OpcTable[3][2] = {
1709 { AArch64::ANDWrs, AArch64::ANDXrs },
1710 { AArch64::ORRWrs, AArch64::ORRXrs },
1711 { AArch64::EORWrs, AArch64::EORXrs }
1714 // Don't deal with undefined shifts.
1715 if (ShiftImm >= RetVT.getSizeInBits())
1718 const TargetRegisterClass *RC;
1720 switch (RetVT.SimpleTy) {
1727 Opc = OpcTable[ISDOpc - ISD::AND][0];
1728 RC = &AArch64::GPR32RegClass;
1731 Opc = OpcTable[ISDOpc - ISD::AND][1];
1732 RC = &AArch64::GPR64RegClass;
1735 unsigned ResultReg =
1736 fastEmitInst_rri(Opc, RC, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1737 AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftImm));
1738 if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1739 uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1740 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1745 unsigned AArch64FastISel::emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1747 return emitLogicalOp_ri(ISD::AND, RetVT, LHSReg, LHSIsKill, Imm);
1750 unsigned AArch64FastISel::emitLoad(MVT VT, MVT RetVT, Address Addr,
1751 bool WantZExt, MachineMemOperand *MMO) {
1752 if (!TLI.allowsMisalignedMemoryAccesses(VT))
1755 // Simplify this down to something we can handle.
1756 if (!simplifyAddress(Addr, VT))
1759 unsigned ScaleFactor = getImplicitScaleFactor(VT);
1761 llvm_unreachable("Unexpected value type.");
1763 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1764 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1765 bool UseScaled = true;
1766 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1771 static const unsigned GPOpcTable[2][8][4] = {
1773 { { AArch64::LDURSBWi, AArch64::LDURSHWi, AArch64::LDURWi,
1775 { AArch64::LDURSBXi, AArch64::LDURSHXi, AArch64::LDURSWi,
1777 { AArch64::LDRSBWui, AArch64::LDRSHWui, AArch64::LDRWui,
1779 { AArch64::LDRSBXui, AArch64::LDRSHXui, AArch64::LDRSWui,
1781 { AArch64::LDRSBWroX, AArch64::LDRSHWroX, AArch64::LDRWroX,
1783 { AArch64::LDRSBXroX, AArch64::LDRSHXroX, AArch64::LDRSWroX,
1785 { AArch64::LDRSBWroW, AArch64::LDRSHWroW, AArch64::LDRWroW,
1787 { AArch64::LDRSBXroW, AArch64::LDRSHXroW, AArch64::LDRSWroW,
1791 { { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1793 { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi,
1795 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1797 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui,
1799 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1801 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX,
1803 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1805 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW,
1810 static const unsigned FPOpcTable[4][2] = {
1811 { AArch64::LDURSi, AArch64::LDURDi },
1812 { AArch64::LDRSui, AArch64::LDRDui },
1813 { AArch64::LDRSroX, AArch64::LDRDroX },
1814 { AArch64::LDRSroW, AArch64::LDRDroW }
1818 const TargetRegisterClass *RC;
1819 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1820 Addr.getOffsetReg();
1821 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1822 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1823 Addr.getExtendType() == AArch64_AM::SXTW)
1826 bool IsRet64Bit = RetVT == MVT::i64;
1827 switch (VT.SimpleTy) {
1829 llvm_unreachable("Unexpected value type.");
1830 case MVT::i1: // Intentional fall-through.
1832 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][0];
1833 RC = (IsRet64Bit && !WantZExt) ?
1834 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1837 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][1];
1838 RC = (IsRet64Bit && !WantZExt) ?
1839 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1842 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][2];
1843 RC = (IsRet64Bit && !WantZExt) ?
1844 &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1847 Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][3];
1848 RC = &AArch64::GPR64RegClass;
1851 Opc = FPOpcTable[Idx][0];
1852 RC = &AArch64::FPR32RegClass;
1855 Opc = FPOpcTable[Idx][1];
1856 RC = &AArch64::FPR64RegClass;
1860 // Create the base instruction, then add the operands.
1861 unsigned ResultReg = createResultReg(RC);
1862 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1863 TII.get(Opc), ResultReg);
1864 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
1866 // Loading an i1 requires special handling.
1867 if (VT == MVT::i1) {
1868 unsigned ANDReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, 1);
1869 assert(ANDReg && "Unexpected AND instruction emission failure.");
1873 // For zero-extending loads to 64bit we emit a 32bit load and then convert
1874 // the 32bit reg to a 64bit reg.
1875 if (WantZExt && RetVT == MVT::i64 && VT <= MVT::i32) {
1876 unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
1877 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1878 TII.get(AArch64::SUBREG_TO_REG), Reg64)
1880 .addReg(ResultReg, getKillRegState(true))
1881 .addImm(AArch64::sub_32);
1887 bool AArch64FastISel::selectAddSub(const Instruction *I) {
1889 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1893 return selectOperator(I, I->getOpcode());
1896 switch (I->getOpcode()) {
1898 llvm_unreachable("Unexpected instruction.");
1899 case Instruction::Add:
1900 ResultReg = emitAdd(VT, I->getOperand(0), I->getOperand(1));
1902 case Instruction::Sub:
1903 ResultReg = emitSub(VT, I->getOperand(0), I->getOperand(1));
1909 updateValueMap(I, ResultReg);
1913 bool AArch64FastISel::selectLogicalOp(const Instruction *I) {
1915 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1919 return selectOperator(I, I->getOpcode());
1922 switch (I->getOpcode()) {
1924 llvm_unreachable("Unexpected instruction.");
1925 case Instruction::And:
1926 ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
1928 case Instruction::Or:
1929 ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
1931 case Instruction::Xor:
1932 ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
1938 updateValueMap(I, ResultReg);
1942 bool AArch64FastISel::selectLoad(const Instruction *I) {
1944 // Verify we have a legal type before going any further. Currently, we handle
1945 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1946 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1947 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true) ||
1948 cast<LoadInst>(I)->isAtomic())
1951 const Value *SV = I->getOperand(0);
1952 if (TLI.supportSwiftError()) {
1953 // Swifterror values can come from either a function parameter with
1954 // swifterror attribute or an alloca with swifterror attribute.
1955 if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1956 if (Arg->hasSwiftErrorAttr())
1960 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1961 if (Alloca->isSwiftError())
1966 // See if we can handle this address.
1968 if (!computeAddress(I->getOperand(0), Addr, I->getType()))
1971 // Fold the following sign-/zero-extend into the load instruction.
1972 bool WantZExt = true;
1974 const Value *IntExtVal = nullptr;
1975 if (I->hasOneUse()) {
1976 if (const auto *ZE = dyn_cast<ZExtInst>(I->use_begin()->getUser())) {
1977 if (isTypeSupported(ZE->getType(), RetVT))
1981 } else if (const auto *SE = dyn_cast<SExtInst>(I->use_begin()->getUser())) {
1982 if (isTypeSupported(SE->getType(), RetVT))
1990 unsigned ResultReg =
1991 emitLoad(VT, RetVT, Addr, WantZExt, createMachineMemOperandFor(I));
1995 // There are a few different cases we have to handle, because the load or the
1996 // sign-/zero-extend might not be selected by FastISel if we fall-back to
1997 // SelectionDAG. There is also an ordering issue when both instructions are in
1998 // different basic blocks.
1999 // 1.) The load instruction is selected by FastISel, but the integer extend
2000 // not. This usually happens when the integer extend is in a different
2001 // basic block and SelectionDAG took over for that basic block.
2002 // 2.) The load instruction is selected before the integer extend. This only
2003 // happens when the integer extend is in a different basic block.
2004 // 3.) The load instruction is selected by SelectionDAG and the integer extend
2005 // by FastISel. This happens if there are instructions between the load
2006 // and the integer extend that couldn't be selected by FastISel.
2008 // The integer extend hasn't been emitted yet. FastISel or SelectionDAG
2009 // could select it. Emit a copy to subreg if necessary. FastISel will remove
2010 // it when it selects the integer extend.
2011 unsigned Reg = lookUpRegForValue(IntExtVal);
2012 auto *MI = MRI.getUniqueVRegDef(Reg);
2014 if (RetVT == MVT::i64 && VT <= MVT::i32) {
2016 // Delete the last emitted instruction from emitLoad (SUBREG_TO_REG).
2017 std::prev(FuncInfo.InsertPt)->eraseFromParent();
2018 ResultReg = std::prev(FuncInfo.InsertPt)->getOperand(0).getReg();
2020 ResultReg = fastEmitInst_extractsubreg(MVT::i32, ResultReg,
2024 updateValueMap(I, ResultReg);
2028 // The integer extend has already been emitted - delete all the instructions
2029 // that have been emitted by the integer extend lowering code and use the
2030 // result from the load instruction directly.
2033 for (auto &Opnd : MI->uses()) {
2035 Reg = Opnd.getReg();
2039 MI->eraseFromParent();
2042 MI = MRI.getUniqueVRegDef(Reg);
2044 updateValueMap(IntExtVal, ResultReg);
2048 updateValueMap(I, ResultReg);
2052 bool AArch64FastISel::emitStoreRelease(MVT VT, unsigned SrcReg,
2054 MachineMemOperand *MMO) {
2056 switch (VT.SimpleTy) {
2057 default: return false;
2058 case MVT::i8: Opc = AArch64::STLRB; break;
2059 case MVT::i16: Opc = AArch64::STLRH; break;
2060 case MVT::i32: Opc = AArch64::STLRW; break;
2061 case MVT::i64: Opc = AArch64::STLRX; break;
2064 const MCInstrDesc &II = TII.get(Opc);
2065 SrcReg = constrainOperandRegClass(II, SrcReg, 0);
2066 AddrReg = constrainOperandRegClass(II, AddrReg, 1);
2067 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
2070 .addMemOperand(MMO);
2074 bool AArch64FastISel::emitStore(MVT VT, unsigned SrcReg, Address Addr,
2075 MachineMemOperand *MMO) {
2076 if (!TLI.allowsMisalignedMemoryAccesses(VT))
2079 // Simplify this down to something we can handle.
2080 if (!simplifyAddress(Addr, VT))
2083 unsigned ScaleFactor = getImplicitScaleFactor(VT);
2085 llvm_unreachable("Unexpected value type.");
2087 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
2088 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
2089 bool UseScaled = true;
2090 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
2095 static const unsigned OpcTable[4][6] = {
2096 { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
2097 AArch64::STURSi, AArch64::STURDi },
2098 { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
2099 AArch64::STRSui, AArch64::STRDui },
2100 { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
2101 AArch64::STRSroX, AArch64::STRDroX },
2102 { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
2103 AArch64::STRSroW, AArch64::STRDroW }
2107 bool VTIsi1 = false;
2108 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
2109 Addr.getOffsetReg();
2110 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
2111 if (Addr.getExtendType() == AArch64_AM::UXTW ||
2112 Addr.getExtendType() == AArch64_AM::SXTW)
2115 switch (VT.SimpleTy) {
2116 default: llvm_unreachable("Unexpected value type.");
2117 case MVT::i1: VTIsi1 = true; LLVM_FALLTHROUGH;
2118 case MVT::i8: Opc = OpcTable[Idx][0]; break;
2119 case MVT::i16: Opc = OpcTable[Idx][1]; break;
2120 case MVT::i32: Opc = OpcTable[Idx][2]; break;
2121 case MVT::i64: Opc = OpcTable[Idx][3]; break;
2122 case MVT::f32: Opc = OpcTable[Idx][4]; break;
2123 case MVT::f64: Opc = OpcTable[Idx][5]; break;
2126 // Storing an i1 requires special handling.
2127 if (VTIsi1 && SrcReg != AArch64::WZR) {
2128 unsigned ANDReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
2129 assert(ANDReg && "Unexpected AND instruction emission failure.");
2132 // Create the base instruction, then add the operands.
2133 const MCInstrDesc &II = TII.get(Opc);
2134 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
2135 MachineInstrBuilder MIB =
2136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(SrcReg);
2137 addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
2142 bool AArch64FastISel::selectStore(const Instruction *I) {
2144 const Value *Op0 = I->getOperand(0);
2145 // Verify we have a legal type before going any further. Currently, we handle
2146 // simple types that will directly fit in a register (i32/f32/i64/f64) or
2147 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
2148 if (!isTypeSupported(Op0->getType(), VT, /*IsVectorAllowed=*/true))
2151 const Value *PtrV = I->getOperand(1);
2152 if (TLI.supportSwiftError()) {
2153 // Swifterror values can come from either a function parameter with
2154 // swifterror attribute or an alloca with swifterror attribute.
2155 if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
2156 if (Arg->hasSwiftErrorAttr())
2160 if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
2161 if (Alloca->isSwiftError())
2166 // Get the value to be stored into a register. Use the zero register directly
2167 // when possible to avoid an unnecessary copy and a wasted register.
2168 unsigned SrcReg = 0;
2169 if (const auto *CI = dyn_cast<ConstantInt>(Op0)) {
2171 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
2172 } else if (const auto *CF = dyn_cast<ConstantFP>(Op0)) {
2173 if (CF->isZero() && !CF->isNegative()) {
2174 VT = MVT::getIntegerVT(VT.getSizeInBits());
2175 SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
2180 SrcReg = getRegForValue(Op0);
2185 auto *SI = cast<StoreInst>(I);
2187 // Try to emit a STLR for seq_cst/release.
2188 if (SI->isAtomic()) {
2189 AtomicOrdering Ord = SI->getOrdering();
2190 // The non-atomic instructions are sufficient for relaxed stores.
2191 if (isReleaseOrStronger(Ord)) {
2192 // The STLR addressing mode only supports a base reg; pass that directly.
2193 unsigned AddrReg = getRegForValue(PtrV);
2194 return emitStoreRelease(VT, SrcReg, AddrReg,
2195 createMachineMemOperandFor(I));
2199 // See if we can handle this address.
2201 if (!computeAddress(PtrV, Addr, Op0->getType()))
2204 if (!emitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
2209 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
2211 case CmpInst::FCMP_ONE:
2212 case CmpInst::FCMP_UEQ:
2214 // AL is our "false" for now. The other two need more compares.
2215 return AArch64CC::AL;
2216 case CmpInst::ICMP_EQ:
2217 case CmpInst::FCMP_OEQ:
2218 return AArch64CC::EQ;
2219 case CmpInst::ICMP_SGT:
2220 case CmpInst::FCMP_OGT:
2221 return AArch64CC::GT;
2222 case CmpInst::ICMP_SGE:
2223 case CmpInst::FCMP_OGE:
2224 return AArch64CC::GE;
2225 case CmpInst::ICMP_UGT:
2226 case CmpInst::FCMP_UGT:
2227 return AArch64CC::HI;
2228 case CmpInst::FCMP_OLT:
2229 return AArch64CC::MI;
2230 case CmpInst::ICMP_ULE:
2231 case CmpInst::FCMP_OLE:
2232 return AArch64CC::LS;
2233 case CmpInst::FCMP_ORD:
2234 return AArch64CC::VC;
2235 case CmpInst::FCMP_UNO:
2236 return AArch64CC::VS;
2237 case CmpInst::FCMP_UGE:
2238 return AArch64CC::PL;
2239 case CmpInst::ICMP_SLT:
2240 case CmpInst::FCMP_ULT:
2241 return AArch64CC::LT;
2242 case CmpInst::ICMP_SLE:
2243 case CmpInst::FCMP_ULE:
2244 return AArch64CC::LE;
2245 case CmpInst::FCMP_UNE:
2246 case CmpInst::ICMP_NE:
2247 return AArch64CC::NE;
2248 case CmpInst::ICMP_UGE:
2249 return AArch64CC::HS;
2250 case CmpInst::ICMP_ULT:
2251 return AArch64CC::LO;
2255 /// \brief Try to emit a combined compare-and-branch instruction.
2256 bool AArch64FastISel::emitCompareAndBranch(const BranchInst *BI) {
2257 assert(isa<CmpInst>(BI->getCondition()) && "Expected cmp instruction");
2258 const CmpInst *CI = cast<CmpInst>(BI->getCondition());
2259 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2261 const Value *LHS = CI->getOperand(0);
2262 const Value *RHS = CI->getOperand(1);
2265 if (!isTypeSupported(LHS->getType(), VT))
2268 unsigned BW = VT.getSizeInBits();
2272 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2273 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2275 // Try to take advantage of fallthrough opportunities.
2276 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2277 std::swap(TBB, FBB);
2278 Predicate = CmpInst::getInversePredicate(Predicate);
2283 switch (Predicate) {
2286 case CmpInst::ICMP_EQ:
2287 case CmpInst::ICMP_NE:
2288 if (isa<Constant>(LHS) && cast<Constant>(LHS)->isNullValue())
2289 std::swap(LHS, RHS);
2291 if (!isa<Constant>(RHS) || !cast<Constant>(RHS)->isNullValue())
2294 if (const auto *AI = dyn_cast<BinaryOperator>(LHS))
2295 if (AI->getOpcode() == Instruction::And && isValueAvailable(AI)) {
2296 const Value *AndLHS = AI->getOperand(0);
2297 const Value *AndRHS = AI->getOperand(1);
2299 if (const auto *C = dyn_cast<ConstantInt>(AndLHS))
2300 if (C->getValue().isPowerOf2())
2301 std::swap(AndLHS, AndRHS);
2303 if (const auto *C = dyn_cast<ConstantInt>(AndRHS))
2304 if (C->getValue().isPowerOf2()) {
2305 TestBit = C->getValue().logBase2();
2313 IsCmpNE = Predicate == CmpInst::ICMP_NE;
2315 case CmpInst::ICMP_SLT:
2316 case CmpInst::ICMP_SGE:
2317 if (!isa<Constant>(RHS) || !cast<Constant>(RHS)->isNullValue())
2321 IsCmpNE = Predicate == CmpInst::ICMP_SLT;
2323 case CmpInst::ICMP_SGT:
2324 case CmpInst::ICMP_SLE:
2325 if (!isa<ConstantInt>(RHS))
2328 if (cast<ConstantInt>(RHS)->getValue() != APInt(BW, -1, true))
2332 IsCmpNE = Predicate == CmpInst::ICMP_SLE;
2336 static const unsigned OpcTable[2][2][2] = {
2337 { {AArch64::CBZW, AArch64::CBZX },
2338 {AArch64::CBNZW, AArch64::CBNZX} },
2339 { {AArch64::TBZW, AArch64::TBZX },
2340 {AArch64::TBNZW, AArch64::TBNZX} }
2343 bool IsBitTest = TestBit != -1;
2344 bool Is64Bit = BW == 64;
2345 if (TestBit < 32 && TestBit >= 0)
2348 unsigned Opc = OpcTable[IsBitTest][IsCmpNE][Is64Bit];
2349 const MCInstrDesc &II = TII.get(Opc);
2351 unsigned SrcReg = getRegForValue(LHS);
2354 bool SrcIsKill = hasTrivialKill(LHS);
2356 if (BW == 64 && !Is64Bit)
2357 SrcReg = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
2360 if ((BW < 32) && !IsBitTest)
2361 SrcReg = emitIntExt(VT, SrcReg, MVT::i32, /*IsZExt=*/true);
2363 // Emit the combined compare and branch instruction.
2364 SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
2365 MachineInstrBuilder MIB =
2366 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
2367 .addReg(SrcReg, getKillRegState(SrcIsKill));
2369 MIB.addImm(TestBit);
2372 finishCondBranch(BI->getParent(), TBB, FBB);
2376 bool AArch64FastISel::selectBranch(const Instruction *I) {
2377 const BranchInst *BI = cast<BranchInst>(I);
2378 if (BI->isUnconditional()) {
2379 MachineBasicBlock *MSucc = FuncInfo.MBBMap[BI->getSuccessor(0)];
2380 fastEmitBranch(MSucc, BI->getDebugLoc());
2384 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2385 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2387 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
2388 if (CI->hasOneUse() && isValueAvailable(CI)) {
2389 // Try to optimize or fold the cmp.
2390 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2391 switch (Predicate) {
2394 case CmpInst::FCMP_FALSE:
2395 fastEmitBranch(FBB, DbgLoc);
2397 case CmpInst::FCMP_TRUE:
2398 fastEmitBranch(TBB, DbgLoc);
2402 // Try to emit a combined compare-and-branch first.
2403 if (emitCompareAndBranch(BI))
2406 // Try to take advantage of fallthrough opportunities.
2407 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2408 std::swap(TBB, FBB);
2409 Predicate = CmpInst::getInversePredicate(Predicate);
2413 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2416 // FCMP_UEQ and FCMP_ONE cannot be checked with a single branch
2418 AArch64CC::CondCode CC = getCompareCC(Predicate);
2419 AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2420 switch (Predicate) {
2423 case CmpInst::FCMP_UEQ:
2424 ExtraCC = AArch64CC::EQ;
2427 case CmpInst::FCMP_ONE:
2428 ExtraCC = AArch64CC::MI;
2432 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2434 // Emit the extra branch for FCMP_UEQ and FCMP_ONE.
2435 if (ExtraCC != AArch64CC::AL) {
2436 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2442 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2446 finishCondBranch(BI->getParent(), TBB, FBB);
2449 } else if (const auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
2450 uint64_t Imm = CI->getZExtValue();
2451 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
2452 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
2455 // Obtain the branch probability and add the target to the successor list.
2457 auto BranchProbability = FuncInfo.BPI->getEdgeProbability(
2458 BI->getParent(), Target->getBasicBlock());
2459 FuncInfo.MBB->addSuccessor(Target, BranchProbability);
2461 FuncInfo.MBB->addSuccessorWithoutProb(Target);
2464 AArch64CC::CondCode CC = AArch64CC::NE;
2465 if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
2466 // Fake request the condition, otherwise the intrinsic might be completely
2468 unsigned CondReg = getRegForValue(BI->getCondition());
2473 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2477 finishCondBranch(BI->getParent(), TBB, FBB);
2482 unsigned CondReg = getRegForValue(BI->getCondition());
2485 bool CondRegIsKill = hasTrivialKill(BI->getCondition());
2487 // i1 conditions come as i32 values, test the lowest bit with tb(n)z.
2488 unsigned Opcode = AArch64::TBNZW;
2489 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2490 std::swap(TBB, FBB);
2491 Opcode = AArch64::TBZW;
2494 const MCInstrDesc &II = TII.get(Opcode);
2495 unsigned ConstrainedCondReg
2496 = constrainOperandRegClass(II, CondReg, II.getNumDefs());
2497 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
2498 .addReg(ConstrainedCondReg, getKillRegState(CondRegIsKill))
2502 finishCondBranch(BI->getParent(), TBB, FBB);
2506 bool AArch64FastISel::selectIndirectBr(const Instruction *I) {
2507 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
2508 unsigned AddrReg = getRegForValue(BI->getOperand(0));
2512 // Emit the indirect branch.
2513 const MCInstrDesc &II = TII.get(AArch64::BR);
2514 AddrReg = constrainOperandRegClass(II, AddrReg, II.getNumDefs());
2515 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(AddrReg);
2517 // Make sure the CFG is up-to-date.
2518 for (auto *Succ : BI->successors())
2519 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[Succ]);
2524 bool AArch64FastISel::selectCmp(const Instruction *I) {
2525 const CmpInst *CI = cast<CmpInst>(I);
2527 // Vectors of i1 are weird: bail out.
2528 if (CI->getType()->isVectorTy())
2531 // Try to optimize or fold the cmp.
2532 CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2533 unsigned ResultReg = 0;
2534 switch (Predicate) {
2537 case CmpInst::FCMP_FALSE:
2538 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2539 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2540 TII.get(TargetOpcode::COPY), ResultReg)
2541 .addReg(AArch64::WZR, getKillRegState(true));
2543 case CmpInst::FCMP_TRUE:
2544 ResultReg = fastEmit_i(MVT::i32, MVT::i32, ISD::Constant, 1);
2549 updateValueMap(I, ResultReg);
2554 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2557 ResultReg = createResultReg(&AArch64::GPR32RegClass);
2559 // FCMP_UEQ and FCMP_ONE cannot be checked with a single instruction. These
2560 // condition codes are inverted, because they are used by CSINC.
2561 static unsigned CondCodeTable[2][2] = {
2562 { AArch64CC::NE, AArch64CC::VC },
2563 { AArch64CC::PL, AArch64CC::LE }
2565 unsigned *CondCodes = nullptr;
2566 switch (Predicate) {
2569 case CmpInst::FCMP_UEQ:
2570 CondCodes = &CondCodeTable[0][0];
2572 case CmpInst::FCMP_ONE:
2573 CondCodes = &CondCodeTable[1][0];
2578 unsigned TmpReg1 = createResultReg(&AArch64::GPR32RegClass);
2579 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2581 .addReg(AArch64::WZR, getKillRegState(true))
2582 .addReg(AArch64::WZR, getKillRegState(true))
2583 .addImm(CondCodes[0]);
2584 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2586 .addReg(TmpReg1, getKillRegState(true))
2587 .addReg(AArch64::WZR, getKillRegState(true))
2588 .addImm(CondCodes[1]);
2590 updateValueMap(I, ResultReg);
2594 // Now set a register based on the comparison.
2595 AArch64CC::CondCode CC = getCompareCC(Predicate);
2596 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2597 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
2598 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2600 .addReg(AArch64::WZR, getKillRegState(true))
2601 .addReg(AArch64::WZR, getKillRegState(true))
2602 .addImm(invertedCC);
2604 updateValueMap(I, ResultReg);
2608 /// \brief Optimize selects of i1 if one of the operands has a 'true' or 'false'
2610 bool AArch64FastISel::optimizeSelect(const SelectInst *SI) {
2611 if (!SI->getType()->isIntegerTy(1))
2614 const Value *Src1Val, *Src2Val;
2616 bool NeedExtraOp = false;
2617 if (auto *CI = dyn_cast<ConstantInt>(SI->getTrueValue())) {
2619 Src1Val = SI->getCondition();
2620 Src2Val = SI->getFalseValue();
2621 Opc = AArch64::ORRWrr;
2623 assert(CI->isZero());
2624 Src1Val = SI->getFalseValue();
2625 Src2Val = SI->getCondition();
2626 Opc = AArch64::BICWrr;
2628 } else if (auto *CI = dyn_cast<ConstantInt>(SI->getFalseValue())) {
2630 Src1Val = SI->getCondition();
2631 Src2Val = SI->getTrueValue();
2632 Opc = AArch64::ORRWrr;
2635 assert(CI->isZero());
2636 Src1Val = SI->getCondition();
2637 Src2Val = SI->getTrueValue();
2638 Opc = AArch64::ANDWrr;
2645 unsigned Src1Reg = getRegForValue(Src1Val);
2648 bool Src1IsKill = hasTrivialKill(Src1Val);
2650 unsigned Src2Reg = getRegForValue(Src2Val);
2653 bool Src2IsKill = hasTrivialKill(Src2Val);
2656 Src1Reg = emitLogicalOp_ri(ISD::XOR, MVT::i32, Src1Reg, Src1IsKill, 1);
2659 unsigned ResultReg = fastEmitInst_rr(Opc, &AArch64::GPR32RegClass, Src1Reg,
2660 Src1IsKill, Src2Reg, Src2IsKill);
2661 updateValueMap(SI, ResultReg);
2665 bool AArch64FastISel::selectSelect(const Instruction *I) {
2666 assert(isa<SelectInst>(I) && "Expected a select instruction.");
2668 if (!isTypeSupported(I->getType(), VT))
2672 const TargetRegisterClass *RC;
2673 switch (VT.SimpleTy) {
2680 Opc = AArch64::CSELWr;
2681 RC = &AArch64::GPR32RegClass;
2684 Opc = AArch64::CSELXr;
2685 RC = &AArch64::GPR64RegClass;
2688 Opc = AArch64::FCSELSrrr;
2689 RC = &AArch64::FPR32RegClass;
2692 Opc = AArch64::FCSELDrrr;
2693 RC = &AArch64::FPR64RegClass;
2697 const SelectInst *SI = cast<SelectInst>(I);
2698 const Value *Cond = SI->getCondition();
2699 AArch64CC::CondCode CC = AArch64CC::NE;
2700 AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2702 if (optimizeSelect(SI))
2705 // Try to pickup the flags, so we don't have to emit another compare.
2706 if (foldXALUIntrinsic(CC, I, Cond)) {
2707 // Fake request the condition to force emission of the XALU intrinsic.
2708 unsigned CondReg = getRegForValue(Cond);
2711 } else if (isa<CmpInst>(Cond) && cast<CmpInst>(Cond)->hasOneUse() &&
2712 isValueAvailable(Cond)) {
2713 const auto *Cmp = cast<CmpInst>(Cond);
2714 // Try to optimize or fold the cmp.
2715 CmpInst::Predicate Predicate = optimizeCmpPredicate(Cmp);
2716 const Value *FoldSelect = nullptr;
2717 switch (Predicate) {
2720 case CmpInst::FCMP_FALSE:
2721 FoldSelect = SI->getFalseValue();
2723 case CmpInst::FCMP_TRUE:
2724 FoldSelect = SI->getTrueValue();
2729 unsigned SrcReg = getRegForValue(FoldSelect);
2732 unsigned UseReg = lookUpRegForValue(SI);
2734 MRI.clearKillFlags(UseReg);
2736 updateValueMap(I, SrcReg);
2741 if (!emitCmp(Cmp->getOperand(0), Cmp->getOperand(1), Cmp->isUnsigned()))
2744 // FCMP_UEQ and FCMP_ONE cannot be checked with a single select instruction.
2745 CC = getCompareCC(Predicate);
2746 switch (Predicate) {
2749 case CmpInst::FCMP_UEQ:
2750 ExtraCC = AArch64CC::EQ;
2753 case CmpInst::FCMP_ONE:
2754 ExtraCC = AArch64CC::MI;
2758 assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2760 unsigned CondReg = getRegForValue(Cond);
2763 bool CondIsKill = hasTrivialKill(Cond);
2765 const MCInstrDesc &II = TII.get(AArch64::ANDSWri);
2766 CondReg = constrainOperandRegClass(II, CondReg, 1);
2768 // Emit a TST instruction (ANDS wzr, reg, #imm).
2769 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
2771 .addReg(CondReg, getKillRegState(CondIsKill))
2772 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
2775 unsigned Src1Reg = getRegForValue(SI->getTrueValue());
2776 bool Src1IsKill = hasTrivialKill(SI->getTrueValue());
2778 unsigned Src2Reg = getRegForValue(SI->getFalseValue());
2779 bool Src2IsKill = hasTrivialKill(SI->getFalseValue());
2781 if (!Src1Reg || !Src2Reg)
2784 if (ExtraCC != AArch64CC::AL) {
2785 Src2Reg = fastEmitInst_rri(Opc, RC, Src1Reg, Src1IsKill, Src2Reg,
2786 Src2IsKill, ExtraCC);
2789 unsigned ResultReg = fastEmitInst_rri(Opc, RC, Src1Reg, Src1IsKill, Src2Reg,
2791 updateValueMap(I, ResultReg);
2795 bool AArch64FastISel::selectFPExt(const Instruction *I) {
2796 Value *V = I->getOperand(0);
2797 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
2800 unsigned Op = getRegForValue(V);
2804 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
2805 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
2806 ResultReg).addReg(Op);
2807 updateValueMap(I, ResultReg);
2811 bool AArch64FastISel::selectFPTrunc(const Instruction *I) {
2812 Value *V = I->getOperand(0);
2813 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
2816 unsigned Op = getRegForValue(V);
2820 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
2821 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
2822 ResultReg).addReg(Op);
2823 updateValueMap(I, ResultReg);
2827 // FPToUI and FPToSI
2828 bool AArch64FastISel::selectFPToInt(const Instruction *I, bool Signed) {
2830 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2833 unsigned SrcReg = getRegForValue(I->getOperand(0));
2837 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType(), true);
2838 if (SrcVT == MVT::f128 || SrcVT == MVT::f16)
2842 if (SrcVT == MVT::f64) {
2844 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
2846 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
2849 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
2851 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
2853 unsigned ResultReg = createResultReg(
2854 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
2855 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2857 updateValueMap(I, ResultReg);
2861 bool AArch64FastISel::selectIntToFP(const Instruction *I, bool Signed) {
2863 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2865 // Let regular ISEL handle FP16
2866 if (DestVT == MVT::f16)
2869 assert((DestVT == MVT::f32 || DestVT == MVT::f64) &&
2870 "Unexpected value type.");
2872 unsigned SrcReg = getRegForValue(I->getOperand(0));
2875 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
2877 EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType(), true);
2879 // Handle sign-extension.
2880 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
2882 emitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
2889 if (SrcVT == MVT::i64) {
2891 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
2893 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
2896 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
2898 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
2901 unsigned ResultReg = fastEmitInst_r(Opc, TLI.getRegClassFor(DestVT), SrcReg,
2903 updateValueMap(I, ResultReg);
2907 bool AArch64FastISel::fastLowerArguments() {
2908 if (!FuncInfo.CanLowerReturn)
2911 const Function *F = FuncInfo.Fn;
2915 CallingConv::ID CC = F->getCallingConv();
2916 if (CC != CallingConv::C && CC != CallingConv::Swift)
2919 // Only handle simple cases of up to 8 GPR and FPR each.
2920 unsigned GPRCnt = 0;
2921 unsigned FPRCnt = 0;
2922 for (auto const &Arg : F->args()) {
2923 if (Arg.hasAttribute(Attribute::ByVal) ||
2924 Arg.hasAttribute(Attribute::InReg) ||
2925 Arg.hasAttribute(Attribute::StructRet) ||
2926 Arg.hasAttribute(Attribute::SwiftSelf) ||
2927 Arg.hasAttribute(Attribute::SwiftError) ||
2928 Arg.hasAttribute(Attribute::Nest))
2931 Type *ArgTy = Arg.getType();
2932 if (ArgTy->isStructTy() || ArgTy->isArrayTy())
2935 EVT ArgVT = TLI.getValueType(DL, ArgTy);
2936 if (!ArgVT.isSimple())
2939 MVT VT = ArgVT.getSimpleVT().SimpleTy;
2940 if (VT.isFloatingPoint() && !Subtarget->hasFPARMv8())
2943 if (VT.isVector() &&
2944 (!Subtarget->hasNEON() || !Subtarget->isLittleEndian()))
2947 if (VT >= MVT::i1 && VT <= MVT::i64)
2949 else if ((VT >= MVT::f16 && VT <= MVT::f64) || VT.is64BitVector() ||
2950 VT.is128BitVector())
2955 if (GPRCnt > 8 || FPRCnt > 8)
2959 static const MCPhysReg Registers[6][8] = {
2960 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
2961 AArch64::W5, AArch64::W6, AArch64::W7 },
2962 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
2963 AArch64::X5, AArch64::X6, AArch64::X7 },
2964 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
2965 AArch64::H5, AArch64::H6, AArch64::H7 },
2966 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
2967 AArch64::S5, AArch64::S6, AArch64::S7 },
2968 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
2969 AArch64::D5, AArch64::D6, AArch64::D7 },
2970 { AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, AArch64::Q4,
2971 AArch64::Q5, AArch64::Q6, AArch64::Q7 }
2974 unsigned GPRIdx = 0;
2975 unsigned FPRIdx = 0;
2976 for (auto const &Arg : F->args()) {
2977 MVT VT = TLI.getSimpleValueType(DL, Arg.getType());
2979 const TargetRegisterClass *RC;
2980 if (VT >= MVT::i1 && VT <= MVT::i32) {
2981 SrcReg = Registers[0][GPRIdx++];
2982 RC = &AArch64::GPR32RegClass;
2984 } else if (VT == MVT::i64) {
2985 SrcReg = Registers[1][GPRIdx++];
2986 RC = &AArch64::GPR64RegClass;
2987 } else if (VT == MVT::f16) {
2988 SrcReg = Registers[2][FPRIdx++];
2989 RC = &AArch64::FPR16RegClass;
2990 } else if (VT == MVT::f32) {
2991 SrcReg = Registers[3][FPRIdx++];
2992 RC = &AArch64::FPR32RegClass;
2993 } else if ((VT == MVT::f64) || VT.is64BitVector()) {
2994 SrcReg = Registers[4][FPRIdx++];
2995 RC = &AArch64::FPR64RegClass;
2996 } else if (VT.is128BitVector()) {
2997 SrcReg = Registers[5][FPRIdx++];
2998 RC = &AArch64::FPR128RegClass;
3000 llvm_unreachable("Unexpected value type.");
3002 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
3003 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
3004 // Without this, EmitLiveInCopies may eliminate the livein if its only
3005 // use is a bitcast (which isn't turned into an instruction).
3006 unsigned ResultReg = createResultReg(RC);
3007 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3008 TII.get(TargetOpcode::COPY), ResultReg)
3009 .addReg(DstReg, getKillRegState(true));
3010 updateValueMap(&Arg, ResultReg);
3015 bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
3016 SmallVectorImpl<MVT> &OutVTs,
3017 unsigned &NumBytes) {
3018 CallingConv::ID CC = CLI.CallConv;
3019 SmallVector<CCValAssign, 16> ArgLocs;
3020 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
3021 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
3023 // Get a count of how many bytes are to be pushed on the stack.
3024 NumBytes = CCInfo.getNextStackOffset();
3026 // Issue CALLSEQ_START
3027 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
3028 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
3029 .addImm(NumBytes).addImm(0);
3031 // Process the args.
3032 for (CCValAssign &VA : ArgLocs) {
3033 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
3034 MVT ArgVT = OutVTs[VA.getValNo()];
3036 unsigned ArgReg = getRegForValue(ArgVal);
3040 // Handle arg promotion: SExt, ZExt, AExt.
3041 switch (VA.getLocInfo()) {
3042 case CCValAssign::Full:
3044 case CCValAssign::SExt: {
3045 MVT DestVT = VA.getLocVT();
3047 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
3052 case CCValAssign::AExt:
3053 // Intentional fall-through.
3054 case CCValAssign::ZExt: {
3055 MVT DestVT = VA.getLocVT();
3057 ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
3063 llvm_unreachable("Unknown arg promotion!");
3066 // Now copy/store arg to correct locations.
3067 if (VA.isRegLoc() && !VA.needsCustom()) {
3068 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3069 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
3070 CLI.OutRegs.push_back(VA.getLocReg());
3071 } else if (VA.needsCustom()) {
3072 // FIXME: Handle custom args.
3075 assert(VA.isMemLoc() && "Assuming store on stack.");
3077 // Don't emit stores for undef values.
3078 if (isa<UndefValue>(ArgVal))
3081 // Need to store on the stack.
3082 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
3084 unsigned BEAlign = 0;
3085 if (ArgSize < 8 && !Subtarget->isLittleEndian())
3086 BEAlign = 8 - ArgSize;
3089 Addr.setKind(Address::RegBase);
3090 Addr.setReg(AArch64::SP);
3091 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
3093 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
3094 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
3095 MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
3096 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
3098 if (!emitStore(ArgVT, ArgReg, Addr, MMO))
3105 bool AArch64FastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
3106 unsigned NumBytes) {
3107 CallingConv::ID CC = CLI.CallConv;
3109 // Issue CALLSEQ_END
3110 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
3111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
3112 .addImm(NumBytes).addImm(0);
3114 // Now the return value.
3115 if (RetVT != MVT::isVoid) {
3116 SmallVector<CCValAssign, 16> RVLocs;
3117 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
3118 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
3120 // Only handle a single return value.
3121 if (RVLocs.size() != 1)
3124 // Copy all of the result registers out of their specified physreg.
3125 MVT CopyVT = RVLocs[0].getValVT();
3127 // TODO: Handle big-endian results
3128 if (CopyVT.isVector() && !Subtarget->isLittleEndian())
3131 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
3132 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3133 TII.get(TargetOpcode::COPY), ResultReg)
3134 .addReg(RVLocs[0].getLocReg());
3135 CLI.InRegs.push_back(RVLocs[0].getLocReg());
3137 CLI.ResultReg = ResultReg;
3138 CLI.NumResultRegs = 1;
3144 bool AArch64FastISel::fastLowerCall(CallLoweringInfo &CLI) {
3145 CallingConv::ID CC = CLI.CallConv;
3146 bool IsTailCall = CLI.IsTailCall;
3147 bool IsVarArg = CLI.IsVarArg;
3148 const Value *Callee = CLI.Callee;
3149 MCSymbol *Symbol = CLI.Symbol;
3151 if (!Callee && !Symbol)
3154 // Allow SelectionDAG isel to handle tail calls.
3158 CodeModel::Model CM = TM.getCodeModel();
3159 // Only support the small-addressing and large code models.
3160 if (CM != CodeModel::Large && !Subtarget->useSmallAddressing())
3163 // FIXME: Add large code model support for ELF.
3164 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
3167 // Let SDISel handle vararg functions.
3171 // FIXME: Only handle *simple* calls for now.
3173 if (CLI.RetTy->isVoidTy())
3174 RetVT = MVT::isVoid;
3175 else if (!isTypeLegal(CLI.RetTy, RetVT))
3178 for (auto Flag : CLI.OutFlags)
3179 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal() ||
3180 Flag.isSwiftSelf() || Flag.isSwiftError())
3183 // Set up the argument vectors.
3184 SmallVector<MVT, 16> OutVTs;
3185 OutVTs.reserve(CLI.OutVals.size());
3187 for (auto *Val : CLI.OutVals) {
3189 if (!isTypeLegal(Val->getType(), VT) &&
3190 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
3193 // We don't handle vector parameters yet.
3194 if (VT.isVector() || VT.getSizeInBits() > 64)
3197 OutVTs.push_back(VT);
3201 if (Callee && !computeCallAddress(Callee, Addr))
3204 // Handle the arguments now that we've gotten them.
3206 if (!processCallArgs(CLI, OutVTs, NumBytes))
3210 MachineInstrBuilder MIB;
3211 if (Subtarget->useSmallAddressing()) {
3212 const MCInstrDesc &II = TII.get(Addr.getReg() ? AArch64::BLR : AArch64::BL);
3213 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II);
3215 MIB.addSym(Symbol, 0);
3216 else if (Addr.getGlobalValue())
3217 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
3218 else if (Addr.getReg()) {
3219 unsigned Reg = constrainOperandRegClass(II, Addr.getReg(), 0);
3224 unsigned CallReg = 0;
3226 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
3227 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
3229 .addSym(Symbol, AArch64II::MO_GOT | AArch64II::MO_PAGE);
3231 CallReg = createResultReg(&AArch64::GPR64RegClass);
3232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3233 TII.get(AArch64::LDRXui), CallReg)
3236 AArch64II::MO_GOT | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
3237 } else if (Addr.getGlobalValue())
3238 CallReg = materializeGV(Addr.getGlobalValue());
3239 else if (Addr.getReg())
3240 CallReg = Addr.getReg();
3245 const MCInstrDesc &II = TII.get(AArch64::BLR);
3246 CallReg = constrainOperandRegClass(II, CallReg, 0);
3247 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(CallReg);
3250 // Add implicit physical register uses to the call.
3251 for (auto Reg : CLI.OutRegs)
3252 MIB.addReg(Reg, RegState::Implicit);
3254 // Add a register mask with the call-preserved registers.
3255 // Proper defs for return values will be added by setPhysRegsDeadExcept().
3256 MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
3260 // Finish off the call including any return values.
3261 return finishCall(CLI, RetVT, NumBytes);
3264 bool AArch64FastISel::isMemCpySmall(uint64_t Len, unsigned Alignment) {
3266 return Len / Alignment <= 4;
3271 bool AArch64FastISel::tryEmitSmallMemCpy(Address Dest, Address Src,
3272 uint64_t Len, unsigned Alignment) {
3273 // Make sure we don't bloat code by inlining very large memcpy's.
3274 if (!isMemCpySmall(Len, Alignment))
3277 int64_t UnscaledOffset = 0;
3278 Address OrigDest = Dest;
3279 Address OrigSrc = Src;
3283 if (!Alignment || Alignment >= 8) {
3294 // Bound based on alignment.
3295 if (Len >= 4 && Alignment == 4)
3297 else if (Len >= 2 && Alignment == 2)
3304 unsigned ResultReg = emitLoad(VT, VT, Src);
3308 if (!emitStore(VT, ResultReg, Dest))
3311 int64_t Size = VT.getSizeInBits() / 8;
3313 UnscaledOffset += Size;
3315 // We need to recompute the unscaled offset for each iteration.
3316 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
3317 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
3323 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
3324 /// into the user. The condition code will only be updated on success.
3325 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
3326 const Instruction *I,
3327 const Value *Cond) {
3328 if (!isa<ExtractValueInst>(Cond))
3331 const auto *EV = cast<ExtractValueInst>(Cond);
3332 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
3335 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
3337 const Function *Callee = II->getCalledFunction();
3339 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
3340 if (!isTypeLegal(RetTy, RetVT))
3343 if (RetVT != MVT::i32 && RetVT != MVT::i64)
3346 const Value *LHS = II->getArgOperand(0);
3347 const Value *RHS = II->getArgOperand(1);
3349 // Canonicalize immediate to the RHS.
3350 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3351 isCommutativeIntrinsic(II))
3352 std::swap(LHS, RHS);
3354 // Simplify multiplies.
3355 Intrinsic::ID IID = II->getIntrinsicID();
3359 case Intrinsic::smul_with_overflow:
3360 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3361 if (C->getValue() == 2)
3362 IID = Intrinsic::sadd_with_overflow;
3364 case Intrinsic::umul_with_overflow:
3365 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3366 if (C->getValue() == 2)
3367 IID = Intrinsic::uadd_with_overflow;
3371 AArch64CC::CondCode TmpCC;
3375 case Intrinsic::sadd_with_overflow:
3376 case Intrinsic::ssub_with_overflow:
3377 TmpCC = AArch64CC::VS;
3379 case Intrinsic::uadd_with_overflow:
3380 TmpCC = AArch64CC::HS;
3382 case Intrinsic::usub_with_overflow:
3383 TmpCC = AArch64CC::LO;
3385 case Intrinsic::smul_with_overflow:
3386 case Intrinsic::umul_with_overflow:
3387 TmpCC = AArch64CC::NE;
3391 // Check if both instructions are in the same basic block.
3392 if (!isValueAvailable(II))
3395 // Make sure nothing is in the way
3396 BasicBlock::const_iterator Start(I);
3397 BasicBlock::const_iterator End(II);
3398 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
3399 // We only expect extractvalue instructions between the intrinsic and the
3400 // instruction to be selected.
3401 if (!isa<ExtractValueInst>(Itr))
3404 // Check that the extractvalue operand comes from the intrinsic.
3405 const auto *EVI = cast<ExtractValueInst>(Itr);
3406 if (EVI->getAggregateOperand() != II)
3414 bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
3415 // FIXME: Handle more intrinsics.
3416 switch (II->getIntrinsicID()) {
3417 default: return false;
3418 case Intrinsic::frameaddress: {
3419 MachineFrameInfo &MFI = FuncInfo.MF->getFrameInfo();
3420 MFI.setFrameAddressIsTaken(true);
3422 const AArch64RegisterInfo *RegInfo = Subtarget->getRegisterInfo();
3423 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
3424 unsigned SrcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3425 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3426 TII.get(TargetOpcode::COPY), SrcReg).addReg(FramePtr);
3427 // Recursively load frame address
3433 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
3435 DestReg = fastEmitInst_ri(AArch64::LDRXui, &AArch64::GPR64RegClass,
3436 SrcReg, /*IsKill=*/true, 0);
3437 assert(DestReg && "Unexpected LDR instruction emission failure.");
3441 updateValueMap(II, SrcReg);
3444 case Intrinsic::memcpy:
3445 case Intrinsic::memmove: {
3446 const auto *MTI = cast<MemTransferInst>(II);
3447 // Don't handle volatile.
3448 if (MTI->isVolatile())
3451 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
3452 // we would emit dead code because we don't currently handle memmoves.
3453 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
3454 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
3455 // Small memcpy's are common enough that we want to do them without a call
3457 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
3458 unsigned Alignment = MTI->getAlignment();
3459 if (isMemCpySmall(Len, Alignment)) {
3461 if (!computeAddress(MTI->getRawDest(), Dest) ||
3462 !computeAddress(MTI->getRawSource(), Src))
3464 if (tryEmitSmallMemCpy(Dest, Src, Len, Alignment))
3469 if (!MTI->getLength()->getType()->isIntegerTy(64))
3472 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
3473 // Fast instruction selection doesn't support the special
3477 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
3478 return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
3480 case Intrinsic::memset: {
3481 const MemSetInst *MSI = cast<MemSetInst>(II);
3482 // Don't handle volatile.
3483 if (MSI->isVolatile())
3486 if (!MSI->getLength()->getType()->isIntegerTy(64))
3489 if (MSI->getDestAddressSpace() > 255)
3490 // Fast instruction selection doesn't support the special
3494 return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
3496 case Intrinsic::sin:
3497 case Intrinsic::cos:
3498 case Intrinsic::pow: {
3500 if (!isTypeLegal(II->getType(), RetVT))
3503 if (RetVT != MVT::f32 && RetVT != MVT::f64)
3506 static const RTLIB::Libcall LibCallTable[3][2] = {
3507 { RTLIB::SIN_F32, RTLIB::SIN_F64 },
3508 { RTLIB::COS_F32, RTLIB::COS_F64 },
3509 { RTLIB::POW_F32, RTLIB::POW_F64 }
3512 bool Is64Bit = RetVT == MVT::f64;
3513 switch (II->getIntrinsicID()) {
3515 llvm_unreachable("Unexpected intrinsic.");
3516 case Intrinsic::sin:
3517 LC = LibCallTable[0][Is64Bit];
3519 case Intrinsic::cos:
3520 LC = LibCallTable[1][Is64Bit];
3522 case Intrinsic::pow:
3523 LC = LibCallTable[2][Is64Bit];
3528 Args.reserve(II->getNumArgOperands());
3530 // Populate the argument list.
3531 for (auto &Arg : II->arg_operands()) {
3534 Entry.Ty = Arg->getType();
3535 Args.push_back(Entry);
3538 CallLoweringInfo CLI;
3539 MCContext &Ctx = MF->getContext();
3540 CLI.setCallee(DL, Ctx, TLI.getLibcallCallingConv(LC), II->getType(),
3541 TLI.getLibcallName(LC), std::move(Args));
3542 if (!lowerCallTo(CLI))
3544 updateValueMap(II, CLI.ResultReg);
3547 case Intrinsic::fabs: {
3549 if (!isTypeLegal(II->getType(), VT))
3553 switch (VT.SimpleTy) {
3557 Opc = AArch64::FABSSr;
3560 Opc = AArch64::FABSDr;
3563 unsigned SrcReg = getRegForValue(II->getOperand(0));
3566 bool SrcRegIsKill = hasTrivialKill(II->getOperand(0));
3567 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3568 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
3569 .addReg(SrcReg, getKillRegState(SrcRegIsKill));
3570 updateValueMap(II, ResultReg);
3573 case Intrinsic::trap:
3574 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
3578 case Intrinsic::sqrt: {
3579 Type *RetTy = II->getCalledFunction()->getReturnType();
3582 if (!isTypeLegal(RetTy, VT))
3585 unsigned Op0Reg = getRegForValue(II->getOperand(0));
3588 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
3590 unsigned ResultReg = fastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
3594 updateValueMap(II, ResultReg);
3597 case Intrinsic::sadd_with_overflow:
3598 case Intrinsic::uadd_with_overflow:
3599 case Intrinsic::ssub_with_overflow:
3600 case Intrinsic::usub_with_overflow:
3601 case Intrinsic::smul_with_overflow:
3602 case Intrinsic::umul_with_overflow: {
3603 // This implements the basic lowering of the xalu with overflow intrinsics.
3604 const Function *Callee = II->getCalledFunction();
3605 auto *Ty = cast<StructType>(Callee->getReturnType());
3606 Type *RetTy = Ty->getTypeAtIndex(0U);
3609 if (!isTypeLegal(RetTy, VT))
3612 if (VT != MVT::i32 && VT != MVT::i64)
3615 const Value *LHS = II->getArgOperand(0);
3616 const Value *RHS = II->getArgOperand(1);
3617 // Canonicalize immediate to the RHS.
3618 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3619 isCommutativeIntrinsic(II))
3620 std::swap(LHS, RHS);
3622 // Simplify multiplies.
3623 Intrinsic::ID IID = II->getIntrinsicID();
3627 case Intrinsic::smul_with_overflow:
3628 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3629 if (C->getValue() == 2) {
3630 IID = Intrinsic::sadd_with_overflow;
3634 case Intrinsic::umul_with_overflow:
3635 if (const auto *C = dyn_cast<ConstantInt>(RHS))
3636 if (C->getValue() == 2) {
3637 IID = Intrinsic::uadd_with_overflow;
3643 unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
3644 AArch64CC::CondCode CC = AArch64CC::Invalid;
3646 default: llvm_unreachable("Unexpected intrinsic!");
3647 case Intrinsic::sadd_with_overflow:
3648 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3651 case Intrinsic::uadd_with_overflow:
3652 ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3655 case Intrinsic::ssub_with_overflow:
3656 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3659 case Intrinsic::usub_with_overflow:
3660 ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3663 case Intrinsic::smul_with_overflow: {
3665 unsigned LHSReg = getRegForValue(LHS);
3668 bool LHSIsKill = hasTrivialKill(LHS);
3670 unsigned RHSReg = getRegForValue(RHS);
3673 bool RHSIsKill = hasTrivialKill(RHS);
3675 if (VT == MVT::i32) {
3676 MulReg = emitSMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3677 unsigned ShiftReg = emitLSR_ri(MVT::i64, MVT::i64, MulReg,
3678 /*IsKill=*/false, 32);
3679 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3681 ShiftReg = fastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
3683 emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3684 AArch64_AM::ASR, 31, /*WantResult=*/false);
3686 assert(VT == MVT::i64 && "Unexpected value type.");
3687 // LHSReg and RHSReg cannot be killed by this Mul, since they are
3688 // reused in the next instruction.
3689 MulReg = emitMul_rr(VT, LHSReg, /*IsKill=*/false, RHSReg,
3691 unsigned SMULHReg = fastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
3693 emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3694 AArch64_AM::ASR, 63, /*WantResult=*/false);
3698 case Intrinsic::umul_with_overflow: {
3700 unsigned LHSReg = getRegForValue(LHS);
3703 bool LHSIsKill = hasTrivialKill(LHS);
3705 unsigned RHSReg = getRegForValue(RHS);
3708 bool RHSIsKill = hasTrivialKill(RHS);
3710 if (VT == MVT::i32) {
3711 MulReg = emitUMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3712 emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
3713 /*IsKill=*/false, AArch64_AM::LSR, 32,
3714 /*WantResult=*/false);
3715 MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3718 assert(VT == MVT::i64 && "Unexpected value type.");
3719 // LHSReg and RHSReg cannot be killed by this Mul, since they are
3720 // reused in the next instruction.
3721 MulReg = emitMul_rr(VT, LHSReg, /*IsKill=*/false, RHSReg,
3723 unsigned UMULHReg = fastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
3725 emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
3726 /*IsKill=*/false, /*WantResult=*/false);
3733 ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
3734 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3735 TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
3738 ResultReg2 = fastEmitInst_rri(AArch64::CSINCWr, &AArch64::GPR32RegClass,
3739 AArch64::WZR, /*IsKill=*/true, AArch64::WZR,
3740 /*IsKill=*/true, getInvertedCondCode(CC));
3742 assert((ResultReg1 + 1) == ResultReg2 &&
3743 "Nonconsecutive result registers.");
3744 updateValueMap(II, ResultReg1, 2);
3751 bool AArch64FastISel::selectRet(const Instruction *I) {
3752 const ReturnInst *Ret = cast<ReturnInst>(I);
3753 const Function &F = *I->getParent()->getParent();
3755 if (!FuncInfo.CanLowerReturn)
3761 if (TLI.supportSwiftError() &&
3762 F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
3765 if (TLI.supportSplitCSR(FuncInfo.MF))
3768 // Build a list of return value registers.
3769 SmallVector<unsigned, 4> RetRegs;
3771 if (Ret->getNumOperands() > 0) {
3772 CallingConv::ID CC = F.getCallingConv();
3773 SmallVector<ISD::OutputArg, 4> Outs;
3774 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
3776 // Analyze operands of the call, assigning locations to each operand.
3777 SmallVector<CCValAssign, 16> ValLocs;
3778 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
3779 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
3780 : RetCC_AArch64_AAPCS;
3781 CCInfo.AnalyzeReturn(Outs, RetCC);
3783 // Only handle a single return value for now.
3784 if (ValLocs.size() != 1)
3787 CCValAssign &VA = ValLocs[0];
3788 const Value *RV = Ret->getOperand(0);
3790 // Don't bother handling odd stuff for now.
3791 if ((VA.getLocInfo() != CCValAssign::Full) &&
3792 (VA.getLocInfo() != CCValAssign::BCvt))
3795 // Only handle register returns for now.
3799 unsigned Reg = getRegForValue(RV);
3803 unsigned SrcReg = Reg + VA.getValNo();
3804 unsigned DestReg = VA.getLocReg();
3805 // Avoid a cross-class copy. This is very unlikely.
3806 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
3809 EVT RVEVT = TLI.getValueType(DL, RV->getType());
3810 if (!RVEVT.isSimple())
3813 // Vectors (of > 1 lane) in big endian need tricky handling.
3814 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1 &&
3815 !Subtarget->isLittleEndian())
3818 MVT RVVT = RVEVT.getSimpleVT();
3819 if (RVVT == MVT::f128)
3822 MVT DestVT = VA.getValVT();
3823 // Special handling for extended integers.
3824 if (RVVT != DestVT) {
3825 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
3828 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
3831 bool IsZExt = Outs[0].Flags.isZExt();
3832 SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
3838 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3839 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
3841 // Add register to return instruction.
3842 RetRegs.push_back(VA.getLocReg());
3845 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3846 TII.get(AArch64::RET_ReallyLR));
3847 for (unsigned RetReg : RetRegs)
3848 MIB.addReg(RetReg, RegState::Implicit);
3852 bool AArch64FastISel::selectTrunc(const Instruction *I) {
3853 Type *DestTy = I->getType();
3854 Value *Op = I->getOperand(0);
3855 Type *SrcTy = Op->getType();
3857 EVT SrcEVT = TLI.getValueType(DL, SrcTy, true);
3858 EVT DestEVT = TLI.getValueType(DL, DestTy, true);
3859 if (!SrcEVT.isSimple())
3861 if (!DestEVT.isSimple())
3864 MVT SrcVT = SrcEVT.getSimpleVT();
3865 MVT DestVT = DestEVT.getSimpleVT();
3867 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
3870 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
3874 unsigned SrcReg = getRegForValue(Op);
3877 bool SrcIsKill = hasTrivialKill(Op);
3879 // If we're truncating from i64 to a smaller non-legal type then generate an
3880 // AND. Otherwise, we know the high bits are undefined and a truncate only
3881 // generate a COPY. We cannot mark the source register also as result
3882 // register, because this can incorrectly transfer the kill flag onto the
3885 if (SrcVT == MVT::i64) {
3887 switch (DestVT.SimpleTy) {
3889 // Trunc i64 to i32 is handled by the target-independent fast-isel.
3901 // Issue an extract_subreg to get the lower 32-bits.
3902 unsigned Reg32 = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
3904 // Create the AND instruction which performs the actual truncation.
3905 ResultReg = emitAnd_ri(MVT::i32, Reg32, /*IsKill=*/true, Mask);
3906 assert(ResultReg && "Unexpected AND instruction emission failure.");
3908 ResultReg = createResultReg(&AArch64::GPR32RegClass);
3909 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3910 TII.get(TargetOpcode::COPY), ResultReg)
3911 .addReg(SrcReg, getKillRegState(SrcIsKill));
3914 updateValueMap(I, ResultReg);
3918 unsigned AArch64FastISel::emiti1Ext(unsigned SrcReg, MVT DestVT, bool IsZExt) {
3919 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
3920 DestVT == MVT::i64) &&
3921 "Unexpected value type.");
3922 // Handle i8 and i16 as i32.
3923 if (DestVT == MVT::i8 || DestVT == MVT::i16)
3927 unsigned ResultReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
3928 assert(ResultReg && "Unexpected AND instruction emission failure.");
3929 if (DestVT == MVT::i64) {
3930 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
3931 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
3932 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3933 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3934 TII.get(AArch64::SUBREG_TO_REG), Reg64)
3937 .addImm(AArch64::sub_32);
3942 if (DestVT == MVT::i64) {
3943 // FIXME: We're SExt i1 to i64.
3946 return fastEmitInst_rii(AArch64::SBFMWri, &AArch64::GPR32RegClass, SrcReg,
3947 /*TODO:IsKill=*/false, 0, 0);
3951 unsigned AArch64FastISel::emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3952 unsigned Op1, bool Op1IsKill) {
3954 switch (RetVT.SimpleTy) {
3960 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
3962 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
3965 const TargetRegisterClass *RC =
3966 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3967 return fastEmitInst_rrr(Opc, RC, Op0, Op0IsKill, Op1, Op1IsKill,
3968 /*IsKill=*/ZReg, true);
3971 unsigned AArch64FastISel::emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3972 unsigned Op1, bool Op1IsKill) {
3973 if (RetVT != MVT::i64)
3976 return fastEmitInst_rrr(AArch64::SMADDLrrr, &AArch64::GPR64RegClass,
3977 Op0, Op0IsKill, Op1, Op1IsKill,
3978 AArch64::XZR, /*IsKill=*/true);
3981 unsigned AArch64FastISel::emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3982 unsigned Op1, bool Op1IsKill) {
3983 if (RetVT != MVT::i64)
3986 return fastEmitInst_rrr(AArch64::UMADDLrrr, &AArch64::GPR64RegClass,
3987 Op0, Op0IsKill, Op1, Op1IsKill,
3988 AArch64::XZR, /*IsKill=*/true);
3991 unsigned AArch64FastISel::emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3992 unsigned Op1Reg, bool Op1IsKill) {
3994 bool NeedTrunc = false;
3996 switch (RetVT.SimpleTy) {
3998 case MVT::i8: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xff; break;
3999 case MVT::i16: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xffff; break;
4000 case MVT::i32: Opc = AArch64::LSLVWr; break;
4001 case MVT::i64: Opc = AArch64::LSLVXr; break;
4004 const TargetRegisterClass *RC =
4005 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4007 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
4010 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
4013 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
4017 unsigned AArch64FastISel::emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
4018 bool Op0IsKill, uint64_t Shift,
4020 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
4021 "Unexpected source/return type pair.");
4022 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
4023 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
4024 "Unexpected source value type.");
4025 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
4026 RetVT == MVT::i64) && "Unexpected return value type.");
4028 bool Is64Bit = (RetVT == MVT::i64);
4029 unsigned RegSize = Is64Bit ? 64 : 32;
4030 unsigned DstBits = RetVT.getSizeInBits();
4031 unsigned SrcBits = SrcVT.getSizeInBits();
4032 const TargetRegisterClass *RC =
4033 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4035 // Just emit a copy for "zero" shifts.
4037 if (RetVT == SrcVT) {
4038 unsigned ResultReg = createResultReg(RC);
4039 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4040 TII.get(TargetOpcode::COPY), ResultReg)
4041 .addReg(Op0, getKillRegState(Op0IsKill));
4044 return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4047 // Don't deal with undefined shifts.
4048 if (Shift >= DstBits)
4051 // For immediate shifts we can fold the zero-/sign-extension into the shift.
4052 // {S|U}BFM Wd, Wn, #r, #s
4053 // Wd<32+s-r,32-r> = Wn<s:0> when r > s
4055 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4056 // %2 = shl i16 %1, 4
4057 // Wd<32+7-28,32-28> = Wn<7:0> <- clamp s to 7
4058 // 0b1111_1111_1111_1111__1111_1010_1010_0000 sext
4059 // 0b0000_0000_0000_0000__0000_0101_0101_0000 sext | zext
4060 // 0b0000_0000_0000_0000__0000_1010_1010_0000 zext
4062 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4063 // %2 = shl i16 %1, 8
4064 // Wd<32+7-24,32-24> = Wn<7:0>
4065 // 0b1111_1111_1111_1111__1010_1010_0000_0000 sext
4066 // 0b0000_0000_0000_0000__0101_0101_0000_0000 sext | zext
4067 // 0b0000_0000_0000_0000__1010_1010_0000_0000 zext
4069 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4070 // %2 = shl i16 %1, 12
4071 // Wd<32+3-20,32-20> = Wn<3:0>
4072 // 0b1111_1111_1111_1111__1010_0000_0000_0000 sext
4073 // 0b0000_0000_0000_0000__0101_0000_0000_0000 sext | zext
4074 // 0b0000_0000_0000_0000__1010_0000_0000_0000 zext
4076 unsigned ImmR = RegSize - Shift;
4077 // Limit the width to the length of the source type.
4078 unsigned ImmS = std::min<unsigned>(SrcBits - 1, DstBits - 1 - Shift);
4079 static const unsigned OpcTable[2][2] = {
4080 {AArch64::SBFMWri, AArch64::SBFMXri},
4081 {AArch64::UBFMWri, AArch64::UBFMXri}
4083 unsigned Opc = OpcTable[IsZExt][Is64Bit];
4084 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
4085 unsigned TmpReg = MRI.createVirtualRegister(RC);
4086 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4087 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4089 .addReg(Op0, getKillRegState(Op0IsKill))
4090 .addImm(AArch64::sub_32);
4094 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4097 unsigned AArch64FastISel::emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
4098 unsigned Op1Reg, bool Op1IsKill) {
4100 bool NeedTrunc = false;
4102 switch (RetVT.SimpleTy) {
4104 case MVT::i8: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xff; break;
4105 case MVT::i16: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xffff; break;
4106 case MVT::i32: Opc = AArch64::LSRVWr; break;
4107 case MVT::i64: Opc = AArch64::LSRVXr; break;
4110 const TargetRegisterClass *RC =
4111 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4113 Op0Reg = emitAnd_ri(MVT::i32, Op0Reg, Op0IsKill, Mask);
4114 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
4115 Op0IsKill = Op1IsKill = true;
4117 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
4120 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
4124 unsigned AArch64FastISel::emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
4125 bool Op0IsKill, uint64_t Shift,
4127 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
4128 "Unexpected source/return type pair.");
4129 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
4130 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
4131 "Unexpected source value type.");
4132 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
4133 RetVT == MVT::i64) && "Unexpected return value type.");
4135 bool Is64Bit = (RetVT == MVT::i64);
4136 unsigned RegSize = Is64Bit ? 64 : 32;
4137 unsigned DstBits = RetVT.getSizeInBits();
4138 unsigned SrcBits = SrcVT.getSizeInBits();
4139 const TargetRegisterClass *RC =
4140 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4142 // Just emit a copy for "zero" shifts.
4144 if (RetVT == SrcVT) {
4145 unsigned ResultReg = createResultReg(RC);
4146 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4147 TII.get(TargetOpcode::COPY), ResultReg)
4148 .addReg(Op0, getKillRegState(Op0IsKill));
4151 return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4154 // Don't deal with undefined shifts.
4155 if (Shift >= DstBits)
4158 // For immediate shifts we can fold the zero-/sign-extension into the shift.
4159 // {S|U}BFM Wd, Wn, #r, #s
4160 // Wd<s-r:0> = Wn<s:r> when r <= s
4162 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4163 // %2 = lshr i16 %1, 4
4164 // Wd<7-4:0> = Wn<7:4>
4165 // 0b0000_0000_0000_0000__0000_1111_1111_1010 sext
4166 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
4167 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
4169 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4170 // %2 = lshr i16 %1, 8
4171 // Wd<7-7,0> = Wn<7:7>
4172 // 0b0000_0000_0000_0000__0000_0000_1111_1111 sext
4173 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4174 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4176 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4177 // %2 = lshr i16 %1, 12
4178 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
4179 // 0b0000_0000_0000_0000__0000_0000_0000_1111 sext
4180 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4181 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4183 if (Shift >= SrcBits && IsZExt)
4184 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
4186 // It is not possible to fold a sign-extend into the LShr instruction. In this
4187 // case emit a sign-extend.
4189 Op0 = emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4194 SrcBits = SrcVT.getSizeInBits();
4198 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
4199 unsigned ImmS = SrcBits - 1;
4200 static const unsigned OpcTable[2][2] = {
4201 {AArch64::SBFMWri, AArch64::SBFMXri},
4202 {AArch64::UBFMWri, AArch64::UBFMXri}
4204 unsigned Opc = OpcTable[IsZExt][Is64Bit];
4205 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
4206 unsigned TmpReg = MRI.createVirtualRegister(RC);
4207 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4208 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4210 .addReg(Op0, getKillRegState(Op0IsKill))
4211 .addImm(AArch64::sub_32);
4215 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4218 unsigned AArch64FastISel::emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
4219 unsigned Op1Reg, bool Op1IsKill) {
4221 bool NeedTrunc = false;
4223 switch (RetVT.SimpleTy) {
4225 case MVT::i8: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xff; break;
4226 case MVT::i16: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xffff; break;
4227 case MVT::i32: Opc = AArch64::ASRVWr; break;
4228 case MVT::i64: Opc = AArch64::ASRVXr; break;
4231 const TargetRegisterClass *RC =
4232 (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4234 Op0Reg = emitIntExt(RetVT, Op0Reg, MVT::i32, /*IsZExt=*/false);
4235 Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
4236 Op0IsKill = Op1IsKill = true;
4238 unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
4241 ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
4245 unsigned AArch64FastISel::emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
4246 bool Op0IsKill, uint64_t Shift,
4248 assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
4249 "Unexpected source/return type pair.");
4250 assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
4251 SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
4252 "Unexpected source value type.");
4253 assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
4254 RetVT == MVT::i64) && "Unexpected return value type.");
4256 bool Is64Bit = (RetVT == MVT::i64);
4257 unsigned RegSize = Is64Bit ? 64 : 32;
4258 unsigned DstBits = RetVT.getSizeInBits();
4259 unsigned SrcBits = SrcVT.getSizeInBits();
4260 const TargetRegisterClass *RC =
4261 Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4263 // Just emit a copy for "zero" shifts.
4265 if (RetVT == SrcVT) {
4266 unsigned ResultReg = createResultReg(RC);
4267 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4268 TII.get(TargetOpcode::COPY), ResultReg)
4269 .addReg(Op0, getKillRegState(Op0IsKill));
4272 return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4275 // Don't deal with undefined shifts.
4276 if (Shift >= DstBits)
4279 // For immediate shifts we can fold the zero-/sign-extension into the shift.
4280 // {S|U}BFM Wd, Wn, #r, #s
4281 // Wd<s-r:0> = Wn<s:r> when r <= s
4283 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4284 // %2 = ashr i16 %1, 4
4285 // Wd<7-4:0> = Wn<7:4>
4286 // 0b1111_1111_1111_1111__1111_1111_1111_1010 sext
4287 // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
4288 // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
4290 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4291 // %2 = ashr i16 %1, 8
4292 // Wd<7-7,0> = Wn<7:7>
4293 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
4294 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4295 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4297 // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4298 // %2 = ashr i16 %1, 12
4299 // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
4300 // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
4301 // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4302 // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4304 if (Shift >= SrcBits && IsZExt)
4305 return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
4307 unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
4308 unsigned ImmS = SrcBits - 1;
4309 static const unsigned OpcTable[2][2] = {
4310 {AArch64::SBFMWri, AArch64::SBFMXri},
4311 {AArch64::UBFMWri, AArch64::UBFMXri}
4313 unsigned Opc = OpcTable[IsZExt][Is64Bit];
4314 if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
4315 unsigned TmpReg = MRI.createVirtualRegister(RC);
4316 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4317 TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4319 .addReg(Op0, getKillRegState(Op0IsKill))
4320 .addImm(AArch64::sub_32);
4324 return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4327 unsigned AArch64FastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
4329 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
4331 // FastISel does not have plumbing to deal with extensions where the SrcVT or
4332 // DestVT are odd things, so test to make sure that they are both types we can
4333 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
4334 // bail out to SelectionDAG.
4335 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
4336 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
4337 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
4338 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
4344 switch (SrcVT.SimpleTy) {
4348 return emiti1Ext(SrcReg, DestVT, IsZExt);
4350 if (DestVT == MVT::i64)
4351 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4353 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
4357 if (DestVT == MVT::i64)
4358 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4360 Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
4364 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
4365 Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4370 // Handle i8 and i16 as i32.
4371 if (DestVT == MVT::i8 || DestVT == MVT::i16)
4373 else if (DestVT == MVT::i64) {
4374 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
4375 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4376 TII.get(AArch64::SUBREG_TO_REG), Src64)
4379 .addImm(AArch64::sub_32);
4383 const TargetRegisterClass *RC =
4384 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4385 return fastEmitInst_rii(Opc, RC, SrcReg, /*TODO:IsKill=*/false, 0, Imm);
4388 static bool isZExtLoad(const MachineInstr *LI) {
4389 switch (LI->getOpcode()) {
4392 case AArch64::LDURBBi:
4393 case AArch64::LDURHHi:
4394 case AArch64::LDURWi:
4395 case AArch64::LDRBBui:
4396 case AArch64::LDRHHui:
4397 case AArch64::LDRWui:
4398 case AArch64::LDRBBroX:
4399 case AArch64::LDRHHroX:
4400 case AArch64::LDRWroX:
4401 case AArch64::LDRBBroW:
4402 case AArch64::LDRHHroW:
4403 case AArch64::LDRWroW:
4408 static bool isSExtLoad(const MachineInstr *LI) {
4409 switch (LI->getOpcode()) {
4412 case AArch64::LDURSBWi:
4413 case AArch64::LDURSHWi:
4414 case AArch64::LDURSBXi:
4415 case AArch64::LDURSHXi:
4416 case AArch64::LDURSWi:
4417 case AArch64::LDRSBWui:
4418 case AArch64::LDRSHWui:
4419 case AArch64::LDRSBXui:
4420 case AArch64::LDRSHXui:
4421 case AArch64::LDRSWui:
4422 case AArch64::LDRSBWroX:
4423 case AArch64::LDRSHWroX:
4424 case AArch64::LDRSBXroX:
4425 case AArch64::LDRSHXroX:
4426 case AArch64::LDRSWroX:
4427 case AArch64::LDRSBWroW:
4428 case AArch64::LDRSHWroW:
4429 case AArch64::LDRSBXroW:
4430 case AArch64::LDRSHXroW:
4431 case AArch64::LDRSWroW:
4436 bool AArch64FastISel::optimizeIntExtLoad(const Instruction *I, MVT RetVT,
4438 const auto *LI = dyn_cast<LoadInst>(I->getOperand(0));
4439 if (!LI || !LI->hasOneUse())
4442 // Check if the load instruction has already been selected.
4443 unsigned Reg = lookUpRegForValue(LI);
4447 MachineInstr *MI = MRI.getUniqueVRegDef(Reg);
4451 // Check if the correct load instruction has been emitted - SelectionDAG might
4452 // have emitted a zero-extending load, but we need a sign-extending load.
4453 bool IsZExt = isa<ZExtInst>(I);
4454 const auto *LoadMI = MI;
4455 if (LoadMI->getOpcode() == TargetOpcode::COPY &&
4456 LoadMI->getOperand(1).getSubReg() == AArch64::sub_32) {
4457 unsigned LoadReg = MI->getOperand(1).getReg();
4458 LoadMI = MRI.getUniqueVRegDef(LoadReg);
4459 assert(LoadMI && "Expected valid instruction");
4461 if (!(IsZExt && isZExtLoad(LoadMI)) && !(!IsZExt && isSExtLoad(LoadMI)))
4464 // Nothing to be done.
4465 if (RetVT != MVT::i64 || SrcVT > MVT::i32) {
4466 updateValueMap(I, Reg);
4471 unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
4472 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4473 TII.get(AArch64::SUBREG_TO_REG), Reg64)
4475 .addReg(Reg, getKillRegState(true))
4476 .addImm(AArch64::sub_32);
4479 assert((MI->getOpcode() == TargetOpcode::COPY &&
4480 MI->getOperand(1).getSubReg() == AArch64::sub_32) &&
4481 "Expected copy instruction");
4482 Reg = MI->getOperand(1).getReg();
4483 MI->eraseFromParent();
4485 updateValueMap(I, Reg);
4489 bool AArch64FastISel::selectIntExt(const Instruction *I) {
4490 assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
4491 "Unexpected integer extend instruction.");
4494 if (!isTypeSupported(I->getType(), RetVT))
4497 if (!isTypeSupported(I->getOperand(0)->getType(), SrcVT))
4500 // Try to optimize already sign-/zero-extended values from load instructions.
4501 if (optimizeIntExtLoad(I, RetVT, SrcVT))
4504 unsigned SrcReg = getRegForValue(I->getOperand(0));
4507 bool SrcIsKill = hasTrivialKill(I->getOperand(0));
4509 // Try to optimize already sign-/zero-extended values from function arguments.
4510 bool IsZExt = isa<ZExtInst>(I);
4511 if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0))) {
4512 if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr())) {
4513 if (RetVT == MVT::i64 && SrcVT != MVT::i64) {
4514 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
4515 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4516 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
4518 .addReg(SrcReg, getKillRegState(SrcIsKill))
4519 .addImm(AArch64::sub_32);
4522 // Conservatively clear all kill flags from all uses, because we are
4523 // replacing a sign-/zero-extend instruction at IR level with a nop at MI
4524 // level. The result of the instruction at IR level might have been
4525 // trivially dead, which is now not longer true.
4526 unsigned UseReg = lookUpRegForValue(I);
4528 MRI.clearKillFlags(UseReg);
4530 updateValueMap(I, SrcReg);
4535 unsigned ResultReg = emitIntExt(SrcVT, SrcReg, RetVT, IsZExt);
4539 updateValueMap(I, ResultReg);
4543 bool AArch64FastISel::selectRem(const Instruction *I, unsigned ISDOpcode) {
4544 EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
4545 if (!DestEVT.isSimple())
4548 MVT DestVT = DestEVT.getSimpleVT();
4549 if (DestVT != MVT::i64 && DestVT != MVT::i32)
4553 bool Is64bit = (DestVT == MVT::i64);
4554 switch (ISDOpcode) {
4558 DivOpc = Is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
4561 DivOpc = Is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
4564 unsigned MSubOpc = Is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
4565 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4568 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4570 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4573 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4575 const TargetRegisterClass *RC =
4576 (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4577 unsigned QuotReg = fastEmitInst_rr(DivOpc, RC, Src0Reg, /*IsKill=*/false,
4578 Src1Reg, /*IsKill=*/false);
4579 assert(QuotReg && "Unexpected DIV instruction emission failure.");
4580 // The remainder is computed as numerator - (quotient * denominator) using the
4581 // MSUB instruction.
4582 unsigned ResultReg = fastEmitInst_rrr(MSubOpc, RC, QuotReg, /*IsKill=*/true,
4583 Src1Reg, Src1IsKill, Src0Reg,
4585 updateValueMap(I, ResultReg);
4589 bool AArch64FastISel::selectMul(const Instruction *I) {
4591 if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
4595 return selectBinaryOp(I, ISD::MUL);
4597 const Value *Src0 = I->getOperand(0);
4598 const Value *Src1 = I->getOperand(1);
4599 if (const auto *C = dyn_cast<ConstantInt>(Src0))
4600 if (C->getValue().isPowerOf2())
4601 std::swap(Src0, Src1);
4603 // Try to simplify to a shift instruction.
4604 if (const auto *C = dyn_cast<ConstantInt>(Src1))
4605 if (C->getValue().isPowerOf2()) {
4606 uint64_t ShiftVal = C->getValue().logBase2();
4609 if (const auto *ZExt = dyn_cast<ZExtInst>(Src0)) {
4610 if (!isIntExtFree(ZExt)) {
4612 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), VT)) {
4615 Src0 = ZExt->getOperand(0);
4618 } else if (const auto *SExt = dyn_cast<SExtInst>(Src0)) {
4619 if (!isIntExtFree(SExt)) {
4621 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), VT)) {
4624 Src0 = SExt->getOperand(0);
4629 unsigned Src0Reg = getRegForValue(Src0);
4632 bool Src0IsKill = hasTrivialKill(Src0);
4634 unsigned ResultReg =
4635 emitLSL_ri(VT, SrcVT, Src0Reg, Src0IsKill, ShiftVal, IsZExt);
4638 updateValueMap(I, ResultReg);
4643 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4646 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4648 unsigned Src1Reg = getRegForValue(I->getOperand(1));
4651 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4653 unsigned ResultReg = emitMul_rr(VT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
4658 updateValueMap(I, ResultReg);
4662 bool AArch64FastISel::selectShift(const Instruction *I) {
4664 if (!isTypeSupported(I->getType(), RetVT, /*IsVectorAllowed=*/true))
4667 if (RetVT.isVector())
4668 return selectOperator(I, I->getOpcode());
4670 if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
4671 unsigned ResultReg = 0;
4672 uint64_t ShiftVal = C->getZExtValue();
4674 bool IsZExt = I->getOpcode() != Instruction::AShr;
4675 const Value *Op0 = I->getOperand(0);
4676 if (const auto *ZExt = dyn_cast<ZExtInst>(Op0)) {
4677 if (!isIntExtFree(ZExt)) {
4679 if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), TmpVT)) {
4682 Op0 = ZExt->getOperand(0);
4685 } else if (const auto *SExt = dyn_cast<SExtInst>(Op0)) {
4686 if (!isIntExtFree(SExt)) {
4688 if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), TmpVT)) {
4691 Op0 = SExt->getOperand(0);
4696 unsigned Op0Reg = getRegForValue(Op0);
4699 bool Op0IsKill = hasTrivialKill(Op0);
4701 switch (I->getOpcode()) {
4702 default: llvm_unreachable("Unexpected instruction.");
4703 case Instruction::Shl:
4704 ResultReg = emitLSL_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4706 case Instruction::AShr:
4707 ResultReg = emitASR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4709 case Instruction::LShr:
4710 ResultReg = emitLSR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4716 updateValueMap(I, ResultReg);
4720 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4723 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4725 unsigned Op1Reg = getRegForValue(I->getOperand(1));
4728 bool Op1IsKill = hasTrivialKill(I->getOperand(1));
4730 unsigned ResultReg = 0;
4731 switch (I->getOpcode()) {
4732 default: llvm_unreachable("Unexpected instruction.");
4733 case Instruction::Shl:
4734 ResultReg = emitLSL_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4736 case Instruction::AShr:
4737 ResultReg = emitASR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4739 case Instruction::LShr:
4740 ResultReg = emitLSR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4747 updateValueMap(I, ResultReg);
4751 bool AArch64FastISel::selectBitCast(const Instruction *I) {
4754 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
4756 if (!isTypeLegal(I->getType(), RetVT))
4760 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
4761 Opc = AArch64::FMOVWSr;
4762 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
4763 Opc = AArch64::FMOVXDr;
4764 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
4765 Opc = AArch64::FMOVSWr;
4766 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
4767 Opc = AArch64::FMOVDXr;
4771 const TargetRegisterClass *RC = nullptr;
4772 switch (RetVT.SimpleTy) {
4773 default: llvm_unreachable("Unexpected value type.");
4774 case MVT::i32: RC = &AArch64::GPR32RegClass; break;
4775 case MVT::i64: RC = &AArch64::GPR64RegClass; break;
4776 case MVT::f32: RC = &AArch64::FPR32RegClass; break;
4777 case MVT::f64: RC = &AArch64::FPR64RegClass; break;
4779 unsigned Op0Reg = getRegForValue(I->getOperand(0));
4782 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4783 unsigned ResultReg = fastEmitInst_r(Opc, RC, Op0Reg, Op0IsKill);
4788 updateValueMap(I, ResultReg);
4792 bool AArch64FastISel::selectFRem(const Instruction *I) {
4794 if (!isTypeLegal(I->getType(), RetVT))
4798 switch (RetVT.SimpleTy) {
4802 LC = RTLIB::REM_F32;
4805 LC = RTLIB::REM_F64;
4810 Args.reserve(I->getNumOperands());
4812 // Populate the argument list.
4813 for (auto &Arg : I->operands()) {
4816 Entry.Ty = Arg->getType();
4817 Args.push_back(Entry);
4820 CallLoweringInfo CLI;
4821 MCContext &Ctx = MF->getContext();
4822 CLI.setCallee(DL, Ctx, TLI.getLibcallCallingConv(LC), I->getType(),
4823 TLI.getLibcallName(LC), std::move(Args));
4824 if (!lowerCallTo(CLI))
4826 updateValueMap(I, CLI.ResultReg);
4830 bool AArch64FastISel::selectSDiv(const Instruction *I) {
4832 if (!isTypeLegal(I->getType(), VT))
4835 if (!isa<ConstantInt>(I->getOperand(1)))
4836 return selectBinaryOp(I, ISD::SDIV);
4838 const APInt &C = cast<ConstantInt>(I->getOperand(1))->getValue();
4839 if ((VT != MVT::i32 && VT != MVT::i64) || !C ||
4840 !(C.isPowerOf2() || (-C).isPowerOf2()))
4841 return selectBinaryOp(I, ISD::SDIV);
4843 unsigned Lg2 = C.countTrailingZeros();
4844 unsigned Src0Reg = getRegForValue(I->getOperand(0));
4847 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4849 if (cast<BinaryOperator>(I)->isExact()) {
4850 unsigned ResultReg = emitASR_ri(VT, VT, Src0Reg, Src0IsKill, Lg2);
4853 updateValueMap(I, ResultReg);
4857 int64_t Pow2MinusOne = (1ULL << Lg2) - 1;
4858 unsigned AddReg = emitAdd_ri_(VT, Src0Reg, /*IsKill=*/false, Pow2MinusOne);
4862 // (Src0 < 0) ? Pow2 - 1 : 0;
4863 if (!emitICmp_ri(VT, Src0Reg, /*IsKill=*/false, 0))
4867 const TargetRegisterClass *RC;
4868 if (VT == MVT::i64) {
4869 SelectOpc = AArch64::CSELXr;
4870 RC = &AArch64::GPR64RegClass;
4872 SelectOpc = AArch64::CSELWr;
4873 RC = &AArch64::GPR32RegClass;
4875 unsigned SelectReg =
4876 fastEmitInst_rri(SelectOpc, RC, AddReg, /*IsKill=*/true, Src0Reg,
4877 Src0IsKill, AArch64CC::LT);
4881 // Divide by Pow2 --> ashr. If we're dividing by a negative value we must also
4882 // negate the result.
4883 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
4886 ResultReg = emitAddSub_rs(/*UseAdd=*/false, VT, ZeroReg, /*IsKill=*/true,
4887 SelectReg, /*IsKill=*/true, AArch64_AM::ASR, Lg2);
4889 ResultReg = emitASR_ri(VT, VT, SelectReg, /*IsKill=*/true, Lg2);
4894 updateValueMap(I, ResultReg);
4898 /// This is mostly a copy of the existing FastISel getRegForGEPIndex code. We
4899 /// have to duplicate it for AArch64, because otherwise we would fail during the
4900 /// sign-extend emission.
4901 std::pair<unsigned, bool> AArch64FastISel::getRegForGEPIndex(const Value *Idx) {
4902 unsigned IdxN = getRegForValue(Idx);
4904 // Unhandled operand. Halt "fast" selection and bail.
4905 return std::pair<unsigned, bool>(0, false);
4907 bool IdxNIsKill = hasTrivialKill(Idx);
4909 // If the index is smaller or larger than intptr_t, truncate or extend it.
4910 MVT PtrVT = TLI.getPointerTy(DL);
4911 EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
4912 if (IdxVT.bitsLT(PtrVT)) {
4913 IdxN = emitIntExt(IdxVT.getSimpleVT(), IdxN, PtrVT, /*IsZExt=*/false);
4915 } else if (IdxVT.bitsGT(PtrVT))
4916 llvm_unreachable("AArch64 FastISel doesn't support types larger than i64");
4917 return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
4920 /// This is mostly a copy of the existing FastISel GEP code, but we have to
4921 /// duplicate it for AArch64, because otherwise we would bail out even for
4922 /// simple cases. This is because the standard fastEmit functions don't cover
4923 /// MUL at all and ADD is lowered very inefficientily.
4924 bool AArch64FastISel::selectGetElementPtr(const Instruction *I) {
4925 unsigned N = getRegForValue(I->getOperand(0));
4928 bool NIsKill = hasTrivialKill(I->getOperand(0));
4930 // Keep a running tab of the total offset to coalesce multiple N = N + Offset
4931 // into a single N = N + TotalOffset.
4932 uint64_t TotalOffs = 0;
4933 MVT VT = TLI.getPointerTy(DL);
4934 for (gep_type_iterator GTI = gep_type_begin(I), E = gep_type_end(I);
4936 const Value *Idx = GTI.getOperand();
4937 if (auto *StTy = GTI.getStructTypeOrNull()) {
4938 unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
4941 TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
4943 Type *Ty = GTI.getIndexedType();
4945 // If this is a constant subscript, handle it quickly.
4946 if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
4951 DL.getTypeAllocSize(Ty) * cast<ConstantInt>(CI)->getSExtValue();
4955 N = emitAdd_ri_(VT, N, NIsKill, TotalOffs);
4962 // N = N + Idx * ElementSize;
4963 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
4964 std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
4965 unsigned IdxN = Pair.first;
4966 bool IdxNIsKill = Pair.second;
4970 if (ElementSize != 1) {
4971 unsigned C = fastEmit_i(VT, VT, ISD::Constant, ElementSize);
4974 IdxN = emitMul_rr(VT, IdxN, IdxNIsKill, C, true);
4979 N = fastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
4985 N = emitAdd_ri_(VT, N, NIsKill, TotalOffs);
4989 updateValueMap(I, N);
4993 bool AArch64FastISel::selectAtomicCmpXchg(const AtomicCmpXchgInst *I) {
4994 assert(TM.getOptLevel() == CodeGenOpt::None &&
4995 "cmpxchg survived AtomicExpand at optlevel > -O0");
4997 auto *RetPairTy = cast<StructType>(I->getType());
4998 Type *RetTy = RetPairTy->getTypeAtIndex(0U);
4999 assert(RetPairTy->getTypeAtIndex(1U)->isIntegerTy(1) &&
5000 "cmpxchg has a non-i1 status result");
5003 if (!isTypeLegal(RetTy, VT))
5006 const TargetRegisterClass *ResRC;
5007 unsigned Opc, CmpOpc;
5008 // This only supports i32/i64, because i8/i16 aren't legal, and the generic
5009 // extractvalue selection doesn't support that.
5010 if (VT == MVT::i32) {
5011 Opc = AArch64::CMP_SWAP_32;
5012 CmpOpc = AArch64::SUBSWrs;
5013 ResRC = &AArch64::GPR32RegClass;
5014 } else if (VT == MVT::i64) {
5015 Opc = AArch64::CMP_SWAP_64;
5016 CmpOpc = AArch64::SUBSXrs;
5017 ResRC = &AArch64::GPR64RegClass;
5022 const MCInstrDesc &II = TII.get(Opc);
5024 const unsigned AddrReg = constrainOperandRegClass(
5025 II, getRegForValue(I->getPointerOperand()), II.getNumDefs());
5026 const unsigned DesiredReg = constrainOperandRegClass(
5027 II, getRegForValue(I->getCompareOperand()), II.getNumDefs() + 1);
5028 const unsigned NewReg = constrainOperandRegClass(
5029 II, getRegForValue(I->getNewValOperand()), II.getNumDefs() + 2);
5031 const unsigned ResultReg1 = createResultReg(ResRC);
5032 const unsigned ResultReg2 = createResultReg(&AArch64::GPR32RegClass);
5033 const unsigned ScratchReg = createResultReg(&AArch64::GPR32RegClass);
5035 // FIXME: MachineMemOperand doesn't support cmpxchg yet.
5036 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
5043 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
5044 .addDef(VT == MVT::i32 ? AArch64::WZR : AArch64::XZR)
5049 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr))
5051 .addUse(AArch64::WZR)
5052 .addUse(AArch64::WZR)
5053 .addImm(AArch64CC::NE);
5055 assert((ResultReg1 + 1) == ResultReg2 && "Nonconsecutive result registers.");
5056 updateValueMap(I, ResultReg1, 2);
5060 bool AArch64FastISel::fastSelectInstruction(const Instruction *I) {
5061 switch (I->getOpcode()) {
5064 case Instruction::Add:
5065 case Instruction::Sub:
5066 return selectAddSub(I);
5067 case Instruction::Mul:
5068 return selectMul(I);
5069 case Instruction::SDiv:
5070 return selectSDiv(I);
5071 case Instruction::SRem:
5072 if (!selectBinaryOp(I, ISD::SREM))
5073 return selectRem(I, ISD::SREM);
5075 case Instruction::URem:
5076 if (!selectBinaryOp(I, ISD::UREM))
5077 return selectRem(I, ISD::UREM);
5079 case Instruction::Shl:
5080 case Instruction::LShr:
5081 case Instruction::AShr:
5082 return selectShift(I);
5083 case Instruction::And:
5084 case Instruction::Or:
5085 case Instruction::Xor:
5086 return selectLogicalOp(I);
5087 case Instruction::Br:
5088 return selectBranch(I);
5089 case Instruction::IndirectBr:
5090 return selectIndirectBr(I);
5091 case Instruction::BitCast:
5092 if (!FastISel::selectBitCast(I))
5093 return selectBitCast(I);
5095 case Instruction::FPToSI:
5096 if (!selectCast(I, ISD::FP_TO_SINT))
5097 return selectFPToInt(I, /*Signed=*/true);
5099 case Instruction::FPToUI:
5100 return selectFPToInt(I, /*Signed=*/false);
5101 case Instruction::ZExt:
5102 case Instruction::SExt:
5103 return selectIntExt(I);
5104 case Instruction::Trunc:
5105 if (!selectCast(I, ISD::TRUNCATE))
5106 return selectTrunc(I);
5108 case Instruction::FPExt:
5109 return selectFPExt(I);
5110 case Instruction::FPTrunc:
5111 return selectFPTrunc(I);
5112 case Instruction::SIToFP:
5113 if (!selectCast(I, ISD::SINT_TO_FP))
5114 return selectIntToFP(I, /*Signed=*/true);
5116 case Instruction::UIToFP:
5117 return selectIntToFP(I, /*Signed=*/false);
5118 case Instruction::Load:
5119 return selectLoad(I);
5120 case Instruction::Store:
5121 return selectStore(I);
5122 case Instruction::FCmp:
5123 case Instruction::ICmp:
5124 return selectCmp(I);
5125 case Instruction::Select:
5126 return selectSelect(I);
5127 case Instruction::Ret:
5128 return selectRet(I);
5129 case Instruction::FRem:
5130 return selectFRem(I);
5131 case Instruction::GetElementPtr:
5132 return selectGetElementPtr(I);
5133 case Instruction::AtomicCmpXchg:
5134 return selectAtomicCmpXchg(cast<AtomicCmpXchgInst>(I));
5137 // fall-back to target-independent instruction selection.
5138 return selectOperator(I, I->getOpcode());
5139 // Silence warnings.
5140 (void)&CC_AArch64_DarwinPCS_VarArg;
5141 (void)&CC_AArch64_Win64_VarArg;
5146 FastISel *AArch64::createFastISel(FunctionLoweringInfo &FuncInfo,
5147 const TargetLibraryInfo *LibInfo) {
5148 return new AArch64FastISel(FuncInfo, LibInfo);
5151 } // end namespace llvm