]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/AArch64/AArch64FastISel.cpp
Merge OpenSSL 1.0.2k.
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Target / AArch64 / AArch64FastISel.cpp
1 //===-- 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 "AArch64CallingConvention.h"
18 #include "AArch64Subtarget.h"
19 #include "AArch64TargetMachine.h"
20 #include "MCTargetDesc/AArch64AddressingModes.h"
21 #include "llvm/Analysis/BranchProbabilityInfo.h"
22 #include "llvm/CodeGen/CallingConvLower.h"
23 #include "llvm/CodeGen/FastISel.h"
24 #include "llvm/CodeGen/FunctionLoweringInfo.h"
25 #include "llvm/CodeGen/MachineConstantPool.h"
26 #include "llvm/CodeGen/MachineFrameInfo.h"
27 #include "llvm/CodeGen/MachineInstrBuilder.h"
28 #include "llvm/CodeGen/MachineRegisterInfo.h"
29 #include "llvm/IR/CallingConv.h"
30 #include "llvm/IR/DataLayout.h"
31 #include "llvm/IR/DerivedTypes.h"
32 #include "llvm/IR/Function.h"
33 #include "llvm/IR/GetElementPtrTypeIterator.h"
34 #include "llvm/IR/GlobalAlias.h"
35 #include "llvm/IR/GlobalVariable.h"
36 #include "llvm/IR/Instructions.h"
37 #include "llvm/IR/IntrinsicInst.h"
38 #include "llvm/IR/Operator.h"
39 #include "llvm/MC/MCSymbol.h"
40 using namespace llvm;
41
42 namespace {
43
44 class AArch64FastISel final : public FastISel {
45   class Address {
46   public:
47     typedef enum {
48       RegBase,
49       FrameIndexBase
50     } BaseKind;
51
52   private:
53     BaseKind Kind;
54     AArch64_AM::ShiftExtendType ExtType;
55     union {
56       unsigned Reg;
57       int FI;
58     } Base;
59     unsigned OffsetReg;
60     unsigned Shift;
61     int64_t Offset;
62     const GlobalValue *GV;
63
64   public:
65     Address() : Kind(RegBase), ExtType(AArch64_AM::InvalidShiftExtend),
66       OffsetReg(0), Shift(0), Offset(0), GV(nullptr) { Base.Reg = 0; }
67     void setKind(BaseKind K) { Kind = K; }
68     BaseKind getKind() const { return Kind; }
69     void setExtendType(AArch64_AM::ShiftExtendType E) { ExtType = E; }
70     AArch64_AM::ShiftExtendType getExtendType() const { return ExtType; }
71     bool isRegBase() const { return Kind == RegBase; }
72     bool isFIBase() const { return Kind == FrameIndexBase; }
73     void setReg(unsigned Reg) {
74       assert(isRegBase() && "Invalid base register access!");
75       Base.Reg = Reg;
76     }
77     unsigned getReg() const {
78       assert(isRegBase() && "Invalid base register access!");
79       return Base.Reg;
80     }
81     void setOffsetReg(unsigned Reg) {
82       OffsetReg = Reg;
83     }
84     unsigned getOffsetReg() const {
85       return OffsetReg;
86     }
87     void setFI(unsigned FI) {
88       assert(isFIBase() && "Invalid base frame index  access!");
89       Base.FI = FI;
90     }
91     unsigned getFI() const {
92       assert(isFIBase() && "Invalid base frame index access!");
93       return Base.FI;
94     }
95     void setOffset(int64_t O) { Offset = O; }
96     int64_t getOffset() { return Offset; }
97     void setShift(unsigned S) { Shift = S; }
98     unsigned getShift() { return Shift; }
99
100     void setGlobalValue(const GlobalValue *G) { GV = G; }
101     const GlobalValue *getGlobalValue() { return GV; }
102   };
103
104   /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
105   /// make the right decision when generating code for different targets.
106   const AArch64Subtarget *Subtarget;
107   LLVMContext *Context;
108
109   bool fastLowerArguments() override;
110   bool fastLowerCall(CallLoweringInfo &CLI) override;
111   bool fastLowerIntrinsicCall(const IntrinsicInst *II) override;
112
113 private:
114   // Selection routines.
115   bool selectAddSub(const Instruction *I);
116   bool selectLogicalOp(const Instruction *I);
117   bool selectLoad(const Instruction *I);
118   bool selectStore(const Instruction *I);
119   bool selectBranch(const Instruction *I);
120   bool selectIndirectBr(const Instruction *I);
121   bool selectCmp(const Instruction *I);
122   bool selectSelect(const Instruction *I);
123   bool selectFPExt(const Instruction *I);
124   bool selectFPTrunc(const Instruction *I);
125   bool selectFPToInt(const Instruction *I, bool Signed);
126   bool selectIntToFP(const Instruction *I, bool Signed);
127   bool selectRem(const Instruction *I, unsigned ISDOpcode);
128   bool selectRet(const Instruction *I);
129   bool selectTrunc(const Instruction *I);
130   bool selectIntExt(const Instruction *I);
131   bool selectMul(const Instruction *I);
132   bool selectShift(const Instruction *I);
133   bool selectBitCast(const Instruction *I);
134   bool selectFRem(const Instruction *I);
135   bool selectSDiv(const Instruction *I);
136   bool selectGetElementPtr(const Instruction *I);
137
138   // Utility helper routines.
139   bool isTypeLegal(Type *Ty, MVT &VT);
140   bool isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed = false);
141   bool isValueAvailable(const Value *V) const;
142   bool computeAddress(const Value *Obj, Address &Addr, Type *Ty = nullptr);
143   bool computeCallAddress(const Value *V, Address &Addr);
144   bool simplifyAddress(Address &Addr, MVT VT);
145   void addLoadStoreOperands(Address &Addr, const MachineInstrBuilder &MIB,
146                             MachineMemOperand::Flags Flags,
147                             unsigned ScaleFactor, MachineMemOperand *MMO);
148   bool isMemCpySmall(uint64_t Len, unsigned Alignment);
149   bool tryEmitSmallMemCpy(Address Dest, Address Src, uint64_t Len,
150                           unsigned Alignment);
151   bool foldXALUIntrinsic(AArch64CC::CondCode &CC, const Instruction *I,
152                          const Value *Cond);
153   bool optimizeIntExtLoad(const Instruction *I, MVT RetVT, MVT SrcVT);
154   bool optimizeSelect(const SelectInst *SI);
155   std::pair<unsigned, bool> getRegForGEPIndex(const Value *Idx);
156
157   // Emit helper routines.
158   unsigned emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
159                       const Value *RHS, bool SetFlags = false,
160                       bool WantResult = true,  bool IsZExt = false);
161   unsigned emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
162                          bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
163                          bool SetFlags = false, bool WantResult = true);
164   unsigned emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
165                          bool LHSIsKill, uint64_t Imm, bool SetFlags = false,
166                          bool WantResult = true);
167   unsigned emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
168                          bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
169                          AArch64_AM::ShiftExtendType ShiftType,
170                          uint64_t ShiftImm, bool SetFlags = false,
171                          bool WantResult = true);
172   unsigned emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
173                          bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
174                           AArch64_AM::ShiftExtendType ExtType,
175                           uint64_t ShiftImm, bool SetFlags = false,
176                          bool WantResult = true);
177
178   // Emit functions.
179   bool emitCompareAndBranch(const BranchInst *BI);
180   bool emitCmp(const Value *LHS, const Value *RHS, bool IsZExt);
181   bool emitICmp(MVT RetVT, const Value *LHS, const Value *RHS, bool IsZExt);
182   bool emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
183   bool emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS);
184   unsigned emitLoad(MVT VT, MVT ResultVT, Address Addr, bool WantZExt = true,
185                     MachineMemOperand *MMO = nullptr);
186   bool emitStore(MVT VT, unsigned SrcReg, Address Addr,
187                  MachineMemOperand *MMO = nullptr);
188   unsigned emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT, bool isZExt);
189   unsigned emiti1Ext(unsigned SrcReg, MVT DestVT, bool isZExt);
190   unsigned emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
191                    bool SetFlags = false, bool WantResult = true,
192                    bool IsZExt = false);
193   unsigned emitAdd_ri_(MVT VT, unsigned Op0, bool Op0IsKill, int64_t Imm);
194   unsigned emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
195                    bool SetFlags = false, bool WantResult = true,
196                    bool IsZExt = false);
197   unsigned emitSubs_rr(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
198                        unsigned RHSReg, bool RHSIsKill, bool WantResult = true);
199   unsigned emitSubs_rs(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
200                        unsigned RHSReg, bool RHSIsKill,
201                        AArch64_AM::ShiftExtendType ShiftType, uint64_t ShiftImm,
202                        bool WantResult = true);
203   unsigned emitLogicalOp(unsigned ISDOpc, MVT RetVT, const Value *LHS,
204                          const Value *RHS);
205   unsigned emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
206                             bool LHSIsKill, uint64_t Imm);
207   unsigned emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT, unsigned LHSReg,
208                             bool LHSIsKill, unsigned RHSReg, bool RHSIsKill,
209                             uint64_t ShiftImm);
210   unsigned emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill, uint64_t Imm);
211   unsigned emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
212                       unsigned Op1, bool Op1IsKill);
213   unsigned emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
214                         unsigned Op1, bool Op1IsKill);
215   unsigned emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
216                         unsigned Op1, bool Op1IsKill);
217   unsigned emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
218                       unsigned Op1Reg, bool Op1IsKill);
219   unsigned emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
220                       uint64_t Imm, bool IsZExt = true);
221   unsigned emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
222                       unsigned Op1Reg, bool Op1IsKill);
223   unsigned emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
224                       uint64_t Imm, bool IsZExt = true);
225   unsigned emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
226                       unsigned Op1Reg, bool Op1IsKill);
227   unsigned emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0Reg, bool Op0IsKill,
228                       uint64_t Imm, bool IsZExt = false);
229
230   unsigned materializeInt(const ConstantInt *CI, MVT VT);
231   unsigned materializeFP(const ConstantFP *CFP, MVT VT);
232   unsigned materializeGV(const GlobalValue *GV);
233
234   // Call handling routines.
235 private:
236   CCAssignFn *CCAssignFnForCall(CallingConv::ID CC) const;
237   bool processCallArgs(CallLoweringInfo &CLI, SmallVectorImpl<MVT> &ArgVTs,
238                        unsigned &NumBytes);
239   bool finishCall(CallLoweringInfo &CLI, MVT RetVT, unsigned NumBytes);
240
241 public:
242   // Backend specific FastISel code.
243   unsigned fastMaterializeAlloca(const AllocaInst *AI) override;
244   unsigned fastMaterializeConstant(const Constant *C) override;
245   unsigned fastMaterializeFloatZero(const ConstantFP* CF) override;
246
247   explicit AArch64FastISel(FunctionLoweringInfo &FuncInfo,
248                            const TargetLibraryInfo *LibInfo)
249       : FastISel(FuncInfo, LibInfo, /*SkipTargetIndependentISel=*/true) {
250     Subtarget =
251         &static_cast<const AArch64Subtarget &>(FuncInfo.MF->getSubtarget());
252     Context = &FuncInfo.Fn->getContext();
253   }
254
255   bool fastSelectInstruction(const Instruction *I) override;
256
257 #include "AArch64GenFastISel.inc"
258 };
259
260 } // end anonymous namespace
261
262 #include "AArch64GenCallingConv.inc"
263
264 /// \brief Check if the sign-/zero-extend will be a noop.
265 static bool isIntExtFree(const Instruction *I) {
266   assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
267          "Unexpected integer extend instruction.");
268   assert(!I->getType()->isVectorTy() && I->getType()->isIntegerTy() &&
269          "Unexpected value type.");
270   bool IsZExt = isa<ZExtInst>(I);
271
272   if (const auto *LI = dyn_cast<LoadInst>(I->getOperand(0)))
273     if (LI->hasOneUse())
274       return true;
275
276   if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0)))
277     if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr()))
278       return true;
279
280   return false;
281 }
282
283 /// \brief Determine the implicit scale factor that is applied by a memory
284 /// operation for a given value type.
285 static unsigned getImplicitScaleFactor(MVT VT) {
286   switch (VT.SimpleTy) {
287   default:
288     return 0;    // invalid
289   case MVT::i1:  // fall-through
290   case MVT::i8:
291     return 1;
292   case MVT::i16:
293     return 2;
294   case MVT::i32: // fall-through
295   case MVT::f32:
296     return 4;
297   case MVT::i64: // fall-through
298   case MVT::f64:
299     return 8;
300   }
301 }
302
303 CCAssignFn *AArch64FastISel::CCAssignFnForCall(CallingConv::ID CC) const {
304   if (CC == CallingConv::WebKit_JS)
305     return CC_AArch64_WebKit_JS;
306   if (CC == CallingConv::GHC)
307     return CC_AArch64_GHC;
308   return Subtarget->isTargetDarwin() ? CC_AArch64_DarwinPCS : CC_AArch64_AAPCS;
309 }
310
311 unsigned AArch64FastISel::fastMaterializeAlloca(const AllocaInst *AI) {
312   assert(TLI.getValueType(DL, AI->getType(), true) == MVT::i64 &&
313          "Alloca should always return a pointer.");
314
315   // Don't handle dynamic allocas.
316   if (!FuncInfo.StaticAllocaMap.count(AI))
317     return 0;
318
319   DenseMap<const AllocaInst *, int>::iterator SI =
320       FuncInfo.StaticAllocaMap.find(AI);
321
322   if (SI != FuncInfo.StaticAllocaMap.end()) {
323     unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
324     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
325             ResultReg)
326         .addFrameIndex(SI->second)
327         .addImm(0)
328         .addImm(0);
329     return ResultReg;
330   }
331
332   return 0;
333 }
334
335 unsigned AArch64FastISel::materializeInt(const ConstantInt *CI, MVT VT) {
336   if (VT > MVT::i64)
337     return 0;
338
339   if (!CI->isZero())
340     return fastEmit_i(VT, VT, ISD::Constant, CI->getZExtValue());
341
342   // Create a copy from the zero register to materialize a "0" value.
343   const TargetRegisterClass *RC = (VT == MVT::i64) ? &AArch64::GPR64RegClass
344                                                    : &AArch64::GPR32RegClass;
345   unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
346   unsigned ResultReg = createResultReg(RC);
347   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(TargetOpcode::COPY),
348           ResultReg).addReg(ZeroReg, getKillRegState(true));
349   return ResultReg;
350 }
351
352 unsigned AArch64FastISel::materializeFP(const ConstantFP *CFP, MVT VT) {
353   // Positive zero (+0.0) has to be materialized with a fmov from the zero
354   // register, because the immediate version of fmov cannot encode zero.
355   if (CFP->isNullValue())
356     return fastMaterializeFloatZero(CFP);
357
358   if (VT != MVT::f32 && VT != MVT::f64)
359     return 0;
360
361   const APFloat Val = CFP->getValueAPF();
362   bool Is64Bit = (VT == MVT::f64);
363   // This checks to see if we can use FMOV instructions to materialize
364   // a constant, otherwise we have to materialize via the constant pool.
365   if (TLI.isFPImmLegal(Val, VT)) {
366     int Imm =
367         Is64Bit ? AArch64_AM::getFP64Imm(Val) : AArch64_AM::getFP32Imm(Val);
368     assert((Imm != -1) && "Cannot encode floating-point constant.");
369     unsigned Opc = Is64Bit ? AArch64::FMOVDi : AArch64::FMOVSi;
370     return fastEmitInst_i(Opc, TLI.getRegClassFor(VT), Imm);
371   }
372
373   // For the MachO large code model materialize the FP constant in code.
374   if (Subtarget->isTargetMachO() && TM.getCodeModel() == CodeModel::Large) {
375     unsigned Opc1 = Is64Bit ? AArch64::MOVi64imm : AArch64::MOVi32imm;
376     const TargetRegisterClass *RC = Is64Bit ?
377         &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
378
379     unsigned TmpReg = createResultReg(RC);
380     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc1), TmpReg)
381         .addImm(CFP->getValueAPF().bitcastToAPInt().getZExtValue());
382
383     unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
384     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
385             TII.get(TargetOpcode::COPY), ResultReg)
386         .addReg(TmpReg, getKillRegState(true));
387
388     return ResultReg;
389   }
390
391   // Materialize via constant pool.  MachineConstantPool wants an explicit
392   // alignment.
393   unsigned Align = DL.getPrefTypeAlignment(CFP->getType());
394   if (Align == 0)
395     Align = DL.getTypeAllocSize(CFP->getType());
396
397   unsigned CPI = MCP.getConstantPoolIndex(cast<Constant>(CFP), Align);
398   unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
399   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
400           ADRPReg).addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGE);
401
402   unsigned Opc = Is64Bit ? AArch64::LDRDui : AArch64::LDRSui;
403   unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
404   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
405       .addReg(ADRPReg)
406       .addConstantPoolIndex(CPI, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
407   return ResultReg;
408 }
409
410 unsigned AArch64FastISel::materializeGV(const GlobalValue *GV) {
411   // We can't handle thread-local variables quickly yet.
412   if (GV->isThreadLocal())
413     return 0;
414
415   // MachO still uses GOT for large code-model accesses, but ELF requires
416   // movz/movk sequences, which FastISel doesn't handle yet.
417   if (TM.getCodeModel() != CodeModel::Small && !Subtarget->isTargetMachO())
418     return 0;
419
420   unsigned char OpFlags = Subtarget->ClassifyGlobalReference(GV, TM);
421
422   EVT DestEVT = TLI.getValueType(DL, GV->getType(), true);
423   if (!DestEVT.isSimple())
424     return 0;
425
426   unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
427   unsigned ResultReg;
428
429   if (OpFlags & AArch64II::MO_GOT) {
430     // ADRP + LDRX
431     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
432             ADRPReg)
433       .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGE);
434
435     ResultReg = createResultReg(&AArch64::GPR64RegClass);
436     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::LDRXui),
437             ResultReg)
438       .addReg(ADRPReg)
439       .addGlobalAddress(GV, 0, AArch64II::MO_GOT | AArch64II::MO_PAGEOFF |
440                         AArch64II::MO_NC);
441   } else {
442     // ADRP + ADDX
443     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
444             ADRPReg)
445       .addGlobalAddress(GV, 0, AArch64II::MO_PAGE);
446
447     ResultReg = createResultReg(&AArch64::GPR64spRegClass);
448     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
449             ResultReg)
450       .addReg(ADRPReg)
451       .addGlobalAddress(GV, 0, AArch64II::MO_PAGEOFF | AArch64II::MO_NC)
452       .addImm(0);
453   }
454   return ResultReg;
455 }
456
457 unsigned AArch64FastISel::fastMaterializeConstant(const Constant *C) {
458   EVT CEVT = TLI.getValueType(DL, C->getType(), true);
459
460   // Only handle simple types.
461   if (!CEVT.isSimple())
462     return 0;
463   MVT VT = CEVT.getSimpleVT();
464
465   if (const auto *CI = dyn_cast<ConstantInt>(C))
466     return materializeInt(CI, VT);
467   else if (const ConstantFP *CFP = dyn_cast<ConstantFP>(C))
468     return materializeFP(CFP, VT);
469   else if (const GlobalValue *GV = dyn_cast<GlobalValue>(C))
470     return materializeGV(GV);
471
472   return 0;
473 }
474
475 unsigned AArch64FastISel::fastMaterializeFloatZero(const ConstantFP* CFP) {
476   assert(CFP->isNullValue() &&
477          "Floating-point constant is not a positive zero.");
478   MVT VT;
479   if (!isTypeLegal(CFP->getType(), VT))
480     return 0;
481
482   if (VT != MVT::f32 && VT != MVT::f64)
483     return 0;
484
485   bool Is64Bit = (VT == MVT::f64);
486   unsigned ZReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
487   unsigned Opc = Is64Bit ? AArch64::FMOVXDr : AArch64::FMOVWSr;
488   return fastEmitInst_r(Opc, TLI.getRegClassFor(VT), ZReg, /*IsKill=*/true);
489 }
490
491 /// \brief Check if the multiply is by a power-of-2 constant.
492 static bool isMulPowOf2(const Value *I) {
493   if (const auto *MI = dyn_cast<MulOperator>(I)) {
494     if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(0)))
495       if (C->getValue().isPowerOf2())
496         return true;
497     if (const auto *C = dyn_cast<ConstantInt>(MI->getOperand(1)))
498       if (C->getValue().isPowerOf2())
499         return true;
500   }
501   return false;
502 }
503
504 // Computes the address to get to an object.
505 bool AArch64FastISel::computeAddress(const Value *Obj, Address &Addr, Type *Ty)
506 {
507   const User *U = nullptr;
508   unsigned Opcode = Instruction::UserOp1;
509   if (const Instruction *I = dyn_cast<Instruction>(Obj)) {
510     // Don't walk into other basic blocks unless the object is an alloca from
511     // another block, otherwise it may not have a virtual register assigned.
512     if (FuncInfo.StaticAllocaMap.count(static_cast<const AllocaInst *>(Obj)) ||
513         FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
514       Opcode = I->getOpcode();
515       U = I;
516     }
517   } else if (const ConstantExpr *C = dyn_cast<ConstantExpr>(Obj)) {
518     Opcode = C->getOpcode();
519     U = C;
520   }
521
522   if (auto *Ty = dyn_cast<PointerType>(Obj->getType()))
523     if (Ty->getAddressSpace() > 255)
524       // Fast instruction selection doesn't support the special
525       // address spaces.
526       return false;
527
528   switch (Opcode) {
529   default:
530     break;
531   case Instruction::BitCast: {
532     // Look through bitcasts.
533     return computeAddress(U->getOperand(0), Addr, Ty);
534   }
535   case Instruction::IntToPtr: {
536     // Look past no-op inttoptrs.
537     if (TLI.getValueType(DL, U->getOperand(0)->getType()) ==
538         TLI.getPointerTy(DL))
539       return computeAddress(U->getOperand(0), Addr, Ty);
540     break;
541   }
542   case Instruction::PtrToInt: {
543     // Look past no-op ptrtoints.
544     if (TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
545       return computeAddress(U->getOperand(0), Addr, Ty);
546     break;
547   }
548   case Instruction::GetElementPtr: {
549     Address SavedAddr = Addr;
550     uint64_t TmpOffset = Addr.getOffset();
551
552     // Iterate through the GEP folding the constants into offsets where
553     // we can.
554     for (gep_type_iterator GTI = gep_type_begin(U), E = gep_type_end(U);
555          GTI != E; ++GTI) {
556       const Value *Op = GTI.getOperand();
557       if (StructType *STy = dyn_cast<StructType>(*GTI)) {
558         const StructLayout *SL = DL.getStructLayout(STy);
559         unsigned Idx = cast<ConstantInt>(Op)->getZExtValue();
560         TmpOffset += SL->getElementOffset(Idx);
561       } else {
562         uint64_t S = DL.getTypeAllocSize(GTI.getIndexedType());
563         for (;;) {
564           if (const ConstantInt *CI = dyn_cast<ConstantInt>(Op)) {
565             // Constant-offset addressing.
566             TmpOffset += CI->getSExtValue() * S;
567             break;
568           }
569           if (canFoldAddIntoGEP(U, Op)) {
570             // A compatible add with a constant operand. Fold the constant.
571             ConstantInt *CI =
572                 cast<ConstantInt>(cast<AddOperator>(Op)->getOperand(1));
573             TmpOffset += CI->getSExtValue() * S;
574             // Iterate on the other operand.
575             Op = cast<AddOperator>(Op)->getOperand(0);
576             continue;
577           }
578           // Unsupported
579           goto unsupported_gep;
580         }
581       }
582     }
583
584     // Try to grab the base operand now.
585     Addr.setOffset(TmpOffset);
586     if (computeAddress(U->getOperand(0), Addr, Ty))
587       return true;
588
589     // We failed, restore everything and try the other options.
590     Addr = SavedAddr;
591
592   unsupported_gep:
593     break;
594   }
595   case Instruction::Alloca: {
596     const AllocaInst *AI = cast<AllocaInst>(Obj);
597     DenseMap<const AllocaInst *, int>::iterator SI =
598         FuncInfo.StaticAllocaMap.find(AI);
599     if (SI != FuncInfo.StaticAllocaMap.end()) {
600       Addr.setKind(Address::FrameIndexBase);
601       Addr.setFI(SI->second);
602       return true;
603     }
604     break;
605   }
606   case Instruction::Add: {
607     // Adds of constants are common and easy enough.
608     const Value *LHS = U->getOperand(0);
609     const Value *RHS = U->getOperand(1);
610
611     if (isa<ConstantInt>(LHS))
612       std::swap(LHS, RHS);
613
614     if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
615       Addr.setOffset(Addr.getOffset() + CI->getSExtValue());
616       return computeAddress(LHS, Addr, Ty);
617     }
618
619     Address Backup = Addr;
620     if (computeAddress(LHS, Addr, Ty) && computeAddress(RHS, Addr, Ty))
621       return true;
622     Addr = Backup;
623
624     break;
625   }
626   case Instruction::Sub: {
627     // Subs of constants are common and easy enough.
628     const Value *LHS = U->getOperand(0);
629     const Value *RHS = U->getOperand(1);
630
631     if (const ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
632       Addr.setOffset(Addr.getOffset() - CI->getSExtValue());
633       return computeAddress(LHS, Addr, Ty);
634     }
635     break;
636   }
637   case Instruction::Shl: {
638     if (Addr.getOffsetReg())
639       break;
640
641     const auto *CI = dyn_cast<ConstantInt>(U->getOperand(1));
642     if (!CI)
643       break;
644
645     unsigned Val = CI->getZExtValue();
646     if (Val < 1 || Val > 3)
647       break;
648
649     uint64_t NumBytes = 0;
650     if (Ty && Ty->isSized()) {
651       uint64_t NumBits = DL.getTypeSizeInBits(Ty);
652       NumBytes = NumBits / 8;
653       if (!isPowerOf2_64(NumBits))
654         NumBytes = 0;
655     }
656
657     if (NumBytes != (1ULL << Val))
658       break;
659
660     Addr.setShift(Val);
661     Addr.setExtendType(AArch64_AM::LSL);
662
663     const Value *Src = U->getOperand(0);
664     if (const auto *I = dyn_cast<Instruction>(Src)) {
665       if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
666         // Fold the zext or sext when it won't become a noop.
667         if (const auto *ZE = dyn_cast<ZExtInst>(I)) {
668           if (!isIntExtFree(ZE) &&
669               ZE->getOperand(0)->getType()->isIntegerTy(32)) {
670             Addr.setExtendType(AArch64_AM::UXTW);
671             Src = ZE->getOperand(0);
672           }
673         } else if (const auto *SE = dyn_cast<SExtInst>(I)) {
674           if (!isIntExtFree(SE) &&
675               SE->getOperand(0)->getType()->isIntegerTy(32)) {
676             Addr.setExtendType(AArch64_AM::SXTW);
677             Src = SE->getOperand(0);
678           }
679         }
680       }
681     }
682
683     if (const auto *AI = dyn_cast<BinaryOperator>(Src))
684       if (AI->getOpcode() == Instruction::And) {
685         const Value *LHS = AI->getOperand(0);
686         const Value *RHS = AI->getOperand(1);
687
688         if (const auto *C = dyn_cast<ConstantInt>(LHS))
689           if (C->getValue() == 0xffffffff)
690             std::swap(LHS, RHS);
691
692         if (const auto *C = dyn_cast<ConstantInt>(RHS))
693           if (C->getValue() == 0xffffffff) {
694             Addr.setExtendType(AArch64_AM::UXTW);
695             unsigned Reg = getRegForValue(LHS);
696             if (!Reg)
697               return false;
698             bool RegIsKill = hasTrivialKill(LHS);
699             Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
700                                              AArch64::sub_32);
701             Addr.setOffsetReg(Reg);
702             return true;
703           }
704       }
705
706     unsigned Reg = getRegForValue(Src);
707     if (!Reg)
708       return false;
709     Addr.setOffsetReg(Reg);
710     return true;
711   }
712   case Instruction::Mul: {
713     if (Addr.getOffsetReg())
714       break;
715
716     if (!isMulPowOf2(U))
717       break;
718
719     const Value *LHS = U->getOperand(0);
720     const Value *RHS = U->getOperand(1);
721
722     // Canonicalize power-of-2 value to the RHS.
723     if (const auto *C = dyn_cast<ConstantInt>(LHS))
724       if (C->getValue().isPowerOf2())
725         std::swap(LHS, RHS);
726
727     assert(isa<ConstantInt>(RHS) && "Expected an ConstantInt.");
728     const auto *C = cast<ConstantInt>(RHS);
729     unsigned Val = C->getValue().logBase2();
730     if (Val < 1 || Val > 3)
731       break;
732
733     uint64_t NumBytes = 0;
734     if (Ty && Ty->isSized()) {
735       uint64_t NumBits = DL.getTypeSizeInBits(Ty);
736       NumBytes = NumBits / 8;
737       if (!isPowerOf2_64(NumBits))
738         NumBytes = 0;
739     }
740
741     if (NumBytes != (1ULL << Val))
742       break;
743
744     Addr.setShift(Val);
745     Addr.setExtendType(AArch64_AM::LSL);
746
747     const Value *Src = LHS;
748     if (const auto *I = dyn_cast<Instruction>(Src)) {
749       if (FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB) {
750         // Fold the zext or sext when it won't become a noop.
751         if (const auto *ZE = dyn_cast<ZExtInst>(I)) {
752           if (!isIntExtFree(ZE) &&
753               ZE->getOperand(0)->getType()->isIntegerTy(32)) {
754             Addr.setExtendType(AArch64_AM::UXTW);
755             Src = ZE->getOperand(0);
756           }
757         } else if (const auto *SE = dyn_cast<SExtInst>(I)) {
758           if (!isIntExtFree(SE) &&
759               SE->getOperand(0)->getType()->isIntegerTy(32)) {
760             Addr.setExtendType(AArch64_AM::SXTW);
761             Src = SE->getOperand(0);
762           }
763         }
764       }
765     }
766
767     unsigned Reg = getRegForValue(Src);
768     if (!Reg)
769       return false;
770     Addr.setOffsetReg(Reg);
771     return true;
772   }
773   case Instruction::And: {
774     if (Addr.getOffsetReg())
775       break;
776
777     if (!Ty || DL.getTypeSizeInBits(Ty) != 8)
778       break;
779
780     const Value *LHS = U->getOperand(0);
781     const Value *RHS = U->getOperand(1);
782
783     if (const auto *C = dyn_cast<ConstantInt>(LHS))
784       if (C->getValue() == 0xffffffff)
785         std::swap(LHS, RHS);
786
787     if (const auto *C = dyn_cast<ConstantInt>(RHS))
788       if (C->getValue() == 0xffffffff) {
789         Addr.setShift(0);
790         Addr.setExtendType(AArch64_AM::LSL);
791         Addr.setExtendType(AArch64_AM::UXTW);
792
793         unsigned Reg = getRegForValue(LHS);
794         if (!Reg)
795           return false;
796         bool RegIsKill = hasTrivialKill(LHS);
797         Reg = fastEmitInst_extractsubreg(MVT::i32, Reg, RegIsKill,
798                                          AArch64::sub_32);
799         Addr.setOffsetReg(Reg);
800         return true;
801       }
802     break;
803   }
804   case Instruction::SExt:
805   case Instruction::ZExt: {
806     if (!Addr.getReg() || Addr.getOffsetReg())
807       break;
808
809     const Value *Src = nullptr;
810     // Fold the zext or sext when it won't become a noop.
811     if (const auto *ZE = dyn_cast<ZExtInst>(U)) {
812       if (!isIntExtFree(ZE) && ZE->getOperand(0)->getType()->isIntegerTy(32)) {
813         Addr.setExtendType(AArch64_AM::UXTW);
814         Src = ZE->getOperand(0);
815       }
816     } else if (const auto *SE = dyn_cast<SExtInst>(U)) {
817       if (!isIntExtFree(SE) && SE->getOperand(0)->getType()->isIntegerTy(32)) {
818         Addr.setExtendType(AArch64_AM::SXTW);
819         Src = SE->getOperand(0);
820       }
821     }
822
823     if (!Src)
824       break;
825
826     Addr.setShift(0);
827     unsigned Reg = getRegForValue(Src);
828     if (!Reg)
829       return false;
830     Addr.setOffsetReg(Reg);
831     return true;
832   }
833   } // end switch
834
835   if (Addr.isRegBase() && !Addr.getReg()) {
836     unsigned Reg = getRegForValue(Obj);
837     if (!Reg)
838       return false;
839     Addr.setReg(Reg);
840     return true;
841   }
842
843   if (!Addr.getOffsetReg()) {
844     unsigned Reg = getRegForValue(Obj);
845     if (!Reg)
846       return false;
847     Addr.setOffsetReg(Reg);
848     return true;
849   }
850
851   return false;
852 }
853
854 bool AArch64FastISel::computeCallAddress(const Value *V, Address &Addr) {
855   const User *U = nullptr;
856   unsigned Opcode = Instruction::UserOp1;
857   bool InMBB = true;
858
859   if (const auto *I = dyn_cast<Instruction>(V)) {
860     Opcode = I->getOpcode();
861     U = I;
862     InMBB = I->getParent() == FuncInfo.MBB->getBasicBlock();
863   } else if (const auto *C = dyn_cast<ConstantExpr>(V)) {
864     Opcode = C->getOpcode();
865     U = C;
866   }
867
868   switch (Opcode) {
869   default: break;
870   case Instruction::BitCast:
871     // Look past bitcasts if its operand is in the same BB.
872     if (InMBB)
873       return computeCallAddress(U->getOperand(0), Addr);
874     break;
875   case Instruction::IntToPtr:
876     // Look past no-op inttoptrs if its operand is in the same BB.
877     if (InMBB &&
878         TLI.getValueType(DL, U->getOperand(0)->getType()) ==
879             TLI.getPointerTy(DL))
880       return computeCallAddress(U->getOperand(0), Addr);
881     break;
882   case Instruction::PtrToInt:
883     // Look past no-op ptrtoints if its operand is in the same BB.
884     if (InMBB && TLI.getValueType(DL, U->getType()) == TLI.getPointerTy(DL))
885       return computeCallAddress(U->getOperand(0), Addr);
886     break;
887   }
888
889   if (const GlobalValue *GV = dyn_cast<GlobalValue>(V)) {
890     Addr.setGlobalValue(GV);
891     return true;
892   }
893
894   // If all else fails, try to materialize the value in a register.
895   if (!Addr.getGlobalValue()) {
896     Addr.setReg(getRegForValue(V));
897     return Addr.getReg() != 0;
898   }
899
900   return false;
901 }
902
903
904 bool AArch64FastISel::isTypeLegal(Type *Ty, MVT &VT) {
905   EVT evt = TLI.getValueType(DL, Ty, true);
906
907   // Only handle simple types.
908   if (evt == MVT::Other || !evt.isSimple())
909     return false;
910   VT = evt.getSimpleVT();
911
912   // This is a legal type, but it's not something we handle in fast-isel.
913   if (VT == MVT::f128)
914     return false;
915
916   // Handle all other legal types, i.e. a register that will directly hold this
917   // value.
918   return TLI.isTypeLegal(VT);
919 }
920
921 /// \brief Determine if the value type is supported by FastISel.
922 ///
923 /// FastISel for AArch64 can handle more value types than are legal. This adds
924 /// simple value type such as i1, i8, and i16.
925 bool AArch64FastISel::isTypeSupported(Type *Ty, MVT &VT, bool IsVectorAllowed) {
926   if (Ty->isVectorTy() && !IsVectorAllowed)
927     return false;
928
929   if (isTypeLegal(Ty, VT))
930     return true;
931
932   // If this is a type than can be sign or zero-extended to a basic operation
933   // go ahead and accept it now.
934   if (VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16)
935     return true;
936
937   return false;
938 }
939
940 bool AArch64FastISel::isValueAvailable(const Value *V) const {
941   if (!isa<Instruction>(V))
942     return true;
943
944   const auto *I = cast<Instruction>(V);
945   return FuncInfo.MBBMap[I->getParent()] == FuncInfo.MBB;
946 }
947
948 bool AArch64FastISel::simplifyAddress(Address &Addr, MVT VT) {
949   unsigned ScaleFactor = getImplicitScaleFactor(VT);
950   if (!ScaleFactor)
951     return false;
952
953   bool ImmediateOffsetNeedsLowering = false;
954   bool RegisterOffsetNeedsLowering = false;
955   int64_t Offset = Addr.getOffset();
956   if (((Offset < 0) || (Offset & (ScaleFactor - 1))) && !isInt<9>(Offset))
957     ImmediateOffsetNeedsLowering = true;
958   else if (Offset > 0 && !(Offset & (ScaleFactor - 1)) &&
959            !isUInt<12>(Offset / ScaleFactor))
960     ImmediateOffsetNeedsLowering = true;
961
962   // Cannot encode an offset register and an immediate offset in the same
963   // instruction. Fold the immediate offset into the load/store instruction and
964   // emit an additional add to take care of the offset register.
965   if (!ImmediateOffsetNeedsLowering && Addr.getOffset() && Addr.getOffsetReg())
966     RegisterOffsetNeedsLowering = true;
967
968   // Cannot encode zero register as base.
969   if (Addr.isRegBase() && Addr.getOffsetReg() && !Addr.getReg())
970     RegisterOffsetNeedsLowering = true;
971
972   // If this is a stack pointer and the offset needs to be simplified then put
973   // the alloca address into a register, set the base type back to register and
974   // continue. This should almost never happen.
975   if ((ImmediateOffsetNeedsLowering || Addr.getOffsetReg()) && Addr.isFIBase())
976   {
977     unsigned ResultReg = createResultReg(&AArch64::GPR64spRegClass);
978     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADDXri),
979             ResultReg)
980       .addFrameIndex(Addr.getFI())
981       .addImm(0)
982       .addImm(0);
983     Addr.setKind(Address::RegBase);
984     Addr.setReg(ResultReg);
985   }
986
987   if (RegisterOffsetNeedsLowering) {
988     unsigned ResultReg = 0;
989     if (Addr.getReg()) {
990       if (Addr.getExtendType() == AArch64_AM::SXTW ||
991           Addr.getExtendType() == AArch64_AM::UXTW   )
992         ResultReg = emitAddSub_rx(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
993                                   /*TODO:IsKill=*/false, Addr.getOffsetReg(),
994                                   /*TODO:IsKill=*/false, Addr.getExtendType(),
995                                   Addr.getShift());
996       else
997         ResultReg = emitAddSub_rs(/*UseAdd=*/true, MVT::i64, Addr.getReg(),
998                                   /*TODO:IsKill=*/false, Addr.getOffsetReg(),
999                                   /*TODO:IsKill=*/false, AArch64_AM::LSL,
1000                                   Addr.getShift());
1001     } else {
1002       if (Addr.getExtendType() == AArch64_AM::UXTW)
1003         ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
1004                                /*Op0IsKill=*/false, Addr.getShift(),
1005                                /*IsZExt=*/true);
1006       else if (Addr.getExtendType() == AArch64_AM::SXTW)
1007         ResultReg = emitLSL_ri(MVT::i64, MVT::i32, Addr.getOffsetReg(),
1008                                /*Op0IsKill=*/false, Addr.getShift(),
1009                                /*IsZExt=*/false);
1010       else
1011         ResultReg = emitLSL_ri(MVT::i64, MVT::i64, Addr.getOffsetReg(),
1012                                /*Op0IsKill=*/false, Addr.getShift());
1013     }
1014     if (!ResultReg)
1015       return false;
1016
1017     Addr.setReg(ResultReg);
1018     Addr.setOffsetReg(0);
1019     Addr.setShift(0);
1020     Addr.setExtendType(AArch64_AM::InvalidShiftExtend);
1021   }
1022
1023   // Since the offset is too large for the load/store instruction get the
1024   // reg+offset into a register.
1025   if (ImmediateOffsetNeedsLowering) {
1026     unsigned ResultReg;
1027     if (Addr.getReg())
1028       // Try to fold the immediate into the add instruction.
1029       ResultReg = emitAdd_ri_(MVT::i64, Addr.getReg(), /*IsKill=*/false, Offset);
1030     else
1031       ResultReg = fastEmit_i(MVT::i64, MVT::i64, ISD::Constant, Offset);
1032
1033     if (!ResultReg)
1034       return false;
1035     Addr.setReg(ResultReg);
1036     Addr.setOffset(0);
1037   }
1038   return true;
1039 }
1040
1041 void AArch64FastISel::addLoadStoreOperands(Address &Addr,
1042                                            const MachineInstrBuilder &MIB,
1043                                            MachineMemOperand::Flags Flags,
1044                                            unsigned ScaleFactor,
1045                                            MachineMemOperand *MMO) {
1046   int64_t Offset = Addr.getOffset() / ScaleFactor;
1047   // Frame base works a bit differently. Handle it separately.
1048   if (Addr.isFIBase()) {
1049     int FI = Addr.getFI();
1050     // FIXME: We shouldn't be using getObjectSize/getObjectAlignment.  The size
1051     // and alignment should be based on the VT.
1052     MMO = FuncInfo.MF->getMachineMemOperand(
1053         MachinePointerInfo::getFixedStack(*FuncInfo.MF, FI, Offset), Flags,
1054         MFI.getObjectSize(FI), MFI.getObjectAlignment(FI));
1055     // Now add the rest of the operands.
1056     MIB.addFrameIndex(FI).addImm(Offset);
1057   } else {
1058     assert(Addr.isRegBase() && "Unexpected address kind.");
1059     const MCInstrDesc &II = MIB->getDesc();
1060     unsigned Idx = (Flags & MachineMemOperand::MOStore) ? 1 : 0;
1061     Addr.setReg(
1062       constrainOperandRegClass(II, Addr.getReg(), II.getNumDefs()+Idx));
1063     Addr.setOffsetReg(
1064       constrainOperandRegClass(II, Addr.getOffsetReg(), II.getNumDefs()+Idx+1));
1065     if (Addr.getOffsetReg()) {
1066       assert(Addr.getOffset() == 0 && "Unexpected offset");
1067       bool IsSigned = Addr.getExtendType() == AArch64_AM::SXTW ||
1068                       Addr.getExtendType() == AArch64_AM::SXTX;
1069       MIB.addReg(Addr.getReg());
1070       MIB.addReg(Addr.getOffsetReg());
1071       MIB.addImm(IsSigned);
1072       MIB.addImm(Addr.getShift() != 0);
1073     } else
1074       MIB.addReg(Addr.getReg()).addImm(Offset);
1075   }
1076
1077   if (MMO)
1078     MIB.addMemOperand(MMO);
1079 }
1080
1081 unsigned AArch64FastISel::emitAddSub(bool UseAdd, MVT RetVT, const Value *LHS,
1082                                      const Value *RHS, bool SetFlags,
1083                                      bool WantResult,  bool IsZExt) {
1084   AArch64_AM::ShiftExtendType ExtendType = AArch64_AM::InvalidShiftExtend;
1085   bool NeedExtend = false;
1086   switch (RetVT.SimpleTy) {
1087   default:
1088     return 0;
1089   case MVT::i1:
1090     NeedExtend = true;
1091     break;
1092   case MVT::i8:
1093     NeedExtend = true;
1094     ExtendType = IsZExt ? AArch64_AM::UXTB : AArch64_AM::SXTB;
1095     break;
1096   case MVT::i16:
1097     NeedExtend = true;
1098     ExtendType = IsZExt ? AArch64_AM::UXTH : AArch64_AM::SXTH;
1099     break;
1100   case MVT::i32:  // fall-through
1101   case MVT::i64:
1102     break;
1103   }
1104   MVT SrcVT = RetVT;
1105   RetVT.SimpleTy = std::max(RetVT.SimpleTy, MVT::i32);
1106
1107   // Canonicalize immediates to the RHS first.
1108   if (UseAdd && isa<Constant>(LHS) && !isa<Constant>(RHS))
1109     std::swap(LHS, RHS);
1110
1111   // Canonicalize mul by power of 2 to the RHS.
1112   if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1113     if (isMulPowOf2(LHS))
1114       std::swap(LHS, RHS);
1115
1116   // Canonicalize shift immediate to the RHS.
1117   if (UseAdd && LHS->hasOneUse() && isValueAvailable(LHS))
1118     if (const auto *SI = dyn_cast<BinaryOperator>(LHS))
1119       if (isa<ConstantInt>(SI->getOperand(1)))
1120         if (SI->getOpcode() == Instruction::Shl  ||
1121             SI->getOpcode() == Instruction::LShr ||
1122             SI->getOpcode() == Instruction::AShr   )
1123           std::swap(LHS, RHS);
1124
1125   unsigned LHSReg = getRegForValue(LHS);
1126   if (!LHSReg)
1127     return 0;
1128   bool LHSIsKill = hasTrivialKill(LHS);
1129
1130   if (NeedExtend)
1131     LHSReg = emitIntExt(SrcVT, LHSReg, RetVT, IsZExt);
1132
1133   unsigned ResultReg = 0;
1134   if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1135     uint64_t Imm = IsZExt ? C->getZExtValue() : C->getSExtValue();
1136     if (C->isNegative())
1137       ResultReg = emitAddSub_ri(!UseAdd, RetVT, LHSReg, LHSIsKill, -Imm,
1138                                 SetFlags, WantResult);
1139     else
1140       ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, Imm, SetFlags,
1141                                 WantResult);
1142   } else if (const auto *C = dyn_cast<Constant>(RHS))
1143     if (C->isNullValue())
1144       ResultReg = emitAddSub_ri(UseAdd, RetVT, LHSReg, LHSIsKill, 0, SetFlags,
1145                                 WantResult);
1146
1147   if (ResultReg)
1148     return ResultReg;
1149
1150   // Only extend the RHS within the instruction if there is a valid extend type.
1151   if (ExtendType != AArch64_AM::InvalidShiftExtend && RHS->hasOneUse() &&
1152       isValueAvailable(RHS)) {
1153     if (const auto *SI = dyn_cast<BinaryOperator>(RHS))
1154       if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1)))
1155         if ((SI->getOpcode() == Instruction::Shl) && (C->getZExtValue() < 4)) {
1156           unsigned RHSReg = getRegForValue(SI->getOperand(0));
1157           if (!RHSReg)
1158             return 0;
1159           bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1160           return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1161                                RHSIsKill, ExtendType, C->getZExtValue(),
1162                                SetFlags, WantResult);
1163         }
1164     unsigned RHSReg = getRegForValue(RHS);
1165     if (!RHSReg)
1166       return 0;
1167     bool RHSIsKill = hasTrivialKill(RHS);
1168     return emitAddSub_rx(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1169                          ExtendType, 0, SetFlags, WantResult);
1170   }
1171
1172   // Check if the mul can be folded into the instruction.
1173   if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1174     if (isMulPowOf2(RHS)) {
1175       const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1176       const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1177
1178       if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1179         if (C->getValue().isPowerOf2())
1180           std::swap(MulLHS, MulRHS);
1181
1182       assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1183       uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1184       unsigned RHSReg = getRegForValue(MulLHS);
1185       if (!RHSReg)
1186         return 0;
1187       bool RHSIsKill = hasTrivialKill(MulLHS);
1188       ResultReg = emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1189                                 RHSIsKill, AArch64_AM::LSL, ShiftVal, SetFlags,
1190                                 WantResult);
1191       if (ResultReg)
1192         return ResultReg;
1193     }
1194   }
1195
1196   // Check if the shift can be folded into the instruction.
1197   if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1198     if (const auto *SI = dyn_cast<BinaryOperator>(RHS)) {
1199       if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1200         AArch64_AM::ShiftExtendType ShiftType = AArch64_AM::InvalidShiftExtend;
1201         switch (SI->getOpcode()) {
1202         default: break;
1203         case Instruction::Shl:  ShiftType = AArch64_AM::LSL; break;
1204         case Instruction::LShr: ShiftType = AArch64_AM::LSR; break;
1205         case Instruction::AShr: ShiftType = AArch64_AM::ASR; break;
1206         }
1207         uint64_t ShiftVal = C->getZExtValue();
1208         if (ShiftType != AArch64_AM::InvalidShiftExtend) {
1209           unsigned RHSReg = getRegForValue(SI->getOperand(0));
1210           if (!RHSReg)
1211             return 0;
1212           bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1213           ResultReg = emitAddSub_rs(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg,
1214                                     RHSIsKill, ShiftType, ShiftVal, SetFlags,
1215                                     WantResult);
1216           if (ResultReg)
1217             return ResultReg;
1218         }
1219       }
1220     }
1221   }
1222
1223   unsigned RHSReg = getRegForValue(RHS);
1224   if (!RHSReg)
1225     return 0;
1226   bool RHSIsKill = hasTrivialKill(RHS);
1227
1228   if (NeedExtend)
1229     RHSReg = emitIntExt(SrcVT, RHSReg, RetVT, IsZExt);
1230
1231   return emitAddSub_rr(UseAdd, RetVT, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1232                        SetFlags, WantResult);
1233 }
1234
1235 unsigned AArch64FastISel::emitAddSub_rr(bool UseAdd, MVT RetVT, unsigned LHSReg,
1236                                         bool LHSIsKill, unsigned RHSReg,
1237                                         bool RHSIsKill, bool SetFlags,
1238                                         bool WantResult) {
1239   assert(LHSReg && RHSReg && "Invalid register number.");
1240
1241   if (RetVT != MVT::i32 && RetVT != MVT::i64)
1242     return 0;
1243
1244   static const unsigned OpcTable[2][2][2] = {
1245     { { AArch64::SUBWrr,  AArch64::SUBXrr  },
1246       { AArch64::ADDWrr,  AArch64::ADDXrr  }  },
1247     { { AArch64::SUBSWrr, AArch64::SUBSXrr },
1248       { AArch64::ADDSWrr, AArch64::ADDSXrr }  }
1249   };
1250   bool Is64Bit = RetVT == MVT::i64;
1251   unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1252   const TargetRegisterClass *RC =
1253       Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1254   unsigned ResultReg;
1255   if (WantResult)
1256     ResultReg = createResultReg(RC);
1257   else
1258     ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1259
1260   const MCInstrDesc &II = TII.get(Opc);
1261   LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1262   RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1263   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1264       .addReg(LHSReg, getKillRegState(LHSIsKill))
1265       .addReg(RHSReg, getKillRegState(RHSIsKill));
1266   return ResultReg;
1267 }
1268
1269 unsigned AArch64FastISel::emitAddSub_ri(bool UseAdd, MVT RetVT, unsigned LHSReg,
1270                                         bool LHSIsKill, uint64_t Imm,
1271                                         bool SetFlags, bool WantResult) {
1272   assert(LHSReg && "Invalid register number.");
1273
1274   if (RetVT != MVT::i32 && RetVT != MVT::i64)
1275     return 0;
1276
1277   unsigned ShiftImm;
1278   if (isUInt<12>(Imm))
1279     ShiftImm = 0;
1280   else if ((Imm & 0xfff000) == Imm) {
1281     ShiftImm = 12;
1282     Imm >>= 12;
1283   } else
1284     return 0;
1285
1286   static const unsigned OpcTable[2][2][2] = {
1287     { { AArch64::SUBWri,  AArch64::SUBXri  },
1288       { AArch64::ADDWri,  AArch64::ADDXri  }  },
1289     { { AArch64::SUBSWri, AArch64::SUBSXri },
1290       { AArch64::ADDSWri, AArch64::ADDSXri }  }
1291   };
1292   bool Is64Bit = RetVT == MVT::i64;
1293   unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1294   const TargetRegisterClass *RC;
1295   if (SetFlags)
1296     RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1297   else
1298     RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1299   unsigned ResultReg;
1300   if (WantResult)
1301     ResultReg = createResultReg(RC);
1302   else
1303     ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1304
1305   const MCInstrDesc &II = TII.get(Opc);
1306   LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1307   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1308       .addReg(LHSReg, getKillRegState(LHSIsKill))
1309       .addImm(Imm)
1310       .addImm(getShifterImm(AArch64_AM::LSL, ShiftImm));
1311   return ResultReg;
1312 }
1313
1314 unsigned AArch64FastISel::emitAddSub_rs(bool UseAdd, MVT RetVT, unsigned LHSReg,
1315                                         bool LHSIsKill, unsigned RHSReg,
1316                                         bool RHSIsKill,
1317                                         AArch64_AM::ShiftExtendType ShiftType,
1318                                         uint64_t ShiftImm, bool SetFlags,
1319                                         bool WantResult) {
1320   assert(LHSReg && RHSReg && "Invalid register number.");
1321
1322   if (RetVT != MVT::i32 && RetVT != MVT::i64)
1323     return 0;
1324
1325   // Don't deal with undefined shifts.
1326   if (ShiftImm >= RetVT.getSizeInBits())
1327     return 0;
1328
1329   static const unsigned OpcTable[2][2][2] = {
1330     { { AArch64::SUBWrs,  AArch64::SUBXrs  },
1331       { AArch64::ADDWrs,  AArch64::ADDXrs  }  },
1332     { { AArch64::SUBSWrs, AArch64::SUBSXrs },
1333       { AArch64::ADDSWrs, AArch64::ADDSXrs }  }
1334   };
1335   bool Is64Bit = RetVT == MVT::i64;
1336   unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1337   const TargetRegisterClass *RC =
1338       Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1339   unsigned ResultReg;
1340   if (WantResult)
1341     ResultReg = createResultReg(RC);
1342   else
1343     ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1344
1345   const MCInstrDesc &II = TII.get(Opc);
1346   LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1347   RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1348   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1349       .addReg(LHSReg, getKillRegState(LHSIsKill))
1350       .addReg(RHSReg, getKillRegState(RHSIsKill))
1351       .addImm(getShifterImm(ShiftType, ShiftImm));
1352   return ResultReg;
1353 }
1354
1355 unsigned AArch64FastISel::emitAddSub_rx(bool UseAdd, MVT RetVT, unsigned LHSReg,
1356                                         bool LHSIsKill, unsigned RHSReg,
1357                                         bool RHSIsKill,
1358                                         AArch64_AM::ShiftExtendType ExtType,
1359                                         uint64_t ShiftImm, bool SetFlags,
1360                                         bool WantResult) {
1361   assert(LHSReg && RHSReg && "Invalid register number.");
1362
1363   if (RetVT != MVT::i32 && RetVT != MVT::i64)
1364     return 0;
1365
1366   if (ShiftImm >= 4)
1367     return 0;
1368
1369   static const unsigned OpcTable[2][2][2] = {
1370     { { AArch64::SUBWrx,  AArch64::SUBXrx  },
1371       { AArch64::ADDWrx,  AArch64::ADDXrx  }  },
1372     { { AArch64::SUBSWrx, AArch64::SUBSXrx },
1373       { AArch64::ADDSWrx, AArch64::ADDSXrx }  }
1374   };
1375   bool Is64Bit = RetVT == MVT::i64;
1376   unsigned Opc = OpcTable[SetFlags][UseAdd][Is64Bit];
1377   const TargetRegisterClass *RC = nullptr;
1378   if (SetFlags)
1379     RC = Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
1380   else
1381     RC = Is64Bit ? &AArch64::GPR64spRegClass : &AArch64::GPR32spRegClass;
1382   unsigned ResultReg;
1383   if (WantResult)
1384     ResultReg = createResultReg(RC);
1385   else
1386     ResultReg = Is64Bit ? AArch64::XZR : AArch64::WZR;
1387
1388   const MCInstrDesc &II = TII.get(Opc);
1389   LHSReg = constrainOperandRegClass(II, LHSReg, II.getNumDefs());
1390   RHSReg = constrainOperandRegClass(II, RHSReg, II.getNumDefs() + 1);
1391   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II, ResultReg)
1392       .addReg(LHSReg, getKillRegState(LHSIsKill))
1393       .addReg(RHSReg, getKillRegState(RHSIsKill))
1394       .addImm(getArithExtendImm(ExtType, ShiftImm));
1395   return ResultReg;
1396 }
1397
1398 bool AArch64FastISel::emitCmp(const Value *LHS, const Value *RHS, bool IsZExt) {
1399   Type *Ty = LHS->getType();
1400   EVT EVT = TLI.getValueType(DL, Ty, true);
1401   if (!EVT.isSimple())
1402     return false;
1403   MVT VT = EVT.getSimpleVT();
1404
1405   switch (VT.SimpleTy) {
1406   default:
1407     return false;
1408   case MVT::i1:
1409   case MVT::i8:
1410   case MVT::i16:
1411   case MVT::i32:
1412   case MVT::i64:
1413     return emitICmp(VT, LHS, RHS, IsZExt);
1414   case MVT::f32:
1415   case MVT::f64:
1416     return emitFCmp(VT, LHS, RHS);
1417   }
1418 }
1419
1420 bool AArch64FastISel::emitICmp(MVT RetVT, const Value *LHS, const Value *RHS,
1421                                bool IsZExt) {
1422   return emitSub(RetVT, LHS, RHS, /*SetFlags=*/true, /*WantResult=*/false,
1423                  IsZExt) != 0;
1424 }
1425
1426 bool AArch64FastISel::emitICmp_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1427                                   uint64_t Imm) {
1428   return emitAddSub_ri(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, Imm,
1429                        /*SetFlags=*/true, /*WantResult=*/false) != 0;
1430 }
1431
1432 bool AArch64FastISel::emitFCmp(MVT RetVT, const Value *LHS, const Value *RHS) {
1433   if (RetVT != MVT::f32 && RetVT != MVT::f64)
1434     return false;
1435
1436   // Check to see if the 2nd operand is a constant that we can encode directly
1437   // in the compare.
1438   bool UseImm = false;
1439   if (const auto *CFP = dyn_cast<ConstantFP>(RHS))
1440     if (CFP->isZero() && !CFP->isNegative())
1441       UseImm = true;
1442
1443   unsigned LHSReg = getRegForValue(LHS);
1444   if (!LHSReg)
1445     return false;
1446   bool LHSIsKill = hasTrivialKill(LHS);
1447
1448   if (UseImm) {
1449     unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDri : AArch64::FCMPSri;
1450     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1451         .addReg(LHSReg, getKillRegState(LHSIsKill));
1452     return true;
1453   }
1454
1455   unsigned RHSReg = getRegForValue(RHS);
1456   if (!RHSReg)
1457     return false;
1458   bool RHSIsKill = hasTrivialKill(RHS);
1459
1460   unsigned Opc = (RetVT == MVT::f64) ? AArch64::FCMPDrr : AArch64::FCMPSrr;
1461   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
1462       .addReg(LHSReg, getKillRegState(LHSIsKill))
1463       .addReg(RHSReg, getKillRegState(RHSIsKill));
1464   return true;
1465 }
1466
1467 unsigned AArch64FastISel::emitAdd(MVT RetVT, const Value *LHS, const Value *RHS,
1468                                   bool SetFlags, bool WantResult, bool IsZExt) {
1469   return emitAddSub(/*UseAdd=*/true, RetVT, LHS, RHS, SetFlags, WantResult,
1470                     IsZExt);
1471 }
1472
1473 /// \brief This method is a wrapper to simplify add emission.
1474 ///
1475 /// First try to emit an add with an immediate operand using emitAddSub_ri. If
1476 /// that fails, then try to materialize the immediate into a register and use
1477 /// emitAddSub_rr instead.
1478 unsigned AArch64FastISel::emitAdd_ri_(MVT VT, unsigned Op0, bool Op0IsKill,
1479                                       int64_t Imm) {
1480   unsigned ResultReg;
1481   if (Imm < 0)
1482     ResultReg = emitAddSub_ri(false, VT, Op0, Op0IsKill, -Imm);
1483   else
1484     ResultReg = emitAddSub_ri(true, VT, Op0, Op0IsKill, Imm);
1485
1486   if (ResultReg)
1487     return ResultReg;
1488
1489   unsigned CReg = fastEmit_i(VT, VT, ISD::Constant, Imm);
1490   if (!CReg)
1491     return 0;
1492
1493   ResultReg = emitAddSub_rr(true, VT, Op0, Op0IsKill, CReg, true);
1494   return ResultReg;
1495 }
1496
1497 unsigned AArch64FastISel::emitSub(MVT RetVT, const Value *LHS, const Value *RHS,
1498                                   bool SetFlags, bool WantResult, bool IsZExt) {
1499   return emitAddSub(/*UseAdd=*/false, RetVT, LHS, RHS, SetFlags, WantResult,
1500                     IsZExt);
1501 }
1502
1503 unsigned AArch64FastISel::emitSubs_rr(MVT RetVT, unsigned LHSReg,
1504                                       bool LHSIsKill, unsigned RHSReg,
1505                                       bool RHSIsKill, bool WantResult) {
1506   return emitAddSub_rr(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1507                        RHSIsKill, /*SetFlags=*/true, WantResult);
1508 }
1509
1510 unsigned AArch64FastISel::emitSubs_rs(MVT RetVT, unsigned LHSReg,
1511                                       bool LHSIsKill, unsigned RHSReg,
1512                                       bool RHSIsKill,
1513                                       AArch64_AM::ShiftExtendType ShiftType,
1514                                       uint64_t ShiftImm, bool WantResult) {
1515   return emitAddSub_rs(/*UseAdd=*/false, RetVT, LHSReg, LHSIsKill, RHSReg,
1516                        RHSIsKill, ShiftType, ShiftImm, /*SetFlags=*/true,
1517                        WantResult);
1518 }
1519
1520 unsigned AArch64FastISel::emitLogicalOp(unsigned ISDOpc, MVT RetVT,
1521                                         const Value *LHS, const Value *RHS) {
1522   // Canonicalize immediates to the RHS first.
1523   if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS))
1524     std::swap(LHS, RHS);
1525
1526   // Canonicalize mul by power-of-2 to the RHS.
1527   if (LHS->hasOneUse() && isValueAvailable(LHS))
1528     if (isMulPowOf2(LHS))
1529       std::swap(LHS, RHS);
1530
1531   // Canonicalize shift immediate to the RHS.
1532   if (LHS->hasOneUse() && isValueAvailable(LHS))
1533     if (const auto *SI = dyn_cast<ShlOperator>(LHS))
1534       if (isa<ConstantInt>(SI->getOperand(1)))
1535         std::swap(LHS, RHS);
1536
1537   unsigned LHSReg = getRegForValue(LHS);
1538   if (!LHSReg)
1539     return 0;
1540   bool LHSIsKill = hasTrivialKill(LHS);
1541
1542   unsigned ResultReg = 0;
1543   if (const auto *C = dyn_cast<ConstantInt>(RHS)) {
1544     uint64_t Imm = C->getZExtValue();
1545     ResultReg = emitLogicalOp_ri(ISDOpc, RetVT, LHSReg, LHSIsKill, Imm);
1546   }
1547   if (ResultReg)
1548     return ResultReg;
1549
1550   // Check if the mul can be folded into the instruction.
1551   if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1552     if (isMulPowOf2(RHS)) {
1553       const Value *MulLHS = cast<MulOperator>(RHS)->getOperand(0);
1554       const Value *MulRHS = cast<MulOperator>(RHS)->getOperand(1);
1555
1556       if (const auto *C = dyn_cast<ConstantInt>(MulLHS))
1557         if (C->getValue().isPowerOf2())
1558           std::swap(MulLHS, MulRHS);
1559
1560       assert(isa<ConstantInt>(MulRHS) && "Expected a ConstantInt.");
1561       uint64_t ShiftVal = cast<ConstantInt>(MulRHS)->getValue().logBase2();
1562
1563       unsigned RHSReg = getRegForValue(MulLHS);
1564       if (!RHSReg)
1565         return 0;
1566       bool RHSIsKill = hasTrivialKill(MulLHS);
1567       ResultReg = emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1568                                    RHSIsKill, ShiftVal);
1569       if (ResultReg)
1570         return ResultReg;
1571     }
1572   }
1573
1574   // Check if the shift can be folded into the instruction.
1575   if (RHS->hasOneUse() && isValueAvailable(RHS)) {
1576     if (const auto *SI = dyn_cast<ShlOperator>(RHS))
1577       if (const auto *C = dyn_cast<ConstantInt>(SI->getOperand(1))) {
1578         uint64_t ShiftVal = C->getZExtValue();
1579         unsigned RHSReg = getRegForValue(SI->getOperand(0));
1580         if (!RHSReg)
1581           return 0;
1582         bool RHSIsKill = hasTrivialKill(SI->getOperand(0));
1583         ResultReg = emitLogicalOp_rs(ISDOpc, RetVT, LHSReg, LHSIsKill, RHSReg,
1584                                      RHSIsKill, ShiftVal);
1585         if (ResultReg)
1586           return ResultReg;
1587       }
1588   }
1589
1590   unsigned RHSReg = getRegForValue(RHS);
1591   if (!RHSReg)
1592     return 0;
1593   bool RHSIsKill = hasTrivialKill(RHS);
1594
1595   MVT VT = std::max(MVT::i32, RetVT.SimpleTy);
1596   ResultReg = fastEmit_rr(VT, VT, ISDOpc, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
1597   if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1598     uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1599     ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1600   }
1601   return ResultReg;
1602 }
1603
1604 unsigned AArch64FastISel::emitLogicalOp_ri(unsigned ISDOpc, MVT RetVT,
1605                                            unsigned LHSReg, bool LHSIsKill,
1606                                            uint64_t Imm) {
1607   static_assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR),
1608                 "ISD nodes are not consecutive!");
1609   static const unsigned OpcTable[3][2] = {
1610     { AArch64::ANDWri, AArch64::ANDXri },
1611     { AArch64::ORRWri, AArch64::ORRXri },
1612     { AArch64::EORWri, AArch64::EORXri }
1613   };
1614   const TargetRegisterClass *RC;
1615   unsigned Opc;
1616   unsigned RegSize;
1617   switch (RetVT.SimpleTy) {
1618   default:
1619     return 0;
1620   case MVT::i1:
1621   case MVT::i8:
1622   case MVT::i16:
1623   case MVT::i32: {
1624     unsigned Idx = ISDOpc - ISD::AND;
1625     Opc = OpcTable[Idx][0];
1626     RC = &AArch64::GPR32spRegClass;
1627     RegSize = 32;
1628     break;
1629   }
1630   case MVT::i64:
1631     Opc = OpcTable[ISDOpc - ISD::AND][1];
1632     RC = &AArch64::GPR64spRegClass;
1633     RegSize = 64;
1634     break;
1635   }
1636
1637   if (!AArch64_AM::isLogicalImmediate(Imm, RegSize))
1638     return 0;
1639
1640   unsigned ResultReg =
1641       fastEmitInst_ri(Opc, RC, LHSReg, LHSIsKill,
1642                       AArch64_AM::encodeLogicalImmediate(Imm, RegSize));
1643   if (RetVT >= MVT::i8 && RetVT <= MVT::i16 && ISDOpc != ISD::AND) {
1644     uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1645     ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1646   }
1647   return ResultReg;
1648 }
1649
1650 unsigned AArch64FastISel::emitLogicalOp_rs(unsigned ISDOpc, MVT RetVT,
1651                                            unsigned LHSReg, bool LHSIsKill,
1652                                            unsigned RHSReg, bool RHSIsKill,
1653                                            uint64_t ShiftImm) {
1654   static_assert((ISD::AND + 1 == ISD::OR) && (ISD::AND + 2 == ISD::XOR),
1655                 "ISD nodes are not consecutive!");
1656   static const unsigned OpcTable[3][2] = {
1657     { AArch64::ANDWrs, AArch64::ANDXrs },
1658     { AArch64::ORRWrs, AArch64::ORRXrs },
1659     { AArch64::EORWrs, AArch64::EORXrs }
1660   };
1661
1662   // Don't deal with undefined shifts.
1663   if (ShiftImm >= RetVT.getSizeInBits())
1664     return 0;
1665
1666   const TargetRegisterClass *RC;
1667   unsigned Opc;
1668   switch (RetVT.SimpleTy) {
1669   default:
1670     return 0;
1671   case MVT::i1:
1672   case MVT::i8:
1673   case MVT::i16:
1674   case MVT::i32:
1675     Opc = OpcTable[ISDOpc - ISD::AND][0];
1676     RC = &AArch64::GPR32RegClass;
1677     break;
1678   case MVT::i64:
1679     Opc = OpcTable[ISDOpc - ISD::AND][1];
1680     RC = &AArch64::GPR64RegClass;
1681     break;
1682   }
1683   unsigned ResultReg =
1684       fastEmitInst_rri(Opc, RC, LHSReg, LHSIsKill, RHSReg, RHSIsKill,
1685                        AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftImm));
1686   if (RetVT >= MVT::i8 && RetVT <= MVT::i16) {
1687     uint64_t Mask = (RetVT == MVT::i8) ? 0xff : 0xffff;
1688     ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
1689   }
1690   return ResultReg;
1691 }
1692
1693 unsigned AArch64FastISel::emitAnd_ri(MVT RetVT, unsigned LHSReg, bool LHSIsKill,
1694                                      uint64_t Imm) {
1695   return emitLogicalOp_ri(ISD::AND, RetVT, LHSReg, LHSIsKill, Imm);
1696 }
1697
1698 unsigned AArch64FastISel::emitLoad(MVT VT, MVT RetVT, Address Addr,
1699                                    bool WantZExt, MachineMemOperand *MMO) {
1700   if (!TLI.allowsMisalignedMemoryAccesses(VT))
1701     return 0;
1702
1703   // Simplify this down to something we can handle.
1704   if (!simplifyAddress(Addr, VT))
1705     return 0;
1706
1707   unsigned ScaleFactor = getImplicitScaleFactor(VT);
1708   if (!ScaleFactor)
1709     llvm_unreachable("Unexpected value type.");
1710
1711   // Negative offsets require unscaled, 9-bit, signed immediate offsets.
1712   // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
1713   bool UseScaled = true;
1714   if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
1715     UseScaled = false;
1716     ScaleFactor = 1;
1717   }
1718
1719   static const unsigned GPOpcTable[2][8][4] = {
1720     // Sign-extend.
1721     { { AArch64::LDURSBWi,  AArch64::LDURSHWi,  AArch64::LDURWi,
1722         AArch64::LDURXi  },
1723       { AArch64::LDURSBXi,  AArch64::LDURSHXi,  AArch64::LDURSWi,
1724         AArch64::LDURXi  },
1725       { AArch64::LDRSBWui,  AArch64::LDRSHWui,  AArch64::LDRWui,
1726         AArch64::LDRXui  },
1727       { AArch64::LDRSBXui,  AArch64::LDRSHXui,  AArch64::LDRSWui,
1728         AArch64::LDRXui  },
1729       { AArch64::LDRSBWroX, AArch64::LDRSHWroX, AArch64::LDRWroX,
1730         AArch64::LDRXroX },
1731       { AArch64::LDRSBXroX, AArch64::LDRSHXroX, AArch64::LDRSWroX,
1732         AArch64::LDRXroX },
1733       { AArch64::LDRSBWroW, AArch64::LDRSHWroW, AArch64::LDRWroW,
1734         AArch64::LDRXroW },
1735       { AArch64::LDRSBXroW, AArch64::LDRSHXroW, AArch64::LDRSWroW,
1736         AArch64::LDRXroW }
1737     },
1738     // Zero-extend.
1739     { { AArch64::LDURBBi,   AArch64::LDURHHi,   AArch64::LDURWi,
1740         AArch64::LDURXi  },
1741       { AArch64::LDURBBi,   AArch64::LDURHHi,   AArch64::LDURWi,
1742         AArch64::LDURXi  },
1743       { AArch64::LDRBBui,   AArch64::LDRHHui,   AArch64::LDRWui,
1744         AArch64::LDRXui  },
1745       { AArch64::LDRBBui,   AArch64::LDRHHui,   AArch64::LDRWui,
1746         AArch64::LDRXui  },
1747       { AArch64::LDRBBroX,  AArch64::LDRHHroX,  AArch64::LDRWroX,
1748         AArch64::LDRXroX },
1749       { AArch64::LDRBBroX,  AArch64::LDRHHroX,  AArch64::LDRWroX,
1750         AArch64::LDRXroX },
1751       { AArch64::LDRBBroW,  AArch64::LDRHHroW,  AArch64::LDRWroW,
1752         AArch64::LDRXroW },
1753       { AArch64::LDRBBroW,  AArch64::LDRHHroW,  AArch64::LDRWroW,
1754         AArch64::LDRXroW }
1755     }
1756   };
1757
1758   static const unsigned FPOpcTable[4][2] = {
1759     { AArch64::LDURSi,  AArch64::LDURDi  },
1760     { AArch64::LDRSui,  AArch64::LDRDui  },
1761     { AArch64::LDRSroX, AArch64::LDRDroX },
1762     { AArch64::LDRSroW, AArch64::LDRDroW }
1763   };
1764
1765   unsigned Opc;
1766   const TargetRegisterClass *RC;
1767   bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
1768                       Addr.getOffsetReg();
1769   unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
1770   if (Addr.getExtendType() == AArch64_AM::UXTW ||
1771       Addr.getExtendType() == AArch64_AM::SXTW)
1772     Idx++;
1773
1774   bool IsRet64Bit = RetVT == MVT::i64;
1775   switch (VT.SimpleTy) {
1776   default:
1777     llvm_unreachable("Unexpected value type.");
1778   case MVT::i1: // Intentional fall-through.
1779   case MVT::i8:
1780     Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][0];
1781     RC = (IsRet64Bit && !WantZExt) ?
1782              &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1783     break;
1784   case MVT::i16:
1785     Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][1];
1786     RC = (IsRet64Bit && !WantZExt) ?
1787              &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1788     break;
1789   case MVT::i32:
1790     Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][2];
1791     RC = (IsRet64Bit && !WantZExt) ?
1792              &AArch64::GPR64RegClass: &AArch64::GPR32RegClass;
1793     break;
1794   case MVT::i64:
1795     Opc = GPOpcTable[WantZExt][2 * Idx + IsRet64Bit][3];
1796     RC = &AArch64::GPR64RegClass;
1797     break;
1798   case MVT::f32:
1799     Opc = FPOpcTable[Idx][0];
1800     RC = &AArch64::FPR32RegClass;
1801     break;
1802   case MVT::f64:
1803     Opc = FPOpcTable[Idx][1];
1804     RC = &AArch64::FPR64RegClass;
1805     break;
1806   }
1807
1808   // Create the base instruction, then add the operands.
1809   unsigned ResultReg = createResultReg(RC);
1810   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1811                                     TII.get(Opc), ResultReg);
1812   addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOLoad, ScaleFactor, MMO);
1813
1814   // Loading an i1 requires special handling.
1815   if (VT == MVT::i1) {
1816     unsigned ANDReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, 1);
1817     assert(ANDReg && "Unexpected AND instruction emission failure.");
1818     ResultReg = ANDReg;
1819   }
1820
1821   // For zero-extending loads to 64bit we emit a 32bit load and then convert
1822   // the 32bit reg to a 64bit reg.
1823   if (WantZExt && RetVT == MVT::i64 && VT <= MVT::i32) {
1824     unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
1825     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
1826             TII.get(AArch64::SUBREG_TO_REG), Reg64)
1827         .addImm(0)
1828         .addReg(ResultReg, getKillRegState(true))
1829         .addImm(AArch64::sub_32);
1830     ResultReg = Reg64;
1831   }
1832   return ResultReg;
1833 }
1834
1835 bool AArch64FastISel::selectAddSub(const Instruction *I) {
1836   MVT VT;
1837   if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1838     return false;
1839
1840   if (VT.isVector())
1841     return selectOperator(I, I->getOpcode());
1842
1843   unsigned ResultReg;
1844   switch (I->getOpcode()) {
1845   default:
1846     llvm_unreachable("Unexpected instruction.");
1847   case Instruction::Add:
1848     ResultReg = emitAdd(VT, I->getOperand(0), I->getOperand(1));
1849     break;
1850   case Instruction::Sub:
1851     ResultReg = emitSub(VT, I->getOperand(0), I->getOperand(1));
1852     break;
1853   }
1854   if (!ResultReg)
1855     return false;
1856
1857   updateValueMap(I, ResultReg);
1858   return true;
1859 }
1860
1861 bool AArch64FastISel::selectLogicalOp(const Instruction *I) {
1862   MVT VT;
1863   if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
1864     return false;
1865
1866   if (VT.isVector())
1867     return selectOperator(I, I->getOpcode());
1868
1869   unsigned ResultReg;
1870   switch (I->getOpcode()) {
1871   default:
1872     llvm_unreachable("Unexpected instruction.");
1873   case Instruction::And:
1874     ResultReg = emitLogicalOp(ISD::AND, VT, I->getOperand(0), I->getOperand(1));
1875     break;
1876   case Instruction::Or:
1877     ResultReg = emitLogicalOp(ISD::OR, VT, I->getOperand(0), I->getOperand(1));
1878     break;
1879   case Instruction::Xor:
1880     ResultReg = emitLogicalOp(ISD::XOR, VT, I->getOperand(0), I->getOperand(1));
1881     break;
1882   }
1883   if (!ResultReg)
1884     return false;
1885
1886   updateValueMap(I, ResultReg);
1887   return true;
1888 }
1889
1890 bool AArch64FastISel::selectLoad(const Instruction *I) {
1891   MVT VT;
1892   // Verify we have a legal type before going any further.  Currently, we handle
1893   // simple types that will directly fit in a register (i32/f32/i64/f64) or
1894   // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
1895   if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true) ||
1896       cast<LoadInst>(I)->isAtomic())
1897     return false;
1898
1899   const Value *SV = I->getOperand(0);
1900   if (TLI.supportSwiftError()) {
1901     // Swifterror values can come from either a function parameter with
1902     // swifterror attribute or an alloca with swifterror attribute.
1903     if (const Argument *Arg = dyn_cast<Argument>(SV)) {
1904       if (Arg->hasSwiftErrorAttr())
1905         return false;
1906     }
1907
1908     if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(SV)) {
1909       if (Alloca->isSwiftError())
1910         return false;
1911     }
1912   }
1913
1914   // See if we can handle this address.
1915   Address Addr;
1916   if (!computeAddress(I->getOperand(0), Addr, I->getType()))
1917     return false;
1918
1919   // Fold the following sign-/zero-extend into the load instruction.
1920   bool WantZExt = true;
1921   MVT RetVT = VT;
1922   const Value *IntExtVal = nullptr;
1923   if (I->hasOneUse()) {
1924     if (const auto *ZE = dyn_cast<ZExtInst>(I->use_begin()->getUser())) {
1925       if (isTypeSupported(ZE->getType(), RetVT))
1926         IntExtVal = ZE;
1927       else
1928         RetVT = VT;
1929     } else if (const auto *SE = dyn_cast<SExtInst>(I->use_begin()->getUser())) {
1930       if (isTypeSupported(SE->getType(), RetVT))
1931         IntExtVal = SE;
1932       else
1933         RetVT = VT;
1934       WantZExt = false;
1935     }
1936   }
1937
1938   unsigned ResultReg =
1939       emitLoad(VT, RetVT, Addr, WantZExt, createMachineMemOperandFor(I));
1940   if (!ResultReg)
1941     return false;
1942
1943   // There are a few different cases we have to handle, because the load or the
1944   // sign-/zero-extend might not be selected by FastISel if we fall-back to
1945   // SelectionDAG. There is also an ordering issue when both instructions are in
1946   // different basic blocks.
1947   // 1.) The load instruction is selected by FastISel, but the integer extend
1948   //     not. This usually happens when the integer extend is in a different
1949   //     basic block and SelectionDAG took over for that basic block.
1950   // 2.) The load instruction is selected before the integer extend. This only
1951   //     happens when the integer extend is in a different basic block.
1952   // 3.) The load instruction is selected by SelectionDAG and the integer extend
1953   //     by FastISel. This happens if there are instructions between the load
1954   //     and the integer extend that couldn't be selected by FastISel.
1955   if (IntExtVal) {
1956     // The integer extend hasn't been emitted yet. FastISel or SelectionDAG
1957     // could select it. Emit a copy to subreg if necessary. FastISel will remove
1958     // it when it selects the integer extend.
1959     unsigned Reg = lookUpRegForValue(IntExtVal);
1960     auto *MI = MRI.getUniqueVRegDef(Reg);
1961     if (!MI) {
1962       if (RetVT == MVT::i64 && VT <= MVT::i32) {
1963         if (WantZExt) {
1964           // Delete the last emitted instruction from emitLoad (SUBREG_TO_REG).
1965           std::prev(FuncInfo.InsertPt)->eraseFromParent();
1966           ResultReg = std::prev(FuncInfo.InsertPt)->getOperand(0).getReg();
1967         } else
1968           ResultReg = fastEmitInst_extractsubreg(MVT::i32, ResultReg,
1969                                                  /*IsKill=*/true,
1970                                                  AArch64::sub_32);
1971       }
1972       updateValueMap(I, ResultReg);
1973       return true;
1974     }
1975
1976     // The integer extend has already been emitted - delete all the instructions
1977     // that have been emitted by the integer extend lowering code and use the
1978     // result from the load instruction directly.
1979     while (MI) {
1980       Reg = 0;
1981       for (auto &Opnd : MI->uses()) {
1982         if (Opnd.isReg()) {
1983           Reg = Opnd.getReg();
1984           break;
1985         }
1986       }
1987       MI->eraseFromParent();
1988       MI = nullptr;
1989       if (Reg)
1990         MI = MRI.getUniqueVRegDef(Reg);
1991     }
1992     updateValueMap(IntExtVal, ResultReg);
1993     return true;
1994   }
1995
1996   updateValueMap(I, ResultReg);
1997   return true;
1998 }
1999
2000 bool AArch64FastISel::emitStore(MVT VT, unsigned SrcReg, Address Addr,
2001                                 MachineMemOperand *MMO) {
2002   if (!TLI.allowsMisalignedMemoryAccesses(VT))
2003     return false;
2004
2005   // Simplify this down to something we can handle.
2006   if (!simplifyAddress(Addr, VT))
2007     return false;
2008
2009   unsigned ScaleFactor = getImplicitScaleFactor(VT);
2010   if (!ScaleFactor)
2011     llvm_unreachable("Unexpected value type.");
2012
2013   // Negative offsets require unscaled, 9-bit, signed immediate offsets.
2014   // Otherwise, we try using scaled, 12-bit, unsigned immediate offsets.
2015   bool UseScaled = true;
2016   if ((Addr.getOffset() < 0) || (Addr.getOffset() & (ScaleFactor - 1))) {
2017     UseScaled = false;
2018     ScaleFactor = 1;
2019   }
2020
2021   static const unsigned OpcTable[4][6] = {
2022     { AArch64::STURBBi,  AArch64::STURHHi,  AArch64::STURWi,  AArch64::STURXi,
2023       AArch64::STURSi,   AArch64::STURDi },
2024     { AArch64::STRBBui,  AArch64::STRHHui,  AArch64::STRWui,  AArch64::STRXui,
2025       AArch64::STRSui,   AArch64::STRDui },
2026     { AArch64::STRBBroX, AArch64::STRHHroX, AArch64::STRWroX, AArch64::STRXroX,
2027       AArch64::STRSroX,  AArch64::STRDroX },
2028     { AArch64::STRBBroW, AArch64::STRHHroW, AArch64::STRWroW, AArch64::STRXroW,
2029       AArch64::STRSroW,  AArch64::STRDroW }
2030   };
2031
2032   unsigned Opc;
2033   bool VTIsi1 = false;
2034   bool UseRegOffset = Addr.isRegBase() && !Addr.getOffset() && Addr.getReg() &&
2035                       Addr.getOffsetReg();
2036   unsigned Idx = UseRegOffset ? 2 : UseScaled ? 1 : 0;
2037   if (Addr.getExtendType() == AArch64_AM::UXTW ||
2038       Addr.getExtendType() == AArch64_AM::SXTW)
2039     Idx++;
2040
2041   switch (VT.SimpleTy) {
2042   default: llvm_unreachable("Unexpected value type.");
2043   case MVT::i1:  VTIsi1 = true;
2044   case MVT::i8:  Opc = OpcTable[Idx][0]; break;
2045   case MVT::i16: Opc = OpcTable[Idx][1]; break;
2046   case MVT::i32: Opc = OpcTable[Idx][2]; break;
2047   case MVT::i64: Opc = OpcTable[Idx][3]; break;
2048   case MVT::f32: Opc = OpcTable[Idx][4]; break;
2049   case MVT::f64: Opc = OpcTable[Idx][5]; break;
2050   }
2051
2052   // Storing an i1 requires special handling.
2053   if (VTIsi1 && SrcReg != AArch64::WZR) {
2054     unsigned ANDReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
2055     assert(ANDReg && "Unexpected AND instruction emission failure.");
2056     SrcReg = ANDReg;
2057   }
2058   // Create the base instruction, then add the operands.
2059   const MCInstrDesc &II = TII.get(Opc);
2060   SrcReg = constrainOperandRegClass(II, SrcReg, II.getNumDefs());
2061   MachineInstrBuilder MIB =
2062       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(SrcReg);
2063   addLoadStoreOperands(Addr, MIB, MachineMemOperand::MOStore, ScaleFactor, MMO);
2064
2065   return true;
2066 }
2067
2068 bool AArch64FastISel::selectStore(const Instruction *I) {
2069   MVT VT;
2070   const Value *Op0 = I->getOperand(0);
2071   // Verify we have a legal type before going any further.  Currently, we handle
2072   // simple types that will directly fit in a register (i32/f32/i64/f64) or
2073   // those that can be sign or zero-extended to a basic operation (i1/i8/i16).
2074   if (!isTypeSupported(Op0->getType(), VT, /*IsVectorAllowed=*/true) ||
2075       cast<StoreInst>(I)->isAtomic())
2076     return false;
2077
2078   const Value *PtrV = I->getOperand(1);
2079   if (TLI.supportSwiftError()) {
2080     // Swifterror values can come from either a function parameter with
2081     // swifterror attribute or an alloca with swifterror attribute.
2082     if (const Argument *Arg = dyn_cast<Argument>(PtrV)) {
2083       if (Arg->hasSwiftErrorAttr())
2084         return false;
2085     }
2086
2087     if (const AllocaInst *Alloca = dyn_cast<AllocaInst>(PtrV)) {
2088       if (Alloca->isSwiftError())
2089         return false;
2090     }
2091   }
2092
2093   // Get the value to be stored into a register. Use the zero register directly
2094   // when possible to avoid an unnecessary copy and a wasted register.
2095   unsigned SrcReg = 0;
2096   if (const auto *CI = dyn_cast<ConstantInt>(Op0)) {
2097     if (CI->isZero())
2098       SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
2099   } else if (const auto *CF = dyn_cast<ConstantFP>(Op0)) {
2100     if (CF->isZero() && !CF->isNegative()) {
2101       VT = MVT::getIntegerVT(VT.getSizeInBits());
2102       SrcReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
2103     }
2104   }
2105
2106   if (!SrcReg)
2107     SrcReg = getRegForValue(Op0);
2108
2109   if (!SrcReg)
2110     return false;
2111
2112   // See if we can handle this address.
2113   Address Addr;
2114   if (!computeAddress(I->getOperand(1), Addr, I->getOperand(0)->getType()))
2115     return false;
2116
2117   if (!emitStore(VT, SrcReg, Addr, createMachineMemOperandFor(I)))
2118     return false;
2119   return true;
2120 }
2121
2122 static AArch64CC::CondCode getCompareCC(CmpInst::Predicate Pred) {
2123   switch (Pred) {
2124   case CmpInst::FCMP_ONE:
2125   case CmpInst::FCMP_UEQ:
2126   default:
2127     // AL is our "false" for now. The other two need more compares.
2128     return AArch64CC::AL;
2129   case CmpInst::ICMP_EQ:
2130   case CmpInst::FCMP_OEQ:
2131     return AArch64CC::EQ;
2132   case CmpInst::ICMP_SGT:
2133   case CmpInst::FCMP_OGT:
2134     return AArch64CC::GT;
2135   case CmpInst::ICMP_SGE:
2136   case CmpInst::FCMP_OGE:
2137     return AArch64CC::GE;
2138   case CmpInst::ICMP_UGT:
2139   case CmpInst::FCMP_UGT:
2140     return AArch64CC::HI;
2141   case CmpInst::FCMP_OLT:
2142     return AArch64CC::MI;
2143   case CmpInst::ICMP_ULE:
2144   case CmpInst::FCMP_OLE:
2145     return AArch64CC::LS;
2146   case CmpInst::FCMP_ORD:
2147     return AArch64CC::VC;
2148   case CmpInst::FCMP_UNO:
2149     return AArch64CC::VS;
2150   case CmpInst::FCMP_UGE:
2151     return AArch64CC::PL;
2152   case CmpInst::ICMP_SLT:
2153   case CmpInst::FCMP_ULT:
2154     return AArch64CC::LT;
2155   case CmpInst::ICMP_SLE:
2156   case CmpInst::FCMP_ULE:
2157     return AArch64CC::LE;
2158   case CmpInst::FCMP_UNE:
2159   case CmpInst::ICMP_NE:
2160     return AArch64CC::NE;
2161   case CmpInst::ICMP_UGE:
2162     return AArch64CC::HS;
2163   case CmpInst::ICMP_ULT:
2164     return AArch64CC::LO;
2165   }
2166 }
2167
2168 /// \brief Try to emit a combined compare-and-branch instruction.
2169 bool AArch64FastISel::emitCompareAndBranch(const BranchInst *BI) {
2170   assert(isa<CmpInst>(BI->getCondition()) && "Expected cmp instruction");
2171   const CmpInst *CI = cast<CmpInst>(BI->getCondition());
2172   CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2173
2174   const Value *LHS = CI->getOperand(0);
2175   const Value *RHS = CI->getOperand(1);
2176
2177   MVT VT;
2178   if (!isTypeSupported(LHS->getType(), VT))
2179     return false;
2180
2181   unsigned BW = VT.getSizeInBits();
2182   if (BW > 64)
2183     return false;
2184
2185   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2186   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2187
2188   // Try to take advantage of fallthrough opportunities.
2189   if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2190     std::swap(TBB, FBB);
2191     Predicate = CmpInst::getInversePredicate(Predicate);
2192   }
2193
2194   int TestBit = -1;
2195   bool IsCmpNE;
2196   switch (Predicate) {
2197   default:
2198     return false;
2199   case CmpInst::ICMP_EQ:
2200   case CmpInst::ICMP_NE:
2201     if (isa<Constant>(LHS) && cast<Constant>(LHS)->isNullValue())
2202       std::swap(LHS, RHS);
2203
2204     if (!isa<Constant>(RHS) || !cast<Constant>(RHS)->isNullValue())
2205       return false;
2206
2207     if (const auto *AI = dyn_cast<BinaryOperator>(LHS))
2208       if (AI->getOpcode() == Instruction::And && isValueAvailable(AI)) {
2209         const Value *AndLHS = AI->getOperand(0);
2210         const Value *AndRHS = AI->getOperand(1);
2211
2212         if (const auto *C = dyn_cast<ConstantInt>(AndLHS))
2213           if (C->getValue().isPowerOf2())
2214             std::swap(AndLHS, AndRHS);
2215
2216         if (const auto *C = dyn_cast<ConstantInt>(AndRHS))
2217           if (C->getValue().isPowerOf2()) {
2218             TestBit = C->getValue().logBase2();
2219             LHS = AndLHS;
2220           }
2221       }
2222
2223     if (VT == MVT::i1)
2224       TestBit = 0;
2225
2226     IsCmpNE = Predicate == CmpInst::ICMP_NE;
2227     break;
2228   case CmpInst::ICMP_SLT:
2229   case CmpInst::ICMP_SGE:
2230     if (!isa<Constant>(RHS) || !cast<Constant>(RHS)->isNullValue())
2231       return false;
2232
2233     TestBit = BW - 1;
2234     IsCmpNE = Predicate == CmpInst::ICMP_SLT;
2235     break;
2236   case CmpInst::ICMP_SGT:
2237   case CmpInst::ICMP_SLE:
2238     if (!isa<ConstantInt>(RHS))
2239       return false;
2240
2241     if (cast<ConstantInt>(RHS)->getValue() != APInt(BW, -1, true))
2242       return false;
2243
2244     TestBit = BW - 1;
2245     IsCmpNE = Predicate == CmpInst::ICMP_SLE;
2246     break;
2247   } // end switch
2248
2249   static const unsigned OpcTable[2][2][2] = {
2250     { {AArch64::CBZW,  AArch64::CBZX },
2251       {AArch64::CBNZW, AArch64::CBNZX} },
2252     { {AArch64::TBZW,  AArch64::TBZX },
2253       {AArch64::TBNZW, AArch64::TBNZX} }
2254   };
2255
2256   bool IsBitTest = TestBit != -1;
2257   bool Is64Bit = BW == 64;
2258   if (TestBit < 32 && TestBit >= 0)
2259     Is64Bit = false;
2260
2261   unsigned Opc = OpcTable[IsBitTest][IsCmpNE][Is64Bit];
2262   const MCInstrDesc &II = TII.get(Opc);
2263
2264   unsigned SrcReg = getRegForValue(LHS);
2265   if (!SrcReg)
2266     return false;
2267   bool SrcIsKill = hasTrivialKill(LHS);
2268
2269   if (BW == 64 && !Is64Bit)
2270     SrcReg = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
2271                                         AArch64::sub_32);
2272
2273   if ((BW < 32) && !IsBitTest)
2274     SrcReg = emitIntExt(VT, SrcReg, MVT::i32, /*IsZExt=*/true);
2275
2276   // Emit the combined compare and branch instruction.
2277   SrcReg = constrainOperandRegClass(II, SrcReg,  II.getNumDefs());
2278   MachineInstrBuilder MIB =
2279       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc))
2280           .addReg(SrcReg, getKillRegState(SrcIsKill));
2281   if (IsBitTest)
2282     MIB.addImm(TestBit);
2283   MIB.addMBB(TBB);
2284
2285   finishCondBranch(BI->getParent(), TBB, FBB);
2286   return true;
2287 }
2288
2289 bool AArch64FastISel::selectBranch(const Instruction *I) {
2290   const BranchInst *BI = cast<BranchInst>(I);
2291   if (BI->isUnconditional()) {
2292     MachineBasicBlock *MSucc = FuncInfo.MBBMap[BI->getSuccessor(0)];
2293     fastEmitBranch(MSucc, BI->getDebugLoc());
2294     return true;
2295   }
2296
2297   MachineBasicBlock *TBB = FuncInfo.MBBMap[BI->getSuccessor(0)];
2298   MachineBasicBlock *FBB = FuncInfo.MBBMap[BI->getSuccessor(1)];
2299
2300   if (const CmpInst *CI = dyn_cast<CmpInst>(BI->getCondition())) {
2301     if (CI->hasOneUse() && isValueAvailable(CI)) {
2302       // Try to optimize or fold the cmp.
2303       CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2304       switch (Predicate) {
2305       default:
2306         break;
2307       case CmpInst::FCMP_FALSE:
2308         fastEmitBranch(FBB, DbgLoc);
2309         return true;
2310       case CmpInst::FCMP_TRUE:
2311         fastEmitBranch(TBB, DbgLoc);
2312         return true;
2313       }
2314
2315       // Try to emit a combined compare-and-branch first.
2316       if (emitCompareAndBranch(BI))
2317         return true;
2318
2319       // Try to take advantage of fallthrough opportunities.
2320       if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2321         std::swap(TBB, FBB);
2322         Predicate = CmpInst::getInversePredicate(Predicate);
2323       }
2324
2325       // Emit the cmp.
2326       if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2327         return false;
2328
2329       // FCMP_UEQ and FCMP_ONE cannot be checked with a single branch
2330       // instruction.
2331       AArch64CC::CondCode CC = getCompareCC(Predicate);
2332       AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2333       switch (Predicate) {
2334       default:
2335         break;
2336       case CmpInst::FCMP_UEQ:
2337         ExtraCC = AArch64CC::EQ;
2338         CC = AArch64CC::VS;
2339         break;
2340       case CmpInst::FCMP_ONE:
2341         ExtraCC = AArch64CC::MI;
2342         CC = AArch64CC::GT;
2343         break;
2344       }
2345       assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2346
2347       // Emit the extra branch for FCMP_UEQ and FCMP_ONE.
2348       if (ExtraCC != AArch64CC::AL) {
2349         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2350             .addImm(ExtraCC)
2351             .addMBB(TBB);
2352       }
2353
2354       // Emit the branch.
2355       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2356           .addImm(CC)
2357           .addMBB(TBB);
2358
2359       finishCondBranch(BI->getParent(), TBB, FBB);
2360       return true;
2361     }
2362   } else if (const auto *CI = dyn_cast<ConstantInt>(BI->getCondition())) {
2363     uint64_t Imm = CI->getZExtValue();
2364     MachineBasicBlock *Target = (Imm == 0) ? FBB : TBB;
2365     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::B))
2366         .addMBB(Target);
2367
2368     // Obtain the branch probability and add the target to the successor list.
2369     if (FuncInfo.BPI) {
2370       auto BranchProbability = FuncInfo.BPI->getEdgeProbability(
2371           BI->getParent(), Target->getBasicBlock());
2372       FuncInfo.MBB->addSuccessor(Target, BranchProbability);
2373     } else
2374       FuncInfo.MBB->addSuccessorWithoutProb(Target);
2375     return true;
2376   } else {
2377     AArch64CC::CondCode CC = AArch64CC::NE;
2378     if (foldXALUIntrinsic(CC, I, BI->getCondition())) {
2379       // Fake request the condition, otherwise the intrinsic might be completely
2380       // optimized away.
2381       unsigned CondReg = getRegForValue(BI->getCondition());
2382       if (!CondReg)
2383         return false;
2384
2385       // Emit the branch.
2386       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::Bcc))
2387         .addImm(CC)
2388         .addMBB(TBB);
2389
2390       finishCondBranch(BI->getParent(), TBB, FBB);
2391       return true;
2392     }
2393   }
2394
2395   unsigned CondReg = getRegForValue(BI->getCondition());
2396   if (CondReg == 0)
2397     return false;
2398   bool CondRegIsKill = hasTrivialKill(BI->getCondition());
2399
2400   // i1 conditions come as i32 values, test the lowest bit with tb(n)z.
2401   unsigned Opcode = AArch64::TBNZW;
2402   if (FuncInfo.MBB->isLayoutSuccessor(TBB)) {
2403     std::swap(TBB, FBB);
2404     Opcode = AArch64::TBZW;
2405   }
2406
2407   const MCInstrDesc &II = TII.get(Opcode);
2408   unsigned ConstrainedCondReg
2409     = constrainOperandRegClass(II, CondReg, II.getNumDefs());
2410   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II)
2411       .addReg(ConstrainedCondReg, getKillRegState(CondRegIsKill))
2412       .addImm(0)
2413       .addMBB(TBB);
2414
2415   finishCondBranch(BI->getParent(), TBB, FBB);
2416   return true;
2417 }
2418
2419 bool AArch64FastISel::selectIndirectBr(const Instruction *I) {
2420   const IndirectBrInst *BI = cast<IndirectBrInst>(I);
2421   unsigned AddrReg = getRegForValue(BI->getOperand(0));
2422   if (AddrReg == 0)
2423     return false;
2424
2425   // Emit the indirect branch.
2426   const MCInstrDesc &II = TII.get(AArch64::BR);
2427   AddrReg = constrainOperandRegClass(II, AddrReg,  II.getNumDefs());
2428   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(AddrReg);
2429
2430   // Make sure the CFG is up-to-date.
2431   for (auto *Succ : BI->successors())
2432     FuncInfo.MBB->addSuccessor(FuncInfo.MBBMap[Succ]);
2433
2434   return true;
2435 }
2436
2437 bool AArch64FastISel::selectCmp(const Instruction *I) {
2438   const CmpInst *CI = cast<CmpInst>(I);
2439
2440   // Vectors of i1 are weird: bail out.
2441   if (CI->getType()->isVectorTy())
2442     return false;
2443
2444   // Try to optimize or fold the cmp.
2445   CmpInst::Predicate Predicate = optimizeCmpPredicate(CI);
2446   unsigned ResultReg = 0;
2447   switch (Predicate) {
2448   default:
2449     break;
2450   case CmpInst::FCMP_FALSE:
2451     ResultReg = createResultReg(&AArch64::GPR32RegClass);
2452     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2453             TII.get(TargetOpcode::COPY), ResultReg)
2454         .addReg(AArch64::WZR, getKillRegState(true));
2455     break;
2456   case CmpInst::FCMP_TRUE:
2457     ResultReg = fastEmit_i(MVT::i32, MVT::i32, ISD::Constant, 1);
2458     break;
2459   }
2460
2461   if (ResultReg) {
2462     updateValueMap(I, ResultReg);
2463     return true;
2464   }
2465
2466   // Emit the cmp.
2467   if (!emitCmp(CI->getOperand(0), CI->getOperand(1), CI->isUnsigned()))
2468     return false;
2469
2470   ResultReg = createResultReg(&AArch64::GPR32RegClass);
2471
2472   // FCMP_UEQ and FCMP_ONE cannot be checked with a single instruction. These
2473   // condition codes are inverted, because they are used by CSINC.
2474   static unsigned CondCodeTable[2][2] = {
2475     { AArch64CC::NE, AArch64CC::VC },
2476     { AArch64CC::PL, AArch64CC::LE }
2477   };
2478   unsigned *CondCodes = nullptr;
2479   switch (Predicate) {
2480   default:
2481     break;
2482   case CmpInst::FCMP_UEQ:
2483     CondCodes = &CondCodeTable[0][0];
2484     break;
2485   case CmpInst::FCMP_ONE:
2486     CondCodes = &CondCodeTable[1][0];
2487     break;
2488   }
2489
2490   if (CondCodes) {
2491     unsigned TmpReg1 = createResultReg(&AArch64::GPR32RegClass);
2492     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2493             TmpReg1)
2494         .addReg(AArch64::WZR, getKillRegState(true))
2495         .addReg(AArch64::WZR, getKillRegState(true))
2496         .addImm(CondCodes[0]);
2497     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2498             ResultReg)
2499         .addReg(TmpReg1, getKillRegState(true))
2500         .addReg(AArch64::WZR, getKillRegState(true))
2501         .addImm(CondCodes[1]);
2502
2503     updateValueMap(I, ResultReg);
2504     return true;
2505   }
2506
2507   // Now set a register based on the comparison.
2508   AArch64CC::CondCode CC = getCompareCC(Predicate);
2509   assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2510   AArch64CC::CondCode invertedCC = getInvertedCondCode(CC);
2511   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::CSINCWr),
2512           ResultReg)
2513       .addReg(AArch64::WZR, getKillRegState(true))
2514       .addReg(AArch64::WZR, getKillRegState(true))
2515       .addImm(invertedCC);
2516
2517   updateValueMap(I, ResultReg);
2518   return true;
2519 }
2520
2521 /// \brief Optimize selects of i1 if one of the operands has a 'true' or 'false'
2522 /// value.
2523 bool AArch64FastISel::optimizeSelect(const SelectInst *SI) {
2524   if (!SI->getType()->isIntegerTy(1))
2525     return false;
2526
2527   const Value *Src1Val, *Src2Val;
2528   unsigned Opc = 0;
2529   bool NeedExtraOp = false;
2530   if (auto *CI = dyn_cast<ConstantInt>(SI->getTrueValue())) {
2531     if (CI->isOne()) {
2532       Src1Val = SI->getCondition();
2533       Src2Val = SI->getFalseValue();
2534       Opc = AArch64::ORRWrr;
2535     } else {
2536       assert(CI->isZero());
2537       Src1Val = SI->getFalseValue();
2538       Src2Val = SI->getCondition();
2539       Opc = AArch64::BICWrr;
2540     }
2541   } else if (auto *CI = dyn_cast<ConstantInt>(SI->getFalseValue())) {
2542     if (CI->isOne()) {
2543       Src1Val = SI->getCondition();
2544       Src2Val = SI->getTrueValue();
2545       Opc = AArch64::ORRWrr;
2546       NeedExtraOp = true;
2547     } else {
2548       assert(CI->isZero());
2549       Src1Val = SI->getCondition();
2550       Src2Val = SI->getTrueValue();
2551       Opc = AArch64::ANDWrr;
2552     }
2553   }
2554
2555   if (!Opc)
2556     return false;
2557
2558   unsigned Src1Reg = getRegForValue(Src1Val);
2559   if (!Src1Reg)
2560     return false;
2561   bool Src1IsKill = hasTrivialKill(Src1Val);
2562
2563   unsigned Src2Reg = getRegForValue(Src2Val);
2564   if (!Src2Reg)
2565     return false;
2566   bool Src2IsKill = hasTrivialKill(Src2Val);
2567
2568   if (NeedExtraOp) {
2569     Src1Reg = emitLogicalOp_ri(ISD::XOR, MVT::i32, Src1Reg, Src1IsKill, 1);
2570     Src1IsKill = true;
2571   }
2572   unsigned ResultReg = fastEmitInst_rr(Opc, &AArch64::GPR32RegClass, Src1Reg,
2573                                        Src1IsKill, Src2Reg, Src2IsKill);
2574   updateValueMap(SI, ResultReg);
2575   return true;
2576 }
2577
2578 bool AArch64FastISel::selectSelect(const Instruction *I) {
2579   assert(isa<SelectInst>(I) && "Expected a select instruction.");
2580   MVT VT;
2581   if (!isTypeSupported(I->getType(), VT))
2582     return false;
2583
2584   unsigned Opc;
2585   const TargetRegisterClass *RC;
2586   switch (VT.SimpleTy) {
2587   default:
2588     return false;
2589   case MVT::i1:
2590   case MVT::i8:
2591   case MVT::i16:
2592   case MVT::i32:
2593     Opc = AArch64::CSELWr;
2594     RC = &AArch64::GPR32RegClass;
2595     break;
2596   case MVT::i64:
2597     Opc = AArch64::CSELXr;
2598     RC = &AArch64::GPR64RegClass;
2599     break;
2600   case MVT::f32:
2601     Opc = AArch64::FCSELSrrr;
2602     RC = &AArch64::FPR32RegClass;
2603     break;
2604   case MVT::f64:
2605     Opc = AArch64::FCSELDrrr;
2606     RC = &AArch64::FPR64RegClass;
2607     break;
2608   }
2609
2610   const SelectInst *SI = cast<SelectInst>(I);
2611   const Value *Cond = SI->getCondition();
2612   AArch64CC::CondCode CC = AArch64CC::NE;
2613   AArch64CC::CondCode ExtraCC = AArch64CC::AL;
2614
2615   if (optimizeSelect(SI))
2616     return true;
2617
2618   // Try to pickup the flags, so we don't have to emit another compare.
2619   if (foldXALUIntrinsic(CC, I, Cond)) {
2620     // Fake request the condition to force emission of the XALU intrinsic.
2621     unsigned CondReg = getRegForValue(Cond);
2622     if (!CondReg)
2623       return false;
2624   } else if (isa<CmpInst>(Cond) && cast<CmpInst>(Cond)->hasOneUse() &&
2625              isValueAvailable(Cond)) {
2626     const auto *Cmp = cast<CmpInst>(Cond);
2627     // Try to optimize or fold the cmp.
2628     CmpInst::Predicate Predicate = optimizeCmpPredicate(Cmp);
2629     const Value *FoldSelect = nullptr;
2630     switch (Predicate) {
2631     default:
2632       break;
2633     case CmpInst::FCMP_FALSE:
2634       FoldSelect = SI->getFalseValue();
2635       break;
2636     case CmpInst::FCMP_TRUE:
2637       FoldSelect = SI->getTrueValue();
2638       break;
2639     }
2640
2641     if (FoldSelect) {
2642       unsigned SrcReg = getRegForValue(FoldSelect);
2643       if (!SrcReg)
2644         return false;
2645       unsigned UseReg = lookUpRegForValue(SI);
2646       if (UseReg)
2647         MRI.clearKillFlags(UseReg);
2648
2649       updateValueMap(I, SrcReg);
2650       return true;
2651     }
2652
2653     // Emit the cmp.
2654     if (!emitCmp(Cmp->getOperand(0), Cmp->getOperand(1), Cmp->isUnsigned()))
2655       return false;
2656
2657     // FCMP_UEQ and FCMP_ONE cannot be checked with a single select instruction.
2658     CC = getCompareCC(Predicate);
2659     switch (Predicate) {
2660     default:
2661       break;
2662     case CmpInst::FCMP_UEQ:
2663       ExtraCC = AArch64CC::EQ;
2664       CC = AArch64CC::VS;
2665       break;
2666     case CmpInst::FCMP_ONE:
2667       ExtraCC = AArch64CC::MI;
2668       CC = AArch64CC::GT;
2669       break;
2670     }
2671     assert((CC != AArch64CC::AL) && "Unexpected condition code.");
2672   } else {
2673     unsigned CondReg = getRegForValue(Cond);
2674     if (!CondReg)
2675       return false;
2676     bool CondIsKill = hasTrivialKill(Cond);
2677
2678     const MCInstrDesc &II = TII.get(AArch64::ANDSWri);
2679     CondReg = constrainOperandRegClass(II, CondReg, 1);
2680
2681     // Emit a TST instruction (ANDS wzr, reg, #imm).
2682     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II,
2683             AArch64::WZR)
2684         .addReg(CondReg, getKillRegState(CondIsKill))
2685         .addImm(AArch64_AM::encodeLogicalImmediate(1, 32));
2686   }
2687
2688   unsigned Src1Reg = getRegForValue(SI->getTrueValue());
2689   bool Src1IsKill = hasTrivialKill(SI->getTrueValue());
2690
2691   unsigned Src2Reg = getRegForValue(SI->getFalseValue());
2692   bool Src2IsKill = hasTrivialKill(SI->getFalseValue());
2693
2694   if (!Src1Reg || !Src2Reg)
2695     return false;
2696
2697   if (ExtraCC != AArch64CC::AL) {
2698     Src2Reg = fastEmitInst_rri(Opc, RC, Src1Reg, Src1IsKill, Src2Reg,
2699                                Src2IsKill, ExtraCC);
2700     Src2IsKill = true;
2701   }
2702   unsigned ResultReg = fastEmitInst_rri(Opc, RC, Src1Reg, Src1IsKill, Src2Reg,
2703                                         Src2IsKill, CC);
2704   updateValueMap(I, ResultReg);
2705   return true;
2706 }
2707
2708 bool AArch64FastISel::selectFPExt(const Instruction *I) {
2709   Value *V = I->getOperand(0);
2710   if (!I->getType()->isDoubleTy() || !V->getType()->isFloatTy())
2711     return false;
2712
2713   unsigned Op = getRegForValue(V);
2714   if (Op == 0)
2715     return false;
2716
2717   unsigned ResultReg = createResultReg(&AArch64::FPR64RegClass);
2718   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTDSr),
2719           ResultReg).addReg(Op);
2720   updateValueMap(I, ResultReg);
2721   return true;
2722 }
2723
2724 bool AArch64FastISel::selectFPTrunc(const Instruction *I) {
2725   Value *V = I->getOperand(0);
2726   if (!I->getType()->isFloatTy() || !V->getType()->isDoubleTy())
2727     return false;
2728
2729   unsigned Op = getRegForValue(V);
2730   if (Op == 0)
2731     return false;
2732
2733   unsigned ResultReg = createResultReg(&AArch64::FPR32RegClass);
2734   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::FCVTSDr),
2735           ResultReg).addReg(Op);
2736   updateValueMap(I, ResultReg);
2737   return true;
2738 }
2739
2740 // FPToUI and FPToSI
2741 bool AArch64FastISel::selectFPToInt(const Instruction *I, bool Signed) {
2742   MVT DestVT;
2743   if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2744     return false;
2745
2746   unsigned SrcReg = getRegForValue(I->getOperand(0));
2747   if (SrcReg == 0)
2748     return false;
2749
2750   EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType(), true);
2751   if (SrcVT == MVT::f128)
2752     return false;
2753
2754   unsigned Opc;
2755   if (SrcVT == MVT::f64) {
2756     if (Signed)
2757       Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWDr : AArch64::FCVTZSUXDr;
2758     else
2759       Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWDr : AArch64::FCVTZUUXDr;
2760   } else {
2761     if (Signed)
2762       Opc = (DestVT == MVT::i32) ? AArch64::FCVTZSUWSr : AArch64::FCVTZSUXSr;
2763     else
2764       Opc = (DestVT == MVT::i32) ? AArch64::FCVTZUUWSr : AArch64::FCVTZUUXSr;
2765   }
2766   unsigned ResultReg = createResultReg(
2767       DestVT == MVT::i32 ? &AArch64::GPR32RegClass : &AArch64::GPR64RegClass);
2768   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
2769       .addReg(SrcReg);
2770   updateValueMap(I, ResultReg);
2771   return true;
2772 }
2773
2774 bool AArch64FastISel::selectIntToFP(const Instruction *I, bool Signed) {
2775   MVT DestVT;
2776   if (!isTypeLegal(I->getType(), DestVT) || DestVT.isVector())
2777     return false;
2778   assert ((DestVT == MVT::f32 || DestVT == MVT::f64) &&
2779           "Unexpected value type.");
2780
2781   unsigned SrcReg = getRegForValue(I->getOperand(0));
2782   if (!SrcReg)
2783     return false;
2784   bool SrcIsKill = hasTrivialKill(I->getOperand(0));
2785
2786   EVT SrcVT = TLI.getValueType(DL, I->getOperand(0)->getType(), true);
2787
2788   // Handle sign-extension.
2789   if (SrcVT == MVT::i16 || SrcVT == MVT::i8 || SrcVT == MVT::i1) {
2790     SrcReg =
2791         emitIntExt(SrcVT.getSimpleVT(), SrcReg, MVT::i32, /*isZExt*/ !Signed);
2792     if (!SrcReg)
2793       return false;
2794     SrcIsKill = true;
2795   }
2796
2797   unsigned Opc;
2798   if (SrcVT == MVT::i64) {
2799     if (Signed)
2800       Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUXSri : AArch64::SCVTFUXDri;
2801     else
2802       Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUXSri : AArch64::UCVTFUXDri;
2803   } else {
2804     if (Signed)
2805       Opc = (DestVT == MVT::f32) ? AArch64::SCVTFUWSri : AArch64::SCVTFUWDri;
2806     else
2807       Opc = (DestVT == MVT::f32) ? AArch64::UCVTFUWSri : AArch64::UCVTFUWDri;
2808   }
2809
2810   unsigned ResultReg = fastEmitInst_r(Opc, TLI.getRegClassFor(DestVT), SrcReg,
2811                                       SrcIsKill);
2812   updateValueMap(I, ResultReg);
2813   return true;
2814 }
2815
2816 bool AArch64FastISel::fastLowerArguments() {
2817   if (!FuncInfo.CanLowerReturn)
2818     return false;
2819
2820   const Function *F = FuncInfo.Fn;
2821   if (F->isVarArg())
2822     return false;
2823
2824   CallingConv::ID CC = F->getCallingConv();
2825   if (CC != CallingConv::C)
2826     return false;
2827
2828   // Only handle simple cases of up to 8 GPR and FPR each.
2829   unsigned GPRCnt = 0;
2830   unsigned FPRCnt = 0;
2831   unsigned Idx = 0;
2832   for (auto const &Arg : F->args()) {
2833     // The first argument is at index 1.
2834     ++Idx;
2835     if (F->getAttributes().hasAttribute(Idx, Attribute::ByVal) ||
2836         F->getAttributes().hasAttribute(Idx, Attribute::InReg) ||
2837         F->getAttributes().hasAttribute(Idx, Attribute::StructRet) ||
2838         F->getAttributes().hasAttribute(Idx, Attribute::SwiftSelf) ||
2839         F->getAttributes().hasAttribute(Idx, Attribute::SwiftError) ||
2840         F->getAttributes().hasAttribute(Idx, Attribute::Nest))
2841       return false;
2842
2843     Type *ArgTy = Arg.getType();
2844     if (ArgTy->isStructTy() || ArgTy->isArrayTy())
2845       return false;
2846
2847     EVT ArgVT = TLI.getValueType(DL, ArgTy);
2848     if (!ArgVT.isSimple())
2849       return false;
2850
2851     MVT VT = ArgVT.getSimpleVT().SimpleTy;
2852     if (VT.isFloatingPoint() && !Subtarget->hasFPARMv8())
2853       return false;
2854
2855     if (VT.isVector() &&
2856         (!Subtarget->hasNEON() || !Subtarget->isLittleEndian()))
2857       return false;
2858
2859     if (VT >= MVT::i1 && VT <= MVT::i64)
2860       ++GPRCnt;
2861     else if ((VT >= MVT::f16 && VT <= MVT::f64) || VT.is64BitVector() ||
2862              VT.is128BitVector())
2863       ++FPRCnt;
2864     else
2865       return false;
2866
2867     if (GPRCnt > 8 || FPRCnt > 8)
2868       return false;
2869   }
2870
2871   static const MCPhysReg Registers[6][8] = {
2872     { AArch64::W0, AArch64::W1, AArch64::W2, AArch64::W3, AArch64::W4,
2873       AArch64::W5, AArch64::W6, AArch64::W7 },
2874     { AArch64::X0, AArch64::X1, AArch64::X2, AArch64::X3, AArch64::X4,
2875       AArch64::X5, AArch64::X6, AArch64::X7 },
2876     { AArch64::H0, AArch64::H1, AArch64::H2, AArch64::H3, AArch64::H4,
2877       AArch64::H5, AArch64::H6, AArch64::H7 },
2878     { AArch64::S0, AArch64::S1, AArch64::S2, AArch64::S3, AArch64::S4,
2879       AArch64::S5, AArch64::S6, AArch64::S7 },
2880     { AArch64::D0, AArch64::D1, AArch64::D2, AArch64::D3, AArch64::D4,
2881       AArch64::D5, AArch64::D6, AArch64::D7 },
2882     { AArch64::Q0, AArch64::Q1, AArch64::Q2, AArch64::Q3, AArch64::Q4,
2883       AArch64::Q5, AArch64::Q6, AArch64::Q7 }
2884   };
2885
2886   unsigned GPRIdx = 0;
2887   unsigned FPRIdx = 0;
2888   for (auto const &Arg : F->args()) {
2889     MVT VT = TLI.getSimpleValueType(DL, Arg.getType());
2890     unsigned SrcReg;
2891     const TargetRegisterClass *RC;
2892     if (VT >= MVT::i1 && VT <= MVT::i32) {
2893       SrcReg = Registers[0][GPRIdx++];
2894       RC = &AArch64::GPR32RegClass;
2895       VT = MVT::i32;
2896     } else if (VT == MVT::i64) {
2897       SrcReg = Registers[1][GPRIdx++];
2898       RC = &AArch64::GPR64RegClass;
2899     } else if (VT == MVT::f16) {
2900       SrcReg = Registers[2][FPRIdx++];
2901       RC = &AArch64::FPR16RegClass;
2902     } else if (VT ==  MVT::f32) {
2903       SrcReg = Registers[3][FPRIdx++];
2904       RC = &AArch64::FPR32RegClass;
2905     } else if ((VT == MVT::f64) || VT.is64BitVector()) {
2906       SrcReg = Registers[4][FPRIdx++];
2907       RC = &AArch64::FPR64RegClass;
2908     } else if (VT.is128BitVector()) {
2909       SrcReg = Registers[5][FPRIdx++];
2910       RC = &AArch64::FPR128RegClass;
2911     } else
2912       llvm_unreachable("Unexpected value type.");
2913
2914     unsigned DstReg = FuncInfo.MF->addLiveIn(SrcReg, RC);
2915     // FIXME: Unfortunately it's necessary to emit a copy from the livein copy.
2916     // Without this, EmitLiveInCopies may eliminate the livein if its only
2917     // use is a bitcast (which isn't turned into an instruction).
2918     unsigned ResultReg = createResultReg(RC);
2919     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2920             TII.get(TargetOpcode::COPY), ResultReg)
2921         .addReg(DstReg, getKillRegState(true));
2922     updateValueMap(&Arg, ResultReg);
2923   }
2924   return true;
2925 }
2926
2927 bool AArch64FastISel::processCallArgs(CallLoweringInfo &CLI,
2928                                       SmallVectorImpl<MVT> &OutVTs,
2929                                       unsigned &NumBytes) {
2930   CallingConv::ID CC = CLI.CallConv;
2931   SmallVector<CCValAssign, 16> ArgLocs;
2932   CCState CCInfo(CC, false, *FuncInfo.MF, ArgLocs, *Context);
2933   CCInfo.AnalyzeCallOperands(OutVTs, CLI.OutFlags, CCAssignFnForCall(CC));
2934
2935   // Get a count of how many bytes are to be pushed on the stack.
2936   NumBytes = CCInfo.getNextStackOffset();
2937
2938   // Issue CALLSEQ_START
2939   unsigned AdjStackDown = TII.getCallFrameSetupOpcode();
2940   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackDown))
2941     .addImm(NumBytes);
2942
2943   // Process the args.
2944   for (CCValAssign &VA : ArgLocs) {
2945     const Value *ArgVal = CLI.OutVals[VA.getValNo()];
2946     MVT ArgVT = OutVTs[VA.getValNo()];
2947
2948     unsigned ArgReg = getRegForValue(ArgVal);
2949     if (!ArgReg)
2950       return false;
2951
2952     // Handle arg promotion: SExt, ZExt, AExt.
2953     switch (VA.getLocInfo()) {
2954     case CCValAssign::Full:
2955       break;
2956     case CCValAssign::SExt: {
2957       MVT DestVT = VA.getLocVT();
2958       MVT SrcVT = ArgVT;
2959       ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/false);
2960       if (!ArgReg)
2961         return false;
2962       break;
2963     }
2964     case CCValAssign::AExt:
2965     // Intentional fall-through.
2966     case CCValAssign::ZExt: {
2967       MVT DestVT = VA.getLocVT();
2968       MVT SrcVT = ArgVT;
2969       ArgReg = emitIntExt(SrcVT, ArgReg, DestVT, /*isZExt=*/true);
2970       if (!ArgReg)
2971         return false;
2972       break;
2973     }
2974     default:
2975       llvm_unreachable("Unknown arg promotion!");
2976     }
2977
2978     // Now copy/store arg to correct locations.
2979     if (VA.isRegLoc() && !VA.needsCustom()) {
2980       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
2981               TII.get(TargetOpcode::COPY), VA.getLocReg()).addReg(ArgReg);
2982       CLI.OutRegs.push_back(VA.getLocReg());
2983     } else if (VA.needsCustom()) {
2984       // FIXME: Handle custom args.
2985       return false;
2986     } else {
2987       assert(VA.isMemLoc() && "Assuming store on stack.");
2988
2989       // Don't emit stores for undef values.
2990       if (isa<UndefValue>(ArgVal))
2991         continue;
2992
2993       // Need to store on the stack.
2994       unsigned ArgSize = (ArgVT.getSizeInBits() + 7) / 8;
2995
2996       unsigned BEAlign = 0;
2997       if (ArgSize < 8 && !Subtarget->isLittleEndian())
2998         BEAlign = 8 - ArgSize;
2999
3000       Address Addr;
3001       Addr.setKind(Address::RegBase);
3002       Addr.setReg(AArch64::SP);
3003       Addr.setOffset(VA.getLocMemOffset() + BEAlign);
3004
3005       unsigned Alignment = DL.getABITypeAlignment(ArgVal->getType());
3006       MachineMemOperand *MMO = FuncInfo.MF->getMachineMemOperand(
3007           MachinePointerInfo::getStack(*FuncInfo.MF, Addr.getOffset()),
3008           MachineMemOperand::MOStore, ArgVT.getStoreSize(), Alignment);
3009
3010       if (!emitStore(ArgVT, ArgReg, Addr, MMO))
3011         return false;
3012     }
3013   }
3014   return true;
3015 }
3016
3017 bool AArch64FastISel::finishCall(CallLoweringInfo &CLI, MVT RetVT,
3018                                  unsigned NumBytes) {
3019   CallingConv::ID CC = CLI.CallConv;
3020
3021   // Issue CALLSEQ_END
3022   unsigned AdjStackUp = TII.getCallFrameDestroyOpcode();
3023   BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AdjStackUp))
3024     .addImm(NumBytes).addImm(0);
3025
3026   // Now the return value.
3027   if (RetVT != MVT::isVoid) {
3028     SmallVector<CCValAssign, 16> RVLocs;
3029     CCState CCInfo(CC, false, *FuncInfo.MF, RVLocs, *Context);
3030     CCInfo.AnalyzeCallResult(RetVT, CCAssignFnForCall(CC));
3031
3032     // Only handle a single return value.
3033     if (RVLocs.size() != 1)
3034       return false;
3035
3036     // Copy all of the result registers out of their specified physreg.
3037     MVT CopyVT = RVLocs[0].getValVT();
3038
3039     // TODO: Handle big-endian results
3040     if (CopyVT.isVector() && !Subtarget->isLittleEndian())
3041       return false;
3042
3043     unsigned ResultReg = createResultReg(TLI.getRegClassFor(CopyVT));
3044     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3045             TII.get(TargetOpcode::COPY), ResultReg)
3046         .addReg(RVLocs[0].getLocReg());
3047     CLI.InRegs.push_back(RVLocs[0].getLocReg());
3048
3049     CLI.ResultReg = ResultReg;
3050     CLI.NumResultRegs = 1;
3051   }
3052
3053   return true;
3054 }
3055
3056 bool AArch64FastISel::fastLowerCall(CallLoweringInfo &CLI) {
3057   CallingConv::ID CC  = CLI.CallConv;
3058   bool IsTailCall     = CLI.IsTailCall;
3059   bool IsVarArg       = CLI.IsVarArg;
3060   const Value *Callee = CLI.Callee;
3061   MCSymbol *Symbol = CLI.Symbol;
3062
3063   if (!Callee && !Symbol)
3064     return false;
3065
3066   // Allow SelectionDAG isel to handle tail calls.
3067   if (IsTailCall)
3068     return false;
3069
3070   CodeModel::Model CM = TM.getCodeModel();
3071   // Only support the small and large code model.
3072   if (CM != CodeModel::Small && CM != CodeModel::Large)
3073     return false;
3074
3075   // FIXME: Add large code model support for ELF.
3076   if (CM == CodeModel::Large && !Subtarget->isTargetMachO())
3077     return false;
3078
3079   // Let SDISel handle vararg functions.
3080   if (IsVarArg)
3081     return false;
3082
3083   // FIXME: Only handle *simple* calls for now.
3084   MVT RetVT;
3085   if (CLI.RetTy->isVoidTy())
3086     RetVT = MVT::isVoid;
3087   else if (!isTypeLegal(CLI.RetTy, RetVT))
3088     return false;
3089
3090   for (auto Flag : CLI.OutFlags)
3091     if (Flag.isInReg() || Flag.isSRet() || Flag.isNest() || Flag.isByVal() ||
3092         Flag.isSwiftSelf() || Flag.isSwiftError())
3093       return false;
3094
3095   // Set up the argument vectors.
3096   SmallVector<MVT, 16> OutVTs;
3097   OutVTs.reserve(CLI.OutVals.size());
3098
3099   for (auto *Val : CLI.OutVals) {
3100     MVT VT;
3101     if (!isTypeLegal(Val->getType(), VT) &&
3102         !(VT == MVT::i1 || VT == MVT::i8 || VT == MVT::i16))
3103       return false;
3104
3105     // We don't handle vector parameters yet.
3106     if (VT.isVector() || VT.getSizeInBits() > 64)
3107       return false;
3108
3109     OutVTs.push_back(VT);
3110   }
3111
3112   Address Addr;
3113   if (Callee && !computeCallAddress(Callee, Addr))
3114     return false;
3115
3116   // Handle the arguments now that we've gotten them.
3117   unsigned NumBytes;
3118   if (!processCallArgs(CLI, OutVTs, NumBytes))
3119     return false;
3120
3121   // Issue the call.
3122   MachineInstrBuilder MIB;
3123   if (CM == CodeModel::Small) {
3124     const MCInstrDesc &II = TII.get(Addr.getReg() ? AArch64::BLR : AArch64::BL);
3125     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II);
3126     if (Symbol)
3127       MIB.addSym(Symbol, 0);
3128     else if (Addr.getGlobalValue())
3129       MIB.addGlobalAddress(Addr.getGlobalValue(), 0, 0);
3130     else if (Addr.getReg()) {
3131       unsigned Reg = constrainOperandRegClass(II, Addr.getReg(), 0);
3132       MIB.addReg(Reg);
3133     } else
3134       return false;
3135   } else {
3136     unsigned CallReg = 0;
3137     if (Symbol) {
3138       unsigned ADRPReg = createResultReg(&AArch64::GPR64commonRegClass);
3139       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::ADRP),
3140               ADRPReg)
3141           .addSym(Symbol, AArch64II::MO_GOT | AArch64II::MO_PAGE);
3142
3143       CallReg = createResultReg(&AArch64::GPR64RegClass);
3144       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3145               TII.get(AArch64::LDRXui), CallReg)
3146           .addReg(ADRPReg)
3147           .addSym(Symbol,
3148                   AArch64II::MO_GOT | AArch64II::MO_PAGEOFF | AArch64II::MO_NC);
3149     } else if (Addr.getGlobalValue())
3150       CallReg = materializeGV(Addr.getGlobalValue());
3151     else if (Addr.getReg())
3152       CallReg = Addr.getReg();
3153
3154     if (!CallReg)
3155       return false;
3156
3157     const MCInstrDesc &II = TII.get(AArch64::BLR);
3158     CallReg = constrainOperandRegClass(II, CallReg, 0);
3159     MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, II).addReg(CallReg);
3160   }
3161
3162   // Add implicit physical register uses to the call.
3163   for (auto Reg : CLI.OutRegs)
3164     MIB.addReg(Reg, RegState::Implicit);
3165
3166   // Add a register mask with the call-preserved registers.
3167   // Proper defs for return values will be added by setPhysRegsDeadExcept().
3168   MIB.addRegMask(TRI.getCallPreservedMask(*FuncInfo.MF, CC));
3169
3170   CLI.Call = MIB;
3171
3172   // Finish off the call including any return values.
3173   return finishCall(CLI, RetVT, NumBytes);
3174 }
3175
3176 bool AArch64FastISel::isMemCpySmall(uint64_t Len, unsigned Alignment) {
3177   if (Alignment)
3178     return Len / Alignment <= 4;
3179   else
3180     return Len < 32;
3181 }
3182
3183 bool AArch64FastISel::tryEmitSmallMemCpy(Address Dest, Address Src,
3184                                          uint64_t Len, unsigned Alignment) {
3185   // Make sure we don't bloat code by inlining very large memcpy's.
3186   if (!isMemCpySmall(Len, Alignment))
3187     return false;
3188
3189   int64_t UnscaledOffset = 0;
3190   Address OrigDest = Dest;
3191   Address OrigSrc = Src;
3192
3193   while (Len) {
3194     MVT VT;
3195     if (!Alignment || Alignment >= 8) {
3196       if (Len >= 8)
3197         VT = MVT::i64;
3198       else if (Len >= 4)
3199         VT = MVT::i32;
3200       else if (Len >= 2)
3201         VT = MVT::i16;
3202       else {
3203         VT = MVT::i8;
3204       }
3205     } else {
3206       // Bound based on alignment.
3207       if (Len >= 4 && Alignment == 4)
3208         VT = MVT::i32;
3209       else if (Len >= 2 && Alignment == 2)
3210         VT = MVT::i16;
3211       else {
3212         VT = MVT::i8;
3213       }
3214     }
3215
3216     unsigned ResultReg = emitLoad(VT, VT, Src);
3217     if (!ResultReg)
3218       return false;
3219
3220     if (!emitStore(VT, ResultReg, Dest))
3221       return false;
3222
3223     int64_t Size = VT.getSizeInBits() / 8;
3224     Len -= Size;
3225     UnscaledOffset += Size;
3226
3227     // We need to recompute the unscaled offset for each iteration.
3228     Dest.setOffset(OrigDest.getOffset() + UnscaledOffset);
3229     Src.setOffset(OrigSrc.getOffset() + UnscaledOffset);
3230   }
3231
3232   return true;
3233 }
3234
3235 /// \brief Check if it is possible to fold the condition from the XALU intrinsic
3236 /// into the user. The condition code will only be updated on success.
3237 bool AArch64FastISel::foldXALUIntrinsic(AArch64CC::CondCode &CC,
3238                                         const Instruction *I,
3239                                         const Value *Cond) {
3240   if (!isa<ExtractValueInst>(Cond))
3241     return false;
3242
3243   const auto *EV = cast<ExtractValueInst>(Cond);
3244   if (!isa<IntrinsicInst>(EV->getAggregateOperand()))
3245     return false;
3246
3247   const auto *II = cast<IntrinsicInst>(EV->getAggregateOperand());
3248   MVT RetVT;
3249   const Function *Callee = II->getCalledFunction();
3250   Type *RetTy =
3251   cast<StructType>(Callee->getReturnType())->getTypeAtIndex(0U);
3252   if (!isTypeLegal(RetTy, RetVT))
3253     return false;
3254
3255   if (RetVT != MVT::i32 && RetVT != MVT::i64)
3256     return false;
3257
3258   const Value *LHS = II->getArgOperand(0);
3259   const Value *RHS = II->getArgOperand(1);
3260
3261   // Canonicalize immediate to the RHS.
3262   if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3263       isCommutativeIntrinsic(II))
3264     std::swap(LHS, RHS);
3265
3266   // Simplify multiplies.
3267   Intrinsic::ID IID = II->getIntrinsicID();
3268   switch (IID) {
3269   default:
3270     break;
3271   case Intrinsic::smul_with_overflow:
3272     if (const auto *C = dyn_cast<ConstantInt>(RHS))
3273       if (C->getValue() == 2)
3274         IID = Intrinsic::sadd_with_overflow;
3275     break;
3276   case Intrinsic::umul_with_overflow:
3277     if (const auto *C = dyn_cast<ConstantInt>(RHS))
3278       if (C->getValue() == 2)
3279         IID = Intrinsic::uadd_with_overflow;
3280     break;
3281   }
3282
3283   AArch64CC::CondCode TmpCC;
3284   switch (IID) {
3285   default:
3286     return false;
3287   case Intrinsic::sadd_with_overflow:
3288   case Intrinsic::ssub_with_overflow:
3289     TmpCC = AArch64CC::VS;
3290     break;
3291   case Intrinsic::uadd_with_overflow:
3292     TmpCC = AArch64CC::HS;
3293     break;
3294   case Intrinsic::usub_with_overflow:
3295     TmpCC = AArch64CC::LO;
3296     break;
3297   case Intrinsic::smul_with_overflow:
3298   case Intrinsic::umul_with_overflow:
3299     TmpCC = AArch64CC::NE;
3300     break;
3301   }
3302
3303   // Check if both instructions are in the same basic block.
3304   if (!isValueAvailable(II))
3305     return false;
3306
3307   // Make sure nothing is in the way
3308   BasicBlock::const_iterator Start(I);
3309   BasicBlock::const_iterator End(II);
3310   for (auto Itr = std::prev(Start); Itr != End; --Itr) {
3311     // We only expect extractvalue instructions between the intrinsic and the
3312     // instruction to be selected.
3313     if (!isa<ExtractValueInst>(Itr))
3314       return false;
3315
3316     // Check that the extractvalue operand comes from the intrinsic.
3317     const auto *EVI = cast<ExtractValueInst>(Itr);
3318     if (EVI->getAggregateOperand() != II)
3319       return false;
3320   }
3321
3322   CC = TmpCC;
3323   return true;
3324 }
3325
3326 bool AArch64FastISel::fastLowerIntrinsicCall(const IntrinsicInst *II) {
3327   // FIXME: Handle more intrinsics.
3328   switch (II->getIntrinsicID()) {
3329   default: return false;
3330   case Intrinsic::frameaddress: {
3331     MachineFrameInfo *MFI = FuncInfo.MF->getFrameInfo();
3332     MFI->setFrameAddressIsTaken(true);
3333
3334     const AArch64RegisterInfo *RegInfo =
3335         static_cast<const AArch64RegisterInfo *>(Subtarget->getRegisterInfo());
3336     unsigned FramePtr = RegInfo->getFrameRegister(*(FuncInfo.MF));
3337     unsigned SrcReg = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3338     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3339             TII.get(TargetOpcode::COPY), SrcReg).addReg(FramePtr);
3340     // Recursively load frame address
3341     // ldr x0, [fp]
3342     // ldr x0, [x0]
3343     // ldr x0, [x0]
3344     // ...
3345     unsigned DestReg;
3346     unsigned Depth = cast<ConstantInt>(II->getOperand(0))->getZExtValue();
3347     while (Depth--) {
3348       DestReg = fastEmitInst_ri(AArch64::LDRXui, &AArch64::GPR64RegClass,
3349                                 SrcReg, /*IsKill=*/true, 0);
3350       assert(DestReg && "Unexpected LDR instruction emission failure.");
3351       SrcReg = DestReg;
3352     }
3353
3354     updateValueMap(II, SrcReg);
3355     return true;
3356   }
3357   case Intrinsic::memcpy:
3358   case Intrinsic::memmove: {
3359     const auto *MTI = cast<MemTransferInst>(II);
3360     // Don't handle volatile.
3361     if (MTI->isVolatile())
3362       return false;
3363
3364     // Disable inlining for memmove before calls to ComputeAddress.  Otherwise,
3365     // we would emit dead code because we don't currently handle memmoves.
3366     bool IsMemCpy = (II->getIntrinsicID() == Intrinsic::memcpy);
3367     if (isa<ConstantInt>(MTI->getLength()) && IsMemCpy) {
3368       // Small memcpy's are common enough that we want to do them without a call
3369       // if possible.
3370       uint64_t Len = cast<ConstantInt>(MTI->getLength())->getZExtValue();
3371       unsigned Alignment = MTI->getAlignment();
3372       if (isMemCpySmall(Len, Alignment)) {
3373         Address Dest, Src;
3374         if (!computeAddress(MTI->getRawDest(), Dest) ||
3375             !computeAddress(MTI->getRawSource(), Src))
3376           return false;
3377         if (tryEmitSmallMemCpy(Dest, Src, Len, Alignment))
3378           return true;
3379       }
3380     }
3381
3382     if (!MTI->getLength()->getType()->isIntegerTy(64))
3383       return false;
3384
3385     if (MTI->getSourceAddressSpace() > 255 || MTI->getDestAddressSpace() > 255)
3386       // Fast instruction selection doesn't support the special
3387       // address spaces.
3388       return false;
3389
3390     const char *IntrMemName = isa<MemCpyInst>(II) ? "memcpy" : "memmove";
3391     return lowerCallTo(II, IntrMemName, II->getNumArgOperands() - 2);
3392   }
3393   case Intrinsic::memset: {
3394     const MemSetInst *MSI = cast<MemSetInst>(II);
3395     // Don't handle volatile.
3396     if (MSI->isVolatile())
3397       return false;
3398
3399     if (!MSI->getLength()->getType()->isIntegerTy(64))
3400       return false;
3401
3402     if (MSI->getDestAddressSpace() > 255)
3403       // Fast instruction selection doesn't support the special
3404       // address spaces.
3405       return false;
3406
3407     return lowerCallTo(II, "memset", II->getNumArgOperands() - 2);
3408   }
3409   case Intrinsic::sin:
3410   case Intrinsic::cos:
3411   case Intrinsic::pow: {
3412     MVT RetVT;
3413     if (!isTypeLegal(II->getType(), RetVT))
3414       return false;
3415
3416     if (RetVT != MVT::f32 && RetVT != MVT::f64)
3417       return false;
3418
3419     static const RTLIB::Libcall LibCallTable[3][2] = {
3420       { RTLIB::SIN_F32, RTLIB::SIN_F64 },
3421       { RTLIB::COS_F32, RTLIB::COS_F64 },
3422       { RTLIB::POW_F32, RTLIB::POW_F64 }
3423     };
3424     RTLIB::Libcall LC;
3425     bool Is64Bit = RetVT == MVT::f64;
3426     switch (II->getIntrinsicID()) {
3427     default:
3428       llvm_unreachable("Unexpected intrinsic.");
3429     case Intrinsic::sin:
3430       LC = LibCallTable[0][Is64Bit];
3431       break;
3432     case Intrinsic::cos:
3433       LC = LibCallTable[1][Is64Bit];
3434       break;
3435     case Intrinsic::pow:
3436       LC = LibCallTable[2][Is64Bit];
3437       break;
3438     }
3439
3440     ArgListTy Args;
3441     Args.reserve(II->getNumArgOperands());
3442
3443     // Populate the argument list.
3444     for (auto &Arg : II->arg_operands()) {
3445       ArgListEntry Entry;
3446       Entry.Val = Arg;
3447       Entry.Ty = Arg->getType();
3448       Args.push_back(Entry);
3449     }
3450
3451     CallLoweringInfo CLI;
3452     MCContext &Ctx = MF->getContext();
3453     CLI.setCallee(DL, Ctx, TLI.getLibcallCallingConv(LC), II->getType(),
3454                   TLI.getLibcallName(LC), std::move(Args));
3455     if (!lowerCallTo(CLI))
3456       return false;
3457     updateValueMap(II, CLI.ResultReg);
3458     return true;
3459   }
3460   case Intrinsic::fabs: {
3461     MVT VT;
3462     if (!isTypeLegal(II->getType(), VT))
3463       return false;
3464
3465     unsigned Opc;
3466     switch (VT.SimpleTy) {
3467     default:
3468       return false;
3469     case MVT::f32:
3470       Opc = AArch64::FABSSr;
3471       break;
3472     case MVT::f64:
3473       Opc = AArch64::FABSDr;
3474       break;
3475     }
3476     unsigned SrcReg = getRegForValue(II->getOperand(0));
3477     if (!SrcReg)
3478       return false;
3479     bool SrcRegIsKill = hasTrivialKill(II->getOperand(0));
3480     unsigned ResultReg = createResultReg(TLI.getRegClassFor(VT));
3481     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(Opc), ResultReg)
3482       .addReg(SrcReg, getKillRegState(SrcRegIsKill));
3483     updateValueMap(II, ResultReg);
3484     return true;
3485   }
3486   case Intrinsic::trap: {
3487     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc, TII.get(AArch64::BRK))
3488         .addImm(1);
3489     return true;
3490   }
3491   case Intrinsic::sqrt: {
3492     Type *RetTy = II->getCalledFunction()->getReturnType();
3493
3494     MVT VT;
3495     if (!isTypeLegal(RetTy, VT))
3496       return false;
3497
3498     unsigned Op0Reg = getRegForValue(II->getOperand(0));
3499     if (!Op0Reg)
3500       return false;
3501     bool Op0IsKill = hasTrivialKill(II->getOperand(0));
3502
3503     unsigned ResultReg = fastEmit_r(VT, VT, ISD::FSQRT, Op0Reg, Op0IsKill);
3504     if (!ResultReg)
3505       return false;
3506
3507     updateValueMap(II, ResultReg);
3508     return true;
3509   }
3510   case Intrinsic::sadd_with_overflow:
3511   case Intrinsic::uadd_with_overflow:
3512   case Intrinsic::ssub_with_overflow:
3513   case Intrinsic::usub_with_overflow:
3514   case Intrinsic::smul_with_overflow:
3515   case Intrinsic::umul_with_overflow: {
3516     // This implements the basic lowering of the xalu with overflow intrinsics.
3517     const Function *Callee = II->getCalledFunction();
3518     auto *Ty = cast<StructType>(Callee->getReturnType());
3519     Type *RetTy = Ty->getTypeAtIndex(0U);
3520
3521     MVT VT;
3522     if (!isTypeLegal(RetTy, VT))
3523       return false;
3524
3525     if (VT != MVT::i32 && VT != MVT::i64)
3526       return false;
3527
3528     const Value *LHS = II->getArgOperand(0);
3529     const Value *RHS = II->getArgOperand(1);
3530     // Canonicalize immediate to the RHS.
3531     if (isa<ConstantInt>(LHS) && !isa<ConstantInt>(RHS) &&
3532         isCommutativeIntrinsic(II))
3533       std::swap(LHS, RHS);
3534
3535     // Simplify multiplies.
3536     Intrinsic::ID IID = II->getIntrinsicID();
3537     switch (IID) {
3538     default:
3539       break;
3540     case Intrinsic::smul_with_overflow:
3541       if (const auto *C = dyn_cast<ConstantInt>(RHS))
3542         if (C->getValue() == 2) {
3543           IID = Intrinsic::sadd_with_overflow;
3544           RHS = LHS;
3545         }
3546       break;
3547     case Intrinsic::umul_with_overflow:
3548       if (const auto *C = dyn_cast<ConstantInt>(RHS))
3549         if (C->getValue() == 2) {
3550           IID = Intrinsic::uadd_with_overflow;
3551           RHS = LHS;
3552         }
3553       break;
3554     }
3555
3556     unsigned ResultReg1 = 0, ResultReg2 = 0, MulReg = 0;
3557     AArch64CC::CondCode CC = AArch64CC::Invalid;
3558     switch (IID) {
3559     default: llvm_unreachable("Unexpected intrinsic!");
3560     case Intrinsic::sadd_with_overflow:
3561       ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3562       CC = AArch64CC::VS;
3563       break;
3564     case Intrinsic::uadd_with_overflow:
3565       ResultReg1 = emitAdd(VT, LHS, RHS, /*SetFlags=*/true);
3566       CC = AArch64CC::HS;
3567       break;
3568     case Intrinsic::ssub_with_overflow:
3569       ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3570       CC = AArch64CC::VS;
3571       break;
3572     case Intrinsic::usub_with_overflow:
3573       ResultReg1 = emitSub(VT, LHS, RHS, /*SetFlags=*/true);
3574       CC = AArch64CC::LO;
3575       break;
3576     case Intrinsic::smul_with_overflow: {
3577       CC = AArch64CC::NE;
3578       unsigned LHSReg = getRegForValue(LHS);
3579       if (!LHSReg)
3580         return false;
3581       bool LHSIsKill = hasTrivialKill(LHS);
3582
3583       unsigned RHSReg = getRegForValue(RHS);
3584       if (!RHSReg)
3585         return false;
3586       bool RHSIsKill = hasTrivialKill(RHS);
3587
3588       if (VT == MVT::i32) {
3589         MulReg = emitSMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3590         unsigned ShiftReg = emitLSR_ri(MVT::i64, MVT::i64, MulReg,
3591                                        /*IsKill=*/false, 32);
3592         MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3593                                             AArch64::sub_32);
3594         ShiftReg = fastEmitInst_extractsubreg(VT, ShiftReg, /*IsKill=*/true,
3595                                               AArch64::sub_32);
3596         emitSubs_rs(VT, ShiftReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3597                     AArch64_AM::ASR, 31, /*WantResult=*/false);
3598       } else {
3599         assert(VT == MVT::i64 && "Unexpected value type.");
3600         // LHSReg and RHSReg cannot be killed by this Mul, since they are
3601         // reused in the next instruction.
3602         MulReg = emitMul_rr(VT, LHSReg, /*IsKill=*/false, RHSReg,
3603                             /*IsKill=*/false);
3604         unsigned SMULHReg = fastEmit_rr(VT, VT, ISD::MULHS, LHSReg, LHSIsKill,
3605                                         RHSReg, RHSIsKill);
3606         emitSubs_rs(VT, SMULHReg, /*IsKill=*/true, MulReg, /*IsKill=*/false,
3607                     AArch64_AM::ASR, 63, /*WantResult=*/false);
3608       }
3609       break;
3610     }
3611     case Intrinsic::umul_with_overflow: {
3612       CC = AArch64CC::NE;
3613       unsigned LHSReg = getRegForValue(LHS);
3614       if (!LHSReg)
3615         return false;
3616       bool LHSIsKill = hasTrivialKill(LHS);
3617
3618       unsigned RHSReg = getRegForValue(RHS);
3619       if (!RHSReg)
3620         return false;
3621       bool RHSIsKill = hasTrivialKill(RHS);
3622
3623       if (VT == MVT::i32) {
3624         MulReg = emitUMULL_rr(MVT::i64, LHSReg, LHSIsKill, RHSReg, RHSIsKill);
3625         emitSubs_rs(MVT::i64, AArch64::XZR, /*IsKill=*/true, MulReg,
3626                     /*IsKill=*/false, AArch64_AM::LSR, 32,
3627                     /*WantResult=*/false);
3628         MulReg = fastEmitInst_extractsubreg(VT, MulReg, /*IsKill=*/true,
3629                                             AArch64::sub_32);
3630       } else {
3631         assert(VT == MVT::i64 && "Unexpected value type.");
3632         // LHSReg and RHSReg cannot be killed by this Mul, since they are
3633         // reused in the next instruction.
3634         MulReg = emitMul_rr(VT, LHSReg, /*IsKill=*/false, RHSReg,
3635                             /*IsKill=*/false);
3636         unsigned UMULHReg = fastEmit_rr(VT, VT, ISD::MULHU, LHSReg, LHSIsKill,
3637                                         RHSReg, RHSIsKill);
3638         emitSubs_rr(VT, AArch64::XZR, /*IsKill=*/true, UMULHReg,
3639                     /*IsKill=*/false, /*WantResult=*/false);
3640       }
3641       break;
3642     }
3643     }
3644
3645     if (MulReg) {
3646       ResultReg1 = createResultReg(TLI.getRegClassFor(VT));
3647       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3648               TII.get(TargetOpcode::COPY), ResultReg1).addReg(MulReg);
3649     }
3650
3651     ResultReg2 = fastEmitInst_rri(AArch64::CSINCWr, &AArch64::GPR32RegClass,
3652                                   AArch64::WZR, /*IsKill=*/true, AArch64::WZR,
3653                                   /*IsKill=*/true, getInvertedCondCode(CC));
3654     (void)ResultReg2;
3655     assert((ResultReg1 + 1) == ResultReg2 &&
3656            "Nonconsecutive result registers.");
3657     updateValueMap(II, ResultReg1, 2);
3658     return true;
3659   }
3660   }
3661   return false;
3662 }
3663
3664 bool AArch64FastISel::selectRet(const Instruction *I) {
3665   const ReturnInst *Ret = cast<ReturnInst>(I);
3666   const Function &F = *I->getParent()->getParent();
3667
3668   if (!FuncInfo.CanLowerReturn)
3669     return false;
3670
3671   if (F.isVarArg())
3672     return false;
3673
3674   if (TLI.supportSwiftError() &&
3675       F.getAttributes().hasAttrSomewhere(Attribute::SwiftError))
3676     return false;
3677
3678   if (TLI.supportSplitCSR(FuncInfo.MF))
3679     return false;
3680
3681   // Build a list of return value registers.
3682   SmallVector<unsigned, 4> RetRegs;
3683
3684   if (Ret->getNumOperands() > 0) {
3685     CallingConv::ID CC = F.getCallingConv();
3686     SmallVector<ISD::OutputArg, 4> Outs;
3687     GetReturnInfo(F.getReturnType(), F.getAttributes(), Outs, TLI, DL);
3688
3689     // Analyze operands of the call, assigning locations to each operand.
3690     SmallVector<CCValAssign, 16> ValLocs;
3691     CCState CCInfo(CC, F.isVarArg(), *FuncInfo.MF, ValLocs, I->getContext());
3692     CCAssignFn *RetCC = CC == CallingConv::WebKit_JS ? RetCC_AArch64_WebKit_JS
3693                                                      : RetCC_AArch64_AAPCS;
3694     CCInfo.AnalyzeReturn(Outs, RetCC);
3695
3696     // Only handle a single return value for now.
3697     if (ValLocs.size() != 1)
3698       return false;
3699
3700     CCValAssign &VA = ValLocs[0];
3701     const Value *RV = Ret->getOperand(0);
3702
3703     // Don't bother handling odd stuff for now.
3704     if ((VA.getLocInfo() != CCValAssign::Full) &&
3705         (VA.getLocInfo() != CCValAssign::BCvt))
3706       return false;
3707
3708     // Only handle register returns for now.
3709     if (!VA.isRegLoc())
3710       return false;
3711
3712     unsigned Reg = getRegForValue(RV);
3713     if (Reg == 0)
3714       return false;
3715
3716     unsigned SrcReg = Reg + VA.getValNo();
3717     unsigned DestReg = VA.getLocReg();
3718     // Avoid a cross-class copy. This is very unlikely.
3719     if (!MRI.getRegClass(SrcReg)->contains(DestReg))
3720       return false;
3721
3722     EVT RVEVT = TLI.getValueType(DL, RV->getType());
3723     if (!RVEVT.isSimple())
3724       return false;
3725
3726     // Vectors (of > 1 lane) in big endian need tricky handling.
3727     if (RVEVT.isVector() && RVEVT.getVectorNumElements() > 1 &&
3728         !Subtarget->isLittleEndian())
3729       return false;
3730
3731     MVT RVVT = RVEVT.getSimpleVT();
3732     if (RVVT == MVT::f128)
3733       return false;
3734
3735     MVT DestVT = VA.getValVT();
3736     // Special handling for extended integers.
3737     if (RVVT != DestVT) {
3738       if (RVVT != MVT::i1 && RVVT != MVT::i8 && RVVT != MVT::i16)
3739         return false;
3740
3741       if (!Outs[0].Flags.isZExt() && !Outs[0].Flags.isSExt())
3742         return false;
3743
3744       bool IsZExt = Outs[0].Flags.isZExt();
3745       SrcReg = emitIntExt(RVVT, SrcReg, DestVT, IsZExt);
3746       if (SrcReg == 0)
3747         return false;
3748     }
3749
3750     // Make the copy.
3751     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3752             TII.get(TargetOpcode::COPY), DestReg).addReg(SrcReg);
3753
3754     // Add register to return instruction.
3755     RetRegs.push_back(VA.getLocReg());
3756   }
3757
3758   MachineInstrBuilder MIB = BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3759                                     TII.get(AArch64::RET_ReallyLR));
3760   for (unsigned RetReg : RetRegs)
3761     MIB.addReg(RetReg, RegState::Implicit);
3762   return true;
3763 }
3764
3765 bool AArch64FastISel::selectTrunc(const Instruction *I) {
3766   Type *DestTy = I->getType();
3767   Value *Op = I->getOperand(0);
3768   Type *SrcTy = Op->getType();
3769
3770   EVT SrcEVT = TLI.getValueType(DL, SrcTy, true);
3771   EVT DestEVT = TLI.getValueType(DL, DestTy, true);
3772   if (!SrcEVT.isSimple())
3773     return false;
3774   if (!DestEVT.isSimple())
3775     return false;
3776
3777   MVT SrcVT = SrcEVT.getSimpleVT();
3778   MVT DestVT = DestEVT.getSimpleVT();
3779
3780   if (SrcVT != MVT::i64 && SrcVT != MVT::i32 && SrcVT != MVT::i16 &&
3781       SrcVT != MVT::i8)
3782     return false;
3783   if (DestVT != MVT::i32 && DestVT != MVT::i16 && DestVT != MVT::i8 &&
3784       DestVT != MVT::i1)
3785     return false;
3786
3787   unsigned SrcReg = getRegForValue(Op);
3788   if (!SrcReg)
3789     return false;
3790   bool SrcIsKill = hasTrivialKill(Op);
3791
3792   // If we're truncating from i64 to a smaller non-legal type then generate an
3793   // AND. Otherwise, we know the high bits are undefined and a truncate only
3794   // generate a COPY. We cannot mark the source register also as result
3795   // register, because this can incorrectly transfer the kill flag onto the
3796   // source register.
3797   unsigned ResultReg;
3798   if (SrcVT == MVT::i64) {
3799     uint64_t Mask = 0;
3800     switch (DestVT.SimpleTy) {
3801     default:
3802       // Trunc i64 to i32 is handled by the target-independent fast-isel.
3803       return false;
3804     case MVT::i1:
3805       Mask = 0x1;
3806       break;
3807     case MVT::i8:
3808       Mask = 0xff;
3809       break;
3810     case MVT::i16:
3811       Mask = 0xffff;
3812       break;
3813     }
3814     // Issue an extract_subreg to get the lower 32-bits.
3815     unsigned Reg32 = fastEmitInst_extractsubreg(MVT::i32, SrcReg, SrcIsKill,
3816                                                 AArch64::sub_32);
3817     // Create the AND instruction which performs the actual truncation.
3818     ResultReg = emitAnd_ri(MVT::i32, Reg32, /*IsKill=*/true, Mask);
3819     assert(ResultReg && "Unexpected AND instruction emission failure.");
3820   } else {
3821     ResultReg = createResultReg(&AArch64::GPR32RegClass);
3822     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3823             TII.get(TargetOpcode::COPY), ResultReg)
3824         .addReg(SrcReg, getKillRegState(SrcIsKill));
3825   }
3826
3827   updateValueMap(I, ResultReg);
3828   return true;
3829 }
3830
3831 unsigned AArch64FastISel::emiti1Ext(unsigned SrcReg, MVT DestVT, bool IsZExt) {
3832   assert((DestVT == MVT::i8 || DestVT == MVT::i16 || DestVT == MVT::i32 ||
3833           DestVT == MVT::i64) &&
3834          "Unexpected value type.");
3835   // Handle i8 and i16 as i32.
3836   if (DestVT == MVT::i8 || DestVT == MVT::i16)
3837     DestVT = MVT::i32;
3838
3839   if (IsZExt) {
3840     unsigned ResultReg = emitAnd_ri(MVT::i32, SrcReg, /*TODO:IsKill=*/false, 1);
3841     assert(ResultReg && "Unexpected AND instruction emission failure.");
3842     if (DestVT == MVT::i64) {
3843       // We're ZExt i1 to i64.  The ANDWri Wd, Ws, #1 implicitly clears the
3844       // upper 32 bits.  Emit a SUBREG_TO_REG to extend from Wd to Xd.
3845       unsigned Reg64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
3846       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3847               TII.get(AArch64::SUBREG_TO_REG), Reg64)
3848           .addImm(0)
3849           .addReg(ResultReg)
3850           .addImm(AArch64::sub_32);
3851       ResultReg = Reg64;
3852     }
3853     return ResultReg;
3854   } else {
3855     if (DestVT == MVT::i64) {
3856       // FIXME: We're SExt i1 to i64.
3857       return 0;
3858     }
3859     return fastEmitInst_rii(AArch64::SBFMWri, &AArch64::GPR32RegClass, SrcReg,
3860                             /*TODO:IsKill=*/false, 0, 0);
3861   }
3862 }
3863
3864 unsigned AArch64FastISel::emitMul_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3865                                       unsigned Op1, bool Op1IsKill) {
3866   unsigned Opc, ZReg;
3867   switch (RetVT.SimpleTy) {
3868   default: return 0;
3869   case MVT::i8:
3870   case MVT::i16:
3871   case MVT::i32:
3872     RetVT = MVT::i32;
3873     Opc = AArch64::MADDWrrr; ZReg = AArch64::WZR; break;
3874   case MVT::i64:
3875     Opc = AArch64::MADDXrrr; ZReg = AArch64::XZR; break;
3876   }
3877
3878   const TargetRegisterClass *RC =
3879       (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3880   return fastEmitInst_rrr(Opc, RC, Op0, Op0IsKill, Op1, Op1IsKill,
3881                           /*IsKill=*/ZReg, true);
3882 }
3883
3884 unsigned AArch64FastISel::emitSMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3885                                         unsigned Op1, bool Op1IsKill) {
3886   if (RetVT != MVT::i64)
3887     return 0;
3888
3889   return fastEmitInst_rrr(AArch64::SMADDLrrr, &AArch64::GPR64RegClass,
3890                           Op0, Op0IsKill, Op1, Op1IsKill,
3891                           AArch64::XZR, /*IsKill=*/true);
3892 }
3893
3894 unsigned AArch64FastISel::emitUMULL_rr(MVT RetVT, unsigned Op0, bool Op0IsKill,
3895                                         unsigned Op1, bool Op1IsKill) {
3896   if (RetVT != MVT::i64)
3897     return 0;
3898
3899   return fastEmitInst_rrr(AArch64::UMADDLrrr, &AArch64::GPR64RegClass,
3900                           Op0, Op0IsKill, Op1, Op1IsKill,
3901                           AArch64::XZR, /*IsKill=*/true);
3902 }
3903
3904 unsigned AArch64FastISel::emitLSL_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
3905                                      unsigned Op1Reg, bool Op1IsKill) {
3906   unsigned Opc = 0;
3907   bool NeedTrunc = false;
3908   uint64_t Mask = 0;
3909   switch (RetVT.SimpleTy) {
3910   default: return 0;
3911   case MVT::i8:  Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xff;   break;
3912   case MVT::i16: Opc = AArch64::LSLVWr; NeedTrunc = true; Mask = 0xffff; break;
3913   case MVT::i32: Opc = AArch64::LSLVWr;                                  break;
3914   case MVT::i64: Opc = AArch64::LSLVXr;                                  break;
3915   }
3916
3917   const TargetRegisterClass *RC =
3918       (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3919   if (NeedTrunc) {
3920     Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
3921     Op1IsKill = true;
3922   }
3923   unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
3924                                        Op1IsKill);
3925   if (NeedTrunc)
3926     ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
3927   return ResultReg;
3928 }
3929
3930 unsigned AArch64FastISel::emitLSL_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
3931                                      bool Op0IsKill, uint64_t Shift,
3932                                      bool IsZExt) {
3933   assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
3934          "Unexpected source/return type pair.");
3935   assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
3936           SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
3937          "Unexpected source value type.");
3938   assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
3939           RetVT == MVT::i64) && "Unexpected return value type.");
3940
3941   bool Is64Bit = (RetVT == MVT::i64);
3942   unsigned RegSize = Is64Bit ? 64 : 32;
3943   unsigned DstBits = RetVT.getSizeInBits();
3944   unsigned SrcBits = SrcVT.getSizeInBits();
3945   const TargetRegisterClass *RC =
3946       Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
3947
3948   // Just emit a copy for "zero" shifts.
3949   if (Shift == 0) {
3950     if (RetVT == SrcVT) {
3951       unsigned ResultReg = createResultReg(RC);
3952       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
3953               TII.get(TargetOpcode::COPY), ResultReg)
3954           .addReg(Op0, getKillRegState(Op0IsKill));
3955       return ResultReg;
3956     } else
3957       return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
3958   }
3959
3960   // Don't deal with undefined shifts.
3961   if (Shift >= DstBits)
3962     return 0;
3963
3964   // For immediate shifts we can fold the zero-/sign-extension into the shift.
3965   // {S|U}BFM Wd, Wn, #r, #s
3966   // Wd<32+s-r,32-r> = Wn<s:0> when r > s
3967
3968   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3969   // %2 = shl i16 %1, 4
3970   // Wd<32+7-28,32-28> = Wn<7:0> <- clamp s to 7
3971   // 0b1111_1111_1111_1111__1111_1010_1010_0000 sext
3972   // 0b0000_0000_0000_0000__0000_0101_0101_0000 sext | zext
3973   // 0b0000_0000_0000_0000__0000_1010_1010_0000 zext
3974
3975   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3976   // %2 = shl i16 %1, 8
3977   // Wd<32+7-24,32-24> = Wn<7:0>
3978   // 0b1111_1111_1111_1111__1010_1010_0000_0000 sext
3979   // 0b0000_0000_0000_0000__0101_0101_0000_0000 sext | zext
3980   // 0b0000_0000_0000_0000__1010_1010_0000_0000 zext
3981
3982   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
3983   // %2 = shl i16 %1, 12
3984   // Wd<32+3-20,32-20> = Wn<3:0>
3985   // 0b1111_1111_1111_1111__1010_0000_0000_0000 sext
3986   // 0b0000_0000_0000_0000__0101_0000_0000_0000 sext | zext
3987   // 0b0000_0000_0000_0000__1010_0000_0000_0000 zext
3988
3989   unsigned ImmR = RegSize - Shift;
3990   // Limit the width to the length of the source type.
3991   unsigned ImmS = std::min<unsigned>(SrcBits - 1, DstBits - 1 - Shift);
3992   static const unsigned OpcTable[2][2] = {
3993     {AArch64::SBFMWri, AArch64::SBFMXri},
3994     {AArch64::UBFMWri, AArch64::UBFMXri}
3995   };
3996   unsigned Opc = OpcTable[IsZExt][Is64Bit];
3997   if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
3998     unsigned TmpReg = MRI.createVirtualRegister(RC);
3999     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4000             TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4001         .addImm(0)
4002         .addReg(Op0, getKillRegState(Op0IsKill))
4003         .addImm(AArch64::sub_32);
4004     Op0 = TmpReg;
4005     Op0IsKill = true;
4006   }
4007   return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4008 }
4009
4010 unsigned AArch64FastISel::emitLSR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
4011                                      unsigned Op1Reg, bool Op1IsKill) {
4012   unsigned Opc = 0;
4013   bool NeedTrunc = false;
4014   uint64_t Mask = 0;
4015   switch (RetVT.SimpleTy) {
4016   default: return 0;
4017   case MVT::i8:  Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xff;   break;
4018   case MVT::i16: Opc = AArch64::LSRVWr; NeedTrunc = true; Mask = 0xffff; break;
4019   case MVT::i32: Opc = AArch64::LSRVWr; break;
4020   case MVT::i64: Opc = AArch64::LSRVXr; break;
4021   }
4022
4023   const TargetRegisterClass *RC =
4024       (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4025   if (NeedTrunc) {
4026     Op0Reg = emitAnd_ri(MVT::i32, Op0Reg, Op0IsKill, Mask);
4027     Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
4028     Op0IsKill = Op1IsKill = true;
4029   }
4030   unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
4031                                        Op1IsKill);
4032   if (NeedTrunc)
4033     ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
4034   return ResultReg;
4035 }
4036
4037 unsigned AArch64FastISel::emitLSR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
4038                                      bool Op0IsKill, uint64_t Shift,
4039                                      bool IsZExt) {
4040   assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
4041          "Unexpected source/return type pair.");
4042   assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
4043           SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
4044          "Unexpected source value type.");
4045   assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
4046           RetVT == MVT::i64) && "Unexpected return value type.");
4047
4048   bool Is64Bit = (RetVT == MVT::i64);
4049   unsigned RegSize = Is64Bit ? 64 : 32;
4050   unsigned DstBits = RetVT.getSizeInBits();
4051   unsigned SrcBits = SrcVT.getSizeInBits();
4052   const TargetRegisterClass *RC =
4053       Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4054
4055   // Just emit a copy for "zero" shifts.
4056   if (Shift == 0) {
4057     if (RetVT == SrcVT) {
4058       unsigned ResultReg = createResultReg(RC);
4059       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4060               TII.get(TargetOpcode::COPY), ResultReg)
4061       .addReg(Op0, getKillRegState(Op0IsKill));
4062       return ResultReg;
4063     } else
4064       return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4065   }
4066
4067   // Don't deal with undefined shifts.
4068   if (Shift >= DstBits)
4069     return 0;
4070
4071   // For immediate shifts we can fold the zero-/sign-extension into the shift.
4072   // {S|U}BFM Wd, Wn, #r, #s
4073   // Wd<s-r:0> = Wn<s:r> when r <= s
4074
4075   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4076   // %2 = lshr i16 %1, 4
4077   // Wd<7-4:0> = Wn<7:4>
4078   // 0b0000_0000_0000_0000__0000_1111_1111_1010 sext
4079   // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
4080   // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
4081
4082   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4083   // %2 = lshr i16 %1, 8
4084   // Wd<7-7,0> = Wn<7:7>
4085   // 0b0000_0000_0000_0000__0000_0000_1111_1111 sext
4086   // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4087   // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4088
4089   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4090   // %2 = lshr i16 %1, 12
4091   // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
4092   // 0b0000_0000_0000_0000__0000_0000_0000_1111 sext
4093   // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4094   // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4095
4096   if (Shift >= SrcBits && IsZExt)
4097     return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
4098
4099   // It is not possible to fold a sign-extend into the LShr instruction. In this
4100   // case emit a sign-extend.
4101   if (!IsZExt) {
4102     Op0 = emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4103     if (!Op0)
4104       return 0;
4105     Op0IsKill = true;
4106     SrcVT = RetVT;
4107     SrcBits = SrcVT.getSizeInBits();
4108     IsZExt = true;
4109   }
4110
4111   unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
4112   unsigned ImmS = SrcBits - 1;
4113   static const unsigned OpcTable[2][2] = {
4114     {AArch64::SBFMWri, AArch64::SBFMXri},
4115     {AArch64::UBFMWri, AArch64::UBFMXri}
4116   };
4117   unsigned Opc = OpcTable[IsZExt][Is64Bit];
4118   if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
4119     unsigned TmpReg = MRI.createVirtualRegister(RC);
4120     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4121             TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4122         .addImm(0)
4123         .addReg(Op0, getKillRegState(Op0IsKill))
4124         .addImm(AArch64::sub_32);
4125     Op0 = TmpReg;
4126     Op0IsKill = true;
4127   }
4128   return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4129 }
4130
4131 unsigned AArch64FastISel::emitASR_rr(MVT RetVT, unsigned Op0Reg, bool Op0IsKill,
4132                                      unsigned Op1Reg, bool Op1IsKill) {
4133   unsigned Opc = 0;
4134   bool NeedTrunc = false;
4135   uint64_t Mask = 0;
4136   switch (RetVT.SimpleTy) {
4137   default: return 0;
4138   case MVT::i8:  Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xff;   break;
4139   case MVT::i16: Opc = AArch64::ASRVWr; NeedTrunc = true; Mask = 0xffff; break;
4140   case MVT::i32: Opc = AArch64::ASRVWr;                                  break;
4141   case MVT::i64: Opc = AArch64::ASRVXr;                                  break;
4142   }
4143
4144   const TargetRegisterClass *RC =
4145       (RetVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4146   if (NeedTrunc) {
4147     Op0Reg = emitIntExt(RetVT, Op0Reg, MVT::i32, /*IsZExt=*/false);
4148     Op1Reg = emitAnd_ri(MVT::i32, Op1Reg, Op1IsKill, Mask);
4149     Op0IsKill = Op1IsKill = true;
4150   }
4151   unsigned ResultReg = fastEmitInst_rr(Opc, RC, Op0Reg, Op0IsKill, Op1Reg,
4152                                        Op1IsKill);
4153   if (NeedTrunc)
4154     ResultReg = emitAnd_ri(MVT::i32, ResultReg, /*IsKill=*/true, Mask);
4155   return ResultReg;
4156 }
4157
4158 unsigned AArch64FastISel::emitASR_ri(MVT RetVT, MVT SrcVT, unsigned Op0,
4159                                      bool Op0IsKill, uint64_t Shift,
4160                                      bool IsZExt) {
4161   assert(RetVT.SimpleTy >= SrcVT.SimpleTy &&
4162          "Unexpected source/return type pair.");
4163   assert((SrcVT == MVT::i1 || SrcVT == MVT::i8 || SrcVT == MVT::i16 ||
4164           SrcVT == MVT::i32 || SrcVT == MVT::i64) &&
4165          "Unexpected source value type.");
4166   assert((RetVT == MVT::i8 || RetVT == MVT::i16 || RetVT == MVT::i32 ||
4167           RetVT == MVT::i64) && "Unexpected return value type.");
4168
4169   bool Is64Bit = (RetVT == MVT::i64);
4170   unsigned RegSize = Is64Bit ? 64 : 32;
4171   unsigned DstBits = RetVT.getSizeInBits();
4172   unsigned SrcBits = SrcVT.getSizeInBits();
4173   const TargetRegisterClass *RC =
4174       Is64Bit ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4175
4176   // Just emit a copy for "zero" shifts.
4177   if (Shift == 0) {
4178     if (RetVT == SrcVT) {
4179       unsigned ResultReg = createResultReg(RC);
4180       BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4181               TII.get(TargetOpcode::COPY), ResultReg)
4182       .addReg(Op0, getKillRegState(Op0IsKill));
4183       return ResultReg;
4184     } else
4185       return emitIntExt(SrcVT, Op0, RetVT, IsZExt);
4186   }
4187
4188   // Don't deal with undefined shifts.
4189   if (Shift >= DstBits)
4190     return 0;
4191
4192   // For immediate shifts we can fold the zero-/sign-extension into the shift.
4193   // {S|U}BFM Wd, Wn, #r, #s
4194   // Wd<s-r:0> = Wn<s:r> when r <= s
4195
4196   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4197   // %2 = ashr i16 %1, 4
4198   // Wd<7-4:0> = Wn<7:4>
4199   // 0b1111_1111_1111_1111__1111_1111_1111_1010 sext
4200   // 0b0000_0000_0000_0000__0000_0000_0000_0101 sext | zext
4201   // 0b0000_0000_0000_0000__0000_0000_0000_1010 zext
4202
4203   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4204   // %2 = ashr i16 %1, 8
4205   // Wd<7-7,0> = Wn<7:7>
4206   // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
4207   // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4208   // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4209
4210   // %1 = {s|z}ext i8 {0b1010_1010|0b0101_0101} to i16
4211   // %2 = ashr i16 %1, 12
4212   // Wd<7-7,0> = Wn<7:7> <- clamp r to 7
4213   // 0b1111_1111_1111_1111__1111_1111_1111_1111 sext
4214   // 0b0000_0000_0000_0000__0000_0000_0000_0000 sext
4215   // 0b0000_0000_0000_0000__0000_0000_0000_0000 zext
4216
4217   if (Shift >= SrcBits && IsZExt)
4218     return materializeInt(ConstantInt::get(*Context, APInt(RegSize, 0)), RetVT);
4219
4220   unsigned ImmR = std::min<unsigned>(SrcBits - 1, Shift);
4221   unsigned ImmS = SrcBits - 1;
4222   static const unsigned OpcTable[2][2] = {
4223     {AArch64::SBFMWri, AArch64::SBFMXri},
4224     {AArch64::UBFMWri, AArch64::UBFMXri}
4225   };
4226   unsigned Opc = OpcTable[IsZExt][Is64Bit];
4227   if (SrcVT.SimpleTy <= MVT::i32 && RetVT == MVT::i64) {
4228     unsigned TmpReg = MRI.createVirtualRegister(RC);
4229     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4230             TII.get(AArch64::SUBREG_TO_REG), TmpReg)
4231         .addImm(0)
4232         .addReg(Op0, getKillRegState(Op0IsKill))
4233         .addImm(AArch64::sub_32);
4234     Op0 = TmpReg;
4235     Op0IsKill = true;
4236   }
4237   return fastEmitInst_rii(Opc, RC, Op0, Op0IsKill, ImmR, ImmS);
4238 }
4239
4240 unsigned AArch64FastISel::emitIntExt(MVT SrcVT, unsigned SrcReg, MVT DestVT,
4241                                      bool IsZExt) {
4242   assert(DestVT != MVT::i1 && "ZeroExt/SignExt an i1?");
4243
4244   // FastISel does not have plumbing to deal with extensions where the SrcVT or
4245   // DestVT are odd things, so test to make sure that they are both types we can
4246   // handle (i1/i8/i16/i32 for SrcVT and i8/i16/i32/i64 for DestVT), otherwise
4247   // bail out to SelectionDAG.
4248   if (((DestVT != MVT::i8) && (DestVT != MVT::i16) &&
4249        (DestVT != MVT::i32) && (DestVT != MVT::i64)) ||
4250       ((SrcVT !=  MVT::i1) && (SrcVT !=  MVT::i8) &&
4251        (SrcVT !=  MVT::i16) && (SrcVT !=  MVT::i32)))
4252     return 0;
4253
4254   unsigned Opc;
4255   unsigned Imm = 0;
4256
4257   switch (SrcVT.SimpleTy) {
4258   default:
4259     return 0;
4260   case MVT::i1:
4261     return emiti1Ext(SrcReg, DestVT, IsZExt);
4262   case MVT::i8:
4263     if (DestVT == MVT::i64)
4264       Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4265     else
4266       Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
4267     Imm = 7;
4268     break;
4269   case MVT::i16:
4270     if (DestVT == MVT::i64)
4271       Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4272     else
4273       Opc = IsZExt ? AArch64::UBFMWri : AArch64::SBFMWri;
4274     Imm = 15;
4275     break;
4276   case MVT::i32:
4277     assert(DestVT == MVT::i64 && "IntExt i32 to i32?!?");
4278     Opc = IsZExt ? AArch64::UBFMXri : AArch64::SBFMXri;
4279     Imm = 31;
4280     break;
4281   }
4282
4283   // Handle i8 and i16 as i32.
4284   if (DestVT == MVT::i8 || DestVT == MVT::i16)
4285     DestVT = MVT::i32;
4286   else if (DestVT == MVT::i64) {
4287     unsigned Src64 = MRI.createVirtualRegister(&AArch64::GPR64RegClass);
4288     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4289             TII.get(AArch64::SUBREG_TO_REG), Src64)
4290         .addImm(0)
4291         .addReg(SrcReg)
4292         .addImm(AArch64::sub_32);
4293     SrcReg = Src64;
4294   }
4295
4296   const TargetRegisterClass *RC =
4297       (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4298   return fastEmitInst_rii(Opc, RC, SrcReg, /*TODO:IsKill=*/false, 0, Imm);
4299 }
4300
4301 static bool isZExtLoad(const MachineInstr *LI) {
4302   switch (LI->getOpcode()) {
4303   default:
4304     return false;
4305   case AArch64::LDURBBi:
4306   case AArch64::LDURHHi:
4307   case AArch64::LDURWi:
4308   case AArch64::LDRBBui:
4309   case AArch64::LDRHHui:
4310   case AArch64::LDRWui:
4311   case AArch64::LDRBBroX:
4312   case AArch64::LDRHHroX:
4313   case AArch64::LDRWroX:
4314   case AArch64::LDRBBroW:
4315   case AArch64::LDRHHroW:
4316   case AArch64::LDRWroW:
4317     return true;
4318   }
4319 }
4320
4321 static bool isSExtLoad(const MachineInstr *LI) {
4322   switch (LI->getOpcode()) {
4323   default:
4324     return false;
4325   case AArch64::LDURSBWi:
4326   case AArch64::LDURSHWi:
4327   case AArch64::LDURSBXi:
4328   case AArch64::LDURSHXi:
4329   case AArch64::LDURSWi:
4330   case AArch64::LDRSBWui:
4331   case AArch64::LDRSHWui:
4332   case AArch64::LDRSBXui:
4333   case AArch64::LDRSHXui:
4334   case AArch64::LDRSWui:
4335   case AArch64::LDRSBWroX:
4336   case AArch64::LDRSHWroX:
4337   case AArch64::LDRSBXroX:
4338   case AArch64::LDRSHXroX:
4339   case AArch64::LDRSWroX:
4340   case AArch64::LDRSBWroW:
4341   case AArch64::LDRSHWroW:
4342   case AArch64::LDRSBXroW:
4343   case AArch64::LDRSHXroW:
4344   case AArch64::LDRSWroW:
4345     return true;
4346   }
4347 }
4348
4349 bool AArch64FastISel::optimizeIntExtLoad(const Instruction *I, MVT RetVT,
4350                                          MVT SrcVT) {
4351   const auto *LI = dyn_cast<LoadInst>(I->getOperand(0));
4352   if (!LI || !LI->hasOneUse())
4353     return false;
4354
4355   // Check if the load instruction has already been selected.
4356   unsigned Reg = lookUpRegForValue(LI);
4357   if (!Reg)
4358     return false;
4359
4360   MachineInstr *MI = MRI.getUniqueVRegDef(Reg);
4361   if (!MI)
4362     return false;
4363
4364   // Check if the correct load instruction has been emitted - SelectionDAG might
4365   // have emitted a zero-extending load, but we need a sign-extending load.
4366   bool IsZExt = isa<ZExtInst>(I);
4367   const auto *LoadMI = MI;
4368   if (LoadMI->getOpcode() == TargetOpcode::COPY &&
4369       LoadMI->getOperand(1).getSubReg() == AArch64::sub_32) {
4370     unsigned LoadReg = MI->getOperand(1).getReg();
4371     LoadMI = MRI.getUniqueVRegDef(LoadReg);
4372     assert(LoadMI && "Expected valid instruction");
4373   }
4374   if (!(IsZExt && isZExtLoad(LoadMI)) && !(!IsZExt && isSExtLoad(LoadMI)))
4375     return false;
4376
4377   // Nothing to be done.
4378   if (RetVT != MVT::i64 || SrcVT > MVT::i32) {
4379     updateValueMap(I, Reg);
4380     return true;
4381   }
4382
4383   if (IsZExt) {
4384     unsigned Reg64 = createResultReg(&AArch64::GPR64RegClass);
4385     BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4386             TII.get(AArch64::SUBREG_TO_REG), Reg64)
4387         .addImm(0)
4388         .addReg(Reg, getKillRegState(true))
4389         .addImm(AArch64::sub_32);
4390     Reg = Reg64;
4391   } else {
4392     assert((MI->getOpcode() == TargetOpcode::COPY &&
4393             MI->getOperand(1).getSubReg() == AArch64::sub_32) &&
4394            "Expected copy instruction");
4395     Reg = MI->getOperand(1).getReg();
4396     MI->eraseFromParent();
4397   }
4398   updateValueMap(I, Reg);
4399   return true;
4400 }
4401
4402 bool AArch64FastISel::selectIntExt(const Instruction *I) {
4403   assert((isa<ZExtInst>(I) || isa<SExtInst>(I)) &&
4404          "Unexpected integer extend instruction.");
4405   MVT RetVT;
4406   MVT SrcVT;
4407   if (!isTypeSupported(I->getType(), RetVT))
4408     return false;
4409
4410   if (!isTypeSupported(I->getOperand(0)->getType(), SrcVT))
4411     return false;
4412
4413   // Try to optimize already sign-/zero-extended values from load instructions.
4414   if (optimizeIntExtLoad(I, RetVT, SrcVT))
4415     return true;
4416
4417   unsigned SrcReg = getRegForValue(I->getOperand(0));
4418   if (!SrcReg)
4419     return false;
4420   bool SrcIsKill = hasTrivialKill(I->getOperand(0));
4421
4422   // Try to optimize already sign-/zero-extended values from function arguments.
4423   bool IsZExt = isa<ZExtInst>(I);
4424   if (const auto *Arg = dyn_cast<Argument>(I->getOperand(0))) {
4425     if ((IsZExt && Arg->hasZExtAttr()) || (!IsZExt && Arg->hasSExtAttr())) {
4426       if (RetVT == MVT::i64 && SrcVT != MVT::i64) {
4427         unsigned ResultReg = createResultReg(&AArch64::GPR64RegClass);
4428         BuildMI(*FuncInfo.MBB, FuncInfo.InsertPt, DbgLoc,
4429                 TII.get(AArch64::SUBREG_TO_REG), ResultReg)
4430             .addImm(0)
4431             .addReg(SrcReg, getKillRegState(SrcIsKill))
4432             .addImm(AArch64::sub_32);
4433         SrcReg = ResultReg;
4434       }
4435       // Conservatively clear all kill flags from all uses, because we are
4436       // replacing a sign-/zero-extend instruction at IR level with a nop at MI
4437       // level. The result of the instruction at IR level might have been
4438       // trivially dead, which is now not longer true.
4439       unsigned UseReg = lookUpRegForValue(I);
4440       if (UseReg)
4441         MRI.clearKillFlags(UseReg);
4442
4443       updateValueMap(I, SrcReg);
4444       return true;
4445     }
4446   }
4447
4448   unsigned ResultReg = emitIntExt(SrcVT, SrcReg, RetVT, IsZExt);
4449   if (!ResultReg)
4450     return false;
4451
4452   updateValueMap(I, ResultReg);
4453   return true;
4454 }
4455
4456 bool AArch64FastISel::selectRem(const Instruction *I, unsigned ISDOpcode) {
4457   EVT DestEVT = TLI.getValueType(DL, I->getType(), true);
4458   if (!DestEVT.isSimple())
4459     return false;
4460
4461   MVT DestVT = DestEVT.getSimpleVT();
4462   if (DestVT != MVT::i64 && DestVT != MVT::i32)
4463     return false;
4464
4465   unsigned DivOpc;
4466   bool Is64bit = (DestVT == MVT::i64);
4467   switch (ISDOpcode) {
4468   default:
4469     return false;
4470   case ISD::SREM:
4471     DivOpc = Is64bit ? AArch64::SDIVXr : AArch64::SDIVWr;
4472     break;
4473   case ISD::UREM:
4474     DivOpc = Is64bit ? AArch64::UDIVXr : AArch64::UDIVWr;
4475     break;
4476   }
4477   unsigned MSubOpc = Is64bit ? AArch64::MSUBXrrr : AArch64::MSUBWrrr;
4478   unsigned Src0Reg = getRegForValue(I->getOperand(0));
4479   if (!Src0Reg)
4480     return false;
4481   bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4482
4483   unsigned Src1Reg = getRegForValue(I->getOperand(1));
4484   if (!Src1Reg)
4485     return false;
4486   bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4487
4488   const TargetRegisterClass *RC =
4489       (DestVT == MVT::i64) ? &AArch64::GPR64RegClass : &AArch64::GPR32RegClass;
4490   unsigned QuotReg = fastEmitInst_rr(DivOpc, RC, Src0Reg, /*IsKill=*/false,
4491                                      Src1Reg, /*IsKill=*/false);
4492   assert(QuotReg && "Unexpected DIV instruction emission failure.");
4493   // The remainder is computed as numerator - (quotient * denominator) using the
4494   // MSUB instruction.
4495   unsigned ResultReg = fastEmitInst_rrr(MSubOpc, RC, QuotReg, /*IsKill=*/true,
4496                                         Src1Reg, Src1IsKill, Src0Reg,
4497                                         Src0IsKill);
4498   updateValueMap(I, ResultReg);
4499   return true;
4500 }
4501
4502 bool AArch64FastISel::selectMul(const Instruction *I) {
4503   MVT VT;
4504   if (!isTypeSupported(I->getType(), VT, /*IsVectorAllowed=*/true))
4505     return false;
4506
4507   if (VT.isVector())
4508     return selectBinaryOp(I, ISD::MUL);
4509
4510   const Value *Src0 = I->getOperand(0);
4511   const Value *Src1 = I->getOperand(1);
4512   if (const auto *C = dyn_cast<ConstantInt>(Src0))
4513     if (C->getValue().isPowerOf2())
4514       std::swap(Src0, Src1);
4515
4516   // Try to simplify to a shift instruction.
4517   if (const auto *C = dyn_cast<ConstantInt>(Src1))
4518     if (C->getValue().isPowerOf2()) {
4519       uint64_t ShiftVal = C->getValue().logBase2();
4520       MVT SrcVT = VT;
4521       bool IsZExt = true;
4522       if (const auto *ZExt = dyn_cast<ZExtInst>(Src0)) {
4523         if (!isIntExtFree(ZExt)) {
4524           MVT VT;
4525           if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), VT)) {
4526             SrcVT = VT;
4527             IsZExt = true;
4528             Src0 = ZExt->getOperand(0);
4529           }
4530         }
4531       } else if (const auto *SExt = dyn_cast<SExtInst>(Src0)) {
4532         if (!isIntExtFree(SExt)) {
4533           MVT VT;
4534           if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), VT)) {
4535             SrcVT = VT;
4536             IsZExt = false;
4537             Src0 = SExt->getOperand(0);
4538           }
4539         }
4540       }
4541
4542       unsigned Src0Reg = getRegForValue(Src0);
4543       if (!Src0Reg)
4544         return false;
4545       bool Src0IsKill = hasTrivialKill(Src0);
4546
4547       unsigned ResultReg =
4548           emitLSL_ri(VT, SrcVT, Src0Reg, Src0IsKill, ShiftVal, IsZExt);
4549
4550       if (ResultReg) {
4551         updateValueMap(I, ResultReg);
4552         return true;
4553       }
4554     }
4555
4556   unsigned Src0Reg = getRegForValue(I->getOperand(0));
4557   if (!Src0Reg)
4558     return false;
4559   bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4560
4561   unsigned Src1Reg = getRegForValue(I->getOperand(1));
4562   if (!Src1Reg)
4563     return false;
4564   bool Src1IsKill = hasTrivialKill(I->getOperand(1));
4565
4566   unsigned ResultReg = emitMul_rr(VT, Src0Reg, Src0IsKill, Src1Reg, Src1IsKill);
4567
4568   if (!ResultReg)
4569     return false;
4570
4571   updateValueMap(I, ResultReg);
4572   return true;
4573 }
4574
4575 bool AArch64FastISel::selectShift(const Instruction *I) {
4576   MVT RetVT;
4577   if (!isTypeSupported(I->getType(), RetVT, /*IsVectorAllowed=*/true))
4578     return false;
4579
4580   if (RetVT.isVector())
4581     return selectOperator(I, I->getOpcode());
4582
4583   if (const auto *C = dyn_cast<ConstantInt>(I->getOperand(1))) {
4584     unsigned ResultReg = 0;
4585     uint64_t ShiftVal = C->getZExtValue();
4586     MVT SrcVT = RetVT;
4587     bool IsZExt = I->getOpcode() != Instruction::AShr;
4588     const Value *Op0 = I->getOperand(0);
4589     if (const auto *ZExt = dyn_cast<ZExtInst>(Op0)) {
4590       if (!isIntExtFree(ZExt)) {
4591         MVT TmpVT;
4592         if (isValueAvailable(ZExt) && isTypeSupported(ZExt->getSrcTy(), TmpVT)) {
4593           SrcVT = TmpVT;
4594           IsZExt = true;
4595           Op0 = ZExt->getOperand(0);
4596         }
4597       }
4598     } else if (const auto *SExt = dyn_cast<SExtInst>(Op0)) {
4599       if (!isIntExtFree(SExt)) {
4600         MVT TmpVT;
4601         if (isValueAvailable(SExt) && isTypeSupported(SExt->getSrcTy(), TmpVT)) {
4602           SrcVT = TmpVT;
4603           IsZExt = false;
4604           Op0 = SExt->getOperand(0);
4605         }
4606       }
4607     }
4608
4609     unsigned Op0Reg = getRegForValue(Op0);
4610     if (!Op0Reg)
4611       return false;
4612     bool Op0IsKill = hasTrivialKill(Op0);
4613
4614     switch (I->getOpcode()) {
4615     default: llvm_unreachable("Unexpected instruction.");
4616     case Instruction::Shl:
4617       ResultReg = emitLSL_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4618       break;
4619     case Instruction::AShr:
4620       ResultReg = emitASR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4621       break;
4622     case Instruction::LShr:
4623       ResultReg = emitLSR_ri(RetVT, SrcVT, Op0Reg, Op0IsKill, ShiftVal, IsZExt);
4624       break;
4625     }
4626     if (!ResultReg)
4627       return false;
4628
4629     updateValueMap(I, ResultReg);
4630     return true;
4631   }
4632
4633   unsigned Op0Reg = getRegForValue(I->getOperand(0));
4634   if (!Op0Reg)
4635     return false;
4636   bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4637
4638   unsigned Op1Reg = getRegForValue(I->getOperand(1));
4639   if (!Op1Reg)
4640     return false;
4641   bool Op1IsKill = hasTrivialKill(I->getOperand(1));
4642
4643   unsigned ResultReg = 0;
4644   switch (I->getOpcode()) {
4645   default: llvm_unreachable("Unexpected instruction.");
4646   case Instruction::Shl:
4647     ResultReg = emitLSL_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4648     break;
4649   case Instruction::AShr:
4650     ResultReg = emitASR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4651     break;
4652   case Instruction::LShr:
4653     ResultReg = emitLSR_rr(RetVT, Op0Reg, Op0IsKill, Op1Reg, Op1IsKill);
4654     break;
4655   }
4656
4657   if (!ResultReg)
4658     return false;
4659
4660   updateValueMap(I, ResultReg);
4661   return true;
4662 }
4663
4664 bool AArch64FastISel::selectBitCast(const Instruction *I) {
4665   MVT RetVT, SrcVT;
4666
4667   if (!isTypeLegal(I->getOperand(0)->getType(), SrcVT))
4668     return false;
4669   if (!isTypeLegal(I->getType(), RetVT))
4670     return false;
4671
4672   unsigned Opc;
4673   if (RetVT == MVT::f32 && SrcVT == MVT::i32)
4674     Opc = AArch64::FMOVWSr;
4675   else if (RetVT == MVT::f64 && SrcVT == MVT::i64)
4676     Opc = AArch64::FMOVXDr;
4677   else if (RetVT == MVT::i32 && SrcVT == MVT::f32)
4678     Opc = AArch64::FMOVSWr;
4679   else if (RetVT == MVT::i64 && SrcVT == MVT::f64)
4680     Opc = AArch64::FMOVDXr;
4681   else
4682     return false;
4683
4684   const TargetRegisterClass *RC = nullptr;
4685   switch (RetVT.SimpleTy) {
4686   default: llvm_unreachable("Unexpected value type.");
4687   case MVT::i32: RC = &AArch64::GPR32RegClass; break;
4688   case MVT::i64: RC = &AArch64::GPR64RegClass; break;
4689   case MVT::f32: RC = &AArch64::FPR32RegClass; break;
4690   case MVT::f64: RC = &AArch64::FPR64RegClass; break;
4691   }
4692   unsigned Op0Reg = getRegForValue(I->getOperand(0));
4693   if (!Op0Reg)
4694     return false;
4695   bool Op0IsKill = hasTrivialKill(I->getOperand(0));
4696   unsigned ResultReg = fastEmitInst_r(Opc, RC, Op0Reg, Op0IsKill);
4697
4698   if (!ResultReg)
4699     return false;
4700
4701   updateValueMap(I, ResultReg);
4702   return true;
4703 }
4704
4705 bool AArch64FastISel::selectFRem(const Instruction *I) {
4706   MVT RetVT;
4707   if (!isTypeLegal(I->getType(), RetVT))
4708     return false;
4709
4710   RTLIB::Libcall LC;
4711   switch (RetVT.SimpleTy) {
4712   default:
4713     return false;
4714   case MVT::f32:
4715     LC = RTLIB::REM_F32;
4716     break;
4717   case MVT::f64:
4718     LC = RTLIB::REM_F64;
4719     break;
4720   }
4721
4722   ArgListTy Args;
4723   Args.reserve(I->getNumOperands());
4724
4725   // Populate the argument list.
4726   for (auto &Arg : I->operands()) {
4727     ArgListEntry Entry;
4728     Entry.Val = Arg;
4729     Entry.Ty = Arg->getType();
4730     Args.push_back(Entry);
4731   }
4732
4733   CallLoweringInfo CLI;
4734   MCContext &Ctx = MF->getContext();
4735   CLI.setCallee(DL, Ctx, TLI.getLibcallCallingConv(LC), I->getType(),
4736                 TLI.getLibcallName(LC), std::move(Args));
4737   if (!lowerCallTo(CLI))
4738     return false;
4739   updateValueMap(I, CLI.ResultReg);
4740   return true;
4741 }
4742
4743 bool AArch64FastISel::selectSDiv(const Instruction *I) {
4744   MVT VT;
4745   if (!isTypeLegal(I->getType(), VT))
4746     return false;
4747
4748   if (!isa<ConstantInt>(I->getOperand(1)))
4749     return selectBinaryOp(I, ISD::SDIV);
4750
4751   const APInt &C = cast<ConstantInt>(I->getOperand(1))->getValue();
4752   if ((VT != MVT::i32 && VT != MVT::i64) || !C ||
4753       !(C.isPowerOf2() || (-C).isPowerOf2()))
4754     return selectBinaryOp(I, ISD::SDIV);
4755
4756   unsigned Lg2 = C.countTrailingZeros();
4757   unsigned Src0Reg = getRegForValue(I->getOperand(0));
4758   if (!Src0Reg)
4759     return false;
4760   bool Src0IsKill = hasTrivialKill(I->getOperand(0));
4761
4762   if (cast<BinaryOperator>(I)->isExact()) {
4763     unsigned ResultReg = emitASR_ri(VT, VT, Src0Reg, Src0IsKill, Lg2);
4764     if (!ResultReg)
4765       return false;
4766     updateValueMap(I, ResultReg);
4767     return true;
4768   }
4769
4770   int64_t Pow2MinusOne = (1ULL << Lg2) - 1;
4771   unsigned AddReg = emitAdd_ri_(VT, Src0Reg, /*IsKill=*/false, Pow2MinusOne);
4772   if (!AddReg)
4773     return false;
4774
4775   // (Src0 < 0) ? Pow2 - 1 : 0;
4776   if (!emitICmp_ri(VT, Src0Reg, /*IsKill=*/false, 0))
4777     return false;
4778
4779   unsigned SelectOpc;
4780   const TargetRegisterClass *RC;
4781   if (VT == MVT::i64) {
4782     SelectOpc = AArch64::CSELXr;
4783     RC = &AArch64::GPR64RegClass;
4784   } else {
4785     SelectOpc = AArch64::CSELWr;
4786     RC = &AArch64::GPR32RegClass;
4787   }
4788   unsigned SelectReg =
4789       fastEmitInst_rri(SelectOpc, RC, AddReg, /*IsKill=*/true, Src0Reg,
4790                        Src0IsKill, AArch64CC::LT);
4791   if (!SelectReg)
4792     return false;
4793
4794   // Divide by Pow2 --> ashr. If we're dividing by a negative value we must also
4795   // negate the result.
4796   unsigned ZeroReg = (VT == MVT::i64) ? AArch64::XZR : AArch64::WZR;
4797   unsigned ResultReg;
4798   if (C.isNegative())
4799     ResultReg = emitAddSub_rs(/*UseAdd=*/false, VT, ZeroReg, /*IsKill=*/true,
4800                               SelectReg, /*IsKill=*/true, AArch64_AM::ASR, Lg2);
4801   else
4802     ResultReg = emitASR_ri(VT, VT, SelectReg, /*IsKill=*/true, Lg2);
4803
4804   if (!ResultReg)
4805     return false;
4806
4807   updateValueMap(I, ResultReg);
4808   return true;
4809 }
4810
4811 /// This is mostly a copy of the existing FastISel getRegForGEPIndex code. We
4812 /// have to duplicate it for AArch64, because otherwise we would fail during the
4813 /// sign-extend emission.
4814 std::pair<unsigned, bool> AArch64FastISel::getRegForGEPIndex(const Value *Idx) {
4815   unsigned IdxN = getRegForValue(Idx);
4816   if (IdxN == 0)
4817     // Unhandled operand. Halt "fast" selection and bail.
4818     return std::pair<unsigned, bool>(0, false);
4819
4820   bool IdxNIsKill = hasTrivialKill(Idx);
4821
4822   // If the index is smaller or larger than intptr_t, truncate or extend it.
4823   MVT PtrVT = TLI.getPointerTy(DL);
4824   EVT IdxVT = EVT::getEVT(Idx->getType(), /*HandleUnknown=*/false);
4825   if (IdxVT.bitsLT(PtrVT)) {
4826     IdxN = emitIntExt(IdxVT.getSimpleVT(), IdxN, PtrVT, /*IsZExt=*/false);
4827     IdxNIsKill = true;
4828   } else if (IdxVT.bitsGT(PtrVT))
4829     llvm_unreachable("AArch64 FastISel doesn't support types larger than i64");
4830   return std::pair<unsigned, bool>(IdxN, IdxNIsKill);
4831 }
4832
4833 /// This is mostly a copy of the existing FastISel GEP code, but we have to
4834 /// duplicate it for AArch64, because otherwise we would bail out even for
4835 /// simple cases. This is because the standard fastEmit functions don't cover
4836 /// MUL at all and ADD is lowered very inefficientily.
4837 bool AArch64FastISel::selectGetElementPtr(const Instruction *I) {
4838   unsigned N = getRegForValue(I->getOperand(0));
4839   if (!N)
4840     return false;
4841   bool NIsKill = hasTrivialKill(I->getOperand(0));
4842
4843   // Keep a running tab of the total offset to coalesce multiple N = N + Offset
4844   // into a single N = N + TotalOffset.
4845   uint64_t TotalOffs = 0;
4846   MVT VT = TLI.getPointerTy(DL);
4847   for (gep_type_iterator GTI = gep_type_begin(I), E = gep_type_end(I);
4848        GTI != E; ++GTI) {
4849     const Value *Idx = GTI.getOperand();
4850     if (auto *StTy = dyn_cast<StructType>(*GTI)) {
4851       unsigned Field = cast<ConstantInt>(Idx)->getZExtValue();
4852       // N = N + Offset
4853       if (Field)
4854         TotalOffs += DL.getStructLayout(StTy)->getElementOffset(Field);
4855     } else {
4856       Type *Ty = GTI.getIndexedType();
4857
4858       // If this is a constant subscript, handle it quickly.
4859       if (const auto *CI = dyn_cast<ConstantInt>(Idx)) {
4860         if (CI->isZero())
4861           continue;
4862         // N = N + Offset
4863         TotalOffs +=
4864             DL.getTypeAllocSize(Ty) * cast<ConstantInt>(CI)->getSExtValue();
4865         continue;
4866       }
4867       if (TotalOffs) {
4868         N = emitAdd_ri_(VT, N, NIsKill, TotalOffs);
4869         if (!N)
4870           return false;
4871         NIsKill = true;
4872         TotalOffs = 0;
4873       }
4874
4875       // N = N + Idx * ElementSize;
4876       uint64_t ElementSize = DL.getTypeAllocSize(Ty);
4877       std::pair<unsigned, bool> Pair = getRegForGEPIndex(Idx);
4878       unsigned IdxN = Pair.first;
4879       bool IdxNIsKill = Pair.second;
4880       if (!IdxN)
4881         return false;
4882
4883       if (ElementSize != 1) {
4884         unsigned C = fastEmit_i(VT, VT, ISD::Constant, ElementSize);
4885         if (!C)
4886           return false;
4887         IdxN = emitMul_rr(VT, IdxN, IdxNIsKill, C, true);
4888         if (!IdxN)
4889           return false;
4890         IdxNIsKill = true;
4891       }
4892       N = fastEmit_rr(VT, VT, ISD::ADD, N, NIsKill, IdxN, IdxNIsKill);
4893       if (!N)
4894         return false;
4895     }
4896   }
4897   if (TotalOffs) {
4898     N = emitAdd_ri_(VT, N, NIsKill, TotalOffs);
4899     if (!N)
4900       return false;
4901   }
4902   updateValueMap(I, N);
4903   return true;
4904 }
4905
4906 bool AArch64FastISel::fastSelectInstruction(const Instruction *I) {
4907   switch (I->getOpcode()) {
4908   default:
4909     break;
4910   case Instruction::Add:
4911   case Instruction::Sub:
4912     return selectAddSub(I);
4913   case Instruction::Mul:
4914     return selectMul(I);
4915   case Instruction::SDiv:
4916     return selectSDiv(I);
4917   case Instruction::SRem:
4918     if (!selectBinaryOp(I, ISD::SREM))
4919       return selectRem(I, ISD::SREM);
4920     return true;
4921   case Instruction::URem:
4922     if (!selectBinaryOp(I, ISD::UREM))
4923       return selectRem(I, ISD::UREM);
4924     return true;
4925   case Instruction::Shl:
4926   case Instruction::LShr:
4927   case Instruction::AShr:
4928     return selectShift(I);
4929   case Instruction::And:
4930   case Instruction::Or:
4931   case Instruction::Xor:
4932     return selectLogicalOp(I);
4933   case Instruction::Br:
4934     return selectBranch(I);
4935   case Instruction::IndirectBr:
4936     return selectIndirectBr(I);
4937   case Instruction::BitCast:
4938     if (!FastISel::selectBitCast(I))
4939       return selectBitCast(I);
4940     return true;
4941   case Instruction::FPToSI:
4942     if (!selectCast(I, ISD::FP_TO_SINT))
4943       return selectFPToInt(I, /*Signed=*/true);
4944     return true;
4945   case Instruction::FPToUI:
4946     return selectFPToInt(I, /*Signed=*/false);
4947   case Instruction::ZExt:
4948   case Instruction::SExt:
4949     return selectIntExt(I);
4950   case Instruction::Trunc:
4951     if (!selectCast(I, ISD::TRUNCATE))
4952       return selectTrunc(I);
4953     return true;
4954   case Instruction::FPExt:
4955     return selectFPExt(I);
4956   case Instruction::FPTrunc:
4957     return selectFPTrunc(I);
4958   case Instruction::SIToFP:
4959     if (!selectCast(I, ISD::SINT_TO_FP))
4960       return selectIntToFP(I, /*Signed=*/true);
4961     return true;
4962   case Instruction::UIToFP:
4963     return selectIntToFP(I, /*Signed=*/false);
4964   case Instruction::Load:
4965     return selectLoad(I);
4966   case Instruction::Store:
4967     return selectStore(I);
4968   case Instruction::FCmp:
4969   case Instruction::ICmp:
4970     return selectCmp(I);
4971   case Instruction::Select:
4972     return selectSelect(I);
4973   case Instruction::Ret:
4974     return selectRet(I);
4975   case Instruction::FRem:
4976     return selectFRem(I);
4977   case Instruction::GetElementPtr:
4978     return selectGetElementPtr(I);
4979   }
4980
4981   // fall-back to target-independent instruction selection.
4982   return selectOperator(I, I->getOpcode());
4983   // Silence warnings.
4984   (void)&CC_AArch64_DarwinPCS_VarArg;
4985 }
4986
4987 namespace llvm {
4988 llvm::FastISel *AArch64::createFastISel(FunctionLoweringInfo &FuncInfo,
4989                                         const TargetLibraryInfo *LibInfo) {
4990   return new AArch64FastISel(FuncInfo, LibInfo);
4991 }
4992 }