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