blob: d93e59ccf77ce984031f4e0f2bab497e1b0e963f [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 {
37 AArch64TargetMachine &TM;
38
39 /// Subtarget - Keep a pointer to the AArch64Subtarget around so that we can
40 /// make the right decision when generating code for different targets.
41 const AArch64Subtarget *Subtarget;
42
43 bool ForCodeSize;
44
45public:
46 explicit AArch64DAGToDAGISel(AArch64TargetMachine &tm,
47 CodeGenOpt::Level OptLevel)
48 : SelectionDAGISel(tm, OptLevel), TM(tm), Subtarget(nullptr),
49 ForCodeSize(false) {}
50
51 const char *getPassName() const override {
52 return "AArch64 Instruction Selection";
53 }
54
55 bool runOnMachineFunction(MachineFunction &MF) override {
Sanjay Patel924879a2015-08-04 15:49:57 +000056 ForCodeSize = MF.getFunction()->optForSize();
Eric Christopher1e513342015-01-30 23:46:40 +000057 Subtarget = &MF.getSubtarget<AArch64Subtarget>();
Tim Northover3b0846e2014-05-24 12:50:23 +000058 return SelectionDAGISel::runOnMachineFunction(MF);
59 }
60
61 SDNode *Select(SDNode *Node) override;
62
63 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
64 /// inline asm expressions.
65 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
Daniel Sanders60f1db02015-03-13 12:45:09 +000066 unsigned ConstraintID,
Tim Northover3b0846e2014-05-24 12:50:23 +000067 std::vector<SDValue> &OutOps) override;
68
69 SDNode *SelectMLAV64LaneV128(SDNode *N);
70 SDNode *SelectMULLV64LaneV128(unsigned IntNo, SDNode *N);
71 bool SelectArithExtendedRegister(SDValue N, SDValue &Reg, SDValue &Shift);
72 bool SelectArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
73 bool SelectNegArithImmed(SDValue N, SDValue &Val, SDValue &Shift);
74 bool SelectArithShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
75 return SelectShiftedRegister(N, false, Reg, Shift);
76 }
77 bool SelectLogicalShiftedRegister(SDValue N, SDValue &Reg, SDValue &Shift) {
78 return SelectShiftedRegister(N, true, Reg, Shift);
79 }
80 bool SelectAddrModeIndexed8(SDValue N, SDValue &Base, SDValue &OffImm) {
81 return SelectAddrModeIndexed(N, 1, Base, OffImm);
82 }
83 bool SelectAddrModeIndexed16(SDValue N, SDValue &Base, SDValue &OffImm) {
84 return SelectAddrModeIndexed(N, 2, Base, OffImm);
85 }
86 bool SelectAddrModeIndexed32(SDValue N, SDValue &Base, SDValue &OffImm) {
87 return SelectAddrModeIndexed(N, 4, Base, OffImm);
88 }
89 bool SelectAddrModeIndexed64(SDValue N, SDValue &Base, SDValue &OffImm) {
90 return SelectAddrModeIndexed(N, 8, Base, OffImm);
91 }
92 bool SelectAddrModeIndexed128(SDValue N, SDValue &Base, SDValue &OffImm) {
93 return SelectAddrModeIndexed(N, 16, Base, OffImm);
94 }
95 bool SelectAddrModeUnscaled8(SDValue N, SDValue &Base, SDValue &OffImm) {
96 return SelectAddrModeUnscaled(N, 1, Base, OffImm);
97 }
98 bool SelectAddrModeUnscaled16(SDValue N, SDValue &Base, SDValue &OffImm) {
99 return SelectAddrModeUnscaled(N, 2, Base, OffImm);
100 }
101 bool SelectAddrModeUnscaled32(SDValue N, SDValue &Base, SDValue &OffImm) {
102 return SelectAddrModeUnscaled(N, 4, Base, OffImm);
103 }
104 bool SelectAddrModeUnscaled64(SDValue N, SDValue &Base, SDValue &OffImm) {
105 return SelectAddrModeUnscaled(N, 8, Base, OffImm);
106 }
107 bool SelectAddrModeUnscaled128(SDValue N, SDValue &Base, SDValue &OffImm) {
108 return SelectAddrModeUnscaled(N, 16, Base, OffImm);
109 }
110
111 template<int Width>
112 bool SelectAddrModeWRO(SDValue N, SDValue &Base, SDValue &Offset,
113 SDValue &SignExtend, SDValue &DoShift) {
114 return SelectAddrModeWRO(N, Width / 8, Base, Offset, SignExtend, DoShift);
115 }
116
117 template<int Width>
118 bool SelectAddrModeXRO(SDValue N, SDValue &Base, SDValue &Offset,
119 SDValue &SignExtend, SDValue &DoShift) {
120 return SelectAddrModeXRO(N, Width / 8, Base, Offset, SignExtend, DoShift);
121 }
122
123
124 /// Form sequences of consecutive 64/128-bit registers for use in NEON
125 /// instructions making use of a vector-list (e.g. ldN, tbl). Vecs must have
126 /// between 1 and 4 elements. If it contains a single element that is returned
127 /// unchanged; otherwise a REG_SEQUENCE value is returned.
128 SDValue createDTuple(ArrayRef<SDValue> Vecs);
129 SDValue createQTuple(ArrayRef<SDValue> Vecs);
130
131 /// Generic helper for the createDTuple/createQTuple
132 /// functions. Those should almost always be called instead.
Benjamin Kramerea68a942015-02-19 15:26:17 +0000133 SDValue createTuple(ArrayRef<SDValue> Vecs, const unsigned RegClassIDs[],
134 const unsigned SubRegs[]);
Tim Northover3b0846e2014-05-24 12:50:23 +0000135
136 SDNode *SelectTable(SDNode *N, unsigned NumVecs, unsigned Opc, bool isExt);
137
138 SDNode *SelectIndexedLoad(SDNode *N, bool &Done);
139
140 SDNode *SelectLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
141 unsigned SubRegIdx);
142 SDNode *SelectPostLoad(SDNode *N, unsigned NumVecs, unsigned Opc,
143 unsigned SubRegIdx);
144 SDNode *SelectLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
145 SDNode *SelectPostLoadLane(SDNode *N, unsigned NumVecs, unsigned Opc);
146
147 SDNode *SelectStore(SDNode *N, unsigned NumVecs, unsigned Opc);
148 SDNode *SelectPostStore(SDNode *N, unsigned NumVecs, unsigned Opc);
149 SDNode *SelectStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
150 SDNode *SelectPostStoreLane(SDNode *N, unsigned NumVecs, unsigned Opc);
151
Tim Northover3b0846e2014-05-24 12:50:23 +0000152 SDNode *SelectBitfieldExtractOp(SDNode *N);
153 SDNode *SelectBitfieldInsertOp(SDNode *N);
154
155 SDNode *SelectLIBM(SDNode *N);
Geoff Berryc573bf7a2015-07-28 15:24:10 +0000156 SDNode *SelectFPConvertWithRound(SDNode *N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000157
Luke Cheeseman85fd06d2015-06-01 12:02:47 +0000158 SDNode *SelectReadRegister(SDNode *N);
159 SDNode *SelectWriteRegister(SDNode *N);
160
Tim Northover3b0846e2014-05-24 12:50:23 +0000161// Include the pieces autogenerated from the target description.
162#include "AArch64GenDAGISel.inc"
163
164private:
165 bool SelectShiftedRegister(SDValue N, bool AllowROR, SDValue &Reg,
166 SDValue &Shift);
167 bool SelectAddrModeIndexed(SDValue N, unsigned Size, SDValue &Base,
168 SDValue &OffImm);
169 bool SelectAddrModeUnscaled(SDValue N, unsigned Size, SDValue &Base,
170 SDValue &OffImm);
171 bool SelectAddrModeWRO(SDValue N, unsigned Size, SDValue &Base,
172 SDValue &Offset, SDValue &SignExtend,
173 SDValue &DoShift);
174 bool SelectAddrModeXRO(SDValue N, unsigned Size, SDValue &Base,
175 SDValue &Offset, SDValue &SignExtend,
176 SDValue &DoShift);
177 bool isWorthFolding(SDValue V) const;
178 bool SelectExtendedSHL(SDValue N, unsigned Size, bool WantExtend,
179 SDValue &Offset, SDValue &SignExtend);
180
181 template<unsigned RegWidth>
182 bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos) {
183 return SelectCVTFixedPosOperand(N, FixedPos, RegWidth);
184 }
185
186 bool SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos, unsigned Width);
Geoff Berryc573bf7a2015-07-28 15:24:10 +0000187
188 SDNode *GenerateInexactFlagIfNeeded(const SDValue &In, unsigned InTyVariant,
189 SDLoc DL);
Tim Northover3b0846e2014-05-24 12:50:23 +0000190};
191} // end anonymous namespace
192
193/// isIntImmediate - This method tests to see if the node is a constant
194/// operand. If so Imm will receive the 32-bit value.
195static bool isIntImmediate(const SDNode *N, uint64_t &Imm) {
196 if (const ConstantSDNode *C = dyn_cast<const ConstantSDNode>(N)) {
197 Imm = C->getZExtValue();
198 return true;
199 }
200 return false;
201}
202
203// isIntImmediate - This method tests to see if a constant operand.
204// If so Imm will receive the value.
205static bool isIntImmediate(SDValue N, uint64_t &Imm) {
206 return isIntImmediate(N.getNode(), Imm);
207}
208
209// isOpcWithIntImmediate - This method tests to see if the node is a specific
210// opcode and that it has a immediate integer right operand.
211// If so Imm will receive the 32 bit value.
212static bool isOpcWithIntImmediate(const SDNode *N, unsigned Opc,
213 uint64_t &Imm) {
214 return N->getOpcode() == Opc &&
215 isIntImmediate(N->getOperand(1).getNode(), Imm);
216}
217
218bool AArch64DAGToDAGISel::SelectInlineAsmMemoryOperand(
Daniel Sanders60f1db02015-03-13 12:45:09 +0000219 const SDValue &Op, unsigned ConstraintID, std::vector<SDValue> &OutOps) {
Daniel Sandersf731eee2015-03-23 11:33:15 +0000220 switch(ConstraintID) {
221 default:
222 llvm_unreachable("Unexpected asm memory constraint");
223 case InlineAsm::Constraint_i:
224 case InlineAsm::Constraint_m:
225 case InlineAsm::Constraint_Q:
226 // Require the address to be in a register. That is safe for all AArch64
227 // variants and it is hard to do anything much smarter without knowing
228 // how the operand is used.
229 OutOps.push_back(Op);
230 return false;
231 }
232 return true;
Tim Northover3b0846e2014-05-24 12:50:23 +0000233}
234
235/// SelectArithImmed - Select an immediate value that can be represented as
236/// a 12-bit value shifted left by either 0 or 12. If so, return true with
237/// Val set to the 12-bit value and Shift set to the shifter operand.
238bool AArch64DAGToDAGISel::SelectArithImmed(SDValue N, SDValue &Val,
239 SDValue &Shift) {
240 // This function is called from the addsub_shifted_imm ComplexPattern,
241 // which lists [imm] as the list of opcode it's interested in, however
242 // we still need to check whether the operand is actually an immediate
243 // here because the ComplexPattern opcode list is only used in
244 // root-level opcode matching.
245 if (!isa<ConstantSDNode>(N.getNode()))
246 return false;
247
248 uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
249 unsigned ShiftAmt;
250
251 if (Immed >> 12 == 0) {
252 ShiftAmt = 0;
253 } else if ((Immed & 0xfff) == 0 && Immed >> 24 == 0) {
254 ShiftAmt = 12;
255 Immed = Immed >> 12;
256 } else
257 return false;
258
259 unsigned ShVal = AArch64_AM::getShifterImm(AArch64_AM::LSL, ShiftAmt);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000260 SDLoc dl(N);
261 Val = CurDAG->getTargetConstant(Immed, dl, MVT::i32);
262 Shift = CurDAG->getTargetConstant(ShVal, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000263 return true;
264}
265
266/// SelectNegArithImmed - As above, but negates the value before trying to
267/// select it.
268bool AArch64DAGToDAGISel::SelectNegArithImmed(SDValue N, SDValue &Val,
269 SDValue &Shift) {
270 // This function is called from the addsub_shifted_imm ComplexPattern,
271 // which lists [imm] as the list of opcode it's interested in, however
272 // we still need to check whether the operand is actually an immediate
273 // here because the ComplexPattern opcode list is only used in
274 // root-level opcode matching.
275 if (!isa<ConstantSDNode>(N.getNode()))
276 return false;
277
278 // The immediate operand must be a 24-bit zero-extended immediate.
279 uint64_t Immed = cast<ConstantSDNode>(N.getNode())->getZExtValue();
280
281 // This negation is almost always valid, but "cmp wN, #0" and "cmn wN, #0"
282 // have the opposite effect on the C flag, so this pattern mustn't match under
283 // those circumstances.
284 if (Immed == 0)
285 return false;
286
287 if (N.getValueType() == MVT::i32)
288 Immed = ~((uint32_t)Immed) + 1;
289 else
290 Immed = ~Immed + 1ULL;
291 if (Immed & 0xFFFFFFFFFF000000ULL)
292 return false;
293
294 Immed &= 0xFFFFFFULL;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000295 return SelectArithImmed(CurDAG->getConstant(Immed, SDLoc(N), MVT::i32), Val,
296 Shift);
Tim Northover3b0846e2014-05-24 12:50:23 +0000297}
298
299/// getShiftTypeForNode - Translate a shift node to the corresponding
300/// ShiftType value.
301static AArch64_AM::ShiftExtendType getShiftTypeForNode(SDValue N) {
302 switch (N.getOpcode()) {
303 default:
304 return AArch64_AM::InvalidShiftExtend;
305 case ISD::SHL:
306 return AArch64_AM::LSL;
307 case ISD::SRL:
308 return AArch64_AM::LSR;
309 case ISD::SRA:
310 return AArch64_AM::ASR;
311 case ISD::ROTR:
312 return AArch64_AM::ROR;
313 }
314}
315
Eric Christopher25dbdeb2015-03-07 01:39:09 +0000316/// \brief Determine whether it is worth to fold V into an extended register.
Tim Northover3b0846e2014-05-24 12:50:23 +0000317bool AArch64DAGToDAGISel::isWorthFolding(SDValue V) const {
Robin Morisset039781e2014-08-29 21:53:01 +0000318 // it hurts if the value is used at least twice, unless we are optimizing
Tim Northover3b0846e2014-05-24 12:50:23 +0000319 // for code size.
320 if (ForCodeSize || V.hasOneUse())
321 return true;
322 return false;
323}
324
325/// SelectShiftedRegister - Select a "shifted register" operand. If the value
326/// is not shifted, set the Shift operand to default of "LSL 0". The logical
327/// instructions allow the shifted register to be rotated, but the arithmetic
328/// instructions do not. The AllowROR parameter specifies whether ROR is
329/// supported.
330bool AArch64DAGToDAGISel::SelectShiftedRegister(SDValue N, bool AllowROR,
331 SDValue &Reg, SDValue &Shift) {
332 AArch64_AM::ShiftExtendType ShType = getShiftTypeForNode(N);
333 if (ShType == AArch64_AM::InvalidShiftExtend)
334 return false;
335 if (!AllowROR && ShType == AArch64_AM::ROR)
336 return false;
337
338 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
339 unsigned BitSize = N.getValueType().getSizeInBits();
340 unsigned Val = RHS->getZExtValue() & (BitSize - 1);
341 unsigned ShVal = AArch64_AM::getShifterImm(ShType, Val);
342
343 Reg = N.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000344 Shift = CurDAG->getTargetConstant(ShVal, SDLoc(N), MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000345 return isWorthFolding(N);
346 }
347
348 return false;
349}
350
351/// getExtendTypeForNode - Translate an extend node to the corresponding
352/// ExtendType value.
353static AArch64_AM::ShiftExtendType
354getExtendTypeForNode(SDValue N, bool IsLoadStore = false) {
355 if (N.getOpcode() == ISD::SIGN_EXTEND ||
356 N.getOpcode() == ISD::SIGN_EXTEND_INREG) {
357 EVT SrcVT;
358 if (N.getOpcode() == ISD::SIGN_EXTEND_INREG)
359 SrcVT = cast<VTSDNode>(N.getOperand(1))->getVT();
360 else
361 SrcVT = N.getOperand(0).getValueType();
362
363 if (!IsLoadStore && SrcVT == MVT::i8)
364 return AArch64_AM::SXTB;
365 else if (!IsLoadStore && SrcVT == MVT::i16)
366 return AArch64_AM::SXTH;
367 else if (SrcVT == MVT::i32)
368 return AArch64_AM::SXTW;
369 assert(SrcVT != MVT::i64 && "extend from 64-bits?");
370
371 return AArch64_AM::InvalidShiftExtend;
372 } else if (N.getOpcode() == ISD::ZERO_EXTEND ||
373 N.getOpcode() == ISD::ANY_EXTEND) {
374 EVT SrcVT = N.getOperand(0).getValueType();
375 if (!IsLoadStore && SrcVT == MVT::i8)
376 return AArch64_AM::UXTB;
377 else if (!IsLoadStore && SrcVT == MVT::i16)
378 return AArch64_AM::UXTH;
379 else if (SrcVT == MVT::i32)
380 return AArch64_AM::UXTW;
381 assert(SrcVT != MVT::i64 && "extend from 64-bits?");
382
383 return AArch64_AM::InvalidShiftExtend;
384 } else if (N.getOpcode() == ISD::AND) {
385 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
386 if (!CSD)
387 return AArch64_AM::InvalidShiftExtend;
388 uint64_t AndMask = CSD->getZExtValue();
389
390 switch (AndMask) {
391 default:
392 return AArch64_AM::InvalidShiftExtend;
393 case 0xFF:
394 return !IsLoadStore ? AArch64_AM::UXTB : AArch64_AM::InvalidShiftExtend;
395 case 0xFFFF:
396 return !IsLoadStore ? AArch64_AM::UXTH : AArch64_AM::InvalidShiftExtend;
397 case 0xFFFFFFFF:
398 return AArch64_AM::UXTW;
399 }
400 }
401
402 return AArch64_AM::InvalidShiftExtend;
403}
404
405// Helper for SelectMLAV64LaneV128 - Recognize high lane extracts.
406static bool checkHighLaneIndex(SDNode *DL, SDValue &LaneOp, int &LaneIdx) {
407 if (DL->getOpcode() != AArch64ISD::DUPLANE16 &&
408 DL->getOpcode() != AArch64ISD::DUPLANE32)
409 return false;
410
411 SDValue SV = DL->getOperand(0);
412 if (SV.getOpcode() != ISD::INSERT_SUBVECTOR)
413 return false;
414
415 SDValue EV = SV.getOperand(1);
416 if (EV.getOpcode() != ISD::EXTRACT_SUBVECTOR)
417 return false;
418
419 ConstantSDNode *DLidx = cast<ConstantSDNode>(DL->getOperand(1).getNode());
420 ConstantSDNode *EVidx = cast<ConstantSDNode>(EV.getOperand(1).getNode());
421 LaneIdx = DLidx->getSExtValue() + EVidx->getSExtValue();
422 LaneOp = EV.getOperand(0);
423
424 return true;
425}
426
427// Helper for SelectOpcV64LaneV128 - Recogzine operatinos where one operand is a
428// high lane extract.
429static bool checkV64LaneV128(SDValue Op0, SDValue Op1, SDValue &StdOp,
430 SDValue &LaneOp, int &LaneIdx) {
431
432 if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx)) {
433 std::swap(Op0, Op1);
434 if (!checkHighLaneIndex(Op0.getNode(), LaneOp, LaneIdx))
435 return false;
436 }
437 StdOp = Op1;
438 return true;
439}
440
441/// SelectMLAV64LaneV128 - AArch64 supports vector MLAs where one multiplicand
442/// is a lane in the upper half of a 128-bit vector. Recognize and select this
443/// so that we don't emit unnecessary lane extracts.
444SDNode *AArch64DAGToDAGISel::SelectMLAV64LaneV128(SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000445 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000446 SDValue Op0 = N->getOperand(0);
447 SDValue Op1 = N->getOperand(1);
448 SDValue MLAOp1; // Will hold ordinary multiplicand for MLA.
449 SDValue MLAOp2; // Will hold lane-accessed multiplicand for MLA.
450 int LaneIdx = -1; // Will hold the lane index.
451
452 if (Op1.getOpcode() != ISD::MUL ||
453 !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
454 LaneIdx)) {
455 std::swap(Op0, Op1);
456 if (Op1.getOpcode() != ISD::MUL ||
457 !checkV64LaneV128(Op1.getOperand(0), Op1.getOperand(1), MLAOp1, MLAOp2,
458 LaneIdx))
459 return nullptr;
460 }
461
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000462 SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000463
464 SDValue Ops[] = { Op0, MLAOp1, MLAOp2, LaneIdxVal };
465
466 unsigned MLAOpc = ~0U;
467
468 switch (N->getSimpleValueType(0).SimpleTy) {
469 default:
470 llvm_unreachable("Unrecognized MLA.");
471 case MVT::v4i16:
472 MLAOpc = AArch64::MLAv4i16_indexed;
473 break;
474 case MVT::v8i16:
475 MLAOpc = AArch64::MLAv8i16_indexed;
476 break;
477 case MVT::v2i32:
478 MLAOpc = AArch64::MLAv2i32_indexed;
479 break;
480 case MVT::v4i32:
481 MLAOpc = AArch64::MLAv4i32_indexed;
482 break;
483 }
484
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000485 return CurDAG->getMachineNode(MLAOpc, dl, N->getValueType(0), Ops);
Tim Northover3b0846e2014-05-24 12:50:23 +0000486}
487
488SDNode *AArch64DAGToDAGISel::SelectMULLV64LaneV128(unsigned IntNo, SDNode *N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000489 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000490 SDValue SMULLOp0;
491 SDValue SMULLOp1;
492 int LaneIdx;
493
494 if (!checkV64LaneV128(N->getOperand(1), N->getOperand(2), SMULLOp0, SMULLOp1,
495 LaneIdx))
496 return nullptr;
497
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000498 SDValue LaneIdxVal = CurDAG->getTargetConstant(LaneIdx, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000499
500 SDValue Ops[] = { SMULLOp0, SMULLOp1, LaneIdxVal };
501
502 unsigned SMULLOpc = ~0U;
503
504 if (IntNo == Intrinsic::aarch64_neon_smull) {
505 switch (N->getSimpleValueType(0).SimpleTy) {
506 default:
507 llvm_unreachable("Unrecognized SMULL.");
508 case MVT::v4i32:
509 SMULLOpc = AArch64::SMULLv4i16_indexed;
510 break;
511 case MVT::v2i64:
512 SMULLOpc = AArch64::SMULLv2i32_indexed;
513 break;
514 }
515 } else if (IntNo == Intrinsic::aarch64_neon_umull) {
516 switch (N->getSimpleValueType(0).SimpleTy) {
517 default:
518 llvm_unreachable("Unrecognized SMULL.");
519 case MVT::v4i32:
520 SMULLOpc = AArch64::UMULLv4i16_indexed;
521 break;
522 case MVT::v2i64:
523 SMULLOpc = AArch64::UMULLv2i32_indexed;
524 break;
525 }
526 } else
527 llvm_unreachable("Unrecognized intrinsic.");
528
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000529 return CurDAG->getMachineNode(SMULLOpc, dl, N->getValueType(0), Ops);
Tim Northover3b0846e2014-05-24 12:50:23 +0000530}
531
532/// Instructions that accept extend modifiers like UXTW expect the register
533/// being extended to be a GPR32, but the incoming DAG might be acting on a
534/// GPR64 (either via SEXT_INREG or AND). Extract the appropriate low bits if
535/// this is the case.
536static SDValue narrowIfNeeded(SelectionDAG *CurDAG, SDValue N) {
537 if (N.getValueType() == MVT::i32)
538 return N;
539
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000540 SDLoc dl(N);
541 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000542 MachineSDNode *Node = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000543 dl, MVT::i32, N, SubReg);
Tim Northover3b0846e2014-05-24 12:50:23 +0000544 return SDValue(Node, 0);
545}
546
547
548/// SelectArithExtendedRegister - Select a "extended register" operand. This
549/// operand folds in an extend followed by an optional left shift.
550bool AArch64DAGToDAGISel::SelectArithExtendedRegister(SDValue N, SDValue &Reg,
551 SDValue &Shift) {
552 unsigned ShiftVal = 0;
553 AArch64_AM::ShiftExtendType Ext;
554
555 if (N.getOpcode() == ISD::SHL) {
556 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
557 if (!CSD)
558 return false;
559 ShiftVal = CSD->getZExtValue();
560 if (ShiftVal > 4)
561 return false;
562
563 Ext = getExtendTypeForNode(N.getOperand(0));
564 if (Ext == AArch64_AM::InvalidShiftExtend)
565 return false;
566
567 Reg = N.getOperand(0).getOperand(0);
568 } else {
569 Ext = getExtendTypeForNode(N);
570 if (Ext == AArch64_AM::InvalidShiftExtend)
571 return false;
572
573 Reg = N.getOperand(0);
574 }
575
576 // AArch64 mandates that the RHS of the operation must use the smallest
577 // register classs that could contain the size being extended from. Thus,
578 // if we're folding a (sext i8), we need the RHS to be a GPR32, even though
579 // there might not be an actual 32-bit value in the program. We can
580 // (harmlessly) synthesize one by injected an EXTRACT_SUBREG here.
581 assert(Ext != AArch64_AM::UXTX && Ext != AArch64_AM::SXTX);
582 Reg = narrowIfNeeded(CurDAG, Reg);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000583 Shift = CurDAG->getTargetConstant(getArithExtendImm(Ext, ShiftVal), SDLoc(N),
584 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000585 return isWorthFolding(N);
586}
587
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000588/// If there's a use of this ADDlow that's not itself a load/store then we'll
589/// need to create a real ADD instruction from it anyway and there's no point in
590/// folding it into the mem op. Theoretically, it shouldn't matter, but there's
591/// a single pseudo-instruction for an ADRP/ADD pair so over-aggressive folding
592/// leads to duplaicated ADRP instructions.
593static bool isWorthFoldingADDlow(SDValue N) {
594 for (auto Use : N->uses()) {
595 if (Use->getOpcode() != ISD::LOAD && Use->getOpcode() != ISD::STORE &&
596 Use->getOpcode() != ISD::ATOMIC_LOAD &&
597 Use->getOpcode() != ISD::ATOMIC_STORE)
598 return false;
599
600 // ldar and stlr have much more restrictive addressing modes (just a
601 // register).
602 if (cast<MemSDNode>(Use)->getOrdering() > Monotonic)
603 return false;
604 }
605
606 return true;
607}
608
Tim Northover3b0846e2014-05-24 12:50:23 +0000609/// SelectAddrModeIndexed - Select a "register plus scaled unsigned 12-bit
610/// immediate" address. The "Size" argument is the size in bytes of the memory
611/// reference, which determines the scale.
612bool AArch64DAGToDAGISel::SelectAddrModeIndexed(SDValue N, unsigned Size,
613 SDValue &Base, SDValue &OffImm) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000614 SDLoc dl(N);
Mehdi Amini44ede332015-07-09 02:09:04 +0000615 const DataLayout &DL = CurDAG->getDataLayout();
Tim Northover3b0846e2014-05-24 12:50:23 +0000616 const TargetLowering *TLI = getTargetLowering();
617 if (N.getOpcode() == ISD::FrameIndex) {
618 int FI = cast<FrameIndexSDNode>(N)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000619 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000620 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000621 return true;
622 }
623
Tim Northoverec7ebeb2014-12-02 23:13:39 +0000624 if (N.getOpcode() == AArch64ISD::ADDlow && isWorthFoldingADDlow(N)) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000625 GlobalAddressSDNode *GAN =
626 dyn_cast<GlobalAddressSDNode>(N.getOperand(1).getNode());
627 Base = N.getOperand(0);
628 OffImm = N.getOperand(1);
629 if (!GAN)
630 return true;
631
632 const GlobalValue *GV = GAN->getGlobal();
633 unsigned Alignment = GV->getAlignment();
Chad Rosier304fe3f2014-06-30 15:03:00 +0000634 Type *Ty = GV->getType()->getElementType();
Tim Northover4a8ac262014-12-02 23:53:43 +0000635 if (Alignment == 0 && Ty->isSized())
Mehdi Amini44ede332015-07-09 02:09:04 +0000636 Alignment = DL.getABITypeAlignment(Ty);
Tim Northover3b0846e2014-05-24 12:50:23 +0000637
638 if (Alignment >= Size)
639 return true;
640 }
641
642 if (CurDAG->isBaseWithConstantOffset(N)) {
643 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
644 int64_t RHSC = (int64_t)RHS->getZExtValue();
645 unsigned Scale = Log2_32(Size);
646 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 && RHSC < (0x1000 << Scale)) {
647 Base = N.getOperand(0);
648 if (Base.getOpcode() == ISD::FrameIndex) {
649 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
Mehdi Amini44ede332015-07-09 02:09:04 +0000650 Base = CurDAG->getTargetFrameIndex(FI, TLI->getPointerTy(DL));
Tim Northover3b0846e2014-05-24 12:50:23 +0000651 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000652 OffImm = CurDAG->getTargetConstant(RHSC >> Scale, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000653 return true;
654 }
655 }
656 }
657
658 // Before falling back to our general case, check if the unscaled
659 // instructions can handle this. If so, that's preferable.
660 if (SelectAddrModeUnscaled(N, Size, Base, OffImm))
661 return false;
662
663 // Base only. The address will be materialized into a register before
664 // the memory is accessed.
665 // add x0, Xbase, #offset
666 // ldr x0, [x0]
667 Base = N;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000668 OffImm = CurDAG->getTargetConstant(0, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000669 return true;
670}
671
672/// SelectAddrModeUnscaled - Select a "register plus unscaled signed 9-bit
673/// immediate" address. This should only match when there is an offset that
674/// is not valid for a scaled immediate addressing mode. The "Size" argument
675/// is the size in bytes of the memory reference, which is needed here to know
676/// what is valid for a scaled immediate.
677bool AArch64DAGToDAGISel::SelectAddrModeUnscaled(SDValue N, unsigned Size,
678 SDValue &Base,
679 SDValue &OffImm) {
680 if (!CurDAG->isBaseWithConstantOffset(N))
681 return false;
682 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
683 int64_t RHSC = RHS->getSExtValue();
684 // If the offset is valid as a scaled immediate, don't match here.
685 if ((RHSC & (Size - 1)) == 0 && RHSC >= 0 &&
686 RHSC < (0x1000 << Log2_32(Size)))
687 return false;
688 if (RHSC >= -256 && RHSC < 256) {
689 Base = N.getOperand(0);
690 if (Base.getOpcode() == ISD::FrameIndex) {
691 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
692 const TargetLowering *TLI = getTargetLowering();
Mehdi Amini44ede332015-07-09 02:09:04 +0000693 Base = CurDAG->getTargetFrameIndex(
694 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Tim Northover3b0846e2014-05-24 12:50:23 +0000695 }
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000696 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +0000697 return true;
698 }
699 }
700 return false;
701}
702
703static SDValue Widen(SelectionDAG *CurDAG, SDValue N) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000704 SDLoc dl(N);
705 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000706 SDValue ImpDef = SDValue(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000707 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, MVT::i64), 0);
Tim Northover3b0846e2014-05-24 12:50:23 +0000708 MachineSDNode *Node = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000709 TargetOpcode::INSERT_SUBREG, dl, MVT::i64, ImpDef, N, SubReg);
Tim Northover3b0846e2014-05-24 12:50:23 +0000710 return SDValue(Node, 0);
711}
712
713/// \brief Check if the given SHL node (\p N), can be used to form an
714/// extended register for an addressing mode.
715bool AArch64DAGToDAGISel::SelectExtendedSHL(SDValue N, unsigned Size,
716 bool WantExtend, SDValue &Offset,
717 SDValue &SignExtend) {
718 assert(N.getOpcode() == ISD::SHL && "Invalid opcode.");
719 ConstantSDNode *CSD = dyn_cast<ConstantSDNode>(N.getOperand(1));
720 if (!CSD || (CSD->getZExtValue() & 0x7) != CSD->getZExtValue())
721 return false;
722
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000723 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000724 if (WantExtend) {
725 AArch64_AM::ShiftExtendType Ext =
726 getExtendTypeForNode(N.getOperand(0), true);
727 if (Ext == AArch64_AM::InvalidShiftExtend)
728 return false;
729
730 Offset = narrowIfNeeded(CurDAG, N.getOperand(0).getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000731 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
732 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000733 } else {
734 Offset = N.getOperand(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000735 SignExtend = CurDAG->getTargetConstant(0, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000736 }
737
738 unsigned LegalShiftVal = Log2_32(Size);
739 unsigned ShiftVal = CSD->getZExtValue();
740
741 if (ShiftVal != 0 && ShiftVal != LegalShiftVal)
742 return false;
743
744 if (isWorthFolding(N))
745 return true;
746
747 return false;
748}
749
750bool AArch64DAGToDAGISel::SelectAddrModeWRO(SDValue N, unsigned Size,
751 SDValue &Base, SDValue &Offset,
752 SDValue &SignExtend,
753 SDValue &DoShift) {
754 if (N.getOpcode() != ISD::ADD)
755 return false;
756 SDValue LHS = N.getOperand(0);
757 SDValue RHS = N.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000758 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000759
760 // We don't want to match immediate adds here, because they are better lowered
761 // to the register-immediate addressing modes.
762 if (isa<ConstantSDNode>(LHS) || isa<ConstantSDNode>(RHS))
763 return false;
764
765 // Check if this particular node is reused in any non-memory related
766 // operation. If yes, do not try to fold this node into the address
767 // computation, since the computation will be kept.
768 const SDNode *Node = N.getNode();
769 for (SDNode *UI : Node->uses()) {
770 if (!isa<MemSDNode>(*UI))
771 return false;
772 }
773
774 // Remember if it is worth folding N when it produces extended register.
775 bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
776
777 // Try to match a shifted extend on the RHS.
778 if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
779 SelectExtendedSHL(RHS, Size, true, Offset, SignExtend)) {
780 Base = LHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000781 DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000782 return true;
783 }
784
785 // Try to match a shifted extend on the LHS.
786 if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
787 SelectExtendedSHL(LHS, Size, true, Offset, SignExtend)) {
788 Base = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000789 DoShift = CurDAG->getTargetConstant(true, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000790 return true;
791 }
792
793 // There was no shift, whatever else we find.
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000794 DoShift = CurDAG->getTargetConstant(false, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000795
796 AArch64_AM::ShiftExtendType Ext = AArch64_AM::InvalidShiftExtend;
797 // Try to match an unshifted extend on the LHS.
798 if (IsExtendedRegisterWorthFolding &&
799 (Ext = getExtendTypeForNode(LHS, true)) !=
800 AArch64_AM::InvalidShiftExtend) {
801 Base = RHS;
802 Offset = narrowIfNeeded(CurDAG, LHS.getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000803 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
804 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000805 if (isWorthFolding(LHS))
806 return true;
807 }
808
809 // Try to match an unshifted extend on the RHS.
810 if (IsExtendedRegisterWorthFolding &&
811 (Ext = getExtendTypeForNode(RHS, true)) !=
812 AArch64_AM::InvalidShiftExtend) {
813 Base = LHS;
814 Offset = narrowIfNeeded(CurDAG, RHS.getOperand(0));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000815 SignExtend = CurDAG->getTargetConstant(Ext == AArch64_AM::SXTW, dl,
816 MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000817 if (isWorthFolding(RHS))
818 return true;
819 }
820
821 return false;
822}
823
Hao Liu3cb826c2014-10-14 06:50:36 +0000824// Check if the given immediate is preferred by ADD. If an immediate can be
825// encoded in an ADD, or it can be encoded in an "ADD LSL #12" and can not be
826// encoded by one MOVZ, return true.
827static bool isPreferredADD(int64_t ImmOff) {
828 // Constant in [0x0, 0xfff] can be encoded in ADD.
829 if ((ImmOff & 0xfffffffffffff000LL) == 0x0LL)
830 return true;
831 // Check if it can be encoded in an "ADD LSL #12".
832 if ((ImmOff & 0xffffffffff000fffLL) == 0x0LL)
833 // As a single MOVZ is faster than a "ADD of LSL #12", ignore such constant.
834 return (ImmOff & 0xffffffffff00ffffLL) != 0x0LL &&
835 (ImmOff & 0xffffffffffff0fffLL) != 0x0LL;
836 return false;
837}
838
Tim Northover3b0846e2014-05-24 12:50:23 +0000839bool AArch64DAGToDAGISel::SelectAddrModeXRO(SDValue N, unsigned Size,
840 SDValue &Base, SDValue &Offset,
841 SDValue &SignExtend,
842 SDValue &DoShift) {
843 if (N.getOpcode() != ISD::ADD)
844 return false;
845 SDValue LHS = N.getOperand(0);
846 SDValue RHS = N.getOperand(1);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000847 SDLoc DL(N);
Tim Northover3b0846e2014-05-24 12:50:23 +0000848
Tim Northover3b0846e2014-05-24 12:50:23 +0000849 // Check if this particular node is reused in any non-memory related
850 // operation. If yes, do not try to fold this node into the address
851 // computation, since the computation will be kept.
852 const SDNode *Node = N.getNode();
853 for (SDNode *UI : Node->uses()) {
854 if (!isa<MemSDNode>(*UI))
855 return false;
856 }
857
Hao Liu3cb826c2014-10-14 06:50:36 +0000858 // Watch out if RHS is a wide immediate, it can not be selected into
859 // [BaseReg+Imm] addressing mode. Also it may not be able to be encoded into
860 // ADD/SUB. Instead it will use [BaseReg + 0] address mode and generate
861 // instructions like:
862 // MOV X0, WideImmediate
863 // ADD X1, BaseReg, X0
864 // LDR X2, [X1, 0]
865 // For such situation, using [BaseReg, XReg] addressing mode can save one
866 // ADD/SUB:
867 // MOV X0, WideImmediate
868 // LDR X2, [BaseReg, X0]
869 if (isa<ConstantSDNode>(RHS)) {
Benjamin Kramer619c4e52015-04-10 11:24:51 +0000870 int64_t ImmOff = (int64_t)cast<ConstantSDNode>(RHS)->getZExtValue();
Hao Liu3cb826c2014-10-14 06:50:36 +0000871 unsigned Scale = Log2_32(Size);
872 // Skip the immediate can be seleced by load/store addressing mode.
873 // Also skip the immediate can be encoded by a single ADD (SUB is also
874 // checked by using -ImmOff).
875 if ((ImmOff % Size == 0 && ImmOff >= 0 && ImmOff < (0x1000 << Scale)) ||
876 isPreferredADD(ImmOff) || isPreferredADD(-ImmOff))
877 return false;
878
Hao Liu3cb826c2014-10-14 06:50:36 +0000879 SDValue Ops[] = { RHS };
880 SDNode *MOVI =
881 CurDAG->getMachineNode(AArch64::MOVi64imm, DL, MVT::i64, Ops);
882 SDValue MOVIV = SDValue(MOVI, 0);
883 // This ADD of two X register will be selected into [Reg+Reg] mode.
884 N = CurDAG->getNode(ISD::ADD, DL, MVT::i64, LHS, MOVIV);
885 }
886
Tim Northover3b0846e2014-05-24 12:50:23 +0000887 // Remember if it is worth folding N when it produces extended register.
888 bool IsExtendedRegisterWorthFolding = isWorthFolding(N);
889
890 // Try to match a shifted extend on the RHS.
891 if (IsExtendedRegisterWorthFolding && RHS.getOpcode() == ISD::SHL &&
892 SelectExtendedSHL(RHS, Size, false, Offset, SignExtend)) {
893 Base = LHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000894 DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000895 return true;
896 }
897
898 // Try to match a shifted extend on the LHS.
899 if (IsExtendedRegisterWorthFolding && LHS.getOpcode() == ISD::SHL &&
900 SelectExtendedSHL(LHS, Size, false, Offset, SignExtend)) {
901 Base = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000902 DoShift = CurDAG->getTargetConstant(true, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000903 return true;
904 }
905
906 // Match any non-shifted, non-extend, non-immediate add expression.
907 Base = LHS;
908 Offset = RHS;
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000909 SignExtend = CurDAG->getTargetConstant(false, DL, MVT::i32);
910 DoShift = CurDAG->getTargetConstant(false, DL, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +0000911 // Reg1 + Reg2 is free: no check needed.
912 return true;
913}
914
915SDValue AArch64DAGToDAGISel::createDTuple(ArrayRef<SDValue> Regs) {
Benjamin Kramerea68a942015-02-19 15:26:17 +0000916 static const unsigned RegClassIDs[] = {
Tim Northover3b0846e2014-05-24 12:50:23 +0000917 AArch64::DDRegClassID, AArch64::DDDRegClassID, AArch64::DDDDRegClassID};
Benjamin Kramerea68a942015-02-19 15:26:17 +0000918 static const unsigned SubRegs[] = {AArch64::dsub0, AArch64::dsub1,
919 AArch64::dsub2, AArch64::dsub3};
Tim Northover3b0846e2014-05-24 12:50:23 +0000920
921 return createTuple(Regs, RegClassIDs, SubRegs);
922}
923
924SDValue AArch64DAGToDAGISel::createQTuple(ArrayRef<SDValue> Regs) {
Benjamin Kramerea68a942015-02-19 15:26:17 +0000925 static const unsigned RegClassIDs[] = {
Tim Northover3b0846e2014-05-24 12:50:23 +0000926 AArch64::QQRegClassID, AArch64::QQQRegClassID, AArch64::QQQQRegClassID};
Benjamin Kramerea68a942015-02-19 15:26:17 +0000927 static const unsigned SubRegs[] = {AArch64::qsub0, AArch64::qsub1,
928 AArch64::qsub2, AArch64::qsub3};
Tim Northover3b0846e2014-05-24 12:50:23 +0000929
930 return createTuple(Regs, RegClassIDs, SubRegs);
931}
932
933SDValue AArch64DAGToDAGISel::createTuple(ArrayRef<SDValue> Regs,
Benjamin Kramerea68a942015-02-19 15:26:17 +0000934 const unsigned RegClassIDs[],
935 const unsigned SubRegs[]) {
Tim Northover3b0846e2014-05-24 12:50:23 +0000936 // There's no special register-class for a vector-list of 1 element: it's just
937 // a vector.
938 if (Regs.size() == 1)
939 return Regs[0];
940
941 assert(Regs.size() >= 2 && Regs.size() <= 4);
942
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000943 SDLoc DL(Regs[0]);
Tim Northover3b0846e2014-05-24 12:50:23 +0000944
945 SmallVector<SDValue, 4> Ops;
946
947 // First operand of REG_SEQUENCE is the desired RegClass.
948 Ops.push_back(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000949 CurDAG->getTargetConstant(RegClassIDs[Regs.size() - 2], DL, MVT::i32));
Tim Northover3b0846e2014-05-24 12:50:23 +0000950
951 // Then we get pairs of source & subregister-position for the components.
952 for (unsigned i = 0; i < Regs.size(); ++i) {
953 Ops.push_back(Regs[i]);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +0000954 Ops.push_back(CurDAG->getTargetConstant(SubRegs[i], DL, MVT::i32));
Tim Northover3b0846e2014-05-24 12:50:23 +0000955 }
956
957 SDNode *N =
958 CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, MVT::Untyped, Ops);
959 return SDValue(N, 0);
960}
961
962SDNode *AArch64DAGToDAGISel::SelectTable(SDNode *N, unsigned NumVecs,
963 unsigned Opc, bool isExt) {
964 SDLoc dl(N);
965 EVT VT = N->getValueType(0);
966
967 unsigned ExtOff = isExt;
968
969 // Form a REG_SEQUENCE to force register allocation.
970 unsigned Vec0Off = ExtOff + 1;
971 SmallVector<SDValue, 4> Regs(N->op_begin() + Vec0Off,
972 N->op_begin() + Vec0Off + NumVecs);
973 SDValue RegSeq = createQTuple(Regs);
974
975 SmallVector<SDValue, 6> Ops;
976 if (isExt)
977 Ops.push_back(N->getOperand(1));
978 Ops.push_back(RegSeq);
979 Ops.push_back(N->getOperand(NumVecs + ExtOff + 1));
980 return CurDAG->getMachineNode(Opc, dl, VT, Ops);
981}
982
983SDNode *AArch64DAGToDAGISel::SelectIndexedLoad(SDNode *N, bool &Done) {
984 LoadSDNode *LD = cast<LoadSDNode>(N);
985 if (LD->isUnindexed())
986 return nullptr;
987 EVT VT = LD->getMemoryVT();
988 EVT DstVT = N->getValueType(0);
989 ISD::MemIndexedMode AM = LD->getAddressingMode();
990 bool IsPre = AM == ISD::PRE_INC || AM == ISD::PRE_DEC;
991
992 // We're not doing validity checking here. That was done when checking
993 // if we should mark the load as indexed or not. We're just selecting
994 // the right instruction.
995 unsigned Opcode = 0;
996
997 ISD::LoadExtType ExtType = LD->getExtensionType();
998 bool InsertTo64 = false;
999 if (VT == MVT::i64)
1000 Opcode = IsPre ? AArch64::LDRXpre : AArch64::LDRXpost;
1001 else if (VT == MVT::i32) {
1002 if (ExtType == ISD::NON_EXTLOAD)
1003 Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
1004 else if (ExtType == ISD::SEXTLOAD)
1005 Opcode = IsPre ? AArch64::LDRSWpre : AArch64::LDRSWpost;
1006 else {
1007 Opcode = IsPre ? AArch64::LDRWpre : AArch64::LDRWpost;
1008 InsertTo64 = true;
1009 // The result of the load is only i32. It's the subreg_to_reg that makes
1010 // it into an i64.
1011 DstVT = MVT::i32;
1012 }
1013 } else if (VT == MVT::i16) {
1014 if (ExtType == ISD::SEXTLOAD) {
1015 if (DstVT == MVT::i64)
1016 Opcode = IsPre ? AArch64::LDRSHXpre : AArch64::LDRSHXpost;
1017 else
1018 Opcode = IsPre ? AArch64::LDRSHWpre : AArch64::LDRSHWpost;
1019 } else {
1020 Opcode = IsPre ? AArch64::LDRHHpre : AArch64::LDRHHpost;
1021 InsertTo64 = DstVT == MVT::i64;
1022 // The result of the load is only i32. It's the subreg_to_reg that makes
1023 // it into an i64.
1024 DstVT = MVT::i32;
1025 }
1026 } else if (VT == MVT::i8) {
1027 if (ExtType == ISD::SEXTLOAD) {
1028 if (DstVT == MVT::i64)
1029 Opcode = IsPre ? AArch64::LDRSBXpre : AArch64::LDRSBXpost;
1030 else
1031 Opcode = IsPre ? AArch64::LDRSBWpre : AArch64::LDRSBWpost;
1032 } else {
1033 Opcode = IsPre ? AArch64::LDRBBpre : AArch64::LDRBBpost;
1034 InsertTo64 = DstVT == MVT::i64;
1035 // The result of the load is only i32. It's the subreg_to_reg that makes
1036 // it into an i64.
1037 DstVT = MVT::i32;
1038 }
Ahmed Bougachae0e12db2015-08-04 01:29:38 +00001039 } else if (VT == MVT::f16) {
1040 Opcode = IsPre ? AArch64::LDRHpre : AArch64::LDRHpost;
Tim Northover3b0846e2014-05-24 12:50:23 +00001041 } else if (VT == MVT::f32) {
1042 Opcode = IsPre ? AArch64::LDRSpre : AArch64::LDRSpost;
1043 } else if (VT == MVT::f64 || VT.is64BitVector()) {
1044 Opcode = IsPre ? AArch64::LDRDpre : AArch64::LDRDpost;
1045 } else if (VT.is128BitVector()) {
1046 Opcode = IsPre ? AArch64::LDRQpre : AArch64::LDRQpost;
1047 } else
1048 return nullptr;
1049 SDValue Chain = LD->getChain();
1050 SDValue Base = LD->getBasePtr();
1051 ConstantSDNode *OffsetOp = cast<ConstantSDNode>(LD->getOffset());
1052 int OffsetVal = (int)OffsetOp->getZExtValue();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001053 SDLoc dl(N);
1054 SDValue Offset = CurDAG->getTargetConstant(OffsetVal, dl, MVT::i64);
Tim Northover3b0846e2014-05-24 12:50:23 +00001055 SDValue Ops[] = { Base, Offset, Chain };
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001056 SDNode *Res = CurDAG->getMachineNode(Opcode, dl, MVT::i64, DstVT,
Tim Northover3b0846e2014-05-24 12:50:23 +00001057 MVT::Other, Ops);
1058 // Either way, we're replacing the node, so tell the caller that.
1059 Done = true;
1060 SDValue LoadedVal = SDValue(Res, 1);
1061 if (InsertTo64) {
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001062 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +00001063 LoadedVal =
1064 SDValue(CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001065 AArch64::SUBREG_TO_REG, dl, MVT::i64,
1066 CurDAG->getTargetConstant(0, dl, MVT::i64), LoadedVal,
1067 SubReg),
Tim Northover3b0846e2014-05-24 12:50:23 +00001068 0);
1069 }
1070
1071 ReplaceUses(SDValue(N, 0), LoadedVal);
1072 ReplaceUses(SDValue(N, 1), SDValue(Res, 0));
1073 ReplaceUses(SDValue(N, 2), SDValue(Res, 2));
1074
1075 return nullptr;
1076}
1077
1078SDNode *AArch64DAGToDAGISel::SelectLoad(SDNode *N, unsigned NumVecs,
1079 unsigned Opc, unsigned SubRegIdx) {
1080 SDLoc dl(N);
1081 EVT VT = N->getValueType(0);
1082 SDValue Chain = N->getOperand(0);
1083
Benjamin Kramerea68a942015-02-19 15:26:17 +00001084 SDValue Ops[] = {N->getOperand(2), // Mem operand;
1085 Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00001086
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001087 const EVT ResTys[] = {MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001088
1089 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1090 SDValue SuperReg = SDValue(Ld, 0);
1091 for (unsigned i = 0; i < NumVecs; ++i)
1092 ReplaceUses(SDValue(N, i),
1093 CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
1094
1095 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
1096 return nullptr;
1097}
1098
1099SDNode *AArch64DAGToDAGISel::SelectPostLoad(SDNode *N, unsigned NumVecs,
1100 unsigned Opc, unsigned SubRegIdx) {
1101 SDLoc dl(N);
1102 EVT VT = N->getValueType(0);
1103 SDValue Chain = N->getOperand(0);
1104
Benjamin Kramerea68a942015-02-19 15:26:17 +00001105 SDValue Ops[] = {N->getOperand(1), // Mem operand
1106 N->getOperand(2), // Incremental
1107 Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00001108
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001109 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1110 MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001111
1112 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1113
1114 // Update uses of write back register
1115 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1116
1117 // Update uses of vector list
1118 SDValue SuperReg = SDValue(Ld, 1);
1119 if (NumVecs == 1)
1120 ReplaceUses(SDValue(N, 0), SuperReg);
1121 else
1122 for (unsigned i = 0; i < NumVecs; ++i)
1123 ReplaceUses(SDValue(N, i),
1124 CurDAG->getTargetExtractSubreg(SubRegIdx + i, dl, VT, SuperReg));
1125
1126 // Update the chain
1127 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
1128 return nullptr;
1129}
1130
1131SDNode *AArch64DAGToDAGISel::SelectStore(SDNode *N, unsigned NumVecs,
1132 unsigned Opc) {
1133 SDLoc dl(N);
1134 EVT VT = N->getOperand(2)->getValueType(0);
1135
1136 // Form a REG_SEQUENCE to force register allocation.
1137 bool Is128Bit = VT.getSizeInBits() == 128;
1138 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1139 SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1140
Benjamin Kramerea68a942015-02-19 15:26:17 +00001141 SDValue Ops[] = {RegSeq, N->getOperand(NumVecs + 2), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001142 SDNode *St = CurDAG->getMachineNode(Opc, dl, N->getValueType(0), Ops);
1143
1144 return St;
1145}
1146
1147SDNode *AArch64DAGToDAGISel::SelectPostStore(SDNode *N, unsigned NumVecs,
1148 unsigned Opc) {
1149 SDLoc dl(N);
1150 EVT VT = N->getOperand(2)->getValueType(0);
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001151 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1152 MVT::Other}; // Type for the Chain
Tim Northover3b0846e2014-05-24 12:50:23 +00001153
1154 // Form a REG_SEQUENCE to force register allocation.
1155 bool Is128Bit = VT.getSizeInBits() == 128;
1156 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1157 SDValue RegSeq = Is128Bit ? createQTuple(Regs) : createDTuple(Regs);
1158
Benjamin Kramerea68a942015-02-19 15:26:17 +00001159 SDValue Ops[] = {RegSeq,
1160 N->getOperand(NumVecs + 1), // base register
1161 N->getOperand(NumVecs + 2), // Incremental
1162 N->getOperand(0)}; // Chain
Tim Northover3b0846e2014-05-24 12:50:23 +00001163 SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1164
1165 return St;
1166}
1167
Benjamin Kramer51f6096c2015-03-23 12:30:58 +00001168namespace {
Tim Northover3b0846e2014-05-24 12:50:23 +00001169/// WidenVector - Given a value in the V64 register class, produce the
1170/// equivalent value in the V128 register class.
1171class WidenVector {
1172 SelectionDAG &DAG;
1173
1174public:
1175 WidenVector(SelectionDAG &DAG) : DAG(DAG) {}
1176
1177 SDValue operator()(SDValue V64Reg) {
1178 EVT VT = V64Reg.getValueType();
1179 unsigned NarrowSize = VT.getVectorNumElements();
1180 MVT EltTy = VT.getVectorElementType().getSimpleVT();
1181 MVT WideTy = MVT::getVectorVT(EltTy, 2 * NarrowSize);
1182 SDLoc DL(V64Reg);
1183
1184 SDValue Undef =
1185 SDValue(DAG.getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, WideTy), 0);
1186 return DAG.getTargetInsertSubreg(AArch64::dsub, DL, WideTy, Undef, V64Reg);
1187 }
1188};
Benjamin Kramer51f6096c2015-03-23 12:30:58 +00001189} // namespace
Tim Northover3b0846e2014-05-24 12:50:23 +00001190
1191/// NarrowVector - Given a value in the V128 register class, produce the
1192/// equivalent value in the V64 register class.
1193static SDValue NarrowVector(SDValue V128Reg, SelectionDAG &DAG) {
1194 EVT VT = V128Reg.getValueType();
1195 unsigned WideSize = VT.getVectorNumElements();
1196 MVT EltTy = VT.getVectorElementType().getSimpleVT();
1197 MVT NarrowTy = MVT::getVectorVT(EltTy, WideSize / 2);
1198
1199 return DAG.getTargetExtractSubreg(AArch64::dsub, SDLoc(V128Reg), NarrowTy,
1200 V128Reg);
1201}
1202
1203SDNode *AArch64DAGToDAGISel::SelectLoadLane(SDNode *N, unsigned NumVecs,
1204 unsigned Opc) {
1205 SDLoc dl(N);
1206 EVT VT = N->getValueType(0);
1207 bool Narrow = VT.getSizeInBits() == 64;
1208
1209 // Form a REG_SEQUENCE to force register allocation.
1210 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1211
1212 if (Narrow)
1213 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1214 WidenVector(*CurDAG));
1215
1216 SDValue RegSeq = createQTuple(Regs);
1217
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001218 const EVT ResTys[] = {MVT::Untyped, MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001219
1220 unsigned LaneNo =
1221 cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1222
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001223 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001224 N->getOperand(NumVecs + 3), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001225 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1226 SDValue SuperReg = SDValue(Ld, 0);
1227
1228 EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
1229 static unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1, AArch64::qsub2,
1230 AArch64::qsub3 };
1231 for (unsigned i = 0; i < NumVecs; ++i) {
1232 SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT, SuperReg);
1233 if (Narrow)
1234 NV = NarrowVector(NV, *CurDAG);
1235 ReplaceUses(SDValue(N, i), NV);
1236 }
1237
1238 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 1));
1239
1240 return Ld;
1241}
1242
1243SDNode *AArch64DAGToDAGISel::SelectPostLoadLane(SDNode *N, unsigned NumVecs,
1244 unsigned Opc) {
1245 SDLoc dl(N);
1246 EVT VT = N->getValueType(0);
1247 bool Narrow = VT.getSizeInBits() == 64;
1248
1249 // Form a REG_SEQUENCE to force register allocation.
1250 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1251
1252 if (Narrow)
1253 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1254 WidenVector(*CurDAG));
1255
1256 SDValue RegSeq = createQTuple(Regs);
1257
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001258 const EVT ResTys[] = {MVT::i64, // Type of the write back register
Ahmed Bougachae14a4d42015-04-17 23:43:33 +00001259 RegSeq->getValueType(0), MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001260
1261 unsigned LaneNo =
1262 cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1263
Benjamin Kramerea68a942015-02-19 15:26:17 +00001264 SDValue Ops[] = {RegSeq,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001265 CurDAG->getTargetConstant(LaneNo, dl,
1266 MVT::i64), // Lane Number
Benjamin Kramerea68a942015-02-19 15:26:17 +00001267 N->getOperand(NumVecs + 2), // Base register
1268 N->getOperand(NumVecs + 3), // Incremental
1269 N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001270 SDNode *Ld = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1271
1272 // Update uses of the write back register
1273 ReplaceUses(SDValue(N, NumVecs), SDValue(Ld, 0));
1274
1275 // Update uses of the vector list
1276 SDValue SuperReg = SDValue(Ld, 1);
1277 if (NumVecs == 1) {
1278 ReplaceUses(SDValue(N, 0),
1279 Narrow ? NarrowVector(SuperReg, *CurDAG) : SuperReg);
1280 } else {
1281 EVT WideVT = RegSeq.getOperand(1)->getValueType(0);
1282 static unsigned QSubs[] = { AArch64::qsub0, AArch64::qsub1, AArch64::qsub2,
1283 AArch64::qsub3 };
1284 for (unsigned i = 0; i < NumVecs; ++i) {
1285 SDValue NV = CurDAG->getTargetExtractSubreg(QSubs[i], dl, WideVT,
1286 SuperReg);
1287 if (Narrow)
1288 NV = NarrowVector(NV, *CurDAG);
1289 ReplaceUses(SDValue(N, i), NV);
1290 }
1291 }
1292
1293 // Update the Chain
1294 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(Ld, 2));
1295
1296 return Ld;
1297}
1298
1299SDNode *AArch64DAGToDAGISel::SelectStoreLane(SDNode *N, unsigned NumVecs,
1300 unsigned Opc) {
1301 SDLoc dl(N);
1302 EVT VT = N->getOperand(2)->getValueType(0);
1303 bool Narrow = VT.getSizeInBits() == 64;
1304
1305 // Form a REG_SEQUENCE to force register allocation.
1306 SmallVector<SDValue, 4> Regs(N->op_begin() + 2, N->op_begin() + 2 + NumVecs);
1307
1308 if (Narrow)
1309 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1310 WidenVector(*CurDAG));
1311
1312 SDValue RegSeq = createQTuple(Regs);
1313
1314 unsigned LaneNo =
1315 cast<ConstantSDNode>(N->getOperand(NumVecs + 2))->getZExtValue();
1316
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001317 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001318 N->getOperand(NumVecs + 3), N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001319 SDNode *St = CurDAG->getMachineNode(Opc, dl, MVT::Other, Ops);
1320
1321 // Transfer memoperands.
1322 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1323 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1324 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1325
1326 return St;
1327}
1328
1329SDNode *AArch64DAGToDAGISel::SelectPostStoreLane(SDNode *N, unsigned NumVecs,
1330 unsigned Opc) {
1331 SDLoc dl(N);
1332 EVT VT = N->getOperand(2)->getValueType(0);
1333 bool Narrow = VT.getSizeInBits() == 64;
1334
1335 // Form a REG_SEQUENCE to force register allocation.
1336 SmallVector<SDValue, 4> Regs(N->op_begin() + 1, N->op_begin() + 1 + NumVecs);
1337
1338 if (Narrow)
1339 std::transform(Regs.begin(), Regs.end(), Regs.begin(),
1340 WidenVector(*CurDAG));
1341
1342 SDValue RegSeq = createQTuple(Regs);
1343
Benjamin Kramer867bfc52015-03-07 17:41:00 +00001344 const EVT ResTys[] = {MVT::i64, // Type of the write back register
1345 MVT::Other};
Tim Northover3b0846e2014-05-24 12:50:23 +00001346
1347 unsigned LaneNo =
1348 cast<ConstantSDNode>(N->getOperand(NumVecs + 1))->getZExtValue();
1349
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001350 SDValue Ops[] = {RegSeq, CurDAG->getTargetConstant(LaneNo, dl, MVT::i64),
Benjamin Kramerea68a942015-02-19 15:26:17 +00001351 N->getOperand(NumVecs + 2), // Base Register
1352 N->getOperand(NumVecs + 3), // Incremental
1353 N->getOperand(0)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001354 SDNode *St = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1355
1356 // Transfer memoperands.
1357 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1358 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1359 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
1360
1361 return St;
1362}
1363
1364static bool isBitfieldExtractOpFromAnd(SelectionDAG *CurDAG, SDNode *N,
1365 unsigned &Opc, SDValue &Opd0,
1366 unsigned &LSB, unsigned &MSB,
1367 unsigned NumberOfIgnoredLowBits,
1368 bool BiggerPattern) {
1369 assert(N->getOpcode() == ISD::AND &&
1370 "N must be a AND operation to call this function");
1371
1372 EVT VT = N->getValueType(0);
1373
1374 // Here we can test the type of VT and return false when the type does not
1375 // match, but since it is done prior to that call in the current context
1376 // we turned that into an assert to avoid redundant code.
1377 assert((VT == MVT::i32 || VT == MVT::i64) &&
1378 "Type checking must have been done before calling this function");
1379
1380 // FIXME: simplify-demanded-bits in DAGCombine will probably have
1381 // changed the AND node to a 32-bit mask operation. We'll have to
1382 // undo that as part of the transform here if we want to catch all
1383 // the opportunities.
1384 // Currently the NumberOfIgnoredLowBits argument helps to recover
1385 // form these situations when matching bigger pattern (bitfield insert).
1386
1387 // For unsigned extracts, check for a shift right and mask
1388 uint64_t And_imm = 0;
1389 if (!isOpcWithIntImmediate(N, ISD::AND, And_imm))
1390 return false;
1391
1392 const SDNode *Op0 = N->getOperand(0).getNode();
1393
1394 // Because of simplify-demanded-bits in DAGCombine, the mask may have been
1395 // simplified. Try to undo that
1396 And_imm |= (1 << NumberOfIgnoredLowBits) - 1;
1397
1398 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
1399 if (And_imm & (And_imm + 1))
1400 return false;
1401
1402 bool ClampMSB = false;
1403 uint64_t Srl_imm = 0;
1404 // Handle the SRL + ANY_EXTEND case.
1405 if (VT == MVT::i64 && Op0->getOpcode() == ISD::ANY_EXTEND &&
1406 isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL, Srl_imm)) {
1407 // Extend the incoming operand of the SRL to 64-bit.
1408 Opd0 = Widen(CurDAG, Op0->getOperand(0).getOperand(0));
1409 // Make sure to clamp the MSB so that we preserve the semantics of the
1410 // original operations.
1411 ClampMSB = true;
1412 } else if (VT == MVT::i32 && Op0->getOpcode() == ISD::TRUNCATE &&
1413 isOpcWithIntImmediate(Op0->getOperand(0).getNode(), ISD::SRL,
1414 Srl_imm)) {
1415 // If the shift result was truncated, we can still combine them.
1416 Opd0 = Op0->getOperand(0).getOperand(0);
1417
1418 // Use the type of SRL node.
1419 VT = Opd0->getValueType(0);
1420 } else if (isOpcWithIntImmediate(Op0, ISD::SRL, Srl_imm)) {
1421 Opd0 = Op0->getOperand(0);
1422 } else if (BiggerPattern) {
1423 // Let's pretend a 0 shift right has been performed.
1424 // The resulting code will be at least as good as the original one
1425 // plus it may expose more opportunities for bitfield insert pattern.
1426 // FIXME: Currently we limit this to the bigger pattern, because
1427 // some optimizations expect AND and not UBFM
1428 Opd0 = N->getOperand(0);
1429 } else
1430 return false;
1431
Matthias Braun75260352015-02-24 18:52:04 +00001432 // Bail out on large immediates. This happens when no proper
1433 // combining/constant folding was performed.
Matthias Braun02892ec2015-02-25 18:03:50 +00001434 if (!BiggerPattern && (Srl_imm <= 0 || Srl_imm >= VT.getSizeInBits())) {
1435 DEBUG((dbgs() << N
1436 << ": Found large shift immediate, this should not happen\n"));
Matthias Braun75260352015-02-24 18:52:04 +00001437 return false;
Matthias Braun02892ec2015-02-25 18:03:50 +00001438 }
Tim Northover3b0846e2014-05-24 12:50:23 +00001439
1440 LSB = Srl_imm;
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00001441 MSB = Srl_imm + (VT == MVT::i32 ? countTrailingOnes<uint32_t>(And_imm)
1442 : countTrailingOnes<uint64_t>(And_imm)) -
Tim Northover3b0846e2014-05-24 12:50:23 +00001443 1;
1444 if (ClampMSB)
1445 // Since we're moving the extend before the right shift operation, we need
1446 // to clamp the MSB to make sure we don't shift in undefined bits instead of
1447 // the zeros which would get shifted in with the original right shift
1448 // operation.
1449 MSB = MSB > 31 ? 31 : MSB;
1450
1451 Opc = VT == MVT::i32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1452 return true;
1453}
1454
David Xu052b9d92014-09-02 09:33:56 +00001455static bool isSeveralBitsExtractOpFromShr(SDNode *N, unsigned &Opc,
1456 SDValue &Opd0, unsigned &LSB,
1457 unsigned &MSB) {
1458 // We are looking for the following pattern which basically extracts several
1459 // continuous bits from the source value and places it from the LSB of the
1460 // destination value, all other bits of the destination value or set to zero:
Tim Northover3b0846e2014-05-24 12:50:23 +00001461 //
1462 // Value2 = AND Value, MaskImm
1463 // SRL Value2, ShiftImm
1464 //
David Xu052b9d92014-09-02 09:33:56 +00001465 // with MaskImm >> ShiftImm to search for the bit width.
Tim Northover3b0846e2014-05-24 12:50:23 +00001466 //
1467 // This gets selected into a single UBFM:
1468 //
David Xu052b9d92014-09-02 09:33:56 +00001469 // UBFM Value, ShiftImm, BitWide + Srl_imm -1
Tim Northover3b0846e2014-05-24 12:50:23 +00001470 //
1471
1472 if (N->getOpcode() != ISD::SRL)
1473 return false;
1474
1475 uint64_t And_mask = 0;
1476 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, And_mask))
1477 return false;
1478
1479 Opd0 = N->getOperand(0).getOperand(0);
1480
1481 uint64_t Srl_imm = 0;
1482 if (!isIntImmediate(N->getOperand(1), Srl_imm))
1483 return false;
1484
David Xu052b9d92014-09-02 09:33:56 +00001485 // Check whether we really have several bits extract here.
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00001486 unsigned BitWide = 64 - countLeadingOnes(~(And_mask >> Srl_imm));
David Xu052b9d92014-09-02 09:33:56 +00001487 if (BitWide && isMask_64(And_mask >> Srl_imm)) {
Tim Northover3b0846e2014-05-24 12:50:23 +00001488 if (N->getValueType(0) == MVT::i32)
1489 Opc = AArch64::UBFMWri;
1490 else
1491 Opc = AArch64::UBFMXri;
1492
David Xu052b9d92014-09-02 09:33:56 +00001493 LSB = Srl_imm;
1494 MSB = BitWide + Srl_imm - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001495 return true;
1496 }
1497
1498 return false;
1499}
1500
1501static bool isBitfieldExtractOpFromShr(SDNode *N, unsigned &Opc, SDValue &Opd0,
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001502 unsigned &Immr, unsigned &Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001503 bool BiggerPattern) {
1504 assert((N->getOpcode() == ISD::SRA || N->getOpcode() == ISD::SRL) &&
1505 "N must be a SHR/SRA operation to call this function");
1506
1507 EVT VT = N->getValueType(0);
1508
1509 // Here we can test the type of VT and return false when the type does not
1510 // match, but since it is done prior to that call in the current context
1511 // we turned that into an assert to avoid redundant code.
1512 assert((VT == MVT::i32 || VT == MVT::i64) &&
1513 "Type checking must have been done before calling this function");
1514
David Xu052b9d92014-09-02 09:33:56 +00001515 // Check for AND + SRL doing several bits extract.
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001516 if (isSeveralBitsExtractOpFromShr(N, Opc, Opd0, Immr, Imms))
Tim Northover3b0846e2014-05-24 12:50:23 +00001517 return true;
1518
1519 // we're looking for a shift of a shift
1520 uint64_t Shl_imm = 0;
1521 uint64_t Trunc_bits = 0;
1522 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
1523 Opd0 = N->getOperand(0).getOperand(0);
1524 } else if (VT == MVT::i32 && N->getOpcode() == ISD::SRL &&
1525 N->getOperand(0).getNode()->getOpcode() == ISD::TRUNCATE) {
1526 // We are looking for a shift of truncate. Truncate from i64 to i32 could
1527 // be considered as setting high 32 bits as zero. Our strategy here is to
1528 // always generate 64bit UBFM. This consistency will help the CSE pass
1529 // later find more redundancy.
1530 Opd0 = N->getOperand(0).getOperand(0);
1531 Trunc_bits = Opd0->getValueType(0).getSizeInBits() - VT.getSizeInBits();
1532 VT = Opd0->getValueType(0);
1533 assert(VT == MVT::i64 && "the promoted type should be i64");
1534 } else if (BiggerPattern) {
1535 // Let's pretend a 0 shift left has been performed.
1536 // FIXME: Currently we limit this to the bigger pattern case,
1537 // because some optimizations expect AND and not UBFM
1538 Opd0 = N->getOperand(0);
1539 } else
1540 return false;
1541
Matthias Braun75260352015-02-24 18:52:04 +00001542 // Missing combines/constant folding may have left us with strange
1543 // constants.
Matthias Braun02892ec2015-02-25 18:03:50 +00001544 if (Shl_imm >= VT.getSizeInBits()) {
1545 DEBUG((dbgs() << N
1546 << ": Found large shift immediate, this should not happen\n"));
Matthias Braun75260352015-02-24 18:52:04 +00001547 return false;
Matthias Braun02892ec2015-02-25 18:03:50 +00001548 }
Matthias Braun75260352015-02-24 18:52:04 +00001549
Tim Northover3b0846e2014-05-24 12:50:23 +00001550 uint64_t Srl_imm = 0;
1551 if (!isIntImmediate(N->getOperand(1), Srl_imm))
1552 return false;
1553
1554 assert(Srl_imm > 0 && Srl_imm < VT.getSizeInBits() &&
1555 "bad amount in shift node!");
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001556 int immr = Srl_imm - Shl_imm;
1557 Immr = immr < 0 ? immr + VT.getSizeInBits() : immr;
1558 Imms = VT.getSizeInBits() - Shl_imm - Trunc_bits - 1;
Tim Northover3b0846e2014-05-24 12:50:23 +00001559 // SRA requires a signed extraction
1560 if (VT == MVT::i32)
1561 Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMWri : AArch64::UBFMWri;
1562 else
1563 Opc = N->getOpcode() == ISD::SRA ? AArch64::SBFMXri : AArch64::UBFMXri;
1564 return true;
1565}
1566
1567static bool isBitfieldExtractOp(SelectionDAG *CurDAG, SDNode *N, unsigned &Opc,
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001568 SDValue &Opd0, unsigned &Immr, unsigned &Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001569 unsigned NumberOfIgnoredLowBits = 0,
1570 bool BiggerPattern = false) {
1571 if (N->getValueType(0) != MVT::i32 && N->getValueType(0) != MVT::i64)
1572 return false;
1573
1574 switch (N->getOpcode()) {
1575 default:
1576 if (!N->isMachineOpcode())
1577 return false;
1578 break;
1579 case ISD::AND:
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001580 return isBitfieldExtractOpFromAnd(CurDAG, N, Opc, Opd0, Immr, Imms,
Tim Northover3b0846e2014-05-24 12:50:23 +00001581 NumberOfIgnoredLowBits, BiggerPattern);
1582 case ISD::SRL:
1583 case ISD::SRA:
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001584 return isBitfieldExtractOpFromShr(N, Opc, Opd0, Immr, Imms, BiggerPattern);
Tim Northover3b0846e2014-05-24 12:50:23 +00001585 }
1586
1587 unsigned NOpc = N->getMachineOpcode();
1588 switch (NOpc) {
1589 default:
1590 return false;
1591 case AArch64::SBFMWri:
1592 case AArch64::UBFMWri:
1593 case AArch64::SBFMXri:
1594 case AArch64::UBFMXri:
1595 Opc = NOpc;
1596 Opd0 = N->getOperand(0);
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001597 Immr = cast<ConstantSDNode>(N->getOperand(1).getNode())->getZExtValue();
1598 Imms = cast<ConstantSDNode>(N->getOperand(2).getNode())->getZExtValue();
Tim Northover3b0846e2014-05-24 12:50:23 +00001599 return true;
1600 }
1601 // Unreachable
1602 return false;
1603}
1604
1605SDNode *AArch64DAGToDAGISel::SelectBitfieldExtractOp(SDNode *N) {
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001606 unsigned Opc, Immr, Imms;
Tim Northover3b0846e2014-05-24 12:50:23 +00001607 SDValue Opd0;
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001608 if (!isBitfieldExtractOp(CurDAG, N, Opc, Opd0, Immr, Imms))
Tim Northover3b0846e2014-05-24 12:50:23 +00001609 return nullptr;
1610
1611 EVT VT = N->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001612 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00001613
1614 // If the bit extract operation is 64bit but the original type is 32bit, we
1615 // need to add one EXTRACT_SUBREG.
1616 if ((Opc == AArch64::SBFMXri || Opc == AArch64::UBFMXri) && VT == MVT::i32) {
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001617 SDValue Ops64[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, MVT::i64),
1618 CurDAG->getTargetConstant(Imms, dl, MVT::i64)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001619
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001620 SDNode *BFM = CurDAG->getMachineNode(Opc, dl, MVT::i64, Ops64);
1621 SDValue SubReg = CurDAG->getTargetConstant(AArch64::sub_32, dl, MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +00001622 MachineSDNode *Node =
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001623 CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, dl, MVT::i32,
Tim Northover3b0846e2014-05-24 12:50:23 +00001624 SDValue(BFM, 0), SubReg);
1625 return Node;
1626 }
1627
Arnaud A. de Grandmaisonf40f99e2015-07-09 14:33:38 +00001628 SDValue Ops[] = {Opd0, CurDAG->getTargetConstant(Immr, dl, VT),
1629 CurDAG->getTargetConstant(Imms, dl, VT)};
Tim Northover3b0846e2014-05-24 12:50:23 +00001630 return CurDAG->SelectNodeTo(N, Opc, VT, Ops);
1631}
1632
1633/// Does DstMask form a complementary pair with the mask provided by
1634/// BitsToBeInserted, suitable for use in a BFI instruction. Roughly speaking,
1635/// this asks whether DstMask zeroes precisely those bits that will be set by
1636/// the other half.
1637static bool isBitfieldDstMask(uint64_t DstMask, APInt BitsToBeInserted,
1638 unsigned NumberOfIgnoredHighBits, EVT VT) {
1639 assert((VT == MVT::i32 || VT == MVT::i64) &&
1640 "i32 or i64 mask type expected!");
1641 unsigned BitWidth = VT.getSizeInBits() - NumberOfIgnoredHighBits;
1642
1643 APInt SignificantDstMask = APInt(BitWidth, DstMask);
1644 APInt SignificantBitsToBeInserted = BitsToBeInserted.zextOrTrunc(BitWidth);
1645
1646 return (SignificantDstMask & SignificantBitsToBeInserted) == 0 &&
1647 (SignificantDstMask | SignificantBitsToBeInserted).isAllOnesValue();
1648}
1649
1650// Look for bits that will be useful for later uses.
1651// A bit is consider useless as soon as it is dropped and never used
1652// before it as been dropped.
1653// E.g., looking for useful bit of x
1654// 1. y = x & 0x7
1655// 2. z = y >> 2
1656// After #1, x useful bits are 0x7, then the useful bits of x, live through
1657// y.
1658// After #2, the useful bits of x are 0x4.
1659// However, if x is used on an unpredicatable instruction, then all its bits
1660// are useful.
1661// E.g.
1662// 1. y = x & 0x7
1663// 2. z = y >> 2
1664// 3. str x, [@x]
1665static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth = 0);
1666
1667static void getUsefulBitsFromAndWithImmediate(SDValue Op, APInt &UsefulBits,
1668 unsigned Depth) {
1669 uint64_t Imm =
1670 cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1671 Imm = AArch64_AM::decodeLogicalImmediate(Imm, UsefulBits.getBitWidth());
1672 UsefulBits &= APInt(UsefulBits.getBitWidth(), Imm);
1673 getUsefulBits(Op, UsefulBits, Depth + 1);
1674}
1675
1676static void getUsefulBitsFromBitfieldMoveOpd(SDValue Op, APInt &UsefulBits,
1677 uint64_t Imm, uint64_t MSB,
1678 unsigned Depth) {
1679 // inherit the bitwidth value
1680 APInt OpUsefulBits(UsefulBits);
1681 OpUsefulBits = 1;
1682
1683 if (MSB >= Imm) {
1684 OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1685 --OpUsefulBits;
1686 // The interesting part will be in the lower part of the result
1687 getUsefulBits(Op, OpUsefulBits, Depth + 1);
1688 // The interesting part was starting at Imm in the argument
1689 OpUsefulBits = OpUsefulBits.shl(Imm);
1690 } else {
1691 OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1692 --OpUsefulBits;
1693 // The interesting part will be shifted in the result
1694 OpUsefulBits = OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm);
1695 getUsefulBits(Op, OpUsefulBits, Depth + 1);
1696 // The interesting part was at zero in the argument
1697 OpUsefulBits = OpUsefulBits.lshr(OpUsefulBits.getBitWidth() - Imm);
1698 }
1699
1700 UsefulBits &= OpUsefulBits;
1701}
1702
1703static void getUsefulBitsFromUBFM(SDValue Op, APInt &UsefulBits,
1704 unsigned Depth) {
1705 uint64_t Imm =
1706 cast<const ConstantSDNode>(Op.getOperand(1).getNode())->getZExtValue();
1707 uint64_t MSB =
1708 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1709
1710 getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1711}
1712
1713static void getUsefulBitsFromOrWithShiftedReg(SDValue Op, APInt &UsefulBits,
1714 unsigned Depth) {
1715 uint64_t ShiftTypeAndValue =
1716 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1717 APInt Mask(UsefulBits);
1718 Mask.clearAllBits();
1719 Mask.flipAllBits();
1720
1721 if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSL) {
1722 // Shift Left
1723 uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1724 Mask = Mask.shl(ShiftAmt);
1725 getUsefulBits(Op, Mask, Depth + 1);
1726 Mask = Mask.lshr(ShiftAmt);
1727 } else if (AArch64_AM::getShiftType(ShiftTypeAndValue) == AArch64_AM::LSR) {
1728 // Shift Right
1729 // We do not handle AArch64_AM::ASR, because the sign will change the
1730 // number of useful bits
1731 uint64_t ShiftAmt = AArch64_AM::getShiftValue(ShiftTypeAndValue);
1732 Mask = Mask.lshr(ShiftAmt);
1733 getUsefulBits(Op, Mask, Depth + 1);
1734 Mask = Mask.shl(ShiftAmt);
1735 } else
1736 return;
1737
1738 UsefulBits &= Mask;
1739}
1740
1741static void getUsefulBitsFromBFM(SDValue Op, SDValue Orig, APInt &UsefulBits,
1742 unsigned Depth) {
1743 uint64_t Imm =
1744 cast<const ConstantSDNode>(Op.getOperand(2).getNode())->getZExtValue();
1745 uint64_t MSB =
1746 cast<const ConstantSDNode>(Op.getOperand(3).getNode())->getZExtValue();
1747
1748 if (Op.getOperand(1) == Orig)
1749 return getUsefulBitsFromBitfieldMoveOpd(Op, UsefulBits, Imm, MSB, Depth);
1750
1751 APInt OpUsefulBits(UsefulBits);
1752 OpUsefulBits = 1;
1753
1754 if (MSB >= Imm) {
1755 OpUsefulBits = OpUsefulBits.shl(MSB - Imm + 1);
1756 --OpUsefulBits;
1757 UsefulBits &= ~OpUsefulBits;
1758 getUsefulBits(Op, UsefulBits, Depth + 1);
1759 } else {
1760 OpUsefulBits = OpUsefulBits.shl(MSB + 1);
1761 --OpUsefulBits;
1762 UsefulBits = ~(OpUsefulBits.shl(OpUsefulBits.getBitWidth() - Imm));
1763 getUsefulBits(Op, UsefulBits, Depth + 1);
1764 }
1765}
1766
1767static void getUsefulBitsForUse(SDNode *UserNode, APInt &UsefulBits,
1768 SDValue Orig, unsigned Depth) {
1769
1770 // Users of this node should have already been instruction selected
1771 // FIXME: Can we turn that into an assert?
1772 if (!UserNode->isMachineOpcode())
1773 return;
1774
1775 switch (UserNode->getMachineOpcode()) {
1776 default:
1777 return;
1778 case AArch64::ANDSWri:
1779 case AArch64::ANDSXri:
1780 case AArch64::ANDWri:
1781 case AArch64::ANDXri:
1782 // We increment Depth only when we call the getUsefulBits
1783 return getUsefulBitsFromAndWithImmediate(SDValue(UserNode, 0), UsefulBits,
1784 Depth);
1785 case AArch64::UBFMWri:
1786 case AArch64::UBFMXri:
1787 return getUsefulBitsFromUBFM(SDValue(UserNode, 0), UsefulBits, Depth);
1788
1789 case AArch64::ORRWrs:
1790 case AArch64::ORRXrs:
1791 if (UserNode->getOperand(1) != Orig)
1792 return;
1793 return getUsefulBitsFromOrWithShiftedReg(SDValue(UserNode, 0), UsefulBits,
1794 Depth);
1795 case AArch64::BFMWri:
1796 case AArch64::BFMXri:
1797 return getUsefulBitsFromBFM(SDValue(UserNode, 0), Orig, UsefulBits, Depth);
1798 }
1799}
1800
1801static void getUsefulBits(SDValue Op, APInt &UsefulBits, unsigned Depth) {
1802 if (Depth >= 6)
1803 return;
1804 // Initialize UsefulBits
1805 if (!Depth) {
1806 unsigned Bitwidth = Op.getValueType().getScalarType().getSizeInBits();
1807 // At the beginning, assume every produced bits is useful
1808 UsefulBits = APInt(Bitwidth, 0);
1809 UsefulBits.flipAllBits();
1810 }
1811 APInt UsersUsefulBits(UsefulBits.getBitWidth(), 0);
1812
1813 for (SDNode *Node : Op.getNode()->uses()) {
1814 // A use cannot produce useful bits
1815 APInt UsefulBitsForUse = APInt(UsefulBits);
1816 getUsefulBitsForUse(Node, UsefulBitsForUse, Op, Depth);
1817 UsersUsefulBits |= UsefulBitsForUse;
1818 }
1819 // UsefulBits contains the produced bits that are meaningful for the
1820 // current definition, thus a user cannot make a bit meaningful at
1821 // this point
1822 UsefulBits &= UsersUsefulBits;
1823}
1824
1825/// Create a machine node performing a notional SHL of Op by ShlAmount. If
1826/// ShlAmount is negative, do a (logical) right-shift instead. If ShlAmount is
1827/// 0, return Op unchanged.
1828static SDValue getLeftShift(SelectionDAG *CurDAG, SDValue Op, int ShlAmount) {
1829 if (ShlAmount == 0)
1830 return Op;
1831
1832 EVT VT = Op.getValueType();
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001833 SDLoc dl(Op);
Tim Northover3b0846e2014-05-24 12:50:23 +00001834 unsigned BitWidth = VT.getSizeInBits();
1835 unsigned UBFMOpc = BitWidth == 32 ? AArch64::UBFMWri : AArch64::UBFMXri;
1836
1837 SDNode *ShiftNode;
1838 if (ShlAmount > 0) {
1839 // LSL wD, wN, #Amt == UBFM wD, wN, #32-Amt, #31-Amt
1840 ShiftNode = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001841 UBFMOpc, dl, VT, Op,
1842 CurDAG->getTargetConstant(BitWidth - ShlAmount, dl, VT),
1843 CurDAG->getTargetConstant(BitWidth - 1 - ShlAmount, dl, VT));
Tim Northover3b0846e2014-05-24 12:50:23 +00001844 } else {
1845 // LSR wD, wN, #Amt == UBFM wD, wN, #Amt, #32-1
1846 assert(ShlAmount < 0 && "expected right shift");
1847 int ShrAmount = -ShlAmount;
1848 ShiftNode = CurDAG->getMachineNode(
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00001849 UBFMOpc, dl, VT, Op, CurDAG->getTargetConstant(ShrAmount, dl, VT),
1850 CurDAG->getTargetConstant(BitWidth - 1, dl, VT));
Tim Northover3b0846e2014-05-24 12:50:23 +00001851 }
1852
1853 return SDValue(ShiftNode, 0);
1854}
1855
1856/// Does this tree qualify as an attempt to move a bitfield into position,
1857/// essentially "(and (shl VAL, N), Mask)".
1858static bool isBitfieldPositioningOp(SelectionDAG *CurDAG, SDValue Op,
1859 SDValue &Src, int &ShiftAmount,
1860 int &MaskWidth) {
1861 EVT VT = Op.getValueType();
1862 unsigned BitWidth = VT.getSizeInBits();
1863 (void)BitWidth;
1864 assert(BitWidth == 32 || BitWidth == 64);
1865
1866 APInt KnownZero, KnownOne;
1867 CurDAG->computeKnownBits(Op, KnownZero, KnownOne);
1868
1869 // Non-zero in the sense that they're not provably zero, which is the key
1870 // point if we want to use this value
1871 uint64_t NonZeroBits = (~KnownZero).getZExtValue();
1872
1873 // Discard a constant AND mask if present. It's safe because the node will
1874 // already have been factored into the computeKnownBits calculation above.
1875 uint64_t AndImm;
1876 if (isOpcWithIntImmediate(Op.getNode(), ISD::AND, AndImm)) {
1877 assert((~APInt(BitWidth, AndImm) & ~KnownZero) == 0);
1878 Op = Op.getOperand(0);
1879 }
1880
1881 uint64_t ShlImm;
1882 if (!isOpcWithIntImmediate(Op.getNode(), ISD::SHL, ShlImm))
1883 return false;
1884 Op = Op.getOperand(0);
1885
1886 if (!isShiftedMask_64(NonZeroBits))
1887 return false;
1888
1889 ShiftAmount = countTrailingZeros(NonZeroBits);
Benjamin Kramer5f6a9072015-02-12 15:35:40 +00001890 MaskWidth = countTrailingOnes(NonZeroBits >> ShiftAmount);
Tim Northover3b0846e2014-05-24 12:50:23 +00001891
1892 // BFI encompasses sufficiently many nodes that it's worth inserting an extra
1893 // LSL/LSR if the mask in NonZeroBits doesn't quite match up with the ISD::SHL
1894 // amount.
1895 Src = getLeftShift(CurDAG, Op, ShlImm - ShiftAmount);
1896
1897 return true;
1898}
1899
1900// Given a OR operation, check if we have the following pattern
1901// ubfm c, b, imm, imm2 (or something that does the same jobs, see
1902// isBitfieldExtractOp)
1903// d = e & mask2 ; where mask is a binary sequence of 1..10..0 and
1904// countTrailingZeros(mask2) == imm2 - imm + 1
1905// f = d | c
1906// if yes, given reference arguments will be update so that one can replace
1907// the OR instruction with:
1908// f = Opc Opd0, Opd1, LSB, MSB ; where Opc is a BFM, LSB = imm, and MSB = imm2
1909static bool isBitfieldInsertOpFromOr(SDNode *N, unsigned &Opc, SDValue &Dst,
1910 SDValue &Src, unsigned &ImmR,
1911 unsigned &ImmS, SelectionDAG *CurDAG) {
1912 assert(N->getOpcode() == ISD::OR && "Expect a OR operation");
1913
1914 // Set Opc
1915 EVT VT = N->getValueType(0);
1916 if (VT == MVT::i32)
1917 Opc = AArch64::BFMWri;
1918 else if (VT == MVT::i64)
1919 Opc = AArch64::BFMXri;
1920 else
1921 return false;
1922
1923 // Because of simplify-demanded-bits in DAGCombine, involved masks may not
1924 // have the expected shape. Try to undo that.
1925 APInt UsefulBits;
1926 getUsefulBits(SDValue(N, 0), UsefulBits);
1927
1928 unsigned NumberOfIgnoredLowBits = UsefulBits.countTrailingZeros();
1929 unsigned NumberOfIgnoredHighBits = UsefulBits.countLeadingZeros();
1930
1931 // OR is commutative, check both possibilities (does llvm provide a
1932 // way to do that directely, e.g., via code matcher?)
1933 SDValue OrOpd1Val = N->getOperand(1);
1934 SDNode *OrOpd0 = N->getOperand(0).getNode();
1935 SDNode *OrOpd1 = N->getOperand(1).getNode();
1936 for (int i = 0; i < 2;
1937 ++i, std::swap(OrOpd0, OrOpd1), OrOpd1Val = N->getOperand(0)) {
1938 unsigned BFXOpc;
1939 int DstLSB, Width;
1940 if (isBitfieldExtractOp(CurDAG, OrOpd0, BFXOpc, Src, ImmR, ImmS,
1941 NumberOfIgnoredLowBits, true)) {
1942 // Check that the returned opcode is compatible with the pattern,
1943 // i.e., same type and zero extended (U and not S)
1944 if ((BFXOpc != AArch64::UBFMXri && VT == MVT::i64) ||
1945 (BFXOpc != AArch64::UBFMWri && VT == MVT::i32))
1946 continue;
1947
1948 // Compute the width of the bitfield insertion
1949 DstLSB = 0;
1950 Width = ImmS - ImmR + 1;
1951 // FIXME: This constraint is to catch bitfield insertion we may
1952 // want to widen the pattern if we want to grab general bitfied
1953 // move case
1954 if (Width <= 0)
1955 continue;
1956
1957 // If the mask on the insertee is correct, we have a BFXIL operation. We
1958 // can share the ImmR and ImmS values from the already-computed UBFM.
1959 } else if (isBitfieldPositioningOp(CurDAG, SDValue(OrOpd0, 0), Src,
1960 DstLSB, Width)) {
1961 ImmR = (VT.getSizeInBits() - DstLSB) % VT.getSizeInBits();
1962 ImmS = Width - 1;
1963 } else
1964 continue;
1965
1966 // Check the second part of the pattern
1967 EVT VT = OrOpd1->getValueType(0);
1968 assert((VT == MVT::i32 || VT == MVT::i64) && "unexpected OR operand");
1969
1970 // Compute the Known Zero for the candidate of the first operand.
1971 // This allows to catch more general case than just looking for
1972 // AND with imm. Indeed, simplify-demanded-bits may have removed
1973 // the AND instruction because it proves it was useless.
1974 APInt KnownZero, KnownOne;
1975 CurDAG->computeKnownBits(OrOpd1Val, KnownZero, KnownOne);
1976
1977 // Check if there is enough room for the second operand to appear
1978 // in the first one
1979 APInt BitsToBeInserted =
1980 APInt::getBitsSet(KnownZero.getBitWidth(), DstLSB, DstLSB + Width);
1981
1982 if ((BitsToBeInserted & ~KnownZero) != 0)
1983 continue;
1984
1985 // Set the first operand
1986 uint64_t Imm;
1987 if (isOpcWithIntImmediate(OrOpd1, ISD::AND, Imm) &&
1988 isBitfieldDstMask(Imm, BitsToBeInserted, NumberOfIgnoredHighBits, VT))
1989 // In that case, we can eliminate the AND
1990 Dst = OrOpd1->getOperand(0);
1991 else
1992 // Maybe the AND has been removed by simplify-demanded-bits
1993 // or is useful because it discards more bits
1994 Dst = OrOpd1Val;
1995
1996 // both parts match
1997 return true;
1998 }
1999
2000 return false;
2001}
2002
2003SDNode *AArch64DAGToDAGISel::SelectBitfieldInsertOp(SDNode *N) {
2004 if (N->getOpcode() != ISD::OR)
2005 return nullptr;
2006
2007 unsigned Opc;
2008 unsigned LSB, MSB;
2009 SDValue Opd0, Opd1;
2010
2011 if (!isBitfieldInsertOpFromOr(N, Opc, Opd0, Opd1, LSB, MSB, CurDAG))
2012 return nullptr;
2013
2014 EVT VT = N->getValueType(0);
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002015 SDLoc dl(N);
Tim Northover3b0846e2014-05-24 12:50:23 +00002016 SDValue Ops[] = { Opd0,
2017 Opd1,
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002018 CurDAG->getTargetConstant(LSB, dl, VT),
2019 CurDAG->getTargetConstant(MSB, dl, VT) };
Tim Northover3b0846e2014-05-24 12:50:23 +00002020 return CurDAG->SelectNodeTo(N, Opc, VT, Ops);
2021}
2022
Geoff Berryc573bf7a2015-07-28 15:24:10 +00002023/// GenerateInexactFlagIfNeeded - Insert FRINTX instruction to generate inexact
2024/// signal on round-to-integer operations if needed. C11 leaves it
2025/// implementation-defined whether these operations trigger an inexact
2026/// exception. IEEE says they don't. Unfortunately, Darwin decided they do so
2027/// we sometimes have to insert a special instruction just to set the right bit
2028/// in FPSR.
2029SDNode *AArch64DAGToDAGISel::GenerateInexactFlagIfNeeded(const SDValue &In,
2030 unsigned InTyVariant,
2031 SDLoc DL) {
2032 if (Subtarget->isTargetDarwin() && !TM.Options.UnsafeFPMath) {
2033 // Pick the right FRINTX using InTyVariant needed to set the flags.
2034 // InTyVariant is 0 for 32-bit and 1 for 64-bit.
2035 unsigned FRINTXOpcs[] = { AArch64::FRINTXSr, AArch64::FRINTXDr };
2036 return CurDAG->getMachineNode(FRINTXOpcs[InTyVariant], DL,
2037 In.getValueType(), MVT::Glue, In);
2038 }
2039 return nullptr;
2040}
2041
Tim Northover3b0846e2014-05-24 12:50:23 +00002042SDNode *AArch64DAGToDAGISel::SelectLIBM(SDNode *N) {
2043 EVT VT = N->getValueType(0);
2044 unsigned Variant;
2045 unsigned Opc;
Tim Northover3b0846e2014-05-24 12:50:23 +00002046
2047 if (VT == MVT::f32) {
2048 Variant = 0;
2049 } else if (VT == MVT::f64) {
2050 Variant = 1;
2051 } else
2052 return nullptr; // Unrecognized argument type. Fall back on default codegen.
2053
Tim Northover3b0846e2014-05-24 12:50:23 +00002054 switch (N->getOpcode()) {
2055 default:
2056 return nullptr; // Unrecognized libm ISD node. Fall back on default codegen.
2057 case ISD::FCEIL: {
2058 unsigned FRINTPOpcs[] = { AArch64::FRINTPSr, AArch64::FRINTPDr };
2059 Opc = FRINTPOpcs[Variant];
2060 break;
2061 }
2062 case ISD::FFLOOR: {
2063 unsigned FRINTMOpcs[] = { AArch64::FRINTMSr, AArch64::FRINTMDr };
2064 Opc = FRINTMOpcs[Variant];
2065 break;
2066 }
2067 case ISD::FTRUNC: {
2068 unsigned FRINTZOpcs[] = { AArch64::FRINTZSr, AArch64::FRINTZDr };
2069 Opc = FRINTZOpcs[Variant];
2070 break;
2071 }
2072 case ISD::FROUND: {
2073 unsigned FRINTAOpcs[] = { AArch64::FRINTASr, AArch64::FRINTADr };
2074 Opc = FRINTAOpcs[Variant];
2075 break;
2076 }
2077 }
2078
2079 SDLoc dl(N);
2080 SDValue In = N->getOperand(0);
2081 SmallVector<SDValue, 2> Ops;
2082 Ops.push_back(In);
2083
Geoff Berryc573bf7a2015-07-28 15:24:10 +00002084 if (SDNode *FRINTXNode = GenerateInexactFlagIfNeeded(In, Variant, dl))
2085 Ops.push_back(SDValue(FRINTXNode, 1));
Tim Northover3b0846e2014-05-24 12:50:23 +00002086
2087 return CurDAG->getMachineNode(Opc, dl, VT, Ops);
2088}
2089
Geoff Berryc573bf7a2015-07-28 15:24:10 +00002090/// SelectFPConvertWithRound - Try to combine FP rounding and
2091/// FP-INT conversion.
2092SDNode *AArch64DAGToDAGISel::SelectFPConvertWithRound(SDNode *N) {
2093 SDNode *Op0 = N->getOperand(0).getNode();
2094
2095 // Return if the round op is used by other nodes, as this would result in two
2096 // FRINTX, one each for round and convert.
2097 if (!Op0->hasOneUse())
2098 return nullptr;
2099
2100 unsigned InTyVariant;
2101 EVT InTy = Op0->getValueType(0);
2102 if (InTy == MVT::f32)
2103 InTyVariant = 0;
2104 else if (InTy == MVT::f64)
2105 InTyVariant = 1;
2106 else
2107 return nullptr;
2108
2109 unsigned OutTyVariant;
2110 EVT OutTy = N->getValueType(0);
2111 if (OutTy == MVT::i32)
2112 OutTyVariant = 0;
2113 else if (OutTy == MVT::i64)
2114 OutTyVariant = 1;
2115 else
2116 return nullptr;
2117
2118 assert((N->getOpcode() == ISD::FP_TO_SINT
2119 || N->getOpcode() == ISD::FP_TO_UINT) && "Unexpected opcode!");
2120 unsigned FpConVariant = N->getOpcode() == ISD::FP_TO_SINT ? 0 : 1;
2121
2122 unsigned Opc;
2123 switch (Op0->getOpcode()) {
2124 default:
2125 return nullptr;
2126 case ISD::FCEIL: {
2127 unsigned FCVTPOpcs[2][2][2] = {
2128 { { AArch64::FCVTPSUWSr, AArch64::FCVTPSUXSr },
2129 { AArch64::FCVTPSUWDr, AArch64::FCVTPSUXDr } },
2130 { { AArch64::FCVTPUUWSr, AArch64::FCVTPUUXSr },
2131 { AArch64::FCVTPUUWDr, AArch64::FCVTPUUXDr } } };
2132 Opc = FCVTPOpcs[FpConVariant][InTyVariant][OutTyVariant];
2133 break;
2134 }
2135 case ISD::FFLOOR: {
2136 unsigned FCVTMOpcs[2][2][2] = {
2137 { { AArch64::FCVTMSUWSr, AArch64::FCVTMSUXSr },
2138 { AArch64::FCVTMSUWDr, AArch64::FCVTMSUXDr } },
2139 { { AArch64::FCVTMUUWSr, AArch64::FCVTMUUXSr },
2140 { AArch64::FCVTMUUWDr, AArch64::FCVTMUUXDr } } };
2141 Opc = FCVTMOpcs[FpConVariant][InTyVariant][OutTyVariant];
2142 break;
2143 }
2144 case ISD::FTRUNC: {
2145 unsigned FCVTZOpcs[2][2][2] = {
2146 { { AArch64::FCVTZSUWSr, AArch64::FCVTZSUXSr },
2147 { AArch64::FCVTZSUWDr, AArch64::FCVTZSUXDr } },
2148 { { AArch64::FCVTZUUWSr, AArch64::FCVTZUUXSr },
2149 { AArch64::FCVTZUUWDr, AArch64::FCVTZUUXDr } } };
2150 Opc = FCVTZOpcs[FpConVariant][InTyVariant][OutTyVariant];
2151 break;
2152 }
2153 case ISD::FROUND: {
2154 unsigned FCVTAOpcs[2][2][2] = {
2155 { { AArch64::FCVTASUWSr, AArch64::FCVTASUXSr },
2156 { AArch64::FCVTASUWDr, AArch64::FCVTASUXDr } },
2157 { { AArch64::FCVTAUUWSr, AArch64::FCVTAUUXSr },
2158 { AArch64::FCVTAUUWDr, AArch64::FCVTAUUXDr } } };
2159 Opc = FCVTAOpcs[FpConVariant][InTyVariant][OutTyVariant];
2160 break;
2161 }
2162 }
2163
2164 SDLoc DL(N);
2165 SDValue In = Op0->getOperand(0);
2166 SmallVector<SDValue, 2> Ops;
2167 Ops.push_back(In);
2168
2169 if (SDNode *FRINTXNode = GenerateInexactFlagIfNeeded(In, InTyVariant, DL))
2170 Ops.push_back(SDValue(FRINTXNode, 1));
2171
2172 return CurDAG->getMachineNode(Opc, DL, OutTy, Ops);
2173}
2174
Tim Northover3b0846e2014-05-24 12:50:23 +00002175bool
2176AArch64DAGToDAGISel::SelectCVTFixedPosOperand(SDValue N, SDValue &FixedPos,
2177 unsigned RegWidth) {
2178 APFloat FVal(0.0);
2179 if (ConstantFPSDNode *CN = dyn_cast<ConstantFPSDNode>(N))
2180 FVal = CN->getValueAPF();
2181 else if (LoadSDNode *LN = dyn_cast<LoadSDNode>(N)) {
2182 // Some otherwise illegal constants are allowed in this case.
2183 if (LN->getOperand(1).getOpcode() != AArch64ISD::ADDlow ||
2184 !isa<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1)))
2185 return false;
2186
2187 ConstantPoolSDNode *CN =
2188 dyn_cast<ConstantPoolSDNode>(LN->getOperand(1)->getOperand(1));
2189 FVal = cast<ConstantFP>(CN->getConstVal())->getValueAPF();
2190 } else
2191 return false;
2192
2193 // An FCVT[SU] instruction performs: convertToInt(Val * 2^fbits) where fbits
2194 // is between 1 and 32 for a destination w-register, or 1 and 64 for an
2195 // x-register.
2196 //
2197 // By this stage, we've detected (fp_to_[su]int (fmul Val, THIS_NODE)) so we
2198 // want THIS_NODE to be 2^fbits. This is much easier to deal with using
2199 // integers.
2200 bool IsExact;
2201
2202 // fbits is between 1 and 64 in the worst-case, which means the fmul
2203 // could have 2^64 as an actual operand. Need 65 bits of precision.
2204 APSInt IntVal(65, true);
2205 FVal.convertToInteger(IntVal, APFloat::rmTowardZero, &IsExact);
2206
2207 // N.b. isPowerOf2 also checks for > 0.
2208 if (!IsExact || !IntVal.isPowerOf2()) return false;
2209 unsigned FBits = IntVal.logBase2();
2210
2211 // Checks above should have guaranteed that we haven't lost information in
2212 // finding FBits, but it must still be in range.
2213 if (FBits == 0 || FBits > RegWidth) return false;
2214
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002215 FixedPos = CurDAG->getTargetConstant(FBits, SDLoc(N), MVT::i32);
Tim Northover3b0846e2014-05-24 12:50:23 +00002216 return true;
2217}
2218
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002219// Inspects a register string of the form o0:op1:CRn:CRm:op2 gets the fields
2220// of the string and obtains the integer values from them and combines these
2221// into a single value to be used in the MRS/MSR instruction.
2222static int getIntOperandFromRegisterString(StringRef RegString) {
2223 SmallVector<StringRef, 5> Fields;
2224 RegString.split(Fields, ":");
2225
2226 if (Fields.size() == 1)
2227 return -1;
2228
2229 assert(Fields.size() == 5
2230 && "Invalid number of fields in read register string");
2231
2232 SmallVector<int, 5> Ops;
2233 bool AllIntFields = true;
2234
2235 for (StringRef Field : Fields) {
2236 unsigned IntField;
2237 AllIntFields &= !Field.getAsInteger(10, IntField);
2238 Ops.push_back(IntField);
2239 }
2240
2241 assert(AllIntFields &&
2242 "Unexpected non-integer value in special register string.");
2243
2244 // Need to combine the integer fields of the string into a single value
2245 // based on the bit encoding of MRS/MSR instruction.
2246 return (Ops[0] << 14) | (Ops[1] << 11) | (Ops[2] << 7) |
2247 (Ops[3] << 3) | (Ops[4]);
2248}
2249
2250// Lower the read_register intrinsic to an MRS instruction node if the special
2251// register string argument is either of the form detailed in the ALCE (the
2252// form described in getIntOperandsFromRegsterString) or is a named register
2253// known by the MRS SysReg mapper.
2254SDNode *AArch64DAGToDAGISel::SelectReadRegister(SDNode *N) {
2255 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2256 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2257 SDLoc DL(N);
2258
2259 int Reg = getIntOperandFromRegisterString(RegString->getString());
2260 if (Reg != -1)
2261 return CurDAG->getMachineNode(AArch64::MRS, DL, N->getSimpleValueType(0),
2262 MVT::Other,
2263 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2264 N->getOperand(0));
2265
2266 // Use the sysreg mapper to map the remaining possible strings to the
2267 // value for the register to be used for the instruction operand.
2268 AArch64SysReg::MRSMapper mapper;
2269 bool IsValidSpecialReg;
2270 Reg = mapper.fromString(RegString->getString(),
2271 Subtarget->getFeatureBits(),
2272 IsValidSpecialReg);
2273 if (IsValidSpecialReg)
2274 return CurDAG->getMachineNode(AArch64::MRS, DL, N->getSimpleValueType(0),
2275 MVT::Other,
2276 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2277 N->getOperand(0));
2278
2279 return nullptr;
2280}
2281
2282// Lower the write_register intrinsic to an MSR instruction node if the special
2283// register string argument is either of the form detailed in the ALCE (the
2284// form described in getIntOperandsFromRegsterString) or is a named register
2285// known by the MSR SysReg mapper.
2286SDNode *AArch64DAGToDAGISel::SelectWriteRegister(SDNode *N) {
2287 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
2288 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
2289 SDLoc DL(N);
2290
2291 int Reg = getIntOperandFromRegisterString(RegString->getString());
2292 if (Reg != -1)
2293 return CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
2294 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2295 N->getOperand(2), N->getOperand(0));
2296
2297 // Check if the register was one of those allowed as the pstatefield value in
2298 // the MSR (immediate) instruction. To accept the values allowed in the
2299 // pstatefield for the MSR (immediate) instruction, we also require that an
2300 // immediate value has been provided as an argument, we know that this is
2301 // the case as it has been ensured by semantic checking.
2302 AArch64PState::PStateMapper PMapper;
2303 bool IsValidSpecialReg;
2304 Reg = PMapper.fromString(RegString->getString(),
2305 Subtarget->getFeatureBits(),
2306 IsValidSpecialReg);
2307 if (IsValidSpecialReg) {
2308 assert (isa<ConstantSDNode>(N->getOperand(2))
2309 && "Expected a constant integer expression.");
2310 uint64_t Immed = cast<ConstantSDNode>(N->getOperand(2))->getZExtValue();
2311 return CurDAG->getMachineNode(AArch64::MSRpstate, DL, MVT::Other,
2312 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2313 CurDAG->getTargetConstant(Immed, DL, MVT::i16),
2314 N->getOperand(0));
2315 }
2316
2317 // Use the sysreg mapper to attempt to map the remaining possible strings
2318 // to the value for the register to be used for the MSR (register)
2319 // instruction operand.
2320 AArch64SysReg::MSRMapper Mapper;
2321 Reg = Mapper.fromString(RegString->getString(),
2322 Subtarget->getFeatureBits(),
2323 IsValidSpecialReg);
2324
2325 if (IsValidSpecialReg)
2326 return CurDAG->getMachineNode(AArch64::MSR, DL, MVT::Other,
2327 CurDAG->getTargetConstant(Reg, DL, MVT::i32),
2328 N->getOperand(2), N->getOperand(0));
2329
2330 return nullptr;
2331}
2332
Tim Northover3b0846e2014-05-24 12:50:23 +00002333SDNode *AArch64DAGToDAGISel::Select(SDNode *Node) {
2334 // Dump information about the Node being selected
2335 DEBUG(errs() << "Selecting: ");
2336 DEBUG(Node->dump(CurDAG));
2337 DEBUG(errs() << "\n");
2338
2339 // If we have a custom node, we already have selected!
2340 if (Node->isMachineOpcode()) {
2341 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n");
2342 Node->setNodeId(-1);
2343 return nullptr;
2344 }
2345
2346 // Few custom selection stuff.
2347 SDNode *ResNode = nullptr;
2348 EVT VT = Node->getValueType(0);
2349
2350 switch (Node->getOpcode()) {
2351 default:
2352 break;
2353
Luke Cheeseman85fd06d2015-06-01 12:02:47 +00002354 case ISD::READ_REGISTER:
2355 if (SDNode *Res = SelectReadRegister(Node))
2356 return Res;
2357 break;
2358
2359 case ISD::WRITE_REGISTER:
2360 if (SDNode *Res = SelectWriteRegister(Node))
2361 return Res;
2362 break;
2363
Tim Northover3b0846e2014-05-24 12:50:23 +00002364 case ISD::ADD:
2365 if (SDNode *I = SelectMLAV64LaneV128(Node))
2366 return I;
2367 break;
2368
2369 case ISD::LOAD: {
2370 // Try to select as an indexed load. Fall through to normal processing
2371 // if we can't.
2372 bool Done = false;
2373 SDNode *I = SelectIndexedLoad(Node, Done);
2374 if (Done)
2375 return I;
2376 break;
2377 }
2378
2379 case ISD::SRL:
2380 case ISD::AND:
2381 case ISD::SRA:
2382 if (SDNode *I = SelectBitfieldExtractOp(Node))
2383 return I;
2384 break;
2385
2386 case ISD::OR:
2387 if (SDNode *I = SelectBitfieldInsertOp(Node))
2388 return I;
2389 break;
2390
2391 case ISD::EXTRACT_VECTOR_ELT: {
2392 // Extracting lane zero is a special case where we can just use a plain
2393 // EXTRACT_SUBREG instruction, which will become FMOV. This is easier for
2394 // the rest of the compiler, especially the register allocator and copyi
2395 // propagation, to reason about, so is preferred when it's possible to
2396 // use it.
2397 ConstantSDNode *LaneNode = cast<ConstantSDNode>(Node->getOperand(1));
2398 // Bail and use the default Select() for non-zero lanes.
2399 if (LaneNode->getZExtValue() != 0)
2400 break;
2401 // If the element type is not the same as the result type, likewise
2402 // bail and use the default Select(), as there's more to do than just
2403 // a cross-class COPY. This catches extracts of i8 and i16 elements
2404 // since they will need an explicit zext.
2405 if (VT != Node->getOperand(0).getValueType().getVectorElementType())
2406 break;
2407 unsigned SubReg;
2408 switch (Node->getOperand(0)
2409 .getValueType()
2410 .getVectorElementType()
2411 .getSizeInBits()) {
2412 default:
Craig Topper2a30d782014-06-18 05:05:13 +00002413 llvm_unreachable("Unexpected vector element type!");
Tim Northover3b0846e2014-05-24 12:50:23 +00002414 case 64:
2415 SubReg = AArch64::dsub;
2416 break;
2417 case 32:
2418 SubReg = AArch64::ssub;
2419 break;
Oliver Stannard89d15422014-08-27 16:16:04 +00002420 case 16:
2421 SubReg = AArch64::hsub;
2422 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00002423 case 8:
2424 llvm_unreachable("unexpected zext-requiring extract element!");
2425 }
2426 SDValue Extract = CurDAG->getTargetExtractSubreg(SubReg, SDLoc(Node), VT,
2427 Node->getOperand(0));
2428 DEBUG(dbgs() << "ISEL: Custom selection!\n=> ");
2429 DEBUG(Extract->dumpr(CurDAG));
2430 DEBUG(dbgs() << "\n");
2431 return Extract.getNode();
2432 }
2433 case ISD::Constant: {
2434 // Materialize zero constants as copies from WZR/XZR. This allows
2435 // the coalescer to propagate these into other instructions.
2436 ConstantSDNode *ConstNode = cast<ConstantSDNode>(Node);
2437 if (ConstNode->isNullValue()) {
2438 if (VT == MVT::i32)
2439 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(), SDLoc(Node),
2440 AArch64::WZR, MVT::i32).getNode();
2441 else if (VT == MVT::i64)
2442 return CurDAG->getCopyFromReg(CurDAG->getEntryNode(), SDLoc(Node),
2443 AArch64::XZR, MVT::i64).getNode();
2444 }
2445 break;
2446 }
2447
2448 case ISD::FrameIndex: {
2449 // Selects to ADDXri FI, 0 which in turn will become ADDXri SP, imm.
2450 int FI = cast<FrameIndexSDNode>(Node)->getIndex();
2451 unsigned Shifter = AArch64_AM::getShifterImm(AArch64_AM::LSL, 0);
2452 const TargetLowering *TLI = getTargetLowering();
Mehdi Amini44ede332015-07-09 02:09:04 +00002453 SDValue TFI = CurDAG->getTargetFrameIndex(
2454 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
Sergey Dmitrouk842a51b2015-04-28 14:05:47 +00002455 SDLoc DL(Node);
2456 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, DL, MVT::i32),
2457 CurDAG->getTargetConstant(Shifter, DL, MVT::i32) };
Tim Northover3b0846e2014-05-24 12:50:23 +00002458 return CurDAG->SelectNodeTo(Node, AArch64::ADDXri, MVT::i64, Ops);
2459 }
2460 case ISD::INTRINSIC_W_CHAIN: {
2461 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2462 switch (IntNo) {
2463 default:
2464 break;
2465 case Intrinsic::aarch64_ldaxp:
2466 case Intrinsic::aarch64_ldxp: {
2467 unsigned Op =
2468 IntNo == Intrinsic::aarch64_ldaxp ? AArch64::LDAXPX : AArch64::LDXPX;
2469 SDValue MemAddr = Node->getOperand(2);
2470 SDLoc DL(Node);
2471 SDValue Chain = Node->getOperand(0);
2472
2473 SDNode *Ld = CurDAG->getMachineNode(Op, DL, MVT::i64, MVT::i64,
2474 MVT::Other, MemAddr, Chain);
2475
2476 // Transfer memoperands.
2477 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2478 MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2479 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
2480 return Ld;
2481 }
2482 case Intrinsic::aarch64_stlxp:
2483 case Intrinsic::aarch64_stxp: {
2484 unsigned Op =
2485 IntNo == Intrinsic::aarch64_stlxp ? AArch64::STLXPX : AArch64::STXPX;
2486 SDLoc DL(Node);
2487 SDValue Chain = Node->getOperand(0);
2488 SDValue ValLo = Node->getOperand(2);
2489 SDValue ValHi = Node->getOperand(3);
2490 SDValue MemAddr = Node->getOperand(4);
2491
2492 // Place arguments in the right order.
Benjamin Kramerea68a942015-02-19 15:26:17 +00002493 SDValue Ops[] = {ValLo, ValHi, MemAddr, Chain};
Tim Northover3b0846e2014-05-24 12:50:23 +00002494
2495 SDNode *St = CurDAG->getMachineNode(Op, DL, MVT::i32, MVT::Other, Ops);
2496 // Transfer memoperands.
2497 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2498 MemOp[0] = cast<MemIntrinsicSDNode>(Node)->getMemOperand();
2499 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
2500
2501 return St;
2502 }
2503 case Intrinsic::aarch64_neon_ld1x2:
2504 if (VT == MVT::v8i8)
2505 return SelectLoad(Node, 2, AArch64::LD1Twov8b, AArch64::dsub0);
2506 else if (VT == MVT::v16i8)
2507 return SelectLoad(Node, 2, AArch64::LD1Twov16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002508 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002509 return SelectLoad(Node, 2, AArch64::LD1Twov4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002510 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002511 return SelectLoad(Node, 2, AArch64::LD1Twov8h, AArch64::qsub0);
2512 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2513 return SelectLoad(Node, 2, AArch64::LD1Twov2s, AArch64::dsub0);
2514 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2515 return SelectLoad(Node, 2, AArch64::LD1Twov4s, AArch64::qsub0);
2516 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2517 return SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2518 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2519 return SelectLoad(Node, 2, AArch64::LD1Twov2d, AArch64::qsub0);
2520 break;
2521 case Intrinsic::aarch64_neon_ld1x3:
2522 if (VT == MVT::v8i8)
2523 return SelectLoad(Node, 3, AArch64::LD1Threev8b, AArch64::dsub0);
2524 else if (VT == MVT::v16i8)
2525 return SelectLoad(Node, 3, AArch64::LD1Threev16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002526 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002527 return SelectLoad(Node, 3, AArch64::LD1Threev4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002528 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002529 return SelectLoad(Node, 3, AArch64::LD1Threev8h, AArch64::qsub0);
2530 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2531 return SelectLoad(Node, 3, AArch64::LD1Threev2s, AArch64::dsub0);
2532 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2533 return SelectLoad(Node, 3, AArch64::LD1Threev4s, AArch64::qsub0);
2534 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2535 return SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2536 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2537 return SelectLoad(Node, 3, AArch64::LD1Threev2d, AArch64::qsub0);
2538 break;
2539 case Intrinsic::aarch64_neon_ld1x4:
2540 if (VT == MVT::v8i8)
2541 return SelectLoad(Node, 4, AArch64::LD1Fourv8b, AArch64::dsub0);
2542 else if (VT == MVT::v16i8)
2543 return SelectLoad(Node, 4, AArch64::LD1Fourv16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002544 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002545 return SelectLoad(Node, 4, AArch64::LD1Fourv4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002546 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002547 return SelectLoad(Node, 4, AArch64::LD1Fourv8h, AArch64::qsub0);
2548 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2549 return SelectLoad(Node, 4, AArch64::LD1Fourv2s, AArch64::dsub0);
2550 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2551 return SelectLoad(Node, 4, AArch64::LD1Fourv4s, AArch64::qsub0);
2552 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2553 return SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2554 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2555 return SelectLoad(Node, 4, AArch64::LD1Fourv2d, AArch64::qsub0);
2556 break;
2557 case Intrinsic::aarch64_neon_ld2:
2558 if (VT == MVT::v8i8)
2559 return SelectLoad(Node, 2, AArch64::LD2Twov8b, AArch64::dsub0);
2560 else if (VT == MVT::v16i8)
2561 return SelectLoad(Node, 2, AArch64::LD2Twov16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002562 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002563 return SelectLoad(Node, 2, AArch64::LD2Twov4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002564 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002565 return SelectLoad(Node, 2, AArch64::LD2Twov8h, AArch64::qsub0);
2566 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2567 return SelectLoad(Node, 2, AArch64::LD2Twov2s, AArch64::dsub0);
2568 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2569 return SelectLoad(Node, 2, AArch64::LD2Twov4s, AArch64::qsub0);
2570 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2571 return SelectLoad(Node, 2, AArch64::LD1Twov1d, AArch64::dsub0);
2572 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2573 return SelectLoad(Node, 2, AArch64::LD2Twov2d, AArch64::qsub0);
2574 break;
2575 case Intrinsic::aarch64_neon_ld3:
2576 if (VT == MVT::v8i8)
2577 return SelectLoad(Node, 3, AArch64::LD3Threev8b, AArch64::dsub0);
2578 else if (VT == MVT::v16i8)
2579 return SelectLoad(Node, 3, AArch64::LD3Threev16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002580 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002581 return SelectLoad(Node, 3, AArch64::LD3Threev4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002582 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002583 return SelectLoad(Node, 3, AArch64::LD3Threev8h, AArch64::qsub0);
2584 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2585 return SelectLoad(Node, 3, AArch64::LD3Threev2s, AArch64::dsub0);
2586 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2587 return SelectLoad(Node, 3, AArch64::LD3Threev4s, AArch64::qsub0);
2588 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2589 return SelectLoad(Node, 3, AArch64::LD1Threev1d, AArch64::dsub0);
2590 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2591 return SelectLoad(Node, 3, AArch64::LD3Threev2d, AArch64::qsub0);
2592 break;
2593 case Intrinsic::aarch64_neon_ld4:
2594 if (VT == MVT::v8i8)
2595 return SelectLoad(Node, 4, AArch64::LD4Fourv8b, AArch64::dsub0);
2596 else if (VT == MVT::v16i8)
2597 return SelectLoad(Node, 4, AArch64::LD4Fourv16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002598 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002599 return SelectLoad(Node, 4, AArch64::LD4Fourv4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002600 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002601 return SelectLoad(Node, 4, AArch64::LD4Fourv8h, AArch64::qsub0);
2602 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2603 return SelectLoad(Node, 4, AArch64::LD4Fourv2s, AArch64::dsub0);
2604 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2605 return SelectLoad(Node, 4, AArch64::LD4Fourv4s, AArch64::qsub0);
2606 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2607 return SelectLoad(Node, 4, AArch64::LD1Fourv1d, AArch64::dsub0);
2608 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2609 return SelectLoad(Node, 4, AArch64::LD4Fourv2d, AArch64::qsub0);
2610 break;
2611 case Intrinsic::aarch64_neon_ld2r:
2612 if (VT == MVT::v8i8)
2613 return SelectLoad(Node, 2, AArch64::LD2Rv8b, AArch64::dsub0);
2614 else if (VT == MVT::v16i8)
2615 return SelectLoad(Node, 2, AArch64::LD2Rv16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002616 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002617 return SelectLoad(Node, 2, AArch64::LD2Rv4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002618 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002619 return SelectLoad(Node, 2, AArch64::LD2Rv8h, AArch64::qsub0);
2620 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2621 return SelectLoad(Node, 2, AArch64::LD2Rv2s, AArch64::dsub0);
2622 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2623 return SelectLoad(Node, 2, AArch64::LD2Rv4s, AArch64::qsub0);
2624 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2625 return SelectLoad(Node, 2, AArch64::LD2Rv1d, AArch64::dsub0);
2626 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2627 return SelectLoad(Node, 2, AArch64::LD2Rv2d, AArch64::qsub0);
2628 break;
2629 case Intrinsic::aarch64_neon_ld3r:
2630 if (VT == MVT::v8i8)
2631 return SelectLoad(Node, 3, AArch64::LD3Rv8b, AArch64::dsub0);
2632 else if (VT == MVT::v16i8)
2633 return SelectLoad(Node, 3, AArch64::LD3Rv16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002634 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002635 return SelectLoad(Node, 3, AArch64::LD3Rv4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002636 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002637 return SelectLoad(Node, 3, AArch64::LD3Rv8h, AArch64::qsub0);
2638 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2639 return SelectLoad(Node, 3, AArch64::LD3Rv2s, AArch64::dsub0);
2640 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2641 return SelectLoad(Node, 3, AArch64::LD3Rv4s, AArch64::qsub0);
2642 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2643 return SelectLoad(Node, 3, AArch64::LD3Rv1d, AArch64::dsub0);
2644 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2645 return SelectLoad(Node, 3, AArch64::LD3Rv2d, AArch64::qsub0);
2646 break;
2647 case Intrinsic::aarch64_neon_ld4r:
2648 if (VT == MVT::v8i8)
2649 return SelectLoad(Node, 4, AArch64::LD4Rv8b, AArch64::dsub0);
2650 else if (VT == MVT::v16i8)
2651 return SelectLoad(Node, 4, AArch64::LD4Rv16b, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002652 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002653 return SelectLoad(Node, 4, AArch64::LD4Rv4h, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002654 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002655 return SelectLoad(Node, 4, AArch64::LD4Rv8h, AArch64::qsub0);
2656 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2657 return SelectLoad(Node, 4, AArch64::LD4Rv2s, AArch64::dsub0);
2658 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2659 return SelectLoad(Node, 4, AArch64::LD4Rv4s, AArch64::qsub0);
2660 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2661 return SelectLoad(Node, 4, AArch64::LD4Rv1d, AArch64::dsub0);
2662 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2663 return SelectLoad(Node, 4, AArch64::LD4Rv2d, AArch64::qsub0);
2664 break;
2665 case Intrinsic::aarch64_neon_ld2lane:
2666 if (VT == MVT::v16i8 || VT == MVT::v8i8)
2667 return SelectLoadLane(Node, 2, AArch64::LD2i8);
Oliver Stannard89d15422014-08-27 16:16:04 +00002668 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2669 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002670 return SelectLoadLane(Node, 2, AArch64::LD2i16);
2671 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2672 VT == MVT::v2f32)
2673 return SelectLoadLane(Node, 2, AArch64::LD2i32);
2674 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2675 VT == MVT::v1f64)
2676 return SelectLoadLane(Node, 2, AArch64::LD2i64);
2677 break;
2678 case Intrinsic::aarch64_neon_ld3lane:
2679 if (VT == MVT::v16i8 || VT == MVT::v8i8)
2680 return SelectLoadLane(Node, 3, AArch64::LD3i8);
Oliver Stannard89d15422014-08-27 16:16:04 +00002681 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2682 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002683 return SelectLoadLane(Node, 3, AArch64::LD3i16);
2684 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2685 VT == MVT::v2f32)
2686 return SelectLoadLane(Node, 3, AArch64::LD3i32);
2687 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2688 VT == MVT::v1f64)
2689 return SelectLoadLane(Node, 3, AArch64::LD3i64);
2690 break;
2691 case Intrinsic::aarch64_neon_ld4lane:
2692 if (VT == MVT::v16i8 || VT == MVT::v8i8)
2693 return SelectLoadLane(Node, 4, AArch64::LD4i8);
Oliver Stannard89d15422014-08-27 16:16:04 +00002694 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2695 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002696 return SelectLoadLane(Node, 4, AArch64::LD4i16);
2697 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2698 VT == MVT::v2f32)
2699 return SelectLoadLane(Node, 4, AArch64::LD4i32);
2700 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2701 VT == MVT::v1f64)
2702 return SelectLoadLane(Node, 4, AArch64::LD4i64);
2703 break;
2704 }
2705 } break;
2706 case ISD::INTRINSIC_WO_CHAIN: {
2707 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(0))->getZExtValue();
2708 switch (IntNo) {
2709 default:
2710 break;
2711 case Intrinsic::aarch64_neon_tbl2:
2712 return SelectTable(Node, 2, VT == MVT::v8i8 ? AArch64::TBLv8i8Two
2713 : AArch64::TBLv16i8Two,
2714 false);
2715 case Intrinsic::aarch64_neon_tbl3:
2716 return SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBLv8i8Three
2717 : AArch64::TBLv16i8Three,
2718 false);
2719 case Intrinsic::aarch64_neon_tbl4:
2720 return SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBLv8i8Four
2721 : AArch64::TBLv16i8Four,
2722 false);
2723 case Intrinsic::aarch64_neon_tbx2:
2724 return SelectTable(Node, 2, VT == MVT::v8i8 ? AArch64::TBXv8i8Two
2725 : AArch64::TBXv16i8Two,
2726 true);
2727 case Intrinsic::aarch64_neon_tbx3:
2728 return SelectTable(Node, 3, VT == MVT::v8i8 ? AArch64::TBXv8i8Three
2729 : AArch64::TBXv16i8Three,
2730 true);
2731 case Intrinsic::aarch64_neon_tbx4:
2732 return SelectTable(Node, 4, VT == MVT::v8i8 ? AArch64::TBXv8i8Four
2733 : AArch64::TBXv16i8Four,
2734 true);
2735 case Intrinsic::aarch64_neon_smull:
2736 case Intrinsic::aarch64_neon_umull:
2737 if (SDNode *N = SelectMULLV64LaneV128(IntNo, Node))
2738 return N;
2739 break;
2740 }
2741 break;
2742 }
2743 case ISD::INTRINSIC_VOID: {
2744 unsigned IntNo = cast<ConstantSDNode>(Node->getOperand(1))->getZExtValue();
2745 if (Node->getNumOperands() >= 3)
2746 VT = Node->getOperand(2)->getValueType(0);
2747 switch (IntNo) {
2748 default:
2749 break;
2750 case Intrinsic::aarch64_neon_st1x2: {
2751 if (VT == MVT::v8i8)
2752 return SelectStore(Node, 2, AArch64::ST1Twov8b);
2753 else if (VT == MVT::v16i8)
2754 return SelectStore(Node, 2, AArch64::ST1Twov16b);
Oliver Stannard89d15422014-08-27 16:16:04 +00002755 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002756 return SelectStore(Node, 2, AArch64::ST1Twov4h);
Oliver Stannard89d15422014-08-27 16:16:04 +00002757 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002758 return SelectStore(Node, 2, AArch64::ST1Twov8h);
2759 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2760 return SelectStore(Node, 2, AArch64::ST1Twov2s);
2761 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2762 return SelectStore(Node, 2, AArch64::ST1Twov4s);
2763 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2764 return SelectStore(Node, 2, AArch64::ST1Twov2d);
2765 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2766 return SelectStore(Node, 2, AArch64::ST1Twov1d);
2767 break;
2768 }
2769 case Intrinsic::aarch64_neon_st1x3: {
2770 if (VT == MVT::v8i8)
2771 return SelectStore(Node, 3, AArch64::ST1Threev8b);
2772 else if (VT == MVT::v16i8)
2773 return SelectStore(Node, 3, AArch64::ST1Threev16b);
Oliver Stannard89d15422014-08-27 16:16:04 +00002774 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002775 return SelectStore(Node, 3, AArch64::ST1Threev4h);
Oliver Stannard89d15422014-08-27 16:16:04 +00002776 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002777 return SelectStore(Node, 3, AArch64::ST1Threev8h);
2778 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2779 return SelectStore(Node, 3, AArch64::ST1Threev2s);
2780 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2781 return SelectStore(Node, 3, AArch64::ST1Threev4s);
2782 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2783 return SelectStore(Node, 3, AArch64::ST1Threev2d);
2784 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2785 return SelectStore(Node, 3, AArch64::ST1Threev1d);
2786 break;
2787 }
2788 case Intrinsic::aarch64_neon_st1x4: {
2789 if (VT == MVT::v8i8)
2790 return SelectStore(Node, 4, AArch64::ST1Fourv8b);
2791 else if (VT == MVT::v16i8)
2792 return SelectStore(Node, 4, AArch64::ST1Fourv16b);
Oliver Stannard89d15422014-08-27 16:16:04 +00002793 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002794 return SelectStore(Node, 4, AArch64::ST1Fourv4h);
Oliver Stannard89d15422014-08-27 16:16:04 +00002795 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002796 return SelectStore(Node, 4, AArch64::ST1Fourv8h);
2797 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2798 return SelectStore(Node, 4, AArch64::ST1Fourv2s);
2799 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2800 return SelectStore(Node, 4, AArch64::ST1Fourv4s);
2801 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2802 return SelectStore(Node, 4, AArch64::ST1Fourv2d);
2803 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2804 return SelectStore(Node, 4, AArch64::ST1Fourv1d);
2805 break;
2806 }
2807 case Intrinsic::aarch64_neon_st2: {
2808 if (VT == MVT::v8i8)
2809 return SelectStore(Node, 2, AArch64::ST2Twov8b);
2810 else if (VT == MVT::v16i8)
2811 return SelectStore(Node, 2, AArch64::ST2Twov16b);
Oliver Stannard89d15422014-08-27 16:16:04 +00002812 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002813 return SelectStore(Node, 2, AArch64::ST2Twov4h);
Oliver Stannard89d15422014-08-27 16:16:04 +00002814 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002815 return SelectStore(Node, 2, AArch64::ST2Twov8h);
2816 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2817 return SelectStore(Node, 2, AArch64::ST2Twov2s);
2818 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2819 return SelectStore(Node, 2, AArch64::ST2Twov4s);
2820 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2821 return SelectStore(Node, 2, AArch64::ST2Twov2d);
2822 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2823 return SelectStore(Node, 2, AArch64::ST1Twov1d);
2824 break;
2825 }
2826 case Intrinsic::aarch64_neon_st3: {
2827 if (VT == MVT::v8i8)
2828 return SelectStore(Node, 3, AArch64::ST3Threev8b);
2829 else if (VT == MVT::v16i8)
2830 return SelectStore(Node, 3, AArch64::ST3Threev16b);
Oliver Stannard89d15422014-08-27 16:16:04 +00002831 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002832 return SelectStore(Node, 3, AArch64::ST3Threev4h);
Oliver Stannard89d15422014-08-27 16:16:04 +00002833 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002834 return SelectStore(Node, 3, AArch64::ST3Threev8h);
2835 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2836 return SelectStore(Node, 3, AArch64::ST3Threev2s);
2837 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2838 return SelectStore(Node, 3, AArch64::ST3Threev4s);
2839 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2840 return SelectStore(Node, 3, AArch64::ST3Threev2d);
2841 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2842 return SelectStore(Node, 3, AArch64::ST1Threev1d);
2843 break;
2844 }
2845 case Intrinsic::aarch64_neon_st4: {
2846 if (VT == MVT::v8i8)
2847 return SelectStore(Node, 4, AArch64::ST4Fourv8b);
2848 else if (VT == MVT::v16i8)
2849 return SelectStore(Node, 4, AArch64::ST4Fourv16b);
Oliver Stannard89d15422014-08-27 16:16:04 +00002850 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002851 return SelectStore(Node, 4, AArch64::ST4Fourv4h);
Oliver Stannard89d15422014-08-27 16:16:04 +00002852 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002853 return SelectStore(Node, 4, AArch64::ST4Fourv8h);
2854 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2855 return SelectStore(Node, 4, AArch64::ST4Fourv2s);
2856 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2857 return SelectStore(Node, 4, AArch64::ST4Fourv4s);
2858 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2859 return SelectStore(Node, 4, AArch64::ST4Fourv2d);
2860 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2861 return SelectStore(Node, 4, AArch64::ST1Fourv1d);
2862 break;
2863 }
2864 case Intrinsic::aarch64_neon_st2lane: {
2865 if (VT == MVT::v16i8 || VT == MVT::v8i8)
2866 return SelectStoreLane(Node, 2, AArch64::ST2i8);
Oliver Stannard89d15422014-08-27 16:16:04 +00002867 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2868 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002869 return SelectStoreLane(Node, 2, AArch64::ST2i16);
2870 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2871 VT == MVT::v2f32)
2872 return SelectStoreLane(Node, 2, AArch64::ST2i32);
2873 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2874 VT == MVT::v1f64)
2875 return SelectStoreLane(Node, 2, AArch64::ST2i64);
2876 break;
2877 }
2878 case Intrinsic::aarch64_neon_st3lane: {
2879 if (VT == MVT::v16i8 || VT == MVT::v8i8)
2880 return SelectStoreLane(Node, 3, AArch64::ST3i8);
Oliver Stannard89d15422014-08-27 16:16:04 +00002881 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2882 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002883 return SelectStoreLane(Node, 3, AArch64::ST3i16);
2884 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2885 VT == MVT::v2f32)
2886 return SelectStoreLane(Node, 3, AArch64::ST3i32);
2887 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2888 VT == MVT::v1f64)
2889 return SelectStoreLane(Node, 3, AArch64::ST3i64);
2890 break;
2891 }
2892 case Intrinsic::aarch64_neon_st4lane: {
2893 if (VT == MVT::v16i8 || VT == MVT::v8i8)
2894 return SelectStoreLane(Node, 4, AArch64::ST4i8);
Oliver Stannard89d15422014-08-27 16:16:04 +00002895 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
2896 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002897 return SelectStoreLane(Node, 4, AArch64::ST4i16);
2898 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
2899 VT == MVT::v2f32)
2900 return SelectStoreLane(Node, 4, AArch64::ST4i32);
2901 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
2902 VT == MVT::v1f64)
2903 return SelectStoreLane(Node, 4, AArch64::ST4i64);
2904 break;
2905 }
2906 }
Mehdi Aminia7583982015-08-23 00:42:57 +00002907 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00002908 }
2909 case AArch64ISD::LD2post: {
2910 if (VT == MVT::v8i8)
2911 return SelectPostLoad(Node, 2, AArch64::LD2Twov8b_POST, AArch64::dsub0);
2912 else if (VT == MVT::v16i8)
2913 return SelectPostLoad(Node, 2, AArch64::LD2Twov16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002914 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002915 return SelectPostLoad(Node, 2, AArch64::LD2Twov4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002916 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002917 return SelectPostLoad(Node, 2, AArch64::LD2Twov8h_POST, AArch64::qsub0);
2918 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2919 return SelectPostLoad(Node, 2, AArch64::LD2Twov2s_POST, AArch64::dsub0);
2920 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2921 return SelectPostLoad(Node, 2, AArch64::LD2Twov4s_POST, AArch64::qsub0);
2922 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2923 return SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
2924 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2925 return SelectPostLoad(Node, 2, AArch64::LD2Twov2d_POST, AArch64::qsub0);
2926 break;
2927 }
2928 case AArch64ISD::LD3post: {
2929 if (VT == MVT::v8i8)
2930 return SelectPostLoad(Node, 3, AArch64::LD3Threev8b_POST, AArch64::dsub0);
2931 else if (VT == MVT::v16i8)
2932 return SelectPostLoad(Node, 3, AArch64::LD3Threev16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002933 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002934 return SelectPostLoad(Node, 3, AArch64::LD3Threev4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002935 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002936 return SelectPostLoad(Node, 3, AArch64::LD3Threev8h_POST, AArch64::qsub0);
2937 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2938 return SelectPostLoad(Node, 3, AArch64::LD3Threev2s_POST, AArch64::dsub0);
2939 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2940 return SelectPostLoad(Node, 3, AArch64::LD3Threev4s_POST, AArch64::qsub0);
2941 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2942 return SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
2943 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2944 return SelectPostLoad(Node, 3, AArch64::LD3Threev2d_POST, AArch64::qsub0);
2945 break;
2946 }
2947 case AArch64ISD::LD4post: {
2948 if (VT == MVT::v8i8)
2949 return SelectPostLoad(Node, 4, AArch64::LD4Fourv8b_POST, AArch64::dsub0);
2950 else if (VT == MVT::v16i8)
2951 return SelectPostLoad(Node, 4, AArch64::LD4Fourv16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002952 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002953 return SelectPostLoad(Node, 4, AArch64::LD4Fourv4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002954 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002955 return SelectPostLoad(Node, 4, AArch64::LD4Fourv8h_POST, AArch64::qsub0);
2956 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2957 return SelectPostLoad(Node, 4, AArch64::LD4Fourv2s_POST, AArch64::dsub0);
2958 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2959 return SelectPostLoad(Node, 4, AArch64::LD4Fourv4s_POST, AArch64::qsub0);
2960 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2961 return SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
2962 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2963 return SelectPostLoad(Node, 4, AArch64::LD4Fourv2d_POST, AArch64::qsub0);
2964 break;
2965 }
2966 case AArch64ISD::LD1x2post: {
2967 if (VT == MVT::v8i8)
2968 return SelectPostLoad(Node, 2, AArch64::LD1Twov8b_POST, AArch64::dsub0);
2969 else if (VT == MVT::v16i8)
2970 return SelectPostLoad(Node, 2, AArch64::LD1Twov16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002971 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002972 return SelectPostLoad(Node, 2, AArch64::LD1Twov4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002973 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002974 return SelectPostLoad(Node, 2, AArch64::LD1Twov8h_POST, AArch64::qsub0);
2975 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2976 return SelectPostLoad(Node, 2, AArch64::LD1Twov2s_POST, AArch64::dsub0);
2977 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2978 return SelectPostLoad(Node, 2, AArch64::LD1Twov4s_POST, AArch64::qsub0);
2979 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2980 return SelectPostLoad(Node, 2, AArch64::LD1Twov1d_POST, AArch64::dsub0);
2981 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
2982 return SelectPostLoad(Node, 2, AArch64::LD1Twov2d_POST, AArch64::qsub0);
2983 break;
2984 }
2985 case AArch64ISD::LD1x3post: {
2986 if (VT == MVT::v8i8)
2987 return SelectPostLoad(Node, 3, AArch64::LD1Threev8b_POST, AArch64::dsub0);
2988 else if (VT == MVT::v16i8)
2989 return SelectPostLoad(Node, 3, AArch64::LD1Threev16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002990 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002991 return SelectPostLoad(Node, 3, AArch64::LD1Threev4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00002992 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00002993 return SelectPostLoad(Node, 3, AArch64::LD1Threev8h_POST, AArch64::qsub0);
2994 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
2995 return SelectPostLoad(Node, 3, AArch64::LD1Threev2s_POST, AArch64::dsub0);
2996 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
2997 return SelectPostLoad(Node, 3, AArch64::LD1Threev4s_POST, AArch64::qsub0);
2998 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
2999 return SelectPostLoad(Node, 3, AArch64::LD1Threev1d_POST, AArch64::dsub0);
3000 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3001 return SelectPostLoad(Node, 3, AArch64::LD1Threev2d_POST, AArch64::qsub0);
3002 break;
3003 }
3004 case AArch64ISD::LD1x4post: {
3005 if (VT == MVT::v8i8)
3006 return SelectPostLoad(Node, 4, AArch64::LD1Fourv8b_POST, AArch64::dsub0);
3007 else if (VT == MVT::v16i8)
3008 return SelectPostLoad(Node, 4, AArch64::LD1Fourv16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003009 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003010 return SelectPostLoad(Node, 4, AArch64::LD1Fourv4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003011 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003012 return SelectPostLoad(Node, 4, AArch64::LD1Fourv8h_POST, AArch64::qsub0);
3013 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3014 return SelectPostLoad(Node, 4, AArch64::LD1Fourv2s_POST, AArch64::dsub0);
3015 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3016 return SelectPostLoad(Node, 4, AArch64::LD1Fourv4s_POST, AArch64::qsub0);
3017 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3018 return SelectPostLoad(Node, 4, AArch64::LD1Fourv1d_POST, AArch64::dsub0);
3019 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3020 return SelectPostLoad(Node, 4, AArch64::LD1Fourv2d_POST, AArch64::qsub0);
3021 break;
3022 }
3023 case AArch64ISD::LD1DUPpost: {
3024 if (VT == MVT::v8i8)
3025 return SelectPostLoad(Node, 1, AArch64::LD1Rv8b_POST, AArch64::dsub0);
3026 else if (VT == MVT::v16i8)
3027 return SelectPostLoad(Node, 1, AArch64::LD1Rv16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003028 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003029 return SelectPostLoad(Node, 1, AArch64::LD1Rv4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003030 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003031 return SelectPostLoad(Node, 1, AArch64::LD1Rv8h_POST, AArch64::qsub0);
3032 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3033 return SelectPostLoad(Node, 1, AArch64::LD1Rv2s_POST, AArch64::dsub0);
3034 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3035 return SelectPostLoad(Node, 1, AArch64::LD1Rv4s_POST, AArch64::qsub0);
3036 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3037 return SelectPostLoad(Node, 1, AArch64::LD1Rv1d_POST, AArch64::dsub0);
3038 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3039 return SelectPostLoad(Node, 1, AArch64::LD1Rv2d_POST, AArch64::qsub0);
3040 break;
3041 }
3042 case AArch64ISD::LD2DUPpost: {
3043 if (VT == MVT::v8i8)
3044 return SelectPostLoad(Node, 2, AArch64::LD2Rv8b_POST, AArch64::dsub0);
3045 else if (VT == MVT::v16i8)
3046 return SelectPostLoad(Node, 2, AArch64::LD2Rv16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003047 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003048 return SelectPostLoad(Node, 2, AArch64::LD2Rv4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003049 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003050 return SelectPostLoad(Node, 2, AArch64::LD2Rv8h_POST, AArch64::qsub0);
3051 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3052 return SelectPostLoad(Node, 2, AArch64::LD2Rv2s_POST, AArch64::dsub0);
3053 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3054 return SelectPostLoad(Node, 2, AArch64::LD2Rv4s_POST, AArch64::qsub0);
3055 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3056 return SelectPostLoad(Node, 2, AArch64::LD2Rv1d_POST, AArch64::dsub0);
3057 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3058 return SelectPostLoad(Node, 2, AArch64::LD2Rv2d_POST, AArch64::qsub0);
3059 break;
3060 }
3061 case AArch64ISD::LD3DUPpost: {
3062 if (VT == MVT::v8i8)
3063 return SelectPostLoad(Node, 3, AArch64::LD3Rv8b_POST, AArch64::dsub0);
3064 else if (VT == MVT::v16i8)
3065 return SelectPostLoad(Node, 3, AArch64::LD3Rv16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003066 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003067 return SelectPostLoad(Node, 3, AArch64::LD3Rv4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003068 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003069 return SelectPostLoad(Node, 3, AArch64::LD3Rv8h_POST, AArch64::qsub0);
3070 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3071 return SelectPostLoad(Node, 3, AArch64::LD3Rv2s_POST, AArch64::dsub0);
3072 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3073 return SelectPostLoad(Node, 3, AArch64::LD3Rv4s_POST, AArch64::qsub0);
3074 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3075 return SelectPostLoad(Node, 3, AArch64::LD3Rv1d_POST, AArch64::dsub0);
3076 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3077 return SelectPostLoad(Node, 3, AArch64::LD3Rv2d_POST, AArch64::qsub0);
3078 break;
3079 }
3080 case AArch64ISD::LD4DUPpost: {
3081 if (VT == MVT::v8i8)
3082 return SelectPostLoad(Node, 4, AArch64::LD4Rv8b_POST, AArch64::dsub0);
3083 else if (VT == MVT::v16i8)
3084 return SelectPostLoad(Node, 4, AArch64::LD4Rv16b_POST, AArch64::qsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003085 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003086 return SelectPostLoad(Node, 4, AArch64::LD4Rv4h_POST, AArch64::dsub0);
Oliver Stannard89d15422014-08-27 16:16:04 +00003087 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003088 return SelectPostLoad(Node, 4, AArch64::LD4Rv8h_POST, AArch64::qsub0);
3089 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3090 return SelectPostLoad(Node, 4, AArch64::LD4Rv2s_POST, AArch64::dsub0);
3091 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3092 return SelectPostLoad(Node, 4, AArch64::LD4Rv4s_POST, AArch64::qsub0);
3093 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3094 return SelectPostLoad(Node, 4, AArch64::LD4Rv1d_POST, AArch64::dsub0);
3095 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3096 return SelectPostLoad(Node, 4, AArch64::LD4Rv2d_POST, AArch64::qsub0);
3097 break;
3098 }
3099 case AArch64ISD::LD1LANEpost: {
3100 if (VT == MVT::v16i8 || VT == MVT::v8i8)
3101 return SelectPostLoadLane(Node, 1, AArch64::LD1i8_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003102 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3103 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003104 return SelectPostLoadLane(Node, 1, AArch64::LD1i16_POST);
3105 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3106 VT == MVT::v2f32)
3107 return SelectPostLoadLane(Node, 1, AArch64::LD1i32_POST);
3108 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3109 VT == MVT::v1f64)
3110 return SelectPostLoadLane(Node, 1, AArch64::LD1i64_POST);
3111 break;
3112 }
3113 case AArch64ISD::LD2LANEpost: {
3114 if (VT == MVT::v16i8 || VT == MVT::v8i8)
3115 return SelectPostLoadLane(Node, 2, AArch64::LD2i8_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003116 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3117 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003118 return SelectPostLoadLane(Node, 2, AArch64::LD2i16_POST);
3119 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3120 VT == MVT::v2f32)
3121 return SelectPostLoadLane(Node, 2, AArch64::LD2i32_POST);
3122 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3123 VT == MVT::v1f64)
3124 return SelectPostLoadLane(Node, 2, AArch64::LD2i64_POST);
3125 break;
3126 }
3127 case AArch64ISD::LD3LANEpost: {
3128 if (VT == MVT::v16i8 || VT == MVT::v8i8)
3129 return SelectPostLoadLane(Node, 3, AArch64::LD3i8_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003130 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3131 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003132 return SelectPostLoadLane(Node, 3, AArch64::LD3i16_POST);
3133 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3134 VT == MVT::v2f32)
3135 return SelectPostLoadLane(Node, 3, AArch64::LD3i32_POST);
3136 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3137 VT == MVT::v1f64)
3138 return SelectPostLoadLane(Node, 3, AArch64::LD3i64_POST);
3139 break;
3140 }
3141 case AArch64ISD::LD4LANEpost: {
3142 if (VT == MVT::v16i8 || VT == MVT::v8i8)
3143 return SelectPostLoadLane(Node, 4, AArch64::LD4i8_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003144 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3145 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003146 return SelectPostLoadLane(Node, 4, AArch64::LD4i16_POST);
3147 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3148 VT == MVT::v2f32)
3149 return SelectPostLoadLane(Node, 4, AArch64::LD4i32_POST);
3150 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3151 VT == MVT::v1f64)
3152 return SelectPostLoadLane(Node, 4, AArch64::LD4i64_POST);
3153 break;
3154 }
3155 case AArch64ISD::ST2post: {
3156 VT = Node->getOperand(1).getValueType();
3157 if (VT == MVT::v8i8)
3158 return SelectPostStore(Node, 2, AArch64::ST2Twov8b_POST);
3159 else if (VT == MVT::v16i8)
3160 return SelectPostStore(Node, 2, AArch64::ST2Twov16b_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003161 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003162 return SelectPostStore(Node, 2, AArch64::ST2Twov4h_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003163 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003164 return SelectPostStore(Node, 2, AArch64::ST2Twov8h_POST);
3165 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3166 return SelectPostStore(Node, 2, AArch64::ST2Twov2s_POST);
3167 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3168 return SelectPostStore(Node, 2, AArch64::ST2Twov4s_POST);
3169 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3170 return SelectPostStore(Node, 2, AArch64::ST2Twov2d_POST);
3171 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3172 return SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3173 break;
3174 }
3175 case AArch64ISD::ST3post: {
3176 VT = Node->getOperand(1).getValueType();
3177 if (VT == MVT::v8i8)
3178 return SelectPostStore(Node, 3, AArch64::ST3Threev8b_POST);
3179 else if (VT == MVT::v16i8)
3180 return SelectPostStore(Node, 3, AArch64::ST3Threev16b_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003181 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003182 return SelectPostStore(Node, 3, AArch64::ST3Threev4h_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003183 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003184 return SelectPostStore(Node, 3, AArch64::ST3Threev8h_POST);
3185 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3186 return SelectPostStore(Node, 3, AArch64::ST3Threev2s_POST);
3187 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3188 return SelectPostStore(Node, 3, AArch64::ST3Threev4s_POST);
3189 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3190 return SelectPostStore(Node, 3, AArch64::ST3Threev2d_POST);
3191 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3192 return SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3193 break;
3194 }
3195 case AArch64ISD::ST4post: {
3196 VT = Node->getOperand(1).getValueType();
3197 if (VT == MVT::v8i8)
3198 return SelectPostStore(Node, 4, AArch64::ST4Fourv8b_POST);
3199 else if (VT == MVT::v16i8)
3200 return SelectPostStore(Node, 4, AArch64::ST4Fourv16b_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003201 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003202 return SelectPostStore(Node, 4, AArch64::ST4Fourv4h_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003203 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003204 return SelectPostStore(Node, 4, AArch64::ST4Fourv8h_POST);
3205 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3206 return SelectPostStore(Node, 4, AArch64::ST4Fourv2s_POST);
3207 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3208 return SelectPostStore(Node, 4, AArch64::ST4Fourv4s_POST);
3209 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3210 return SelectPostStore(Node, 4, AArch64::ST4Fourv2d_POST);
3211 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3212 return SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3213 break;
3214 }
3215 case AArch64ISD::ST1x2post: {
3216 VT = Node->getOperand(1).getValueType();
3217 if (VT == MVT::v8i8)
3218 return SelectPostStore(Node, 2, AArch64::ST1Twov8b_POST);
3219 else if (VT == MVT::v16i8)
3220 return SelectPostStore(Node, 2, AArch64::ST1Twov16b_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003221 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003222 return SelectPostStore(Node, 2, AArch64::ST1Twov4h_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003223 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003224 return SelectPostStore(Node, 2, AArch64::ST1Twov8h_POST);
3225 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3226 return SelectPostStore(Node, 2, AArch64::ST1Twov2s_POST);
3227 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3228 return SelectPostStore(Node, 2, AArch64::ST1Twov4s_POST);
3229 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3230 return SelectPostStore(Node, 2, AArch64::ST1Twov1d_POST);
3231 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3232 return SelectPostStore(Node, 2, AArch64::ST1Twov2d_POST);
3233 break;
3234 }
3235 case AArch64ISD::ST1x3post: {
3236 VT = Node->getOperand(1).getValueType();
3237 if (VT == MVT::v8i8)
3238 return SelectPostStore(Node, 3, AArch64::ST1Threev8b_POST);
3239 else if (VT == MVT::v16i8)
3240 return SelectPostStore(Node, 3, AArch64::ST1Threev16b_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003241 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003242 return SelectPostStore(Node, 3, AArch64::ST1Threev4h_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003243 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003244 return SelectPostStore(Node, 3, AArch64::ST1Threev8h_POST);
3245 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3246 return SelectPostStore(Node, 3, AArch64::ST1Threev2s_POST);
3247 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3248 return SelectPostStore(Node, 3, AArch64::ST1Threev4s_POST);
3249 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3250 return SelectPostStore(Node, 3, AArch64::ST1Threev1d_POST);
3251 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3252 return SelectPostStore(Node, 3, AArch64::ST1Threev2d_POST);
3253 break;
3254 }
3255 case AArch64ISD::ST1x4post: {
3256 VT = Node->getOperand(1).getValueType();
3257 if (VT == MVT::v8i8)
3258 return SelectPostStore(Node, 4, AArch64::ST1Fourv8b_POST);
3259 else if (VT == MVT::v16i8)
3260 return SelectPostStore(Node, 4, AArch64::ST1Fourv16b_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003261 else if (VT == MVT::v4i16 || VT == MVT::v4f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003262 return SelectPostStore(Node, 4, AArch64::ST1Fourv4h_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003263 else if (VT == MVT::v8i16 || VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003264 return SelectPostStore(Node, 4, AArch64::ST1Fourv8h_POST);
3265 else if (VT == MVT::v2i32 || VT == MVT::v2f32)
3266 return SelectPostStore(Node, 4, AArch64::ST1Fourv2s_POST);
3267 else if (VT == MVT::v4i32 || VT == MVT::v4f32)
3268 return SelectPostStore(Node, 4, AArch64::ST1Fourv4s_POST);
3269 else if (VT == MVT::v1i64 || VT == MVT::v1f64)
3270 return SelectPostStore(Node, 4, AArch64::ST1Fourv1d_POST);
3271 else if (VT == MVT::v2i64 || VT == MVT::v2f64)
3272 return SelectPostStore(Node, 4, AArch64::ST1Fourv2d_POST);
3273 break;
3274 }
3275 case AArch64ISD::ST2LANEpost: {
3276 VT = Node->getOperand(1).getValueType();
3277 if (VT == MVT::v16i8 || VT == MVT::v8i8)
3278 return SelectPostStoreLane(Node, 2, AArch64::ST2i8_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003279 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3280 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003281 return SelectPostStoreLane(Node, 2, AArch64::ST2i16_POST);
3282 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3283 VT == MVT::v2f32)
3284 return SelectPostStoreLane(Node, 2, AArch64::ST2i32_POST);
3285 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3286 VT == MVT::v1f64)
3287 return SelectPostStoreLane(Node, 2, AArch64::ST2i64_POST);
3288 break;
3289 }
3290 case AArch64ISD::ST3LANEpost: {
3291 VT = Node->getOperand(1).getValueType();
3292 if (VT == MVT::v16i8 || VT == MVT::v8i8)
3293 return SelectPostStoreLane(Node, 3, AArch64::ST3i8_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003294 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3295 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003296 return SelectPostStoreLane(Node, 3, AArch64::ST3i16_POST);
3297 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3298 VT == MVT::v2f32)
3299 return SelectPostStoreLane(Node, 3, AArch64::ST3i32_POST);
3300 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3301 VT == MVT::v1f64)
3302 return SelectPostStoreLane(Node, 3, AArch64::ST3i64_POST);
3303 break;
3304 }
3305 case AArch64ISD::ST4LANEpost: {
3306 VT = Node->getOperand(1).getValueType();
3307 if (VT == MVT::v16i8 || VT == MVT::v8i8)
3308 return SelectPostStoreLane(Node, 4, AArch64::ST4i8_POST);
Oliver Stannard89d15422014-08-27 16:16:04 +00003309 else if (VT == MVT::v8i16 || VT == MVT::v4i16 || VT == MVT::v4f16 ||
3310 VT == MVT::v8f16)
Tim Northover3b0846e2014-05-24 12:50:23 +00003311 return SelectPostStoreLane(Node, 4, AArch64::ST4i16_POST);
3312 else if (VT == MVT::v4i32 || VT == MVT::v2i32 || VT == MVT::v4f32 ||
3313 VT == MVT::v2f32)
3314 return SelectPostStoreLane(Node, 4, AArch64::ST4i32_POST);
3315 else if (VT == MVT::v2i64 || VT == MVT::v1i64 || VT == MVT::v2f64 ||
3316 VT == MVT::v1f64)
3317 return SelectPostStoreLane(Node, 4, AArch64::ST4i64_POST);
3318 break;
3319 }
3320
3321 case ISD::FCEIL:
3322 case ISD::FFLOOR:
3323 case ISD::FTRUNC:
3324 case ISD::FROUND:
3325 if (SDNode *I = SelectLIBM(Node))
3326 return I;
3327 break;
Geoff Berryc573bf7a2015-07-28 15:24:10 +00003328
3329 case ISD::FP_TO_SINT:
3330 case ISD::FP_TO_UINT:
3331 if (SDNode *I = SelectFPConvertWithRound(Node))
3332 return I;
3333 break;
Tim Northover3b0846e2014-05-24 12:50:23 +00003334 }
3335
3336 // Select the default instruction
3337 ResNode = SelectCode(Node);
3338
3339 DEBUG(errs() << "=> ");
3340 if (ResNode == nullptr || ResNode == Node)
3341 DEBUG(Node->dump(CurDAG));
3342 else
3343 DEBUG(ResNode->dump(CurDAG));
3344 DEBUG(errs() << "\n");
3345
3346 return ResNode;
3347}
3348
3349/// createAArch64ISelDag - This pass converts a legalized DAG into a
3350/// AArch64-specific DAG, ready for instruction scheduling.
3351FunctionPass *llvm::createAArch64ISelDag(AArch64TargetMachine &TM,
3352 CodeGenOpt::Level OptLevel) {
3353 return new AArch64DAGToDAGISel(TM, OptLevel);
3354}