blob: d5f03d08c0a7ca6dd91aeaa563b12778db94f1d0 [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
Chad Rosier2d658702016-06-03 15:00:09 +00001508static bool isBitfieldExtractOpFromSExtInReg(SDNode *N, unsigned &Opc,
1509 SDValue &Opd0, unsigned &Immr,
1510 unsigned &Imms) {
1511 assert(N->getOpcode() == ISD::SIGN_EXTEND_INREG);
1512
1513 EVT VT = N->getValueType(0);
1514 unsigned BitWidth = VT.getSizeInBits();
1515 assert((VT == MVT::i32 || VT == MVT::i64) &&
1516 "Type checking must have been done before calling this function");
1517
1518 SDValue Op = N->getOperand(0);
1519 if (Op->getOpcode() == ISD::TRUNCATE) {
1520 Op = Op->getOperand(0);
1521 VT = Op->getValueType(0);
1522 BitWidth = VT.getSizeInBits();
1523 }
1524
1525 uint64_t ShiftImm;
1526 if (!isOpcWithIntImmediate(Op.getNode(), ISD::SRL, ShiftImm) &&
1527 !isOpcWithIntImmediate(Op.getNode(), ISD::SRA, ShiftImm))
1528 return false;
1529
1530 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
1531 if (ShiftImm + Width > BitWidth)
1532 return false;
1533
1534 Opc = (VT == MVT::i32) ? AArch64::SBFMWri : AArch64::SBFMXri;
1535 Opd0 = Op.getOperand(0);
1536 Immr = ShiftImm;
1537 Imms = ShiftImm + Width - 1;
1538 return true;
1539}
1540
David Xu052b9d92014-09-02 09:33:56 +00001541static bool isSeveralBitsExtractOpFromShr(SDNode *N, unsigned &Opc,
1542 SDValue &Opd0, unsigned &LSB,
1543 unsigned &MSB) {
1544 // We are looking for the following pattern which basically extracts several
1545 // continuous bits from the source value and places it from the LSB of the
1546 // destination value, all other bits of the destination value or set to zero:
Tim Northover3b0846e2014-05-24 12:50:23 +00001547 //
1548 // Value2 = AND Value, MaskImm
1549 // SRL Value2, ShiftImm
1550 //
David Xu052b9d92014-09-02 09:33:56 +00001551 // with MaskImm >> ShiftImm to search for the bit width.
Tim Northover3b0846e2014-05-24 12:50:23 +00001552 //
1553 // This gets selected into a single UBFM:
1554 //
Chad Rosier7e8dd512016-05-14 18:56:28 +00001555 // UBFM Value, ShiftImm, BitWide + SrlImm -1
Tim Northover3b0846e2014-05-24 12:50:23 +00001556 //
1557
1558 if (N->getOpcode() != ISD::SRL)
1559 return false;
1560
Chad Rosier7e8dd512016-05-14 18:56:28 +00001561 uint64_t AndMask = 0;
1562 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, AndMask))
Tim Northover3b0846e2014-05-24 12:50:23 +00001563 return false;
1564
1565 Opd0 = N->getOperand(0).getOperand(0);
1566
Chad Rosier7e8dd512016-05-14 18:56:28 +00001567 uint64_t SrlImm = 0;
1568 if (!isIntImmediate(N->getOperand(1), SrlImm))
Tim Northover3b0846e2014-05-24 12:50:23 +00001569 return false;
1570
David Xu052b9d92014-09-02 09:33:56 +00001571 // Check whether we really have several bits extract here.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001572 unsigned BitWide = 64 - countLeadingOnes(~(AndMask >> SrlImm));
1573 if (BitWide && isMask_64(AndMask >> SrlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001574 if (N->getValueType(0) == MVT::i32)
1575 Opc = AArch64::UBFMWri;
1576 else
1577 Opc = AArch64::UBFMXri;
1578
Chad Rosier7e8dd512016-05-14 18:56:28 +00001579 LSB = SrlImm;
1580 MSB = BitWide + SrlImm - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001581 return true;
1582 }
1583
1584 return false;
1585}
1586
1587static bool isBitfieldExtractOpFromShr(SDNode *N, unsigned &Opc, SDValue &Opd0,
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001588 unsigned &Immr, unsigned &Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001589 bool BiggerPattern) {
1590 assert((N->getOpcode() == ISD::SRA || N->getOpcode() == ISD::SRL) &&
1591 "N must be a SHR/SRA operation to call this function");
1592
1593 EVT VT = N->getValueType(0);
1594
1595 // Here we can test the type of VT and return false when the type does not
1596 // match, but since it is done prior to that call in the current context
1597 // we turned that into an assert to avoid redundant code.
1598 assert((VT == MVT::i32 || VT == MVT::i64) &&
1599 "Type checking must have been done before calling this function");
1600
David Xu052b9d92014-09-02 09:33:56 +00001601 // Check for AND + SRL doing several bits extract.
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001602 if (isSeveralBitsExtractOpFromShr(N, Opc, Opd0, Immr, Imms))
Tim Northover3b0846e2014-05-24 12:50:23 +00001603 return true;
1604
Chad Rosierc73d5592016-05-16 12:55:01 +00001605 // We're looking for a shift of a shift.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001606 uint64_t ShlImm = 0;
1607 uint64_t TruncBits = 0;
1608 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, ShlImm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001609 Opd0 = N->getOperand(0).getOperand(0);
1610 } else if (VT == MVT::i32 && N->getOpcode() == ISD::SRL &&
1611 N->getOperand(0).getNode()->getOpcode() == ISD::TRUNCATE) {
1612 // We are looking for a shift of truncate. Truncate from i64 to i32 could
1613 // be considered as setting high 32 bits as zero. Our strategy here is to
1614 // always generate 64bit UBFM. This consistency will help the CSE pass
1615 // later find more redundancy.
1616 Opd0 = N->getOperand(0).getOperand(0);
Chad Rosier7e8dd512016-05-14 18:56:28 +00001617 TruncBits = Opd0->getValueType(0).getSizeInBits() - VT.getSizeInBits();
Tim Northover3b0846e2014-05-24 12:50:23 +00001618 VT = Opd0->getValueType(0);
1619 assert(VT == MVT::i64 && "the promoted type should be i64");
1620 } else if (BiggerPattern) {
1621 // Let's pretend a 0 shift left has been performed.
1622 // FIXME: Currently we limit this to the bigger pattern case,
1623 // because some optimizations expect AND and not UBFM
1624 Opd0 = N->getOperand(0);
1625 } else
1626 return false;
1627
Matthias Braun75260352015-02-24 18:52:04 +00001628 // Missing combines/constant folding may have left us with strange
1629 // constants.
Chad Rosier7e8dd512016-05-14 18:56:28 +00001630 if (ShlImm >= VT.getSizeInBits()) {
Matthias Braun02892ec2015-02-25 18:03:50 +00001631 DEBUG((dbgs() << N
1632 << ": Found large shift immediate, this should not happen\n"));
Matthias Braun75260352015-02-24 18:52:04 +00001633 return false;
Matthias Braun02892ec2015-02-25 18:03:50 +00001634 }
Matthias Braun75260352015-02-24 18:52:04 +00001635
Chad Rosier7e8dd512016-05-14 18:56:28 +00001636 uint64_t SrlImm = 0;
1637 if (!isIntImmediate(N->getOperand(1), SrlImm))
Tim Northover3b0846e2014-05-24 12:50:23 +00001638 return false;
1639
Chad Rosier7e8dd512016-05-14 18:56:28 +00001640 assert(SrlImm > 0 && SrlImm < VT.getSizeInBits() &&
Tim Northover3b0846e2014-05-24 12:50:23 +00001641 "bad amount in shift node!");
Chad Rosier7e8dd512016-05-14 18:56:28 +00001642 int immr = SrlImm - ShlImm;
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001643 Immr = immr < 0 ? immr + VT.getSizeInBits() : immr;
Chad Rosier7e8dd512016-05-14 18:56:28 +00001644 Imms = VT.getSizeInBits() - ShlImm - TruncBits - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001645 // SRA requires a signed extraction
1646 if (VT == MVT::i32)
1647 Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMWri : AArch64::UBFMWri;
1648 else
1649 Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMXri : AArch64::UBFMXri;
1650 return true;
1651}
1652
1653static bool isBitfieldExtractOp(SelectionDAG *CurDAG, SDNode *N, unsigned &Opc,
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001654 SDValue &Opd0, unsigned &Immr, unsigned &Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001655 unsigned NumberOfIgnoredLowBits = 0,
1656 bool BiggerPattern = false) {
1657 if (N->getValueType(0) != MVT::i32 && N->getValueType(0) != MVT::i64)
1658 return false;
1659
1660 switch (N->getOpcode()) {
1661 default:
1662 if (!N->isMachineOpcode())
1663 return false;
1664 break;
1665 case ISD::AND:
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001666 return isBitfieldExtractOpFromAnd(CurDAG, N, Opc, Opd0, Immr, Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001667 NumberOfIgnoredLowBits, BiggerPattern);
1668 case ISD::SRL:
1669 case ISD::SRA:
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001670 return isBitfieldExtractOpFromShr(N, Opc, Opd0, Immr, Imms, BiggerPattern);
Chad Rosier2d658702016-06-03 15:00:09 +00001671
1672 case ISD::SIGN_EXTEND_INREG:
1673 return isBitfieldExtractOpFromSExtInReg(N, Opc, Opd0, Immr, Imms);
Tim Northover3b0846e2014-05-24 12:50:23 +00001674 }
1675
1676 unsigned NOpc = N->getMachineOpcode();
1677 switch (NOpc) {
1678 default:
1679 return false;
1680 case AArch64::SBFMWri:
1681 case AArch64::UBFMWri:
1682 case AArch64::SBFMXri:
1683 case AArch64::UBFMXri:
1684 Opc = NOpc;
1685 Opd0 = N->getOperand(0);
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001686 Immr = cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
1687 Imms = cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
Tim Northover3b0846e2014-05-24 12:50:23 +00001688 return true;
1689 }
1690 // Unreachable
1691 return false;
1692}
1693
Justin Bogner283e3bd2016-05-12 23:10:30 +00001694bool AArch64DAGToDAGISel::tryBitfieldExtractOp(SDNode *N) {
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001695 unsigned Opc, Immr, Imms;
Tim Northover3b0846e2014-05-24 12:50:23 +00001696 SDValue Opd0;
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001697 if (!isBitfieldExtractOp(CurDAG, N, Opc, Opd0, Immr, Imms))
Justin Bogner283e3bd2016-05-12 23:10:30 +00001698 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00001699
1700 EVT VT = N->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001701 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001702
1703 // If the bit extract operation is 64bit but the original type is 32bit, we
1704 // need to add one EXTRACT_SUBREG.
1705 if ((Opc == AArch64::SBFMXri || Opc == AArch64::UBFMXri) && VT == MVT::i32) {
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001706 SDValue Ops64[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, MVT::i64),
1707 CurDAG->getTargetConstant(Imms, dl, MVT::i64)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001708
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001709 SDNode *BFM = CurDAG->getMachineNode(Opc, dl, MVT::i64, Ops64);
1710 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Justin Bogner283e3bd2016-05-12 23:10:30 +00001711 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl,
1712 MVT::i32, SDValue(BFM, 0), SubReg));
1713 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001714 }
1715
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001716 SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
1717 CurDAG->getTargetConstant(Imms, dl, VT)};
Justin Bogner283e3bd2016-05-12 23:10:30 +00001718 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
1719 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00001720}
1721
1722/// Does DstMask form a complementary pair with the mask provided by
1723/// BitsToBeInserted, suitable for use in a BFI instruction. Roughly speaking,
1724/// this asks whether DstMask zeroes precisely those bits that will be set by
1725/// the other half.
1726static bool isBitfieldDstMask(uint64_t DstMask, APInt BitsToBeInserted,
1727 unsigned NumberOfIgnoredHighBits, EVT VT) {
1728 assert((VT == MVT::i32 || VT == MVT::i64) &&
1729 "i32 or i64 mask type expected!");
1730 unsigned BitWidth = VT.getSizeInBits() - NumberOfIgnoredHighBits;
1731
1732 APInt SignificantDstMask = APInt(BitWidth, DstMask);
1733 APInt SignificantBitsToBeInserted = BitsToBeInserted.zextOrTrunc(BitWidth);
1734
1735 return (SignificantDstMask & SignificantBitsToBeInserted) == 0 &&
1736 (SignificantDstMask | SignificantBitsToBeInserted).isAllOnesValue();
1737}
1738
1739// Look for bits that will be useful for later uses.
1740// A bit is consider useless as soon as it is dropped and never used
1741// before it as been dropped.
1742// E.g., looking for useful bit of x
1743// 1. y = x & 0x7
1744// 2. z = y >> 2
1745// After #1, x useful bits are 0x7, then the useful bits of x, live through
1746// y.
1747// After #2, the useful bits of x are 0x4.
1748// However, if x is used on an unpredicatable instruction, then all its bits
1749// are useful.
1750// E.g.
1751// 1. y = x & 0x7
1752// 2. z = y >> 2
1753// 3. str x, [@x]
1754static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth = 0);
1755
1756static void getUsefulBitsFromAndWithImmediate(SDValue Op, APInt &UsefulBits,
1757 unsigned Depth) {
1758 uint64_t Imm =
1759 cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1760 Imm = AArch64_AM::decodeLogicalImmediate(Imm, UsefulBits.getBitWidth());
1761 UsefulBits &= APInt(UsefulBits.getBitWidth(), Imm);
1762 getUsefulBits(Op, UsefulBits, Depth + 1);
1763}
1764
1765static void getUsefulBitsFromBitfieldMoveOpd(SDValue Op, APInt &UsefulBits,
1766 uint64_t Imm, uint64_t MSB,
1767 unsigned Depth) {
1768 // inherit the bitwidth value
1769 APInt OpUsefulBits(UsefulBits);
1770 OpUsefulBits = 1;
1771
1772 if (MSB >= Imm) {
1773 OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1774 --OpUsefulBits;
1775 // The interesting part will be in the lower part of the result
1776 getUsefulBits(Op, OpUsefulBits, Depth + 1);
1777 // The interesting part was starting at Imm in the argument
1778 OpUsefulBits = OpUsefulBits.shl(Imm);
1779 } else {
1780 OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1781 --OpUsefulBits;
1782 // The interesting part will be shifted in the result
1783 OpUsefulBits = OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm);
1784 getUsefulBits(Op, OpUsefulBits, Depth + 1);
1785 // The interesting part was at zero in the argument
1786 OpUsefulBits = OpUsefulBits.lshr(OpUsefulBits.getBitWidth() - Imm);
1787 }
1788
1789 UsefulBits &= OpUsefulBits;
1790}
1791
1792static void getUsefulBitsFromUBFM(SDValue Op, APInt &UsefulBits,
1793 unsigned Depth) {
1794 uint64_t Imm =
1795 cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1796 uint64_t MSB =
1797 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1798
1799 getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1800}
1801
1802static void getUsefulBitsFromOrWithShiftedReg(SDValue Op, APInt &UsefulBits,
1803 unsigned Depth) {
1804 uint64_t ShiftTypeAndValue =
1805 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1806 APInt Mask(UsefulBits);
1807 Mask.clearAllBits();
1808 Mask.flipAllBits();
1809
1810 if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSL) {
1811 // Shift Left
1812 uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1813 Mask = Mask.shl(ShiftAmt);
1814 getUsefulBits(Op, Mask, Depth + 1);
1815 Mask = Mask.lshr(ShiftAmt);
1816 } else if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSR) {
1817 // Shift Right
1818 // We do not handle AArch64_AM::ASR, because the sign will change the
1819 // number of useful bits
1820 uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1821 Mask = Mask.lshr(ShiftAmt);
1822 getUsefulBits(Op, Mask, Depth + 1);
1823 Mask = Mask.shl(ShiftAmt);
1824 } else
1825 return;
1826
1827 UsefulBits &= Mask;
1828}
1829
1830static void getUsefulBitsFromBFM(SDValue Op, SDValue Orig, APInt &UsefulBits,
1831 unsigned Depth) {
1832 uint64_t Imm =
1833 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1834 uint64_t MSB =
1835 cast<const ConstantSDNode>(Op.getOperand(3).getNode())->getZExtValue();
1836
1837 if (Op.getOperand(1) == Orig)
1838 return getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1839
1840 APInt OpUsefulBits(UsefulBits);
1841 OpUsefulBits = 1;
1842
1843 if (MSB >= Imm) {
1844 OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1845 --OpUsefulBits;
1846 UsefulBits &= ~OpUsefulBits;
1847 getUsefulBits(Op, UsefulBits, Depth + 1);
1848 } else {
1849 OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1850 --OpUsefulBits;
1851 UsefulBits = ~(OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm));
1852 getUsefulBits(Op, UsefulBits, Depth + 1);
1853 }
1854}
1855
1856static void getUsefulBitsForUse(SDNode *UserNode, APInt &UsefulBits,
1857 SDValue Orig, unsigned Depth) {
1858
1859 // Users of this node should have already been instruction selected
1860 // FIXME: Can we turn that into an assert?
1861 if (!UserNode->isMachineOpcode())
1862 return;
1863
1864 switch (UserNode->getMachineOpcode()) {
1865 default:
1866 return;
1867 case AArch64::ANDSWri:
1868 case AArch64::ANDSXri:
1869 case AArch64::ANDWri:
1870 case AArch64::ANDXri:
1871 // We increment Depth only when we call the getUsefulBits
1872 return getUsefulBitsFromAndWithImmediate(SDValue(UserNode, 0), UsefulBits,
1873 Depth);
1874 case AArch64::UBFMWri:
1875 case AArch64::UBFMXri:
1876 return getUsefulBitsFromUBFM(SDValue(UserNode, 0), UsefulBits, Depth);
1877
1878 case AArch64::ORRWrs:
1879 case AArch64::ORRXrs:
1880 if (UserNode->getOperand(1) != Orig)
1881 return;
1882 return getUsefulBitsFromOrWithShiftedReg(SDValue(UserNode, 0), UsefulBits,
1883 Depth);
1884 case AArch64::BFMWri:
1885 case AArch64::BFMXri:
1886 return getUsefulBitsFromBFM(SDValue(UserNode, 0), Orig, UsefulBits, Depth);
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001887
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001888 case AArch64::STRBBui:
Chad Rosier9926a5e2016-05-12 01:42:01 +00001889 case AArch64::STURBBi:
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001890 if (UserNode->getOperand(0) != Orig)
1891 return;
1892 UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xff);
1893 return;
1894
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001895 case AArch64::STRHHui:
Chad Rosier9926a5e2016-05-12 01:42:01 +00001896 case AArch64::STURHHi:
Chad Rosier23a1a9a2016-05-11 20:19:54 +00001897 if (UserNode->getOperand(0) != Orig)
1898 return;
1899 UsefulBits &= APInt(UsefulBits.getBitWidth(), 0xffff);
1900 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00001901 }
1902}
1903
1904static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth) {
1905 if (Depth >= 6)
1906 return;
1907 // Initialize UsefulBits
1908 if (!Depth) {
1909 unsigned Bitwidth = Op.getValueType().getScalarType().getSizeInBits();
1910 // At the beginning, assume every produced bits is useful
1911 UsefulBits = APInt(Bitwidth, 0);
1912 UsefulBits.flipAllBits();
1913 }
1914 APInt UsersUsefulBits(UsefulBits.getBitWidth(), 0);
1915
1916 for (SDNode *Node : Op.getNode()->uses()) {
1917 // A use cannot produce useful bits
1918 APInt UsefulBitsForUse = APInt(UsefulBits);
1919 getUsefulBitsForUse(Node, UsefulBitsForUse, Op, Depth);
1920 UsersUsefulBits |= UsefulBitsForUse;
1921 }
1922 // UsefulBits contains the produced bits that are meaningful for the
1923 // current definition, thus a user cannot make a bit meaningful at
1924 // this point
1925 UsefulBits &= UsersUsefulBits;
1926}
1927
1928/// Create a machine node performing a notional SHL of Op by ShlAmount. If
1929/// ShlAmount is negative, do a (logical) right-shift instead. If ShlAmount is
1930/// 0, return Op unchanged.
1931static SDValue getLeftShift(SelectionDAG *CurDAG, SDValue Op, int ShlAmount) {
1932 if (ShlAmount == 0)
1933 return Op;
1934
1935 EVT VT = Op.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001936 SDLoc dl(Op);
Tim Northover3b0846e2014-05-24 12:50:23 +00001937 unsigned BitWidth = VT.getSizeInBits();
1938 unsigned UBFMOpc = BitWidth == 32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1939
1940 SDNode *ShiftNode;
1941 if (ShlAmount > 0) {
1942 // LSL wD, wN, #Amt == UBFM wD, wN, #32-Amt, #31-Amt
1943 ShiftNode = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001944 UBFMOpc, dl, VT, Op,
1945 CurDAG->getTargetConstant(BitWidth - ShlAmount, dl, VT),
1946 CurDAG->getTargetConstant(BitWidth - 1 - ShlAmount, dl, VT));
Tim Northover3b0846e2014-05-24 12:50:23 +00001947 } else {
1948 // LSR wD, wN, #Amt == UBFM wD, wN, #Amt, #32-1
1949 assert(ShlAmount < 0 && "expected right shift");
1950 int ShrAmount = -ShlAmount;
1951 ShiftNode = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001952 UBFMOpc, dl, VT, Op, CurDAG->getTargetConstant(ShrAmount, dl, VT),
1953 CurDAG->getTargetConstant(BitWidth - 1, dl, VT));
Tim Northover3b0846e2014-05-24 12:50:23 +00001954 }
1955
1956 return SDValue(ShiftNode, 0);
1957}
1958
1959/// Does this tree qualify as an attempt to move a bitfield into position,
1960/// essentially "(and (shl VAL, N), Mask)".
1961static bool isBitfieldPositioningOp(SelectionDAG *CurDAG, SDValue Op,
Geoff Berry43ec15e2015-09-18 17:11:53 +00001962 bool BiggerPattern,
Tim Northover3b0846e2014-05-24 12:50:23 +00001963 SDValue &Src, int &ShiftAmount,
1964 int &MaskWidth) {
1965 EVT VT = Op.getValueType();
1966 unsigned BitWidth = VT.getSizeInBits();
1967 (void)BitWidth;
1968 assert(BitWidth == 32 || BitWidth == 64);
1969
1970 APInt KnownZero, KnownOne;
1971 CurDAG->computeKnownBits(Op, KnownZero, KnownOne);
1972
1973 // Non-zero in the sense that they're not provably zero, which is the key
1974 // point if we want to use this value
1975 uint64_t NonZeroBits = (~KnownZero).getZExtValue();
1976
1977 // Discard a constant AND mask if present. It's safe because the node will
1978 // already have been factored into the computeKnownBits calculation above.
1979 uint64_t AndImm;
1980 if (isOpcWithIntImmediate(Op.getNode(), ISD::AND, AndImm)) {
1981 assert((~APInt(BitWidth, AndImm) & ~KnownZero) == 0);
1982 Op = Op.getOperand(0);
1983 }
1984
Geoff Berry43ec15e2015-09-18 17:11:53 +00001985 // Don't match if the SHL has more than one use, since then we'll end up
1986 // generating SHL+UBFIZ instead of just keeping SHL+AND.
1987 if (!BiggerPattern && !Op.hasOneUse())
1988 return false;
1989
Tim Northover3b0846e2014-05-24 12:50:23 +00001990 uint64_t ShlImm;
1991 if (!isOpcWithIntImmediate(Op.getNode(), ISD::SHL, ShlImm))
1992 return false;
1993 Op = Op.getOperand(0);
1994
1995 if (!isShiftedMask_64(NonZeroBits))
1996 return false;
1997
1998 ShiftAmount = countTrailingZeros(NonZeroBits);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00001999 MaskWidth = countTrailingOnes(NonZeroBits >> ShiftAmount);
Tim Northover3b0846e2014-05-24 12:50:23 +00002000
2001 // BFI encompasses sufficiently many nodes that it's worth inserting an extra
2002 // LSL/LSR if the mask in NonZeroBits doesn't quite match up with the ISD::SHL
Geoff Berry43ec15e2015-09-18 17:11:53 +00002003 // amount. BiggerPattern is true when this pattern is being matched for BFI,
2004 // BiggerPattern is false when this pattern is being matched for UBFIZ, in
2005 // which case it is not profitable to insert an extra shift.
2006 if (ShlImm - ShiftAmount != 0 && !BiggerPattern)
2007 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002008 Src = getLeftShift(CurDAG, Op, ShlImm - ShiftAmount);
2009
2010 return true;
2011}
2012
Chad Rosier02f25a92016-05-19 14:19:47 +00002013static bool isShiftedMask(uint64_t Mask, EVT VT) {
2014 assert(VT == MVT::i32 || VT == MVT::i64);
2015 if (VT == MVT::i32)
2016 return isShiftedMask_32(Mask);
2017 return isShiftedMask_64(Mask);
2018}
2019
Chad Rosier816a67d2016-05-26 13:27:56 +00002020// Generate a BFI/BFXIL from 'or (and X, MaskImm), OrImm' iff the value being
2021// inserted only sets known zero bits.
2022static bool tryBitfieldInsertOpFromOrAndImm(SDNode *N, SelectionDAG *CurDAG) {
2023 assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
2024
2025 EVT VT = N->getValueType(0);
2026 if (VT != MVT::i32 && VT != MVT::i64)
2027 return false;
2028
2029 unsigned BitWidth = VT.getSizeInBits();
2030
2031 uint64_t OrImm;
2032 if (!isOpcWithIntImmediate(N, ISD::OR, OrImm))
2033 return false;
2034
2035 // Skip this transformation if the ORR immediate can be encoded in the ORR.
2036 // Otherwise, we'll trade an AND+ORR for ORR+BFI/BFXIL, which is most likely
2037 // performance neutral.
2038 if (AArch64_AM::isLogicalImmediate(OrImm, BitWidth))
2039 return false;
2040
2041 uint64_t MaskImm;
2042 SDValue And = N->getOperand(0);
2043 // Must be a single use AND with an immediate operand.
2044 if (!And.hasOneUse() ||
2045 !isOpcWithIntImmediate(And.getNode(), ISD::AND, MaskImm))
2046 return false;
2047
2048 // Compute the Known Zero for the AND as this allows us to catch more general
2049 // cases than just looking for AND with imm.
2050 APInt KnownZero, KnownOne;
2051 CurDAG->computeKnownBits(And, KnownZero, KnownOne);
2052
2053 // Non-zero in the sense that they're not provably zero, which is the key
2054 // point if we want to use this value.
2055 uint64_t NotKnownZero = (~KnownZero).getZExtValue();
2056
2057 // The KnownZero mask must be a shifted mask (e.g., 1110..011, 11100..00).
2058 if (!isShiftedMask(KnownZero.getZExtValue(), VT))
2059 return false;
2060
2061 // The bits being inserted must only set those bits that are known to be zero.
2062 if ((OrImm & NotKnownZero) != 0) {
2063 // FIXME: It's okay if the OrImm sets NotKnownZero bits to 1, but we don't
2064 // currently handle this case.
2065 return false;
2066 }
2067
2068 // BFI/BFXIL dst, src, #lsb, #width.
2069 int LSB = countTrailingOnes(NotKnownZero);
2070 int Width = BitWidth - APInt(BitWidth, NotKnownZero).countPopulation();
2071
2072 // BFI/BFXIL is an alias of BFM, so translate to BFM operands.
2073 unsigned ImmR = (BitWidth - LSB) % BitWidth;
2074 unsigned ImmS = Width - 1;
2075
2076 // If we're creating a BFI instruction avoid cases where we need more
2077 // instructions to materialize the BFI constant as compared to the original
2078 // ORR. A BFXIL will use the same constant as the original ORR, so the code
2079 // should be no worse in this case.
2080 bool IsBFI = LSB != 0;
2081 uint64_t BFIImm = OrImm >> LSB;
2082 if (IsBFI && !AArch64_AM::isLogicalImmediate(BFIImm, BitWidth)) {
2083 // We have a BFI instruction and we know the constant can't be materialized
2084 // with a ORR-immediate with the zero register.
2085 unsigned OrChunks = 0, BFIChunks = 0;
2086 for (unsigned Shift = 0; Shift < BitWidth; Shift += 16) {
2087 if (((OrImm >> Shift) & 0xFFFF) != 0)
2088 ++OrChunks;
2089 if (((BFIImm >> Shift) & 0xFFFF) != 0)
2090 ++BFIChunks;
2091 }
2092 if (BFIChunks > OrChunks)
2093 return false;
2094 }
2095
2096 // Materialize the constant to be inserted.
2097 SDLoc DL(N);
2098 unsigned MOVIOpc = VT == MVT::i32 ? AArch64::MOVi32imm : AArch64::MOVi64imm;
2099 SDNode *MOVI = CurDAG->getMachineNode(
2100 MOVIOpc, DL, VT, CurDAG->getTargetConstant(BFIImm, DL, VT));
2101
2102 // Create the BFI/BFXIL instruction.
2103 SDValue Ops[] = {And.getOperand(0), SDValue(MOVI, 0),
2104 CurDAG->getTargetConstant(ImmR, DL, VT),
2105 CurDAG->getTargetConstant(ImmS, DL, VT)};
2106 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2107 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2108 return true;
2109}
2110
Justin Bogner283e3bd2016-05-12 23:10:30 +00002111static bool tryBitfieldInsertOpFromOr(SDNode *N, const APInt &UsefulBits,
2112 SelectionDAG *CurDAG) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002113 assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
2114
Tim Northover3b0846e2014-05-24 12:50:23 +00002115 EVT VT = N->getValueType(0);
Chad Rosier042ac2c2016-05-12 19:38:18 +00002116 if (VT != MVT::i32 && VT != MVT::i64)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002117 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002118
Chad Rosier91294c52016-05-18 17:43:11 +00002119 unsigned BitWidth = VT.getSizeInBits();
2120
Tim Northover3b0846e2014-05-24 12:50:23 +00002121 // Because of simplify-demanded-bits in DAGCombine, involved masks may not
2122 // have the expected shape. Try to undo that.
Tim Northover3b0846e2014-05-24 12:50:23 +00002123
2124 unsigned NumberOfIgnoredLowBits = UsefulBits.countTrailingZeros();
2125 unsigned NumberOfIgnoredHighBits = UsefulBits.countLeadingZeros();
2126
Chad Rosiere0062022016-05-18 23:51:17 +00002127 // Given a OR operation, check if we have the following pattern
2128 // ubfm c, b, imm, imm2 (or something that does the same jobs, see
2129 // isBitfieldExtractOp)
2130 // d = e & mask2 ; where mask is a binary sequence of 1..10..0 and
2131 // countTrailingZeros(mask2) == imm2 - imm + 1
2132 // f = d | c
2133 // if yes, replace the OR instruction with:
2134 // f = BFM Opd0, Opd1, LSB, MSB ; where LSB = imm, and MSB = imm2
2135
Geoff Berry43ec15e2015-09-18 17:11:53 +00002136 // OR is commutative, check all combinations of operand order and values of
2137 // BiggerPattern, i.e.
2138 // Opd0, Opd1, BiggerPattern=false
2139 // Opd1, Opd0, BiggerPattern=false
2140 // Opd0, Opd1, BiggerPattern=true
2141 // Opd1, Opd0, BiggerPattern=true
2142 // Several of these combinations may match, so check with BiggerPattern=false
2143 // first since that will produce better results by matching more instructions
2144 // and/or inserting fewer extra instructions.
2145 for (int I = 0; I < 4; ++I) {
2146
Chad Rosier91294c52016-05-18 17:43:11 +00002147 SDValue Dst, Src;
2148 unsigned ImmR, ImmS;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002149 bool BiggerPattern = I / 2;
2150 SDNode *OrOpd0 = N->getOperand(I % 2).getNode();
2151 SDValue OrOpd1Val = N->getOperand((I + 1) % 2);
2152 SDNode *OrOpd1 = OrOpd1Val.getNode();
2153
Tim Northover3b0846e2014-05-24 12:50:23 +00002154 unsigned BFXOpc;
2155 int DstLSB, Width;
2156 if (isBitfieldExtractOp(CurDAG, OrOpd0, BFXOpc, Src, ImmR, ImmS,
Geoff Berry43ec15e2015-09-18 17:11:53 +00002157 NumberOfIgnoredLowBits, BiggerPattern)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002158 // Check that the returned opcode is compatible with the pattern,
2159 // i.e., same type and zero extended (U and not S)
2160 if ((BFXOpc != AArch64::UBFMXri && VT == MVT::i64) ||
2161 (BFXOpc != AArch64::UBFMWri && VT == MVT::i32))
2162 continue;
2163
2164 // Compute the width of the bitfield insertion
2165 DstLSB = 0;
2166 Width = ImmS - ImmR + 1;
2167 // FIXME: This constraint is to catch bitfield insertion we may
2168 // want to widen the pattern if we want to grab general bitfied
2169 // move case
2170 if (Width <= 0)
2171 continue;
2172
2173 // If the mask on the insertee is correct, we have a BFXIL operation. We
2174 // can share the ImmR and ImmS values from the already-computed UBFM.
Geoff Berry43ec15e2015-09-18 17:11:53 +00002175 } else if (isBitfieldPositioningOp(CurDAG, SDValue(OrOpd0, 0),
2176 BiggerPattern,
2177 Src, DstLSB, Width)) {
Chad Rosier91294c52016-05-18 17:43:11 +00002178 ImmR = (BitWidth - DstLSB) % BitWidth;
Tim Northover3b0846e2014-05-24 12:50:23 +00002179 ImmS = Width - 1;
2180 } else
2181 continue;
2182
2183 // Check the second part of the pattern
2184 EVT VT = OrOpd1->getValueType(0);
2185 assert((VT == MVT::i32 || VT == MVT::i64) && "unexpected OR operand");
2186
2187 // Compute the Known Zero for the candidate of the first operand.
2188 // This allows to catch more general case than just looking for
2189 // AND with imm. Indeed, simplify-demanded-bits may have removed
2190 // the AND instruction because it proves it was useless.
2191 APInt KnownZero, KnownOne;
2192 CurDAG->computeKnownBits(OrOpd1Val, KnownZero, KnownOne);
2193
2194 // Check if there is enough room for the second operand to appear
2195 // in the first one
2196 APInt BitsToBeInserted =
2197 APInt::getBitsSet(KnownZero.getBitWidth(), DstLSB, DstLSB + Width);
2198
2199 if ((BitsToBeInserted & ~KnownZero) != 0)
2200 continue;
2201
2202 // Set the first operand
2203 uint64_t Imm;
2204 if (isOpcWithIntImmediate(OrOpd1, ISD::AND, Imm) &&
2205 isBitfieldDstMask(Imm, BitsToBeInserted, NumberOfIgnoredHighBits, VT))
2206 // In that case, we can eliminate the AND
2207 Dst = OrOpd1->getOperand(0);
2208 else
2209 // Maybe the AND has been removed by simplify-demanded-bits
2210 // or is useful because it discards more bits
2211 Dst = OrOpd1Val;
2212
2213 // both parts match
Chad Rosier042ac2c2016-05-12 19:38:18 +00002214 SDLoc DL(N);
2215 SDValue Ops[] = {Dst, Src, CurDAG->getTargetConstant(ImmR, DL, VT),
2216 CurDAG->getTargetConstant(ImmS, DL, VT)};
2217 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
Justin Bogner283e3bd2016-05-12 23:10:30 +00002218 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2219 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +00002220 }
Chad Rosier02f25a92016-05-19 14:19:47 +00002221
2222 // Generate a BFXIL from 'or (and X, Mask0Imm), (and Y, Mask1Imm)' iff
2223 // Mask0Imm and ~Mask1Imm are equivalent and one of the MaskImms is a shifted
2224 // mask (e.g., 0x000ffff0).
2225 uint64_t Mask0Imm, Mask1Imm;
2226 SDValue And0 = N->getOperand(0);
2227 SDValue And1 = N->getOperand(1);
2228 if (And0.hasOneUse() && And1.hasOneUse() &&
2229 isOpcWithIntImmediate(And0.getNode(), ISD::AND, Mask0Imm) &&
2230 isOpcWithIntImmediate(And1.getNode(), ISD::AND, Mask1Imm) &&
2231 APInt(BitWidth, Mask0Imm) == ~APInt(BitWidth, Mask1Imm) &&
2232 (isShiftedMask(Mask0Imm, VT) || isShiftedMask(Mask1Imm, VT))) {
2233
2234 // We should have already caught the case where we extract hi and low parts.
2235 // E.g. BFXIL from 'or (and X, 0xffff0000), (and Y, 0x0000ffff)'.
2236 assert(!(isShiftedMask(Mask0Imm, VT) && isShiftedMask(Mask1Imm, VT)) &&
2237 "BFXIL should have already been optimized.");
2238
2239 // ORR is commutative, so canonicalize to the form 'or (and X, Mask0Imm),
2240 // (and Y, Mask1Imm)' where Mask1Imm is the shifted mask masking off the
2241 // bits to be inserted.
2242 if (isShiftedMask(Mask0Imm, VT)) {
2243 std::swap(And0, And1);
2244 std::swap(Mask0Imm, Mask1Imm);
2245 }
2246
2247 SDValue Src = And1->getOperand(0);
2248 SDValue Dst = And0->getOperand(0);
2249 unsigned LSB = countTrailingZeros(Mask1Imm);
2250 int Width = BitWidth - APInt(BitWidth, Mask0Imm).countPopulation();
2251
2252 // The BFXIL inserts the low-order bits from a source register, so right
2253 // shift the needed bits into place.
2254 SDLoc DL(N);
2255 unsigned ShiftOpc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
2256 SDNode *LSR = CurDAG->getMachineNode(
2257 ShiftOpc, DL, VT, Src, CurDAG->getTargetConstant(LSB, DL, VT),
2258 CurDAG->getTargetConstant(BitWidth - 1, DL, VT));
2259
2260 // BFXIL is an alias of BFM, so translate to BFM operands.
2261 unsigned ImmR = (BitWidth - LSB) % BitWidth;
2262 unsigned ImmS = Width - 1;
2263
2264 // Create the BFXIL instruction.
2265 SDValue Ops[] = {Dst, SDValue(LSR, 0),
2266 CurDAG->getTargetConstant(ImmR, DL, VT),
2267 CurDAG->getTargetConstant(ImmS, DL, VT)};
2268 unsigned Opc = (VT == MVT::i32) ? AArch64::BFMWri : AArch64::BFMXri;
2269 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2270 return true;
2271 }
2272
Justin Bogner283e3bd2016-05-12 23:10:30 +00002273 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002274}
2275
Justin Bogner283e3bd2016-05-12 23:10:30 +00002276bool AArch64DAGToDAGISel::tryBitfieldInsertOp(SDNode *N) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002277 if (N->getOpcode() != ISD::OR)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002278 return false;
Tim Northover3b0846e2014-05-24 12:50:23 +00002279
Weiming Zhao56ab5182015-12-01 19:17:49 +00002280 APInt NUsefulBits;
2281 getUsefulBits(SDValue(N, 0), NUsefulBits);
Tim Northover3b0846e2014-05-24 12:50:23 +00002282
Weiming Zhao56ab5182015-12-01 19:17:49 +00002283 // If all bits are not useful, just return UNDEF.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002284 if (!NUsefulBits) {
2285 CurDAG->SelectNodeTo(N, TargetOpcode::IMPLICIT_DEF, N->getValueType(0));
2286 return true;
2287 }
Weiming Zhao56ab5182015-12-01 19:17:49 +00002288
Chad Rosier816a67d2016-05-26 13:27:56 +00002289 if (tryBitfieldInsertOpFromOr(N, NUsefulBits, CurDAG))
2290 return true;
2291
2292 return tryBitfieldInsertOpFromOrAndImm(N, CurDAG);
Tim Northover3b0846e2014-05-24 12:50:23 +00002293}
2294
Geoff Berry43ec15e2015-09-18 17:11:53 +00002295/// SelectBitfieldInsertInZeroOp - Match a UBFIZ instruction that is the
2296/// equivalent of a left shift by a constant amount followed by an and masking
2297/// out a contiguous set of bits.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002298bool AArch64DAGToDAGISel::tryBitfieldInsertInZeroOp(SDNode *N) {
Geoff Berry43ec15e2015-09-18 17:11:53 +00002299 if (N->getOpcode() != ISD::AND)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002300 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002301
2302 EVT VT = N->getValueType(0);
Chad Rosier08d99082016-05-13 22:53:13 +00002303 if (VT != MVT::i32 && VT != MVT::i64)
Justin Bogner283e3bd2016-05-12 23:10:30 +00002304 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002305
2306 SDValue Op0;
2307 int DstLSB, Width;
2308 if (!isBitfieldPositioningOp(CurDAG, SDValue(N, 0), /*BiggerPattern=*/false,
2309 Op0, DstLSB, Width))
Justin Bogner283e3bd2016-05-12 23:10:30 +00002310 return false;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002311
2312 // ImmR is the rotate right amount.
2313 unsigned ImmR = (VT.getSizeInBits() - DstLSB) % VT.getSizeInBits();
2314 // ImmS is the most significant bit of the source to be moved.
2315 unsigned ImmS = Width - 1;
2316
2317 SDLoc DL(N);
2318 SDValue Ops[] = {Op0, CurDAG->getTargetConstant(ImmR, DL, VT),
2319 CurDAG->getTargetConstant(ImmS, DL, VT)};
Chad Rosier08d99082016-05-13 22:53:13 +00002320 unsigned Opc = (VT == MVT::i32) ? AArch64::UBFMWri : AArch64::UBFMXri;
Justin Bogner283e3bd2016-05-12 23:10:30 +00002321 CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2322 return true;
Geoff Berry43ec15e2015-09-18 17:11:53 +00002323}
2324
Tim Northover3b0846e2014-05-24 12:50:23 +00002325bool
2326AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
2327 unsigned RegWidth) {
2328 APFloat FVal(0.0);
2329 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
2330 FVal = CN->getValueAPF();
2331 else if (LoadSDNode *LN = dyn_cast<LoadSDNode>(N)) {
2332 // Some otherwise illegal constants are allowed in this case.
2333 if (LN->getOperand(1).getOpcode() != AArch64ISD::ADDlow ||
2334 !isa<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1)))
2335 return false;
2336
2337 ConstantPoolSDNode *CN =
2338 dyn_cast<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1));
2339 FVal = cast<ConstantFP>(CN->getConstVal())->getValueAPF();
2340 } else
2341 return false;
2342
2343 // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
2344 // is between 1 and 32 for a destination w-register, or 1 and 64 for an
2345 // x-register.
2346 //
2347 // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
2348 // want THIS_NODE to be 2^fbits. This is much easier to deal with using
2349 // integers.
2350 bool IsExact;
2351
2352 // fbits is between 1 and 64 in the worst-case, which means the fmul
2353 // could have 2^64 as an actual operand. Need 65 bits of precision.
2354 APSInt IntVal(65, true);
2355 FVal.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
2356
2357 // N.b. isPowerOf2 also checks for > 0.
2358 if (!IsExact || !IntVal.isPowerOf2()) return false;
2359 unsigned FBits = IntVal.logBase2();
2360
2361 // Checks above should have guaranteed that we haven't lost information in
2362 // finding FBits, but it must still be in range.
2363 if (FBits == 0 || FBits > RegWidth) return false;
2364
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002365 FixedPos = CurDAG->getTargetConstant(FBits, SDLoc(N), MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +00002366 return true;
2367}
2368
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002369// Inspects a register string of the form o0:op1:CRn:CRm:op2 gets the fields
2370// of the string and obtains the integer values from them and combines these
2371// into a single value to be used in the MRS/MSR instruction.
2372static int getIntOperandFromRegisterString(StringRef RegString) {
2373 SmallVector<StringRef, 5> Fields;
Chandler Carruthe4405e92015-09-10 06:12:31 +00002374 RegString.split(Fields, ':');
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002375
2376 if (Fields.size() == 1)
2377 return -1;
2378
2379 assert(Fields.size() == 5
2380 && "Invalid number of fields in read register string");
2381
2382 SmallVector<int, 5> Ops;
2383 bool AllIntFields = true;
2384
2385 for (StringRef Field : Fields) {
2386 unsigned IntField;
2387 AllIntFields &= !Field.getAsInteger(10, IntField);
2388 Ops.push_back(IntField);
2389 }
2390
2391 assert(AllIntFields &&
2392 "Unexpected non-integer value in special register string.");
2393
2394 // Need to combine the integer fields of the string into a single value
2395 // based on the bit encoding of MRS/MSR instruction.
2396 return (Ops[0] << 14) | (Ops[1] << 11) | (Ops[2] << 7) |
2397 (Ops[3] << 3) | (Ops[4]);
2398}
2399
2400// Lower the read_register intrinsic to an MRS 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 MRS SysReg mapper.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002404bool AArch64DAGToDAGISel::tryReadRegister(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(N, CurDAG->getMachineNode(
2412 AArch64::MRS, DL, N->getSimpleValueType(0), MVT::Other,
2413 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2414 N->getOperand(0)));
2415 return true;
2416 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002417
2418 // Use the sysreg mapper to map the remaining possible strings to the
2419 // value for the register to be used for the instruction operand.
2420 AArch64SysReg::MRSMapper mapper;
2421 bool IsValidSpecialReg;
2422 Reg = mapper.fromString(RegString->getString(),
2423 Subtarget->getFeatureBits(),
2424 IsValidSpecialReg);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002425 if (IsValidSpecialReg) {
2426 ReplaceNode(N, CurDAG->getMachineNode(
2427 AArch64::MRS, DL, N->getSimpleValueType(0), MVT::Other,
2428 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2429 N->getOperand(0)));
2430 return true;
2431 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002432
Justin Bogner283e3bd2016-05-12 23:10:30 +00002433 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002434}
2435
2436// Lower the write_register intrinsic to an MSR instruction node if the special
2437// register string argument is either of the form detailed in the ALCE (the
2438// form described in getIntOperandsFromRegsterString) or is a named register
2439// known by the MSR SysReg mapper.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002440bool AArch64DAGToDAGISel::tryWriteRegister(SDNode *N) {
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002441 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2442 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2443 SDLoc DL(N);
2444
2445 int Reg = getIntOperandFromRegisterString(RegString->getString());
Justin Bogner283e3bd2016-05-12 23:10:30 +00002446 if (Reg != -1) {
2447 ReplaceNode(
2448 N, CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002449 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
Justin Bogner283e3bd2016-05-12 23:10:30 +00002450 N->getOperand(2), N->getOperand(0)));
2451 return true;
2452 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002453
2454 // Check if the register was one of those allowed as the pstatefield value in
2455 // the MSR (immediate) instruction. To accept the values allowed in the
2456 // pstatefield for the MSR (immediate) instruction, we also require that an
2457 // immediate value has been provided as an argument, we know that this is
2458 // the case as it has been ensured by semantic checking.
2459 AArch64PState::PStateMapper PMapper;
2460 bool IsValidSpecialReg;
2461 Reg = PMapper.fromString(RegString->getString(),
2462 Subtarget->getFeatureBits(),
2463 IsValidSpecialReg);
2464 if (IsValidSpecialReg) {
2465 assert (isa<ConstantSDNode>(N->getOperand(2))
2466 && "Expected a constant integer expression.");
2467 uint64_t Immed = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
Alexandros Lamprineas1bab1912015-10-05 13:42:31 +00002468 unsigned State;
Oliver Stannard911ea202015-11-26 15:32:30 +00002469 if (Reg == AArch64PState::PAN || Reg == AArch64PState::UAO) {
Alexandros Lamprineas1bab1912015-10-05 13:42:31 +00002470 assert(Immed < 2 && "Bad imm");
2471 State = AArch64::MSRpstateImm1;
2472 } else {
2473 assert(Immed < 16 && "Bad imm");
2474 State = AArch64::MSRpstateImm4;
2475 }
Justin Bogner283e3bd2016-05-12 23:10:30 +00002476 ReplaceNode(N, CurDAG->getMachineNode(
2477 State, DL, MVT::Other,
2478 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2479 CurDAG->getTargetConstant(Immed, DL, MVT::i16),
2480 N->getOperand(0)));
2481 return true;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002482 }
2483
2484 // Use the sysreg mapper to attempt to map the remaining possible strings
2485 // to the value for the register to be used for the MSR (register)
2486 // instruction operand.
2487 AArch64SysReg::MSRMapper Mapper;
2488 Reg = Mapper.fromString(RegString->getString(),
2489 Subtarget->getFeatureBits(),
2490 IsValidSpecialReg);
2491
Justin Bogner283e3bd2016-05-12 23:10:30 +00002492 if (IsValidSpecialReg) {
2493 ReplaceNode(
2494 N, CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002495 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
Justin Bogner283e3bd2016-05-12 23:10:30 +00002496 N->getOperand(2), N->getOperand(0)));
2497 return true;
2498 }
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002499
Justin Bogner283e3bd2016-05-12 23:10:30 +00002500 return false;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002501}
2502
Tim Northovercdf15292016-04-14 17:03:29 +00002503/// We've got special pseudo-instructions for these
2504void AArch64DAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
2505 unsigned Opcode;
2506 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
2507 if (MemTy == MVT::i8)
2508 Opcode = AArch64::CMP_SWAP_8;
2509 else if (MemTy == MVT::i16)
2510 Opcode = AArch64::CMP_SWAP_16;
2511 else if (MemTy == MVT::i32)
2512 Opcode = AArch64::CMP_SWAP_32;
2513 else if (MemTy == MVT::i64)
2514 Opcode = AArch64::CMP_SWAP_64;
2515 else
2516 llvm_unreachable("Unknown AtomicCmpSwap type");
2517
2518 MVT RegTy = MemTy == MVT::i64 ? MVT::i64 : MVT::i32;
2519 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
2520 N->getOperand(0)};
2521 SDNode *CmpSwap = CurDAG->getMachineNode(
2522 Opcode, SDLoc(N),
2523 CurDAG->getVTList(RegTy, MVT::i32, MVT::Other), Ops);
2524
2525 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2526 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2527 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
2528
2529 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
2530 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
Justin Bogner3525da72016-05-12 20:54:27 +00002531 CurDAG->RemoveDeadNode(N);
Tim Northovercdf15292016-04-14 17:03:29 +00002532}
2533
Justin Bogner283e3bd2016-05-12 23:10:30 +00002534void AArch64DAGToDAGISel::Select(SDNode *Node) {
Tim Northover3b0846e2014-05-24 12:50:23 +00002535 // Dump information about the Node being selected
2536 DEBUG(errs() << "Selecting: ");
2537 DEBUG(Node->dump(CurDAG));
2538 DEBUG(errs() << "\n");
2539
2540 // If we have a custom node, we already have selected!
2541 if (Node->isMachineOpcode()) {
2542 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
2543 Node->setNodeId(-1);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002544 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002545 }
2546
2547 // Few custom selection stuff.
Tim Northover3b0846e2014-05-24 12:50:23 +00002548 EVT VT = Node->getValueType(0);
2549
2550 switch (Node->getOpcode()) {
2551 default:
2552 break;
2553
Tim Northovercdf15292016-04-14 17:03:29 +00002554 case ISD::ATOMIC_CMP_SWAP:
2555 SelectCMP_SWAP(Node);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002556 return;
Tim Northovercdf15292016-04-14 17:03:29 +00002557
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002558 case ISD::READ_REGISTER:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002559 if (tryReadRegister(Node))
2560 return;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002561 break;
2562
2563 case ISD::WRITE_REGISTER:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002564 if (tryWriteRegister(Node))
2565 return;
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002566 break;
2567
Tim Northover3b0846e2014-05-24 12:50:23 +00002568 case ISD::ADD:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002569 if (tryMLAV64LaneV128(Node))
2570 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002571 break;
2572
2573 case ISD::LOAD: {
2574 // Try to select as an indexed load. Fall through to normal processing
2575 // if we can't.
Justin Bogner283e3bd2016-05-12 23:10:30 +00002576 if (tryIndexedLoad(Node))
2577 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002578 break;
2579 }
2580
2581 case ISD::SRL:
2582 case ISD::AND:
2583 case ISD::SRA:
Chad Rosier2d658702016-06-03 15:00:09 +00002584 case ISD::SIGN_EXTEND_INREG:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002585 if (tryBitfieldExtractOp(Node))
2586 return;
2587 if (tryBitfieldInsertInZeroOp(Node))
2588 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002589 break;
2590
2591 case ISD::OR:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002592 if (tryBitfieldInsertOp(Node))
2593 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002594 break;
2595
2596 case ISD::EXTRACT_VECTOR_ELT: {
2597 // Extracting lane zero is a special case where we can just use a plain
2598 // EXTRACT_SUBREG instruction, which will become FMOV. This is easier for
2599 // the rest of the compiler, especially the register allocator and copyi
2600 // propagation, to reason about, so is preferred when it's possible to
2601 // use it.
2602 ConstantSDNode *LaneNode = cast<ConstantSDNode>(Node->getOperand(1));
2603 // Bail and use the default Select() for non-zero lanes.
2604 if (LaneNode->getZExtValue() != 0)
2605 break;
2606 // If the element type is not the same as the result type, likewise
2607 // bail and use the default Select(), as there's more to do than just
2608 // a cross-class COPY. This catches extracts of i8 and i16 elements
2609 // since they will need an explicit zext.
2610 if (VT != Node->getOperand(0).getValueType().getVectorElementType())
2611 break;
2612 unsigned SubReg;
2613 switch (Node->getOperand(0)
2614 .getValueType()
2615 .getVectorElementType()
2616 .getSizeInBits()) {
2617 default:
Craig Topper2a30d782014-06-18 05:05:13 +00002618 llvm_unreachable("Unexpected vector element type!");
Tim Northover3b0846e2014-05-24 12:50:23 +00002619 case 64:
2620 SubReg = AArch64::dsub;
2621 break;
2622 case 32:
2623 SubReg = AArch64::ssub;
2624 break;
Oliver Stannard89d15422014-08-27 16:16:04 +00002625 case 16:
2626 SubReg = AArch64::hsub;
2627 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00002628 case 8:
2629 llvm_unreachable("unexpected zext-requiring extract element!");
2630 }
2631 SDValue Extract = CurDAG->getTargetExtractSubreg(SubReg, SDLoc(Node), VT,
2632 Node->getOperand(0));
2633 DEBUG(dbgs() << "ISEL: Custom selection!\n=> ");
2634 DEBUG(Extract->dumpr(CurDAG));
2635 DEBUG(dbgs() << "\n");
Justin Bogner283e3bd2016-05-12 23:10:30 +00002636 ReplaceNode(Node, Extract.getNode());
2637 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002638 }
2639 case ISD::Constant: {
2640 // Materialize zero constants as copies from WZR/XZR. This allows
2641 // the coalescer to propagate these into other instructions.
2642 ConstantSDNode *ConstNode = cast<ConstantSDNode>(Node);
2643 if (ConstNode->isNullValue()) {
Justin Bogner283e3bd2016-05-12 23:10:30 +00002644 if (VT == MVT::i32) {
2645 SDValue New = CurDAG->getCopyFromReg(
2646 CurDAG->getEntryNode(), SDLoc(Node), AArch64::WZR, MVT::i32);
2647 ReplaceNode(Node, New.getNode());
2648 return;
2649 } else if (VT == MVT::i64) {
2650 SDValue New = CurDAG->getCopyFromReg(
2651 CurDAG->getEntryNode(), SDLoc(Node), AArch64::XZR, MVT::i64);
2652 ReplaceNode(Node, New.getNode());
2653 return;
2654 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002655 }
2656 break;
2657 }
2658
2659 case ISD::FrameIndex: {
2660 // Selects to ADDXri FI, 0 which in turn will become ADDXri SP, imm.
2661 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
2662 unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
2663 const TargetLowering *TLI = getTargetLowering();
Mehdi Amini44ede332015-07-09 02:09:04 +00002664 SDValue TFI = CurDAG->getTargetFrameIndex(
2665 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002666 SDLoc DL(Node);
2667 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, DL, MVT::i32),
2668 CurDAG->getTargetConstant(Shifter, DL, MVT::i32) };
Justin Bogner283e3bd2016-05-12 23:10:30 +00002669 CurDAG->SelectNodeTo(Node, AArch64::ADDXri, MVT::i64, Ops);
2670 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002671 }
2672 case ISD::INTRINSIC_W_CHAIN: {
2673 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2674 switch (IntNo) {
2675 default:
2676 break;
2677 case Intrinsic::aarch64_ldaxp:
2678 case Intrinsic::aarch64_ldxp: {
2679 unsigned Op =
2680 IntNo == Intrinsic::aarch64_ldaxp ? AArch64::LDAXPX : AArch64::LDXPX;
2681 SDValue MemAddr = Node->getOperand(2);
2682 SDLoc DL(Node);
2683 SDValue Chain = Node->getOperand(0);
2684
2685 SDNode *Ld = CurDAG->getMachineNode(Op, DL, MVT::i64, MVT::i64,
2686 MVT::Other, MemAddr, Chain);
2687
2688 // Transfer memoperands.
2689 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2690 MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2691 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
Justin Bogner283e3bd2016-05-12 23:10:30 +00002692 ReplaceNode(Node, Ld);
2693 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002694 }
2695 case Intrinsic::aarch64_stlxp:
2696 case Intrinsic::aarch64_stxp: {
2697 unsigned Op =
2698 IntNo == Intrinsic::aarch64_stlxp ? AArch64::STLXPX : AArch64::STXPX;
2699 SDLoc DL(Node);
2700 SDValue Chain = Node->getOperand(0);
2701 SDValue ValLo = Node->getOperand(2);
2702 SDValue ValHi = Node->getOperand(3);
2703 SDValue MemAddr = Node->getOperand(4);
2704
2705 // Place arguments in the right order.
Benjamin Kramerea68a942015-02-19 15:26:17 +00002706 SDValue Ops[] = {ValLo, ValHi, MemAddr, Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00002707
2708 SDNode *St = CurDAG->getMachineNode(Op, DL, MVT::i32, MVT::Other, Ops);
2709 // Transfer memoperands.
2710 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2711 MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2712 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
2713
Justin Bogner283e3bd2016-05-12 23:10:30 +00002714 ReplaceNode(Node, St);
2715 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00002716 }
2717 case Intrinsic::aarch64_neon_ld1x2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002718 if (VT == MVT::v8i8) {
2719 SelectLoad(Node, 2, AArch64::LD1Twov8b, AArch64::dsub0);
2720 return;
2721 } else if (VT == MVT::v16i8) {
2722 SelectLoad(Node, 2, AArch64::LD1Twov16b, AArch64::qsub0);
2723 return;
2724 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2725 SelectLoad(Node, 2, AArch64::LD1Twov4h, AArch64::dsub0);
2726 return;
2727 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2728 SelectLoad(Node, 2, AArch64::LD1Twov8h, AArch64::qsub0);
2729 return;
2730 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2731 SelectLoad(Node, 2, AArch64::LD1Twov2s, AArch64::dsub0);
2732 return;
2733 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2734 SelectLoad(Node, 2, AArch64::LD1Twov4s, AArch64::qsub0);
2735 return;
2736 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2737 SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2738 return;
2739 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2740 SelectLoad(Node, 2, AArch64::LD1Twov2d, AArch64::qsub0);
2741 return;
2742 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002743 break;
2744 case Intrinsic::aarch64_neon_ld1x3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002745 if (VT == MVT::v8i8) {
2746 SelectLoad(Node, 3, AArch64::LD1Threev8b, AArch64::dsub0);
2747 return;
2748 } else if (VT == MVT::v16i8) {
2749 SelectLoad(Node, 3, AArch64::LD1Threev16b, AArch64::qsub0);
2750 return;
2751 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2752 SelectLoad(Node, 3, AArch64::LD1Threev4h, AArch64::dsub0);
2753 return;
2754 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2755 SelectLoad(Node, 3, AArch64::LD1Threev8h, AArch64::qsub0);
2756 return;
2757 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2758 SelectLoad(Node, 3, AArch64::LD1Threev2s, AArch64::dsub0);
2759 return;
2760 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2761 SelectLoad(Node, 3, AArch64::LD1Threev4s, AArch64::qsub0);
2762 return;
2763 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2764 SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2765 return;
2766 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2767 SelectLoad(Node, 3, AArch64::LD1Threev2d, AArch64::qsub0);
2768 return;
2769 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002770 break;
2771 case Intrinsic::aarch64_neon_ld1x4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002772 if (VT == MVT::v8i8) {
2773 SelectLoad(Node, 4, AArch64::LD1Fourv8b, AArch64::dsub0);
2774 return;
2775 } else if (VT == MVT::v16i8) {
2776 SelectLoad(Node, 4, AArch64::LD1Fourv16b, AArch64::qsub0);
2777 return;
2778 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2779 SelectLoad(Node, 4, AArch64::LD1Fourv4h, AArch64::dsub0);
2780 return;
2781 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2782 SelectLoad(Node, 4, AArch64::LD1Fourv8h, AArch64::qsub0);
2783 return;
2784 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2785 SelectLoad(Node, 4, AArch64::LD1Fourv2s, AArch64::dsub0);
2786 return;
2787 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2788 SelectLoad(Node, 4, AArch64::LD1Fourv4s, AArch64::qsub0);
2789 return;
2790 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2791 SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2792 return;
2793 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2794 SelectLoad(Node, 4, AArch64::LD1Fourv2d, AArch64::qsub0);
2795 return;
2796 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002797 break;
2798 case Intrinsic::aarch64_neon_ld2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002799 if (VT == MVT::v8i8) {
2800 SelectLoad(Node, 2, AArch64::LD2Twov8b, AArch64::dsub0);
2801 return;
2802 } else if (VT == MVT::v16i8) {
2803 SelectLoad(Node, 2, AArch64::LD2Twov16b, AArch64::qsub0);
2804 return;
2805 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2806 SelectLoad(Node, 2, AArch64::LD2Twov4h, AArch64::dsub0);
2807 return;
2808 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2809 SelectLoad(Node, 2, AArch64::LD2Twov8h, AArch64::qsub0);
2810 return;
2811 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2812 SelectLoad(Node, 2, AArch64::LD2Twov2s, AArch64::dsub0);
2813 return;
2814 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2815 SelectLoad(Node, 2, AArch64::LD2Twov4s, AArch64::qsub0);
2816 return;
2817 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2818 SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2819 return;
2820 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2821 SelectLoad(Node, 2, AArch64::LD2Twov2d, AArch64::qsub0);
2822 return;
2823 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002824 break;
2825 case Intrinsic::aarch64_neon_ld3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002826 if (VT == MVT::v8i8) {
2827 SelectLoad(Node, 3, AArch64::LD3Threev8b, AArch64::dsub0);
2828 return;
2829 } else if (VT == MVT::v16i8) {
2830 SelectLoad(Node, 3, AArch64::LD3Threev16b, AArch64::qsub0);
2831 return;
2832 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2833 SelectLoad(Node, 3, AArch64::LD3Threev4h, AArch64::dsub0);
2834 return;
2835 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2836 SelectLoad(Node, 3, AArch64::LD3Threev8h, AArch64::qsub0);
2837 return;
2838 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2839 SelectLoad(Node, 3, AArch64::LD3Threev2s, AArch64::dsub0);
2840 return;
2841 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2842 SelectLoad(Node, 3, AArch64::LD3Threev4s, AArch64::qsub0);
2843 return;
2844 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2845 SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2846 return;
2847 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2848 SelectLoad(Node, 3, AArch64::LD3Threev2d, AArch64::qsub0);
2849 return;
2850 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002851 break;
2852 case Intrinsic::aarch64_neon_ld4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002853 if (VT == MVT::v8i8) {
2854 SelectLoad(Node, 4, AArch64::LD4Fourv8b, AArch64::dsub0);
2855 return;
2856 } else if (VT == MVT::v16i8) {
2857 SelectLoad(Node, 4, AArch64::LD4Fourv16b, AArch64::qsub0);
2858 return;
2859 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2860 SelectLoad(Node, 4, AArch64::LD4Fourv4h, AArch64::dsub0);
2861 return;
2862 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2863 SelectLoad(Node, 4, AArch64::LD4Fourv8h, AArch64::qsub0);
2864 return;
2865 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2866 SelectLoad(Node, 4, AArch64::LD4Fourv2s, AArch64::dsub0);
2867 return;
2868 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2869 SelectLoad(Node, 4, AArch64::LD4Fourv4s, AArch64::qsub0);
2870 return;
2871 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2872 SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2873 return;
2874 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2875 SelectLoad(Node, 4, AArch64::LD4Fourv2d, AArch64::qsub0);
2876 return;
2877 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002878 break;
2879 case Intrinsic::aarch64_neon_ld2r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002880 if (VT == MVT::v8i8) {
2881 SelectLoad(Node, 2, AArch64::LD2Rv8b, AArch64::dsub0);
2882 return;
2883 } else if (VT == MVT::v16i8) {
2884 SelectLoad(Node, 2, AArch64::LD2Rv16b, AArch64::qsub0);
2885 return;
2886 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2887 SelectLoad(Node, 2, AArch64::LD2Rv4h, AArch64::dsub0);
2888 return;
2889 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2890 SelectLoad(Node, 2, AArch64::LD2Rv8h, AArch64::qsub0);
2891 return;
2892 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2893 SelectLoad(Node, 2, AArch64::LD2Rv2s, AArch64::dsub0);
2894 return;
2895 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2896 SelectLoad(Node, 2, AArch64::LD2Rv4s, AArch64::qsub0);
2897 return;
2898 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2899 SelectLoad(Node, 2, AArch64::LD2Rv1d, AArch64::dsub0);
2900 return;
2901 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2902 SelectLoad(Node, 2, AArch64::LD2Rv2d, AArch64::qsub0);
2903 return;
2904 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002905 break;
2906 case Intrinsic::aarch64_neon_ld3r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002907 if (VT == MVT::v8i8) {
2908 SelectLoad(Node, 3, AArch64::LD3Rv8b, AArch64::dsub0);
2909 return;
2910 } else if (VT == MVT::v16i8) {
2911 SelectLoad(Node, 3, AArch64::LD3Rv16b, AArch64::qsub0);
2912 return;
2913 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2914 SelectLoad(Node, 3, AArch64::LD3Rv4h, AArch64::dsub0);
2915 return;
2916 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2917 SelectLoad(Node, 3, AArch64::LD3Rv8h, AArch64::qsub0);
2918 return;
2919 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2920 SelectLoad(Node, 3, AArch64::LD3Rv2s, AArch64::dsub0);
2921 return;
2922 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2923 SelectLoad(Node, 3, AArch64::LD3Rv4s, AArch64::qsub0);
2924 return;
2925 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2926 SelectLoad(Node, 3, AArch64::LD3Rv1d, AArch64::dsub0);
2927 return;
2928 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2929 SelectLoad(Node, 3, AArch64::LD3Rv2d, AArch64::qsub0);
2930 return;
2931 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002932 break;
2933 case Intrinsic::aarch64_neon_ld4r:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002934 if (VT == MVT::v8i8) {
2935 SelectLoad(Node, 4, AArch64::LD4Rv8b, AArch64::dsub0);
2936 return;
2937 } else if (VT == MVT::v16i8) {
2938 SelectLoad(Node, 4, AArch64::LD4Rv16b, AArch64::qsub0);
2939 return;
2940 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
2941 SelectLoad(Node, 4, AArch64::LD4Rv4h, AArch64::dsub0);
2942 return;
2943 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
2944 SelectLoad(Node, 4, AArch64::LD4Rv8h, AArch64::qsub0);
2945 return;
2946 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
2947 SelectLoad(Node, 4, AArch64::LD4Rv2s, AArch64::dsub0);
2948 return;
2949 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
2950 SelectLoad(Node, 4, AArch64::LD4Rv4s, AArch64::qsub0);
2951 return;
2952 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
2953 SelectLoad(Node, 4, AArch64::LD4Rv1d, AArch64::dsub0);
2954 return;
2955 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
2956 SelectLoad(Node, 4, AArch64::LD4Rv2d, AArch64::qsub0);
2957 return;
2958 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002959 break;
2960 case Intrinsic::aarch64_neon_ld2lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002961 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
2962 SelectLoadLane(Node, 2, AArch64::LD2i8);
2963 return;
2964 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2965 VT == MVT::v8f16) {
2966 SelectLoadLane(Node, 2, AArch64::LD2i16);
2967 return;
2968 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2969 VT == MVT::v2f32) {
2970 SelectLoadLane(Node, 2, AArch64::LD2i32);
2971 return;
2972 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2973 VT == MVT::v1f64) {
2974 SelectLoadLane(Node, 2, AArch64::LD2i64);
2975 return;
2976 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002977 break;
2978 case Intrinsic::aarch64_neon_ld3lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002979 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
2980 SelectLoadLane(Node, 3, AArch64::LD3i8);
2981 return;
2982 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2983 VT == MVT::v8f16) {
2984 SelectLoadLane(Node, 3, AArch64::LD3i16);
2985 return;
2986 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2987 VT == MVT::v2f32) {
2988 SelectLoadLane(Node, 3, AArch64::LD3i32);
2989 return;
2990 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2991 VT == MVT::v1f64) {
2992 SelectLoadLane(Node, 3, AArch64::LD3i64);
2993 return;
2994 }
Tim Northover3b0846e2014-05-24 12:50:23 +00002995 break;
2996 case Intrinsic::aarch64_neon_ld4lane:
Justin Bogner283e3bd2016-05-12 23:10:30 +00002997 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
2998 SelectLoadLane(Node, 4, AArch64::LD4i8);
2999 return;
3000 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3001 VT == MVT::v8f16) {
3002 SelectLoadLane(Node, 4, AArch64::LD4i16);
3003 return;
3004 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3005 VT == MVT::v2f32) {
3006 SelectLoadLane(Node, 4, AArch64::LD4i32);
3007 return;
3008 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3009 VT == MVT::v1f64) {
3010 SelectLoadLane(Node, 4, AArch64::LD4i64);
3011 return;
3012 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003013 break;
3014 }
3015 } break;
3016 case ISD::INTRINSIC_WO_CHAIN: {
3017 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
3018 switch (IntNo) {
3019 default:
3020 break;
3021 case Intrinsic::aarch64_neon_tbl2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003022 SelectTable(Node, 2,
3023 VT == MVT::v8i8 ? AArch64::TBLv8i8Two : AArch64::TBLv16i8Two,
3024 false);
3025 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003026 case Intrinsic::aarch64_neon_tbl3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003027 SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBLv8i8Three
3028 : AArch64::TBLv16i8Three,
3029 false);
3030 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003031 case Intrinsic::aarch64_neon_tbl4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003032 SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBLv8i8Four
3033 : AArch64::TBLv16i8Four,
3034 false);
3035 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003036 case Intrinsic::aarch64_neon_tbx2:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003037 SelectTable(Node, 2,
3038 VT == MVT::v8i8 ? AArch64::TBXv8i8Two : AArch64::TBXv16i8Two,
3039 true);
3040 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003041 case Intrinsic::aarch64_neon_tbx3:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003042 SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBXv8i8Three
3043 : AArch64::TBXv16i8Three,
3044 true);
3045 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003046 case Intrinsic::aarch64_neon_tbx4:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003047 SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBXv8i8Four
3048 : AArch64::TBXv16i8Four,
3049 true);
3050 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003051 case Intrinsic::aarch64_neon_smull:
3052 case Intrinsic::aarch64_neon_umull:
Justin Bogner283e3bd2016-05-12 23:10:30 +00003053 if (tryMULLV64LaneV128(IntNo, Node))
3054 return;
Tim Northover3b0846e2014-05-24 12:50:23 +00003055 break;
3056 }
3057 break;
3058 }
3059 case ISD::INTRINSIC_VOID: {
3060 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
3061 if (Node->getNumOperands() >= 3)
3062 VT = Node->getOperand(2)->getValueType(0);
3063 switch (IntNo) {
3064 default:
3065 break;
3066 case Intrinsic::aarch64_neon_st1x2: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003067 if (VT == MVT::v8i8) {
3068 SelectStore(Node, 2, AArch64::ST1Twov8b);
3069 return;
3070 } else if (VT == MVT::v16i8) {
3071 SelectStore(Node, 2, AArch64::ST1Twov16b);
3072 return;
3073 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3074 SelectStore(Node, 2, AArch64::ST1Twov4h);
3075 return;
3076 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3077 SelectStore(Node, 2, AArch64::ST1Twov8h);
3078 return;
3079 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3080 SelectStore(Node, 2, AArch64::ST1Twov2s);
3081 return;
3082 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3083 SelectStore(Node, 2, AArch64::ST1Twov4s);
3084 return;
3085 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3086 SelectStore(Node, 2, AArch64::ST1Twov2d);
3087 return;
3088 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3089 SelectStore(Node, 2, AArch64::ST1Twov1d);
3090 return;
3091 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003092 break;
3093 }
3094 case Intrinsic::aarch64_neon_st1x3: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003095 if (VT == MVT::v8i8) {
3096 SelectStore(Node, 3, AArch64::ST1Threev8b);
3097 return;
3098 } else if (VT == MVT::v16i8) {
3099 SelectStore(Node, 3, AArch64::ST1Threev16b);
3100 return;
3101 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3102 SelectStore(Node, 3, AArch64::ST1Threev4h);
3103 return;
3104 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3105 SelectStore(Node, 3, AArch64::ST1Threev8h);
3106 return;
3107 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3108 SelectStore(Node, 3, AArch64::ST1Threev2s);
3109 return;
3110 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3111 SelectStore(Node, 3, AArch64::ST1Threev4s);
3112 return;
3113 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3114 SelectStore(Node, 3, AArch64::ST1Threev2d);
3115 return;
3116 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3117 SelectStore(Node, 3, AArch64::ST1Threev1d);
3118 return;
3119 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003120 break;
3121 }
3122 case Intrinsic::aarch64_neon_st1x4: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003123 if (VT == MVT::v8i8) {
3124 SelectStore(Node, 4, AArch64::ST1Fourv8b);
3125 return;
3126 } else if (VT == MVT::v16i8) {
3127 SelectStore(Node, 4, AArch64::ST1Fourv16b);
3128 return;
3129 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3130 SelectStore(Node, 4, AArch64::ST1Fourv4h);
3131 return;
3132 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3133 SelectStore(Node, 4, AArch64::ST1Fourv8h);
3134 return;
3135 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3136 SelectStore(Node, 4, AArch64::ST1Fourv2s);
3137 return;
3138 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3139 SelectStore(Node, 4, AArch64::ST1Fourv4s);
3140 return;
3141 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3142 SelectStore(Node, 4, AArch64::ST1Fourv2d);
3143 return;
3144 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3145 SelectStore(Node, 4, AArch64::ST1Fourv1d);
3146 return;
3147 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003148 break;
3149 }
3150 case Intrinsic::aarch64_neon_st2: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003151 if (VT == MVT::v8i8) {
3152 SelectStore(Node, 2, AArch64::ST2Twov8b);
3153 return;
3154 } else if (VT == MVT::v16i8) {
3155 SelectStore(Node, 2, AArch64::ST2Twov16b);
3156 return;
3157 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3158 SelectStore(Node, 2, AArch64::ST2Twov4h);
3159 return;
3160 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3161 SelectStore(Node, 2, AArch64::ST2Twov8h);
3162 return;
3163 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3164 SelectStore(Node, 2, AArch64::ST2Twov2s);
3165 return;
3166 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3167 SelectStore(Node, 2, AArch64::ST2Twov4s);
3168 return;
3169 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3170 SelectStore(Node, 2, AArch64::ST2Twov2d);
3171 return;
3172 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3173 SelectStore(Node, 2, AArch64::ST1Twov1d);
3174 return;
3175 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003176 break;
3177 }
3178 case Intrinsic::aarch64_neon_st3: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003179 if (VT == MVT::v8i8) {
3180 SelectStore(Node, 3, AArch64::ST3Threev8b);
3181 return;
3182 } else if (VT == MVT::v16i8) {
3183 SelectStore(Node, 3, AArch64::ST3Threev16b);
3184 return;
3185 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3186 SelectStore(Node, 3, AArch64::ST3Threev4h);
3187 return;
3188 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3189 SelectStore(Node, 3, AArch64::ST3Threev8h);
3190 return;
3191 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3192 SelectStore(Node, 3, AArch64::ST3Threev2s);
3193 return;
3194 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3195 SelectStore(Node, 3, AArch64::ST3Threev4s);
3196 return;
3197 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3198 SelectStore(Node, 3, AArch64::ST3Threev2d);
3199 return;
3200 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3201 SelectStore(Node, 3, AArch64::ST1Threev1d);
3202 return;
3203 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003204 break;
3205 }
3206 case Intrinsic::aarch64_neon_st4: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003207 if (VT == MVT::v8i8) {
3208 SelectStore(Node, 4, AArch64::ST4Fourv8b);
3209 return;
3210 } else if (VT == MVT::v16i8) {
3211 SelectStore(Node, 4, AArch64::ST4Fourv16b);
3212 return;
3213 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3214 SelectStore(Node, 4, AArch64::ST4Fourv4h);
3215 return;
3216 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3217 SelectStore(Node, 4, AArch64::ST4Fourv8h);
3218 return;
3219 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3220 SelectStore(Node, 4, AArch64::ST4Fourv2s);
3221 return;
3222 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3223 SelectStore(Node, 4, AArch64::ST4Fourv4s);
3224 return;
3225 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3226 SelectStore(Node, 4, AArch64::ST4Fourv2d);
3227 return;
3228 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3229 SelectStore(Node, 4, AArch64::ST1Fourv1d);
3230 return;
3231 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003232 break;
3233 }
3234 case Intrinsic::aarch64_neon_st2lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003235 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3236 SelectStoreLane(Node, 2, AArch64::ST2i8);
3237 return;
3238 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3239 VT == MVT::v8f16) {
3240 SelectStoreLane(Node, 2, AArch64::ST2i16);
3241 return;
3242 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3243 VT == MVT::v2f32) {
3244 SelectStoreLane(Node, 2, AArch64::ST2i32);
3245 return;
3246 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3247 VT == MVT::v1f64) {
3248 SelectStoreLane(Node, 2, AArch64::ST2i64);
3249 return;
3250 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003251 break;
3252 }
3253 case Intrinsic::aarch64_neon_st3lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003254 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3255 SelectStoreLane(Node, 3, AArch64::ST3i8);
3256 return;
3257 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3258 VT == MVT::v8f16) {
3259 SelectStoreLane(Node, 3, AArch64::ST3i16);
3260 return;
3261 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3262 VT == MVT::v2f32) {
3263 SelectStoreLane(Node, 3, AArch64::ST3i32);
3264 return;
3265 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3266 VT == MVT::v1f64) {
3267 SelectStoreLane(Node, 3, AArch64::ST3i64);
3268 return;
3269 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003270 break;
3271 }
3272 case Intrinsic::aarch64_neon_st4lane: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003273 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3274 SelectStoreLane(Node, 4, AArch64::ST4i8);
3275 return;
3276 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3277 VT == MVT::v8f16) {
3278 SelectStoreLane(Node, 4, AArch64::ST4i16);
3279 return;
3280 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3281 VT == MVT::v2f32) {
3282 SelectStoreLane(Node, 4, AArch64::ST4i32);
3283 return;
3284 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3285 VT == MVT::v1f64) {
3286 SelectStoreLane(Node, 4, AArch64::ST4i64);
3287 return;
3288 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003289 break;
3290 }
3291 }
Mehdi Aminia7583982015-08-23 00:42:57 +00003292 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00003293 }
3294 case AArch64ISD::LD2post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003295 if (VT == MVT::v8i8) {
3296 SelectPostLoad(Node, 2, AArch64::LD2Twov8b_POST, AArch64::dsub0);
3297 return;
3298 } else if (VT == MVT::v16i8) {
3299 SelectPostLoad(Node, 2, AArch64::LD2Twov16b_POST, AArch64::qsub0);
3300 return;
3301 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3302 SelectPostLoad(Node, 2, AArch64::LD2Twov4h_POST, AArch64::dsub0);
3303 return;
3304 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3305 SelectPostLoad(Node, 2, AArch64::LD2Twov8h_POST, AArch64::qsub0);
3306 return;
3307 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3308 SelectPostLoad(Node, 2, AArch64::LD2Twov2s_POST, AArch64::dsub0);
3309 return;
3310 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3311 SelectPostLoad(Node, 2, AArch64::LD2Twov4s_POST, AArch64::qsub0);
3312 return;
3313 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3314 SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
3315 return;
3316 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3317 SelectPostLoad(Node, 2, AArch64::LD2Twov2d_POST, AArch64::qsub0);
3318 return;
3319 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003320 break;
3321 }
3322 case AArch64ISD::LD3post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003323 if (VT == MVT::v8i8) {
3324 SelectPostLoad(Node, 3, AArch64::LD3Threev8b_POST, AArch64::dsub0);
3325 return;
3326 } else if (VT == MVT::v16i8) {
3327 SelectPostLoad(Node, 3, AArch64::LD3Threev16b_POST, AArch64::qsub0);
3328 return;
3329 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3330 SelectPostLoad(Node, 3, AArch64::LD3Threev4h_POST, AArch64::dsub0);
3331 return;
3332 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3333 SelectPostLoad(Node, 3, AArch64::LD3Threev8h_POST, AArch64::qsub0);
3334 return;
3335 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3336 SelectPostLoad(Node, 3, AArch64::LD3Threev2s_POST, AArch64::dsub0);
3337 return;
3338 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3339 SelectPostLoad(Node, 3, AArch64::LD3Threev4s_POST, AArch64::qsub0);
3340 return;
3341 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3342 SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3343 return;
3344 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3345 SelectPostLoad(Node, 3, AArch64::LD3Threev2d_POST, AArch64::qsub0);
3346 return;
3347 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003348 break;
3349 }
3350 case AArch64ISD::LD4post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003351 if (VT == MVT::v8i8) {
3352 SelectPostLoad(Node, 4, AArch64::LD4Fourv8b_POST, AArch64::dsub0);
3353 return;
3354 } else if (VT == MVT::v16i8) {
3355 SelectPostLoad(Node, 4, AArch64::LD4Fourv16b_POST, AArch64::qsub0);
3356 return;
3357 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3358 SelectPostLoad(Node, 4, AArch64::LD4Fourv4h_POST, AArch64::dsub0);
3359 return;
3360 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3361 SelectPostLoad(Node, 4, AArch64::LD4Fourv8h_POST, AArch64::qsub0);
3362 return;
3363 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3364 SelectPostLoad(Node, 4, AArch64::LD4Fourv2s_POST, AArch64::dsub0);
3365 return;
3366 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3367 SelectPostLoad(Node, 4, AArch64::LD4Fourv4s_POST, AArch64::qsub0);
3368 return;
3369 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3370 SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3371 return;
3372 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3373 SelectPostLoad(Node, 4, AArch64::LD4Fourv2d_POST, AArch64::qsub0);
3374 return;
3375 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003376 break;
3377 }
3378 case AArch64ISD::LD1x2post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003379 if (VT == MVT::v8i8) {
3380 SelectPostLoad(Node, 2, AArch64::LD1Twov8b_POST, AArch64::dsub0);
3381 return;
3382 } else if (VT == MVT::v16i8) {
3383 SelectPostLoad(Node, 2, AArch64::LD1Twov16b_POST, AArch64::qsub0);
3384 return;
3385 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3386 SelectPostLoad(Node, 2, AArch64::LD1Twov4h_POST, AArch64::dsub0);
3387 return;
3388 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3389 SelectPostLoad(Node, 2, AArch64::LD1Twov8h_POST, AArch64::qsub0);
3390 return;
3391 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3392 SelectPostLoad(Node, 2, AArch64::LD1Twov2s_POST, AArch64::dsub0);
3393 return;
3394 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3395 SelectPostLoad(Node, 2, AArch64::LD1Twov4s_POST, AArch64::qsub0);
3396 return;
3397 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3398 SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
3399 return;
3400 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3401 SelectPostLoad(Node, 2, AArch64::LD1Twov2d_POST, AArch64::qsub0);
3402 return;
3403 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003404 break;
3405 }
3406 case AArch64ISD::LD1x3post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003407 if (VT == MVT::v8i8) {
3408 SelectPostLoad(Node, 3, AArch64::LD1Threev8b_POST, AArch64::dsub0);
3409 return;
3410 } else if (VT == MVT::v16i8) {
3411 SelectPostLoad(Node, 3, AArch64::LD1Threev16b_POST, AArch64::qsub0);
3412 return;
3413 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3414 SelectPostLoad(Node, 3, AArch64::LD1Threev4h_POST, AArch64::dsub0);
3415 return;
3416 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3417 SelectPostLoad(Node, 3, AArch64::LD1Threev8h_POST, AArch64::qsub0);
3418 return;
3419 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3420 SelectPostLoad(Node, 3, AArch64::LD1Threev2s_POST, AArch64::dsub0);
3421 return;
3422 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3423 SelectPostLoad(Node, 3, AArch64::LD1Threev4s_POST, AArch64::qsub0);
3424 return;
3425 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3426 SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3427 return;
3428 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3429 SelectPostLoad(Node, 3, AArch64::LD1Threev2d_POST, AArch64::qsub0);
3430 return;
3431 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003432 break;
3433 }
3434 case AArch64ISD::LD1x4post: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003435 if (VT == MVT::v8i8) {
3436 SelectPostLoad(Node, 4, AArch64::LD1Fourv8b_POST, AArch64::dsub0);
3437 return;
3438 } else if (VT == MVT::v16i8) {
3439 SelectPostLoad(Node, 4, AArch64::LD1Fourv16b_POST, AArch64::qsub0);
3440 return;
3441 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3442 SelectPostLoad(Node, 4, AArch64::LD1Fourv4h_POST, AArch64::dsub0);
3443 return;
3444 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3445 SelectPostLoad(Node, 4, AArch64::LD1Fourv8h_POST, AArch64::qsub0);
3446 return;
3447 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3448 SelectPostLoad(Node, 4, AArch64::LD1Fourv2s_POST, AArch64::dsub0);
3449 return;
3450 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3451 SelectPostLoad(Node, 4, AArch64::LD1Fourv4s_POST, AArch64::qsub0);
3452 return;
3453 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3454 SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3455 return;
3456 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3457 SelectPostLoad(Node, 4, AArch64::LD1Fourv2d_POST, AArch64::qsub0);
3458 return;
3459 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003460 break;
3461 }
3462 case AArch64ISD::LD1DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003463 if (VT == MVT::v8i8) {
3464 SelectPostLoad(Node, 1, AArch64::LD1Rv8b_POST, AArch64::dsub0);
3465 return;
3466 } else if (VT == MVT::v16i8) {
3467 SelectPostLoad(Node, 1, AArch64::LD1Rv16b_POST, AArch64::qsub0);
3468 return;
3469 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3470 SelectPostLoad(Node, 1, AArch64::LD1Rv4h_POST, AArch64::dsub0);
3471 return;
3472 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3473 SelectPostLoad(Node, 1, AArch64::LD1Rv8h_POST, AArch64::qsub0);
3474 return;
3475 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3476 SelectPostLoad(Node, 1, AArch64::LD1Rv2s_POST, AArch64::dsub0);
3477 return;
3478 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3479 SelectPostLoad(Node, 1, AArch64::LD1Rv4s_POST, AArch64::qsub0);
3480 return;
3481 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3482 SelectPostLoad(Node, 1, AArch64::LD1Rv1d_POST, AArch64::dsub0);
3483 return;
3484 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3485 SelectPostLoad(Node, 1, AArch64::LD1Rv2d_POST, AArch64::qsub0);
3486 return;
3487 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003488 break;
3489 }
3490 case AArch64ISD::LD2DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003491 if (VT == MVT::v8i8) {
3492 SelectPostLoad(Node, 2, AArch64::LD2Rv8b_POST, AArch64::dsub0);
3493 return;
3494 } else if (VT == MVT::v16i8) {
3495 SelectPostLoad(Node, 2, AArch64::LD2Rv16b_POST, AArch64::qsub0);
3496 return;
3497 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3498 SelectPostLoad(Node, 2, AArch64::LD2Rv4h_POST, AArch64::dsub0);
3499 return;
3500 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3501 SelectPostLoad(Node, 2, AArch64::LD2Rv8h_POST, AArch64::qsub0);
3502 return;
3503 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3504 SelectPostLoad(Node, 2, AArch64::LD2Rv2s_POST, AArch64::dsub0);
3505 return;
3506 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3507 SelectPostLoad(Node, 2, AArch64::LD2Rv4s_POST, AArch64::qsub0);
3508 return;
3509 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3510 SelectPostLoad(Node, 2, AArch64::LD2Rv1d_POST, AArch64::dsub0);
3511 return;
3512 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3513 SelectPostLoad(Node, 2, AArch64::LD2Rv2d_POST, AArch64::qsub0);
3514 return;
3515 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003516 break;
3517 }
3518 case AArch64ISD::LD3DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003519 if (VT == MVT::v8i8) {
3520 SelectPostLoad(Node, 3, AArch64::LD3Rv8b_POST, AArch64::dsub0);
3521 return;
3522 } else if (VT == MVT::v16i8) {
3523 SelectPostLoad(Node, 3, AArch64::LD3Rv16b_POST, AArch64::qsub0);
3524 return;
3525 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3526 SelectPostLoad(Node, 3, AArch64::LD3Rv4h_POST, AArch64::dsub0);
3527 return;
3528 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3529 SelectPostLoad(Node, 3, AArch64::LD3Rv8h_POST, AArch64::qsub0);
3530 return;
3531 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3532 SelectPostLoad(Node, 3, AArch64::LD3Rv2s_POST, AArch64::dsub0);
3533 return;
3534 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3535 SelectPostLoad(Node, 3, AArch64::LD3Rv4s_POST, AArch64::qsub0);
3536 return;
3537 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3538 SelectPostLoad(Node, 3, AArch64::LD3Rv1d_POST, AArch64::dsub0);
3539 return;
3540 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3541 SelectPostLoad(Node, 3, AArch64::LD3Rv2d_POST, AArch64::qsub0);
3542 return;
3543 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003544 break;
3545 }
3546 case AArch64ISD::LD4DUPpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003547 if (VT == MVT::v8i8) {
3548 SelectPostLoad(Node, 4, AArch64::LD4Rv8b_POST, AArch64::dsub0);
3549 return;
3550 } else if (VT == MVT::v16i8) {
3551 SelectPostLoad(Node, 4, AArch64::LD4Rv16b_POST, AArch64::qsub0);
3552 return;
3553 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3554 SelectPostLoad(Node, 4, AArch64::LD4Rv4h_POST, AArch64::dsub0);
3555 return;
3556 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3557 SelectPostLoad(Node, 4, AArch64::LD4Rv8h_POST, AArch64::qsub0);
3558 return;
3559 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3560 SelectPostLoad(Node, 4, AArch64::LD4Rv2s_POST, AArch64::dsub0);
3561 return;
3562 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3563 SelectPostLoad(Node, 4, AArch64::LD4Rv4s_POST, AArch64::qsub0);
3564 return;
3565 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3566 SelectPostLoad(Node, 4, AArch64::LD4Rv1d_POST, AArch64::dsub0);
3567 return;
3568 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3569 SelectPostLoad(Node, 4, AArch64::LD4Rv2d_POST, AArch64::qsub0);
3570 return;
3571 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003572 break;
3573 }
3574 case AArch64ISD::LD1LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003575 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3576 SelectPostLoadLane(Node, 1, AArch64::LD1i8_POST);
3577 return;
3578 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3579 VT == MVT::v8f16) {
3580 SelectPostLoadLane(Node, 1, AArch64::LD1i16_POST);
3581 return;
3582 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3583 VT == MVT::v2f32) {
3584 SelectPostLoadLane(Node, 1, AArch64::LD1i32_POST);
3585 return;
3586 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3587 VT == MVT::v1f64) {
3588 SelectPostLoadLane(Node, 1, AArch64::LD1i64_POST);
3589 return;
3590 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003591 break;
3592 }
3593 case AArch64ISD::LD2LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003594 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3595 SelectPostLoadLane(Node, 2, AArch64::LD2i8_POST);
3596 return;
3597 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3598 VT == MVT::v8f16) {
3599 SelectPostLoadLane(Node, 2, AArch64::LD2i16_POST);
3600 return;
3601 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3602 VT == MVT::v2f32) {
3603 SelectPostLoadLane(Node, 2, AArch64::LD2i32_POST);
3604 return;
3605 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3606 VT == MVT::v1f64) {
3607 SelectPostLoadLane(Node, 2, AArch64::LD2i64_POST);
3608 return;
3609 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003610 break;
3611 }
3612 case AArch64ISD::LD3LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003613 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3614 SelectPostLoadLane(Node, 3, AArch64::LD3i8_POST);
3615 return;
3616 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3617 VT == MVT::v8f16) {
3618 SelectPostLoadLane(Node, 3, AArch64::LD3i16_POST);
3619 return;
3620 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3621 VT == MVT::v2f32) {
3622 SelectPostLoadLane(Node, 3, AArch64::LD3i32_POST);
3623 return;
3624 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3625 VT == MVT::v1f64) {
3626 SelectPostLoadLane(Node, 3, AArch64::LD3i64_POST);
3627 return;
3628 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003629 break;
3630 }
3631 case AArch64ISD::LD4LANEpost: {
Justin Bogner283e3bd2016-05-12 23:10:30 +00003632 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3633 SelectPostLoadLane(Node, 4, AArch64::LD4i8_POST);
3634 return;
3635 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3636 VT == MVT::v8f16) {
3637 SelectPostLoadLane(Node, 4, AArch64::LD4i16_POST);
3638 return;
3639 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3640 VT == MVT::v2f32) {
3641 SelectPostLoadLane(Node, 4, AArch64::LD4i32_POST);
3642 return;
3643 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3644 VT == MVT::v1f64) {
3645 SelectPostLoadLane(Node, 4, AArch64::LD4i64_POST);
3646 return;
3647 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003648 break;
3649 }
3650 case AArch64ISD::ST2post: {
3651 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003652 if (VT == MVT::v8i8) {
3653 SelectPostStore(Node, 2, AArch64::ST2Twov8b_POST);
3654 return;
3655 } else if (VT == MVT::v16i8) {
3656 SelectPostStore(Node, 2, AArch64::ST2Twov16b_POST);
3657 return;
3658 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3659 SelectPostStore(Node, 2, AArch64::ST2Twov4h_POST);
3660 return;
3661 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3662 SelectPostStore(Node, 2, AArch64::ST2Twov8h_POST);
3663 return;
3664 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3665 SelectPostStore(Node, 2, AArch64::ST2Twov2s_POST);
3666 return;
3667 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3668 SelectPostStore(Node, 2, AArch64::ST2Twov4s_POST);
3669 return;
3670 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3671 SelectPostStore(Node, 2, AArch64::ST2Twov2d_POST);
3672 return;
3673 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3674 SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3675 return;
3676 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003677 break;
3678 }
3679 case AArch64ISD::ST3post: {
3680 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003681 if (VT == MVT::v8i8) {
3682 SelectPostStore(Node, 3, AArch64::ST3Threev8b_POST);
3683 return;
3684 } else if (VT == MVT::v16i8) {
3685 SelectPostStore(Node, 3, AArch64::ST3Threev16b_POST);
3686 return;
3687 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3688 SelectPostStore(Node, 3, AArch64::ST3Threev4h_POST);
3689 return;
3690 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3691 SelectPostStore(Node, 3, AArch64::ST3Threev8h_POST);
3692 return;
3693 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3694 SelectPostStore(Node, 3, AArch64::ST3Threev2s_POST);
3695 return;
3696 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3697 SelectPostStore(Node, 3, AArch64::ST3Threev4s_POST);
3698 return;
3699 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3700 SelectPostStore(Node, 3, AArch64::ST3Threev2d_POST);
3701 return;
3702 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3703 SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3704 return;
3705 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003706 break;
3707 }
3708 case AArch64ISD::ST4post: {
3709 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003710 if (VT == MVT::v8i8) {
3711 SelectPostStore(Node, 4, AArch64::ST4Fourv8b_POST);
3712 return;
3713 } else if (VT == MVT::v16i8) {
3714 SelectPostStore(Node, 4, AArch64::ST4Fourv16b_POST);
3715 return;
3716 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3717 SelectPostStore(Node, 4, AArch64::ST4Fourv4h_POST);
3718 return;
3719 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3720 SelectPostStore(Node, 4, AArch64::ST4Fourv8h_POST);
3721 return;
3722 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3723 SelectPostStore(Node, 4, AArch64::ST4Fourv2s_POST);
3724 return;
3725 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3726 SelectPostStore(Node, 4, AArch64::ST4Fourv4s_POST);
3727 return;
3728 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3729 SelectPostStore(Node, 4, AArch64::ST4Fourv2d_POST);
3730 return;
3731 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3732 SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3733 return;
3734 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003735 break;
3736 }
3737 case AArch64ISD::ST1x2post: {
3738 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003739 if (VT == MVT::v8i8) {
3740 SelectPostStore(Node, 2, AArch64::ST1Twov8b_POST);
3741 return;
3742 } else if (VT == MVT::v16i8) {
3743 SelectPostStore(Node, 2, AArch64::ST1Twov16b_POST);
3744 return;
3745 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3746 SelectPostStore(Node, 2, AArch64::ST1Twov4h_POST);
3747 return;
3748 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3749 SelectPostStore(Node, 2, AArch64::ST1Twov8h_POST);
3750 return;
3751 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3752 SelectPostStore(Node, 2, AArch64::ST1Twov2s_POST);
3753 return;
3754 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3755 SelectPostStore(Node, 2, AArch64::ST1Twov4s_POST);
3756 return;
3757 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3758 SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3759 return;
3760 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3761 SelectPostStore(Node, 2, AArch64::ST1Twov2d_POST);
3762 return;
3763 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003764 break;
3765 }
3766 case AArch64ISD::ST1x3post: {
3767 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003768 if (VT == MVT::v8i8) {
3769 SelectPostStore(Node, 3, AArch64::ST1Threev8b_POST);
3770 return;
3771 } else if (VT == MVT::v16i8) {
3772 SelectPostStore(Node, 3, AArch64::ST1Threev16b_POST);
3773 return;
3774 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3775 SelectPostStore(Node, 3, AArch64::ST1Threev4h_POST);
3776 return;
3777 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3778 SelectPostStore(Node, 3, AArch64::ST1Threev8h_POST);
3779 return;
3780 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3781 SelectPostStore(Node, 3, AArch64::ST1Threev2s_POST);
3782 return;
3783 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3784 SelectPostStore(Node, 3, AArch64::ST1Threev4s_POST);
3785 return;
3786 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3787 SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3788 return;
3789 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3790 SelectPostStore(Node, 3, AArch64::ST1Threev2d_POST);
3791 return;
3792 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003793 break;
3794 }
3795 case AArch64ISD::ST1x4post: {
3796 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003797 if (VT == MVT::v8i8) {
3798 SelectPostStore(Node, 4, AArch64::ST1Fourv8b_POST);
3799 return;
3800 } else if (VT == MVT::v16i8) {
3801 SelectPostStore(Node, 4, AArch64::ST1Fourv16b_POST);
3802 return;
3803 } else if (VT == MVT::v4i16 || VT == MVT::v4f16) {
3804 SelectPostStore(Node, 4, AArch64::ST1Fourv4h_POST);
3805 return;
3806 } else if (VT == MVT::v8i16 || VT == MVT::v8f16) {
3807 SelectPostStore(Node, 4, AArch64::ST1Fourv8h_POST);
3808 return;
3809 } else if (VT == MVT::v2i32 || VT == MVT::v2f32) {
3810 SelectPostStore(Node, 4, AArch64::ST1Fourv2s_POST);
3811 return;
3812 } else if (VT == MVT::v4i32 || VT == MVT::v4f32) {
3813 SelectPostStore(Node, 4, AArch64::ST1Fourv4s_POST);
3814 return;
3815 } else if (VT == MVT::v1i64 || VT == MVT::v1f64) {
3816 SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3817 return;
3818 } else if (VT == MVT::v2i64 || VT == MVT::v2f64) {
3819 SelectPostStore(Node, 4, AArch64::ST1Fourv2d_POST);
3820 return;
3821 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003822 break;
3823 }
3824 case AArch64ISD::ST2LANEpost: {
3825 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003826 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3827 SelectPostStoreLane(Node, 2, AArch64::ST2i8_POST);
3828 return;
3829 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3830 VT == MVT::v8f16) {
3831 SelectPostStoreLane(Node, 2, AArch64::ST2i16_POST);
3832 return;
3833 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3834 VT == MVT::v2f32) {
3835 SelectPostStoreLane(Node, 2, AArch64::ST2i32_POST);
3836 return;
3837 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3838 VT == MVT::v1f64) {
3839 SelectPostStoreLane(Node, 2, AArch64::ST2i64_POST);
3840 return;
3841 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003842 break;
3843 }
3844 case AArch64ISD::ST3LANEpost: {
3845 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003846 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3847 SelectPostStoreLane(Node, 3, AArch64::ST3i8_POST);
3848 return;
3849 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3850 VT == MVT::v8f16) {
3851 SelectPostStoreLane(Node, 3, AArch64::ST3i16_POST);
3852 return;
3853 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3854 VT == MVT::v2f32) {
3855 SelectPostStoreLane(Node, 3, AArch64::ST3i32_POST);
3856 return;
3857 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3858 VT == MVT::v1f64) {
3859 SelectPostStoreLane(Node, 3, AArch64::ST3i64_POST);
3860 return;
3861 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003862 break;
3863 }
3864 case AArch64ISD::ST4LANEpost: {
3865 VT = Node->getOperand(1).getValueType();
Justin Bogner283e3bd2016-05-12 23:10:30 +00003866 if (VT == MVT::v16i8 || VT == MVT::v8i8) {
3867 SelectPostStoreLane(Node, 4, AArch64::ST4i8_POST);
3868 return;
3869 } else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3870 VT == MVT::v8f16) {
3871 SelectPostStoreLane(Node, 4, AArch64::ST4i16_POST);
3872 return;
3873 } else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3874 VT == MVT::v2f32) {
3875 SelectPostStoreLane(Node, 4, AArch64::ST4i32_POST);
3876 return;
3877 } else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3878 VT == MVT::v1f64) {
3879 SelectPostStoreLane(Node, 4, AArch64::ST4i64_POST);
3880 return;
3881 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003882 break;
3883 }
Tim Northover3b0846e2014-05-24 12:50:23 +00003884 }
3885
3886 // Select the default instruction
Justin Bogner283e3bd2016-05-12 23:10:30 +00003887 SelectCode(Node);
Tim Northover3b0846e2014-05-24 12:50:23 +00003888}
3889
3890/// createAArch64ISelDag - This pass converts a legalized DAG into a
3891/// AArch64-specific DAG, ready for instruction scheduling.
3892FunctionPass *llvm::createAArch64ISelDag(AArch64TargetMachine &TM,
3893 CodeGenOpt::Level OptLevel) {
3894 return new AArch64DAGToDAGISel(TM, OptLevel);
3895}