blob: c042447ce5bd45057ad7e7da05a6de97f4440889 [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===-- AArch6464FastISel.cpp - AArch64 FastISel implementation -----------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#include "AArch64.h"
17#include "AArch64TargetMachine.h"
18#include "AArch64Subtarget.h"
Tim Northover3b0846e2014-05-24 12:50:23 +000019#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"
38using namespace llvm;
39
40namespace {
41
42class AArch64FastISel : public FastISel {
43
44 class Address {
45 public:
46 typedef enum {
47 RegBase,
48 FrameIndexBase
49 } BaseKind;
50
51 private:
52 BaseKind Kind;
53 union {
54 unsigned Reg;
55 int FI;
56 } Base;
57 int64_t Offset;
58
59 public:
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!");
67 Base.Reg = Reg;
68 }
69 unsigned getReg() const {
70 assert(isRegBase() && "Invalid base register access!");
71 return Base.Reg;
72 }
73 void setFI(unsigned FI) {
74 assert(isFIBase() && "Invalid base frame index access!");
75 Base.FI = FI;
76 }
77 unsigned getFI() const {
78 assert(isFIBase() && "Invalid base frame index access!");
79 return Base.FI;
80 }
81 void setOffset(int64_t O) { Offset = O; }
82 int64_t getOffset() { return Offset; }
83
84 bool isValid() { return isFIBase() || (isRegBase() && getReg() != 0); }
85 };
86
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;
90 LLVMContext *Context;
91
Juergen Ributzka2581fa52014-07-22 23:14:58 +000092 bool FastLowerCall(CallLoweringInfo &CLI) override;
93 bool FastLowerIntrinsicCall(const IntrinsicInst *II) override;
94
Tim Northover3b0846e2014-05-24 12:50:23 +000095private:
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);
Tim Northover3b0846e2014-05-24 12:50:23 +0000108 bool SelectRet(const Instruction *I);
109 bool SelectTrunc(const Instruction *I);
110 bool SelectIntExt(const Instruction *I);
111 bool SelectMul(const Instruction *I);
112
113 // Utility helper routines.
114 bool isTypeLegal(Type *Ty, MVT &VT);
115 bool isLoadStoreTypeLegal(Type *Ty, MVT &VT);
116 bool ComputeAddress(const Value *Obj, Address &Addr);
117 bool SimplifyAddress(Address &Addr, MVT VT, int64_t ScaleFactor,
118 bool UseUnscaled);
119 void AddLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
120 unsigned Flags, bool UseUnscaled);
121 bool IsMemCpySmall(uint64_t Len, unsigned Alignment);
122 bool TryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
123 unsigned Alignment);
124 // Emit functions.
125 bool EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt);
126 bool EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
127 bool UseUnscaled = false);
128 bool EmitStore(MVT VT, unsigned SrcReg, Address Addr,
129 bool UseUnscaled = false);
130 unsigned EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
131 unsigned Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
132
133 unsigned AArch64MaterializeFP(const ConstantFP *CFP, MVT VT);
134 unsigned AArch64MaterializeGV(const GlobalValue *GV);
135
136 // Call handling routines.
137private:
138 CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
Juergen Ributzka2581fa52014-07-22 23:14:58 +0000139 bool ProcessCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
Tim Northover3b0846e2014-05-24 12:50:23 +0000140 unsigned &NumBytes);
Juergen Ributzka1b014502014-07-23 20:03:13 +0000141 bool FinishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
Tim Northover3b0846e2014-05-24 12:50:23 +0000142
143public:
144 // Backend specific FastISel code.
145 unsigned TargetMaterializeAlloca(const AllocaInst *AI) override;
146 unsigned TargetMaterializeConstant(const Constant *C) override;
147
148 explicit AArch64FastISel(FunctionLoweringInfo &funcInfo,
149 const TargetLibraryInfo *libInfo)
150 : FastISel(funcInfo, libInfo) {
151 Subtarget = &TM.getSubtarget<AArch64Subtarget>();
152 Context = &funcInfo.Fn->getContext();
153 }
154
155 bool TargetSelectInstruction(const Instruction *I) override;
156
157#include "AArch64GenFastISel.inc"
158};
159
160} // end anonymous namespace
161
162#include "AArch64GenCallingConv.inc"
163
164CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
165 if (CC == CallingConv::WebKit_JS)
166 return CC_AArch64_WebKit_JS;
167 return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
168}
169
170unsigned AArch64FastISel::TargetMaterializeAlloca(const AllocaInst *AI) {
171 assert(TLI.getValueType(AI->getType(), true) == MVT::i64 &&
172 "Alloca should always return a pointer.");
173
174 // Don't handle dynamic allocas.
175 if (!FuncInfo.StaticAllocaMap.count(AI))
176 return 0;
177
178 DenseMap<const AllocaInst *, int>::iterator SI =
179 FuncInfo.StaticAllocaMap.find(AI);
180
181 if (SI != FuncInfo.StaticAllocaMap.end()) {
182 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
183 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
184 ResultReg)
185 .addFrameIndex(SI->second)
186 .addImm(0)
187 .addImm(0);
188 return ResultReg;
189 }
190
191 return 0;
192}
193
194unsigned AArch64FastISel::AArch64MaterializeFP(const ConstantFP *CFP, MVT VT) {
195 if (VT != MVT::f32 && VT != MVT::f64)
196 return 0;
197
198 const APFloat Val = CFP->getValueAPF();
199 bool is64bit = (VT == MVT::f64);
200
201 // This checks to see if we can use FMOV instructions to materialize
202 // a constant, otherwise we have to materialize via the constant pool.
203 if (TLI.isFPImmLegal(Val, VT)) {
204 int Imm;
205 unsigned Opc;
206 if (is64bit) {
207 Imm = AArch64_AM::getFP64Imm(Val);
208 Opc = AArch64::FMOVDi;
209 } else {
210 Imm = AArch64_AM::getFP32Imm(Val);
211 Opc = AArch64::FMOVSi;
212 }
213 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
214 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
215 .addImm(Imm);
216 return ResultReg;
217 }
218
219 // Materialize via constant pool. MachineConstantPool wants an explicit
220 // alignment.
221 unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
222 if (Align == 0)
223 Align = DL.getTypeAllocSize(CFP->getType());
224
225 unsigned Idx = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
226 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
227 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
228 ADRPReg).addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGE);
229
230 unsigned Opc = is64bit ? AArch64::LDRDui : AArch64::LDRSui;
231 unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
232 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
233 .addReg(ADRPReg)
234 .addConstantPoolIndex(Idx, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
235 return ResultReg;
236}
237
238unsigned AArch64FastISel::AArch64MaterializeGV(const GlobalValue *GV) {
Rafael Espindola59f7eba2014-05-28 18:15:43 +0000239 // We can't handle thread-local variables quickly yet.
240 if (GV->isThreadLocal())
241 return 0;
Tim Northover3b0846e2014-05-24 12:50:23 +0000242
Tim Northover391f93a2014-05-24 19:45:41 +0000243 // MachO still uses GOT for large code-model accesses, but ELF requires
244 // movz/movk sequences, which FastISel doesn't handle yet.
245 if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
246 return 0;
247
Tim Northover3b0846e2014-05-24 12:50:23 +0000248 unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
249
250 EVT DestEVT = TLI.getValueType(GV->getType(), true);
251 if (!DestEVT.isSimple())
252 return 0;
253
254 unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
255 unsigned ResultReg;
256
257 if (OpFlags & AArch64II::MO_GOT) {
258 // ADRP + LDRX
259 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
260 ADRPReg)
261 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
262
263 ResultReg = createResultReg(&AArch64::GPR64RegClass);
264 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
265 ResultReg)
266 .addReg(ADRPReg)
267 .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
268 AArch64II::MO_NC);
269 } else {
270 // ADRP + ADDX
271 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
272 ADRPReg).addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
273
274 ResultReg = createResultReg(&AArch64::GPR64spRegClass);
275 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
276 ResultReg)
277 .addReg(ADRPReg)
278 .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
279 .addImm(0);
280 }
281 return ResultReg;
282}
283
284unsigned AArch64FastISel::TargetMaterializeConstant(const Constant *C) {
285 EVT CEVT = TLI.getValueType(C->getType(), true);
286
287 // Only handle simple types.
288 if (!CEVT.isSimple())
289 return 0;
290 MVT VT = CEVT.getSimpleVT();
291
292 // FIXME: Handle ConstantInt.
293 if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
294 return AArch64MaterializeFP(CFP, VT);
295 else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
296 return AArch64MaterializeGV(GV);
297
298 return 0;
299}
300
301// Computes the address to get to an object.
302bool AArch64FastISel::ComputeAddress(const Value *Obj, Address &Addr) {
303 const User *U = nullptr;
304 unsigned Opcode = Instruction::UserOp1;
305 if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
306 // Don't walk into other basic blocks unless the object is an alloca from
307 // another block, otherwise it may not have a virtual register assigned.
308 if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
309 FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
310 Opcode = I->getOpcode();
311 U = I;
312 }
313 } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
314 Opcode = C->getOpcode();
315 U = C;
316 }
317
318 if (const PointerType *Ty = dyn_cast<PointerType>(Obj->getType()))
319 if (Ty->getAddressSpace() > 255)
320 // Fast instruction selection doesn't support the special
321 // address spaces.
322 return false;
323
324 switch (Opcode) {
325 default:
326 break;
327 case Instruction::BitCast: {
328 // Look through bitcasts.
329 return ComputeAddress(U->getOperand(0), Addr);
330 }
331 case Instruction::IntToPtr: {
332 // Look past no-op inttoptrs.
333 if (TLI.getValueType(U->getOperand(0)->getType()) == TLI.getPointerTy())
334 return ComputeAddress(U->getOperand(0), Addr);
335 break;
336 }
337 case Instruction::PtrToInt: {
338 // Look past no-op ptrtoints.
339 if (TLI.getValueType(U->getType()) == TLI.getPointerTy())
340 return ComputeAddress(U->getOperand(0), Addr);
341 break;
342 }
343 case Instruction::GetElementPtr: {
344 Address SavedAddr = Addr;
345 uint64_t TmpOffset = Addr.getOffset();
346
347 // Iterate through the GEP folding the constants into offsets where
348 // we can.
349 gep_type_iterator GTI = gep_type_begin(U);
350 for (User::const_op_iterator i = U->op_begin() + 1, e = U->op_end(); i != e;
351 ++i, ++GTI) {
352 const Value *Op = *i;
353 if (StructType *STy = dyn_cast<StructType>(*GTI)) {
354 const StructLayout *SL = DL.getStructLayout(STy);
355 unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
356 TmpOffset += SL->getElementOffset(Idx);
357 } else {
358 uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
359 for (;;) {
360 if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
361 // Constant-offset addressing.
362 TmpOffset += CI->getSExtValue() * S;
363 break;
364 }
365 if (canFoldAddIntoGEP(U, Op)) {
366 // A compatible add with a constant operand. Fold the constant.
367 ConstantInt *CI =
368 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
369 TmpOffset += CI->getSExtValue() * S;
370 // Iterate on the other operand.
371 Op = cast<AddOperator>(Op)->getOperand(0);
372 continue;
373 }
374 // Unsupported
375 goto unsupported_gep;
376 }
377 }
378 }
379
380 // Try to grab the base operand now.
381 Addr.setOffset(TmpOffset);
382 if (ComputeAddress(U->getOperand(0), Addr))
383 return true;
384
385 // We failed, restore everything and try the other options.
386 Addr = SavedAddr;
387
388 unsupported_gep:
389 break;
390 }
391 case Instruction::Alloca: {
392 const AllocaInst *AI = cast<AllocaInst>(Obj);
393 DenseMap<const AllocaInst *, int>::iterator SI =
394 FuncInfo.StaticAllocaMap.find(AI);
395 if (SI != FuncInfo.StaticAllocaMap.end()) {
396 Addr.setKind(Address::FrameIndexBase);
397 Addr.setFI(SI->second);
398 return true;
399 }
400 break;
401 }
402 }
403
404 // Try to get this in a register if nothing else has worked.
405 if (!Addr.isValid())
406 Addr.setReg(getRegForValue(Obj));
407 return Addr.isValid();
408}
409
410bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
411 EVT evt = TLI.getValueType(Ty, true);
412
413 // Only handle simple types.
414 if (evt == MVT::Other || !evt.isSimple())
415 return false;
416 VT = evt.getSimpleVT();
417
418 // This is a legal type, but it's not something we handle in fast-isel.
419 if (VT == MVT::f128)
420 return false;
421
422 // Handle all other legal types, i.e. a register that will directly hold this
423 // value.
424 return TLI.isTypeLegal(VT);
425}
426
427bool AArch64FastISel::isLoadStoreTypeLegal(Type *Ty, MVT &VT) {
428 if (isTypeLegal(Ty, VT))
429 return true;
430
431 // If this is a type than can be sign or zero-extended to a basic operation
432 // go ahead and accept it now. For stores, this reflects truncation.
433 if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
434 return true;
435
436 return false;
437}
438
439bool AArch64FastISel::SimplifyAddress(Address &Addr, MVT VT,
440 int64_t ScaleFactor, bool UseUnscaled) {
441 bool needsLowering = false;
442 int64_t Offset = Addr.getOffset();
443 switch (VT.SimpleTy) {
444 default:
445 return false;
446 case MVT::i1:
447 case MVT::i8:
448 case MVT::i16:
449 case MVT::i32:
450 case MVT::i64:
451 case MVT::f32:
452 case MVT::f64:
453 if (!UseUnscaled)
454 // Using scaled, 12-bit, unsigned immediate offsets.
455 needsLowering = ((Offset & 0xfff) != Offset);
456 else
457 // Using unscaled, 9-bit, signed immediate offsets.
458 needsLowering = (Offset > 256 || Offset < -256);
459 break;
460 }
461
Tim Northoverc141ad42014-06-10 09:52:44 +0000462 //If this is a stack pointer and the offset needs to be simplified then put
463 // the alloca address into a register, set the base type back to register and
464 // continue. This should almost never happen.
Tim Northover3b0846e2014-05-24 12:50:23 +0000465 if (needsLowering && Addr.getKind() == Address::FrameIndexBase) {
Tim Northoverc141ad42014-06-10 09:52:44 +0000466 unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
467 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
468 ResultReg)
469 .addFrameIndex(Addr.getFI())
470 .addImm(0)
471 .addImm(0);
472 Addr.setKind(Address::RegBase);
473 Addr.setReg(ResultReg);
Tim Northover3b0846e2014-05-24 12:50:23 +0000474 }
475
476 // Since the offset is too large for the load/store instruction get the
477 // reg+offset into a register.
478 if (needsLowering) {
479 uint64_t UnscaledOffset = Addr.getOffset() * ScaleFactor;
480 unsigned ResultReg = FastEmit_ri_(MVT::i64, ISD::ADD, Addr.getReg(), false,
481 UnscaledOffset, MVT::i64);
482 if (ResultReg == 0)
483 return false;
484 Addr.setReg(ResultReg);
485 Addr.setOffset(0);
486 }
487 return true;
488}
489
490void AArch64FastISel::AddLoadStoreOperands(Address &Addr,
491 const MachineInstrBuilder &MIB,
492 unsigned Flags, bool UseUnscaled) {
493 int64_t Offset = Addr.getOffset();
494 // Frame base works a bit differently. Handle it separately.
495 if (Addr.getKind() == Address::FrameIndexBase) {
496 int FI = Addr.getFI();
497 // FIXME: We shouldn't be using getObjectSize/getObjectAlignment. The size
498 // and alignment should be based on the VT.
499 MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
500 MachinePointerInfo::getFixedStack(FI, Offset), Flags,
501 MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
502 // Now add the rest of the operands.
503 MIB.addFrameIndex(FI).addImm(Offset).addMemOperand(MMO);
504 } else {
505 // Now add the rest of the operands.
506 MIB.addReg(Addr.getReg());
507 MIB.addImm(Offset);
508 }
509}
510
511bool AArch64FastISel::EmitLoad(MVT VT, unsigned &ResultReg, Address Addr,
512 bool UseUnscaled) {
513 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
514 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
515 if (!UseUnscaled && Addr.getOffset() < 0)
516 UseUnscaled = true;
517
518 unsigned Opc;
519 const TargetRegisterClass *RC;
520 bool VTIsi1 = false;
521 int64_t ScaleFactor = 0;
522 switch (VT.SimpleTy) {
523 default:
524 return false;
525 case MVT::i1:
526 VTIsi1 = true;
527 // Intentional fall-through.
528 case MVT::i8:
529 Opc = UseUnscaled ? AArch64::LDURBBi : AArch64::LDRBBui;
530 RC = &AArch64::GPR32RegClass;
531 ScaleFactor = 1;
532 break;
533 case MVT::i16:
534 Opc = UseUnscaled ? AArch64::LDURHHi : AArch64::LDRHHui;
535 RC = &AArch64::GPR32RegClass;
536 ScaleFactor = 2;
537 break;
538 case MVT::i32:
539 Opc = UseUnscaled ? AArch64::LDURWi : AArch64::LDRWui;
540 RC = &AArch64::GPR32RegClass;
541 ScaleFactor = 4;
542 break;
543 case MVT::i64:
544 Opc = UseUnscaled ? AArch64::LDURXi : AArch64::LDRXui;
545 RC = &AArch64::GPR64RegClass;
546 ScaleFactor = 8;
547 break;
548 case MVT::f32:
549 Opc = UseUnscaled ? AArch64::LDURSi : AArch64::LDRSui;
550 RC = TLI.getRegClassFor(VT);
551 ScaleFactor = 4;
552 break;
553 case MVT::f64:
554 Opc = UseUnscaled ? AArch64::LDURDi : AArch64::LDRDui;
555 RC = TLI.getRegClassFor(VT);
556 ScaleFactor = 8;
557 break;
558 }
559 // Scale the offset.
560 if (!UseUnscaled) {
561 int64_t Offset = Addr.getOffset();
562 if (Offset & (ScaleFactor - 1))
563 // Retry using an unscaled, 9-bit, signed immediate offset.
564 return EmitLoad(VT, ResultReg, Addr, /*UseUnscaled*/ true);
565
566 Addr.setOffset(Offset / ScaleFactor);
567 }
568
569 // Simplify this down to something we can handle.
570 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled))
571 return false;
572
573 // Create the base instruction, then add the operands.
574 ResultReg = createResultReg(RC);
575 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
576 TII.get(Opc), ResultReg);
577 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, UseUnscaled);
578
579 // Loading an i1 requires special handling.
580 if (VTIsi1) {
581 MRI.constrainRegClass(ResultReg, &AArch64::GPR32RegClass);
582 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
583 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
584 ANDReg)
585 .addReg(ResultReg)
586 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
587 ResultReg = ANDReg;
588 }
589 return true;
590}
591
592bool AArch64FastISel::SelectLoad(const Instruction *I) {
593 MVT VT;
594 // Verify we have a legal type before going any further. Currently, we handle
595 // simple types that will directly fit in a register (i32/f32/i64/f64) or
596 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
597 if (!isLoadStoreTypeLegal(I->getType(), VT) || cast<LoadInst>(I)->isAtomic())
598 return false;
599
600 // See if we can handle this address.
601 Address Addr;
602 if (!ComputeAddress(I->getOperand(0), Addr))
603 return false;
604
605 unsigned ResultReg;
606 if (!EmitLoad(VT, ResultReg, Addr))
607 return false;
608
609 UpdateValueMap(I, ResultReg);
610 return true;
611}
612
613bool AArch64FastISel::EmitStore(MVT VT, unsigned SrcReg, Address Addr,
614 bool UseUnscaled) {
615 // Negative offsets require unscaled, 9-bit, signed immediate offsets.
616 // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
617 if (!UseUnscaled && Addr.getOffset() < 0)
618 UseUnscaled = true;
619
620 unsigned StrOpc;
621 bool VTIsi1 = false;
622 int64_t ScaleFactor = 0;
623 // Using scaled, 12-bit, unsigned immediate offsets.
624 switch (VT.SimpleTy) {
625 default:
626 return false;
627 case MVT::i1:
628 VTIsi1 = true;
629 case MVT::i8:
630 StrOpc = UseUnscaled ? AArch64::STURBBi : AArch64::STRBBui;
631 ScaleFactor = 1;
632 break;
633 case MVT::i16:
634 StrOpc = UseUnscaled ? AArch64::STURHHi : AArch64::STRHHui;
635 ScaleFactor = 2;
636 break;
637 case MVT::i32:
638 StrOpc = UseUnscaled ? AArch64::STURWi : AArch64::STRWui;
639 ScaleFactor = 4;
640 break;
641 case MVT::i64:
642 StrOpc = UseUnscaled ? AArch64::STURXi : AArch64::STRXui;
643 ScaleFactor = 8;
644 break;
645 case MVT::f32:
646 StrOpc = UseUnscaled ? AArch64::STURSi : AArch64::STRSui;
647 ScaleFactor = 4;
648 break;
649 case MVT::f64:
650 StrOpc = UseUnscaled ? AArch64::STURDi : AArch64::STRDui;
651 ScaleFactor = 8;
652 break;
653 }
654 // Scale the offset.
655 if (!UseUnscaled) {
656 int64_t Offset = Addr.getOffset();
657 if (Offset & (ScaleFactor - 1))
658 // Retry using an unscaled, 9-bit, signed immediate offset.
659 return EmitStore(VT, SrcReg, Addr, /*UseUnscaled*/ true);
660
661 Addr.setOffset(Offset / ScaleFactor);
662 }
663
664 // Simplify this down to something we can handle.
665 if (!SimplifyAddress(Addr, VT, UseUnscaled ? 1 : ScaleFactor, UseUnscaled))
666 return false;
667
668 // Storing an i1 requires special handling.
669 if (VTIsi1) {
670 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
671 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
672 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
673 ANDReg)
674 .addReg(SrcReg)
675 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
676 SrcReg = ANDReg;
677 }
678 // Create the base instruction, then add the operands.
679 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
680 TII.get(StrOpc)).addReg(SrcReg);
681 AddLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, UseUnscaled);
682 return true;
683}
684
685bool AArch64FastISel::SelectStore(const Instruction *I) {
686 MVT VT;
687 Value *Op0 = I->getOperand(0);
688 // Verify we have a legal type before going any further. Currently, we handle
689 // simple types that will directly fit in a register (i32/f32/i64/f64) or
690 // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
691 if (!isLoadStoreTypeLegal(Op0->getType(), VT) ||
692 cast<StoreInst>(I)->isAtomic())
693 return false;
694
695 // Get the value to be stored into a register.
696 unsigned SrcReg = getRegForValue(Op0);
697 if (SrcReg == 0)
698 return false;
699
700 // See if we can handle this address.
701 Address Addr;
702 if (!ComputeAddress(I->getOperand(1), Addr))
703 return false;
704
705 if (!EmitStore(VT, SrcReg, Addr))
706 return false;
707 return true;
708}
709
710static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
711 switch (Pred) {
712 case CmpInst::FCMP_ONE:
713 case CmpInst::FCMP_UEQ:
714 default:
715 // AL is our "false" for now. The other two need more compares.
716 return AArch64CC::AL;
717 case CmpInst::ICMP_EQ:
718 case CmpInst::FCMP_OEQ:
719 return AArch64CC::EQ;
720 case CmpInst::ICMP_SGT:
721 case CmpInst::FCMP_OGT:
722 return AArch64CC::GT;
723 case CmpInst::ICMP_SGE:
724 case CmpInst::FCMP_OGE:
725 return AArch64CC::GE;
726 case CmpInst::ICMP_UGT:
727 case CmpInst::FCMP_UGT:
728 return AArch64CC::HI;
729 case CmpInst::FCMP_OLT:
730 return AArch64CC::MI;
731 case CmpInst::ICMP_ULE:
732 case CmpInst::FCMP_OLE:
733 return AArch64CC::LS;
734 case CmpInst::FCMP_ORD:
735 return AArch64CC::VC;
736 case CmpInst::FCMP_UNO:
737 return AArch64CC::VS;
738 case CmpInst::FCMP_UGE:
739 return AArch64CC::PL;
740 case CmpInst::ICMP_SLT:
741 case CmpInst::FCMP_ULT:
742 return AArch64CC::LT;
743 case CmpInst::ICMP_SLE:
744 case CmpInst::FCMP_ULE:
745 return AArch64CC::LE;
746 case CmpInst::FCMP_UNE:
747 case CmpInst::ICMP_NE:
748 return AArch64CC::NE;
749 case CmpInst::ICMP_UGE:
750 return AArch64CC::HS;
751 case CmpInst::ICMP_ULT:
752 return AArch64CC::LO;
753 }
754}
755
756bool AArch64FastISel::SelectBranch(const Instruction *I) {
757 const BranchInst *BI = cast<BranchInst>(I);
758 MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
759 MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
760
761 if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
762 if (CI->hasOneUse() && (CI->getParent() == I->getParent())) {
763 // We may not handle every CC for now.
764 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate());
765 if (CC == AArch64CC::AL)
766 return false;
767
768 // Emit the cmp.
769 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
770 return false;
771
772 // Emit the branch.
773 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
774 .addImm(CC)
775 .addMBB(TBB);
776 FuncInfo.MBB->addSuccessor(TBB);
777
778 FastEmitBranch(FBB, DbgLoc);
779 return true;
780 }
781 } else if (TruncInst *TI = dyn_cast<TruncInst>(BI->getCondition())) {
782 MVT SrcVT;
783 if (TI->hasOneUse() && TI->getParent() == I->getParent() &&
784 (isLoadStoreTypeLegal(TI->getOperand(0)->getType(), SrcVT))) {
785 unsigned CondReg = getRegForValue(TI->getOperand(0));
786 if (CondReg == 0)
787 return false;
788
789 // Issue an extract_subreg to get the lower 32-bits.
790 if (SrcVT == MVT::i64)
791 CondReg = FastEmitInst_extractsubreg(MVT::i32, CondReg, /*Kill=*/true,
792 AArch64::sub_32);
793
794 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
795 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
796 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
797 TII.get(AArch64::ANDWri), ANDReg)
798 .addReg(CondReg)
799 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
800 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
801 TII.get(AArch64::SUBSWri))
802 .addReg(ANDReg)
803 .addReg(ANDReg)
804 .addImm(0)
805 .addImm(0);
806
807 unsigned CC = AArch64CC::NE;
808 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
809 std::swap(TBB, FBB);
810 CC = AArch64CC::EQ;
811 }
812 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
813 .addImm(CC)
814 .addMBB(TBB);
815 FuncInfo.MBB->addSuccessor(TBB);
816 FastEmitBranch(FBB, DbgLoc);
817 return true;
818 }
819 } else if (const ConstantInt *CI =
820 dyn_cast<ConstantInt>(BI->getCondition())) {
821 uint64_t Imm = CI->getZExtValue();
822 MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
823 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
824 .addMBB(Target);
825 FuncInfo.MBB->addSuccessor(Target);
826 return true;
827 }
828
829 unsigned CondReg = getRegForValue(BI->getCondition());
830 if (CondReg == 0)
831 return false;
832
833 // We've been divorced from our compare! Our block was split, and
834 // now our compare lives in a predecessor block. We musn't
835 // re-compare here, as the children of the compare aren't guaranteed
836 // live across the block boundary (we *could* check for this).
837 // Regardless, the compare has been done in the predecessor block,
838 // and it left a value for us in a virtual register. Ergo, we test
839 // the one-bit value left in the virtual register.
840 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri),
841 AArch64::WZR)
842 .addReg(CondReg)
843 .addImm(0)
844 .addImm(0);
845
846 unsigned CC = AArch64CC::NE;
847 if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
848 std::swap(TBB, FBB);
849 CC = AArch64CC::EQ;
850 }
851
852 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
853 .addImm(CC)
854 .addMBB(TBB);
855 FuncInfo.MBB->addSuccessor(TBB);
856 FastEmitBranch(FBB, DbgLoc);
857 return true;
858}
859
860bool AArch64FastISel::SelectIndirectBr(const Instruction *I) {
861 const IndirectBrInst *BI = cast<IndirectBrInst>(I);
862 unsigned AddrReg = getRegForValue(BI->getOperand(0));
863 if (AddrReg == 0)
864 return false;
865
866 // Emit the indirect branch.
867 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BR))
868 .addReg(AddrReg);
869
870 // Make sure the CFG is up-to-date.
871 for (unsigned i = 0, e = BI->getNumSuccessors(); i != e; ++i)
872 FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[BI->getSuccessor(i)]);
873
874 return true;
875}
876
877bool AArch64FastISel::EmitCmp(Value *Src1Value, Value *Src2Value, bool isZExt) {
878 Type *Ty = Src1Value->getType();
879 EVT SrcEVT = TLI.getValueType(Ty, true);
880 if (!SrcEVT.isSimple())
881 return false;
882 MVT SrcVT = SrcEVT.getSimpleVT();
883
884 // Check to see if the 2nd operand is a constant that we can encode directly
885 // in the compare.
886 uint64_t Imm;
887 bool UseImm = false;
888 bool isNegativeImm = false;
889 if (const ConstantInt *ConstInt = dyn_cast<ConstantInt>(Src2Value)) {
890 if (SrcVT == MVT::i64 || SrcVT == MVT::i32 || SrcVT == MVT::i16 ||
891 SrcVT == MVT::i8 || SrcVT == MVT::i1) {
892 const APInt &CIVal = ConstInt->getValue();
893
894 Imm = (isZExt) ? CIVal.getZExtValue() : CIVal.getSExtValue();
895 if (CIVal.isNegative()) {
896 isNegativeImm = true;
897 Imm = -Imm;
898 }
899 // FIXME: We can handle more immediates using shifts.
900 UseImm = ((Imm & 0xfff) == Imm);
901 }
902 } else if (const ConstantFP *ConstFP = dyn_cast<ConstantFP>(Src2Value)) {
903 if (SrcVT == MVT::f32 || SrcVT == MVT::f64)
904 if (ConstFP->isZero() && !ConstFP->isNegative())
905 UseImm = true;
906 }
907
908 unsigned ZReg;
909 unsigned CmpOpc;
910 bool isICmp = true;
911 bool needsExt = false;
912 switch (SrcVT.SimpleTy) {
913 default:
914 return false;
915 case MVT::i1:
916 case MVT::i8:
917 case MVT::i16:
918 needsExt = true;
919 // Intentional fall-through.
920 case MVT::i32:
921 ZReg = AArch64::WZR;
922 if (UseImm)
923 CmpOpc = isNegativeImm ? AArch64::ADDSWri : AArch64::SUBSWri;
924 else
925 CmpOpc = AArch64::SUBSWrr;
926 break;
927 case MVT::i64:
928 ZReg = AArch64::XZR;
929 if (UseImm)
930 CmpOpc = isNegativeImm ? AArch64::ADDSXri : AArch64::SUBSXri;
931 else
932 CmpOpc = AArch64::SUBSXrr;
933 break;
934 case MVT::f32:
935 isICmp = false;
936 CmpOpc = UseImm ? AArch64::FCMPSri : AArch64::FCMPSrr;
937 break;
938 case MVT::f64:
939 isICmp = false;
940 CmpOpc = UseImm ? AArch64::FCMPDri : AArch64::FCMPDrr;
941 break;
942 }
943
944 unsigned SrcReg1 = getRegForValue(Src1Value);
945 if (SrcReg1 == 0)
946 return false;
947
948 unsigned SrcReg2;
949 if (!UseImm) {
950 SrcReg2 = getRegForValue(Src2Value);
951 if (SrcReg2 == 0)
952 return false;
953 }
954
955 // We have i1, i8, or i16, we need to either zero extend or sign extend.
956 if (needsExt) {
957 SrcReg1 = EmitIntExt(SrcVT, SrcReg1, MVT::i32, isZExt);
958 if (SrcReg1 == 0)
959 return false;
960 if (!UseImm) {
961 SrcReg2 = EmitIntExt(SrcVT, SrcReg2, MVT::i32, isZExt);
962 if (SrcReg2 == 0)
963 return false;
964 }
965 }
966
967 if (isICmp) {
968 if (UseImm)
969 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
970 .addReg(ZReg)
971 .addReg(SrcReg1)
972 .addImm(Imm)
973 .addImm(0);
974 else
975 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
976 .addReg(ZReg)
977 .addReg(SrcReg1)
978 .addReg(SrcReg2);
979 } else {
980 if (UseImm)
981 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
982 .addReg(SrcReg1);
983 else
984 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(CmpOpc))
985 .addReg(SrcReg1)
986 .addReg(SrcReg2);
987 }
988 return true;
989}
990
991bool AArch64FastISel::SelectCmp(const Instruction *I) {
992 const CmpInst *CI = cast<CmpInst>(I);
993
994 // We may not handle every CC for now.
995 AArch64CC::CondCode CC = getCompareCC(CI->getPredicate());
996 if (CC == AArch64CC::AL)
997 return false;
998
999 // Emit the cmp.
1000 if (!EmitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
1001 return false;
1002
1003 // Now set a register based on the comparison.
1004 AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
1005 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1006 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
1007 ResultReg)
1008 .addReg(AArch64::WZR)
1009 .addReg(AArch64::WZR)
1010 .addImm(invertedCC);
1011
1012 UpdateValueMap(I, ResultReg);
1013 return true;
1014}
1015
1016bool AArch64FastISel::SelectSelect(const Instruction *I) {
1017 const SelectInst *SI = cast<SelectInst>(I);
1018
1019 EVT DestEVT = TLI.getValueType(SI->getType(), true);
1020 if (!DestEVT.isSimple())
1021 return false;
1022
1023 MVT DestVT = DestEVT.getSimpleVT();
1024 if (DestVT != MVT::i32 && DestVT != MVT::i64 && DestVT != MVT::f32 &&
1025 DestVT != MVT::f64)
1026 return false;
1027
1028 unsigned CondReg = getRegForValue(SI->getCondition());
1029 if (CondReg == 0)
1030 return false;
1031 unsigned TrueReg = getRegForValue(SI->getTrueValue());
1032 if (TrueReg == 0)
1033 return false;
1034 unsigned FalseReg = getRegForValue(SI->getFalseValue());
1035 if (FalseReg == 0)
1036 return false;
1037
1038
1039 MRI.constrainRegClass(CondReg, &AArch64::GPR32RegClass);
1040 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1041 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1042 ANDReg)
1043 .addReg(CondReg)
1044 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1045
1046 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SUBSWri))
1047 .addReg(ANDReg)
1048 .addReg(ANDReg)
1049 .addImm(0)
1050 .addImm(0);
1051
1052 unsigned SelectOpc;
1053 switch (DestVT.SimpleTy) {
1054 default:
1055 return false;
1056 case MVT::i32:
1057 SelectOpc = AArch64::CSELWr;
1058 break;
1059 case MVT::i64:
1060 SelectOpc = AArch64::CSELXr;
1061 break;
1062 case MVT::f32:
1063 SelectOpc = AArch64::FCSELSrrr;
1064 break;
1065 case MVT::f64:
1066 SelectOpc = AArch64::FCSELDrrr;
1067 break;
1068 }
1069
1070 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1071 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(SelectOpc),
1072 ResultReg)
1073 .addReg(TrueReg)
1074 .addReg(FalseReg)
1075 .addImm(AArch64CC::NE);
1076
1077 UpdateValueMap(I, ResultReg);
1078 return true;
1079}
1080
1081bool AArch64FastISel::SelectFPExt(const Instruction *I) {
1082 Value *V = I->getOperand(0);
1083 if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
1084 return false;
1085
1086 unsigned Op = getRegForValue(V);
1087 if (Op == 0)
1088 return false;
1089
1090 unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
1091 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
1092 ResultReg).addReg(Op);
1093 UpdateValueMap(I, ResultReg);
1094 return true;
1095}
1096
1097bool AArch64FastISel::SelectFPTrunc(const Instruction *I) {
1098 Value *V = I->getOperand(0);
1099 if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
1100 return false;
1101
1102 unsigned Op = getRegForValue(V);
1103 if (Op == 0)
1104 return false;
1105
1106 unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
1107 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
1108 ResultReg).addReg(Op);
1109 UpdateValueMap(I, ResultReg);
1110 return true;
1111}
1112
1113// FPToUI and FPToSI
1114bool AArch64FastISel::SelectFPToInt(const Instruction *I, bool Signed) {
1115 MVT DestVT;
1116 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1117 return false;
1118
1119 unsigned SrcReg = getRegForValue(I->getOperand(0));
1120 if (SrcReg == 0)
1121 return false;
1122
1123 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1124 if (SrcVT == MVT::f128)
1125 return false;
1126
1127 unsigned Opc;
1128 if (SrcVT == MVT::f64) {
1129 if (Signed)
1130 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
1131 else
1132 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
1133 } else {
1134 if (Signed)
1135 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
1136 else
1137 Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
1138 }
1139 unsigned ResultReg = createResultReg(
1140 DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
1141 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1142 .addReg(SrcReg);
1143 UpdateValueMap(I, ResultReg);
1144 return true;
1145}
1146
1147bool AArch64FastISel::SelectIntToFP(const Instruction *I, bool Signed) {
1148 MVT DestVT;
1149 if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
1150 return false;
1151 assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
1152 "Unexpected value type.");
1153
1154 unsigned SrcReg = getRegForValue(I->getOperand(0));
1155 if (SrcReg == 0)
1156 return false;
1157
1158 EVT SrcVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1159
1160 // Handle sign-extension.
1161 if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
1162 SrcReg =
1163 EmitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
1164 if (SrcReg == 0)
1165 return false;
1166 }
1167
1168 MRI.constrainRegClass(SrcReg, SrcVT == MVT::i64 ? &AArch64::GPR64RegClass
1169 : &AArch64::GPR32RegClass);
1170
1171 unsigned Opc;
1172 if (SrcVT == MVT::i64) {
1173 if (Signed)
1174 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
1175 else
1176 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
1177 } else {
1178 if (Signed)
1179 Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
1180 else
1181 Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
1182 }
1183
1184 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1185 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1186 .addReg(SrcReg);
1187 UpdateValueMap(I, ResultReg);
1188 return true;
1189}
1190
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001191bool AArch64FastISel::ProcessCallArgs(CallLoweringInfo &CLI,
1192 SmallVectorImpl<MVT> &OutVTs,
1193 unsigned &NumBytes) {
1194 CallingConv::ID CC = CLI.CallConv;
Tim Northover3b0846e2014-05-24 12:50:23 +00001195 SmallVector<CCValAssign, 16> ArgLocs;
1196 CCState CCInfo(CC, false, *FuncInfo.MF, TM, ArgLocs, *Context);
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001197 CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
Tim Northover3b0846e2014-05-24 12:50:23 +00001198
1199 // Get a count of how many bytes are to be pushed on the stack.
1200 NumBytes = CCInfo.getNextStackOffset();
1201
1202 // Issue CALLSEQ_START
1203 unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
1204 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001205 .addImm(NumBytes);
Tim Northover3b0846e2014-05-24 12:50:23 +00001206
1207 // Process the args.
1208 for (unsigned i = 0, e = ArgLocs.size(); i != e; ++i) {
1209 CCValAssign &VA = ArgLocs[i];
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001210 const Value *ArgVal = CLI.OutVals[VA.getValNo()];
1211 MVT ArgVT = OutVTs[VA.getValNo()];
1212
1213 unsigned ArgReg = getRegForValue(ArgVal);
1214 if (!ArgReg)
1215 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001216
1217 // Handle arg promotion: SExt, ZExt, AExt.
1218 switch (VA.getLocInfo()) {
1219 case CCValAssign::Full:
1220 break;
1221 case CCValAssign::SExt: {
1222 MVT DestVT = VA.getLocVT();
1223 MVT SrcVT = ArgVT;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001224 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
1225 if (!ArgReg)
Tim Northover3b0846e2014-05-24 12:50:23 +00001226 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001227 break;
1228 }
1229 case CCValAssign::AExt:
1230 // Intentional fall-through.
1231 case CCValAssign::ZExt: {
1232 MVT DestVT = VA.getLocVT();
1233 MVT SrcVT = ArgVT;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001234 ArgReg = EmitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
1235 if (!ArgReg)
Tim Northover3b0846e2014-05-24 12:50:23 +00001236 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001237 break;
1238 }
1239 default:
1240 llvm_unreachable("Unknown arg promotion!");
1241 }
1242
1243 // Now copy/store arg to correct locations.
1244 if (VA.isRegLoc() && !VA.needsCustom()) {
1245 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001246 TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
1247 CLI.OutRegs.push_back(VA.getLocReg());
Tim Northover3b0846e2014-05-24 12:50:23 +00001248 } else if (VA.needsCustom()) {
1249 // FIXME: Handle custom args.
1250 return false;
1251 } else {
1252 assert(VA.isMemLoc() && "Assuming store on stack.");
1253
1254 // Need to store on the stack.
Tim Northover6890add2014-06-03 13:54:53 +00001255 unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
Tim Northover3b0846e2014-05-24 12:50:23 +00001256
1257 unsigned BEAlign = 0;
1258 if (ArgSize < 8 && !Subtarget->isLittleEndian())
1259 BEAlign = 8 - ArgSize;
1260
1261 Address Addr;
1262 Addr.setKind(Address::RegBase);
1263 Addr.setReg(AArch64::SP);
1264 Addr.setOffset(VA.getLocMemOffset() + BEAlign);
1265
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001266 if (!EmitStore(ArgVT, ArgReg, Addr))
Tim Northover3b0846e2014-05-24 12:50:23 +00001267 return false;
1268 }
1269 }
1270 return true;
1271}
1272
Juergen Ributzka1b014502014-07-23 20:03:13 +00001273bool AArch64FastISel::FinishCall(CallLoweringInfo &CLI, MVT RetVT,
1274 unsigned NumBytes) {
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001275 CallingConv::ID CC = CLI.CallConv;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001276
Tim Northover3b0846e2014-05-24 12:50:23 +00001277 // Issue CALLSEQ_END
1278 unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
1279 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001280 .addImm(NumBytes).addImm(0);
Tim Northover3b0846e2014-05-24 12:50:23 +00001281
1282 // Now the return value.
1283 if (RetVT != MVT::isVoid) {
1284 SmallVector<CCValAssign, 16> RVLocs;
1285 CCState CCInfo(CC, false, *FuncInfo.MF, TM, RVLocs, *Context);
1286 CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
1287
1288 // Only handle a single return value.
1289 if (RVLocs.size() != 1)
1290 return false;
1291
1292 // Copy all of the result registers out of their specified physreg.
1293 MVT CopyVT = RVLocs[0].getValVT();
1294 unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
1295 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001296 TII.get(TargetOpcode::COPY), ResultReg)
1297 .addReg(RVLocs[0].getLocReg());
1298 CLI.InRegs.push_back(RVLocs[0].getLocReg());
Tim Northover3b0846e2014-05-24 12:50:23 +00001299
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001300 CLI.ResultReg = ResultReg;
1301 CLI.NumResultRegs = 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001302 }
1303
1304 return true;
1305}
1306
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001307bool AArch64FastISel::FastLowerCall(CallLoweringInfo &CLI) {
1308 CallingConv::ID CC = CLI.CallConv;
1309 bool IsVarArg = CLI.IsVarArg;
1310 const Value *Callee = CLI.Callee;
1311 const char *SymName = CLI.SymName;
Tim Northover3b0846e2014-05-24 12:50:23 +00001312
1313 // Only handle global variable Callees.
1314 const GlobalValue *GV = dyn_cast<GlobalValue>(Callee);
1315 if (!GV)
1316 return false;
1317
Tim Northover3b0846e2014-05-24 12:50:23 +00001318 // Let SDISel handle vararg functions.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001319 if (IsVarArg)
Tim Northover3b0846e2014-05-24 12:50:23 +00001320 return false;
1321
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001322 // FIXME: Only handle *simple* calls for now.
Tim Northover3b0846e2014-05-24 12:50:23 +00001323 MVT RetVT;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001324 if (CLI.RetTy->isVoidTy())
Tim Northover3b0846e2014-05-24 12:50:23 +00001325 RetVT = MVT::isVoid;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001326 else if (!isTypeLegal(CLI.RetTy, RetVT))
Tim Northover3b0846e2014-05-24 12:50:23 +00001327 return false;
1328
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001329 for (auto Flag : CLI.OutFlags)
1330 if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal())
1331 return false;
1332
Tim Northover3b0846e2014-05-24 12:50:23 +00001333 // Set up the argument vectors.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001334 SmallVector<MVT, 16> OutVTs;
1335 OutVTs.reserve(CLI.OutVals.size());
Tim Northover3b0846e2014-05-24 12:50:23 +00001336
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001337 for (auto *Val : CLI.OutVals) {
1338 MVT VT;
1339 if (!isTypeLegal(Val->getType(), VT) &&
1340 !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
Tim Northover3b0846e2014-05-24 12:50:23 +00001341 return false;
1342
1343 // We don't handle vector parameters yet.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001344 if (VT.isVector() || VT.getSizeInBits() > 64)
Tim Northover3b0846e2014-05-24 12:50:23 +00001345 return false;
1346
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001347 OutVTs.push_back(VT);
Tim Northover3b0846e2014-05-24 12:50:23 +00001348 }
1349
1350 // Handle the arguments now that we've gotten them.
Tim Northover3b0846e2014-05-24 12:50:23 +00001351 unsigned NumBytes;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001352 if (!ProcessCallArgs(CLI, OutVTs, NumBytes))
Tim Northover3b0846e2014-05-24 12:50:23 +00001353 return false;
1354
1355 // Issue the call.
1356 MachineInstrBuilder MIB;
1357 MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BL));
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001358 CLI.Call = MIB;
1359 if (!SymName)
Tim Northover3b0846e2014-05-24 12:50:23 +00001360 MIB.addGlobalAddress(GV, 0, 0);
1361 else
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001362 MIB.addExternalSymbol(SymName, 0);
Tim Northover3b0846e2014-05-24 12:50:23 +00001363
1364 // Add implicit physical register uses to the call.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001365 for (auto Reg : CLI.OutRegs)
1366 MIB.addReg(Reg, RegState::Implicit);
Tim Northover3b0846e2014-05-24 12:50:23 +00001367
1368 // Add a register mask with the call-preserved registers.
1369 // Proper defs for return values will be added by setPhysRegsDeadExcept().
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001370 MIB.addRegMask(TRI.getCallPreservedMask(CC));
Tim Northover3b0846e2014-05-24 12:50:23 +00001371
1372 // Finish off the call including any return values.
Juergen Ributzka1b014502014-07-23 20:03:13 +00001373 return FinishCall(CLI, RetVT, NumBytes);
Tim Northover3b0846e2014-05-24 12:50:23 +00001374}
1375
1376bool AArch64FastISel::IsMemCpySmall(uint64_t Len, unsigned Alignment) {
1377 if (Alignment)
1378 return Len / Alignment <= 4;
1379 else
1380 return Len < 32;
1381}
1382
1383bool AArch64FastISel::TryEmitSmallMemCpy(Address Dest, Address Src,
1384 uint64_t Len, unsigned Alignment) {
1385 // Make sure we don't bloat code by inlining very large memcpy's.
1386 if (!IsMemCpySmall(Len, Alignment))
1387 return false;
1388
1389 int64_t UnscaledOffset = 0;
1390 Address OrigDest = Dest;
1391 Address OrigSrc = Src;
1392
1393 while (Len) {
1394 MVT VT;
1395 if (!Alignment || Alignment >= 8) {
1396 if (Len >= 8)
1397 VT = MVT::i64;
1398 else if (Len >= 4)
1399 VT = MVT::i32;
1400 else if (Len >= 2)
1401 VT = MVT::i16;
1402 else {
1403 VT = MVT::i8;
1404 }
1405 } else {
1406 // Bound based on alignment.
1407 if (Len >= 4 && Alignment == 4)
1408 VT = MVT::i32;
1409 else if (Len >= 2 && Alignment == 2)
1410 VT = MVT::i16;
1411 else {
1412 VT = MVT::i8;
1413 }
1414 }
1415
1416 bool RV;
1417 unsigned ResultReg;
1418 RV = EmitLoad(VT, ResultReg, Src);
Tim Northoverc19445d2014-06-10 09:52:40 +00001419 if (!RV)
1420 return false;
1421
Tim Northover3b0846e2014-05-24 12:50:23 +00001422 RV = EmitStore(VT, ResultReg, Dest);
Tim Northoverc19445d2014-06-10 09:52:40 +00001423 if (!RV)
1424 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001425
1426 int64_t Size = VT.getSizeInBits() / 8;
1427 Len -= Size;
1428 UnscaledOffset += Size;
1429
1430 // We need to recompute the unscaled offset for each iteration.
1431 Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
1432 Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
1433 }
1434
1435 return true;
1436}
1437
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001438bool AArch64FastISel::FastLowerIntrinsicCall(const IntrinsicInst *II) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001439 // FIXME: Handle more intrinsics.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001440 switch (II->getIntrinsicID()) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001441 default:
1442 return false;
1443 case Intrinsic::memcpy:
1444 case Intrinsic::memmove: {
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001445 const auto *MTI = cast<MemTransferInst>(II);
Tim Northover3b0846e2014-05-24 12:50:23 +00001446 // Don't handle volatile.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001447 if (MTI->isVolatile())
Tim Northover3b0846e2014-05-24 12:50:23 +00001448 return false;
1449
1450 // Disable inlining for memmove before calls to ComputeAddress. Otherwise,
1451 // we would emit dead code because we don't currently handle memmoves.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001452 bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
1453 if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001454 // Small memcpy's are common enough that we want to do them without a call
1455 // if possible.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001456 uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
1457 unsigned Alignment = MTI->getAlignment();
Tim Northover3b0846e2014-05-24 12:50:23 +00001458 if (IsMemCpySmall(Len, Alignment)) {
1459 Address Dest, Src;
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001460 if (!ComputeAddress(MTI->getRawDest(), Dest) ||
1461 !ComputeAddress(MTI->getRawSource(), Src))
Tim Northover3b0846e2014-05-24 12:50:23 +00001462 return false;
1463 if (TryEmitSmallMemCpy(Dest, Src, Len, Alignment))
1464 return true;
1465 }
1466 }
1467
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001468 if (!MTI->getLength()->getType()->isIntegerTy(64))
Tim Northover3b0846e2014-05-24 12:50:23 +00001469 return false;
1470
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001471 if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
Tim Northover3b0846e2014-05-24 12:50:23 +00001472 // Fast instruction selection doesn't support the special
1473 // address spaces.
1474 return false;
1475
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001476 const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
1477 return LowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
Tim Northover3b0846e2014-05-24 12:50:23 +00001478 }
1479 case Intrinsic::memset: {
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001480 const MemSetInst *MSI = cast<MemSetInst>(II);
Tim Northover3b0846e2014-05-24 12:50:23 +00001481 // Don't handle volatile.
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001482 if (MSI->isVolatile())
Tim Northover3b0846e2014-05-24 12:50:23 +00001483 return false;
1484
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001485 if (!MSI->getLength()->getType()->isIntegerTy(64))
Tim Northover3b0846e2014-05-24 12:50:23 +00001486 return false;
1487
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001488 if (MSI->getDestAddressSpace() > 255)
Tim Northover3b0846e2014-05-24 12:50:23 +00001489 // Fast instruction selection doesn't support the special
1490 // address spaces.
1491 return false;
1492
Juergen Ributzka2581fa52014-07-22 23:14:58 +00001493 return LowerCallTo(II, "memset", II->getNumArgOperands() - 2);
Tim Northover3b0846e2014-05-24 12:50:23 +00001494 }
1495 case Intrinsic::trap: {
1496 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
1497 .addImm(1);
1498 return true;
1499 }
1500 }
1501 return false;
1502}
1503
1504bool AArch64FastISel::SelectRet(const Instruction *I) {
1505 const ReturnInst *Ret = cast<ReturnInst>(I);
1506 const Function &F = *I->getParent()->getParent();
1507
1508 if (!FuncInfo.CanLowerReturn)
1509 return false;
1510
1511 if (F.isVarArg())
1512 return false;
1513
1514 // Build a list of return value registers.
1515 SmallVector<unsigned, 4> RetRegs;
1516
1517 if (Ret->getNumOperands() > 0) {
1518 CallingConv::ID CC = F.getCallingConv();
1519 SmallVector<ISD::OutputArg, 4> Outs;
1520 GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI);
1521
1522 // Analyze operands of the call, assigning locations to each operand.
1523 SmallVector<CCValAssign, 16> ValLocs;
1524 CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, TM, ValLocs,
1525 I->getContext());
1526 CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
1527 : RetCC_AArch64_AAPCS;
1528 CCInfo.AnalyzeReturn(Outs, RetCC);
1529
1530 // Only handle a single return value for now.
1531 if (ValLocs.size() != 1)
1532 return false;
1533
1534 CCValAssign &VA = ValLocs[0];
1535 const Value *RV = Ret->getOperand(0);
1536
1537 // Don't bother handling odd stuff for now.
1538 if (VA.getLocInfo() != CCValAssign::Full)
1539 return false;
1540 // Only handle register returns for now.
1541 if (!VA.isRegLoc())
1542 return false;
1543 unsigned Reg = getRegForValue(RV);
1544 if (Reg == 0)
1545 return false;
1546
1547 unsigned SrcReg = Reg + VA.getValNo();
1548 unsigned DestReg = VA.getLocReg();
1549 // Avoid a cross-class copy. This is very unlikely.
1550 if (!MRI.getRegClass(SrcReg)->contains(DestReg))
1551 return false;
1552
1553 EVT RVEVT = TLI.getValueType(RV->getType());
1554 if (!RVEVT.isSimple())
1555 return false;
1556
1557 // Vectors (of > 1 lane) in big endian need tricky handling.
1558 if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1)
1559 return false;
1560
1561 MVT RVVT = RVEVT.getSimpleVT();
1562 if (RVVT == MVT::f128)
1563 return false;
1564 MVT DestVT = VA.getValVT();
1565 // Special handling for extended integers.
1566 if (RVVT != DestVT) {
1567 if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
1568 return false;
1569
1570 if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
1571 return false;
1572
1573 bool isZExt = Outs[0].Flags.isZExt();
1574 SrcReg = EmitIntExt(RVVT, SrcReg, DestVT, isZExt);
1575 if (SrcReg == 0)
1576 return false;
1577 }
1578
1579 // Make the copy.
1580 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1581 TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
1582
1583 // Add register to return instruction.
1584 RetRegs.push_back(VA.getLocReg());
1585 }
1586
1587 MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1588 TII.get(AArch64::RET_ReallyLR));
1589 for (unsigned i = 0, e = RetRegs.size(); i != e; ++i)
1590 MIB.addReg(RetRegs[i], RegState::Implicit);
1591 return true;
1592}
1593
1594bool AArch64FastISel::SelectTrunc(const Instruction *I) {
1595 Type *DestTy = I->getType();
1596 Value *Op = I->getOperand(0);
1597 Type *SrcTy = Op->getType();
1598
1599 EVT SrcEVT = TLI.getValueType(SrcTy, true);
1600 EVT DestEVT = TLI.getValueType(DestTy, true);
1601 if (!SrcEVT.isSimple())
1602 return false;
1603 if (!DestEVT.isSimple())
1604 return false;
1605
1606 MVT SrcVT = SrcEVT.getSimpleVT();
1607 MVT DestVT = DestEVT.getSimpleVT();
1608
1609 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
1610 SrcVT != MVT::i8)
1611 return false;
1612 if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
1613 DestVT != MVT::i1)
1614 return false;
1615
1616 unsigned SrcReg = getRegForValue(Op);
1617 if (!SrcReg)
1618 return false;
1619
1620 // If we're truncating from i64 to a smaller non-legal type then generate an
1621 // AND. Otherwise, we know the high bits are undefined and a truncate doesn't
1622 // generate any code.
1623 if (SrcVT == MVT::i64) {
1624 uint64_t Mask = 0;
1625 switch (DestVT.SimpleTy) {
1626 default:
1627 // Trunc i64 to i32 is handled by the target-independent fast-isel.
1628 return false;
1629 case MVT::i1:
1630 Mask = 0x1;
1631 break;
1632 case MVT::i8:
1633 Mask = 0xff;
1634 break;
1635 case MVT::i16:
1636 Mask = 0xffff;
1637 break;
1638 }
1639 // Issue an extract_subreg to get the lower 32-bits.
1640 unsigned Reg32 = FastEmitInst_extractsubreg(MVT::i32, SrcReg, /*Kill=*/true,
1641 AArch64::sub_32);
1642 MRI.constrainRegClass(Reg32, &AArch64::GPR32RegClass);
1643 // Create the AND instruction which performs the actual truncation.
1644 unsigned ANDReg = createResultReg(&AArch64::GPR32spRegClass);
1645 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1646 ANDReg)
1647 .addReg(Reg32)
1648 .addImm(AArch64_AM::encodeLogicalImmediate(Mask, 32));
1649 SrcReg = ANDReg;
1650 }
1651
1652 UpdateValueMap(I, SrcReg);
1653 return true;
1654}
1655
1656unsigned AArch64FastISel::Emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt) {
1657 assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
1658 DestVT == MVT::i64) &&
1659 "Unexpected value type.");
1660 // Handle i8 and i16 as i32.
1661 if (DestVT == MVT::i8 || DestVT == MVT::i16)
1662 DestVT = MVT::i32;
1663
1664 if (isZExt) {
1665 MRI.constrainRegClass(SrcReg, &AArch64::GPR32RegClass);
1666 unsigned ResultReg = createResultReg(&AArch64::GPR32spRegClass);
1667 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ANDWri),
1668 ResultReg)
1669 .addReg(SrcReg)
1670 .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
1671
1672 if (DestVT == MVT::i64) {
1673 // We're ZExt i1 to i64. The ANDWri Wd, Ws, #1 implicitly clears the
1674 // upper 32 bits. Emit a SUBREG_TO_REG to extend from Wd to Xd.
1675 unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1676 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1677 TII.get(AArch64::SUBREG_TO_REG), Reg64)
1678 .addImm(0)
1679 .addReg(ResultReg)
1680 .addImm(AArch64::sub_32);
1681 ResultReg = Reg64;
1682 }
1683 return ResultReg;
1684 } else {
1685 if (DestVT == MVT::i64) {
1686 // FIXME: We're SExt i1 to i64.
1687 return 0;
1688 }
1689 unsigned ResultReg = createResultReg(&AArch64::GPR32RegClass);
1690 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::SBFMWri),
1691 ResultReg)
1692 .addReg(SrcReg)
1693 .addImm(0)
1694 .addImm(0);
1695 return ResultReg;
1696 }
1697}
1698
1699unsigned AArch64FastISel::EmitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
1700 bool isZExt) {
1701 assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
Louis Gerbarg4c5b4052014-07-07 21:37:51 +00001702
Louis Gerbarg1ce0c37bf2014-07-09 17:54:32 +00001703 // FastISel does not have plumbing to deal with extensions where the SrcVT or
1704 // DestVT are odd things, so test to make sure that they are both types we can
1705 // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
1706 // bail out to SelectionDAG.
1707 if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
1708 (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
1709 ((SrcVT != MVT::i1) && (SrcVT != MVT::i8) &&
1710 (SrcVT != MVT::i16) && (SrcVT != MVT::i32)))
Louis Gerbarg4c5b4052014-07-07 21:37:51 +00001711 return 0;
1712
Tim Northover3b0846e2014-05-24 12:50:23 +00001713 unsigned Opc;
1714 unsigned Imm = 0;
1715
1716 switch (SrcVT.SimpleTy) {
1717 default:
1718 return 0;
1719 case MVT::i1:
1720 return Emiti1Ext(SrcReg, DestVT, isZExt);
1721 case MVT::i8:
1722 if (DestVT == MVT::i64)
1723 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
1724 else
1725 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
1726 Imm = 7;
1727 break;
1728 case MVT::i16:
1729 if (DestVT == MVT::i64)
1730 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
1731 else
1732 Opc = isZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
1733 Imm = 15;
1734 break;
1735 case MVT::i32:
1736 assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
1737 Opc = isZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
1738 Imm = 31;
1739 break;
1740 }
1741
1742 // Handle i8 and i16 as i32.
1743 if (DestVT == MVT::i8 || DestVT == MVT::i16)
1744 DestVT = MVT::i32;
1745 else if (DestVT == MVT::i64) {
1746 unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
1747 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1748 TII.get(AArch64::SUBREG_TO_REG), Src64)
1749 .addImm(0)
1750 .addReg(SrcReg)
1751 .addImm(AArch64::sub_32);
1752 SrcReg = Src64;
1753 }
1754
1755 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1756 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1757 .addReg(SrcReg)
1758 .addImm(0)
1759 .addImm(Imm);
1760
1761 return ResultReg;
1762}
1763
1764bool AArch64FastISel::SelectIntExt(const Instruction *I) {
1765 // On ARM, in general, integer casts don't involve legal types; this code
1766 // handles promotable integers. The high bits for a type smaller than
1767 // the register size are assumed to be undefined.
1768 Type *DestTy = I->getType();
1769 Value *Src = I->getOperand(0);
1770 Type *SrcTy = Src->getType();
1771
1772 bool isZExt = isa<ZExtInst>(I);
1773 unsigned SrcReg = getRegForValue(Src);
1774 if (!SrcReg)
1775 return false;
1776
1777 EVT SrcEVT = TLI.getValueType(SrcTy, true);
1778 EVT DestEVT = TLI.getValueType(DestTy, true);
1779 if (!SrcEVT.isSimple())
1780 return false;
1781 if (!DestEVT.isSimple())
1782 return false;
1783
1784 MVT SrcVT = SrcEVT.getSimpleVT();
1785 MVT DestVT = DestEVT.getSimpleVT();
1786 unsigned ResultReg = EmitIntExt(SrcVT, SrcReg, DestVT, isZExt);
1787 if (ResultReg == 0)
1788 return false;
1789 UpdateValueMap(I, ResultReg);
1790 return true;
1791}
1792
1793bool AArch64FastISel::SelectRem(const Instruction *I, unsigned ISDOpcode) {
1794 EVT DestEVT = TLI.getValueType(I->getType(), true);
1795 if (!DestEVT.isSimple())
1796 return false;
1797
1798 MVT DestVT = DestEVT.getSimpleVT();
1799 if (DestVT != MVT::i64 && DestVT != MVT::i32)
1800 return false;
1801
1802 unsigned DivOpc;
1803 bool is64bit = (DestVT == MVT::i64);
1804 switch (ISDOpcode) {
1805 default:
1806 return false;
1807 case ISD::SREM:
1808 DivOpc = is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
1809 break;
1810 case ISD::UREM:
1811 DivOpc = is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
1812 break;
1813 }
1814 unsigned MSubOpc = is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
1815 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1816 if (!Src0Reg)
1817 return false;
1818
1819 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1820 if (!Src1Reg)
1821 return false;
1822
1823 unsigned QuotReg = createResultReg(TLI.getRegClassFor(DestVT));
1824 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(DivOpc), QuotReg)
1825 .addReg(Src0Reg)
1826 .addReg(Src1Reg);
1827 // The remainder is computed as numerator - (quotient * denominator) using the
1828 // MSUB instruction.
1829 unsigned ResultReg = createResultReg(TLI.getRegClassFor(DestVT));
1830 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(MSubOpc), ResultReg)
1831 .addReg(QuotReg)
1832 .addReg(Src1Reg)
1833 .addReg(Src0Reg);
1834 UpdateValueMap(I, ResultReg);
1835 return true;
1836}
1837
1838bool AArch64FastISel::SelectMul(const Instruction *I) {
1839 EVT SrcEVT = TLI.getValueType(I->getOperand(0)->getType(), true);
1840 if (!SrcEVT.isSimple())
1841 return false;
1842 MVT SrcVT = SrcEVT.getSimpleVT();
1843
1844 // Must be simple value type. Don't handle vectors.
1845 if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
1846 SrcVT != MVT::i8)
1847 return false;
1848
1849 unsigned Opc;
1850 unsigned ZReg;
1851 switch (SrcVT.SimpleTy) {
1852 default:
1853 return false;
1854 case MVT::i8:
1855 case MVT::i16:
1856 case MVT::i32:
1857 ZReg = AArch64::WZR;
1858 Opc = AArch64::MADDWrrr;
Tim Northoverfee2ade2014-07-10 14:18:46 +00001859 SrcVT = MVT::i32;
Tim Northover3b0846e2014-05-24 12:50:23 +00001860 break;
1861 case MVT::i64:
1862 ZReg = AArch64::XZR;
1863 Opc = AArch64::MADDXrrr;
1864 break;
1865 }
1866
1867 unsigned Src0Reg = getRegForValue(I->getOperand(0));
1868 if (!Src0Reg)
1869 return false;
1870
1871 unsigned Src1Reg = getRegForValue(I->getOperand(1));
1872 if (!Src1Reg)
1873 return false;
1874
1875 // Create the base instruction, then add the operands.
1876 unsigned ResultReg = createResultReg(TLI.getRegClassFor(SrcVT));
1877 BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
1878 .addReg(Src0Reg)
1879 .addReg(Src1Reg)
1880 .addReg(ZReg);
1881 UpdateValueMap(I, ResultReg);
1882 return true;
1883}
1884
1885bool AArch64FastISel::TargetSelectInstruction(const Instruction *I) {
1886 switch (I->getOpcode()) {
1887 default:
1888 break;
1889 case Instruction::Load:
1890 return SelectLoad(I);
1891 case Instruction::Store:
1892 return SelectStore(I);
1893 case Instruction::Br:
1894 return SelectBranch(I);
1895 case Instruction::IndirectBr:
1896 return SelectIndirectBr(I);
1897 case Instruction::FCmp:
1898 case Instruction::ICmp:
1899 return SelectCmp(I);
1900 case Instruction::Select:
1901 return SelectSelect(I);
1902 case Instruction::FPExt:
1903 return SelectFPExt(I);
1904 case Instruction::FPTrunc:
1905 return SelectFPTrunc(I);
1906 case Instruction::FPToSI:
1907 return SelectFPToInt(I, /*Signed=*/true);
1908 case Instruction::FPToUI:
1909 return SelectFPToInt(I, /*Signed=*/false);
1910 case Instruction::SIToFP:
1911 return SelectIntToFP(I, /*Signed=*/true);
1912 case Instruction::UIToFP:
1913 return SelectIntToFP(I, /*Signed=*/false);
1914 case Instruction::SRem:
1915 return SelectRem(I, ISD::SREM);
1916 case Instruction::URem:
1917 return SelectRem(I, ISD::UREM);
Tim Northover3b0846e2014-05-24 12:50:23 +00001918 case Instruction::Ret:
1919 return SelectRet(I);
1920 case Instruction::Trunc:
1921 return SelectTrunc(I);
1922 case Instruction::ZExt:
1923 case Instruction::SExt:
1924 return SelectIntExt(I);
1925 case Instruction::Mul:
1926 // FIXME: This really should be handled by the target-independent selector.
1927 return SelectMul(I);
1928 }
1929 return false;
1930 // Silence warnings.
1931 (void)&CC_AArch64_DarwinPCS_VarArg;
1932}
1933
1934namespace llvm {
1935llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &funcInfo,
1936 const TargetLibraryInfo *libInfo) {
1937 return new AArch64FastISel(funcInfo, libInfo);
1938}
1939}