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 {
58 const GlobalValue *GV;
61 Address() : Kind(RegBase), Offset(0), GV(nullptr) { Base.Reg = 0; }
62 void setKind(BaseKind K) { Kind = K; }
63 BaseKind getKind() const { return Kind; }
64 bool isRegBase() const { return Kind == RegBase; }
65 bool isFIBase() const { return Kind == FrameIndexBase; }
66 void setReg(unsigned Reg) {
67 assert(isRegBase() && "Invalid base register access!");
70 unsigned getReg() const {
71 assert(isRegBase() && "Invalid base register access!");
74 void setFI(unsigned FI) {
75 assert(isFIBase() && "Invalid base frame index access!");
78 unsigned getFI() const {
79 assert(isFIBase() && "Invalid base frame index access!");
82 void setOffset(int64_t O) { Offset = O; }
83 int64_t getOffset() { return Offset; }
85 void setGlobalValue(const GlobalValue *G) { GV = G; }
86 const GlobalValue *getGlobalValue() { return GV; }
88 bool isValid() { return isFIBase() || (isRegBase() && getReg() != 0); }
91 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
92 /// make the right decision when generating code for different targets.
93 const AArch64Subtarget *Subtarget;
96 bool FastLowerCall(CallLoweringInfo &CLI) override;
97 bool FastLowerIntrinsicCall(const IntrinsicInst *II) override;
100 // Selection routines.
101 bool SelectLoad(const Instruction *I);
102 bool SelectStore(const Instruction *I);
103 bool SelectBranch(const Instruction *I);
104 bool SelectIndirectBr(const Instruction *I);
105 bool SelectCmp(const Instruction *I);
106 bool SelectSelect(const Instruction *I);
107 bool SelectFPExt(const Instruction *I);
108 bool SelectFPTrunc(const Instruction *I);
109 bool SelectFPToInt(const Instruction *I, bool Signed);
110 bool SelectIntToFP(const Instruction *I, bool Signed);
111 bool SelectRem(const Instruction *I, unsigned ISDOpcode);
112 bool SelectRet(const Instruction *I);
113 bool SelectTrunc(const Instruction *I);
114 bool SelectIntExt(const Instruction *I);
115 bool SelectMul(const Instruction *I);
116 bool SelectShift(const Instruction *I, bool IsLeftShift, bool IsArithmetic);
117 bool SelectBitCast(const Instruction *I);
119 // Utility helper routines.
120 bool isTypeLegal(Type *Ty, MVT &VT);
121 bool isLoadStoreTypeLegal(Type *Ty, MVT &VT);
122 bool ComputeAddress(const Value *Obj, Address &Addr);
123 bool ComputeCallAddress(const Value *V, Address &Addr);
124 bool SimplifyAddress(Address &Addr, MVT VT, int64_t ScaleFactor,
126 void AddLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
127 unsigned Flags, bool UseUnscaled);
128 bool IsMemCpySmall(uint64_t Len, unsigned Alignment);
129 bool TryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
131 bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
135 bool EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt);
136 bool EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
137 bool UseUnscaled = false);
138 bool EmitStore(MVT VT, unsigned SrcReg, Address Addr,
139 bool UseUnscaled = false);
140 unsigned EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
141 unsigned Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
142 unsigned Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
143 unsigned Op1, bool Op1IsKill);
144 unsigned Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
145 unsigned Op1, bool Op1IsKill);
146 unsigned Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
147 unsigned Op1, bool Op1IsKill);
148 unsigned Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
149 unsigned Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
150 unsigned Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill, uint64_t Imm);
152 unsigned AArch64MaterializeFP(const ConstantFP *CFP, MVT VT);
153 unsigned AArch64MaterializeGV(const GlobalValue *GV);
155 // Call handling routines.
157 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
158 bool ProcessCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
160 bool FinishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
163 // Backend specific FastISel code.
164 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
165 unsigned TargetMaterializeConstant(const Constant *C) override;
167 explicit AArch64FastISel(FunctionLoweringInfo &funcInfo,
168 const TargetLibraryInfo *libInfo)
169 : FastISel(funcInfo, libInfo) {
170 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
171 Context = &funcInfo.Fn->getContext();
174 bool TargetSelectInstruction(const Instruction *I) override;
176 #include "AArch64GenFastISel.inc"
179 } // end anonymous namespace
181 #include "AArch64GenCallingConv.inc"
183 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
184 if (CC == CallingConv::WebKit_JS)
185 return CC_AArch64_WebKit_JS;
186 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
189 unsigned AArch64FastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
190 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
191 "Alloca should always return a pointer.");
193 // Don't handle dynamic allocas.
194 if (!FuncInfo.StaticAllocaMap.count(AI))
197 DenseMap<const AllocaInst *, int>::iterator SI =
198 FuncInfo.StaticAllocaMap.find(AI);
200 if (SI != FuncInfo.StaticAllocaMap.end()) {
201 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
202 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
204 .addFrameIndex(SI->second)
213 unsigned AArch64FastISel::AArch64MaterializeFP(const ConstantFP *CFP, MVT VT) {
214 if (VT != MVT::f32 && VT != MVT::f64)
217 const APFloat Val = CFP->getValueAPF();
218 bool is64bit = (VT == MVT::f64);
220 // This checks to see if we can use FMOV instructions to materialize
221 // a constant, otherwise we have to materialize via the constant pool.
222 if (TLI.isFPImmLegal(Val, VT)) {
226 Imm = AArch64_AM::getFP64Imm(Val);
227 Opc = AArch64::FMOVDi;
229 Imm = AArch64_AM::getFP32Imm(Val);
230 Opc = AArch64::FMOVSi;
232 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
233 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
238 // Materialize via constant pool. MachineConstantPool wants an explicit
240 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
242 Align = DL.getTypeAllocSize(CFP->getType());
244 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
245 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
246 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
247 ADRPReg).addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGE);
249 unsigned Opc = is64bit ? AArch64::LDRDui : AArch64::LDRSui;
250 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
251 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
253 .addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
257 unsigned AArch64FastISel::AArch64MaterializeGV(const GlobalValue *GV) {
258 // We can't handle thread-local variables quickly yet.
259 if (GV->isThreadLocal())
262 // MachO still uses GOT for large code-model accesses, but ELF requires
263 // movz/movk sequences, which FastISel doesn't handle yet.
264 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
267 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
269 EVT DestEVT = TLI.getValueType(GV->getType(), true);
270 if (!DestEVT.isSimple())
273 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
276 if (OpFlags & AArch64II::MO_GOT) {
278 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
280 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
282 ResultReg = createResultReg(&AArch64::GPR64RegClass);
283 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
286 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
290 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
291 ADRPReg).addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
293 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
294 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
297 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
303 unsigned AArch64FastISel::TargetMaterializeConstant(const Constant *C) {
304 EVT CEVT = TLI.getValueType(C->getType(), true);
306 // Only handle simple types.
307 if (!CEVT.isSimple())
309 MVT VT = CEVT.getSimpleVT();
311 // FIXME: Handle ConstantInt.
312 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
313 return AArch64MaterializeFP(CFP, VT);
314 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
315 return AArch64MaterializeGV(GV);
320 // Computes the address to get to an object.
321 bool AArch64FastISel::ComputeAddress(const Value *Obj, Address &Addr) {
322 const User *U = nullptr;
323 unsigned Opcode = Instruction::UserOp1;
324 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
325 // Don't walk into other basic blocks unless the object is an alloca from
326 // another block, otherwise it may not have a virtual register assigned.
327 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
328 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
329 Opcode = I->getOpcode();
332 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
333 Opcode = C->getOpcode();
337 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
338 if (Ty->getAddressSpace() > 255)
339 // Fast instruction selection doesn't support the special
346 case Instruction::BitCast: {
347 // Look through bitcasts.
348 return ComputeAddress(U->getOperand(0), Addr);
350 case Instruction::IntToPtr: {
351 // Look past no-op inttoptrs.
352 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
353 return ComputeAddress(U->getOperand(0), Addr);
356 case Instruction::PtrToInt: {
357 // Look past no-op ptrtoints.
358 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
359 return ComputeAddress(U->getOperand(0), Addr);
362 case Instruction::GetElementPtr: {
363 Address SavedAddr = Addr;
364 uint64_t TmpOffset = Addr.getOffset();
366 // Iterate through the GEP folding the constants into offsets where
368 gep_type_iterator GTI = gep_type_begin(U);
369 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
371 const Value *Op = *i;
372 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
373 const StructLayout *SL = DL.getStructLayout(STy);
374 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
375 TmpOffset += SL->getElementOffset(Idx);
377 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
379 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
380 // Constant-offset addressing.
381 TmpOffset += CI->getSExtValue() * S;
384 if (canFoldAddIntoGEP(U, Op)) {
385 // A compatible add with a constant operand. Fold the constant.
387 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
388 TmpOffset += CI->getSExtValue() * S;
389 // Iterate on the other operand.
390 Op = cast<AddOperator>(Op)->getOperand(0);
394 goto unsupported_gep;
399 // Try to grab the base operand now.
400 Addr.setOffset(TmpOffset);
401 if (ComputeAddress(U->getOperand(0), Addr))
404 // We failed, restore everything and try the other options.
410 case Instruction::Alloca: {
411 const AllocaInst *AI = cast<AllocaInst>(Obj);
412 DenseMap<const AllocaInst *, int>::iterator SI =
413 FuncInfo.StaticAllocaMap.find(AI);
414 if (SI != FuncInfo.StaticAllocaMap.end()) {
415 Addr.setKind(Address::FrameIndexBase);
416 Addr.setFI(SI->second);
423 // Try to get this in a register if nothing else has worked.
425 Addr.setReg(getRegForValue(Obj));
426 return Addr.isValid();
429 bool AArch64FastISel::ComputeCallAddress(const Value *V, Address &Addr) {
430 const User *U = nullptr;
431 unsigned Opcode = Instruction::UserOp1;
434 if (const auto *I = dyn_cast<Instruction>(V)) {
435 Opcode = I->getOpcode();
437 InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
438 } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
439 Opcode = C->getOpcode();
445 case Instruction::BitCast:
446 // Look past bitcasts if its operand is in the same BB.
448 return ComputeCallAddress(U->getOperand(0), Addr);
450 case Instruction::IntToPtr:
451 // Look past no-op inttoptrs if its operand is in the same BB.
453 TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
454 return ComputeCallAddress(U->getOperand(0), Addr);
456 case Instruction::PtrToInt:
457 // Look past no-op ptrtoints if its operand is in the same BB.
459 TLI.getValueType(U->getType()) == TLI.getPointerTy())
460 return ComputeCallAddress(U->getOperand(0), Addr);
464 if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
465 Addr.setGlobalValue(GV);
469 // If all else fails, try to materialize the value in a register.
470 if (!Addr.getGlobalValue()) {
471 Addr.setReg(getRegForValue(V));
472 return Addr.getReg() != 0;
479 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
480 EVT evt = TLI.getValueType(Ty, true);
482 // Only handle simple types.
483 if (evt == MVT::Other || !evt.isSimple())
485 VT = evt.getSimpleVT();
487 // This is a legal type, but it's not something we handle in fast-isel.
491 // Handle all other legal types, i.e. a register that will directly hold this
493 return TLI.isTypeLegal(VT);
496 bool AArch64FastISel::isLoadStoreTypeLegal(Type *Ty, MVT &VT) {
497 if (isTypeLegal(Ty, VT))
500 // If this is a type than can be sign or zero-extended to a basic operation
501 // go ahead and accept it now. For stores, this reflects truncation.
502 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
508 bool AArch64FastISel::SimplifyAddress(Address &Addr, MVT VT,
509 int64_t ScaleFactor, bool UseUnscaled) {
510 bool needsLowering = false;
511 int64_t Offset = Addr.getOffset();
512 switch (VT.SimpleTy) {
523 // Using scaled, 12-bit, unsigned immediate offsets.
524 needsLowering = ((Offset & 0xfff) != Offset);
526 // Using unscaled, 9-bit, signed immediate offsets.
527 needsLowering = (Offset > 256 || Offset < -256);
531 //If this is a stack pointer and the offset needs to be simplified then put
532 // the alloca address into a register, set the base type back to register and
533 // continue. This should almost never happen.
534 if (needsLowering && Addr.getKind() == Address::FrameIndexBase) {
535 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
536 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
538 .addFrameIndex(Addr.getFI())
541 Addr.setKind(Address::RegBase);
542 Addr.setReg(ResultReg);
545 // Since the offset is too large for the load/store instruction get the
546 // reg+offset into a register.
548 uint64_t UnscaledOffset = Addr.getOffset() * ScaleFactor;
549 unsigned ResultReg = FastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(), false,
550 UnscaledOffset, MVT::i64);
553 Addr.setReg(ResultReg);
559 void AArch64FastISel::AddLoadStoreOperands(Address &Addr,
560 const MachineInstrBuilder &MIB,
561 unsigned Flags, bool UseUnscaled) {
562 int64_t Offset = Addr.getOffset();
563 // Frame base works a bit differently. Handle it separately.
564 if (Addr.getKind() == Address::FrameIndexBase) {
565 int FI = Addr.getFI();
566 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
567 // and alignment should be based on the VT.
568 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
569 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
570 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
571 // Now add the rest of the operands.
572 MIB.addFrameIndex(FI).addImm(Offset).addMemOperand(MMO);
574 // Now add the rest of the operands.
575 MIB.addReg(Addr.getReg());
580 bool AArch64FastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
582 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
583 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
584 if (!UseUnscaled && Addr.getOffset() < 0)
588 const TargetRegisterClass *RC;
590 int64_t ScaleFactor = 0;
591 switch (VT.SimpleTy) {
596 // Intentional fall-through.
598 Opc = UseUnscaled ? AArch64::LDURBBi : AArch64::LDRBBui;
599 RC = &AArch64::GPR32RegClass;
603 Opc = UseUnscaled ? AArch64::LDURHHi : AArch64::LDRHHui;
604 RC = &AArch64::GPR32RegClass;
608 Opc = UseUnscaled ? AArch64::LDURWi : AArch64::LDRWui;
609 RC = &AArch64::GPR32RegClass;
613 Opc = UseUnscaled ? AArch64::LDURXi : AArch64::LDRXui;
614 RC = &AArch64::GPR64RegClass;
618 Opc = UseUnscaled ? AArch64::LDURSi : AArch64::LDRSui;
619 RC = TLI.getRegClassFor(VT);
623 Opc = UseUnscaled ? AArch64::LDURDi : AArch64::LDRDui;
624 RC = TLI.getRegClassFor(VT);
630 int64_t Offset = Addr.getOffset();
631 if (Offset & (ScaleFactor - 1))
632 // Retry using an unscaled, 9-bit, signed immediate offset.
633 return EmitLoad(VT, ResultReg, Addr, /*UseUnscaled*/ true);
635 Addr.setOffset(Offset / ScaleFactor);
638 // Simplify this down to something we can handle.
639 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled))
642 // Create the base instruction, then add the operands.
643 ResultReg = createResultReg(RC);
644 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
645 TII.get(Opc), ResultReg);
646 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, UseUnscaled);
648 // Loading an i1 requires special handling.
650 MRI.constrainRegClass(ResultReg, &AArch64::GPR32RegClass);
651 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
652 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
655 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
661 bool AArch64FastISel::SelectLoad(const Instruction *I) {
663 // Verify we have a legal type before going any further. Currently, we handle
664 // simple types that will directly fit in a register (i32/f32/i64/f64) or
665 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
666 if (!isLoadStoreTypeLegal(I->getType(), VT) || cast<LoadInst>(I)->isAtomic())
669 // See if we can handle this address.
671 if (!ComputeAddress(I->getOperand(0), Addr))
675 if (!EmitLoad(VT, ResultReg, Addr))
678 UpdateValueMap(I, ResultReg);
682 bool AArch64FastISel::EmitStore(MVT VT, unsigned SrcReg, Address Addr,
684 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
685 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
686 if (!UseUnscaled && Addr.getOffset() < 0)
691 int64_t ScaleFactor = 0;
692 // Using scaled, 12-bit, unsigned immediate offsets.
693 switch (VT.SimpleTy) {
699 StrOpc = UseUnscaled ? AArch64::STURBBi : AArch64::STRBBui;
703 StrOpc = UseUnscaled ? AArch64::STURHHi : AArch64::STRHHui;
707 StrOpc = UseUnscaled ? AArch64::STURWi : AArch64::STRWui;
711 StrOpc = UseUnscaled ? AArch64::STURXi : AArch64::STRXui;
715 StrOpc = UseUnscaled ? AArch64::STURSi : AArch64::STRSui;
719 StrOpc = UseUnscaled ? AArch64::STURDi : AArch64::STRDui;
725 int64_t Offset = Addr.getOffset();
726 if (Offset & (ScaleFactor - 1))
727 // Retry using an unscaled, 9-bit, signed immediate offset.
728 return EmitStore(VT, SrcReg, Addr, /*UseUnscaled*/ true);
730 Addr.setOffset(Offset / ScaleFactor);
733 // Simplify this down to something we can handle.
734 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled))
737 // Storing an i1 requires special handling.
739 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
740 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
741 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
744 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
747 // Create the base instruction, then add the operands.
748 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
749 TII.get(StrOpc)).addReg(SrcReg);
750 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, UseUnscaled);
754 bool AArch64FastISel::SelectStore(const Instruction *I) {
756 Value *Op0 = I->getOperand(0);
757 // Verify we have a legal type before going any further. Currently, we handle
758 // simple types that will directly fit in a register (i32/f32/i64/f64) or
759 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
760 if (!isLoadStoreTypeLegal(Op0->getType(), VT) ||
761 cast<StoreInst>(I)->isAtomic())
764 // Get the value to be stored into a register.
765 unsigned SrcReg = getRegForValue(Op0);
769 // See if we can handle this address.
771 if (!ComputeAddress(I->getOperand(1), Addr))
774 if (!EmitStore(VT, SrcReg, Addr))
779 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
781 case CmpInst::FCMP_ONE:
782 case CmpInst::FCMP_UEQ:
784 // AL is our "false" for now. The other two need more compares.
785 return AArch64CC::AL;
786 case CmpInst::ICMP_EQ:
787 case CmpInst::FCMP_OEQ:
788 return AArch64CC::EQ;
789 case CmpInst::ICMP_SGT:
790 case CmpInst::FCMP_OGT:
791 return AArch64CC::GT;
792 case CmpInst::ICMP_SGE:
793 case CmpInst::FCMP_OGE:
794 return AArch64CC::GE;
795 case CmpInst::ICMP_UGT:
796 case CmpInst::FCMP_UGT:
797 return AArch64CC::HI;
798 case CmpInst::FCMP_OLT:
799 return AArch64CC::MI;
800 case CmpInst::ICMP_ULE:
801 case CmpInst::FCMP_OLE:
802 return AArch64CC::LS;
803 case CmpInst::FCMP_ORD:
804 return AArch64CC::VC;
805 case CmpInst::FCMP_UNO:
806 return AArch64CC::VS;
807 case CmpInst::FCMP_UGE:
808 return AArch64CC::PL;
809 case CmpInst::ICMP_SLT:
810 case CmpInst::FCMP_ULT:
811 return AArch64CC::LT;
812 case CmpInst::ICMP_SLE:
813 case CmpInst::FCMP_ULE:
814 return AArch64CC::LE;
815 case CmpInst::FCMP_UNE:
816 case CmpInst::ICMP_NE:
817 return AArch64CC::NE;
818 case CmpInst::ICMP_UGE:
819 return AArch64CC::HS;
820 case CmpInst::ICMP_ULT:
821 return AArch64CC::LO;
825 bool AArch64FastISel::SelectBranch(const Instruction *I) {
826 const BranchInst *BI = cast<BranchInst>(I);
827 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
828 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
830 AArch64CC::CondCode CC = AArch64CC::NE;
831 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
832 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
833 // We may not handle every CC for now.
834 CC = getCompareCC(CI->getPredicate());
835 if (CC == AArch64CC::AL)
839 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
843 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
846 FuncInfo.MBB->addSuccessor(TBB);
848 FastEmitBranch(FBB, DbgLoc);
851 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
853 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
854 (isLoadStoreTypeLegal(TI->getOperand(0)->getType(), SrcVT))) {
855 unsigned CondReg = getRegForValue(TI->getOperand(0));
859 // Issue an extract_subreg to get the lower 32-bits.
860 if (SrcVT == MVT::i64)
861 CondReg = FastEmitInst_extractsubreg(MVT::i32, CondReg, /*Kill=*/true,
864 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
865 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
866 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
867 TII.get(AArch64::ANDWri), ANDReg)
869 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
870 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
871 TII.get(AArch64::SUBSWri))
877 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
881 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
884 FuncInfo.MBB->addSuccessor(TBB);
885 FastEmitBranch(FBB, DbgLoc);
888 } else if (const ConstantInt *CI =
889 dyn_cast<ConstantInt>(BI->getCondition())) {
890 uint64_t Imm = CI->getZExtValue();
891 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
892 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
894 FuncInfo.MBB->addSuccessor(Target);
896 } else if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
897 // Fake request the condition, otherwise the intrinsic might be completely
899 unsigned CondReg = getRegForValue(BI->getCondition());
904 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
907 FuncInfo.MBB->addSuccessor(TBB);
909 FastEmitBranch(FBB, DbgLoc);
913 unsigned CondReg = getRegForValue(BI->getCondition());
917 // We've been divorced from our compare! Our block was split, and
918 // now our compare lives in a predecessor block. We musn't
919 // re-compare here, as the children of the compare aren't guaranteed
920 // live across the block boundary (we *could* check for this).
921 // Regardless, the compare has been done in the predecessor block,
922 // and it left a value for us in a virtual register. Ergo, we test
923 // the one-bit value left in the virtual register.
924 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri),
930 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
935 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
938 FuncInfo.MBB->addSuccessor(TBB);
939 FastEmitBranch(FBB, DbgLoc);
943 bool AArch64FastISel::SelectIndirectBr(const Instruction *I) {
944 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
945 unsigned AddrReg = getRegForValue(BI->getOperand(0));
949 // Emit the indirect branch.
950 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BR))
953 // Make sure the CFG is up-to-date.
954 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
955 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
960 bool AArch64FastISel::EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt) {
961 Type *Ty = Src1Value->getType();
962 EVT SrcEVT = TLI.getValueType(Ty, true);
963 if (!SrcEVT.isSimple())
965 MVT SrcVT = SrcEVT.getSimpleVT();
967 // Check to see if the 2nd operand is a constant that we can encode directly
971 bool isNegativeImm = false;
972 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
973 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
974 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
975 const APInt &CIVal = ConstInt->getValue();
977 Imm = (isZExt) ? CIVal.getZExtValue() : CIVal.getSExtValue();
978 if (CIVal.isNegative()) {
979 isNegativeImm = true;
982 // FIXME: We can handle more immediates using shifts.
983 UseImm = ((Imm & 0xfff) == Imm);
985 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
986 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
987 if (ConstFP->isZero() && !ConstFP->isNegative())
994 bool needsExt = false;
995 switch (SrcVT.SimpleTy) {
1002 // Intentional fall-through.
1004 ZReg = AArch64::WZR;
1006 CmpOpc = isNegativeImm ? AArch64::ADDSWri : AArch64::SUBSWri;
1008 CmpOpc = AArch64::SUBSWrr;
1011 ZReg = AArch64::XZR;
1013 CmpOpc = isNegativeImm ? AArch64::ADDSXri : AArch64::SUBSXri;
1015 CmpOpc = AArch64::SUBSXrr;
1019 CmpOpc = UseImm ? AArch64::FCMPSri : AArch64::FCMPSrr;
1023 CmpOpc = UseImm ? AArch64::FCMPDri : AArch64::FCMPDrr;
1027 unsigned SrcReg1 = getRegForValue(Src1Value);
1033 SrcReg2 = getRegForValue(Src2Value);
1038 // We have i1, i8, or i16, we need to either zero extend or sign extend.
1040 SrcReg1 = EmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
1044 SrcReg2 = EmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
1052 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1058 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1064 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1067 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
1074 bool AArch64FastISel::SelectCmp(const Instruction *I) {
1075 const CmpInst *CI = cast<CmpInst>(I);
1077 // We may not handle every CC for now.
1078 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate());
1079 if (CC == AArch64CC::AL)
1083 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1086 // Now set a register based on the comparison.
1087 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
1088 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1089 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1091 .addReg(AArch64::WZR)
1092 .addReg(AArch64::WZR)
1093 .addImm(invertedCC);
1095 UpdateValueMap(I, ResultReg);
1099 bool AArch64FastISel::SelectSelect(const Instruction *I) {
1100 const SelectInst *SI = cast<SelectInst>(I);
1102 EVT DestEVT = TLI.getValueType(SI->getType(), true);
1103 if (!DestEVT.isSimple())
1106 MVT DestVT = DestEVT.getSimpleVT();
1107 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
1112 switch (DestVT.SimpleTy) {
1113 default: return false;
1114 case MVT::i32: SelectOpc = AArch64::CSELWr; break;
1115 case MVT::i64: SelectOpc = AArch64::CSELXr; break;
1116 case MVT::f32: SelectOpc = AArch64::FCSELSrrr; break;
1117 case MVT::f64: SelectOpc = AArch64::FCSELDrrr; break;
1120 const Value *Cond = SI->getCondition();
1121 bool NeedTest = true;
1122 AArch64CC::CondCode CC = AArch64CC::NE;
1123 if (foldXALUIntrinsic(CC, I, Cond))
1126 unsigned CondReg = getRegForValue(Cond);
1129 bool CondIsKill = hasTrivialKill(Cond);
1132 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
1133 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1134 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1136 .addReg(CondReg, getKillRegState(CondIsKill))
1137 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1139 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri))
1146 unsigned TrueReg = getRegForValue(SI->getTrueValue());
1147 bool TrueIsKill = hasTrivialKill(SI->getTrueValue());
1149 unsigned FalseReg = getRegForValue(SI->getFalseValue());
1150 bool FalseIsKill = hasTrivialKill(SI->getFalseValue());
1152 if (!TrueReg || !FalseReg)
1155 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1156 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SelectOpc),
1158 .addReg(TrueReg, getKillRegState(TrueIsKill))
1159 .addReg(FalseReg, getKillRegState(FalseIsKill))
1162 UpdateValueMap(I, ResultReg);
1166 bool AArch64FastISel::SelectFPExt(const Instruction *I) {
1167 Value *V = I->getOperand(0);
1168 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
1171 unsigned Op = getRegForValue(V);
1175 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
1176 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
1177 ResultReg).addReg(Op);
1178 UpdateValueMap(I, ResultReg);
1182 bool AArch64FastISel::SelectFPTrunc(const Instruction *I) {
1183 Value *V = I->getOperand(0);
1184 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
1187 unsigned Op = getRegForValue(V);
1191 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
1192 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
1193 ResultReg).addReg(Op);
1194 UpdateValueMap(I, ResultReg);
1198 // FPToUI and FPToSI
1199 bool AArch64FastISel::SelectFPToInt(const Instruction *I, bool Signed) {
1201 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1204 unsigned SrcReg = getRegForValue(I->getOperand(0));
1208 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1209 if (SrcVT == MVT::f128)
1213 if (SrcVT == MVT::f64) {
1215 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
1217 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
1220 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
1222 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
1224 unsigned ResultReg = createResultReg(
1225 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
1226 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1228 UpdateValueMap(I, ResultReg);
1232 bool AArch64FastISel::SelectIntToFP(const Instruction *I, bool Signed) {
1234 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1236 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
1237 "Unexpected value type.");
1239 unsigned SrcReg = getRegForValue(I->getOperand(0));
1243 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1245 // Handle sign-extension.
1246 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
1248 EmitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
1253 MRI.constrainRegClass(SrcReg, SrcVT == MVT::i64 ? &AArch64::GPR64RegClass
1254 : &AArch64::GPR32RegClass);
1257 if (SrcVT == MVT::i64) {
1259 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
1261 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
1264 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
1266 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
1269 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1270 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1272 UpdateValueMap(I, ResultReg);
1276 bool AArch64FastISel::ProcessCallArgs(CallLoweringInfo &CLI,
1277 SmallVectorImpl<MVT> &OutVTs,
1278 unsigned &NumBytes) {
1279 CallingConv::ID CC = CLI.CallConv;
1280 SmallVector<CCValAssign, 16> ArgLocs;
1281 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
1282 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
1284 // Get a count of how many bytes are to be pushed on the stack.
1285 NumBytes = CCInfo.getNextStackOffset();
1287 // Issue CALLSEQ_START
1288 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1289 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
1292 // Process the args.
1293 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1294 CCValAssign &VA = ArgLocs[i];
1295 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1296 MVT ArgVT = OutVTs[VA.getValNo()];
1298 unsigned ArgReg = getRegForValue(ArgVal);
1302 // Handle arg promotion: SExt, ZExt, AExt.
1303 switch (VA.getLocInfo()) {
1304 case CCValAssign::Full:
1306 case CCValAssign::SExt: {
1307 MVT DestVT = VA.getLocVT();
1309 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1314 case CCValAssign::AExt:
1315 // Intentional fall-through.
1316 case CCValAssign::ZExt: {
1317 MVT DestVT = VA.getLocVT();
1319 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1325 llvm_unreachable("Unknown arg promotion!");
1328 // Now copy/store arg to correct locations.
1329 if (VA.isRegLoc() && !VA.needsCustom()) {
1330 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1331 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1332 CLI.OutRegs.push_back(VA.getLocReg());
1333 } else if (VA.needsCustom()) {
1334 // FIXME: Handle custom args.
1337 assert(VA.isMemLoc() && "Assuming store on stack.");
1339 // Don't emit stores for undef values.
1340 if (isa<UndefValue>(ArgVal))
1343 // Need to store on the stack.
1344 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
1346 unsigned BEAlign = 0;
1347 if (ArgSize < 8 && !Subtarget->isLittleEndian())
1348 BEAlign = 8 - ArgSize;
1351 Addr.setKind(Address::RegBase);
1352 Addr.setReg(AArch64::SP);
1353 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1355 if (!EmitStore(ArgVT, ArgReg, Addr))
1362 bool AArch64FastISel::FinishCall(CallLoweringInfo &CLI, MVT RetVT,
1363 unsigned NumBytes) {
1364 CallingConv::ID CC = CLI.CallConv;
1366 // Issue CALLSEQ_END
1367 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1368 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
1369 .addImm(NumBytes).addImm(0);
1371 // Now the return value.
1372 if (RetVT != MVT::isVoid) {
1373 SmallVector<CCValAssign, 16> RVLocs;
1374 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
1375 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
1377 // Only handle a single return value.
1378 if (RVLocs.size() != 1)
1381 // Copy all of the result registers out of their specified physreg.
1382 MVT CopyVT = RVLocs[0].getValVT();
1383 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1384 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1385 TII.get(TargetOpcode::COPY), ResultReg)
1386 .addReg(RVLocs[0].getLocReg());
1387 CLI.InRegs.push_back(RVLocs[0].getLocReg());
1389 CLI.ResultReg = ResultReg;
1390 CLI.NumResultRegs = 1;
1396 bool AArch64FastISel::FastLowerCall(CallLoweringInfo &CLI) {
1397 CallingConv::ID CC = CLI.CallConv;
1398 bool IsVarArg = CLI.IsVarArg;
1399 const Value *Callee = CLI.Callee;
1400 const char *SymName = CLI.SymName;
1402 CodeModel::Model CM = TM.getCodeModel();
1403 // Only support the small and large code model.
1404 if (CM != CodeModel::Small && CM != CodeModel::Large)
1407 // FIXME: Add large code model support for ELF.
1408 if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
1411 // Let SDISel handle vararg functions.
1415 // FIXME: Only handle *simple* calls for now.
1417 if (CLI.RetTy->isVoidTy())
1418 RetVT = MVT::isVoid;
1419 else if (!isTypeLegal(CLI.RetTy, RetVT))
1422 for (auto Flag : CLI.OutFlags)
1423 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1426 // Set up the argument vectors.
1427 SmallVector<MVT, 16> OutVTs;
1428 OutVTs.reserve(CLI.OutVals.size());
1430 for (auto *Val : CLI.OutVals) {
1432 if (!isTypeLegal(Val->getType(), VT) &&
1433 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
1436 // We don't handle vector parameters yet.
1437 if (VT.isVector() || VT.getSizeInBits() > 64)
1440 OutVTs.push_back(VT);
1444 if (!ComputeCallAddress(Callee, Addr))
1447 // Handle the arguments now that we've gotten them.
1449 if (!ProcessCallArgs(CLI, OutVTs, NumBytes))
1453 MachineInstrBuilder MIB;
1454 if (CM == CodeModel::Small) {
1455 unsigned CallOpc = Addr.getReg() ? AArch64::BLR : AArch64::BL;
1456 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CallOpc));
1458 MIB.addExternalSymbol(SymName, 0);
1459 else if (Addr.getGlobalValue())
1460 MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
1461 else if (Addr.getReg())
1462 MIB.addReg(Addr.getReg());
1466 unsigned CallReg = 0;
1468 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
1469 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
1471 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGE);
1473 CallReg = createResultReg(&AArch64::GPR64RegClass);
1474 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
1477 .addExternalSymbol(SymName, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
1479 } else if (Addr.getGlobalValue()) {
1480 CallReg = AArch64MaterializeGV(Addr.getGlobalValue());
1481 } else if (Addr.getReg())
1482 CallReg = Addr.getReg();
1487 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1488 TII.get(AArch64::BLR)).addReg(CallReg);
1491 // Add implicit physical register uses to the call.
1492 for (auto Reg : CLI.OutRegs)
1493 MIB.addReg(Reg, RegState::Implicit);
1495 // Add a register mask with the call-preserved registers.
1496 // Proper defs for return values will be added by setPhysRegsDeadExcept().
1497 MIB.addRegMask(TRI.getCallPreservedMask(CC));
1501 // Finish off the call including any return values.
1502 return FinishCall(CLI, RetVT, NumBytes);
1505 bool AArch64FastISel::IsMemCpySmall(uint64_t Len, unsigned Alignment) {
1507 return Len / Alignment <= 4;
1512 bool AArch64FastISel::TryEmitSmallMemCpy(Address Dest, Address Src,
1513 uint64_t Len, unsigned Alignment) {
1514 // Make sure we don't bloat code by inlining very large memcpy's.
1515 if (!IsMemCpySmall(Len, Alignment))
1518 int64_t UnscaledOffset = 0;
1519 Address OrigDest = Dest;
1520 Address OrigSrc = Src;
1524 if (!Alignment || Alignment >= 8) {
1535 // Bound based on alignment.
1536 if (Len >= 4 && Alignment == 4)
1538 else if (Len >= 2 && Alignment == 2)
1547 RV = EmitLoad(VT, ResultReg, Src);
1551 RV = EmitStore(VT, ResultReg, Dest);
1555 int64_t Size = VT.getSizeInBits() / 8;
1557 UnscaledOffset += Size;
1559 // We need to recompute the unscaled offset for each iteration.
1560 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
1561 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
1567 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
1568 /// into the user. The condition code will only be updated on success.
1569 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
1570 const Instruction *I,
1571 const Value *Cond) {
1572 if (!isa<ExtractValueInst>(Cond))
1575 const auto *EV = cast<ExtractValueInst>(Cond);
1576 if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
1579 const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
1581 const Function *Callee = II->getCalledFunction();
1583 cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
1584 if (!isTypeLegal(RetTy, RetVT))
1587 if (RetVT != MVT::i32 && RetVT != MVT::i64)
1590 AArch64CC::CondCode TmpCC;
1591 switch (II->getIntrinsicID()) {
1592 default: return false;
1593 case Intrinsic::sadd_with_overflow:
1594 case Intrinsic::ssub_with_overflow: TmpCC = AArch64CC::VS; break;
1595 case Intrinsic::uadd_with_overflow: TmpCC = AArch64CC::HS; break;
1596 case Intrinsic::usub_with_overflow: TmpCC = AArch64CC::LO; break;
1597 case Intrinsic::smul_with_overflow:
1598 case Intrinsic::umul_with_overflow: TmpCC = AArch64CC::NE; break;
1601 // Check if both instructions are in the same basic block.
1602 if (II->getParent() != I->getParent())
1605 // Make sure nothing is in the way
1606 BasicBlock::const_iterator Start = I;
1607 BasicBlock::const_iterator End = II;
1608 for (auto Itr = std::prev(Start); Itr != End; --Itr) {
1609 // We only expect extractvalue instructions between the intrinsic and the
1610 // instruction to be selected.
1611 if (!isa<ExtractValueInst>(Itr))
1614 // Check that the extractvalue operand comes from the intrinsic.
1615 const auto *EVI = cast<ExtractValueInst>(Itr);
1616 if (EVI->getAggregateOperand() != II)
1624 bool AArch64FastISel::FastLowerIntrinsicCall(const IntrinsicInst *II) {
1625 // FIXME: Handle more intrinsics.
1626 switch (II->getIntrinsicID()) {
1627 default: return false;
1628 case Intrinsic::frameaddress: {
1629 MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
1630 MFI->setFrameAddressIsTaken(true);
1632 const AArch64RegisterInfo *RegInfo =
1633 static_cast<const AArch64RegisterInfo *>(TM.getRegisterInfo());
1634 unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
1635 unsigned SrcReg = FramePtr;
1637 // Recursively load frame address
1643 unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
1645 DestReg = createResultReg(&AArch64::GPR64RegClass);
1646 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1647 TII.get(AArch64::LDRXui), DestReg)
1648 .addReg(SrcReg).addImm(0);
1652 UpdateValueMap(II, SrcReg);
1655 case Intrinsic::memcpy:
1656 case Intrinsic::memmove: {
1657 const auto *MTI = cast<MemTransferInst>(II);
1658 // Don't handle volatile.
1659 if (MTI->isVolatile())
1662 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
1663 // we would emit dead code because we don't currently handle memmoves.
1664 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
1665 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
1666 // Small memcpy's are common enough that we want to do them without a call
1668 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
1669 unsigned Alignment = MTI->getAlignment();
1670 if (IsMemCpySmall(Len, Alignment)) {
1672 if (!ComputeAddress(MTI->getRawDest(), Dest) ||
1673 !ComputeAddress(MTI->getRawSource(), Src))
1675 if (TryEmitSmallMemCpy(Dest, Src, Len, Alignment))
1680 if (!MTI->getLength()->getType()->isIntegerTy(64))
1683 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
1684 // Fast instruction selection doesn't support the special
1688 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
1689 return LowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
1691 case Intrinsic::memset: {
1692 const MemSetInst *MSI = cast<MemSetInst>(II);
1693 // Don't handle volatile.
1694 if (MSI->isVolatile())
1697 if (!MSI->getLength()->getType()->isIntegerTy(64))
1700 if (MSI->getDestAddressSpace() > 255)
1701 // Fast instruction selection doesn't support the special
1705 return LowerCallTo(II, "memset", II->getNumArgOperands() - 2);
1707 case Intrinsic::trap: {
1708 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
1712 case Intrinsic::sqrt: {
1713 Type *RetTy = II->getCalledFunction()->getReturnType();
1716 if (!isTypeLegal(RetTy, VT))
1719 unsigned Op0Reg = getRegForValue(II->getOperand(0));
1722 bool Op0IsKill = hasTrivialKill(II->getOperand(0));
1724 unsigned ResultReg = FastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
1728 UpdateValueMap(II, ResultReg);
1731 case Intrinsic::sadd_with_overflow:
1732 case Intrinsic::uadd_with_overflow:
1733 case Intrinsic::ssub_with_overflow:
1734 case Intrinsic::usub_with_overflow:
1735 case Intrinsic::smul_with_overflow:
1736 case Intrinsic::umul_with_overflow: {
1737 // This implements the basic lowering of the xalu with overflow intrinsics.
1738 const Function *Callee = II->getCalledFunction();
1739 auto *Ty = cast<StructType>(Callee->getReturnType());
1740 Type *RetTy = Ty->getTypeAtIndex(0U);
1741 Type *CondTy = Ty->getTypeAtIndex(1);
1744 if (!isTypeLegal(RetTy, VT))
1747 if (VT != MVT::i32 && VT != MVT::i64)
1750 const Value *LHS = II->getArgOperand(0);
1751 const Value *RHS = II->getArgOperand(1);
1752 // Canonicalize immediate to the RHS.
1753 if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
1754 isCommutativeIntrinsic(II))
1755 std::swap(LHS, RHS);
1757 unsigned LHSReg = getRegForValue(LHS);
1760 bool LHSIsKill = hasTrivialKill(LHS);
1762 unsigned RHSReg = 0;
1763 bool RHSIsKill = false;
1765 if (!isa<ConstantInt>(RHS)) {
1766 RHSReg = getRegForValue(RHS);
1769 RHSIsKill = hasTrivialKill(RHS);
1774 unsigned MulReg = 0;
1775 AArch64CC::CondCode CC = AArch64CC::Invalid;
1776 bool Is64Bit = VT == MVT::i64;
1777 switch (II->getIntrinsicID()) {
1778 default: llvm_unreachable("Unexpected intrinsic!");
1779 case Intrinsic::sadd_with_overflow:
1781 Opc = Is64Bit ? AArch64::ADDSXri : AArch64::ADDSWri;
1783 Opc = Is64Bit ? AArch64::ADDSXrr : AArch64::ADDSWrr;
1786 case Intrinsic::uadd_with_overflow:
1788 Opc = Is64Bit ? AArch64::ADDSXri : AArch64::ADDSWri;
1790 Opc = Is64Bit ? AArch64::ADDSXrr : AArch64::ADDSWrr;
1793 case Intrinsic::ssub_with_overflow:
1795 Opc = Is64Bit ? AArch64::SUBSXri : AArch64::SUBSWri;
1797 Opc = Is64Bit ? AArch64::SUBSXrr : AArch64::SUBSWrr;
1800 case Intrinsic::usub_with_overflow:
1802 Opc = Is64Bit ? AArch64::SUBSXri : AArch64::SUBSWri;
1804 Opc = Is64Bit ? AArch64::SUBSXrr : AArch64::SUBSWrr;
1807 case Intrinsic::smul_with_overflow: {
1810 RHSReg = getRegForValue(RHS);
1813 RHSIsKill = hasTrivialKill(RHS);
1815 if (VT == MVT::i32) {
1816 MulReg = Emit_SMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1817 unsigned ShiftReg = Emit_LSR_ri(MVT::i64, MulReg, false, 32);
1818 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
1820 ShiftReg = FastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
1822 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT));
1823 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1824 TII.get(AArch64::SUBSWrs), CmpReg)
1825 .addReg(ShiftReg, getKillRegState(true))
1826 .addReg(MulReg, getKillRegState(false))
1827 .addImm(159); // 159 <-> asr #31
1829 assert(VT == MVT::i64 && "Unexpected value type.");
1830 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1831 unsigned SMULHReg = FastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
1833 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT));
1834 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1835 TII.get(AArch64::SUBSXrs), CmpReg)
1836 .addReg(SMULHReg, getKillRegState(true))
1837 .addReg(MulReg, getKillRegState(false))
1838 .addImm(191); // 191 <-> asr #63
1842 case Intrinsic::umul_with_overflow: {
1845 RHSReg = getRegForValue(RHS);
1848 RHSIsKill = hasTrivialKill(RHS);
1850 if (VT == MVT::i32) {
1851 MulReg = Emit_UMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1852 unsigned CmpReg = createResultReg(TLI.getRegClassFor(MVT::i64));
1853 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1854 TII.get(AArch64::SUBSXrs), CmpReg)
1855 .addReg(AArch64::XZR, getKillRegState(true))
1856 .addReg(MulReg, getKillRegState(false))
1857 .addImm(96); // 96 <-> lsr #32
1858 MulReg = FastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
1861 assert(VT == MVT::i64 && "Unexpected value type.");
1862 MulReg = Emit_MUL_rr(VT, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1863 unsigned UMULHReg = FastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
1865 unsigned CmpReg = createResultReg(TLI.getRegClassFor(VT));
1866 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1867 TII.get(AArch64::SUBSXrr), CmpReg)
1868 .addReg(AArch64::XZR, getKillRegState(true))
1869 .addReg(UMULHReg, getKillRegState(false));
1875 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
1877 MachineInstrBuilder MIB;
1878 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc),
1880 .addReg(LHSReg, getKillRegState(LHSIsKill));
1882 MIB.addImm(cast<ConstantInt>(RHS)->getZExtValue());
1884 MIB.addReg(RHSReg, getKillRegState(RHSIsKill));
1887 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1888 TII.get(TargetOpcode::COPY), ResultReg)
1891 unsigned ResultReg2 = FuncInfo.CreateRegs(CondTy);
1892 assert((ResultReg+1) == ResultReg2 && "Nonconsecutive result registers.");
1893 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1895 .addReg(AArch64::WZR, getKillRegState(true))
1896 .addReg(AArch64::WZR, getKillRegState(true))
1897 .addImm(getInvertedCondCode(CC));
1899 UpdateValueMap(II, ResultReg, 2);
1906 bool AArch64FastISel::SelectRet(const Instruction *I) {
1907 const ReturnInst *Ret = cast<ReturnInst>(I);
1908 const Function &F = *I->getParent()->getParent();
1910 if (!FuncInfo.CanLowerReturn)
1916 // Build a list of return value registers.
1917 SmallVector<unsigned, 4> RetRegs;
1919 if (Ret->getNumOperands() > 0) {
1920 CallingConv::ID CC = F.getCallingConv();
1921 SmallVector<ISD::OutputArg, 4> Outs;
1922 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1924 // Analyze operands of the call, assigning locations to each operand.
1925 SmallVector<CCValAssign, 16> ValLocs;
1926 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
1928 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
1929 : RetCC_AArch64_AAPCS;
1930 CCInfo.AnalyzeReturn(Outs, RetCC);
1932 // Only handle a single return value for now.
1933 if (ValLocs.size() != 1)
1936 CCValAssign &VA = ValLocs[0];
1937 const Value *RV = Ret->getOperand(0);
1939 // Don't bother handling odd stuff for now.
1940 if (VA.getLocInfo() != CCValAssign::Full)
1942 // Only handle register returns for now.
1945 unsigned Reg = getRegForValue(RV);
1949 unsigned SrcReg = Reg + VA.getValNo();
1950 unsigned DestReg = VA.getLocReg();
1951 // Avoid a cross-class copy. This is very unlikely.
1952 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1955 EVT RVEVT = TLI.getValueType(RV->getType());
1956 if (!RVEVT.isSimple())
1959 // Vectors (of > 1 lane) in big endian need tricky handling.
1960 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1)
1963 MVT RVVT = RVEVT.getSimpleVT();
1964 if (RVVT == MVT::f128)
1966 MVT DestVT = VA.getValVT();
1967 // Special handling for extended integers.
1968 if (RVVT != DestVT) {
1969 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1972 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1975 bool isZExt = Outs[0].Flags.isZExt();
1976 SrcReg = EmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1982 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1983 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1985 // Add register to return instruction.
1986 RetRegs.push_back(VA.getLocReg());
1989 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1990 TII.get(AArch64::RET_ReallyLR));
1991 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1992 MIB.addReg(RetRegs[i], RegState::Implicit);
1996 bool AArch64FastISel::SelectTrunc(const Instruction *I) {
1997 Type *DestTy = I->getType();
1998 Value *Op = I->getOperand(0);
1999 Type *SrcTy = Op->getType();
2001 EVT SrcEVT = TLI.getValueType(SrcTy, true);
2002 EVT DestEVT = TLI.getValueType(DestTy, true);
2003 if (!SrcEVT.isSimple())
2005 if (!DestEVT.isSimple())
2008 MVT SrcVT = SrcEVT.getSimpleVT();
2009 MVT DestVT = DestEVT.getSimpleVT();
2011 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
2014 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
2018 unsigned SrcReg = getRegForValue(Op);
2022 // If we're truncating from i64 to a smaller non-legal type then generate an
2023 // AND. Otherwise, we know the high bits are undefined and a truncate doesn't
2024 // generate any code.
2025 if (SrcVT == MVT::i64) {
2027 switch (DestVT.SimpleTy) {
2029 // Trunc i64 to i32 is handled by the target-independent fast-isel.
2041 // Issue an extract_subreg to get the lower 32-bits.
2042 unsigned Reg32 = FastEmitInst_extractsubreg(MVT::i32, SrcReg, /*Kill=*/true,
2044 MRI.constrainRegClass(Reg32, &AArch64::GPR32RegClass);
2045 // Create the AND instruction which performs the actual truncation.
2046 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
2047 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
2050 .addImm(AArch64_AM::encodeLogicalImmediate(Mask, 32));
2054 UpdateValueMap(I, SrcReg);
2058 unsigned AArch64FastISel::Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt) {
2059 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
2060 DestVT == MVT::i64) &&
2061 "Unexpected value type.");
2062 // Handle i8 and i16 as i32.
2063 if (DestVT == MVT::i8 || DestVT == MVT::i16)
2067 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
2068 unsigned ResultReg = createResultReg(&AArch64::GPR32spRegClass);
2069 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
2072 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
2074 if (DestVT == MVT::i64) {
2075 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
2076 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
2077 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2078 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2079 TII.get(AArch64::SUBREG_TO_REG), Reg64)
2082 .addImm(AArch64::sub_32);
2087 if (DestVT == MVT::i64) {
2088 // FIXME: We're SExt i1 to i64.
2091 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
2092 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SBFMWri),
2101 unsigned AArch64FastISel::Emit_MUL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2102 unsigned Op1, bool Op1IsKill) {
2104 switch (RetVT.SimpleTy) {
2110 Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
2112 Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
2115 // Create the base instruction, then add the operands.
2116 unsigned ResultReg = createResultReg(TLI.getRegClassFor(RetVT));
2117 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2118 .addReg(Op0, getKillRegState(Op0IsKill))
2119 .addReg(Op1, getKillRegState(Op1IsKill))
2120 .addReg(ZReg, getKillRegState(true));
2125 unsigned AArch64FastISel::Emit_SMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2126 unsigned Op1, bool Op1IsKill) {
2127 if (RetVT != MVT::i64)
2130 // Create the base instruction, then add the operands.
2131 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
2132 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SMADDLrrr),
2134 .addReg(Op0, getKillRegState(Op0IsKill))
2135 .addReg(Op1, getKillRegState(Op1IsKill))
2136 .addReg(AArch64::XZR, getKillRegState(true));
2141 unsigned AArch64FastISel::Emit_UMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
2142 unsigned Op1, bool Op1IsKill) {
2143 if (RetVT != MVT::i64)
2146 // Create the base instruction, then add the operands.
2147 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
2148 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::UMADDLrrr),
2150 .addReg(Op0, getKillRegState(Op0IsKill))
2151 .addReg(Op1, getKillRegState(Op1IsKill))
2152 .addReg(AArch64::XZR, getKillRegState(true));
2157 unsigned AArch64FastISel::Emit_LSL_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2159 unsigned Opc, ImmR, ImmS;
2160 switch (RetVT.SimpleTy) {
2166 Opc = AArch64::UBFMWri; ImmR = -Shift % 32; ImmS = 31 - Shift; break;
2168 Opc = AArch64::UBFMXri; ImmR = -Shift % 64; ImmS = 63 - Shift; break;
2171 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, ImmR,
2175 unsigned AArch64FastISel::Emit_LSR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2178 switch (RetVT.SimpleTy) {
2184 Opc = AArch64::UBFMWri; ImmS = 31; break;
2186 Opc = AArch64::UBFMXri; ImmS = 63; break;
2189 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift,
2193 unsigned AArch64FastISel::Emit_ASR_ri(MVT RetVT, unsigned Op0, bool Op0IsKill,
2196 switch (RetVT.SimpleTy) {
2202 Opc = AArch64::SBFMWri; ImmS = 31; break;
2204 Opc = AArch64::SBFMXri; ImmS = 63; break;
2207 return FastEmitInst_rii(Opc, TLI.getRegClassFor(RetVT), Op0, Op0IsKill, Shift,
2211 unsigned AArch64FastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
2213 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
2215 // FastISel does not have plumbing to deal with extensions where the SrcVT or
2216 // DestVT are odd things, so test to make sure that they are both types we can
2217 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
2218 // bail out to SelectionDAG.
2219 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
2220 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
2221 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
2222 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
2228 switch (SrcVT.SimpleTy) {
2232 return Emiti1Ext(SrcReg, DestVT, isZExt);
2234 if (DestVT == MVT::i64)
2235 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2237 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
2241 if (DestVT == MVT::i64)
2242 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2244 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
2248 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
2249 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
2254 // Handle i8 and i16 as i32.
2255 if (DestVT == MVT::i8 || DestVT == MVT::i16)
2257 else if (DestVT == MVT::i64) {
2258 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
2259 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2260 TII.get(AArch64::SUBREG_TO_REG), Src64)
2263 .addImm(AArch64::sub_32);
2267 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2268 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2276 bool AArch64FastISel::SelectIntExt(const Instruction *I) {
2277 // On ARM, in general, integer casts don't involve legal types; this code
2278 // handles promotable integers. The high bits for a type smaller than
2279 // the register size are assumed to be undefined.
2280 Type *DestTy = I->getType();
2281 Value *Src = I->getOperand(0);
2282 Type *SrcTy = Src->getType();
2284 bool isZExt = isa<ZExtInst>(I);
2285 unsigned SrcReg = getRegForValue(Src);
2289 EVT SrcEVT = TLI.getValueType(SrcTy, true);
2290 EVT DestEVT = TLI.getValueType(DestTy, true);
2291 if (!SrcEVT.isSimple())
2293 if (!DestEVT.isSimple())
2296 MVT SrcVT = SrcEVT.getSimpleVT();
2297 MVT DestVT = DestEVT.getSimpleVT();
2298 unsigned ResultReg = EmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
2301 UpdateValueMap(I, ResultReg);
2305 bool AArch64FastISel::SelectRem(const Instruction *I, unsigned ISDOpcode) {
2306 EVT DestEVT = TLI.getValueType(I->getType(), true);
2307 if (!DestEVT.isSimple())
2310 MVT DestVT = DestEVT.getSimpleVT();
2311 if (DestVT != MVT::i64 && DestVT != MVT::i32)
2315 bool is64bit = (DestVT == MVT::i64);
2316 switch (ISDOpcode) {
2320 DivOpc = is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
2323 DivOpc = is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
2326 unsigned MSubOpc = is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
2327 unsigned Src0Reg = getRegForValue(I->getOperand(0));
2331 unsigned Src1Reg = getRegForValue(I->getOperand(1));
2335 unsigned QuotReg = createResultReg(TLI.getRegClassFor(DestVT));
2336 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(DivOpc), QuotReg)
2339 // The remainder is computed as numerator - (quotient * denominator) using the
2340 // MSUB instruction.
2341 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
2342 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MSubOpc), ResultReg)
2346 UpdateValueMap(I, ResultReg);
2350 bool AArch64FastISel::SelectMul(const Instruction *I) {
2351 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType(), true);
2352 if (!SrcEVT.isSimple())
2354 MVT SrcVT = SrcEVT.getSimpleVT();
2356 // Must be simple value type. Don't handle vectors.
2357 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
2361 unsigned Src0Reg = getRegForValue(I->getOperand(0));
2364 bool Src0IsKill = hasTrivialKill(I->getOperand(0));
2366 unsigned Src1Reg = getRegForValue(I->getOperand(1));
2369 bool Src1IsKill = hasTrivialKill(I->getOperand(1));
2371 unsigned ResultReg =
2372 Emit_MUL_rr(SrcVT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
2377 UpdateValueMap(I, ResultReg);
2381 bool AArch64FastISel::SelectShift(const Instruction *I, bool IsLeftShift,
2382 bool IsArithmetic) {
2383 EVT RetEVT = TLI.getValueType(I->getType(), true);
2384 if (!RetEVT.isSimple())
2386 MVT RetVT = RetEVT.getSimpleVT();
2388 if (!isa<ConstantInt>(I->getOperand(1)))
2391 unsigned Op0Reg = getRegForValue(I->getOperand(0));
2394 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
2396 uint64_t ShiftVal = cast<ConstantInt>(I->getOperand(1))->getZExtValue();
2400 ResultReg = Emit_LSL_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2403 ResultReg = Emit_ASR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2405 ResultReg = Emit_LSR_ri(RetVT, Op0Reg, Op0IsKill, ShiftVal);
2411 UpdateValueMap(I, ResultReg);
2415 bool AArch64FastISel::SelectBitCast(const Instruction *I) {
2418 if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
2420 if (!isTypeLegal(I->getType(), RetVT))
2424 if (RetVT == MVT::f32 && SrcVT == MVT::i32)
2425 Opc = AArch64::FMOVWSr;
2426 else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
2427 Opc = AArch64::FMOVXDr;
2428 else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
2429 Opc = AArch64::FMOVSWr;
2430 else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
2431 Opc = AArch64::FMOVDXr;
2435 unsigned Op0Reg = getRegForValue(I->getOperand(0));
2438 bool Op0IsKill = hasTrivialKill(I->getOperand(0));
2439 unsigned ResultReg = FastEmitInst_r(Opc, TLI.getRegClassFor(RetVT),
2445 UpdateValueMap(I, ResultReg);
2449 bool AArch64FastISel::TargetSelectInstruction(const Instruction *I) {
2450 switch (I->getOpcode()) {
2453 case Instruction::Load:
2454 return SelectLoad(I);
2455 case Instruction::Store:
2456 return SelectStore(I);
2457 case Instruction::Br:
2458 return SelectBranch(I);
2459 case Instruction::IndirectBr:
2460 return SelectIndirectBr(I);
2461 case Instruction::FCmp:
2462 case Instruction::ICmp:
2463 return SelectCmp(I);
2464 case Instruction::Select:
2465 return SelectSelect(I);
2466 case Instruction::FPExt:
2467 return SelectFPExt(I);
2468 case Instruction::FPTrunc:
2469 return SelectFPTrunc(I);
2470 case Instruction::FPToSI:
2471 return SelectFPToInt(I, /*Signed=*/true);
2472 case Instruction::FPToUI:
2473 return SelectFPToInt(I, /*Signed=*/false);
2474 case Instruction::SIToFP:
2475 return SelectIntToFP(I, /*Signed=*/true);
2476 case Instruction::UIToFP:
2477 return SelectIntToFP(I, /*Signed=*/false);
2478 case Instruction::SRem:
2479 return SelectRem(I, ISD::SREM);
2480 case Instruction::URem:
2481 return SelectRem(I, ISD::UREM);
2482 case Instruction::Ret:
2483 return SelectRet(I);
2484 case Instruction::Trunc:
2485 return SelectTrunc(I);
2486 case Instruction::ZExt:
2487 case Instruction::SExt:
2488 return SelectIntExt(I);
2490 // FIXME: All of these should really be handled by the target-independent
2491 // selector -> improve FastISel tblgen.
2492 case Instruction::Mul:
2493 return SelectMul(I);
2494 case Instruction::Shl:
2495 return SelectShift(I, /*IsLeftShift=*/true, /*IsArithmetic=*/false);
2496 case Instruction::LShr:
2497 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/false);
2498 case Instruction::AShr:
2499 return SelectShift(I, /*IsLeftShift=*/false, /*IsArithmetic=*/true);
2500 case Instruction::BitCast:
2501 return SelectBitCast(I);
2504 // Silence warnings.
2505 (void)&CC_AArch64_DarwinPCS_VarArg;
2509 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &funcInfo,
2510 const TargetLibraryInfo *libInfo) {
2511 return new AArch64FastISel(funcInfo, libInfo);