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 "AArch64TargetMachine.h"
18 #include "AArch64Subtarget.h"
19 #include "MCTargetDesc/AArch64AddressingModes.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/FastISel.h"
22 #include "llvm/CodeGen/FunctionLoweringInfo.h"
23 #include "llvm/CodeGen/MachineConstantPool.h"
24 #include "llvm/CodeGen/MachineFrameInfo.h"
25 #include "llvm/CodeGen/MachineInstrBuilder.h"
26 #include "llvm/CodeGen/MachineRegisterInfo.h"
27 #include "llvm/IR/CallingConv.h"
28 #include "llvm/IR/DataLayout.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/GetElementPtrTypeIterator.h"
32 #include "llvm/IR/GlobalAlias.h"
33 #include "llvm/IR/GlobalVariable.h"
34 #include "llvm/IR/Instructions.h"
35 #include "llvm/IR/IntrinsicInst.h"
36 #include "llvm/IR/Operator.h"
37 #include "llvm/Support/CommandLine.h"
42 class AArch64FastISel : public FastISel {
60 Address() : Kind(RegBase), Offset(0) { Base.Reg = 0; }
61 void setKind(BaseKind K) { Kind = K; }
62 BaseKind getKind() const { return Kind; }
63 bool isRegBase() const { return Kind == RegBase; }
64 bool isFIBase() const { return Kind == FrameIndexBase; }
65 void setReg(unsigned Reg) {
66 assert(isRegBase() && "Invalid base register access!");
69 unsigned getReg() const {
70 assert(isRegBase() && "Invalid base register access!");
73 void setFI(unsigned FI) {
74 assert(isFIBase() && "Invalid base frame index access!");
77 unsigned getFI() const {
78 assert(isFIBase() && "Invalid base frame index access!");
81 void setOffset(int64_t O) { Offset = O; }
82 int64_t getOffset() { return Offset; }
84 bool isValid() { return isFIBase() || (isRegBase() && getReg() != 0); }
87 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
88 /// make the right decision when generating code for different targets.
89 const AArch64Subtarget *Subtarget;
93 // Selection routines.
94 bool SelectLoad(const Instruction *I);
95 bool SelectStore(const Instruction *I);
96 bool SelectBranch(const Instruction *I);
97 bool SelectIndirectBr(const Instruction *I);
98 bool SelectCmp(const Instruction *I);
99 bool SelectSelect(const Instruction *I);
100 bool SelectFPExt(const Instruction *I);
101 bool SelectFPTrunc(const Instruction *I);
102 bool SelectFPToInt(const Instruction *I, bool Signed);
103 bool SelectIntToFP(const Instruction *I, bool Signed);
104 bool SelectRem(const Instruction *I, unsigned ISDOpcode);
105 bool SelectCall(const Instruction *I, const char *IntrMemName);
106 bool SelectIntrinsicCall(const IntrinsicInst &I);
107 bool SelectRet(const Instruction *I);
108 bool SelectTrunc(const Instruction *I);
109 bool SelectIntExt(const Instruction *I);
110 bool SelectMul(const Instruction *I);
112 // Utility helper routines.
113 bool isTypeLegal(Type *Ty, MVT &VT);
114 bool isLoadStoreTypeLegal(Type *Ty, MVT &VT);
115 bool ComputeAddress(const Value *Obj, Address &Addr);
116 bool SimplifyAddress(Address &Addr, MVT VT, int64_t ScaleFactor,
118 void AddLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
119 unsigned Flags, bool UseUnscaled);
120 bool IsMemCpySmall(uint64_t Len, unsigned Alignment);
121 bool TryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
124 bool EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt);
125 bool EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
126 bool UseUnscaled = false);
127 bool EmitStore(MVT VT, unsigned SrcReg, Address Addr,
128 bool UseUnscaled = false);
129 unsigned EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
130 unsigned Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
132 unsigned AArch64MaterializeFP(const ConstantFP *CFP, MVT VT);
133 unsigned AArch64MaterializeGV(const GlobalValue *GV);
135 // Call handling routines.
137 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
138 bool ProcessCallArgs(SmallVectorImpl<Value *> &Args,
139 SmallVectorImpl<unsigned> &ArgRegs,
140 SmallVectorImpl<MVT> &ArgVTs,
141 SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
142 SmallVectorImpl<unsigned> &RegArgs, CallingConv::ID CC,
144 bool FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
145 const Instruction *I, CallingConv::ID CC, unsigned &NumBytes);
148 // Backend specific FastISel code.
149 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
150 unsigned TargetMaterializeConstant(const Constant *C) override;
152 explicit AArch64FastISel(FunctionLoweringInfo &funcInfo,
153 const TargetLibraryInfo *libInfo)
154 : FastISel(funcInfo, libInfo) {
155 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
156 Context = &funcInfo.Fn->getContext();
159 bool TargetSelectInstruction(const Instruction *I) override;
161 #include "AArch64GenFastISel.inc"
164 } // end anonymous namespace
166 #include "AArch64GenCallingConv.inc"
168 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
169 if (CC == CallingConv::WebKit_JS)
170 return CC_AArch64_WebKit_JS;
171 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
174 unsigned AArch64FastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
175 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
176 "Alloca should always return a pointer.");
178 // Don't handle dynamic allocas.
179 if (!FuncInfo.StaticAllocaMap.count(AI))
182 DenseMap<const AllocaInst *, int>::iterator SI =
183 FuncInfo.StaticAllocaMap.find(AI);
185 if (SI != FuncInfo.StaticAllocaMap.end()) {
186 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
187 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
189 .addFrameIndex(SI->second)
198 unsigned AArch64FastISel::AArch64MaterializeFP(const ConstantFP *CFP, MVT VT) {
199 if (VT != MVT::f32 && VT != MVT::f64)
202 const APFloat Val = CFP->getValueAPF();
203 bool is64bit = (VT == MVT::f64);
205 // This checks to see if we can use FMOV instructions to materialize
206 // a constant, otherwise we have to materialize via the constant pool.
207 if (TLI.isFPImmLegal(Val, VT)) {
211 Imm = AArch64_AM::getFP64Imm(Val);
212 Opc = AArch64::FMOVDi;
214 Imm = AArch64_AM::getFP32Imm(Val);
215 Opc = AArch64::FMOVSi;
217 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
218 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
223 // Materialize via constant pool. MachineConstantPool wants an explicit
225 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
227 Align = DL.getTypeAllocSize(CFP->getType());
229 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
230 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
231 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
232 ADRPReg).addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGE);
234 unsigned Opc = is64bit ? AArch64::LDRDui : AArch64::LDRSui;
235 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
236 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
238 .addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
242 unsigned AArch64FastISel::AArch64MaterializeGV(const GlobalValue *GV) {
243 // We can't handle thread-local variables quickly yet.
244 if (GV->isThreadLocal())
247 // MachO still uses GOT for large code-model accesses, but ELF requires
248 // movz/movk sequences, which FastISel doesn't handle yet.
249 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
252 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
254 EVT DestEVT = TLI.getValueType(GV->getType(), true);
255 if (!DestEVT.isSimple())
258 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
261 if (OpFlags & AArch64II::MO_GOT) {
263 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
265 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
267 ResultReg = createResultReg(&AArch64::GPR64RegClass);
268 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
271 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
275 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
276 ADRPReg).addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
278 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
279 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
282 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
288 unsigned AArch64FastISel::TargetMaterializeConstant(const Constant *C) {
289 EVT CEVT = TLI.getValueType(C->getType(), true);
291 // Only handle simple types.
292 if (!CEVT.isSimple())
294 MVT VT = CEVT.getSimpleVT();
296 // FIXME: Handle ConstantInt.
297 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
298 return AArch64MaterializeFP(CFP, VT);
299 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
300 return AArch64MaterializeGV(GV);
305 // Computes the address to get to an object.
306 bool AArch64FastISel::ComputeAddress(const Value *Obj, Address &Addr) {
307 const User *U = nullptr;
308 unsigned Opcode = Instruction::UserOp1;
309 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
310 // Don't walk into other basic blocks unless the object is an alloca from
311 // another block, otherwise it may not have a virtual register assigned.
312 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
313 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
314 Opcode = I->getOpcode();
317 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
318 Opcode = C->getOpcode();
322 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
323 if (Ty->getAddressSpace() > 255)
324 // Fast instruction selection doesn't support the special
331 case Instruction::BitCast: {
332 // Look through bitcasts.
333 return ComputeAddress(U->getOperand(0), Addr);
335 case Instruction::IntToPtr: {
336 // Look past no-op inttoptrs.
337 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
338 return ComputeAddress(U->getOperand(0), Addr);
341 case Instruction::PtrToInt: {
342 // Look past no-op ptrtoints.
343 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
344 return ComputeAddress(U->getOperand(0), Addr);
347 case Instruction::GetElementPtr: {
348 Address SavedAddr = Addr;
349 uint64_t TmpOffset = Addr.getOffset();
351 // Iterate through the GEP folding the constants into offsets where
353 gep_type_iterator GTI = gep_type_begin(U);
354 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
356 const Value *Op = *i;
357 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
358 const StructLayout *SL = DL.getStructLayout(STy);
359 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
360 TmpOffset += SL->getElementOffset(Idx);
362 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
364 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
365 // Constant-offset addressing.
366 TmpOffset += CI->getSExtValue() * S;
369 if (canFoldAddIntoGEP(U, Op)) {
370 // A compatible add with a constant operand. Fold the constant.
372 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
373 TmpOffset += CI->getSExtValue() * S;
374 // Iterate on the other operand.
375 Op = cast<AddOperator>(Op)->getOperand(0);
379 goto unsupported_gep;
384 // Try to grab the base operand now.
385 Addr.setOffset(TmpOffset);
386 if (ComputeAddress(U->getOperand(0), Addr))
389 // We failed, restore everything and try the other options.
395 case Instruction::Alloca: {
396 const AllocaInst *AI = cast<AllocaInst>(Obj);
397 DenseMap<const AllocaInst *, int>::iterator SI =
398 FuncInfo.StaticAllocaMap.find(AI);
399 if (SI != FuncInfo.StaticAllocaMap.end()) {
400 Addr.setKind(Address::FrameIndexBase);
401 Addr.setFI(SI->second);
408 // Try to get this in a register if nothing else has worked.
410 Addr.setReg(getRegForValue(Obj));
411 return Addr.isValid();
414 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
415 EVT evt = TLI.getValueType(Ty, true);
417 // Only handle simple types.
418 if (evt == MVT::Other || !evt.isSimple())
420 VT = evt.getSimpleVT();
422 // This is a legal type, but it's not something we handle in fast-isel.
426 // Handle all other legal types, i.e. a register that will directly hold this
428 return TLI.isTypeLegal(VT);
431 bool AArch64FastISel::isLoadStoreTypeLegal(Type *Ty, MVT &VT) {
432 if (isTypeLegal(Ty, VT))
435 // If this is a type than can be sign or zero-extended to a basic operation
436 // go ahead and accept it now. For stores, this reflects truncation.
437 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
443 bool AArch64FastISel::SimplifyAddress(Address &Addr, MVT VT,
444 int64_t ScaleFactor, bool UseUnscaled) {
445 bool needsLowering = false;
446 int64_t Offset = Addr.getOffset();
447 switch (VT.SimpleTy) {
458 // Using scaled, 12-bit, unsigned immediate offsets.
459 needsLowering = ((Offset & 0xfff) != Offset);
461 // Using unscaled, 9-bit, signed immediate offsets.
462 needsLowering = (Offset > 256 || Offset < -256);
466 //If this is a stack pointer and the offset needs to be simplified then put
467 // the alloca address into a register, set the base type back to register and
468 // continue. This should almost never happen.
469 if (needsLowering && Addr.getKind() == Address::FrameIndexBase) {
470 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
471 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
473 .addFrameIndex(Addr.getFI())
476 Addr.setKind(Address::RegBase);
477 Addr.setReg(ResultReg);
480 // Since the offset is too large for the load/store instruction get the
481 // reg+offset into a register.
483 uint64_t UnscaledOffset = Addr.getOffset() * ScaleFactor;
484 unsigned ResultReg = FastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(), false,
485 UnscaledOffset, MVT::i64);
488 Addr.setReg(ResultReg);
494 void AArch64FastISel::AddLoadStoreOperands(Address &Addr,
495 const MachineInstrBuilder &MIB,
496 unsigned Flags, bool UseUnscaled) {
497 int64_t Offset = Addr.getOffset();
498 // Frame base works a bit differently. Handle it separately.
499 if (Addr.getKind() == Address::FrameIndexBase) {
500 int FI = Addr.getFI();
501 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
502 // and alignment should be based on the VT.
503 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
504 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
505 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
506 // Now add the rest of the operands.
507 MIB.addFrameIndex(FI).addImm(Offset).addMemOperand(MMO);
509 // Now add the rest of the operands.
510 MIB.addReg(Addr.getReg());
515 bool AArch64FastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
517 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
518 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
519 if (!UseUnscaled && Addr.getOffset() < 0)
523 const TargetRegisterClass *RC;
525 int64_t ScaleFactor = 0;
526 switch (VT.SimpleTy) {
531 // Intentional fall-through.
533 Opc = UseUnscaled ? AArch64::LDURBBi : AArch64::LDRBBui;
534 RC = &AArch64::GPR32RegClass;
538 Opc = UseUnscaled ? AArch64::LDURHHi : AArch64::LDRHHui;
539 RC = &AArch64::GPR32RegClass;
543 Opc = UseUnscaled ? AArch64::LDURWi : AArch64::LDRWui;
544 RC = &AArch64::GPR32RegClass;
548 Opc = UseUnscaled ? AArch64::LDURXi : AArch64::LDRXui;
549 RC = &AArch64::GPR64RegClass;
553 Opc = UseUnscaled ? AArch64::LDURSi : AArch64::LDRSui;
554 RC = TLI.getRegClassFor(VT);
558 Opc = UseUnscaled ? AArch64::LDURDi : AArch64::LDRDui;
559 RC = TLI.getRegClassFor(VT);
565 int64_t Offset = Addr.getOffset();
566 if (Offset & (ScaleFactor - 1))
567 // Retry using an unscaled, 9-bit, signed immediate offset.
568 return EmitLoad(VT, ResultReg, Addr, /*UseUnscaled*/ true);
570 Addr.setOffset(Offset / ScaleFactor);
573 // Simplify this down to something we can handle.
574 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled))
577 // Create the base instruction, then add the operands.
578 ResultReg = createResultReg(RC);
579 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
580 TII.get(Opc), ResultReg);
581 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, UseUnscaled);
583 // Loading an i1 requires special handling.
585 MRI.constrainRegClass(ResultReg, &AArch64::GPR32RegClass);
586 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
587 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
590 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
596 bool AArch64FastISel::SelectLoad(const Instruction *I) {
598 // Verify we have a legal type before going any further. Currently, we handle
599 // simple types that will directly fit in a register (i32/f32/i64/f64) or
600 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
601 if (!isLoadStoreTypeLegal(I->getType(), VT) || cast<LoadInst>(I)->isAtomic())
604 // See if we can handle this address.
606 if (!ComputeAddress(I->getOperand(0), Addr))
610 if (!EmitLoad(VT, ResultReg, Addr))
613 UpdateValueMap(I, ResultReg);
617 bool AArch64FastISel::EmitStore(MVT VT, unsigned SrcReg, Address Addr,
619 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
620 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
621 if (!UseUnscaled && Addr.getOffset() < 0)
626 int64_t ScaleFactor = 0;
627 // Using scaled, 12-bit, unsigned immediate offsets.
628 switch (VT.SimpleTy) {
634 StrOpc = UseUnscaled ? AArch64::STURBBi : AArch64::STRBBui;
638 StrOpc = UseUnscaled ? AArch64::STURHHi : AArch64::STRHHui;
642 StrOpc = UseUnscaled ? AArch64::STURWi : AArch64::STRWui;
646 StrOpc = UseUnscaled ? AArch64::STURXi : AArch64::STRXui;
650 StrOpc = UseUnscaled ? AArch64::STURSi : AArch64::STRSui;
654 StrOpc = UseUnscaled ? AArch64::STURDi : AArch64::STRDui;
660 int64_t Offset = Addr.getOffset();
661 if (Offset & (ScaleFactor - 1))
662 // Retry using an unscaled, 9-bit, signed immediate offset.
663 return EmitStore(VT, SrcReg, Addr, /*UseUnscaled*/ true);
665 Addr.setOffset(Offset / ScaleFactor);
668 // Simplify this down to something we can handle.
669 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled))
672 // Storing an i1 requires special handling.
674 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
675 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
676 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
679 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
682 // Create the base instruction, then add the operands.
683 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
684 TII.get(StrOpc)).addReg(SrcReg);
685 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, UseUnscaled);
689 bool AArch64FastISel::SelectStore(const Instruction *I) {
691 Value *Op0 = I->getOperand(0);
692 // Verify we have a legal type before going any further. Currently, we handle
693 // simple types that will directly fit in a register (i32/f32/i64/f64) or
694 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
695 if (!isLoadStoreTypeLegal(Op0->getType(), VT) ||
696 cast<StoreInst>(I)->isAtomic())
699 // Get the value to be stored into a register.
700 unsigned SrcReg = getRegForValue(Op0);
704 // See if we can handle this address.
706 if (!ComputeAddress(I->getOperand(1), Addr))
709 if (!EmitStore(VT, SrcReg, Addr))
714 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
716 case CmpInst::FCMP_ONE:
717 case CmpInst::FCMP_UEQ:
719 // AL is our "false" for now. The other two need more compares.
720 return AArch64CC::AL;
721 case CmpInst::ICMP_EQ:
722 case CmpInst::FCMP_OEQ:
723 return AArch64CC::EQ;
724 case CmpInst::ICMP_SGT:
725 case CmpInst::FCMP_OGT:
726 return AArch64CC::GT;
727 case CmpInst::ICMP_SGE:
728 case CmpInst::FCMP_OGE:
729 return AArch64CC::GE;
730 case CmpInst::ICMP_UGT:
731 case CmpInst::FCMP_UGT:
732 return AArch64CC::HI;
733 case CmpInst::FCMP_OLT:
734 return AArch64CC::MI;
735 case CmpInst::ICMP_ULE:
736 case CmpInst::FCMP_OLE:
737 return AArch64CC::LS;
738 case CmpInst::FCMP_ORD:
739 return AArch64CC::VC;
740 case CmpInst::FCMP_UNO:
741 return AArch64CC::VS;
742 case CmpInst::FCMP_UGE:
743 return AArch64CC::PL;
744 case CmpInst::ICMP_SLT:
745 case CmpInst::FCMP_ULT:
746 return AArch64CC::LT;
747 case CmpInst::ICMP_SLE:
748 case CmpInst::FCMP_ULE:
749 return AArch64CC::LE;
750 case CmpInst::FCMP_UNE:
751 case CmpInst::ICMP_NE:
752 return AArch64CC::NE;
753 case CmpInst::ICMP_UGE:
754 return AArch64CC::HS;
755 case CmpInst::ICMP_ULT:
756 return AArch64CC::LO;
760 bool AArch64FastISel::SelectBranch(const Instruction *I) {
761 const BranchInst *BI = cast<BranchInst>(I);
762 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
763 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
765 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
766 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
767 // We may not handle every CC for now.
768 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate());
769 if (CC == AArch64CC::AL)
773 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
777 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
780 FuncInfo.MBB->addSuccessor(TBB);
782 FastEmitBranch(FBB, DbgLoc);
785 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
787 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
788 (isLoadStoreTypeLegal(TI->getOperand(0)->getType(), SrcVT))) {
789 unsigned CondReg = getRegForValue(TI->getOperand(0));
793 // Issue an extract_subreg to get the lower 32-bits.
794 if (SrcVT == MVT::i64)
795 CondReg = FastEmitInst_extractsubreg(MVT::i32, CondReg, /*Kill=*/true,
798 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
799 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
800 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
801 TII.get(AArch64::ANDWri), ANDReg)
803 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
804 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
805 TII.get(AArch64::SUBSWri))
811 unsigned CC = AArch64CC::NE;
812 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
816 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
819 FuncInfo.MBB->addSuccessor(TBB);
820 FastEmitBranch(FBB, DbgLoc);
823 } else if (const ConstantInt *CI =
824 dyn_cast<ConstantInt>(BI->getCondition())) {
825 uint64_t Imm = CI->getZExtValue();
826 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
827 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
829 FuncInfo.MBB->addSuccessor(Target);
833 unsigned CondReg = getRegForValue(BI->getCondition());
837 // We've been divorced from our compare! Our block was split, and
838 // now our compare lives in a predecessor block. We musn't
839 // re-compare here, as the children of the compare aren't guaranteed
840 // live across the block boundary (we *could* check for this).
841 // Regardless, the compare has been done in the predecessor block,
842 // and it left a value for us in a virtual register. Ergo, we test
843 // the one-bit value left in the virtual register.
844 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri),
850 unsigned CC = AArch64CC::NE;
851 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
856 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
859 FuncInfo.MBB->addSuccessor(TBB);
860 FastEmitBranch(FBB, DbgLoc);
864 bool AArch64FastISel::SelectIndirectBr(const Instruction *I) {
865 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
866 unsigned AddrReg = getRegForValue(BI->getOperand(0));
870 // Emit the indirect branch.
871 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BR))
874 // Make sure the CFG is up-to-date.
875 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
876 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
881 bool AArch64FastISel::EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt) {
882 Type *Ty = Src1Value->getType();
883 EVT SrcEVT = TLI.getValueType(Ty, true);
884 if (!SrcEVT.isSimple())
886 MVT SrcVT = SrcEVT.getSimpleVT();
888 // Check to see if the 2nd operand is a constant that we can encode directly
892 bool isNegativeImm = false;
893 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
894 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
895 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
896 const APInt &CIVal = ConstInt->getValue();
898 Imm = (isZExt) ? CIVal.getZExtValue() : CIVal.getSExtValue();
899 if (CIVal.isNegative()) {
900 isNegativeImm = true;
903 // FIXME: We can handle more immediates using shifts.
904 UseImm = ((Imm & 0xfff) == Imm);
906 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
907 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
908 if (ConstFP->isZero() && !ConstFP->isNegative())
915 bool needsExt = false;
916 switch (SrcVT.SimpleTy) {
923 // Intentional fall-through.
927 CmpOpc = isNegativeImm ? AArch64::ADDSWri : AArch64::SUBSWri;
929 CmpOpc = AArch64::SUBSWrr;
934 CmpOpc = isNegativeImm ? AArch64::ADDSXri : AArch64::SUBSXri;
936 CmpOpc = AArch64::SUBSXrr;
940 CmpOpc = UseImm ? AArch64::FCMPSri : AArch64::FCMPSrr;
944 CmpOpc = UseImm ? AArch64::FCMPDri : AArch64::FCMPDrr;
948 unsigned SrcReg1 = getRegForValue(Src1Value);
954 SrcReg2 = getRegForValue(Src2Value);
959 // We have i1, i8, or i16, we need to either zero extend or sign extend.
961 SrcReg1 = EmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
965 SrcReg2 = EmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
973 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
979 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
985 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
988 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
995 bool AArch64FastISel::SelectCmp(const Instruction *I) {
996 const CmpInst *CI = cast<CmpInst>(I);
998 // We may not handle every CC for now.
999 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate());
1000 if (CC == AArch64CC::AL)
1004 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1007 // Now set a register based on the comparison.
1008 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
1009 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1010 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1012 .addReg(AArch64::WZR)
1013 .addReg(AArch64::WZR)
1014 .addImm(invertedCC);
1016 UpdateValueMap(I, ResultReg);
1020 bool AArch64FastISel::SelectSelect(const Instruction *I) {
1021 const SelectInst *SI = cast<SelectInst>(I);
1023 EVT DestEVT = TLI.getValueType(SI->getType(), true);
1024 if (!DestEVT.isSimple())
1027 MVT DestVT = DestEVT.getSimpleVT();
1028 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
1032 unsigned CondReg = getRegForValue(SI->getCondition());
1035 unsigned TrueReg = getRegForValue(SI->getTrueValue());
1038 unsigned FalseReg = getRegForValue(SI->getFalseValue());
1043 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
1044 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1045 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1048 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1050 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri))
1057 switch (DestVT.SimpleTy) {
1061 SelectOpc = AArch64::CSELWr;
1064 SelectOpc = AArch64::CSELXr;
1067 SelectOpc = AArch64::FCSELSrrr;
1070 SelectOpc = AArch64::FCSELDrrr;
1074 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1075 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SelectOpc),
1079 .addImm(AArch64CC::NE);
1081 UpdateValueMap(I, ResultReg);
1085 bool AArch64FastISel::SelectFPExt(const Instruction *I) {
1086 Value *V = I->getOperand(0);
1087 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
1090 unsigned Op = getRegForValue(V);
1094 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
1095 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
1096 ResultReg).addReg(Op);
1097 UpdateValueMap(I, ResultReg);
1101 bool AArch64FastISel::SelectFPTrunc(const Instruction *I) {
1102 Value *V = I->getOperand(0);
1103 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
1106 unsigned Op = getRegForValue(V);
1110 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
1111 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
1112 ResultReg).addReg(Op);
1113 UpdateValueMap(I, ResultReg);
1117 // FPToUI and FPToSI
1118 bool AArch64FastISel::SelectFPToInt(const Instruction *I, bool Signed) {
1120 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1123 unsigned SrcReg = getRegForValue(I->getOperand(0));
1127 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1128 if (SrcVT == MVT::f128)
1132 if (SrcVT == MVT::f64) {
1134 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
1136 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
1139 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
1141 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
1143 unsigned ResultReg = createResultReg(
1144 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
1145 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1147 UpdateValueMap(I, ResultReg);
1151 bool AArch64FastISel::SelectIntToFP(const Instruction *I, bool Signed) {
1153 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1155 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
1156 "Unexpected value type.");
1158 unsigned SrcReg = getRegForValue(I->getOperand(0));
1162 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1164 // Handle sign-extension.
1165 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
1167 EmitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
1172 MRI.constrainRegClass(SrcReg, SrcVT == MVT::i64 ? &AArch64::GPR64RegClass
1173 : &AArch64::GPR32RegClass);
1176 if (SrcVT == MVT::i64) {
1178 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
1180 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
1183 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
1185 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
1188 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1189 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1191 UpdateValueMap(I, ResultReg);
1195 bool AArch64FastISel::ProcessCallArgs(
1196 SmallVectorImpl<Value *> &Args, SmallVectorImpl<unsigned> &ArgRegs,
1197 SmallVectorImpl<MVT> &ArgVTs, SmallVectorImpl<ISD::ArgFlagsTy> &ArgFlags,
1198 SmallVectorImpl<unsigned> &RegArgs, CallingConv::ID CC,
1199 unsigned &NumBytes) {
1200 SmallVector<CCValAssign, 16> ArgLocs;
1201 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
1202 CCInfo.AnalyzeCallOperands(ArgVTs, ArgFlags, CCAssignFnForCall(CC));
1204 // Get a count of how many bytes are to be pushed on the stack.
1205 NumBytes = CCInfo.getNextStackOffset();
1207 // Issue CALLSEQ_START
1208 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1209 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
1212 // Process the args.
1213 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1214 CCValAssign &VA = ArgLocs[i];
1215 unsigned Arg = ArgRegs[VA.getValNo()];
1216 MVT ArgVT = ArgVTs[VA.getValNo()];
1218 // Handle arg promotion: SExt, ZExt, AExt.
1219 switch (VA.getLocInfo()) {
1220 case CCValAssign::Full:
1222 case CCValAssign::SExt: {
1223 MVT DestVT = VA.getLocVT();
1225 Arg = EmitIntExt(SrcVT, Arg, DestVT, /*isZExt*/ false);
1230 case CCValAssign::AExt:
1231 // Intentional fall-through.
1232 case CCValAssign::ZExt: {
1233 MVT DestVT = VA.getLocVT();
1235 Arg = EmitIntExt(SrcVT, Arg, DestVT, /*isZExt*/ true);
1241 llvm_unreachable("Unknown arg promotion!");
1244 // Now copy/store arg to correct locations.
1245 if (VA.isRegLoc() && !VA.needsCustom()) {
1246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1247 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(Arg);
1248 RegArgs.push_back(VA.getLocReg());
1249 } else if (VA.needsCustom()) {
1250 // FIXME: Handle custom args.
1253 assert(VA.isMemLoc() && "Assuming store on stack.");
1255 // Need to store on the stack.
1256 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
1258 unsigned BEAlign = 0;
1259 if (ArgSize < 8 && !Subtarget->isLittleEndian())
1260 BEAlign = 8 - ArgSize;
1263 Addr.setKind(Address::RegBase);
1264 Addr.setReg(AArch64::SP);
1265 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1267 if (!EmitStore(ArgVT, Arg, Addr))
1274 bool AArch64FastISel::FinishCall(MVT RetVT, SmallVectorImpl<unsigned> &UsedRegs,
1275 const Instruction *I, CallingConv::ID CC,
1276 unsigned &NumBytes) {
1277 // Issue CALLSEQ_END
1278 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1279 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
1283 // Now the return value.
1284 if (RetVT != MVT::isVoid) {
1285 SmallVector<CCValAssign, 16> RVLocs;
1286 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
1287 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
1289 // Only handle a single return value.
1290 if (RVLocs.size() != 1)
1293 // Copy all of the result registers out of their specified physreg.
1294 MVT CopyVT = RVLocs[0].getValVT();
1295 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1296 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1297 TII.get(TargetOpcode::COPY),
1298 ResultReg).addReg(RVLocs[0].getLocReg());
1299 UsedRegs.push_back(RVLocs[0].getLocReg());
1301 // Finally update the result.
1302 UpdateValueMap(I, ResultReg);
1308 bool AArch64FastISel::SelectCall(const Instruction *I,
1309 const char *IntrMemName = nullptr) {
1310 const CallInst *CI = cast<CallInst>(I);
1311 const Value *Callee = CI->getCalledValue();
1313 // Don't handle inline asm or intrinsics.
1314 if (isa<InlineAsm>(Callee))
1317 // Only handle global variable Callees.
1318 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1322 // Check the calling convention.
1323 ImmutableCallSite CS(CI);
1324 CallingConv::ID CC = CS.getCallingConv();
1326 // Let SDISel handle vararg functions.
1327 PointerType *PT = cast<PointerType>(CS.getCalledValue()->getType());
1328 FunctionType *FTy = cast<FunctionType>(PT->getElementType());
1329 if (FTy->isVarArg())
1332 // Handle *simple* calls for now.
1334 Type *RetTy = I->getType();
1335 if (RetTy->isVoidTy())
1336 RetVT = MVT::isVoid;
1337 else if (!isTypeLegal(RetTy, RetVT))
1340 // Set up the argument vectors.
1341 SmallVector<Value *, 8> Args;
1342 SmallVector<unsigned, 8> ArgRegs;
1343 SmallVector<MVT, 8> ArgVTs;
1344 SmallVector<ISD::ArgFlagsTy, 8> ArgFlags;
1345 Args.reserve(CS.arg_size());
1346 ArgRegs.reserve(CS.arg_size());
1347 ArgVTs.reserve(CS.arg_size());
1348 ArgFlags.reserve(CS.arg_size());
1350 for (ImmutableCallSite::arg_iterator i = CS.arg_begin(), e = CS.arg_end();
1352 // If we're lowering a memory intrinsic instead of a regular call, skip the
1353 // last two arguments, which shouldn't be passed to the underlying function.
1354 if (IntrMemName && e - i <= 2)
1357 unsigned Arg = getRegForValue(*i);
1361 ISD::ArgFlagsTy Flags;
1362 unsigned AttrInd = i - CS.arg_begin() + 1;
1363 if (CS.paramHasAttr(AttrInd, Attribute::SExt))
1365 if (CS.paramHasAttr(AttrInd, Attribute::ZExt))
1368 // FIXME: Only handle *easy* calls for now.
1369 if (CS.paramHasAttr(AttrInd, Attribute::InReg) ||
1370 CS.paramHasAttr(AttrInd, Attribute::StructRet) ||
1371 CS.paramHasAttr(AttrInd, Attribute::Nest) ||
1372 CS.paramHasAttr(AttrInd, Attribute::ByVal))
1376 Type *ArgTy = (*i)->getType();
1377 if (!isTypeLegal(ArgTy, ArgVT) &&
1378 !(ArgVT == MVT::i1 || ArgVT == MVT::i8 || ArgVT == MVT::i16))
1381 // We don't handle vector parameters yet.
1382 if (ArgVT.isVector() || ArgVT.getSizeInBits() > 64)
1385 unsigned OriginalAlignment = DL.getABITypeAlignment(ArgTy);
1386 Flags.setOrigAlign(OriginalAlignment);
1389 ArgRegs.push_back(Arg);
1390 ArgVTs.push_back(ArgVT);
1391 ArgFlags.push_back(Flags);
1394 // Handle the arguments now that we've gotten them.
1395 SmallVector<unsigned, 4> RegArgs;
1397 if (!ProcessCallArgs(Args, ArgRegs, ArgVTs, ArgFlags, RegArgs, CC, NumBytes))
1401 MachineInstrBuilder MIB;
1402 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BL));
1404 MIB.addGlobalAddress(GV, 0, 0);
1406 MIB.addExternalSymbol(IntrMemName, 0);
1408 // Add implicit physical register uses to the call.
1409 for (unsigned i = 0, e = RegArgs.size(); i != e; ++i)
1410 MIB.addReg(RegArgs[i], RegState::Implicit);
1412 // Add a register mask with the call-preserved registers.
1413 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1414 MIB.addRegMask(TRI.getCallPreservedMask(CS.getCallingConv()));
1416 // Finish off the call including any return values.
1417 SmallVector<unsigned, 4> UsedRegs;
1418 if (!FinishCall(RetVT, UsedRegs, I, CC, NumBytes))
1421 // Set all unused physreg defs as dead.
1422 static_cast<MachineInstr *>(MIB)->setPhysRegsDeadExcept(UsedRegs, TRI);
1427 bool AArch64FastISel::IsMemCpySmall(uint64_t Len, unsigned Alignment) {
1429 return Len / Alignment <= 4;
1434 bool AArch64FastISel::TryEmitSmallMemCpy(Address Dest, Address Src,
1435 uint64_t Len, unsigned Alignment) {
1436 // Make sure we don't bloat code by inlining very large memcpy's.
1437 if (!IsMemCpySmall(Len, Alignment))
1440 int64_t UnscaledOffset = 0;
1441 Address OrigDest = Dest;
1442 Address OrigSrc = Src;
1446 if (!Alignment || Alignment >= 8) {
1457 // Bound based on alignment.
1458 if (Len >= 4 && Alignment == 4)
1460 else if (Len >= 2 && Alignment == 2)
1469 RV = EmitLoad(VT, ResultReg, Src);
1473 RV = EmitStore(VT, ResultReg, Dest);
1477 int64_t Size = VT.getSizeInBits() / 8;
1479 UnscaledOffset += Size;
1481 // We need to recompute the unscaled offset for each iteration.
1482 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
1483 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
1489 bool AArch64FastISel::SelectIntrinsicCall(const IntrinsicInst &I) {
1490 // FIXME: Handle more intrinsics.
1491 switch (I.getIntrinsicID()) {
1494 case Intrinsic::memcpy:
1495 case Intrinsic::memmove: {
1496 const MemTransferInst &MTI = cast<MemTransferInst>(I);
1497 // Don't handle volatile.
1498 if (MTI.isVolatile())
1501 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
1502 // we would emit dead code because we don't currently handle memmoves.
1503 bool isMemCpy = (I.getIntrinsicID() == Intrinsic::memcpy);
1504 if (isa<ConstantInt>(MTI.getLength()) && isMemCpy) {
1505 // Small memcpy's are common enough that we want to do them without a call
1507 uint64_t Len = cast<ConstantInt>(MTI.getLength())->getZExtValue();
1508 unsigned Alignment = MTI.getAlignment();
1509 if (IsMemCpySmall(Len, Alignment)) {
1511 if (!ComputeAddress(MTI.getRawDest(), Dest) ||
1512 !ComputeAddress(MTI.getRawSource(), Src))
1514 if (TryEmitSmallMemCpy(Dest, Src, Len, Alignment))
1519 if (!MTI.getLength()->getType()->isIntegerTy(64))
1522 if (MTI.getSourceAddressSpace() > 255 || MTI.getDestAddressSpace() > 255)
1523 // Fast instruction selection doesn't support the special
1527 const char *IntrMemName = isa<MemCpyInst>(I) ? "memcpy" : "memmove";
1528 return SelectCall(&I, IntrMemName);
1530 case Intrinsic::memset: {
1531 const MemSetInst &MSI = cast<MemSetInst>(I);
1532 // Don't handle volatile.
1533 if (MSI.isVolatile())
1536 if (!MSI.getLength()->getType()->isIntegerTy(64))
1539 if (MSI.getDestAddressSpace() > 255)
1540 // Fast instruction selection doesn't support the special
1544 return SelectCall(&I, "memset");
1546 case Intrinsic::trap: {
1547 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
1555 bool AArch64FastISel::SelectRet(const Instruction *I) {
1556 const ReturnInst *Ret = cast<ReturnInst>(I);
1557 const Function &F = *I->getParent()->getParent();
1559 if (!FuncInfo.CanLowerReturn)
1565 // Build a list of return value registers.
1566 SmallVector<unsigned, 4> RetRegs;
1568 if (Ret->getNumOperands() > 0) {
1569 CallingConv::ID CC = F.getCallingConv();
1570 SmallVector<ISD::OutputArg, 4> Outs;
1571 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1573 // Analyze operands of the call, assigning locations to each operand.
1574 SmallVector<CCValAssign, 16> ValLocs;
1575 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
1577 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
1578 : RetCC_AArch64_AAPCS;
1579 CCInfo.AnalyzeReturn(Outs, RetCC);
1581 // Only handle a single return value for now.
1582 if (ValLocs.size() != 1)
1585 CCValAssign &VA = ValLocs[0];
1586 const Value *RV = Ret->getOperand(0);
1588 // Don't bother handling odd stuff for now.
1589 if (VA.getLocInfo() != CCValAssign::Full)
1591 // Only handle register returns for now.
1594 unsigned Reg = getRegForValue(RV);
1598 unsigned SrcReg = Reg + VA.getValNo();
1599 unsigned DestReg = VA.getLocReg();
1600 // Avoid a cross-class copy. This is very unlikely.
1601 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1604 EVT RVEVT = TLI.getValueType(RV->getType());
1605 if (!RVEVT.isSimple())
1608 // Vectors (of > 1 lane) in big endian need tricky handling.
1609 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1)
1612 MVT RVVT = RVEVT.getSimpleVT();
1613 if (RVVT == MVT::f128)
1615 MVT DestVT = VA.getValVT();
1616 // Special handling for extended integers.
1617 if (RVVT != DestVT) {
1618 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1621 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1624 bool isZExt = Outs[0].Flags.isZExt();
1625 SrcReg = EmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1631 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1632 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1634 // Add register to return instruction.
1635 RetRegs.push_back(VA.getLocReg());
1638 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1639 TII.get(AArch64::RET_ReallyLR));
1640 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1641 MIB.addReg(RetRegs[i], RegState::Implicit);
1645 bool AArch64FastISel::SelectTrunc(const Instruction *I) {
1646 Type *DestTy = I->getType();
1647 Value *Op = I->getOperand(0);
1648 Type *SrcTy = Op->getType();
1650 EVT SrcEVT = TLI.getValueType(SrcTy, true);
1651 EVT DestEVT = TLI.getValueType(DestTy, true);
1652 if (!SrcEVT.isSimple())
1654 if (!DestEVT.isSimple())
1657 MVT SrcVT = SrcEVT.getSimpleVT();
1658 MVT DestVT = DestEVT.getSimpleVT();
1660 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
1663 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
1667 unsigned SrcReg = getRegForValue(Op);
1671 // If we're truncating from i64 to a smaller non-legal type then generate an
1672 // AND. Otherwise, we know the high bits are undefined and a truncate doesn't
1673 // generate any code.
1674 if (SrcVT == MVT::i64) {
1676 switch (DestVT.SimpleTy) {
1678 // Trunc i64 to i32 is handled by the target-independent fast-isel.
1690 // Issue an extract_subreg to get the lower 32-bits.
1691 unsigned Reg32 = FastEmitInst_extractsubreg(MVT::i32, SrcReg, /*Kill=*/true,
1693 MRI.constrainRegClass(Reg32, &AArch64::GPR32RegClass);
1694 // Create the AND instruction which performs the actual truncation.
1695 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1696 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1699 .addImm(AArch64_AM::encodeLogicalImmediate(Mask, 32));
1703 UpdateValueMap(I, SrcReg);
1707 unsigned AArch64FastISel::Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt) {
1708 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
1709 DestVT == MVT::i64) &&
1710 "Unexpected value type.");
1711 // Handle i8 and i16 as i32.
1712 if (DestVT == MVT::i8 || DestVT == MVT::i16)
1716 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
1717 unsigned ResultReg = createResultReg(&AArch64::GPR32spRegClass);
1718 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1721 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1723 if (DestVT == MVT::i64) {
1724 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
1725 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
1726 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1727 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1728 TII.get(AArch64::SUBREG_TO_REG), Reg64)
1731 .addImm(AArch64::sub_32);
1736 if (DestVT == MVT::i64) {
1737 // FIXME: We're SExt i1 to i64.
1740 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1741 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SBFMWri),
1750 unsigned AArch64FastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1752 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
1754 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1755 // DestVT are odd things, so test to make sure that they are both types we can
1756 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1757 // bail out to SelectionDAG.
1758 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
1759 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
1760 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
1761 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
1767 switch (SrcVT.SimpleTy) {
1771 return Emiti1Ext(SrcReg, DestVT, isZExt);
1773 if (DestVT == MVT::i64)
1774 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
1776 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
1780 if (DestVT == MVT::i64)
1781 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
1783 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
1787 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
1788 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
1793 // Handle i8 and i16 as i32.
1794 if (DestVT == MVT::i8 || DestVT == MVT::i16)
1796 else if (DestVT == MVT::i64) {
1797 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1798 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1799 TII.get(AArch64::SUBREG_TO_REG), Src64)
1802 .addImm(AArch64::sub_32);
1806 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1807 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1815 bool AArch64FastISel::SelectIntExt(const Instruction *I) {
1816 // On ARM, in general, integer casts don't involve legal types; this code
1817 // handles promotable integers. The high bits for a type smaller than
1818 // the register size are assumed to be undefined.
1819 Type *DestTy = I->getType();
1820 Value *Src = I->getOperand(0);
1821 Type *SrcTy = Src->getType();
1823 bool isZExt = isa<ZExtInst>(I);
1824 unsigned SrcReg = getRegForValue(Src);
1828 EVT SrcEVT = TLI.getValueType(SrcTy, true);
1829 EVT DestEVT = TLI.getValueType(DestTy, true);
1830 if (!SrcEVT.isSimple())
1832 if (!DestEVT.isSimple())
1835 MVT SrcVT = SrcEVT.getSimpleVT();
1836 MVT DestVT = DestEVT.getSimpleVT();
1837 unsigned ResultReg = EmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
1840 UpdateValueMap(I, ResultReg);
1844 bool AArch64FastISel::SelectRem(const Instruction *I, unsigned ISDOpcode) {
1845 EVT DestEVT = TLI.getValueType(I->getType(), true);
1846 if (!DestEVT.isSimple())
1849 MVT DestVT = DestEVT.getSimpleVT();
1850 if (DestVT != MVT::i64 && DestVT != MVT::i32)
1854 bool is64bit = (DestVT == MVT::i64);
1855 switch (ISDOpcode) {
1859 DivOpc = is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
1862 DivOpc = is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
1865 unsigned MSubOpc = is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
1866 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1870 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1874 unsigned QuotReg = createResultReg(TLI.getRegClassFor(DestVT));
1875 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(DivOpc), QuotReg)
1878 // The remainder is computed as numerator - (quotient * denominator) using the
1879 // MSUB instruction.
1880 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1881 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MSubOpc), ResultReg)
1885 UpdateValueMap(I, ResultReg);
1889 bool AArch64FastISel::SelectMul(const Instruction *I) {
1890 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1891 if (!SrcEVT.isSimple())
1893 MVT SrcVT = SrcEVT.getSimpleVT();
1895 // Must be simple value type. Don't handle vectors.
1896 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
1902 switch (SrcVT.SimpleTy) {
1908 ZReg = AArch64::WZR;
1909 Opc = AArch64::MADDWrrr;
1913 ZReg = AArch64::XZR;
1914 Opc = AArch64::MADDXrrr;
1918 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1922 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1926 // Create the base instruction, then add the operands.
1927 unsigned ResultReg = createResultReg(TLI.getRegClassFor(SrcVT));
1928 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1932 UpdateValueMap(I, ResultReg);
1936 bool AArch64FastISel::TargetSelectInstruction(const Instruction *I) {
1937 switch (I->getOpcode()) {
1940 case Instruction::Load:
1941 return SelectLoad(I);
1942 case Instruction::Store:
1943 return SelectStore(I);
1944 case Instruction::Br:
1945 return SelectBranch(I);
1946 case Instruction::IndirectBr:
1947 return SelectIndirectBr(I);
1948 case Instruction::FCmp:
1949 case Instruction::ICmp:
1950 return SelectCmp(I);
1951 case Instruction::Select:
1952 return SelectSelect(I);
1953 case Instruction::FPExt:
1954 return SelectFPExt(I);
1955 case Instruction::FPTrunc:
1956 return SelectFPTrunc(I);
1957 case Instruction::FPToSI:
1958 return SelectFPToInt(I, /*Signed=*/true);
1959 case Instruction::FPToUI:
1960 return SelectFPToInt(I, /*Signed=*/false);
1961 case Instruction::SIToFP:
1962 return SelectIntToFP(I, /*Signed=*/true);
1963 case Instruction::UIToFP:
1964 return SelectIntToFP(I, /*Signed=*/false);
1965 case Instruction::SRem:
1966 return SelectRem(I, ISD::SREM);
1967 case Instruction::URem:
1968 return SelectRem(I, ISD::UREM);
1969 case Instruction::Call:
1970 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(I))
1971 return SelectIntrinsicCall(*II);
1972 return SelectCall(I);
1973 case Instruction::Ret:
1974 return SelectRet(I);
1975 case Instruction::Trunc:
1976 return SelectTrunc(I);
1977 case Instruction::ZExt:
1978 case Instruction::SExt:
1979 return SelectIntExt(I);
1980 case Instruction::Mul:
1981 // FIXME: This really should be handled by the target-independent selector.
1982 return SelectMul(I);
1985 // Silence warnings.
1986 (void)&CC_AArch64_DarwinPCS_VarArg;
1990 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &funcInfo,
1991 const TargetLibraryInfo *libInfo) {
1992 return new AArch64FastISel(funcInfo, libInfo);