1 //===-- AArch6464FastISel.cpp - AArch64 FastISel implementation -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the AArch64-specific support for the FastISel class. Some
11 // of the target-specific code is generated by tablegen in the file
12 // AArch64GenFastISel.inc, which is #included here.
14 //===----------------------------------------------------------------------===//
17 #include "AArch64Subtarget.h"
18 #include "AArch64TargetMachine.h"
19 #include "MCTargetDesc/AArch64AddressingModes.h"
20 #include "llvm/Analysis/BranchProbabilityInfo.h"
21 #include "llvm/CodeGen/CallingConvLower.h"
22 #include "llvm/CodeGen/FastISel.h"
23 #include "llvm/CodeGen/FunctionLoweringInfo.h"
24 #include "llvm/CodeGen/MachineConstantPool.h"
25 #include "llvm/CodeGen/MachineFrameInfo.h"
26 #include "llvm/CodeGen/MachineInstrBuilder.h"
27 #include "llvm/CodeGen/MachineRegisterInfo.h"
28 #include "llvm/IR/CallingConv.h"
29 #include "llvm/IR/DataLayout.h"
30 #include "llvm/IR/DerivedTypes.h"
31 #include "llvm/IR/Function.h"
32 #include "llvm/IR/GetElementPtrTypeIterator.h"
33 #include "llvm/IR/GlobalAlias.h"
34 #include "llvm/IR/GlobalVariable.h"
35 #include "llvm/IR/Instructions.h"
36 #include "llvm/IR/IntrinsicInst.h"
37 #include "llvm/IR/Operator.h"
38 #include "llvm/Support/CommandLine.h"
43 class AArch64FastISel : public FastISel {
53 AArch64_AM::ShiftExtendType ExtType;
61 const GlobalValue *GV;
64 Address() : Kind(RegBase), ExtType(AArch64_AM::InvalidShiftExtend),
65 OffsetReg(0), Shift(0), Offset(0), GV(nullptr) { Base.Reg = 0; }
66 void setKind(BaseKind K) { Kind = K; }
67 BaseKind getKind() const { return Kind; }
68 void setExtendType(AArch64_AM::ShiftExtendType E) { ExtType = E; }
69 AArch64_AM::ShiftExtendType getExtendType() const { return ExtType; }
70 bool isRegBase() const { return Kind == RegBase; }
71 bool isFIBase() const { return Kind == FrameIndexBase; }
72 void setReg(unsigned Reg) {
73 assert(isRegBase() && "Invalid base register access!");
76 unsigned getReg() const {
77 assert(isRegBase() && "Invalid base register access!");
80 void setOffsetReg(unsigned Reg) {
81 assert(isRegBase() && "Invalid offset register access!");
84 unsigned getOffsetReg() const {
85 assert(isRegBase() && "Invalid offset register access!");
88 void setFI(unsigned FI) {
89 assert(isFIBase() && "Invalid base frame index access!");
92 unsigned getFI() const {
93 assert(isFIBase() && "Invalid base frame index access!");
96 void setOffset(int64_t O) { Offset = O; }
97 int64_t getOffset() { return Offset; }
98 void setShift(unsigned S) { Shift = S; }
99 unsigned getShift() { return Shift; }
101 void setGlobalValue(const GlobalValue *G) { GV = G; }
102 const GlobalValue *getGlobalValue() { return GV; }
105 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
106 /// make the right decision when generating code for different targets.
107 const AArch64Subtarget *Subtarget;
108 LLVMContext *Context;
110 bool FastLowerArguments() override;
111 bool FastLowerCall(CallLoweringInfo &CLI) override;
112 bool FastLowerIntrinsicCall(const IntrinsicInst *II) override;
115 // Selection routines.
116 bool SelectLoad(const Instruction *I);
117 bool SelectStore(const Instruction *I);
118 bool SelectBranch(const Instruction *I);
119 bool SelectIndirectBr(const Instruction *I);
120 bool SelectCmp(const Instruction *I);
121 bool SelectSelect(const Instruction *I);
122 bool SelectFPExt(const Instruction *I);
123 bool SelectFPTrunc(const Instruction *I);
124 bool SelectFPToInt(const Instruction *I, bool Signed);
125 bool SelectIntToFP(const Instruction *I, bool Signed);
126 bool SelectRem(const Instruction *I, unsigned ISDOpcode);
127 bool SelectRet(const Instruction *I);
128 bool SelectTrunc(const Instruction *I);
129 bool SelectIntExt(const Instruction *I);
130 bool SelectMul(const Instruction *I);
131 bool SelectShift(const Instruction *I, bool IsLeftShift, bool IsArithmetic);
132 bool SelectBitCast(const Instruction *I);
134 // Utility helper routines.
135 bool isTypeLegal(Type *Ty, MVT &VT);
136 bool isLoadStoreTypeLegal(Type *Ty, MVT &VT);
137 bool ComputeAddress(const Value *Obj, Address &Addr, Type *Ty = nullptr);
138 bool ComputeCallAddress(const Value *V, Address &Addr);
139 bool SimplifyAddress(Address &Addr, MVT VT);
140 void AddLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
141 unsigned Flags, unsigned ScaleFactor,
142 MachineMemOperand *MMO);
143 bool IsMemCpySmall(uint64_t Len, unsigned Alignment);
144 bool TryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
146 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
149 // Emit helper routines.
150 unsigned emitAddsSubs(bool UseAdds, MVT RetVT, const Value *LHS,
151 const Value *RHS, bool IsZExt = false,
152 bool WantResult = true);
153 unsigned emitAddsSubs_rr(bool UseAdds, MVT RetVT, unsigned LHSReg,
154 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
155 bool WantResult = true);
156 unsigned emitAddsSubs_ri(bool UseAdds, MVT RetVT, unsigned LHSReg,
157 bool LHSIsKill, uint64_t Imm,
158 bool WantResult = true);
159 unsigned emitAddsSubs_rs(bool UseAdds, MVT RetVT, unsigned LHSReg,
160 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
161 AArch64_AM::ShiftExtendType ShiftType,
162 uint64_t ShiftImm, bool WantResult = true);
163 unsigned emitAddsSubs_rx(bool UseAdds, MVT RetVT, unsigned LHSReg,
164 bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
165 AArch64_AM::ShiftExtendType ExtType,
166 uint64_t ShiftImm, bool WantResult = true);
169 bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
170 bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
171 bool emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
172 bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
173 bool EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
174 MachineMemOperand *MMO = nullptr);
175 bool EmitStore(MVT VT, unsigned SrcReg, Address Addr,
176 MachineMemOperand *MMO = nullptr);
177 unsigned EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
178 unsigned Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
179 unsigned emitAdds(MVT RetVT, const Value *LHS, const Value *RHS,
180 bool IsZExt = false, bool WantResult = true);
181 unsigned emitSubs(MVT RetVT, const Value *LHS, const Value *RHS,
182 bool IsZExt = false, bool WantResult = true);
183 unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
184 unsigned RHSReg, bool RHSIsKill, bool WantResult = true);
185 unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
186 unsigned RHSReg, bool RHSIsKill,
187 AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
188 bool WantResult = true);
189 unsigned Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
190 unsigned Op1, bool Op1IsKill);
191 unsigned Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
192 unsigned Op1, bool Op1IsKill);
193 unsigned Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
194 unsigned Op1, bool Op1IsKill);
195 unsigned Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
196 unsigned Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
197 unsigned Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
199 unsigned AArch64MaterializeInt(const ConstantInt *CI, MVT VT);
200 unsigned AArch64MaterializeFP(const ConstantFP *CFP, MVT VT);
201 unsigned AArch64MaterializeGV(const GlobalValue *GV);
203 // Call handling routines.
205 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
206 bool ProcessCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
208 bool FinishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
211 // Backend specific FastISel code.
212 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
213 unsigned TargetMaterializeConstant(const Constant *C) override;
215 explicit AArch64FastISel(FunctionLoweringInfo &funcInfo,
216 const TargetLibraryInfo *libInfo)
217 : FastISel(funcInfo, libInfo) {
218 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
219 Context = &funcInfo.Fn->getContext();
222 bool TargetSelectInstruction(const Instruction *I) override;
224 #include "AArch64GenFastISel.inc"
227 } // end anonymous namespace
229 #include "AArch64GenCallingConv.inc"
231 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
232 if (CC == CallingConv::WebKit_JS)
233 return CC_AArch64_WebKit_JS;
234 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
237 unsigned AArch64FastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
238 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
239 "Alloca should always return a pointer.");
241 // Don't handle dynamic allocas.
242 if (!FuncInfo.StaticAllocaMap.count(AI))
245 DenseMap<const AllocaInst *, int>::iterator SI =
246 FuncInfo.StaticAllocaMap.find(AI);
248 if (SI != FuncInfo.StaticAllocaMap.end()) {
249 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
250 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
252 .addFrameIndex(SI->second)
261 unsigned AArch64FastISel::AArch64MaterializeInt(const ConstantInt *CI, MVT VT) {
266 return FastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
268 // Create a copy from the zero register to materialize a "0" value.
269 const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
270 : &AArch64::GPR32RegClass;
271 unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
272 unsigned ResultReg = createResultReg(RC);
273 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
274 TII.get(TargetOpcode::COPY), ResultReg)
275 .addReg(ZeroReg, getKillRegState(true));
279 unsigned AArch64FastISel::AArch64MaterializeFP(const ConstantFP *CFP, MVT VT) {
280 if (VT != MVT::f32 && VT != MVT::f64)
283 const APFloat Val = CFP->getValueAPF();
284 bool Is64Bit = (VT == MVT::f64);
286 // This checks to see if we can use FMOV instructions to materialize
287 // a constant, otherwise we have to materialize via the constant pool.
288 if (TLI.isFPImmLegal(Val, VT)) {
289 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
290 // Positive zero (+0.0) has to be materialized with a fmov from the zero
291 // register, because the immediate version of fmov cannot encode zero.
292 if (Val.isPosZero()) {
293 unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
294 unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
296 .addReg(ZReg, getKillRegState(true));
299 int Imm = Is64Bit ? AArch64_AM::getFP64Imm(Val)
300 : AArch64_AM::getFP32Imm(Val);
301 unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
302 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
307 // Materialize via constant pool. MachineConstantPool wants an explicit
309 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
311 Align = DL.getTypeAllocSize(CFP->getType());
313 unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
314 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
315 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
317 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
319 unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
320 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
321 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
323 .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
327 unsigned AArch64FastISel::AArch64MaterializeGV(const GlobalValue *GV) {
328 // We can't handle thread-local variables quickly yet.
329 if (GV->isThreadLocal())
332 // MachO still uses GOT for large code-model accesses, but ELF requires
333 // movz/movk sequences, which FastISel doesn't handle yet.
334 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
337 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
339 EVT DestEVT = TLI.getValueType(GV->getType(), true);
340 if (!DestEVT.isSimple())
343 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
346 if (OpFlags & AArch64II::MO_GOT) {
348 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
350 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
352 ResultReg = createResultReg(&AArch64::GPR64RegClass);
353 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
356 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
360 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
362 .addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
364 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
365 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
368 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
374 unsigned AArch64FastISel::TargetMaterializeConstant(const Constant *C) {
375 EVT CEVT = TLI.getValueType(C->getType(), true);
377 // Only handle simple types.
378 if (!CEVT.isSimple())
380 MVT VT = CEVT.getSimpleVT();
382 if (const auto *CI = dyn_cast<ConstantInt>(C))
383 return AArch64MaterializeInt(CI, VT);
384 else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
385 return AArch64MaterializeFP(CFP, VT);
386 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
387 return AArch64MaterializeGV(GV);
392 // Computes the address to get to an object.
393 bool AArch64FastISel::ComputeAddress(const Value *Obj, Address &Addr, Type *Ty)
395 const User *U = nullptr;
396 unsigned Opcode = Instruction::UserOp1;
397 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
398 // Don't walk into other basic blocks unless the object is an alloca from
399 // another block, otherwise it may not have a virtual register assigned.
400 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
401 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
402 Opcode = I->getOpcode();
405 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
406 Opcode = C->getOpcode();
410 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
411 if (Ty->getAddressSpace() > 255)
412 // Fast instruction selection doesn't support the special
419 case Instruction::BitCast: {
420 // Look through bitcasts.
421 return ComputeAddress(U->getOperand(0), Addr, Ty);
423 case Instruction::IntToPtr: {
424 // Look past no-op inttoptrs.
425 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
426 return ComputeAddress(U->getOperand(0), Addr, Ty);
429 case Instruction::PtrToInt: {
430 // Look past no-op ptrtoints.
431 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
432 return ComputeAddress(U->getOperand(0), Addr, Ty);
435 case Instruction::GetElementPtr: {
436 Address SavedAddr = Addr;
437 uint64_t TmpOffset = Addr.getOffset();
439 // Iterate through the GEP folding the constants into offsets where
441 gep_type_iterator GTI = gep_type_begin(U);
442 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
444 const Value *Op = *i;
445 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
446 const StructLayout *SL = DL.getStructLayout(STy);
447 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
448 TmpOffset += SL->getElementOffset(Idx);
450 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
452 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
453 // Constant-offset addressing.
454 TmpOffset += CI->getSExtValue() * S;
457 if (canFoldAddIntoGEP(U, Op)) {
458 // A compatible add with a constant operand. Fold the constant.
460 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
461 TmpOffset += CI->getSExtValue() * S;
462 // Iterate on the other operand.
463 Op = cast<AddOperator>(Op)->getOperand(0);
467 goto unsupported_gep;
472 // Try to grab the base operand now.
473 Addr.setOffset(TmpOffset);
474 if (ComputeAddress(U->getOperand(0), Addr, Ty))
477 // We failed, restore everything and try the other options.
483 case Instruction::Alloca: {
484 const AllocaInst *AI = cast<AllocaInst>(Obj);
485 DenseMap<const AllocaInst *, int>::iterator SI =
486 FuncInfo.StaticAllocaMap.find(AI);
487 if (SI != FuncInfo.StaticAllocaMap.end()) {
488 Addr.setKind(Address::FrameIndexBase);
489 Addr.setFI(SI->second);
494 case Instruction::Add: {
495 // Adds of constants are common and easy enough.
496 const Value *LHS = U->getOperand(0);
497 const Value *RHS = U->getOperand(1);
499 if (isa<ConstantInt>(LHS))
502 if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
503 Addr.setOffset(Addr.getOffset() + (uint64_t)CI->getSExtValue());
504 return ComputeAddress(LHS, Addr, Ty);
507 Address Backup = Addr;
508 if (ComputeAddress(LHS, Addr, Ty) && ComputeAddress(RHS, Addr, Ty))
514 case Instruction::Shl:
515 if (Addr.getOffsetReg())
518 if (const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1))) {
519 unsigned Val = CI->getZExtValue();
520 if (Val < 1 || Val > 3)
523 uint64_t NumBytes = 0;
524 if (Ty && Ty->isSized()) {
525 uint64_t NumBits = DL.getTypeSizeInBits(Ty);
526 NumBytes = NumBits / 8;
527 if (!isPowerOf2_64(NumBits))
531 if (NumBytes != (1ULL << Val))
535 Addr.setExtendType(AArch64_AM::LSL);
537 if (const auto *I = dyn_cast<Instruction>(U->getOperand(0)))
538 if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB)
541 if (const auto *ZE = dyn_cast<ZExtInst>(U))
542 if (ZE->getOperand(0)->getType()->isIntegerTy(32))
543 Addr.setExtendType(AArch64_AM::UXTW);
545 if (const auto *SE = dyn_cast<SExtInst>(U))
546 if (SE->getOperand(0)->getType()->isIntegerTy(32))
547 Addr.setExtendType(AArch64_AM::SXTW);
549 unsigned Reg = getRegForValue(U->getOperand(0));
552 Addr.setOffsetReg(Reg);
559 if (!Addr.getOffsetReg()) {
560 unsigned Reg = getRegForValue(Obj);
563 Addr.setOffsetReg(Reg);
569 unsigned Reg = getRegForValue(Obj);
576 bool AArch64FastISel::ComputeCallAddress(const Value *V, Address &Addr) {
577 const User *U = nullptr;
578 unsigned Opcode = Instruction::UserOp1;
581 if (const auto *I = dyn_cast<Instruction>(V)) {
582 Opcode = I->getOpcode();
584 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
585 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
586 Opcode = C->getOpcode();
592 case Instruction::BitCast:
593 // Look past bitcasts if its operand is in the same BB.
595 return ComputeCallAddress(U->getOperand(0), Addr);
597 case Instruction::IntToPtr:
598 // Look past no-op inttoptrs if its operand is in the same BB.
600 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
601 return ComputeCallAddress(U->getOperand(0), Addr);
603 case Instruction::PtrToInt:
604 // Look past no-op ptrtoints if its operand is in the same BB.
606 TLI.getValueType(U->getType()) == TLI.getPointerTy())
607 return ComputeCallAddress(U->getOperand(0), Addr);
611 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
612 Addr.setGlobalValue(GV);
616 // If all else fails, try to materialize the value in a register.
617 if (!Addr.getGlobalValue()) {
618 Addr.setReg(getRegForValue(V));
619 return Addr.getReg() != 0;
626 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
627 EVT evt = TLI.getValueType(Ty, true);
629 // Only handle simple types.
630 if (evt == MVT::Other || !evt.isSimple())
632 VT = evt.getSimpleVT();
634 // This is a legal type, but it's not something we handle in fast-isel.
638 // Handle all other legal types, i.e. a register that will directly hold this
640 return TLI.isTypeLegal(VT);
643 bool AArch64FastISel::isLoadStoreTypeLegal(Type *Ty, MVT &VT) {
644 if (isTypeLegal(Ty, VT))
647 // If this is a type than can be sign or zero-extended to a basic operation
648 // go ahead and accept it now. For stores, this reflects truncation.
649 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
655 bool AArch64FastISel::SimplifyAddress(Address &Addr, MVT VT) {
656 unsigned ScaleFactor;
657 switch (VT.SimpleTy) {
658 default: return false;
659 case MVT::i1: // fall-through
660 case MVT::i8: ScaleFactor = 1; break;
661 case MVT::i16: ScaleFactor = 2; break;
662 case MVT::i32: // fall-through
663 case MVT::f32: ScaleFactor = 4; break;
664 case MVT::i64: // fall-through
665 case MVT::f64: ScaleFactor = 8; break;
668 bool ImmediateOffsetNeedsLowering = false;
669 bool RegisterOffsetNeedsLowering = false;
670 int64_t Offset = Addr.getOffset();
671 if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
672 ImmediateOffsetNeedsLowering = true;
673 else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
674 !isUInt<12>(Offset / ScaleFactor))
675 ImmediateOffsetNeedsLowering = true;
677 // Cannot encode an offset register and an immediate offset in the same
678 // instruction. Fold the immediate offset into the load/store instruction and
679 // emit an additonal add to take care of the offset register.
680 if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.isRegBase() &&
682 RegisterOffsetNeedsLowering = true;
684 // If this is a stack pointer and the offset needs to be simplified then put
685 // the alloca address into a register, set the base type back to register and
686 // continue. This should almost never happen.
687 if (ImmediateOffsetNeedsLowering && Addr.isFIBase()) {
688 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
689 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
691 .addFrameIndex(Addr.getFI())
694 Addr.setKind(Address::RegBase);
695 Addr.setReg(ResultReg);
698 if (RegisterOffsetNeedsLowering) {
699 unsigned ResultReg = 0;
701 ResultReg = createResultReg(&AArch64::GPR64RegClass);
702 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
703 TII.get(AArch64::ADDXrs), ResultReg)
704 .addReg(Addr.getReg())
705 .addReg(Addr.getOffsetReg())
706 .addImm(Addr.getShift());
708 ResultReg = Emit_LSL_ri(MVT::i64, Addr.getOffsetReg(),
709 /*Op0IsKill=*/false, Addr.getShift());
713 Addr.setReg(ResultReg);
714 Addr.setOffsetReg(0);
718 // Since the offset is too large for the load/store instruction get the
719 // reg+offset into a register.
720 if (ImmediateOffsetNeedsLowering) {
721 unsigned ResultReg = 0;
723 ResultReg = FastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(),
724 /*IsKill=*/false, Offset, MVT::i64);
726 ResultReg = FastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
730 Addr.setReg(ResultReg);
736 void AArch64FastISel::AddLoadStoreOperands(Address &Addr,
737 const MachineInstrBuilder &MIB,
739 unsigned ScaleFactor,
740 MachineMemOperand *MMO) {
741 int64_t Offset = Addr.getOffset() / ScaleFactor;
742 // Frame base works a bit differently. Handle it separately.
743 if (Addr.isFIBase()) {
744 int FI = Addr.getFI();
745 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
746 // and alignment should be based on the VT.
747 MMO = FuncInfo.MF->getMachineMemOperand(
748 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
749 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
750 // Now add the rest of the operands.
751 MIB.addFrameIndex(FI).addImm(Offset);
753 assert(Addr.isRegBase() && "Unexpected address kind.");
754 if (Addr.getOffsetReg()) {
755 assert(Addr.getOffset() == 0 && "Unexpected offset");
756 bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
757 Addr.getExtendType() == AArch64_AM::SXTX;
758 MIB.addReg(Addr.getReg());
759 MIB.addReg(Addr.getOffsetReg());
760 MIB.addImm(IsSigned);
761 MIB.addImm(Addr.getShift() != 0);
763 MIB.addReg(Addr.getReg());
769 MIB.addMemOperand(MMO);
772 unsigned AArch64FastISel::emitAddsSubs(bool UseAdds, MVT RetVT,
773 const Value *LHS, const Value *RHS,
774 bool IsZExt, bool WantResult) {
775 AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
776 bool NeedExtend = false;
777 switch (RetVT.SimpleTy) {
785 ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
789 ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
791 case MVT::i32: // fall-through
796 RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
798 // Canonicalize immediates to the RHS first.
799 if (UseAdds && isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
802 // Canonicalize shift immediate to the RHS.
804 if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
805 if (isa<ConstantInt>(SI->getOperand(1)))
806 if (SI->getOpcode() == Instruction::Shl ||
807 SI->getOpcode() == Instruction::LShr ||
808 SI->getOpcode() == Instruction::AShr )
811 unsigned LHSReg = getRegForValue(LHS);
814 bool LHSIsKill = hasTrivialKill(LHS);
817 LHSReg = EmitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
819 unsigned ResultReg = 0;
820 if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
821 uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
824 emitAddsSubs_ri(!UseAdds, RetVT, LHSReg, LHSIsKill, -Imm, WantResult);
827 emitAddsSubs_ri(UseAdds, RetVT, LHSReg, LHSIsKill, Imm, WantResult);
832 // Only extend the RHS within the instruction if there is a valid extend type.
833 if (ExtendType != AArch64_AM::InvalidShiftExtend) {
834 if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
835 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
836 if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
837 unsigned RHSReg = getRegForValue(SI->getOperand(0));
840 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
841 return emitAddsSubs_rx(UseAdds, RetVT, LHSReg, LHSIsKill, RHSReg,
842 RHSIsKill, ExtendType, C->getZExtValue(),
845 unsigned RHSReg = getRegForValue(RHS);
848 bool RHSIsKill = hasTrivialKill(RHS);
849 return emitAddsSubs_rx(UseAdds, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
850 ExtendType, 0, WantResult);
853 // Check if the shift can be folded into the instruction.
854 if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
855 if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
856 AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
857 switch (SI->getOpcode()) {
859 case Instruction::Shl: ShiftType = AArch64_AM::LSL; break;
860 case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
861 case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
863 uint64_t ShiftVal = C->getZExtValue();
864 if (ShiftType != AArch64_AM::InvalidShiftExtend) {
865 unsigned RHSReg = getRegForValue(SI->getOperand(0));
868 bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
869 return emitAddsSubs_rs(UseAdds, RetVT, LHSReg, LHSIsKill, RHSReg,
870 RHSIsKill, ShiftType, ShiftVal, WantResult);
875 unsigned RHSReg = getRegForValue(RHS);
878 bool RHSIsKill = hasTrivialKill(RHS);
881 RHSReg = EmitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
883 return emitAddsSubs_rr(UseAdds, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
887 unsigned AArch64FastISel::emitAddsSubs_rr(bool UseAdds, MVT RetVT,
888 unsigned LHSReg, bool LHSIsKill,
889 unsigned RHSReg, bool RHSIsKill,
891 assert(LHSReg && RHSReg && "Invalid register number.");
893 if (RetVT != MVT::i32 && RetVT != MVT::i64)
896 static const unsigned OpcTable[2][2] = {
897 { AArch64::ADDSWrr, AArch64::ADDSXrr },
898 { AArch64::SUBSWrr, AArch64::SUBSXrr }
900 unsigned Opc = OpcTable[!UseAdds][(RetVT == MVT::i64)];
903 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
905 ResultReg = (RetVT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
907 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
908 .addReg(LHSReg, getKillRegState(LHSIsKill))
909 .addReg(RHSReg, getKillRegState(RHSIsKill));
914 unsigned AArch64FastISel::emitAddsSubs_ri(bool UseAdds, MVT RetVT,
915 unsigned LHSReg, bool LHSIsKill,
916 uint64_t Imm, bool WantResult) {
917 assert(LHSReg && "Invalid register number.");
919 if (RetVT != MVT::i32 && RetVT != MVT::i64)
925 else if ((Imm & 0xfff000) == Imm) {
931 static const unsigned OpcTable[2][2] = {
932 { AArch64::ADDSWri, AArch64::ADDSXri },
933 { AArch64::SUBSWri, AArch64::SUBSXri }
935 unsigned Opc = OpcTable[!UseAdds][(RetVT == MVT::i64)];
938 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
940 ResultReg = (RetVT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
942 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
943 .addReg(LHSReg, getKillRegState(LHSIsKill))
945 .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
950 unsigned AArch64FastISel::emitAddsSubs_rs(bool UseAdds, MVT RetVT,
951 unsigned LHSReg, bool LHSIsKill,
952 unsigned RHSReg, bool RHSIsKill,
953 AArch64_AM::ShiftExtendType ShiftType,
954 uint64_t ShiftImm, bool WantResult) {
955 assert(LHSReg && RHSReg && "Invalid register number.");
957 if (RetVT != MVT::i32 && RetVT != MVT::i64)
960 static const unsigned OpcTable[2][2] = {
961 { AArch64::ADDSWrs, AArch64::ADDSXrs },
962 { AArch64::SUBSWrs, AArch64::SUBSXrs }
964 unsigned Opc = OpcTable[!UseAdds][(RetVT == MVT::i64)];
967 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
969 ResultReg = (RetVT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
971 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
972 .addReg(LHSReg, getKillRegState(LHSIsKill))
973 .addReg(RHSReg, getKillRegState(RHSIsKill))
974 .addImm(getShifterImm(ShiftType, ShiftImm));
979 unsigned AArch64FastISel::emitAddsSubs_rx(bool UseAdds, MVT RetVT,
980 unsigned LHSReg, bool LHSIsKill,
981 unsigned RHSReg, bool RHSIsKill,
982 AArch64_AM::ShiftExtendType ExtType,
983 uint64_t ShiftImm, bool WantResult) {
984 assert(LHSReg && RHSReg && "Invalid register number.");
986 if (RetVT != MVT::i32 && RetVT != MVT::i64)
989 static const unsigned OpcTable[2][2] = {
990 { AArch64::ADDSWrx, AArch64::ADDSXrx },
991 { AArch64::SUBSWrx, AArch64::SUBSXrx }
993 unsigned Opc = OpcTable[!UseAdds][(RetVT == MVT::i64)];
996 ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
998 ResultReg = (RetVT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
1000 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1001 .addReg(LHSReg, getKillRegState(LHSIsKill))
1002 .addReg(RHSReg, getKillRegState(RHSIsKill))
1003 .addImm(getArithExtendImm(ExtType, ShiftImm));
1008 bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1009 Type *Ty = LHS->getType();
1010 EVT EVT = TLI.getValueType(Ty, true);
1011 if (!EVT.isSimple())
1013 MVT VT = EVT.getSimpleVT();
1015 switch (VT.SimpleTy) {
1023 return emitICmp(VT, LHS, RHS, IsZExt);
1026 return emitFCmp(VT, LHS, RHS);
1030 bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1032 return emitSubs(RetVT, LHS, RHS, IsZExt, /*WantResult=*/false) != 0;
1035 bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1037 return emitAddsSubs_ri(false, RetVT, LHSReg, LHSIsKill, Imm,
1038 /*WantResult=*/false) != 0;
1041 bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1042 if (RetVT != MVT::f32 && RetVT != MVT::f64)
1045 // Check to see if the 2nd operand is a constant that we can encode directly
1047 bool UseImm = false;
1048 if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1049 if (CFP->isZero() && !CFP->isNegative())
1052 unsigned LHSReg = getRegForValue(LHS);
1055 bool LHSIsKill = hasTrivialKill(LHS);
1058 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1059 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1060 .addReg(LHSReg, getKillRegState(LHSIsKill));
1064 unsigned RHSReg = getRegForValue(RHS);
1067 bool RHSIsKill = hasTrivialKill(RHS);
1069 unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1070 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1071 .addReg(LHSReg, getKillRegState(LHSIsKill))
1072 .addReg(RHSReg, getKillRegState(RHSIsKill));
1076 unsigned AArch64FastISel::emitAdds(MVT RetVT, const Value *LHS,
1077 const Value *RHS, bool IsZExt,
1079 return emitAddsSubs(true, RetVT, LHS, RHS, IsZExt, WantResult);
1082 unsigned AArch64FastISel::emitSubs(MVT RetVT, const Value *LHS,
1083 const Value *RHS, bool IsZExt,
1085 return emitAddsSubs(false, RetVT, LHS, RHS, IsZExt, WantResult);
1088 unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1089 bool LHSIsKill, unsigned RHSReg,
1090 bool RHSIsKill, bool WantResult) {
1091 return emitAddsSubs_rr(false, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1095 unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1096 bool LHSIsKill, unsigned RHSReg,
1098 AArch64_AM::ShiftExtendType ShiftType,
1099 uint64_t ShiftImm, bool WantResult) {
1100 return emitAddsSubs_rs(false, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1101 ShiftType, ShiftImm, WantResult);
1104 bool AArch64FastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
1105 MachineMemOperand *MMO) {
1106 // Simplify this down to something we can handle.
1107 if (!SimplifyAddress(Addr, VT))
1110 unsigned ScaleFactor;
1111 switch (VT.SimpleTy) {
1112 default: llvm_unreachable("Unexpected value type.");
1113 case MVT::i1: // fall-through
1114 case MVT::i8: ScaleFactor = 1; break;
1115 case MVT::i16: ScaleFactor = 2; break;
1116 case MVT::i32: // fall-through
1117 case MVT::f32: ScaleFactor = 4; break;
1118 case MVT::i64: // fall-through
1119 case MVT::f64: ScaleFactor = 8; break;
1122 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1123 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1124 bool UseScaled = true;
1125 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1130 static const unsigned OpcTable[4][6] = {
1131 { AArch64::LDURBBi, AArch64::LDURHHi, AArch64::LDURWi, AArch64::LDURXi,
1132 AArch64::LDURSi, AArch64::LDURDi },
1133 { AArch64::LDRBBui, AArch64::LDRHHui, AArch64::LDRWui, AArch64::LDRXui,
1134 AArch64::LDRSui, AArch64::LDRDui },
1135 { AArch64::LDRBBroX, AArch64::LDRHHroX, AArch64::LDRWroX, AArch64::LDRXroX,
1136 AArch64::LDRSroX, AArch64::LDRDroX },
1137 { AArch64::LDRBBroW, AArch64::LDRHHroW, AArch64::LDRWroW, AArch64::LDRXroW,
1138 AArch64::LDRSroW, AArch64::LDRDroW }
1142 const TargetRegisterClass *RC;
1143 bool VTIsi1 = false;
1144 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1145 Addr.getOffsetReg();
1146 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1147 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1148 Addr.getExtendType() == AArch64_AM::SXTW)
1151 switch (VT.SimpleTy) {
1152 default: llvm_unreachable("Unexpected value type.");
1153 case MVT::i1: VTIsi1 = true; // Intentional fall-through.
1154 case MVT::i8: Opc = OpcTable[Idx][0]; RC = &AArch64::GPR32RegClass; break;
1155 case MVT::i16: Opc = OpcTable[Idx][1]; RC = &AArch64::GPR32RegClass; break;
1156 case MVT::i32: Opc = OpcTable[Idx][2]; RC = &AArch64::GPR32RegClass; break;
1157 case MVT::i64: Opc = OpcTable[Idx][3]; RC = &AArch64::GPR64RegClass; break;
1158 case MVT::f32: Opc = OpcTable[Idx][4]; RC = &AArch64::FPR32RegClass; break;
1159 case MVT::f64: Opc = OpcTable[Idx][5]; RC = &AArch64::FPR64RegClass; break;
1162 // Create the base instruction, then add the operands.
1163 ResultReg = createResultReg(RC);
1164 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1165 TII.get(Opc), ResultReg);
1166 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
1168 // Loading an i1 requires special handling.
1170 MRI.constrainRegClass(ResultReg, &AArch64::GPR32RegClass);
1171 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1172 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1175 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1181 bool AArch64FastISel::SelectLoad(const Instruction *I) {
1183 // Verify we have a legal type before going any further. Currently, we handle
1184 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1185 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1186 if (!isLoadStoreTypeLegal(I->getType(), VT) || cast<LoadInst>(I)->isAtomic())
1189 // See if we can handle this address.
1191 if (!ComputeAddress(I->getOperand(0), Addr, I->getType()))
1195 if (!EmitLoad(VT, ResultReg, Addr, createMachineMemOperandFor(I)))
1198 UpdateValueMap(I, ResultReg);
1202 bool AArch64FastISel::EmitStore(MVT VT, unsigned SrcReg, Address Addr,
1203 MachineMemOperand *MMO) {
1204 // Simplify this down to something we can handle.
1205 if (!SimplifyAddress(Addr, VT))
1208 unsigned ScaleFactor;
1209 switch (VT.SimpleTy) {
1210 default: llvm_unreachable("Unexpected value type.");
1211 case MVT::i1: // fall-through
1212 case MVT::i8: ScaleFactor = 1; break;
1213 case MVT::i16: ScaleFactor = 2; break;
1214 case MVT::i32: // fall-through
1215 case MVT::f32: ScaleFactor = 4; break;
1216 case MVT::i64: // fall-through
1217 case MVT::f64: ScaleFactor = 8; break;
1220 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1221 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1222 bool UseScaled = true;
1223 if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1229 static const unsigned OpcTable[4][6] = {
1230 { AArch64::STURBBi, AArch64::STURHHi, AArch64::STURWi, AArch64::STURXi,
1231 AArch64::STURSi, AArch64::STURDi },
1232 { AArch64::STRBBui, AArch64::STRHHui, AArch64::STRWui, AArch64::STRXui,
1233 AArch64::STRSui, AArch64::STRDui },
1234 { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
1235 AArch64::STRSroX, AArch64::STRDroX },
1236 { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
1237 AArch64::STRSroW, AArch64::STRDroW }
1242 bool VTIsi1 = false;
1243 bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1244 Addr.getOffsetReg();
1245 unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1246 if (Addr.getExtendType() == AArch64_AM::UXTW ||
1247 Addr.getExtendType() == AArch64_AM::SXTW)
1250 switch (VT.SimpleTy) {
1251 default: llvm_unreachable("Unexpected value type.");
1252 case MVT::i1: VTIsi1 = true;
1253 case MVT::i8: Opc = OpcTable[Idx][0]; break;
1254 case MVT::i16: Opc = OpcTable[Idx][1]; break;
1255 case MVT::i32: Opc = OpcTable[Idx][2]; break;
1256 case MVT::i64: Opc = OpcTable[Idx][3]; break;
1257 case MVT::f32: Opc = OpcTable[Idx][4]; break;
1258 case MVT::f64: Opc = OpcTable[Idx][5]; break;
1261 // Storing an i1 requires special handling.
1263 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
1264 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1265 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1268 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1271 // Create the base instruction, then add the operands.
1272 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1275 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
1280 bool AArch64FastISel::SelectStore(const Instruction *I) {
1282 Value *Op0 = I->getOperand(0);
1283 // Verify we have a legal type before going any further. Currently, we handle
1284 // simple types that will directly fit in a register (i32/f32/i64/f64) or
1285 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1286 if (!isLoadStoreTypeLegal(Op0->getType(), VT) ||
1287 cast<StoreInst>(I)->isAtomic())
1290 // Get the value to be stored into a register.
1291 unsigned SrcReg = getRegForValue(Op0);
1295 // See if we can handle this address.
1297 if (!ComputeAddress(I->getOperand(1), Addr, I->getOperand(0)->getType()))
1300 if (!EmitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
1305 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
1307 case CmpInst::FCMP_ONE:
1308 case CmpInst::FCMP_UEQ:
1310 // AL is our "false" for now. The other two need more compares.
1311 return AArch64CC::AL;
1312 case CmpInst::ICMP_EQ:
1313 case CmpInst::FCMP_OEQ:
1314 return AArch64CC::EQ;
1315 case CmpInst::ICMP_SGT:
1316 case CmpInst::FCMP_OGT:
1317 return AArch64CC::GT;
1318 case CmpInst::ICMP_SGE:
1319 case CmpInst::FCMP_OGE:
1320 return AArch64CC::GE;
1321 case CmpInst::ICMP_UGT:
1322 case CmpInst::FCMP_UGT:
1323 return AArch64CC::HI;
1324 case CmpInst::FCMP_OLT:
1325 return AArch64CC::MI;
1326 case CmpInst::ICMP_ULE:
1327 case CmpInst::FCMP_OLE:
1328 return AArch64CC::LS;
1329 case CmpInst::FCMP_ORD:
1330 return AArch64CC::VC;
1331 case CmpInst::FCMP_UNO:
1332 return AArch64CC::VS;
1333 case CmpInst::FCMP_UGE:
1334 return AArch64CC::PL;
1335 case CmpInst::ICMP_SLT:
1336 case CmpInst::FCMP_ULT:
1337 return AArch64CC::LT;
1338 case CmpInst::ICMP_SLE:
1339 case CmpInst::FCMP_ULE:
1340 return AArch64CC::LE;
1341 case CmpInst::FCMP_UNE:
1342 case CmpInst::ICMP_NE:
1343 return AArch64CC::NE;
1344 case CmpInst::ICMP_UGE:
1345 return AArch64CC::HS;
1346 case CmpInst::ICMP_ULT:
1347 return AArch64CC::LO;
1351 bool AArch64FastISel::SelectBranch(const Instruction *I) {
1352 const BranchInst *BI = cast<BranchInst>(I);
1353 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
1354 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
1356 AArch64CC::CondCode CC = AArch64CC::NE;
1357 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
1358 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
1359 // We may not handle every CC for now.
1360 CC = getCompareCC(CI->getPredicate());
1361 if (CC == AArch64CC::AL)
1365 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1369 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1373 // Obtain the branch weight and add the TrueBB to the successor list.
1374 uint32_t BranchWeight = 0;
1376 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1377 TBB->getBasicBlock());
1378 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1380 FastEmitBranch(FBB, DbgLoc);
1383 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
1385 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
1386 (isLoadStoreTypeLegal(TI->getOperand(0)->getType(), SrcVT))) {
1387 unsigned CondReg = getRegForValue(TI->getOperand(0));
1391 // Issue an extract_subreg to get the lower 32-bits.
1392 if (SrcVT == MVT::i64)
1393 CondReg = FastEmitInst_extractsubreg(MVT::i32, CondReg, /*Kill=*/true,
1396 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
1397 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1398 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1399 TII.get(AArch64::ANDWri), ANDReg)
1401 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1402 emitICmp_ri(MVT::i32, ANDReg, /*IsKill=*/true, 0);
1404 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1405 std::swap(TBB, FBB);
1408 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1412 // Obtain the branch weight and add the TrueBB to the successor list.
1413 uint32_t BranchWeight = 0;
1415 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1416 TBB->getBasicBlock());
1417 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1419 FastEmitBranch(FBB, DbgLoc);
1422 } else if (const ConstantInt *CI =
1423 dyn_cast<ConstantInt>(BI->getCondition())) {
1424 uint64_t Imm = CI->getZExtValue();
1425 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
1426 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
1429 // Obtain the branch weight and add the target to the successor list.
1430 uint32_t BranchWeight = 0;
1432 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1433 Target->getBasicBlock());
1434 FuncInfo.MBB->addSuccessor(Target, BranchWeight);
1436 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
1437 // Fake request the condition, otherwise the intrinsic might be completely
1439 unsigned CondReg = getRegForValue(BI->getCondition());
1444 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1448 // Obtain the branch weight and add the TrueBB to the successor list.
1449 uint32_t BranchWeight = 0;
1451 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1452 TBB->getBasicBlock());
1453 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1455 FastEmitBranch(FBB, DbgLoc);
1459 unsigned CondReg = getRegForValue(BI->getCondition());
1462 bool CondRegIsKill = hasTrivialKill(BI->getCondition());
1464 // We've been divorced from our compare! Our block was split, and
1465 // now our compare lives in a predecessor block. We musn't
1466 // re-compare here, as the children of the compare aren't guaranteed
1467 // live across the block boundary (we *could* check for this).
1468 // Regardless, the compare has been done in the predecessor block,
1469 // and it left a value for us in a virtual register. Ergo, we test
1470 // the one-bit value left in the virtual register.
1471 emitICmp_ri(MVT::i32, CondReg, CondRegIsKill, 0);
1473 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
1474 std::swap(TBB, FBB);
1478 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
1482 // Obtain the branch weight and add the TrueBB to the successor list.
1483 uint32_t BranchWeight = 0;
1485 BranchWeight = FuncInfo.BPI->getEdgeWeight(BI->getParent(),
1486 TBB->getBasicBlock());
1487 FuncInfo.MBB->addSuccessor(TBB, BranchWeight);
1489 FastEmitBranch(FBB, DbgLoc);
1493 bool AArch64FastISel::SelectIndirectBr(const Instruction *I) {
1494 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
1495 unsigned AddrReg = getRegForValue(BI->getOperand(0));
1499 // Emit the indirect branch.
1500 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BR))
1503 // Make sure the CFG is up-to-date.
1504 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
1505 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
1510 bool AArch64FastISel::SelectCmp(const Instruction *I) {
1511 const CmpInst *CI = cast<CmpInst>(I);
1513 // We may not handle every CC for now.
1514 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate());
1515 if (CC == AArch64CC::AL)
1519 if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1522 // Now set a register based on the comparison.
1523 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
1524 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1525 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1527 .addReg(AArch64::WZR)
1528 .addReg(AArch64::WZR)
1529 .addImm(invertedCC);
1531 UpdateValueMap(I, ResultReg);
1535 bool AArch64FastISel::SelectSelect(const Instruction *I) {
1536 const SelectInst *SI = cast<SelectInst>(I);
1538 EVT DestEVT = TLI.getValueType(SI->getType(), true);
1539 if (!DestEVT.isSimple())
1542 MVT DestVT = DestEVT.getSimpleVT();
1543 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
1548 switch (DestVT.SimpleTy) {
1549 default: return false;
1550 case MVT::i32: SelectOpc = AArch64::CSELWr; break;
1551 case MVT::i64: SelectOpc = AArch64::CSELXr; break;
1552 case MVT::f32: SelectOpc = AArch64::FCSELSrrr; break;
1553 case MVT::f64: SelectOpc = AArch64::FCSELDrrr; break;
1556 const Value *Cond = SI->getCondition();
1557 bool NeedTest = true;
1558 AArch64CC::CondCode CC = AArch64CC::NE;
1559 if (foldXALUIntrinsic(CC, I, Cond))
1562 unsigned CondReg = getRegForValue(Cond);
1565 bool CondIsKill = hasTrivialKill(Cond);
1568 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
1569 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1570 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1572 .addReg(CondReg, getKillRegState(CondIsKill))
1573 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1574 emitICmp_ri(MVT::i32, ANDReg, true, 0);
1577 unsigned TrueReg = getRegForValue(SI->getTrueValue());
1578 bool TrueIsKill = hasTrivialKill(SI->getTrueValue());
1580 unsigned FalseReg = getRegForValue(SI->getFalseValue());
1581 bool FalseIsKill = hasTrivialKill(SI->getFalseValue());
1583 if (!TrueReg || !FalseReg)
1586 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1587 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SelectOpc),
1589 .addReg(TrueReg, getKillRegState(TrueIsKill))
1590 .addReg(FalseReg, getKillRegState(FalseIsKill))
1593 UpdateValueMap(I, ResultReg);
1597 bool AArch64FastISel::SelectFPExt(const Instruction *I) {
1598 Value *V = I->getOperand(0);
1599 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
1602 unsigned Op = getRegForValue(V);
1606 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
1607 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
1608 ResultReg).addReg(Op);
1609 UpdateValueMap(I, ResultReg);
1613 bool AArch64FastISel::SelectFPTrunc(const Instruction *I) {
1614 Value *V = I->getOperand(0);
1615 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
1618 unsigned Op = getRegForValue(V);
1622 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
1623 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
1624 ResultReg).addReg(Op);
1625 UpdateValueMap(I, ResultReg);
1629 // FPToUI and FPToSI
1630 bool AArch64FastISel::SelectFPToInt(const Instruction *I, bool Signed) {
1632 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1635 unsigned SrcReg = getRegForValue(I->getOperand(0));
1639 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1640 if (SrcVT == MVT::f128)
1644 if (SrcVT == MVT::f64) {
1646 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
1648 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
1651 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
1653 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
1655 unsigned ResultReg = createResultReg(
1656 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
1657 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1659 UpdateValueMap(I, ResultReg);
1663 bool AArch64FastISel::SelectIntToFP(const Instruction *I, bool Signed) {
1665 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1667 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
1668 "Unexpected value type.");
1670 unsigned SrcReg = getRegForValue(I->getOperand(0));
1674 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1676 // Handle sign-extension.
1677 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
1679 EmitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
1684 MRI.constrainRegClass(SrcReg, SrcVT == MVT::i64 ? &AArch64::GPR64RegClass
1685 : &AArch64::GPR32RegClass);
1688 if (SrcVT == MVT::i64) {
1690 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
1692 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
1695 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
1697 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
1700 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1701 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1703 UpdateValueMap(I, ResultReg);
1707 bool AArch64FastISel::FastLowerArguments() {
1708 if (!FuncInfo.CanLowerReturn)
1711 const Function *F = FuncInfo.Fn;
1715 CallingConv::ID CC = F->getCallingConv();
1716 if (CC != CallingConv::C)
1719 // Only handle simple cases like i1/i8/i16/i32/i64/f32/f64 of up to 8 GPR and
1721 unsigned GPRCnt = 0;
1722 unsigned FPRCnt = 0;
1724 for (auto const &Arg : F->args()) {
1725 // The first argument is at index 1.
1727 if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
1728 F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
1729 F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
1730 F->getAttributes().hasAttribute(Idx, Attribute::Nest))
1733 Type *ArgTy = Arg.getType();
1734 if (ArgTy->isStructTy() || ArgTy->isArrayTy() || ArgTy->isVectorTy())
1737 EVT ArgVT = TLI.getValueType(ArgTy);
1738 if (!ArgVT.isSimple()) return false;
1739 switch (ArgVT.getSimpleVT().SimpleTy) {
1740 default: return false;
1755 if (GPRCnt > 8 || FPRCnt > 8)
1759 static const MCPhysReg Registers[5][8] = {
1760 { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
1761 AArch64::W5, AArch64::W6, AArch64::W7 },
1762 { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
1763 AArch64::X5, AArch64::X6, AArch64::X7 },
1764 { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
1765 AArch64::H5, AArch64::H6, AArch64::H7 },
1766 { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
1767 AArch64::S5, AArch64::S6, AArch64::S7 },
1768 { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
1769 AArch64::D5, AArch64::D6, AArch64::D7 }
1772 unsigned GPRIdx = 0;
1773 unsigned FPRIdx = 0;
1774 for (auto const &Arg : F->args()) {
1775 MVT VT = TLI.getSimpleValueType(Arg.getType());
1777 switch (VT.SimpleTy) {
1778 default: llvm_unreachable("Unexpected value type.");
1781 case MVT::i16: VT = MVT::i32; // fall-through
1782 case MVT::i32: SrcReg = Registers[0][GPRIdx++]; break;
1783 case MVT::i64: SrcReg = Registers[1][GPRIdx++]; break;
1784 case MVT::f16: SrcReg = Registers[2][FPRIdx++]; break;
1785 case MVT::f32: SrcReg = Registers[3][FPRIdx++]; break;
1786 case MVT::f64: SrcReg = Registers[4][FPRIdx++]; break;
1789 // Skip unused arguments.
1790 if (Arg.use_empty()) {
1791 UpdateValueMap(&Arg, 0);
1795 const TargetRegisterClass *RC = TLI.getRegClassFor(VT);
1796 unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
1797 // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
1798 // Without this, EmitLiveInCopies may eliminate the livein if its only
1799 // use is a bitcast (which isn't turned into an instruction).
1800 unsigned ResultReg = createResultReg(RC);
1801 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1802 TII.get(TargetOpcode::COPY), ResultReg)
1803 .addReg(DstReg, getKillRegState(true));
1804 UpdateValueMap(&Arg, ResultReg);
1809 bool AArch64FastISel::ProcessCallArgs(CallLoweringInfo &CLI,
1810 SmallVectorImpl<MVT> &OutVTs,
1811 unsigned &NumBytes) {
1812 CallingConv::ID CC = CLI.CallConv;
1813 SmallVector<CCValAssign, 16> ArgLocs;
1814 CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
1815 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1817 // Get a count of how many bytes are to be pushed on the stack.
1818 NumBytes = CCInfo.getNextStackOffset();
1820 // Issue CALLSEQ_START
1821 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1822 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
1825 // Process the args.
1826 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1827 CCValAssign &VA = ArgLocs[i];
1828 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1829 MVT ArgVT = OutVTs[VA.getValNo()];
1831 unsigned ArgReg = getRegForValue(ArgVal);
1835 // Handle arg promotion: SExt, ZExt, AExt.
1836 switch (VA.getLocInfo()) {
1837 case CCValAssign::Full:
1839 case CCValAssign::SExt: {
1840 MVT DestVT = VA.getLocVT();
1842 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1847 case CCValAssign::AExt:
1848 // Intentional fall-through.
1849 case CCValAssign::ZExt: {
1850 MVT DestVT = VA.getLocVT();
1852 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1858 llvm_unreachable("Unknown arg promotion!");
1861 // Now copy/store arg to correct locations.
1862 if (VA.isRegLoc() && !VA.needsCustom()) {
1863 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1864 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1865 CLI.OutRegs.push_back(VA.getLocReg());
1866 } else if (VA.needsCustom()) {
1867 // FIXME: Handle custom args.
1870 assert(VA.isMemLoc() && "Assuming store on stack.");
1872 // Don't emit stores for undef values.
1873 if (isa<UndefValue>(ArgVal))
1876 // Need to store on the stack.
1877 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
1879 unsigned BEAlign = 0;
1880 if (ArgSize < 8 && !Subtarget->isLittleEndian())
1881 BEAlign = 8 - ArgSize;
1884 Addr.setKind(Address::RegBase);
1885 Addr.setReg(AArch64::SP);
1886 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1888 unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
1889 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
1890 MachinePointerInfo::getStack(Addr.getOffset()),
1891 MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
1893 if (!EmitStore(ArgVT, ArgReg, Addr, MMO))
1900 bool AArch64FastISel::FinishCall(CallLoweringInfo &CLI, MVT RetVT,
1901 unsigned NumBytes) {
1902 CallingConv::ID CC = CLI.CallConv;
1904 // Issue CALLSEQ_END
1905 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1906 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
1907 .addImm(NumBytes).addImm(0);
1909 // Now the return value.
1910 if (RetVT != MVT::isVoid) {
1911 SmallVector<CCValAssign, 16> RVLocs;
1912 CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
1913 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
1915 // Only handle a single return value.
1916 if (RVLocs.size() != 1)
1919 // Copy all of the result registers out of their specified physreg.
1920 MVT CopyVT = RVLocs[0].getValVT();
1921 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1922 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1923 TII.get(TargetOpcode::COPY), ResultReg)
1924 .addReg(RVLocs[0].getLocReg());
1925 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1927 CLI.ResultReg = ResultReg;
1928 CLI.NumResultRegs = 1;
1934 bool AArch64FastISel::FastLowerCall(CallLoweringInfo &CLI) {
1935 CallingConv::ID CC = CLI.CallConv;
1936 bool IsTailCall = CLI.IsTailCall;
1937 bool IsVarArg = CLI.IsVarArg;
1938 const Value *Callee = CLI.Callee;
1939 const char *SymName = CLI.SymName;
1941 // Allow SelectionDAG isel to handle tail calls.
1945 CodeModel::Model CM = TM.getCodeModel();
1946 // Only support the small and large code model.
1947 if (CM != CodeModel::Small && CM != CodeModel::Large)
1950 // FIXME: Add large code model support for ELF.
1951 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
1954 // Let SDISel handle vararg functions.
1958 // FIXME: Only handle *simple* calls for now.
1960 if (CLI.RetTy->isVoidTy())
1961 RetVT = MVT::isVoid;
1962 else if (!isTypeLegal(CLI.RetTy, RetVT))
1965 for (auto Flag : CLI.OutFlags)
1966 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1969 // Set up the argument vectors.
1970 SmallVector<MVT, 16> OutVTs;
1971 OutVTs.reserve(CLI.OutVals.size());
1973 for (auto *Val : CLI.OutVals) {
1975 if (!isTypeLegal(Val->getType(), VT) &&
1976 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1979 // We don't handle vector parameters yet.
1980 if (VT.isVector() || VT.getSizeInBits() > 64)
1983 OutVTs.push_back(VT);
1987 if (!ComputeCallAddress(Callee, Addr))
1990 // Handle the arguments now that we've gotten them.
1992 if (!ProcessCallArgs(CLI, OutVTs, NumBytes))
1996 MachineInstrBuilder MIB;
1997 if (CM == CodeModel::Small) {
1998 unsigned CallOpc = Addr.getReg() ? AArch64::BLR : AArch64::BL;
1999 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
2001 MIB.addExternalSymbol(SymName, 0);
2002 else if (Addr.getGlobalValue())
2003 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
2004 else if (Addr.getReg())
2005 MIB.addReg(Addr.getReg());
2009 unsigned CallReg = 0;
2011 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
2012 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
2014 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGE);
2016 CallReg = createResultReg(&AArch64::GPR64RegClass);
2017 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
2020 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
2022 } else if (Addr.getGlobalValue()) {
2023 CallReg = AArch64MaterializeGV(Addr.getGlobalValue());
2024 } else if (Addr.getReg())
2025 CallReg = Addr.getReg();
2030 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2031 TII.get(AArch64::BLR)).addReg(CallReg);
2034 // Add implicit physical register uses to the call.
2035 for (auto Reg : CLI.OutRegs)
2036 MIB.addReg(Reg, RegState::Implicit);
2038 // Add a register mask with the call-preserved registers.
2039 // Proper defs for return values will be added by setPhysRegsDeadExcept().
2040 MIB.addRegMask(TRI.getCallPreservedMask(CC));
2044 // Finish off the call including any return values.
2045 return FinishCall(CLI, RetVT, NumBytes);
2048 bool AArch64FastISel::IsMemCpySmall(uint64_t Len, unsigned Alignment) {
2050 return Len / Alignment <= 4;
2055 bool AArch64FastISel::TryEmitSmallMemCpy(Address Dest, Address Src,
2056 uint64_t Len, unsigned Alignment) {
2057 // Make sure we don't bloat code by inlining very large memcpy's.
2058 if (!IsMemCpySmall(Len, Alignment))
2061 int64_t UnscaledOffset = 0;
2062 Address OrigDest = Dest;
2063 Address OrigSrc = Src;
2067 if (!Alignment || Alignment >= 8) {
2078 // Bound based on alignment.
2079 if (Len >= 4 && Alignment == 4)
2081 else if (Len >= 2 && Alignment == 2)
2090 RV = EmitLoad(VT, ResultReg, Src);
2094 RV = EmitStore(VT, ResultReg, Dest);
2098 int64_t Size = VT.getSizeInBits() / 8;
2100 UnscaledOffset += Size;
2102 // We need to recompute the unscaled offset for each iteration.
2103 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
2104 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
2110 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
2111 /// into the user. The condition code will only be updated on success.
2112 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
2113 const Instruction *I,
2114 const Value *Cond) {
2115 if (!isa<ExtractValueInst>(Cond))
2118 const auto *EV = cast<ExtractValueInst>(Cond);
2119 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
2122 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
2124 const Function *Callee = II->getCalledFunction();
2126 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
2127 if (!isTypeLegal(RetTy, RetVT))
2130 if (RetVT != MVT::i32 && RetVT != MVT::i64)
2133 AArch64CC::CondCode TmpCC;
2134 switch (II->getIntrinsicID()) {
2135 default: return false;
2136 case Intrinsic::sadd_with_overflow:
2137 case Intrinsic::ssub_with_overflow: TmpCC = AArch64CC::VS; break;
2138 case Intrinsic::uadd_with_overflow: TmpCC = AArch64CC::HS; break;
2139 case Intrinsic::usub_with_overflow: TmpCC = AArch64CC::LO; break;
2140 case Intrinsic::smul_with_overflow:
2141 case Intrinsic::umul_with_overflow: TmpCC = AArch64CC::NE; break;
2144 // Check if both instructions are in the same basic block.
2145 if (II->getParent() != I->getParent())
2148 // Make sure nothing is in the way
2149 BasicBlock::const_iterator Start = I;
2150 BasicBlock::const_iterator End = II;
2151 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
2152 // We only expect extractvalue instructions between the intrinsic and the
2153 // instruction to be selected.
2154 if (!isa<ExtractValueInst>(Itr))
2157 // Check that the extractvalue operand comes from the intrinsic.
2158 const auto *EVI = cast<ExtractValueInst>(Itr);
2159 if (EVI->getAggregateOperand() != II)
2167 bool AArch64FastISel::FastLowerIntrinsicCall(const IntrinsicInst *II) {
2168 // FIXME: Handle more intrinsics.
2169 switch (II->getIntrinsicID()) {
2170 default: return false;
2171 case Intrinsic::frameaddress: {
2172 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
2173 MFI->setFrameAddressIsTaken(true);
2175 const AArch64RegisterInfo *RegInfo =
2176 static_cast<const AArch64RegisterInfo *>(
2177 TM.getSubtargetImpl()->getRegisterInfo());
2178 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
2179 unsigned SrcReg = FramePtr;
2181 // Recursively load frame address
2187 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
2189 DestReg = createResultReg(&AArch64::GPR64RegClass);
2190 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2191 TII.get(AArch64::LDRXui), DestReg)
2192 .addReg(SrcReg).addImm(0);
2196 UpdateValueMap(II, SrcReg);
2199 case Intrinsic::memcpy:
2200 case Intrinsic::memmove: {
2201 const auto *MTI = cast<MemTransferInst>(II);
2202 // Don't handle volatile.
2203 if (MTI->isVolatile())
2206 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
2207 // we would emit dead code because we don't currently handle memmoves.
2208 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
2209 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
2210 // Small memcpy's are common enough that we want to do them without a call
2212 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
2213 unsigned Alignment = MTI->getAlignment();
2214 if (IsMemCpySmall(Len, Alignment)) {
2216 if (!ComputeAddress(MTI->getRawDest(), Dest) ||
2217 !ComputeAddress(MTI->getRawSource(), Src))
2219 if (TryEmitSmallMemCpy(Dest, Src, Len, Alignment))
2224 if (!MTI->getLength()->getType()->isIntegerTy(64))
2227 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
2228 // Fast instruction selection doesn't support the special
2232 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
2233 return LowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
2235 case Intrinsic::memset: {
2236 const MemSetInst *MSI = cast<MemSetInst>(II);
2237 // Don't handle volatile.
2238 if (MSI->isVolatile())
2241 if (!MSI->getLength()->getType()->isIntegerTy(64))
2244 if (MSI->getDestAddressSpace() > 255)
2245 // Fast instruction selection doesn't support the special
2249 return LowerCallTo(II, "memset", II->getNumArgOperands() - 2);
2251 case Intrinsic::trap: {
2252 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
2256 case Intrinsic::sqrt: {
2257 Type *RetTy = II->getCalledFunction()->getReturnType();
2260 if (!isTypeLegal(RetTy, VT))
2263 unsigned Op0Reg = getRegForValue(II->getOperand(0));
2266 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
2268 unsigned ResultReg = FastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
2272 UpdateValueMap(II, ResultReg);
2275 case Intrinsic::sadd_with_overflow:
2276 case Intrinsic::uadd_with_overflow:
2277 case Intrinsic::ssub_with_overflow:
2278 case Intrinsic::usub_with_overflow:
2279 case Intrinsic::smul_with_overflow:
2280 case Intrinsic::umul_with_overflow: {
2281 // This implements the basic lowering of the xalu with overflow intrinsics.
2282 const Function *Callee = II->getCalledFunction();
2283 auto *Ty = cast<StructType>(Callee->getReturnType());
2284 Type *RetTy = Ty->getTypeAtIndex(0U);
2285 Type *CondTy = Ty->getTypeAtIndex(1);
2288 if (!isTypeLegal(RetTy, VT))
2291 if (VT != MVT::i32 && VT != MVT::i64)
2294 const Value *LHS = II->getArgOperand(0);
2295 const Value *RHS = II->getArgOperand(1);
2296 // Canonicalize immediate to the RHS.
2297 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
2298 isCommutativeIntrinsic(II))
2299 std::swap(LHS, RHS);
2301 unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
2302 AArch64CC::CondCode CC = AArch64CC::Invalid;
2303 switch (II->getIntrinsicID()) {
2304 default: llvm_unreachable("Unexpected intrinsic!");
2305 case Intrinsic::sadd_with_overflow:
2306 ResultReg1 = emitAdds(VT, LHS, RHS); CC = AArch64CC::VS; break;
2307 case Intrinsic::uadd_with_overflow:
2308 ResultReg1 = emitAdds(VT, LHS, RHS); CC = AArch64CC::HS; break;
2309 case Intrinsic::ssub_with_overflow:
2310 ResultReg1 = emitSubs(VT, LHS, RHS); CC = AArch64CC::VS; break;
2311 case Intrinsic::usub_with_overflow:
2312 ResultReg1 = emitSubs(VT, LHS, RHS); CC = AArch64CC::LO; break;
2313 case Intrinsic::smul_with_overflow: {
2315 unsigned LHSReg = getRegForValue(LHS);
2318 bool LHSIsKill = hasTrivialKill(LHS);
2320 unsigned RHSReg = getRegForValue(RHS);
2323 bool RHSIsKill = hasTrivialKill(RHS);
2325 if (VT == MVT::i32) {
2326 MulReg = Emit_SMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2327 unsigned ShiftReg = Emit_LSR_ri(MVT::i64, MulReg, false, 32);
2328 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
2330 ShiftReg = FastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
2332 emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
2333 AArch64_AM::ASR, 31, /*WantResult=*/false);
2335 assert(VT == MVT::i64 && "Unexpected value type.");
2336 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2337 unsigned SMULHReg = FastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
2339 emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
2340 AArch64_AM::ASR, 63, /*WantResult=*/false);
2344 case Intrinsic::umul_with_overflow: {
2346 unsigned LHSReg = getRegForValue(LHS);
2349 bool LHSIsKill = hasTrivialKill(LHS);
2351 unsigned RHSReg = getRegForValue(RHS);
2354 bool RHSIsKill = hasTrivialKill(RHS);
2356 if (VT == MVT::i32) {
2357 MulReg = Emit_UMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2358 emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
2359 /*IsKill=*/false, AArch64_AM::LSR, 32,
2360 /*WantResult=*/false);
2361 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
2364 assert(VT == MVT::i64 && "Unexpected value type.");
2365 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
2366 unsigned UMULHReg = FastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
2368 emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
2369 /*IsKill=*/false, /*WantResult=*/false);
2376 ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
2377 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2378 TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
2381 ResultReg2 = FuncInfo.CreateRegs(CondTy);
2382 assert((ResultReg1 + 1) == ResultReg2 &&
2383 "Nonconsecutive result registers.");
2384 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2386 .addReg(AArch64::WZR, getKillRegState(true))
2387 .addReg(AArch64::WZR, getKillRegState(true))
2388 .addImm(getInvertedCondCode(CC));
2390 UpdateValueMap(II, ResultReg1, 2);
2397 bool AArch64FastISel::SelectRet(const Instruction *I) {
2398 const ReturnInst *Ret = cast<ReturnInst>(I);
2399 const Function &F = *I->getParent()->getParent();
2401 if (!FuncInfo.CanLowerReturn)
2407 // Build a list of return value registers.
2408 SmallVector<unsigned, 4> RetRegs;
2410 if (Ret->getNumOperands() > 0) {
2411 CallingConv::ID CC = F.getCallingConv();
2412 SmallVector<ISD::OutputArg, 4> Outs;
2413 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
2415 // Analyze operands of the call, assigning locations to each operand.
2416 SmallVector<CCValAssign, 16> ValLocs;
2417 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
2418 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
2419 : RetCC_AArch64_AAPCS;
2420 CCInfo.AnalyzeReturn(Outs, RetCC);
2422 // Only handle a single return value for now.
2423 if (ValLocs.size() != 1)
2426 CCValAssign &VA = ValLocs[0];
2427 const Value *RV = Ret->getOperand(0);
2429 // Don't bother handling odd stuff for now.
2430 if (VA.getLocInfo() != CCValAssign::Full)
2432 // Only handle register returns for now.
2435 unsigned Reg = getRegForValue(RV);
2439 unsigned SrcReg = Reg + VA.getValNo();
2440 unsigned DestReg = VA.getLocReg();
2441 // Avoid a cross-class copy. This is very unlikely.
2442 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
2445 EVT RVEVT = TLI.getValueType(RV->getType());
2446 if (!RVEVT.isSimple())
2449 // Vectors (of > 1 lane) in big endian need tricky handling.
2450 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1)
2453 MVT RVVT = RVEVT.getSimpleVT();
2454 if (RVVT == MVT::f128)
2456 MVT DestVT = VA.getValVT();
2457 // Special handling for extended integers.
2458 if (RVVT != DestVT) {
2459 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
2462 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
2465 bool isZExt = Outs[0].Flags.isZExt();
2466 SrcReg = EmitIntExt(RVVT, SrcReg, DestVT, isZExt);
2472 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2473 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
2475 // Add register to return instruction.
2476 RetRegs.push_back(VA.getLocReg());
2479 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2480 TII.get(AArch64::RET_ReallyLR));
2481 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
2482 MIB.addReg(RetRegs[i], RegState::Implicit);
2486 bool AArch64FastISel::SelectTrunc(const Instruction *I) {
2487 Type *DestTy = I->getType();
2488 Value *Op = I->getOperand(0);
2489 Type *SrcTy = Op->getType();
2491 EVT SrcEVT = TLI.getValueType(SrcTy, true);
2492 EVT DestEVT = TLI.getValueType(DestTy, true);
2493 if (!SrcEVT.isSimple())
2495 if (!DestEVT.isSimple())
2498 MVT SrcVT = SrcEVT.getSimpleVT();
2499 MVT DestVT = DestEVT.getSimpleVT();
2501 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
2504 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
2508 unsigned SrcReg = getRegForValue(Op);
2512 // If we're truncating from i64 to a smaller non-legal type then generate an
2513 // AND. Otherwise, we know the high bits are undefined and a truncate doesn't
2514 // generate any code.
2515 if (SrcVT == MVT::i64) {
2517 switch (DestVT.SimpleTy) {
2519 // Trunc i64 to i32 is handled by the target-independent fast-isel.
2531 // Issue an extract_subreg to get the lower 32-bits.
2532 unsigned Reg32 = FastEmitInst_extractsubreg(MVT::i32, SrcReg, /*Kill=*/true,
2534 MRI.constrainRegClass(Reg32, &AArch64::GPR32RegClass);
2535 // Create the AND instruction which performs the actual truncation.
2536 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
2537 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
2540 .addImm(AArch64_AM::encodeLogicalImmediate(Mask, 32));
2544 UpdateValueMap(I, SrcReg);
2548 unsigned AArch64FastISel::Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt) {
2549 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
2550 DestVT == MVT::i64) &&
2551 "Unexpected value type.");
2552 // Handle i8 and i16 as i32.
2553 if (DestVT == MVT::i8 || DestVT == MVT::i16)
2557 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
2558 unsigned ResultReg = createResultReg(&AArch64::GPR32spRegClass);
2559 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
2562 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
2564 if (DestVT == MVT::i64) {
2565 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
2566 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
2567 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2568 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2569 TII.get(AArch64::SUBREG_TO_REG), Reg64)
2572 .addImm(AArch64::sub_32);
2577 if (DestVT == MVT::i64) {
2578 // FIXME: We're SExt i1 to i64.
2581 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
2582 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SBFMWri),
2591 unsigned AArch64FastISel::Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2592 unsigned Op1, bool Op1IsKill) {
2594 switch (RetVT.SimpleTy) {
2600 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
2602 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
2605 // Create the base instruction, then add the operands.
2606 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
2607 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2608 .addReg(Op0, getKillRegState(Op0IsKill))
2609 .addReg(Op1, getKillRegState(Op1IsKill))
2610 .addReg(ZReg, getKillRegState(true));
2615 unsigned AArch64FastISel::Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2616 unsigned Op1, bool Op1IsKill) {
2617 if (RetVT != MVT::i64)
2620 // Create the base instruction, then add the operands.
2621 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
2622 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SMADDLrrr),
2624 .addReg(Op0, getKillRegState(Op0IsKill))
2625 .addReg(Op1, getKillRegState(Op1IsKill))
2626 .addReg(AArch64::XZR, getKillRegState(true));
2631 unsigned AArch64FastISel::Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2632 unsigned Op1, bool Op1IsKill) {
2633 if (RetVT != MVT::i64)
2636 // Create the base instruction, then add the operands.
2637 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
2638 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::UMADDLrrr),
2640 .addReg(Op0, getKillRegState(Op0IsKill))
2641 .addReg(Op1, getKillRegState(Op1IsKill))
2642 .addReg(AArch64::XZR, getKillRegState(true));
2647 unsigned AArch64FastISel::Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2649 unsigned Opc, ImmR, ImmS;
2650 switch (RetVT.SimpleTy) {
2653 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 7 - Shift; break;
2655 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 15 - Shift; break;
2657 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 31 - Shift; break;
2659 Opc = AArch64::UBFMXri; ImmR = -Shift % 64; ImmS = 63 - Shift; break;
2662 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy);
2663 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, ImmR,
2667 unsigned AArch64FastISel::Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2670 switch (RetVT.SimpleTy) {
2672 case MVT::i8: Opc = AArch64::UBFMWri; ImmS = 7; break;
2673 case MVT::i16: Opc = AArch64::UBFMWri; ImmS = 15; break;
2674 case MVT::i32: Opc = AArch64::UBFMWri; ImmS = 31; break;
2675 case MVT::i64: Opc = AArch64::UBFMXri; ImmS = 63; break;
2678 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy);
2679 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift,
2683 unsigned AArch64FastISel::Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2686 switch (RetVT.SimpleTy) {
2688 case MVT::i8: Opc = AArch64::SBFMWri; ImmS = 7; break;
2689 case MVT::i16: Opc = AArch64::SBFMWri; ImmS = 15; break;
2690 case MVT::i32: Opc = AArch64::SBFMWri; ImmS = 31; break;
2691 case MVT::i64: Opc = AArch64::SBFMXri; ImmS = 63; break;
2694 RetVT.SimpleTy = std::max(MVT::i32, RetVT.SimpleTy);
2695 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift,
2699 unsigned AArch64FastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
2701 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
2703 // FastISel does not have plumbing to deal with extensions where the SrcVT or
2704 // DestVT are odd things, so test to make sure that they are both types we can
2705 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
2706 // bail out to SelectionDAG.
2707 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
2708 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
2709 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
2710 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
2716 switch (SrcVT.SimpleTy) {
2720 return Emiti1Ext(SrcReg, DestVT, isZExt);
2722 if (DestVT == MVT::i64)
2723 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2725 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
2729 if (DestVT == MVT::i64)
2730 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2732 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
2736 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
2737 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2742 // Handle i8 and i16 as i32.
2743 if (DestVT == MVT::i8 || DestVT == MVT::i16)
2745 else if (DestVT == MVT::i64) {
2746 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2747 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2748 TII.get(AArch64::SUBREG_TO_REG), Src64)
2751 .addImm(AArch64::sub_32);
2755 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2756 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2764 bool AArch64FastISel::SelectIntExt(const Instruction *I) {
2765 // On ARM, in general, integer casts don't involve legal types; this code
2766 // handles promotable integers. The high bits for a type smaller than
2767 // the register size are assumed to be undefined.
2768 Type *DestTy = I->getType();
2769 Value *Src = I->getOperand(0);
2770 Type *SrcTy = Src->getType();
2772 bool isZExt = isa<ZExtInst>(I);
2773 unsigned SrcReg = getRegForValue(Src);
2777 EVT SrcEVT = TLI.getValueType(SrcTy, true);
2778 EVT DestEVT = TLI.getValueType(DestTy, true);
2779 if (!SrcEVT.isSimple())
2781 if (!DestEVT.isSimple())
2784 MVT SrcVT = SrcEVT.getSimpleVT();
2785 MVT DestVT = DestEVT.getSimpleVT();
2786 unsigned ResultReg = 0;
2788 // Check if it is an argument and if it is already zero/sign-extended.
2789 if (const auto *Arg = dyn_cast<Argument>(Src)) {
2790 if ((isZExt && Arg->hasZExtAttr()) || (!isZExt && Arg->hasSExtAttr())) {
2791 if (DestVT == MVT::i64) {
2792 ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2793 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2794 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
2797 .addImm(AArch64::sub_32);
2804 ResultReg = EmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2809 UpdateValueMap(I, ResultReg);
2813 bool AArch64FastISel::SelectRem(const Instruction *I, unsigned ISDOpcode) {
2814 EVT DestEVT = TLI.getValueType(I->getType(), true);
2815 if (!DestEVT.isSimple())
2818 MVT DestVT = DestEVT.getSimpleVT();
2819 if (DestVT != MVT::i64 && DestVT != MVT::i32)
2823 bool is64bit = (DestVT == MVT::i64);
2824 switch (ISDOpcode) {
2828 DivOpc = is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
2831 DivOpc = is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
2834 unsigned MSubOpc = is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
2835 unsigned Src0Reg = getRegForValue(I->getOperand(0));
2839 unsigned Src1Reg = getRegForValue(I->getOperand(1));
2843 unsigned QuotReg = createResultReg(TLI.getRegClassFor(DestVT));
2844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(DivOpc), QuotReg)
2847 // The remainder is computed as numerator - (quotient * denominator) using the
2848 // MSUB instruction.
2849 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2850 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MSubOpc), ResultReg)
2854 UpdateValueMap(I, ResultReg);
2858 bool AArch64FastISel::SelectMul(const Instruction *I) {
2859 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2860 if (!SrcEVT.isSimple())
2862 MVT SrcVT = SrcEVT.getSimpleVT();
2864 // Must be simple value type. Don't handle vectors.
2865 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
2869 unsigned Src0Reg = getRegForValue(I->getOperand(0));
2872 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
2874 unsigned Src1Reg = getRegForValue(I->getOperand(1));
2877 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
2879 unsigned ResultReg =
2880 Emit_MUL_rr(SrcVT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
2885 UpdateValueMap(I, ResultReg);
2889 bool AArch64FastISel::SelectShift(const Instruction *I, bool IsLeftShift,
2890 bool IsArithmetic) {
2891 EVT RetEVT = TLI.getValueType(I->getType(), true);
2892 if (!RetEVT.isSimple())
2894 MVT RetVT = RetEVT.getSimpleVT();
2896 if (!isa<ConstantInt>(I->getOperand(1)))
2899 unsigned Op0Reg = getRegForValue(I->getOperand(0));
2902 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
2904 uint64_t ShiftVal = cast<ConstantInt>(I->getOperand(1))->getZExtValue();
2908 ResultReg = Emit_LSL_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2911 ResultReg = Emit_ASR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2913 ResultReg = Emit_LSR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2919 UpdateValueMap(I, ResultReg);
2923 bool AArch64FastISel::SelectBitCast(const Instruction *I) {
2926 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
2928 if (!isTypeLegal(I->getType(), RetVT))
2932 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
2933 Opc = AArch64::FMOVWSr;
2934 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
2935 Opc = AArch64::FMOVXDr;
2936 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
2937 Opc = AArch64::FMOVSWr;
2938 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
2939 Opc = AArch64::FMOVDXr;
2943 unsigned Op0Reg = getRegForValue(I->getOperand(0));
2946 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
2947 unsigned ResultReg = FastEmitInst_r(Opc, TLI.getRegClassFor(RetVT),
2953 UpdateValueMap(I, ResultReg);
2957 bool AArch64FastISel::TargetSelectInstruction(const Instruction *I) {
2958 switch (I->getOpcode()) {
2961 case Instruction::Load:
2962 return SelectLoad(I);
2963 case Instruction::Store:
2964 return SelectStore(I);
2965 case Instruction::Br:
2966 return SelectBranch(I);
2967 case Instruction::IndirectBr:
2968 return SelectIndirectBr(I);
2969 case Instruction::FCmp:
2970 case Instruction::ICmp:
2971 return SelectCmp(I);
2972 case Instruction::Select:
2973 return SelectSelect(I);
2974 case Instruction::FPExt:
2975 return SelectFPExt(I);
2976 case Instruction::FPTrunc:
2977 return SelectFPTrunc(I);
2978 case Instruction::FPToSI:
2979 return SelectFPToInt(I, /*Signed=*/true);
2980 case Instruction::FPToUI:
2981 return SelectFPToInt(I, /*Signed=*/false);
2982 case Instruction::SIToFP:
2983 return SelectIntToFP(I, /*Signed=*/true);
2984 case Instruction::UIToFP:
2985 return SelectIntToFP(I, /*Signed=*/false);
2986 case Instruction::SRem:
2987 return SelectRem(I, ISD::SREM);
2988 case Instruction::URem:
2989 return SelectRem(I, ISD::UREM);
2990 case Instruction::Ret:
2991 return SelectRet(I);
2992 case Instruction::Trunc:
2993 return SelectTrunc(I);
2994 case Instruction::ZExt:
2995 case Instruction::SExt:
2996 return SelectIntExt(I);
2998 // FIXME: All of these should really be handled by the target-independent
2999 // selector -> improve FastISel tblgen.
3000 case Instruction::Mul:
3001 return SelectMul(I);
3002 case Instruction::Shl:
3003 return SelectShift(I, /*IsLeftShift=*/true, /*IsArithmetic=*/false);
3004 case Instruction::LShr:
3005 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/false);
3006 case Instruction::AShr:
3007 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/true);
3008 case Instruction::BitCast:
3009 return SelectBitCast(I);
3012 // Silence warnings.
3013 (void)&CC_AArch64_DarwinPCS_VarArg;
3017 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &funcInfo,
3018 const TargetLibraryInfo *libInfo) {
3019 return new AArch64FastISel(funcInfo, libInfo);