OSDN Git Service

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