AArch64/ARM64: Port NEON post-increment load/store with 2/3/4 vectors to ARM64 backend.
[oota-llvm.git] / lib / Target / ARM64 / ARM64ISelDAGToDAG.cpp
1 //===-- ARM64ISelDAGToDAG.cpp - A dag to dag inst selector for ARM64 ------===//
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 an instruction selector for the ARM64 target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "ARM64TargetMachine.h"
15 #include "MCTargetDesc/ARM64AddressingModes.h"
16 #include "llvm/ADT/APSInt.h"
17 #include "llvm/CodeGen/SelectionDAGISel.h"
18 #include "llvm/IR/Function.h" // To access function attributes.
19 #include "llvm/IR/GlobalValue.h"
20 #include "llvm/IR/Intrinsics.h"
21 #include "llvm/Support/Debug.h"
22 #include "llvm/Support/ErrorHandling.h"
23 #include "llvm/Support/MathExtras.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 #define DEBUG_TYPE "arm64-isel"
29
30 //===--------------------------------------------------------------------===//
31 /// ARM64DAGToDAGISel - ARM64 specific code to select ARM64 machine
32 /// instructions for SelectionDAG operations.
33 ///
34 namespace {
35
36 class ARM64DAGToDAGISel : public SelectionDAGISel {
37   ARM64TargetMachine &TM;
38
39   /// Subtarget - Keep a pointer to the ARM64Subtarget around so that we can
40   /// make the right decision when generating code for different targets.
41   const ARM64Subtarget *Subtarget;
42
43   bool ForCodeSize;
44
45 public:
46   explicit ARM64DAGToDAGISel(ARM64TargetMachine &tm, CodeGenOpt::Level OptLevel)
47       : SelectionDAGISel(tm, OptLevel), TM(tm),
48         Subtarget(&TM.getSubtarget<ARM64Subtarget>()), ForCodeSize(false) {}
49
50   const char *getPassName() const override {
51     return "ARM64 Instruction Selection";
52   }
53
54   bool runOnMachineFunction(MachineFunction &MF) override {
55     AttributeSet FnAttrs = MF.getFunction()->getAttributes();
56     ForCodeSize =
57         FnAttrs.hasAttribute(AttributeSet::FunctionIndex,
58                              Attribute::OptimizeForSize) ||
59         FnAttrs.hasAttribute(AttributeSet::FunctionIndex, Attribute::MinSize);
60     return SelectionDAGISel::runOnMachineFunction(MF);
61   }
62
63   SDNode *Select(SDNode *Node) override;
64
65   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
66   /// inline asm expressions.
67   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
68                                     char ConstraintCode,
69                                     std::vector<SDValue> &OutOps) override;
70
71   SDNode *SelectMLAV64LaneV128(SDNode *N);
72   SDNode *SelectMULLV64LaneV128(unsigned IntNo, SDNode *N);
73   bool SelectArithExtendedRegister(SDValue N, SDValue &Reg, SDValue &Shift);
74   bool SelectArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
75   bool SelectNegArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
76   bool SelectArithShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
77     return SelectShiftedRegister(N, false, Reg, Shift);
78   }
79   bool SelectLogicalShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
80     return SelectShiftedRegister(N, true, Reg, Shift);
81   }
82   bool SelectAddrModeIndexed8(SDValue N, SDValue &Base, SDValue &OffImm) {
83     return SelectAddrModeIndexed(N, 1, Base, OffImm);
84   }
85   bool SelectAddrModeIndexed16(SDValue N, SDValue &Base, SDValue &OffImm) {
86     return SelectAddrModeIndexed(N, 2, Base, OffImm);
87   }
88   bool SelectAddrModeIndexed32(SDValue N, SDValue &Base, SDValue &OffImm) {
89     return SelectAddrModeIndexed(N, 4, Base, OffImm);
90   }
91   bool SelectAddrModeIndexed64(SDValue N, SDValue &Base, SDValue &OffImm) {
92     return SelectAddrModeIndexed(N, 8, Base, OffImm);
93   }
94   bool SelectAddrModeIndexed128(SDValue N, SDValue &Base, SDValue &OffImm) {
95     return SelectAddrModeIndexed(N, 16, Base, OffImm);
96   }
97   bool SelectAddrModeUnscaled8(SDValue N, SDValue &Base, SDValue &OffImm) {
98     return SelectAddrModeUnscaled(N, 1, Base, OffImm);
99   }
100   bool SelectAddrModeUnscaled16(SDValue N, SDValue &Base, SDValue &OffImm) {
101     return SelectAddrModeUnscaled(N, 2, Base, OffImm);
102   }
103   bool SelectAddrModeUnscaled32(SDValue N, SDValue &Base, SDValue &OffImm) {
104     return SelectAddrModeUnscaled(N, 4, Base, OffImm);
105   }
106   bool SelectAddrModeUnscaled64(SDValue N, SDValue &Base, SDValue &OffImm) {
107     return SelectAddrModeUnscaled(N, 8, Base, OffImm);
108   }
109   bool SelectAddrModeUnscaled128(SDValue N, SDValue &Base, SDValue &OffImm) {
110     return SelectAddrModeUnscaled(N, 16, Base, OffImm);
111   }
112
113   bool SelectAddrModeRO8(SDValue N, SDValue &Base, SDValue &Offset,
114                          SDValue &Imm) {
115     return SelectAddrModeRO(N, 1, Base, Offset, Imm);
116   }
117   bool SelectAddrModeRO16(SDValue N, SDValue &Base, SDValue &Offset,
118                           SDValue &Imm) {
119     return SelectAddrModeRO(N, 2, Base, Offset, Imm);
120   }
121   bool SelectAddrModeRO32(SDValue N, SDValue &Base, SDValue &Offset,
122                           SDValue &Imm) {
123     return SelectAddrModeRO(N, 4, Base, Offset, Imm);
124   }
125   bool SelectAddrModeRO64(SDValue N, SDValue &Base, SDValue &Offset,
126                           SDValue &Imm) {
127     return SelectAddrModeRO(N, 8, Base, Offset, Imm);
128   }
129   bool SelectAddrModeRO128(SDValue N, SDValue &Base, SDValue &Offset,
130                            SDValue &Imm) {
131     return SelectAddrModeRO(N, 16, Base, Offset, Imm);
132   }
133   bool SelectAddrModeNoIndex(SDValue N, SDValue &Val);
134
135   /// Form sequences of consecutive 64/128-bit registers for use in NEON
136   /// instructions making use of a vector-list (e.g. ldN, tbl). Vecs must have
137   /// between 1 and 4 elements. If it contains a single element that is returned
138   /// unchanged; otherwise a REG_SEQUENCE value is returned.
139   SDValue createDTuple(ArrayRef<SDValue> Vecs);
140   SDValue createQTuple(ArrayRef<SDValue> Vecs);
141
142   /// Generic helper for the createDTuple/createQTuple
143   /// functions. Those should almost always be called instead.
144   SDValue createTuple(ArrayRef<SDValue> Vecs, unsigned RegClassIDs[],
145                       unsigned SubRegs[]);
146
147   SDNode *SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc, bool isExt);
148
149   SDNode *SelectIndexedLoad(SDNode *N, bool &Done);
150
151   SDNode *SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
152                      unsigned SubRegIdx);
153   SDNode *SelectPostLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
154                          unsigned SubRegIdx);
155   SDNode *SelectLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
156   SDNode *SelectPostLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
157
158   SDNode *SelectStore(SDNode *N, unsigned NumVecs, unsigned Opc);
159   SDNode *SelectPostStore(SDNode *N, unsigned NumVecs, unsigned Opc);
160   SDNode *SelectStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
161   SDNode *SelectPostStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
162
163   SDNode *SelectSIMDAddSubNarrowing(unsigned IntNo, SDNode *Node);
164   SDNode *SelectSIMDXtnNarrowing(unsigned IntNo, SDNode *Node);
165
166   SDNode *SelectBitfieldExtractOp(SDNode *N);
167   SDNode *SelectBitfieldInsertOp(SDNode *N);
168
169   SDNode *SelectLIBM(SDNode *N);
170
171 // Include the pieces autogenerated from the target description.
172 #include "ARM64GenDAGISel.inc"
173
174 private:
175   bool SelectShiftedRegister(SDValue N, bool AllowROR, SDValue &Reg,
176                              SDValue &Shift);
177   bool SelectAddrModeIndexed(SDValue N, unsigned Size, SDValue &Base,
178                              SDValue &OffImm);
179   bool SelectAddrModeUnscaled(SDValue N, unsigned Size, SDValue &Base,
180                               SDValue &OffImm);
181   bool SelectAddrModeRO(SDValue N, unsigned Size, SDValue &Base,
182                         SDValue &Offset, SDValue &Imm);
183   bool isWorthFolding(SDValue V) const;
184   bool SelectExtendedSHL(SDValue N, unsigned Size, SDValue &Offset,
185                          SDValue &Imm);
186
187   template<unsigned RegWidth>
188   bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
189     return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
190   }
191
192   bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos, unsigned Width);
193 };
194 } // end anonymous namespace
195
196 /// isIntImmediate - This method tests to see if the node is a constant
197 /// operand. If so Imm will receive the 32-bit value.
198 static bool isIntImmediate(const SDNode *N, uint64_t &Imm) {
199   if (const ConstantSDNode *C = dyn_cast<const ConstantSDNode>(N)) {
200     Imm = C->getZExtValue();
201     return true;
202   }
203   return false;
204 }
205
206 // isIntImmediate - This method tests to see if a constant operand.
207 // If so Imm will receive the value.
208 static bool isIntImmediate(SDValue N, uint64_t &Imm) {
209   return isIntImmediate(N.getNode(), Imm);
210 }
211
212 // isOpcWithIntImmediate - This method tests to see if the node is a specific
213 // opcode and that it has a immediate integer right operand.
214 // If so Imm will receive the 32 bit value.
215 static bool isOpcWithIntImmediate(const SDNode *N, unsigned Opc,
216                                   uint64_t &Imm) {
217   return N->getOpcode() == Opc &&
218          isIntImmediate(N->getOperand(1).getNode(), Imm);
219 }
220
221 bool ARM64DAGToDAGISel::SelectAddrModeNoIndex(SDValue N, SDValue &Val) {
222   EVT ValTy = N.getValueType();
223   if (ValTy != MVT::i64)
224     return false;
225   Val = N;
226   return true;
227 }
228
229 bool ARM64DAGToDAGISel::SelectInlineAsmMemoryOperand(
230     const SDValue &Op, char ConstraintCode, std::vector<SDValue> &OutOps) {
231   assert(ConstraintCode == 'm' && "unexpected asm memory constraint");
232   // Require the address to be in a register.  That is safe for all ARM64
233   // variants and it is hard to do anything much smarter without knowing
234   // how the operand is used.
235   OutOps.push_back(Op);
236   return false;
237 }
238
239 /// SelectArithImmed - Select an immediate value that can be represented as
240 /// a 12-bit value shifted left by either 0 or 12.  If so, return true with
241 /// Val set to the 12-bit value and Shift set to the shifter operand.
242 bool ARM64DAGToDAGISel::SelectArithImmed(SDValue N, SDValue &Val,
243                                          SDValue &Shift) {
244   // This function is called from the addsub_shifted_imm ComplexPattern,
245   // which lists [imm] as the list of opcode it's interested in, however
246   // we still need to check whether the operand is actually an immediate
247   // here because the ComplexPattern opcode list is only used in
248   // root-level opcode matching.
249   if (!isa<ConstantSDNode>(N.getNode()))
250     return false;
251
252   uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
253   unsigned ShiftAmt;
254
255   if (Immed >> 12 == 0) {
256     ShiftAmt = 0;
257   } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
258     ShiftAmt = 12;
259     Immed = Immed >> 12;
260   } else
261     return false;
262
263   unsigned ShVal = ARM64_AM::getShifterImm(ARM64_AM::LSL, ShiftAmt);
264   Val = CurDAG->getTargetConstant(Immed, MVT::i32);
265   Shift = CurDAG->getTargetConstant(ShVal, MVT::i32);
266   return true;
267 }
268
269 /// SelectNegArithImmed - As above, but negates the value before trying to
270 /// select it.
271 bool ARM64DAGToDAGISel::SelectNegArithImmed(SDValue N, SDValue &Val,
272                                             SDValue &Shift) {
273   // This function is called from the addsub_shifted_imm ComplexPattern,
274   // which lists [imm] as the list of opcode it's interested in, however
275   // we still need to check whether the operand is actually an immediate
276   // here because the ComplexPattern opcode list is only used in
277   // root-level opcode matching.
278   if (!isa<ConstantSDNode>(N.getNode()))
279     return false;
280
281   // The immediate operand must be a 24-bit zero-extended immediate.
282   uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
283
284   // This negation is almost always valid, but "cmp wN, #0" and "cmn wN, #0"
285   // have the opposite effect on the C flag, so this pattern mustn't match under
286   // those circumstances.
287   if (Immed == 0)
288     return false;
289
290   if (N.getValueType() == MVT::i32)
291     Immed = ~((uint32_t)Immed) + 1;
292   else
293     Immed = ~Immed + 1ULL;
294   if (Immed & 0xFFFFFFFFFF000000ULL)
295     return false;
296
297   Immed &= 0xFFFFFFULL;
298   return SelectArithImmed(CurDAG->getConstant(Immed, MVT::i32), Val, Shift);
299 }
300
301 /// getShiftTypeForNode - Translate a shift node to the corresponding
302 /// ShiftType value.
303 static ARM64_AM::ShiftType getShiftTypeForNode(SDValue N) {
304   switch (N.getOpcode()) {
305   default:
306     return ARM64_AM::InvalidShift;
307   case ISD::SHL:
308     return ARM64_AM::LSL;
309   case ISD::SRL:
310     return ARM64_AM::LSR;
311   case ISD::SRA:
312     return ARM64_AM::ASR;
313   case ISD::ROTR:
314     return ARM64_AM::ROR;
315   }
316 }
317
318 /// \brief Determine wether it is worth to fold V into an extended register.
319 bool ARM64DAGToDAGISel::isWorthFolding(SDValue V) const {
320   // it hurts if the a value is used at least twice, unless we are optimizing
321   // for code size.
322   if (ForCodeSize || V.hasOneUse())
323     return true;
324   return false;
325 }
326
327 /// SelectShiftedRegister - Select a "shifted register" operand.  If the value
328 /// is not shifted, set the Shift operand to default of "LSL 0".  The logical
329 /// instructions allow the shifted register to be rotated, but the arithmetic
330 /// instructions do not.  The AllowROR parameter specifies whether ROR is
331 /// supported.
332 bool ARM64DAGToDAGISel::SelectShiftedRegister(SDValue N, bool AllowROR,
333                                               SDValue &Reg, SDValue &Shift) {
334   ARM64_AM::ShiftType ShType = getShiftTypeForNode(N);
335   if (ShType == ARM64_AM::InvalidShift)
336     return false;
337   if (!AllowROR && ShType == ARM64_AM::ROR)
338     return false;
339
340   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
341     unsigned BitSize = N.getValueType().getSizeInBits();
342     unsigned Val = RHS->getZExtValue() & (BitSize - 1);
343     unsigned ShVal = ARM64_AM::getShifterImm(ShType, Val);
344
345     Reg = N.getOperand(0);
346     Shift = CurDAG->getTargetConstant(ShVal, MVT::i32);
347     return isWorthFolding(N);
348   }
349
350   return false;
351 }
352
353 /// getExtendTypeForNode - Translate an extend node to the corresponding
354 /// ExtendType value.
355 static ARM64_AM::ExtendType getExtendTypeForNode(SDValue N,
356                                                  bool IsLoadStore = false) {
357   if (N.getOpcode() == ISD::SIGN_EXTEND ||
358       N.getOpcode() == ISD::SIGN_EXTEND_INREG) {
359     EVT SrcVT;
360     if (N.getOpcode() == ISD::SIGN_EXTEND_INREG)
361       SrcVT = cast<VTSDNode>(N.getOperand(1))->getVT();
362     else
363       SrcVT = N.getOperand(0).getValueType();
364
365     if (!IsLoadStore && SrcVT == MVT::i8)
366       return ARM64_AM::SXTB;
367     else if (!IsLoadStore && SrcVT == MVT::i16)
368       return ARM64_AM::SXTH;
369     else if (SrcVT == MVT::i32)
370       return ARM64_AM::SXTW;
371     else if (SrcVT == MVT::i64)
372       return ARM64_AM::SXTX;
373
374     return ARM64_AM::InvalidExtend;
375   } else if (N.getOpcode() == ISD::ZERO_EXTEND ||
376              N.getOpcode() == ISD::ANY_EXTEND) {
377     EVT SrcVT = N.getOperand(0).getValueType();
378     if (!IsLoadStore && SrcVT == MVT::i8)
379       return ARM64_AM::UXTB;
380     else if (!IsLoadStore && SrcVT == MVT::i16)
381       return ARM64_AM::UXTH;
382     else if (SrcVT == MVT::i32)
383       return ARM64_AM::UXTW;
384     else if (SrcVT == MVT::i64)
385       return ARM64_AM::UXTX;
386
387     return ARM64_AM::InvalidExtend;
388   } else if (N.getOpcode() == ISD::AND) {
389     ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
390     if (!CSD)
391       return ARM64_AM::InvalidExtend;
392     uint64_t AndMask = CSD->getZExtValue();
393
394     switch (AndMask) {
395     default:
396       return ARM64_AM::InvalidExtend;
397     case 0xFF:
398       return !IsLoadStore ? ARM64_AM::UXTB : ARM64_AM::InvalidExtend;
399     case 0xFFFF:
400       return !IsLoadStore ? ARM64_AM::UXTH : ARM64_AM::InvalidExtend;
401     case 0xFFFFFFFF:
402       return ARM64_AM::UXTW;
403     }
404   }
405
406   return ARM64_AM::InvalidExtend;
407 }
408
409 // Helper for SelectMLAV64LaneV128 - Recognize high lane extracts.
410 static bool checkHighLaneIndex(SDNode *DL, SDValue &LaneOp, int &LaneIdx) {
411   if (DL->getOpcode() != ARM64ISD::DUPLANE16 &&
412       DL->getOpcode() != ARM64ISD::DUPLANE32)
413     return false;
414
415   SDValue SV = DL->getOperand(0);
416   if (SV.getOpcode() != ISD::INSERT_SUBVECTOR)
417     return false;
418
419   SDValue EV = SV.getOperand(1);
420   if (EV.getOpcode() != ISD::EXTRACT_SUBVECTOR)
421     return false;
422
423   ConstantSDNode *DLidx = cast<ConstantSDNode>(DL->getOperand(1).getNode());
424   ConstantSDNode *EVidx = cast<ConstantSDNode>(EV.getOperand(1).getNode());
425   LaneIdx = DLidx->getSExtValue() + EVidx->getSExtValue();
426   LaneOp = EV.getOperand(0);
427
428   return true;
429 }
430
431 // Helper for SelectOpcV64LaneV128 - Recogzine operatinos where one operand is a
432 // high lane extract.
433 static bool checkV64LaneV128(SDValue Op0, SDValue Op1, SDValue &StdOp,
434                              SDValue &LaneOp, int &LaneIdx) {
435
436   if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx)) {
437     std::swap(Op0, Op1);
438     if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx))
439       return false;
440   }
441   StdOp = Op1;
442   return true;
443 }
444
445 /// SelectMLAV64LaneV128 - ARM64 supports vector MLAs where one multiplicand is
446 /// a lane in the upper half of a 128-bit vector.  Recognize and select this so
447 /// that we don't emit unnecessary lane extracts.
448 SDNode *ARM64DAGToDAGISel::SelectMLAV64LaneV128(SDNode *N) {
449   SDValue Op0 = N->getOperand(0);
450   SDValue Op1 = N->getOperand(1);
451   SDValue MLAOp1;   // Will hold ordinary multiplicand for MLA.
452   SDValue MLAOp2;   // Will hold lane-accessed multiplicand for MLA.
453   int LaneIdx = -1; // Will hold the lane index.
454
455   if (Op1.getOpcode() != ISD::MUL ||
456       !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
457                         LaneIdx)) {
458     std::swap(Op0, Op1);
459     if (Op1.getOpcode() != ISD::MUL ||
460         !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
461                           LaneIdx))
462       return nullptr;
463   }
464
465   SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, MVT::i64);
466
467   SDValue Ops[] = { Op0, MLAOp1, MLAOp2, LaneIdxVal };
468
469   unsigned MLAOpc = ~0U;
470
471   switch (N->getSimpleValueType(0).SimpleTy) {
472   default:
473     llvm_unreachable("Unrecognized MLA.");
474   case MVT::v4i16:
475     MLAOpc = ARM64::MLAv4i16_indexed;
476     break;
477   case MVT::v8i16:
478     MLAOpc = ARM64::MLAv8i16_indexed;
479     break;
480   case MVT::v2i32:
481     MLAOpc = ARM64::MLAv2i32_indexed;
482     break;
483   case MVT::v4i32:
484     MLAOpc = ARM64::MLAv4i32_indexed;
485     break;
486   }
487
488   return CurDAG->getMachineNode(MLAOpc, SDLoc(N), N->getValueType(0), Ops);
489 }
490
491 SDNode *ARM64DAGToDAGISel::SelectMULLV64LaneV128(unsigned IntNo, SDNode *N) {
492   SDValue SMULLOp0;
493   SDValue SMULLOp1;
494   int LaneIdx;
495
496   if (!checkV64LaneV128(N->getOperand(1), N->getOperand(2), SMULLOp0, SMULLOp1,
497                         LaneIdx))
498     return nullptr;
499
500   SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, MVT::i64);
501
502   SDValue Ops[] = { SMULLOp0, SMULLOp1, LaneIdxVal };
503
504   unsigned SMULLOpc = ~0U;
505
506   if (IntNo == Intrinsic::arm64_neon_smull) {
507     switch (N->getSimpleValueType(0).SimpleTy) {
508     default:
509       llvm_unreachable("Unrecognized SMULL.");
510     case MVT::v4i32:
511       SMULLOpc = ARM64::SMULLv4i16_indexed;
512       break;
513     case MVT::v2i64:
514       SMULLOpc = ARM64::SMULLv2i32_indexed;
515       break;
516     }
517   } else if (IntNo == Intrinsic::arm64_neon_umull) {
518     switch (N->getSimpleValueType(0).SimpleTy) {
519     default:
520       llvm_unreachable("Unrecognized SMULL.");
521     case MVT::v4i32:
522       SMULLOpc = ARM64::UMULLv4i16_indexed;
523       break;
524     case MVT::v2i64:
525       SMULLOpc = ARM64::UMULLv2i32_indexed;
526       break;
527     }
528   } else
529     llvm_unreachable("Unrecognized intrinsic.");
530
531   return CurDAG->getMachineNode(SMULLOpc, SDLoc(N), N->getValueType(0), Ops);
532 }
533
534 /// SelectArithExtendedRegister - Select a "extended register" operand.  This
535 /// operand folds in an extend followed by an optional left shift.
536 bool ARM64DAGToDAGISel::SelectArithExtendedRegister(SDValue N, SDValue &Reg,
537                                                     SDValue &Shift) {
538   unsigned ShiftVal = 0;
539   ARM64_AM::ExtendType Ext;
540
541   if (N.getOpcode() == ISD::SHL) {
542     ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
543     if (!CSD)
544       return false;
545     ShiftVal = CSD->getZExtValue();
546     if (ShiftVal > 4)
547       return false;
548
549     Ext = getExtendTypeForNode(N.getOperand(0));
550     if (Ext == ARM64_AM::InvalidExtend)
551       return false;
552
553     Reg = N.getOperand(0).getOperand(0);
554   } else {
555     Ext = getExtendTypeForNode(N);
556     if (Ext == ARM64_AM::InvalidExtend)
557       return false;
558
559     Reg = N.getOperand(0);
560   }
561
562   // ARM64 mandates that the RHS of the operation must use the smallest
563   // register classs that could contain the size being extended from.  Thus,
564   // if we're folding a (sext i8), we need the RHS to be a GPR32, even though
565   // there might not be an actual 32-bit value in the program.  We can
566   // (harmlessly) synthesize one by injected an EXTRACT_SUBREG here.
567   if (Reg.getValueType() == MVT::i64 && Ext != ARM64_AM::UXTX &&
568       Ext != ARM64_AM::SXTX) {
569     SDValue SubReg = CurDAG->getTargetConstant(ARM64::sub_32, MVT::i32);
570     MachineSDNode *Node = CurDAG->getMachineNode(
571         TargetOpcode::EXTRACT_SUBREG, SDLoc(N), MVT::i32, Reg, SubReg);
572     Reg = SDValue(Node, 0);
573   }
574
575   Shift = CurDAG->getTargetConstant(getArithExtendImm(Ext, ShiftVal), MVT::i32);
576   return isWorthFolding(N);
577 }
578
579 /// SelectAddrModeIndexed - Select a "register plus scaled unsigned 12-bit
580 /// immediate" address.  The "Size" argument is the size in bytes of the memory
581 /// reference, which determines the scale.
582 bool ARM64DAGToDAGISel::SelectAddrModeIndexed(SDValue N, unsigned Size,
583                                               SDValue &Base, SDValue &OffImm) {
584   const TargetLowering *TLI = getTargetLowering();
585   if (N.getOpcode() == ISD::FrameIndex) {
586     int FI = cast<FrameIndexSDNode>(N)->getIndex();
587     Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy());
588     OffImm = CurDAG->getTargetConstant(0, MVT::i64);
589     return true;
590   }
591
592   if (N.getOpcode() == ARM64ISD::ADDlow) {
593     GlobalAddressSDNode *GAN =
594         dyn_cast<GlobalAddressSDNode>(N.getOperand(1).getNode());
595     Base = N.getOperand(0);
596     OffImm = N.getOperand(1);
597     if (!GAN)
598       return true;
599
600     const GlobalValue *GV = GAN->getGlobal();
601     unsigned Alignment = GV->getAlignment();
602     const DataLayout *DL = TLI->getDataLayout();
603     if (Alignment == 0 && !Subtarget->isTargetDarwin())
604       Alignment = DL->getABITypeAlignment(GV->getType()->getElementType());
605
606     if (Alignment >= Size)
607       return true;
608   }
609
610   if (CurDAG->isBaseWithConstantOffset(N)) {
611     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
612       int64_t RHSC = (int64_t)RHS->getZExtValue();
613       unsigned Scale = Log2_32(Size);
614       if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
615         Base = N.getOperand(0);
616         if (Base.getOpcode() == ISD::FrameIndex) {
617           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
618           Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy());
619         }
620         OffImm = CurDAG->getTargetConstant(RHSC >> Scale, MVT::i64);
621         return true;
622       }
623     }
624   }
625
626   // Before falling back to our general case, check if the unscaled
627   // instructions can handle this. If so, that's preferable.
628   if (SelectAddrModeUnscaled(N, Size, Base, OffImm))
629     return false;
630
631   // Base only. The address will be materialized into a register before
632   // the memory is accessed.
633   //    add x0, Xbase, #offset
634   //    ldr x0, [x0]
635   Base = N;
636   OffImm = CurDAG->getTargetConstant(0, MVT::i64);
637   return true;
638 }
639
640 /// SelectAddrModeUnscaled - Select a "register plus unscaled signed 9-bit
641 /// immediate" address.  This should only match when there is an offset that
642 /// is not valid for a scaled immediate addressing mode.  The "Size" argument
643 /// is the size in bytes of the memory reference, which is needed here to know
644 /// what is valid for a scaled immediate.
645 bool ARM64DAGToDAGISel::SelectAddrModeUnscaled(SDValue N, unsigned Size,
646                                                SDValue &Base, SDValue &OffImm) {
647   if (!CurDAG->isBaseWithConstantOffset(N))
648     return false;
649   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
650     int64_t RHSC = RHS->getSExtValue();
651     // If the offset is valid as a scaled immediate, don't match here.
652     if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 &&
653         RHSC < (0x1000 << Log2_32(Size)))
654       return false;
655     if (RHSC >= -256 && RHSC < 256) {
656       Base = N.getOperand(0);
657       if (Base.getOpcode() == ISD::FrameIndex) {
658         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
659         const TargetLowering *TLI = getTargetLowering();
660         Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy());
661       }
662       OffImm = CurDAG->getTargetConstant(RHSC, MVT::i64);
663       return true;
664     }
665   }
666   return false;
667 }
668
669 static SDValue Widen(SelectionDAG *CurDAG, SDValue N) {
670   SDValue SubReg = CurDAG->getTargetConstant(ARM64::sub_32, MVT::i32);
671   SDValue ImpDef = SDValue(
672       CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, SDLoc(N), MVT::i64),
673       0);
674   MachineSDNode *Node = CurDAG->getMachineNode(
675       TargetOpcode::INSERT_SUBREG, SDLoc(N), MVT::i64, ImpDef, N, SubReg);
676   return SDValue(Node, 0);
677 }
678
679 static SDValue WidenIfNeeded(SelectionDAG *CurDAG, SDValue N) {
680   if (N.getValueType() == MVT::i32) {
681     return Widen(CurDAG, N);
682   }
683
684   return N;
685 }
686
687 /// \brief Check if the given SHL node (\p N), can be used to form an
688 /// extended register for an addressing mode.
689 bool ARM64DAGToDAGISel::SelectExtendedSHL(SDValue N, unsigned Size,
690                                           SDValue &Offset, SDValue &Imm) {
691   assert(N.getOpcode() == ISD::SHL && "Invalid opcode.");
692   ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
693   if (CSD && (CSD->getZExtValue() & 0x7) == CSD->getZExtValue()) {
694
695     ARM64_AM::ExtendType Ext = getExtendTypeForNode(N.getOperand(0), true);
696     if (Ext == ARM64_AM::InvalidExtend) {
697       Ext = ARM64_AM::UXTX;
698       Offset = WidenIfNeeded(CurDAG, N.getOperand(0));
699     } else {
700       Offset = WidenIfNeeded(CurDAG, N.getOperand(0).getOperand(0));
701     }
702
703     unsigned LegalShiftVal = Log2_32(Size);
704     unsigned ShiftVal = CSD->getZExtValue();
705
706     if (ShiftVal != 0 && ShiftVal != LegalShiftVal)
707       return false;
708
709     Imm = CurDAG->getTargetConstant(
710         ARM64_AM::getMemExtendImm(Ext, ShiftVal != 0), MVT::i32);
711     if (isWorthFolding(N))
712       return true;
713   }
714   return false;
715 }
716
717 bool ARM64DAGToDAGISel::SelectAddrModeRO(SDValue N, unsigned Size,
718                                          SDValue &Base, SDValue &Offset,
719                                          SDValue &Imm) {
720   if (N.getOpcode() != ISD::ADD)
721     return false;
722   SDValue LHS = N.getOperand(0);
723   SDValue RHS = N.getOperand(1);
724
725   // We don't want to match immediate adds here, because they are better lowered
726   // to the register-immediate addressing modes.
727   if (isa<ConstantSDNode>(LHS) || isa<ConstantSDNode>(RHS))
728     return false;
729
730   // Check if this particular node is reused in any non-memory related
731   // operation.  If yes, do not try to fold this node into the address
732   // computation, since the computation will be kept.
733   const SDNode *Node = N.getNode();
734   for (SDNode *UI : Node->uses()) {
735     if (!isa<MemSDNode>(*UI))
736       return false;
737   }
738
739   // Remember if it is worth folding N when it produces extended register.
740   bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
741
742   // Try to match a shifted extend on the RHS.
743   if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
744       SelectExtendedSHL(RHS, Size, Offset, Imm)) {
745     Base = LHS;
746     return true;
747   }
748
749   // Try to match a shifted extend on the LHS.
750   if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
751       SelectExtendedSHL(LHS, Size, Offset, Imm)) {
752     Base = RHS;
753     return true;
754   }
755
756   ARM64_AM::ExtendType Ext = ARM64_AM::UXTX;
757   // Try to match an unshifted extend on the LHS.
758   if (IsExtendedRegisterWorthFolding &&
759       (Ext = getExtendTypeForNode(LHS, true)) != ARM64_AM::InvalidExtend) {
760     Base = RHS;
761     Offset = WidenIfNeeded(CurDAG, LHS.getOperand(0));
762     Imm = CurDAG->getTargetConstant(ARM64_AM::getMemExtendImm(Ext, false),
763                                     MVT::i32);
764     if (isWorthFolding(LHS))
765       return true;
766   }
767
768   // Try to match an unshifted extend on the RHS.
769   if (IsExtendedRegisterWorthFolding &&
770       (Ext = getExtendTypeForNode(RHS, true)) != ARM64_AM::InvalidExtend) {
771     Base = LHS;
772     Offset = WidenIfNeeded(CurDAG, RHS.getOperand(0));
773     Imm = CurDAG->getTargetConstant(ARM64_AM::getMemExtendImm(Ext, false),
774                                     MVT::i32);
775     if (isWorthFolding(RHS))
776       return true;
777   }
778
779   // Match any non-shifted, non-extend, non-immediate add expression.
780   Base = LHS;
781   Offset = WidenIfNeeded(CurDAG, RHS);
782   Ext = ARM64_AM::UXTX;
783   Imm = CurDAG->getTargetConstant(ARM64_AM::getMemExtendImm(Ext, false),
784                                   MVT::i32);
785   // Reg1 + Reg2 is free: no check needed.
786   return true;
787 }
788
789 SDValue ARM64DAGToDAGISel::createDTuple(ArrayRef<SDValue> Regs) {
790   static unsigned RegClassIDs[] = { ARM64::DDRegClassID, ARM64::DDDRegClassID,
791                                     ARM64::DDDDRegClassID };
792   static unsigned SubRegs[] = { ARM64::dsub0, ARM64::dsub1,
793                                 ARM64::dsub2, ARM64::dsub3 };
794
795   return createTuple(Regs, RegClassIDs, SubRegs);
796 }
797
798 SDValue ARM64DAGToDAGISel::createQTuple(ArrayRef<SDValue> Regs) {
799   static unsigned RegClassIDs[] = { ARM64::QQRegClassID, ARM64::QQQRegClassID,
800                                     ARM64::QQQQRegClassID };
801   static unsigned SubRegs[] = { ARM64::qsub0, ARM64::qsub1,
802                                 ARM64::qsub2, ARM64::qsub3 };
803
804   return createTuple(Regs, RegClassIDs, SubRegs);
805 }
806
807 SDValue ARM64DAGToDAGISel::createTuple(ArrayRef<SDValue> Regs,
808                                        unsigned RegClassIDs[],
809                                        unsigned SubRegs[]) {
810   // There's no special register-class for a vector-list of 1 element: it's just
811   // a vector.
812   if (Regs.size() == 1)
813     return Regs[0];
814
815   assert(Regs.size() >= 2 && Regs.size() <= 4);
816
817   SDLoc DL(Regs[0].getNode());
818
819   SmallVector<SDValue, 4> Ops;
820
821   // First operand of REG_SEQUENCE is the desired RegClass.
822   Ops.push_back(
823       CurDAG->getTargetConstant(RegClassIDs[Regs.size() - 2], MVT::i32));
824
825   // Then we get pairs of source & subregister-position for the components.
826   for (unsigned i = 0; i < Regs.size(); ++i) {
827     Ops.push_back(Regs[i]);
828     Ops.push_back(CurDAG->getTargetConstant(SubRegs[i], MVT::i32));
829   }
830
831   SDNode *N =
832       CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops);
833   return SDValue(N, 0);
834 }
835
836 SDNode *ARM64DAGToDAGISel::SelectTable(SDNode *N, unsigned NumVecs,
837                                        unsigned Opc, bool isExt) {
838   SDLoc dl(N);
839   EVT VT = N->getValueType(0);
840
841   unsigned ExtOff = isExt;
842
843   // Form a REG_SEQUENCE to force register allocation.
844   unsigned Vec0Off = ExtOff + 1;
845   SmallVector<SDValue, 4> Regs(N->op_begin() + Vec0Off,
846                                N->op_begin() + Vec0Off + NumVecs);
847   SDValue RegSeq = createQTuple(Regs);
848
849   SmallVector<SDValue, 6> Ops;
850   if (isExt)
851     Ops.push_back(N->getOperand(1));
852   Ops.push_back(RegSeq);
853   Ops.push_back(N->getOperand(NumVecs + ExtOff + 1));
854   return CurDAG->getMachineNode(Opc, dl, VT, Ops);
855 }
856
857 SDNode *ARM64DAGToDAGISel::SelectIndexedLoad(SDNode *N, bool &Done) {
858   LoadSDNode *LD = cast<LoadSDNode>(N);
859   if (LD->isUnindexed())
860     return nullptr;
861   EVT VT = LD->getMemoryVT();
862   EVT DstVT = N->getValueType(0);
863   ISD::MemIndexedMode AM = LD->getAddressingMode();
864   bool IsPre = AM == ISD::PRE_INC || AM == ISD::PRE_DEC;
865
866   // We're not doing validity checking here. That was done when checking
867   // if we should mark the load as indexed or not. We're just selecting
868   // the right instruction.
869   unsigned Opcode = 0;
870
871   ISD::LoadExtType ExtType = LD->getExtensionType();
872   bool InsertTo64 = false;
873   if (VT == MVT::i64)
874     Opcode = IsPre ? ARM64::LDRXpre_isel : ARM64::LDRXpost_isel;
875   else if (VT == MVT::i32) {
876     if (ExtType == ISD::NON_EXTLOAD)
877       Opcode = IsPre ? ARM64::LDRWpre_isel : ARM64::LDRWpost_isel;
878     else if (ExtType == ISD::SEXTLOAD)
879       Opcode = IsPre ? ARM64::LDRSWpre_isel : ARM64::LDRSWpost_isel;
880     else {
881       Opcode = IsPre ? ARM64::LDRWpre_isel : ARM64::LDRWpost_isel;
882       InsertTo64 = true;
883       // The result of the load is only i32. It's the subreg_to_reg that makes
884       // it into an i64.
885       DstVT = MVT::i32;
886     }
887   } else if (VT == MVT::i16) {
888     if (ExtType == ISD::SEXTLOAD) {
889       if (DstVT == MVT::i64)
890         Opcode = IsPre ? ARM64::LDRSHXpre_isel : ARM64::LDRSHXpost_isel;
891       else
892         Opcode = IsPre ? ARM64::LDRSHWpre_isel : ARM64::LDRSHWpost_isel;
893     } else {
894       Opcode = IsPre ? ARM64::LDRHHpre_isel : ARM64::LDRHHpost_isel;
895       InsertTo64 = DstVT == MVT::i64;
896       // The result of the load is only i32. It's the subreg_to_reg that makes
897       // it into an i64.
898       DstVT = MVT::i32;
899     }
900   } else if (VT == MVT::i8) {
901     if (ExtType == ISD::SEXTLOAD) {
902       if (DstVT == MVT::i64)
903         Opcode = IsPre ? ARM64::LDRSBXpre_isel : ARM64::LDRSBXpost_isel;
904       else
905         Opcode = IsPre ? ARM64::LDRSBWpre_isel : ARM64::LDRSBWpost_isel;
906     } else {
907       Opcode = IsPre ? ARM64::LDRBBpre_isel : ARM64::LDRBBpost_isel;
908       InsertTo64 = DstVT == MVT::i64;
909       // The result of the load is only i32. It's the subreg_to_reg that makes
910       // it into an i64.
911       DstVT = MVT::i32;
912     }
913   } else if (VT == MVT::f32) {
914     Opcode = IsPre ? ARM64::LDRSpre_isel : ARM64::LDRSpost_isel;
915   } else if (VT == MVT::f64 || VT.is64BitVector()) {
916     Opcode = IsPre ? ARM64::LDRDpre_isel : ARM64::LDRDpost_isel;
917   } else if (VT.is128BitVector()) {
918     Opcode = IsPre ? ARM64::LDRQpre_isel : ARM64::LDRQpost_isel;
919   } else
920     return nullptr;
921   SDValue Chain = LD->getChain();
922   SDValue Base = LD->getBasePtr();
923   ConstantSDNode *OffsetOp = cast<ConstantSDNode>(LD->getOffset());
924   int OffsetVal = (int)OffsetOp->getZExtValue();
925   SDValue Offset = CurDAG->getTargetConstant(OffsetVal, MVT::i64);
926   SDValue Ops[] = { Base, Offset, Chain };
927   SDNode *Res = CurDAG->getMachineNode(Opcode, SDLoc(N), DstVT, MVT::i64,
928                                        MVT::Other, Ops);
929   // Either way, we're replacing the node, so tell the caller that.
930   Done = true;
931   if (InsertTo64) {
932     SDValue SubReg = CurDAG->getTargetConstant(ARM64::sub_32, MVT::i32);
933     SDNode *Sub = CurDAG->getMachineNode(
934         ARM64::SUBREG_TO_REG, SDLoc(N), MVT::i64,
935         CurDAG->getTargetConstant(0, MVT::i64), SDValue(Res, 0), SubReg);
936     ReplaceUses(SDValue(N, 0), SDValue(Sub, 0));
937     ReplaceUses(SDValue(N, 1), SDValue(Res, 1));
938     ReplaceUses(SDValue(N, 2), SDValue(Res, 2));
939     return nullptr;
940   }
941   return Res;
942 }
943
944 SDNode *ARM64DAGToDAGISel::SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
945                                       unsigned SubRegIdx) {
946   SDLoc dl(N);
947   EVT VT = N->getValueType(0);
948   SDValue Chain = N->getOperand(0);
949
950   SmallVector<SDValue, 6> Ops;
951   Ops.push_back(N->getOperand(2)); // Mem operand;
952   Ops.push_back(Chain);
953
954   std::vector<EVT> ResTys;
955   ResTys.push_back(MVT::Untyped);
956   ResTys.push_back(MVT::Other);
957
958   SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
959   SDValue SuperReg = SDValue(Ld, 0);
960   for (unsigned i = 0; i < NumVecs; ++i)
961     ReplaceUses(SDValue(N, i),
962         CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
963
964   ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
965   return nullptr;
966 }
967
968 SDNode *ARM64DAGToDAGISel::SelectPostLoad(SDNode *N, unsigned NumVecs,
969                                           unsigned Opc, unsigned SubRegIdx) {
970   SDLoc dl(N);
971   EVT VT = N->getValueType(0);
972   SDValue Chain = N->getOperand(0);
973
974   SmallVector<SDValue, 6> Ops;
975   Ops.push_back(N->getOperand(1)); // Mem operand
976   Ops.push_back(N->getOperand(2)); // Incremental
977   Ops.push_back(Chain);
978
979   std::vector<EVT> ResTys;
980   ResTys.push_back(MVT::i64); // Type of the write back register
981   ResTys.push_back(MVT::Untyped);
982   ResTys.push_back(MVT::Other);
983
984   SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
985
986   // Update uses of write back register
987   ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
988
989   // Update uses of vector list
990   SDValue SuperReg = SDValue(Ld, 1);
991   for (unsigned i = 0; i < NumVecs; ++i)
992     ReplaceUses(SDValue(N, i),
993         CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
994
995   // Update the chain
996   ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
997   return nullptr;
998 }
999
1000 SDNode *ARM64DAGToDAGISel::SelectStore(SDNode *N, unsigned NumVecs,
1001                                        unsigned Opc) {
1002   SDLoc dl(N);
1003   EVT VT = N->getOperand(2)->getValueType(0);
1004
1005   // Form a REG_SEQUENCE to force register allocation.
1006   bool Is128Bit = VT.getSizeInBits() == 128;
1007   SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1008   SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1009
1010   SmallVector<SDValue, 6> Ops;
1011   Ops.push_back(RegSeq);
1012   Ops.push_back(N->getOperand(NumVecs + 2));
1013   Ops.push_back(N->getOperand(0));
1014   SDNode *St = CurDAG->getMachineNode(Opc, dl, N->getValueType(0), Ops);
1015
1016   return St;
1017 }
1018
1019 SDNode *ARM64DAGToDAGISel::SelectPostStore(SDNode *N, unsigned NumVecs,
1020                                                unsigned Opc) {
1021   SDLoc dl(N);
1022   EVT VT = N->getOperand(2)->getValueType(0);
1023   SmallVector<EVT, 2> ResTys;
1024   ResTys.push_back(MVT::i64);   // Type of the write back register
1025   ResTys.push_back(MVT::Other); // Type for the Chain
1026
1027   // Form a REG_SEQUENCE to force register allocation.
1028   bool Is128Bit = VT.getSizeInBits() == 128;
1029   SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1030   SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1031
1032   SmallVector<SDValue, 6> Ops;
1033   Ops.push_back(RegSeq);
1034   Ops.push_back(N->getOperand(NumVecs + 1)); // base register
1035   Ops.push_back(N->getOperand(NumVecs + 2)); // Incremental
1036   Ops.push_back(N->getOperand(0)); // Chain
1037   SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1038
1039   return St;
1040 }
1041
1042 /// WidenVector - Given a value in the V64 register class, produce the
1043 /// equivalent value in the V128 register class.
1044 class WidenVector {
1045   SelectionDAG &DAG;
1046
1047 public:
1048   WidenVector(SelectionDAG &DAG) : DAG(DAG) {}
1049
1050   SDValue operator()(SDValue V64Reg) {
1051     EVT VT = V64Reg.getValueType();
1052     unsigned NarrowSize = VT.getVectorNumElements();
1053     MVT EltTy = VT.getVectorElementType().getSimpleVT();
1054     MVT WideTy = MVT::getVectorVT(EltTy, 2 * NarrowSize);
1055     SDLoc DL(V64Reg);
1056
1057     SDValue Undef =
1058         SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, WideTy), 0);
1059     return DAG.getTargetInsertSubreg(ARM64::dsub, DL, WideTy, Undef, V64Reg);
1060   }
1061 };
1062
1063 /// NarrowVector - Given a value in the V128 register class, produce the
1064 /// equivalent value in the V64 register class.
1065 static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG) {
1066   EVT VT = V128Reg.getValueType();
1067   unsigned WideSize = VT.getVectorNumElements();
1068   MVT EltTy = VT.getVectorElementType().getSimpleVT();
1069   MVT NarrowTy = MVT::getVectorVT(EltTy, WideSize / 2);
1070
1071   return DAG.getTargetExtractSubreg(ARM64::dsub, SDLoc(V128Reg), NarrowTy,
1072                                     V128Reg);
1073 }
1074
1075 SDNode *ARM64DAGToDAGISel::SelectLoadLane(SDNode *N, unsigned NumVecs,
1076                                           unsigned Opc) {
1077   SDLoc dl(N);
1078   EVT VT = N->getValueType(0);
1079   bool Narrow = VT.getSizeInBits() == 64;
1080
1081   // Form a REG_SEQUENCE to force register allocation.
1082   SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1083
1084   if (Narrow)
1085     std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1086                    WidenVector(*CurDAG));
1087
1088   SDValue RegSeq = createQTuple(Regs);
1089
1090   std::vector<EVT> ResTys;
1091   ResTys.push_back(MVT::Untyped);
1092   ResTys.push_back(MVT::Other);
1093
1094   unsigned LaneNo =
1095       cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1096
1097   SmallVector<SDValue, 6> Ops;
1098   Ops.push_back(RegSeq);
1099   Ops.push_back(CurDAG->getTargetConstant(LaneNo, MVT::i64));
1100   Ops.push_back(N->getOperand(NumVecs + 3));
1101   Ops.push_back(N->getOperand(0));
1102   SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1103   SDValue SuperReg = SDValue(Ld, 0);
1104
1105   EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
1106   static unsigned QSubs[] = { ARM64::qsub0, ARM64::qsub1, ARM64::qsub2,
1107                               ARM64::qsub3 };
1108   for (unsigned i = 0; i < NumVecs; ++i) {
1109     SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT, SuperReg);
1110     if (Narrow)
1111       NV = NarrowVector(NV, *CurDAG);
1112     ReplaceUses(SDValue(N, i), NV);
1113   }
1114
1115   ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
1116
1117   return Ld;
1118 }
1119
1120 SDNode *ARM64DAGToDAGISel::SelectPostLoadLane(SDNode *N, unsigned NumVecs,
1121                                               unsigned Opc) {
1122   SDLoc dl(N);
1123   EVT VT = N->getValueType(0);
1124   bool Narrow = VT.getSizeInBits() == 64;
1125
1126   // Form a REG_SEQUENCE to force register allocation.
1127   SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1128
1129   if (Narrow)
1130     std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1131                    WidenVector(*CurDAG));
1132
1133   SDValue RegSeq = createQTuple(Regs);
1134
1135   std::vector<EVT> ResTys;
1136   ResTys.push_back(MVT::i64); // Type of the write back register
1137   ResTys.push_back(MVT::Untyped);
1138   ResTys.push_back(MVT::Other);
1139
1140   unsigned LaneNo =
1141       cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1142
1143   SmallVector<SDValue, 6> Ops;
1144   Ops.push_back(RegSeq);
1145   Ops.push_back(CurDAG->getTargetConstant(LaneNo, MVT::i64)); // Lane Number
1146   Ops.push_back(N->getOperand(NumVecs + 2)); // Base register
1147   Ops.push_back(N->getOperand(NumVecs + 3)); // Incremental
1148   Ops.push_back(N->getOperand(0));
1149   SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1150
1151   // Update uses of the write back register
1152   ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1153
1154   // Update uses of the vector list
1155   SDValue SuperReg = SDValue(Ld, 1);
1156   EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
1157   static unsigned QSubs[] = { ARM64::qsub0, ARM64::qsub1, ARM64::qsub2,
1158                               ARM64::qsub3 };
1159   for (unsigned i = 0; i < NumVecs; ++i) {
1160     SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT, SuperReg);
1161     if (Narrow)
1162       NV = NarrowVector(NV, *CurDAG);
1163     ReplaceUses(SDValue(N, i), NV);
1164   }
1165
1166   // Update the Chain
1167   ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
1168
1169   return Ld;
1170 }
1171
1172 SDNode *ARM64DAGToDAGISel::SelectStoreLane(SDNode *N, unsigned NumVecs,
1173                                            unsigned Opc) {
1174   SDLoc dl(N);
1175   EVT VT = N->getOperand(2)->getValueType(0);
1176   bool Narrow = VT.getSizeInBits() == 64;
1177
1178   // Form a REG_SEQUENCE to force register allocation.
1179   SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1180
1181   if (Narrow)
1182     std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1183                    WidenVector(*CurDAG));
1184
1185   SDValue RegSeq = createQTuple(Regs);
1186
1187   unsigned LaneNo =
1188       cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1189
1190   SmallVector<SDValue, 6> Ops;
1191   Ops.push_back(RegSeq);
1192   Ops.push_back(CurDAG->getTargetConstant(LaneNo, MVT::i64));
1193   Ops.push_back(N->getOperand(NumVecs + 3));
1194   Ops.push_back(N->getOperand(0));
1195   SDNode *St = CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops);
1196
1197   // Transfer memoperands.
1198   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1199   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1200   cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1201
1202   return St;
1203 }
1204
1205 SDNode *ARM64DAGToDAGISel::SelectPostStoreLane(SDNode *N, unsigned NumVecs,
1206                                                unsigned Opc) {
1207   SDLoc dl(N);
1208   EVT VT = N->getOperand(2)->getValueType(0);
1209   bool Narrow = VT.getSizeInBits() == 64;
1210
1211   // Form a REG_SEQUENCE to force register allocation.
1212   SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1213
1214   if (Narrow)
1215     std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1216                    WidenVector(*CurDAG));
1217
1218   SDValue RegSeq = createQTuple(Regs);
1219
1220   SmallVector<EVT, 2> ResTys;
1221   ResTys.push_back(MVT::i64);   // Type of the write back register
1222   ResTys.push_back(MVT::Other);
1223
1224   unsigned LaneNo =
1225       cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1226
1227   SmallVector<SDValue, 6> Ops;
1228   Ops.push_back(RegSeq);
1229   Ops.push_back(CurDAG->getTargetConstant(LaneNo, MVT::i64));
1230   Ops.push_back(N->getOperand(NumVecs + 2)); // Base Register
1231   Ops.push_back(N->getOperand(NumVecs + 3)); // Incremental
1232   Ops.push_back(N->getOperand(0));
1233   SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1234
1235   // Transfer memoperands.
1236   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1237   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1238   cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1239
1240   return St;
1241 }
1242
1243 static bool isBitfieldExtractOpFromAnd(SelectionDAG *CurDAG, SDNode *N,
1244                                        unsigned &Opc, SDValue &Opd0,
1245                                        unsigned &LSB, unsigned &MSB,
1246                                        unsigned NumberOfIgnoredLowBits,
1247                                        bool BiggerPattern) {
1248   assert(N->getOpcode() == ISD::AND &&
1249          "N must be a AND operation to call this function");
1250
1251   EVT VT = N->getValueType(0);
1252
1253   // Here we can test the type of VT and return false when the type does not
1254   // match, but since it is done prior to that call in the current context
1255   // we turned that into an assert to avoid redundant code.
1256   assert((VT == MVT::i32 || VT == MVT::i64) &&
1257          "Type checking must have been done before calling this function");
1258
1259   // FIXME: simplify-demanded-bits in DAGCombine will probably have
1260   // changed the AND node to a 32-bit mask operation. We'll have to
1261   // undo that as part of the transform here if we want to catch all
1262   // the opportunities.
1263   // Currently the NumberOfIgnoredLowBits argument helps to recover
1264   // form these situations when matching bigger pattern (bitfield insert).
1265
1266   // For unsigned extracts, check for a shift right and mask
1267   uint64_t And_imm = 0;
1268   if (!isOpcWithIntImmediate(N, ISD::AND, And_imm))
1269     return false;
1270
1271   const SDNode *Op0 = N->getOperand(0).getNode();
1272
1273   // Because of simplify-demanded-bits in DAGCombine, the mask may have been
1274   // simplified. Try to undo that
1275   And_imm |= (1 << NumberOfIgnoredLowBits) - 1;
1276
1277   // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1278   if (And_imm & (And_imm + 1))
1279     return false;
1280
1281   bool ClampMSB = false;
1282   uint64_t Srl_imm = 0;
1283   // Handle the SRL + ANY_EXTEND case.
1284   if (VT == MVT::i64 && Op0->getOpcode() == ISD::ANY_EXTEND &&
1285       isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL, Srl_imm)) {
1286     // Extend the incoming operand of the SRL to 64-bit.
1287     Opd0 = Widen(CurDAG, Op0->getOperand(0).getOperand(0));
1288     // Make sure to clamp the MSB so that we preserve the semantics of the
1289     // original operations.
1290     ClampMSB = true;
1291   } else if (VT == MVT::i32 && Op0->getOpcode() == ISD::TRUNCATE &&
1292              isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL,
1293                                    Srl_imm)) {
1294     // If the shift result was truncated, we can still combine them.
1295     Opd0 = Op0->getOperand(0).getOperand(0);
1296
1297     // Use the type of SRL node.
1298     VT = Opd0->getValueType(0);
1299   } else if (isOpcWithIntImmediate(Op0, ISD::SRL, Srl_imm)) {
1300     Opd0 = Op0->getOperand(0);
1301   } else if (BiggerPattern) {
1302     // Let's pretend a 0 shift right has been performed.
1303     // The resulting code will be at least as good as the original one
1304     // plus it may expose more opportunities for bitfield insert pattern.
1305     // FIXME: Currently we limit this to the bigger pattern, because
1306     // some optimizations expect AND and not UBFM
1307     Opd0 = N->getOperand(0);
1308   } else
1309     return false;
1310
1311   assert((BiggerPattern || (Srl_imm > 0 && Srl_imm < VT.getSizeInBits())) &&
1312          "bad amount in shift node!");
1313
1314   LSB = Srl_imm;
1315   MSB = Srl_imm + (VT == MVT::i32 ? CountTrailingOnes_32(And_imm)
1316                                   : CountTrailingOnes_64(And_imm)) -
1317         1;
1318   if (ClampMSB)
1319     // Since we're moving the extend before the right shift operation, we need
1320     // to clamp the MSB to make sure we don't shift in undefined bits instead of
1321     // the zeros which would get shifted in with the original right shift
1322     // operation.
1323     MSB = MSB > 31 ? 31 : MSB;
1324
1325   Opc = VT == MVT::i32 ? ARM64::UBFMWri : ARM64::UBFMXri;
1326   return true;
1327 }
1328
1329 static bool isOneBitExtractOpFromShr(SDNode *N, unsigned &Opc, SDValue &Opd0,
1330                                      unsigned &LSB, unsigned &MSB) {
1331   // We are looking for the following pattern which basically extracts a single
1332   // bit from the source value and places it in the LSB of the destination
1333   // value, all other bits of the destination value or set to zero:
1334   //
1335   // Value2 = AND Value, MaskImm
1336   // SRL Value2, ShiftImm
1337   //
1338   // with MaskImm >> ShiftImm == 1.
1339   //
1340   // This gets selected into a single UBFM:
1341   //
1342   // UBFM Value, ShiftImm, ShiftImm
1343   //
1344
1345   if (N->getOpcode() != ISD::SRL)
1346     return false;
1347
1348   uint64_t And_mask = 0;
1349   if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, And_mask))
1350     return false;
1351
1352   Opd0 = N->getOperand(0).getOperand(0);
1353
1354   uint64_t Srl_imm = 0;
1355   if (!isIntImmediate(N->getOperand(1), Srl_imm))
1356     return false;
1357
1358   // Check whether we really have a one bit extract here.
1359   if (And_mask >> Srl_imm == 0x1) {
1360     if (N->getValueType(0) == MVT::i32)
1361       Opc = ARM64::UBFMWri;
1362     else
1363       Opc = ARM64::UBFMXri;
1364
1365     LSB = MSB = Srl_imm;
1366
1367     return true;
1368   }
1369
1370   return false;
1371 }
1372
1373 static bool isBitfieldExtractOpFromShr(SDNode *N, unsigned &Opc, SDValue &Opd0,
1374                                        unsigned &LSB, unsigned &MSB,
1375                                        bool BiggerPattern) {
1376   assert((N->getOpcode() == ISD::SRA || N->getOpcode() == ISD::SRL) &&
1377          "N must be a SHR/SRA operation to call this function");
1378
1379   EVT VT = N->getValueType(0);
1380
1381   // Here we can test the type of VT and return false when the type does not
1382   // match, but since it is done prior to that call in the current context
1383   // we turned that into an assert to avoid redundant code.
1384   assert((VT == MVT::i32 || VT == MVT::i64) &&
1385          "Type checking must have been done before calling this function");
1386
1387   // Check for AND + SRL doing a one bit extract.
1388   if (isOneBitExtractOpFromShr(N, Opc, Opd0, LSB, MSB))
1389     return true;
1390
1391   // we're looking for a shift of a shift
1392   uint64_t Shl_imm = 0;
1393   uint64_t Trunc_bits = 0;
1394   if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1395     Opd0 = N->getOperand(0).getOperand(0);
1396   } else if (VT == MVT::i32 && N->getOpcode() == ISD::SRL &&
1397              N->getOperand(0).getNode()->getOpcode() == ISD::TRUNCATE) {
1398     // We are looking for a shift of truncate. Truncate from i64 to i32 could
1399     // be considered as setting high 32 bits as zero. Our strategy here is to
1400     // always generate 64bit UBFM. This consistency will help the CSE pass
1401     // later find more redundancy.
1402     Opd0 = N->getOperand(0).getOperand(0);
1403     Trunc_bits = Opd0->getValueType(0).getSizeInBits() - VT.getSizeInBits();
1404     VT = Opd0->getValueType(0);
1405     assert(VT == MVT::i64 && "the promoted type should be i64");
1406   } else if (BiggerPattern) {
1407     // Let's pretend a 0 shift left has been performed.
1408     // FIXME: Currently we limit this to the bigger pattern case,
1409     // because some optimizations expect AND and not UBFM
1410     Opd0 = N->getOperand(0);
1411   } else
1412     return false;
1413
1414   assert(Shl_imm < VT.getSizeInBits() && "bad amount in shift node!");
1415   uint64_t Srl_imm = 0;
1416   if (!isIntImmediate(N->getOperand(1), Srl_imm))
1417     return false;
1418
1419   assert(Srl_imm > 0 && Srl_imm < VT.getSizeInBits() &&
1420          "bad amount in shift node!");
1421   // Note: The width operand is encoded as width-1.
1422   unsigned Width = VT.getSizeInBits() - Trunc_bits - Srl_imm - 1;
1423   int sLSB = Srl_imm - Shl_imm;
1424   if (sLSB < 0)
1425     return false;
1426   LSB = sLSB;
1427   MSB = LSB + Width;
1428   // SRA requires a signed extraction
1429   if (VT == MVT::i32)
1430     Opc = N->getOpcode() == ISD::SRA ? ARM64::SBFMWri : ARM64::UBFMWri;
1431   else
1432     Opc = N->getOpcode() == ISD::SRA ? ARM64::SBFMXri : ARM64::UBFMXri;
1433   return true;
1434 }
1435
1436 static bool isBitfieldExtractOp(SelectionDAG *CurDAG, SDNode *N, unsigned &Opc,
1437                                 SDValue &Opd0, unsigned &LSB, unsigned &MSB,
1438                                 unsigned NumberOfIgnoredLowBits = 0,
1439                                 bool BiggerPattern = false) {
1440   if (N->getValueType(0) != MVT::i32 && N->getValueType(0) != MVT::i64)
1441     return false;
1442
1443   switch (N->getOpcode()) {
1444   default:
1445     if (!N->isMachineOpcode())
1446       return false;
1447     break;
1448   case ISD::AND:
1449     return isBitfieldExtractOpFromAnd(CurDAG, N, Opc, Opd0, LSB, MSB,
1450                                       NumberOfIgnoredLowBits, BiggerPattern);
1451   case ISD::SRL:
1452   case ISD::SRA:
1453     return isBitfieldExtractOpFromShr(N, Opc, Opd0, LSB, MSB, BiggerPattern);
1454   }
1455
1456   unsigned NOpc = N->getMachineOpcode();
1457   switch (NOpc) {
1458   default:
1459     return false;
1460   case ARM64::SBFMWri:
1461   case ARM64::UBFMWri:
1462   case ARM64::SBFMXri:
1463   case ARM64::UBFMXri:
1464     Opc = NOpc;
1465     Opd0 = N->getOperand(0);
1466     LSB = cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
1467     MSB = cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
1468     return true;
1469   }
1470   // Unreachable
1471   return false;
1472 }
1473
1474 SDNode *ARM64DAGToDAGISel::SelectBitfieldExtractOp(SDNode *N) {
1475   unsigned Opc, LSB, MSB;
1476   SDValue Opd0;
1477   if (!isBitfieldExtractOp(CurDAG, N, Opc, Opd0, LSB, MSB))
1478     return nullptr;
1479
1480   EVT VT = N->getValueType(0);
1481
1482   // If the bit extract operation is 64bit but the original type is 32bit, we
1483   // need to add one EXTRACT_SUBREG.
1484   if ((Opc == ARM64::SBFMXri || Opc == ARM64::UBFMXri) && VT == MVT::i32) {
1485     SDValue Ops64[] = {Opd0, CurDAG->getTargetConstant(LSB, MVT::i64),
1486                        CurDAG->getTargetConstant(MSB, MVT::i64)};
1487
1488     SDNode *BFM = CurDAG->getMachineNode(Opc, SDLoc(N), MVT::i64, Ops64);
1489     SDValue SubReg = CurDAG->getTargetConstant(ARM64::sub_32, MVT::i32);
1490     MachineSDNode *Node =
1491         CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, SDLoc(N), MVT::i32,
1492                                SDValue(BFM, 0), SubReg);
1493     return Node;
1494   }
1495
1496   SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(LSB, VT),
1497                    CurDAG->getTargetConstant(MSB, VT)};
1498   return CurDAG->SelectNodeTo(N, Opc, VT, Ops);
1499 }
1500
1501 /// Does DstMask form a complementary pair with the mask provided by
1502 /// BitsToBeInserted, suitable for use in a BFI instruction. Roughly speaking,
1503 /// this asks whether DstMask zeroes precisely those bits that will be set by
1504 /// the other half.
1505 static bool isBitfieldDstMask(uint64_t DstMask, APInt BitsToBeInserted,
1506                               unsigned NumberOfIgnoredHighBits, EVT VT) {
1507   assert((VT == MVT::i32 || VT == MVT::i64) &&
1508          "i32 or i64 mask type expected!");
1509   unsigned BitWidth = VT.getSizeInBits() - NumberOfIgnoredHighBits;
1510
1511   APInt SignificantDstMask = APInt(BitWidth, DstMask);
1512   APInt SignificantBitsToBeInserted = BitsToBeInserted.zextOrTrunc(BitWidth);
1513
1514   return (SignificantDstMask & SignificantBitsToBeInserted) == 0 &&
1515          (SignificantDstMask | SignificantBitsToBeInserted).isAllOnesValue();
1516 }
1517
1518 // Look for bits that will be useful for later uses.
1519 // A bit is consider useless as soon as it is dropped and never used
1520 // before it as been dropped.
1521 // E.g., looking for useful bit of x
1522 // 1. y = x & 0x7
1523 // 2. z = y >> 2
1524 // After #1, x useful bits are 0x7, then the useful bits of x, live through
1525 // y.
1526 // After #2, the useful bits of x are 0x4.
1527 // However, if x is used on an unpredicatable instruction, then all its bits
1528 // are useful.
1529 // E.g.
1530 // 1. y = x & 0x7
1531 // 2. z = y >> 2
1532 // 3. str x, [@x]
1533 static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth = 0);
1534
1535 static void getUsefulBitsFromAndWithImmediate(SDValue Op, APInt &UsefulBits,
1536                                               unsigned Depth) {
1537   uint64_t Imm =
1538       cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1539   Imm = ARM64_AM::decodeLogicalImmediate(Imm, UsefulBits.getBitWidth());
1540   UsefulBits &= APInt(UsefulBits.getBitWidth(), Imm);
1541   getUsefulBits(Op, UsefulBits, Depth + 1);
1542 }
1543
1544 static void getUsefulBitsFromBitfieldMoveOpd(SDValue Op, APInt &UsefulBits,
1545                                              uint64_t Imm, uint64_t MSB,
1546                                              unsigned Depth) {
1547   // inherit the bitwidth value
1548   APInt OpUsefulBits(UsefulBits);
1549   OpUsefulBits = 1;
1550
1551   if (MSB >= Imm) {
1552     OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1553     --OpUsefulBits;
1554     // The interesting part will be in the lower part of the result
1555     getUsefulBits(Op, OpUsefulBits, Depth + 1);
1556     // The interesting part was starting at Imm in the argument
1557     OpUsefulBits = OpUsefulBits.shl(Imm);
1558   } else {
1559     OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1560     --OpUsefulBits;
1561     // The interesting part will be shifted in the result
1562     OpUsefulBits = OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm);
1563     getUsefulBits(Op, OpUsefulBits, Depth + 1);
1564     // The interesting part was at zero in the argument
1565     OpUsefulBits = OpUsefulBits.lshr(OpUsefulBits.getBitWidth() - Imm);
1566   }
1567
1568   UsefulBits &= OpUsefulBits;
1569 }
1570
1571 static void getUsefulBitsFromUBFM(SDValue Op, APInt &UsefulBits,
1572                                   unsigned Depth) {
1573   uint64_t Imm =
1574       cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1575   uint64_t MSB =
1576       cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1577
1578   getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1579 }
1580
1581 static void getUsefulBitsFromOrWithShiftedReg(SDValue Op, APInt &UsefulBits,
1582                                               unsigned Depth) {
1583   uint64_t ShiftTypeAndValue =
1584       cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1585   APInt Mask(UsefulBits);
1586   Mask.clearAllBits();
1587   Mask.flipAllBits();
1588
1589   if (ARM64_AM::getShiftType(ShiftTypeAndValue) == ARM64_AM::LSL) {
1590     // Shift Left
1591     uint64_t ShiftAmt = ARM64_AM::getShiftValue(ShiftTypeAndValue);
1592     Mask = Mask.shl(ShiftAmt);
1593     getUsefulBits(Op, Mask, Depth + 1);
1594     Mask = Mask.lshr(ShiftAmt);
1595   } else if (ARM64_AM::getShiftType(ShiftTypeAndValue) == ARM64_AM::LSR) {
1596     // Shift Right
1597     // We do not handle ARM64_AM::ASR, because the sign will change the
1598     // number of useful bits
1599     uint64_t ShiftAmt = ARM64_AM::getShiftValue(ShiftTypeAndValue);
1600     Mask = Mask.lshr(ShiftAmt);
1601     getUsefulBits(Op, Mask, Depth + 1);
1602     Mask = Mask.shl(ShiftAmt);
1603   } else
1604     return;
1605
1606   UsefulBits &= Mask;
1607 }
1608
1609 static void getUsefulBitsFromBFM(SDValue Op, SDValue Orig, APInt &UsefulBits,
1610                                  unsigned Depth) {
1611   uint64_t Imm =
1612       cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1613   uint64_t MSB =
1614       cast<const ConstantSDNode>(Op.getOperand(3).getNode())->getZExtValue();
1615
1616   if (Op.getOperand(1) == Orig)
1617     return getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1618
1619   APInt OpUsefulBits(UsefulBits);
1620   OpUsefulBits = 1;
1621
1622   if (MSB >= Imm) {
1623     OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1624     --OpUsefulBits;
1625     UsefulBits &= ~OpUsefulBits;
1626     getUsefulBits(Op, UsefulBits, Depth + 1);
1627   } else {
1628     OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1629     --OpUsefulBits;
1630     UsefulBits = ~(OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm));
1631     getUsefulBits(Op, UsefulBits, Depth + 1);
1632   }
1633 }
1634
1635 static void getUsefulBitsForUse(SDNode *UserNode, APInt &UsefulBits,
1636                                 SDValue Orig, unsigned Depth) {
1637
1638   // Users of this node should have already been instruction selected
1639   // FIXME: Can we turn that into an assert?
1640   if (!UserNode->isMachineOpcode())
1641     return;
1642
1643   switch (UserNode->getMachineOpcode()) {
1644   default:
1645     return;
1646   case ARM64::ANDSWri:
1647   case ARM64::ANDSXri:
1648   case ARM64::ANDWri:
1649   case ARM64::ANDXri:
1650     // We increment Depth only when we call the getUsefulBits
1651     return getUsefulBitsFromAndWithImmediate(SDValue(UserNode, 0), UsefulBits,
1652                                              Depth);
1653   case ARM64::UBFMWri:
1654   case ARM64::UBFMXri:
1655     return getUsefulBitsFromUBFM(SDValue(UserNode, 0), UsefulBits, Depth);
1656
1657   case ARM64::ORRWrs:
1658   case ARM64::ORRXrs:
1659     if (UserNode->getOperand(1) != Orig)
1660       return;
1661     return getUsefulBitsFromOrWithShiftedReg(SDValue(UserNode, 0), UsefulBits,
1662                                              Depth);
1663   case ARM64::BFMWri:
1664   case ARM64::BFMXri:
1665     return getUsefulBitsFromBFM(SDValue(UserNode, 0), Orig, UsefulBits, Depth);
1666   }
1667 }
1668
1669 static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth) {
1670   if (Depth >= 6)
1671     return;
1672   // Initialize UsefulBits
1673   if (!Depth) {
1674     unsigned Bitwidth = Op.getValueType().getScalarType().getSizeInBits();
1675     // At the beginning, assume every produced bits is useful
1676     UsefulBits = APInt(Bitwidth, 0);
1677     UsefulBits.flipAllBits();
1678   }
1679   APInt UsersUsefulBits(UsefulBits.getBitWidth(), 0);
1680
1681   for (SDNode *Node : Op.getNode()->uses()) {
1682     // A use cannot produce useful bits
1683     APInt UsefulBitsForUse = APInt(UsefulBits);
1684     getUsefulBitsForUse(Node, UsefulBitsForUse, Op, Depth);
1685     UsersUsefulBits |= UsefulBitsForUse;
1686   }
1687   // UsefulBits contains the produced bits that are meaningful for the
1688   // current definition, thus a user cannot make a bit meaningful at
1689   // this point
1690   UsefulBits &= UsersUsefulBits;
1691 }
1692
1693 /// Create a machine node performing a notional SHL of Op by ShlAmount. If
1694 /// ShlAmount is negative, do a (logical) right-shift instead. If ShlAmount is
1695 /// 0, return Op unchanged.
1696 static SDValue getLeftShift(SelectionDAG *CurDAG, SDValue Op, int ShlAmount) {
1697   if (ShlAmount == 0)
1698     return Op;
1699
1700   EVT VT = Op.getValueType();
1701   unsigned BitWidth = VT.getSizeInBits();
1702   unsigned UBFMOpc = BitWidth == 32 ? ARM64::UBFMWri : ARM64::UBFMXri;
1703
1704   SDNode *ShiftNode;
1705   if (ShlAmount > 0) {
1706     // LSL wD, wN, #Amt == UBFM wD, wN, #32-Amt, #31-Amt
1707     ShiftNode = CurDAG->getMachineNode(
1708         UBFMOpc, SDLoc(Op), VT, Op,
1709         CurDAG->getTargetConstant(BitWidth - ShlAmount, VT),
1710         CurDAG->getTargetConstant(BitWidth - 1 - ShlAmount, VT));
1711   } else {
1712     // LSR wD, wN, #Amt == UBFM wD, wN, #Amt, #32-1
1713     assert(ShlAmount < 0 && "expected right shift");
1714     int ShrAmount = -ShlAmount;
1715     ShiftNode = CurDAG->getMachineNode(
1716         UBFMOpc, SDLoc(Op), VT, Op, CurDAG->getTargetConstant(ShrAmount, VT),
1717         CurDAG->getTargetConstant(BitWidth - 1, VT));
1718   }
1719
1720   return SDValue(ShiftNode, 0);
1721 }
1722
1723 /// Does this tree qualify as an attempt to move a bitfield into position,
1724 /// essentially "(and (shl VAL, N), Mask)".
1725 static bool isBitfieldPositioningOp(SelectionDAG *CurDAG, SDValue Op,
1726                                     SDValue &Src, int &ShiftAmount,
1727                                     int &MaskWidth) {
1728   EVT VT = Op.getValueType();
1729   unsigned BitWidth = VT.getSizeInBits();
1730   (void)BitWidth;
1731   assert(BitWidth == 32 || BitWidth == 64);
1732
1733   APInt KnownZero, KnownOne;
1734   CurDAG->ComputeMaskedBits(Op, KnownZero, KnownOne);
1735
1736   // Non-zero in the sense that they're not provably zero, which is the key
1737   // point if we want to use this value
1738   uint64_t NonZeroBits = (~KnownZero).getZExtValue();
1739
1740   // Discard a constant AND mask if present. It's safe because the node will
1741   // already have been factored into the ComputeMaskedBits calculation above.
1742   uint64_t AndImm;
1743   if (isOpcWithIntImmediate(Op.getNode(), ISD::AND, AndImm)) {
1744     assert((~APInt(BitWidth, AndImm) & ~KnownZero) == 0);
1745     Op = Op.getOperand(0);
1746   }
1747
1748   uint64_t ShlImm;
1749   if (!isOpcWithIntImmediate(Op.getNode(), ISD::SHL, ShlImm))
1750     return false;
1751   Op = Op.getOperand(0);
1752
1753   if (!isShiftedMask_64(NonZeroBits))
1754     return false;
1755
1756   ShiftAmount = countTrailingZeros(NonZeroBits);
1757   MaskWidth = CountTrailingOnes_64(NonZeroBits >> ShiftAmount);
1758
1759   // BFI encompasses sufficiently many nodes that it's worth inserting an extra
1760   // LSL/LSR if the mask in NonZeroBits doesn't quite match up with the ISD::SHL
1761   // amount.
1762   Src = getLeftShift(CurDAG, Op, ShlImm - ShiftAmount);
1763
1764   return true;
1765 }
1766
1767 // Given a OR operation, check if we have the following pattern
1768 // ubfm c, b, imm, imm2 (or something that does the same jobs, see
1769 //                       isBitfieldExtractOp)
1770 // d = e & mask2 ; where mask is a binary sequence of 1..10..0 and
1771 //                 countTrailingZeros(mask2) == imm2 - imm + 1
1772 // f = d | c
1773 // if yes, given reference arguments will be update so that one can replace
1774 // the OR instruction with:
1775 // f = Opc Opd0, Opd1, LSB, MSB ; where Opc is a BFM, LSB = imm, and MSB = imm2
1776 static bool isBitfieldInsertOpFromOr(SDNode *N, unsigned &Opc, SDValue &Dst,
1777                                      SDValue &Src, unsigned &ImmR,
1778                                      unsigned &ImmS, SelectionDAG *CurDAG) {
1779   assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
1780
1781   // Set Opc
1782   EVT VT = N->getValueType(0);
1783   if (VT == MVT::i32)
1784     Opc = ARM64::BFMWri;
1785   else if (VT == MVT::i64)
1786     Opc = ARM64::BFMXri;
1787   else
1788     return false;
1789
1790   // Because of simplify-demanded-bits in DAGCombine, involved masks may not
1791   // have the expected shape. Try to undo that.
1792   APInt UsefulBits;
1793   getUsefulBits(SDValue(N, 0), UsefulBits);
1794
1795   unsigned NumberOfIgnoredLowBits = UsefulBits.countTrailingZeros();
1796   unsigned NumberOfIgnoredHighBits = UsefulBits.countLeadingZeros();
1797
1798   // OR is commutative, check both possibilities (does llvm provide a
1799   // way to do that directely, e.g., via code matcher?)
1800   SDValue OrOpd1Val = N->getOperand(1);
1801   SDNode *OrOpd0 = N->getOperand(0).getNode();
1802   SDNode *OrOpd1 = N->getOperand(1).getNode();
1803   for (int i = 0; i < 2;
1804        ++i, std::swap(OrOpd0, OrOpd1), OrOpd1Val = N->getOperand(0)) {
1805     unsigned BFXOpc;
1806     int DstLSB, Width;
1807     if (isBitfieldExtractOp(CurDAG, OrOpd0, BFXOpc, Src, ImmR, ImmS,
1808                             NumberOfIgnoredLowBits, true)) {
1809       // Check that the returned opcode is compatible with the pattern,
1810       // i.e., same type and zero extended (U and not S)
1811       if ((BFXOpc != ARM64::UBFMXri && VT == MVT::i64) ||
1812           (BFXOpc != ARM64::UBFMWri && VT == MVT::i32))
1813         continue;
1814
1815       // Compute the width of the bitfield insertion
1816       DstLSB = 0;
1817       Width = ImmS - ImmR + 1;
1818       // FIXME: This constraint is to catch bitfield insertion we may
1819       // want to widen the pattern if we want to grab general bitfied
1820       // move case
1821       if (Width <= 0)
1822         continue;
1823
1824       // If the mask on the insertee is correct, we have a BFXIL operation. We
1825       // can share the ImmR and ImmS values from the already-computed UBFM.
1826     } else if (isBitfieldPositioningOp(CurDAG, SDValue(OrOpd0, 0), Src,
1827                                        DstLSB, Width)) {
1828       ImmR = (VT.getSizeInBits() - DstLSB) % VT.getSizeInBits();
1829       ImmS = Width - 1;
1830     } else
1831       continue;
1832
1833     // Check the second part of the pattern
1834     EVT VT = OrOpd1->getValueType(0);
1835     assert((VT == MVT::i32 || VT == MVT::i64) && "unexpected OR operand");
1836
1837     // Compute the Known Zero for the candidate of the first operand.
1838     // This allows to catch more general case than just looking for
1839     // AND with imm. Indeed, simplify-demanded-bits may have removed
1840     // the AND instruction because it proves it was useless.
1841     APInt KnownZero, KnownOne;
1842     CurDAG->ComputeMaskedBits(OrOpd1Val, KnownZero, KnownOne);
1843
1844     // Check if there is enough room for the second operand to appear
1845     // in the first one
1846     APInt BitsToBeInserted =
1847         APInt::getBitsSet(KnownZero.getBitWidth(), DstLSB, DstLSB + Width);
1848
1849     if ((BitsToBeInserted & ~KnownZero) != 0)
1850       continue;
1851
1852     // Set the first operand
1853     uint64_t Imm;
1854     if (isOpcWithIntImmediate(OrOpd1, ISD::AND, Imm) &&
1855         isBitfieldDstMask(Imm, BitsToBeInserted, NumberOfIgnoredHighBits, VT))
1856       // In that case, we can eliminate the AND
1857       Dst = OrOpd1->getOperand(0);
1858     else
1859       // Maybe the AND has been removed by simplify-demanded-bits
1860       // or is useful because it discards more bits
1861       Dst = OrOpd1Val;
1862
1863     // both parts match
1864     return true;
1865   }
1866
1867   return false;
1868 }
1869
1870 SDNode *ARM64DAGToDAGISel::SelectBitfieldInsertOp(SDNode *N) {
1871   if (N->getOpcode() != ISD::OR)
1872     return nullptr;
1873
1874   unsigned Opc;
1875   unsigned LSB, MSB;
1876   SDValue Opd0, Opd1;
1877
1878   if (!isBitfieldInsertOpFromOr(N, Opc, Opd0, Opd1, LSB, MSB, CurDAG))
1879     return nullptr;
1880
1881   EVT VT = N->getValueType(0);
1882   SDValue Ops[] = { Opd0,
1883                     Opd1,
1884                     CurDAG->getTargetConstant(LSB, VT),
1885                     CurDAG->getTargetConstant(MSB, VT) };
1886   return CurDAG->SelectNodeTo(N, Opc, VT, Ops);
1887 }
1888
1889 SDNode *ARM64DAGToDAGISel::SelectLIBM(SDNode *N) {
1890   EVT VT = N->getValueType(0);
1891   unsigned Variant;
1892   unsigned Opc;
1893   unsigned FRINTXOpcs[] = { ARM64::FRINTXSr, ARM64::FRINTXDr };
1894
1895   if (VT == MVT::f32) {
1896     Variant = 0;
1897   } else if (VT == MVT::f64) {
1898     Variant = 1;
1899   } else
1900     return nullptr; // Unrecognized argument type. Fall back on default codegen.
1901
1902   // Pick the FRINTX variant needed to set the flags.
1903   unsigned FRINTXOpc = FRINTXOpcs[Variant];
1904
1905   switch (N->getOpcode()) {
1906   default:
1907     return nullptr; // Unrecognized libm ISD node. Fall back on default codegen.
1908   case ISD::FCEIL: {
1909     unsigned FRINTPOpcs[] = { ARM64::FRINTPSr, ARM64::FRINTPDr };
1910     Opc = FRINTPOpcs[Variant];
1911     break;
1912   }
1913   case ISD::FFLOOR: {
1914     unsigned FRINTMOpcs[] = { ARM64::FRINTMSr, ARM64::FRINTMDr };
1915     Opc = FRINTMOpcs[Variant];
1916     break;
1917   }
1918   case ISD::FTRUNC: {
1919     unsigned FRINTZOpcs[] = { ARM64::FRINTZSr, ARM64::FRINTZDr };
1920     Opc = FRINTZOpcs[Variant];
1921     break;
1922   }
1923   case ISD::FROUND: {
1924     unsigned FRINTAOpcs[] = { ARM64::FRINTASr, ARM64::FRINTADr };
1925     Opc = FRINTAOpcs[Variant];
1926     break;
1927   }
1928   }
1929
1930   SDLoc dl(N);
1931   SDValue In = N->getOperand(0);
1932   SmallVector<SDValue, 2> Ops;
1933   Ops.push_back(In);
1934
1935   if (!TM.Options.UnsafeFPMath) {
1936     SDNode *FRINTX = CurDAG->getMachineNode(FRINTXOpc, dl, VT, MVT::Glue, In);
1937     Ops.push_back(SDValue(FRINTX, 1));
1938   }
1939
1940   return CurDAG->getMachineNode(Opc, dl, VT, Ops);
1941 }
1942
1943 bool
1944 ARM64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
1945                                               unsigned RegWidth) {
1946   APFloat FVal(0.0);
1947   if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
1948     FVal = CN->getValueAPF();
1949   else if (LoadSDNode *LN = dyn_cast<LoadSDNode>(N)) {
1950     // Some otherwise illegal constants are allowed in this case.
1951     if (LN->getOperand(1).getOpcode() != ARM64ISD::ADDlow ||
1952         !isa<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1)))
1953       return false;
1954
1955     ConstantPoolSDNode *CN =
1956         dyn_cast<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1));
1957     FVal = cast<ConstantFP>(CN->getConstVal())->getValueAPF();
1958   } else
1959     return false;
1960
1961   // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
1962   // is between 1 and 32 for a destination w-register, or 1 and 64 for an
1963   // x-register.
1964   //
1965   // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
1966   // want THIS_NODE to be 2^fbits. This is much easier to deal with using
1967   // integers.
1968   bool IsExact;
1969
1970   // fbits is between 1 and 64 in the worst-case, which means the fmul
1971   // could have 2^64 as an actual operand. Need 65 bits of precision.
1972   APSInt IntVal(65, true);
1973   FVal.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
1974
1975   // N.b. isPowerOf2 also checks for > 0.
1976   if (!IsExact || !IntVal.isPowerOf2()) return false;
1977   unsigned FBits = IntVal.logBase2();
1978
1979   // Checks above should have guaranteed that we haven't lost information in
1980   // finding FBits, but it must still be in range.
1981   if (FBits == 0 || FBits > RegWidth) return false;
1982
1983   FixedPos = CurDAG->getTargetConstant(FBits, MVT::i32);
1984   return true;
1985 }
1986
1987 SDNode *ARM64DAGToDAGISel::Select(SDNode *Node) {
1988   // Dump information about the Node being selected
1989   DEBUG(errs() << "Selecting: ");
1990   DEBUG(Node->dump(CurDAG));
1991   DEBUG(errs() << "\n");
1992
1993   // If we have a custom node, we already have selected!
1994   if (Node->isMachineOpcode()) {
1995     DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
1996     Node->setNodeId(-1);
1997     return nullptr;
1998   }
1999
2000   // Few custom selection stuff.
2001   SDNode *ResNode = nullptr;
2002   EVT VT = Node->getValueType(0);
2003
2004   switch (Node->getOpcode()) {
2005   default:
2006     break;
2007
2008   case ISD::ADD:
2009     if (SDNode *I = SelectMLAV64LaneV128(Node))
2010       return I;
2011     break;
2012
2013   case ISD::LOAD: {
2014     // Try to select as an indexed load. Fall through to normal processing
2015     // if we can't.
2016     bool Done = false;
2017     SDNode *I = SelectIndexedLoad(Node, Done);
2018     if (Done)
2019       return I;
2020     break;
2021   }
2022
2023   case ISD::SRL:
2024   case ISD::AND:
2025   case ISD::SRA:
2026     if (SDNode *I = SelectBitfieldExtractOp(Node))
2027       return I;
2028     break;
2029
2030   case ISD::OR:
2031     if (SDNode *I = SelectBitfieldInsertOp(Node))
2032       return I;
2033     break;
2034
2035   case ISD::EXTRACT_VECTOR_ELT: {
2036     // Extracting lane zero is a special case where we can just use a plain
2037     // EXTRACT_SUBREG instruction, which will become FMOV. This is easier for
2038     // the rest of the compiler, especially the register allocator and copyi
2039     // propagation, to reason about, so is preferred when it's possible to
2040     // use it.
2041     ConstantSDNode *LaneNode = cast<ConstantSDNode>(Node->getOperand(1));
2042     // Bail and use the default Select() for non-zero lanes.
2043     if (LaneNode->getZExtValue() != 0)
2044       break;
2045     // If the element type is not the same as the result type, likewise
2046     // bail and use the default Select(), as there's more to do than just
2047     // a cross-class COPY. This catches extracts of i8 and i16 elements
2048     // since they will need an explicit zext.
2049     if (VT != Node->getOperand(0).getValueType().getVectorElementType())
2050       break;
2051     unsigned SubReg;
2052     switch (Node->getOperand(0)
2053                 .getValueType()
2054                 .getVectorElementType()
2055                 .getSizeInBits()) {
2056     default:
2057       assert(0 && "Unexpected vector element type!");
2058     case 64:
2059       SubReg = ARM64::dsub;
2060       break;
2061     case 32:
2062       SubReg = ARM64::ssub;
2063       break;
2064     case 16: // FALLTHROUGH
2065     case 8:
2066       llvm_unreachable("unexpected zext-requiring extract element!");
2067     }
2068     SDValue Extract = CurDAG->getTargetExtractSubreg(SubReg, SDLoc(Node), VT,
2069                                                      Node->getOperand(0));
2070     DEBUG(dbgs() << "ISEL: Custom selection!\n=> ");
2071     DEBUG(Extract->dumpr(CurDAG));
2072     DEBUG(dbgs() << "\n");
2073     return Extract.getNode();
2074   }
2075   case ISD::Constant: {
2076     // Materialize zero constants as copies from WZR/XZR.  This allows
2077     // the coalescer to propagate these into other instructions.
2078     ConstantSDNode *ConstNode = cast<ConstantSDNode>(Node);
2079     if (ConstNode->isNullValue()) {
2080       if (VT == MVT::i32)
2081         return CurDAG->getCopyFromReg(CurDAG->getEntryNode(), SDLoc(Node),
2082                                       ARM64::WZR, MVT::i32).getNode();
2083       else if (VT == MVT::i64)
2084         return CurDAG->getCopyFromReg(CurDAG->getEntryNode(), SDLoc(Node),
2085                                       ARM64::XZR, MVT::i64).getNode();
2086     }
2087     break;
2088   }
2089
2090   case ISD::FrameIndex: {
2091     // Selects to ADDXri FI, 0 which in turn will become ADDXri SP, imm.
2092     int FI = cast<FrameIndexSDNode>(Node)->getIndex();
2093     unsigned Shifter = ARM64_AM::getShifterImm(ARM64_AM::LSL, 0);
2094     const TargetLowering *TLI = getTargetLowering();
2095     SDValue TFI = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy());
2096     SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, MVT::i32),
2097                       CurDAG->getTargetConstant(Shifter, MVT::i32) };
2098     return CurDAG->SelectNodeTo(Node, ARM64::ADDXri, MVT::i64, Ops);
2099   }
2100   case ISD::INTRINSIC_W_CHAIN: {
2101     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2102     switch (IntNo) {
2103     default:
2104       break;
2105     case Intrinsic::arm64_ldaxp:
2106     case Intrinsic::arm64_ldxp: {
2107       unsigned Op =
2108           IntNo == Intrinsic::arm64_ldaxp ? ARM64::LDAXPX : ARM64::LDXPX;
2109       SDValue MemAddr = Node->getOperand(2);
2110       SDLoc DL(Node);
2111       SDValue Chain = Node->getOperand(0);
2112
2113       SDNode *Ld = CurDAG->getMachineNode(Op, DL, MVT::i64, MVT::i64,
2114                                           MVT::Other, MemAddr, Chain);
2115
2116       // Transfer memoperands.
2117       MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2118       MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2119       cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
2120       return Ld;
2121     }
2122     case Intrinsic::arm64_stlxp:
2123     case Intrinsic::arm64_stxp: {
2124       unsigned Op =
2125           IntNo == Intrinsic::arm64_stlxp ? ARM64::STLXPX : ARM64::STXPX;
2126       SDLoc DL(Node);
2127       SDValue Chain = Node->getOperand(0);
2128       SDValue ValLo = Node->getOperand(2);
2129       SDValue ValHi = Node->getOperand(3);
2130       SDValue MemAddr = Node->getOperand(4);
2131
2132       // Place arguments in the right order.
2133       SmallVector<SDValue, 7> Ops;
2134       Ops.push_back(ValLo);
2135       Ops.push_back(ValHi);
2136       Ops.push_back(MemAddr);
2137       Ops.push_back(Chain);
2138
2139       SDNode *St = CurDAG->getMachineNode(Op, DL, MVT::i32, MVT::Other, Ops);
2140       // Transfer memoperands.
2141       MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2142       MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2143       cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
2144
2145       return St;
2146     }
2147     case Intrinsic::arm64_neon_ld1x2:
2148       if (VT == MVT::v8i8)
2149         return SelectLoad(Node, 2, ARM64::LD1Twov8b, ARM64::dsub0);
2150       else if (VT == MVT::v16i8)
2151         return SelectLoad(Node, 2, ARM64::LD1Twov16b, ARM64::qsub0);
2152       else if (VT == MVT::v4i16)
2153         return SelectLoad(Node, 2, ARM64::LD1Twov4h, ARM64::dsub0);
2154       else if (VT == MVT::v8i16)
2155         return SelectLoad(Node, 2, ARM64::LD1Twov8h, ARM64::qsub0);
2156       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2157         return SelectLoad(Node, 2, ARM64::LD1Twov2s, ARM64::dsub0);
2158       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2159         return SelectLoad(Node, 2, ARM64::LD1Twov4s, ARM64::qsub0);
2160       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2161         return SelectLoad(Node, 2, ARM64::LD1Twov1d, ARM64::dsub0);
2162       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2163         return SelectLoad(Node, 2, ARM64::LD1Twov2d, ARM64::qsub0);
2164       break;
2165     case Intrinsic::arm64_neon_ld1x3:
2166       if (VT == MVT::v8i8)
2167         return SelectLoad(Node, 3, ARM64::LD1Threev8b, ARM64::dsub0);
2168       else if (VT == MVT::v16i8)
2169         return SelectLoad(Node, 3, ARM64::LD1Threev16b, ARM64::qsub0);
2170       else if (VT == MVT::v4i16)
2171         return SelectLoad(Node, 3, ARM64::LD1Threev4h, ARM64::dsub0);
2172       else if (VT == MVT::v8i16)
2173         return SelectLoad(Node, 3, ARM64::LD1Threev8h, ARM64::qsub0);
2174       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2175         return SelectLoad(Node, 3, ARM64::LD1Threev2s, ARM64::dsub0);
2176       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2177         return SelectLoad(Node, 3, ARM64::LD1Threev4s, ARM64::qsub0);
2178       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2179         return SelectLoad(Node, 3, ARM64::LD1Threev1d, ARM64::dsub0);
2180       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2181         return SelectLoad(Node, 3, ARM64::LD1Threev2d, ARM64::qsub0);
2182       break;
2183     case Intrinsic::arm64_neon_ld1x4:
2184       if (VT == MVT::v8i8)
2185         return SelectLoad(Node, 4, ARM64::LD1Fourv8b, ARM64::dsub0);
2186       else if (VT == MVT::v16i8)
2187         return SelectLoad(Node, 4, ARM64::LD1Fourv16b, ARM64::qsub0);
2188       else if (VT == MVT::v4i16)
2189         return SelectLoad(Node, 4, ARM64::LD1Fourv4h, ARM64::dsub0);
2190       else if (VT == MVT::v8i16)
2191         return SelectLoad(Node, 4, ARM64::LD1Fourv8h, ARM64::qsub0);
2192       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2193         return SelectLoad(Node, 4, ARM64::LD1Fourv2s, ARM64::dsub0);
2194       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2195         return SelectLoad(Node, 4, ARM64::LD1Fourv4s, ARM64::qsub0);
2196       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2197         return SelectLoad(Node, 4, ARM64::LD1Fourv1d, ARM64::dsub0);
2198       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2199         return SelectLoad(Node, 4, ARM64::LD1Fourv2d, ARM64::qsub0);
2200       break;
2201     case Intrinsic::arm64_neon_ld2:
2202       if (VT == MVT::v8i8)
2203         return SelectLoad(Node, 2, ARM64::LD2Twov8b, ARM64::dsub0);
2204       else if (VT == MVT::v16i8)
2205         return SelectLoad(Node, 2, ARM64::LD2Twov16b, ARM64::qsub0);
2206       else if (VT == MVT::v4i16)
2207         return SelectLoad(Node, 2, ARM64::LD2Twov4h, ARM64::dsub0);
2208       else if (VT == MVT::v8i16)
2209         return SelectLoad(Node, 2, ARM64::LD2Twov8h, ARM64::qsub0);
2210       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2211         return SelectLoad(Node, 2, ARM64::LD2Twov2s, ARM64::dsub0);
2212       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2213         return SelectLoad(Node, 2, ARM64::LD2Twov4s, ARM64::qsub0);
2214       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2215         return SelectLoad(Node, 2, ARM64::LD1Twov1d, ARM64::dsub0);
2216       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2217         return SelectLoad(Node, 2, ARM64::LD2Twov2d, ARM64::qsub0);
2218       break;
2219     case Intrinsic::arm64_neon_ld3:
2220       if (VT == MVT::v8i8)
2221         return SelectLoad(Node, 3, ARM64::LD3Threev8b, ARM64::dsub0);
2222       else if (VT == MVT::v16i8)
2223         return SelectLoad(Node, 3, ARM64::LD3Threev16b, ARM64::qsub0);
2224       else if (VT == MVT::v4i16)
2225         return SelectLoad(Node, 3, ARM64::LD3Threev4h, ARM64::dsub0);
2226       else if (VT == MVT::v8i16)
2227         return SelectLoad(Node, 3, ARM64::LD3Threev8h, ARM64::qsub0);
2228       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2229         return SelectLoad(Node, 3, ARM64::LD3Threev2s, ARM64::dsub0);
2230       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2231         return SelectLoad(Node, 3, ARM64::LD3Threev4s, ARM64::qsub0);
2232       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2233         return SelectLoad(Node, 3, ARM64::LD1Threev1d, ARM64::dsub0);
2234       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2235         return SelectLoad(Node, 3, ARM64::LD3Threev2d, ARM64::qsub0);
2236       break;
2237     case Intrinsic::arm64_neon_ld4:
2238       if (VT == MVT::v8i8)
2239         return SelectLoad(Node, 4, ARM64::LD4Fourv8b, ARM64::dsub0);
2240       else if (VT == MVT::v16i8)
2241         return SelectLoad(Node, 4, ARM64::LD4Fourv16b, ARM64::qsub0);
2242       else if (VT == MVT::v4i16)
2243         return SelectLoad(Node, 4, ARM64::LD4Fourv4h, ARM64::dsub0);
2244       else if (VT == MVT::v8i16)
2245         return SelectLoad(Node, 4, ARM64::LD4Fourv8h, ARM64::qsub0);
2246       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2247         return SelectLoad(Node, 4, ARM64::LD4Fourv2s, ARM64::dsub0);
2248       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2249         return SelectLoad(Node, 4, ARM64::LD4Fourv4s, ARM64::qsub0);
2250       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2251         return SelectLoad(Node, 4, ARM64::LD1Fourv1d, ARM64::dsub0);
2252       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2253         return SelectLoad(Node, 4, ARM64::LD4Fourv2d, ARM64::qsub0);
2254       break;
2255     case Intrinsic::arm64_neon_ld2r:
2256       if (VT == MVT::v8i8)
2257         return SelectLoad(Node, 2, ARM64::LD2Rv8b, ARM64::dsub0);
2258       else if (VT == MVT::v16i8)
2259         return SelectLoad(Node, 2, ARM64::LD2Rv16b, ARM64::qsub0);
2260       else if (VT == MVT::v4i16)
2261         return SelectLoad(Node, 2, ARM64::LD2Rv4h, ARM64::dsub0);
2262       else if (VT == MVT::v8i16)
2263         return SelectLoad(Node, 2, ARM64::LD2Rv8h, ARM64::qsub0);
2264       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2265         return SelectLoad(Node, 2, ARM64::LD2Rv2s, ARM64::dsub0);
2266       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2267         return SelectLoad(Node, 2, ARM64::LD2Rv4s, ARM64::qsub0);
2268       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2269         return SelectLoad(Node, 2, ARM64::LD2Rv1d, ARM64::dsub0);
2270       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2271         return SelectLoad(Node, 2, ARM64::LD2Rv2d, ARM64::qsub0);
2272       break;
2273     case Intrinsic::arm64_neon_ld3r:
2274       if (VT == MVT::v8i8)
2275         return SelectLoad(Node, 3, ARM64::LD3Rv8b, ARM64::dsub0);
2276       else if (VT == MVT::v16i8)
2277         return SelectLoad(Node, 3, ARM64::LD3Rv16b, ARM64::qsub0);
2278       else if (VT == MVT::v4i16)
2279         return SelectLoad(Node, 3, ARM64::LD3Rv4h, ARM64::dsub0);
2280       else if (VT == MVT::v8i16)
2281         return SelectLoad(Node, 3, ARM64::LD3Rv8h, ARM64::qsub0);
2282       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2283         return SelectLoad(Node, 3, ARM64::LD3Rv2s, ARM64::dsub0);
2284       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2285         return SelectLoad(Node, 3, ARM64::LD3Rv4s, ARM64::qsub0);
2286       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2287         return SelectLoad(Node, 3, ARM64::LD3Rv1d, ARM64::dsub0);
2288       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2289         return SelectLoad(Node, 3, ARM64::LD3Rv2d, ARM64::qsub0);
2290       break;
2291     case Intrinsic::arm64_neon_ld4r:
2292       if (VT == MVT::v8i8)
2293         return SelectLoad(Node, 4, ARM64::LD4Rv8b, ARM64::dsub0);
2294       else if (VT == MVT::v16i8)
2295         return SelectLoad(Node, 4, ARM64::LD4Rv16b, ARM64::qsub0);
2296       else if (VT == MVT::v4i16)
2297         return SelectLoad(Node, 4, ARM64::LD4Rv4h, ARM64::dsub0);
2298       else if (VT == MVT::v8i16)
2299         return SelectLoad(Node, 4, ARM64::LD4Rv8h, ARM64::qsub0);
2300       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2301         return SelectLoad(Node, 4, ARM64::LD4Rv2s, ARM64::dsub0);
2302       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2303         return SelectLoad(Node, 4, ARM64::LD4Rv4s, ARM64::qsub0);
2304       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2305         return SelectLoad(Node, 4, ARM64::LD4Rv1d, ARM64::dsub0);
2306       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2307         return SelectLoad(Node, 4, ARM64::LD4Rv2d, ARM64::qsub0);
2308       break;
2309     case Intrinsic::arm64_neon_ld2lane:
2310       if (VT == MVT::v16i8 || VT == MVT::v8i8)
2311         return SelectLoadLane(Node, 2, ARM64::LD2i8);
2312       else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2313         return SelectLoadLane(Node, 2, ARM64::LD2i16);
2314       else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2315                VT == MVT::v2f32)
2316         return SelectLoadLane(Node, 2, ARM64::LD2i32);
2317       else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2318                VT == MVT::v1f64)
2319         return SelectLoadLane(Node, 2, ARM64::LD2i64);
2320       break;
2321     case Intrinsic::arm64_neon_ld3lane:
2322       if (VT == MVT::v16i8 || VT == MVT::v8i8)
2323         return SelectLoadLane(Node, 3, ARM64::LD3i8);
2324       else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2325         return SelectLoadLane(Node, 3, ARM64::LD3i16);
2326       else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2327                VT == MVT::v2f32)
2328         return SelectLoadLane(Node, 3, ARM64::LD3i32);
2329       else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2330                VT == MVT::v1f64)
2331         return SelectLoadLane(Node, 3, ARM64::LD3i64);
2332       break;
2333     case Intrinsic::arm64_neon_ld4lane:
2334       if (VT == MVT::v16i8 || VT == MVT::v8i8)
2335         return SelectLoadLane(Node, 4, ARM64::LD4i8);
2336       else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2337         return SelectLoadLane(Node, 4, ARM64::LD4i16);
2338       else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2339                VT == MVT::v2f32)
2340         return SelectLoadLane(Node, 4, ARM64::LD4i32);
2341       else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2342                VT == MVT::v1f64)
2343         return SelectLoadLane(Node, 4, ARM64::LD4i64);
2344       break;
2345     }
2346   } break;
2347   case ISD::INTRINSIC_WO_CHAIN: {
2348     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
2349     switch (IntNo) {
2350     default:
2351       break;
2352     case Intrinsic::arm64_neon_tbl2:
2353       return SelectTable(Node, 2, VT == MVT::v8i8 ? ARM64::TBLv8i8Two
2354                                                   : ARM64::TBLv16i8Two,
2355                          false);
2356     case Intrinsic::arm64_neon_tbl3:
2357       return SelectTable(Node, 3, VT == MVT::v8i8 ? ARM64::TBLv8i8Three
2358                                                   : ARM64::TBLv16i8Three,
2359                          false);
2360     case Intrinsic::arm64_neon_tbl4:
2361       return SelectTable(Node, 4, VT == MVT::v8i8 ? ARM64::TBLv8i8Four
2362                                                   : ARM64::TBLv16i8Four,
2363                          false);
2364     case Intrinsic::arm64_neon_tbx2:
2365       return SelectTable(Node, 2, VT == MVT::v8i8 ? ARM64::TBXv8i8Two
2366                                                   : ARM64::TBXv16i8Two,
2367                          true);
2368     case Intrinsic::arm64_neon_tbx3:
2369       return SelectTable(Node, 3, VT == MVT::v8i8 ? ARM64::TBXv8i8Three
2370                                                   : ARM64::TBXv16i8Three,
2371                          true);
2372     case Intrinsic::arm64_neon_tbx4:
2373       return SelectTable(Node, 4, VT == MVT::v8i8 ? ARM64::TBXv8i8Four
2374                                                   : ARM64::TBXv16i8Four,
2375                          true);
2376     case Intrinsic::arm64_neon_smull:
2377     case Intrinsic::arm64_neon_umull:
2378       if (SDNode *N = SelectMULLV64LaneV128(IntNo, Node))
2379         return N;
2380       break;
2381     }
2382     break;
2383   }
2384   case ISD::INTRINSIC_VOID: {
2385     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2386     if (Node->getNumOperands() >= 3)
2387       VT = Node->getOperand(2)->getValueType(0);
2388     switch (IntNo) {
2389     default:
2390       break;
2391     case Intrinsic::arm64_neon_st1x2: {
2392       if (VT == MVT::v8i8)
2393         return SelectStore(Node, 2, ARM64::ST1Twov8b);
2394       else if (VT == MVT::v16i8)
2395         return SelectStore(Node, 2, ARM64::ST1Twov16b);
2396       else if (VT == MVT::v4i16)
2397         return SelectStore(Node, 2, ARM64::ST1Twov4h);
2398       else if (VT == MVT::v8i16)
2399         return SelectStore(Node, 2, ARM64::ST1Twov8h);
2400       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2401         return SelectStore(Node, 2, ARM64::ST1Twov2s);
2402       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2403         return SelectStore(Node, 2, ARM64::ST1Twov4s);
2404       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2405         return SelectStore(Node, 2, ARM64::ST1Twov2d);
2406       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2407         return SelectStore(Node, 2, ARM64::ST1Twov1d);
2408       break;
2409     }
2410     case Intrinsic::arm64_neon_st1x3: {
2411       if (VT == MVT::v8i8)
2412         return SelectStore(Node, 3, ARM64::ST1Threev8b);
2413       else if (VT == MVT::v16i8)
2414         return SelectStore(Node, 3, ARM64::ST1Threev16b);
2415       else if (VT == MVT::v4i16)
2416         return SelectStore(Node, 3, ARM64::ST1Threev4h);
2417       else if (VT == MVT::v8i16)
2418         return SelectStore(Node, 3, ARM64::ST1Threev8h);
2419       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2420         return SelectStore(Node, 3, ARM64::ST1Threev2s);
2421       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2422         return SelectStore(Node, 3, ARM64::ST1Threev4s);
2423       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2424         return SelectStore(Node, 3, ARM64::ST1Threev2d);
2425       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2426         return SelectStore(Node, 3, ARM64::ST1Threev1d);
2427       break;
2428     }
2429     case Intrinsic::arm64_neon_st1x4: {
2430       if (VT == MVT::v8i8)
2431         return SelectStore(Node, 4, ARM64::ST1Fourv8b);
2432       else if (VT == MVT::v16i8)
2433         return SelectStore(Node, 4, ARM64::ST1Fourv16b);
2434       else if (VT == MVT::v4i16)
2435         return SelectStore(Node, 4, ARM64::ST1Fourv4h);
2436       else if (VT == MVT::v8i16)
2437         return SelectStore(Node, 4, ARM64::ST1Fourv8h);
2438       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2439         return SelectStore(Node, 4, ARM64::ST1Fourv2s);
2440       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2441         return SelectStore(Node, 4, ARM64::ST1Fourv4s);
2442       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2443         return SelectStore(Node, 4, ARM64::ST1Fourv2d);
2444       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2445         return SelectStore(Node, 4, ARM64::ST1Fourv1d);
2446       break;
2447     }
2448     case Intrinsic::arm64_neon_st2: {
2449       if (VT == MVT::v8i8)
2450         return SelectStore(Node, 2, ARM64::ST2Twov8b);
2451       else if (VT == MVT::v16i8)
2452         return SelectStore(Node, 2, ARM64::ST2Twov16b);
2453       else if (VT == MVT::v4i16)
2454         return SelectStore(Node, 2, ARM64::ST2Twov4h);
2455       else if (VT == MVT::v8i16)
2456         return SelectStore(Node, 2, ARM64::ST2Twov8h);
2457       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2458         return SelectStore(Node, 2, ARM64::ST2Twov2s);
2459       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2460         return SelectStore(Node, 2, ARM64::ST2Twov4s);
2461       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2462         return SelectStore(Node, 2, ARM64::ST2Twov2d);
2463       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2464         return SelectStore(Node, 2, ARM64::ST1Twov1d);
2465       break;
2466     }
2467     case Intrinsic::arm64_neon_st3: {
2468       if (VT == MVT::v8i8)
2469         return SelectStore(Node, 3, ARM64::ST3Threev8b);
2470       else if (VT == MVT::v16i8)
2471         return SelectStore(Node, 3, ARM64::ST3Threev16b);
2472       else if (VT == MVT::v4i16)
2473         return SelectStore(Node, 3, ARM64::ST3Threev4h);
2474       else if (VT == MVT::v8i16)
2475         return SelectStore(Node, 3, ARM64::ST3Threev8h);
2476       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2477         return SelectStore(Node, 3, ARM64::ST3Threev2s);
2478       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2479         return SelectStore(Node, 3, ARM64::ST3Threev4s);
2480       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2481         return SelectStore(Node, 3, ARM64::ST3Threev2d);
2482       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2483         return SelectStore(Node, 3, ARM64::ST1Threev1d);
2484       break;
2485     }
2486     case Intrinsic::arm64_neon_st4: {
2487       if (VT == MVT::v8i8)
2488         return SelectStore(Node, 4, ARM64::ST4Fourv8b);
2489       else if (VT == MVT::v16i8)
2490         return SelectStore(Node, 4, ARM64::ST4Fourv16b);
2491       else if (VT == MVT::v4i16)
2492         return SelectStore(Node, 4, ARM64::ST4Fourv4h);
2493       else if (VT == MVT::v8i16)
2494         return SelectStore(Node, 4, ARM64::ST4Fourv8h);
2495       else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2496         return SelectStore(Node, 4, ARM64::ST4Fourv2s);
2497       else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2498         return SelectStore(Node, 4, ARM64::ST4Fourv4s);
2499       else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2500         return SelectStore(Node, 4, ARM64::ST4Fourv2d);
2501       else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2502         return SelectStore(Node, 4, ARM64::ST1Fourv1d);
2503       break;
2504     }
2505     case Intrinsic::arm64_neon_st2lane: {
2506       if (VT == MVT::v16i8 || VT == MVT::v8i8)
2507         return SelectStoreLane(Node, 2, ARM64::ST2i8);
2508       else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2509         return SelectStoreLane(Node, 2, ARM64::ST2i16);
2510       else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2511                VT == MVT::v2f32)
2512         return SelectStoreLane(Node, 2, ARM64::ST2i32);
2513       else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2514                VT == MVT::v1f64)
2515         return SelectStoreLane(Node, 2, ARM64::ST2i64);
2516       break;
2517     }
2518     case Intrinsic::arm64_neon_st3lane: {
2519       if (VT == MVT::v16i8 || VT == MVT::v8i8)
2520         return SelectStoreLane(Node, 3, ARM64::ST3i8);
2521       else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2522         return SelectStoreLane(Node, 3, ARM64::ST3i16);
2523       else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2524                VT == MVT::v2f32)
2525         return SelectStoreLane(Node, 3, ARM64::ST3i32);
2526       else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2527                VT == MVT::v1f64)
2528         return SelectStoreLane(Node, 3, ARM64::ST3i64);
2529       break;
2530     }
2531     case Intrinsic::arm64_neon_st4lane: {
2532       if (VT == MVT::v16i8 || VT == MVT::v8i8)
2533         return SelectStoreLane(Node, 4, ARM64::ST4i8);
2534       else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2535         return SelectStoreLane(Node, 4, ARM64::ST4i16);
2536       else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2537                VT == MVT::v2f32)
2538         return SelectStoreLane(Node, 4, ARM64::ST4i32);
2539       else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2540                VT == MVT::v1f64)
2541         return SelectStoreLane(Node, 4, ARM64::ST4i64);
2542       break;
2543     }
2544     }
2545   }
2546   case ARM64ISD::LD2post: {
2547     if (VT == MVT::v8i8)
2548       return SelectPostLoad(Node, 2, ARM64::LD2Twov8b_POST, ARM64::dsub0);
2549     else if (VT == MVT::v16i8)
2550       return SelectPostLoad(Node, 2, ARM64::LD2Twov16b_POST, ARM64::qsub0);
2551     else if (VT == MVT::v4i16)
2552       return SelectPostLoad(Node, 2, ARM64::LD2Twov4h_POST, ARM64::dsub0);
2553     else if (VT == MVT::v8i16)
2554       return SelectPostLoad(Node, 2, ARM64::LD2Twov8h_POST, ARM64::qsub0);
2555     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2556       return SelectPostLoad(Node, 2, ARM64::LD2Twov2s_POST, ARM64::dsub0);
2557     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2558       return SelectPostLoad(Node, 2, ARM64::LD2Twov4s_POST, ARM64::qsub0);
2559     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2560       return SelectPostLoad(Node, 2, ARM64::LD1Twov1d_POST, ARM64::dsub0);
2561     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2562       return SelectPostLoad(Node, 2, ARM64::LD2Twov2d_POST, ARM64::qsub0);
2563     break;
2564   }
2565   case ARM64ISD::LD3post: {
2566     if (VT == MVT::v8i8)
2567       return SelectPostLoad(Node, 3, ARM64::LD3Threev8b_POST, ARM64::dsub0);
2568     else if (VT == MVT::v16i8)
2569       return SelectPostLoad(Node, 3, ARM64::LD3Threev16b_POST, ARM64::qsub0);
2570     else if (VT == MVT::v4i16)
2571       return SelectPostLoad(Node, 3, ARM64::LD3Threev4h_POST, ARM64::dsub0);
2572     else if (VT == MVT::v8i16)
2573       return SelectPostLoad(Node, 3, ARM64::LD3Threev8h_POST, ARM64::qsub0);
2574     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2575       return SelectPostLoad(Node, 3, ARM64::LD3Threev2s_POST, ARM64::dsub0);
2576     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2577       return SelectPostLoad(Node, 3, ARM64::LD3Threev4s_POST, ARM64::qsub0);
2578     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2579       return SelectPostLoad(Node, 3, ARM64::LD1Threev1d_POST, ARM64::dsub0);
2580     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2581       return SelectPostLoad(Node, 3, ARM64::LD3Threev2d_POST, ARM64::qsub0);
2582     break;
2583   }
2584   case ARM64ISD::LD4post: {
2585     if (VT == MVT::v8i8)
2586       return SelectPostLoad(Node, 4, ARM64::LD4Fourv8b_POST, ARM64::dsub0);
2587     else if (VT == MVT::v16i8)
2588       return SelectPostLoad(Node, 4, ARM64::LD4Fourv16b_POST, ARM64::qsub0);
2589     else if (VT == MVT::v4i16)
2590       return SelectPostLoad(Node, 4, ARM64::LD4Fourv4h_POST, ARM64::dsub0);
2591     else if (VT == MVT::v8i16)
2592       return SelectPostLoad(Node, 4, ARM64::LD4Fourv8h_POST, ARM64::qsub0);
2593     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2594       return SelectPostLoad(Node, 4, ARM64::LD4Fourv2s_POST, ARM64::dsub0);
2595     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2596       return SelectPostLoad(Node, 4, ARM64::LD4Fourv4s_POST, ARM64::qsub0);
2597     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2598       return SelectPostLoad(Node, 4, ARM64::LD1Fourv1d_POST, ARM64::dsub0);
2599     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2600       return SelectPostLoad(Node, 4, ARM64::LD4Fourv2d_POST, ARM64::qsub0);
2601     break;
2602   }
2603   case ARM64ISD::LD1x2post: {
2604     if (VT == MVT::v8i8)
2605       return SelectPostLoad(Node, 2, ARM64::LD1Twov8b_POST, ARM64::dsub0);
2606     else if (VT == MVT::v16i8)
2607       return SelectPostLoad(Node, 2, ARM64::LD1Twov16b_POST, ARM64::qsub0);
2608     else if (VT == MVT::v4i16)
2609       return SelectPostLoad(Node, 2, ARM64::LD1Twov4h_POST, ARM64::dsub0);
2610     else if (VT == MVT::v8i16)
2611       return SelectPostLoad(Node, 2, ARM64::LD1Twov8h_POST, ARM64::qsub0);
2612     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2613       return SelectPostLoad(Node, 2, ARM64::LD1Twov2s_POST, ARM64::dsub0);
2614     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2615       return SelectPostLoad(Node, 2, ARM64::LD1Twov4s_POST, ARM64::qsub0);
2616     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2617       return SelectPostLoad(Node, 2, ARM64::LD1Twov1d_POST, ARM64::dsub0);
2618     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2619       return SelectPostLoad(Node, 2, ARM64::LD1Twov2d_POST, ARM64::qsub0);
2620     break;
2621   }
2622   case ARM64ISD::LD1x3post: {
2623     if (VT == MVT::v8i8)
2624       return SelectPostLoad(Node, 3, ARM64::LD1Threev8b_POST, ARM64::dsub0);
2625     else if (VT == MVT::v16i8)
2626       return SelectPostLoad(Node, 3, ARM64::LD1Threev16b_POST, ARM64::qsub0);
2627     else if (VT == MVT::v4i16)
2628       return SelectPostLoad(Node, 3, ARM64::LD1Threev4h_POST, ARM64::dsub0);
2629     else if (VT == MVT::v8i16)
2630       return SelectPostLoad(Node, 3, ARM64::LD1Threev8h_POST, ARM64::qsub0);
2631     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2632       return SelectPostLoad(Node, 3, ARM64::LD1Threev2s_POST, ARM64::dsub0);
2633     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2634       return SelectPostLoad(Node, 3, ARM64::LD1Threev4s_POST, ARM64::qsub0);
2635     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2636       return SelectPostLoad(Node, 3, ARM64::LD1Threev1d_POST, ARM64::dsub0);
2637     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2638       return SelectPostLoad(Node, 3, ARM64::LD1Threev2d_POST, ARM64::qsub0);
2639     break;
2640   }
2641   case ARM64ISD::LD1x4post: {
2642     if (VT == MVT::v8i8)
2643       return SelectPostLoad(Node, 4, ARM64::LD1Fourv8b_POST, ARM64::dsub0);
2644     else if (VT == MVT::v16i8)
2645       return SelectPostLoad(Node, 4, ARM64::LD1Fourv16b_POST, ARM64::qsub0);
2646     else if (VT == MVT::v4i16)
2647       return SelectPostLoad(Node, 4, ARM64::LD1Fourv4h_POST, ARM64::dsub0);
2648     else if (VT == MVT::v8i16)
2649       return SelectPostLoad(Node, 4, ARM64::LD1Fourv8h_POST, ARM64::qsub0);
2650     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2651       return SelectPostLoad(Node, 4, ARM64::LD1Fourv2s_POST, ARM64::dsub0);
2652     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2653       return SelectPostLoad(Node, 4, ARM64::LD1Fourv4s_POST, ARM64::qsub0);
2654     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2655       return SelectPostLoad(Node, 4, ARM64::LD1Fourv1d_POST, ARM64::dsub0);
2656     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2657       return SelectPostLoad(Node, 4, ARM64::LD1Fourv2d_POST, ARM64::qsub0);
2658     break;
2659   }
2660   case ARM64ISD::LD2DUPpost: {
2661     if (VT == MVT::v8i8)
2662       return SelectPostLoad(Node, 2, ARM64::LD2Rv8b_POST, ARM64::dsub0);
2663     else if (VT == MVT::v16i8)
2664       return SelectPostLoad(Node, 2, ARM64::LD2Rv16b_POST, ARM64::qsub0);
2665     else if (VT == MVT::v4i16)
2666       return SelectPostLoad(Node, 2, ARM64::LD2Rv4h_POST, ARM64::dsub0);
2667     else if (VT == MVT::v8i16)
2668       return SelectPostLoad(Node, 2, ARM64::LD2Rv8h_POST, ARM64::qsub0);
2669     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2670       return SelectPostLoad(Node, 2, ARM64::LD2Rv2s_POST, ARM64::dsub0);
2671     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2672       return SelectPostLoad(Node, 2, ARM64::LD2Rv4s_POST, ARM64::qsub0);
2673     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2674       return SelectPostLoad(Node, 2, ARM64::LD2Rv1d_POST, ARM64::dsub0);
2675     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2676       return SelectPostLoad(Node, 2, ARM64::LD2Rv2d_POST, ARM64::qsub0);
2677     break;
2678   }
2679   case ARM64ISD::LD3DUPpost: {
2680     if (VT == MVT::v8i8)
2681       return SelectPostLoad(Node, 3, ARM64::LD3Rv8b_POST, ARM64::dsub0);
2682     else if (VT == MVT::v16i8)
2683       return SelectPostLoad(Node, 3, ARM64::LD3Rv16b_POST, ARM64::qsub0);
2684     else if (VT == MVT::v4i16)
2685       return SelectPostLoad(Node, 3, ARM64::LD3Rv4h_POST, ARM64::dsub0);
2686     else if (VT == MVT::v8i16)
2687       return SelectPostLoad(Node, 3, ARM64::LD3Rv8h_POST, ARM64::qsub0);
2688     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2689       return SelectPostLoad(Node, 3, ARM64::LD3Rv2s_POST, ARM64::dsub0);
2690     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2691       return SelectPostLoad(Node, 3, ARM64::LD3Rv4s_POST, ARM64::qsub0);
2692     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2693       return SelectPostLoad(Node, 3, ARM64::LD3Rv1d_POST, ARM64::dsub0);
2694     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2695       return SelectPostLoad(Node, 3, ARM64::LD3Rv2d_POST, ARM64::qsub0);
2696     break;
2697   }
2698   case ARM64ISD::LD4DUPpost: {
2699     if (VT == MVT::v8i8)
2700       return SelectPostLoad(Node, 4, ARM64::LD4Rv8b_POST, ARM64::dsub0);
2701     else if (VT == MVT::v16i8)
2702       return SelectPostLoad(Node, 4, ARM64::LD4Rv16b_POST, ARM64::qsub0);
2703     else if (VT == MVT::v4i16)
2704       return SelectPostLoad(Node, 4, ARM64::LD4Rv4h_POST, ARM64::dsub0);
2705     else if (VT == MVT::v8i16)
2706       return SelectPostLoad(Node, 4, ARM64::LD4Rv8h_POST, ARM64::qsub0);
2707     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2708       return SelectPostLoad(Node, 4, ARM64::LD4Rv2s_POST, ARM64::dsub0);
2709     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2710       return SelectPostLoad(Node, 4, ARM64::LD4Rv4s_POST, ARM64::qsub0);
2711     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2712       return SelectPostLoad(Node, 4, ARM64::LD4Rv1d_POST, ARM64::dsub0);
2713     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2714       return SelectPostLoad(Node, 4, ARM64::LD4Rv2d_POST, ARM64::qsub0);
2715     break;
2716   }
2717   case ARM64ISD::LD2LANEpost: {
2718     if (VT == MVT::v16i8 || VT == MVT::v8i8)
2719       return SelectPostLoadLane(Node, 2, ARM64::LD2i8_POST);
2720     else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2721       return SelectPostLoadLane(Node, 2, ARM64::LD2i16_POST);
2722     else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2723              VT == MVT::v2f32)
2724       return SelectPostLoadLane(Node, 2, ARM64::LD2i32_POST);
2725     else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2726              VT == MVT::v1f64)
2727       return SelectPostLoadLane(Node, 2, ARM64::LD2i64_POST);
2728     break;
2729   }
2730   case ARM64ISD::LD3LANEpost: {
2731     if (VT == MVT::v16i8 || VT == MVT::v8i8)
2732       return SelectPostLoadLane(Node, 3, ARM64::LD3i8_POST);
2733     else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2734       return SelectPostLoadLane(Node, 3, ARM64::LD3i16_POST);
2735     else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2736              VT == MVT::v2f32)
2737       return SelectPostLoadLane(Node, 3, ARM64::LD3i32_POST);
2738     else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2739              VT == MVT::v1f64)
2740       return SelectPostLoadLane(Node, 3, ARM64::LD3i64_POST);
2741     break;
2742   }
2743   case ARM64ISD::LD4LANEpost: {
2744     if (VT == MVT::v16i8 || VT == MVT::v8i8)
2745       return SelectPostLoadLane(Node, 4, ARM64::LD4i8_POST);
2746     else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2747       return SelectPostLoadLane(Node, 4, ARM64::LD4i16_POST);
2748     else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2749              VT == MVT::v2f32)
2750       return SelectPostLoadLane(Node, 4, ARM64::LD4i32_POST);
2751     else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2752              VT == MVT::v1f64)
2753       return SelectPostLoadLane(Node, 4, ARM64::LD4i64_POST);
2754     break;
2755   }
2756   case ARM64ISD::ST2post: {
2757     VT = Node->getOperand(1).getValueType();
2758     if (VT == MVT::v8i8)
2759       return SelectPostStore(Node, 2, ARM64::ST2Twov8b_POST);
2760     else if (VT == MVT::v16i8)
2761       return SelectPostStore(Node, 2, ARM64::ST2Twov16b_POST);
2762     else if (VT == MVT::v4i16)
2763       return SelectPostStore(Node, 2, ARM64::ST2Twov4h_POST);
2764     else if (VT == MVT::v8i16)
2765       return SelectPostStore(Node, 2, ARM64::ST2Twov8h_POST);
2766     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2767       return SelectPostStore(Node, 2, ARM64::ST2Twov2s_POST);
2768     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2769       return SelectPostStore(Node, 2, ARM64::ST2Twov4s_POST);
2770     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2771       return SelectPostStore(Node, 2, ARM64::ST2Twov2d_POST);
2772     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2773       return SelectPostStore(Node, 2, ARM64::ST1Twov1d_POST);
2774     break;
2775   }
2776   case ARM64ISD::ST3post: {
2777     VT = Node->getOperand(1).getValueType();
2778     if (VT == MVT::v8i8)
2779       return SelectPostStore(Node, 3, ARM64::ST3Threev8b_POST);
2780     else if (VT == MVT::v16i8)
2781       return SelectPostStore(Node, 3, ARM64::ST3Threev16b_POST);
2782     else if (VT == MVT::v4i16)
2783       return SelectPostStore(Node, 3, ARM64::ST3Threev4h_POST);
2784     else if (VT == MVT::v8i16)
2785       return SelectPostStore(Node, 3, ARM64::ST3Threev8h_POST);
2786     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2787       return SelectPostStore(Node, 3, ARM64::ST3Threev2s_POST);
2788     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2789       return SelectPostStore(Node, 3, ARM64::ST3Threev4s_POST);
2790     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2791       return SelectPostStore(Node, 3, ARM64::ST3Threev2d_POST);
2792     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2793       return SelectPostStore(Node, 3, ARM64::ST1Threev1d_POST);
2794     break;
2795   }
2796   case ARM64ISD::ST4post: {
2797     VT = Node->getOperand(1).getValueType();
2798     if (VT == MVT::v8i8)
2799       return SelectPostStore(Node, 4, ARM64::ST4Fourv8b_POST);
2800     else if (VT == MVT::v16i8)
2801       return SelectPostStore(Node, 4, ARM64::ST4Fourv16b_POST);
2802     else if (VT == MVT::v4i16)
2803       return SelectPostStore(Node, 4, ARM64::ST4Fourv4h_POST);
2804     else if (VT == MVT::v8i16)
2805       return SelectPostStore(Node, 4, ARM64::ST4Fourv8h_POST);
2806     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2807       return SelectPostStore(Node, 4, ARM64::ST4Fourv2s_POST);
2808     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2809       return SelectPostStore(Node, 4, ARM64::ST4Fourv4s_POST);
2810     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2811       return SelectPostStore(Node, 4, ARM64::ST4Fourv2d_POST);
2812     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2813       return SelectPostStore(Node, 4, ARM64::ST1Fourv1d_POST);
2814     break;
2815   }
2816   case ARM64ISD::ST1x2post: {
2817     VT = Node->getOperand(1).getValueType();
2818     if (VT == MVT::v8i8)
2819       return SelectPostStore(Node, 2, ARM64::ST1Twov8b_POST);
2820     else if (VT == MVT::v16i8)
2821       return SelectPostStore(Node, 2, ARM64::ST1Twov16b_POST);
2822     else if (VT == MVT::v4i16)
2823       return SelectPostStore(Node, 2, ARM64::ST1Twov4h_POST);
2824     else if (VT == MVT::v8i16)
2825       return SelectPostStore(Node, 2, ARM64::ST1Twov8h_POST);
2826     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2827       return SelectPostStore(Node, 2, ARM64::ST1Twov2s_POST);
2828     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2829       return SelectPostStore(Node, 2, ARM64::ST1Twov4s_POST);
2830     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2831       return SelectPostStore(Node, 2, ARM64::ST1Twov1d_POST);
2832     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2833       return SelectPostStore(Node, 2, ARM64::ST1Twov2d_POST);
2834     break;
2835   }
2836   case ARM64ISD::ST1x3post: {
2837     VT = Node->getOperand(1).getValueType();
2838     if (VT == MVT::v8i8)
2839       return SelectPostStore(Node, 3, ARM64::ST1Threev8b_POST);
2840     else if (VT == MVT::v16i8)
2841       return SelectPostStore(Node, 3, ARM64::ST1Threev16b_POST);
2842     else if (VT == MVT::v4i16)
2843       return SelectPostStore(Node, 3, ARM64::ST1Threev4h_POST);
2844     else if (VT == MVT::v8i16)
2845       return SelectPostStore(Node, 3, ARM64::ST1Threev8h_POST);
2846     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2847       return SelectPostStore(Node, 3, ARM64::ST1Threev2s_POST);
2848     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2849       return SelectPostStore(Node, 3, ARM64::ST1Threev4s_POST);
2850     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2851       return SelectPostStore(Node, 3, ARM64::ST1Threev1d_POST);
2852     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2853       return SelectPostStore(Node, 3, ARM64::ST1Threev2d_POST);
2854     break;
2855   }
2856   case ARM64ISD::ST1x4post: {
2857     VT = Node->getOperand(1).getValueType();
2858     if (VT == MVT::v8i8)
2859       return SelectPostStore(Node, 4, ARM64::ST1Fourv8b_POST);
2860     else if (VT == MVT::v16i8)
2861       return SelectPostStore(Node, 4, ARM64::ST1Fourv16b_POST);
2862     else if (VT == MVT::v4i16)
2863       return SelectPostStore(Node, 4, ARM64::ST1Fourv4h_POST);
2864     else if (VT == MVT::v8i16)
2865       return SelectPostStore(Node, 4, ARM64::ST1Fourv8h_POST);
2866     else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2867       return SelectPostStore(Node, 4, ARM64::ST1Fourv2s_POST);
2868     else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2869       return SelectPostStore(Node, 4, ARM64::ST1Fourv4s_POST);
2870     else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2871       return SelectPostStore(Node, 4, ARM64::ST1Fourv1d_POST);
2872     else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2873       return SelectPostStore(Node, 4, ARM64::ST1Fourv2d_POST);
2874     break;
2875   }
2876   case ARM64ISD::ST2LANEpost: {
2877     VT = Node->getOperand(1).getValueType();
2878     if (VT == MVT::v16i8 || VT == MVT::v8i8)
2879       return SelectPostStoreLane(Node, 2, ARM64::ST2i8_POST);
2880     else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2881       return SelectPostStoreLane(Node, 2, ARM64::ST2i16_POST);
2882     else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2883              VT == MVT::v2f32)
2884       return SelectPostStoreLane(Node, 2, ARM64::ST2i32_POST);
2885     else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2886              VT == MVT::v1f64)
2887       return SelectPostStoreLane(Node, 2, ARM64::ST2i64_POST);
2888     break;
2889   }
2890   case ARM64ISD::ST3LANEpost: {
2891     VT = Node->getOperand(1).getValueType();
2892     if (VT == MVT::v16i8 || VT == MVT::v8i8)
2893       return SelectPostStoreLane(Node, 3, ARM64::ST3i8_POST);
2894     else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2895       return SelectPostStoreLane(Node, 3, ARM64::ST3i16_POST);
2896     else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2897              VT == MVT::v2f32)
2898       return SelectPostStoreLane(Node, 3, ARM64::ST3i32_POST);
2899     else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2900              VT == MVT::v1f64)
2901       return SelectPostStoreLane(Node, 3, ARM64::ST3i64_POST);
2902     break;
2903   }
2904   case ARM64ISD::ST4LANEpost: {
2905     VT = Node->getOperand(1).getValueType();
2906     if (VT == MVT::v16i8 || VT == MVT::v8i8)
2907       return SelectPostStoreLane(Node, 4, ARM64::ST4i8_POST);
2908     else if (VT == MVT::v8i16 || VT == MVT::v4i16)
2909       return SelectPostStoreLane(Node, 4, ARM64::ST4i16_POST);
2910     else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2911              VT == MVT::v2f32)
2912       return SelectPostStoreLane(Node, 4, ARM64::ST4i32_POST);
2913     else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2914              VT == MVT::v1f64)
2915       return SelectPostStoreLane(Node, 4, ARM64::ST4i64_POST);
2916     break;
2917   }
2918
2919   case ISD::FCEIL:
2920   case ISD::FFLOOR:
2921   case ISD::FTRUNC:
2922   case ISD::FROUND:
2923     if (SDNode *I = SelectLIBM(Node))
2924       return I;
2925     break;
2926   }
2927
2928   // Select the default instruction
2929   ResNode = SelectCode(Node);
2930
2931   DEBUG(errs() << "=> ");
2932   if (ResNode == nullptr || ResNode == Node)
2933     DEBUG(Node->dump(CurDAG));
2934   else
2935     DEBUG(ResNode->dump(CurDAG));
2936   DEBUG(errs() << "\n");
2937
2938   return ResNode;
2939 }
2940
2941 /// createARM64ISelDag - This pass converts a legalized DAG into a
2942 /// ARM64-specific DAG, ready for instruction scheduling.
2943 FunctionPass *llvm::createARM64ISelDag(ARM64TargetMachine &TM,
2944                                        CodeGenOpt::Level OptLevel) {
2945   return new ARM64DAGToDAGISel(TM, OptLevel);
2946 }