blob: fa2696c69ca282940c439054e730b4daf283eb77 [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);
167 bool tryBitfieldInsertOp(SDNode *N);
168 bool tryBitfieldInsertInZeroOp(SDNode *N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000169
Justin Bogner283e3bd2016-05-12 23:10:30 +0000170 bool tryReadRegister(SDNode *N);
171 bool tryWriteRegister(SDNode *N);
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000172
Tim Northover3b0846e2014-05-24 12:50:23 +0000173// Include the pieces autogenerated from the target description.
174#include "AArch64GenDAGISel.inc"
175
176private:
177 bool SelectShiftedRegister(SDValue N, bool AllowROR, SDValue &Reg,
178 SDValue &Shift);
Ahmed Bougachab8886b52015-09-10 01:42:28 +0000179 bool SelectAddrModeIndexed7S(SDValue N, unsigned Size, SDValue &Base,
180 SDValue &OffImm);
Tim Northover3b0846e2014-05-24 12:50:23 +0000181 bool SelectAddrModeIndexed(SDValue N, unsigned Size, SDValue &Base,
182 SDValue &OffImm);
183 bool SelectAddrModeUnscaled(SDValue N, unsigned Size, SDValue &Base,
184 SDValue &OffImm);
185 bool SelectAddrModeWRO(SDValue N, unsigned Size, SDValue &Base,
186 SDValue &Offset, SDValue &SignExtend,
187 SDValue &DoShift);
188 bool SelectAddrModeXRO(SDValue N, unsigned Size, SDValue &Base,
189 SDValue &Offset, SDValue &SignExtend,
190 SDValue &DoShift);
191 bool isWorthFolding(SDValue V) const;
192 bool SelectExtendedSHL(SDValue N, unsigned Size, bool WantExtend,
193 SDValue &Offset, SDValue &SignExtend);
194
195 template<unsigned RegWidth>
196 bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
197 return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
198 }
199
200 bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos, unsigned Width);
Tim Northovercdf15292016-04-14 17:03:29 +0000201
202 void SelectCMP_SWAP(SDNode *N);
203
Tim Northover3b0846e2014-05-24 12:50:23 +0000204};
205} // end anonymous namespace
206
207/// isIntImmediate - This method tests to see if the node is a constant
208/// operand. If so Imm will receive the 32-bit value.
209static bool isIntImmediate(const SDNode *N, uint64_t &Imm) {
210 if (const ConstantSDNode *C = dyn_cast<const ConstantSDNode>(N)) {
211 Imm = C->getZExtValue();
212 return true;
213 }
214 return false;
215}
216
217// isIntImmediate - This method tests to see if a constant operand.
218// If so Imm will receive the value.
219static bool isIntImmediate(SDValue N, uint64_t &Imm) {
220 return isIntImmediate(N.getNode(), Imm);
221}
222
223// isOpcWithIntImmediate - This method tests to see if the node is a specific
224// opcode and that it has a immediate integer right operand.
225// If so Imm will receive the 32 bit value.
226static bool isOpcWithIntImmediate(const SDNode *N, unsigned Opc,
227 uint64_t &Imm) {
228 return N->getOpcode() == Opc &&
229 isIntImmediate(N->getOperand(1).getNode(), Imm);
230}
231
232bool AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(
Daniel Sanders60f1db02015-03-13 12:45:09 +0000233 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
Daniel Sandersf731eee2015-03-23 11:33:15 +0000234 switch(ConstraintID) {
235 default:
236 llvm_unreachable("Unexpected asm memory constraint");
237 case InlineAsm::Constraint_i:
238 case InlineAsm::Constraint_m:
239 case InlineAsm::Constraint_Q:
240 // Require the address to be in a register. That is safe for all AArch64
241 // variants and it is hard to do anything much smarter without knowing
242 // how the operand is used.
243 OutOps.push_back(Op);
244 return false;
245 }
246 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000247}
248
249/// SelectArithImmed - Select an immediate value that can be represented as
250/// a 12-bit value shifted left by either 0 or 12. If so, return true with
251/// Val set to the 12-bit value and Shift set to the shifter operand.
252bool AArch64DAGToDAGISel::SelectArithImmed(SDValue N, SDValue &Val,
253 SDValue &Shift) {
254 // This function is called from the addsub_shifted_imm ComplexPattern,
255 // which lists [imm] as the list of opcode it's interested in, however
256 // we still need to check whether the operand is actually an immediate
257 // here because the ComplexPattern opcode list is only used in
258 // root-level opcode matching.
259 if (!isa<ConstantSDNode>(N.getNode()))
260 return false;
261
262 uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
263 unsigned ShiftAmt;
264
265 if (Immed >> 12 == 0) {
266 ShiftAmt = 0;
267 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
268 ShiftAmt = 12;
269 Immed = Immed >> 12;
270 } else
271 return false;
272
273 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000274 SDLoc dl(N);
275 Val = CurDAG->getTargetConstant(Immed, dl, MVT::i32);
276 Shift = CurDAG->getTargetConstant(ShVal, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000277 return true;
278}
279
280/// SelectNegArithImmed - As above, but negates the value before trying to
281/// select it.
282bool AArch64DAGToDAGISel::SelectNegArithImmed(SDValue N, SDValue &Val,
283 SDValue &Shift) {
284 // This function is called from the addsub_shifted_imm ComplexPattern,
285 // which lists [imm] as the list of opcode it's interested in, however
286 // we still need to check whether the operand is actually an immediate
287 // here because the ComplexPattern opcode list is only used in
288 // root-level opcode matching.
289 if (!isa<ConstantSDNode>(N.getNode()))
290 return false;
291
292 // The immediate operand must be a 24-bit zero-extended immediate.
293 uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
294
295 // This negation is almost always valid, but "cmp wN, #0" and "cmn wN, #0"
296 // have the opposite effect on the C flag, so this pattern mustn't match under
297 // those circumstances.
298 if (Immed == 0)
299 return false;
300
301 if (N.getValueType() == MVT::i32)
302 Immed = ~((uint32_t)Immed) + 1;
303 else
304 Immed = ~Immed + 1ULL;
305 if (Immed & 0xFFFFFFFFFF000000ULL)
306 return false;
307
308 Immed &= 0xFFFFFFULL;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000309 return SelectArithImmed(CurDAG->getConstant(Immed, SDLoc(N), MVT::i32), Val,
310 Shift);
Tim Northover3b0846e2014-05-24 12:50:23 +0000311}
312
313/// getShiftTypeForNode - Translate a shift node to the corresponding
314/// ShiftType value.
315static AArch64_AM::ShiftExtendType getShiftTypeForNode(SDValue N) {
316 switch (N.getOpcode()) {
317 default:
318 return AArch64_AM::InvalidShiftExtend;
319 case ISD::SHL:
320 return AArch64_AM::LSL;
321 case ISD::SRL:
322 return AArch64_AM::LSR;
323 case ISD::SRA:
324 return AArch64_AM::ASR;
325 case ISD::ROTR:
326 return AArch64_AM::ROR;
327 }
328}
329
Eric Christopher25dbdeb2015-03-07 01:39:09 +0000330/// \brief Determine whether it is worth to fold V into an extended register.
Tim Northover3b0846e2014-05-24 12:50:23 +0000331bool AArch64DAGToDAGISel::isWorthFolding(SDValue V) const {
Robin Morisset039781e2014-08-29 21:53:01 +0000332 // it hurts if the value is used at least twice, unless we are optimizing
Tim Northover3b0846e2014-05-24 12:50:23 +0000333 // for code size.
Eric Christopher114fa1c2016-02-29 22:50:49 +0000334 return ForCodeSize || V.hasOneUse();
Tim Northover3b0846e2014-05-24 12:50:23 +0000335}
336
337/// SelectShiftedRegister - Select a "shifted register" operand. If the value
338/// is not shifted, set the Shift operand to default of "LSL 0". The logical
339/// instructions allow the shifted register to be rotated, but the arithmetic
340/// instructions do not. The AllowROR parameter specifies whether ROR is
341/// supported.
342bool AArch64DAGToDAGISel::SelectShiftedRegister(SDValue N, bool AllowROR,
343 SDValue &Reg, SDValue &Shift) {
344 AArch64_AM::ShiftExtendType ShType = getShiftTypeForNode(N);
345 if (ShType == AArch64_AM::InvalidShiftExtend)
346 return false;
347 if (!AllowROR && ShType == AArch64_AM::ROR)
348 return false;
349
350 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
351 unsigned BitSize = N.getValueType().getSizeInBits();
352 unsigned Val = RHS->getZExtValue() & (BitSize - 1);
353 unsigned ShVal = AArch64_AM::getShifterImm(ShType, Val);
354
355 Reg = N.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000356 Shift = CurDAG->getTargetConstant(ShVal, SDLoc(N), MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000357 return isWorthFolding(N);
358 }
359
360 return false;
361}
362
363/// getExtendTypeForNode - Translate an extend node to the corresponding
364/// ExtendType value.
365static AArch64_AM::ShiftExtendType
366getExtendTypeForNode(SDValue N, bool IsLoadStore = false) {
367 if (N.getOpcode() == ISD::SIGN_EXTEND ||
368 N.getOpcode() == ISD::SIGN_EXTEND_INREG) {
369 EVT SrcVT;
370 if (N.getOpcode() == ISD::SIGN_EXTEND_INREG)
371 SrcVT = cast<VTSDNode>(N.getOperand(1))->getVT();
372 else
373 SrcVT = N.getOperand(0).getValueType();
374
375 if (!IsLoadStore && SrcVT == MVT::i8)
376 return AArch64_AM::SXTB;
377 else if (!IsLoadStore && SrcVT == MVT::i16)
378 return AArch64_AM::SXTH;
379 else if (SrcVT == MVT::i32)
380 return AArch64_AM::SXTW;
381 assert(SrcVT != MVT::i64 && "extend from 64-bits?");
382
383 return AArch64_AM::InvalidShiftExtend;
384 } else if (N.getOpcode() == ISD::ZERO_EXTEND ||
385 N.getOpcode() == ISD::ANY_EXTEND) {
386 EVT SrcVT = N.getOperand(0).getValueType();
387 if (!IsLoadStore && SrcVT == MVT::i8)
388 return AArch64_AM::UXTB;
389 else if (!IsLoadStore && SrcVT == MVT::i16)
390 return AArch64_AM::UXTH;
391 else if (SrcVT == MVT::i32)
392 return AArch64_AM::UXTW;
393 assert(SrcVT != MVT::i64 && "extend from 64-bits?");
394
395 return AArch64_AM::InvalidShiftExtend;
396 } else if (N.getOpcode() == ISD::AND) {
397 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
398 if (!CSD)
399 return AArch64_AM::InvalidShiftExtend;
400 uint64_t AndMask = CSD->getZExtValue();
401
402 switch (AndMask) {
403 default:
404 return AArch64_AM::InvalidShiftExtend;
405 case 0xFF:
406 return !IsLoadStore ? AArch64_AM::UXTB : AArch64_AM::InvalidShiftExtend;
407 case 0xFFFF:
408 return !IsLoadStore ? AArch64_AM::UXTH : AArch64_AM::InvalidShiftExtend;
409 case 0xFFFFFFFF:
410 return AArch64_AM::UXTW;
411 }
412 }
413
414 return AArch64_AM::InvalidShiftExtend;
415}
416
417// Helper for SelectMLAV64LaneV128 - Recognize high lane extracts.
418static bool checkHighLaneIndex(SDNode *DL, SDValue &LaneOp, int &LaneIdx) {
419 if (DL->getOpcode() != AArch64ISD::DUPLANE16 &&
420 DL->getOpcode() != AArch64ISD::DUPLANE32)
421 return false;
422
423 SDValue SV = DL->getOperand(0);
424 if (SV.getOpcode() != ISD::INSERT_SUBVECTOR)
425 return false;
426
427 SDValue EV = SV.getOperand(1);
428 if (EV.getOpcode() != ISD::EXTRACT_SUBVECTOR)
429 return false;
430
431 ConstantSDNode *DLidx = cast<ConstantSDNode>(DL->getOperand(1).getNode());
432 ConstantSDNode *EVidx = cast<ConstantSDNode>(EV.getOperand(1).getNode());
433 LaneIdx = DLidx->getSExtValue() + EVidx->getSExtValue();
434 LaneOp = EV.getOperand(0);
435
436 return true;
437}
438
Chad Rosier6c1f0932015-09-17 13:10:27 +0000439// Helper for SelectOpcV64LaneV128 - Recognize operations where one operand is a
Tim Northover3b0846e2014-05-24 12:50:23 +0000440// high lane extract.
441static bool checkV64LaneV128(SDValue Op0, SDValue Op1, SDValue &StdOp,
442 SDValue &LaneOp, int &LaneIdx) {
443
444 if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx)) {
445 std::swap(Op0, Op1);
446 if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx))
447 return false;
448 }
449 StdOp = Op1;
450 return true;
451}
452
453/// SelectMLAV64LaneV128 - AArch64 supports vector MLAs where one multiplicand
454/// is a lane in the upper half of a 128-bit vector. Recognize and select this
455/// so that we don't emit unnecessary lane extracts.
Justin Bogner283e3bd2016-05-12 23:10:30 +0000456bool AArch64DAGToDAGISel::tryMLAV64LaneV128(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000457 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000458 SDValue Op0 = N->getOperand(0);
459 SDValue Op1 = N->getOperand(1);
460 SDValue MLAOp1; // Will hold ordinary multiplicand for MLA.
461 SDValue MLAOp2; // Will hold lane-accessed multiplicand for MLA.
462 int LaneIdx = -1; // Will hold the lane index.
463
464 if (Op1.getOpcode() != ISD::MUL ||
465 !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
466 LaneIdx)) {
467 std::swap(Op0, Op1);
468 if (Op1.getOpcode() != ISD::MUL ||
469 !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
470 LaneIdx))
Justin Bogner283e3bd2016-05-12 23:10:30 +0000471 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000472 }
473
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000474 SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000475
476 SDValue Ops[] = { Op0, MLAOp1, MLAOp2, LaneIdxVal };
477
478 unsigned MLAOpc = ~0U;
479
480 switch (N->getSimpleValueType(0).SimpleTy) {
481 default:
482 llvm_unreachable("Unrecognized MLA.");
483 case MVT::v4i16:
484 MLAOpc = AArch64::MLAv4i16_indexed;
485 break;
486 case MVT::v8i16:
487 MLAOpc = AArch64::MLAv8i16_indexed;
488 break;
489 case MVT::v2i32:
490 MLAOpc = AArch64::MLAv2i32_indexed;
491 break;
492 case MVT::v4i32:
493 MLAOpc = AArch64::MLAv4i32_indexed;
494 break;
495 }
496
Justin Bogner283e3bd2016-05-12 23:10:30 +0000497 ReplaceNode(N, CurDAG->getMachineNode(MLAOpc, dl, N->getValueType(0), Ops));
498 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000499}
500
Justin Bogner283e3bd2016-05-12 23:10:30 +0000501bool AArch64DAGToDAGISel::tryMULLV64LaneV128(unsigned IntNo, SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000502 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000503 SDValue SMULLOp0;
504 SDValue SMULLOp1;
505 int LaneIdx;
506
507 if (!checkV64LaneV128(N->getOperand(1), N->getOperand(2), SMULLOp0, SMULLOp1,
508 LaneIdx))
Justin Bogner283e3bd2016-05-12 23:10:30 +0000509 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +0000510
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000511 SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000512
513 SDValue Ops[] = { SMULLOp0, SMULLOp1, LaneIdxVal };
514
515 unsigned SMULLOpc = ~0U;
516
517 if (IntNo == Intrinsic::aarch64_neon_smull) {
518 switch (N->getSimpleValueType(0).SimpleTy) {
519 default:
520 llvm_unreachable("Unrecognized SMULL.");
521 case MVT::v4i32:
522 SMULLOpc = AArch64::SMULLv4i16_indexed;
523 break;
524 case MVT::v2i64:
525 SMULLOpc = AArch64::SMULLv2i32_indexed;
526 break;
527 }
528 } else if (IntNo == Intrinsic::aarch64_neon_umull) {
529 switch (N->getSimpleValueType(0).SimpleTy) {
530 default:
531 llvm_unreachable("Unrecognized SMULL.");
532 case MVT::v4i32:
533 SMULLOpc = AArch64::UMULLv4i16_indexed;
534 break;
535 case MVT::v2i64:
536 SMULLOpc = AArch64::UMULLv2i32_indexed;
537 break;
538 }
539 } else
540 llvm_unreachable("Unrecognized intrinsic.");
541
Justin Bogner283e3bd2016-05-12 23:10:30 +0000542 ReplaceNode(N, CurDAG->getMachineNode(SMULLOpc, dl, N->getValueType(0), Ops));
543 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000544}
545
546/// Instructions that accept extend modifiers like UXTW expect the register
547/// being extended to be a GPR32, but the incoming DAG might be acting on a
548/// GPR64 (either via SEXT_INREG or AND). Extract the appropriate low bits if
549/// this is the case.
550static SDValue narrowIfNeeded(SelectionDAG *CurDAG, SDValue N) {
551 if (N.getValueType() == MVT::i32)
552 return N;
553
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000554 SDLoc dl(N);
555 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000556 MachineSDNode *Node = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000557 dl, MVT::i32, N, SubReg);
Tim Northover3b0846e2014-05-24 12:50:23 +0000558 return SDValue(Node, 0);
559}
560
561
562/// SelectArithExtendedRegister - Select a "extended register" operand. This
563/// operand folds in an extend followed by an optional left shift.
564bool AArch64DAGToDAGISel::SelectArithExtendedRegister(SDValue N, SDValue &Reg,
565 SDValue &Shift) {
566 unsigned ShiftVal = 0;
567 AArch64_AM::ShiftExtendType Ext;
568
569 if (N.getOpcode() == ISD::SHL) {
570 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
571 if (!CSD)
572 return false;
573 ShiftVal = CSD->getZExtValue();
574 if (ShiftVal > 4)
575 return false;
576
577 Ext = getExtendTypeForNode(N.getOperand(0));
578 if (Ext == AArch64_AM::InvalidShiftExtend)
579 return false;
580
581 Reg = N.getOperand(0).getOperand(0);
582 } else {
583 Ext = getExtendTypeForNode(N);
584 if (Ext == AArch64_AM::InvalidShiftExtend)
585 return false;
586
587 Reg = N.getOperand(0);
588 }
589
590 // AArch64 mandates that the RHS of the operation must use the smallest
Chad Rosier6c1f0932015-09-17 13:10:27 +0000591 // register class that could contain the size being extended from. Thus,
Tim Northover3b0846e2014-05-24 12:50:23 +0000592 // if we're folding a (sext i8), we need the RHS to be a GPR32, even though
593 // there might not be an actual 32-bit value in the program. We can
594 // (harmlessly) synthesize one by injected an EXTRACT_SUBREG here.
595 assert(Ext != AArch64_AM::UXTX && Ext != AArch64_AM::SXTX);
596 Reg = narrowIfNeeded(CurDAG, Reg);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000597 Shift = CurDAG->getTargetConstant(getArithExtendImm(Ext, ShiftVal), SDLoc(N),
598 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000599 return isWorthFolding(N);
600}
601
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000602/// If there's a use of this ADDlow that's not itself a load/store then we'll
603/// need to create a real ADD instruction from it anyway and there's no point in
604/// folding it into the mem op. Theoretically, it shouldn't matter, but there's
605/// a single pseudo-instruction for an ADRP/ADD pair so over-aggressive folding
Chad Rosier6c1f0932015-09-17 13:10:27 +0000606/// leads to duplicated ADRP instructions.
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000607static bool isWorthFoldingADDlow(SDValue N) {
608 for (auto Use : N->uses()) {
609 if (Use->getOpcode() != ISD::LOAD && Use->getOpcode() != ISD::STORE &&
610 Use->getOpcode() != ISD::ATOMIC_LOAD &&
611 Use->getOpcode() != ISD::ATOMIC_STORE)
612 return false;
613
614 // ldar and stlr have much more restrictive addressing modes (just a
615 // register).
JF Bastien800f87a2016-04-06 21:19:33 +0000616 if (isStrongerThanMonotonic(cast<MemSDNode>(Use)->getOrdering()))
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000617 return false;
618 }
619
620 return true;
621}
622
Ahmed Bougachab8886b52015-09-10 01:42:28 +0000623/// SelectAddrModeIndexed7S - Select a "register plus scaled signed 7-bit
624/// immediate" address. The "Size" argument is the size in bytes of the memory
625/// reference, which determines the scale.
626bool AArch64DAGToDAGISel::SelectAddrModeIndexed7S(SDValue N, unsigned Size,
627 SDValue &Base,
628 SDValue &OffImm) {
629 SDLoc dl(N);
Ahmed Bougacha05541452015-09-10 01:54:43 +0000630 const DataLayout &DL = CurDAG->getDataLayout();
631 const TargetLowering *TLI = getTargetLowering();
632 if (N.getOpcode() == ISD::FrameIndex) {
633 int FI = cast<FrameIndexSDNode>(N)->getIndex();
634 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
635 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
636 return true;
637 }
638
Ahmed Bougachac0ac38d2015-09-10 01:48:29 +0000639 // As opposed to the (12-bit) Indexed addressing mode below, the 7-bit signed
640 // selected here doesn't support labels/immediates, only base+offset.
641
642 if (CurDAG->isBaseWithConstantOffset(N)) {
643 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
644 int64_t RHSC = RHS->getSExtValue();
645 unsigned Scale = Log2_32(Size);
Steven Wue3b1f2b2015-09-10 16:32:28 +0000646 if ((RHSC & (Size - 1)) == 0 && RHSC >= -(0x40 << Scale) &&
Ahmed Bougachac0ac38d2015-09-10 01:48:29 +0000647 RHSC < (0x40 << Scale)) {
648 Base = N.getOperand(0);
Ahmed Bougacha05541452015-09-10 01:54:43 +0000649 if (Base.getOpcode() == ISD::FrameIndex) {
650 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
651 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
652 }
Ahmed Bougachac0ac38d2015-09-10 01:48:29 +0000653 OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
654 return true;
655 }
656 }
657 }
658
Ahmed Bougachab8886b52015-09-10 01:42:28 +0000659 // Base only. The address will be materialized into a register before
660 // the memory is accessed.
661 // add x0, Xbase, #offset
662 // stp x1, x2, [x0]
663 Base = N;
664 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
665 return true;
666}
667
Tim Northover3b0846e2014-05-24 12:50:23 +0000668/// SelectAddrModeIndexed - Select a "register plus scaled unsigned 12-bit
669/// immediate" address. The "Size" argument is the size in bytes of the memory
670/// reference, which determines the scale.
671bool AArch64DAGToDAGISel::SelectAddrModeIndexed(SDValue N, unsigned Size,
672 SDValue &Base, SDValue &OffImm) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000673 SDLoc dl(N);
Mehdi Amini44ede332015-07-09 02:09:04 +0000674 const DataLayout &DL = CurDAG->getDataLayout();
Tim Northover3b0846e2014-05-24 12:50:23 +0000675 const TargetLowering *TLI = getTargetLowering();
676 if (N.getOpcode() == ISD::FrameIndex) {
677 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000678 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000679 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000680 return true;
681 }
682
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000683 if (N.getOpcode() == AArch64ISD::ADDlow && isWorthFoldingADDlow(N)) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000684 GlobalAddressSDNode *GAN =
685 dyn_cast<GlobalAddressSDNode>(N.getOperand(1).getNode());
686 Base = N.getOperand(0);
687 OffImm = N.getOperand(1);
688 if (!GAN)
689 return true;
690
691 const GlobalValue *GV = GAN->getGlobal();
692 unsigned Alignment = GV->getAlignment();
Manuel Jacob5f6eaac2016-01-16 20:30:46 +0000693 Type *Ty = GV->getValueType();
Tim Northover4a8ac262014-12-02 23:53:43 +0000694 if (Alignment == 0 && Ty->isSized())
Mehdi Amini44ede332015-07-09 02:09:04 +0000695 Alignment = DL.getABITypeAlignment(Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000696
697 if (Alignment >= Size)
698 return true;
699 }
700
701 if (CurDAG->isBaseWithConstantOffset(N)) {
702 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
703 int64_t RHSC = (int64_t)RHS->getZExtValue();
704 unsigned Scale = Log2_32(Size);
705 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
706 Base = N.getOperand(0);
707 if (Base.getOpcode() == ISD::FrameIndex) {
708 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000709 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
Tim Northover3b0846e2014-05-24 12:50:23 +0000710 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000711 OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000712 return true;
713 }
714 }
715 }
716
717 // Before falling back to our general case, check if the unscaled
718 // instructions can handle this. If so, that's preferable.
719 if (SelectAddrModeUnscaled(N, Size, Base, OffImm))
720 return false;
721
722 // Base only. The address will be materialized into a register before
723 // the memory is accessed.
724 // add x0, Xbase, #offset
725 // ldr x0, [x0]
726 Base = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000727 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000728 return true;
729}
730
731/// SelectAddrModeUnscaled - Select a "register plus unscaled signed 9-bit
732/// immediate" address. This should only match when there is an offset that
733/// is not valid for a scaled immediate addressing mode. The "Size" argument
734/// is the size in bytes of the memory reference, which is needed here to know
735/// what is valid for a scaled immediate.
736bool AArch64DAGToDAGISel::SelectAddrModeUnscaled(SDValue N, unsigned Size,
737 SDValue &Base,
738 SDValue &OffImm) {
739 if (!CurDAG->isBaseWithConstantOffset(N))
740 return false;
741 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
742 int64_t RHSC = RHS->getSExtValue();
743 // If the offset is valid as a scaled immediate, don't match here.
744 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 &&
745 RHSC < (0x1000 << Log2_32(Size)))
746 return false;
747 if (RHSC >= -256 && RHSC < 256) {
748 Base = N.getOperand(0);
749 if (Base.getOpcode() == ISD::FrameIndex) {
750 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
751 const TargetLowering *TLI = getTargetLowering();
Mehdi Amini44ede332015-07-09 02:09:04 +0000752 Base = CurDAG->getTargetFrameIndex(
753 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Tim Northover3b0846e2014-05-24 12:50:23 +0000754 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000755 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000756 return true;
757 }
758 }
759 return false;
760}
761
762static SDValue Widen(SelectionDAG *CurDAG, SDValue N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000763 SDLoc dl(N);
764 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000765 SDValue ImpDef = SDValue(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000766 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::i64), 0);
Tim Northover3b0846e2014-05-24 12:50:23 +0000767 MachineSDNode *Node = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000768 TargetOpcode::INSERT_SUBREG, dl, MVT::i64, ImpDef, N, SubReg);
Tim Northover3b0846e2014-05-24 12:50:23 +0000769 return SDValue(Node, 0);
770}
771
772/// \brief Check if the given SHL node (\p N), can be used to form an
773/// extended register for an addressing mode.
774bool AArch64DAGToDAGISel::SelectExtendedSHL(SDValue N, unsigned Size,
775 bool WantExtend, SDValue &Offset,
776 SDValue &SignExtend) {
777 assert(N.getOpcode() == ISD::SHL && "Invalid opcode.");
778 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
779 if (!CSD || (CSD->getZExtValue() & 0x7) != CSD->getZExtValue())
780 return false;
781
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000782 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000783 if (WantExtend) {
784 AArch64_AM::ShiftExtendType Ext =
785 getExtendTypeForNode(N.getOperand(0), true);
786 if (Ext == AArch64_AM::InvalidShiftExtend)
787 return false;
788
789 Offset = narrowIfNeeded(CurDAG, N.getOperand(0).getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000790 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
791 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000792 } else {
793 Offset = N.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000794 SignExtend = CurDAG->getTargetConstant(0, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000795 }
796
797 unsigned LegalShiftVal = Log2_32(Size);
798 unsigned ShiftVal = CSD->getZExtValue();
799
800 if (ShiftVal != 0 && ShiftVal != LegalShiftVal)
801 return false;
802
Eric Christopher114fa1c2016-02-29 22:50:49 +0000803 return isWorthFolding(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000804}
805
806bool AArch64DAGToDAGISel::SelectAddrModeWRO(SDValue N, unsigned Size,
807 SDValue &Base, SDValue &Offset,
808 SDValue &SignExtend,
809 SDValue &DoShift) {
810 if (N.getOpcode() != ISD::ADD)
811 return false;
812 SDValue LHS = N.getOperand(0);
813 SDValue RHS = N.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000814 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000815
816 // We don't want to match immediate adds here, because they are better lowered
817 // to the register-immediate addressing modes.
818 if (isa<ConstantSDNode>(LHS) || isa<ConstantSDNode>(RHS))
819 return false;
820
821 // Check if this particular node is reused in any non-memory related
822 // operation. If yes, do not try to fold this node into the address
823 // computation, since the computation will be kept.
824 const SDNode *Node = N.getNode();
825 for (SDNode *UI : Node->uses()) {
826 if (!isa<MemSDNode>(*UI))
827 return false;
828 }
829
830 // Remember if it is worth folding N when it produces extended register.
831 bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
832
833 // Try to match a shifted extend on the RHS.
834 if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
835 SelectExtendedSHL(RHS, Size, true, Offset, SignExtend)) {
836 Base = LHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000837 DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000838 return true;
839 }
840
841 // Try to match a shifted extend on the LHS.
842 if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
843 SelectExtendedSHL(LHS, Size, true, Offset, SignExtend)) {
844 Base = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000845 DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000846 return true;
847 }
848
849 // There was no shift, whatever else we find.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000850 DoShift = CurDAG->getTargetConstant(false, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000851
852 AArch64_AM::ShiftExtendType Ext = AArch64_AM::InvalidShiftExtend;
853 // Try to match an unshifted extend on the LHS.
854 if (IsExtendedRegisterWorthFolding &&
855 (Ext = getExtendTypeForNode(LHS, true)) !=
856 AArch64_AM::InvalidShiftExtend) {
857 Base = RHS;
858 Offset = narrowIfNeeded(CurDAG, LHS.getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000859 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
860 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000861 if (isWorthFolding(LHS))
862 return true;
863 }
864
865 // Try to match an unshifted extend on the RHS.
866 if (IsExtendedRegisterWorthFolding &&
867 (Ext = getExtendTypeForNode(RHS, true)) !=
868 AArch64_AM::InvalidShiftExtend) {
869 Base = LHS;
870 Offset = narrowIfNeeded(CurDAG, RHS.getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000871 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
872 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000873 if (isWorthFolding(RHS))
874 return true;
875 }
876
877 return false;
878}
879
Hao Liu3cb826c2014-10-14 06:50:36 +0000880// Check if the given immediate is preferred by ADD. If an immediate can be
881// encoded in an ADD, or it can be encoded in an "ADD LSL #12" and can not be
882// encoded by one MOVZ, return true.
883static bool isPreferredADD(int64_t ImmOff) {
884 // Constant in [0x0, 0xfff] can be encoded in ADD.
885 if ((ImmOff & 0xfffffffffffff000LL) == 0x0LL)
886 return true;
887 // Check if it can be encoded in an "ADD LSL #12".
888 if ((ImmOff & 0xffffffffff000fffLL) == 0x0LL)
889 // As a single MOVZ is faster than a "ADD of LSL #12", ignore such constant.
890 return (ImmOff & 0xffffffffff00ffffLL) != 0x0LL &&
891 (ImmOff & 0xffffffffffff0fffLL) != 0x0LL;
892 return false;
893}
894
Tim Northover3b0846e2014-05-24 12:50:23 +0000895bool AArch64DAGToDAGISel::SelectAddrModeXRO(SDValue N, unsigned Size,
896 SDValue &Base, SDValue &Offset,
897 SDValue &SignExtend,
898 SDValue &DoShift) {
899 if (N.getOpcode() != ISD::ADD)
900 return false;
901 SDValue LHS = N.getOperand(0);
902 SDValue RHS = N.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000903 SDLoc DL(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000904
Tim Northover3b0846e2014-05-24 12:50:23 +0000905 // Check if this particular node is reused in any non-memory related
906 // operation. If yes, do not try to fold this node into the address
907 // computation, since the computation will be kept.
908 const SDNode *Node = N.getNode();
909 for (SDNode *UI : Node->uses()) {
910 if (!isa<MemSDNode>(*UI))
911 return false;
912 }
913
Hao Liu3cb826c2014-10-14 06:50:36 +0000914 // Watch out if RHS is a wide immediate, it can not be selected into
915 // [BaseReg+Imm] addressing mode. Also it may not be able to be encoded into
916 // ADD/SUB. Instead it will use [BaseReg + 0] address mode and generate
917 // instructions like:
918 // MOV X0, WideImmediate
919 // ADD X1, BaseReg, X0
920 // LDR X2, [X1, 0]
921 // For such situation, using [BaseReg, XReg] addressing mode can save one
922 // ADD/SUB:
923 // MOV X0, WideImmediate
924 // LDR X2, [BaseReg, X0]
925 if (isa<ConstantSDNode>(RHS)) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000926 int64_t ImmOff = (int64_t)cast<ConstantSDNode>(RHS)->getZExtValue();
Hao Liu3cb826c2014-10-14 06:50:36 +0000927 unsigned Scale = Log2_32(Size);
Chad Rosier6c1f0932015-09-17 13:10:27 +0000928 // Skip the immediate can be selected by load/store addressing mode.
Hao Liu3cb826c2014-10-14 06:50:36 +0000929 // Also skip the immediate can be encoded by a single ADD (SUB is also
930 // checked by using -ImmOff).
931 if ((ImmOff % Size == 0 && ImmOff >= 0 && ImmOff < (0x1000 << Scale)) ||
932 isPreferredADD(ImmOff) || isPreferredADD(-ImmOff))
933 return false;
934
Hao Liu3cb826c2014-10-14 06:50:36 +0000935 SDValue Ops[] = { RHS };
936 SDNode *MOVI =
937 CurDAG->getMachineNode(AArch64::MOVi64imm, DL, MVT::i64, Ops);
938 SDValue MOVIV = SDValue(MOVI, 0);
939 // This ADD of two X register will be selected into [Reg+Reg] mode.
940 N = CurDAG->getNode(ISD::ADD, DL, MVT::i64, LHS, MOVIV);
941 }
942
Tim Northover3b0846e2014-05-24 12:50:23 +0000943 // Remember if it is worth folding N when it produces extended register.
944 bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
945
946 // Try to match a shifted extend on the RHS.
947 if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
948 SelectExtendedSHL(RHS, Size, false, Offset, SignExtend)) {
949 Base = LHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000950 DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000951 return true;
952 }
953
954 // Try to match a shifted extend on the LHS.
955 if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
956 SelectExtendedSHL(LHS, Size, false, Offset, SignExtend)) {
957 Base = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000958 DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000959 return true;
960 }
961
962 // Match any non-shifted, non-extend, non-immediate add expression.
963 Base = LHS;
964 Offset = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000965 SignExtend = CurDAG->getTargetConstant(false, DL, MVT::i32);
966 DoShift = CurDAG->getTargetConstant(false, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000967 // Reg1 + Reg2 is free: no check needed.
968 return true;
969}
970
971SDValue AArch64DAGToDAGISel::createDTuple(ArrayRef<SDValue> Regs) {
Benjamin Kramerea68a942015-02-19 15:26:17 +0000972 static const unsigned RegClassIDs[] = {
Tim Northover3b0846e2014-05-24 12:50:23 +0000973 AArch64::DDRegClassID, AArch64::DDDRegClassID, AArch64::DDDDRegClassID};
Benjamin Kramerea68a942015-02-19 15:26:17 +0000974 static const unsigned SubRegs[] = {AArch64::dsub0, AArch64::dsub1,
975 AArch64::dsub2, AArch64::dsub3};
Tim Northover3b0846e2014-05-24 12:50:23 +0000976
977 return createTuple(Regs, RegClassIDs, SubRegs);
978}
979
980SDValue AArch64DAGToDAGISel::createQTuple(ArrayRef<SDValue> Regs) {
Benjamin Kramerea68a942015-02-19 15:26:17 +0000981 static const unsigned RegClassIDs[] = {
Tim Northover3b0846e2014-05-24 12:50:23 +0000982 AArch64::QQRegClassID, AArch64::QQQRegClassID, AArch64::QQQQRegClassID};
Benjamin Kramerea68a942015-02-19 15:26:17 +0000983 static const unsigned SubRegs[] = {AArch64::qsub0, AArch64::qsub1,
984 AArch64::qsub2, AArch64::qsub3};
Tim Northover3b0846e2014-05-24 12:50:23 +0000985
986 return createTuple(Regs, RegClassIDs, SubRegs);
987}
988
989SDValue AArch64DAGToDAGISel::createTuple(ArrayRef<SDValue> Regs,
Benjamin Kramerea68a942015-02-19 15:26:17 +0000990 const unsigned RegClassIDs[],
991 const unsigned SubRegs[]) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000992 // There's no special register-class for a vector-list of 1 element: it's just
993 // a vector.
994 if (Regs.size() == 1)
995 return Regs[0];
996
997 assert(Regs.size() >= 2 && Regs.size() <= 4);
998
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000999 SDLoc DL(Regs[0]);
Tim Northover3b0846e2014-05-24 12:50:23 +00001000
1001 SmallVector<SDValue, 4> Ops;
1002
1003 // First operand of REG_SEQUENCE is the desired RegClass.
1004 Ops.push_back(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001005 CurDAG->getTargetConstant(RegClassIDs[Regs.size() - 2], DL, MVT::i32));
Tim Northover3b0846e2014-05-24 12:50:23 +00001006
1007 // Then we get pairs of source & subregister-position for the components.
1008 for (unsigned i = 0; i < Regs.size(); ++i) {
1009 Ops.push_back(Regs[i]);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001010 Ops.push_back(CurDAG->getTargetConstant(SubRegs[i], DL, MVT::i32));
Tim Northover3b0846e2014-05-24 12:50:23 +00001011 }
1012
1013 SDNode *N =
1014 CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops);
1015 return SDValue(N, 0);
1016}
1017
Justin Bogner283e3bd2016-05-12 23:10:30 +00001018void AArch64DAGToDAGISel::SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc,
1019 bool isExt) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001020 SDLoc dl(N);
1021 EVT VT = N->getValueType(0);
1022
1023 unsigned ExtOff = isExt;
1024
1025 // Form a REG_SEQUENCE to force register allocation.
1026 unsigned Vec0Off = ExtOff + 1;
1027 SmallVector<SDValue, 4> Regs(N->op_begin() + Vec0Off,
1028 N->op_begin() + Vec0Off + NumVecs);
1029 SDValue RegSeq = createQTuple(Regs);
1030
1031 SmallVector<SDValue, 6> Ops;
1032 if (isExt)
1033 Ops.push_back(N->getOperand(1));
1034 Ops.push_back(RegSeq);
1035 Ops.push_back(N->getOperand(NumVecs + ExtOff + 1));
Justin Bogner283e3bd2016-05-12 23:10:30 +00001036 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
Tim Northover3b0846e2014-05-24 12:50:23 +00001037}
1038
Justin Bogner283e3bd2016-05-12 23:10:30 +00001039bool AArch64DAGToDAGISel::tryIndexedLoad(SDNode *N) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001040 LoadSDNode *LD = cast<LoadSDNode>(N);
1041 if (LD->isUnindexed())
Justin Bogner283e3bd2016-05-12 23:10:30 +00001042 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001043 EVT VT = LD->getMemoryVT();
1044 EVT DstVT = N->getValueType(0);
1045 ISD::MemIndexedMode AM = LD->getAddressingMode();
1046 bool IsPre = AM == ISD::PRE_INC || AM == ISD::PRE_DEC;
1047
1048 // We're not doing validity checking here. That was done when checking
1049 // if we should mark the load as indexed or not. We're just selecting
1050 // the right instruction.
1051 unsigned Opcode = 0;
1052
1053 ISD::LoadExtType ExtType = LD->getExtensionType();
1054 bool InsertTo64 = false;
1055 if (VT == MVT::i64)
1056 Opcode = IsPre ? AArch64::LDRXpre : AArch64::LDRXpost;
1057 else if (VT == MVT::i32) {
1058 if (ExtType == ISD::NON_EXTLOAD)
1059 Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
1060 else if (ExtType == ISD::SEXTLOAD)
1061 Opcode = IsPre ? AArch64::LDRSWpre : AArch64::LDRSWpost;
1062 else {
1063 Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
1064 InsertTo64 = true;
1065 // The result of the load is only i32. It's the subreg_to_reg that makes
1066 // it into an i64.
1067 DstVT = MVT::i32;
1068 }
1069 } else if (VT == MVT::i16) {
1070 if (ExtType == ISD::SEXTLOAD) {
1071 if (DstVT == MVT::i64)
1072 Opcode = IsPre ? AArch64::LDRSHXpre : AArch64::LDRSHXpost;
1073 else
1074 Opcode = IsPre ? AArch64::LDRSHWpre : AArch64::LDRSHWpost;
1075 } else {
1076 Opcode = IsPre ? AArch64::LDRHHpre : AArch64::LDRHHpost;
1077 InsertTo64 = DstVT == MVT::i64;
1078 // The result of the load is only i32. It's the subreg_to_reg that makes
1079 // it into an i64.
1080 DstVT = MVT::i32;
1081 }
1082 } else if (VT == MVT::i8) {
1083 if (ExtType == ISD::SEXTLOAD) {
1084 if (DstVT == MVT::i64)
1085 Opcode = IsPre ? AArch64::LDRSBXpre : AArch64::LDRSBXpost;
1086 else
1087 Opcode = IsPre ? AArch64::LDRSBWpre : AArch64::LDRSBWpost;
1088 } else {
1089 Opcode = IsPre ? AArch64::LDRBBpre : AArch64::LDRBBpost;
1090 InsertTo64 = DstVT == MVT::i64;
1091 // The result of the load is only i32. It's the subreg_to_reg that makes
1092 // it into an i64.
1093 DstVT = MVT::i32;
1094 }
Ahmed Bougachae0e12db2015-08-04 01:29:38 +00001095 } else if (VT == MVT::f16) {
1096 Opcode = IsPre ? AArch64::LDRHpre : AArch64::LDRHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +00001097 } else if (VT == MVT::f32) {
1098 Opcode = IsPre ? AArch64::LDRSpre : AArch64::LDRSpost;
1099 } else if (VT == MVT::f64 || VT.is64BitVector()) {
1100 Opcode = IsPre ? AArch64::LDRDpre : AArch64::LDRDpost;
1101 } else if (VT.is128BitVector()) {
1102 Opcode = IsPre ? AArch64::LDRQpre : AArch64::LDRQpost;
1103 } else
Justin Bogner283e3bd2016-05-12 23:10:30 +00001104 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001105 SDValue Chain = LD->getChain();
1106 SDValue Base = LD->getBasePtr();
1107 ConstantSDNode *OffsetOp = cast<ConstantSDNode>(LD->getOffset());
1108 int OffsetVal = (int)OffsetOp->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001109 SDLoc dl(N);
1110 SDValue Offset = CurDAG->getTargetConstant(OffsetVal, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +00001111 SDValue Ops[] = { Base, Offset, Chain };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001112 SDNode *Res = CurDAG->getMachineNode(Opcode, dl, MVT::i64, DstVT,
Tim Northover3b0846e2014-05-24 12:50:23 +00001113 MVT::Other, Ops);
1114 // Either way, we're replacing the node, so tell the caller that.
Tim Northover3b0846e2014-05-24 12:50:23 +00001115 SDValue LoadedVal = SDValue(Res, 1);
1116 if (InsertTo64) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001117 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +00001118 LoadedVal =
1119 SDValue(CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001120 AArch64::SUBREG_TO_REG, dl, MVT::i64,
1121 CurDAG->getTargetConstant(0, dl, MVT::i64), LoadedVal,
1122 SubReg),
Tim Northover3b0846e2014-05-24 12:50:23 +00001123 0);
1124 }
1125
1126 ReplaceUses(SDValue(N, 0), LoadedVal);
1127 ReplaceUses(SDValue(N, 1), SDValue(Res, 0));
1128 ReplaceUses(SDValue(N, 2), SDValue(Res, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00001129 CurDAG->RemoveDeadNode(N);
Justin Bogner283e3bd2016-05-12 23:10:30 +00001130 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001131}
1132
Justin Bogner283e3bd2016-05-12 23:10:30 +00001133void AArch64DAGToDAGISel::SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
1134 unsigned SubRegIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001135 SDLoc dl(N);
1136 EVT VT = N->getValueType(0);
1137 SDValue Chain = N->getOperand(0);
1138
Benjamin Kramerea68a942015-02-19 15:26:17 +00001139 SDValue Ops[] = {N->getOperand(2), // Mem operand;
1140 Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00001141
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001142 const EVT ResTys[] = {MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001143
1144 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1145 SDValue SuperReg = SDValue(Ld, 0);
1146 for (unsigned i = 0; i < NumVecs; ++i)
1147 ReplaceUses(SDValue(N, i),
1148 CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
1149
1150 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
Justin Bogner3525da72016-05-12 20:54:27 +00001151 CurDAG->RemoveDeadNode(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001152}
1153
Justin Bogner283e3bd2016-05-12 23:10:30 +00001154void AArch64DAGToDAGISel::SelectPostLoad(SDNode *N, unsigned NumVecs,
1155 unsigned Opc, unsigned SubRegIdx) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001156 SDLoc dl(N);
1157 EVT VT = N->getValueType(0);
1158 SDValue Chain = N->getOperand(0);
1159
Benjamin Kramerea68a942015-02-19 15:26:17 +00001160 SDValue Ops[] = {N->getOperand(1), // Mem operand
1161 N->getOperand(2), // Incremental
1162 Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00001163
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001164 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1165 MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001166
1167 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1168
1169 // Update uses of write back register
1170 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1171
1172 // Update uses of vector list
1173 SDValue SuperReg = SDValue(Ld, 1);
1174 if (NumVecs == 1)
1175 ReplaceUses(SDValue(N, 0), SuperReg);
1176 else
1177 for (unsigned i = 0; i < NumVecs; ++i)
1178 ReplaceUses(SDValue(N, i),
1179 CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
1180
1181 // Update the chain
1182 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00001183 CurDAG->RemoveDeadNode(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001184}
1185
Justin Bogner283e3bd2016-05-12 23:10:30 +00001186void AArch64DAGToDAGISel::SelectStore(SDNode *N, unsigned NumVecs,
1187 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001188 SDLoc dl(N);
1189 EVT VT = N->getOperand(2)->getValueType(0);
1190
1191 // Form a REG_SEQUENCE to force register allocation.
1192 bool Is128Bit = VT.getSizeInBits() == 128;
1193 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1194 SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1195
Benjamin Kramerea68a942015-02-19 15:26:17 +00001196 SDValue Ops[] = {RegSeq, N->getOperand(NumVecs + 2), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001197 SDNode *St = CurDAG->getMachineNode(Opc, dl, N->getValueType(0), Ops);
1198
Justin Bogner283e3bd2016-05-12 23:10:30 +00001199 ReplaceNode(N, St);
Tim Northover3b0846e2014-05-24 12:50:23 +00001200}
1201
Justin Bogner283e3bd2016-05-12 23:10:30 +00001202void AArch64DAGToDAGISel::SelectPostStore(SDNode *N, unsigned NumVecs,
1203 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001204 SDLoc dl(N);
1205 EVT VT = N->getOperand(2)->getValueType(0);
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001206 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1207 MVT::Other}; // Type for the Chain
Tim Northover3b0846e2014-05-24 12:50:23 +00001208
1209 // Form a REG_SEQUENCE to force register allocation.
1210 bool Is128Bit = VT.getSizeInBits() == 128;
1211 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1212 SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1213
Benjamin Kramerea68a942015-02-19 15:26:17 +00001214 SDValue Ops[] = {RegSeq,
1215 N->getOperand(NumVecs + 1), // base register
1216 N->getOperand(NumVecs + 2), // Incremental
1217 N->getOperand(0)}; // Chain
Tim Northover3b0846e2014-05-24 12:50:23 +00001218 SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1219
Justin Bogner283e3bd2016-05-12 23:10:30 +00001220 ReplaceNode(N, St);
Tim Northover3b0846e2014-05-24 12:50:23 +00001221}
1222
Benjamin Kramer51f6096c2015-03-23 12:30:58 +00001223namespace {
Tim Northover3b0846e2014-05-24 12:50:23 +00001224/// WidenVector - Given a value in the V64 register class, produce the
1225/// equivalent value in the V128 register class.
1226class WidenVector {
1227 SelectionDAG &DAG;
1228
1229public:
1230 WidenVector(SelectionDAG &DAG) : DAG(DAG) {}
1231
1232 SDValue operator()(SDValue V64Reg) {
1233 EVT VT = V64Reg.getValueType();
1234 unsigned NarrowSize = VT.getVectorNumElements();
1235 MVT EltTy = VT.getVectorElementType().getSimpleVT();
1236 MVT WideTy = MVT::getVectorVT(EltTy, 2 * NarrowSize);
1237 SDLoc DL(V64Reg);
1238
1239 SDValue Undef =
1240 SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, WideTy), 0);
1241 return DAG.getTargetInsertSubreg(AArch64::dsub, DL, WideTy, Undef, V64Reg);
1242 }
1243};
Benjamin Kramer51f6096c2015-03-23 12:30:58 +00001244} // namespace
Tim Northover3b0846e2014-05-24 12:50:23 +00001245
1246/// NarrowVector - Given a value in the V128 register class, produce the
1247/// equivalent value in the V64 register class.
1248static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG) {
1249 EVT VT = V128Reg.getValueType();
1250 unsigned WideSize = VT.getVectorNumElements();
1251 MVT EltTy = VT.getVectorElementType().getSimpleVT();
1252 MVT NarrowTy = MVT::getVectorVT(EltTy, WideSize / 2);
1253
1254 return DAG.getTargetExtractSubreg(AArch64::dsub, SDLoc(V128Reg), NarrowTy,
1255 V128Reg);
1256}
1257
Justin Bogner283e3bd2016-05-12 23:10:30 +00001258void AArch64DAGToDAGISel::SelectLoadLane(SDNode *N, unsigned NumVecs,
1259 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001260 SDLoc dl(N);
1261 EVT VT = N->getValueType(0);
1262 bool Narrow = VT.getSizeInBits() == 64;
1263
1264 // Form a REG_SEQUENCE to force register allocation.
1265 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1266
1267 if (Narrow)
1268 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1269 WidenVector(*CurDAG));
1270
1271 SDValue RegSeq = createQTuple(Regs);
1272
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001273 const EVT ResTys[] = {MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001274
1275 unsigned LaneNo =
1276 cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1277
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001278 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001279 N->getOperand(NumVecs + 3), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001280 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1281 SDValue SuperReg = SDValue(Ld, 0);
1282
1283 EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
Craig Topper26260942015-10-18 05:15:34 +00001284 static const unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1,
1285 AArch64::qsub2, AArch64::qsub3 };
Tim Northover3b0846e2014-05-24 12:50:23 +00001286 for (unsigned i = 0; i < NumVecs; ++i) {
1287 SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT, SuperReg);
1288 if (Narrow)
1289 NV = NarrowVector(NV, *CurDAG);
1290 ReplaceUses(SDValue(N, i), NV);
1291 }
1292
1293 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
Justin Bogner3525da72016-05-12 20:54:27 +00001294 CurDAG->RemoveDeadNode(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001295}
1296
Justin Bogner283e3bd2016-05-12 23:10:30 +00001297void AArch64DAGToDAGISel::SelectPostLoadLane(SDNode *N, unsigned NumVecs,
1298 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001299 SDLoc dl(N);
1300 EVT VT = N->getValueType(0);
1301 bool Narrow = VT.getSizeInBits() == 64;
1302
1303 // Form a REG_SEQUENCE to force register allocation.
1304 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1305
1306 if (Narrow)
1307 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1308 WidenVector(*CurDAG));
1309
1310 SDValue RegSeq = createQTuple(Regs);
1311
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001312 const EVT ResTys[] = {MVT::i64, // Type of the write back register
Ahmed Bougachae14a4d42015-04-17 23:43:33 +00001313 RegSeq->getValueType(0), MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001314
1315 unsigned LaneNo =
1316 cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1317
Benjamin Kramerea68a942015-02-19 15:26:17 +00001318 SDValue Ops[] = {RegSeq,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001319 CurDAG->getTargetConstant(LaneNo, dl,
1320 MVT::i64), // Lane Number
Benjamin Kramerea68a942015-02-19 15:26:17 +00001321 N->getOperand(NumVecs + 2), // Base register
1322 N->getOperand(NumVecs + 3), // Incremental
1323 N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001324 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1325
1326 // Update uses of the write back register
1327 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1328
1329 // Update uses of the vector list
1330 SDValue SuperReg = SDValue(Ld, 1);
1331 if (NumVecs == 1) {
1332 ReplaceUses(SDValue(N, 0),
1333 Narrow ? NarrowVector(SuperReg, *CurDAG) : SuperReg);
1334 } else {
1335 EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
Craig Topper26260942015-10-18 05:15:34 +00001336 static const unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1,
1337 AArch64::qsub2, AArch64::qsub3 };
Tim Northover3b0846e2014-05-24 12:50:23 +00001338 for (unsigned i = 0; i < NumVecs; ++i) {
1339 SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT,
1340 SuperReg);
1341 if (Narrow)
1342 NV = NarrowVector(NV, *CurDAG);
1343 ReplaceUses(SDValue(N, i), NV);
1344 }
1345 }
1346
1347 // Update the Chain
1348 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00001349 CurDAG->RemoveDeadNode(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001350}
1351
Justin Bogner283e3bd2016-05-12 23:10:30 +00001352void AArch64DAGToDAGISel::SelectStoreLane(SDNode *N, unsigned NumVecs,
1353 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001354 SDLoc dl(N);
1355 EVT VT = N->getOperand(2)->getValueType(0);
1356 bool Narrow = VT.getSizeInBits() == 64;
1357
1358 // Form a REG_SEQUENCE to force register allocation.
1359 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1360
1361 if (Narrow)
1362 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1363 WidenVector(*CurDAG));
1364
1365 SDValue RegSeq = createQTuple(Regs);
1366
1367 unsigned LaneNo =
1368 cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1369
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001370 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001371 N->getOperand(NumVecs + 3), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001372 SDNode *St = CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops);
1373
1374 // Transfer memoperands.
1375 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1376 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1377 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1378
Justin Bogner283e3bd2016-05-12 23:10:30 +00001379 ReplaceNode(N, St);
Tim Northover3b0846e2014-05-24 12:50:23 +00001380}
1381
Justin Bogner283e3bd2016-05-12 23:10:30 +00001382void AArch64DAGToDAGISel::SelectPostStoreLane(SDNode *N, unsigned NumVecs,
1383 unsigned Opc) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001384 SDLoc dl(N);
1385 EVT VT = N->getOperand(2)->getValueType(0);
1386 bool Narrow = VT.getSizeInBits() == 64;
1387
1388 // Form a REG_SEQUENCE to force register allocation.
1389 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1390
1391 if (Narrow)
1392 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1393 WidenVector(*CurDAG));
1394
1395 SDValue RegSeq = createQTuple(Regs);
1396
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001397 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1398 MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001399
1400 unsigned LaneNo =
1401 cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1402
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001403 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001404 N->getOperand(NumVecs + 2), // Base Register
1405 N->getOperand(NumVecs + 3), // Incremental
1406 N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001407 SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1408
1409 // Transfer memoperands.
1410 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1411 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1412 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1413
Justin Bogner283e3bd2016-05-12 23:10:30 +00001414 ReplaceNode(N, St);
Tim Northover3b0846e2014-05-24 12:50:23 +00001415}
1416
1417static bool isBitfieldExtractOpFromAnd(SelectionDAG *CurDAG, SDNode *N,
1418 unsigned &Opc, SDValue &Opd0,
1419 unsigned &LSB, unsigned &MSB,
1420 unsigned NumberOfIgnoredLowBits,
1421 bool BiggerPattern) {
1422 assert(N->getOpcode() == ISD::AND &&
1423 "N must be a AND operation to call this function");
1424
1425 EVT VT = N->getValueType(0);
1426
1427 // Here we can test the type of VT and return false when the type does not
1428 // match, but since it is done prior to that call in the current context
1429 // we turned that into an assert to avoid redundant code.
1430 assert((VT == MVT::i32 || VT == MVT::i64) &&
1431 "Type checking must have been done before calling this function");
1432
1433 // FIXME: simplify-demanded-bits in DAGCombine will probably have
1434 // changed the AND node to a 32-bit mask operation. We'll have to
1435 // undo that as part of the transform here if we want to catch all
1436 // the opportunities.
1437 // Currently the NumberOfIgnoredLowBits argument helps to recover
1438 // form these situations when matching bigger pattern (bitfield insert).
1439
1440 // For unsigned extracts, check for a shift right and mask
Chad Rosier7e8dd512016-05-14 18:56:28 +00001441 uint64_t AndImm = 0;
1442 if (!isOpcWithIntImmediate(N, ISD::AND, AndImm))
Tim Northover3b0846e2014-05-24 12:50:23 +00001443 return false;
1444
1445 const SDNode *Op0 = N->getOperand(0).getNode();
1446
1447 // Because of simplify-demanded-bits in DAGCombine, the mask may have been
1448 // simplified. Try to undo that
Chad Rosier7e8dd512016-05-14 18:56:28 +00001449 AndImm |= (1 << NumberOfIgnoredLowBits) - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001450
1451 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
Chad Rosier7e8dd512016-05-14 18:56:28 +00001452 if (AndImm & (AndImm + 1))
Tim Northover3b0846e2014-05-24 12:50:23 +00001453 return false;
1454
1455 bool ClampMSB = false;
Chad Rosier7e8dd512016-05-14 18:56:28 +00001456 uint64_t SrlImm = 0;
Tim Northover3b0846e2014-05-24 12:50:23 +00001457 // Handle the SRL + ANY_EXTEND case.
1458 if (VT == MVT::i64 && Op0->getOpcode() == ISD::ANY_EXTEND &&
Chad Rosier7e8dd512016-05-14 18:56:28 +00001459 isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL, SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001460 // Extend the incoming operand of the SRL to 64-bit.
1461 Opd0 = Widen(CurDAG, Op0->getOperand(0).getOperand(0));
1462 // Make sure to clamp the MSB so that we preserve the semantics of the
1463 // original operations.
1464 ClampMSB = true;
1465 } else if (VT == MVT::i32 && Op0->getOpcode() == ISD::TRUNCATE &&
1466 isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL,
Chad Rosier7e8dd512016-05-14 18:56:28 +00001467 SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001468 // If the shift result was truncated, we can still combine them.
1469 Opd0 = Op0->getOperand(0).getOperand(0);
1470
1471 // Use the type of SRL node.
1472 VT = Opd0->getValueType(0);
Chad Rosier7e8dd512016-05-14 18:56:28 +00001473 } else if (isOpcWithIntImmediate(Op0, ISD::SRL, SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001474 Opd0 = Op0->getOperand(0);
1475 } else if (BiggerPattern) {
1476 // Let's pretend a 0 shift right has been performed.
1477 // The resulting code will be at least as good as the original one
1478 // plus it may expose more opportunities for bitfield insert pattern.
1479 // FIXME: Currently we limit this to the bigger pattern, because
Chad Rosier6c1f0932015-09-17 13:10:27 +00001480 // some optimizations expect AND and not UBFM.
Tim Northover3b0846e2014-05-24 12:50:23 +00001481 Opd0 = N->getOperand(0);
1482 } else
1483 return false;
1484
Matthias Braun75260352015-02-24 18:52:04 +00001485 // Bail out on large immediates. This happens when no proper
1486 // combining/constant folding was performed.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001487 if (!BiggerPattern && (SrlImm <= 0 || SrlImm >= VT.getSizeInBits())) {
Matthias Braun02892ec2015-02-25 18:03:50 +00001488 DEBUG((dbgs() << N
1489 << ": Found large shift immediate, this should not happen\n"));
Matthias Braun75260352015-02-24 18:52:04 +00001490 return false;
Matthias Braun02892ec2015-02-25 18:03:50 +00001491 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001492
Chad Rosier7e8dd512016-05-14 18:56:28 +00001493 LSB = SrlImm;
1494 MSB = SrlImm + (VT == MVT::i32 ? countTrailingOnes<uint32_t>(AndImm)
1495 : countTrailingOnes<uint64_t>(AndImm)) -
Tim Northover3b0846e2014-05-24 12:50:23 +00001496 1;
1497 if (ClampMSB)
1498 // Since we're moving the extend before the right shift operation, we need
1499 // to clamp the MSB to make sure we don't shift in undefined bits instead of
1500 // the zeros which would get shifted in with the original right shift
1501 // operation.
1502 MSB = MSB > 31 ? 31 : MSB;
1503
1504 Opc = VT == MVT::i32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1505 return true;
1506}
1507
David Xu052b9d92014-09-02 09:33:56 +00001508static bool isSeveralBitsExtractOpFromShr(SDNode *N, unsigned &Opc,
1509 SDValue &Opd0, unsigned &LSB,
1510 unsigned &MSB) {
1511 // We are looking for the following pattern which basically extracts several
1512 // continuous bits from the source value and places it from the LSB of the
1513 // destination value, all other bits of the destination value or set to zero:
Tim Northover3b0846e2014-05-24 12:50:23 +00001514 //
1515 // Value2 = AND Value, MaskImm
1516 // SRL Value2, ShiftImm
1517 //
David Xu052b9d92014-09-02 09:33:56 +00001518 // with MaskImm >> ShiftImm to search for the bit width.
Tim Northover3b0846e2014-05-24 12:50:23 +00001519 //
1520 // This gets selected into a single UBFM:
1521 //
Chad Rosier7e8dd512016-05-14 18:56:28 +00001522 // UBFM Value, ShiftImm, BitWide + SrlImm -1
Tim Northover3b0846e2014-05-24 12:50:23 +00001523 //
1524
1525 if (N->getOpcode() != ISD::SRL)
1526 return false;
1527
Chad Rosier7e8dd512016-05-14 18:56:28 +00001528 uint64_t AndMask = 0;
1529 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, AndMask))
Tim Northover3b0846e2014-05-24 12:50:23 +00001530 return false;
1531
1532 Opd0 = N->getOperand(0).getOperand(0);
1533
Chad Rosier7e8dd512016-05-14 18:56:28 +00001534 uint64_t SrlImm = 0;
1535 if (!isIntImmediate(N->getOperand(1), SrlImm))
Tim Northover3b0846e2014-05-24 12:50:23 +00001536 return false;
1537
David Xu052b9d92014-09-02 09:33:56 +00001538 // Check whether we really have several bits extract here.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001539 unsigned BitWide = 64 - countLeadingOnes(~(AndMask >> SrlImm));
1540 if (BitWide && isMask_64(AndMask >> SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001541 if (N->getValueType(0) == MVT::i32)
1542 Opc = AArch64::UBFMWri;
1543 else
1544 Opc = AArch64::UBFMXri;
1545
Chad Rosier7e8dd512016-05-14 18:56:28 +00001546 LSB = SrlImm;
1547 MSB = BitWide + SrlImm - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001548 return true;
1549 }
1550
1551 return false;
1552}
1553
1554static bool isBitfieldExtractOpFromShr(SDNode *N, unsigned &Opc, SDValue &Opd0,
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001555 unsigned &Immr, unsigned &Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001556 bool BiggerPattern) {
1557 assert((N->getOpcode() == ISD::SRA || N->getOpcode() == ISD::SRL) &&
1558 "N must be a SHR/SRA operation to call this function");
1559
1560 EVT VT = N->getValueType(0);
1561
1562 // Here we can test the type of VT and return false when the type does not
1563 // match, but since it is done prior to that call in the current context
1564 // we turned that into an assert to avoid redundant code.
1565 assert((VT == MVT::i32 || VT == MVT::i64) &&
1566 "Type checking must have been done before calling this function");
1567
David Xu052b9d92014-09-02 09:33:56 +00001568 // Check for AND + SRL doing several bits extract.
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001569 if (isSeveralBitsExtractOpFromShr(N, Opc, Opd0, Immr, Imms))
Tim Northover3b0846e2014-05-24 12:50:23 +00001570 return true;
1571
Chad Rosierc73d5592016-05-16 12:55:01 +00001572 // We're looking for a shift of a shift.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001573 uint64_t ShlImm = 0;
1574 uint64_t TruncBits = 0;
1575 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, ShlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001576 Opd0 = N->getOperand(0).getOperand(0);
1577 } else if (VT == MVT::i32 && N->getOpcode() == ISD::SRL &&
1578 N->getOperand(0).getNode()->getOpcode() == ISD::TRUNCATE) {
1579 // We are looking for a shift of truncate. Truncate from i64 to i32 could
1580 // be considered as setting high 32 bits as zero. Our strategy here is to
1581 // always generate 64bit UBFM. This consistency will help the CSE pass
1582 // later find more redundancy.
1583 Opd0 = N->getOperand(0).getOperand(0);
Chad Rosier7e8dd512016-05-14 18:56:28 +00001584 TruncBits = Opd0->getValueType(0).getSizeInBits() - VT.getSizeInBits();
Tim Northover3b0846e2014-05-24 12:50:23 +00001585 VT = Opd0->getValueType(0);
1586 assert(VT == MVT::i64 && "the promoted type should be i64");
1587 } else if (BiggerPattern) {
1588 // Let's pretend a 0 shift left has been performed.
1589 // FIXME: Currently we limit this to the bigger pattern case,
1590 // because some optimizations expect AND and not UBFM
1591 Opd0 = N->getOperand(0);
1592 } else
1593 return false;
1594
Matthias Braun75260352015-02-24 18:52:04 +00001595 // Missing combines/constant folding may have left us with strange
1596 // constants.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001597 if (ShlImm >= VT.getSizeInBits()) {
Matthias Braun02892ec2015-02-25 18:03:50 +00001598 DEBUG((dbgs() << N
1599 << ": Found large shift immediate, this should not happen\n"));
Matthias Braun75260352015-02-24 18:52:04 +00001600 return false;
Matthias Braun02892ec2015-02-25 18:03:50 +00001601 }
Matthias Braun75260352015-02-24 18:52:04 +00001602
Chad Rosier7e8dd512016-05-14 18:56:28 +00001603 uint64_t SrlImm = 0;
1604 if (!isIntImmediate(N->getOperand(1), SrlImm))
Tim Northover3b0846e2014-05-24 12:50:23 +00001605 return false;
1606
Chad Rosier7e8dd512016-05-14 18:56:28 +00001607 assert(SrlImm > 0 && SrlImm < VT.getSizeInBits() &&
Tim Northover3b0846e2014-05-24 12:50:23 +00001608 "bad amount in shift node!");
Chad Rosier7e8dd512016-05-14 18:56:28 +00001609 int immr = SrlImm - ShlImm;
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001610 Immr = immr < 0 ? immr + VT.getSizeInBits() : immr;
Chad Rosier7e8dd512016-05-14 18:56:28 +00001611 Imms = VT.getSizeInBits() - ShlImm - TruncBits - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001612 // SRA requires a signed extraction
1613 if (VT == MVT::i32)
1614 Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMWri : AArch64::UBFMWri;
1615 else
1616 Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMXri : AArch64::UBFMXri;
1617 return true;
1618}
1619
1620static bool isBitfieldExtractOp(SelectionDAG *CurDAG, SDNode *N, unsigned &Opc,
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001621 SDValue &Opd0, unsigned &Immr, unsigned &Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001622 unsigned NumberOfIgnoredLowBits = 0,
1623 bool BiggerPattern = false) {
1624 if (N->getValueType(0) != MVT::i32 && N->getValueType(0) != MVT::i64)
1625 return false;
1626
1627 switch (N->getOpcode()) {
1628 default:
1629 if (!N->isMachineOpcode())
1630 return false;
1631 break;
1632 case ISD::AND:
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001633 return isBitfieldExtractOpFromAnd(CurDAG, N, Opc, Opd0, Immr, Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001634 NumberOfIgnoredLowBits, BiggerPattern);
1635 case ISD::SRL:
1636 case ISD::SRA:
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001637 return isBitfieldExtractOpFromShr(N, Opc, Opd0, Immr, Imms, BiggerPattern);
Tim Northover3b0846e2014-05-24 12:50:23 +00001638 }
1639
1640 unsigned NOpc = N->getMachineOpcode();
1641 switch (NOpc) {
1642 default:
1643 return false;
1644 case AArch64::SBFMWri:
1645 case AArch64::UBFMWri:
1646 case AArch64::SBFMXri:
1647 case AArch64::UBFMXri:
1648 Opc = NOpc;
1649 Opd0 = N->getOperand(0);
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001650 Immr = cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
1651 Imms = cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
Tim Northover3b0846e2014-05-24 12:50:23 +00001652 return true;
1653 }
1654 // Unreachable
1655 return false;
1656}
1657
Justin Bogner283e3bd2016-05-12 23:10:30 +00001658bool AArch64DAGToDAGISel::tryBitfieldExtractOp(SDNode *N) {
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001659 unsigned Opc, Immr, Imms;
Tim Northover3b0846e2014-05-24 12:50:23 +00001660 SDValue Opd0;
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001661 if (!isBitfieldExtractOp(CurDAG, N, Opc, Opd0, Immr, Imms))
Justin Bogner283e3bd2016-05-12 23:10:30 +00001662 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001663
1664 EVT VT = N->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001665 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001666
1667 // If the bit extract operation is 64bit but the original type is 32bit, we
1668 // need to add one EXTRACT_SUBREG.
1669 if ((Opc == AArch64::SBFMXri || Opc == AArch64::UBFMXri) && VT == MVT::i32) {
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001670 SDValue Ops64[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, MVT::i64),
1671 CurDAG->getTargetConstant(Imms, dl, MVT::i64)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001672
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001673 SDNode *BFM = CurDAG->getMachineNode(Opc, dl, MVT::i64, Ops64);
1674 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Justin Bogner283e3bd2016-05-12 23:10:30 +00001675 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
1676 MVT::i32, SDValue(BFM, 0), SubReg));
1677 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001678 }
1679
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001680 SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
1681 CurDAG->getTargetConstant(Imms, dl, VT)};
Justin Bogner283e3bd2016-05-12 23:10:30 +00001682 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
1683 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001684}
1685
1686/// Does DstMask form a complementary pair with the mask provided by
1687/// BitsToBeInserted, suitable for use in a BFI instruction. Roughly speaking,
1688/// this asks whether DstMask zeroes precisely those bits that will be set by
1689/// the other half.
1690static bool isBitfieldDstMask(uint64_t DstMask, APInt BitsToBeInserted,
1691 unsigned NumberOfIgnoredHighBits, EVT VT) {
1692 assert((VT == MVT::i32 || VT == MVT::i64) &&
1693 "i32 or i64 mask type expected!");
1694 unsigned BitWidth = VT.getSizeInBits() - NumberOfIgnoredHighBits;
1695
1696 APInt SignificantDstMask = APInt(BitWidth, DstMask);
1697 APInt SignificantBitsToBeInserted = BitsToBeInserted.zextOrTrunc(BitWidth);
1698
1699 return (SignificantDstMask & SignificantBitsToBeInserted) == 0 &&
1700 (SignificantDstMask | SignificantBitsToBeInserted).isAllOnesValue();
1701}
1702
1703// Look for bits that will be useful for later uses.
1704// A bit is consider useless as soon as it is dropped and never used
1705// before it as been dropped.
1706// E.g., looking for useful bit of x
1707// 1. y = x & 0x7
1708// 2. z = y >> 2
1709// After #1, x useful bits are 0x7, then the useful bits of x, live through
1710// y.
1711// After #2, the useful bits of x are 0x4.
1712// However, if x is used on an unpredicatable instruction, then all its bits
1713// are useful.
1714// E.g.
1715// 1. y = x & 0x7
1716// 2. z = y >> 2
1717// 3. str x, [@x]
1718static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth = 0);
1719
1720static void getUsefulBitsFromAndWithImmediate(SDValue Op, APInt &UsefulBits,
1721 unsigned Depth) {
1722 uint64_t Imm =
1723 cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1724 Imm = AArch64_AM::decodeLogicalImmediate(Imm, UsefulBits.getBitWidth());
1725 UsefulBits &= APInt(UsefulBits.getBitWidth(), Imm);
1726 getUsefulBits(Op, UsefulBits, Depth + 1);
1727}
1728
1729static void getUsefulBitsFromBitfieldMoveOpd(SDValue Op, APInt &UsefulBits,
1730 uint64_t Imm, uint64_t MSB,
1731 unsigned Depth) {
1732 // inherit the bitwidth value
1733 APInt OpUsefulBits(UsefulBits);
1734 OpUsefulBits = 1;
1735
1736 if (MSB >= Imm) {
1737 OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1738 --OpUsefulBits;
1739 // The interesting part will be in the lower part of the result
1740 getUsefulBits(Op, OpUsefulBits, Depth + 1);
1741 // The interesting part was starting at Imm in the argument
1742 OpUsefulBits = OpUsefulBits.shl(Imm);
1743 } else {
1744 OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1745 --OpUsefulBits;
1746 // The interesting part will be shifted in the result
1747 OpUsefulBits = OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm);
1748 getUsefulBits(Op, OpUsefulBits, Depth + 1);
1749 // The interesting part was at zero in the argument
1750 OpUsefulBits = OpUsefulBits.lshr(OpUsefulBits.getBitWidth() - Imm);
1751 }
1752
1753 UsefulBits &= OpUsefulBits;
1754}
1755
1756static void getUsefulBitsFromUBFM(SDValue Op, APInt &UsefulBits,
1757 unsigned Depth) {
1758 uint64_t Imm =
1759 cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1760 uint64_t MSB =
1761 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1762
1763 getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1764}
1765
1766static void getUsefulBitsFromOrWithShiftedReg(SDValue Op, APInt &UsefulBits,
1767 unsigned Depth) {
1768 uint64_t ShiftTypeAndValue =
1769 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1770 APInt Mask(UsefulBits);
1771 Mask.clearAllBits();
1772 Mask.flipAllBits();
1773
1774 if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSL) {
1775 // Shift Left
1776 uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1777 Mask = Mask.shl(ShiftAmt);
1778 getUsefulBits(Op, Mask, Depth + 1);
1779 Mask = Mask.lshr(ShiftAmt);
1780 } else if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSR) {
1781 // Shift Right
1782 // We do not handle AArch64_AM::ASR, because the sign will change the
1783 // number of useful bits
1784 uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1785 Mask = Mask.lshr(ShiftAmt);
1786 getUsefulBits(Op, Mask, Depth + 1);
1787 Mask = Mask.shl(ShiftAmt);
1788 } else
1789 return;
1790
1791 UsefulBits &= Mask;
1792}
1793
1794static void getUsefulBitsFromBFM(SDValue Op, SDValue Orig, APInt &UsefulBits,
1795 unsigned Depth) {
1796 uint64_t Imm =
1797 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1798 uint64_t MSB =
1799 cast<const ConstantSDNode>(Op.getOperand(3).getNode())->getZExtValue();
1800
1801 if (Op.getOperand(1) == Orig)
1802 return getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1803
1804 APInt OpUsefulBits(UsefulBits);
1805 OpUsefulBits = 1;
1806
1807 if (MSB >= Imm) {
1808 OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1809 --OpUsefulBits;
1810 UsefulBits &= ~OpUsefulBits;
1811 getUsefulBits(Op, UsefulBits, Depth + 1);
1812 } else {
1813 OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1814 --OpUsefulBits;
1815 UsefulBits = ~(OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm));
1816 getUsefulBits(Op, UsefulBits, Depth + 1);
1817 }
1818}
1819
1820static void getUsefulBitsForUse(SDNode *UserNode, APInt &UsefulBits,
1821 SDValue Orig, unsigned Depth) {
1822
1823 // Users of this node should have already been instruction selected
1824 // FIXME: Can we turn that into an assert?
1825 if (!UserNode->isMachineOpcode())
1826 return;
1827
1828 switch (UserNode->getMachineOpcode()) {
1829 default:
1830 return;
1831 case AArch64::ANDSWri:
1832 case AArch64::ANDSXri:
1833 case AArch64::ANDWri:
1834 case AArch64::ANDXri:
1835 // We increment Depth only when we call the getUsefulBits
1836 return getUsefulBitsFromAndWithImmediate(SDValue(UserNode, 0), UsefulBits,
1837 Depth);
1838 case AArch64::UBFMWri:
1839 case AArch64::UBFMXri:
1840 return getUsefulBitsFromUBFM(SDValue(UserNode, 0), UsefulBits, Depth);
1841
1842 case AArch64::ORRWrs:
1843 case AArch64::ORRXrs:
1844 if (UserNode->getOperand(1) != Orig)
1845 return;
1846 return getUsefulBitsFromOrWithShiftedReg(SDValue(UserNode, 0), UsefulBits,
1847 Depth);
1848 case AArch64::BFMWri:
1849 case AArch64::BFMXri:
1850 return getUsefulBitsFromBFM(SDValue(UserNode, 0), Orig, UsefulBits, Depth);
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001851
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001852 case AArch64::STRBBui:
Chad Rosier9926a5e2016-05-12 01:42:01 +00001853 case AArch64::STURBBi:
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001854 if (UserNode->getOperand(0) != Orig)
1855 return;
1856 UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xff);
1857 return;
1858
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001859 case AArch64::STRHHui:
Chad Rosier9926a5e2016-05-12 01:42:01 +00001860 case AArch64::STURHHi:
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001861 if (UserNode->getOperand(0) != Orig)
1862 return;
1863 UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xffff);
1864 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00001865 }
1866}
1867
1868static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth) {
1869 if (Depth >= 6)
1870 return;
1871 // Initialize UsefulBits
1872 if (!Depth) {
1873 unsigned Bitwidth = Op.getValueType().getScalarType().getSizeInBits();
1874 // At the beginning, assume every produced bits is useful
1875 UsefulBits = APInt(Bitwidth, 0);
1876 UsefulBits.flipAllBits();
1877 }
1878 APInt UsersUsefulBits(UsefulBits.getBitWidth(), 0);
1879
1880 for (SDNode *Node : Op.getNode()->uses()) {
1881 // A use cannot produce useful bits
1882 APInt UsefulBitsForUse = APInt(UsefulBits);
1883 getUsefulBitsForUse(Node, UsefulBitsForUse, Op, Depth);
1884 UsersUsefulBits |= UsefulBitsForUse;
1885 }
1886 // UsefulBits contains the produced bits that are meaningful for the
1887 // current definition, thus a user cannot make a bit meaningful at
1888 // this point
1889 UsefulBits &= UsersUsefulBits;
1890}
1891
1892/// Create a machine node performing a notional SHL of Op by ShlAmount. If
1893/// ShlAmount is negative, do a (logical) right-shift instead. If ShlAmount is
1894/// 0, return Op unchanged.
1895static SDValue getLeftShift(SelectionDAG *CurDAG, SDValue Op, int ShlAmount) {
1896 if (ShlAmount == 0)
1897 return Op;
1898
1899 EVT VT = Op.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001900 SDLoc dl(Op);
Tim Northover3b0846e2014-05-24 12:50:23 +00001901 unsigned BitWidth = VT.getSizeInBits();
1902 unsigned UBFMOpc = BitWidth == 32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1903
1904 SDNode *ShiftNode;
1905 if (ShlAmount > 0) {
1906 // LSL wD, wN, #Amt == UBFM wD, wN, #32-Amt, #31-Amt
1907 ShiftNode = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001908 UBFMOpc, dl, VT, Op,
1909 CurDAG->getTargetConstant(BitWidth - ShlAmount, dl, VT),
1910 CurDAG->getTargetConstant(BitWidth - 1 - ShlAmount, dl, VT));
Tim Northover3b0846e2014-05-24 12:50:23 +00001911 } else {
1912 // LSR wD, wN, #Amt == UBFM wD, wN, #Amt, #32-1
1913 assert(ShlAmount < 0 && "expected right shift");
1914 int ShrAmount = -ShlAmount;
1915 ShiftNode = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001916 UBFMOpc, dl, VT, Op, CurDAG->getTargetConstant(ShrAmount, dl, VT),
1917 CurDAG->getTargetConstant(BitWidth - 1, dl, VT));
Tim Northover3b0846e2014-05-24 12:50:23 +00001918 }
1919
1920 return SDValue(ShiftNode, 0);
1921}
1922
1923/// Does this tree qualify as an attempt to move a bitfield into position,
1924/// essentially "(and (shl VAL, N), Mask)".
1925static bool isBitfieldPositioningOp(SelectionDAG *CurDAG, SDValue Op,
Geoff Berry43ec15e2015-09-18 17:11:53 +00001926 bool BiggerPattern,
Tim Northover3b0846e2014-05-24 12:50:23 +00001927 SDValue &Src, int &ShiftAmount,
1928 int &MaskWidth) {
1929 EVT VT = Op.getValueType();
1930 unsigned BitWidth = VT.getSizeInBits();
1931 (void)BitWidth;
1932 assert(BitWidth == 32 || BitWidth == 64);
1933
1934 APInt KnownZero, KnownOne;
1935 CurDAG->computeKnownBits(Op, KnownZero, KnownOne);
1936
1937 // Non-zero in the sense that they're not provably zero, which is the key
1938 // point if we want to use this value
1939 uint64_t NonZeroBits = (~KnownZero).getZExtValue();
1940
1941 // Discard a constant AND mask if present. It's safe because the node will
1942 // already have been factored into the computeKnownBits calculation above.
1943 uint64_t AndImm;
1944 if (isOpcWithIntImmediate(Op.getNode(), ISD::AND, AndImm)) {
1945 assert((~APInt(BitWidth, AndImm) & ~KnownZero) == 0);
1946 Op = Op.getOperand(0);
1947 }
1948
Geoff Berry43ec15e2015-09-18 17:11:53 +00001949 // Don't match if the SHL has more than one use, since then we'll end up
1950 // generating SHL+UBFIZ instead of just keeping SHL+AND.
1951 if (!BiggerPattern && !Op.hasOneUse())
1952 return false;
1953
Tim Northover3b0846e2014-05-24 12:50:23 +00001954 uint64_t ShlImm;
1955 if (!isOpcWithIntImmediate(Op.getNode(), ISD::SHL, ShlImm))
1956 return false;
1957 Op = Op.getOperand(0);
1958
1959 if (!isShiftedMask_64(NonZeroBits))
1960 return false;
1961
1962 ShiftAmount = countTrailingZeros(NonZeroBits);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00001963 MaskWidth = countTrailingOnes(NonZeroBits >> ShiftAmount);
Tim Northover3b0846e2014-05-24 12:50:23 +00001964
1965 // BFI encompasses sufficiently many nodes that it's worth inserting an extra
1966 // LSL/LSR if the mask in NonZeroBits doesn't quite match up with the ISD::SHL
Geoff Berry43ec15e2015-09-18 17:11:53 +00001967 // amount. BiggerPattern is true when this pattern is being matched for BFI,
1968 // BiggerPattern is false when this pattern is being matched for UBFIZ, in
1969 // which case it is not profitable to insert an extra shift.
1970 if (ShlImm - ShiftAmount != 0 && !BiggerPattern)
1971 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001972 Src = getLeftShift(CurDAG, Op, ShlImm - ShiftAmount);
1973
1974 return true;
1975}
1976
Chad Rosier02f25a92016-05-19 14:19:47 +00001977static bool isShiftedMask(uint64_t Mask, EVT VT) {
1978 assert(VT == MVT::i32 || VT == MVT::i64);
1979 if (VT == MVT::i32)
1980 return isShiftedMask_32(Mask);
1981 return isShiftedMask_64(Mask);
1982}
1983
Chad Rosier816a67d2016-05-26 13:27:56 +00001984// Generate a BFI/BFXIL from 'or (and X, MaskImm), OrImm' iff the value being
1985// inserted only sets known zero bits.
1986static bool tryBitfieldInsertOpFromOrAndImm(SDNode *N, SelectionDAG *CurDAG) {
1987 assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
1988
1989 EVT VT = N->getValueType(0);
1990 if (VT != MVT::i32 && VT != MVT::i64)
1991 return false;
1992
1993 unsigned BitWidth = VT.getSizeInBits();
1994
1995 uint64_t OrImm;
1996 if (!isOpcWithIntImmediate(N, ISD::OR, OrImm))
1997 return false;
1998
1999 // Skip this transformation if the ORR immediate can be encoded in the ORR.
2000 // Otherwise, we'll trade an AND+ORR for ORR+BFI/BFXIL, which is most likely
2001 // performance neutral.
2002 if (AArch64_AM::isLogicalImmediate(OrImm, BitWidth))
2003 return false;
2004
2005 uint64_t MaskImm;
2006 SDValue And = N->getOperand(0);
2007 // Must be a single use AND with an immediate operand.
2008 if (!And.hasOneUse() ||
2009 !isOpcWithIntImmediate(And.getNode(), ISD::AND, MaskImm))
2010 return false;
2011
2012 // Compute the Known Zero for the AND as this allows us to catch more general
2013 // cases than just looking for AND with imm.
2014 APInt KnownZero, KnownOne;
2015 CurDAG->computeKnownBits(And, KnownZero, KnownOne);
2016
2017 // Non-zero in the sense that they're not provably zero, which is the key
2018 // point if we want to use this value.
2019 uint64_t NotKnownZero = (~KnownZero).getZExtValue();
2020
2021 // The KnownZero mask must be a shifted mask (e.g., 1110..011, 11100..00).
2022 if (!isShiftedMask(KnownZero.getZExtValue(), VT))
2023 return false;
2024
2025 // The bits being inserted must only set those bits that are known to be zero.
2026 if ((OrImm & NotKnownZero) != 0) {
2027 // FIXME: It's okay if the OrImm sets NotKnownZero bits to 1, but we don't
2028 // currently handle this case.
2029 return false;
2030 }
2031
2032 // BFI/BFXIL dst, src, #lsb, #width.
2033 int LSB = countTrailingOnes(NotKnownZero);
2034 int Width = BitWidth - APInt(BitWidth, NotKnownZero).countPopulation();
2035
2036 // BFI/BFXIL is an alias of BFM, so translate to BFM operands.
2037 unsigned ImmR = (BitWidth - LSB) % BitWidth;
2038 unsigned ImmS = Width - 1;
2039
2040 // If we're creating a BFI instruction avoid cases where we need more
2041 // instructions to materialize the BFI constant as compared to the original
2042 // ORR. A BFXIL will use the same constant as the original ORR, so the code
2043 // should be no worse in this case.
2044 bool IsBFI = LSB != 0;
2045 uint64_t BFIImm = OrImm >> LSB;
2046 if (IsBFI && !AArch64_AM::isLogicalImmediate(BFIImm, BitWidth)) {
2047 // We have a BFI instruction and we know the constant can't be materialized
2048 // with a ORR-immediate with the zero register.
2049 unsigned OrChunks = 0, BFIChunks = 0;
2050 for (unsigned Shift = 0; Shift < BitWidth; Shift += 16) {
2051 if (((OrImm >> Shift) & 0xFFFF) != 0)
2052 ++OrChunks;
2053 if (((BFIImm >> Shift) & 0xFFFF) != 0)
2054 ++BFIChunks;
2055 }
2056 if (BFIChunks > OrChunks)
2057 return false;
2058 }
2059
2060 // Materialize the constant to be inserted.
2061 SDLoc DL(N);
2062 unsigned MOVIOpc = VT == MVT::i32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
2063 SDNode *MOVI = CurDAG->getMachineNode(
2064 MOVIOpc, DL, VT, CurDAG->getTargetConstant(BFIImm, DL, VT));
2065
2066 // Create the BFI/BFXIL instruction.
2067 SDValue Ops[] = {And.getOperand(0), SDValue(MOVI, 0),
2068 CurDAG->getTargetConstant(ImmR, DL, VT),
2069 CurDAG->getTargetConstant(ImmS, DL, VT)};
2070 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2071 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2072 return true;
2073}
2074
Justin Bogner283e3bd2016-05-12 23:10:30 +00002075static bool tryBitfieldInsertOpFromOr(SDNode *N, const APInt &UsefulBits,
2076 SelectionDAG *CurDAG) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002077 assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
2078
Tim Northover3b0846e2014-05-24 12:50:23 +00002079 EVT VT = N->getValueType(0);
Chad Rosier042ac2c2016-05-12 19:38:18 +00002080 if (VT != MVT::i32 && VT != MVT::i64)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002081 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002082
Chad Rosier91294c52016-05-18 17:43:11 +00002083 unsigned BitWidth = VT.getSizeInBits();
2084
Tim Northover3b0846e2014-05-24 12:50:23 +00002085 // Because of simplify-demanded-bits in DAGCombine, involved masks may not
2086 // have the expected shape. Try to undo that.
Tim Northover3b0846e2014-05-24 12:50:23 +00002087
2088 unsigned NumberOfIgnoredLowBits = UsefulBits.countTrailingZeros();
2089 unsigned NumberOfIgnoredHighBits = UsefulBits.countLeadingZeros();
2090
Chad Rosiere0062022016-05-18 23:51:17 +00002091 // Given a OR operation, check if we have the following pattern
2092 // ubfm c, b, imm, imm2 (or something that does the same jobs, see
2093 // isBitfieldExtractOp)
2094 // d = e & mask2 ; where mask is a binary sequence of 1..10..0 and
2095 // countTrailingZeros(mask2) == imm2 - imm + 1
2096 // f = d | c
2097 // if yes, replace the OR instruction with:
2098 // f = BFM Opd0, Opd1, LSB, MSB ; where LSB = imm, and MSB = imm2
2099
Geoff Berry43ec15e2015-09-18 17:11:53 +00002100 // OR is commutative, check all combinations of operand order and values of
2101 // BiggerPattern, i.e.
2102 // Opd0, Opd1, BiggerPattern=false
2103 // Opd1, Opd0, BiggerPattern=false
2104 // Opd0, Opd1, BiggerPattern=true
2105 // Opd1, Opd0, BiggerPattern=true
2106 // Several of these combinations may match, so check with BiggerPattern=false
2107 // first since that will produce better results by matching more instructions
2108 // and/or inserting fewer extra instructions.
2109 for (int I = 0; I < 4; ++I) {
2110
Chad Rosier91294c52016-05-18 17:43:11 +00002111 SDValue Dst, Src;
2112 unsigned ImmR, ImmS;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002113 bool BiggerPattern = I / 2;
2114 SDNode *OrOpd0 = N->getOperand(I % 2).getNode();
2115 SDValue OrOpd1Val = N->getOperand((I + 1) % 2);
2116 SDNode *OrOpd1 = OrOpd1Val.getNode();
2117
Tim Northover3b0846e2014-05-24 12:50:23 +00002118 unsigned BFXOpc;
2119 int DstLSB, Width;
2120 if (isBitfieldExtractOp(CurDAG, OrOpd0, BFXOpc, Src, ImmR, ImmS,
Geoff Berry43ec15e2015-09-18 17:11:53 +00002121 NumberOfIgnoredLowBits, BiggerPattern)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002122 // Check that the returned opcode is compatible with the pattern,
2123 // i.e., same type and zero extended (U and not S)
2124 if ((BFXOpc != AArch64::UBFMXri && VT == MVT::i64) ||
2125 (BFXOpc != AArch64::UBFMWri && VT == MVT::i32))
2126 continue;
2127
2128 // Compute the width of the bitfield insertion
2129 DstLSB = 0;
2130 Width = ImmS - ImmR + 1;
2131 // FIXME: This constraint is to catch bitfield insertion we may
2132 // want to widen the pattern if we want to grab general bitfied
2133 // move case
2134 if (Width <= 0)
2135 continue;
2136
2137 // If the mask on the insertee is correct, we have a BFXIL operation. We
2138 // can share the ImmR and ImmS values from the already-computed UBFM.
Geoff Berry43ec15e2015-09-18 17:11:53 +00002139 } else if (isBitfieldPositioningOp(CurDAG, SDValue(OrOpd0, 0),
2140 BiggerPattern,
2141 Src, DstLSB, Width)) {
Chad Rosier91294c52016-05-18 17:43:11 +00002142 ImmR = (BitWidth - DstLSB) % BitWidth;
Tim Northover3b0846e2014-05-24 12:50:23 +00002143 ImmS = Width - 1;
2144 } else
2145 continue;
2146
2147 // Check the second part of the pattern
2148 EVT VT = OrOpd1->getValueType(0);
2149 assert((VT == MVT::i32 || VT == MVT::i64) && "unexpected OR operand");
2150
2151 // Compute the Known Zero for the candidate of the first operand.
2152 // This allows to catch more general case than just looking for
2153 // AND with imm. Indeed, simplify-demanded-bits may have removed
2154 // the AND instruction because it proves it was useless.
2155 APInt KnownZero, KnownOne;
2156 CurDAG->computeKnownBits(OrOpd1Val, KnownZero, KnownOne);
2157
2158 // Check if there is enough room for the second operand to appear
2159 // in the first one
2160 APInt BitsToBeInserted =
2161 APInt::getBitsSet(KnownZero.getBitWidth(), DstLSB, DstLSB + Width);
2162
2163 if ((BitsToBeInserted & ~KnownZero) != 0)
2164 continue;
2165
2166 // Set the first operand
2167 uint64_t Imm;
2168 if (isOpcWithIntImmediate(OrOpd1, ISD::AND, Imm) &&
2169 isBitfieldDstMask(Imm, BitsToBeInserted, NumberOfIgnoredHighBits, VT))
2170 // In that case, we can eliminate the AND
2171 Dst = OrOpd1->getOperand(0);
2172 else
2173 // Maybe the AND has been removed by simplify-demanded-bits
2174 // or is useful because it discards more bits
2175 Dst = OrOpd1Val;
2176
2177 // both parts match
Chad Rosier042ac2c2016-05-12 19:38:18 +00002178 SDLoc DL(N);
2179 SDValue Ops[] = {Dst, Src, CurDAG->getTargetConstant(ImmR, DL, VT),
2180 CurDAG->getTargetConstant(ImmS, DL, VT)};
2181 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
Justin Bogner283e3bd2016-05-12 23:10:30 +00002182 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2183 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00002184 }
Chad Rosier02f25a92016-05-19 14:19:47 +00002185
2186 // Generate a BFXIL from 'or (and X, Mask0Imm), (and Y, Mask1Imm)' iff
2187 // Mask0Imm and ~Mask1Imm are equivalent and one of the MaskImms is a shifted
2188 // mask (e.g., 0x000ffff0).
2189 uint64_t Mask0Imm, Mask1Imm;
2190 SDValue And0 = N->getOperand(0);
2191 SDValue And1 = N->getOperand(1);
2192 if (And0.hasOneUse() && And1.hasOneUse() &&
2193 isOpcWithIntImmediate(And0.getNode(), ISD::AND, Mask0Imm) &&
2194 isOpcWithIntImmediate(And1.getNode(), ISD::AND, Mask1Imm) &&
2195 APInt(BitWidth, Mask0Imm) == ~APInt(BitWidth, Mask1Imm) &&
2196 (isShiftedMask(Mask0Imm, VT) || isShiftedMask(Mask1Imm, VT))) {
2197
2198 // We should have already caught the case where we extract hi and low parts.
2199 // E.g. BFXIL from 'or (and X, 0xffff0000), (and Y, 0x0000ffff)'.
2200 assert(!(isShiftedMask(Mask0Imm, VT) && isShiftedMask(Mask1Imm, VT)) &&
2201 "BFXIL should have already been optimized.");
2202
2203 // ORR is commutative, so canonicalize to the form 'or (and X, Mask0Imm),
2204 // (and Y, Mask1Imm)' where Mask1Imm is the shifted mask masking off the
2205 // bits to be inserted.
2206 if (isShiftedMask(Mask0Imm, VT)) {
2207 std::swap(And0, And1);
2208 std::swap(Mask0Imm, Mask1Imm);
2209 }
2210
2211 SDValue Src = And1->getOperand(0);
2212 SDValue Dst = And0->getOperand(0);
2213 unsigned LSB = countTrailingZeros(Mask1Imm);
2214 int Width = BitWidth - APInt(BitWidth, Mask0Imm).countPopulation();
2215
2216 // The BFXIL inserts the low-order bits from a source register, so right
2217 // shift the needed bits into place.
2218 SDLoc DL(N);
2219 unsigned ShiftOpc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
2220 SDNode *LSR = CurDAG->getMachineNode(
2221 ShiftOpc, DL, VT, Src, CurDAG->getTargetConstant(LSB, DL, VT),
2222 CurDAG->getTargetConstant(BitWidth - 1, DL, VT));
2223
2224 // BFXIL is an alias of BFM, so translate to BFM operands.
2225 unsigned ImmR = (BitWidth - LSB) % BitWidth;
2226 unsigned ImmS = Width - 1;
2227
2228 // Create the BFXIL instruction.
2229 SDValue Ops[] = {Dst, SDValue(LSR, 0),
2230 CurDAG->getTargetConstant(ImmR, DL, VT),
2231 CurDAG->getTargetConstant(ImmS, DL, VT)};
2232 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2233 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2234 return true;
2235 }
2236
Justin Bogner283e3bd2016-05-12 23:10:30 +00002237 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002238}
2239
Justin Bogner283e3bd2016-05-12 23:10:30 +00002240bool AArch64DAGToDAGISel::tryBitfieldInsertOp(SDNode *N) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002241 if (N->getOpcode() != ISD::OR)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002242 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002243
Weiming Zhao56ab5182015-12-01 19:17:49 +00002244 APInt NUsefulBits;
2245 getUsefulBits(SDValue(N, 0), NUsefulBits);
Tim Northover3b0846e2014-05-24 12:50:23 +00002246
Weiming Zhao56ab5182015-12-01 19:17:49 +00002247 // If all bits are not useful, just return UNDEF.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002248 if (!NUsefulBits) {
2249 CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0));
2250 return true;
2251 }
Weiming Zhao56ab5182015-12-01 19:17:49 +00002252
Chad Rosier816a67d2016-05-26 13:27:56 +00002253 if (tryBitfieldInsertOpFromOr(N, NUsefulBits, CurDAG))
2254 return true;
2255
2256 return tryBitfieldInsertOpFromOrAndImm(N, CurDAG);
Tim Northover3b0846e2014-05-24 12:50:23 +00002257}
2258
Geoff Berry43ec15e2015-09-18 17:11:53 +00002259/// SelectBitfieldInsertInZeroOp - Match a UBFIZ instruction that is the
2260/// equivalent of a left shift by a constant amount followed by an and masking
2261/// out a contiguous set of bits.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002262bool AArch64DAGToDAGISel::tryBitfieldInsertInZeroOp(SDNode *N) {
Geoff Berry43ec15e2015-09-18 17:11:53 +00002263 if (N->getOpcode() != ISD::AND)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002264 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002265
2266 EVT VT = N->getValueType(0);
Chad Rosier08d99082016-05-13 22:53:13 +00002267 if (VT != MVT::i32 && VT != MVT::i64)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002268 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002269
2270 SDValue Op0;
2271 int DstLSB, Width;
2272 if (!isBitfieldPositioningOp(CurDAG, SDValue(N, 0), /*BiggerPattern=*/false,
2273 Op0, DstLSB, Width))
Justin Bogner283e3bd2016-05-12 23:10:30 +00002274 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002275
2276 // ImmR is the rotate right amount.
2277 unsigned ImmR = (VT.getSizeInBits() - DstLSB) % VT.getSizeInBits();
2278 // ImmS is the most significant bit of the source to be moved.
2279 unsigned ImmS = Width - 1;
2280
2281 SDLoc DL(N);
2282 SDValue Ops[] = {Op0, CurDAG->getTargetConstant(ImmR, DL, VT),
2283 CurDAG->getTargetConstant(ImmS, DL, VT)};
Chad Rosier08d99082016-05-13 22:53:13 +00002284 unsigned Opc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
Justin Bogner283e3bd2016-05-12 23:10:30 +00002285 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2286 return true;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002287}
2288
Tim Northover3b0846e2014-05-24 12:50:23 +00002289bool
2290AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
2291 unsigned RegWidth) {
2292 APFloat FVal(0.0);
2293 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
2294 FVal = CN->getValueAPF();
2295 else if (LoadSDNode *LN = dyn_cast<LoadSDNode>(N)) {
2296 // Some otherwise illegal constants are allowed in this case.
2297 if (LN->getOperand(1).getOpcode() != AArch64ISD::ADDlow ||
2298 !isa<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1)))
2299 return false;
2300
2301 ConstantPoolSDNode *CN =
2302 dyn_cast<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1));
2303 FVal = cast<ConstantFP>(CN->getConstVal())->getValueAPF();
2304 } else
2305 return false;
2306
2307 // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
2308 // is between 1 and 32 for a destination w-register, or 1 and 64 for an
2309 // x-register.
2310 //
2311 // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
2312 // want THIS_NODE to be 2^fbits. This is much easier to deal with using
2313 // integers.
2314 bool IsExact;
2315
2316 // fbits is between 1 and 64 in the worst-case, which means the fmul
2317 // could have 2^64 as an actual operand. Need 65 bits of precision.
2318 APSInt IntVal(65, true);
2319 FVal.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
2320
2321 // N.b. isPowerOf2 also checks for > 0.
2322 if (!IsExact || !IntVal.isPowerOf2()) return false;
2323 unsigned FBits = IntVal.logBase2();
2324
2325 // Checks above should have guaranteed that we haven't lost information in
2326 // finding FBits, but it must still be in range.
2327 if (FBits == 0 || FBits > RegWidth) return false;
2328
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002329 FixedPos = CurDAG->getTargetConstant(FBits, SDLoc(N), MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +00002330 return true;
2331}
2332
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002333// Inspects a register string of the form o0:op1:CRn:CRm:op2 gets the fields
2334// of the string and obtains the integer values from them and combines these
2335// into a single value to be used in the MRS/MSR instruction.
2336static int getIntOperandFromRegisterString(StringRef RegString) {
2337 SmallVector<StringRef, 5> Fields;
Chandler Carruthe4405e92015-09-10 06:12:31 +00002338 RegString.split(Fields, ':');
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002339
2340 if (Fields.size() == 1)
2341 return -1;
2342
2343 assert(Fields.size() == 5
2344 && "Invalid number of fields in read register string");
2345
2346 SmallVector<int, 5> Ops;
2347 bool AllIntFields = true;
2348
2349 for (StringRef Field : Fields) {
2350 unsigned IntField;
2351 AllIntFields &= !Field.getAsInteger(10, IntField);
2352 Ops.push_back(IntField);
2353 }
2354
2355 assert(AllIntFields &&
2356 "Unexpected non-integer value in special register string.");
2357
2358 // Need to combine the integer fields of the string into a single value
2359 // based on the bit encoding of MRS/MSR instruction.
2360 return (Ops[0] << 14) | (Ops[1] << 11) | (Ops[2] << 7) |
2361 (Ops[3] << 3) | (Ops[4]);
2362}
2363
2364// Lower the read_register intrinsic to an MRS instruction node if the special
2365// register string argument is either of the form detailed in the ALCE (the
2366// form described in getIntOperandsFromRegsterString) or is a named register
2367// known by the MRS SysReg mapper.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002368bool AArch64DAGToDAGISel::tryReadRegister(SDNode *N) {
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002369 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2370 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2371 SDLoc DL(N);
2372
2373 int Reg = getIntOperandFromRegisterString(RegString->getString());
Justin Bogner283e3bd2016-05-12 23:10:30 +00002374 if (Reg != -1) {
2375 ReplaceNode(N, CurDAG->getMachineNode(
2376 AArch64::MRS, DL, N->getSimpleValueType(0), MVT::Other,
2377 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2378 N->getOperand(0)));
2379 return true;
2380 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002381
2382 // Use the sysreg mapper to map the remaining possible strings to the
2383 // value for the register to be used for the instruction operand.
2384 AArch64SysReg::MRSMapper mapper;
2385 bool IsValidSpecialReg;
2386 Reg = mapper.fromString(RegString->getString(),
2387 Subtarget->getFeatureBits(),
2388 IsValidSpecialReg);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002389 if (IsValidSpecialReg) {
2390 ReplaceNode(N, CurDAG->getMachineNode(
2391 AArch64::MRS, DL, N->getSimpleValueType(0), MVT::Other,
2392 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2393 N->getOperand(0)));
2394 return true;
2395 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002396
Justin Bogner283e3bd2016-05-12 23:10:30 +00002397 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002398}
2399
2400// Lower the write_register intrinsic to an MSR instruction node if the special
2401// register string argument is either of the form detailed in the ALCE (the
2402// form described in getIntOperandsFromRegsterString) or is a named register
2403// known by the MSR SysReg mapper.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002404bool AArch64DAGToDAGISel::tryWriteRegister(SDNode *N) {
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002405 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2406 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2407 SDLoc DL(N);
2408
2409 int Reg = getIntOperandFromRegisterString(RegString->getString());
Justin Bogner283e3bd2016-05-12 23:10:30 +00002410 if (Reg != -1) {
2411 ReplaceNode(
2412 N, CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002413 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
Justin Bogner283e3bd2016-05-12 23:10:30 +00002414 N->getOperand(2), N->getOperand(0)));
2415 return true;
2416 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002417
2418 // Check if the register was one of those allowed as the pstatefield value in
2419 // the MSR (immediate) instruction. To accept the values allowed in the
2420 // pstatefield for the MSR (immediate) instruction, we also require that an
2421 // immediate value has been provided as an argument, we know that this is
2422 // the case as it has been ensured by semantic checking.
2423 AArch64PState::PStateMapper PMapper;
2424 bool IsValidSpecialReg;
2425 Reg = PMapper.fromString(RegString->getString(),
2426 Subtarget->getFeatureBits(),
2427 IsValidSpecialReg);
2428 if (IsValidSpecialReg) {
2429 assert (isa<ConstantSDNode>(N->getOperand(2))
2430 && "Expected a constant integer expression.");
2431 uint64_t Immed = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Alexandros Lamprineas1bab1912015-10-05 13:42:31 +00002432 unsigned State;
Oliver Stannard911ea202015-11-26 15:32:30 +00002433 if (Reg == AArch64PState::PAN || Reg == AArch64PState::UAO) {
Alexandros Lamprineas1bab1912015-10-05 13:42:31 +00002434 assert(Immed < 2 && "Bad imm");
2435 State = AArch64::MSRpstateImm1;
2436 } else {
2437 assert(Immed < 16 && "Bad imm");
2438 State = AArch64::MSRpstateImm4;
2439 }
Justin Bogner283e3bd2016-05-12 23:10:30 +00002440 ReplaceNode(N, CurDAG->getMachineNode(
2441 State, DL, MVT::Other,
2442 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2443 CurDAG->getTargetConstant(Immed, DL, MVT::i16),
2444 N->getOperand(0)));
2445 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002446 }
2447
2448 // Use the sysreg mapper to attempt to map the remaining possible strings
2449 // to the value for the register to be used for the MSR (register)
2450 // instruction operand.
2451 AArch64SysReg::MSRMapper Mapper;
2452 Reg = Mapper.fromString(RegString->getString(),
2453 Subtarget->getFeatureBits(),
2454 IsValidSpecialReg);
2455
Justin Bogner283e3bd2016-05-12 23:10:30 +00002456 if (IsValidSpecialReg) {
2457 ReplaceNode(
2458 N, CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002459 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
Justin Bogner283e3bd2016-05-12 23:10:30 +00002460 N->getOperand(2), N->getOperand(0)));
2461 return true;
2462 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002463
Justin Bogner283e3bd2016-05-12 23:10:30 +00002464 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002465}
2466
Tim Northovercdf15292016-04-14 17:03:29 +00002467/// We've got special pseudo-instructions for these
2468void AArch64DAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
2469 unsigned Opcode;
2470 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
2471 if (MemTy == MVT::i8)
2472 Opcode = AArch64::CMP_SWAP_8;
2473 else if (MemTy == MVT::i16)
2474 Opcode = AArch64::CMP_SWAP_16;
2475 else if (MemTy == MVT::i32)
2476 Opcode = AArch64::CMP_SWAP_32;
2477 else if (MemTy == MVT::i64)
2478 Opcode = AArch64::CMP_SWAP_64;
2479 else
2480 llvm_unreachable("Unknown AtomicCmpSwap type");
2481
2482 MVT RegTy = MemTy == MVT::i64 ? MVT::i64 : MVT::i32;
2483 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
2484 N->getOperand(0)};
2485 SDNode *CmpSwap = CurDAG->getMachineNode(
2486 Opcode, SDLoc(N),
2487 CurDAG->getVTList(RegTy, MVT::i32, MVT::Other), Ops);
2488
2489 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2490 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2491 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
2492
2493 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
2494 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00002495 CurDAG->RemoveDeadNode(N);
Tim Northovercdf15292016-04-14 17:03:29 +00002496}
2497
Justin Bogner283e3bd2016-05-12 23:10:30 +00002498void AArch64DAGToDAGISel::Select(SDNode *Node) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002499 // Dump information about the Node being selected
2500 DEBUG(errs() << "Selecting: ");
2501 DEBUG(Node->dump(CurDAG));
2502 DEBUG(errs() << "\n");
2503
2504 // If we have a custom node, we already have selected!
2505 if (Node->isMachineOpcode()) {
2506 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
2507 Node->setNodeId(-1);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002508 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002509 }
2510
2511 // Few custom selection stuff.
Tim Northover3b0846e2014-05-24 12:50:23 +00002512 EVT VT = Node->getValueType(0);
2513
2514 switch (Node->getOpcode()) {
2515 default:
2516 break;
2517
Tim Northovercdf15292016-04-14 17:03:29 +00002518 case ISD::ATOMIC_CMP_SWAP:
2519 SelectCMP_SWAP(Node);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002520 return;
Tim Northovercdf15292016-04-14 17:03:29 +00002521
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002522 case ISD::READ_REGISTER:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002523 if (tryReadRegister(Node))
2524 return;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002525 break;
2526
2527 case ISD::WRITE_REGISTER:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002528 if (tryWriteRegister(Node))
2529 return;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002530 break;
2531
Tim Northover3b0846e2014-05-24 12:50:23 +00002532 case ISD::ADD:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002533 if (tryMLAV64LaneV128(Node))
2534 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002535 break;
2536
2537 case ISD::LOAD: {
2538 // Try to select as an indexed load. Fall through to normal processing
2539 // if we can't.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002540 if (tryIndexedLoad(Node))
2541 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002542 break;
2543 }
2544
2545 case ISD::SRL:
2546 case ISD::AND:
2547 case ISD::SRA:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002548 if (tryBitfieldExtractOp(Node))
2549 return;
2550 if (tryBitfieldInsertInZeroOp(Node))
2551 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002552 break;
2553
2554 case ISD::OR:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002555 if (tryBitfieldInsertOp(Node))
2556 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002557 break;
2558
2559 case ISD::EXTRACT_VECTOR_ELT: {
2560 // Extracting lane zero is a special case where we can just use a plain
2561 // EXTRACT_SUBREG instruction, which will become FMOV. This is easier for
2562 // the rest of the compiler, especially the register allocator and copyi
2563 // propagation, to reason about, so is preferred when it's possible to
2564 // use it.
2565 ConstantSDNode *LaneNode = cast<ConstantSDNode>(Node->getOperand(1));
2566 // Bail and use the default Select() for non-zero lanes.
2567 if (LaneNode->getZExtValue() != 0)
2568 break;
2569 // If the element type is not the same as the result type, likewise
2570 // bail and use the default Select(), as there's more to do than just
2571 // a cross-class COPY. This catches extracts of i8 and i16 elements
2572 // since they will need an explicit zext.
2573 if (VT != Node->getOperand(0).getValueType().getVectorElementType())
2574 break;
2575 unsigned SubReg;
2576 switch (Node->getOperand(0)
2577 .getValueType()
2578 .getVectorElementType()
2579 .getSizeInBits()) {
2580 default:
Craig Topper2a30d782014-06-18 05:05:13 +00002581 llvm_unreachable("Unexpected vector element type!");
Tim Northover3b0846e2014-05-24 12:50:23 +00002582 case 64:
2583 SubReg = AArch64::dsub;
2584 break;
2585 case 32:
2586 SubReg = AArch64::ssub;
2587 break;
Oliver Stannard89d15422014-08-27 16:16:04 +00002588 case 16:
2589 SubReg = AArch64::hsub;
2590 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00002591 case 8:
2592 llvm_unreachable("unexpected zext-requiring extract element!");
2593 }
2594 SDValue Extract = CurDAG->getTargetExtractSubreg(SubReg, SDLoc(Node), VT,
2595 Node->getOperand(0));
2596 DEBUG(dbgs() << "ISEL: Custom selection!\n=> ");
2597 DEBUG(Extract->dumpr(CurDAG));
2598 DEBUG(dbgs() << "\n");
Justin Bogner283e3bd2016-05-12 23:10:30 +00002599 ReplaceNode(Node, Extract.getNode());
2600 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002601 }
2602 case ISD::Constant: {
2603 // Materialize zero constants as copies from WZR/XZR. This allows
2604 // the coalescer to propagate these into other instructions.
2605 ConstantSDNode *ConstNode = cast<ConstantSDNode>(Node);
2606 if (ConstNode->isNullValue()) {
Justin Bogner283e3bd2016-05-12 23:10:30 +00002607 if (VT == MVT::i32) {
2608 SDValue New = CurDAG->getCopyFromReg(
2609 CurDAG->getEntryNode(), SDLoc(Node), AArch64::WZR, MVT::i32);
2610 ReplaceNode(Node, New.getNode());
2611 return;
2612 } else if (VT == MVT::i64) {
2613 SDValue New = CurDAG->getCopyFromReg(
2614 CurDAG->getEntryNode(), SDLoc(Node), AArch64::XZR, MVT::i64);
2615 ReplaceNode(Node, New.getNode());
2616 return;
2617 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002618 }
2619 break;
2620 }
2621
2622 case ISD::FrameIndex: {
2623 // Selects to ADDXri FI, 0 which in turn will become ADDXri SP, imm.
2624 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
2625 unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
2626 const TargetLowering *TLI = getTargetLowering();
Mehdi Amini44ede332015-07-09 02:09:04 +00002627 SDValue TFI = CurDAG->getTargetFrameIndex(
2628 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002629 SDLoc DL(Node);
2630 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, DL, MVT::i32),
2631 CurDAG->getTargetConstant(Shifter, DL, MVT::i32) };
Justin Bogner283e3bd2016-05-12 23:10:30 +00002632 CurDAG->SelectNodeTo(Node, AArch64::ADDXri, MVT::i64, Ops);
2633 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002634 }
2635 case ISD::INTRINSIC_W_CHAIN: {
2636 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2637 switch (IntNo) {
2638 default:
2639 break;
2640 case Intrinsic::aarch64_ldaxp:
2641 case Intrinsic::aarch64_ldxp: {
2642 unsigned Op =
2643 IntNo == Intrinsic::aarch64_ldaxp ? AArch64::LDAXPX : AArch64::LDXPX;
2644 SDValue MemAddr = Node->getOperand(2);
2645 SDLoc DL(Node);
2646 SDValue Chain = Node->getOperand(0);
2647
2648 SDNode *Ld = CurDAG->getMachineNode(Op, DL, MVT::i64, MVT::i64,
2649 MVT::Other, MemAddr, Chain);
2650
2651 // Transfer memoperands.
2652 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2653 MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2654 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002655 ReplaceNode(Node, Ld);
2656 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002657 }
2658 case Intrinsic::aarch64_stlxp:
2659 case Intrinsic::aarch64_stxp: {
2660 unsigned Op =
2661 IntNo == Intrinsic::aarch64_stlxp ? AArch64::STLXPX : AArch64::STXPX;
2662 SDLoc DL(Node);
2663 SDValue Chain = Node->getOperand(0);
2664 SDValue ValLo = Node->getOperand(2);
2665 SDValue ValHi = Node->getOperand(3);
2666 SDValue MemAddr = Node->getOperand(4);
2667
2668 // Place arguments in the right order.
Benjamin Kramerea68a942015-02-19 15:26:17 +00002669 SDValue Ops[] = {ValLo, ValHi, MemAddr, Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00002670
2671 SDNode *St = CurDAG->getMachineNode(Op, DL, MVT::i32, MVT::Other, Ops);
2672 // Transfer memoperands.
2673 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2674 MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2675 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
2676
Justin Bogner283e3bd2016-05-12 23:10:30 +00002677 ReplaceNode(Node, St);
2678 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002679 }
2680 case Intrinsic::aarch64_neon_ld1x2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002681 if (VT == MVT::v8i8) {
2682 SelectLoad(Node, 2, AArch64::LD1Twov8b, AArch64::dsub0);
2683 return;
2684 } else if (VT == MVT::v16i8) {
2685 SelectLoad(Node, 2, AArch64::LD1Twov16b, AArch64::qsub0);
2686 return;
2687 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2688 SelectLoad(Node, 2, AArch64::LD1Twov4h, AArch64::dsub0);
2689 return;
2690 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2691 SelectLoad(Node, 2, AArch64::LD1Twov8h, AArch64::qsub0);
2692 return;
2693 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2694 SelectLoad(Node, 2, AArch64::LD1Twov2s, AArch64::dsub0);
2695 return;
2696 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2697 SelectLoad(Node, 2, AArch64::LD1Twov4s, AArch64::qsub0);
2698 return;
2699 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2700 SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2701 return;
2702 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2703 SelectLoad(Node, 2, AArch64::LD1Twov2d, AArch64::qsub0);
2704 return;
2705 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002706 break;
2707 case Intrinsic::aarch64_neon_ld1x3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002708 if (VT == MVT::v8i8) {
2709 SelectLoad(Node, 3, AArch64::LD1Threev8b, AArch64::dsub0);
2710 return;
2711 } else if (VT == MVT::v16i8) {
2712 SelectLoad(Node, 3, AArch64::LD1Threev16b, AArch64::qsub0);
2713 return;
2714 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2715 SelectLoad(Node, 3, AArch64::LD1Threev4h, AArch64::dsub0);
2716 return;
2717 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2718 SelectLoad(Node, 3, AArch64::LD1Threev8h, AArch64::qsub0);
2719 return;
2720 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2721 SelectLoad(Node, 3, AArch64::LD1Threev2s, AArch64::dsub0);
2722 return;
2723 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2724 SelectLoad(Node, 3, AArch64::LD1Threev4s, AArch64::qsub0);
2725 return;
2726 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2727 SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2728 return;
2729 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2730 SelectLoad(Node, 3, AArch64::LD1Threev2d, AArch64::qsub0);
2731 return;
2732 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002733 break;
2734 case Intrinsic::aarch64_neon_ld1x4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002735 if (VT == MVT::v8i8) {
2736 SelectLoad(Node, 4, AArch64::LD1Fourv8b, AArch64::dsub0);
2737 return;
2738 } else if (VT == MVT::v16i8) {
2739 SelectLoad(Node, 4, AArch64::LD1Fourv16b, AArch64::qsub0);
2740 return;
2741 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2742 SelectLoad(Node, 4, AArch64::LD1Fourv4h, AArch64::dsub0);
2743 return;
2744 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2745 SelectLoad(Node, 4, AArch64::LD1Fourv8h, AArch64::qsub0);
2746 return;
2747 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2748 SelectLoad(Node, 4, AArch64::LD1Fourv2s, AArch64::dsub0);
2749 return;
2750 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2751 SelectLoad(Node, 4, AArch64::LD1Fourv4s, AArch64::qsub0);
2752 return;
2753 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2754 SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2755 return;
2756 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2757 SelectLoad(Node, 4, AArch64::LD1Fourv2d, AArch64::qsub0);
2758 return;
2759 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002760 break;
2761 case Intrinsic::aarch64_neon_ld2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002762 if (VT == MVT::v8i8) {
2763 SelectLoad(Node, 2, AArch64::LD2Twov8b, AArch64::dsub0);
2764 return;
2765 } else if (VT == MVT::v16i8) {
2766 SelectLoad(Node, 2, AArch64::LD2Twov16b, AArch64::qsub0);
2767 return;
2768 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2769 SelectLoad(Node, 2, AArch64::LD2Twov4h, AArch64::dsub0);
2770 return;
2771 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2772 SelectLoad(Node, 2, AArch64::LD2Twov8h, AArch64::qsub0);
2773 return;
2774 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2775 SelectLoad(Node, 2, AArch64::LD2Twov2s, AArch64::dsub0);
2776 return;
2777 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2778 SelectLoad(Node, 2, AArch64::LD2Twov4s, AArch64::qsub0);
2779 return;
2780 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2781 SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2782 return;
2783 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2784 SelectLoad(Node, 2, AArch64::LD2Twov2d, AArch64::qsub0);
2785 return;
2786 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002787 break;
2788 case Intrinsic::aarch64_neon_ld3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002789 if (VT == MVT::v8i8) {
2790 SelectLoad(Node, 3, AArch64::LD3Threev8b, AArch64::dsub0);
2791 return;
2792 } else if (VT == MVT::v16i8) {
2793 SelectLoad(Node, 3, AArch64::LD3Threev16b, AArch64::qsub0);
2794 return;
2795 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2796 SelectLoad(Node, 3, AArch64::LD3Threev4h, AArch64::dsub0);
2797 return;
2798 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2799 SelectLoad(Node, 3, AArch64::LD3Threev8h, AArch64::qsub0);
2800 return;
2801 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2802 SelectLoad(Node, 3, AArch64::LD3Threev2s, AArch64::dsub0);
2803 return;
2804 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2805 SelectLoad(Node, 3, AArch64::LD3Threev4s, AArch64::qsub0);
2806 return;
2807 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2808 SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2809 return;
2810 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2811 SelectLoad(Node, 3, AArch64::LD3Threev2d, AArch64::qsub0);
2812 return;
2813 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002814 break;
2815 case Intrinsic::aarch64_neon_ld4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002816 if (VT == MVT::v8i8) {
2817 SelectLoad(Node, 4, AArch64::LD4Fourv8b, AArch64::dsub0);
2818 return;
2819 } else if (VT == MVT::v16i8) {
2820 SelectLoad(Node, 4, AArch64::LD4Fourv16b, AArch64::qsub0);
2821 return;
2822 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2823 SelectLoad(Node, 4, AArch64::LD4Fourv4h, AArch64::dsub0);
2824 return;
2825 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2826 SelectLoad(Node, 4, AArch64::LD4Fourv8h, AArch64::qsub0);
2827 return;
2828 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2829 SelectLoad(Node, 4, AArch64::LD4Fourv2s, AArch64::dsub0);
2830 return;
2831 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2832 SelectLoad(Node, 4, AArch64::LD4Fourv4s, AArch64::qsub0);
2833 return;
2834 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2835 SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2836 return;
2837 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2838 SelectLoad(Node, 4, AArch64::LD4Fourv2d, AArch64::qsub0);
2839 return;
2840 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002841 break;
2842 case Intrinsic::aarch64_neon_ld2r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002843 if (VT == MVT::v8i8) {
2844 SelectLoad(Node, 2, AArch64::LD2Rv8b, AArch64::dsub0);
2845 return;
2846 } else if (VT == MVT::v16i8) {
2847 SelectLoad(Node, 2, AArch64::LD2Rv16b, AArch64::qsub0);
2848 return;
2849 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2850 SelectLoad(Node, 2, AArch64::LD2Rv4h, AArch64::dsub0);
2851 return;
2852 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2853 SelectLoad(Node, 2, AArch64::LD2Rv8h, AArch64::qsub0);
2854 return;
2855 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2856 SelectLoad(Node, 2, AArch64::LD2Rv2s, AArch64::dsub0);
2857 return;
2858 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2859 SelectLoad(Node, 2, AArch64::LD2Rv4s, AArch64::qsub0);
2860 return;
2861 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2862 SelectLoad(Node, 2, AArch64::LD2Rv1d, AArch64::dsub0);
2863 return;
2864 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2865 SelectLoad(Node, 2, AArch64::LD2Rv2d, AArch64::qsub0);
2866 return;
2867 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002868 break;
2869 case Intrinsic::aarch64_neon_ld3r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002870 if (VT == MVT::v8i8) {
2871 SelectLoad(Node, 3, AArch64::LD3Rv8b, AArch64::dsub0);
2872 return;
2873 } else if (VT == MVT::v16i8) {
2874 SelectLoad(Node, 3, AArch64::LD3Rv16b, AArch64::qsub0);
2875 return;
2876 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2877 SelectLoad(Node, 3, AArch64::LD3Rv4h, AArch64::dsub0);
2878 return;
2879 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2880 SelectLoad(Node, 3, AArch64::LD3Rv8h, AArch64::qsub0);
2881 return;
2882 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2883 SelectLoad(Node, 3, AArch64::LD3Rv2s, AArch64::dsub0);
2884 return;
2885 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2886 SelectLoad(Node, 3, AArch64::LD3Rv4s, AArch64::qsub0);
2887 return;
2888 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2889 SelectLoad(Node, 3, AArch64::LD3Rv1d, AArch64::dsub0);
2890 return;
2891 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2892 SelectLoad(Node, 3, AArch64::LD3Rv2d, AArch64::qsub0);
2893 return;
2894 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002895 break;
2896 case Intrinsic::aarch64_neon_ld4r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002897 if (VT == MVT::v8i8) {
2898 SelectLoad(Node, 4, AArch64::LD4Rv8b, AArch64::dsub0);
2899 return;
2900 } else if (VT == MVT::v16i8) {
2901 SelectLoad(Node, 4, AArch64::LD4Rv16b, AArch64::qsub0);
2902 return;
2903 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2904 SelectLoad(Node, 4, AArch64::LD4Rv4h, AArch64::dsub0);
2905 return;
2906 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2907 SelectLoad(Node, 4, AArch64::LD4Rv8h, AArch64::qsub0);
2908 return;
2909 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2910 SelectLoad(Node, 4, AArch64::LD4Rv2s, AArch64::dsub0);
2911 return;
2912 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2913 SelectLoad(Node, 4, AArch64::LD4Rv4s, AArch64::qsub0);
2914 return;
2915 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2916 SelectLoad(Node, 4, AArch64::LD4Rv1d, AArch64::dsub0);
2917 return;
2918 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2919 SelectLoad(Node, 4, AArch64::LD4Rv2d, AArch64::qsub0);
2920 return;
2921 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002922 break;
2923 case Intrinsic::aarch64_neon_ld2lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002924 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
2925 SelectLoadLane(Node, 2, AArch64::LD2i8);
2926 return;
2927 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2928 VT == MVT::v8f16) {
2929 SelectLoadLane(Node, 2, AArch64::LD2i16);
2930 return;
2931 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2932 VT == MVT::v2f32) {
2933 SelectLoadLane(Node, 2, AArch64::LD2i32);
2934 return;
2935 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2936 VT == MVT::v1f64) {
2937 SelectLoadLane(Node, 2, AArch64::LD2i64);
2938 return;
2939 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002940 break;
2941 case Intrinsic::aarch64_neon_ld3lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002942 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
2943 SelectLoadLane(Node, 3, AArch64::LD3i8);
2944 return;
2945 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2946 VT == MVT::v8f16) {
2947 SelectLoadLane(Node, 3, AArch64::LD3i16);
2948 return;
2949 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2950 VT == MVT::v2f32) {
2951 SelectLoadLane(Node, 3, AArch64::LD3i32);
2952 return;
2953 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2954 VT == MVT::v1f64) {
2955 SelectLoadLane(Node, 3, AArch64::LD3i64);
2956 return;
2957 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002958 break;
2959 case Intrinsic::aarch64_neon_ld4lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002960 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
2961 SelectLoadLane(Node, 4, AArch64::LD4i8);
2962 return;
2963 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2964 VT == MVT::v8f16) {
2965 SelectLoadLane(Node, 4, AArch64::LD4i16);
2966 return;
2967 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2968 VT == MVT::v2f32) {
2969 SelectLoadLane(Node, 4, AArch64::LD4i32);
2970 return;
2971 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2972 VT == MVT::v1f64) {
2973 SelectLoadLane(Node, 4, AArch64::LD4i64);
2974 return;
2975 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002976 break;
2977 }
2978 } break;
2979 case ISD::INTRINSIC_WO_CHAIN: {
2980 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
2981 switch (IntNo) {
2982 default:
2983 break;
2984 case Intrinsic::aarch64_neon_tbl2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002985 SelectTable(Node, 2,
2986 VT == MVT::v8i8 ? AArch64::TBLv8i8Two : AArch64::TBLv16i8Two,
2987 false);
2988 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002989 case Intrinsic::aarch64_neon_tbl3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002990 SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBLv8i8Three
2991 : AArch64::TBLv16i8Three,
2992 false);
2993 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002994 case Intrinsic::aarch64_neon_tbl4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002995 SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBLv8i8Four
2996 : AArch64::TBLv16i8Four,
2997 false);
2998 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002999 case Intrinsic::aarch64_neon_tbx2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003000 SelectTable(Node, 2,
3001 VT == MVT::v8i8 ? AArch64::TBXv8i8Two : AArch64::TBXv16i8Two,
3002 true);
3003 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003004 case Intrinsic::aarch64_neon_tbx3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003005 SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBXv8i8Three
3006 : AArch64::TBXv16i8Three,
3007 true);
3008 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003009 case Intrinsic::aarch64_neon_tbx4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003010 SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBXv8i8Four
3011 : AArch64::TBXv16i8Four,
3012 true);
3013 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003014 case Intrinsic::aarch64_neon_smull:
3015 case Intrinsic::aarch64_neon_umull:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003016 if (tryMULLV64LaneV128(IntNo, Node))
3017 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003018 break;
3019 }
3020 break;
3021 }
3022 case ISD::INTRINSIC_VOID: {
3023 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
3024 if (Node->getNumOperands() >= 3)
3025 VT = Node->getOperand(2)->getValueType(0);
3026 switch (IntNo) {
3027 default:
3028 break;
3029 case Intrinsic::aarch64_neon_st1x2: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003030 if (VT == MVT::v8i8) {
3031 SelectStore(Node, 2, AArch64::ST1Twov8b);
3032 return;
3033 } else if (VT == MVT::v16i8) {
3034 SelectStore(Node, 2, AArch64::ST1Twov16b);
3035 return;
3036 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3037 SelectStore(Node, 2, AArch64::ST1Twov4h);
3038 return;
3039 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3040 SelectStore(Node, 2, AArch64::ST1Twov8h);
3041 return;
3042 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3043 SelectStore(Node, 2, AArch64::ST1Twov2s);
3044 return;
3045 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3046 SelectStore(Node, 2, AArch64::ST1Twov4s);
3047 return;
3048 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3049 SelectStore(Node, 2, AArch64::ST1Twov2d);
3050 return;
3051 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3052 SelectStore(Node, 2, AArch64::ST1Twov1d);
3053 return;
3054 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003055 break;
3056 }
3057 case Intrinsic::aarch64_neon_st1x3: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003058 if (VT == MVT::v8i8) {
3059 SelectStore(Node, 3, AArch64::ST1Threev8b);
3060 return;
3061 } else if (VT == MVT::v16i8) {
3062 SelectStore(Node, 3, AArch64::ST1Threev16b);
3063 return;
3064 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3065 SelectStore(Node, 3, AArch64::ST1Threev4h);
3066 return;
3067 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3068 SelectStore(Node, 3, AArch64::ST1Threev8h);
3069 return;
3070 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3071 SelectStore(Node, 3, AArch64::ST1Threev2s);
3072 return;
3073 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3074 SelectStore(Node, 3, AArch64::ST1Threev4s);
3075 return;
3076 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3077 SelectStore(Node, 3, AArch64::ST1Threev2d);
3078 return;
3079 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3080 SelectStore(Node, 3, AArch64::ST1Threev1d);
3081 return;
3082 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003083 break;
3084 }
3085 case Intrinsic::aarch64_neon_st1x4: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003086 if (VT == MVT::v8i8) {
3087 SelectStore(Node, 4, AArch64::ST1Fourv8b);
3088 return;
3089 } else if (VT == MVT::v16i8) {
3090 SelectStore(Node, 4, AArch64::ST1Fourv16b);
3091 return;
3092 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3093 SelectStore(Node, 4, AArch64::ST1Fourv4h);
3094 return;
3095 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3096 SelectStore(Node, 4, AArch64::ST1Fourv8h);
3097 return;
3098 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3099 SelectStore(Node, 4, AArch64::ST1Fourv2s);
3100 return;
3101 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3102 SelectStore(Node, 4, AArch64::ST1Fourv4s);
3103 return;
3104 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3105 SelectStore(Node, 4, AArch64::ST1Fourv2d);
3106 return;
3107 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3108 SelectStore(Node, 4, AArch64::ST1Fourv1d);
3109 return;
3110 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003111 break;
3112 }
3113 case Intrinsic::aarch64_neon_st2: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003114 if (VT == MVT::v8i8) {
3115 SelectStore(Node, 2, AArch64::ST2Twov8b);
3116 return;
3117 } else if (VT == MVT::v16i8) {
3118 SelectStore(Node, 2, AArch64::ST2Twov16b);
3119 return;
3120 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3121 SelectStore(Node, 2, AArch64::ST2Twov4h);
3122 return;
3123 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3124 SelectStore(Node, 2, AArch64::ST2Twov8h);
3125 return;
3126 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3127 SelectStore(Node, 2, AArch64::ST2Twov2s);
3128 return;
3129 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3130 SelectStore(Node, 2, AArch64::ST2Twov4s);
3131 return;
3132 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3133 SelectStore(Node, 2, AArch64::ST2Twov2d);
3134 return;
3135 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3136 SelectStore(Node, 2, AArch64::ST1Twov1d);
3137 return;
3138 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003139 break;
3140 }
3141 case Intrinsic::aarch64_neon_st3: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003142 if (VT == MVT::v8i8) {
3143 SelectStore(Node, 3, AArch64::ST3Threev8b);
3144 return;
3145 } else if (VT == MVT::v16i8) {
3146 SelectStore(Node, 3, AArch64::ST3Threev16b);
3147 return;
3148 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3149 SelectStore(Node, 3, AArch64::ST3Threev4h);
3150 return;
3151 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3152 SelectStore(Node, 3, AArch64::ST3Threev8h);
3153 return;
3154 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3155 SelectStore(Node, 3, AArch64::ST3Threev2s);
3156 return;
3157 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3158 SelectStore(Node, 3, AArch64::ST3Threev4s);
3159 return;
3160 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3161 SelectStore(Node, 3, AArch64::ST3Threev2d);
3162 return;
3163 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3164 SelectStore(Node, 3, AArch64::ST1Threev1d);
3165 return;
3166 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003167 break;
3168 }
3169 case Intrinsic::aarch64_neon_st4: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003170 if (VT == MVT::v8i8) {
3171 SelectStore(Node, 4, AArch64::ST4Fourv8b);
3172 return;
3173 } else if (VT == MVT::v16i8) {
3174 SelectStore(Node, 4, AArch64::ST4Fourv16b);
3175 return;
3176 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3177 SelectStore(Node, 4, AArch64::ST4Fourv4h);
3178 return;
3179 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3180 SelectStore(Node, 4, AArch64::ST4Fourv8h);
3181 return;
3182 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3183 SelectStore(Node, 4, AArch64::ST4Fourv2s);
3184 return;
3185 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3186 SelectStore(Node, 4, AArch64::ST4Fourv4s);
3187 return;
3188 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3189 SelectStore(Node, 4, AArch64::ST4Fourv2d);
3190 return;
3191 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3192 SelectStore(Node, 4, AArch64::ST1Fourv1d);
3193 return;
3194 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003195 break;
3196 }
3197 case Intrinsic::aarch64_neon_st2lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003198 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3199 SelectStoreLane(Node, 2, AArch64::ST2i8);
3200 return;
3201 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3202 VT == MVT::v8f16) {
3203 SelectStoreLane(Node, 2, AArch64::ST2i16);
3204 return;
3205 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3206 VT == MVT::v2f32) {
3207 SelectStoreLane(Node, 2, AArch64::ST2i32);
3208 return;
3209 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3210 VT == MVT::v1f64) {
3211 SelectStoreLane(Node, 2, AArch64::ST2i64);
3212 return;
3213 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003214 break;
3215 }
3216 case Intrinsic::aarch64_neon_st3lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003217 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3218 SelectStoreLane(Node, 3, AArch64::ST3i8);
3219 return;
3220 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3221 VT == MVT::v8f16) {
3222 SelectStoreLane(Node, 3, AArch64::ST3i16);
3223 return;
3224 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3225 VT == MVT::v2f32) {
3226 SelectStoreLane(Node, 3, AArch64::ST3i32);
3227 return;
3228 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3229 VT == MVT::v1f64) {
3230 SelectStoreLane(Node, 3, AArch64::ST3i64);
3231 return;
3232 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003233 break;
3234 }
3235 case Intrinsic::aarch64_neon_st4lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003236 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3237 SelectStoreLane(Node, 4, AArch64::ST4i8);
3238 return;
3239 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3240 VT == MVT::v8f16) {
3241 SelectStoreLane(Node, 4, AArch64::ST4i16);
3242 return;
3243 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3244 VT == MVT::v2f32) {
3245 SelectStoreLane(Node, 4, AArch64::ST4i32);
3246 return;
3247 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3248 VT == MVT::v1f64) {
3249 SelectStoreLane(Node, 4, AArch64::ST4i64);
3250 return;
3251 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003252 break;
3253 }
3254 }
Mehdi Aminia7583982015-08-23 00:42:57 +00003255 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00003256 }
3257 case AArch64ISD::LD2post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003258 if (VT == MVT::v8i8) {
3259 SelectPostLoad(Node, 2, AArch64::LD2Twov8b_POST, AArch64::dsub0);
3260 return;
3261 } else if (VT == MVT::v16i8) {
3262 SelectPostLoad(Node, 2, AArch64::LD2Twov16b_POST, AArch64::qsub0);
3263 return;
3264 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3265 SelectPostLoad(Node, 2, AArch64::LD2Twov4h_POST, AArch64::dsub0);
3266 return;
3267 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3268 SelectPostLoad(Node, 2, AArch64::LD2Twov8h_POST, AArch64::qsub0);
3269 return;
3270 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3271 SelectPostLoad(Node, 2, AArch64::LD2Twov2s_POST, AArch64::dsub0);
3272 return;
3273 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3274 SelectPostLoad(Node, 2, AArch64::LD2Twov4s_POST, AArch64::qsub0);
3275 return;
3276 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3277 SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
3278 return;
3279 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3280 SelectPostLoad(Node, 2, AArch64::LD2Twov2d_POST, AArch64::qsub0);
3281 return;
3282 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003283 break;
3284 }
3285 case AArch64ISD::LD3post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003286 if (VT == MVT::v8i8) {
3287 SelectPostLoad(Node, 3, AArch64::LD3Threev8b_POST, AArch64::dsub0);
3288 return;
3289 } else if (VT == MVT::v16i8) {
3290 SelectPostLoad(Node, 3, AArch64::LD3Threev16b_POST, AArch64::qsub0);
3291 return;
3292 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3293 SelectPostLoad(Node, 3, AArch64::LD3Threev4h_POST, AArch64::dsub0);
3294 return;
3295 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3296 SelectPostLoad(Node, 3, AArch64::LD3Threev8h_POST, AArch64::qsub0);
3297 return;
3298 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3299 SelectPostLoad(Node, 3, AArch64::LD3Threev2s_POST, AArch64::dsub0);
3300 return;
3301 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3302 SelectPostLoad(Node, 3, AArch64::LD3Threev4s_POST, AArch64::qsub0);
3303 return;
3304 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3305 SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3306 return;
3307 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3308 SelectPostLoad(Node, 3, AArch64::LD3Threev2d_POST, AArch64::qsub0);
3309 return;
3310 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003311 break;
3312 }
3313 case AArch64ISD::LD4post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003314 if (VT == MVT::v8i8) {
3315 SelectPostLoad(Node, 4, AArch64::LD4Fourv8b_POST, AArch64::dsub0);
3316 return;
3317 } else if (VT == MVT::v16i8) {
3318 SelectPostLoad(Node, 4, AArch64::LD4Fourv16b_POST, AArch64::qsub0);
3319 return;
3320 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3321 SelectPostLoad(Node, 4, AArch64::LD4Fourv4h_POST, AArch64::dsub0);
3322 return;
3323 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3324 SelectPostLoad(Node, 4, AArch64::LD4Fourv8h_POST, AArch64::qsub0);
3325 return;
3326 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3327 SelectPostLoad(Node, 4, AArch64::LD4Fourv2s_POST, AArch64::dsub0);
3328 return;
3329 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3330 SelectPostLoad(Node, 4, AArch64::LD4Fourv4s_POST, AArch64::qsub0);
3331 return;
3332 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3333 SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3334 return;
3335 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3336 SelectPostLoad(Node, 4, AArch64::LD4Fourv2d_POST, AArch64::qsub0);
3337 return;
3338 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003339 break;
3340 }
3341 case AArch64ISD::LD1x2post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003342 if (VT == MVT::v8i8) {
3343 SelectPostLoad(Node, 2, AArch64::LD1Twov8b_POST, AArch64::dsub0);
3344 return;
3345 } else if (VT == MVT::v16i8) {
3346 SelectPostLoad(Node, 2, AArch64::LD1Twov16b_POST, AArch64::qsub0);
3347 return;
3348 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3349 SelectPostLoad(Node, 2, AArch64::LD1Twov4h_POST, AArch64::dsub0);
3350 return;
3351 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3352 SelectPostLoad(Node, 2, AArch64::LD1Twov8h_POST, AArch64::qsub0);
3353 return;
3354 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3355 SelectPostLoad(Node, 2, AArch64::LD1Twov2s_POST, AArch64::dsub0);
3356 return;
3357 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3358 SelectPostLoad(Node, 2, AArch64::LD1Twov4s_POST, AArch64::qsub0);
3359 return;
3360 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3361 SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
3362 return;
3363 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3364 SelectPostLoad(Node, 2, AArch64::LD1Twov2d_POST, AArch64::qsub0);
3365 return;
3366 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003367 break;
3368 }
3369 case AArch64ISD::LD1x3post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003370 if (VT == MVT::v8i8) {
3371 SelectPostLoad(Node, 3, AArch64::LD1Threev8b_POST, AArch64::dsub0);
3372 return;
3373 } else if (VT == MVT::v16i8) {
3374 SelectPostLoad(Node, 3, AArch64::LD1Threev16b_POST, AArch64::qsub0);
3375 return;
3376 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3377 SelectPostLoad(Node, 3, AArch64::LD1Threev4h_POST, AArch64::dsub0);
3378 return;
3379 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3380 SelectPostLoad(Node, 3, AArch64::LD1Threev8h_POST, AArch64::qsub0);
3381 return;
3382 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3383 SelectPostLoad(Node, 3, AArch64::LD1Threev2s_POST, AArch64::dsub0);
3384 return;
3385 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3386 SelectPostLoad(Node, 3, AArch64::LD1Threev4s_POST, AArch64::qsub0);
3387 return;
3388 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3389 SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3390 return;
3391 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3392 SelectPostLoad(Node, 3, AArch64::LD1Threev2d_POST, AArch64::qsub0);
3393 return;
3394 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003395 break;
3396 }
3397 case AArch64ISD::LD1x4post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003398 if (VT == MVT::v8i8) {
3399 SelectPostLoad(Node, 4, AArch64::LD1Fourv8b_POST, AArch64::dsub0);
3400 return;
3401 } else if (VT == MVT::v16i8) {
3402 SelectPostLoad(Node, 4, AArch64::LD1Fourv16b_POST, AArch64::qsub0);
3403 return;
3404 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3405 SelectPostLoad(Node, 4, AArch64::LD1Fourv4h_POST, AArch64::dsub0);
3406 return;
3407 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3408 SelectPostLoad(Node, 4, AArch64::LD1Fourv8h_POST, AArch64::qsub0);
3409 return;
3410 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3411 SelectPostLoad(Node, 4, AArch64::LD1Fourv2s_POST, AArch64::dsub0);
3412 return;
3413 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3414 SelectPostLoad(Node, 4, AArch64::LD1Fourv4s_POST, AArch64::qsub0);
3415 return;
3416 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3417 SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3418 return;
3419 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3420 SelectPostLoad(Node, 4, AArch64::LD1Fourv2d_POST, AArch64::qsub0);
3421 return;
3422 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003423 break;
3424 }
3425 case AArch64ISD::LD1DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003426 if (VT == MVT::v8i8) {
3427 SelectPostLoad(Node, 1, AArch64::LD1Rv8b_POST, AArch64::dsub0);
3428 return;
3429 } else if (VT == MVT::v16i8) {
3430 SelectPostLoad(Node, 1, AArch64::LD1Rv16b_POST, AArch64::qsub0);
3431 return;
3432 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3433 SelectPostLoad(Node, 1, AArch64::LD1Rv4h_POST, AArch64::dsub0);
3434 return;
3435 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3436 SelectPostLoad(Node, 1, AArch64::LD1Rv8h_POST, AArch64::qsub0);
3437 return;
3438 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3439 SelectPostLoad(Node, 1, AArch64::LD1Rv2s_POST, AArch64::dsub0);
3440 return;
3441 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3442 SelectPostLoad(Node, 1, AArch64::LD1Rv4s_POST, AArch64::qsub0);
3443 return;
3444 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3445 SelectPostLoad(Node, 1, AArch64::LD1Rv1d_POST, AArch64::dsub0);
3446 return;
3447 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3448 SelectPostLoad(Node, 1, AArch64::LD1Rv2d_POST, AArch64::qsub0);
3449 return;
3450 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003451 break;
3452 }
3453 case AArch64ISD::LD2DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003454 if (VT == MVT::v8i8) {
3455 SelectPostLoad(Node, 2, AArch64::LD2Rv8b_POST, AArch64::dsub0);
3456 return;
3457 } else if (VT == MVT::v16i8) {
3458 SelectPostLoad(Node, 2, AArch64::LD2Rv16b_POST, AArch64::qsub0);
3459 return;
3460 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3461 SelectPostLoad(Node, 2, AArch64::LD2Rv4h_POST, AArch64::dsub0);
3462 return;
3463 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3464 SelectPostLoad(Node, 2, AArch64::LD2Rv8h_POST, AArch64::qsub0);
3465 return;
3466 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3467 SelectPostLoad(Node, 2, AArch64::LD2Rv2s_POST, AArch64::dsub0);
3468 return;
3469 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3470 SelectPostLoad(Node, 2, AArch64::LD2Rv4s_POST, AArch64::qsub0);
3471 return;
3472 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3473 SelectPostLoad(Node, 2, AArch64::LD2Rv1d_POST, AArch64::dsub0);
3474 return;
3475 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3476 SelectPostLoad(Node, 2, AArch64::LD2Rv2d_POST, AArch64::qsub0);
3477 return;
3478 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003479 break;
3480 }
3481 case AArch64ISD::LD3DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003482 if (VT == MVT::v8i8) {
3483 SelectPostLoad(Node, 3, AArch64::LD3Rv8b_POST, AArch64::dsub0);
3484 return;
3485 } else if (VT == MVT::v16i8) {
3486 SelectPostLoad(Node, 3, AArch64::LD3Rv16b_POST, AArch64::qsub0);
3487 return;
3488 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3489 SelectPostLoad(Node, 3, AArch64::LD3Rv4h_POST, AArch64::dsub0);
3490 return;
3491 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3492 SelectPostLoad(Node, 3, AArch64::LD3Rv8h_POST, AArch64::qsub0);
3493 return;
3494 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3495 SelectPostLoad(Node, 3, AArch64::LD3Rv2s_POST, AArch64::dsub0);
3496 return;
3497 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3498 SelectPostLoad(Node, 3, AArch64::LD3Rv4s_POST, AArch64::qsub0);
3499 return;
3500 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3501 SelectPostLoad(Node, 3, AArch64::LD3Rv1d_POST, AArch64::dsub0);
3502 return;
3503 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3504 SelectPostLoad(Node, 3, AArch64::LD3Rv2d_POST, AArch64::qsub0);
3505 return;
3506 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003507 break;
3508 }
3509 case AArch64ISD::LD4DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003510 if (VT == MVT::v8i8) {
3511 SelectPostLoad(Node, 4, AArch64::LD4Rv8b_POST, AArch64::dsub0);
3512 return;
3513 } else if (VT == MVT::v16i8) {
3514 SelectPostLoad(Node, 4, AArch64::LD4Rv16b_POST, AArch64::qsub0);
3515 return;
3516 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3517 SelectPostLoad(Node, 4, AArch64::LD4Rv4h_POST, AArch64::dsub0);
3518 return;
3519 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3520 SelectPostLoad(Node, 4, AArch64::LD4Rv8h_POST, AArch64::qsub0);
3521 return;
3522 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3523 SelectPostLoad(Node, 4, AArch64::LD4Rv2s_POST, AArch64::dsub0);
3524 return;
3525 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3526 SelectPostLoad(Node, 4, AArch64::LD4Rv4s_POST, AArch64::qsub0);
3527 return;
3528 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3529 SelectPostLoad(Node, 4, AArch64::LD4Rv1d_POST, AArch64::dsub0);
3530 return;
3531 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3532 SelectPostLoad(Node, 4, AArch64::LD4Rv2d_POST, AArch64::qsub0);
3533 return;
3534 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003535 break;
3536 }
3537 case AArch64ISD::LD1LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003538 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3539 SelectPostLoadLane(Node, 1, AArch64::LD1i8_POST);
3540 return;
3541 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3542 VT == MVT::v8f16) {
3543 SelectPostLoadLane(Node, 1, AArch64::LD1i16_POST);
3544 return;
3545 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3546 VT == MVT::v2f32) {
3547 SelectPostLoadLane(Node, 1, AArch64::LD1i32_POST);
3548 return;
3549 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3550 VT == MVT::v1f64) {
3551 SelectPostLoadLane(Node, 1, AArch64::LD1i64_POST);
3552 return;
3553 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003554 break;
3555 }
3556 case AArch64ISD::LD2LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003557 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3558 SelectPostLoadLane(Node, 2, AArch64::LD2i8_POST);
3559 return;
3560 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3561 VT == MVT::v8f16) {
3562 SelectPostLoadLane(Node, 2, AArch64::LD2i16_POST);
3563 return;
3564 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3565 VT == MVT::v2f32) {
3566 SelectPostLoadLane(Node, 2, AArch64::LD2i32_POST);
3567 return;
3568 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3569 VT == MVT::v1f64) {
3570 SelectPostLoadLane(Node, 2, AArch64::LD2i64_POST);
3571 return;
3572 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003573 break;
3574 }
3575 case AArch64ISD::LD3LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003576 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3577 SelectPostLoadLane(Node, 3, AArch64::LD3i8_POST);
3578 return;
3579 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3580 VT == MVT::v8f16) {
3581 SelectPostLoadLane(Node, 3, AArch64::LD3i16_POST);
3582 return;
3583 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3584 VT == MVT::v2f32) {
3585 SelectPostLoadLane(Node, 3, AArch64::LD3i32_POST);
3586 return;
3587 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3588 VT == MVT::v1f64) {
3589 SelectPostLoadLane(Node, 3, AArch64::LD3i64_POST);
3590 return;
3591 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003592 break;
3593 }
3594 case AArch64ISD::LD4LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003595 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3596 SelectPostLoadLane(Node, 4, AArch64::LD4i8_POST);
3597 return;
3598 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3599 VT == MVT::v8f16) {
3600 SelectPostLoadLane(Node, 4, AArch64::LD4i16_POST);
3601 return;
3602 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3603 VT == MVT::v2f32) {
3604 SelectPostLoadLane(Node, 4, AArch64::LD4i32_POST);
3605 return;
3606 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3607 VT == MVT::v1f64) {
3608 SelectPostLoadLane(Node, 4, AArch64::LD4i64_POST);
3609 return;
3610 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003611 break;
3612 }
3613 case AArch64ISD::ST2post: {
3614 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003615 if (VT == MVT::v8i8) {
3616 SelectPostStore(Node, 2, AArch64::ST2Twov8b_POST);
3617 return;
3618 } else if (VT == MVT::v16i8) {
3619 SelectPostStore(Node, 2, AArch64::ST2Twov16b_POST);
3620 return;
3621 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3622 SelectPostStore(Node, 2, AArch64::ST2Twov4h_POST);
3623 return;
3624 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3625 SelectPostStore(Node, 2, AArch64::ST2Twov8h_POST);
3626 return;
3627 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3628 SelectPostStore(Node, 2, AArch64::ST2Twov2s_POST);
3629 return;
3630 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3631 SelectPostStore(Node, 2, AArch64::ST2Twov4s_POST);
3632 return;
3633 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3634 SelectPostStore(Node, 2, AArch64::ST2Twov2d_POST);
3635 return;
3636 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3637 SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3638 return;
3639 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003640 break;
3641 }
3642 case AArch64ISD::ST3post: {
3643 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003644 if (VT == MVT::v8i8) {
3645 SelectPostStore(Node, 3, AArch64::ST3Threev8b_POST);
3646 return;
3647 } else if (VT == MVT::v16i8) {
3648 SelectPostStore(Node, 3, AArch64::ST3Threev16b_POST);
3649 return;
3650 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3651 SelectPostStore(Node, 3, AArch64::ST3Threev4h_POST);
3652 return;
3653 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3654 SelectPostStore(Node, 3, AArch64::ST3Threev8h_POST);
3655 return;
3656 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3657 SelectPostStore(Node, 3, AArch64::ST3Threev2s_POST);
3658 return;
3659 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3660 SelectPostStore(Node, 3, AArch64::ST3Threev4s_POST);
3661 return;
3662 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3663 SelectPostStore(Node, 3, AArch64::ST3Threev2d_POST);
3664 return;
3665 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3666 SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3667 return;
3668 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003669 break;
3670 }
3671 case AArch64ISD::ST4post: {
3672 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003673 if (VT == MVT::v8i8) {
3674 SelectPostStore(Node, 4, AArch64::ST4Fourv8b_POST);
3675 return;
3676 } else if (VT == MVT::v16i8) {
3677 SelectPostStore(Node, 4, AArch64::ST4Fourv16b_POST);
3678 return;
3679 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3680 SelectPostStore(Node, 4, AArch64::ST4Fourv4h_POST);
3681 return;
3682 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3683 SelectPostStore(Node, 4, AArch64::ST4Fourv8h_POST);
3684 return;
3685 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3686 SelectPostStore(Node, 4, AArch64::ST4Fourv2s_POST);
3687 return;
3688 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3689 SelectPostStore(Node, 4, AArch64::ST4Fourv4s_POST);
3690 return;
3691 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3692 SelectPostStore(Node, 4, AArch64::ST4Fourv2d_POST);
3693 return;
3694 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3695 SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3696 return;
3697 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003698 break;
3699 }
3700 case AArch64ISD::ST1x2post: {
3701 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003702 if (VT == MVT::v8i8) {
3703 SelectPostStore(Node, 2, AArch64::ST1Twov8b_POST);
3704 return;
3705 } else if (VT == MVT::v16i8) {
3706 SelectPostStore(Node, 2, AArch64::ST1Twov16b_POST);
3707 return;
3708 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3709 SelectPostStore(Node, 2, AArch64::ST1Twov4h_POST);
3710 return;
3711 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3712 SelectPostStore(Node, 2, AArch64::ST1Twov8h_POST);
3713 return;
3714 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3715 SelectPostStore(Node, 2, AArch64::ST1Twov2s_POST);
3716 return;
3717 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3718 SelectPostStore(Node, 2, AArch64::ST1Twov4s_POST);
3719 return;
3720 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3721 SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3722 return;
3723 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3724 SelectPostStore(Node, 2, AArch64::ST1Twov2d_POST);
3725 return;
3726 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003727 break;
3728 }
3729 case AArch64ISD::ST1x3post: {
3730 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003731 if (VT == MVT::v8i8) {
3732 SelectPostStore(Node, 3, AArch64::ST1Threev8b_POST);
3733 return;
3734 } else if (VT == MVT::v16i8) {
3735 SelectPostStore(Node, 3, AArch64::ST1Threev16b_POST);
3736 return;
3737 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3738 SelectPostStore(Node, 3, AArch64::ST1Threev4h_POST);
3739 return;
3740 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3741 SelectPostStore(Node, 3, AArch64::ST1Threev8h_POST);
3742 return;
3743 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3744 SelectPostStore(Node, 3, AArch64::ST1Threev2s_POST);
3745 return;
3746 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3747 SelectPostStore(Node, 3, AArch64::ST1Threev4s_POST);
3748 return;
3749 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3750 SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3751 return;
3752 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3753 SelectPostStore(Node, 3, AArch64::ST1Threev2d_POST);
3754 return;
3755 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003756 break;
3757 }
3758 case AArch64ISD::ST1x4post: {
3759 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003760 if (VT == MVT::v8i8) {
3761 SelectPostStore(Node, 4, AArch64::ST1Fourv8b_POST);
3762 return;
3763 } else if (VT == MVT::v16i8) {
3764 SelectPostStore(Node, 4, AArch64::ST1Fourv16b_POST);
3765 return;
3766 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3767 SelectPostStore(Node, 4, AArch64::ST1Fourv4h_POST);
3768 return;
3769 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3770 SelectPostStore(Node, 4, AArch64::ST1Fourv8h_POST);
3771 return;
3772 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3773 SelectPostStore(Node, 4, AArch64::ST1Fourv2s_POST);
3774 return;
3775 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3776 SelectPostStore(Node, 4, AArch64::ST1Fourv4s_POST);
3777 return;
3778 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3779 SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3780 return;
3781 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3782 SelectPostStore(Node, 4, AArch64::ST1Fourv2d_POST);
3783 return;
3784 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003785 break;
3786 }
3787 case AArch64ISD::ST2LANEpost: {
3788 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003789 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3790 SelectPostStoreLane(Node, 2, AArch64::ST2i8_POST);
3791 return;
3792 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3793 VT == MVT::v8f16) {
3794 SelectPostStoreLane(Node, 2, AArch64::ST2i16_POST);
3795 return;
3796 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3797 VT == MVT::v2f32) {
3798 SelectPostStoreLane(Node, 2, AArch64::ST2i32_POST);
3799 return;
3800 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3801 VT == MVT::v1f64) {
3802 SelectPostStoreLane(Node, 2, AArch64::ST2i64_POST);
3803 return;
3804 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003805 break;
3806 }
3807 case AArch64ISD::ST3LANEpost: {
3808 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003809 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3810 SelectPostStoreLane(Node, 3, AArch64::ST3i8_POST);
3811 return;
3812 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3813 VT == MVT::v8f16) {
3814 SelectPostStoreLane(Node, 3, AArch64::ST3i16_POST);
3815 return;
3816 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3817 VT == MVT::v2f32) {
3818 SelectPostStoreLane(Node, 3, AArch64::ST3i32_POST);
3819 return;
3820 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3821 VT == MVT::v1f64) {
3822 SelectPostStoreLane(Node, 3, AArch64::ST3i64_POST);
3823 return;
3824 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003825 break;
3826 }
3827 case AArch64ISD::ST4LANEpost: {
3828 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003829 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3830 SelectPostStoreLane(Node, 4, AArch64::ST4i8_POST);
3831 return;
3832 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3833 VT == MVT::v8f16) {
3834 SelectPostStoreLane(Node, 4, AArch64::ST4i16_POST);
3835 return;
3836 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3837 VT == MVT::v2f32) {
3838 SelectPostStoreLane(Node, 4, AArch64::ST4i32_POST);
3839 return;
3840 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3841 VT == MVT::v1f64) {
3842 SelectPostStoreLane(Node, 4, AArch64::ST4i64_POST);
3843 return;
3844 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003845 break;
3846 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003847 }
3848
3849 // Select the default instruction
Justin Bogner283e3bd2016-05-12 23:10:30 +00003850 SelectCode(Node);
Tim Northover3b0846e2014-05-24 12:50:23 +00003851}
3852
3853/// createAArch64ISelDag - This pass converts a legalized DAG into a
3854/// AArch64-specific DAG, ready for instruction scheduling.
3855FunctionPass *llvm::createAArch64ISelDag(AArch64TargetMachine &TM,
3856 CodeGenOpt::Level OptLevel) {
3857 return new AArch64DAGToDAGISel(TM, OptLevel);
3858}