OSDN Git Service

Implement the newly added ACLE functions for ld1/st1 with 2/3/4 vectors.
[android-x86/external-llvm.git] / lib / Target / AArch64 / AArch64ISelDAGToDAG.cpp
1 //===-- AArch64ISelDAGToDAG.cpp - A dag to dag inst selector for AArch64 --===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an instruction selector for the AArch64 target.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #define DEBUG_TYPE "aarch64-isel"
15 #include "AArch64.h"
16 #include "AArch64InstrInfo.h"
17 #include "AArch64Subtarget.h"
18 #include "AArch64TargetMachine.h"
19 #include "Utils/AArch64BaseInfo.h"
20 #include "llvm/ADT/APSInt.h"
21 #include "llvm/CodeGen/SelectionDAGISel.h"
22 #include "llvm/IR/GlobalValue.h"
23 #include "llvm/Support/Debug.h"
24 #include "llvm/Support/raw_ostream.h"
25
26 using namespace llvm;
27
28 //===--------------------------------------------------------------------===//
29 /// AArch64 specific code to select AArch64 machine instructions for
30 /// SelectionDAG operations.
31 ///
32 namespace {
33
34 class AArch64DAGToDAGISel : public SelectionDAGISel {
35   AArch64TargetMachine &TM;
36
37   /// Keep a pointer to the AArch64Subtarget around so that we can
38   /// make the right decision when generating code for different targets.
39   const AArch64Subtarget *Subtarget;
40
41 public:
42   explicit AArch64DAGToDAGISel(AArch64TargetMachine &tm,
43                                CodeGenOpt::Level OptLevel)
44     : SelectionDAGISel(tm, OptLevel), TM(tm),
45       Subtarget(&TM.getSubtarget<AArch64Subtarget>()) {
46   }
47
48   virtual const char *getPassName() const {
49     return "AArch64 Instruction Selection";
50   }
51
52   // Include the pieces autogenerated from the target description.
53 #include "AArch64GenDAGISel.inc"
54
55   template<unsigned MemSize>
56   bool SelectOffsetUImm12(SDValue N, SDValue &UImm12) {
57     const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
58     if (!CN || CN->getZExtValue() % MemSize != 0
59         || CN->getZExtValue() / MemSize > 0xfff)
60       return false;
61
62     UImm12 =  CurDAG->getTargetConstant(CN->getZExtValue() / MemSize, MVT::i64);
63     return true;
64   }
65
66   template<unsigned RegWidth>
67   bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
68     return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
69   }
70
71   /// Used for pre-lowered address-reference nodes, so we already know
72   /// the fields match. This operand's job is simply to add an
73   /// appropriate shift operand to the MOVZ/MOVK instruction.
74   template<unsigned LogShift>
75   bool SelectMOVWAddressRef(SDValue N, SDValue &Imm, SDValue &Shift) {
76     Imm = N;
77     Shift = CurDAG->getTargetConstant(LogShift, MVT::i32);
78     return true;
79   }
80
81   bool SelectFPZeroOperand(SDValue N, SDValue &Dummy);
82
83   bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
84                                 unsigned RegWidth);
85
86   bool SelectInlineAsmMemoryOperand(const SDValue &Op,
87                                     char ConstraintCode,
88                                     std::vector<SDValue> &OutOps);
89
90   bool SelectLogicalImm(SDValue N, SDValue &Imm);
91
92   template<unsigned RegWidth>
93   bool SelectTSTBOperand(SDValue N, SDValue &FixedPos) {
94     return SelectTSTBOperand(N, FixedPos, RegWidth);
95   }
96
97   bool SelectTSTBOperand(SDValue N, SDValue &FixedPos, unsigned RegWidth);
98
99   SDNode *SelectAtomic(SDNode *N, unsigned Op8, unsigned Op16, unsigned Op32,
100                        unsigned Op64);
101
102   /// Put the given constant into a pool and return a DAG which will give its
103   /// address.
104   SDValue getConstantPoolItemAddress(SDLoc DL, const Constant *CV);
105
106   SDNode *TrySelectToMoveImm(SDNode *N);
107   SDNode *LowerToFPLitPool(SDNode *Node);
108   SDNode *SelectToLitPool(SDNode *N);
109
110   SDNode* Select(SDNode*);
111 private:
112   /// Get the opcode for table lookup instruction
113   unsigned getTBLOpc(bool IsExt, bool Is64Bit, unsigned NumOfVec);
114
115   /// Select NEON table lookup intrinsics.  NumVecs should be 1, 2, 3 or 4.
116   /// IsExt is to indicate if the result will be extended with an argument.
117   SDNode *SelectVTBL(SDNode *N, unsigned NumVecs, bool IsExt);
118
119   /// Select NEON load intrinsics.  NumVecs should be 1, 2, 3 or 4.
120   SDNode *SelectVLD(SDNode *N, unsigned NumVecs, bool isUpdating,
121                     const uint16_t *Opcode);
122
123   /// Select NEON store intrinsics.  NumVecs should be 1, 2, 3 or 4.
124   SDNode *SelectVST(SDNode *N, unsigned NumVecs, bool isUpdating,
125                     const uint16_t *Opcodes);
126
127   /// Form sequences of consecutive 64/128-bit registers for use in NEON
128   /// instructions making use of a vector-list (e.g. ldN, tbl). Vecs must have
129   /// between 1 and 4 elements. If it contains a single element that is returned
130   /// unchanged; otherwise a REG_SEQUENCE value is returned.
131   SDValue createDTuple(ArrayRef<SDValue> Vecs);
132   SDValue createQTuple(ArrayRef<SDValue> Vecs);
133
134   /// Generic helper for the createDTuple/createQTuple
135   /// functions. Those should almost always be called instead.
136   SDValue createTuple(ArrayRef<SDValue> Vecs, unsigned RegClassIDs[],
137                       unsigned SubRegs[]);
138 };
139 }
140
141 bool
142 AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
143                                               unsigned RegWidth) {
144   const ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N);
145   if (!CN) return false;
146
147   // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
148   // is between 1 and 32 for a destination w-register, or 1 and 64 for an
149   // x-register.
150   //
151   // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
152   // want THIS_NODE to be 2^fbits. This is much easier to deal with using
153   // integers.
154   bool IsExact;
155
156   // fbits is between 1 and 64 in the worst-case, which means the fmul
157   // could have 2^64 as an actual operand. Need 65 bits of precision.
158   APSInt IntVal(65, true);
159   CN->getValueAPF().convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
160
161   // N.b. isPowerOf2 also checks for > 0.
162   if (!IsExact || !IntVal.isPowerOf2()) return false;
163   unsigned FBits = IntVal.logBase2();
164
165   // Checks above should have guaranteed that we haven't lost information in
166   // finding FBits, but it must still be in range.
167   if (FBits == 0 || FBits > RegWidth) return false;
168
169   FixedPos = CurDAG->getTargetConstant(64 - FBits, MVT::i32);
170   return true;
171 }
172
173 bool
174 AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(const SDValue &Op,
175                                                  char ConstraintCode,
176                                                  std::vector<SDValue> &OutOps) {
177   switch (ConstraintCode) {
178   default: llvm_unreachable("Unrecognised AArch64 memory constraint");
179   case 'm':
180     // FIXME: more freedom is actually permitted for 'm'. We can go
181     // hunting for a base and an offset if we want. Of course, since
182     // we don't really know how the operand is going to be used we're
183     // probably restricted to the load/store pair's simm7 as an offset
184     // range anyway.
185   case 'Q':
186     OutOps.push_back(Op);
187   }
188
189   return false;
190 }
191
192 bool
193 AArch64DAGToDAGISel::SelectFPZeroOperand(SDValue N, SDValue &Dummy) {
194   ConstantFPSDNode *Imm = dyn_cast<ConstantFPSDNode>(N);
195   if (!Imm || !Imm->getValueAPF().isPosZero())
196     return false;
197
198   // Doesn't actually carry any information, but keeps TableGen quiet.
199   Dummy = CurDAG->getTargetConstant(0, MVT::i32);
200   return true;
201 }
202
203 bool AArch64DAGToDAGISel::SelectLogicalImm(SDValue N, SDValue &Imm) {
204   uint32_t Bits;
205   uint32_t RegWidth = N.getValueType().getSizeInBits();
206
207   ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
208   if (!CN) return false;
209
210   if (!A64Imms::isLogicalImm(RegWidth, CN->getZExtValue(), Bits))
211     return false;
212
213   Imm = CurDAG->getTargetConstant(Bits, MVT::i32);
214   return true;
215 }
216
217 SDNode *AArch64DAGToDAGISel::TrySelectToMoveImm(SDNode *Node) {
218   SDNode *ResNode;
219   SDLoc dl(Node);
220   EVT DestType = Node->getValueType(0);
221   unsigned DestWidth = DestType.getSizeInBits();
222
223   unsigned MOVOpcode;
224   EVT MOVType;
225   int UImm16, Shift;
226   uint32_t LogicalBits;
227
228   uint64_t BitPat = cast<ConstantSDNode>(Node)->getZExtValue();
229   if (A64Imms::isMOVZImm(DestWidth, BitPat, UImm16, Shift)) {
230     MOVType = DestType;
231     MOVOpcode = DestWidth == 64 ? AArch64::MOVZxii : AArch64::MOVZwii;
232   } else if (A64Imms::isMOVNImm(DestWidth, BitPat, UImm16, Shift)) {
233     MOVType = DestType;
234     MOVOpcode = DestWidth == 64 ? AArch64::MOVNxii : AArch64::MOVNwii;
235   } else if (DestWidth == 64 && A64Imms::isMOVNImm(32, BitPat, UImm16, Shift)) {
236     // To get something like 0x0000_0000_ffff_1234 into a 64-bit register we can
237     // use a 32-bit instruction: "movn w0, 0xedbc".
238     MOVType = MVT::i32;
239     MOVOpcode = AArch64::MOVNwii;
240   } else if (A64Imms::isLogicalImm(DestWidth, BitPat, LogicalBits))  {
241     MOVOpcode = DestWidth == 64 ? AArch64::ORRxxi : AArch64::ORRwwi;
242     uint16_t ZR = DestWidth == 64 ? AArch64::XZR : AArch64::WZR;
243
244     return CurDAG->getMachineNode(MOVOpcode, dl, DestType,
245                               CurDAG->getRegister(ZR, DestType),
246                               CurDAG->getTargetConstant(LogicalBits, MVT::i32));
247   } else {
248     // Can't handle it in one instruction. There's scope for permitting two (or
249     // more) instructions, but that'll need more thought.
250     return NULL;
251   }
252
253   ResNode = CurDAG->getMachineNode(MOVOpcode, dl, MOVType,
254                                    CurDAG->getTargetConstant(UImm16, MVT::i32),
255                                    CurDAG->getTargetConstant(Shift, MVT::i32));
256
257   if (MOVType != DestType) {
258     ResNode = CurDAG->getMachineNode(TargetOpcode::SUBREG_TO_REG, dl,
259                           MVT::i64, MVT::i32, MVT::Other,
260                           CurDAG->getTargetConstant(0, MVT::i64),
261                           SDValue(ResNode, 0),
262                           CurDAG->getTargetConstant(AArch64::sub_32, MVT::i32));
263   }
264
265   return ResNode;
266 }
267
268 SDValue
269 AArch64DAGToDAGISel::getConstantPoolItemAddress(SDLoc DL,
270                                                 const Constant *CV) {
271   EVT PtrVT = getTargetLowering()->getPointerTy();
272
273   switch (getTargetLowering()->getTargetMachine().getCodeModel()) {
274   case CodeModel::Small: {
275     unsigned Alignment =
276       getTargetLowering()->getDataLayout()->getABITypeAlignment(CV->getType());
277     return CurDAG->getNode(
278         AArch64ISD::WrapperSmall, DL, PtrVT,
279         CurDAG->getTargetConstantPool(CV, PtrVT, 0, 0, AArch64II::MO_NO_FLAG),
280         CurDAG->getTargetConstantPool(CV, PtrVT, 0, 0, AArch64II::MO_LO12),
281         CurDAG->getConstant(Alignment, MVT::i32));
282   }
283   case CodeModel::Large: {
284     SDNode *LitAddr;
285     LitAddr = CurDAG->getMachineNode(
286         AArch64::MOVZxii, DL, PtrVT,
287         CurDAG->getTargetConstantPool(CV, PtrVT, 0, 0, AArch64II::MO_ABS_G3),
288         CurDAG->getTargetConstant(3, MVT::i32));
289     LitAddr = CurDAG->getMachineNode(
290         AArch64::MOVKxii, DL, PtrVT, SDValue(LitAddr, 0),
291         CurDAG->getTargetConstantPool(CV, PtrVT, 0, 0, AArch64II::MO_ABS_G2_NC),
292         CurDAG->getTargetConstant(2, MVT::i32));
293     LitAddr = CurDAG->getMachineNode(
294         AArch64::MOVKxii, DL, PtrVT, SDValue(LitAddr, 0),
295         CurDAG->getTargetConstantPool(CV, PtrVT, 0, 0, AArch64II::MO_ABS_G1_NC),
296         CurDAG->getTargetConstant(1, MVT::i32));
297     LitAddr = CurDAG->getMachineNode(
298         AArch64::MOVKxii, DL, PtrVT, SDValue(LitAddr, 0),
299         CurDAG->getTargetConstantPool(CV, PtrVT, 0, 0, AArch64II::MO_ABS_G0_NC),
300         CurDAG->getTargetConstant(0, MVT::i32));
301     return SDValue(LitAddr, 0);
302   }
303   default:
304     llvm_unreachable("Only small and large code models supported now");
305   }
306 }
307
308 SDNode *AArch64DAGToDAGISel::SelectToLitPool(SDNode *Node) {
309   SDLoc DL(Node);
310   uint64_t UnsignedVal = cast<ConstantSDNode>(Node)->getZExtValue();
311   int64_t SignedVal = cast<ConstantSDNode>(Node)->getSExtValue();
312   EVT DestType = Node->getValueType(0);
313
314   // Since we may end up loading a 64-bit constant from a 32-bit entry the
315   // constant in the pool may have a different type to the eventual node.
316   ISD::LoadExtType Extension;
317   EVT MemType;
318
319   assert((DestType == MVT::i64 || DestType == MVT::i32)
320          && "Only expect integer constants at the moment");
321
322   if (DestType == MVT::i32) {
323     Extension = ISD::NON_EXTLOAD;
324     MemType = MVT::i32;
325   } else if (UnsignedVal <= UINT32_MAX) {
326     Extension = ISD::ZEXTLOAD;
327     MemType = MVT::i32;
328   } else if (SignedVal >= INT32_MIN && SignedVal <= INT32_MAX) {
329     Extension = ISD::SEXTLOAD;
330     MemType = MVT::i32;
331   } else {
332     Extension = ISD::NON_EXTLOAD;
333     MemType = MVT::i64;
334   }
335
336   Constant *CV = ConstantInt::get(Type::getIntNTy(*CurDAG->getContext(),
337                                                   MemType.getSizeInBits()),
338                                   UnsignedVal);
339   SDValue PoolAddr = getConstantPoolItemAddress(DL, CV);
340   unsigned Alignment =
341     getTargetLowering()->getDataLayout()->getABITypeAlignment(CV->getType());
342
343   return CurDAG->getExtLoad(Extension, DL, DestType, CurDAG->getEntryNode(),
344                             PoolAddr,
345                             MachinePointerInfo::getConstantPool(), MemType,
346                             /* isVolatile = */ false,
347                             /* isNonTemporal = */ false,
348                             Alignment).getNode();
349 }
350
351 SDNode *AArch64DAGToDAGISel::LowerToFPLitPool(SDNode *Node) {
352   SDLoc DL(Node);
353   const ConstantFP *FV = cast<ConstantFPSDNode>(Node)->getConstantFPValue();
354   EVT DestType = Node->getValueType(0);
355
356   unsigned Alignment =
357     getTargetLowering()->getDataLayout()->getABITypeAlignment(FV->getType());
358   SDValue PoolAddr = getConstantPoolItemAddress(DL, FV);
359
360   return CurDAG->getLoad(DestType, DL, CurDAG->getEntryNode(), PoolAddr,
361                          MachinePointerInfo::getConstantPool(),
362                          /* isVolatile = */ false,
363                          /* isNonTemporal = */ false,
364                          /* isInvariant = */ true,
365                          Alignment).getNode();
366 }
367
368 bool
369 AArch64DAGToDAGISel::SelectTSTBOperand(SDValue N, SDValue &FixedPos,
370                                        unsigned RegWidth) {
371   const ConstantSDNode *CN = dyn_cast<ConstantSDNode>(N);
372   if (!CN) return false;
373
374   uint64_t Val = CN->getZExtValue();
375
376   if (!isPowerOf2_64(Val)) return false;
377
378   unsigned TestedBit = Log2_64(Val);
379   // Checks above should have guaranteed that we haven't lost information in
380   // finding TestedBit, but it must still be in range.
381   if (TestedBit >= RegWidth) return false;
382
383   FixedPos = CurDAG->getTargetConstant(TestedBit, MVT::i64);
384   return true;
385 }
386
387 SDNode *AArch64DAGToDAGISel::SelectAtomic(SDNode *Node, unsigned Op8,
388                                           unsigned Op16,unsigned Op32,
389                                           unsigned Op64) {
390   // Mostly direct translation to the given operations, except that we preserve
391   // the AtomicOrdering for use later on.
392   AtomicSDNode *AN = cast<AtomicSDNode>(Node);
393   EVT VT = AN->getMemoryVT();
394
395   unsigned Op;
396   if (VT == MVT::i8)
397     Op = Op8;
398   else if (VT == MVT::i16)
399     Op = Op16;
400   else if (VT == MVT::i32)
401     Op = Op32;
402   else if (VT == MVT::i64)
403     Op = Op64;
404   else
405     llvm_unreachable("Unexpected atomic operation");
406
407   SmallVector<SDValue, 4> Ops;
408   for (unsigned i = 1; i < AN->getNumOperands(); ++i)
409       Ops.push_back(AN->getOperand(i));
410
411   Ops.push_back(CurDAG->getTargetConstant(AN->getOrdering(), MVT::i32));
412   Ops.push_back(AN->getOperand(0)); // Chain moves to the end
413
414   return CurDAG->SelectNodeTo(Node, Op,
415                               AN->getValueType(0), MVT::Other,
416                               &Ops[0], Ops.size());
417 }
418
419 SDValue AArch64DAGToDAGISel::createDTuple(ArrayRef<SDValue> Regs) {
420   static unsigned RegClassIDs[] = { AArch64::DPairRegClassID,
421                                     AArch64::DTripleRegClassID,
422                                     AArch64::DQuadRegClassID };
423   static unsigned SubRegs[] = { AArch64::dsub_0, AArch64::dsub_1,
424                                 AArch64::dsub_2, AArch64::dsub_3 };
425
426   return createTuple(Regs, RegClassIDs, SubRegs);
427 }
428
429 SDValue AArch64DAGToDAGISel::createQTuple(ArrayRef<SDValue> Regs) {
430   static unsigned RegClassIDs[] = { AArch64::QPairRegClassID,
431                                     AArch64::QTripleRegClassID,
432                                     AArch64::QQuadRegClassID };
433   static unsigned SubRegs[] = { AArch64::qsub_0, AArch64::qsub_1,
434                                 AArch64::qsub_2, AArch64::qsub_3 };
435
436   return createTuple(Regs, RegClassIDs, SubRegs);
437 }
438
439 SDValue AArch64DAGToDAGISel::createTuple(ArrayRef<SDValue> Regs,
440                                          unsigned RegClassIDs[],
441                                          unsigned SubRegs[]) {
442   // There's no special register-class for a vector-list of 1 element: it's just
443   // a vector.
444   if (Regs.size() == 1)
445     return Regs[0];
446
447   assert(Regs.size() >= 2 && Regs.size() <= 4);
448
449   SDLoc DL(Regs[0].getNode());
450
451   SmallVector<SDValue, 4> Ops;
452
453   // First operand of REG_SEQUENCE is the desired RegClass.
454   Ops.push_back(
455       CurDAG->getTargetConstant(RegClassIDs[Regs.size() - 2], MVT::i32));
456
457   // Then we get pairs of source & subregister-position for the components.
458   for (unsigned i = 0; i < Regs.size(); ++i) {
459     Ops.push_back(Regs[i]);
460     Ops.push_back(CurDAG->getTargetConstant(SubRegs[i], MVT::i32));
461   }
462
463   SDNode *N =
464       CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops);
465   return SDValue(N, 0);
466 }
467
468
469 // Get the register stride update opcode of a VLD/VST instruction that
470 // is otherwise equivalent to the given fixed stride updating instruction.
471 static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) {
472   switch (Opc) {
473   default: break;
474   case AArch64::LD1WB_8B_fixed: return AArch64::LD1WB_8B_register;
475   case AArch64::LD1WB_4H_fixed: return AArch64::LD1WB_4H_register;
476   case AArch64::LD1WB_2S_fixed: return AArch64::LD1WB_2S_register;
477   case AArch64::LD1WB_1D_fixed: return AArch64::LD1WB_1D_register;
478   case AArch64::LD1WB_16B_fixed: return AArch64::LD1WB_16B_register;
479   case AArch64::LD1WB_8H_fixed: return AArch64::LD1WB_8H_register;
480   case AArch64::LD1WB_4S_fixed: return AArch64::LD1WB_4S_register;
481   case AArch64::LD1WB_2D_fixed: return AArch64::LD1WB_2D_register;
482
483   case AArch64::LD2WB_8B_fixed: return AArch64::LD2WB_8B_register;
484   case AArch64::LD2WB_4H_fixed: return AArch64::LD2WB_4H_register;
485   case AArch64::LD2WB_2S_fixed: return AArch64::LD2WB_2S_register;
486   case AArch64::LD2WB_16B_fixed: return AArch64::LD2WB_16B_register;
487   case AArch64::LD2WB_8H_fixed: return AArch64::LD2WB_8H_register;
488   case AArch64::LD2WB_4S_fixed: return AArch64::LD2WB_4S_register;
489   case AArch64::LD2WB_2D_fixed: return AArch64::LD2WB_2D_register;
490
491   case AArch64::LD3WB_8B_fixed: return AArch64::LD3WB_8B_register;
492   case AArch64::LD3WB_4H_fixed: return AArch64::LD3WB_4H_register;
493   case AArch64::LD3WB_2S_fixed: return AArch64::LD3WB_2S_register;
494   case AArch64::LD3WB_16B_fixed: return AArch64::LD3WB_16B_register;
495   case AArch64::LD3WB_8H_fixed: return AArch64::LD3WB_8H_register;
496   case AArch64::LD3WB_4S_fixed: return AArch64::LD3WB_4S_register;
497   case AArch64::LD3WB_2D_fixed: return AArch64::LD3WB_2D_register;
498
499   case AArch64::LD4WB_8B_fixed: return AArch64::LD4WB_8B_register;
500   case AArch64::LD4WB_4H_fixed: return AArch64::LD4WB_4H_register;
501   case AArch64::LD4WB_2S_fixed: return AArch64::LD4WB_2S_register;
502   case AArch64::LD4WB_16B_fixed: return AArch64::LD4WB_16B_register;
503   case AArch64::LD4WB_8H_fixed: return AArch64::LD4WB_8H_register;
504   case AArch64::LD4WB_4S_fixed: return AArch64::LD4WB_4S_register;
505   case AArch64::LD4WB_2D_fixed: return AArch64::LD4WB_2D_register;
506
507   case AArch64::LD1x2WB_8B_fixed: return AArch64::LD1x2WB_8B_register;
508   case AArch64::LD1x2WB_4H_fixed: return AArch64::LD1x2WB_4H_register;
509   case AArch64::LD1x2WB_2S_fixed: return AArch64::LD1x2WB_2S_register;
510   case AArch64::LD1x2WB_1D_fixed: return AArch64::LD1x2WB_1D_register;
511   case AArch64::LD1x2WB_16B_fixed: return AArch64::LD1x2WB_16B_register;
512   case AArch64::LD1x2WB_8H_fixed: return AArch64::LD1x2WB_8H_register;
513   case AArch64::LD1x2WB_4S_fixed: return AArch64::LD1x2WB_4S_register;
514   case AArch64::LD1x2WB_2D_fixed: return AArch64::LD1x2WB_2D_register;
515
516   case AArch64::LD1x3WB_8B_fixed: return AArch64::LD1x3WB_8B_register;
517   case AArch64::LD1x3WB_4H_fixed: return AArch64::LD1x3WB_4H_register;
518   case AArch64::LD1x3WB_2S_fixed: return AArch64::LD1x3WB_2S_register;
519   case AArch64::LD1x3WB_1D_fixed: return AArch64::LD1x3WB_1D_register;
520   case AArch64::LD1x3WB_16B_fixed: return AArch64::LD1x3WB_16B_register;
521   case AArch64::LD1x3WB_8H_fixed: return AArch64::LD1x3WB_8H_register;
522   case AArch64::LD1x3WB_4S_fixed: return AArch64::LD1x3WB_4S_register;
523   case AArch64::LD1x3WB_2D_fixed: return AArch64::LD1x3WB_2D_register;
524
525   case AArch64::LD1x4WB_8B_fixed: return AArch64::LD1x4WB_8B_register;
526   case AArch64::LD1x4WB_4H_fixed: return AArch64::LD1x4WB_4H_register;
527   case AArch64::LD1x4WB_2S_fixed: return AArch64::LD1x4WB_2S_register;
528   case AArch64::LD1x4WB_1D_fixed: return AArch64::LD1x4WB_1D_register;
529   case AArch64::LD1x4WB_16B_fixed: return AArch64::LD1x4WB_16B_register;
530   case AArch64::LD1x4WB_8H_fixed: return AArch64::LD1x4WB_8H_register;
531   case AArch64::LD1x4WB_4S_fixed: return AArch64::LD1x4WB_4S_register;
532   case AArch64::LD1x4WB_2D_fixed: return AArch64::LD1x4WB_2D_register;
533
534   case AArch64::ST1WB_8B_fixed: return AArch64::ST1WB_8B_register;
535   case AArch64::ST1WB_4H_fixed: return AArch64::ST1WB_4H_register;
536   case AArch64::ST1WB_2S_fixed: return AArch64::ST1WB_2S_register;
537   case AArch64::ST1WB_1D_fixed: return AArch64::ST1WB_1D_register;
538   case AArch64::ST1WB_16B_fixed: return AArch64::ST1WB_16B_register;
539   case AArch64::ST1WB_8H_fixed: return AArch64::ST1WB_8H_register;
540   case AArch64::ST1WB_4S_fixed: return AArch64::ST1WB_4S_register;
541   case AArch64::ST1WB_2D_fixed: return AArch64::ST1WB_2D_register;
542
543   case AArch64::ST2WB_8B_fixed: return AArch64::ST2WB_8B_register;
544   case AArch64::ST2WB_4H_fixed: return AArch64::ST2WB_4H_register;
545   case AArch64::ST2WB_2S_fixed: return AArch64::ST2WB_2S_register;
546   case AArch64::ST2WB_16B_fixed: return AArch64::ST2WB_16B_register;
547   case AArch64::ST2WB_8H_fixed: return AArch64::ST2WB_8H_register;
548   case AArch64::ST2WB_4S_fixed: return AArch64::ST2WB_4S_register;
549   case AArch64::ST2WB_2D_fixed: return AArch64::ST2WB_2D_register;
550
551   case AArch64::ST3WB_8B_fixed: return AArch64::ST3WB_8B_register;
552   case AArch64::ST3WB_4H_fixed: return AArch64::ST3WB_4H_register;
553   case AArch64::ST3WB_2S_fixed: return AArch64::ST3WB_2S_register;
554   case AArch64::ST3WB_16B_fixed: return AArch64::ST3WB_16B_register;
555   case AArch64::ST3WB_8H_fixed: return AArch64::ST3WB_8H_register;
556   case AArch64::ST3WB_4S_fixed: return AArch64::ST3WB_4S_register;
557   case AArch64::ST3WB_2D_fixed: return AArch64::ST3WB_2D_register;
558
559   case AArch64::ST4WB_8B_fixed: return AArch64::ST4WB_8B_register;
560   case AArch64::ST4WB_4H_fixed: return AArch64::ST4WB_4H_register;
561   case AArch64::ST4WB_2S_fixed: return AArch64::ST4WB_2S_register;
562   case AArch64::ST4WB_16B_fixed: return AArch64::ST4WB_16B_register;
563   case AArch64::ST4WB_8H_fixed: return AArch64::ST4WB_8H_register;
564   case AArch64::ST4WB_4S_fixed: return AArch64::ST4WB_4S_register;
565   case AArch64::ST4WB_2D_fixed: return AArch64::ST4WB_2D_register;
566
567   case AArch64::ST1x2WB_8B_fixed: return AArch64::ST1x2WB_8B_register;
568   case AArch64::ST1x2WB_4H_fixed: return AArch64::ST1x2WB_4H_register;
569   case AArch64::ST1x2WB_2S_fixed: return AArch64::ST1x2WB_2S_register;
570   case AArch64::ST1x2WB_1D_fixed: return AArch64::ST1x2WB_1D_register;
571   case AArch64::ST1x2WB_16B_fixed: return AArch64::ST1x2WB_16B_register;
572   case AArch64::ST1x2WB_8H_fixed: return AArch64::ST1x2WB_8H_register;
573   case AArch64::ST1x2WB_4S_fixed: return AArch64::ST1x2WB_4S_register;
574   case AArch64::ST1x2WB_2D_fixed: return AArch64::ST1x2WB_2D_register;
575
576   case AArch64::ST1x3WB_8B_fixed: return AArch64::ST1x3WB_8B_register;
577   case AArch64::ST1x3WB_4H_fixed: return AArch64::ST1x3WB_4H_register;
578   case AArch64::ST1x3WB_2S_fixed: return AArch64::ST1x3WB_2S_register;
579   case AArch64::ST1x3WB_1D_fixed: return AArch64::ST1x3WB_1D_register;
580   case AArch64::ST1x3WB_16B_fixed: return AArch64::ST1x3WB_16B_register;
581   case AArch64::ST1x3WB_8H_fixed: return AArch64::ST1x3WB_8H_register;
582   case AArch64::ST1x3WB_4S_fixed: return AArch64::ST1x3WB_4S_register;
583   case AArch64::ST1x3WB_2D_fixed: return AArch64::ST1x3WB_2D_register;
584
585   case AArch64::ST1x4WB_8B_fixed: return AArch64::ST1x4WB_8B_register;
586   case AArch64::ST1x4WB_4H_fixed: return AArch64::ST1x4WB_4H_register;
587   case AArch64::ST1x4WB_2S_fixed: return AArch64::ST1x4WB_2S_register;
588   case AArch64::ST1x4WB_1D_fixed: return AArch64::ST1x4WB_1D_register;
589   case AArch64::ST1x4WB_16B_fixed: return AArch64::ST1x4WB_16B_register;
590   case AArch64::ST1x4WB_8H_fixed: return AArch64::ST1x4WB_8H_register;
591   case AArch64::ST1x4WB_4S_fixed: return AArch64::ST1x4WB_4S_register;
592   case AArch64::ST1x4WB_2D_fixed: return AArch64::ST1x4WB_2D_register;
593   }
594   return Opc; // If not one we handle, return it unchanged.
595 }
596
597 SDNode *AArch64DAGToDAGISel::SelectVLD(SDNode *N, unsigned NumVecs,
598                                        bool isUpdating,
599                                        const uint16_t *Opcodes) {
600   assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
601
602   EVT VT = N->getValueType(0);
603   unsigned OpcodeIndex;
604   switch (VT.getSimpleVT().SimpleTy) {
605   default: llvm_unreachable("unhandled vector load type");
606   case MVT::v8i8:  OpcodeIndex = 0; break;
607   case MVT::v4i16: OpcodeIndex = 1; break;
608   case MVT::v2f32:
609   case MVT::v2i32: OpcodeIndex = 2; break;
610   case MVT::v1f64:
611   case MVT::v1i64: OpcodeIndex = 3; break;
612   case MVT::v16i8: OpcodeIndex = 4; break;
613   case MVT::v8f16:
614   case MVT::v8i16: OpcodeIndex = 5; break;
615   case MVT::v4f32:
616   case MVT::v4i32: OpcodeIndex = 6; break;
617   case MVT::v2f64:
618   case MVT::v2i64: OpcodeIndex = 7; break;
619   }
620   unsigned Opc = Opcodes[OpcodeIndex];
621
622   SmallVector<SDValue, 2> Ops;
623   unsigned AddrOpIdx = isUpdating ? 1 : 2;
624   Ops.push_back(N->getOperand(AddrOpIdx)); // Push back the Memory Address
625
626   if (isUpdating) {
627     SDValue Inc = N->getOperand(AddrOpIdx + 1);
628     if (!isa<ConstantSDNode>(Inc.getNode())) // Increment in Register
629       Opc = getVLDSTRegisterUpdateOpcode(Opc);
630     Ops.push_back(Inc);
631   }
632
633   Ops.push_back(N->getOperand(0)); // Push back the Chain
634
635   std::vector<EVT> ResTys;
636   bool is64BitVector = VT.is64BitVector();
637
638   if (NumVecs == 1)
639     ResTys.push_back(VT);
640   else if (NumVecs == 3)
641     ResTys.push_back(MVT::Untyped);
642   else {
643     EVT ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64,
644                                  is64BitVector ? NumVecs : NumVecs * 2);
645     ResTys.push_back(ResTy);
646   }
647
648   if (isUpdating)
649     ResTys.push_back(MVT::i64); // Type of the updated register
650   ResTys.push_back(MVT::Other); // Type of the Chain
651   SDLoc dl(N);
652   SDNode *VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
653
654   // Transfer memoperands.
655   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
656   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
657   cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1);
658
659   if (NumVecs == 1)
660     return VLd;
661
662   // If NumVecs > 1, the return result is a super register containing 2-4
663   // consecutive vector registers.
664   SDValue SuperReg = SDValue(VLd, 0);
665
666   unsigned Sub0 = is64BitVector ? AArch64::dsub_0 : AArch64::qsub_0;
667   for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
668     ReplaceUses(SDValue(N, Vec),
669                 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
670   // Update users of the Chain
671   ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
672   if (isUpdating)
673     ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
674
675   return NULL;
676 }
677
678 SDNode *AArch64DAGToDAGISel::SelectVST(SDNode *N, unsigned NumVecs,
679                                        bool isUpdating,
680                                        const uint16_t *Opcodes) {
681   assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
682   SDLoc dl(N);
683
684   MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
685   MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
686
687   unsigned AddrOpIdx = isUpdating ? 1 : 2;
688   unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
689   EVT VT = N->getOperand(Vec0Idx).getValueType();
690   unsigned OpcodeIndex;
691   switch (VT.getSimpleVT().SimpleTy) {
692   default: llvm_unreachable("unhandled vector store type");
693   case MVT::v8i8:  OpcodeIndex = 0; break;
694   case MVT::v4i16: OpcodeIndex = 1; break;
695   case MVT::v2f32:
696   case MVT::v2i32: OpcodeIndex = 2; break;
697   case MVT::v1f64:
698   case MVT::v1i64: OpcodeIndex = 3; break;
699   case MVT::v16i8: OpcodeIndex = 4; break;
700   case MVT::v8f16:
701   case MVT::v8i16: OpcodeIndex = 5; break;
702   case MVT::v4f32:
703   case MVT::v4i32: OpcodeIndex = 6; break;
704   case MVT::v2f64:
705   case MVT::v2i64: OpcodeIndex = 7; break;
706   }
707   unsigned Opc = Opcodes[OpcodeIndex];
708
709   std::vector<EVT> ResTys;
710   if (isUpdating)
711     ResTys.push_back(MVT::i64);
712   ResTys.push_back(MVT::Other); // Type for the Chain
713
714   SmallVector<SDValue, 6> Ops;
715   Ops.push_back(N->getOperand(AddrOpIdx)); // Push back the Memory Address
716
717   if (isUpdating) {
718     SDValue Inc = N->getOperand(AddrOpIdx + 1);
719     if (!isa<ConstantSDNode>(Inc.getNode())) // Increment in Register
720       Opc = getVLDSTRegisterUpdateOpcode(Opc);
721     Ops.push_back(Inc);
722   }
723   bool is64BitVector = VT.is64BitVector();
724
725   SmallVector<SDValue, 4> Regs(N->op_begin() + Vec0Idx,
726                                N->op_begin() + Vec0Idx + NumVecs);
727   SDValue SrcReg = is64BitVector ? createDTuple(Regs) : createQTuple(Regs);
728   Ops.push_back(SrcReg);
729
730   // Push back the Chain
731   Ops.push_back(N->getOperand(0));
732
733   // Transfer memoperands.
734   SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
735   cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1);
736
737   return VSt;
738 }
739
740 unsigned AArch64DAGToDAGISel::getTBLOpc(bool IsExt, bool Is64Bit,
741                                         unsigned NumOfVec) {
742   assert(NumOfVec >= 1 && NumOfVec <= 4 && "VST NumVecs out-of-range");
743
744   unsigned Opc = 0;
745   switch (NumOfVec) {
746   default:
747     break;
748   case 1:
749     if (IsExt)
750       Opc = Is64Bit ? AArch64::TBX1_8b : AArch64::TBX1_16b;
751     else
752       Opc = Is64Bit ? AArch64::TBL1_8b : AArch64::TBL1_16b;
753     break;
754   case 2:
755     if (IsExt)
756       Opc = Is64Bit ? AArch64::TBX2_8b : AArch64::TBX2_16b;
757     else
758       Opc = Is64Bit ? AArch64::TBL2_8b : AArch64::TBL2_16b;
759     break;
760   case 3:
761     if (IsExt)
762       Opc = Is64Bit ? AArch64::TBX3_8b : AArch64::TBX3_16b;
763     else
764       Opc = Is64Bit ? AArch64::TBL3_8b : AArch64::TBL3_16b;
765     break;
766   case 4:
767     if (IsExt)
768       Opc = Is64Bit ? AArch64::TBX4_8b : AArch64::TBX4_16b;
769     else
770       Opc = Is64Bit ? AArch64::TBL4_8b : AArch64::TBL4_16b;
771     break;
772   }
773
774   return Opc;
775 }
776
777 SDNode *AArch64DAGToDAGISel::SelectVTBL(SDNode *N, unsigned NumVecs,
778                                         bool IsExt) {
779   assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
780   SDLoc dl(N);
781
782   // Check the element of look up table is 64-bit or not
783   unsigned Vec0Idx = IsExt ? 2 : 1;
784   assert(!N->getOperand(Vec0Idx + 0).getValueType().is64BitVector() &&
785          "The element of lookup table for vtbl and vtbx must be 128-bit");
786
787   // Check the return value type is 64-bit or not
788   EVT ResVT = N->getValueType(0);
789   bool is64BitRes = ResVT.is64BitVector();
790
791   // Create new SDValue for vector list
792   SmallVector<SDValue, 4> Regs(N->op_begin() + Vec0Idx,
793                                N->op_begin() + Vec0Idx + NumVecs);
794   SDValue TblReg = createQTuple(Regs);
795   unsigned Opc = getTBLOpc(IsExt, is64BitRes, NumVecs);
796
797   SmallVector<SDValue, 3> Ops;
798   if (IsExt)
799     Ops.push_back(N->getOperand(1));
800   Ops.push_back(TblReg);
801   Ops.push_back(N->getOperand(Vec0Idx + NumVecs));
802   return CurDAG->getMachineNode(Opc, dl, ResVT, Ops);
803 }
804
805 SDNode *AArch64DAGToDAGISel::Select(SDNode *Node) {
806   // Dump information about the Node being selected
807   DEBUG(dbgs() << "Selecting: "; Node->dump(CurDAG); dbgs() << "\n");
808
809   if (Node->isMachineOpcode()) {
810     DEBUG(dbgs() << "== "; Node->dump(CurDAG); dbgs() << "\n");
811     Node->setNodeId(-1);
812     return NULL;
813   }
814
815   switch (Node->getOpcode()) {
816   case ISD::ATOMIC_LOAD_ADD:
817     return SelectAtomic(Node,
818                         AArch64::ATOMIC_LOAD_ADD_I8,
819                         AArch64::ATOMIC_LOAD_ADD_I16,
820                         AArch64::ATOMIC_LOAD_ADD_I32,
821                         AArch64::ATOMIC_LOAD_ADD_I64);
822   case ISD::ATOMIC_LOAD_SUB:
823     return SelectAtomic(Node,
824                         AArch64::ATOMIC_LOAD_SUB_I8,
825                         AArch64::ATOMIC_LOAD_SUB_I16,
826                         AArch64::ATOMIC_LOAD_SUB_I32,
827                         AArch64::ATOMIC_LOAD_SUB_I64);
828   case ISD::ATOMIC_LOAD_AND:
829     return SelectAtomic(Node,
830                         AArch64::ATOMIC_LOAD_AND_I8,
831                         AArch64::ATOMIC_LOAD_AND_I16,
832                         AArch64::ATOMIC_LOAD_AND_I32,
833                         AArch64::ATOMIC_LOAD_AND_I64);
834   case ISD::ATOMIC_LOAD_OR:
835     return SelectAtomic(Node,
836                         AArch64::ATOMIC_LOAD_OR_I8,
837                         AArch64::ATOMIC_LOAD_OR_I16,
838                         AArch64::ATOMIC_LOAD_OR_I32,
839                         AArch64::ATOMIC_LOAD_OR_I64);
840   case ISD::ATOMIC_LOAD_XOR:
841     return SelectAtomic(Node,
842                         AArch64::ATOMIC_LOAD_XOR_I8,
843                         AArch64::ATOMIC_LOAD_XOR_I16,
844                         AArch64::ATOMIC_LOAD_XOR_I32,
845                         AArch64::ATOMIC_LOAD_XOR_I64);
846   case ISD::ATOMIC_LOAD_NAND:
847     return SelectAtomic(Node,
848                         AArch64::ATOMIC_LOAD_NAND_I8,
849                         AArch64::ATOMIC_LOAD_NAND_I16,
850                         AArch64::ATOMIC_LOAD_NAND_I32,
851                         AArch64::ATOMIC_LOAD_NAND_I64);
852   case ISD::ATOMIC_LOAD_MIN:
853     return SelectAtomic(Node,
854                         AArch64::ATOMIC_LOAD_MIN_I8,
855                         AArch64::ATOMIC_LOAD_MIN_I16,
856                         AArch64::ATOMIC_LOAD_MIN_I32,
857                         AArch64::ATOMIC_LOAD_MIN_I64);
858   case ISD::ATOMIC_LOAD_MAX:
859     return SelectAtomic(Node,
860                         AArch64::ATOMIC_LOAD_MAX_I8,
861                         AArch64::ATOMIC_LOAD_MAX_I16,
862                         AArch64::ATOMIC_LOAD_MAX_I32,
863                         AArch64::ATOMIC_LOAD_MAX_I64);
864   case ISD::ATOMIC_LOAD_UMIN:
865     return SelectAtomic(Node,
866                         AArch64::ATOMIC_LOAD_UMIN_I8,
867                         AArch64::ATOMIC_LOAD_UMIN_I16,
868                         AArch64::ATOMIC_LOAD_UMIN_I32,
869                         AArch64::ATOMIC_LOAD_UMIN_I64);
870   case ISD::ATOMIC_LOAD_UMAX:
871     return SelectAtomic(Node,
872                         AArch64::ATOMIC_LOAD_UMAX_I8,
873                         AArch64::ATOMIC_LOAD_UMAX_I16,
874                         AArch64::ATOMIC_LOAD_UMAX_I32,
875                         AArch64::ATOMIC_LOAD_UMAX_I64);
876   case ISD::ATOMIC_SWAP:
877     return SelectAtomic(Node,
878                         AArch64::ATOMIC_SWAP_I8,
879                         AArch64::ATOMIC_SWAP_I16,
880                         AArch64::ATOMIC_SWAP_I32,
881                         AArch64::ATOMIC_SWAP_I64);
882   case ISD::ATOMIC_CMP_SWAP:
883     return SelectAtomic(Node,
884                         AArch64::ATOMIC_CMP_SWAP_I8,
885                         AArch64::ATOMIC_CMP_SWAP_I16,
886                         AArch64::ATOMIC_CMP_SWAP_I32,
887                         AArch64::ATOMIC_CMP_SWAP_I64);
888   case ISD::FrameIndex: {
889     int FI = cast<FrameIndexSDNode>(Node)->getIndex();
890     EVT PtrTy = getTargetLowering()->getPointerTy();
891     SDValue TFI = CurDAG->getTargetFrameIndex(FI, PtrTy);
892     return CurDAG->SelectNodeTo(Node, AArch64::ADDxxi_lsl0_s, PtrTy,
893                                 TFI, CurDAG->getTargetConstant(0, PtrTy));
894   }
895   case ISD::ConstantPool: {
896     // Constant pools are fine, just create a Target entry.
897     ConstantPoolSDNode *CN = cast<ConstantPoolSDNode>(Node);
898     const Constant *C = CN->getConstVal();
899     SDValue CP = CurDAG->getTargetConstantPool(C, CN->getValueType(0));
900
901     ReplaceUses(SDValue(Node, 0), CP);
902     return NULL;
903   }
904   case ISD::Constant: {
905     SDNode *ResNode = 0;
906     if (cast<ConstantSDNode>(Node)->getZExtValue() == 0) {
907       // XZR and WZR are probably even better than an actual move: most of the
908       // time they can be folded into another instruction with *no* cost.
909
910       EVT Ty = Node->getValueType(0);
911       assert((Ty == MVT::i32 || Ty == MVT::i64) && "unexpected type");
912       uint16_t Register = Ty == MVT::i32 ? AArch64::WZR : AArch64::XZR;
913       ResNode = CurDAG->getCopyFromReg(CurDAG->getEntryNode(),
914                                        SDLoc(Node),
915                                        Register, Ty).getNode();
916     }
917
918     // Next best option is a move-immediate, see if we can do that.
919     if (!ResNode) {
920       ResNode = TrySelectToMoveImm(Node);
921     }
922
923     if (ResNode)
924       return ResNode;
925
926     // If even that fails we fall back to a lit-pool entry at the moment. Future
927     // tuning may change this to a sequence of MOVZ/MOVN/MOVK instructions.
928     ResNode = SelectToLitPool(Node);
929     assert(ResNode && "We need *some* way to materialise a constant");
930
931     // We want to continue selection at this point since the litpool access
932     // generated used generic nodes for simplicity.
933     ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
934     Node = ResNode;
935     break;
936   }
937   case ISD::ConstantFP: {
938     if (A64Imms::isFPImm(cast<ConstantFPSDNode>(Node)->getValueAPF())) {
939       // FMOV will take care of it from TableGen
940       break;
941     }
942
943     SDNode *ResNode = LowerToFPLitPool(Node);
944     ReplaceUses(SDValue(Node, 0), SDValue(ResNode, 0));
945
946     // We want to continue selection at this point since the litpool access
947     // generated used generic nodes for simplicity.
948     Node = ResNode;
949     break;
950   }
951   case AArch64ISD::NEON_LD1_UPD: {
952     static const uint16_t Opcodes[] = {
953       AArch64::LD1WB_8B_fixed,  AArch64::LD1WB_4H_fixed,
954       AArch64::LD1WB_2S_fixed,  AArch64::LD1WB_1D_fixed,
955       AArch64::LD1WB_16B_fixed, AArch64::LD1WB_8H_fixed,
956       AArch64::LD1WB_4S_fixed,  AArch64::LD1WB_2D_fixed
957     };
958     return SelectVLD(Node, 1, true, Opcodes);
959   }
960   case AArch64ISD::NEON_LD2_UPD: {
961     static const uint16_t Opcodes[] = {
962       AArch64::LD2WB_8B_fixed,  AArch64::LD2WB_4H_fixed,
963       AArch64::LD2WB_2S_fixed,  AArch64::LD1x2WB_1D_fixed,
964       AArch64::LD2WB_16B_fixed, AArch64::LD2WB_8H_fixed,
965       AArch64::LD2WB_4S_fixed,  AArch64::LD2WB_2D_fixed
966     };
967     return SelectVLD(Node, 2, true, Opcodes);
968   }
969   case AArch64ISD::NEON_LD3_UPD: {
970     static const uint16_t Opcodes[] = {
971       AArch64::LD3WB_8B_fixed,  AArch64::LD3WB_4H_fixed,
972       AArch64::LD3WB_2S_fixed,  AArch64::LD1x3WB_1D_fixed,
973       AArch64::LD3WB_16B_fixed, AArch64::LD3WB_8H_fixed,
974       AArch64::LD3WB_4S_fixed,  AArch64::LD3WB_2D_fixed
975     };
976     return SelectVLD(Node, 3, true, Opcodes);
977   }
978   case AArch64ISD::NEON_LD4_UPD: {
979     static const uint16_t Opcodes[] = {
980       AArch64::LD4WB_8B_fixed,  AArch64::LD4WB_4H_fixed,
981       AArch64::LD4WB_2S_fixed,  AArch64::LD1x4WB_1D_fixed,
982       AArch64::LD4WB_16B_fixed, AArch64::LD4WB_8H_fixed,
983       AArch64::LD4WB_4S_fixed,  AArch64::LD4WB_2D_fixed
984     };
985     return SelectVLD(Node, 4, true, Opcodes);
986   }
987   case AArch64ISD::NEON_LD1x2_UPD: {
988     static const uint16_t Opcodes[] = {
989       AArch64::LD1x2WB_8B_fixed,  AArch64::LD1x2WB_4H_fixed,
990       AArch64::LD1x2WB_2S_fixed,  AArch64::LD1x2WB_1D_fixed,
991       AArch64::LD1x2WB_16B_fixed, AArch64::LD1x2WB_8H_fixed,
992       AArch64::LD1x2WB_4S_fixed,  AArch64::LD1x2WB_2D_fixed
993     };
994     return SelectVLD(Node, 2, true, Opcodes);
995   }
996   case AArch64ISD::NEON_LD1x3_UPD: {
997     static const uint16_t Opcodes[] = {
998       AArch64::LD1x3WB_8B_fixed,  AArch64::LD1x3WB_4H_fixed,
999       AArch64::LD1x3WB_2S_fixed,  AArch64::LD1x3WB_1D_fixed,
1000       AArch64::LD1x3WB_16B_fixed, AArch64::LD1x3WB_8H_fixed,
1001       AArch64::LD1x3WB_4S_fixed,  AArch64::LD1x3WB_2D_fixed
1002     };
1003     return SelectVLD(Node, 3, true, Opcodes);
1004   }
1005   case AArch64ISD::NEON_LD1x4_UPD: {
1006     static const uint16_t Opcodes[] = {
1007       AArch64::LD1x4WB_8B_fixed,  AArch64::LD1x4WB_4H_fixed,
1008       AArch64::LD1x4WB_2S_fixed,  AArch64::LD1x4WB_1D_fixed,
1009       AArch64::LD1x4WB_16B_fixed, AArch64::LD1x4WB_8H_fixed,
1010       AArch64::LD1x4WB_4S_fixed,  AArch64::LD1x4WB_2D_fixed
1011     };
1012     return SelectVLD(Node, 4, true, Opcodes);
1013   }
1014   case AArch64ISD::NEON_ST1_UPD: {
1015     static const uint16_t Opcodes[] = {
1016       AArch64::ST1WB_8B_fixed,  AArch64::ST1WB_4H_fixed,
1017       AArch64::ST1WB_2S_fixed,  AArch64::ST1WB_1D_fixed,
1018       AArch64::ST1WB_16B_fixed, AArch64::ST1WB_8H_fixed,
1019       AArch64::ST1WB_4S_fixed,  AArch64::ST1WB_2D_fixed
1020     };
1021     return SelectVST(Node, 1, true, Opcodes);
1022   }
1023   case AArch64ISD::NEON_ST2_UPD: {
1024     static const uint16_t Opcodes[] = {
1025       AArch64::ST2WB_8B_fixed,  AArch64::ST2WB_4H_fixed,
1026       AArch64::ST2WB_2S_fixed,  AArch64::ST1x2WB_1D_fixed,
1027       AArch64::ST2WB_16B_fixed, AArch64::ST2WB_8H_fixed,
1028       AArch64::ST2WB_4S_fixed,  AArch64::ST2WB_2D_fixed
1029     };
1030     return SelectVST(Node, 2, true, Opcodes);
1031   }
1032   case AArch64ISD::NEON_ST3_UPD: {
1033     static const uint16_t Opcodes[] = {
1034       AArch64::ST3WB_8B_fixed,  AArch64::ST3WB_4H_fixed,
1035       AArch64::ST3WB_2S_fixed,  AArch64::ST1x3WB_1D_fixed,
1036       AArch64::ST3WB_16B_fixed, AArch64::ST3WB_8H_fixed,
1037       AArch64::ST3WB_4S_fixed,  AArch64::ST3WB_2D_fixed
1038     };
1039     return SelectVST(Node, 3, true, Opcodes);
1040   }
1041   case AArch64ISD::NEON_ST4_UPD: {
1042     static const uint16_t Opcodes[] = {
1043       AArch64::ST4WB_8B_fixed,  AArch64::ST4WB_4H_fixed,
1044       AArch64::ST4WB_2S_fixed,  AArch64::ST1x4WB_1D_fixed,
1045       AArch64::ST4WB_16B_fixed, AArch64::ST4WB_8H_fixed,
1046       AArch64::ST4WB_4S_fixed,  AArch64::ST4WB_2D_fixed
1047     };
1048     return SelectVST(Node, 4, true, Opcodes);
1049   }
1050   case AArch64ISD::NEON_ST1x2_UPD: {
1051     static const uint16_t Opcodes[] = {
1052       AArch64::ST1x2WB_8B_fixed,  AArch64::ST1x2WB_4H_fixed,
1053       AArch64::ST1x2WB_2S_fixed,  AArch64::ST1x2WB_1D_fixed,
1054       AArch64::ST1x2WB_16B_fixed, AArch64::ST1x2WB_8H_fixed,
1055       AArch64::ST1x2WB_4S_fixed,  AArch64::ST1x2WB_2D_fixed
1056     };
1057     return SelectVST(Node, 2, true, Opcodes);
1058   }
1059   case AArch64ISD::NEON_ST1x3_UPD: {
1060     static const uint16_t Opcodes[] = {
1061       AArch64::ST1x3WB_8B_fixed,  AArch64::ST1x3WB_4H_fixed,
1062       AArch64::ST1x3WB_2S_fixed,  AArch64::ST1x3WB_1D_fixed,
1063       AArch64::ST1x3WB_16B_fixed, AArch64::ST1x3WB_8H_fixed,
1064       AArch64::ST1x3WB_4S_fixed,  AArch64::ST1x3WB_2D_fixed
1065     };
1066     return SelectVST(Node, 3, true, Opcodes);
1067   }
1068   case AArch64ISD::NEON_ST1x4_UPD: {
1069     static const uint16_t Opcodes[] = {
1070       AArch64::ST1x4WB_8B_fixed,  AArch64::ST1x4WB_4H_fixed,
1071       AArch64::ST1x4WB_2S_fixed,  AArch64::ST1x4WB_1D_fixed,
1072       AArch64::ST1x4WB_16B_fixed, AArch64::ST1x4WB_8H_fixed,
1073       AArch64::ST1x4WB_4S_fixed,  AArch64::ST1x4WB_2D_fixed
1074     };
1075     return SelectVST(Node, 4, true, Opcodes);
1076   }
1077   case ISD::INTRINSIC_WO_CHAIN: {
1078     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
1079     bool IsExt = false;
1080     switch (IntNo) {
1081       default:
1082         break;
1083       case Intrinsic::aarch64_neon_vtbx1:
1084         IsExt = true;
1085       case Intrinsic::aarch64_neon_vtbl1:
1086         return SelectVTBL(Node, 1, IsExt);
1087       case Intrinsic::aarch64_neon_vtbx2:
1088         IsExt = true;
1089       case Intrinsic::aarch64_neon_vtbl2:
1090         return SelectVTBL(Node, 2, IsExt);
1091       case Intrinsic::aarch64_neon_vtbx3:
1092         IsExt = true;
1093       case Intrinsic::aarch64_neon_vtbl3:
1094         return SelectVTBL(Node, 3, IsExt);
1095       case Intrinsic::aarch64_neon_vtbx4:
1096         IsExt = true;
1097       case Intrinsic::aarch64_neon_vtbl4:
1098         return SelectVTBL(Node, 4, IsExt);
1099     }
1100     break;
1101   }
1102   case ISD::INTRINSIC_VOID:
1103   case ISD::INTRINSIC_W_CHAIN: {
1104     unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
1105     switch (IntNo) {
1106     default:
1107       break;
1108
1109     case Intrinsic::arm_neon_vld1: {
1110       static const uint16_t Opcodes[] = { AArch64::LD1_8B,  AArch64::LD1_4H,
1111                                           AArch64::LD1_2S,  AArch64::LD1_1D,
1112                                           AArch64::LD1_16B, AArch64::LD1_8H,
1113                                           AArch64::LD1_4S,  AArch64::LD1_2D };
1114       return SelectVLD(Node, 1, false, Opcodes);
1115     }
1116     case Intrinsic::arm_neon_vld2: {
1117       static const uint16_t Opcodes[] = { AArch64::LD2_8B,  AArch64::LD2_4H,
1118                                           AArch64::LD2_2S,  AArch64::LD1x2_1D,
1119                                           AArch64::LD2_16B, AArch64::LD2_8H,
1120                                           AArch64::LD2_4S,  AArch64::LD2_2D };
1121       return SelectVLD(Node, 2, false, Opcodes);
1122     }
1123     case Intrinsic::arm_neon_vld3: {
1124       static const uint16_t Opcodes[] = { AArch64::LD3_8B,  AArch64::LD3_4H,
1125                                           AArch64::LD3_2S,  AArch64::LD1x3_1D,
1126                                           AArch64::LD3_16B, AArch64::LD3_8H,
1127                                           AArch64::LD3_4S,  AArch64::LD3_2D };
1128       return SelectVLD(Node, 3, false, Opcodes);
1129     }
1130     case Intrinsic::arm_neon_vld4: {
1131       static const uint16_t Opcodes[] = { AArch64::LD4_8B,  AArch64::LD4_4H,
1132                                           AArch64::LD4_2S,  AArch64::LD1x4_1D,
1133                                           AArch64::LD4_16B, AArch64::LD4_8H,
1134                                           AArch64::LD4_4S,  AArch64::LD4_2D };
1135       return SelectVLD(Node, 4, false, Opcodes);
1136     }
1137     case Intrinsic::aarch64_neon_vld1x2: {
1138       static const uint16_t Opcodes[] = {
1139         AArch64::LD1x2_8B, AArch64::LD1x2_4H,  AArch64::LD1x2_2S,
1140         AArch64::LD1x2_1D, AArch64::LD1x2_16B, AArch64::LD1x2_8H,
1141         AArch64::LD1x2_4S, AArch64::LD1x2_2D
1142       };
1143       return SelectVLD(Node, 2, false, Opcodes);
1144     }
1145     case Intrinsic::aarch64_neon_vld1x3: {
1146       static const uint16_t Opcodes[] = {
1147         AArch64::LD1x3_8B, AArch64::LD1x3_4H,  AArch64::LD1x3_2S,
1148         AArch64::LD1x3_1D, AArch64::LD1x3_16B, AArch64::LD1x3_8H,
1149         AArch64::LD1x3_4S, AArch64::LD1x3_2D
1150       };
1151       return SelectVLD(Node, 3, false, Opcodes);
1152     }
1153     case Intrinsic::aarch64_neon_vld1x4: {
1154       static const uint16_t Opcodes[] = {
1155         AArch64::LD1x4_8B, AArch64::LD1x4_4H,  AArch64::LD1x4_2S,
1156         AArch64::LD1x4_1D, AArch64::LD1x4_16B, AArch64::LD1x4_8H,
1157         AArch64::LD1x4_4S, AArch64::LD1x4_2D
1158       };
1159       return SelectVLD(Node, 4, false, Opcodes);
1160     }
1161     case Intrinsic::arm_neon_vst1: {
1162       static const uint16_t Opcodes[] = { AArch64::ST1_8B,  AArch64::ST1_4H,
1163                                           AArch64::ST1_2S,  AArch64::ST1_1D,
1164                                           AArch64::ST1_16B, AArch64::ST1_8H,
1165                                           AArch64::ST1_4S,  AArch64::ST1_2D };
1166       return SelectVST(Node, 1, false, Opcodes);
1167     }
1168     case Intrinsic::arm_neon_vst2: {
1169       static const uint16_t Opcodes[] = { AArch64::ST2_8B,  AArch64::ST2_4H,
1170                                           AArch64::ST2_2S,  AArch64::ST1x2_1D,
1171                                           AArch64::ST2_16B, AArch64::ST2_8H,
1172                                           AArch64::ST2_4S,  AArch64::ST2_2D };
1173       return SelectVST(Node, 2, false, Opcodes);
1174     }
1175     case Intrinsic::arm_neon_vst3: {
1176       static const uint16_t Opcodes[] = { AArch64::ST3_8B,  AArch64::ST3_4H,
1177                                           AArch64::ST3_2S,  AArch64::ST1x3_1D,
1178                                           AArch64::ST3_16B, AArch64::ST3_8H,
1179                                           AArch64::ST3_4S,  AArch64::ST3_2D };
1180       return SelectVST(Node, 3, false, Opcodes);
1181     }
1182     case Intrinsic::arm_neon_vst4: {
1183       static const uint16_t Opcodes[] = { AArch64::ST4_8B,  AArch64::ST4_4H,
1184                                           AArch64::ST4_2S,  AArch64::ST1x4_1D,
1185                                           AArch64::ST4_16B, AArch64::ST4_8H,
1186                                           AArch64::ST4_4S,  AArch64::ST4_2D };
1187       return SelectVST(Node, 4, false, Opcodes);
1188     }
1189     case Intrinsic::aarch64_neon_vst1x2: {
1190       static const uint16_t Opcodes[] = {
1191         AArch64::ST1x2_8B, AArch64::ST1x2_4H,  AArch64::ST1x2_2S,
1192         AArch64::ST1x2_1D, AArch64::ST1x2_16B, AArch64::ST1x2_8H,
1193         AArch64::ST1x2_4S, AArch64::ST1x2_2D
1194       };
1195       return SelectVST(Node, 2, false, Opcodes);
1196     }
1197     case Intrinsic::aarch64_neon_vst1x3: {
1198       static const uint16_t Opcodes[] = {
1199         AArch64::ST1x3_8B, AArch64::ST1x3_4H,  AArch64::ST1x3_2S,
1200         AArch64::ST1x3_1D, AArch64::ST1x3_16B, AArch64::ST1x3_8H,
1201         AArch64::ST1x3_4S, AArch64::ST1x3_2D
1202       };
1203       return SelectVST(Node, 3, false, Opcodes);
1204     }
1205     case Intrinsic::aarch64_neon_vst1x4: {
1206       static const uint16_t Opcodes[] = {
1207         AArch64::ST1x4_8B, AArch64::ST1x4_4H,  AArch64::ST1x4_2S,
1208         AArch64::ST1x4_1D, AArch64::ST1x4_16B, AArch64::ST1x4_8H,
1209         AArch64::ST1x4_4S, AArch64::ST1x4_2D
1210       };
1211       return SelectVST(Node, 4, false, Opcodes);
1212     }
1213     }
1214     break;
1215   }
1216   default:
1217     break; // Let generic code handle it
1218   }
1219
1220   SDNode *ResNode = SelectCode(Node);
1221
1222   DEBUG(dbgs() << "=> ";
1223         if (ResNode == NULL || ResNode == Node)
1224           Node->dump(CurDAG);
1225         else
1226           ResNode->dump(CurDAG);
1227         dbgs() << "\n");
1228
1229   return ResNode;
1230 }
1231
1232 /// This pass converts a legalized DAG into a AArch64-specific DAG, ready for
1233 /// instruction scheduling.
1234 FunctionPass *llvm::createAArch64ISelDAG(AArch64TargetMachine &TM,
1235                                          CodeGenOpt::Level OptLevel) {
1236   return new AArch64DAGToDAGISel(TM, OptLevel);
1237 }