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/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;
92 bool FastLowerCall(CallLoweringInfo &CLI) override;
93 bool FastLowerIntrinsicCall(const IntrinsicInst *II) override;
96 // Selection routines.
97 bool SelectLoad(const Instruction *I);
98 bool SelectStore(const Instruction *I);
99 bool SelectBranch(const Instruction *I);
100 bool SelectIndirectBr(const Instruction *I);
101 bool SelectCmp(const Instruction *I);
102 bool SelectSelect(const Instruction *I);
103 bool SelectFPExt(const Instruction *I);
104 bool SelectFPTrunc(const Instruction *I);
105 bool SelectFPToInt(const Instruction *I, bool Signed);
106 bool SelectIntToFP(const Instruction *I, bool Signed);
107 bool SelectRem(const Instruction *I, unsigned ISDOpcode);
108 bool SelectRet(const Instruction *I);
109 bool SelectTrunc(const Instruction *I);
110 bool SelectIntExt(const Instruction *I);
111 bool SelectMul(const Instruction *I);
112 bool SelectShift(const Instruction *I, bool IsLeftShift, bool IsArithmetic);
114 // Utility helper routines.
115 bool isTypeLegal(Type *Ty, MVT &VT);
116 bool isLoadStoreTypeLegal(Type *Ty, MVT &VT);
117 bool ComputeAddress(const Value *Obj, Address &Addr);
118 bool SimplifyAddress(Address &Addr, MVT VT, int64_t ScaleFactor,
120 void AddLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
121 unsigned Flags, bool UseUnscaled);
122 bool IsMemCpySmall(uint64_t Len, unsigned Alignment);
123 bool TryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
125 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
129 bool EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt);
130 bool EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
131 bool UseUnscaled = false);
132 bool EmitStore(MVT VT, unsigned SrcReg, Address Addr,
133 bool UseUnscaled = false);
134 unsigned EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
135 unsigned Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
136 unsigned Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
137 unsigned Op1, bool Op1IsKill);
138 unsigned Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
139 unsigned Op1, bool Op1IsKill);
140 unsigned Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
141 unsigned Op1, bool Op1IsKill);
142 unsigned Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
143 unsigned Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
144 unsigned Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
146 unsigned AArch64MaterializeFP(const ConstantFP *CFP, MVT VT);
147 unsigned AArch64MaterializeGV(const GlobalValue *GV);
149 // Call handling routines.
151 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
152 bool ProcessCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
154 bool FinishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
157 // Backend specific FastISel code.
158 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
159 unsigned TargetMaterializeConstant(const Constant *C) override;
161 explicit AArch64FastISel(FunctionLoweringInfo &funcInfo,
162 const TargetLibraryInfo *libInfo)
163 : FastISel(funcInfo, libInfo) {
164 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
165 Context = &funcInfo.Fn->getContext();
168 bool TargetSelectInstruction(const Instruction *I) override;
170 #include "AArch64GenFastISel.inc"
173 } // end anonymous namespace
175 #include "AArch64GenCallingConv.inc"
177 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
178 if (CC == CallingConv::WebKit_JS)
179 return CC_AArch64_WebKit_JS;
180 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
183 unsigned AArch64FastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
184 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
185 "Alloca should always return a pointer.");
187 // Don't handle dynamic allocas.
188 if (!FuncInfo.StaticAllocaMap.count(AI))
191 DenseMap<const AllocaInst *, int>::iterator SI =
192 FuncInfo.StaticAllocaMap.find(AI);
194 if (SI != FuncInfo.StaticAllocaMap.end()) {
195 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
196 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
198 .addFrameIndex(SI->second)
207 unsigned AArch64FastISel::AArch64MaterializeFP(const ConstantFP *CFP, MVT VT) {
208 if (VT != MVT::f32 && VT != MVT::f64)
211 const APFloat Val = CFP->getValueAPF();
212 bool is64bit = (VT == MVT::f64);
214 // This checks to see if we can use FMOV instructions to materialize
215 // a constant, otherwise we have to materialize via the constant pool.
216 if (TLI.isFPImmLegal(Val, VT)) {
220 Imm = AArch64_AM::getFP64Imm(Val);
221 Opc = AArch64::FMOVDi;
223 Imm = AArch64_AM::getFP32Imm(Val);
224 Opc = AArch64::FMOVSi;
226 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
227 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
232 // Materialize via constant pool. MachineConstantPool wants an explicit
234 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
236 Align = DL.getTypeAllocSize(CFP->getType());
238 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
239 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
240 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
241 ADRPReg).addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGE);
243 unsigned Opc = is64bit ? AArch64::LDRDui : AArch64::LDRSui;
244 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
247 .addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
251 unsigned AArch64FastISel::AArch64MaterializeGV(const GlobalValue *GV) {
252 // We can't handle thread-local variables quickly yet.
253 if (GV->isThreadLocal())
256 // MachO still uses GOT for large code-model accesses, but ELF requires
257 // movz/movk sequences, which FastISel doesn't handle yet.
258 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
261 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
263 EVT DestEVT = TLI.getValueType(GV->getType(), true);
264 if (!DestEVT.isSimple())
267 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
270 if (OpFlags & AArch64II::MO_GOT) {
272 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
274 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
276 ResultReg = createResultReg(&AArch64::GPR64RegClass);
277 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
280 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
284 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
285 ADRPReg).addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
287 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
288 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
291 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
297 unsigned AArch64FastISel::TargetMaterializeConstant(const Constant *C) {
298 EVT CEVT = TLI.getValueType(C->getType(), true);
300 // Only handle simple types.
301 if (!CEVT.isSimple())
303 MVT VT = CEVT.getSimpleVT();
305 // FIXME: Handle ConstantInt.
306 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
307 return AArch64MaterializeFP(CFP, VT);
308 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
309 return AArch64MaterializeGV(GV);
314 // Computes the address to get to an object.
315 bool AArch64FastISel::ComputeAddress(const Value *Obj, Address &Addr) {
316 const User *U = nullptr;
317 unsigned Opcode = Instruction::UserOp1;
318 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
319 // Don't walk into other basic blocks unless the object is an alloca from
320 // another block, otherwise it may not have a virtual register assigned.
321 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
322 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
323 Opcode = I->getOpcode();
326 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
327 Opcode = C->getOpcode();
331 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
332 if (Ty->getAddressSpace() > 255)
333 // Fast instruction selection doesn't support the special
340 case Instruction::BitCast: {
341 // Look through bitcasts.
342 return ComputeAddress(U->getOperand(0), Addr);
344 case Instruction::IntToPtr: {
345 // Look past no-op inttoptrs.
346 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
347 return ComputeAddress(U->getOperand(0), Addr);
350 case Instruction::PtrToInt: {
351 // Look past no-op ptrtoints.
352 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
353 return ComputeAddress(U->getOperand(0), Addr);
356 case Instruction::GetElementPtr: {
357 Address SavedAddr = Addr;
358 uint64_t TmpOffset = Addr.getOffset();
360 // Iterate through the GEP folding the constants into offsets where
362 gep_type_iterator GTI = gep_type_begin(U);
363 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
365 const Value *Op = *i;
366 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
367 const StructLayout *SL = DL.getStructLayout(STy);
368 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
369 TmpOffset += SL->getElementOffset(Idx);
371 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
373 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
374 // Constant-offset addressing.
375 TmpOffset += CI->getSExtValue() * S;
378 if (canFoldAddIntoGEP(U, Op)) {
379 // A compatible add with a constant operand. Fold the constant.
381 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
382 TmpOffset += CI->getSExtValue() * S;
383 // Iterate on the other operand.
384 Op = cast<AddOperator>(Op)->getOperand(0);
388 goto unsupported_gep;
393 // Try to grab the base operand now.
394 Addr.setOffset(TmpOffset);
395 if (ComputeAddress(U->getOperand(0), Addr))
398 // We failed, restore everything and try the other options.
404 case Instruction::Alloca: {
405 const AllocaInst *AI = cast<AllocaInst>(Obj);
406 DenseMap<const AllocaInst *, int>::iterator SI =
407 FuncInfo.StaticAllocaMap.find(AI);
408 if (SI != FuncInfo.StaticAllocaMap.end()) {
409 Addr.setKind(Address::FrameIndexBase);
410 Addr.setFI(SI->second);
417 // Try to get this in a register if nothing else has worked.
419 Addr.setReg(getRegForValue(Obj));
420 return Addr.isValid();
423 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
424 EVT evt = TLI.getValueType(Ty, true);
426 // Only handle simple types.
427 if (evt == MVT::Other || !evt.isSimple())
429 VT = evt.getSimpleVT();
431 // This is a legal type, but it's not something we handle in fast-isel.
435 // Handle all other legal types, i.e. a register that will directly hold this
437 return TLI.isTypeLegal(VT);
440 bool AArch64FastISel::isLoadStoreTypeLegal(Type *Ty, MVT &VT) {
441 if (isTypeLegal(Ty, VT))
444 // If this is a type than can be sign or zero-extended to a basic operation
445 // go ahead and accept it now. For stores, this reflects truncation.
446 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
452 bool AArch64FastISel::SimplifyAddress(Address &Addr, MVT VT,
453 int64_t ScaleFactor, bool UseUnscaled) {
454 bool needsLowering = false;
455 int64_t Offset = Addr.getOffset();
456 switch (VT.SimpleTy) {
467 // Using scaled, 12-bit, unsigned immediate offsets.
468 needsLowering = ((Offset & 0xfff) != Offset);
470 // Using unscaled, 9-bit, signed immediate offsets.
471 needsLowering = (Offset > 256 || Offset < -256);
475 //If this is a stack pointer and the offset needs to be simplified then put
476 // the alloca address into a register, set the base type back to register and
477 // continue. This should almost never happen.
478 if (needsLowering && Addr.getKind() == Address::FrameIndexBase) {
479 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
480 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
482 .addFrameIndex(Addr.getFI())
485 Addr.setKind(Address::RegBase);
486 Addr.setReg(ResultReg);
489 // Since the offset is too large for the load/store instruction get the
490 // reg+offset into a register.
492 uint64_t UnscaledOffset = Addr.getOffset() * ScaleFactor;
493 unsigned ResultReg = FastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(), false,
494 UnscaledOffset, MVT::i64);
497 Addr.setReg(ResultReg);
503 void AArch64FastISel::AddLoadStoreOperands(Address &Addr,
504 const MachineInstrBuilder &MIB,
505 unsigned Flags, bool UseUnscaled) {
506 int64_t Offset = Addr.getOffset();
507 // Frame base works a bit differently. Handle it separately.
508 if (Addr.getKind() == Address::FrameIndexBase) {
509 int FI = Addr.getFI();
510 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
511 // and alignment should be based on the VT.
512 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
513 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
514 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
515 // Now add the rest of the operands.
516 MIB.addFrameIndex(FI).addImm(Offset).addMemOperand(MMO);
518 // Now add the rest of the operands.
519 MIB.addReg(Addr.getReg());
524 bool AArch64FastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
526 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
527 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
528 if (!UseUnscaled && Addr.getOffset() < 0)
532 const TargetRegisterClass *RC;
534 int64_t ScaleFactor = 0;
535 switch (VT.SimpleTy) {
540 // Intentional fall-through.
542 Opc = UseUnscaled ? AArch64::LDURBBi : AArch64::LDRBBui;
543 RC = &AArch64::GPR32RegClass;
547 Opc = UseUnscaled ? AArch64::LDURHHi : AArch64::LDRHHui;
548 RC = &AArch64::GPR32RegClass;
552 Opc = UseUnscaled ? AArch64::LDURWi : AArch64::LDRWui;
553 RC = &AArch64::GPR32RegClass;
557 Opc = UseUnscaled ? AArch64::LDURXi : AArch64::LDRXui;
558 RC = &AArch64::GPR64RegClass;
562 Opc = UseUnscaled ? AArch64::LDURSi : AArch64::LDRSui;
563 RC = TLI.getRegClassFor(VT);
567 Opc = UseUnscaled ? AArch64::LDURDi : AArch64::LDRDui;
568 RC = TLI.getRegClassFor(VT);
574 int64_t Offset = Addr.getOffset();
575 if (Offset & (ScaleFactor - 1))
576 // Retry using an unscaled, 9-bit, signed immediate offset.
577 return EmitLoad(VT, ResultReg, Addr, /*UseUnscaled*/ true);
579 Addr.setOffset(Offset / ScaleFactor);
582 // Simplify this down to something we can handle.
583 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled))
586 // Create the base instruction, then add the operands.
587 ResultReg = createResultReg(RC);
588 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
589 TII.get(Opc), ResultReg);
590 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, UseUnscaled);
592 // Loading an i1 requires special handling.
594 MRI.constrainRegClass(ResultReg, &AArch64::GPR32RegClass);
595 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
596 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
599 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
605 bool AArch64FastISel::SelectLoad(const Instruction *I) {
607 // Verify we have a legal type before going any further. Currently, we handle
608 // simple types that will directly fit in a register (i32/f32/i64/f64) or
609 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
610 if (!isLoadStoreTypeLegal(I->getType(), VT) || cast<LoadInst>(I)->isAtomic())
613 // See if we can handle this address.
615 if (!ComputeAddress(I->getOperand(0), Addr))
619 if (!EmitLoad(VT, ResultReg, Addr))
622 UpdateValueMap(I, ResultReg);
626 bool AArch64FastISel::EmitStore(MVT VT, unsigned SrcReg, Address Addr,
628 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
629 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
630 if (!UseUnscaled && Addr.getOffset() < 0)
635 int64_t ScaleFactor = 0;
636 // Using scaled, 12-bit, unsigned immediate offsets.
637 switch (VT.SimpleTy) {
643 StrOpc = UseUnscaled ? AArch64::STURBBi : AArch64::STRBBui;
647 StrOpc = UseUnscaled ? AArch64::STURHHi : AArch64::STRHHui;
651 StrOpc = UseUnscaled ? AArch64::STURWi : AArch64::STRWui;
655 StrOpc = UseUnscaled ? AArch64::STURXi : AArch64::STRXui;
659 StrOpc = UseUnscaled ? AArch64::STURSi : AArch64::STRSui;
663 StrOpc = UseUnscaled ? AArch64::STURDi : AArch64::STRDui;
669 int64_t Offset = Addr.getOffset();
670 if (Offset & (ScaleFactor - 1))
671 // Retry using an unscaled, 9-bit, signed immediate offset.
672 return EmitStore(VT, SrcReg, Addr, /*UseUnscaled*/ true);
674 Addr.setOffset(Offset / ScaleFactor);
677 // Simplify this down to something we can handle.
678 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled))
681 // Storing an i1 requires special handling.
683 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
684 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
685 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
688 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
691 // Create the base instruction, then add the operands.
692 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
693 TII.get(StrOpc)).addReg(SrcReg);
694 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, UseUnscaled);
698 bool AArch64FastISel::SelectStore(const Instruction *I) {
700 Value *Op0 = I->getOperand(0);
701 // Verify we have a legal type before going any further. Currently, we handle
702 // simple types that will directly fit in a register (i32/f32/i64/f64) or
703 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
704 if (!isLoadStoreTypeLegal(Op0->getType(), VT) ||
705 cast<StoreInst>(I)->isAtomic())
708 // Get the value to be stored into a register.
709 unsigned SrcReg = getRegForValue(Op0);
713 // See if we can handle this address.
715 if (!ComputeAddress(I->getOperand(1), Addr))
718 if (!EmitStore(VT, SrcReg, Addr))
723 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
725 case CmpInst::FCMP_ONE:
726 case CmpInst::FCMP_UEQ:
728 // AL is our "false" for now. The other two need more compares.
729 return AArch64CC::AL;
730 case CmpInst::ICMP_EQ:
731 case CmpInst::FCMP_OEQ:
732 return AArch64CC::EQ;
733 case CmpInst::ICMP_SGT:
734 case CmpInst::FCMP_OGT:
735 return AArch64CC::GT;
736 case CmpInst::ICMP_SGE:
737 case CmpInst::FCMP_OGE:
738 return AArch64CC::GE;
739 case CmpInst::ICMP_UGT:
740 case CmpInst::FCMP_UGT:
741 return AArch64CC::HI;
742 case CmpInst::FCMP_OLT:
743 return AArch64CC::MI;
744 case CmpInst::ICMP_ULE:
745 case CmpInst::FCMP_OLE:
746 return AArch64CC::LS;
747 case CmpInst::FCMP_ORD:
748 return AArch64CC::VC;
749 case CmpInst::FCMP_UNO:
750 return AArch64CC::VS;
751 case CmpInst::FCMP_UGE:
752 return AArch64CC::PL;
753 case CmpInst::ICMP_SLT:
754 case CmpInst::FCMP_ULT:
755 return AArch64CC::LT;
756 case CmpInst::ICMP_SLE:
757 case CmpInst::FCMP_ULE:
758 return AArch64CC::LE;
759 case CmpInst::FCMP_UNE:
760 case CmpInst::ICMP_NE:
761 return AArch64CC::NE;
762 case CmpInst::ICMP_UGE:
763 return AArch64CC::HS;
764 case CmpInst::ICMP_ULT:
765 return AArch64CC::LO;
769 bool AArch64FastISel::SelectBranch(const Instruction *I) {
770 const BranchInst *BI = cast<BranchInst>(I);
771 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
772 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
774 AArch64CC::CondCode CC = AArch64CC::NE;
775 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
776 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
777 // We may not handle every CC for now.
778 CC = getCompareCC(CI->getPredicate());
779 if (CC == AArch64CC::AL)
783 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
787 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
790 FuncInfo.MBB->addSuccessor(TBB);
792 FastEmitBranch(FBB, DbgLoc);
795 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
797 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
798 (isLoadStoreTypeLegal(TI->getOperand(0)->getType(), SrcVT))) {
799 unsigned CondReg = getRegForValue(TI->getOperand(0));
803 // Issue an extract_subreg to get the lower 32-bits.
804 if (SrcVT == MVT::i64)
805 CondReg = FastEmitInst_extractsubreg(MVT::i32, CondReg, /*Kill=*/true,
808 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
809 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
810 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
811 TII.get(AArch64::ANDWri), ANDReg)
813 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
814 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
815 TII.get(AArch64::SUBSWri))
821 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
825 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
828 FuncInfo.MBB->addSuccessor(TBB);
829 FastEmitBranch(FBB, DbgLoc);
832 } else if (const ConstantInt *CI =
833 dyn_cast<ConstantInt>(BI->getCondition())) {
834 uint64_t Imm = CI->getZExtValue();
835 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
836 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
838 FuncInfo.MBB->addSuccessor(Target);
840 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
841 // Fake request the condition, otherwise the intrinsic might be completely
843 unsigned CondReg = getRegForValue(BI->getCondition());
848 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
851 FuncInfo.MBB->addSuccessor(TBB);
853 FastEmitBranch(FBB, DbgLoc);
857 unsigned CondReg = getRegForValue(BI->getCondition());
861 // We've been divorced from our compare! Our block was split, and
862 // now our compare lives in a predecessor block. We musn't
863 // re-compare here, as the children of the compare aren't guaranteed
864 // live across the block boundary (we *could* check for this).
865 // Regardless, the compare has been done in the predecessor block,
866 // and it left a value for us in a virtual register. Ergo, we test
867 // the one-bit value left in the virtual register.
868 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri),
874 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
879 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
882 FuncInfo.MBB->addSuccessor(TBB);
883 FastEmitBranch(FBB, DbgLoc);
887 bool AArch64FastISel::SelectIndirectBr(const Instruction *I) {
888 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
889 unsigned AddrReg = getRegForValue(BI->getOperand(0));
893 // Emit the indirect branch.
894 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BR))
897 // Make sure the CFG is up-to-date.
898 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
899 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
904 bool AArch64FastISel::EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt) {
905 Type *Ty = Src1Value->getType();
906 EVT SrcEVT = TLI.getValueType(Ty, true);
907 if (!SrcEVT.isSimple())
909 MVT SrcVT = SrcEVT.getSimpleVT();
911 // Check to see if the 2nd operand is a constant that we can encode directly
915 bool isNegativeImm = false;
916 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
917 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
918 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
919 const APInt &CIVal = ConstInt->getValue();
921 Imm = (isZExt) ? CIVal.getZExtValue() : CIVal.getSExtValue();
922 if (CIVal.isNegative()) {
923 isNegativeImm = true;
926 // FIXME: We can handle more immediates using shifts.
927 UseImm = ((Imm & 0xfff) == Imm);
929 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
930 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
931 if (ConstFP->isZero() && !ConstFP->isNegative())
938 bool needsExt = false;
939 switch (SrcVT.SimpleTy) {
946 // Intentional fall-through.
950 CmpOpc = isNegativeImm ? AArch64::ADDSWri : AArch64::SUBSWri;
952 CmpOpc = AArch64::SUBSWrr;
957 CmpOpc = isNegativeImm ? AArch64::ADDSXri : AArch64::SUBSXri;
959 CmpOpc = AArch64::SUBSXrr;
963 CmpOpc = UseImm ? AArch64::FCMPSri : AArch64::FCMPSrr;
967 CmpOpc = UseImm ? AArch64::FCMPDri : AArch64::FCMPDrr;
971 unsigned SrcReg1 = getRegForValue(Src1Value);
977 SrcReg2 = getRegForValue(Src2Value);
982 // We have i1, i8, or i16, we need to either zero extend or sign extend.
984 SrcReg1 = EmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
988 SrcReg2 = EmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
996 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1002 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1008 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1011 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1018 bool AArch64FastISel::SelectCmp(const Instruction *I) {
1019 const CmpInst *CI = cast<CmpInst>(I);
1021 // We may not handle every CC for now.
1022 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate());
1023 if (CC == AArch64CC::AL)
1027 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1030 // Now set a register based on the comparison.
1031 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
1032 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1033 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1035 .addReg(AArch64::WZR)
1036 .addReg(AArch64::WZR)
1037 .addImm(invertedCC);
1039 UpdateValueMap(I, ResultReg);
1043 bool AArch64FastISel::SelectSelect(const Instruction *I) {
1044 const SelectInst *SI = cast<SelectInst>(I);
1046 EVT DestEVT = TLI.getValueType(SI->getType(), true);
1047 if (!DestEVT.isSimple())
1050 MVT DestVT = DestEVT.getSimpleVT();
1051 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
1056 switch (DestVT.SimpleTy) {
1057 default: return false;
1058 case MVT::i32: SelectOpc = AArch64::CSELWr; break;
1059 case MVT::i64: SelectOpc = AArch64::CSELXr; break;
1060 case MVT::f32: SelectOpc = AArch64::FCSELSrrr; break;
1061 case MVT::f64: SelectOpc = AArch64::FCSELDrrr; break;
1064 const Value *Cond = SI->getCondition();
1065 bool NeedTest = true;
1066 AArch64CC::CondCode CC = AArch64CC::NE;
1067 if (foldXALUIntrinsic(CC, I, Cond))
1070 unsigned CondReg = getRegForValue(Cond);
1073 bool CondIsKill = hasTrivialKill(Cond);
1076 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
1077 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1078 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1080 .addReg(CondReg, getKillRegState(CondIsKill))
1081 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1083 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri))
1090 unsigned TrueReg = getRegForValue(SI->getTrueValue());
1091 bool TrueIsKill = hasTrivialKill(SI->getTrueValue());
1093 unsigned FalseReg = getRegForValue(SI->getFalseValue());
1094 bool FalseIsKill = hasTrivialKill(SI->getFalseValue());
1096 if (!TrueReg || !FalseReg)
1099 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1100 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SelectOpc),
1102 .addReg(TrueReg, getKillRegState(TrueIsKill))
1103 .addReg(FalseReg, getKillRegState(FalseIsKill))
1106 UpdateValueMap(I, ResultReg);
1110 bool AArch64FastISel::SelectFPExt(const Instruction *I) {
1111 Value *V = I->getOperand(0);
1112 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
1115 unsigned Op = getRegForValue(V);
1119 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
1120 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
1121 ResultReg).addReg(Op);
1122 UpdateValueMap(I, ResultReg);
1126 bool AArch64FastISel::SelectFPTrunc(const Instruction *I) {
1127 Value *V = I->getOperand(0);
1128 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
1131 unsigned Op = getRegForValue(V);
1135 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
1136 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
1137 ResultReg).addReg(Op);
1138 UpdateValueMap(I, ResultReg);
1142 // FPToUI and FPToSI
1143 bool AArch64FastISel::SelectFPToInt(const Instruction *I, bool Signed) {
1145 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1148 unsigned SrcReg = getRegForValue(I->getOperand(0));
1152 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1153 if (SrcVT == MVT::f128)
1157 if (SrcVT == MVT::f64) {
1159 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
1161 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
1164 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
1166 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
1168 unsigned ResultReg = createResultReg(
1169 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
1170 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1172 UpdateValueMap(I, ResultReg);
1176 bool AArch64FastISel::SelectIntToFP(const Instruction *I, bool Signed) {
1178 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1180 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
1181 "Unexpected value type.");
1183 unsigned SrcReg = getRegForValue(I->getOperand(0));
1187 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1189 // Handle sign-extension.
1190 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
1192 EmitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
1197 MRI.constrainRegClass(SrcReg, SrcVT == MVT::i64 ? &AArch64::GPR64RegClass
1198 : &AArch64::GPR32RegClass);
1201 if (SrcVT == MVT::i64) {
1203 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
1205 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
1208 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
1210 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
1213 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1216 UpdateValueMap(I, ResultReg);
1220 bool AArch64FastISel::ProcessCallArgs(CallLoweringInfo &CLI,
1221 SmallVectorImpl<MVT> &OutVTs,
1222 unsigned &NumBytes) {
1223 CallingConv::ID CC = CLI.CallConv;
1224 SmallVector<CCValAssign, 16> ArgLocs;
1225 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
1226 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1228 // Get a count of how many bytes are to be pushed on the stack.
1229 NumBytes = CCInfo.getNextStackOffset();
1231 // Issue CALLSEQ_START
1232 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1233 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
1236 // Process the args.
1237 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1238 CCValAssign &VA = ArgLocs[i];
1239 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1240 MVT ArgVT = OutVTs[VA.getValNo()];
1242 unsigned ArgReg = getRegForValue(ArgVal);
1246 // Handle arg promotion: SExt, ZExt, AExt.
1247 switch (VA.getLocInfo()) {
1248 case CCValAssign::Full:
1250 case CCValAssign::SExt: {
1251 MVT DestVT = VA.getLocVT();
1253 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1258 case CCValAssign::AExt:
1259 // Intentional fall-through.
1260 case CCValAssign::ZExt: {
1261 MVT DestVT = VA.getLocVT();
1263 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1269 llvm_unreachable("Unknown arg promotion!");
1272 // Now copy/store arg to correct locations.
1273 if (VA.isRegLoc() && !VA.needsCustom()) {
1274 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1275 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1276 CLI.OutRegs.push_back(VA.getLocReg());
1277 } else if (VA.needsCustom()) {
1278 // FIXME: Handle custom args.
1281 assert(VA.isMemLoc() && "Assuming store on stack.");
1283 // Need to store on the stack.
1284 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
1286 unsigned BEAlign = 0;
1287 if (ArgSize < 8 && !Subtarget->isLittleEndian())
1288 BEAlign = 8 - ArgSize;
1291 Addr.setKind(Address::RegBase);
1292 Addr.setReg(AArch64::SP);
1293 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1295 if (!EmitStore(ArgVT, ArgReg, Addr))
1302 bool AArch64FastISel::FinishCall(CallLoweringInfo &CLI, MVT RetVT,
1303 unsigned NumBytes) {
1304 CallingConv::ID CC = CLI.CallConv;
1306 // Issue CALLSEQ_END
1307 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1308 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
1309 .addImm(NumBytes).addImm(0);
1311 // Now the return value.
1312 if (RetVT != MVT::isVoid) {
1313 SmallVector<CCValAssign, 16> RVLocs;
1314 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
1315 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
1317 // Only handle a single return value.
1318 if (RVLocs.size() != 1)
1321 // Copy all of the result registers out of their specified physreg.
1322 MVT CopyVT = RVLocs[0].getValVT();
1323 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1324 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1325 TII.get(TargetOpcode::COPY), ResultReg)
1326 .addReg(RVLocs[0].getLocReg());
1327 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1329 CLI.ResultReg = ResultReg;
1330 CLI.NumResultRegs = 1;
1336 bool AArch64FastISel::FastLowerCall(CallLoweringInfo &CLI) {
1337 CallingConv::ID CC = CLI.CallConv;
1338 bool IsVarArg = CLI.IsVarArg;
1339 const Value *Callee = CLI.Callee;
1340 const char *SymName = CLI.SymName;
1342 // Only handle global variable Callees.
1343 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1347 // Let SDISel handle vararg functions.
1351 // FIXME: Only handle *simple* calls for now.
1353 if (CLI.RetTy->isVoidTy())
1354 RetVT = MVT::isVoid;
1355 else if (!isTypeLegal(CLI.RetTy, RetVT))
1358 for (auto Flag : CLI.OutFlags)
1359 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1362 // Set up the argument vectors.
1363 SmallVector<MVT, 16> OutVTs;
1364 OutVTs.reserve(CLI.OutVals.size());
1366 for (auto *Val : CLI.OutVals) {
1368 if (!isTypeLegal(Val->getType(), VT) &&
1369 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1372 // We don't handle vector parameters yet.
1373 if (VT.isVector() || VT.getSizeInBits() > 64)
1376 OutVTs.push_back(VT);
1379 // Handle the arguments now that we've gotten them.
1381 if (!ProcessCallArgs(CLI, OutVTs, NumBytes))
1385 MachineInstrBuilder MIB;
1386 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BL));
1389 MIB.addGlobalAddress(GV, 0, 0);
1391 MIB.addExternalSymbol(SymName, 0);
1393 // Add implicit physical register uses to the call.
1394 for (auto Reg : CLI.OutRegs)
1395 MIB.addReg(Reg, RegState::Implicit);
1397 // Add a register mask with the call-preserved registers.
1398 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1399 MIB.addRegMask(TRI.getCallPreservedMask(CC));
1401 // Finish off the call including any return values.
1402 return FinishCall(CLI, RetVT, NumBytes);
1405 bool AArch64FastISel::IsMemCpySmall(uint64_t Len, unsigned Alignment) {
1407 return Len / Alignment <= 4;
1412 bool AArch64FastISel::TryEmitSmallMemCpy(Address Dest, Address Src,
1413 uint64_t Len, unsigned Alignment) {
1414 // Make sure we don't bloat code by inlining very large memcpy's.
1415 if (!IsMemCpySmall(Len, Alignment))
1418 int64_t UnscaledOffset = 0;
1419 Address OrigDest = Dest;
1420 Address OrigSrc = Src;
1424 if (!Alignment || Alignment >= 8) {
1435 // Bound based on alignment.
1436 if (Len >= 4 && Alignment == 4)
1438 else if (Len >= 2 && Alignment == 2)
1447 RV = EmitLoad(VT, ResultReg, Src);
1451 RV = EmitStore(VT, ResultReg, Dest);
1455 int64_t Size = VT.getSizeInBits() / 8;
1457 UnscaledOffset += Size;
1459 // We need to recompute the unscaled offset for each iteration.
1460 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
1461 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
1467 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
1468 /// into the user. The condition code will only be updated on success.
1469 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
1470 const Instruction *I,
1471 const Value *Cond) {
1472 if (!isa<ExtractValueInst>(Cond))
1475 const auto *EV = cast<ExtractValueInst>(Cond);
1476 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
1479 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
1481 const Function *Callee = II->getCalledFunction();
1483 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
1484 if (!isTypeLegal(RetTy, RetVT))
1487 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1490 AArch64CC::CondCode TmpCC;
1491 switch (II->getIntrinsicID()) {
1492 default: return false;
1493 case Intrinsic::sadd_with_overflow:
1494 case Intrinsic::ssub_with_overflow: TmpCC = AArch64CC::VS; break;
1495 case Intrinsic::uadd_with_overflow: TmpCC = AArch64CC::HS; break;
1496 case Intrinsic::usub_with_overflow: TmpCC = AArch64CC::LO; break;
1497 case Intrinsic::smul_with_overflow:
1498 case Intrinsic::umul_with_overflow: TmpCC = AArch64CC::NE; break;
1501 // Check if both instructions are in the same basic block.
1502 if (II->getParent() != I->getParent())
1505 // Make sure nothing is in the way
1506 BasicBlock::const_iterator Start = I;
1507 BasicBlock::const_iterator End = II;
1508 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
1509 // We only expect extractvalue instructions between the intrinsic and the
1510 // instruction to be selected.
1511 if (!isa<ExtractValueInst>(Itr))
1514 // Check that the extractvalue operand comes from the intrinsic.
1515 const auto *EVI = cast<ExtractValueInst>(Itr);
1516 if (EVI->getAggregateOperand() != II)
1524 bool AArch64FastISel::FastLowerIntrinsicCall(const IntrinsicInst *II) {
1525 // FIXME: Handle more intrinsics.
1526 switch (II->getIntrinsicID()) {
1527 default: return false;
1528 case Intrinsic::frameaddress: {
1529 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
1530 MFI->setFrameAddressIsTaken(true);
1532 const AArch64RegisterInfo *RegInfo =
1533 static_cast<const AArch64RegisterInfo *>(TM.getRegisterInfo());
1534 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
1535 unsigned SrcReg = FramePtr;
1537 // Recursively load frame address
1543 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
1545 DestReg = createResultReg(&AArch64::GPR64RegClass);
1546 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1547 TII.get(AArch64::LDRXui), DestReg)
1548 .addReg(SrcReg).addImm(0);
1552 UpdateValueMap(II, SrcReg);
1555 case Intrinsic::memcpy:
1556 case Intrinsic::memmove: {
1557 const auto *MTI = cast<MemTransferInst>(II);
1558 // Don't handle volatile.
1559 if (MTI->isVolatile())
1562 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
1563 // we would emit dead code because we don't currently handle memmoves.
1564 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
1565 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
1566 // Small memcpy's are common enough that we want to do them without a call
1568 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
1569 unsigned Alignment = MTI->getAlignment();
1570 if (IsMemCpySmall(Len, Alignment)) {
1572 if (!ComputeAddress(MTI->getRawDest(), Dest) ||
1573 !ComputeAddress(MTI->getRawSource(), Src))
1575 if (TryEmitSmallMemCpy(Dest, Src, Len, Alignment))
1580 if (!MTI->getLength()->getType()->isIntegerTy(64))
1583 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
1584 // Fast instruction selection doesn't support the special
1588 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
1589 return LowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
1591 case Intrinsic::memset: {
1592 const MemSetInst *MSI = cast<MemSetInst>(II);
1593 // Don't handle volatile.
1594 if (MSI->isVolatile())
1597 if (!MSI->getLength()->getType()->isIntegerTy(64))
1600 if (MSI->getDestAddressSpace() > 255)
1601 // Fast instruction selection doesn't support the special
1605 return LowerCallTo(II, "memset", II->getNumArgOperands() - 2);
1607 case Intrinsic::trap: {
1608 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
1612 case Intrinsic::sadd_with_overflow:
1613 case Intrinsic::uadd_with_overflow:
1614 case Intrinsic::ssub_with_overflow:
1615 case Intrinsic::usub_with_overflow:
1616 case Intrinsic::smul_with_overflow:
1617 case Intrinsic::umul_with_overflow: {
1618 // This implements the basic lowering of the xalu with overflow intrinsics.
1619 const Function *Callee = II->getCalledFunction();
1620 auto *Ty = cast<StructType>(Callee->getReturnType());
1621 Type *RetTy = Ty->getTypeAtIndex(0U);
1622 Type *CondTy = Ty->getTypeAtIndex(1);
1625 if (!isTypeLegal(RetTy, VT))
1628 if (VT != MVT::i32 && VT != MVT::i64)
1631 const Value *LHS = II->getArgOperand(0);
1632 const Value *RHS = II->getArgOperand(1);
1633 // Canonicalize immediate to the RHS.
1634 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
1635 isCommutativeIntrinsic(II))
1636 std::swap(LHS, RHS);
1638 unsigned LHSReg = getRegForValue(LHS);
1641 bool LHSIsKill = hasTrivialKill(LHS);
1643 unsigned RHSReg = 0;
1644 bool RHSIsKill = false;
1646 if (!isa<ConstantInt>(RHS)) {
1647 RHSReg = getRegForValue(RHS);
1650 RHSIsKill = hasTrivialKill(RHS);
1655 unsigned MulReg = 0;
1656 AArch64CC::CondCode CC = AArch64CC::Invalid;
1657 bool Is64Bit = VT == MVT::i64;
1658 switch (II->getIntrinsicID()) {
1659 default: llvm_unreachable("Unexpected intrinsic!");
1660 case Intrinsic::sadd_with_overflow:
1662 Opc = Is64Bit ? AArch64::ADDSXri : AArch64::ADDSWri;
1664 Opc = Is64Bit ? AArch64::ADDSXrr : AArch64::ADDSWrr;
1667 case Intrinsic::uadd_with_overflow:
1669 Opc = Is64Bit ? AArch64::ADDSXri : AArch64::ADDSWri;
1671 Opc = Is64Bit ? AArch64::ADDSXrr : AArch64::ADDSWrr;
1674 case Intrinsic::ssub_with_overflow:
1676 Opc = Is64Bit ? AArch64::SUBSXri : AArch64::SUBSWri;
1678 Opc = Is64Bit ? AArch64::SUBSXrr : AArch64::SUBSWrr;
1681 case Intrinsic::usub_with_overflow:
1683 Opc = Is64Bit ? AArch64::SUBSXri : AArch64::SUBSWri;
1685 Opc = Is64Bit ? AArch64::SUBSXrr : AArch64::SUBSWrr;
1688 case Intrinsic::smul_with_overflow: {
1691 RHSReg = getRegForValue(RHS);
1694 RHSIsKill = hasTrivialKill(RHS);
1696 if (VT == MVT::i32) {
1697 MulReg = Emit_SMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1698 unsigned ShiftReg = Emit_LSR_ri(MVT::i64, MulReg, false, 32);
1699 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
1701 ShiftReg = FastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
1703 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT));
1704 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1705 TII.get(AArch64::SUBSWrs), CmpReg)
1706 .addReg(ShiftReg, getKillRegState(true))
1707 .addReg(MulReg, getKillRegState(false))
1708 .addImm(159); // 159 <-> asr #31
1710 assert(VT == MVT::i64 && "Unexpected value type.");
1711 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1712 unsigned SMULHReg = FastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
1714 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT));
1715 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1716 TII.get(AArch64::SUBSXrs), CmpReg)
1717 .addReg(SMULHReg, getKillRegState(true))
1718 .addReg(MulReg, getKillRegState(false))
1719 .addImm(191); // 191 <-> asr #63
1723 case Intrinsic::umul_with_overflow: {
1726 RHSReg = getRegForValue(RHS);
1729 RHSIsKill = hasTrivialKill(RHS);
1731 if (VT == MVT::i32) {
1732 MulReg = Emit_UMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1733 unsigned CmpReg = createResultReg(TLI.getRegClassFor(MVT::i64));
1734 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1735 TII.get(AArch64::SUBSXrs), CmpReg)
1736 .addReg(AArch64::XZR, getKillRegState(true))
1737 .addReg(MulReg, getKillRegState(false))
1738 .addImm(96); // 96 <-> lsr #32
1739 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
1742 assert(VT == MVT::i64 && "Unexpected value type.");
1743 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1744 unsigned UMULHReg = FastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
1746 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT));
1747 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1748 TII.get(AArch64::SUBSXrr), CmpReg)
1749 .addReg(AArch64::XZR, getKillRegState(true))
1750 .addReg(UMULHReg, getKillRegState(false));
1756 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1758 MachineInstrBuilder MIB;
1759 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1761 .addReg(LHSReg, getKillRegState(LHSIsKill));
1763 MIB.addImm(cast<ConstantInt>(RHS)->getZExtValue());
1765 MIB.addReg(RHSReg, getKillRegState(RHSIsKill));
1768 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1769 TII.get(TargetOpcode::COPY), ResultReg)
1772 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
1773 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
1774 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1776 .addReg(AArch64::WZR, getKillRegState(true))
1777 .addReg(AArch64::WZR, getKillRegState(true))
1778 .addImm(getInvertedCondCode(CC));
1780 UpdateValueMap(II, ResultReg, 2);
1787 bool AArch64FastISel::SelectRet(const Instruction *I) {
1788 const ReturnInst *Ret = cast<ReturnInst>(I);
1789 const Function &F = *I->getParent()->getParent();
1791 if (!FuncInfo.CanLowerReturn)
1797 // Build a list of return value registers.
1798 SmallVector<unsigned, 4> RetRegs;
1800 if (Ret->getNumOperands() > 0) {
1801 CallingConv::ID CC = F.getCallingConv();
1802 SmallVector<ISD::OutputArg, 4> Outs;
1803 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1805 // Analyze operands of the call, assigning locations to each operand.
1806 SmallVector<CCValAssign, 16> ValLocs;
1807 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
1809 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
1810 : RetCC_AArch64_AAPCS;
1811 CCInfo.AnalyzeReturn(Outs, RetCC);
1813 // Only handle a single return value for now.
1814 if (ValLocs.size() != 1)
1817 CCValAssign &VA = ValLocs[0];
1818 const Value *RV = Ret->getOperand(0);
1820 // Don't bother handling odd stuff for now.
1821 if (VA.getLocInfo() != CCValAssign::Full)
1823 // Only handle register returns for now.
1826 unsigned Reg = getRegForValue(RV);
1830 unsigned SrcReg = Reg + VA.getValNo();
1831 unsigned DestReg = VA.getLocReg();
1832 // Avoid a cross-class copy. This is very unlikely.
1833 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1836 EVT RVEVT = TLI.getValueType(RV->getType());
1837 if (!RVEVT.isSimple())
1840 // Vectors (of > 1 lane) in big endian need tricky handling.
1841 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1)
1844 MVT RVVT = RVEVT.getSimpleVT();
1845 if (RVVT == MVT::f128)
1847 MVT DestVT = VA.getValVT();
1848 // Special handling for extended integers.
1849 if (RVVT != DestVT) {
1850 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1853 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1856 bool isZExt = Outs[0].Flags.isZExt();
1857 SrcReg = EmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1863 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1864 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1866 // Add register to return instruction.
1867 RetRegs.push_back(VA.getLocReg());
1870 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1871 TII.get(AArch64::RET_ReallyLR));
1872 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1873 MIB.addReg(RetRegs[i], RegState::Implicit);
1877 bool AArch64FastISel::SelectTrunc(const Instruction *I) {
1878 Type *DestTy = I->getType();
1879 Value *Op = I->getOperand(0);
1880 Type *SrcTy = Op->getType();
1882 EVT SrcEVT = TLI.getValueType(SrcTy, true);
1883 EVT DestEVT = TLI.getValueType(DestTy, true);
1884 if (!SrcEVT.isSimple())
1886 if (!DestEVT.isSimple())
1889 MVT SrcVT = SrcEVT.getSimpleVT();
1890 MVT DestVT = DestEVT.getSimpleVT();
1892 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
1895 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
1899 unsigned SrcReg = getRegForValue(Op);
1903 // If we're truncating from i64 to a smaller non-legal type then generate an
1904 // AND. Otherwise, we know the high bits are undefined and a truncate doesn't
1905 // generate any code.
1906 if (SrcVT == MVT::i64) {
1908 switch (DestVT.SimpleTy) {
1910 // Trunc i64 to i32 is handled by the target-independent fast-isel.
1922 // Issue an extract_subreg to get the lower 32-bits.
1923 unsigned Reg32 = FastEmitInst_extractsubreg(MVT::i32, SrcReg, /*Kill=*/true,
1925 MRI.constrainRegClass(Reg32, &AArch64::GPR32RegClass);
1926 // Create the AND instruction which performs the actual truncation.
1927 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1928 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1931 .addImm(AArch64_AM::encodeLogicalImmediate(Mask, 32));
1935 UpdateValueMap(I, SrcReg);
1939 unsigned AArch64FastISel::Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt) {
1940 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
1941 DestVT == MVT::i64) &&
1942 "Unexpected value type.");
1943 // Handle i8 and i16 as i32.
1944 if (DestVT == MVT::i8 || DestVT == MVT::i16)
1948 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
1949 unsigned ResultReg = createResultReg(&AArch64::GPR32spRegClass);
1950 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1953 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1955 if (DestVT == MVT::i64) {
1956 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
1957 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
1958 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1959 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1960 TII.get(AArch64::SUBREG_TO_REG), Reg64)
1963 .addImm(AArch64::sub_32);
1968 if (DestVT == MVT::i64) {
1969 // FIXME: We're SExt i1 to i64.
1972 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1973 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SBFMWri),
1982 unsigned AArch64FastISel::Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
1983 unsigned Op1, bool Op1IsKill) {
1985 switch (RetVT.SimpleTy) {
1991 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
1993 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
1996 // Create the base instruction, then add the operands.
1997 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
1998 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1999 .addReg(Op0, getKillRegState(Op0IsKill))
2000 .addReg(Op1, getKillRegState(Op1IsKill))
2001 .addReg(ZReg, getKillRegState(true));
2006 unsigned AArch64FastISel::Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2007 unsigned Op1, bool Op1IsKill) {
2008 if (RetVT != MVT::i64)
2011 // Create the base instruction, then add the operands.
2012 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
2013 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SMADDLrrr),
2015 .addReg(Op0, getKillRegState(Op0IsKill))
2016 .addReg(Op1, getKillRegState(Op1IsKill))
2017 .addReg(AArch64::XZR, getKillRegState(true));
2022 unsigned AArch64FastISel::Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2023 unsigned Op1, bool Op1IsKill) {
2024 if (RetVT != MVT::i64)
2027 // Create the base instruction, then add the operands.
2028 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
2029 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::UMADDLrrr),
2031 .addReg(Op0, getKillRegState(Op0IsKill))
2032 .addReg(Op1, getKillRegState(Op1IsKill))
2033 .addReg(AArch64::XZR, getKillRegState(true));
2038 unsigned AArch64FastISel::Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2040 unsigned Opc, ImmR, ImmS;
2041 switch (RetVT.SimpleTy) {
2047 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 31 - Shift; break;
2049 Opc = AArch64::UBFMXri; ImmR = -Shift % 64; ImmS = 63 - Shift; break;
2052 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, ImmR,
2056 unsigned AArch64FastISel::Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2059 switch (RetVT.SimpleTy) {
2065 Opc = AArch64::UBFMWri; ImmS = 31; break;
2067 Opc = AArch64::UBFMXri; ImmS = 63; break;
2070 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift,
2074 unsigned AArch64FastISel::Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2077 switch (RetVT.SimpleTy) {
2083 Opc = AArch64::SBFMWri; ImmS = 31; break;
2085 Opc = AArch64::SBFMXri; ImmS = 63; break;
2088 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift,
2092 unsigned AArch64FastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
2094 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
2096 // FastISel does not have plumbing to deal with extensions where the SrcVT or
2097 // DestVT are odd things, so test to make sure that they are both types we can
2098 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
2099 // bail out to SelectionDAG.
2100 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
2101 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
2102 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
2103 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
2109 switch (SrcVT.SimpleTy) {
2113 return Emiti1Ext(SrcReg, DestVT, isZExt);
2115 if (DestVT == MVT::i64)
2116 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2118 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
2122 if (DestVT == MVT::i64)
2123 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2125 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
2129 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
2130 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2135 // Handle i8 and i16 as i32.
2136 if (DestVT == MVT::i8 || DestVT == MVT::i16)
2138 else if (DestVT == MVT::i64) {
2139 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2140 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2141 TII.get(AArch64::SUBREG_TO_REG), Src64)
2144 .addImm(AArch64::sub_32);
2148 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2149 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2157 bool AArch64FastISel::SelectIntExt(const Instruction *I) {
2158 // On ARM, in general, integer casts don't involve legal types; this code
2159 // handles promotable integers. The high bits for a type smaller than
2160 // the register size are assumed to be undefined.
2161 Type *DestTy = I->getType();
2162 Value *Src = I->getOperand(0);
2163 Type *SrcTy = Src->getType();
2165 bool isZExt = isa<ZExtInst>(I);
2166 unsigned SrcReg = getRegForValue(Src);
2170 EVT SrcEVT = TLI.getValueType(SrcTy, true);
2171 EVT DestEVT = TLI.getValueType(DestTy, true);
2172 if (!SrcEVT.isSimple())
2174 if (!DestEVT.isSimple())
2177 MVT SrcVT = SrcEVT.getSimpleVT();
2178 MVT DestVT = DestEVT.getSimpleVT();
2179 unsigned ResultReg = EmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2182 UpdateValueMap(I, ResultReg);
2186 bool AArch64FastISel::SelectRem(const Instruction *I, unsigned ISDOpcode) {
2187 EVT DestEVT = TLI.getValueType(I->getType(), true);
2188 if (!DestEVT.isSimple())
2191 MVT DestVT = DestEVT.getSimpleVT();
2192 if (DestVT != MVT::i64 && DestVT != MVT::i32)
2196 bool is64bit = (DestVT == MVT::i64);
2197 switch (ISDOpcode) {
2201 DivOpc = is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
2204 DivOpc = is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
2207 unsigned MSubOpc = is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
2208 unsigned Src0Reg = getRegForValue(I->getOperand(0));
2212 unsigned Src1Reg = getRegForValue(I->getOperand(1));
2216 unsigned QuotReg = createResultReg(TLI.getRegClassFor(DestVT));
2217 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(DivOpc), QuotReg)
2220 // The remainder is computed as numerator - (quotient * denominator) using the
2221 // MSUB instruction.
2222 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2223 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MSubOpc), ResultReg)
2227 UpdateValueMap(I, ResultReg);
2231 bool AArch64FastISel::SelectMul(const Instruction *I) {
2232 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2233 if (!SrcEVT.isSimple())
2235 MVT SrcVT = SrcEVT.getSimpleVT();
2237 // Must be simple value type. Don't handle vectors.
2238 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
2242 unsigned Src0Reg = getRegForValue(I->getOperand(0));
2245 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
2247 unsigned Src1Reg = getRegForValue(I->getOperand(1));
2250 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
2252 unsigned ResultReg =
2253 Emit_MUL_rr(SrcVT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
2258 UpdateValueMap(I, ResultReg);
2262 bool AArch64FastISel::SelectShift(const Instruction *I, bool IsLeftShift,
2263 bool IsArithmetic) {
2264 EVT RetEVT = TLI.getValueType(I->getType(), true);
2265 if (!RetEVT.isSimple())
2267 MVT RetVT = RetEVT.getSimpleVT();
2269 if (!isa<ConstantInt>(I->getOperand(1)))
2272 unsigned Op0Reg = getRegForValue(I->getOperand(0));
2275 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
2277 uint64_t ShiftVal = cast<ConstantInt>(I->getOperand(1))->getZExtValue();
2281 ResultReg = Emit_LSL_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2284 ResultReg = Emit_ASR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2286 ResultReg = Emit_LSR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2292 UpdateValueMap(I, ResultReg);
2296 bool AArch64FastISel::TargetSelectInstruction(const Instruction *I) {
2297 switch (I->getOpcode()) {
2300 case Instruction::Load:
2301 return SelectLoad(I);
2302 case Instruction::Store:
2303 return SelectStore(I);
2304 case Instruction::Br:
2305 return SelectBranch(I);
2306 case Instruction::IndirectBr:
2307 return SelectIndirectBr(I);
2308 case Instruction::FCmp:
2309 case Instruction::ICmp:
2310 return SelectCmp(I);
2311 case Instruction::Select:
2312 return SelectSelect(I);
2313 case Instruction::FPExt:
2314 return SelectFPExt(I);
2315 case Instruction::FPTrunc:
2316 return SelectFPTrunc(I);
2317 case Instruction::FPToSI:
2318 return SelectFPToInt(I, /*Signed=*/true);
2319 case Instruction::FPToUI:
2320 return SelectFPToInt(I, /*Signed=*/false);
2321 case Instruction::SIToFP:
2322 return SelectIntToFP(I, /*Signed=*/true);
2323 case Instruction::UIToFP:
2324 return SelectIntToFP(I, /*Signed=*/false);
2325 case Instruction::SRem:
2326 return SelectRem(I, ISD::SREM);
2327 case Instruction::URem:
2328 return SelectRem(I, ISD::UREM);
2329 case Instruction::Ret:
2330 return SelectRet(I);
2331 case Instruction::Trunc:
2332 return SelectTrunc(I);
2333 case Instruction::ZExt:
2334 case Instruction::SExt:
2335 return SelectIntExt(I);
2337 // FIXME: All of these should really be handled by the target-independent
2338 // selector -> improve FastISel tblgen.
2339 case Instruction::Mul:
2340 return SelectMul(I);
2341 case Instruction::Shl:
2342 return SelectShift(I, /*IsLeftShift=*/true, /*IsArithmetic=*/false);
2343 case Instruction::LShr:
2344 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/false);
2345 case Instruction::AShr:
2346 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/true);
2349 // Silence warnings.
2350 (void)&CC_AArch64_DarwinPCS_VarArg;
2354 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &funcInfo,
2355 const TargetLibraryInfo *libInfo) {
2356 return new AArch64FastISel(funcInfo, libInfo);