blob: 5a11fe66b75eaa4a8d6231686cda0d23f3eb3c5e [file] [log] [blame]
Tim Northover3b0846e2014-05-24 12:50:23 +00001//===-- 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
26using 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///
34namespace {
35
36class AArch64DAGToDAGISel : public SelectionDAGISel {
Tim Northover3b0846e2014-05-24 12:50:23 +000037
38 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
39 /// make the right decision when generating code for different targets.
40 const AArch64Subtarget *Subtarget;
41
42 bool ForCodeSize;
43
44public:
45 explicit AArch64DAGToDAGISel(AArch64TargetMachine &tm,
46 CodeGenOpt::Level OptLevel)
Stephen Canon8216d882015-09-22 11:43:17 +000047 : SelectionDAGISel(tm, OptLevel), Subtarget(nullptr),
Tim Northover3b0846e2014-05-24 12:50:23 +000048 ForCodeSize(false) {}
49
50 const char *getPassName() const override {
51 return "AArch64 Instruction Selection";
52 }
53
54 bool runOnMachineFunction(MachineFunction &MF) override {
Sanjay Patel924879a2015-08-04 15:49:57 +000055 ForCodeSize = MF.getFunction()->optForSize();
Eric Christopher1e513342015-01-30 23:46:40 +000056 Subtarget = &MF.getSubtarget<AArch64Subtarget>();
Tim Northover3b0846e2014-05-24 12:50:23 +000057 return SelectionDAGISel::runOnMachineFunction(MF);
58 }
59
Justin Bogner283e3bd2016-05-12 23:10:30 +000060 void Select(SDNode *Node) override;
Tim Northover3b0846e2014-05-24 12:50:23 +000061
62 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
63 /// inline asm expressions.
64 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +000065 unsigned ConstraintID,
Tim Northover3b0846e2014-05-24 12:50:23 +000066 std::vector<SDValue> &OutOps) override;
67
Justin Bogner283e3bd2016-05-12 23:10:30 +000068 bool tryMLAV64LaneV128(SDNode *N);
69 bool tryMULLV64LaneV128(unsigned IntNo, SDNode *N);
Tim Northover3b0846e2014-05-24 12:50:23 +000070 bool SelectArithExtendedRegister(SDValue N, SDValue &Reg, SDValue &Shift);
71 bool SelectArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
72 bool SelectNegArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
73 bool SelectArithShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
74 return SelectShiftedRegister(N, false, Reg, Shift);
75 }
76 bool SelectLogicalShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
77 return SelectShiftedRegister(N, true, Reg, Shift);
78 }
Ahmed Bougachab8886b52015-09-10 01:42:28 +000079 bool SelectAddrModeIndexed7S8(SDValue N, SDValue &Base, SDValue &OffImm) {
80 return SelectAddrModeIndexed7S(N, 1, Base, OffImm);
81 }
82 bool SelectAddrModeIndexed7S16(SDValue N, SDValue &Base, SDValue &OffImm) {
83 return SelectAddrModeIndexed7S(N, 2, Base, OffImm);
84 }
85 bool SelectAddrModeIndexed7S32(SDValue N, SDValue &Base, SDValue &OffImm) {
86 return SelectAddrModeIndexed7S(N, 4, Base, OffImm);
87 }
88 bool SelectAddrModeIndexed7S64(SDValue N, SDValue &Base, SDValue &OffImm) {
89 return SelectAddrModeIndexed7S(N, 8, Base, OffImm);
90 }
91 bool SelectAddrModeIndexed7S128(SDValue N, SDValue &Base, SDValue &OffImm) {
92 return SelectAddrModeIndexed7S(N, 16, Base, OffImm);
93 }
Tim Northover3b0846e2014-05-24 12:50:23 +000094 bool SelectAddrModeIndexed8(SDValue N, SDValue &Base, SDValue &OffImm) {
95 return SelectAddrModeIndexed(N, 1, Base, OffImm);
96 }
97 bool SelectAddrModeIndexed16(SDValue N, SDValue &Base, SDValue &OffImm) {
98 return SelectAddrModeIndexed(N, 2, Base, OffImm);
99 }
100 bool SelectAddrModeIndexed32(SDValue N, SDValue &Base, SDValue &OffImm) {
101 return SelectAddrModeIndexed(N, 4, Base, OffImm);
102 }
103 bool SelectAddrModeIndexed64(SDValue N, SDValue &Base, SDValue &OffImm) {
104 return SelectAddrModeIndexed(N, 8, Base, OffImm);
105 }
106 bool SelectAddrModeIndexed128(SDValue N, SDValue &Base, SDValue &OffImm) {
107 return SelectAddrModeIndexed(N, 16, Base, OffImm);
108 }
109 bool SelectAddrModeUnscaled8(SDValue N, SDValue &Base, SDValue &OffImm) {
110 return SelectAddrModeUnscaled(N, 1, Base, OffImm);
111 }
112 bool SelectAddrModeUnscaled16(SDValue N, SDValue &Base, SDValue &OffImm) {
113 return SelectAddrModeUnscaled(N, 2, Base, OffImm);
114 }
115 bool SelectAddrModeUnscaled32(SDValue N, SDValue &Base, SDValue &OffImm) {
116 return SelectAddrModeUnscaled(N, 4, Base, OffImm);
117 }
118 bool SelectAddrModeUnscaled64(SDValue N, SDValue &Base, SDValue &OffImm) {
119 return SelectAddrModeUnscaled(N, 8, Base, OffImm);
120 }
121 bool SelectAddrModeUnscaled128(SDValue N, SDValue &Base, SDValue &OffImm) {
122 return SelectAddrModeUnscaled(N, 16, Base, OffImm);
123 }
124
125 template<int Width>
126 bool SelectAddrModeWRO(SDValue N, SDValue &Base, SDValue &Offset,
127 SDValue &SignExtend, SDValue &DoShift) {
128 return SelectAddrModeWRO(N, Width / 8, Base, Offset, SignExtend, DoShift);
129 }
130
131 template<int Width>
132 bool SelectAddrModeXRO(SDValue N, SDValue &Base, SDValue &Offset,
133 SDValue &SignExtend, SDValue &DoShift) {
134 return SelectAddrModeXRO(N, Width / 8, Base, Offset, SignExtend, DoShift);
135 }
136
137
138 /// Form sequences of consecutive 64/128-bit registers for use in NEON
139 /// instructions making use of a vector-list (e.g. ldN, tbl). Vecs must have
140 /// between 1 and 4 elements. If it contains a single element that is returned
141 /// unchanged; otherwise a REG_SEQUENCE value is returned.
142 SDValue createDTuple(ArrayRef<SDValue> Vecs);
143 SDValue createQTuple(ArrayRef<SDValue> Vecs);
144
145 /// Generic helper for the createDTuple/createQTuple
146 /// functions. Those should almost always be called instead.
Benjamin Kramerea68a942015-02-19 15:26:17 +0000147 SDValue createTuple(ArrayRef<SDValue> Vecs, const unsigned RegClassIDs[],
148 const unsigned SubRegs[]);
Tim Northover3b0846e2014-05-24 12:50:23 +0000149
Justin Bogner283e3bd2016-05-12 23:10:30 +0000150 void SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc, bool isExt);
Tim Northover3b0846e2014-05-24 12:50:23 +0000151
Justin Bogner283e3bd2016-05-12 23:10:30 +0000152 bool tryIndexedLoad(SDNode *N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000153
Justin Bogner283e3bd2016-05-12 23:10:30 +0000154 void SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
Tim Northover3b0846e2014-05-24 12:50:23 +0000155 unsigned SubRegIdx);
Justin Bogner283e3bd2016-05-12 23:10:30 +0000156 void SelectPostLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
Tim Northover3b0846e2014-05-24 12:50:23 +0000157 unsigned SubRegIdx);
Justin Bogner283e3bd2016-05-12 23:10:30 +0000158 void SelectLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
159 void SelectPostLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
Tim Northover3b0846e2014-05-24 12:50:23 +0000160
Justin Bogner283e3bd2016-05-12 23:10:30 +0000161 void SelectStore(SDNode *N, unsigned NumVecs, unsigned Opc);
162 void SelectPostStore(SDNode *N, unsigned NumVecs, unsigned Opc);
163 void SelectStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
164 void SelectPostStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
Tim Northover3b0846e2014-05-24 12:50:23 +0000165
Justin Bogner283e3bd2016-05-12 23:10:30 +0000166 bool tryBitfieldExtractOp(SDNode *N);
Chad Rosierbe879ea2016-06-03 20:05:49 +0000167 bool tryBitfieldExtractOpFromSExt(SDNode *N);
Justin Bogner283e3bd2016-05-12 23:10:30 +0000168 bool tryBitfieldInsertOp(SDNode *N);
169 bool tryBitfieldInsertInZeroOp(SDNode *N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000170
Justin Bogner283e3bd2016-05-12 23:10:30 +0000171 bool tryReadRegister(SDNode *N);
172 bool tryWriteRegister(SDNode *N);
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000173
Tim Northover3b0846e2014-05-24 12:50:23 +0000174// Include the pieces autogenerated from the target description.
175#include "AArch64GenDAGISel.inc"
176
177private:
178 bool SelectShiftedRegister(SDValue N, bool AllowROR, SDValue &Reg,
179 SDValue &Shift);
Ahmed Bougachab8886b52015-09-10 01:42:28 +0000180 bool SelectAddrModeIndexed7S(SDValue N, unsigned Size, SDValue &Base,
181 SDValue &OffImm);
Tim Northover3b0846e2014-05-24 12:50:23 +0000182 bool SelectAddrModeIndexed(SDValue N, unsigned Size, SDValue &Base,
183 SDValue &OffImm);
184 bool SelectAddrModeUnscaled(SDValue N, unsigned Size, SDValue &Base,
185 SDValue &OffImm);
186 bool SelectAddrModeWRO(SDValue N, unsigned Size, SDValue &Base,
187 SDValue &Offset, SDValue &SignExtend,
188 SDValue &DoShift);
189 bool SelectAddrModeXRO(SDValue N, unsigned Size, SDValue &Base,
190 SDValue &Offset, SDValue &SignExtend,
191 SDValue &DoShift);
192 bool isWorthFolding(SDValue V) const;
193 bool SelectExtendedSHL(SDValue N, unsigned Size, bool WantExtend,
194 SDValue &Offset, SDValue &SignExtend);
195
196 template<unsigned RegWidth>
197 bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
198 return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
199 }
200
201 bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos, unsigned Width);
Tim Northovercdf15292016-04-14 17:03:29 +0000202
203 void SelectCMP_SWAP(SDNode *N);
204
Tim Northover3b0846e2014-05-24 12:50:23 +0000205};
206} // end anonymous namespace
207
208/// isIntImmediate - This method tests to see if the node is a constant
209/// operand. If so Imm will receive the 32-bit value.
210static bool isIntImmediate(const SDNode *N, uint64_t &Imm) {
211 if (const ConstantSDNode *C = dyn_cast<const ConstantSDNode>(N)) {
212 Imm = C->getZExtValue();
213 return true;
214 }
215 return false;
216}
217
218// isIntImmediate - This method tests to see if a constant operand.
219// If so Imm will receive the value.
220static bool isIntImmediate(SDValue N, uint64_t &Imm) {
221 return isIntImmediate(N.getNode(), Imm);
222}
223
224// isOpcWithIntImmediate - This method tests to see if the node is a specific
225// opcode and that it has a immediate integer right operand.
226// If so Imm will receive the 32 bit value.
227static bool isOpcWithIntImmediate(const SDNode *N, unsigned Opc,
228 uint64_t &Imm) {
229 return N->getOpcode() == Opc &&
230 isIntImmediate(N->getOperand(1).getNode(), Imm);
231}
232
233bool AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(
Daniel Sanders60f1db02015-03-13 12:45:09 +0000234 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
Daniel Sandersf731eee2015-03-23 11:33:15 +0000235 switch(ConstraintID) {
236 default:
237 llvm_unreachable("Unexpected asm memory constraint");
238 case InlineAsm::Constraint_i:
239 case InlineAsm::Constraint_m:
240 case InlineAsm::Constraint_Q:
241 // Require the address to be in a register. That is safe for all AArch64
242 // variants and it is hard to do anything much smarter without knowing
243 // how the operand is used.
244 OutOps.push_back(Op);
245 return false;
246 }
247 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000248}
249
250/// SelectArithImmed - Select an immediate value that can be represented as
251/// a 12-bit value shifted left by either 0 or 12. If so, return true with
252/// Val set to the 12-bit value and Shift set to the shifter operand.
253bool AArch64DAGToDAGISel::SelectArithImmed(SDValue N, SDValue &Val,
254 SDValue &Shift) {
255 // This function is called from the addsub_shifted_imm ComplexPattern,
256 // which lists [imm] as the list of opcode it's interested in, however
257 // we still need to check whether the operand is actually an immediate
258 // here because the ComplexPattern opcode list is only used in
259 // root-level opcode matching.
260 if (!isa<ConstantSDNode>(N.getNode()))
261 return false;
262
263 uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
264 unsigned ShiftAmt;
265
266 if (Immed >> 12 == 0) {
267 ShiftAmt = 0;
268 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
269 ShiftAmt = 12;
270 Immed = Immed >> 12;
271 } else
272 return false;
273
274 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000275 SDLoc dl(N);
276 Val = CurDAG->getTargetConstant(Immed, dl, MVT::i32);
277 Shift = CurDAG->getTargetConstant(ShVal, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000278 return true;
279}
280
281/// SelectNegArithImmed - As above, but negates the value before trying to
282/// select it.
283bool AArch64DAGToDAGISel::SelectNegArithImmed(SDValue N, SDValue &Val,
284 SDValue &Shift) {
285 // This function is called from the addsub_shifted_imm ComplexPattern,
286 // which lists [imm] as the list of opcode it's interested in, however
287 // we still need to check whether the operand is actually an immediate
288 // here because the ComplexPattern opcode list is only used in
289 // root-level opcode matching.
290 if (!isa<ConstantSDNode>(N.getNode()))
291 return false;
292
293 // The immediate operand must be a 24-bit zero-extended immediate.
294 uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
295
296 // This negation is almost always valid, but "cmp wN, #0" and "cmn wN, #0"
297 // have the opposite effect on the C flag, so this pattern mustn't match under
298 // those circumstances.
299 if (Immed == 0)
300 return false;
301
302 if (N.getValueType() == MVT::i32)
303 Immed = ~((uint32_t)Immed) + 1;
304 else
305 Immed = ~Immed + 1ULL;
306 if (Immed & 0xFFFFFFFFFF000000ULL)
307 return false;
308
309 Immed &= 0xFFFFFFULL;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000310 return SelectArithImmed(CurDAG->getConstant(Immed, SDLoc(N), MVT::i32), Val,
311 Shift);
Tim Northover3b0846e2014-05-24 12:50:23 +0000312}
313
314/// getShiftTypeForNode - Translate a shift node to the corresponding
315/// ShiftType value.
316static AArch64_AM::ShiftExtendType getShiftTypeForNode(SDValue N) {
317 switch (N.getOpcode()) {
318 default:
319 return AArch64_AM::InvalidShiftExtend;
320 case ISD::SHL:
321 return AArch64_AM::LSL;
322 case ISD::SRL:
323 return AArch64_AM::LSR;
324 case ISD::SRA:
325 return AArch64_AM::ASR;
326 case ISD::ROTR:
327 return AArch64_AM::ROR;
328 }
329}
330
Eric Christopher25dbdeb2015-03-07 01:39:09 +0000331/// \brief Determine whether it is worth to fold V into an extended register.
Tim Northover3b0846e2014-05-24 12:50:23 +0000332bool AArch64DAGToDAGISel::isWorthFolding(SDValue V) const {
Robin Morisset039781e2014-08-29 21:53:01 +0000333 // it hurts if the value is used at least twice, unless we are optimizing
Tim Northover3b0846e2014-05-24 12:50:23 +0000334 // for code size.
Eric Christopher114fa1c2016-02-29 22:50:49 +0000335 return ForCodeSize || V.hasOneUse();
Tim Northover3b0846e2014-05-24 12:50:23 +0000336}
337
338/// SelectShiftedRegister - Select a "shifted register" operand. If the value
339/// is not shifted, set the Shift operand to default of "LSL 0". The logical
340/// instructions allow the shifted register to be rotated, but the arithmetic
341/// instructions do not. The AllowROR parameter specifies whether ROR is
342/// supported.
343bool AArch64DAGToDAGISel::SelectShiftedRegister(SDValue N, bool AllowROR,
344 SDValue &Reg, SDValue &Shift) {
345 AArch64_AM::ShiftExtendType ShType = getShiftTypeForNode(N);
346 if (ShType == AArch64_AM::InvalidShiftExtend)
347 return false;
348 if (!AllowROR && ShType == AArch64_AM::ROR)
349 return false;
350
351 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
352 unsigned BitSize = N.getValueType().getSizeInBits();
353 unsigned Val = RHS->getZExtValue() & (BitSize - 1);
354 unsigned ShVal = AArch64_AM::getShifterImm(ShType, Val);
355
356 Reg = N.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000357 Shift = CurDAG->getTargetConstant(ShVal, SDLoc(N), MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000358 return isWorthFolding(N);
359 }
360
361 return false;
362}
363
364/// getExtendTypeForNode - Translate an extend node to the corresponding
365/// ExtendType value.
366static AArch64_AM::ShiftExtendType
367getExtendTypeForNode(SDValue N, bool IsLoadStore = false) {
368 if (N.getOpcode() == ISD::SIGN_EXTEND ||
369 N.getOpcode() == ISD::SIGN_EXTEND_INREG) {
370 EVT SrcVT;
371 if (N.getOpcode() == ISD::SIGN_EXTEND_INREG)
372 SrcVT = cast<VTSDNode>(N.getOperand(1))->getVT();
373 else
374 SrcVT = N.getOperand(0).getValueType();
375
376 if (!IsLoadStore && SrcVT == MVT::i8)
377 return AArch64_AM::SXTB;
378 else if (!IsLoadStore && SrcVT == MVT::i16)
379 return AArch64_AM::SXTH;
380 else if (SrcVT == MVT::i32)
381 return AArch64_AM::SXTW;
382 assert(SrcVT != MVT::i64 && "extend from 64-bits?");
383
384 return AArch64_AM::InvalidShiftExtend;
385 } else if (N.getOpcode() == ISD::ZERO_EXTEND ||
386 N.getOpcode() == ISD::ANY_EXTEND) {
387 EVT SrcVT = N.getOperand(0).getValueType();
388 if (!IsLoadStore && SrcVT == MVT::i8)
389 return AArch64_AM::UXTB;
390 else if (!IsLoadStore && SrcVT == MVT::i16)
391 return AArch64_AM::UXTH;
392 else if (SrcVT == MVT::i32)
393 return AArch64_AM::UXTW;
394 assert(SrcVT != MVT::i64 && "extend from 64-bits?");
395
396 return AArch64_AM::InvalidShiftExtend;
397 } else if (N.getOpcode() == ISD::AND) {
398 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
399 if (!CSD)
400 return AArch64_AM::InvalidShiftExtend;
401 uint64_t AndMask = CSD->getZExtValue();
402
403 switch (AndMask) {
404 default:
405 return AArch64_AM::InvalidShiftExtend;
406 case 0xFF:
407 return !IsLoadStore ? AArch64_AM::UXTB : AArch64_AM::InvalidShiftExtend;
408 case 0xFFFF:
409 return !IsLoadStore ? AArch64_AM::UXTH : AArch64_AM::InvalidShiftExtend;
410 case 0xFFFFFFFF:
411 return AArch64_AM::UXTW;
412 }
413 }
414
415 return AArch64_AM::InvalidShiftExtend;
416}
417
418// Helper for SelectMLAV64LaneV128 - Recognize high lane extracts.
419static bool checkHighLaneIndex(SDNode *DL, SDValue &LaneOp, int &LaneIdx) {
420 if (DL->getOpcode() != AArch64ISD::DUPLANE16 &&
421 DL->getOpcode() != AArch64ISD::DUPLANE32)
422 return false;
423
424 SDValue SV = DL->getOperand(0);
425 if (SV.getOpcode() != ISD::INSERT_SUBVECTOR)
426 return false;
427
428 SDValue EV = SV.getOperand(1);
429 if (EV.getOpcode() != ISD::EXTRACT_SUBVECTOR)
430 return false;
431
432 ConstantSDNode *DLidx = cast<ConstantSDNode>(DL->getOperand(1).getNode());
433 ConstantSDNode *EVidx = cast<ConstantSDNode>(EV.getOperand(1).getNode());
434 LaneIdx = DLidx->getSExtValue() + EVidx->getSExtValue();
435 LaneOp = EV.getOperand(0);
436
437 return true;
438}
439
Chad Rosier6c1f0932015-09-17 13:10:27 +0000440// Helper for SelectOpcV64LaneV128 - Recognize operations where one operand is a
Tim Northover3b0846e2014-05-24 12:50:23 +0000441// high lane extract.
442static bool checkV64LaneV128(SDValue Op0, SDValue Op1, SDValue &StdOp,
443 SDValue &LaneOp, int &LaneIdx) {
444
445 if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx)) {
446 std::swap(Op0, Op1);
447 if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx))
448 return false;
449 }
450 StdOp = Op1;
451 return true;
452}
453
454/// SelectMLAV64LaneV128 - AArch64 supports vector MLAs where one multiplicand
455/// is a lane in the upper half of a 128-bit vector. Recognize and select this
456/// so that we don't emit unnecessary lane extracts.
Justin Bogner283e3bd2016-05-12 23:10:30 +0000457bool AArch64DAGToDAGISel::tryMLAV64LaneV128(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000458 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000459 SDValue Op0 = N->getOperand(0);
460 SDValue Op1 = N->getOperand(1);
461 SDValue MLAOp1; // Will hold ordinary multiplicand for MLA.
462 SDValue MLAOp2; // Will hold lane-accessed multiplicand for MLA.
463 int LaneIdx = -1; // Will hold the lane index.
464
465 if (Op1.getOpcode() != ISD::MUL ||
466 !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
467 LaneIdx)) {
468 std::swap(Op0, Op1);
469 if (Op1.getOpcode() != ISD::MUL ||
470 !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
471 LaneIdx))
Justin Bogner283e3bd2016-05-12 23:10:30 +0000472 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000473 }
474
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000475 SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000476
477 SDValue Ops[] = { Op0, MLAOp1, MLAOp2, LaneIdxVal };
478
479 unsigned MLAOpc = ~0U;
480
481 switch (N->getSimpleValueType(0).SimpleTy) {
482 default:
483 llvm_unreachable("Unrecognized MLA.");
484 case MVT::v4i16:
485 MLAOpc = AArch64::MLAv4i16_indexed;
486 break;
487 case MVT::v8i16:
488 MLAOpc = AArch64::MLAv8i16_indexed;
489 break;
490 case MVT::v2i32:
491 MLAOpc = AArch64::MLAv2i32_indexed;
492 break;
493 case MVT::v4i32:
494 MLAOpc = AArch64::MLAv4i32_indexed;
495 break;
496 }
497
Justin Bogner283e3bd2016-05-12 23:10:30 +0000498 ReplaceNode(N, CurDAG->getMachineNode(MLAOpc, dl, N->getValueType(0), Ops));
499 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000500}
501
Justin Bogner283e3bd2016-05-12 23:10:30 +0000502bool AArch64DAGToDAGISel::tryMULLV64LaneV128(unsigned IntNo, SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000503 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000504 SDValue SMULLOp0;
505 SDValue SMULLOp1;
506 int LaneIdx;
507
508 if (!checkV64LaneV128(N->getOperand(1), N->getOperand(2), SMULLOp0, SMULLOp1,
509 LaneIdx))
Justin Bogner283e3bd2016-05-12 23:10:30 +0000510 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000511
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000512 SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000513
514 SDValue Ops[] = { SMULLOp0, SMULLOp1, LaneIdxVal };
515
516 unsigned SMULLOpc = ~0U;
517
518 if (IntNo == Intrinsic::aarch64_neon_smull) {
519 switch (N->getSimpleValueType(0).SimpleTy) {
520 default:
521 llvm_unreachable("Unrecognized SMULL.");
522 case MVT::v4i32:
523 SMULLOpc = AArch64::SMULLv4i16_indexed;
524 break;
525 case MVT::v2i64:
526 SMULLOpc = AArch64::SMULLv2i32_indexed;
527 break;
528 }
529 } else if (IntNo == Intrinsic::aarch64_neon_umull) {
530 switch (N->getSimpleValueType(0).SimpleTy) {
531 default:
532 llvm_unreachable("Unrecognized SMULL.");
533 case MVT::v4i32:
534 SMULLOpc = AArch64::UMULLv4i16_indexed;
535 break;
536 case MVT::v2i64:
537 SMULLOpc = AArch64::UMULLv2i32_indexed;
538 break;
539 }
540 } else
541 llvm_unreachable("Unrecognized intrinsic.");
542
Justin Bogner283e3bd2016-05-12 23:10:30 +0000543 ReplaceNode(N, CurDAG->getMachineNode(SMULLOpc, dl, N->getValueType(0), Ops));
544 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000545}
546
547/// Instructions that accept extend modifiers like UXTW expect the register
548/// being extended to be a GPR32, but the incoming DAG might be acting on a
549/// GPR64 (either via SEXT_INREG or AND). Extract the appropriate low bits if
550/// this is the case.
551static SDValue narrowIfNeeded(SelectionDAG *CurDAG, SDValue N) {
552 if (N.getValueType() == MVT::i32)
553 return N;
554
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000555 SDLoc dl(N);
556 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000557 MachineSDNode *Node = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000558 dl, MVT::i32, N, SubReg);
Tim Northover3b0846e2014-05-24 12:50:23 +0000559 return SDValue(Node, 0);
560}
561
562
563/// SelectArithExtendedRegister - Select a "extended register" operand. This
564/// operand folds in an extend followed by an optional left shift.
565bool AArch64DAGToDAGISel::SelectArithExtendedRegister(SDValue N, SDValue &Reg,
566 SDValue &Shift) {
567 unsigned ShiftVal = 0;
568 AArch64_AM::ShiftExtendType Ext;
569
570 if (N.getOpcode() == ISD::SHL) {
571 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
572 if (!CSD)
573 return false;
574 ShiftVal = CSD->getZExtValue();
575 if (ShiftVal > 4)
576 return false;
577
578 Ext = getExtendTypeForNode(N.getOperand(0));
579 if (Ext == AArch64_AM::InvalidShiftExtend)
580 return false;
581
582 Reg = N.getOperand(0).getOperand(0);
583 } else {
584 Ext = getExtendTypeForNode(N);
585 if (Ext == AArch64_AM::InvalidShiftExtend)
586 return false;
587
588 Reg = N.getOperand(0);
589 }
590
591 // AArch64 mandates that the RHS of the operation must use the smallest
Chad Rosier6c1f0932015-09-17 13:10:27 +0000592 // register class that could contain the size being extended from. Thus,
Tim Northover3b0846e2014-05-24 12:50:23 +0000593 // if we're folding a (sext i8), we need the RHS to be a GPR32, even though
594 // there might not be an actual 32-bit value in the program. We can
595 // (harmlessly) synthesize one by injected an EXTRACT_SUBREG here.
596 assert(Ext != AArch64_AM::UXTX && Ext != AArch64_AM::SXTX);
597 Reg = narrowIfNeeded(CurDAG, Reg);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000598 Shift = CurDAG->getTargetConstant(getArithExtendImm(Ext, ShiftVal), SDLoc(N),
599 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000600 return isWorthFolding(N);
601}
602
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000603/// If there's a use of this ADDlow that's not itself a load/store then we'll
604/// need to create a real ADD instruction from it anyway and there's no point in
605/// folding it into the mem op. Theoretically, it shouldn't matter, but there's
606/// a single pseudo-instruction for an ADRP/ADD pair so over-aggressive folding
Chad Rosier6c1f0932015-09-17 13:10:27 +0000607/// leads to duplicated ADRP instructions.
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000608static bool isWorthFoldingADDlow(SDValue N) {
609 for (auto Use : N->uses()) {
610 if (Use->getOpcode() != ISD::LOAD && Use->getOpcode() != ISD::STORE &&
611 Use->getOpcode() != ISD::ATOMIC_LOAD &&
612 Use->getOpcode() != ISD::ATOMIC_STORE)
613 return false;
614
615 // ldar and stlr have much more restrictive addressing modes (just a
616 // register).
JF Bastien800f87a2016-04-06 21:19:33 +0000617 if (isStrongerThanMonotonic(cast<MemSDNode>(Use)->getOrdering()))
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000618 return false;
619 }
620
621 return true;
622}
623
Ahmed Bougachab8886b52015-09-10 01:42:28 +0000624/// SelectAddrModeIndexed7S - Select a "register plus scaled signed 7-bit
625/// immediate" address. The "Size" argument is the size in bytes of the memory
626/// reference, which determines the scale.
627bool AArch64DAGToDAGISel::SelectAddrModeIndexed7S(SDValue N, unsigned Size,
628 SDValue &Base,
629 SDValue &OffImm) {
630 SDLoc dl(N);
Ahmed Bougacha05541452015-09-10 01:54:43 +0000631 const DataLayout &DL = CurDAG->getDataLayout();
632 const TargetLowering *TLI = getTargetLowering();
633 if (N.getOpcode() == ISD::FrameIndex) {
634 int FI = cast<FrameIndexSDNode>(N)->getIndex();
635 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
636 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
637 return true;
638 }
639
Ahmed Bougachac0ac38d2015-09-10 01:48:29 +0000640 // As opposed to the (12-bit) Indexed addressing mode below, the 7-bit signed
641 // selected here doesn't support labels/immediates, only base+offset.
642
643 if (CurDAG->isBaseWithConstantOffset(N)) {
644 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
645 int64_t RHSC = RHS->getSExtValue();
646 unsigned Scale = Log2_32(Size);
Steven Wue3b1f2b2015-09-10 16:32:28 +0000647 if ((RHSC & (Size - 1)) == 0 && RHSC >= -(0x40 << Scale) &&
Ahmed Bougachac0ac38d2015-09-10 01:48:29 +0000648 RHSC < (0x40 << Scale)) {
649 Base = N.getOperand(0);
Ahmed Bougacha05541452015-09-10 01:54:43 +0000650 if (Base.getOpcode() == ISD::FrameIndex) {
651 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
652 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
653 }
Ahmed Bougachac0ac38d2015-09-10 01:48:29 +0000654 OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
655 return true;
656 }
657 }
658 }
659
Ahmed Bougachab8886b52015-09-10 01:42:28 +0000660 // Base only. The address will be materialized into a register before
661 // the memory is accessed.
662 // add x0, Xbase, #offset
663 // stp x1, x2, [x0]
664 Base = N;
665 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
666 return true;
667}
668
Tim Northover3b0846e2014-05-24 12:50:23 +0000669/// SelectAddrModeIndexed - Select a "register plus scaled unsigned 12-bit
670/// immediate" address. The "Size" argument is the size in bytes of the memory
671/// reference, which determines the scale.
672bool AArch64DAGToDAGISel::SelectAddrModeIndexed(SDValue N, unsigned Size,
673 SDValue &Base, SDValue &OffImm) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000674 SDLoc dl(N);
Mehdi Amini44ede332015-07-09 02:09:04 +0000675 const DataLayout &DL = CurDAG->getDataLayout();
Tim Northover3b0846e2014-05-24 12:50:23 +0000676 const TargetLowering *TLI = getTargetLowering();
677 if (N.getOpcode() == ISD::FrameIndex) {
678 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000679 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000680 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000681 return true;
682 }
683
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000684 if (N.getOpcode() == AArch64ISD::ADDlow && isWorthFoldingADDlow(N)) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000685 GlobalAddressSDNode *GAN =
686 dyn_cast<GlobalAddressSDNode>(N.getOperand(1).getNode());
687 Base = N.getOperand(0);
688 OffImm = N.getOperand(1);
689 if (!GAN)
690 return true;
691
692 const GlobalValue *GV = GAN->getGlobal();
693 unsigned Alignment = GV->getAlignment();
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000694 Type *Ty = GV->getValueType();
Tim Northover4a8ac262014-12-02 23:53:43 +0000695 if (Alignment == 0 && Ty->isSized())
Mehdi Amini44ede332015-07-09 02:09:04 +0000696 Alignment = DL.getABITypeAlignment(Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000697
698 if (Alignment >= Size)
699 return true;
700 }
701
702 if (CurDAG->isBaseWithConstantOffset(N)) {
703 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
704 int64_t RHSC = (int64_t)RHS->getZExtValue();
705 unsigned Scale = Log2_32(Size);
706 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
707 Base = N.getOperand(0);
708 if (Base.getOpcode() == ISD::FrameIndex) {
709 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000710 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
Tim Northover3b0846e2014-05-24 12:50:23 +0000711 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000712 OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000713 return true;
714 }
715 }
716 }
717
718 // Before falling back to our general case, check if the unscaled
719 // instructions can handle this. If so, that's preferable.
720 if (SelectAddrModeUnscaled(N, Size, Base, OffImm))
721 return false;
722
723 // Base only. The address will be materialized into a register before
724 // the memory is accessed.
725 // add x0, Xbase, #offset
726 // ldr x0, [x0]
727 Base = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000728 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000729 return true;
730}
731
732/// SelectAddrModeUnscaled - Select a "register plus unscaled signed 9-bit
733/// immediate" address. This should only match when there is an offset that
734/// is not valid for a scaled immediate addressing mode. The "Size" argument
735/// is the size in bytes of the memory reference, which is needed here to know
736/// what is valid for a scaled immediate.
737bool AArch64DAGToDAGISel::SelectAddrModeUnscaled(SDValue N, unsigned Size,
738 SDValue &Base,
739 SDValue &OffImm) {
740 if (!CurDAG->isBaseWithConstantOffset(N))
741 return false;
742 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
743 int64_t RHSC = RHS->getSExtValue();
744 // If the offset is valid as a scaled immediate, don't match here.
745 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 &&
746 RHSC < (0x1000 << Log2_32(Size)))
747 return false;
748 if (RHSC >= -256 && RHSC < 256) {
749 Base = N.getOperand(0);
750 if (Base.getOpcode() == ISD::FrameIndex) {
751 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
752 const TargetLowering *TLI = getTargetLowering();
Mehdi Amini44ede332015-07-09 02:09:04 +0000753 Base = CurDAG->getTargetFrameIndex(
754 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Tim Northover3b0846e2014-05-24 12:50:23 +0000755 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000756 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000757 return true;
758 }
759 }
760 return false;
761}
762
763static SDValue Widen(SelectionDAG *CurDAG, SDValue N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000764 SDLoc dl(N);
765 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000766 SDValue ImpDef = SDValue(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000767 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::i64), 0);
Tim Northover3b0846e2014-05-24 12:50:23 +0000768 MachineSDNode *Node = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000769 TargetOpcode::INSERT_SUBREG, dl, MVT::i64, ImpDef, N, SubReg);
Tim Northover3b0846e2014-05-24 12:50:23 +0000770 return SDValue(Node, 0);
771}
772
773/// \brief Check if the given SHL node (\p N), can be used to form an
774/// extended register for an addressing mode.
775bool AArch64DAGToDAGISel::SelectExtendedSHL(SDValue N, unsigned Size,
776 bool WantExtend, SDValue &Offset,
777 SDValue &SignExtend) {
778 assert(N.getOpcode() == ISD::SHL && "Invalid opcode.");
779 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
780 if (!CSD || (CSD->getZExtValue() & 0x7) != CSD->getZExtValue())
781 return false;
782
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000783 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000784 if (WantExtend) {
785 AArch64_AM::ShiftExtendType Ext =
786 getExtendTypeForNode(N.getOperand(0), true);
787 if (Ext == AArch64_AM::InvalidShiftExtend)
788 return false;
789
790 Offset = narrowIfNeeded(CurDAG, N.getOperand(0).getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000791 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
792 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000793 } else {
794 Offset = N.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000795 SignExtend = CurDAG->getTargetConstant(0, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000796 }
797
798 unsigned LegalShiftVal = Log2_32(Size);
799 unsigned ShiftVal = CSD->getZExtValue();
800
801 if (ShiftVal != 0 && ShiftVal != LegalShiftVal)
802 return false;
803
Eric Christopher114fa1c2016-02-29 22:50:49 +0000804 return isWorthFolding(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000805}
806
807bool AArch64DAGToDAGISel::SelectAddrModeWRO(SDValue N, unsigned Size,
808 SDValue &Base, SDValue &Offset,
809 SDValue &SignExtend,
810 SDValue &DoShift) {
811 if (N.getOpcode() != ISD::ADD)
812 return false;
813 SDValue LHS = N.getOperand(0);
814 SDValue RHS = N.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000815 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000816
817 // We don't want to match immediate adds here, because they are better lowered
818 // to the register-immediate addressing modes.
819 if (isa<ConstantSDNode>(LHS) || isa<ConstantSDNode>(RHS))
820 return false;
821
822 // Check if this particular node is reused in any non-memory related
823 // operation. If yes, do not try to fold this node into the address
824 // computation, since the computation will be kept.
825 const SDNode *Node = N.getNode();
826 for (SDNode *UI : Node->uses()) {
827 if (!isa<MemSDNode>(*UI))
828 return false;
829 }
830
831 // Remember if it is worth folding N when it produces extended register.
832 bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
833
834 // Try to match a shifted extend on the RHS.
835 if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
836 SelectExtendedSHL(RHS, Size, true, Offset, SignExtend)) {
837 Base = LHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000838 DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000839 return true;
840 }
841
842 // Try to match a shifted extend on the LHS.
843 if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
844 SelectExtendedSHL(LHS, Size, true, Offset, SignExtend)) {
845 Base = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000846 DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000847 return true;
848 }
849
850 // There was no shift, whatever else we find.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000851 DoShift = CurDAG->getTargetConstant(false, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000852
853 AArch64_AM::ShiftExtendType Ext = AArch64_AM::InvalidShiftExtend;
854 // Try to match an unshifted extend on the LHS.
855 if (IsExtendedRegisterWorthFolding &&
856 (Ext = getExtendTypeForNode(LHS, true)) !=
857 AArch64_AM::InvalidShiftExtend) {
858 Base = RHS;
859 Offset = narrowIfNeeded(CurDAG, LHS.getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000860 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
861 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000862 if (isWorthFolding(LHS))
863 return true;
864 }
865
866 // Try to match an unshifted extend on the RHS.
867 if (IsExtendedRegisterWorthFolding &&
868 (Ext = getExtendTypeForNode(RHS, true)) !=
869 AArch64_AM::InvalidShiftExtend) {
870 Base = LHS;
871 Offset = narrowIfNeeded(CurDAG, RHS.getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000872 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
873 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000874 if (isWorthFolding(RHS))
875 return true;
876 }
877
878 return false;
879}
880
Hao Liu3cb826c2014-10-14 06:50:36 +0000881// Check if the given immediate is preferred by ADD. If an immediate can be
882// encoded in an ADD, or it can be encoded in an "ADD LSL #12" and can not be
883// encoded by one MOVZ, return true.
884static bool isPreferredADD(int64_t ImmOff) {
885 // Constant in [0x0, 0xfff] can be encoded in ADD.
886 if ((ImmOff & 0xfffffffffffff000LL) == 0x0LL)
887 return true;
888 // Check if it can be encoded in an "ADD LSL #12".
889 if ((ImmOff & 0xffffffffff000fffLL) == 0x0LL)
890 // As a single MOVZ is faster than a "ADD of LSL #12", ignore such constant.
891 return (ImmOff & 0xffffffffff00ffffLL) != 0x0LL &&
892 (ImmOff & 0xffffffffffff0fffLL) != 0x0LL;
893 return false;
894}
895
Tim Northover3b0846e2014-05-24 12:50:23 +0000896bool AArch64DAGToDAGISel::SelectAddrModeXRO(SDValue N, unsigned Size,
897 SDValue &Base, SDValue &Offset,
898 SDValue &SignExtend,
899 SDValue &DoShift) {
900 if (N.getOpcode() != ISD::ADD)
901 return false;
902 SDValue LHS = N.getOperand(0);
903 SDValue RHS = N.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000904 SDLoc DL(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000905
Tim Northover3b0846e2014-05-24 12:50:23 +0000906 // Check if this particular node is reused in any non-memory related
907 // operation. If yes, do not try to fold this node into the address
908 // computation, since the computation will be kept.
909 const SDNode *Node = N.getNode();
910 for (SDNode *UI : Node->uses()) {
911 if (!isa<MemSDNode>(*UI))
912 return false;
913 }
914
Hao Liu3cb826c2014-10-14 06:50:36 +0000915 // Watch out if RHS is a wide immediate, it can not be selected into
916 // [BaseReg+Imm] addressing mode. Also it may not be able to be encoded into
917 // ADD/SUB. Instead it will use [BaseReg + 0] address mode and generate
918 // instructions like:
919 // MOV X0, WideImmediate
920 // ADD X1, BaseReg, X0
921 // LDR X2, [X1, 0]
922 // For such situation, using [BaseReg, XReg] addressing mode can save one
923 // ADD/SUB:
924 // MOV X0, WideImmediate
925 // LDR X2, [BaseReg, X0]
926 if (isa<ConstantSDNode>(RHS)) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000927 int64_t ImmOff = (int64_t)cast<ConstantSDNode>(RHS)->getZExtValue();
Hao Liu3cb826c2014-10-14 06:50:36 +0000928 unsigned Scale = Log2_32(Size);
Chad Rosier6c1f0932015-09-17 13:10:27 +0000929 // Skip the immediate can be selected by load/store addressing mode.
Hao Liu3cb826c2014-10-14 06:50:36 +0000930 // Also skip the immediate can be encoded by a single ADD (SUB is also
931 // checked by using -ImmOff).
932 if ((ImmOff % Size == 0 && ImmOff >= 0 && ImmOff < (0x1000 << Scale)) ||
933 isPreferredADD(ImmOff) || isPreferredADD(-ImmOff))
934 return false;
935
Hao Liu3cb826c2014-10-14 06:50:36 +0000936 SDValue Ops[] = { RHS };
937 SDNode *MOVI =
938 CurDAG->getMachineNode(AArch64::MOVi64imm, DL, MVT::i64, Ops);
939 SDValue MOVIV = SDValue(MOVI, 0);
940 // This ADD of two X register will be selected into [Reg+Reg] mode.
941 N = CurDAG->getNode(ISD::ADD, DL, MVT::i64, LHS, MOVIV);
942 }
943
Tim Northover3b0846e2014-05-24 12:50:23 +0000944 // Remember if it is worth folding N when it produces extended register.
945 bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
946
947 // Try to match a shifted extend on the RHS.
948 if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
949 SelectExtendedSHL(RHS, Size, false, Offset, SignExtend)) {
950 Base = LHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000951 DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000952 return true;
953 }
954
955 // Try to match a shifted extend on the LHS.
956 if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
957 SelectExtendedSHL(LHS, Size, false, Offset, SignExtend)) {
958 Base = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000959 DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000960 return true;
961 }
962
963 // Match any non-shifted, non-extend, non-immediate add expression.
964 Base = LHS;
965 Offset = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000966 SignExtend = CurDAG->getTargetConstant(false, DL, MVT::i32);
967 DoShift = CurDAG->getTargetConstant(false, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000968 // Reg1 + Reg2 is free: no check needed.
969 return true;
970}
971
972SDValue AArch64DAGToDAGISel::createDTuple(ArrayRef<SDValue> Regs) {
Benjamin Kramerea68a942015-02-19 15:26:17 +0000973 static const unsigned RegClassIDs[] = {
Tim Northover3b0846e2014-05-24 12:50:23 +0000974 AArch64::DDRegClassID, AArch64::DDDRegClassID, AArch64::DDDDRegClassID};
Benjamin Kramerea68a942015-02-19 15:26:17 +0000975 static const unsigned SubRegs[] = {AArch64::dsub0, AArch64::dsub1,
976 AArch64::dsub2, AArch64::dsub3};
Tim Northover3b0846e2014-05-24 12:50:23 +0000977
978 return createTuple(Regs, RegClassIDs, SubRegs);
979}
980
981SDValue AArch64DAGToDAGISel::createQTuple(ArrayRef<SDValue> Regs) {
Benjamin Kramerea68a942015-02-19 15:26:17 +0000982 static const unsigned RegClassIDs[] = {
Tim Northover3b0846e2014-05-24 12:50:23 +0000983 AArch64::QQRegClassID, AArch64::QQQRegClassID, AArch64::QQQQRegClassID};
Benjamin Kramerea68a942015-02-19 15:26:17 +0000984 static const unsigned SubRegs[] = {AArch64::qsub0, AArch64::qsub1,
985 AArch64::qsub2, AArch64::qsub3};
Tim Northover3b0846e2014-05-24 12:50:23 +0000986
987 return createTuple(Regs, RegClassIDs, SubRegs);
988}
989
990SDValue AArch64DAGToDAGISel::createTuple(ArrayRef<SDValue> Regs,
Benjamin Kramerea68a942015-02-19 15:26:17 +0000991 const unsigned RegClassIDs[],
992 const unsigned SubRegs[]) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000993 // There's no special register-class for a vector-list of 1 element: it's just
994 // a vector.
995 if (Regs.size() == 1)
996 return Regs[0];
997
998 assert(Regs.size() >= 2 && Regs.size() <= 4);
999
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001000 SDLoc DL(Regs[0]);
Tim Northover3b0846e2014-05-24 12:50:23 +00001001
1002 SmallVector<SDValue, 4> Ops;
1003
1004 // First operand of REG_SEQUENCE is the desired RegClass.
1005 Ops.push_back(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001006 CurDAG->getTargetConstant(RegClassIDs[Regs.size() - 2], DL, MVT::i32));
Tim Northover3b0846e2014-05-24 12:50:23 +00001007
1008 // Then we get pairs of source & subregister-position for the components.
1009 for (unsigned i = 0; i < Regs.size(); ++i) {
1010 Ops.push_back(Regs[i]);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001011 Ops.push_back(CurDAG->getTargetConstant(SubRegs[i], DL, MVT::i32));
Tim Northover3b0846e2014-05-24 12:50:23 +00001012 }
1013
1014 SDNode *N =
1015 CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops);
1016 return SDValue(N, 0);
1017}
1018
Justin Bogner283e3bd2016-05-12 23:10:30 +00001019void AArch64DAGToDAGISel::SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc,
1020 bool isExt) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001021 SDLoc dl(N);
1022 EVT VT = N->getValueType(0);
1023
1024 unsigned ExtOff = isExt;
1025
1026 // Form a REG_SEQUENCE to force register allocation.
1027 unsigned Vec0Off = ExtOff + 1;
1028 SmallVector<SDValue, 4> Regs(N->op_begin() + Vec0Off,
1029 N->op_begin() + Vec0Off + NumVecs);
1030 SDValue RegSeq = createQTuple(Regs);
1031
1032 SmallVector<SDValue, 6> Ops;
1033 if (isExt)
1034 Ops.push_back(N->getOperand(1));
1035 Ops.push_back(RegSeq);
1036 Ops.push_back(N->getOperand(NumVecs + ExtOff + 1));
Justin Bogner283e3bd2016-05-12 23:10:30 +00001037 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
Tim Northover3b0846e2014-05-24 12:50:23 +00001038}
1039
Justin Bogner283e3bd2016-05-12 23:10:30 +00001040bool AArch64DAGToDAGISel::tryIndexedLoad(SDNode *N) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001041 LoadSDNode *LD = cast<LoadSDNode>(N);
1042 if (LD->isUnindexed())
Justin Bogner283e3bd2016-05-12 23:10:30 +00001043 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001044 EVT VT = LD->getMemoryVT();
1045 EVT DstVT = N->getValueType(0);
1046 ISD::MemIndexedMode AM = LD->getAddressingMode();
1047 bool IsPre = AM == ISD::PRE_INC || AM == ISD::PRE_DEC;
1048
1049 // We're not doing validity checking here. That was done when checking
1050 // if we should mark the load as indexed or not. We're just selecting
1051 // the right instruction.
1052 unsigned Opcode = 0;
1053
1054 ISD::LoadExtType ExtType = LD->getExtensionType();
1055 bool InsertTo64 = false;
1056 if (VT == MVT::i64)
1057 Opcode = IsPre ? AArch64::LDRXpre : AArch64::LDRXpost;
1058 else if (VT == MVT::i32) {
1059 if (ExtType == ISD::NON_EXTLOAD)
1060 Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
1061 else if (ExtType == ISD::SEXTLOAD)
1062 Opcode = IsPre ? AArch64::LDRSWpre : AArch64::LDRSWpost;
1063 else {
1064 Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
1065 InsertTo64 = true;
1066 // The result of the load is only i32. It's the subreg_to_reg that makes
1067 // it into an i64.
1068 DstVT = MVT::i32;
1069 }
1070 } else if (VT == MVT::i16) {
1071 if (ExtType == ISD::SEXTLOAD) {
1072 if (DstVT == MVT::i64)
1073 Opcode = IsPre ? AArch64::LDRSHXpre : AArch64::LDRSHXpost;
1074 else
1075 Opcode = IsPre ? AArch64::LDRSHWpre : AArch64::LDRSHWpost;
1076 } else {
1077 Opcode = IsPre ? AArch64::LDRHHpre : AArch64::LDRHHpost;
1078 InsertTo64 = DstVT == MVT::i64;
1079 // The result of the load is only i32. It's the subreg_to_reg that makes
1080 // it into an i64.
1081 DstVT = MVT::i32;
1082 }
1083 } else if (VT == MVT::i8) {
1084 if (ExtType == ISD::SEXTLOAD) {
1085 if (DstVT == MVT::i64)
1086 Opcode = IsPre ? AArch64::LDRSBXpre : AArch64::LDRSBXpost;
1087 else
1088 Opcode = IsPre ? AArch64::LDRSBWpre : AArch64::LDRSBWpost;
1089 } else {
1090 Opcode = IsPre ? AArch64::LDRBBpre : AArch64::LDRBBpost;
1091 InsertTo64 = DstVT == MVT::i64;
1092 // The result of the load is only i32. It's the subreg_to_reg that makes
1093 // it into an i64.
1094 DstVT = MVT::i32;
1095 }
Ahmed Bougachae0e12db2015-08-04 01:29:38 +00001096 } else if (VT == MVT::f16) {
1097 Opcode = IsPre ? AArch64::LDRHpre : AArch64::LDRHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +00001098 } else if (VT == MVT::f32) {
1099 Opcode = IsPre ? AArch64::LDRSpre : AArch64::LDRSpost;
1100 } else if (VT == MVT::f64 || VT.is64BitVector()) {
1101 Opcode = IsPre ? AArch64::LDRDpre : AArch64::LDRDpost;
1102 } else if (VT.is128BitVector()) {
1103 Opcode = IsPre ? AArch64::LDRQpre : AArch64::LDRQpost;
1104 } else
Justin Bogner283e3bd2016-05-12 23:10:30 +00001105 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001106 SDValue Chain = LD->getChain();
1107 SDValue Base = LD->getBasePtr();
1108 ConstantSDNode *OffsetOp = cast<ConstantSDNode>(LD->getOffset());
1109 int OffsetVal = (int)OffsetOp->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001110 SDLoc dl(N);
1111 SDValue Offset = CurDAG->getTargetConstant(OffsetVal, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +00001112 SDValue Ops[] = { Base, Offset, Chain };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001113 SDNode *Res = CurDAG->getMachineNode(Opcode, dl, MVT::i64, DstVT,
Tim Northover3b0846e2014-05-24 12:50:23 +00001114 MVT::Other, Ops);
1115 // Either way, we're replacing the node, so tell the caller that.
Tim Northover3b0846e2014-05-24 12:50:23 +00001116 SDValue LoadedVal = SDValue(Res, 1);
1117 if (InsertTo64) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001118 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +00001119 LoadedVal =
1120 SDValue(CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001121 AArch64::SUBREG_TO_REG, dl, MVT::i64,
1122 CurDAG->getTargetConstant(0, dl, MVT::i64), LoadedVal,
1123 SubReg),
Tim Northover3b0846e2014-05-24 12:50:23 +00001124 0);
1125 }
1126
1127 ReplaceUses(SDValue(N, 0), LoadedVal);
1128 ReplaceUses(SDValue(N, 1), SDValue(Res, 0));
1129 ReplaceUses(SDValue(N, 2), SDValue(Res, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00001130 CurDAG->RemoveDeadNode(N);
Justin Bogner283e3bd2016-05-12 23:10:30 +00001131 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001132}
1133
Justin Bogner283e3bd2016-05-12 23:10:30 +00001134void AArch64DAGToDAGISel::SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
1135 unsigned SubRegIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001136 SDLoc dl(N);
1137 EVT VT = N->getValueType(0);
1138 SDValue Chain = N->getOperand(0);
1139
Benjamin Kramerea68a942015-02-19 15:26:17 +00001140 SDValue Ops[] = {N->getOperand(2), // Mem operand;
1141 Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00001142
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001143 const EVT ResTys[] = {MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001144
1145 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1146 SDValue SuperReg = SDValue(Ld, 0);
1147 for (unsigned i = 0; i < NumVecs; ++i)
1148 ReplaceUses(SDValue(N, i),
1149 CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
1150
1151 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
Justin Bogner3525da72016-05-12 20:54:27 +00001152 CurDAG->RemoveDeadNode(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001153}
1154
Justin Bogner283e3bd2016-05-12 23:10:30 +00001155void AArch64DAGToDAGISel::SelectPostLoad(SDNode *N, unsigned NumVecs,
1156 unsigned Opc, unsigned SubRegIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001157 SDLoc dl(N);
1158 EVT VT = N->getValueType(0);
1159 SDValue Chain = N->getOperand(0);
1160
Benjamin Kramerea68a942015-02-19 15:26:17 +00001161 SDValue Ops[] = {N->getOperand(1), // Mem operand
1162 N->getOperand(2), // Incremental
1163 Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00001164
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001165 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1166 MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001167
1168 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1169
1170 // Update uses of write back register
1171 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1172
1173 // Update uses of vector list
1174 SDValue SuperReg = SDValue(Ld, 1);
1175 if (NumVecs == 1)
1176 ReplaceUses(SDValue(N, 0), SuperReg);
1177 else
1178 for (unsigned i = 0; i < NumVecs; ++i)
1179 ReplaceUses(SDValue(N, i),
1180 CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
1181
1182 // Update the chain
1183 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00001184 CurDAG->RemoveDeadNode(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001185}
1186
Justin Bogner283e3bd2016-05-12 23:10:30 +00001187void AArch64DAGToDAGISel::SelectStore(SDNode *N, unsigned NumVecs,
1188 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001189 SDLoc dl(N);
1190 EVT VT = N->getOperand(2)->getValueType(0);
1191
1192 // Form a REG_SEQUENCE to force register allocation.
1193 bool Is128Bit = VT.getSizeInBits() == 128;
1194 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1195 SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1196
Benjamin Kramerea68a942015-02-19 15:26:17 +00001197 SDValue Ops[] = {RegSeq, N->getOperand(NumVecs + 2), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001198 SDNode *St = CurDAG->getMachineNode(Opc, dl, N->getValueType(0), Ops);
1199
Justin Bogner283e3bd2016-05-12 23:10:30 +00001200 ReplaceNode(N, St);
Tim Northover3b0846e2014-05-24 12:50:23 +00001201}
1202
Justin Bogner283e3bd2016-05-12 23:10:30 +00001203void AArch64DAGToDAGISel::SelectPostStore(SDNode *N, unsigned NumVecs,
1204 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001205 SDLoc dl(N);
1206 EVT VT = N->getOperand(2)->getValueType(0);
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001207 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1208 MVT::Other}; // Type for the Chain
Tim Northover3b0846e2014-05-24 12:50:23 +00001209
1210 // Form a REG_SEQUENCE to force register allocation.
1211 bool Is128Bit = VT.getSizeInBits() == 128;
1212 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1213 SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1214
Benjamin Kramerea68a942015-02-19 15:26:17 +00001215 SDValue Ops[] = {RegSeq,
1216 N->getOperand(NumVecs + 1), // base register
1217 N->getOperand(NumVecs + 2), // Incremental
1218 N->getOperand(0)}; // Chain
Tim Northover3b0846e2014-05-24 12:50:23 +00001219 SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1220
Justin Bogner283e3bd2016-05-12 23:10:30 +00001221 ReplaceNode(N, St);
Tim Northover3b0846e2014-05-24 12:50:23 +00001222}
1223
Benjamin Kramer51f6096c2015-03-23 12:30:58 +00001224namespace {
Tim Northover3b0846e2014-05-24 12:50:23 +00001225/// WidenVector - Given a value in the V64 register class, produce the
1226/// equivalent value in the V128 register class.
1227class WidenVector {
1228 SelectionDAG &DAG;
1229
1230public:
1231 WidenVector(SelectionDAG &DAG) : DAG(DAG) {}
1232
1233 SDValue operator()(SDValue V64Reg) {
1234 EVT VT = V64Reg.getValueType();
1235 unsigned NarrowSize = VT.getVectorNumElements();
1236 MVT EltTy = VT.getVectorElementType().getSimpleVT();
1237 MVT WideTy = MVT::getVectorVT(EltTy, 2 * NarrowSize);
1238 SDLoc DL(V64Reg);
1239
1240 SDValue Undef =
1241 SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, WideTy), 0);
1242 return DAG.getTargetInsertSubreg(AArch64::dsub, DL, WideTy, Undef, V64Reg);
1243 }
1244};
Benjamin Kramer51f6096c2015-03-23 12:30:58 +00001245} // namespace
Tim Northover3b0846e2014-05-24 12:50:23 +00001246
1247/// NarrowVector - Given a value in the V128 register class, produce the
1248/// equivalent value in the V64 register class.
1249static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG) {
1250 EVT VT = V128Reg.getValueType();
1251 unsigned WideSize = VT.getVectorNumElements();
1252 MVT EltTy = VT.getVectorElementType().getSimpleVT();
1253 MVT NarrowTy = MVT::getVectorVT(EltTy, WideSize / 2);
1254
1255 return DAG.getTargetExtractSubreg(AArch64::dsub, SDLoc(V128Reg), NarrowTy,
1256 V128Reg);
1257}
1258
Justin Bogner283e3bd2016-05-12 23:10:30 +00001259void AArch64DAGToDAGISel::SelectLoadLane(SDNode *N, unsigned NumVecs,
1260 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001261 SDLoc dl(N);
1262 EVT VT = N->getValueType(0);
1263 bool Narrow = VT.getSizeInBits() == 64;
1264
1265 // Form a REG_SEQUENCE to force register allocation.
1266 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1267
1268 if (Narrow)
1269 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1270 WidenVector(*CurDAG));
1271
1272 SDValue RegSeq = createQTuple(Regs);
1273
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001274 const EVT ResTys[] = {MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001275
1276 unsigned LaneNo =
1277 cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1278
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001279 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001280 N->getOperand(NumVecs + 3), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001281 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1282 SDValue SuperReg = SDValue(Ld, 0);
1283
1284 EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
Craig Topper26260942015-10-18 05:15:34 +00001285 static const unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1,
1286 AArch64::qsub2, AArch64::qsub3 };
Tim Northover3b0846e2014-05-24 12:50:23 +00001287 for (unsigned i = 0; i < NumVecs; ++i) {
1288 SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT, SuperReg);
1289 if (Narrow)
1290 NV = NarrowVector(NV, *CurDAG);
1291 ReplaceUses(SDValue(N, i), NV);
1292 }
1293
1294 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
Justin Bogner3525da72016-05-12 20:54:27 +00001295 CurDAG->RemoveDeadNode(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001296}
1297
Justin Bogner283e3bd2016-05-12 23:10:30 +00001298void AArch64DAGToDAGISel::SelectPostLoadLane(SDNode *N, unsigned NumVecs,
1299 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001300 SDLoc dl(N);
1301 EVT VT = N->getValueType(0);
1302 bool Narrow = VT.getSizeInBits() == 64;
1303
1304 // Form a REG_SEQUENCE to force register allocation.
1305 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1306
1307 if (Narrow)
1308 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1309 WidenVector(*CurDAG));
1310
1311 SDValue RegSeq = createQTuple(Regs);
1312
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001313 const EVT ResTys[] = {MVT::i64, // Type of the write back register
Ahmed Bougachae14a4d42015-04-17 23:43:33 +00001314 RegSeq->getValueType(0), MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001315
1316 unsigned LaneNo =
1317 cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1318
Benjamin Kramerea68a942015-02-19 15:26:17 +00001319 SDValue Ops[] = {RegSeq,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001320 CurDAG->getTargetConstant(LaneNo, dl,
1321 MVT::i64), // Lane Number
Benjamin Kramerea68a942015-02-19 15:26:17 +00001322 N->getOperand(NumVecs + 2), // Base register
1323 N->getOperand(NumVecs + 3), // Incremental
1324 N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001325 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1326
1327 // Update uses of the write back register
1328 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1329
1330 // Update uses of the vector list
1331 SDValue SuperReg = SDValue(Ld, 1);
1332 if (NumVecs == 1) {
1333 ReplaceUses(SDValue(N, 0),
1334 Narrow ? NarrowVector(SuperReg, *CurDAG) : SuperReg);
1335 } else {
1336 EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
Craig Topper26260942015-10-18 05:15:34 +00001337 static const unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1,
1338 AArch64::qsub2, AArch64::qsub3 };
Tim Northover3b0846e2014-05-24 12:50:23 +00001339 for (unsigned i = 0; i < NumVecs; ++i) {
1340 SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT,
1341 SuperReg);
1342 if (Narrow)
1343 NV = NarrowVector(NV, *CurDAG);
1344 ReplaceUses(SDValue(N, i), NV);
1345 }
1346 }
1347
1348 // Update the Chain
1349 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00001350 CurDAG->RemoveDeadNode(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001351}
1352
Justin Bogner283e3bd2016-05-12 23:10:30 +00001353void AArch64DAGToDAGISel::SelectStoreLane(SDNode *N, unsigned NumVecs,
1354 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001355 SDLoc dl(N);
1356 EVT VT = N->getOperand(2)->getValueType(0);
1357 bool Narrow = VT.getSizeInBits() == 64;
1358
1359 // Form a REG_SEQUENCE to force register allocation.
1360 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1361
1362 if (Narrow)
1363 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1364 WidenVector(*CurDAG));
1365
1366 SDValue RegSeq = createQTuple(Regs);
1367
1368 unsigned LaneNo =
1369 cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1370
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001371 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001372 N->getOperand(NumVecs + 3), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001373 SDNode *St = CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops);
1374
1375 // Transfer memoperands.
1376 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1377 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1378 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1379
Justin Bogner283e3bd2016-05-12 23:10:30 +00001380 ReplaceNode(N, St);
Tim Northover3b0846e2014-05-24 12:50:23 +00001381}
1382
Justin Bogner283e3bd2016-05-12 23:10:30 +00001383void AArch64DAGToDAGISel::SelectPostStoreLane(SDNode *N, unsigned NumVecs,
1384 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001385 SDLoc dl(N);
1386 EVT VT = N->getOperand(2)->getValueType(0);
1387 bool Narrow = VT.getSizeInBits() == 64;
1388
1389 // Form a REG_SEQUENCE to force register allocation.
1390 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1391
1392 if (Narrow)
1393 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1394 WidenVector(*CurDAG));
1395
1396 SDValue RegSeq = createQTuple(Regs);
1397
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001398 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1399 MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001400
1401 unsigned LaneNo =
1402 cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1403
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001404 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001405 N->getOperand(NumVecs + 2), // Base Register
1406 N->getOperand(NumVecs + 3), // Incremental
1407 N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001408 SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1409
1410 // Transfer memoperands.
1411 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1412 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1413 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1414
Justin Bogner283e3bd2016-05-12 23:10:30 +00001415 ReplaceNode(N, St);
Tim Northover3b0846e2014-05-24 12:50:23 +00001416}
1417
1418static bool isBitfieldExtractOpFromAnd(SelectionDAG *CurDAG, SDNode *N,
1419 unsigned &Opc, SDValue &Opd0,
1420 unsigned &LSB, unsigned &MSB,
1421 unsigned NumberOfIgnoredLowBits,
1422 bool BiggerPattern) {
1423 assert(N->getOpcode() == ISD::AND &&
1424 "N must be a AND operation to call this function");
1425
1426 EVT VT = N->getValueType(0);
1427
1428 // Here we can test the type of VT and return false when the type does not
1429 // match, but since it is done prior to that call in the current context
1430 // we turned that into an assert to avoid redundant code.
1431 assert((VT == MVT::i32 || VT == MVT::i64) &&
1432 "Type checking must have been done before calling this function");
1433
1434 // FIXME: simplify-demanded-bits in DAGCombine will probably have
1435 // changed the AND node to a 32-bit mask operation. We'll have to
1436 // undo that as part of the transform here if we want to catch all
1437 // the opportunities.
1438 // Currently the NumberOfIgnoredLowBits argument helps to recover
1439 // form these situations when matching bigger pattern (bitfield insert).
1440
1441 // For unsigned extracts, check for a shift right and mask
Chad Rosier7e8dd512016-05-14 18:56:28 +00001442 uint64_t AndImm = 0;
1443 if (!isOpcWithIntImmediate(N, ISD::AND, AndImm))
Tim Northover3b0846e2014-05-24 12:50:23 +00001444 return false;
1445
1446 const SDNode *Op0 = N->getOperand(0).getNode();
1447
1448 // Because of simplify-demanded-bits in DAGCombine, the mask may have been
1449 // simplified. Try to undo that
Chad Rosier7e8dd512016-05-14 18:56:28 +00001450 AndImm |= (1 << NumberOfIgnoredLowBits) - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001451
1452 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
Chad Rosier7e8dd512016-05-14 18:56:28 +00001453 if (AndImm & (AndImm + 1))
Tim Northover3b0846e2014-05-24 12:50:23 +00001454 return false;
1455
1456 bool ClampMSB = false;
Chad Rosier7e8dd512016-05-14 18:56:28 +00001457 uint64_t SrlImm = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +00001458 // Handle the SRL + ANY_EXTEND case.
1459 if (VT == MVT::i64 && Op0->getOpcode() == ISD::ANY_EXTEND &&
Chad Rosier7e8dd512016-05-14 18:56:28 +00001460 isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL, SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001461 // Extend the incoming operand of the SRL to 64-bit.
1462 Opd0 = Widen(CurDAG, Op0->getOperand(0).getOperand(0));
1463 // Make sure to clamp the MSB so that we preserve the semantics of the
1464 // original operations.
1465 ClampMSB = true;
1466 } else if (VT == MVT::i32 && Op0->getOpcode() == ISD::TRUNCATE &&
1467 isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL,
Chad Rosier7e8dd512016-05-14 18:56:28 +00001468 SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001469 // If the shift result was truncated, we can still combine them.
1470 Opd0 = Op0->getOperand(0).getOperand(0);
1471
1472 // Use the type of SRL node.
1473 VT = Opd0->getValueType(0);
Chad Rosier7e8dd512016-05-14 18:56:28 +00001474 } else if (isOpcWithIntImmediate(Op0, ISD::SRL, SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001475 Opd0 = Op0->getOperand(0);
1476 } else if (BiggerPattern) {
1477 // Let's pretend a 0 shift right has been performed.
1478 // The resulting code will be at least as good as the original one
1479 // plus it may expose more opportunities for bitfield insert pattern.
1480 // FIXME: Currently we limit this to the bigger pattern, because
Chad Rosier6c1f0932015-09-17 13:10:27 +00001481 // some optimizations expect AND and not UBFM.
Tim Northover3b0846e2014-05-24 12:50:23 +00001482 Opd0 = N->getOperand(0);
1483 } else
1484 return false;
1485
Matthias Braun75260352015-02-24 18:52:04 +00001486 // Bail out on large immediates. This happens when no proper
1487 // combining/constant folding was performed.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001488 if (!BiggerPattern && (SrlImm <= 0 || SrlImm >= VT.getSizeInBits())) {
Matthias Braun02892ec2015-02-25 18:03:50 +00001489 DEBUG((dbgs() << N
1490 << ": Found large shift immediate, this should not happen\n"));
Matthias Braun75260352015-02-24 18:52:04 +00001491 return false;
Matthias Braun02892ec2015-02-25 18:03:50 +00001492 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001493
Chad Rosier7e8dd512016-05-14 18:56:28 +00001494 LSB = SrlImm;
1495 MSB = SrlImm + (VT == MVT::i32 ? countTrailingOnes<uint32_t>(AndImm)
1496 : countTrailingOnes<uint64_t>(AndImm)) -
Tim Northover3b0846e2014-05-24 12:50:23 +00001497 1;
1498 if (ClampMSB)
1499 // Since we're moving the extend before the right shift operation, we need
1500 // to clamp the MSB to make sure we don't shift in undefined bits instead of
1501 // the zeros which would get shifted in with the original right shift
1502 // operation.
1503 MSB = MSB > 31 ? 31 : MSB;
1504
1505 Opc = VT == MVT::i32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1506 return true;
1507}
1508
Chad Rosier2d658702016-06-03 15:00:09 +00001509static bool isBitfieldExtractOpFromSExtInReg(SDNode *N, unsigned &Opc,
1510 SDValue &Opd0, unsigned &Immr,
1511 unsigned &Imms) {
1512 assert(N->getOpcode() == ISD::SIGN_EXTEND_INREG);
1513
1514 EVT VT = N->getValueType(0);
1515 unsigned BitWidth = VT.getSizeInBits();
1516 assert((VT == MVT::i32 || VT == MVT::i64) &&
1517 "Type checking must have been done before calling this function");
1518
1519 SDValue Op = N->getOperand(0);
1520 if (Op->getOpcode() == ISD::TRUNCATE) {
1521 Op = Op->getOperand(0);
1522 VT = Op->getValueType(0);
1523 BitWidth = VT.getSizeInBits();
1524 }
1525
1526 uint64_t ShiftImm;
1527 if (!isOpcWithIntImmediate(Op.getNode(), ISD::SRL, ShiftImm) &&
1528 !isOpcWithIntImmediate(Op.getNode(), ISD::SRA, ShiftImm))
1529 return false;
1530
1531 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
1532 if (ShiftImm + Width > BitWidth)
1533 return false;
1534
1535 Opc = (VT == MVT::i32) ? AArch64::SBFMWri : AArch64::SBFMXri;
1536 Opd0 = Op.getOperand(0);
1537 Immr = ShiftImm;
1538 Imms = ShiftImm + Width - 1;
1539 return true;
1540}
1541
David Xu052b9d92014-09-02 09:33:56 +00001542static bool isSeveralBitsExtractOpFromShr(SDNode *N, unsigned &Opc,
1543 SDValue &Opd0, unsigned &LSB,
1544 unsigned &MSB) {
1545 // We are looking for the following pattern which basically extracts several
1546 // continuous bits from the source value and places it from the LSB of the
1547 // destination value, all other bits of the destination value or set to zero:
Tim Northover3b0846e2014-05-24 12:50:23 +00001548 //
1549 // Value2 = AND Value, MaskImm
1550 // SRL Value2, ShiftImm
1551 //
David Xu052b9d92014-09-02 09:33:56 +00001552 // with MaskImm >> ShiftImm to search for the bit width.
Tim Northover3b0846e2014-05-24 12:50:23 +00001553 //
1554 // This gets selected into a single UBFM:
1555 //
Chad Rosier7e8dd512016-05-14 18:56:28 +00001556 // UBFM Value, ShiftImm, BitWide + SrlImm -1
Tim Northover3b0846e2014-05-24 12:50:23 +00001557 //
1558
1559 if (N->getOpcode() != ISD::SRL)
1560 return false;
1561
Chad Rosier7e8dd512016-05-14 18:56:28 +00001562 uint64_t AndMask = 0;
1563 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, AndMask))
Tim Northover3b0846e2014-05-24 12:50:23 +00001564 return false;
1565
1566 Opd0 = N->getOperand(0).getOperand(0);
1567
Chad Rosier7e8dd512016-05-14 18:56:28 +00001568 uint64_t SrlImm = 0;
1569 if (!isIntImmediate(N->getOperand(1), SrlImm))
Tim Northover3b0846e2014-05-24 12:50:23 +00001570 return false;
1571
David Xu052b9d92014-09-02 09:33:56 +00001572 // Check whether we really have several bits extract here.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001573 unsigned BitWide = 64 - countLeadingOnes(~(AndMask >> SrlImm));
1574 if (BitWide && isMask_64(AndMask >> SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001575 if (N->getValueType(0) == MVT::i32)
1576 Opc = AArch64::UBFMWri;
1577 else
1578 Opc = AArch64::UBFMXri;
1579
Chad Rosier7e8dd512016-05-14 18:56:28 +00001580 LSB = SrlImm;
1581 MSB = BitWide + SrlImm - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001582 return true;
1583 }
1584
1585 return false;
1586}
1587
1588static bool isBitfieldExtractOpFromShr(SDNode *N, unsigned &Opc, SDValue &Opd0,
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001589 unsigned &Immr, unsigned &Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001590 bool BiggerPattern) {
1591 assert((N->getOpcode() == ISD::SRA || N->getOpcode() == ISD::SRL) &&
1592 "N must be a SHR/SRA operation to call this function");
1593
1594 EVT VT = N->getValueType(0);
1595
1596 // Here we can test the type of VT and return false when the type does not
1597 // match, but since it is done prior to that call in the current context
1598 // we turned that into an assert to avoid redundant code.
1599 assert((VT == MVT::i32 || VT == MVT::i64) &&
1600 "Type checking must have been done before calling this function");
1601
David Xu052b9d92014-09-02 09:33:56 +00001602 // Check for AND + SRL doing several bits extract.
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001603 if (isSeveralBitsExtractOpFromShr(N, Opc, Opd0, Immr, Imms))
Tim Northover3b0846e2014-05-24 12:50:23 +00001604 return true;
1605
Chad Rosierc73d5592016-05-16 12:55:01 +00001606 // We're looking for a shift of a shift.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001607 uint64_t ShlImm = 0;
1608 uint64_t TruncBits = 0;
1609 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, ShlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001610 Opd0 = N->getOperand(0).getOperand(0);
1611 } else if (VT == MVT::i32 && N->getOpcode() == ISD::SRL &&
1612 N->getOperand(0).getNode()->getOpcode() == ISD::TRUNCATE) {
1613 // We are looking for a shift of truncate. Truncate from i64 to i32 could
1614 // be considered as setting high 32 bits as zero. Our strategy here is to
1615 // always generate 64bit UBFM. This consistency will help the CSE pass
1616 // later find more redundancy.
1617 Opd0 = N->getOperand(0).getOperand(0);
Chad Rosier7e8dd512016-05-14 18:56:28 +00001618 TruncBits = Opd0->getValueType(0).getSizeInBits() - VT.getSizeInBits();
Tim Northover3b0846e2014-05-24 12:50:23 +00001619 VT = Opd0->getValueType(0);
1620 assert(VT == MVT::i64 && "the promoted type should be i64");
1621 } else if (BiggerPattern) {
1622 // Let's pretend a 0 shift left has been performed.
1623 // FIXME: Currently we limit this to the bigger pattern case,
1624 // because some optimizations expect AND and not UBFM
1625 Opd0 = N->getOperand(0);
1626 } else
1627 return false;
1628
Matthias Braun75260352015-02-24 18:52:04 +00001629 // Missing combines/constant folding may have left us with strange
1630 // constants.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001631 if (ShlImm >= VT.getSizeInBits()) {
Matthias Braun02892ec2015-02-25 18:03:50 +00001632 DEBUG((dbgs() << N
1633 << ": Found large shift immediate, this should not happen\n"));
Matthias Braun75260352015-02-24 18:52:04 +00001634 return false;
Matthias Braun02892ec2015-02-25 18:03:50 +00001635 }
Matthias Braun75260352015-02-24 18:52:04 +00001636
Chad Rosier7e8dd512016-05-14 18:56:28 +00001637 uint64_t SrlImm = 0;
1638 if (!isIntImmediate(N->getOperand(1), SrlImm))
Tim Northover3b0846e2014-05-24 12:50:23 +00001639 return false;
1640
Chad Rosier7e8dd512016-05-14 18:56:28 +00001641 assert(SrlImm > 0 && SrlImm < VT.getSizeInBits() &&
Tim Northover3b0846e2014-05-24 12:50:23 +00001642 "bad amount in shift node!");
Chad Rosier7e8dd512016-05-14 18:56:28 +00001643 int immr = SrlImm - ShlImm;
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001644 Immr = immr < 0 ? immr + VT.getSizeInBits() : immr;
Chad Rosier7e8dd512016-05-14 18:56:28 +00001645 Imms = VT.getSizeInBits() - ShlImm - TruncBits - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001646 // SRA requires a signed extraction
1647 if (VT == MVT::i32)
1648 Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMWri : AArch64::UBFMWri;
1649 else
1650 Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMXri : AArch64::UBFMXri;
1651 return true;
1652}
1653
Chad Rosierbe879ea2016-06-03 20:05:49 +00001654bool AArch64DAGToDAGISel::tryBitfieldExtractOpFromSExt(SDNode *N) {
1655 assert(N->getOpcode() == ISD::SIGN_EXTEND);
1656
1657 EVT VT = N->getValueType(0);
1658 EVT NarrowVT = N->getOperand(0)->getValueType(0);
1659 if (VT != MVT::i64 || NarrowVT != MVT::i32)
1660 return false;
1661
1662 uint64_t ShiftImm;
1663 SDValue Op = N->getOperand(0);
1664 if (!isOpcWithIntImmediate(Op.getNode(), ISD::SRA, ShiftImm))
1665 return false;
1666
1667 SDLoc dl(N);
1668 // Extend the incoming operand of the shift to 64-bits.
1669 SDValue Opd0 = Widen(CurDAG, Op.getOperand(0));
1670 unsigned Immr = ShiftImm;
1671 unsigned Imms = NarrowVT.getSizeInBits() - 1;
1672 SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
1673 CurDAG->getTargetConstant(Imms, dl, VT)};
1674 CurDAG->SelectNodeTo(N, AArch64::SBFMXri, VT, Ops);
1675 return true;
1676}
1677
Tim Northover3b0846e2014-05-24 12:50:23 +00001678static bool isBitfieldExtractOp(SelectionDAG *CurDAG, SDNode *N, unsigned &Opc,
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001679 SDValue &Opd0, unsigned &Immr, unsigned &Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001680 unsigned NumberOfIgnoredLowBits = 0,
1681 bool BiggerPattern = false) {
1682 if (N->getValueType(0) != MVT::i32 && N->getValueType(0) != MVT::i64)
1683 return false;
1684
1685 switch (N->getOpcode()) {
1686 default:
1687 if (!N->isMachineOpcode())
1688 return false;
1689 break;
1690 case ISD::AND:
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001691 return isBitfieldExtractOpFromAnd(CurDAG, N, Opc, Opd0, Immr, Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001692 NumberOfIgnoredLowBits, BiggerPattern);
1693 case ISD::SRL:
1694 case ISD::SRA:
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001695 return isBitfieldExtractOpFromShr(N, Opc, Opd0, Immr, Imms, BiggerPattern);
Chad Rosier2d658702016-06-03 15:00:09 +00001696
1697 case ISD::SIGN_EXTEND_INREG:
1698 return isBitfieldExtractOpFromSExtInReg(N, Opc, Opd0, Immr, Imms);
Tim Northover3b0846e2014-05-24 12:50:23 +00001699 }
1700
1701 unsigned NOpc = N->getMachineOpcode();
1702 switch (NOpc) {
1703 default:
1704 return false;
1705 case AArch64::SBFMWri:
1706 case AArch64::UBFMWri:
1707 case AArch64::SBFMXri:
1708 case AArch64::UBFMXri:
1709 Opc = NOpc;
1710 Opd0 = N->getOperand(0);
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001711 Immr = cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
1712 Imms = cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
Tim Northover3b0846e2014-05-24 12:50:23 +00001713 return true;
1714 }
1715 // Unreachable
1716 return false;
1717}
1718
Justin Bogner283e3bd2016-05-12 23:10:30 +00001719bool AArch64DAGToDAGISel::tryBitfieldExtractOp(SDNode *N) {
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001720 unsigned Opc, Immr, Imms;
Tim Northover3b0846e2014-05-24 12:50:23 +00001721 SDValue Opd0;
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001722 if (!isBitfieldExtractOp(CurDAG, N, Opc, Opd0, Immr, Imms))
Justin Bogner283e3bd2016-05-12 23:10:30 +00001723 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001724
1725 EVT VT = N->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001726 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001727
1728 // If the bit extract operation is 64bit but the original type is 32bit, we
1729 // need to add one EXTRACT_SUBREG.
1730 if ((Opc == AArch64::SBFMXri || Opc == AArch64::UBFMXri) && VT == MVT::i32) {
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001731 SDValue Ops64[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, MVT::i64),
1732 CurDAG->getTargetConstant(Imms, dl, MVT::i64)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001733
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001734 SDNode *BFM = CurDAG->getMachineNode(Opc, dl, MVT::i64, Ops64);
1735 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Justin Bogner283e3bd2016-05-12 23:10:30 +00001736 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
1737 MVT::i32, SDValue(BFM, 0), SubReg));
1738 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001739 }
1740
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001741 SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
1742 CurDAG->getTargetConstant(Imms, dl, VT)};
Justin Bogner283e3bd2016-05-12 23:10:30 +00001743 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
1744 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001745}
1746
1747/// Does DstMask form a complementary pair with the mask provided by
1748/// BitsToBeInserted, suitable for use in a BFI instruction. Roughly speaking,
1749/// this asks whether DstMask zeroes precisely those bits that will be set by
1750/// the other half.
Benjamin Kramerc321e532016-06-08 19:09:22 +00001751static bool isBitfieldDstMask(uint64_t DstMask, const APInt &BitsToBeInserted,
Tim Northover3b0846e2014-05-24 12:50:23 +00001752 unsigned NumberOfIgnoredHighBits, EVT VT) {
1753 assert((VT == MVT::i32 || VT == MVT::i64) &&
1754 "i32 or i64 mask type expected!");
1755 unsigned BitWidth = VT.getSizeInBits() - NumberOfIgnoredHighBits;
1756
1757 APInt SignificantDstMask = APInt(BitWidth, DstMask);
1758 APInt SignificantBitsToBeInserted = BitsToBeInserted.zextOrTrunc(BitWidth);
1759
1760 return (SignificantDstMask & SignificantBitsToBeInserted) == 0 &&
1761 (SignificantDstMask | SignificantBitsToBeInserted).isAllOnesValue();
1762}
1763
1764// Look for bits that will be useful for later uses.
1765// A bit is consider useless as soon as it is dropped and never used
1766// before it as been dropped.
1767// E.g., looking for useful bit of x
1768// 1. y = x & 0x7
1769// 2. z = y >> 2
1770// After #1, x useful bits are 0x7, then the useful bits of x, live through
1771// y.
1772// After #2, the useful bits of x are 0x4.
1773// However, if x is used on an unpredicatable instruction, then all its bits
1774// are useful.
1775// E.g.
1776// 1. y = x & 0x7
1777// 2. z = y >> 2
1778// 3. str x, [@x]
1779static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth = 0);
1780
1781static void getUsefulBitsFromAndWithImmediate(SDValue Op, APInt &UsefulBits,
1782 unsigned Depth) {
1783 uint64_t Imm =
1784 cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1785 Imm = AArch64_AM::decodeLogicalImmediate(Imm, UsefulBits.getBitWidth());
1786 UsefulBits &= APInt(UsefulBits.getBitWidth(), Imm);
1787 getUsefulBits(Op, UsefulBits, Depth + 1);
1788}
1789
1790static void getUsefulBitsFromBitfieldMoveOpd(SDValue Op, APInt &UsefulBits,
1791 uint64_t Imm, uint64_t MSB,
1792 unsigned Depth) {
1793 // inherit the bitwidth value
1794 APInt OpUsefulBits(UsefulBits);
1795 OpUsefulBits = 1;
1796
1797 if (MSB >= Imm) {
1798 OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1799 --OpUsefulBits;
1800 // The interesting part will be in the lower part of the result
1801 getUsefulBits(Op, OpUsefulBits, Depth + 1);
1802 // The interesting part was starting at Imm in the argument
1803 OpUsefulBits = OpUsefulBits.shl(Imm);
1804 } else {
1805 OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1806 --OpUsefulBits;
1807 // The interesting part will be shifted in the result
1808 OpUsefulBits = OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm);
1809 getUsefulBits(Op, OpUsefulBits, Depth + 1);
1810 // The interesting part was at zero in the argument
1811 OpUsefulBits = OpUsefulBits.lshr(OpUsefulBits.getBitWidth() - Imm);
1812 }
1813
1814 UsefulBits &= OpUsefulBits;
1815}
1816
1817static void getUsefulBitsFromUBFM(SDValue Op, APInt &UsefulBits,
1818 unsigned Depth) {
1819 uint64_t Imm =
1820 cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1821 uint64_t MSB =
1822 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1823
1824 getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1825}
1826
1827static void getUsefulBitsFromOrWithShiftedReg(SDValue Op, APInt &UsefulBits,
1828 unsigned Depth) {
1829 uint64_t ShiftTypeAndValue =
1830 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1831 APInt Mask(UsefulBits);
1832 Mask.clearAllBits();
1833 Mask.flipAllBits();
1834
1835 if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSL) {
1836 // Shift Left
1837 uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1838 Mask = Mask.shl(ShiftAmt);
1839 getUsefulBits(Op, Mask, Depth + 1);
1840 Mask = Mask.lshr(ShiftAmt);
1841 } else if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSR) {
1842 // Shift Right
1843 // We do not handle AArch64_AM::ASR, because the sign will change the
1844 // number of useful bits
1845 uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1846 Mask = Mask.lshr(ShiftAmt);
1847 getUsefulBits(Op, Mask, Depth + 1);
1848 Mask = Mask.shl(ShiftAmt);
1849 } else
1850 return;
1851
1852 UsefulBits &= Mask;
1853}
1854
1855static void getUsefulBitsFromBFM(SDValue Op, SDValue Orig, APInt &UsefulBits,
1856 unsigned Depth) {
1857 uint64_t Imm =
1858 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1859 uint64_t MSB =
1860 cast<const ConstantSDNode>(Op.getOperand(3).getNode())->getZExtValue();
1861
1862 if (Op.getOperand(1) == Orig)
1863 return getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1864
1865 APInt OpUsefulBits(UsefulBits);
1866 OpUsefulBits = 1;
1867
1868 if (MSB >= Imm) {
1869 OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1870 --OpUsefulBits;
1871 UsefulBits &= ~OpUsefulBits;
1872 getUsefulBits(Op, UsefulBits, Depth + 1);
1873 } else {
1874 OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1875 --OpUsefulBits;
1876 UsefulBits = ~(OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm));
1877 getUsefulBits(Op, UsefulBits, Depth + 1);
1878 }
1879}
1880
1881static void getUsefulBitsForUse(SDNode *UserNode, APInt &UsefulBits,
1882 SDValue Orig, unsigned Depth) {
1883
1884 // Users of this node should have already been instruction selected
1885 // FIXME: Can we turn that into an assert?
1886 if (!UserNode->isMachineOpcode())
1887 return;
1888
1889 switch (UserNode->getMachineOpcode()) {
1890 default:
1891 return;
1892 case AArch64::ANDSWri:
1893 case AArch64::ANDSXri:
1894 case AArch64::ANDWri:
1895 case AArch64::ANDXri:
1896 // We increment Depth only when we call the getUsefulBits
1897 return getUsefulBitsFromAndWithImmediate(SDValue(UserNode, 0), UsefulBits,
1898 Depth);
1899 case AArch64::UBFMWri:
1900 case AArch64::UBFMXri:
1901 return getUsefulBitsFromUBFM(SDValue(UserNode, 0), UsefulBits, Depth);
1902
1903 case AArch64::ORRWrs:
1904 case AArch64::ORRXrs:
1905 if (UserNode->getOperand(1) != Orig)
1906 return;
1907 return getUsefulBitsFromOrWithShiftedReg(SDValue(UserNode, 0), UsefulBits,
1908 Depth);
1909 case AArch64::BFMWri:
1910 case AArch64::BFMXri:
1911 return getUsefulBitsFromBFM(SDValue(UserNode, 0), Orig, UsefulBits, Depth);
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001912
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001913 case AArch64::STRBBui:
Chad Rosier9926a5e2016-05-12 01:42:01 +00001914 case AArch64::STURBBi:
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001915 if (UserNode->getOperand(0) != Orig)
1916 return;
1917 UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xff);
1918 return;
1919
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001920 case AArch64::STRHHui:
Chad Rosier9926a5e2016-05-12 01:42:01 +00001921 case AArch64::STURHHi:
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001922 if (UserNode->getOperand(0) != Orig)
1923 return;
1924 UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xffff);
1925 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00001926 }
1927}
1928
1929static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth) {
1930 if (Depth >= 6)
1931 return;
1932 // Initialize UsefulBits
1933 if (!Depth) {
1934 unsigned Bitwidth = Op.getValueType().getScalarType().getSizeInBits();
1935 // At the beginning, assume every produced bits is useful
1936 UsefulBits = APInt(Bitwidth, 0);
1937 UsefulBits.flipAllBits();
1938 }
1939 APInt UsersUsefulBits(UsefulBits.getBitWidth(), 0);
1940
1941 for (SDNode *Node : Op.getNode()->uses()) {
1942 // A use cannot produce useful bits
1943 APInt UsefulBitsForUse = APInt(UsefulBits);
1944 getUsefulBitsForUse(Node, UsefulBitsForUse, Op, Depth);
1945 UsersUsefulBits |= UsefulBitsForUse;
1946 }
1947 // UsefulBits contains the produced bits that are meaningful for the
1948 // current definition, thus a user cannot make a bit meaningful at
1949 // this point
1950 UsefulBits &= UsersUsefulBits;
1951}
1952
1953/// Create a machine node performing a notional SHL of Op by ShlAmount. If
1954/// ShlAmount is negative, do a (logical) right-shift instead. If ShlAmount is
1955/// 0, return Op unchanged.
1956static SDValue getLeftShift(SelectionDAG *CurDAG, SDValue Op, int ShlAmount) {
1957 if (ShlAmount == 0)
1958 return Op;
1959
1960 EVT VT = Op.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001961 SDLoc dl(Op);
Tim Northover3b0846e2014-05-24 12:50:23 +00001962 unsigned BitWidth = VT.getSizeInBits();
1963 unsigned UBFMOpc = BitWidth == 32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1964
1965 SDNode *ShiftNode;
1966 if (ShlAmount > 0) {
1967 // LSL wD, wN, #Amt == UBFM wD, wN, #32-Amt, #31-Amt
1968 ShiftNode = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001969 UBFMOpc, dl, VT, Op,
1970 CurDAG->getTargetConstant(BitWidth - ShlAmount, dl, VT),
1971 CurDAG->getTargetConstant(BitWidth - 1 - ShlAmount, dl, VT));
Tim Northover3b0846e2014-05-24 12:50:23 +00001972 } else {
1973 // LSR wD, wN, #Amt == UBFM wD, wN, #Amt, #32-1
1974 assert(ShlAmount < 0 && "expected right shift");
1975 int ShrAmount = -ShlAmount;
1976 ShiftNode = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001977 UBFMOpc, dl, VT, Op, CurDAG->getTargetConstant(ShrAmount, dl, VT),
1978 CurDAG->getTargetConstant(BitWidth - 1, dl, VT));
Tim Northover3b0846e2014-05-24 12:50:23 +00001979 }
1980
1981 return SDValue(ShiftNode, 0);
1982}
1983
1984/// Does this tree qualify as an attempt to move a bitfield into position,
1985/// essentially "(and (shl VAL, N), Mask)".
1986static bool isBitfieldPositioningOp(SelectionDAG *CurDAG, SDValue Op,
Geoff Berry43ec15e2015-09-18 17:11:53 +00001987 bool BiggerPattern,
Tim Northover3b0846e2014-05-24 12:50:23 +00001988 SDValue &Src, int &ShiftAmount,
1989 int &MaskWidth) {
1990 EVT VT = Op.getValueType();
1991 unsigned BitWidth = VT.getSizeInBits();
1992 (void)BitWidth;
1993 assert(BitWidth == 32 || BitWidth == 64);
1994
1995 APInt KnownZero, KnownOne;
1996 CurDAG->computeKnownBits(Op, KnownZero, KnownOne);
1997
1998 // Non-zero in the sense that they're not provably zero, which is the key
1999 // point if we want to use this value
2000 uint64_t NonZeroBits = (~KnownZero).getZExtValue();
2001
2002 // Discard a constant AND mask if present. It's safe because the node will
2003 // already have been factored into the computeKnownBits calculation above.
2004 uint64_t AndImm;
2005 if (isOpcWithIntImmediate(Op.getNode(), ISD::AND, AndImm)) {
2006 assert((~APInt(BitWidth, AndImm) & ~KnownZero) == 0);
2007 Op = Op.getOperand(0);
2008 }
2009
Geoff Berry43ec15e2015-09-18 17:11:53 +00002010 // Don't match if the SHL has more than one use, since then we'll end up
2011 // generating SHL+UBFIZ instead of just keeping SHL+AND.
2012 if (!BiggerPattern && !Op.hasOneUse())
2013 return false;
2014
Tim Northover3b0846e2014-05-24 12:50:23 +00002015 uint64_t ShlImm;
2016 if (!isOpcWithIntImmediate(Op.getNode(), ISD::SHL, ShlImm))
2017 return false;
2018 Op = Op.getOperand(0);
2019
2020 if (!isShiftedMask_64(NonZeroBits))
2021 return false;
2022
2023 ShiftAmount = countTrailingZeros(NonZeroBits);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00002024 MaskWidth = countTrailingOnes(NonZeroBits >> ShiftAmount);
Tim Northover3b0846e2014-05-24 12:50:23 +00002025
2026 // BFI encompasses sufficiently many nodes that it's worth inserting an extra
2027 // LSL/LSR if the mask in NonZeroBits doesn't quite match up with the ISD::SHL
Geoff Berry43ec15e2015-09-18 17:11:53 +00002028 // amount. BiggerPattern is true when this pattern is being matched for BFI,
2029 // BiggerPattern is false when this pattern is being matched for UBFIZ, in
2030 // which case it is not profitable to insert an extra shift.
2031 if (ShlImm - ShiftAmount != 0 && !BiggerPattern)
2032 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002033 Src = getLeftShift(CurDAG, Op, ShlImm - ShiftAmount);
2034
2035 return true;
2036}
2037
Chad Rosier02f25a92016-05-19 14:19:47 +00002038static bool isShiftedMask(uint64_t Mask, EVT VT) {
2039 assert(VT == MVT::i32 || VT == MVT::i64);
2040 if (VT == MVT::i32)
2041 return isShiftedMask_32(Mask);
2042 return isShiftedMask_64(Mask);
2043}
2044
Chad Rosier816a67d2016-05-26 13:27:56 +00002045// Generate a BFI/BFXIL from 'or (and X, MaskImm), OrImm' iff the value being
2046// inserted only sets known zero bits.
2047static bool tryBitfieldInsertOpFromOrAndImm(SDNode *N, SelectionDAG *CurDAG) {
2048 assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
2049
2050 EVT VT = N->getValueType(0);
2051 if (VT != MVT::i32 && VT != MVT::i64)
2052 return false;
2053
2054 unsigned BitWidth = VT.getSizeInBits();
2055
2056 uint64_t OrImm;
2057 if (!isOpcWithIntImmediate(N, ISD::OR, OrImm))
2058 return false;
2059
2060 // Skip this transformation if the ORR immediate can be encoded in the ORR.
2061 // Otherwise, we'll trade an AND+ORR for ORR+BFI/BFXIL, which is most likely
2062 // performance neutral.
2063 if (AArch64_AM::isLogicalImmediate(OrImm, BitWidth))
2064 return false;
2065
2066 uint64_t MaskImm;
2067 SDValue And = N->getOperand(0);
2068 // Must be a single use AND with an immediate operand.
2069 if (!And.hasOneUse() ||
2070 !isOpcWithIntImmediate(And.getNode(), ISD::AND, MaskImm))
2071 return false;
2072
2073 // Compute the Known Zero for the AND as this allows us to catch more general
2074 // cases than just looking for AND with imm.
2075 APInt KnownZero, KnownOne;
2076 CurDAG->computeKnownBits(And, KnownZero, KnownOne);
2077
2078 // Non-zero in the sense that they're not provably zero, which is the key
2079 // point if we want to use this value.
2080 uint64_t NotKnownZero = (~KnownZero).getZExtValue();
2081
2082 // The KnownZero mask must be a shifted mask (e.g., 1110..011, 11100..00).
2083 if (!isShiftedMask(KnownZero.getZExtValue(), VT))
2084 return false;
2085
2086 // The bits being inserted must only set those bits that are known to be zero.
2087 if ((OrImm & NotKnownZero) != 0) {
2088 // FIXME: It's okay if the OrImm sets NotKnownZero bits to 1, but we don't
2089 // currently handle this case.
2090 return false;
2091 }
2092
2093 // BFI/BFXIL dst, src, #lsb, #width.
2094 int LSB = countTrailingOnes(NotKnownZero);
2095 int Width = BitWidth - APInt(BitWidth, NotKnownZero).countPopulation();
2096
2097 // BFI/BFXIL is an alias of BFM, so translate to BFM operands.
2098 unsigned ImmR = (BitWidth - LSB) % BitWidth;
2099 unsigned ImmS = Width - 1;
2100
2101 // If we're creating a BFI instruction avoid cases where we need more
2102 // instructions to materialize the BFI constant as compared to the original
2103 // ORR. A BFXIL will use the same constant as the original ORR, so the code
2104 // should be no worse in this case.
2105 bool IsBFI = LSB != 0;
2106 uint64_t BFIImm = OrImm >> LSB;
2107 if (IsBFI && !AArch64_AM::isLogicalImmediate(BFIImm, BitWidth)) {
2108 // We have a BFI instruction and we know the constant can't be materialized
2109 // with a ORR-immediate with the zero register.
2110 unsigned OrChunks = 0, BFIChunks = 0;
2111 for (unsigned Shift = 0; Shift < BitWidth; Shift += 16) {
2112 if (((OrImm >> Shift) & 0xFFFF) != 0)
2113 ++OrChunks;
2114 if (((BFIImm >> Shift) & 0xFFFF) != 0)
2115 ++BFIChunks;
2116 }
2117 if (BFIChunks > OrChunks)
2118 return false;
2119 }
2120
2121 // Materialize the constant to be inserted.
2122 SDLoc DL(N);
2123 unsigned MOVIOpc = VT == MVT::i32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
2124 SDNode *MOVI = CurDAG->getMachineNode(
2125 MOVIOpc, DL, VT, CurDAG->getTargetConstant(BFIImm, DL, VT));
2126
2127 // Create the BFI/BFXIL instruction.
2128 SDValue Ops[] = {And.getOperand(0), SDValue(MOVI, 0),
2129 CurDAG->getTargetConstant(ImmR, DL, VT),
2130 CurDAG->getTargetConstant(ImmS, DL, VT)};
2131 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2132 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2133 return true;
2134}
2135
Justin Bogner283e3bd2016-05-12 23:10:30 +00002136static bool tryBitfieldInsertOpFromOr(SDNode *N, const APInt &UsefulBits,
2137 SelectionDAG *CurDAG) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002138 assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
2139
Tim Northover3b0846e2014-05-24 12:50:23 +00002140 EVT VT = N->getValueType(0);
Chad Rosier042ac2c2016-05-12 19:38:18 +00002141 if (VT != MVT::i32 && VT != MVT::i64)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002142 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002143
Chad Rosier91294c52016-05-18 17:43:11 +00002144 unsigned BitWidth = VT.getSizeInBits();
2145
Tim Northover3b0846e2014-05-24 12:50:23 +00002146 // Because of simplify-demanded-bits in DAGCombine, involved masks may not
2147 // have the expected shape. Try to undo that.
Tim Northover3b0846e2014-05-24 12:50:23 +00002148
2149 unsigned NumberOfIgnoredLowBits = UsefulBits.countTrailingZeros();
2150 unsigned NumberOfIgnoredHighBits = UsefulBits.countLeadingZeros();
2151
Chad Rosiere0062022016-05-18 23:51:17 +00002152 // Given a OR operation, check if we have the following pattern
2153 // ubfm c, b, imm, imm2 (or something that does the same jobs, see
2154 // isBitfieldExtractOp)
2155 // d = e & mask2 ; where mask is a binary sequence of 1..10..0 and
2156 // countTrailingZeros(mask2) == imm2 - imm + 1
2157 // f = d | c
2158 // if yes, replace the OR instruction with:
2159 // f = BFM Opd0, Opd1, LSB, MSB ; where LSB = imm, and MSB = imm2
2160
Geoff Berry43ec15e2015-09-18 17:11:53 +00002161 // OR is commutative, check all combinations of operand order and values of
2162 // BiggerPattern, i.e.
2163 // Opd0, Opd1, BiggerPattern=false
2164 // Opd1, Opd0, BiggerPattern=false
2165 // Opd0, Opd1, BiggerPattern=true
2166 // Opd1, Opd0, BiggerPattern=true
2167 // Several of these combinations may match, so check with BiggerPattern=false
2168 // first since that will produce better results by matching more instructions
2169 // and/or inserting fewer extra instructions.
2170 for (int I = 0; I < 4; ++I) {
2171
Chad Rosier91294c52016-05-18 17:43:11 +00002172 SDValue Dst, Src;
2173 unsigned ImmR, ImmS;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002174 bool BiggerPattern = I / 2;
2175 SDNode *OrOpd0 = N->getOperand(I % 2).getNode();
2176 SDValue OrOpd1Val = N->getOperand((I + 1) % 2);
2177 SDNode *OrOpd1 = OrOpd1Val.getNode();
2178
Tim Northover3b0846e2014-05-24 12:50:23 +00002179 unsigned BFXOpc;
2180 int DstLSB, Width;
2181 if (isBitfieldExtractOp(CurDAG, OrOpd0, BFXOpc, Src, ImmR, ImmS,
Geoff Berry43ec15e2015-09-18 17:11:53 +00002182 NumberOfIgnoredLowBits, BiggerPattern)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002183 // Check that the returned opcode is compatible with the pattern,
2184 // i.e., same type and zero extended (U and not S)
2185 if ((BFXOpc != AArch64::UBFMXri && VT == MVT::i64) ||
2186 (BFXOpc != AArch64::UBFMWri && VT == MVT::i32))
2187 continue;
2188
2189 // Compute the width of the bitfield insertion
2190 DstLSB = 0;
2191 Width = ImmS - ImmR + 1;
2192 // FIXME: This constraint is to catch bitfield insertion we may
2193 // want to widen the pattern if we want to grab general bitfied
2194 // move case
2195 if (Width <= 0)
2196 continue;
2197
2198 // If the mask on the insertee is correct, we have a BFXIL operation. We
2199 // can share the ImmR and ImmS values from the already-computed UBFM.
Geoff Berry43ec15e2015-09-18 17:11:53 +00002200 } else if (isBitfieldPositioningOp(CurDAG, SDValue(OrOpd0, 0),
2201 BiggerPattern,
2202 Src, DstLSB, Width)) {
Chad Rosier91294c52016-05-18 17:43:11 +00002203 ImmR = (BitWidth - DstLSB) % BitWidth;
Tim Northover3b0846e2014-05-24 12:50:23 +00002204 ImmS = Width - 1;
2205 } else
2206 continue;
2207
2208 // Check the second part of the pattern
2209 EVT VT = OrOpd1->getValueType(0);
2210 assert((VT == MVT::i32 || VT == MVT::i64) && "unexpected OR operand");
2211
2212 // Compute the Known Zero for the candidate of the first operand.
2213 // This allows to catch more general case than just looking for
2214 // AND with imm. Indeed, simplify-demanded-bits may have removed
2215 // the AND instruction because it proves it was useless.
2216 APInt KnownZero, KnownOne;
2217 CurDAG->computeKnownBits(OrOpd1Val, KnownZero, KnownOne);
2218
2219 // Check if there is enough room for the second operand to appear
2220 // in the first one
2221 APInt BitsToBeInserted =
2222 APInt::getBitsSet(KnownZero.getBitWidth(), DstLSB, DstLSB + Width);
2223
2224 if ((BitsToBeInserted & ~KnownZero) != 0)
2225 continue;
2226
2227 // Set the first operand
2228 uint64_t Imm;
2229 if (isOpcWithIntImmediate(OrOpd1, ISD::AND, Imm) &&
2230 isBitfieldDstMask(Imm, BitsToBeInserted, NumberOfIgnoredHighBits, VT))
2231 // In that case, we can eliminate the AND
2232 Dst = OrOpd1->getOperand(0);
2233 else
2234 // Maybe the AND has been removed by simplify-demanded-bits
2235 // or is useful because it discards more bits
2236 Dst = OrOpd1Val;
2237
2238 // both parts match
Chad Rosier042ac2c2016-05-12 19:38:18 +00002239 SDLoc DL(N);
2240 SDValue Ops[] = {Dst, Src, CurDAG->getTargetConstant(ImmR, DL, VT),
2241 CurDAG->getTargetConstant(ImmS, DL, VT)};
2242 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
Justin Bogner283e3bd2016-05-12 23:10:30 +00002243 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2244 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00002245 }
Chad Rosier02f25a92016-05-19 14:19:47 +00002246
2247 // Generate a BFXIL from 'or (and X, Mask0Imm), (and Y, Mask1Imm)' iff
2248 // Mask0Imm and ~Mask1Imm are equivalent and one of the MaskImms is a shifted
2249 // mask (e.g., 0x000ffff0).
2250 uint64_t Mask0Imm, Mask1Imm;
2251 SDValue And0 = N->getOperand(0);
2252 SDValue And1 = N->getOperand(1);
2253 if (And0.hasOneUse() && And1.hasOneUse() &&
2254 isOpcWithIntImmediate(And0.getNode(), ISD::AND, Mask0Imm) &&
2255 isOpcWithIntImmediate(And1.getNode(), ISD::AND, Mask1Imm) &&
2256 APInt(BitWidth, Mask0Imm) == ~APInt(BitWidth, Mask1Imm) &&
2257 (isShiftedMask(Mask0Imm, VT) || isShiftedMask(Mask1Imm, VT))) {
2258
2259 // We should have already caught the case where we extract hi and low parts.
2260 // E.g. BFXIL from 'or (and X, 0xffff0000), (and Y, 0x0000ffff)'.
2261 assert(!(isShiftedMask(Mask0Imm, VT) && isShiftedMask(Mask1Imm, VT)) &&
2262 "BFXIL should have already been optimized.");
2263
2264 // ORR is commutative, so canonicalize to the form 'or (and X, Mask0Imm),
2265 // (and Y, Mask1Imm)' where Mask1Imm is the shifted mask masking off the
2266 // bits to be inserted.
2267 if (isShiftedMask(Mask0Imm, VT)) {
2268 std::swap(And0, And1);
2269 std::swap(Mask0Imm, Mask1Imm);
2270 }
2271
2272 SDValue Src = And1->getOperand(0);
2273 SDValue Dst = And0->getOperand(0);
2274 unsigned LSB = countTrailingZeros(Mask1Imm);
2275 int Width = BitWidth - APInt(BitWidth, Mask0Imm).countPopulation();
2276
2277 // The BFXIL inserts the low-order bits from a source register, so right
2278 // shift the needed bits into place.
2279 SDLoc DL(N);
2280 unsigned ShiftOpc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
2281 SDNode *LSR = CurDAG->getMachineNode(
2282 ShiftOpc, DL, VT, Src, CurDAG->getTargetConstant(LSB, DL, VT),
2283 CurDAG->getTargetConstant(BitWidth - 1, DL, VT));
2284
2285 // BFXIL is an alias of BFM, so translate to BFM operands.
2286 unsigned ImmR = (BitWidth - LSB) % BitWidth;
2287 unsigned ImmS = Width - 1;
2288
2289 // Create the BFXIL instruction.
2290 SDValue Ops[] = {Dst, SDValue(LSR, 0),
2291 CurDAG->getTargetConstant(ImmR, DL, VT),
2292 CurDAG->getTargetConstant(ImmS, DL, VT)};
2293 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2294 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2295 return true;
2296 }
2297
Justin Bogner283e3bd2016-05-12 23:10:30 +00002298 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002299}
2300
Justin Bogner283e3bd2016-05-12 23:10:30 +00002301bool AArch64DAGToDAGISel::tryBitfieldInsertOp(SDNode *N) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002302 if (N->getOpcode() != ISD::OR)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002303 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002304
Weiming Zhao56ab5182015-12-01 19:17:49 +00002305 APInt NUsefulBits;
2306 getUsefulBits(SDValue(N, 0), NUsefulBits);
Tim Northover3b0846e2014-05-24 12:50:23 +00002307
Weiming Zhao56ab5182015-12-01 19:17:49 +00002308 // If all bits are not useful, just return UNDEF.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002309 if (!NUsefulBits) {
2310 CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0));
2311 return true;
2312 }
Weiming Zhao56ab5182015-12-01 19:17:49 +00002313
Chad Rosier816a67d2016-05-26 13:27:56 +00002314 if (tryBitfieldInsertOpFromOr(N, NUsefulBits, CurDAG))
2315 return true;
2316
2317 return tryBitfieldInsertOpFromOrAndImm(N, CurDAG);
Tim Northover3b0846e2014-05-24 12:50:23 +00002318}
2319
Geoff Berry43ec15e2015-09-18 17:11:53 +00002320/// SelectBitfieldInsertInZeroOp - Match a UBFIZ instruction that is the
2321/// equivalent of a left shift by a constant amount followed by an and masking
2322/// out a contiguous set of bits.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002323bool AArch64DAGToDAGISel::tryBitfieldInsertInZeroOp(SDNode *N) {
Geoff Berry43ec15e2015-09-18 17:11:53 +00002324 if (N->getOpcode() != ISD::AND)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002325 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002326
2327 EVT VT = N->getValueType(0);
Chad Rosier08d99082016-05-13 22:53:13 +00002328 if (VT != MVT::i32 && VT != MVT::i64)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002329 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002330
2331 SDValue Op0;
2332 int DstLSB, Width;
2333 if (!isBitfieldPositioningOp(CurDAG, SDValue(N, 0), /*BiggerPattern=*/false,
2334 Op0, DstLSB, Width))
Justin Bogner283e3bd2016-05-12 23:10:30 +00002335 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002336
2337 // ImmR is the rotate right amount.
2338 unsigned ImmR = (VT.getSizeInBits() - DstLSB) % VT.getSizeInBits();
2339 // ImmS is the most significant bit of the source to be moved.
2340 unsigned ImmS = Width - 1;
2341
2342 SDLoc DL(N);
2343 SDValue Ops[] = {Op0, CurDAG->getTargetConstant(ImmR, DL, VT),
2344 CurDAG->getTargetConstant(ImmS, DL, VT)};
Chad Rosier08d99082016-05-13 22:53:13 +00002345 unsigned Opc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
Justin Bogner283e3bd2016-05-12 23:10:30 +00002346 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2347 return true;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002348}
2349
Tim Northover3b0846e2014-05-24 12:50:23 +00002350bool
2351AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
2352 unsigned RegWidth) {
2353 APFloat FVal(0.0);
2354 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
2355 FVal = CN->getValueAPF();
2356 else if (LoadSDNode *LN = dyn_cast<LoadSDNode>(N)) {
2357 // Some otherwise illegal constants are allowed in this case.
2358 if (LN->getOperand(1).getOpcode() != AArch64ISD::ADDlow ||
2359 !isa<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1)))
2360 return false;
2361
2362 ConstantPoolSDNode *CN =
2363 dyn_cast<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1));
2364 FVal = cast<ConstantFP>(CN->getConstVal())->getValueAPF();
2365 } else
2366 return false;
2367
2368 // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
2369 // is between 1 and 32 for a destination w-register, or 1 and 64 for an
2370 // x-register.
2371 //
2372 // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
2373 // want THIS_NODE to be 2^fbits. This is much easier to deal with using
2374 // integers.
2375 bool IsExact;
2376
2377 // fbits is between 1 and 64 in the worst-case, which means the fmul
2378 // could have 2^64 as an actual operand. Need 65 bits of precision.
2379 APSInt IntVal(65, true);
2380 FVal.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
2381
2382 // N.b. isPowerOf2 also checks for > 0.
2383 if (!IsExact || !IntVal.isPowerOf2()) return false;
2384 unsigned FBits = IntVal.logBase2();
2385
2386 // Checks above should have guaranteed that we haven't lost information in
2387 // finding FBits, but it must still be in range.
2388 if (FBits == 0 || FBits > RegWidth) return false;
2389
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002390 FixedPos = CurDAG->getTargetConstant(FBits, SDLoc(N), MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +00002391 return true;
2392}
2393
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002394// Inspects a register string of the form o0:op1:CRn:CRm:op2 gets the fields
2395// of the string and obtains the integer values from them and combines these
2396// into a single value to be used in the MRS/MSR instruction.
2397static int getIntOperandFromRegisterString(StringRef RegString) {
2398 SmallVector<StringRef, 5> Fields;
Chandler Carruthe4405e92015-09-10 06:12:31 +00002399 RegString.split(Fields, ':');
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002400
2401 if (Fields.size() == 1)
2402 return -1;
2403
2404 assert(Fields.size() == 5
2405 && "Invalid number of fields in read register string");
2406
2407 SmallVector<int, 5> Ops;
2408 bool AllIntFields = true;
2409
2410 for (StringRef Field : Fields) {
2411 unsigned IntField;
2412 AllIntFields &= !Field.getAsInteger(10, IntField);
2413 Ops.push_back(IntField);
2414 }
2415
2416 assert(AllIntFields &&
2417 "Unexpected non-integer value in special register string.");
2418
2419 // Need to combine the integer fields of the string into a single value
2420 // based on the bit encoding of MRS/MSR instruction.
2421 return (Ops[0] << 14) | (Ops[1] << 11) | (Ops[2] << 7) |
2422 (Ops[3] << 3) | (Ops[4]);
2423}
2424
2425// Lower the read_register intrinsic to an MRS instruction node if the special
2426// register string argument is either of the form detailed in the ALCE (the
2427// form described in getIntOperandsFromRegsterString) or is a named register
2428// known by the MRS SysReg mapper.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002429bool AArch64DAGToDAGISel::tryReadRegister(SDNode *N) {
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002430 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2431 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2432 SDLoc DL(N);
2433
2434 int Reg = getIntOperandFromRegisterString(RegString->getString());
Justin Bogner283e3bd2016-05-12 23:10:30 +00002435 if (Reg != -1) {
2436 ReplaceNode(N, CurDAG->getMachineNode(
2437 AArch64::MRS, DL, N->getSimpleValueType(0), MVT::Other,
2438 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2439 N->getOperand(0)));
2440 return true;
2441 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002442
2443 // Use the sysreg mapper to map the remaining possible strings to the
2444 // value for the register to be used for the instruction operand.
2445 AArch64SysReg::MRSMapper mapper;
2446 bool IsValidSpecialReg;
2447 Reg = mapper.fromString(RegString->getString(),
2448 Subtarget->getFeatureBits(),
2449 IsValidSpecialReg);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002450 if (IsValidSpecialReg) {
2451 ReplaceNode(N, CurDAG->getMachineNode(
2452 AArch64::MRS, DL, N->getSimpleValueType(0), MVT::Other,
2453 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2454 N->getOperand(0)));
2455 return true;
2456 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002457
Justin Bogner283e3bd2016-05-12 23:10:30 +00002458 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002459}
2460
2461// Lower the write_register intrinsic to an MSR instruction node if the special
2462// register string argument is either of the form detailed in the ALCE (the
2463// form described in getIntOperandsFromRegsterString) or is a named register
2464// known by the MSR SysReg mapper.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002465bool AArch64DAGToDAGISel::tryWriteRegister(SDNode *N) {
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002466 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2467 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2468 SDLoc DL(N);
2469
2470 int Reg = getIntOperandFromRegisterString(RegString->getString());
Justin Bogner283e3bd2016-05-12 23:10:30 +00002471 if (Reg != -1) {
2472 ReplaceNode(
2473 N, CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002474 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
Justin Bogner283e3bd2016-05-12 23:10:30 +00002475 N->getOperand(2), N->getOperand(0)));
2476 return true;
2477 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002478
2479 // Check if the register was one of those allowed as the pstatefield value in
2480 // the MSR (immediate) instruction. To accept the values allowed in the
2481 // pstatefield for the MSR (immediate) instruction, we also require that an
2482 // immediate value has been provided as an argument, we know that this is
2483 // the case as it has been ensured by semantic checking.
2484 AArch64PState::PStateMapper PMapper;
2485 bool IsValidSpecialReg;
2486 Reg = PMapper.fromString(RegString->getString(),
2487 Subtarget->getFeatureBits(),
2488 IsValidSpecialReg);
2489 if (IsValidSpecialReg) {
2490 assert (isa<ConstantSDNode>(N->getOperand(2))
2491 && "Expected a constant integer expression.");
2492 uint64_t Immed = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Alexandros Lamprineas1bab1912015-10-05 13:42:31 +00002493 unsigned State;
Oliver Stannard911ea202015-11-26 15:32:30 +00002494 if (Reg == AArch64PState::PAN || Reg == AArch64PState::UAO) {
Alexandros Lamprineas1bab1912015-10-05 13:42:31 +00002495 assert(Immed < 2 && "Bad imm");
2496 State = AArch64::MSRpstateImm1;
2497 } else {
2498 assert(Immed < 16 && "Bad imm");
2499 State = AArch64::MSRpstateImm4;
2500 }
Justin Bogner283e3bd2016-05-12 23:10:30 +00002501 ReplaceNode(N, CurDAG->getMachineNode(
2502 State, DL, MVT::Other,
2503 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2504 CurDAG->getTargetConstant(Immed, DL, MVT::i16),
2505 N->getOperand(0)));
2506 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002507 }
2508
2509 // Use the sysreg mapper to attempt to map the remaining possible strings
2510 // to the value for the register to be used for the MSR (register)
2511 // instruction operand.
2512 AArch64SysReg::MSRMapper Mapper;
2513 Reg = Mapper.fromString(RegString->getString(),
2514 Subtarget->getFeatureBits(),
2515 IsValidSpecialReg);
2516
Justin Bogner283e3bd2016-05-12 23:10:30 +00002517 if (IsValidSpecialReg) {
2518 ReplaceNode(
2519 N, CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002520 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
Justin Bogner283e3bd2016-05-12 23:10:30 +00002521 N->getOperand(2), N->getOperand(0)));
2522 return true;
2523 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002524
Justin Bogner283e3bd2016-05-12 23:10:30 +00002525 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002526}
2527
Tim Northovercdf15292016-04-14 17:03:29 +00002528/// We've got special pseudo-instructions for these
2529void AArch64DAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
2530 unsigned Opcode;
2531 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
2532 if (MemTy == MVT::i8)
2533 Opcode = AArch64::CMP_SWAP_8;
2534 else if (MemTy == MVT::i16)
2535 Opcode = AArch64::CMP_SWAP_16;
2536 else if (MemTy == MVT::i32)
2537 Opcode = AArch64::CMP_SWAP_32;
2538 else if (MemTy == MVT::i64)
2539 Opcode = AArch64::CMP_SWAP_64;
2540 else
2541 llvm_unreachable("Unknown AtomicCmpSwap type");
2542
2543 MVT RegTy = MemTy == MVT::i64 ? MVT::i64 : MVT::i32;
2544 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
2545 N->getOperand(0)};
2546 SDNode *CmpSwap = CurDAG->getMachineNode(
2547 Opcode, SDLoc(N),
2548 CurDAG->getVTList(RegTy, MVT::i32, MVT::Other), Ops);
2549
2550 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2551 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2552 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
2553
2554 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
2555 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00002556 CurDAG->RemoveDeadNode(N);
Tim Northovercdf15292016-04-14 17:03:29 +00002557}
2558
Justin Bogner283e3bd2016-05-12 23:10:30 +00002559void AArch64DAGToDAGISel::Select(SDNode *Node) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002560 // Dump information about the Node being selected
2561 DEBUG(errs() << "Selecting: ");
2562 DEBUG(Node->dump(CurDAG));
2563 DEBUG(errs() << "\n");
2564
2565 // If we have a custom node, we already have selected!
2566 if (Node->isMachineOpcode()) {
2567 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
2568 Node->setNodeId(-1);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002569 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002570 }
2571
2572 // Few custom selection stuff.
Tim Northover3b0846e2014-05-24 12:50:23 +00002573 EVT VT = Node->getValueType(0);
2574
2575 switch (Node->getOpcode()) {
2576 default:
2577 break;
2578
Tim Northovercdf15292016-04-14 17:03:29 +00002579 case ISD::ATOMIC_CMP_SWAP:
2580 SelectCMP_SWAP(Node);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002581 return;
Tim Northovercdf15292016-04-14 17:03:29 +00002582
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002583 case ISD::READ_REGISTER:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002584 if (tryReadRegister(Node))
2585 return;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002586 break;
2587
2588 case ISD::WRITE_REGISTER:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002589 if (tryWriteRegister(Node))
2590 return;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002591 break;
2592
Tim Northover3b0846e2014-05-24 12:50:23 +00002593 case ISD::ADD:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002594 if (tryMLAV64LaneV128(Node))
2595 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002596 break;
2597
2598 case ISD::LOAD: {
2599 // Try to select as an indexed load. Fall through to normal processing
2600 // if we can't.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002601 if (tryIndexedLoad(Node))
2602 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002603 break;
2604 }
2605
2606 case ISD::SRL:
2607 case ISD::AND:
2608 case ISD::SRA:
Chad Rosier2d658702016-06-03 15:00:09 +00002609 case ISD::SIGN_EXTEND_INREG:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002610 if (tryBitfieldExtractOp(Node))
2611 return;
2612 if (tryBitfieldInsertInZeroOp(Node))
2613 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002614 break;
2615
Chad Rosierbe879ea2016-06-03 20:05:49 +00002616 case ISD::SIGN_EXTEND:
2617 if (tryBitfieldExtractOpFromSExt(Node))
2618 return;
2619 break;
2620
Tim Northover3b0846e2014-05-24 12:50:23 +00002621 case ISD::OR:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002622 if (tryBitfieldInsertOp(Node))
2623 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002624 break;
2625
2626 case ISD::EXTRACT_VECTOR_ELT: {
2627 // Extracting lane zero is a special case where we can just use a plain
2628 // EXTRACT_SUBREG instruction, which will become FMOV. This is easier for
2629 // the rest of the compiler, especially the register allocator and copyi
2630 // propagation, to reason about, so is preferred when it's possible to
2631 // use it.
2632 ConstantSDNode *LaneNode = cast<ConstantSDNode>(Node->getOperand(1));
2633 // Bail and use the default Select() for non-zero lanes.
2634 if (LaneNode->getZExtValue() != 0)
2635 break;
2636 // If the element type is not the same as the result type, likewise
2637 // bail and use the default Select(), as there's more to do than just
2638 // a cross-class COPY. This catches extracts of i8 and i16 elements
2639 // since they will need an explicit zext.
2640 if (VT != Node->getOperand(0).getValueType().getVectorElementType())
2641 break;
2642 unsigned SubReg;
2643 switch (Node->getOperand(0)
2644 .getValueType()
2645 .getVectorElementType()
2646 .getSizeInBits()) {
2647 default:
Craig Topper2a30d782014-06-18 05:05:13 +00002648 llvm_unreachable("Unexpected vector element type!");
Tim Northover3b0846e2014-05-24 12:50:23 +00002649 case 64:
2650 SubReg = AArch64::dsub;
2651 break;
2652 case 32:
2653 SubReg = AArch64::ssub;
2654 break;
Oliver Stannard89d15422014-08-27 16:16:04 +00002655 case 16:
2656 SubReg = AArch64::hsub;
2657 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00002658 case 8:
2659 llvm_unreachable("unexpected zext-requiring extract element!");
2660 }
2661 SDValue Extract = CurDAG->getTargetExtractSubreg(SubReg, SDLoc(Node), VT,
2662 Node->getOperand(0));
2663 DEBUG(dbgs() << "ISEL: Custom selection!\n=> ");
2664 DEBUG(Extract->dumpr(CurDAG));
2665 DEBUG(dbgs() << "\n");
Justin Bogner283e3bd2016-05-12 23:10:30 +00002666 ReplaceNode(Node, Extract.getNode());
2667 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002668 }
2669 case ISD::Constant: {
2670 // Materialize zero constants as copies from WZR/XZR. This allows
2671 // the coalescer to propagate these into other instructions.
2672 ConstantSDNode *ConstNode = cast<ConstantSDNode>(Node);
2673 if (ConstNode->isNullValue()) {
Justin Bogner283e3bd2016-05-12 23:10:30 +00002674 if (VT == MVT::i32) {
2675 SDValue New = CurDAG->getCopyFromReg(
2676 CurDAG->getEntryNode(), SDLoc(Node), AArch64::WZR, MVT::i32);
2677 ReplaceNode(Node, New.getNode());
2678 return;
2679 } else if (VT == MVT::i64) {
2680 SDValue New = CurDAG->getCopyFromReg(
2681 CurDAG->getEntryNode(), SDLoc(Node), AArch64::XZR, MVT::i64);
2682 ReplaceNode(Node, New.getNode());
2683 return;
2684 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002685 }
2686 break;
2687 }
2688
2689 case ISD::FrameIndex: {
2690 // Selects to ADDXri FI, 0 which in turn will become ADDXri SP, imm.
2691 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
2692 unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
2693 const TargetLowering *TLI = getTargetLowering();
Mehdi Amini44ede332015-07-09 02:09:04 +00002694 SDValue TFI = CurDAG->getTargetFrameIndex(
2695 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002696 SDLoc DL(Node);
2697 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, DL, MVT::i32),
2698 CurDAG->getTargetConstant(Shifter, DL, MVT::i32) };
Justin Bogner283e3bd2016-05-12 23:10:30 +00002699 CurDAG->SelectNodeTo(Node, AArch64::ADDXri, MVT::i64, Ops);
2700 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002701 }
2702 case ISD::INTRINSIC_W_CHAIN: {
2703 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2704 switch (IntNo) {
2705 default:
2706 break;
2707 case Intrinsic::aarch64_ldaxp:
2708 case Intrinsic::aarch64_ldxp: {
2709 unsigned Op =
2710 IntNo == Intrinsic::aarch64_ldaxp ? AArch64::LDAXPX : AArch64::LDXPX;
2711 SDValue MemAddr = Node->getOperand(2);
2712 SDLoc DL(Node);
2713 SDValue Chain = Node->getOperand(0);
2714
2715 SDNode *Ld = CurDAG->getMachineNode(Op, DL, MVT::i64, MVT::i64,
2716 MVT::Other, MemAddr, Chain);
2717
2718 // Transfer memoperands.
2719 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2720 MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2721 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002722 ReplaceNode(Node, Ld);
2723 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002724 }
2725 case Intrinsic::aarch64_stlxp:
2726 case Intrinsic::aarch64_stxp: {
2727 unsigned Op =
2728 IntNo == Intrinsic::aarch64_stlxp ? AArch64::STLXPX : AArch64::STXPX;
2729 SDLoc DL(Node);
2730 SDValue Chain = Node->getOperand(0);
2731 SDValue ValLo = Node->getOperand(2);
2732 SDValue ValHi = Node->getOperand(3);
2733 SDValue MemAddr = Node->getOperand(4);
2734
2735 // Place arguments in the right order.
Benjamin Kramerea68a942015-02-19 15:26:17 +00002736 SDValue Ops[] = {ValLo, ValHi, MemAddr, Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00002737
2738 SDNode *St = CurDAG->getMachineNode(Op, DL, MVT::i32, MVT::Other, Ops);
2739 // Transfer memoperands.
2740 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2741 MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2742 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
2743
Justin Bogner283e3bd2016-05-12 23:10:30 +00002744 ReplaceNode(Node, St);
2745 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002746 }
2747 case Intrinsic::aarch64_neon_ld1x2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002748 if (VT == MVT::v8i8) {
2749 SelectLoad(Node, 2, AArch64::LD1Twov8b, AArch64::dsub0);
2750 return;
2751 } else if (VT == MVT::v16i8) {
2752 SelectLoad(Node, 2, AArch64::LD1Twov16b, AArch64::qsub0);
2753 return;
2754 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2755 SelectLoad(Node, 2, AArch64::LD1Twov4h, AArch64::dsub0);
2756 return;
2757 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2758 SelectLoad(Node, 2, AArch64::LD1Twov8h, AArch64::qsub0);
2759 return;
2760 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2761 SelectLoad(Node, 2, AArch64::LD1Twov2s, AArch64::dsub0);
2762 return;
2763 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2764 SelectLoad(Node, 2, AArch64::LD1Twov4s, AArch64::qsub0);
2765 return;
2766 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2767 SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2768 return;
2769 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2770 SelectLoad(Node, 2, AArch64::LD1Twov2d, AArch64::qsub0);
2771 return;
2772 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002773 break;
2774 case Intrinsic::aarch64_neon_ld1x3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002775 if (VT == MVT::v8i8) {
2776 SelectLoad(Node, 3, AArch64::LD1Threev8b, AArch64::dsub0);
2777 return;
2778 } else if (VT == MVT::v16i8) {
2779 SelectLoad(Node, 3, AArch64::LD1Threev16b, AArch64::qsub0);
2780 return;
2781 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2782 SelectLoad(Node, 3, AArch64::LD1Threev4h, AArch64::dsub0);
2783 return;
2784 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2785 SelectLoad(Node, 3, AArch64::LD1Threev8h, AArch64::qsub0);
2786 return;
2787 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2788 SelectLoad(Node, 3, AArch64::LD1Threev2s, AArch64::dsub0);
2789 return;
2790 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2791 SelectLoad(Node, 3, AArch64::LD1Threev4s, AArch64::qsub0);
2792 return;
2793 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2794 SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2795 return;
2796 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2797 SelectLoad(Node, 3, AArch64::LD1Threev2d, AArch64::qsub0);
2798 return;
2799 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002800 break;
2801 case Intrinsic::aarch64_neon_ld1x4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002802 if (VT == MVT::v8i8) {
2803 SelectLoad(Node, 4, AArch64::LD1Fourv8b, AArch64::dsub0);
2804 return;
2805 } else if (VT == MVT::v16i8) {
2806 SelectLoad(Node, 4, AArch64::LD1Fourv16b, AArch64::qsub0);
2807 return;
2808 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2809 SelectLoad(Node, 4, AArch64::LD1Fourv4h, AArch64::dsub0);
2810 return;
2811 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2812 SelectLoad(Node, 4, AArch64::LD1Fourv8h, AArch64::qsub0);
2813 return;
2814 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2815 SelectLoad(Node, 4, AArch64::LD1Fourv2s, AArch64::dsub0);
2816 return;
2817 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2818 SelectLoad(Node, 4, AArch64::LD1Fourv4s, AArch64::qsub0);
2819 return;
2820 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2821 SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2822 return;
2823 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2824 SelectLoad(Node, 4, AArch64::LD1Fourv2d, AArch64::qsub0);
2825 return;
2826 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002827 break;
2828 case Intrinsic::aarch64_neon_ld2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002829 if (VT == MVT::v8i8) {
2830 SelectLoad(Node, 2, AArch64::LD2Twov8b, AArch64::dsub0);
2831 return;
2832 } else if (VT == MVT::v16i8) {
2833 SelectLoad(Node, 2, AArch64::LD2Twov16b, AArch64::qsub0);
2834 return;
2835 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2836 SelectLoad(Node, 2, AArch64::LD2Twov4h, AArch64::dsub0);
2837 return;
2838 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2839 SelectLoad(Node, 2, AArch64::LD2Twov8h, AArch64::qsub0);
2840 return;
2841 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2842 SelectLoad(Node, 2, AArch64::LD2Twov2s, AArch64::dsub0);
2843 return;
2844 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2845 SelectLoad(Node, 2, AArch64::LD2Twov4s, AArch64::qsub0);
2846 return;
2847 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2848 SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2849 return;
2850 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2851 SelectLoad(Node, 2, AArch64::LD2Twov2d, AArch64::qsub0);
2852 return;
2853 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002854 break;
2855 case Intrinsic::aarch64_neon_ld3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002856 if (VT == MVT::v8i8) {
2857 SelectLoad(Node, 3, AArch64::LD3Threev8b, AArch64::dsub0);
2858 return;
2859 } else if (VT == MVT::v16i8) {
2860 SelectLoad(Node, 3, AArch64::LD3Threev16b, AArch64::qsub0);
2861 return;
2862 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2863 SelectLoad(Node, 3, AArch64::LD3Threev4h, AArch64::dsub0);
2864 return;
2865 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2866 SelectLoad(Node, 3, AArch64::LD3Threev8h, AArch64::qsub0);
2867 return;
2868 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2869 SelectLoad(Node, 3, AArch64::LD3Threev2s, AArch64::dsub0);
2870 return;
2871 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2872 SelectLoad(Node, 3, AArch64::LD3Threev4s, AArch64::qsub0);
2873 return;
2874 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2875 SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2876 return;
2877 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2878 SelectLoad(Node, 3, AArch64::LD3Threev2d, AArch64::qsub0);
2879 return;
2880 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002881 break;
2882 case Intrinsic::aarch64_neon_ld4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002883 if (VT == MVT::v8i8) {
2884 SelectLoad(Node, 4, AArch64::LD4Fourv8b, AArch64::dsub0);
2885 return;
2886 } else if (VT == MVT::v16i8) {
2887 SelectLoad(Node, 4, AArch64::LD4Fourv16b, AArch64::qsub0);
2888 return;
2889 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2890 SelectLoad(Node, 4, AArch64::LD4Fourv4h, AArch64::dsub0);
2891 return;
2892 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2893 SelectLoad(Node, 4, AArch64::LD4Fourv8h, AArch64::qsub0);
2894 return;
2895 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2896 SelectLoad(Node, 4, AArch64::LD4Fourv2s, AArch64::dsub0);
2897 return;
2898 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2899 SelectLoad(Node, 4, AArch64::LD4Fourv4s, AArch64::qsub0);
2900 return;
2901 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2902 SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2903 return;
2904 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2905 SelectLoad(Node, 4, AArch64::LD4Fourv2d, AArch64::qsub0);
2906 return;
2907 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002908 break;
2909 case Intrinsic::aarch64_neon_ld2r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002910 if (VT == MVT::v8i8) {
2911 SelectLoad(Node, 2, AArch64::LD2Rv8b, AArch64::dsub0);
2912 return;
2913 } else if (VT == MVT::v16i8) {
2914 SelectLoad(Node, 2, AArch64::LD2Rv16b, AArch64::qsub0);
2915 return;
2916 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2917 SelectLoad(Node, 2, AArch64::LD2Rv4h, AArch64::dsub0);
2918 return;
2919 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2920 SelectLoad(Node, 2, AArch64::LD2Rv8h, AArch64::qsub0);
2921 return;
2922 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2923 SelectLoad(Node, 2, AArch64::LD2Rv2s, AArch64::dsub0);
2924 return;
2925 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2926 SelectLoad(Node, 2, AArch64::LD2Rv4s, AArch64::qsub0);
2927 return;
2928 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2929 SelectLoad(Node, 2, AArch64::LD2Rv1d, AArch64::dsub0);
2930 return;
2931 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2932 SelectLoad(Node, 2, AArch64::LD2Rv2d, AArch64::qsub0);
2933 return;
2934 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002935 break;
2936 case Intrinsic::aarch64_neon_ld3r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002937 if (VT == MVT::v8i8) {
2938 SelectLoad(Node, 3, AArch64::LD3Rv8b, AArch64::dsub0);
2939 return;
2940 } else if (VT == MVT::v16i8) {
2941 SelectLoad(Node, 3, AArch64::LD3Rv16b, AArch64::qsub0);
2942 return;
2943 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2944 SelectLoad(Node, 3, AArch64::LD3Rv4h, AArch64::dsub0);
2945 return;
2946 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2947 SelectLoad(Node, 3, AArch64::LD3Rv8h, AArch64::qsub0);
2948 return;
2949 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2950 SelectLoad(Node, 3, AArch64::LD3Rv2s, AArch64::dsub0);
2951 return;
2952 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2953 SelectLoad(Node, 3, AArch64::LD3Rv4s, AArch64::qsub0);
2954 return;
2955 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2956 SelectLoad(Node, 3, AArch64::LD3Rv1d, AArch64::dsub0);
2957 return;
2958 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2959 SelectLoad(Node, 3, AArch64::LD3Rv2d, AArch64::qsub0);
2960 return;
2961 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002962 break;
2963 case Intrinsic::aarch64_neon_ld4r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002964 if (VT == MVT::v8i8) {
2965 SelectLoad(Node, 4, AArch64::LD4Rv8b, AArch64::dsub0);
2966 return;
2967 } else if (VT == MVT::v16i8) {
2968 SelectLoad(Node, 4, AArch64::LD4Rv16b, AArch64::qsub0);
2969 return;
2970 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2971 SelectLoad(Node, 4, AArch64::LD4Rv4h, AArch64::dsub0);
2972 return;
2973 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2974 SelectLoad(Node, 4, AArch64::LD4Rv8h, AArch64::qsub0);
2975 return;
2976 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2977 SelectLoad(Node, 4, AArch64::LD4Rv2s, AArch64::dsub0);
2978 return;
2979 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2980 SelectLoad(Node, 4, AArch64::LD4Rv4s, AArch64::qsub0);
2981 return;
2982 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2983 SelectLoad(Node, 4, AArch64::LD4Rv1d, AArch64::dsub0);
2984 return;
2985 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2986 SelectLoad(Node, 4, AArch64::LD4Rv2d, AArch64::qsub0);
2987 return;
2988 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002989 break;
2990 case Intrinsic::aarch64_neon_ld2lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002991 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
2992 SelectLoadLane(Node, 2, AArch64::LD2i8);
2993 return;
2994 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2995 VT == MVT::v8f16) {
2996 SelectLoadLane(Node, 2, AArch64::LD2i16);
2997 return;
2998 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2999 VT == MVT::v2f32) {
3000 SelectLoadLane(Node, 2, AArch64::LD2i32);
3001 return;
3002 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3003 VT == MVT::v1f64) {
3004 SelectLoadLane(Node, 2, AArch64::LD2i64);
3005 return;
3006 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003007 break;
3008 case Intrinsic::aarch64_neon_ld3lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003009 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3010 SelectLoadLane(Node, 3, AArch64::LD3i8);
3011 return;
3012 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3013 VT == MVT::v8f16) {
3014 SelectLoadLane(Node, 3, AArch64::LD3i16);
3015 return;
3016 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3017 VT == MVT::v2f32) {
3018 SelectLoadLane(Node, 3, AArch64::LD3i32);
3019 return;
3020 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3021 VT == MVT::v1f64) {
3022 SelectLoadLane(Node, 3, AArch64::LD3i64);
3023 return;
3024 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003025 break;
3026 case Intrinsic::aarch64_neon_ld4lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003027 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3028 SelectLoadLane(Node, 4, AArch64::LD4i8);
3029 return;
3030 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3031 VT == MVT::v8f16) {
3032 SelectLoadLane(Node, 4, AArch64::LD4i16);
3033 return;
3034 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3035 VT == MVT::v2f32) {
3036 SelectLoadLane(Node, 4, AArch64::LD4i32);
3037 return;
3038 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3039 VT == MVT::v1f64) {
3040 SelectLoadLane(Node, 4, AArch64::LD4i64);
3041 return;
3042 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003043 break;
3044 }
3045 } break;
3046 case ISD::INTRINSIC_WO_CHAIN: {
3047 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
3048 switch (IntNo) {
3049 default:
3050 break;
3051 case Intrinsic::aarch64_neon_tbl2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003052 SelectTable(Node, 2,
3053 VT == MVT::v8i8 ? AArch64::TBLv8i8Two : AArch64::TBLv16i8Two,
3054 false);
3055 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003056 case Intrinsic::aarch64_neon_tbl3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003057 SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBLv8i8Three
3058 : AArch64::TBLv16i8Three,
3059 false);
3060 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003061 case Intrinsic::aarch64_neon_tbl4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003062 SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBLv8i8Four
3063 : AArch64::TBLv16i8Four,
3064 false);
3065 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003066 case Intrinsic::aarch64_neon_tbx2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003067 SelectTable(Node, 2,
3068 VT == MVT::v8i8 ? AArch64::TBXv8i8Two : AArch64::TBXv16i8Two,
3069 true);
3070 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003071 case Intrinsic::aarch64_neon_tbx3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003072 SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBXv8i8Three
3073 : AArch64::TBXv16i8Three,
3074 true);
3075 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003076 case Intrinsic::aarch64_neon_tbx4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003077 SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBXv8i8Four
3078 : AArch64::TBXv16i8Four,
3079 true);
3080 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003081 case Intrinsic::aarch64_neon_smull:
3082 case Intrinsic::aarch64_neon_umull:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003083 if (tryMULLV64LaneV128(IntNo, Node))
3084 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003085 break;
3086 }
3087 break;
3088 }
3089 case ISD::INTRINSIC_VOID: {
3090 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
3091 if (Node->getNumOperands() >= 3)
3092 VT = Node->getOperand(2)->getValueType(0);
3093 switch (IntNo) {
3094 default:
3095 break;
3096 case Intrinsic::aarch64_neon_st1x2: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003097 if (VT == MVT::v8i8) {
3098 SelectStore(Node, 2, AArch64::ST1Twov8b);
3099 return;
3100 } else if (VT == MVT::v16i8) {
3101 SelectStore(Node, 2, AArch64::ST1Twov16b);
3102 return;
3103 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3104 SelectStore(Node, 2, AArch64::ST1Twov4h);
3105 return;
3106 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3107 SelectStore(Node, 2, AArch64::ST1Twov8h);
3108 return;
3109 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3110 SelectStore(Node, 2, AArch64::ST1Twov2s);
3111 return;
3112 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3113 SelectStore(Node, 2, AArch64::ST1Twov4s);
3114 return;
3115 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3116 SelectStore(Node, 2, AArch64::ST1Twov2d);
3117 return;
3118 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3119 SelectStore(Node, 2, AArch64::ST1Twov1d);
3120 return;
3121 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003122 break;
3123 }
3124 case Intrinsic::aarch64_neon_st1x3: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003125 if (VT == MVT::v8i8) {
3126 SelectStore(Node, 3, AArch64::ST1Threev8b);
3127 return;
3128 } else if (VT == MVT::v16i8) {
3129 SelectStore(Node, 3, AArch64::ST1Threev16b);
3130 return;
3131 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3132 SelectStore(Node, 3, AArch64::ST1Threev4h);
3133 return;
3134 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3135 SelectStore(Node, 3, AArch64::ST1Threev8h);
3136 return;
3137 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3138 SelectStore(Node, 3, AArch64::ST1Threev2s);
3139 return;
3140 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3141 SelectStore(Node, 3, AArch64::ST1Threev4s);
3142 return;
3143 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3144 SelectStore(Node, 3, AArch64::ST1Threev2d);
3145 return;
3146 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3147 SelectStore(Node, 3, AArch64::ST1Threev1d);
3148 return;
3149 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003150 break;
3151 }
3152 case Intrinsic::aarch64_neon_st1x4: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003153 if (VT == MVT::v8i8) {
3154 SelectStore(Node, 4, AArch64::ST1Fourv8b);
3155 return;
3156 } else if (VT == MVT::v16i8) {
3157 SelectStore(Node, 4, AArch64::ST1Fourv16b);
3158 return;
3159 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3160 SelectStore(Node, 4, AArch64::ST1Fourv4h);
3161 return;
3162 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3163 SelectStore(Node, 4, AArch64::ST1Fourv8h);
3164 return;
3165 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3166 SelectStore(Node, 4, AArch64::ST1Fourv2s);
3167 return;
3168 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3169 SelectStore(Node, 4, AArch64::ST1Fourv4s);
3170 return;
3171 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3172 SelectStore(Node, 4, AArch64::ST1Fourv2d);
3173 return;
3174 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3175 SelectStore(Node, 4, AArch64::ST1Fourv1d);
3176 return;
3177 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003178 break;
3179 }
3180 case Intrinsic::aarch64_neon_st2: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003181 if (VT == MVT::v8i8) {
3182 SelectStore(Node, 2, AArch64::ST2Twov8b);
3183 return;
3184 } else if (VT == MVT::v16i8) {
3185 SelectStore(Node, 2, AArch64::ST2Twov16b);
3186 return;
3187 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3188 SelectStore(Node, 2, AArch64::ST2Twov4h);
3189 return;
3190 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3191 SelectStore(Node, 2, AArch64::ST2Twov8h);
3192 return;
3193 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3194 SelectStore(Node, 2, AArch64::ST2Twov2s);
3195 return;
3196 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3197 SelectStore(Node, 2, AArch64::ST2Twov4s);
3198 return;
3199 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3200 SelectStore(Node, 2, AArch64::ST2Twov2d);
3201 return;
3202 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3203 SelectStore(Node, 2, AArch64::ST1Twov1d);
3204 return;
3205 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003206 break;
3207 }
3208 case Intrinsic::aarch64_neon_st3: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003209 if (VT == MVT::v8i8) {
3210 SelectStore(Node, 3, AArch64::ST3Threev8b);
3211 return;
3212 } else if (VT == MVT::v16i8) {
3213 SelectStore(Node, 3, AArch64::ST3Threev16b);
3214 return;
3215 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3216 SelectStore(Node, 3, AArch64::ST3Threev4h);
3217 return;
3218 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3219 SelectStore(Node, 3, AArch64::ST3Threev8h);
3220 return;
3221 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3222 SelectStore(Node, 3, AArch64::ST3Threev2s);
3223 return;
3224 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3225 SelectStore(Node, 3, AArch64::ST3Threev4s);
3226 return;
3227 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3228 SelectStore(Node, 3, AArch64::ST3Threev2d);
3229 return;
3230 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3231 SelectStore(Node, 3, AArch64::ST1Threev1d);
3232 return;
3233 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003234 break;
3235 }
3236 case Intrinsic::aarch64_neon_st4: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003237 if (VT == MVT::v8i8) {
3238 SelectStore(Node, 4, AArch64::ST4Fourv8b);
3239 return;
3240 } else if (VT == MVT::v16i8) {
3241 SelectStore(Node, 4, AArch64::ST4Fourv16b);
3242 return;
3243 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3244 SelectStore(Node, 4, AArch64::ST4Fourv4h);
3245 return;
3246 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3247 SelectStore(Node, 4, AArch64::ST4Fourv8h);
3248 return;
3249 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3250 SelectStore(Node, 4, AArch64::ST4Fourv2s);
3251 return;
3252 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3253 SelectStore(Node, 4, AArch64::ST4Fourv4s);
3254 return;
3255 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3256 SelectStore(Node, 4, AArch64::ST4Fourv2d);
3257 return;
3258 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3259 SelectStore(Node, 4, AArch64::ST1Fourv1d);
3260 return;
3261 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003262 break;
3263 }
3264 case Intrinsic::aarch64_neon_st2lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003265 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3266 SelectStoreLane(Node, 2, AArch64::ST2i8);
3267 return;
3268 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3269 VT == MVT::v8f16) {
3270 SelectStoreLane(Node, 2, AArch64::ST2i16);
3271 return;
3272 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3273 VT == MVT::v2f32) {
3274 SelectStoreLane(Node, 2, AArch64::ST2i32);
3275 return;
3276 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3277 VT == MVT::v1f64) {
3278 SelectStoreLane(Node, 2, AArch64::ST2i64);
3279 return;
3280 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003281 break;
3282 }
3283 case Intrinsic::aarch64_neon_st3lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003284 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3285 SelectStoreLane(Node, 3, AArch64::ST3i8);
3286 return;
3287 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3288 VT == MVT::v8f16) {
3289 SelectStoreLane(Node, 3, AArch64::ST3i16);
3290 return;
3291 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3292 VT == MVT::v2f32) {
3293 SelectStoreLane(Node, 3, AArch64::ST3i32);
3294 return;
3295 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3296 VT == MVT::v1f64) {
3297 SelectStoreLane(Node, 3, AArch64::ST3i64);
3298 return;
3299 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003300 break;
3301 }
3302 case Intrinsic::aarch64_neon_st4lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003303 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3304 SelectStoreLane(Node, 4, AArch64::ST4i8);
3305 return;
3306 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3307 VT == MVT::v8f16) {
3308 SelectStoreLane(Node, 4, AArch64::ST4i16);
3309 return;
3310 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3311 VT == MVT::v2f32) {
3312 SelectStoreLane(Node, 4, AArch64::ST4i32);
3313 return;
3314 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3315 VT == MVT::v1f64) {
3316 SelectStoreLane(Node, 4, AArch64::ST4i64);
3317 return;
3318 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003319 break;
3320 }
3321 }
Mehdi Aminia7583982015-08-23 00:42:57 +00003322 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00003323 }
3324 case AArch64ISD::LD2post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003325 if (VT == MVT::v8i8) {
3326 SelectPostLoad(Node, 2, AArch64::LD2Twov8b_POST, AArch64::dsub0);
3327 return;
3328 } else if (VT == MVT::v16i8) {
3329 SelectPostLoad(Node, 2, AArch64::LD2Twov16b_POST, AArch64::qsub0);
3330 return;
3331 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3332 SelectPostLoad(Node, 2, AArch64::LD2Twov4h_POST, AArch64::dsub0);
3333 return;
3334 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3335 SelectPostLoad(Node, 2, AArch64::LD2Twov8h_POST, AArch64::qsub0);
3336 return;
3337 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3338 SelectPostLoad(Node, 2, AArch64::LD2Twov2s_POST, AArch64::dsub0);
3339 return;
3340 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3341 SelectPostLoad(Node, 2, AArch64::LD2Twov4s_POST, AArch64::qsub0);
3342 return;
3343 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3344 SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
3345 return;
3346 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3347 SelectPostLoad(Node, 2, AArch64::LD2Twov2d_POST, AArch64::qsub0);
3348 return;
3349 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003350 break;
3351 }
3352 case AArch64ISD::LD3post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003353 if (VT == MVT::v8i8) {
3354 SelectPostLoad(Node, 3, AArch64::LD3Threev8b_POST, AArch64::dsub0);
3355 return;
3356 } else if (VT == MVT::v16i8) {
3357 SelectPostLoad(Node, 3, AArch64::LD3Threev16b_POST, AArch64::qsub0);
3358 return;
3359 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3360 SelectPostLoad(Node, 3, AArch64::LD3Threev4h_POST, AArch64::dsub0);
3361 return;
3362 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3363 SelectPostLoad(Node, 3, AArch64::LD3Threev8h_POST, AArch64::qsub0);
3364 return;
3365 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3366 SelectPostLoad(Node, 3, AArch64::LD3Threev2s_POST, AArch64::dsub0);
3367 return;
3368 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3369 SelectPostLoad(Node, 3, AArch64::LD3Threev4s_POST, AArch64::qsub0);
3370 return;
3371 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3372 SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3373 return;
3374 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3375 SelectPostLoad(Node, 3, AArch64::LD3Threev2d_POST, AArch64::qsub0);
3376 return;
3377 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003378 break;
3379 }
3380 case AArch64ISD::LD4post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003381 if (VT == MVT::v8i8) {
3382 SelectPostLoad(Node, 4, AArch64::LD4Fourv8b_POST, AArch64::dsub0);
3383 return;
3384 } else if (VT == MVT::v16i8) {
3385 SelectPostLoad(Node, 4, AArch64::LD4Fourv16b_POST, AArch64::qsub0);
3386 return;
3387 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3388 SelectPostLoad(Node, 4, AArch64::LD4Fourv4h_POST, AArch64::dsub0);
3389 return;
3390 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3391 SelectPostLoad(Node, 4, AArch64::LD4Fourv8h_POST, AArch64::qsub0);
3392 return;
3393 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3394 SelectPostLoad(Node, 4, AArch64::LD4Fourv2s_POST, AArch64::dsub0);
3395 return;
3396 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3397 SelectPostLoad(Node, 4, AArch64::LD4Fourv4s_POST, AArch64::qsub0);
3398 return;
3399 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3400 SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3401 return;
3402 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3403 SelectPostLoad(Node, 4, AArch64::LD4Fourv2d_POST, AArch64::qsub0);
3404 return;
3405 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003406 break;
3407 }
3408 case AArch64ISD::LD1x2post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003409 if (VT == MVT::v8i8) {
3410 SelectPostLoad(Node, 2, AArch64::LD1Twov8b_POST, AArch64::dsub0);
3411 return;
3412 } else if (VT == MVT::v16i8) {
3413 SelectPostLoad(Node, 2, AArch64::LD1Twov16b_POST, AArch64::qsub0);
3414 return;
3415 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3416 SelectPostLoad(Node, 2, AArch64::LD1Twov4h_POST, AArch64::dsub0);
3417 return;
3418 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3419 SelectPostLoad(Node, 2, AArch64::LD1Twov8h_POST, AArch64::qsub0);
3420 return;
3421 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3422 SelectPostLoad(Node, 2, AArch64::LD1Twov2s_POST, AArch64::dsub0);
3423 return;
3424 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3425 SelectPostLoad(Node, 2, AArch64::LD1Twov4s_POST, AArch64::qsub0);
3426 return;
3427 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3428 SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
3429 return;
3430 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3431 SelectPostLoad(Node, 2, AArch64::LD1Twov2d_POST, AArch64::qsub0);
3432 return;
3433 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003434 break;
3435 }
3436 case AArch64ISD::LD1x3post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003437 if (VT == MVT::v8i8) {
3438 SelectPostLoad(Node, 3, AArch64::LD1Threev8b_POST, AArch64::dsub0);
3439 return;
3440 } else if (VT == MVT::v16i8) {
3441 SelectPostLoad(Node, 3, AArch64::LD1Threev16b_POST, AArch64::qsub0);
3442 return;
3443 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3444 SelectPostLoad(Node, 3, AArch64::LD1Threev4h_POST, AArch64::dsub0);
3445 return;
3446 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3447 SelectPostLoad(Node, 3, AArch64::LD1Threev8h_POST, AArch64::qsub0);
3448 return;
3449 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3450 SelectPostLoad(Node, 3, AArch64::LD1Threev2s_POST, AArch64::dsub0);
3451 return;
3452 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3453 SelectPostLoad(Node, 3, AArch64::LD1Threev4s_POST, AArch64::qsub0);
3454 return;
3455 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3456 SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3457 return;
3458 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3459 SelectPostLoad(Node, 3, AArch64::LD1Threev2d_POST, AArch64::qsub0);
3460 return;
3461 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003462 break;
3463 }
3464 case AArch64ISD::LD1x4post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003465 if (VT == MVT::v8i8) {
3466 SelectPostLoad(Node, 4, AArch64::LD1Fourv8b_POST, AArch64::dsub0);
3467 return;
3468 } else if (VT == MVT::v16i8) {
3469 SelectPostLoad(Node, 4, AArch64::LD1Fourv16b_POST, AArch64::qsub0);
3470 return;
3471 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3472 SelectPostLoad(Node, 4, AArch64::LD1Fourv4h_POST, AArch64::dsub0);
3473 return;
3474 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3475 SelectPostLoad(Node, 4, AArch64::LD1Fourv8h_POST, AArch64::qsub0);
3476 return;
3477 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3478 SelectPostLoad(Node, 4, AArch64::LD1Fourv2s_POST, AArch64::dsub0);
3479 return;
3480 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3481 SelectPostLoad(Node, 4, AArch64::LD1Fourv4s_POST, AArch64::qsub0);
3482 return;
3483 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3484 SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3485 return;
3486 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3487 SelectPostLoad(Node, 4, AArch64::LD1Fourv2d_POST, AArch64::qsub0);
3488 return;
3489 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003490 break;
3491 }
3492 case AArch64ISD::LD1DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003493 if (VT == MVT::v8i8) {
3494 SelectPostLoad(Node, 1, AArch64::LD1Rv8b_POST, AArch64::dsub0);
3495 return;
3496 } else if (VT == MVT::v16i8) {
3497 SelectPostLoad(Node, 1, AArch64::LD1Rv16b_POST, AArch64::qsub0);
3498 return;
3499 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3500 SelectPostLoad(Node, 1, AArch64::LD1Rv4h_POST, AArch64::dsub0);
3501 return;
3502 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3503 SelectPostLoad(Node, 1, AArch64::LD1Rv8h_POST, AArch64::qsub0);
3504 return;
3505 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3506 SelectPostLoad(Node, 1, AArch64::LD1Rv2s_POST, AArch64::dsub0);
3507 return;
3508 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3509 SelectPostLoad(Node, 1, AArch64::LD1Rv4s_POST, AArch64::qsub0);
3510 return;
3511 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3512 SelectPostLoad(Node, 1, AArch64::LD1Rv1d_POST, AArch64::dsub0);
3513 return;
3514 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3515 SelectPostLoad(Node, 1, AArch64::LD1Rv2d_POST, AArch64::qsub0);
3516 return;
3517 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003518 break;
3519 }
3520 case AArch64ISD::LD2DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003521 if (VT == MVT::v8i8) {
3522 SelectPostLoad(Node, 2, AArch64::LD2Rv8b_POST, AArch64::dsub0);
3523 return;
3524 } else if (VT == MVT::v16i8) {
3525 SelectPostLoad(Node, 2, AArch64::LD2Rv16b_POST, AArch64::qsub0);
3526 return;
3527 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3528 SelectPostLoad(Node, 2, AArch64::LD2Rv4h_POST, AArch64::dsub0);
3529 return;
3530 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3531 SelectPostLoad(Node, 2, AArch64::LD2Rv8h_POST, AArch64::qsub0);
3532 return;
3533 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3534 SelectPostLoad(Node, 2, AArch64::LD2Rv2s_POST, AArch64::dsub0);
3535 return;
3536 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3537 SelectPostLoad(Node, 2, AArch64::LD2Rv4s_POST, AArch64::qsub0);
3538 return;
3539 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3540 SelectPostLoad(Node, 2, AArch64::LD2Rv1d_POST, AArch64::dsub0);
3541 return;
3542 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3543 SelectPostLoad(Node, 2, AArch64::LD2Rv2d_POST, AArch64::qsub0);
3544 return;
3545 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003546 break;
3547 }
3548 case AArch64ISD::LD3DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003549 if (VT == MVT::v8i8) {
3550 SelectPostLoad(Node, 3, AArch64::LD3Rv8b_POST, AArch64::dsub0);
3551 return;
3552 } else if (VT == MVT::v16i8) {
3553 SelectPostLoad(Node, 3, AArch64::LD3Rv16b_POST, AArch64::qsub0);
3554 return;
3555 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3556 SelectPostLoad(Node, 3, AArch64::LD3Rv4h_POST, AArch64::dsub0);
3557 return;
3558 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3559 SelectPostLoad(Node, 3, AArch64::LD3Rv8h_POST, AArch64::qsub0);
3560 return;
3561 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3562 SelectPostLoad(Node, 3, AArch64::LD3Rv2s_POST, AArch64::dsub0);
3563 return;
3564 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3565 SelectPostLoad(Node, 3, AArch64::LD3Rv4s_POST, AArch64::qsub0);
3566 return;
3567 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3568 SelectPostLoad(Node, 3, AArch64::LD3Rv1d_POST, AArch64::dsub0);
3569 return;
3570 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3571 SelectPostLoad(Node, 3, AArch64::LD3Rv2d_POST, AArch64::qsub0);
3572 return;
3573 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003574 break;
3575 }
3576 case AArch64ISD::LD4DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003577 if (VT == MVT::v8i8) {
3578 SelectPostLoad(Node, 4, AArch64::LD4Rv8b_POST, AArch64::dsub0);
3579 return;
3580 } else if (VT == MVT::v16i8) {
3581 SelectPostLoad(Node, 4, AArch64::LD4Rv16b_POST, AArch64::qsub0);
3582 return;
3583 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3584 SelectPostLoad(Node, 4, AArch64::LD4Rv4h_POST, AArch64::dsub0);
3585 return;
3586 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3587 SelectPostLoad(Node, 4, AArch64::LD4Rv8h_POST, AArch64::qsub0);
3588 return;
3589 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3590 SelectPostLoad(Node, 4, AArch64::LD4Rv2s_POST, AArch64::dsub0);
3591 return;
3592 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3593 SelectPostLoad(Node, 4, AArch64::LD4Rv4s_POST, AArch64::qsub0);
3594 return;
3595 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3596 SelectPostLoad(Node, 4, AArch64::LD4Rv1d_POST, AArch64::dsub0);
3597 return;
3598 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3599 SelectPostLoad(Node, 4, AArch64::LD4Rv2d_POST, AArch64::qsub0);
3600 return;
3601 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003602 break;
3603 }
3604 case AArch64ISD::LD1LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003605 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3606 SelectPostLoadLane(Node, 1, AArch64::LD1i8_POST);
3607 return;
3608 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3609 VT == MVT::v8f16) {
3610 SelectPostLoadLane(Node, 1, AArch64::LD1i16_POST);
3611 return;
3612 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3613 VT == MVT::v2f32) {
3614 SelectPostLoadLane(Node, 1, AArch64::LD1i32_POST);
3615 return;
3616 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3617 VT == MVT::v1f64) {
3618 SelectPostLoadLane(Node, 1, AArch64::LD1i64_POST);
3619 return;
3620 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003621 break;
3622 }
3623 case AArch64ISD::LD2LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003624 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3625 SelectPostLoadLane(Node, 2, AArch64::LD2i8_POST);
3626 return;
3627 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3628 VT == MVT::v8f16) {
3629 SelectPostLoadLane(Node, 2, AArch64::LD2i16_POST);
3630 return;
3631 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3632 VT == MVT::v2f32) {
3633 SelectPostLoadLane(Node, 2, AArch64::LD2i32_POST);
3634 return;
3635 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3636 VT == MVT::v1f64) {
3637 SelectPostLoadLane(Node, 2, AArch64::LD2i64_POST);
3638 return;
3639 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003640 break;
3641 }
3642 case AArch64ISD::LD3LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003643 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3644 SelectPostLoadLane(Node, 3, AArch64::LD3i8_POST);
3645 return;
3646 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3647 VT == MVT::v8f16) {
3648 SelectPostLoadLane(Node, 3, AArch64::LD3i16_POST);
3649 return;
3650 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3651 VT == MVT::v2f32) {
3652 SelectPostLoadLane(Node, 3, AArch64::LD3i32_POST);
3653 return;
3654 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3655 VT == MVT::v1f64) {
3656 SelectPostLoadLane(Node, 3, AArch64::LD3i64_POST);
3657 return;
3658 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003659 break;
3660 }
3661 case AArch64ISD::LD4LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003662 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3663 SelectPostLoadLane(Node, 4, AArch64::LD4i8_POST);
3664 return;
3665 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3666 VT == MVT::v8f16) {
3667 SelectPostLoadLane(Node, 4, AArch64::LD4i16_POST);
3668 return;
3669 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3670 VT == MVT::v2f32) {
3671 SelectPostLoadLane(Node, 4, AArch64::LD4i32_POST);
3672 return;
3673 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3674 VT == MVT::v1f64) {
3675 SelectPostLoadLane(Node, 4, AArch64::LD4i64_POST);
3676 return;
3677 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003678 break;
3679 }
3680 case AArch64ISD::ST2post: {
3681 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003682 if (VT == MVT::v8i8) {
3683 SelectPostStore(Node, 2, AArch64::ST2Twov8b_POST);
3684 return;
3685 } else if (VT == MVT::v16i8) {
3686 SelectPostStore(Node, 2, AArch64::ST2Twov16b_POST);
3687 return;
3688 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3689 SelectPostStore(Node, 2, AArch64::ST2Twov4h_POST);
3690 return;
3691 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3692 SelectPostStore(Node, 2, AArch64::ST2Twov8h_POST);
3693 return;
3694 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3695 SelectPostStore(Node, 2, AArch64::ST2Twov2s_POST);
3696 return;
3697 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3698 SelectPostStore(Node, 2, AArch64::ST2Twov4s_POST);
3699 return;
3700 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3701 SelectPostStore(Node, 2, AArch64::ST2Twov2d_POST);
3702 return;
3703 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3704 SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3705 return;
3706 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003707 break;
3708 }
3709 case AArch64ISD::ST3post: {
3710 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003711 if (VT == MVT::v8i8) {
3712 SelectPostStore(Node, 3, AArch64::ST3Threev8b_POST);
3713 return;
3714 } else if (VT == MVT::v16i8) {
3715 SelectPostStore(Node, 3, AArch64::ST3Threev16b_POST);
3716 return;
3717 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3718 SelectPostStore(Node, 3, AArch64::ST3Threev4h_POST);
3719 return;
3720 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3721 SelectPostStore(Node, 3, AArch64::ST3Threev8h_POST);
3722 return;
3723 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3724 SelectPostStore(Node, 3, AArch64::ST3Threev2s_POST);
3725 return;
3726 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3727 SelectPostStore(Node, 3, AArch64::ST3Threev4s_POST);
3728 return;
3729 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3730 SelectPostStore(Node, 3, AArch64::ST3Threev2d_POST);
3731 return;
3732 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3733 SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3734 return;
3735 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003736 break;
3737 }
3738 case AArch64ISD::ST4post: {
3739 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003740 if (VT == MVT::v8i8) {
3741 SelectPostStore(Node, 4, AArch64::ST4Fourv8b_POST);
3742 return;
3743 } else if (VT == MVT::v16i8) {
3744 SelectPostStore(Node, 4, AArch64::ST4Fourv16b_POST);
3745 return;
3746 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3747 SelectPostStore(Node, 4, AArch64::ST4Fourv4h_POST);
3748 return;
3749 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3750 SelectPostStore(Node, 4, AArch64::ST4Fourv8h_POST);
3751 return;
3752 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3753 SelectPostStore(Node, 4, AArch64::ST4Fourv2s_POST);
3754 return;
3755 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3756 SelectPostStore(Node, 4, AArch64::ST4Fourv4s_POST);
3757 return;
3758 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3759 SelectPostStore(Node, 4, AArch64::ST4Fourv2d_POST);
3760 return;
3761 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3762 SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3763 return;
3764 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003765 break;
3766 }
3767 case AArch64ISD::ST1x2post: {
3768 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003769 if (VT == MVT::v8i8) {
3770 SelectPostStore(Node, 2, AArch64::ST1Twov8b_POST);
3771 return;
3772 } else if (VT == MVT::v16i8) {
3773 SelectPostStore(Node, 2, AArch64::ST1Twov16b_POST);
3774 return;
3775 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3776 SelectPostStore(Node, 2, AArch64::ST1Twov4h_POST);
3777 return;
3778 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3779 SelectPostStore(Node, 2, AArch64::ST1Twov8h_POST);
3780 return;
3781 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3782 SelectPostStore(Node, 2, AArch64::ST1Twov2s_POST);
3783 return;
3784 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3785 SelectPostStore(Node, 2, AArch64::ST1Twov4s_POST);
3786 return;
3787 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3788 SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3789 return;
3790 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3791 SelectPostStore(Node, 2, AArch64::ST1Twov2d_POST);
3792 return;
3793 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003794 break;
3795 }
3796 case AArch64ISD::ST1x3post: {
3797 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003798 if (VT == MVT::v8i8) {
3799 SelectPostStore(Node, 3, AArch64::ST1Threev8b_POST);
3800 return;
3801 } else if (VT == MVT::v16i8) {
3802 SelectPostStore(Node, 3, AArch64::ST1Threev16b_POST);
3803 return;
3804 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3805 SelectPostStore(Node, 3, AArch64::ST1Threev4h_POST);
3806 return;
3807 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3808 SelectPostStore(Node, 3, AArch64::ST1Threev8h_POST);
3809 return;
3810 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3811 SelectPostStore(Node, 3, AArch64::ST1Threev2s_POST);
3812 return;
3813 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3814 SelectPostStore(Node, 3, AArch64::ST1Threev4s_POST);
3815 return;
3816 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3817 SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3818 return;
3819 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3820 SelectPostStore(Node, 3, AArch64::ST1Threev2d_POST);
3821 return;
3822 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003823 break;
3824 }
3825 case AArch64ISD::ST1x4post: {
3826 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003827 if (VT == MVT::v8i8) {
3828 SelectPostStore(Node, 4, AArch64::ST1Fourv8b_POST);
3829 return;
3830 } else if (VT == MVT::v16i8) {
3831 SelectPostStore(Node, 4, AArch64::ST1Fourv16b_POST);
3832 return;
3833 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3834 SelectPostStore(Node, 4, AArch64::ST1Fourv4h_POST);
3835 return;
3836 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3837 SelectPostStore(Node, 4, AArch64::ST1Fourv8h_POST);
3838 return;
3839 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3840 SelectPostStore(Node, 4, AArch64::ST1Fourv2s_POST);
3841 return;
3842 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3843 SelectPostStore(Node, 4, AArch64::ST1Fourv4s_POST);
3844 return;
3845 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3846 SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3847 return;
3848 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3849 SelectPostStore(Node, 4, AArch64::ST1Fourv2d_POST);
3850 return;
3851 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003852 break;
3853 }
3854 case AArch64ISD::ST2LANEpost: {
3855 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003856 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3857 SelectPostStoreLane(Node, 2, AArch64::ST2i8_POST);
3858 return;
3859 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3860 VT == MVT::v8f16) {
3861 SelectPostStoreLane(Node, 2, AArch64::ST2i16_POST);
3862 return;
3863 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3864 VT == MVT::v2f32) {
3865 SelectPostStoreLane(Node, 2, AArch64::ST2i32_POST);
3866 return;
3867 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3868 VT == MVT::v1f64) {
3869 SelectPostStoreLane(Node, 2, AArch64::ST2i64_POST);
3870 return;
3871 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003872 break;
3873 }
3874 case AArch64ISD::ST3LANEpost: {
3875 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003876 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3877 SelectPostStoreLane(Node, 3, AArch64::ST3i8_POST);
3878 return;
3879 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3880 VT == MVT::v8f16) {
3881 SelectPostStoreLane(Node, 3, AArch64::ST3i16_POST);
3882 return;
3883 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3884 VT == MVT::v2f32) {
3885 SelectPostStoreLane(Node, 3, AArch64::ST3i32_POST);
3886 return;
3887 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3888 VT == MVT::v1f64) {
3889 SelectPostStoreLane(Node, 3, AArch64::ST3i64_POST);
3890 return;
3891 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003892 break;
3893 }
3894 case AArch64ISD::ST4LANEpost: {
3895 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003896 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3897 SelectPostStoreLane(Node, 4, AArch64::ST4i8_POST);
3898 return;
3899 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3900 VT == MVT::v8f16) {
3901 SelectPostStoreLane(Node, 4, AArch64::ST4i16_POST);
3902 return;
3903 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3904 VT == MVT::v2f32) {
3905 SelectPostStoreLane(Node, 4, AArch64::ST4i32_POST);
3906 return;
3907 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3908 VT == MVT::v1f64) {
3909 SelectPostStoreLane(Node, 4, AArch64::ST4i64_POST);
3910 return;
3911 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003912 break;
3913 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003914 }
3915
3916 // Select the default instruction
Justin Bogner283e3bd2016-05-12 23:10:30 +00003917 SelectCode(Node);
Tim Northover3b0846e2014-05-24 12:50:23 +00003918}
3919
3920/// createAArch64ISelDag - This pass converts a legalized DAG into a
3921/// AArch64-specific DAG, ready for instruction scheduling.
3922FunctionPass *llvm::createAArch64ISelDag(AArch64TargetMachine &TM,
3923 CodeGenOpt::Level OptLevel) {
3924 return new AArch64DAGToDAGISel(TM, OptLevel);
3925}