1 //===-- AArch64ISelDAGToDAG.cpp - A dag to dag inst selector for AArch64 --===//
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 AArch64 target.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "AArch64TargetMachine.h"
15 #include "MCTargetDesc/AArch64AddressingModes.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 "aarch64-isel"
29 
30 //===--------------------------------------------------------------------===//
31 /// AArch64DAGToDAGISel - AArch64 specific code to select AArch64 machine
32 /// instructions for SelectionDAG operations.
33 ///
34 namespace {
35 
36 class AArch64DAGToDAGISel : public SelectionDAGISel {
37 
38   /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
39   /// make the right decision when generating code for different targets.
40   const AArch64Subtarget *Subtarget;
41 
42   bool ForCodeSize;
43 
44 public:
45   explicit AArch64DAGToDAGISel(AArch64TargetMachine &tm,
46                                CodeGenOpt::Level OptLevel)
47       : SelectionDAGISel(tm, OptLevel), Subtarget(nullptr),
48         ForCodeSize(false) {}
49 
50   StringRef getPassName() const override {
51     return "AArch64 Instruction Selection";
52   }
53 
54   bool runOnMachineFunction(MachineFunction &MF) override {
55     ForCodeSize = MF.getFunction()->optForSize();
56     Subtarget = &MF.getSubtarget<AArch64Subtarget>();
57     return SelectionDAGISel::runOnMachineFunction(MF);
58   }
59 
60   void Select(SDNode *Node) override;
61 
62   /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
63   /// inline asm expressions.
64   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
65                                     unsigned ConstraintID,
66                                     std::vector<SDValue> &OutOps) override;
67 
68   bool tryMLAV64LaneV128(SDNode *N);
69   bool tryMULLV64LaneV128(unsigned IntNo, SDNode *N);
70   bool SelectArithExtendedRegister(SDValue N, SDValue &Reg, SDValue &Shift);
71   bool SelectArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
72   bool SelectNegArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
73   bool SelectArithShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
74     return SelectShiftedRegister(N, false, Reg, Shift);
75   }
76   bool SelectLogicalShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
77     return SelectShiftedRegister(N, true, Reg, Shift);
78   }
79   bool SelectAddrModeIndexed7S8(SDValue N, SDValue &Base, SDValue &OffImm) {
80     return SelectAddrModeIndexed7S(N, 1, Base, OffImm);
81   }
82   bool SelectAddrModeIndexed7S16(SDValue N, SDValue &Base, SDValue &OffImm) {
83     return SelectAddrModeIndexed7S(N, 2, Base, OffImm);
84   }
85   bool SelectAddrModeIndexed7S32(SDValue N, SDValue &Base, SDValue &OffImm) {
86     return SelectAddrModeIndexed7S(N, 4, Base, OffImm);
87   }
88   bool SelectAddrModeIndexed7S64(SDValue N, SDValue &Base, SDValue &OffImm) {
89     return SelectAddrModeIndexed7S(N, 8, Base, OffImm);
90   }
91   bool SelectAddrModeIndexed7S128(SDValue N, SDValue &Base, SDValue &OffImm) {
92     return SelectAddrModeIndexed7S(N, 16, Base, OffImm);
93   }
94   bool SelectAddrModeIndexed8(SDValue N, SDValue &Base, SDValue &OffImm) {
95     return SelectAddrModeIndexed(N, 1, Base, OffImm);
96   }
97   bool SelectAddrModeIndexed16(SDValue N, SDValue &Base, SDValue &OffImm) {
98     return SelectAddrModeIndexed(N, 2, Base, OffImm);
99   }
100   bool SelectAddrModeIndexed32(SDValue N, SDValue &Base, SDValue &OffImm) {
101     return SelectAddrModeIndexed(N, 4, Base, OffImm);
102   }
103   bool SelectAddrModeIndexed64(SDValue N, SDValue &Base, SDValue &OffImm) {
104     return SelectAddrModeIndexed(N, 8, Base, OffImm);
105   }
106   bool SelectAddrModeIndexed128(SDValue N, SDValue &Base, SDValue &OffImm) {
107     return SelectAddrModeIndexed(N, 16, Base, OffImm);
108   }
109   bool SelectAddrModeUnscaled8(SDValue N, SDValue &Base, SDValue &OffImm) {
110     return SelectAddrModeUnscaled(N, 1, Base, OffImm);
111   }
112   bool SelectAddrModeUnscaled16(SDValue N, SDValue &Base, SDValue &OffImm) {
113     return SelectAddrModeUnscaled(N, 2, Base, OffImm);
114   }
115   bool SelectAddrModeUnscaled32(SDValue N, SDValue &Base, SDValue &OffImm) {
116     return SelectAddrModeUnscaled(N, 4, Base, OffImm);
117   }
118   bool SelectAddrModeUnscaled64(SDValue N, SDValue &Base, SDValue &OffImm) {
119     return SelectAddrModeUnscaled(N, 8, Base, OffImm);
120   }
121   bool SelectAddrModeUnscaled128(SDValue N, SDValue &Base, SDValue &OffImm) {
122     return SelectAddrModeUnscaled(N, 16, Base, OffImm);
123   }
124 
125   template<int Width>
126   bool SelectAddrModeWRO(SDValue N, SDValue &Base, SDValue &Offset,
127                          SDValue &SignExtend, SDValue &DoShift) {
128     return SelectAddrModeWRO(N, Width / 8, Base, Offset, SignExtend, DoShift);
129   }
130 
131   template<int Width>
132   bool SelectAddrModeXRO(SDValue N, SDValue &Base, SDValue &Offset,
133                          SDValue &SignExtend, SDValue &DoShift) {
134     return SelectAddrModeXRO(N, Width / 8, Base, Offset, SignExtend, DoShift);
135   }
136 
137 
138   /// Form sequences of consecutive 64/128-bit registers for use in NEON
139   /// instructions making use of a vector-list (e.g. ldN, tbl). Vecs must have
140   /// between 1 and 4 elements. If it contains a single element that is returned
141   /// unchanged; otherwise a REG_SEQUENCE value is returned.
142   SDValue createDTuple(ArrayRef<SDValue> Vecs);
143   SDValue createQTuple(ArrayRef<SDValue> Vecs);
144 
145   /// Generic helper for the createDTuple/createQTuple
146   /// functions. Those should almost always be called instead.
147   SDValue createTuple(ArrayRef<SDValue> Vecs, const unsigned RegClassIDs[],
148                       const unsigned SubRegs[]);
149 
150   void SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc, bool isExt);
151 
152   bool tryIndexedLoad(SDNode *N);
153 
154   void SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
155                      unsigned SubRegIdx);
156   void SelectPostLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
157                          unsigned SubRegIdx);
158   void SelectLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
159   void SelectPostLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
160 
161   void SelectStore(SDNode *N, unsigned NumVecs, unsigned Opc);
162   void SelectPostStore(SDNode *N, unsigned NumVecs, unsigned Opc);
163   void SelectStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
164   void SelectPostStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
165 
166   bool tryBitfieldExtractOp(SDNode *N);
167   bool tryBitfieldExtractOpFromSExt(SDNode *N);
168   bool tryBitfieldInsertOp(SDNode *N);
169   bool tryBitfieldInsertInZeroOp(SDNode *N);
170 
171   bool tryReadRegister(SDNode *N);
172   bool tryWriteRegister(SDNode *N);
173 
174 // Include the pieces autogenerated from the target description.
175 #include "AArch64GenDAGISel.inc"
176 
177 private:
178   bool SelectShiftedRegister(SDValue N, bool AllowROR, SDValue &Reg,
179                              SDValue &Shift);
180   bool SelectAddrModeIndexed7S(SDValue N, unsigned Size, SDValue &Base,
181                                SDValue &OffImm);
182   bool SelectAddrModeIndexed(SDValue N, unsigned Size, SDValue &Base,
183                              SDValue &OffImm);
184   bool SelectAddrModeUnscaled(SDValue N, unsigned Size, SDValue &Base,
185                               SDValue &OffImm);
186   bool SelectAddrModeWRO(SDValue N, unsigned Size, SDValue &Base,
187                          SDValue &Offset, SDValue &SignExtend,
188                          SDValue &DoShift);
189   bool SelectAddrModeXRO(SDValue N, unsigned Size, SDValue &Base,
190                          SDValue &Offset, SDValue &SignExtend,
191                          SDValue &DoShift);
192   bool isWorthFolding(SDValue V) const;
193   bool SelectExtendedSHL(SDValue N, unsigned Size, bool WantExtend,
194                          SDValue &Offset, SDValue &SignExtend);
195 
196   template<unsigned RegWidth>
197   bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
198     return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
199   }
200 
201   bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos, unsigned Width);
202 
203   void SelectCMP_SWAP(SDNode *N);
204 
205 };
206 } // end anonymous namespace
207 
208 /// isIntImmediate - This method tests to see if the node is a constant
209 /// operand. If so Imm will receive the 32-bit value.
210 static bool isIntImmediate(const SDNode *N, uint64_t &Imm) {
211   if (const ConstantSDNode *C = dyn_cast<const ConstantSDNode>(N)) {
212     Imm = C->getZExtValue();
213     return true;
214   }
215   return false;
216 }
217 
218 // isIntImmediate - This method tests to see if a constant operand.
219 // If so Imm will receive the value.
220 static bool isIntImmediate(SDValue N, uint64_t &Imm) {
221   return isIntImmediate(N.getNode(), Imm);
222 }
223 
224 // isOpcWithIntImmediate - This method tests to see if the node is a specific
225 // opcode and that it has a immediate integer right operand.
226 // If so Imm will receive the 32 bit value.
227 static bool isOpcWithIntImmediate(const SDNode *N, unsigned Opc,
228                                   uint64_t &Imm) {
229   return N->getOpcode() == Opc &&
230          isIntImmediate(N->getOperand(1).getNode(), Imm);
231 }
232 
233 bool AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(
234     const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
235   switch(ConstraintID) {
236   default:
237     llvm_unreachable("Unexpected asm memory constraint");
238   case InlineAsm::Constraint_i:
239   case InlineAsm::Constraint_m:
240   case InlineAsm::Constraint_Q:
241     // Require the address to be in a register.  That is safe for all AArch64
242     // variants and it is hard to do anything much smarter without knowing
243     // how the operand is used.
244     OutOps.push_back(Op);
245     return false;
246   }
247   return true;
248 }
249 
250 /// SelectArithImmed - Select an immediate value that can be represented as
251 /// a 12-bit value shifted left by either 0 or 12.  If so, return true with
252 /// Val set to the 12-bit value and Shift set to the shifter operand.
253 bool AArch64DAGToDAGISel::SelectArithImmed(SDValue N, SDValue &Val,
254                                            SDValue &Shift) {
255   // This function is called from the addsub_shifted_imm ComplexPattern,
256   // which lists [imm] as the list of opcode it's interested in, however
257   // we still need to check whether the operand is actually an immediate
258   // here because the ComplexPattern opcode list is only used in
259   // root-level opcode matching.
260   if (!isa<ConstantSDNode>(N.getNode()))
261     return false;
262 
263   uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
264   unsigned ShiftAmt;
265 
266   if (Immed >> 12 == 0) {
267     ShiftAmt = 0;
268   } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
269     ShiftAmt = 12;
270     Immed = Immed >> 12;
271   } else
272     return false;
273 
274   unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
275   SDLoc dl(N);
276   Val = CurDAG->getTargetConstant(Immed, dl, MVT::i32);
277   Shift = CurDAG->getTargetConstant(ShVal, dl, MVT::i32);
278   return true;
279 }
280 
281 /// SelectNegArithImmed - As above, but negates the value before trying to
282 /// select it.
283 bool AArch64DAGToDAGISel::SelectNegArithImmed(SDValue N, SDValue &Val,
284                                               SDValue &Shift) {
285   // This function is called from the addsub_shifted_imm ComplexPattern,
286   // which lists [imm] as the list of opcode it's interested in, however
287   // we still need to check whether the operand is actually an immediate
288   // here because the ComplexPattern opcode list is only used in
289   // root-level opcode matching.
290   if (!isa<ConstantSDNode>(N.getNode()))
291     return false;
292 
293   // The immediate operand must be a 24-bit zero-extended immediate.
294   uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
295 
296   // This negation is almost always valid, but "cmp wN, #0" and "cmn wN, #0"
297   // have the opposite effect on the C flag, so this pattern mustn't match under
298   // those circumstances.
299   if (Immed == 0)
300     return false;
301 
302   if (N.getValueType() == MVT::i32)
303     Immed = ~((uint32_t)Immed) + 1;
304   else
305     Immed = ~Immed + 1ULL;
306   if (Immed & 0xFFFFFFFFFF000000ULL)
307     return false;
308 
309   Immed &= 0xFFFFFFULL;
310   return SelectArithImmed(CurDAG->getConstant(Immed, SDLoc(N), MVT::i32), Val,
311                           Shift);
312 }
313 
314 /// getShiftTypeForNode - Translate a shift node to the corresponding
315 /// ShiftType value.
316 static AArch64_AM::ShiftExtendType getShiftTypeForNode(SDValue N) {
317   switch (N.getOpcode()) {
318   default:
319     return AArch64_AM::InvalidShiftExtend;
320   case ISD::SHL:
321     return AArch64_AM::LSL;
322   case ISD::SRL:
323     return AArch64_AM::LSR;
324   case ISD::SRA:
325     return AArch64_AM::ASR;
326   case ISD::ROTR:
327     return AArch64_AM::ROR;
328   }
329 }
330 
331 /// \brief Determine whether it is worth to fold V into an extended register.
332 bool AArch64DAGToDAGISel::isWorthFolding(SDValue V) const {
333   // it hurts if the value is used at least twice, unless we are optimizing
334   // for code size.
335   return ForCodeSize || V.hasOneUse();
336 }
337 
338 /// SelectShiftedRegister - Select a "shifted register" operand.  If the value
339 /// is not shifted, set the Shift operand to default of "LSL 0".  The logical
340 /// instructions allow the shifted register to be rotated, but the arithmetic
341 /// instructions do not.  The AllowROR parameter specifies whether ROR is
342 /// supported.
343 bool AArch64DAGToDAGISel::SelectShiftedRegister(SDValue N, bool AllowROR,
344                                                 SDValue &Reg, SDValue &Shift) {
345   AArch64_AM::ShiftExtendType ShType = getShiftTypeForNode(N);
346   if (ShType == AArch64_AM::InvalidShiftExtend)
347     return false;
348   if (!AllowROR && ShType == AArch64_AM::ROR)
349     return false;
350 
351   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
352     unsigned BitSize = N.getValueSizeInBits();
353     unsigned Val = RHS->getZExtValue() & (BitSize - 1);
354     unsigned ShVal = AArch64_AM::getShifterImm(ShType, Val);
355 
356     Reg = N.getOperand(0);
357     Shift = CurDAG->getTargetConstant(ShVal, SDLoc(N), MVT::i32);
358     return isWorthFolding(N);
359   }
360 
361   return false;
362 }
363 
364 /// getExtendTypeForNode - Translate an extend node to the corresponding
365 /// ExtendType value.
366 static AArch64_AM::ShiftExtendType
367 getExtendTypeForNode(SDValue N, bool IsLoadStore = false) {
368   if (N.getOpcode() == ISD::SIGN_EXTEND ||
369       N.getOpcode() == ISD::SIGN_EXTEND_INREG) {
370     EVT SrcVT;
371     if (N.getOpcode() == ISD::SIGN_EXTEND_INREG)
372       SrcVT = cast<VTSDNode>(N.getOperand(1))->getVT();
373     else
374       SrcVT = N.getOperand(0).getValueType();
375 
376     if (!IsLoadStore && SrcVT == MVT::i8)
377       return AArch64_AM::SXTB;
378     else if (!IsLoadStore && SrcVT == MVT::i16)
379       return AArch64_AM::SXTH;
380     else if (SrcVT == MVT::i32)
381       return AArch64_AM::SXTW;
382     assert(SrcVT != MVT::i64 && "extend from 64-bits?");
383 
384     return AArch64_AM::InvalidShiftExtend;
385   } else if (N.getOpcode() == ISD::ZERO_EXTEND ||
386              N.getOpcode() == ISD::ANY_EXTEND) {
387     EVT SrcVT = N.getOperand(0).getValueType();
388     if (!IsLoadStore && SrcVT == MVT::i8)
389       return AArch64_AM::UXTB;
390     else if (!IsLoadStore && SrcVT == MVT::i16)
391       return AArch64_AM::UXTH;
392     else if (SrcVT == MVT::i32)
393       return AArch64_AM::UXTW;
394     assert(SrcVT != MVT::i64 && "extend from 64-bits?");
395 
396     return AArch64_AM::InvalidShiftExtend;
397   } else if (N.getOpcode() == ISD::AND) {
398     ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
399     if (!CSD)
400       return AArch64_AM::InvalidShiftExtend;
401     uint64_t AndMask = CSD->getZExtValue();
402 
403     switch (AndMask) {
404     default:
405       return AArch64_AM::InvalidShiftExtend;
406     case 0xFF:
407       return !IsLoadStore ? AArch64_AM::UXTB : AArch64_AM::InvalidShiftExtend;
408     case 0xFFFF:
409       return !IsLoadStore ? AArch64_AM::UXTH : AArch64_AM::InvalidShiftExtend;
410     case 0xFFFFFFFF:
411       return AArch64_AM::UXTW;
412     }
413   }
414 
415   return AArch64_AM::InvalidShiftExtend;
416 }
417 
418 // Helper for SelectMLAV64LaneV128 - Recognize high lane extracts.
419 static bool checkHighLaneIndex(SDNode *DL, SDValue &LaneOp, int &LaneIdx) {
420   if (DL->getOpcode() != AArch64ISD::DUPLANE16 &&
421       DL->getOpcode() != AArch64ISD::DUPLANE32)
422     return false;
423 
424   SDValue SV = DL->getOperand(0);
425   if (SV.getOpcode() != ISD::INSERT_SUBVECTOR)
426     return false;
427 
428   SDValue EV = SV.getOperand(1);
429   if (EV.getOpcode() != ISD::EXTRACT_SUBVECTOR)
430     return false;
431 
432   ConstantSDNode *DLidx = cast<ConstantSDNode>(DL->getOperand(1).getNode());
433   ConstantSDNode *EVidx = cast<ConstantSDNode>(EV.getOperand(1).getNode());
434   LaneIdx = DLidx->getSExtValue() + EVidx->getSExtValue();
435   LaneOp = EV.getOperand(0);
436 
437   return true;
438 }
439 
440 // Helper for SelectOpcV64LaneV128 - Recognize operations where one operand is a
441 // high lane extract.
442 static bool checkV64LaneV128(SDValue Op0, SDValue Op1, SDValue &StdOp,
443                              SDValue &LaneOp, int &LaneIdx) {
444 
445   if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx)) {
446     std::swap(Op0, Op1);
447     if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx))
448       return false;
449   }
450   StdOp = Op1;
451   return true;
452 }
453 
454 /// SelectMLAV64LaneV128 - AArch64 supports vector MLAs where one multiplicand
455 /// is a lane in the upper half of a 128-bit vector.  Recognize and select this
456 /// so that we don't emit unnecessary lane extracts.
457 bool AArch64DAGToDAGISel::tryMLAV64LaneV128(SDNode *N) {
458   SDLoc dl(N);
459   SDValue Op0 = N->getOperand(0);
460   SDValue Op1 = N->getOperand(1);
461   SDValue MLAOp1;   // Will hold ordinary multiplicand for MLA.
462   SDValue MLAOp2;   // Will hold lane-accessed multiplicand for MLA.
463   int LaneIdx = -1; // Will hold the lane index.
464 
465   if (Op1.getOpcode() != ISD::MUL ||
466       !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
467                         LaneIdx)) {
468     std::swap(Op0, Op1);
469     if (Op1.getOpcode() != ISD::MUL ||
470         !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
471                           LaneIdx))
472       return false;
473   }
474 
475   SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
476 
477   SDValue Ops[] = { Op0, MLAOp1, MLAOp2, LaneIdxVal };
478 
479   unsigned MLAOpc = ~0U;
480 
481   switch (N->getSimpleValueType(0).SimpleTy) {
482   default:
483     llvm_unreachable("Unrecognized MLA.");
484   case MVT::v4i16:
485     MLAOpc = AArch64::MLAv4i16_indexed;
486     break;
487   case MVT::v8i16:
488     MLAOpc = AArch64::MLAv8i16_indexed;
489     break;
490   case MVT::v2i32:
491     MLAOpc = AArch64::MLAv2i32_indexed;
492     break;
493   case MVT::v4i32:
494     MLAOpc = AArch64::MLAv4i32_indexed;
495     break;
496   }
497 
498   ReplaceNode(N, CurDAG->getMachineNode(MLAOpc, dl, N->getValueType(0), Ops));
499   return true;
500 }
501 
502 bool AArch64DAGToDAGISel::tryMULLV64LaneV128(unsigned IntNo, SDNode *N) {
503   SDLoc dl(N);
504   SDValue SMULLOp0;
505   SDValue SMULLOp1;
506   int LaneIdx;
507 
508   if (!checkV64LaneV128(N->getOperand(1), N->getOperand(2), SMULLOp0, SMULLOp1,
509                         LaneIdx))
510     return false;
511 
512   SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
513 
514   SDValue Ops[] = { SMULLOp0, SMULLOp1, LaneIdxVal };
515 
516   unsigned SMULLOpc = ~0U;
517 
518   if (IntNo == Intrinsic::aarch64_neon_smull) {
519     switch (N->getSimpleValueType(0).SimpleTy) {
520     default:
521       llvm_unreachable("Unrecognized SMULL.");
522     case MVT::v4i32:
523       SMULLOpc = AArch64::SMULLv4i16_indexed;
524       break;
525     case MVT::v2i64:
526       SMULLOpc = AArch64::SMULLv2i32_indexed;
527       break;
528     }
529   } else if (IntNo == Intrinsic::aarch64_neon_umull) {
530     switch (N->getSimpleValueType(0).SimpleTy) {
531     default:
532       llvm_unreachable("Unrecognized SMULL.");
533     case MVT::v4i32:
534       SMULLOpc = AArch64::UMULLv4i16_indexed;
535       break;
536     case MVT::v2i64:
537       SMULLOpc = AArch64::UMULLv2i32_indexed;
538       break;
539     }
540   } else
541     llvm_unreachable("Unrecognized intrinsic.");
542 
543   ReplaceNode(N, CurDAG->getMachineNode(SMULLOpc, dl, N->getValueType(0), Ops));
544   return true;
545 }
546 
547 /// Instructions that accept extend modifiers like UXTW expect the register
548 /// being extended to be a GPR32, but the incoming DAG might be acting on a
549 /// GPR64 (either via SEXT_INREG or AND). Extract the appropriate low bits if
550 /// this is the case.
551 static SDValue narrowIfNeeded(SelectionDAG *CurDAG, SDValue N) {
552   if (N.getValueType() == MVT::i32)
553     return N;
554 
555   SDLoc dl(N);
556   SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
557   MachineSDNode *Node = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
558                                                dl, MVT::i32, N, SubReg);
559   return SDValue(Node, 0);
560 }
561 
562 
563 /// SelectArithExtendedRegister - Select a "extended register" operand.  This
564 /// operand folds in an extend followed by an optional left shift.
565 bool AArch64DAGToDAGISel::SelectArithExtendedRegister(SDValue N, SDValue &Reg,
566                                                       SDValue &Shift) {
567   unsigned ShiftVal = 0;
568   AArch64_AM::ShiftExtendType Ext;
569 
570   if (N.getOpcode() == ISD::SHL) {
571     ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
572     if (!CSD)
573       return false;
574     ShiftVal = CSD->getZExtValue();
575     if (ShiftVal > 4)
576       return false;
577 
578     Ext = getExtendTypeForNode(N.getOperand(0));
579     if (Ext == AArch64_AM::InvalidShiftExtend)
580       return false;
581 
582     Reg = N.getOperand(0).getOperand(0);
583   } else {
584     Ext = getExtendTypeForNode(N);
585     if (Ext == AArch64_AM::InvalidShiftExtend)
586       return false;
587 
588     Reg = N.getOperand(0);
589 
590     // Don't match if free 32-bit -> 64-bit zext can be used instead.
591     if (Ext == AArch64_AM::UXTW &&
592         Reg->getValueType(0).getSizeInBits() == 32 && isDef32(*Reg.getNode()))
593       return false;
594   }
595 
596   // AArch64 mandates that the RHS of the operation must use the smallest
597   // register class that could contain the size being extended from.  Thus,
598   // if we're folding a (sext i8), we need the RHS to be a GPR32, even though
599   // there might not be an actual 32-bit value in the program.  We can
600   // (harmlessly) synthesize one by injected an EXTRACT_SUBREG here.
601   assert(Ext != AArch64_AM::UXTX && Ext != AArch64_AM::SXTX);
602   Reg = narrowIfNeeded(CurDAG, Reg);
603   Shift = CurDAG->getTargetConstant(getArithExtendImm(Ext, ShiftVal), SDLoc(N),
604                                     MVT::i32);
605   return isWorthFolding(N);
606 }
607 
608 /// If there's a use of this ADDlow that's not itself a load/store then we'll
609 /// need to create a real ADD instruction from it anyway and there's no point in
610 /// folding it into the mem op. Theoretically, it shouldn't matter, but there's
611 /// a single pseudo-instruction for an ADRP/ADD pair so over-aggressive folding
612 /// leads to duplicated ADRP instructions.
613 static bool isWorthFoldingADDlow(SDValue N) {
614   for (auto Use : N->uses()) {
615     if (Use->getOpcode() != ISD::LOAD && Use->getOpcode() != ISD::STORE &&
616         Use->getOpcode() != ISD::ATOMIC_LOAD &&
617         Use->getOpcode() != ISD::ATOMIC_STORE)
618       return false;
619 
620     // ldar and stlr have much more restrictive addressing modes (just a
621     // register).
622     if (isStrongerThanMonotonic(cast<MemSDNode>(Use)->getOrdering()))
623       return false;
624   }
625 
626   return true;
627 }
628 
629 /// SelectAddrModeIndexed7S - Select a "register plus scaled signed 7-bit
630 /// immediate" address.  The "Size" argument is the size in bytes of the memory
631 /// reference, which determines the scale.
632 bool AArch64DAGToDAGISel::SelectAddrModeIndexed7S(SDValue N, unsigned Size,
633                                                   SDValue &Base,
634                                                   SDValue &OffImm) {
635   SDLoc dl(N);
636   const DataLayout &DL = CurDAG->getDataLayout();
637   const TargetLowering *TLI = getTargetLowering();
638   if (N.getOpcode() == ISD::FrameIndex) {
639     int FI = cast<FrameIndexSDNode>(N)->getIndex();
640     Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
641     OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
642     return true;
643   }
644 
645   // As opposed to the (12-bit) Indexed addressing mode below, the 7-bit signed
646   // selected here doesn't support labels/immediates, only base+offset.
647 
648   if (CurDAG->isBaseWithConstantOffset(N)) {
649     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
650       int64_t RHSC = RHS->getSExtValue();
651       unsigned Scale = Log2_32(Size);
652       if ((RHSC & (Size - 1)) == 0 && RHSC >= -(0x40 << Scale) &&
653           RHSC < (0x40 << Scale)) {
654         Base = N.getOperand(0);
655         if (Base.getOpcode() == ISD::FrameIndex) {
656           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
657           Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
658         }
659         OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
660         return true;
661       }
662     }
663   }
664 
665   // Base only. The address will be materialized into a register before
666   // the memory is accessed.
667   //    add x0, Xbase, #offset
668   //    stp x1, x2, [x0]
669   Base = N;
670   OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
671   return true;
672 }
673 
674 /// SelectAddrModeIndexed - Select a "register plus scaled unsigned 12-bit
675 /// immediate" address.  The "Size" argument is the size in bytes of the memory
676 /// reference, which determines the scale.
677 bool AArch64DAGToDAGISel::SelectAddrModeIndexed(SDValue N, unsigned Size,
678                                               SDValue &Base, SDValue &OffImm) {
679   SDLoc dl(N);
680   const DataLayout &DL = CurDAG->getDataLayout();
681   const TargetLowering *TLI = getTargetLowering();
682   if (N.getOpcode() == ISD::FrameIndex) {
683     int FI = cast<FrameIndexSDNode>(N)->getIndex();
684     Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
685     OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
686     return true;
687   }
688 
689   if (N.getOpcode() == AArch64ISD::ADDlow && isWorthFoldingADDlow(N)) {
690     GlobalAddressSDNode *GAN =
691         dyn_cast<GlobalAddressSDNode>(N.getOperand(1).getNode());
692     Base = N.getOperand(0);
693     OffImm = N.getOperand(1);
694     if (!GAN)
695       return true;
696 
697     const GlobalValue *GV = GAN->getGlobal();
698     unsigned Alignment = GV->getAlignment();
699     Type *Ty = GV->getValueType();
700     if (Alignment == 0 && Ty->isSized())
701       Alignment = DL.getABITypeAlignment(Ty);
702 
703     if (Alignment >= Size)
704       return true;
705   }
706 
707   if (CurDAG->isBaseWithConstantOffset(N)) {
708     if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
709       int64_t RHSC = (int64_t)RHS->getZExtValue();
710       unsigned Scale = Log2_32(Size);
711       if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
712         Base = N.getOperand(0);
713         if (Base.getOpcode() == ISD::FrameIndex) {
714           int FI = cast<FrameIndexSDNode>(Base)->getIndex();
715           Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
716         }
717         OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
718         return true;
719       }
720     }
721   }
722 
723   // Before falling back to our general case, check if the unscaled
724   // instructions can handle this. If so, that's preferable.
725   if (SelectAddrModeUnscaled(N, Size, Base, OffImm))
726     return false;
727 
728   // Base only. The address will be materialized into a register before
729   // the memory is accessed.
730   //    add x0, Xbase, #offset
731   //    ldr x0, [x0]
732   Base = N;
733   OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
734   return true;
735 }
736 
737 /// SelectAddrModeUnscaled - Select a "register plus unscaled signed 9-bit
738 /// immediate" address.  This should only match when there is an offset that
739 /// is not valid for a scaled immediate addressing mode.  The "Size" argument
740 /// is the size in bytes of the memory reference, which is needed here to know
741 /// what is valid for a scaled immediate.
742 bool AArch64DAGToDAGISel::SelectAddrModeUnscaled(SDValue N, unsigned Size,
743                                                  SDValue &Base,
744                                                  SDValue &OffImm) {
745   if (!CurDAG->isBaseWithConstantOffset(N))
746     return false;
747   if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
748     int64_t RHSC = RHS->getSExtValue();
749     // If the offset is valid as a scaled immediate, don't match here.
750     if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 &&
751         RHSC < (0x1000 << Log2_32(Size)))
752       return false;
753     if (RHSC >= -256 && RHSC < 256) {
754       Base = N.getOperand(0);
755       if (Base.getOpcode() == ISD::FrameIndex) {
756         int FI = cast<FrameIndexSDNode>(Base)->getIndex();
757         const TargetLowering *TLI = getTargetLowering();
758         Base = CurDAG->getTargetFrameIndex(
759             FI, TLI->getPointerTy(CurDAG->getDataLayout()));
760       }
761       OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i64);
762       return true;
763     }
764   }
765   return false;
766 }
767 
768 static SDValue Widen(SelectionDAG *CurDAG, SDValue N) {
769   SDLoc dl(N);
770   SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
771   SDValue ImpDef = SDValue(
772       CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::i64), 0);
773   MachineSDNode *Node = CurDAG->getMachineNode(
774       TargetOpcode::INSERT_SUBREG, dl, MVT::i64, ImpDef, N, SubReg);
775   return SDValue(Node, 0);
776 }
777 
778 /// \brief Check if the given SHL node (\p N), can be used to form an
779 /// extended register for an addressing mode.
780 bool AArch64DAGToDAGISel::SelectExtendedSHL(SDValue N, unsigned Size,
781                                             bool WantExtend, SDValue &Offset,
782                                             SDValue &SignExtend) {
783   assert(N.getOpcode() == ISD::SHL && "Invalid opcode.");
784   ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
785   if (!CSD || (CSD->getZExtValue() & 0x7) != CSD->getZExtValue())
786     return false;
787 
788   SDLoc dl(N);
789   if (WantExtend) {
790     AArch64_AM::ShiftExtendType Ext =
791         getExtendTypeForNode(N.getOperand(0), true);
792     if (Ext == AArch64_AM::InvalidShiftExtend)
793       return false;
794 
795     Offset = narrowIfNeeded(CurDAG, N.getOperand(0).getOperand(0));
796     SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
797                                            MVT::i32);
798   } else {
799     Offset = N.getOperand(0);
800     SignExtend = CurDAG->getTargetConstant(0, dl, MVT::i32);
801   }
802 
803   unsigned LegalShiftVal = Log2_32(Size);
804   unsigned ShiftVal = CSD->getZExtValue();
805 
806   if (ShiftVal != 0 && ShiftVal != LegalShiftVal)
807     return false;
808 
809   return isWorthFolding(N);
810 }
811 
812 bool AArch64DAGToDAGISel::SelectAddrModeWRO(SDValue N, unsigned Size,
813                                             SDValue &Base, SDValue &Offset,
814                                             SDValue &SignExtend,
815                                             SDValue &DoShift) {
816   if (N.getOpcode() != ISD::ADD)
817     return false;
818   SDValue LHS = N.getOperand(0);
819   SDValue RHS = N.getOperand(1);
820   SDLoc dl(N);
821 
822   // We don't want to match immediate adds here, because they are better lowered
823   // to the register-immediate addressing modes.
824   if (isa<ConstantSDNode>(LHS) || isa<ConstantSDNode>(RHS))
825     return false;
826 
827   // Check if this particular node is reused in any non-memory related
828   // operation.  If yes, do not try to fold this node into the address
829   // computation, since the computation will be kept.
830   const SDNode *Node = N.getNode();
831   for (SDNode *UI : Node->uses()) {
832     if (!isa<MemSDNode>(*UI))
833       return false;
834   }
835 
836   // Remember if it is worth folding N when it produces extended register.
837   bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
838 
839   // Try to match a shifted extend on the RHS.
840   if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
841       SelectExtendedSHL(RHS, Size, true, Offset, SignExtend)) {
842     Base = LHS;
843     DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
844     return true;
845   }
846 
847   // Try to match a shifted extend on the LHS.
848   if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
849       SelectExtendedSHL(LHS, Size, true, Offset, SignExtend)) {
850     Base = RHS;
851     DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
852     return true;
853   }
854 
855   // There was no shift, whatever else we find.
856   DoShift = CurDAG->getTargetConstant(false, dl, MVT::i32);
857 
858   AArch64_AM::ShiftExtendType Ext = AArch64_AM::InvalidShiftExtend;
859   // Try to match an unshifted extend on the LHS.
860   if (IsExtendedRegisterWorthFolding &&
861       (Ext = getExtendTypeForNode(LHS, true)) !=
862           AArch64_AM::InvalidShiftExtend) {
863     Base = RHS;
864     Offset = narrowIfNeeded(CurDAG, LHS.getOperand(0));
865     SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
866                                            MVT::i32);
867     if (isWorthFolding(LHS))
868       return true;
869   }
870 
871   // Try to match an unshifted extend on the RHS.
872   if (IsExtendedRegisterWorthFolding &&
873       (Ext = getExtendTypeForNode(RHS, true)) !=
874           AArch64_AM::InvalidShiftExtend) {
875     Base = LHS;
876     Offset = narrowIfNeeded(CurDAG, RHS.getOperand(0));
877     SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
878                                            MVT::i32);
879     if (isWorthFolding(RHS))
880       return true;
881   }
882 
883   return false;
884 }
885 
886 // Check if the given immediate is preferred by ADD. If an immediate can be
887 // encoded in an ADD, or it can be encoded in an "ADD LSL #12" and can not be
888 // encoded by one MOVZ, return true.
889 static bool isPreferredADD(int64_t ImmOff) {
890   // Constant in [0x0, 0xfff] can be encoded in ADD.
891   if ((ImmOff & 0xfffffffffffff000LL) == 0x0LL)
892     return true;
893   // Check if it can be encoded in an "ADD LSL #12".
894   if ((ImmOff & 0xffffffffff000fffLL) == 0x0LL)
895     // As a single MOVZ is faster than a "ADD of LSL #12", ignore such constant.
896     return (ImmOff & 0xffffffffff00ffffLL) != 0x0LL &&
897            (ImmOff & 0xffffffffffff0fffLL) != 0x0LL;
898   return false;
899 }
900 
901 bool AArch64DAGToDAGISel::SelectAddrModeXRO(SDValue N, unsigned Size,
902                                             SDValue &Base, SDValue &Offset,
903                                             SDValue &SignExtend,
904                                             SDValue &DoShift) {
905   if (N.getOpcode() != ISD::ADD)
906     return false;
907   SDValue LHS = N.getOperand(0);
908   SDValue RHS = N.getOperand(1);
909   SDLoc DL(N);
910 
911   // Check if this particular node is reused in any non-memory related
912   // operation.  If yes, do not try to fold this node into the address
913   // computation, since the computation will be kept.
914   const SDNode *Node = N.getNode();
915   for (SDNode *UI : Node->uses()) {
916     if (!isa<MemSDNode>(*UI))
917       return false;
918   }
919 
920   // Watch out if RHS is a wide immediate, it can not be selected into
921   // [BaseReg+Imm] addressing mode. Also it may not be able to be encoded into
922   // ADD/SUB. Instead it will use [BaseReg + 0] address mode and generate
923   // instructions like:
924   //     MOV  X0, WideImmediate
925   //     ADD  X1, BaseReg, X0
926   //     LDR  X2, [X1, 0]
927   // For such situation, using [BaseReg, XReg] addressing mode can save one
928   // ADD/SUB:
929   //     MOV  X0, WideImmediate
930   //     LDR  X2, [BaseReg, X0]
931   if (isa<ConstantSDNode>(RHS)) {
932     int64_t ImmOff = (int64_t)cast<ConstantSDNode>(RHS)->getZExtValue();
933     unsigned Scale = Log2_32(Size);
934     // Skip the immediate can be selected by load/store addressing mode.
935     // Also skip the immediate can be encoded by a single ADD (SUB is also
936     // checked by using -ImmOff).
937     if ((ImmOff % Size == 0 && ImmOff >= 0 && ImmOff < (0x1000 << Scale)) ||
938         isPreferredADD(ImmOff) || isPreferredADD(-ImmOff))
939       return false;
940 
941     SDValue Ops[] = { RHS };
942     SDNode *MOVI =
943         CurDAG->getMachineNode(AArch64::MOVi64imm, DL, MVT::i64, Ops);
944     SDValue MOVIV = SDValue(MOVI, 0);
945     // This ADD of two X register will be selected into [Reg+Reg] mode.
946     N = CurDAG->getNode(ISD::ADD, DL, MVT::i64, LHS, MOVIV);
947   }
948 
949   // Remember if it is worth folding N when it produces extended register.
950   bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
951 
952   // Try to match a shifted extend on the RHS.
953   if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
954       SelectExtendedSHL(RHS, Size, false, Offset, SignExtend)) {
955     Base = LHS;
956     DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
957     return true;
958   }
959 
960   // Try to match a shifted extend on the LHS.
961   if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
962       SelectExtendedSHL(LHS, Size, false, Offset, SignExtend)) {
963     Base = RHS;
964     DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
965     return true;
966   }
967 
968   // Match any non-shifted, non-extend, non-immediate add expression.
969   Base = LHS;
970   Offset = RHS;
971   SignExtend = CurDAG->getTargetConstant(false, DL, MVT::i32);
972   DoShift = CurDAG->getTargetConstant(false, DL, MVT::i32);
973   // Reg1 + Reg2 is free: no check needed.
974   return true;
975 }
976 
977 SDValue AArch64DAGToDAGISel::createDTuple(ArrayRef<SDValue> Regs) {
978   static const unsigned RegClassIDs[] = {
979       AArch64::DDRegClassID, AArch64::DDDRegClassID, AArch64::DDDDRegClassID};
980   static const unsigned SubRegs[] = {AArch64::dsub0, AArch64::dsub1,
981                                      AArch64::dsub2, AArch64::dsub3};
982 
983   return createTuple(Regs, RegClassIDs, SubRegs);
984 }
985 
986 SDValue AArch64DAGToDAGISel::createQTuple(ArrayRef<SDValue> Regs) {
987   static const unsigned RegClassIDs[] = {
988       AArch64::QQRegClassID, AArch64::QQQRegClassID, AArch64::QQQQRegClassID};
989   static const unsigned SubRegs[] = {AArch64::qsub0, AArch64::qsub1,
990                                      AArch64::qsub2, AArch64::qsub3};
991 
992   return createTuple(Regs, RegClassIDs, SubRegs);
993 }
994 
995 SDValue AArch64DAGToDAGISel::createTuple(ArrayRef<SDValue> Regs,
996                                          const unsigned RegClassIDs[],
997                                          const unsigned SubRegs[]) {
998   // There's no special register-class for a vector-list of 1 element: it's just
999   // a vector.
1000   if (Regs.size() == 1)
1001     return Regs[0];
1002 
1003   assert(Regs.size() >= 2 && Regs.size() <= 4);
1004 
1005   SDLoc DL(Regs[0]);
1006 
1007   SmallVector<SDValue, 4> Ops;
1008 
1009   // First operand of REG_SEQUENCE is the desired RegClass.
1010   Ops.push_back(
1011       CurDAG->getTargetConstant(RegClassIDs[Regs.size() - 2], DL, MVT::i32));
1012 
1013   // Then we get pairs of source & subregister-position for the components.
1014   for (unsigned i = 0; i < Regs.size(); ++i) {
1015     Ops.push_back(Regs[i]);
1016     Ops.push_back(CurDAG->getTargetConstant(SubRegs[i], DL, MVT::i32));
1017   }
1018 
1019   SDNode *N =
1020       CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops);
1021   return SDValue(N, 0);
1022 }
1023 
1024 void AArch64DAGToDAGISel::SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc,
1025                                       bool isExt) {
1026   SDLoc dl(N);
1027   EVT VT = N->getValueType(0);
1028 
1029   unsigned ExtOff = isExt;
1030 
1031   // Form a REG_SEQUENCE to force register allocation.
1032   unsigned Vec0Off = ExtOff + 1;
1033   SmallVector<SDValue, 4> Regs(N->op_begin() + Vec0Off,
1034                                N->op_begin() + Vec0Off + NumVecs);
1035   SDValue RegSeq = createQTuple(Regs);
1036 
1037   SmallVector<SDValue, 6> Ops;
1038   if (isExt)
1039     Ops.push_back(N->getOperand(1));
1040   Ops.push_back(RegSeq);
1041   Ops.push_back(N->getOperand(NumVecs + ExtOff + 1));
1042   ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
1043 }
1044 
1045 bool AArch64DAGToDAGISel::tryIndexedLoad(SDNode *N) {
1046   LoadSDNode *LD = cast<LoadSDNode>(N);
1047   if (LD->isUnindexed())
1048     return false;
1049   EVT VT = LD->getMemoryVT();
1050   EVT DstVT = N->getValueType(0);
1051   ISD::MemIndexedMode AM = LD->getAddressingMode();
1052   bool IsPre = AM == ISD::PRE_INC || AM == ISD::PRE_DEC;
1053 
1054   // We're not doing validity checking here. That was done when checking
1055   // if we should mark the load as indexed or not. We're just selecting
1056   // the right instruction.
1057   unsigned Opcode = 0;
1058 
1059   ISD::LoadExtType ExtType = LD->getExtensionType();
1060   bool InsertTo64 = false;
1061   if (VT == MVT::i64)
1062     Opcode = IsPre ? AArch64::LDRXpre : AArch64::LDRXpost;
1063   else if (VT == MVT::i32) {
1064     if (ExtType == ISD::NON_EXTLOAD)
1065       Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
1066     else if (ExtType == ISD::SEXTLOAD)
1067       Opcode = IsPre ? AArch64::LDRSWpre : AArch64::LDRSWpost;
1068     else {
1069       Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
1070       InsertTo64 = true;
1071       // The result of the load is only i32. It's the subreg_to_reg that makes
1072       // it into an i64.
1073       DstVT = MVT::i32;
1074     }
1075   } else if (VT == MVT::i16) {
1076     if (ExtType == ISD::SEXTLOAD) {
1077       if (DstVT == MVT::i64)
1078         Opcode = IsPre ? AArch64::LDRSHXpre : AArch64::LDRSHXpost;
1079       else
1080         Opcode = IsPre ? AArch64::LDRSHWpre : AArch64::LDRSHWpost;
1081     } else {
1082       Opcode = IsPre ? AArch64::LDRHHpre : AArch64::LDRHHpost;
1083       InsertTo64 = DstVT == MVT::i64;
1084       // The result of the load is only i32. It's the subreg_to_reg that makes
1085       // it into an i64.
1086       DstVT = MVT::i32;
1087     }
1088   } else if (VT == MVT::i8) {
1089     if (ExtType == ISD::SEXTLOAD) {
1090       if (DstVT == MVT::i64)
1091         Opcode = IsPre ? AArch64::LDRSBXpre : AArch64::LDRSBXpost;
1092       else
1093         Opcode = IsPre ? AArch64::LDRSBWpre : AArch64::LDRSBWpost;
1094     } else {
1095       Opcode = IsPre ? AArch64::LDRBBpre : AArch64::LDRBBpost;
1096       InsertTo64 = DstVT == MVT::i64;
1097       // The result of the load is only i32. It's the subreg_to_reg that makes
1098       // it into an i64.
1099       DstVT = MVT::i32;
1100     }
1101   } else if (VT == MVT::f16) {
1102     Opcode = IsPre ? AArch64::LDRHpre : AArch64::LDRHpost;
1103   } else if (VT == MVT::f32) {
1104     Opcode = IsPre ? AArch64::LDRSpre : AArch64::LDRSpost;
1105   } else if (VT == MVT::f64 || VT.is64BitVector()) {
1106     Opcode = IsPre ? AArch64::LDRDpre : AArch64::LDRDpost;
1107   } else if (VT.is128BitVector()) {
1108     Opcode = IsPre ? AArch64::LDRQpre : AArch64::LDRQpost;
1109   } else
1110     return false;
1111   SDValue Chain = LD->getChain();
1112   SDValue Base = LD->getBasePtr();
1113   ConstantSDNode *OffsetOp = cast<ConstantSDNode>(LD->getOffset());
1114   int OffsetVal = (int)OffsetOp->getZExtValue();
1115   SDLoc dl(N);
1116   SDValue Offset = CurDAG->getTargetConstant(OffsetVal, dl, MVT::i64);
1117   SDValue Ops[] = { Base, Offset, Chain };
1118   SDNode *Res = CurDAG->getMachineNode(Opcode, dl, MVT::i64, DstVT,
1119                                        MVT::Other, Ops);
1120   // Either way, we're replacing the node, so tell the caller that.
1121   SDValue LoadedVal = SDValue(Res, 1);
1122   if (InsertTo64) {
1123     SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
1124     LoadedVal =
1125         SDValue(CurDAG->getMachineNode(
1126                     AArch64::SUBREG_TO_REG, dl, MVT::i64,
1127                     CurDAG->getTargetConstant(0, dl, MVT::i64), LoadedVal,
1128                     SubReg),
1129                 0);
1130   }
1131 
1132   ReplaceUses(SDValue(N, 0), LoadedVal);
1133   ReplaceUses(SDValue(N, 1), SDValue(Res, 0));
1134   ReplaceUses(SDValue(N, 2), SDValue(Res, 2));
1135   CurDAG->RemoveDeadNode(N);
1136   return true;
1137 }
1138 
1139 void AArch64DAGToDAGISel::SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
1140                                      unsigned SubRegIdx) {
1141   SDLoc dl(N);
1142   EVT VT = N->getValueType(0);
1143   SDValue Chain = N->getOperand(0);
1144 
1145   SDValue Ops[] = {N->getOperand(2), // Mem operand;
1146                    Chain};
1147 
1148   const EVT ResTys[] = {MVT::Untyped, MVT::Other};
1149 
1150   SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1151   SDValue SuperReg = SDValue(Ld, 0);
1152   for (unsigned i = 0; i < NumVecs; ++i)
1153     ReplaceUses(SDValue(N, i),
1154         CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
1155 
1156   ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
1157   CurDAG->RemoveDeadNode(N);
1158 }
1159 
1160 void AArch64DAGToDAGISel::SelectPostLoad(SDNode *N, unsigned NumVecs,
1161                                          unsigned Opc, unsigned SubRegIdx) {
1162   SDLoc dl(N);
1163   EVT VT = N->getValueType(0);
1164   SDValue Chain = N->getOperand(0);
1165 
1166   SDValue Ops[] = {N->getOperand(1), // Mem operand
1167                    N->getOperand(2), // Incremental
1168                    Chain};
1169 
1170   const EVT ResTys[] = {MVT::i64, // Type of the write back register
1171                         MVT::Untyped, MVT::Other};
1172 
1173   SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1174 
1175   // Update uses of write back register
1176   ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1177 
1178   // Update uses of vector list
1179   SDValue SuperReg = SDValue(Ld, 1);
1180   if (NumVecs == 1)
1181     ReplaceUses(SDValue(N, 0), SuperReg);
1182   else
1183     for (unsigned i = 0; i < NumVecs; ++i)
1184       ReplaceUses(SDValue(N, i),
1185           CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
1186 
1187   // Update the chain
1188   ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
1189   CurDAG->RemoveDeadNode(N);
1190 }
1191 
1192 void AArch64DAGToDAGISel::SelectStore(SDNode *N, unsigned NumVecs,
1193                                       unsigned Opc) {
1194   SDLoc dl(N);
1195   EVT VT = N->getOperand(2)->getValueType(0);
1196 
1197   // Form a REG_SEQUENCE to force register allocation.
1198   bool Is128Bit = VT.getSizeInBits() == 128;
1199   SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1200   SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1201 
1202   SDValue Ops[] = {RegSeq, N->getOperand(NumVecs + 2), N->getOperand(0)};
1203   SDNode *St = CurDAG->getMachineNode(Opc, dl, N->getValueType(0), Ops);
1204 
1205   ReplaceNode(N, St);
1206 }
1207 
1208 void AArch64DAGToDAGISel::SelectPostStore(SDNode *N, unsigned NumVecs,
1209                                           unsigned Opc) {
1210   SDLoc dl(N);
1211   EVT VT = N->getOperand(2)->getValueType(0);
1212   const EVT ResTys[] = {MVT::i64,    // Type of the write back register
1213                         MVT::Other}; // Type for the Chain
1214 
1215   // Form a REG_SEQUENCE to force register allocation.
1216   bool Is128Bit = VT.getSizeInBits() == 128;
1217   SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1218   SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1219 
1220   SDValue Ops[] = {RegSeq,
1221                    N->getOperand(NumVecs + 1), // base register
1222                    N->getOperand(NumVecs + 2), // Incremental
1223                    N->getOperand(0)};          // Chain
1224   SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1225 
1226   ReplaceNode(N, St);
1227 }
1228 
1229 namespace {
1230 /// WidenVector - Given a value in the V64 register class, produce the
1231 /// equivalent value in the V128 register class.
1232 class WidenVector {
1233   SelectionDAG &DAG;
1234 
1235 public:
1236   WidenVector(SelectionDAG &DAG) : DAG(DAG) {}
1237 
1238   SDValue operator()(SDValue V64Reg) {
1239     EVT VT = V64Reg.getValueType();
1240     unsigned NarrowSize = VT.getVectorNumElements();
1241     MVT EltTy = VT.getVectorElementType().getSimpleVT();
1242     MVT WideTy = MVT::getVectorVT(EltTy, 2 * NarrowSize);
1243     SDLoc DL(V64Reg);
1244 
1245     SDValue Undef =
1246         SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, WideTy), 0);
1247     return DAG.getTargetInsertSubreg(AArch64::dsub, DL, WideTy, Undef, V64Reg);
1248   }
1249 };
1250 } // namespace
1251 
1252 /// NarrowVector - Given a value in the V128 register class, produce the
1253 /// equivalent value in the V64 register class.
1254 static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG) {
1255   EVT VT = V128Reg.getValueType();
1256   unsigned WideSize = VT.getVectorNumElements();
1257   MVT EltTy = VT.getVectorElementType().getSimpleVT();
1258   MVT NarrowTy = MVT::getVectorVT(EltTy, WideSize / 2);
1259 
1260   return DAG.getTargetExtractSubreg(AArch64::dsub, SDLoc(V128Reg), NarrowTy,
1261                                     V128Reg);
1262 }
1263 
1264 void AArch64DAGToDAGISel::SelectLoadLane(SDNode *N, unsigned NumVecs,
1265                                          unsigned Opc) {
1266   SDLoc dl(N);
1267   EVT VT = N->getValueType(0);
1268   bool Narrow = VT.getSizeInBits() == 64;
1269 
1270   // Form a REG_SEQUENCE to force register allocation.
1271   SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1272 
1273   if (Narrow)
1274     transform(Regs, Regs.begin(),
1275                    WidenVector(*CurDAG));
1276 
1277   SDValue RegSeq = createQTuple(Regs);
1278 
1279   const EVT ResTys[] = {MVT::Untyped, MVT::Other};
1280 
1281   unsigned LaneNo =
1282       cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1283 
1284   SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
1285                    N->getOperand(NumVecs + 3), N->getOperand(0)};
1286   SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1287   SDValue SuperReg = SDValue(Ld, 0);
1288 
1289   EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
1290   static const unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1,
1291                                     AArch64::qsub2, AArch64::qsub3 };
1292   for (unsigned i = 0; i < NumVecs; ++i) {
1293     SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT, SuperReg);
1294     if (Narrow)
1295       NV = NarrowVector(NV, *CurDAG);
1296     ReplaceUses(SDValue(N, i), NV);
1297   }
1298 
1299   ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
1300   CurDAG->RemoveDeadNode(N);
1301 }
1302 
1303 void AArch64DAGToDAGISel::SelectPostLoadLane(SDNode *N, unsigned NumVecs,
1304                                              unsigned Opc) {
1305   SDLoc dl(N);
1306   EVT VT = N->getValueType(0);
1307   bool Narrow = VT.getSizeInBits() == 64;
1308 
1309   // Form a REG_SEQUENCE to force register allocation.
1310   SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1311 
1312   if (Narrow)
1313     transform(Regs, Regs.begin(),
1314                    WidenVector(*CurDAG));
1315 
1316   SDValue RegSeq = createQTuple(Regs);
1317 
1318   const EVT ResTys[] = {MVT::i64, // Type of the write back register
1319                         RegSeq->getValueType(0), MVT::Other};
1320 
1321   unsigned LaneNo =
1322       cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1323 
1324   SDValue Ops[] = {RegSeq,
1325                    CurDAG->getTargetConstant(LaneNo, dl,
1326                                              MVT::i64),         // Lane Number
1327                    N->getOperand(NumVecs + 2),                  // Base register
1328                    N->getOperand(NumVecs + 3),                  // Incremental
1329                    N->getOperand(0)};
1330   SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1331 
1332   // Update uses of the write back register
1333   ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1334 
1335   // Update uses of the vector list
1336   SDValue SuperReg = SDValue(Ld, 1);
1337   if (NumVecs == 1) {
1338     ReplaceUses(SDValue(N, 0),
1339                 Narrow ? NarrowVector(SuperReg, *CurDAG) : SuperReg);
1340   } else {
1341     EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
1342     static const unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1,
1343                                       AArch64::qsub2, AArch64::qsub3 };
1344     for (unsigned i = 0; i < NumVecs; ++i) {
1345       SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT,
1346                                                   SuperReg);
1347       if (Narrow)
1348         NV = NarrowVector(NV, *CurDAG);
1349       ReplaceUses(SDValue(N, i), NV);
1350     }
1351   }
1352 
1353   // Update the Chain
1354   ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
1355   CurDAG->RemoveDeadNode(N);
1356 }
1357 
1358 void AArch64DAGToDAGISel::SelectStoreLane(SDNode *N, unsigned NumVecs,
1359                                           unsigned Opc) {
1360   SDLoc dl(N);
1361   EVT VT = N->getOperand(2)->getValueType(0);
1362   bool Narrow = VT.getSizeInBits() == 64;
1363 
1364   // Form a REG_SEQUENCE to force register allocation.
1365   SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1366 
1367   if (Narrow)
1368     transform(Regs, Regs.begin(),
1369                    WidenVector(*CurDAG));
1370 
1371   SDValue RegSeq = createQTuple(Regs);
1372 
1373   unsigned LaneNo =
1374       cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1375 
1376   SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
1377                    N->getOperand(NumVecs + 3), N->getOperand(0)};
1378   SDNode *St = CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops);
1379 
1380   // Transfer memoperands.
1381   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1382   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1383   cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1384 
1385   ReplaceNode(N, St);
1386 }
1387 
1388 void AArch64DAGToDAGISel::SelectPostStoreLane(SDNode *N, unsigned NumVecs,
1389                                               unsigned Opc) {
1390   SDLoc dl(N);
1391   EVT VT = N->getOperand(2)->getValueType(0);
1392   bool Narrow = VT.getSizeInBits() == 64;
1393 
1394   // Form a REG_SEQUENCE to force register allocation.
1395   SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1396 
1397   if (Narrow)
1398     transform(Regs, Regs.begin(),
1399                    WidenVector(*CurDAG));
1400 
1401   SDValue RegSeq = createQTuple(Regs);
1402 
1403   const EVT ResTys[] = {MVT::i64, // Type of the write back register
1404                         MVT::Other};
1405 
1406   unsigned LaneNo =
1407       cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1408 
1409   SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
1410                    N->getOperand(NumVecs + 2), // Base Register
1411                    N->getOperand(NumVecs + 3), // Incremental
1412                    N->getOperand(0)};
1413   SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1414 
1415   // Transfer memoperands.
1416   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1417   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1418   cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1419 
1420   ReplaceNode(N, St);
1421 }
1422 
1423 static bool isBitfieldExtractOpFromAnd(SelectionDAG *CurDAG, SDNode *N,
1424                                        unsigned &Opc, SDValue &Opd0,
1425                                        unsigned &LSB, unsigned &MSB,
1426                                        unsigned NumberOfIgnoredLowBits,
1427                                        bool BiggerPattern) {
1428   assert(N->getOpcode() == ISD::AND &&
1429          "N must be a AND operation to call this function");
1430 
1431   EVT VT = N->getValueType(0);
1432 
1433   // Here we can test the type of VT and return false when the type does not
1434   // match, but since it is done prior to that call in the current context
1435   // we turned that into an assert to avoid redundant code.
1436   assert((VT == MVT::i32 || VT == MVT::i64) &&
1437          "Type checking must have been done before calling this function");
1438 
1439   // FIXME: simplify-demanded-bits in DAGCombine will probably have
1440   // changed the AND node to a 32-bit mask operation. We'll have to
1441   // undo that as part of the transform here if we want to catch all
1442   // the opportunities.
1443   // Currently the NumberOfIgnoredLowBits argument helps to recover
1444   // form these situations when matching bigger pattern (bitfield insert).
1445 
1446   // For unsigned extracts, check for a shift right and mask
1447   uint64_t AndImm = 0;
1448   if (!isOpcWithIntImmediate(N, ISD::AND, AndImm))
1449     return false;
1450 
1451   const SDNode *Op0 = N->getOperand(0).getNode();
1452 
1453   // Because of simplify-demanded-bits in DAGCombine, the mask may have been
1454   // simplified. Try to undo that
1455   AndImm |= (1 << NumberOfIgnoredLowBits) - 1;
1456 
1457   // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1458   if (AndImm & (AndImm + 1))
1459     return false;
1460 
1461   bool ClampMSB = false;
1462   uint64_t SrlImm = 0;
1463   // Handle the SRL + ANY_EXTEND case.
1464   if (VT == MVT::i64 && Op0->getOpcode() == ISD::ANY_EXTEND &&
1465       isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL, SrlImm)) {
1466     // Extend the incoming operand of the SRL to 64-bit.
1467     Opd0 = Widen(CurDAG, Op0->getOperand(0).getOperand(0));
1468     // Make sure to clamp the MSB so that we preserve the semantics of the
1469     // original operations.
1470     ClampMSB = true;
1471   } else if (VT == MVT::i32 && Op0->getOpcode() == ISD::TRUNCATE &&
1472              isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL,
1473                                    SrlImm)) {
1474     // If the shift result was truncated, we can still combine them.
1475     Opd0 = Op0->getOperand(0).getOperand(0);
1476 
1477     // Use the type of SRL node.
1478     VT = Opd0->getValueType(0);
1479   } else if (isOpcWithIntImmediate(Op0, ISD::SRL, SrlImm)) {
1480     Opd0 = Op0->getOperand(0);
1481   } else if (BiggerPattern) {
1482     // Let's pretend a 0 shift right has been performed.
1483     // The resulting code will be at least as good as the original one
1484     // plus it may expose more opportunities for bitfield insert pattern.
1485     // FIXME: Currently we limit this to the bigger pattern, because
1486     // some optimizations expect AND and not UBFM.
1487     Opd0 = N->getOperand(0);
1488   } else
1489     return false;
1490 
1491   // Bail out on large immediates. This happens when no proper
1492   // combining/constant folding was performed.
1493   if (!BiggerPattern && (SrlImm <= 0 || SrlImm >= VT.getSizeInBits())) {
1494     DEBUG((dbgs() << N
1495            << ": Found large shift immediate, this should not happen\n"));
1496     return false;
1497   }
1498 
1499   LSB = SrlImm;
1500   MSB = SrlImm + (VT == MVT::i32 ? countTrailingOnes<uint32_t>(AndImm)
1501                                  : countTrailingOnes<uint64_t>(AndImm)) -
1502         1;
1503   if (ClampMSB)
1504     // Since we're moving the extend before the right shift operation, we need
1505     // to clamp the MSB to make sure we don't shift in undefined bits instead of
1506     // the zeros which would get shifted in with the original right shift
1507     // operation.
1508     MSB = MSB > 31 ? 31 : MSB;
1509 
1510   Opc = VT == MVT::i32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1511   return true;
1512 }
1513 
1514 static bool isBitfieldExtractOpFromSExtInReg(SDNode *N, unsigned &Opc,
1515                                              SDValue &Opd0, unsigned &Immr,
1516                                              unsigned &Imms) {
1517   assert(N->getOpcode() == ISD::SIGN_EXTEND_INREG);
1518 
1519   EVT VT = N->getValueType(0);
1520   unsigned BitWidth = VT.getSizeInBits();
1521   assert((VT == MVT::i32 || VT == MVT::i64) &&
1522          "Type checking must have been done before calling this function");
1523 
1524   SDValue Op = N->getOperand(0);
1525   if (Op->getOpcode() == ISD::TRUNCATE) {
1526     Op = Op->getOperand(0);
1527     VT = Op->getValueType(0);
1528     BitWidth = VT.getSizeInBits();
1529   }
1530 
1531   uint64_t ShiftImm;
1532   if (!isOpcWithIntImmediate(Op.getNode(), ISD::SRL, ShiftImm) &&
1533       !isOpcWithIntImmediate(Op.getNode(), ISD::SRA, ShiftImm))
1534     return false;
1535 
1536   unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
1537   if (ShiftImm + Width > BitWidth)
1538     return false;
1539 
1540   Opc = (VT == MVT::i32) ? AArch64::SBFMWri : AArch64::SBFMXri;
1541   Opd0 = Op.getOperand(0);
1542   Immr = ShiftImm;
1543   Imms = ShiftImm + Width - 1;
1544   return true;
1545 }
1546 
1547 static bool isSeveralBitsExtractOpFromShr(SDNode *N, unsigned &Opc,
1548                                           SDValue &Opd0, unsigned &LSB,
1549                                           unsigned &MSB) {
1550   // We are looking for the following pattern which basically extracts several
1551   // continuous bits from the source value and places it from the LSB of the
1552   // destination value, all other bits of the destination value or set to zero:
1553   //
1554   // Value2 = AND Value, MaskImm
1555   // SRL Value2, ShiftImm
1556   //
1557   // with MaskImm >> ShiftImm to search for the bit width.
1558   //
1559   // This gets selected into a single UBFM:
1560   //
1561   // UBFM Value, ShiftImm, BitWide + SrlImm -1
1562   //
1563 
1564   if (N->getOpcode() != ISD::SRL)
1565     return false;
1566 
1567   uint64_t AndMask = 0;
1568   if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, AndMask))
1569     return false;
1570 
1571   Opd0 = N->getOperand(0).getOperand(0);
1572 
1573   uint64_t SrlImm = 0;
1574   if (!isIntImmediate(N->getOperand(1), SrlImm))
1575     return false;
1576 
1577   // Check whether we really have several bits extract here.
1578   unsigned BitWide = 64 - countLeadingOnes(~(AndMask >> SrlImm));
1579   if (BitWide && isMask_64(AndMask >> SrlImm)) {
1580     if (N->getValueType(0) == MVT::i32)
1581       Opc = AArch64::UBFMWri;
1582     else
1583       Opc = AArch64::UBFMXri;
1584 
1585     LSB = SrlImm;
1586     MSB = BitWide + SrlImm - 1;
1587     return true;
1588   }
1589 
1590   return false;
1591 }
1592 
1593 static bool isBitfieldExtractOpFromShr(SDNode *N, unsigned &Opc, SDValue &Opd0,
1594                                        unsigned &Immr, unsigned &Imms,
1595                                        bool BiggerPattern) {
1596   assert((N->getOpcode() == ISD::SRA || N->getOpcode() == ISD::SRL) &&
1597          "N must be a SHR/SRA operation to call this function");
1598 
1599   EVT VT = N->getValueType(0);
1600 
1601   // Here we can test the type of VT and return false when the type does not
1602   // match, but since it is done prior to that call in the current context
1603   // we turned that into an assert to avoid redundant code.
1604   assert((VT == MVT::i32 || VT == MVT::i64) &&
1605          "Type checking must have been done before calling this function");
1606 
1607   // Check for AND + SRL doing several bits extract.
1608   if (isSeveralBitsExtractOpFromShr(N, Opc, Opd0, Immr, Imms))
1609     return true;
1610 
1611   // We're looking for a shift of a shift.
1612   uint64_t ShlImm = 0;
1613   uint64_t TruncBits = 0;
1614   if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, ShlImm)) {
1615     Opd0 = N->getOperand(0).getOperand(0);
1616   } else if (VT == MVT::i32 && N->getOpcode() == ISD::SRL &&
1617              N->getOperand(0).getNode()->getOpcode() == ISD::TRUNCATE) {
1618     // We are looking for a shift of truncate. Truncate from i64 to i32 could
1619     // be considered as setting high 32 bits as zero. Our strategy here is to
1620     // always generate 64bit UBFM. This consistency will help the CSE pass
1621     // later find more redundancy.
1622     Opd0 = N->getOperand(0).getOperand(0);
1623     TruncBits = Opd0->getValueType(0).getSizeInBits() - VT.getSizeInBits();
1624     VT = Opd0->getValueType(0);
1625     assert(VT == MVT::i64 && "the promoted type should be i64");
1626   } else if (BiggerPattern) {
1627     // Let's pretend a 0 shift left has been performed.
1628     // FIXME: Currently we limit this to the bigger pattern case,
1629     // because some optimizations expect AND and not UBFM
1630     Opd0 = N->getOperand(0);
1631   } else
1632     return false;
1633 
1634   // Missing combines/constant folding may have left us with strange
1635   // constants.
1636   if (ShlImm >= VT.getSizeInBits()) {
1637     DEBUG((dbgs() << N
1638            << ": Found large shift immediate, this should not happen\n"));
1639     return false;
1640   }
1641 
1642   uint64_t SrlImm = 0;
1643   if (!isIntImmediate(N->getOperand(1), SrlImm))
1644     return false;
1645 
1646   assert(SrlImm > 0 && SrlImm < VT.getSizeInBits() &&
1647          "bad amount in shift node!");
1648   int immr = SrlImm - ShlImm;
1649   Immr = immr < 0 ? immr + VT.getSizeInBits() : immr;
1650   Imms = VT.getSizeInBits() - ShlImm - TruncBits - 1;
1651   // SRA requires a signed extraction
1652   if (VT == MVT::i32)
1653     Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMWri : AArch64::UBFMWri;
1654   else
1655     Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMXri : AArch64::UBFMXri;
1656   return true;
1657 }
1658 
1659 bool AArch64DAGToDAGISel::tryBitfieldExtractOpFromSExt(SDNode *N) {
1660   assert(N->getOpcode() == ISD::SIGN_EXTEND);
1661 
1662   EVT VT = N->getValueType(0);
1663   EVT NarrowVT = N->getOperand(0)->getValueType(0);
1664   if (VT != MVT::i64 || NarrowVT != MVT::i32)
1665     return false;
1666 
1667   uint64_t ShiftImm;
1668   SDValue Op = N->getOperand(0);
1669   if (!isOpcWithIntImmediate(Op.getNode(), ISD::SRA, ShiftImm))
1670     return false;
1671 
1672   SDLoc dl(N);
1673   // Extend the incoming operand of the shift to 64-bits.
1674   SDValue Opd0 = Widen(CurDAG, Op.getOperand(0));
1675   unsigned Immr = ShiftImm;
1676   unsigned Imms = NarrowVT.getSizeInBits() - 1;
1677   SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
1678                    CurDAG->getTargetConstant(Imms, dl, VT)};
1679   CurDAG->SelectNodeTo(N, AArch64::SBFMXri, VT, Ops);
1680   return true;
1681 }
1682 
1683 static bool isBitfieldExtractOp(SelectionDAG *CurDAG, SDNode *N, unsigned &Opc,
1684                                 SDValue &Opd0, unsigned &Immr, unsigned &Imms,
1685                                 unsigned NumberOfIgnoredLowBits = 0,
1686                                 bool BiggerPattern = false) {
1687   if (N->getValueType(0) != MVT::i32 && N->getValueType(0) != MVT::i64)
1688     return false;
1689 
1690   switch (N->getOpcode()) {
1691   default:
1692     if (!N->isMachineOpcode())
1693       return false;
1694     break;
1695   case ISD::AND:
1696     return isBitfieldExtractOpFromAnd(CurDAG, N, Opc, Opd0, Immr, Imms,
1697                                       NumberOfIgnoredLowBits, BiggerPattern);
1698   case ISD::SRL:
1699   case ISD::SRA:
1700     return isBitfieldExtractOpFromShr(N, Opc, Opd0, Immr, Imms, BiggerPattern);
1701 
1702   case ISD::SIGN_EXTEND_INREG:
1703     return isBitfieldExtractOpFromSExtInReg(N, Opc, Opd0, Immr, Imms);
1704   }
1705 
1706   unsigned NOpc = N->getMachineOpcode();
1707   switch (NOpc) {
1708   default:
1709     return false;
1710   case AArch64::SBFMWri:
1711   case AArch64::UBFMWri:
1712   case AArch64::SBFMXri:
1713   case AArch64::UBFMXri:
1714     Opc = NOpc;
1715     Opd0 = N->getOperand(0);
1716     Immr = cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
1717     Imms = cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
1718     return true;
1719   }
1720   // Unreachable
1721   return false;
1722 }
1723 
1724 bool AArch64DAGToDAGISel::tryBitfieldExtractOp(SDNode *N) {
1725   unsigned Opc, Immr, Imms;
1726   SDValue Opd0;
1727   if (!isBitfieldExtractOp(CurDAG, N, Opc, Opd0, Immr, Imms))
1728     return false;
1729 
1730   EVT VT = N->getValueType(0);
1731   SDLoc dl(N);
1732 
1733   // If the bit extract operation is 64bit but the original type is 32bit, we
1734   // need to add one EXTRACT_SUBREG.
1735   if ((Opc == AArch64::SBFMXri || Opc == AArch64::UBFMXri) && VT == MVT::i32) {
1736     SDValue Ops64[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, MVT::i64),
1737                        CurDAG->getTargetConstant(Imms, dl, MVT::i64)};
1738 
1739     SDNode *BFM = CurDAG->getMachineNode(Opc, dl, MVT::i64, Ops64);
1740     SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
1741     ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
1742                                           MVT::i32, SDValue(BFM, 0), SubReg));
1743     return true;
1744   }
1745 
1746   SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
1747                    CurDAG->getTargetConstant(Imms, dl, VT)};
1748   CurDAG->SelectNodeTo(N, Opc, VT, Ops);
1749   return true;
1750 }
1751 
1752 /// Does DstMask form a complementary pair with the mask provided by
1753 /// BitsToBeInserted, suitable for use in a BFI instruction. Roughly speaking,
1754 /// this asks whether DstMask zeroes precisely those bits that will be set by
1755 /// the other half.
1756 static bool isBitfieldDstMask(uint64_t DstMask, const APInt &BitsToBeInserted,
1757                               unsigned NumberOfIgnoredHighBits, EVT VT) {
1758   assert((VT == MVT::i32 || VT == MVT::i64) &&
1759          "i32 or i64 mask type expected!");
1760   unsigned BitWidth = VT.getSizeInBits() - NumberOfIgnoredHighBits;
1761 
1762   APInt SignificantDstMask = APInt(BitWidth, DstMask);
1763   APInt SignificantBitsToBeInserted = BitsToBeInserted.zextOrTrunc(BitWidth);
1764 
1765   return (SignificantDstMask & SignificantBitsToBeInserted) == 0 &&
1766          (SignificantDstMask | SignificantBitsToBeInserted).isAllOnesValue();
1767 }
1768 
1769 // Look for bits that will be useful for later uses.
1770 // A bit is consider useless as soon as it is dropped and never used
1771 // before it as been dropped.
1772 // E.g., looking for useful bit of x
1773 // 1. y = x & 0x7
1774 // 2. z = y >> 2
1775 // After #1, x useful bits are 0x7, then the useful bits of x, live through
1776 // y.
1777 // After #2, the useful bits of x are 0x4.
1778 // However, if x is used on an unpredicatable instruction, then all its bits
1779 // are useful.
1780 // E.g.
1781 // 1. y = x & 0x7
1782 // 2. z = y >> 2
1783 // 3. str x, [@x]
1784 static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth = 0);
1785 
1786 static void getUsefulBitsFromAndWithImmediate(SDValue Op, APInt &UsefulBits,
1787                                               unsigned Depth) {
1788   uint64_t Imm =
1789       cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1790   Imm = AArch64_AM::decodeLogicalImmediate(Imm, UsefulBits.getBitWidth());
1791   UsefulBits &= APInt(UsefulBits.getBitWidth(), Imm);
1792   getUsefulBits(Op, UsefulBits, Depth + 1);
1793 }
1794 
1795 static void getUsefulBitsFromBitfieldMoveOpd(SDValue Op, APInt &UsefulBits,
1796                                              uint64_t Imm, uint64_t MSB,
1797                                              unsigned Depth) {
1798   // inherit the bitwidth value
1799   APInt OpUsefulBits(UsefulBits);
1800   OpUsefulBits = 1;
1801 
1802   if (MSB >= Imm) {
1803     OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1804     --OpUsefulBits;
1805     // The interesting part will be in the lower part of the result
1806     getUsefulBits(Op, OpUsefulBits, Depth + 1);
1807     // The interesting part was starting at Imm in the argument
1808     OpUsefulBits = OpUsefulBits.shl(Imm);
1809   } else {
1810     OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1811     --OpUsefulBits;
1812     // The interesting part will be shifted in the result
1813     OpUsefulBits = OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm);
1814     getUsefulBits(Op, OpUsefulBits, Depth + 1);
1815     // The interesting part was at zero in the argument
1816     OpUsefulBits = OpUsefulBits.lshr(OpUsefulBits.getBitWidth() - Imm);
1817   }
1818 
1819   UsefulBits &= OpUsefulBits;
1820 }
1821 
1822 static void getUsefulBitsFromUBFM(SDValue Op, APInt &UsefulBits,
1823                                   unsigned Depth) {
1824   uint64_t Imm =
1825       cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1826   uint64_t MSB =
1827       cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1828 
1829   getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1830 }
1831 
1832 static void getUsefulBitsFromOrWithShiftedReg(SDValue Op, APInt &UsefulBits,
1833                                               unsigned Depth) {
1834   uint64_t ShiftTypeAndValue =
1835       cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1836   APInt Mask(UsefulBits);
1837   Mask.clearAllBits();
1838   Mask.flipAllBits();
1839 
1840   if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSL) {
1841     // Shift Left
1842     uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1843     Mask = Mask.shl(ShiftAmt);
1844     getUsefulBits(Op, Mask, Depth + 1);
1845     Mask = Mask.lshr(ShiftAmt);
1846   } else if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSR) {
1847     // Shift Right
1848     // We do not handle AArch64_AM::ASR, because the sign will change the
1849     // number of useful bits
1850     uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1851     Mask = Mask.lshr(ShiftAmt);
1852     getUsefulBits(Op, Mask, Depth + 1);
1853     Mask = Mask.shl(ShiftAmt);
1854   } else
1855     return;
1856 
1857   UsefulBits &= Mask;
1858 }
1859 
1860 static void getUsefulBitsFromBFM(SDValue Op, SDValue Orig, APInt &UsefulBits,
1861                                  unsigned Depth) {
1862   uint64_t Imm =
1863       cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1864   uint64_t MSB =
1865       cast<const ConstantSDNode>(Op.getOperand(3).getNode())->getZExtValue();
1866 
1867   if (Op.getOperand(1) == Orig)
1868     return getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1869 
1870   APInt OpUsefulBits(UsefulBits);
1871   OpUsefulBits = 1;
1872 
1873   if (MSB >= Imm) {
1874     OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1875     --OpUsefulBits;
1876     UsefulBits &= ~OpUsefulBits;
1877     getUsefulBits(Op, UsefulBits, Depth + 1);
1878   } else {
1879     OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1880     --OpUsefulBits;
1881     UsefulBits = ~(OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm));
1882     getUsefulBits(Op, UsefulBits, Depth + 1);
1883   }
1884 }
1885 
1886 static void getUsefulBitsForUse(SDNode *UserNode, APInt &UsefulBits,
1887                                 SDValue Orig, unsigned Depth) {
1888 
1889   // Users of this node should have already been instruction selected
1890   // FIXME: Can we turn that into an assert?
1891   if (!UserNode->isMachineOpcode())
1892     return;
1893 
1894   switch (UserNode->getMachineOpcode()) {
1895   default:
1896     return;
1897   case AArch64::ANDSWri:
1898   case AArch64::ANDSXri:
1899   case AArch64::ANDWri:
1900   case AArch64::ANDXri:
1901     // We increment Depth only when we call the getUsefulBits
1902     return getUsefulBitsFromAndWithImmediate(SDValue(UserNode, 0), UsefulBits,
1903                                              Depth);
1904   case AArch64::UBFMWri:
1905   case AArch64::UBFMXri:
1906     return getUsefulBitsFromUBFM(SDValue(UserNode, 0), UsefulBits, Depth);
1907 
1908   case AArch64::ORRWrs:
1909   case AArch64::ORRXrs:
1910     if (UserNode->getOperand(1) != Orig)
1911       return;
1912     return getUsefulBitsFromOrWithShiftedReg(SDValue(UserNode, 0), UsefulBits,
1913                                              Depth);
1914   case AArch64::BFMWri:
1915   case AArch64::BFMXri:
1916     return getUsefulBitsFromBFM(SDValue(UserNode, 0), Orig, UsefulBits, Depth);
1917 
1918   case AArch64::STRBBui:
1919   case AArch64::STURBBi:
1920     if (UserNode->getOperand(0) != Orig)
1921       return;
1922     UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xff);
1923     return;
1924 
1925   case AArch64::STRHHui:
1926   case AArch64::STURHHi:
1927     if (UserNode->getOperand(0) != Orig)
1928       return;
1929     UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xffff);
1930     return;
1931   }
1932 }
1933 
1934 static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth) {
1935   if (Depth >= 6)
1936     return;
1937   // Initialize UsefulBits
1938   if (!Depth) {
1939     unsigned Bitwidth = Op.getScalarValueSizeInBits();
1940     // At the beginning, assume every produced bits is useful
1941     UsefulBits = APInt(Bitwidth, 0);
1942     UsefulBits.flipAllBits();
1943   }
1944   APInt UsersUsefulBits(UsefulBits.getBitWidth(), 0);
1945 
1946   for (SDNode *Node : Op.getNode()->uses()) {
1947     // A use cannot produce useful bits
1948     APInt UsefulBitsForUse = APInt(UsefulBits);
1949     getUsefulBitsForUse(Node, UsefulBitsForUse, Op, Depth);
1950     UsersUsefulBits |= UsefulBitsForUse;
1951   }
1952   // UsefulBits contains the produced bits that are meaningful for the
1953   // current definition, thus a user cannot make a bit meaningful at
1954   // this point
1955   UsefulBits &= UsersUsefulBits;
1956 }
1957 
1958 /// Create a machine node performing a notional SHL of Op by ShlAmount. If
1959 /// ShlAmount is negative, do a (logical) right-shift instead. If ShlAmount is
1960 /// 0, return Op unchanged.
1961 static SDValue getLeftShift(SelectionDAG *CurDAG, SDValue Op, int ShlAmount) {
1962   if (ShlAmount == 0)
1963     return Op;
1964 
1965   EVT VT = Op.getValueType();
1966   SDLoc dl(Op);
1967   unsigned BitWidth = VT.getSizeInBits();
1968   unsigned UBFMOpc = BitWidth == 32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1969 
1970   SDNode *ShiftNode;
1971   if (ShlAmount > 0) {
1972     // LSL wD, wN, #Amt == UBFM wD, wN, #32-Amt, #31-Amt
1973     ShiftNode = CurDAG->getMachineNode(
1974         UBFMOpc, dl, VT, Op,
1975         CurDAG->getTargetConstant(BitWidth - ShlAmount, dl, VT),
1976         CurDAG->getTargetConstant(BitWidth - 1 - ShlAmount, dl, VT));
1977   } else {
1978     // LSR wD, wN, #Amt == UBFM wD, wN, #Amt, #32-1
1979     assert(ShlAmount < 0 && "expected right shift");
1980     int ShrAmount = -ShlAmount;
1981     ShiftNode = CurDAG->getMachineNode(
1982         UBFMOpc, dl, VT, Op, CurDAG->getTargetConstant(ShrAmount, dl, VT),
1983         CurDAG->getTargetConstant(BitWidth - 1, dl, VT));
1984   }
1985 
1986   return SDValue(ShiftNode, 0);
1987 }
1988 
1989 /// Does this tree qualify as an attempt to move a bitfield into position,
1990 /// essentially "(and (shl VAL, N), Mask)".
1991 static bool isBitfieldPositioningOp(SelectionDAG *CurDAG, SDValue Op,
1992                                     bool BiggerPattern,
1993                                     SDValue &Src, int &ShiftAmount,
1994                                     int &MaskWidth) {
1995   EVT VT = Op.getValueType();
1996   unsigned BitWidth = VT.getSizeInBits();
1997   (void)BitWidth;
1998   assert(BitWidth == 32 || BitWidth == 64);
1999 
2000   APInt KnownZero, KnownOne;
2001   CurDAG->computeKnownBits(Op, KnownZero, KnownOne);
2002 
2003   // Non-zero in the sense that they're not provably zero, which is the key
2004   // point if we want to use this value
2005   uint64_t NonZeroBits = (~KnownZero).getZExtValue();
2006 
2007   // Discard a constant AND mask if present. It's safe because the node will
2008   // already have been factored into the computeKnownBits calculation above.
2009   uint64_t AndImm;
2010   if (isOpcWithIntImmediate(Op.getNode(), ISD::AND, AndImm)) {
2011     assert((~APInt(BitWidth, AndImm) & ~KnownZero) == 0);
2012     Op = Op.getOperand(0);
2013   }
2014 
2015   // Don't match if the SHL has more than one use, since then we'll end up
2016   // generating SHL+UBFIZ instead of just keeping SHL+AND.
2017   if (!BiggerPattern && !Op.hasOneUse())
2018     return false;
2019 
2020   uint64_t ShlImm;
2021   if (!isOpcWithIntImmediate(Op.getNode(), ISD::SHL, ShlImm))
2022     return false;
2023   Op = Op.getOperand(0);
2024 
2025   if (!isShiftedMask_64(NonZeroBits))
2026     return false;
2027 
2028   ShiftAmount = countTrailingZeros(NonZeroBits);
2029   MaskWidth = countTrailingOnes(NonZeroBits >> ShiftAmount);
2030 
2031   // BFI encompasses sufficiently many nodes that it's worth inserting an extra
2032   // LSL/LSR if the mask in NonZeroBits doesn't quite match up with the ISD::SHL
2033   // amount.  BiggerPattern is true when this pattern is being matched for BFI,
2034   // BiggerPattern is false when this pattern is being matched for UBFIZ, in
2035   // which case it is not profitable to insert an extra shift.
2036   if (ShlImm - ShiftAmount != 0 && !BiggerPattern)
2037     return false;
2038   Src = getLeftShift(CurDAG, Op, ShlImm - ShiftAmount);
2039 
2040   return true;
2041 }
2042 
2043 static bool isShiftedMask(uint64_t Mask, EVT VT) {
2044   assert(VT == MVT::i32 || VT == MVT::i64);
2045   if (VT == MVT::i32)
2046     return isShiftedMask_32(Mask);
2047   return isShiftedMask_64(Mask);
2048 }
2049 
2050 // Generate a BFI/BFXIL from 'or (and X, MaskImm), OrImm' iff the value being
2051 // inserted only sets known zero bits.
2052 static bool tryBitfieldInsertOpFromOrAndImm(SDNode *N, SelectionDAG *CurDAG) {
2053   assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
2054 
2055   EVT VT = N->getValueType(0);
2056   if (VT != MVT::i32 && VT != MVT::i64)
2057     return false;
2058 
2059   unsigned BitWidth = VT.getSizeInBits();
2060 
2061   uint64_t OrImm;
2062   if (!isOpcWithIntImmediate(N, ISD::OR, OrImm))
2063     return false;
2064 
2065   // Skip this transformation if the ORR immediate can be encoded in the ORR.
2066   // Otherwise, we'll trade an AND+ORR for ORR+BFI/BFXIL, which is most likely
2067   // performance neutral.
2068   if (AArch64_AM::isLogicalImmediate(OrImm, BitWidth))
2069     return false;
2070 
2071   uint64_t MaskImm;
2072   SDValue And = N->getOperand(0);
2073   // Must be a single use AND with an immediate operand.
2074   if (!And.hasOneUse() ||
2075       !isOpcWithIntImmediate(And.getNode(), ISD::AND, MaskImm))
2076     return false;
2077 
2078   // Compute the Known Zero for the AND as this allows us to catch more general
2079   // cases than just looking for AND with imm.
2080   APInt KnownZero, KnownOne;
2081   CurDAG->computeKnownBits(And, KnownZero, KnownOne);
2082 
2083   // Non-zero in the sense that they're not provably zero, which is the key
2084   // point if we want to use this value.
2085   uint64_t NotKnownZero = (~KnownZero).getZExtValue();
2086 
2087   // The KnownZero mask must be a shifted mask (e.g., 1110..011, 11100..00).
2088   if (!isShiftedMask(KnownZero.getZExtValue(), VT))
2089     return false;
2090 
2091   // The bits being inserted must only set those bits that are known to be zero.
2092   if ((OrImm & NotKnownZero) != 0) {
2093     // FIXME:  It's okay if the OrImm sets NotKnownZero bits to 1, but we don't
2094     // currently handle this case.
2095     return false;
2096   }
2097 
2098   // BFI/BFXIL dst, src, #lsb, #width.
2099   int LSB = countTrailingOnes(NotKnownZero);
2100   int Width = BitWidth - APInt(BitWidth, NotKnownZero).countPopulation();
2101 
2102   // BFI/BFXIL is an alias of BFM, so translate to BFM operands.
2103   unsigned ImmR = (BitWidth - LSB) % BitWidth;
2104   unsigned ImmS = Width - 1;
2105 
2106   // If we're creating a BFI instruction avoid cases where we need more
2107   // instructions to materialize the BFI constant as compared to the original
2108   // ORR.  A BFXIL will use the same constant as the original ORR, so the code
2109   // should be no worse in this case.
2110   bool IsBFI = LSB != 0;
2111   uint64_t BFIImm = OrImm >> LSB;
2112   if (IsBFI && !AArch64_AM::isLogicalImmediate(BFIImm, BitWidth)) {
2113     // We have a BFI instruction and we know the constant can't be materialized
2114     // with a ORR-immediate with the zero register.
2115     unsigned OrChunks = 0, BFIChunks = 0;
2116     for (unsigned Shift = 0; Shift < BitWidth; Shift += 16) {
2117       if (((OrImm >> Shift) & 0xFFFF) != 0)
2118         ++OrChunks;
2119       if (((BFIImm >> Shift) & 0xFFFF) != 0)
2120         ++BFIChunks;
2121     }
2122     if (BFIChunks > OrChunks)
2123       return false;
2124   }
2125 
2126   // Materialize the constant to be inserted.
2127   SDLoc DL(N);
2128   unsigned MOVIOpc = VT == MVT::i32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
2129   SDNode *MOVI = CurDAG->getMachineNode(
2130       MOVIOpc, DL, VT, CurDAG->getTargetConstant(BFIImm, DL, VT));
2131 
2132   // Create the BFI/BFXIL instruction.
2133   SDValue Ops[] = {And.getOperand(0), SDValue(MOVI, 0),
2134                    CurDAG->getTargetConstant(ImmR, DL, VT),
2135                    CurDAG->getTargetConstant(ImmS, DL, VT)};
2136   unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2137   CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2138   return true;
2139 }
2140 
2141 static bool tryBitfieldInsertOpFromOr(SDNode *N, const APInt &UsefulBits,
2142                                       SelectionDAG *CurDAG) {
2143   assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
2144 
2145   EVT VT = N->getValueType(0);
2146   if (VT != MVT::i32 && VT != MVT::i64)
2147     return false;
2148 
2149   unsigned BitWidth = VT.getSizeInBits();
2150 
2151   // Because of simplify-demanded-bits in DAGCombine, involved masks may not
2152   // have the expected shape. Try to undo that.
2153 
2154   unsigned NumberOfIgnoredLowBits = UsefulBits.countTrailingZeros();
2155   unsigned NumberOfIgnoredHighBits = UsefulBits.countLeadingZeros();
2156 
2157   // Given a OR operation, check if we have the following pattern
2158   // ubfm c, b, imm, imm2 (or something that does the same jobs, see
2159   //                       isBitfieldExtractOp)
2160   // d = e & mask2 ; where mask is a binary sequence of 1..10..0 and
2161   //                 countTrailingZeros(mask2) == imm2 - imm + 1
2162   // f = d | c
2163   // if yes, replace the OR instruction with:
2164   // f = BFM Opd0, Opd1, LSB, MSB ; where LSB = imm, and MSB = imm2
2165 
2166   // OR is commutative, check all combinations of operand order and values of
2167   // BiggerPattern, i.e.
2168   //     Opd0, Opd1, BiggerPattern=false
2169   //     Opd1, Opd0, BiggerPattern=false
2170   //     Opd0, Opd1, BiggerPattern=true
2171   //     Opd1, Opd0, BiggerPattern=true
2172   // Several of these combinations may match, so check with BiggerPattern=false
2173   // first since that will produce better results by matching more instructions
2174   // and/or inserting fewer extra instructions.
2175   for (int I = 0; I < 4; ++I) {
2176 
2177     SDValue Dst, Src;
2178     unsigned ImmR, ImmS;
2179     bool BiggerPattern = I / 2;
2180     SDValue OrOpd0Val = N->getOperand(I % 2);
2181     SDNode *OrOpd0 = OrOpd0Val.getNode();
2182     SDValue OrOpd1Val = N->getOperand((I + 1) % 2);
2183     SDNode *OrOpd1 = OrOpd1Val.getNode();
2184 
2185     unsigned BFXOpc;
2186     int DstLSB, Width;
2187     if (isBitfieldExtractOp(CurDAG, OrOpd0, BFXOpc, Src, ImmR, ImmS,
2188                             NumberOfIgnoredLowBits, BiggerPattern)) {
2189       // Check that the returned opcode is compatible with the pattern,
2190       // i.e., same type and zero extended (U and not S)
2191       if ((BFXOpc != AArch64::UBFMXri && VT == MVT::i64) ||
2192           (BFXOpc != AArch64::UBFMWri && VT == MVT::i32))
2193         continue;
2194 
2195       // Compute the width of the bitfield insertion
2196       DstLSB = 0;
2197       Width = ImmS - ImmR + 1;
2198       // FIXME: This constraint is to catch bitfield insertion we may
2199       // want to widen the pattern if we want to grab general bitfied
2200       // move case
2201       if (Width <= 0)
2202         continue;
2203 
2204       // If the mask on the insertee is correct, we have a BFXIL operation. We
2205       // can share the ImmR and ImmS values from the already-computed UBFM.
2206     } else if (isBitfieldPositioningOp(CurDAG, OrOpd0Val,
2207                                        BiggerPattern,
2208                                        Src, DstLSB, Width)) {
2209       ImmR = (BitWidth - DstLSB) % BitWidth;
2210       ImmS = Width - 1;
2211     } else
2212       continue;
2213 
2214     // Check the second part of the pattern
2215     EVT VT = OrOpd1->getValueType(0);
2216     assert((VT == MVT::i32 || VT == MVT::i64) && "unexpected OR operand");
2217 
2218     // Compute the Known Zero for the candidate of the first operand.
2219     // This allows to catch more general case than just looking for
2220     // AND with imm. Indeed, simplify-demanded-bits may have removed
2221     // the AND instruction because it proves it was useless.
2222     APInt KnownZero, KnownOne;
2223     CurDAG->computeKnownBits(OrOpd1Val, KnownZero, KnownOne);
2224 
2225     // Check if there is enough room for the second operand to appear
2226     // in the first one
2227     APInt BitsToBeInserted =
2228         APInt::getBitsSet(KnownZero.getBitWidth(), DstLSB, DstLSB + Width);
2229 
2230     if ((BitsToBeInserted & ~KnownZero) != 0)
2231       continue;
2232 
2233     // Set the first operand
2234     uint64_t Imm;
2235     if (isOpcWithIntImmediate(OrOpd1, ISD::AND, Imm) &&
2236         isBitfieldDstMask(Imm, BitsToBeInserted, NumberOfIgnoredHighBits, VT))
2237       // In that case, we can eliminate the AND
2238       Dst = OrOpd1->getOperand(0);
2239     else
2240       // Maybe the AND has been removed by simplify-demanded-bits
2241       // or is useful because it discards more bits
2242       Dst = OrOpd1Val;
2243 
2244     // both parts match
2245     SDLoc DL(N);
2246     SDValue Ops[] = {Dst, Src, CurDAG->getTargetConstant(ImmR, DL, VT),
2247                      CurDAG->getTargetConstant(ImmS, DL, VT)};
2248     unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2249     CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2250     return true;
2251   }
2252 
2253   // Generate a BFXIL from 'or (and X, Mask0Imm), (and Y, Mask1Imm)' iff
2254   // Mask0Imm and ~Mask1Imm are equivalent and one of the MaskImms is a shifted
2255   // mask (e.g., 0x000ffff0).
2256   uint64_t Mask0Imm, Mask1Imm;
2257   SDValue And0 = N->getOperand(0);
2258   SDValue And1 = N->getOperand(1);
2259   if (And0.hasOneUse() && And1.hasOneUse() &&
2260       isOpcWithIntImmediate(And0.getNode(), ISD::AND, Mask0Imm) &&
2261       isOpcWithIntImmediate(And1.getNode(), ISD::AND, Mask1Imm) &&
2262       APInt(BitWidth, Mask0Imm) == ~APInt(BitWidth, Mask1Imm) &&
2263       (isShiftedMask(Mask0Imm, VT) || isShiftedMask(Mask1Imm, VT))) {
2264 
2265     // ORR is commutative, so canonicalize to the form 'or (and X, Mask0Imm),
2266     // (and Y, Mask1Imm)' where Mask1Imm is the shifted mask masking off the
2267     // bits to be inserted.
2268     if (isShiftedMask(Mask0Imm, VT)) {
2269       std::swap(And0, And1);
2270       std::swap(Mask0Imm, Mask1Imm);
2271     }
2272 
2273     SDValue Src = And1->getOperand(0);
2274     SDValue Dst = And0->getOperand(0);
2275     unsigned LSB = countTrailingZeros(Mask1Imm);
2276     int Width = BitWidth - APInt(BitWidth, Mask0Imm).countPopulation();
2277 
2278     // The BFXIL inserts the low-order bits from a source register, so right
2279     // shift the needed bits into place.
2280     SDLoc DL(N);
2281     unsigned ShiftOpc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
2282     SDNode *LSR = CurDAG->getMachineNode(
2283         ShiftOpc, DL, VT, Src, CurDAG->getTargetConstant(LSB, DL, VT),
2284         CurDAG->getTargetConstant(BitWidth - 1, DL, VT));
2285 
2286     // BFXIL is an alias of BFM, so translate to BFM operands.
2287     unsigned ImmR = (BitWidth - LSB) % BitWidth;
2288     unsigned ImmS = Width - 1;
2289 
2290     // Create the BFXIL instruction.
2291     SDValue Ops[] = {Dst, SDValue(LSR, 0),
2292                      CurDAG->getTargetConstant(ImmR, DL, VT),
2293                      CurDAG->getTargetConstant(ImmS, DL, VT)};
2294     unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2295     CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2296     return true;
2297   }
2298 
2299   return false;
2300 }
2301 
2302 bool AArch64DAGToDAGISel::tryBitfieldInsertOp(SDNode *N) {
2303   if (N->getOpcode() != ISD::OR)
2304     return false;
2305 
2306   APInt NUsefulBits;
2307   getUsefulBits(SDValue(N, 0), NUsefulBits);
2308 
2309   // If all bits are not useful, just return UNDEF.
2310   if (!NUsefulBits) {
2311     CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0));
2312     return true;
2313   }
2314 
2315   if (tryBitfieldInsertOpFromOr(N, NUsefulBits, CurDAG))
2316     return true;
2317 
2318   return tryBitfieldInsertOpFromOrAndImm(N, CurDAG);
2319 }
2320 
2321 /// SelectBitfieldInsertInZeroOp - Match a UBFIZ instruction that is the
2322 /// equivalent of a left shift by a constant amount followed by an and masking
2323 /// out a contiguous set of bits.
2324 bool AArch64DAGToDAGISel::tryBitfieldInsertInZeroOp(SDNode *N) {
2325   if (N->getOpcode() != ISD::AND)
2326     return false;
2327 
2328   EVT VT = N->getValueType(0);
2329   if (VT != MVT::i32 && VT != MVT::i64)
2330     return false;
2331 
2332   SDValue Op0;
2333   int DstLSB, Width;
2334   if (!isBitfieldPositioningOp(CurDAG, SDValue(N, 0), /*BiggerPattern=*/false,
2335                                Op0, DstLSB, Width))
2336     return false;
2337 
2338   // ImmR is the rotate right amount.
2339   unsigned ImmR = (VT.getSizeInBits() - DstLSB) % VT.getSizeInBits();
2340   // ImmS is the most significant bit of the source to be moved.
2341   unsigned ImmS = Width - 1;
2342 
2343   SDLoc DL(N);
2344   SDValue Ops[] = {Op0, CurDAG->getTargetConstant(ImmR, DL, VT),
2345                    CurDAG->getTargetConstant(ImmS, DL, VT)};
2346   unsigned Opc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
2347   CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2348   return true;
2349 }
2350 
2351 bool
2352 AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
2353                                               unsigned RegWidth) {
2354   APFloat FVal(0.0);
2355   if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
2356     FVal = CN->getValueAPF();
2357   else if (LoadSDNode *LN = dyn_cast<LoadSDNode>(N)) {
2358     // Some otherwise illegal constants are allowed in this case.
2359     if (LN->getOperand(1).getOpcode() != AArch64ISD::ADDlow ||
2360         !isa<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1)))
2361       return false;
2362 
2363     ConstantPoolSDNode *CN =
2364         dyn_cast<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1));
2365     FVal = cast<ConstantFP>(CN->getConstVal())->getValueAPF();
2366   } else
2367     return false;
2368 
2369   // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
2370   // is between 1 and 32 for a destination w-register, or 1 and 64 for an
2371   // x-register.
2372   //
2373   // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
2374   // want THIS_NODE to be 2^fbits. This is much easier to deal with using
2375   // integers.
2376   bool IsExact;
2377 
2378   // fbits is between 1 and 64 in the worst-case, which means the fmul
2379   // could have 2^64 as an actual operand. Need 65 bits of precision.
2380   APSInt IntVal(65, true);
2381   FVal.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
2382 
2383   // N.b. isPowerOf2 also checks for > 0.
2384   if (!IsExact || !IntVal.isPowerOf2()) return false;
2385   unsigned FBits = IntVal.logBase2();
2386 
2387   // Checks above should have guaranteed that we haven't lost information in
2388   // finding FBits, but it must still be in range.
2389   if (FBits == 0 || FBits > RegWidth) return false;
2390 
2391   FixedPos = CurDAG->getTargetConstant(FBits, SDLoc(N), MVT::i32);
2392   return true;
2393 }
2394 
2395 // Inspects a register string of the form o0:op1:CRn:CRm:op2 gets the fields
2396 // of the string and obtains the integer values from them and combines these
2397 // into a single value to be used in the MRS/MSR instruction.
2398 static int getIntOperandFromRegisterString(StringRef RegString) {
2399   SmallVector<StringRef, 5> Fields;
2400   RegString.split(Fields, ':');
2401 
2402   if (Fields.size() == 1)
2403     return -1;
2404 
2405   assert(Fields.size() == 5
2406             && "Invalid number of fields in read register string");
2407 
2408   SmallVector<int, 5> Ops;
2409   bool AllIntFields = true;
2410 
2411   for (StringRef Field : Fields) {
2412     unsigned IntField;
2413     AllIntFields &= !Field.getAsInteger(10, IntField);
2414     Ops.push_back(IntField);
2415   }
2416 
2417   assert(AllIntFields &&
2418           "Unexpected non-integer value in special register string.");
2419 
2420   // Need to combine the integer fields of the string into a single value
2421   // based on the bit encoding of MRS/MSR instruction.
2422   return (Ops[0] << 14) | (Ops[1] << 11) | (Ops[2] << 7) |
2423          (Ops[3] << 3) | (Ops[4]);
2424 }
2425 
2426 // Lower the read_register intrinsic to an MRS instruction node if the special
2427 // register string argument is either of the form detailed in the ALCE (the
2428 // form described in getIntOperandsFromRegsterString) or is a named register
2429 // known by the MRS SysReg mapper.
2430 bool AArch64DAGToDAGISel::tryReadRegister(SDNode *N) {
2431   const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2432   const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2433   SDLoc DL(N);
2434 
2435   int Reg = getIntOperandFromRegisterString(RegString->getString());
2436   if (Reg != -1) {
2437     ReplaceNode(N, CurDAG->getMachineNode(
2438                        AArch64::MRS, DL, N->getSimpleValueType(0), MVT::Other,
2439                        CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2440                        N->getOperand(0)));
2441     return true;
2442   }
2443 
2444   // Use the sysreg mapper to map the remaining possible strings to the
2445   // value for the register to be used for the instruction operand.
2446   auto TheReg = AArch64SysReg::lookupSysRegByName(RegString->getString());
2447   if (TheReg && TheReg->Readable &&
2448       TheReg->haveFeatures(Subtarget->getFeatureBits()))
2449     Reg = TheReg->Encoding;
2450   else
2451     Reg = AArch64SysReg::parseGenericRegister(RegString->getString());
2452 
2453   if (Reg != -1) {
2454     ReplaceNode(N, CurDAG->getMachineNode(
2455                        AArch64::MRS, DL, N->getSimpleValueType(0), MVT::Other,
2456                        CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2457                        N->getOperand(0)));
2458     return true;
2459   }
2460 
2461   return false;
2462 }
2463 
2464 // Lower the write_register intrinsic to an MSR instruction node if the special
2465 // register string argument is either of the form detailed in the ALCE (the
2466 // form described in getIntOperandsFromRegsterString) or is a named register
2467 // known by the MSR SysReg mapper.
2468 bool AArch64DAGToDAGISel::tryWriteRegister(SDNode *N) {
2469   const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2470   const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2471   SDLoc DL(N);
2472 
2473   int Reg = getIntOperandFromRegisterString(RegString->getString());
2474   if (Reg != -1) {
2475     ReplaceNode(
2476         N, CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
2477                                   CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2478                                   N->getOperand(2), N->getOperand(0)));
2479     return true;
2480   }
2481 
2482   // Check if the register was one of those allowed as the pstatefield value in
2483   // the MSR (immediate) instruction. To accept the values allowed in the
2484   // pstatefield for the MSR (immediate) instruction, we also require that an
2485   // immediate value has been provided as an argument, we know that this is
2486   // the case as it has been ensured by semantic checking.
2487   auto PMapper = AArch64PState::lookupPStateByName(RegString->getString());;
2488   if (PMapper) {
2489     assert (isa<ConstantSDNode>(N->getOperand(2))
2490               && "Expected a constant integer expression.");
2491     unsigned Reg = PMapper->Encoding;
2492     uint64_t Immed = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
2493     unsigned State;
2494     if (Reg == AArch64PState::PAN || Reg == AArch64PState::UAO) {
2495       assert(Immed < 2 && "Bad imm");
2496       State = AArch64::MSRpstateImm1;
2497     } else {
2498       assert(Immed < 16 && "Bad imm");
2499       State = AArch64::MSRpstateImm4;
2500     }
2501     ReplaceNode(N, CurDAG->getMachineNode(
2502                        State, DL, MVT::Other,
2503                        CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2504                        CurDAG->getTargetConstant(Immed, DL, MVT::i16),
2505                        N->getOperand(0)));
2506     return true;
2507   }
2508 
2509   // Use the sysreg mapper to attempt to map the remaining possible strings
2510   // to the value for the register to be used for the MSR (register)
2511   // instruction operand.
2512   auto TheReg = AArch64SysReg::lookupSysRegByName(RegString->getString());
2513   if (TheReg && TheReg->Writeable &&
2514       TheReg->haveFeatures(Subtarget->getFeatureBits()))
2515     Reg = TheReg->Encoding;
2516   else
2517     Reg = AArch64SysReg::parseGenericRegister(RegString->getString());
2518   if (Reg != -1) {
2519     ReplaceNode(N, CurDAG->getMachineNode(
2520                        AArch64::MSR, DL, MVT::Other,
2521                        CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2522                        N->getOperand(2), N->getOperand(0)));
2523     return true;
2524   }
2525 
2526   return false;
2527 }
2528 
2529 /// We've got special pseudo-instructions for these
2530 void AArch64DAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
2531   unsigned Opcode;
2532   EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
2533   if (MemTy == MVT::i8)
2534     Opcode = AArch64::CMP_SWAP_8;
2535   else if (MemTy == MVT::i16)
2536     Opcode = AArch64::CMP_SWAP_16;
2537   else if (MemTy == MVT::i32)
2538     Opcode = AArch64::CMP_SWAP_32;
2539   else if (MemTy == MVT::i64)
2540     Opcode = AArch64::CMP_SWAP_64;
2541   else
2542     llvm_unreachable("Unknown AtomicCmpSwap type");
2543 
2544   MVT RegTy = MemTy == MVT::i64 ? MVT::i64 : MVT::i32;
2545   SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
2546                    N->getOperand(0)};
2547   SDNode *CmpSwap = CurDAG->getMachineNode(
2548       Opcode, SDLoc(N),
2549       CurDAG->getVTList(RegTy, MVT::i32, MVT::Other), Ops);
2550 
2551   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2552   MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2553   cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
2554 
2555   ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
2556   ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
2557   CurDAG->RemoveDeadNode(N);
2558 }
2559 
2560 void AArch64DAGToDAGISel::Select(SDNode *Node) {
2561   // Dump information about the Node being selected
2562   DEBUG(errs() << "Selecting: ");
2563   DEBUG(Node->dump(CurDAG));
2564   DEBUG(errs() << "\n");
2565 
2566   // If we have a custom node, we already have selected!
2567   if (Node->isMachineOpcode()) {
2568     DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
2569     Node->setNodeId(-1);
2570     return;
2571   }
2572 
2573   // Few custom selection stuff.
2574   EVT VT = Node->getValueType(0);
2575 
2576   switch (Node->getOpcode()) {
2577   default:
2578     break;
2579 
2580   case ISD::ATOMIC_CMP_SWAP:
2581     SelectCMP_SWAP(Node);
2582     return;
2583 
2584   case ISD::READ_REGISTER:
2585     if (tryReadRegister(Node))
2586       return;
2587     break;
2588 
2589   case ISD::WRITE_REGISTER:
2590     if (tryWriteRegister(Node))
2591       return;
2592     break;
2593 
2594   case ISD::ADD:
2595     if (tryMLAV64LaneV128(Node))
2596       return;
2597     break;
2598 
2599   case ISD::LOAD: {
2600     // Try to select as an indexed load. Fall through to normal processing
2601     // if we can't.
2602     if (tryIndexedLoad(Node))
2603       return;
2604     break;
2605   }
2606 
2607   case ISD::SRL:
2608   case ISD::AND:
2609   case ISD::SRA:
2610   case ISD::SIGN_EXTEND_INREG:
2611     if (tryBitfieldExtractOp(Node))
2612       return;
2613     if (tryBitfieldInsertInZeroOp(Node))
2614       return;
2615     break;
2616 
2617   case ISD::SIGN_EXTEND:
2618     if (tryBitfieldExtractOpFromSExt(Node))
2619       return;
2620     break;
2621 
2622   case ISD::OR:
2623     if (tryBitfieldInsertOp(Node))
2624       return;
2625     break;
2626 
2627   case ISD::EXTRACT_VECTOR_ELT: {
2628     // Extracting lane zero is a special case where we can just use a plain
2629     // EXTRACT_SUBREG instruction, which will become FMOV. This is easier for
2630     // the rest of the compiler, especially the register allocator and copyi
2631     // propagation, to reason about, so is preferred when it's possible to
2632     // use it.
2633     ConstantSDNode *LaneNode = cast<ConstantSDNode>(Node->getOperand(1));
2634     // Bail and use the default Select() for non-zero lanes.
2635     if (LaneNode->getZExtValue() != 0)
2636       break;
2637     // If the element type is not the same as the result type, likewise
2638     // bail and use the default Select(), as there's more to do than just
2639     // a cross-class COPY. This catches extracts of i8 and i16 elements
2640     // since they will need an explicit zext.
2641     if (VT != Node->getOperand(0).getValueType().getVectorElementType())
2642       break;
2643     unsigned SubReg;
2644     switch (Node->getOperand(0)
2645                 .getValueType()
2646                 .getVectorElementType()
2647                 .getSizeInBits()) {
2648     default:
2649       llvm_unreachable("Unexpected vector element type!");
2650     case 64:
2651       SubReg = AArch64::dsub;
2652       break;
2653     case 32:
2654       SubReg = AArch64::ssub;
2655       break;
2656     case 16:
2657       SubReg = AArch64::hsub;
2658       break;
2659     case 8:
2660       llvm_unreachable("unexpected zext-requiring extract element!");
2661     }
2662     SDValue Extract = CurDAG->getTargetExtractSubreg(SubReg, SDLoc(Node), VT,
2663                                                      Node->getOperand(0));
2664     DEBUG(dbgs() << "ISEL: Custom selection!\n=> ");
2665     DEBUG(Extract->dumpr(CurDAG));
2666     DEBUG(dbgs() << "\n");
2667     ReplaceNode(Node, Extract.getNode());
2668     return;
2669   }
2670   case ISD::Constant: {
2671     // Materialize zero constants as copies from WZR/XZR.  This allows
2672     // the coalescer to propagate these into other instructions.
2673     ConstantSDNode *ConstNode = cast<ConstantSDNode>(Node);
2674     if (ConstNode->isNullValue()) {
2675       if (VT == MVT::i32) {
2676         SDValue New = CurDAG->getCopyFromReg(
2677             CurDAG->getEntryNode(), SDLoc(Node), AArch64::WZR, MVT::i32);
2678         ReplaceNode(Node, New.getNode());
2679         return;
2680       } else if (VT == MVT::i64) {
2681         SDValue New = CurDAG->getCopyFromReg(
2682             CurDAG->getEntryNode(), SDLoc(Node), AArch64::XZR, MVT::i64);
2683         ReplaceNode(Node, New.getNode());
2684         return;
2685       }
2686     }
2687     break;
2688   }
2689 
2690   case ISD::FrameIndex: {
2691     // Selects to ADDXri FI, 0 which in turn will become ADDXri SP, imm.
2692     int FI = cast<FrameIndexSDNode>(Node)->getIndex();
2693     unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
2694     const TargetLowering *TLI = getTargetLowering();
2695     SDValue TFI = CurDAG->getTargetFrameIndex(
2696         FI, TLI->getPointerTy(CurDAG->getDataLayout()));
2697     SDLoc DL(Node);
2698     SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, DL, MVT::i32),
2699                       CurDAG->getTargetConstant(Shifter, DL, MVT::i32) };
2700     CurDAG->SelectNodeTo(Node, AArch64::ADDXri, MVT::i64, Ops);
2701     return;
2702   }
2703   case ISD::INTRINSIC_W_CHAIN: {
2704     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2705     switch (IntNo) {
2706     default:
2707       break;
2708     case Intrinsic::aarch64_ldaxp:
2709     case Intrinsic::aarch64_ldxp: {
2710       unsigned Op =
2711           IntNo == Intrinsic::aarch64_ldaxp ? AArch64::LDAXPX : AArch64::LDXPX;
2712       SDValue MemAddr = Node->getOperand(2);
2713       SDLoc DL(Node);
2714       SDValue Chain = Node->getOperand(0);
2715 
2716       SDNode *Ld = CurDAG->getMachineNode(Op, DL, MVT::i64, MVT::i64,
2717                                           MVT::Other, MemAddr, Chain);
2718 
2719       // Transfer memoperands.
2720       MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2721       MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2722       cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
2723       ReplaceNode(Node, Ld);
2724       return;
2725     }
2726     case Intrinsic::aarch64_stlxp:
2727     case Intrinsic::aarch64_stxp: {
2728       unsigned Op =
2729           IntNo == Intrinsic::aarch64_stlxp ? AArch64::STLXPX : AArch64::STXPX;
2730       SDLoc DL(Node);
2731       SDValue Chain = Node->getOperand(0);
2732       SDValue ValLo = Node->getOperand(2);
2733       SDValue ValHi = Node->getOperand(3);
2734       SDValue MemAddr = Node->getOperand(4);
2735 
2736       // Place arguments in the right order.
2737       SDValue Ops[] = {ValLo, ValHi, MemAddr, Chain};
2738 
2739       SDNode *St = CurDAG->getMachineNode(Op, DL, MVT::i32, MVT::Other, Ops);
2740       // Transfer memoperands.
2741       MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2742       MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2743       cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
2744 
2745       ReplaceNode(Node, St);
2746       return;
2747     }
2748     case Intrinsic::aarch64_neon_ld1x2:
2749       if (VT == MVT::v8i8) {
2750         SelectLoad(Node, 2, AArch64::LD1Twov8b, AArch64::dsub0);
2751         return;
2752       } else if (VT == MVT::v16i8) {
2753         SelectLoad(Node, 2, AArch64::LD1Twov16b, AArch64::qsub0);
2754         return;
2755       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2756         SelectLoad(Node, 2, AArch64::LD1Twov4h, AArch64::dsub0);
2757         return;
2758       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2759         SelectLoad(Node, 2, AArch64::LD1Twov8h, AArch64::qsub0);
2760         return;
2761       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2762         SelectLoad(Node, 2, AArch64::LD1Twov2s, AArch64::dsub0);
2763         return;
2764       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2765         SelectLoad(Node, 2, AArch64::LD1Twov4s, AArch64::qsub0);
2766         return;
2767       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2768         SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2769         return;
2770       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2771         SelectLoad(Node, 2, AArch64::LD1Twov2d, AArch64::qsub0);
2772         return;
2773       }
2774       break;
2775     case Intrinsic::aarch64_neon_ld1x3:
2776       if (VT == MVT::v8i8) {
2777         SelectLoad(Node, 3, AArch64::LD1Threev8b, AArch64::dsub0);
2778         return;
2779       } else if (VT == MVT::v16i8) {
2780         SelectLoad(Node, 3, AArch64::LD1Threev16b, AArch64::qsub0);
2781         return;
2782       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2783         SelectLoad(Node, 3, AArch64::LD1Threev4h, AArch64::dsub0);
2784         return;
2785       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2786         SelectLoad(Node, 3, AArch64::LD1Threev8h, AArch64::qsub0);
2787         return;
2788       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2789         SelectLoad(Node, 3, AArch64::LD1Threev2s, AArch64::dsub0);
2790         return;
2791       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2792         SelectLoad(Node, 3, AArch64::LD1Threev4s, AArch64::qsub0);
2793         return;
2794       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2795         SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2796         return;
2797       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2798         SelectLoad(Node, 3, AArch64::LD1Threev2d, AArch64::qsub0);
2799         return;
2800       }
2801       break;
2802     case Intrinsic::aarch64_neon_ld1x4:
2803       if (VT == MVT::v8i8) {
2804         SelectLoad(Node, 4, AArch64::LD1Fourv8b, AArch64::dsub0);
2805         return;
2806       } else if (VT == MVT::v16i8) {
2807         SelectLoad(Node, 4, AArch64::LD1Fourv16b, AArch64::qsub0);
2808         return;
2809       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2810         SelectLoad(Node, 4, AArch64::LD1Fourv4h, AArch64::dsub0);
2811         return;
2812       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2813         SelectLoad(Node, 4, AArch64::LD1Fourv8h, AArch64::qsub0);
2814         return;
2815       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2816         SelectLoad(Node, 4, AArch64::LD1Fourv2s, AArch64::dsub0);
2817         return;
2818       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2819         SelectLoad(Node, 4, AArch64::LD1Fourv4s, AArch64::qsub0);
2820         return;
2821       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2822         SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2823         return;
2824       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2825         SelectLoad(Node, 4, AArch64::LD1Fourv2d, AArch64::qsub0);
2826         return;
2827       }
2828       break;
2829     case Intrinsic::aarch64_neon_ld2:
2830       if (VT == MVT::v8i8) {
2831         SelectLoad(Node, 2, AArch64::LD2Twov8b, AArch64::dsub0);
2832         return;
2833       } else if (VT == MVT::v16i8) {
2834         SelectLoad(Node, 2, AArch64::LD2Twov16b, AArch64::qsub0);
2835         return;
2836       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2837         SelectLoad(Node, 2, AArch64::LD2Twov4h, AArch64::dsub0);
2838         return;
2839       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2840         SelectLoad(Node, 2, AArch64::LD2Twov8h, AArch64::qsub0);
2841         return;
2842       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2843         SelectLoad(Node, 2, AArch64::LD2Twov2s, AArch64::dsub0);
2844         return;
2845       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2846         SelectLoad(Node, 2, AArch64::LD2Twov4s, AArch64::qsub0);
2847         return;
2848       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2849         SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2850         return;
2851       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2852         SelectLoad(Node, 2, AArch64::LD2Twov2d, AArch64::qsub0);
2853         return;
2854       }
2855       break;
2856     case Intrinsic::aarch64_neon_ld3:
2857       if (VT == MVT::v8i8) {
2858         SelectLoad(Node, 3, AArch64::LD3Threev8b, AArch64::dsub0);
2859         return;
2860       } else if (VT == MVT::v16i8) {
2861         SelectLoad(Node, 3, AArch64::LD3Threev16b, AArch64::qsub0);
2862         return;
2863       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2864         SelectLoad(Node, 3, AArch64::LD3Threev4h, AArch64::dsub0);
2865         return;
2866       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2867         SelectLoad(Node, 3, AArch64::LD3Threev8h, AArch64::qsub0);
2868         return;
2869       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2870         SelectLoad(Node, 3, AArch64::LD3Threev2s, AArch64::dsub0);
2871         return;
2872       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2873         SelectLoad(Node, 3, AArch64::LD3Threev4s, AArch64::qsub0);
2874         return;
2875       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2876         SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2877         return;
2878       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2879         SelectLoad(Node, 3, AArch64::LD3Threev2d, AArch64::qsub0);
2880         return;
2881       }
2882       break;
2883     case Intrinsic::aarch64_neon_ld4:
2884       if (VT == MVT::v8i8) {
2885         SelectLoad(Node, 4, AArch64::LD4Fourv8b, AArch64::dsub0);
2886         return;
2887       } else if (VT == MVT::v16i8) {
2888         SelectLoad(Node, 4, AArch64::LD4Fourv16b, AArch64::qsub0);
2889         return;
2890       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2891         SelectLoad(Node, 4, AArch64::LD4Fourv4h, AArch64::dsub0);
2892         return;
2893       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2894         SelectLoad(Node, 4, AArch64::LD4Fourv8h, AArch64::qsub0);
2895         return;
2896       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2897         SelectLoad(Node, 4, AArch64::LD4Fourv2s, AArch64::dsub0);
2898         return;
2899       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2900         SelectLoad(Node, 4, AArch64::LD4Fourv4s, AArch64::qsub0);
2901         return;
2902       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2903         SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2904         return;
2905       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2906         SelectLoad(Node, 4, AArch64::LD4Fourv2d, AArch64::qsub0);
2907         return;
2908       }
2909       break;
2910     case Intrinsic::aarch64_neon_ld2r:
2911       if (VT == MVT::v8i8) {
2912         SelectLoad(Node, 2, AArch64::LD2Rv8b, AArch64::dsub0);
2913         return;
2914       } else if (VT == MVT::v16i8) {
2915         SelectLoad(Node, 2, AArch64::LD2Rv16b, AArch64::qsub0);
2916         return;
2917       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2918         SelectLoad(Node, 2, AArch64::LD2Rv4h, AArch64::dsub0);
2919         return;
2920       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2921         SelectLoad(Node, 2, AArch64::LD2Rv8h, AArch64::qsub0);
2922         return;
2923       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2924         SelectLoad(Node, 2, AArch64::LD2Rv2s, AArch64::dsub0);
2925         return;
2926       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2927         SelectLoad(Node, 2, AArch64::LD2Rv4s, AArch64::qsub0);
2928         return;
2929       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2930         SelectLoad(Node, 2, AArch64::LD2Rv1d, AArch64::dsub0);
2931         return;
2932       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2933         SelectLoad(Node, 2, AArch64::LD2Rv2d, AArch64::qsub0);
2934         return;
2935       }
2936       break;
2937     case Intrinsic::aarch64_neon_ld3r:
2938       if (VT == MVT::v8i8) {
2939         SelectLoad(Node, 3, AArch64::LD3Rv8b, AArch64::dsub0);
2940         return;
2941       } else if (VT == MVT::v16i8) {
2942         SelectLoad(Node, 3, AArch64::LD3Rv16b, AArch64::qsub0);
2943         return;
2944       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2945         SelectLoad(Node, 3, AArch64::LD3Rv4h, AArch64::dsub0);
2946         return;
2947       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2948         SelectLoad(Node, 3, AArch64::LD3Rv8h, AArch64::qsub0);
2949         return;
2950       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2951         SelectLoad(Node, 3, AArch64::LD3Rv2s, AArch64::dsub0);
2952         return;
2953       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2954         SelectLoad(Node, 3, AArch64::LD3Rv4s, AArch64::qsub0);
2955         return;
2956       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2957         SelectLoad(Node, 3, AArch64::LD3Rv1d, AArch64::dsub0);
2958         return;
2959       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2960         SelectLoad(Node, 3, AArch64::LD3Rv2d, AArch64::qsub0);
2961         return;
2962       }
2963       break;
2964     case Intrinsic::aarch64_neon_ld4r:
2965       if (VT == MVT::v8i8) {
2966         SelectLoad(Node, 4, AArch64::LD4Rv8b, AArch64::dsub0);
2967         return;
2968       } else if (VT == MVT::v16i8) {
2969         SelectLoad(Node, 4, AArch64::LD4Rv16b, AArch64::qsub0);
2970         return;
2971       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2972         SelectLoad(Node, 4, AArch64::LD4Rv4h, AArch64::dsub0);
2973         return;
2974       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2975         SelectLoad(Node, 4, AArch64::LD4Rv8h, AArch64::qsub0);
2976         return;
2977       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2978         SelectLoad(Node, 4, AArch64::LD4Rv2s, AArch64::dsub0);
2979         return;
2980       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2981         SelectLoad(Node, 4, AArch64::LD4Rv4s, AArch64::qsub0);
2982         return;
2983       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2984         SelectLoad(Node, 4, AArch64::LD4Rv1d, AArch64::dsub0);
2985         return;
2986       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2987         SelectLoad(Node, 4, AArch64::LD4Rv2d, AArch64::qsub0);
2988         return;
2989       }
2990       break;
2991     case Intrinsic::aarch64_neon_ld2lane:
2992       if (VT == MVT::v16i8 || VT == MVT::v8i8) {
2993         SelectLoadLane(Node, 2, AArch64::LD2i8);
2994         return;
2995       } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2996                  VT == MVT::v8f16) {
2997         SelectLoadLane(Node, 2, AArch64::LD2i16);
2998         return;
2999       } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3000                  VT == MVT::v2f32) {
3001         SelectLoadLane(Node, 2, AArch64::LD2i32);
3002         return;
3003       } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3004                  VT == MVT::v1f64) {
3005         SelectLoadLane(Node, 2, AArch64::LD2i64);
3006         return;
3007       }
3008       break;
3009     case Intrinsic::aarch64_neon_ld3lane:
3010       if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3011         SelectLoadLane(Node, 3, AArch64::LD3i8);
3012         return;
3013       } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3014                  VT == MVT::v8f16) {
3015         SelectLoadLane(Node, 3, AArch64::LD3i16);
3016         return;
3017       } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3018                  VT == MVT::v2f32) {
3019         SelectLoadLane(Node, 3, AArch64::LD3i32);
3020         return;
3021       } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3022                  VT == MVT::v1f64) {
3023         SelectLoadLane(Node, 3, AArch64::LD3i64);
3024         return;
3025       }
3026       break;
3027     case Intrinsic::aarch64_neon_ld4lane:
3028       if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3029         SelectLoadLane(Node, 4, AArch64::LD4i8);
3030         return;
3031       } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3032                  VT == MVT::v8f16) {
3033         SelectLoadLane(Node, 4, AArch64::LD4i16);
3034         return;
3035       } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3036                  VT == MVT::v2f32) {
3037         SelectLoadLane(Node, 4, AArch64::LD4i32);
3038         return;
3039       } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3040                  VT == MVT::v1f64) {
3041         SelectLoadLane(Node, 4, AArch64::LD4i64);
3042         return;
3043       }
3044       break;
3045     }
3046   } break;
3047   case ISD::INTRINSIC_WO_CHAIN: {
3048     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
3049     switch (IntNo) {
3050     default:
3051       break;
3052     case Intrinsic::aarch64_neon_tbl2:
3053       SelectTable(Node, 2,
3054                   VT == MVT::v8i8 ? AArch64::TBLv8i8Two : AArch64::TBLv16i8Two,
3055                   false);
3056       return;
3057     case Intrinsic::aarch64_neon_tbl3:
3058       SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBLv8i8Three
3059                                            : AArch64::TBLv16i8Three,
3060                   false);
3061       return;
3062     case Intrinsic::aarch64_neon_tbl4:
3063       SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBLv8i8Four
3064                                            : AArch64::TBLv16i8Four,
3065                   false);
3066       return;
3067     case Intrinsic::aarch64_neon_tbx2:
3068       SelectTable(Node, 2,
3069                   VT == MVT::v8i8 ? AArch64::TBXv8i8Two : AArch64::TBXv16i8Two,
3070                   true);
3071       return;
3072     case Intrinsic::aarch64_neon_tbx3:
3073       SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBXv8i8Three
3074                                            : AArch64::TBXv16i8Three,
3075                   true);
3076       return;
3077     case Intrinsic::aarch64_neon_tbx4:
3078       SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBXv8i8Four
3079                                            : AArch64::TBXv16i8Four,
3080                   true);
3081       return;
3082     case Intrinsic::aarch64_neon_smull:
3083     case Intrinsic::aarch64_neon_umull:
3084       if (tryMULLV64LaneV128(IntNo, Node))
3085         return;
3086       break;
3087     }
3088     break;
3089   }
3090   case ISD::INTRINSIC_VOID: {
3091     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
3092     if (Node->getNumOperands() >= 3)
3093       VT = Node->getOperand(2)->getValueType(0);
3094     switch (IntNo) {
3095     default:
3096       break;
3097     case Intrinsic::aarch64_neon_st1x2: {
3098       if (VT == MVT::v8i8) {
3099         SelectStore(Node, 2, AArch64::ST1Twov8b);
3100         return;
3101       } else if (VT == MVT::v16i8) {
3102         SelectStore(Node, 2, AArch64::ST1Twov16b);
3103         return;
3104       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3105         SelectStore(Node, 2, AArch64::ST1Twov4h);
3106         return;
3107       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3108         SelectStore(Node, 2, AArch64::ST1Twov8h);
3109         return;
3110       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3111         SelectStore(Node, 2, AArch64::ST1Twov2s);
3112         return;
3113       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3114         SelectStore(Node, 2, AArch64::ST1Twov4s);
3115         return;
3116       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3117         SelectStore(Node, 2, AArch64::ST1Twov2d);
3118         return;
3119       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3120         SelectStore(Node, 2, AArch64::ST1Twov1d);
3121         return;
3122       }
3123       break;
3124     }
3125     case Intrinsic::aarch64_neon_st1x3: {
3126       if (VT == MVT::v8i8) {
3127         SelectStore(Node, 3, AArch64::ST1Threev8b);
3128         return;
3129       } else if (VT == MVT::v16i8) {
3130         SelectStore(Node, 3, AArch64::ST1Threev16b);
3131         return;
3132       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3133         SelectStore(Node, 3, AArch64::ST1Threev4h);
3134         return;
3135       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3136         SelectStore(Node, 3, AArch64::ST1Threev8h);
3137         return;
3138       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3139         SelectStore(Node, 3, AArch64::ST1Threev2s);
3140         return;
3141       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3142         SelectStore(Node, 3, AArch64::ST1Threev4s);
3143         return;
3144       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3145         SelectStore(Node, 3, AArch64::ST1Threev2d);
3146         return;
3147       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3148         SelectStore(Node, 3, AArch64::ST1Threev1d);
3149         return;
3150       }
3151       break;
3152     }
3153     case Intrinsic::aarch64_neon_st1x4: {
3154       if (VT == MVT::v8i8) {
3155         SelectStore(Node, 4, AArch64::ST1Fourv8b);
3156         return;
3157       } else if (VT == MVT::v16i8) {
3158         SelectStore(Node, 4, AArch64::ST1Fourv16b);
3159         return;
3160       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3161         SelectStore(Node, 4, AArch64::ST1Fourv4h);
3162         return;
3163       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3164         SelectStore(Node, 4, AArch64::ST1Fourv8h);
3165         return;
3166       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3167         SelectStore(Node, 4, AArch64::ST1Fourv2s);
3168         return;
3169       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3170         SelectStore(Node, 4, AArch64::ST1Fourv4s);
3171         return;
3172       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3173         SelectStore(Node, 4, AArch64::ST1Fourv2d);
3174         return;
3175       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3176         SelectStore(Node, 4, AArch64::ST1Fourv1d);
3177         return;
3178       }
3179       break;
3180     }
3181     case Intrinsic::aarch64_neon_st2: {
3182       if (VT == MVT::v8i8) {
3183         SelectStore(Node, 2, AArch64::ST2Twov8b);
3184         return;
3185       } else if (VT == MVT::v16i8) {
3186         SelectStore(Node, 2, AArch64::ST2Twov16b);
3187         return;
3188       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3189         SelectStore(Node, 2, AArch64::ST2Twov4h);
3190         return;
3191       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3192         SelectStore(Node, 2, AArch64::ST2Twov8h);
3193         return;
3194       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3195         SelectStore(Node, 2, AArch64::ST2Twov2s);
3196         return;
3197       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3198         SelectStore(Node, 2, AArch64::ST2Twov4s);
3199         return;
3200       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3201         SelectStore(Node, 2, AArch64::ST2Twov2d);
3202         return;
3203       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3204         SelectStore(Node, 2, AArch64::ST1Twov1d);
3205         return;
3206       }
3207       break;
3208     }
3209     case Intrinsic::aarch64_neon_st3: {
3210       if (VT == MVT::v8i8) {
3211         SelectStore(Node, 3, AArch64::ST3Threev8b);
3212         return;
3213       } else if (VT == MVT::v16i8) {
3214         SelectStore(Node, 3, AArch64::ST3Threev16b);
3215         return;
3216       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3217         SelectStore(Node, 3, AArch64::ST3Threev4h);
3218         return;
3219       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3220         SelectStore(Node, 3, AArch64::ST3Threev8h);
3221         return;
3222       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3223         SelectStore(Node, 3, AArch64::ST3Threev2s);
3224         return;
3225       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3226         SelectStore(Node, 3, AArch64::ST3Threev4s);
3227         return;
3228       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3229         SelectStore(Node, 3, AArch64::ST3Threev2d);
3230         return;
3231       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3232         SelectStore(Node, 3, AArch64::ST1Threev1d);
3233         return;
3234       }
3235       break;
3236     }
3237     case Intrinsic::aarch64_neon_st4: {
3238       if (VT == MVT::v8i8) {
3239         SelectStore(Node, 4, AArch64::ST4Fourv8b);
3240         return;
3241       } else if (VT == MVT::v16i8) {
3242         SelectStore(Node, 4, AArch64::ST4Fourv16b);
3243         return;
3244       } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3245         SelectStore(Node, 4, AArch64::ST4Fourv4h);
3246         return;
3247       } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3248         SelectStore(Node, 4, AArch64::ST4Fourv8h);
3249         return;
3250       } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3251         SelectStore(Node, 4, AArch64::ST4Fourv2s);
3252         return;
3253       } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3254         SelectStore(Node, 4, AArch64::ST4Fourv4s);
3255         return;
3256       } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3257         SelectStore(Node, 4, AArch64::ST4Fourv2d);
3258         return;
3259       } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3260         SelectStore(Node, 4, AArch64::ST1Fourv1d);
3261         return;
3262       }
3263       break;
3264     }
3265     case Intrinsic::aarch64_neon_st2lane: {
3266       if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3267         SelectStoreLane(Node, 2, AArch64::ST2i8);
3268         return;
3269       } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3270                  VT == MVT::v8f16) {
3271         SelectStoreLane(Node, 2, AArch64::ST2i16);
3272         return;
3273       } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3274                  VT == MVT::v2f32) {
3275         SelectStoreLane(Node, 2, AArch64::ST2i32);
3276         return;
3277       } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3278                  VT == MVT::v1f64) {
3279         SelectStoreLane(Node, 2, AArch64::ST2i64);
3280         return;
3281       }
3282       break;
3283     }
3284     case Intrinsic::aarch64_neon_st3lane: {
3285       if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3286         SelectStoreLane(Node, 3, AArch64::ST3i8);
3287         return;
3288       } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3289                  VT == MVT::v8f16) {
3290         SelectStoreLane(Node, 3, AArch64::ST3i16);
3291         return;
3292       } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3293                  VT == MVT::v2f32) {
3294         SelectStoreLane(Node, 3, AArch64::ST3i32);
3295         return;
3296       } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3297                  VT == MVT::v1f64) {
3298         SelectStoreLane(Node, 3, AArch64::ST3i64);
3299         return;
3300       }
3301       break;
3302     }
3303     case Intrinsic::aarch64_neon_st4lane: {
3304       if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3305         SelectStoreLane(Node, 4, AArch64::ST4i8);
3306         return;
3307       } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3308                  VT == MVT::v8f16) {
3309         SelectStoreLane(Node, 4, AArch64::ST4i16);
3310         return;
3311       } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3312                  VT == MVT::v2f32) {
3313         SelectStoreLane(Node, 4, AArch64::ST4i32);
3314         return;
3315       } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3316                  VT == MVT::v1f64) {
3317         SelectStoreLane(Node, 4, AArch64::ST4i64);
3318         return;
3319       }
3320       break;
3321     }
3322     }
3323     break;
3324   }
3325   case AArch64ISD::LD2post: {
3326     if (VT == MVT::v8i8) {
3327       SelectPostLoad(Node, 2, AArch64::LD2Twov8b_POST, AArch64::dsub0);
3328       return;
3329     } else if (VT == MVT::v16i8) {
3330       SelectPostLoad(Node, 2, AArch64::LD2Twov16b_POST, AArch64::qsub0);
3331       return;
3332     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3333       SelectPostLoad(Node, 2, AArch64::LD2Twov4h_POST, AArch64::dsub0);
3334       return;
3335     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3336       SelectPostLoad(Node, 2, AArch64::LD2Twov8h_POST, AArch64::qsub0);
3337       return;
3338     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3339       SelectPostLoad(Node, 2, AArch64::LD2Twov2s_POST, AArch64::dsub0);
3340       return;
3341     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3342       SelectPostLoad(Node, 2, AArch64::LD2Twov4s_POST, AArch64::qsub0);
3343       return;
3344     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3345       SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
3346       return;
3347     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3348       SelectPostLoad(Node, 2, AArch64::LD2Twov2d_POST, AArch64::qsub0);
3349       return;
3350     }
3351     break;
3352   }
3353   case AArch64ISD::LD3post: {
3354     if (VT == MVT::v8i8) {
3355       SelectPostLoad(Node, 3, AArch64::LD3Threev8b_POST, AArch64::dsub0);
3356       return;
3357     } else if (VT == MVT::v16i8) {
3358       SelectPostLoad(Node, 3, AArch64::LD3Threev16b_POST, AArch64::qsub0);
3359       return;
3360     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3361       SelectPostLoad(Node, 3, AArch64::LD3Threev4h_POST, AArch64::dsub0);
3362       return;
3363     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3364       SelectPostLoad(Node, 3, AArch64::LD3Threev8h_POST, AArch64::qsub0);
3365       return;
3366     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3367       SelectPostLoad(Node, 3, AArch64::LD3Threev2s_POST, AArch64::dsub0);
3368       return;
3369     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3370       SelectPostLoad(Node, 3, AArch64::LD3Threev4s_POST, AArch64::qsub0);
3371       return;
3372     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3373       SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3374       return;
3375     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3376       SelectPostLoad(Node, 3, AArch64::LD3Threev2d_POST, AArch64::qsub0);
3377       return;
3378     }
3379     break;
3380   }
3381   case AArch64ISD::LD4post: {
3382     if (VT == MVT::v8i8) {
3383       SelectPostLoad(Node, 4, AArch64::LD4Fourv8b_POST, AArch64::dsub0);
3384       return;
3385     } else if (VT == MVT::v16i8) {
3386       SelectPostLoad(Node, 4, AArch64::LD4Fourv16b_POST, AArch64::qsub0);
3387       return;
3388     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3389       SelectPostLoad(Node, 4, AArch64::LD4Fourv4h_POST, AArch64::dsub0);
3390       return;
3391     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3392       SelectPostLoad(Node, 4, AArch64::LD4Fourv8h_POST, AArch64::qsub0);
3393       return;
3394     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3395       SelectPostLoad(Node, 4, AArch64::LD4Fourv2s_POST, AArch64::dsub0);
3396       return;
3397     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3398       SelectPostLoad(Node, 4, AArch64::LD4Fourv4s_POST, AArch64::qsub0);
3399       return;
3400     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3401       SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3402       return;
3403     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3404       SelectPostLoad(Node, 4, AArch64::LD4Fourv2d_POST, AArch64::qsub0);
3405       return;
3406     }
3407     break;
3408   }
3409   case AArch64ISD::LD1x2post: {
3410     if (VT == MVT::v8i8) {
3411       SelectPostLoad(Node, 2, AArch64::LD1Twov8b_POST, AArch64::dsub0);
3412       return;
3413     } else if (VT == MVT::v16i8) {
3414       SelectPostLoad(Node, 2, AArch64::LD1Twov16b_POST, AArch64::qsub0);
3415       return;
3416     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3417       SelectPostLoad(Node, 2, AArch64::LD1Twov4h_POST, AArch64::dsub0);
3418       return;
3419     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3420       SelectPostLoad(Node, 2, AArch64::LD1Twov8h_POST, AArch64::qsub0);
3421       return;
3422     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3423       SelectPostLoad(Node, 2, AArch64::LD1Twov2s_POST, AArch64::dsub0);
3424       return;
3425     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3426       SelectPostLoad(Node, 2, AArch64::LD1Twov4s_POST, AArch64::qsub0);
3427       return;
3428     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3429       SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
3430       return;
3431     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3432       SelectPostLoad(Node, 2, AArch64::LD1Twov2d_POST, AArch64::qsub0);
3433       return;
3434     }
3435     break;
3436   }
3437   case AArch64ISD::LD1x3post: {
3438     if (VT == MVT::v8i8) {
3439       SelectPostLoad(Node, 3, AArch64::LD1Threev8b_POST, AArch64::dsub0);
3440       return;
3441     } else if (VT == MVT::v16i8) {
3442       SelectPostLoad(Node, 3, AArch64::LD1Threev16b_POST, AArch64::qsub0);
3443       return;
3444     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3445       SelectPostLoad(Node, 3, AArch64::LD1Threev4h_POST, AArch64::dsub0);
3446       return;
3447     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3448       SelectPostLoad(Node, 3, AArch64::LD1Threev8h_POST, AArch64::qsub0);
3449       return;
3450     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3451       SelectPostLoad(Node, 3, AArch64::LD1Threev2s_POST, AArch64::dsub0);
3452       return;
3453     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3454       SelectPostLoad(Node, 3, AArch64::LD1Threev4s_POST, AArch64::qsub0);
3455       return;
3456     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3457       SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3458       return;
3459     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3460       SelectPostLoad(Node, 3, AArch64::LD1Threev2d_POST, AArch64::qsub0);
3461       return;
3462     }
3463     break;
3464   }
3465   case AArch64ISD::LD1x4post: {
3466     if (VT == MVT::v8i8) {
3467       SelectPostLoad(Node, 4, AArch64::LD1Fourv8b_POST, AArch64::dsub0);
3468       return;
3469     } else if (VT == MVT::v16i8) {
3470       SelectPostLoad(Node, 4, AArch64::LD1Fourv16b_POST, AArch64::qsub0);
3471       return;
3472     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3473       SelectPostLoad(Node, 4, AArch64::LD1Fourv4h_POST, AArch64::dsub0);
3474       return;
3475     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3476       SelectPostLoad(Node, 4, AArch64::LD1Fourv8h_POST, AArch64::qsub0);
3477       return;
3478     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3479       SelectPostLoad(Node, 4, AArch64::LD1Fourv2s_POST, AArch64::dsub0);
3480       return;
3481     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3482       SelectPostLoad(Node, 4, AArch64::LD1Fourv4s_POST, AArch64::qsub0);
3483       return;
3484     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3485       SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3486       return;
3487     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3488       SelectPostLoad(Node, 4, AArch64::LD1Fourv2d_POST, AArch64::qsub0);
3489       return;
3490     }
3491     break;
3492   }
3493   case AArch64ISD::LD1DUPpost: {
3494     if (VT == MVT::v8i8) {
3495       SelectPostLoad(Node, 1, AArch64::LD1Rv8b_POST, AArch64::dsub0);
3496       return;
3497     } else if (VT == MVT::v16i8) {
3498       SelectPostLoad(Node, 1, AArch64::LD1Rv16b_POST, AArch64::qsub0);
3499       return;
3500     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3501       SelectPostLoad(Node, 1, AArch64::LD1Rv4h_POST, AArch64::dsub0);
3502       return;
3503     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3504       SelectPostLoad(Node, 1, AArch64::LD1Rv8h_POST, AArch64::qsub0);
3505       return;
3506     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3507       SelectPostLoad(Node, 1, AArch64::LD1Rv2s_POST, AArch64::dsub0);
3508       return;
3509     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3510       SelectPostLoad(Node, 1, AArch64::LD1Rv4s_POST, AArch64::qsub0);
3511       return;
3512     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3513       SelectPostLoad(Node, 1, AArch64::LD1Rv1d_POST, AArch64::dsub0);
3514       return;
3515     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3516       SelectPostLoad(Node, 1, AArch64::LD1Rv2d_POST, AArch64::qsub0);
3517       return;
3518     }
3519     break;
3520   }
3521   case AArch64ISD::LD2DUPpost: {
3522     if (VT == MVT::v8i8) {
3523       SelectPostLoad(Node, 2, AArch64::LD2Rv8b_POST, AArch64::dsub0);
3524       return;
3525     } else if (VT == MVT::v16i8) {
3526       SelectPostLoad(Node, 2, AArch64::LD2Rv16b_POST, AArch64::qsub0);
3527       return;
3528     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3529       SelectPostLoad(Node, 2, AArch64::LD2Rv4h_POST, AArch64::dsub0);
3530       return;
3531     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3532       SelectPostLoad(Node, 2, AArch64::LD2Rv8h_POST, AArch64::qsub0);
3533       return;
3534     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3535       SelectPostLoad(Node, 2, AArch64::LD2Rv2s_POST, AArch64::dsub0);
3536       return;
3537     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3538       SelectPostLoad(Node, 2, AArch64::LD2Rv4s_POST, AArch64::qsub0);
3539       return;
3540     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3541       SelectPostLoad(Node, 2, AArch64::LD2Rv1d_POST, AArch64::dsub0);
3542       return;
3543     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3544       SelectPostLoad(Node, 2, AArch64::LD2Rv2d_POST, AArch64::qsub0);
3545       return;
3546     }
3547     break;
3548   }
3549   case AArch64ISD::LD3DUPpost: {
3550     if (VT == MVT::v8i8) {
3551       SelectPostLoad(Node, 3, AArch64::LD3Rv8b_POST, AArch64::dsub0);
3552       return;
3553     } else if (VT == MVT::v16i8) {
3554       SelectPostLoad(Node, 3, AArch64::LD3Rv16b_POST, AArch64::qsub0);
3555       return;
3556     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3557       SelectPostLoad(Node, 3, AArch64::LD3Rv4h_POST, AArch64::dsub0);
3558       return;
3559     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3560       SelectPostLoad(Node, 3, AArch64::LD3Rv8h_POST, AArch64::qsub0);
3561       return;
3562     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3563       SelectPostLoad(Node, 3, AArch64::LD3Rv2s_POST, AArch64::dsub0);
3564       return;
3565     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3566       SelectPostLoad(Node, 3, AArch64::LD3Rv4s_POST, AArch64::qsub0);
3567       return;
3568     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3569       SelectPostLoad(Node, 3, AArch64::LD3Rv1d_POST, AArch64::dsub0);
3570       return;
3571     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3572       SelectPostLoad(Node, 3, AArch64::LD3Rv2d_POST, AArch64::qsub0);
3573       return;
3574     }
3575     break;
3576   }
3577   case AArch64ISD::LD4DUPpost: {
3578     if (VT == MVT::v8i8) {
3579       SelectPostLoad(Node, 4, AArch64::LD4Rv8b_POST, AArch64::dsub0);
3580       return;
3581     } else if (VT == MVT::v16i8) {
3582       SelectPostLoad(Node, 4, AArch64::LD4Rv16b_POST, AArch64::qsub0);
3583       return;
3584     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3585       SelectPostLoad(Node, 4, AArch64::LD4Rv4h_POST, AArch64::dsub0);
3586       return;
3587     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3588       SelectPostLoad(Node, 4, AArch64::LD4Rv8h_POST, AArch64::qsub0);
3589       return;
3590     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3591       SelectPostLoad(Node, 4, AArch64::LD4Rv2s_POST, AArch64::dsub0);
3592       return;
3593     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3594       SelectPostLoad(Node, 4, AArch64::LD4Rv4s_POST, AArch64::qsub0);
3595       return;
3596     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3597       SelectPostLoad(Node, 4, AArch64::LD4Rv1d_POST, AArch64::dsub0);
3598       return;
3599     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3600       SelectPostLoad(Node, 4, AArch64::LD4Rv2d_POST, AArch64::qsub0);
3601       return;
3602     }
3603     break;
3604   }
3605   case AArch64ISD::LD1LANEpost: {
3606     if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3607       SelectPostLoadLane(Node, 1, AArch64::LD1i8_POST);
3608       return;
3609     } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3610                VT == MVT::v8f16) {
3611       SelectPostLoadLane(Node, 1, AArch64::LD1i16_POST);
3612       return;
3613     } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3614                VT == MVT::v2f32) {
3615       SelectPostLoadLane(Node, 1, AArch64::LD1i32_POST);
3616       return;
3617     } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3618                VT == MVT::v1f64) {
3619       SelectPostLoadLane(Node, 1, AArch64::LD1i64_POST);
3620       return;
3621     }
3622     break;
3623   }
3624   case AArch64ISD::LD2LANEpost: {
3625     if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3626       SelectPostLoadLane(Node, 2, AArch64::LD2i8_POST);
3627       return;
3628     } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3629                VT == MVT::v8f16) {
3630       SelectPostLoadLane(Node, 2, AArch64::LD2i16_POST);
3631       return;
3632     } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3633                VT == MVT::v2f32) {
3634       SelectPostLoadLane(Node, 2, AArch64::LD2i32_POST);
3635       return;
3636     } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3637                VT == MVT::v1f64) {
3638       SelectPostLoadLane(Node, 2, AArch64::LD2i64_POST);
3639       return;
3640     }
3641     break;
3642   }
3643   case AArch64ISD::LD3LANEpost: {
3644     if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3645       SelectPostLoadLane(Node, 3, AArch64::LD3i8_POST);
3646       return;
3647     } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3648                VT == MVT::v8f16) {
3649       SelectPostLoadLane(Node, 3, AArch64::LD3i16_POST);
3650       return;
3651     } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3652                VT == MVT::v2f32) {
3653       SelectPostLoadLane(Node, 3, AArch64::LD3i32_POST);
3654       return;
3655     } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3656                VT == MVT::v1f64) {
3657       SelectPostLoadLane(Node, 3, AArch64::LD3i64_POST);
3658       return;
3659     }
3660     break;
3661   }
3662   case AArch64ISD::LD4LANEpost: {
3663     if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3664       SelectPostLoadLane(Node, 4, AArch64::LD4i8_POST);
3665       return;
3666     } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3667                VT == MVT::v8f16) {
3668       SelectPostLoadLane(Node, 4, AArch64::LD4i16_POST);
3669       return;
3670     } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3671                VT == MVT::v2f32) {
3672       SelectPostLoadLane(Node, 4, AArch64::LD4i32_POST);
3673       return;
3674     } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3675                VT == MVT::v1f64) {
3676       SelectPostLoadLane(Node, 4, AArch64::LD4i64_POST);
3677       return;
3678     }
3679     break;
3680   }
3681   case AArch64ISD::ST2post: {
3682     VT = Node->getOperand(1).getValueType();
3683     if (VT == MVT::v8i8) {
3684       SelectPostStore(Node, 2, AArch64::ST2Twov8b_POST);
3685       return;
3686     } else if (VT == MVT::v16i8) {
3687       SelectPostStore(Node, 2, AArch64::ST2Twov16b_POST);
3688       return;
3689     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3690       SelectPostStore(Node, 2, AArch64::ST2Twov4h_POST);
3691       return;
3692     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3693       SelectPostStore(Node, 2, AArch64::ST2Twov8h_POST);
3694       return;
3695     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3696       SelectPostStore(Node, 2, AArch64::ST2Twov2s_POST);
3697       return;
3698     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3699       SelectPostStore(Node, 2, AArch64::ST2Twov4s_POST);
3700       return;
3701     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3702       SelectPostStore(Node, 2, AArch64::ST2Twov2d_POST);
3703       return;
3704     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3705       SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3706       return;
3707     }
3708     break;
3709   }
3710   case AArch64ISD::ST3post: {
3711     VT = Node->getOperand(1).getValueType();
3712     if (VT == MVT::v8i8) {
3713       SelectPostStore(Node, 3, AArch64::ST3Threev8b_POST);
3714       return;
3715     } else if (VT == MVT::v16i8) {
3716       SelectPostStore(Node, 3, AArch64::ST3Threev16b_POST);
3717       return;
3718     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3719       SelectPostStore(Node, 3, AArch64::ST3Threev4h_POST);
3720       return;
3721     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3722       SelectPostStore(Node, 3, AArch64::ST3Threev8h_POST);
3723       return;
3724     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3725       SelectPostStore(Node, 3, AArch64::ST3Threev2s_POST);
3726       return;
3727     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3728       SelectPostStore(Node, 3, AArch64::ST3Threev4s_POST);
3729       return;
3730     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3731       SelectPostStore(Node, 3, AArch64::ST3Threev2d_POST);
3732       return;
3733     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3734       SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3735       return;
3736     }
3737     break;
3738   }
3739   case AArch64ISD::ST4post: {
3740     VT = Node->getOperand(1).getValueType();
3741     if (VT == MVT::v8i8) {
3742       SelectPostStore(Node, 4, AArch64::ST4Fourv8b_POST);
3743       return;
3744     } else if (VT == MVT::v16i8) {
3745       SelectPostStore(Node, 4, AArch64::ST4Fourv16b_POST);
3746       return;
3747     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3748       SelectPostStore(Node, 4, AArch64::ST4Fourv4h_POST);
3749       return;
3750     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3751       SelectPostStore(Node, 4, AArch64::ST4Fourv8h_POST);
3752       return;
3753     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3754       SelectPostStore(Node, 4, AArch64::ST4Fourv2s_POST);
3755       return;
3756     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3757       SelectPostStore(Node, 4, AArch64::ST4Fourv4s_POST);
3758       return;
3759     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3760       SelectPostStore(Node, 4, AArch64::ST4Fourv2d_POST);
3761       return;
3762     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3763       SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3764       return;
3765     }
3766     break;
3767   }
3768   case AArch64ISD::ST1x2post: {
3769     VT = Node->getOperand(1).getValueType();
3770     if (VT == MVT::v8i8) {
3771       SelectPostStore(Node, 2, AArch64::ST1Twov8b_POST);
3772       return;
3773     } else if (VT == MVT::v16i8) {
3774       SelectPostStore(Node, 2, AArch64::ST1Twov16b_POST);
3775       return;
3776     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3777       SelectPostStore(Node, 2, AArch64::ST1Twov4h_POST);
3778       return;
3779     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3780       SelectPostStore(Node, 2, AArch64::ST1Twov8h_POST);
3781       return;
3782     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3783       SelectPostStore(Node, 2, AArch64::ST1Twov2s_POST);
3784       return;
3785     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3786       SelectPostStore(Node, 2, AArch64::ST1Twov4s_POST);
3787       return;
3788     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3789       SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3790       return;
3791     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3792       SelectPostStore(Node, 2, AArch64::ST1Twov2d_POST);
3793       return;
3794     }
3795     break;
3796   }
3797   case AArch64ISD::ST1x3post: {
3798     VT = Node->getOperand(1).getValueType();
3799     if (VT == MVT::v8i8) {
3800       SelectPostStore(Node, 3, AArch64::ST1Threev8b_POST);
3801       return;
3802     } else if (VT == MVT::v16i8) {
3803       SelectPostStore(Node, 3, AArch64::ST1Threev16b_POST);
3804       return;
3805     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3806       SelectPostStore(Node, 3, AArch64::ST1Threev4h_POST);
3807       return;
3808     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3809       SelectPostStore(Node, 3, AArch64::ST1Threev8h_POST);
3810       return;
3811     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3812       SelectPostStore(Node, 3, AArch64::ST1Threev2s_POST);
3813       return;
3814     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3815       SelectPostStore(Node, 3, AArch64::ST1Threev4s_POST);
3816       return;
3817     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3818       SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3819       return;
3820     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3821       SelectPostStore(Node, 3, AArch64::ST1Threev2d_POST);
3822       return;
3823     }
3824     break;
3825   }
3826   case AArch64ISD::ST1x4post: {
3827     VT = Node->getOperand(1).getValueType();
3828     if (VT == MVT::v8i8) {
3829       SelectPostStore(Node, 4, AArch64::ST1Fourv8b_POST);
3830       return;
3831     } else if (VT == MVT::v16i8) {
3832       SelectPostStore(Node, 4, AArch64::ST1Fourv16b_POST);
3833       return;
3834     } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3835       SelectPostStore(Node, 4, AArch64::ST1Fourv4h_POST);
3836       return;
3837     } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3838       SelectPostStore(Node, 4, AArch64::ST1Fourv8h_POST);
3839       return;
3840     } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3841       SelectPostStore(Node, 4, AArch64::ST1Fourv2s_POST);
3842       return;
3843     } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3844       SelectPostStore(Node, 4, AArch64::ST1Fourv4s_POST);
3845       return;
3846     } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3847       SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3848       return;
3849     } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3850       SelectPostStore(Node, 4, AArch64::ST1Fourv2d_POST);
3851       return;
3852     }
3853     break;
3854   }
3855   case AArch64ISD::ST2LANEpost: {
3856     VT = Node->getOperand(1).getValueType();
3857     if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3858       SelectPostStoreLane(Node, 2, AArch64::ST2i8_POST);
3859       return;
3860     } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3861                VT == MVT::v8f16) {
3862       SelectPostStoreLane(Node, 2, AArch64::ST2i16_POST);
3863       return;
3864     } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3865                VT == MVT::v2f32) {
3866       SelectPostStoreLane(Node, 2, AArch64::ST2i32_POST);
3867       return;
3868     } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3869                VT == MVT::v1f64) {
3870       SelectPostStoreLane(Node, 2, AArch64::ST2i64_POST);
3871       return;
3872     }
3873     break;
3874   }
3875   case AArch64ISD::ST3LANEpost: {
3876     VT = Node->getOperand(1).getValueType();
3877     if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3878       SelectPostStoreLane(Node, 3, AArch64::ST3i8_POST);
3879       return;
3880     } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3881                VT == MVT::v8f16) {
3882       SelectPostStoreLane(Node, 3, AArch64::ST3i16_POST);
3883       return;
3884     } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3885                VT == MVT::v2f32) {
3886       SelectPostStoreLane(Node, 3, AArch64::ST3i32_POST);
3887       return;
3888     } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3889                VT == MVT::v1f64) {
3890       SelectPostStoreLane(Node, 3, AArch64::ST3i64_POST);
3891       return;
3892     }
3893     break;
3894   }
3895   case AArch64ISD::ST4LANEpost: {
3896     VT = Node->getOperand(1).getValueType();
3897     if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3898       SelectPostStoreLane(Node, 4, AArch64::ST4i8_POST);
3899       return;
3900     } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3901                VT == MVT::v8f16) {
3902       SelectPostStoreLane(Node, 4, AArch64::ST4i16_POST);
3903       return;
3904     } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3905                VT == MVT::v2f32) {
3906       SelectPostStoreLane(Node, 4, AArch64::ST4i32_POST);
3907       return;
3908     } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3909                VT == MVT::v1f64) {
3910       SelectPostStoreLane(Node, 4, AArch64::ST4i64_POST);
3911       return;
3912     }
3913     break;
3914   }
3915   }
3916 
3917   // Select the default instruction
3918   SelectCode(Node);
3919 }
3920 
3921 /// createAArch64ISelDag - This pass converts a legalized DAG into a
3922 /// AArch64-specific DAG, ready for instruction scheduling.
3923 FunctionPass *llvm::createAArch64ISelDag(AArch64TargetMachine &TM,
3924                                          CodeGenOpt::Level OptLevel) {
3925   return new AArch64DAGToDAGISel(TM, OptLevel);
3926 }
3927