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