LLVM 24.0.0git
ARMISelDAGToDAG.cpp
Go to the documentation of this file.
1//===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines an instruction selector for the ARM target.
10//
11//===----------------------------------------------------------------------===//
12
13#include "ARM.h"
14#include "ARMBaseInstrInfo.h"
15#include "ARMTargetMachine.h"
17#include "Utils/ARMBaseInfo.h"
18#include "llvm/ADT/APSInt.h"
27#include "llvm/IR/Constants.h"
29#include "llvm/IR/Function.h"
30#include "llvm/IR/Intrinsics.h"
31#include "llvm/IR/IntrinsicsARM.h"
32#include "llvm/IR/LLVMContext.h"
35#include <optional>
36
37using namespace llvm;
38
39#define DEBUG_TYPE "arm-isel"
40#define PASS_NAME "ARM Instruction Selection"
41
42//===--------------------------------------------------------------------===//
43/// ARMDAGToDAGISel - ARM specific code to select ARM machine
44/// instructions for SelectionDAG operations.
45///
46namespace {
47
48class ARMDAGToDAGISel : public SelectionDAGISel {
49 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
50 /// make the right decision when generating code for different targets.
52
53public:
54 ARMDAGToDAGISel() = delete;
55
56 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm, CodeGenOptLevel OptLevel)
57 : SelectionDAGISel(tm, OptLevel) {}
58
59 bool runOnMachineFunction(MachineFunction &MF) override {
60 // Reset the subtarget each time through.
63 return true;
64 }
65
66 void PreprocessISelDAG() override;
67
68 /// getI32Imm - Return a target constant of type i32 with the specified
69 /// value.
70 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
71 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
72 }
73
74 void Select(SDNode *N) override;
75
76 /// Return true as some complex patterns, like those that call
77 /// canExtractShiftFromMul can modify the DAG inplace.
78 bool ComplexPatternFuncMutatesDAG() const override { return true; }
79
80 bool hasNoVMLxHazardUse(SDNode *N) const;
81 bool isShifterOpProfitable(const SDValue &Shift,
82 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
83 bool SelectRegShifterOperand(SDValue N, SDValue &A,
84 SDValue &B, SDValue &C,
85 bool CheckProfitability = true);
86 bool SelectImmShifterOperand(SDValue N, SDValue &A,
87 SDValue &B, bool CheckProfitability = true);
88 bool SelectShiftRegShifterOperand(SDValue N, SDValue &A, SDValue &B,
89 SDValue &C) {
90 // Don't apply the profitability check
91 return SelectRegShifterOperand(N, A, B, C, false);
92 }
93 bool SelectShiftImmShifterOperand(SDValue N, SDValue &A, SDValue &B) {
94 // Don't apply the profitability check
95 return SelectImmShifterOperand(N, A, B, false);
96 }
97 bool SelectShiftImmShifterOperandOneUse(SDValue N, SDValue &A, SDValue &B) {
98 if (!N.hasOneUse())
99 return false;
100 return SelectImmShifterOperand(N, A, B, false);
101 }
102
103 bool SelectAddLikeOr(SDNode *Parent, SDValue N, SDValue &Out);
104
105 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
106 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
107
108 bool SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
110 bool SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
112 bool SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
114 bool SelectAddrOffsetNone(SDValue N, SDValue &Base);
115 bool SelectAddrMode3(SDValue N, SDValue &Base,
117 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
119 bool IsAddressingMode5(SDValue N, SDValue &Base, SDValue &Offset, bool FP16);
120 bool SelectAddrMode5(SDValue N, SDValue &Base, SDValue &Offset);
121 bool SelectAddrMode5FP16(SDValue N, SDValue &Base, SDValue &Offset);
122 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
123 bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset);
124
125 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
126
127 // Thumb Addressing Modes:
128 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
129 bool SelectThumbAddrModeRRSext(SDValue N, SDValue &Base, SDValue &Offset);
130 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
131 SDValue &OffImm);
132 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
133 SDValue &OffImm);
134 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
135 SDValue &OffImm);
136 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
137 SDValue &OffImm);
138 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
139 template <unsigned Shift>
140 bool SelectTAddrModeImm7(SDValue N, SDValue &Base, SDValue &OffImm);
141
142 // Thumb 2 Addressing Modes:
143 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
144 template <unsigned Shift>
145 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base, SDValue &OffImm);
146 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
147 SDValue &OffImm);
148 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
149 SDValue &OffImm);
150 template <unsigned Shift>
151 bool SelectT2AddrModeImm7Offset(SDNode *Op, SDValue N, SDValue &OffImm);
152 bool SelectT2AddrModeImm7Offset(SDNode *Op, SDValue N, SDValue &OffImm,
153 unsigned Shift);
154 template <unsigned Shift>
155 bool SelectT2AddrModeImm7(SDValue N, SDValue &Base, SDValue &OffImm);
156 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
157 SDValue &OffReg, SDValue &ShImm);
158 bool SelectT2AddrModeExclusive(SDValue N, SDValue &Base, SDValue &OffImm);
159
160 template<int Min, int Max>
161 bool SelectImmediateInRange(SDValue N, SDValue &OffImm);
162
163 inline bool is_so_imm(unsigned Imm) const {
164 return ARM_AM::getSOImmVal(Imm) != -1;
165 }
166
167 inline bool is_so_imm_not(unsigned Imm) const {
168 return ARM_AM::getSOImmVal(~Imm) != -1;
169 }
170
171 inline bool is_t2_so_imm(unsigned Imm) const {
172 return ARM_AM::getT2SOImmVal(Imm) != -1;
173 }
174
175 inline bool is_t2_so_imm_not(unsigned Imm) const {
176 return ARM_AM::getT2SOImmVal(~Imm) != -1;
177 }
178
179 // Include the pieces autogenerated from the target description.
180#include "ARMGenDAGISel.inc"
181
182private:
183 void transferMemOperands(SDNode *Src, SDNode *Dst);
184
185 /// Indexed (pre/post inc/dec) load matching code for ARM.
186 bool tryARMIndexedLoad(SDNode *N);
187 bool tryT1IndexedLoad(SDNode *N);
188 bool tryT2IndexedLoad(SDNode *N);
189 bool tryMVEIndexedLoad(SDNode *N);
190 bool tryFMULFixed(SDNode *N, SDLoc dl);
191 bool tryFP_TO_INT(SDNode *N, SDLoc dl);
192 bool transformFixedFloatingPointConversion(SDNode *N, SDNode *FMul,
193 bool IsUnsigned,
194 bool FixedToFloat);
195
196 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
197 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
198 /// loads of D registers and even subregs and odd subregs of Q registers.
199 /// For NumVecs <= 2, QOpcodes1 is not used.
200 void SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
201 const uint16_t *DOpcodes, const uint16_t *QOpcodes0,
202 const uint16_t *QOpcodes1);
203
204 /// SelectVST - Select NEON store intrinsics. NumVecs should
205 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
206 /// stores of D registers and even subregs and odd subregs of Q registers.
207 /// For NumVecs <= 2, QOpcodes1 is not used.
208 void SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
209 const uint16_t *DOpcodes, const uint16_t *QOpcodes0,
210 const uint16_t *QOpcodes1);
211
212 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
213 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
214 /// load/store of D registers and Q registers.
215 void SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
216 unsigned NumVecs, const uint16_t *DOpcodes,
217 const uint16_t *QOpcodes);
218
219 /// Helper functions for setting up clusters of MVE predication operands.
220 template <typename SDValueVector>
221 void AddMVEPredicateToOps(SDValueVector &Ops, SDLoc Loc,
222 SDValue PredicateMask);
223 template <typename SDValueVector>
224 void AddMVEPredicateToOps(SDValueVector &Ops, SDLoc Loc,
225 SDValue PredicateMask, SDValue Inactive);
226
227 template <typename SDValueVector>
228 void AddEmptyMVEPredicateToOps(SDValueVector &Ops, SDLoc Loc);
229 template <typename SDValueVector>
230 void AddEmptyMVEPredicateToOps(SDValueVector &Ops, SDLoc Loc, EVT InactiveTy);
231
232 /// SelectMVE_WB - Select MVE writeback load/store intrinsics.
233 void SelectMVE_WB(SDNode *N, const uint16_t *Opcodes, bool Predicated);
234
235 /// SelectMVE_LongShift - Select MVE 64-bit scalar shift intrinsics.
236 void SelectMVE_LongShift(SDNode *N, uint16_t Opcode, bool Immediate,
237 bool HasSaturationOperand);
238
239 /// SelectMVE_VADCSBC - Select MVE vector add/sub-with-carry intrinsics.
240 void SelectMVE_VADCSBC(SDNode *N, uint16_t OpcodeWithCarry,
241 uint16_t OpcodeWithNoCarry, bool Add, bool Predicated);
242
243 /// SelectMVE_VSHLC - Select MVE intrinsics for a shift that carries between
244 /// vector lanes.
245 void SelectMVE_VSHLC(SDNode *N, bool Predicated);
246
247 /// Select long MVE vector reductions with two vector operands
248 /// Stride is the number of vector element widths the instruction can operate
249 /// on:
250 /// 2 for long non-rounding variants, vml{a,s}ldav[a][x]: [i16, i32]
251 /// 1 for long rounding variants: vrml{a,s}ldavh[a][x]: [i32]
252 /// Stride is used when addressing the OpcodesS array which contains multiple
253 /// opcodes for each element width.
254 /// TySize is the index into the list of element types listed above
255 void SelectBaseMVE_VMLLDAV(SDNode *N, bool Predicated,
256 const uint16_t *OpcodesS, const uint16_t *OpcodesU,
257 size_t Stride, size_t TySize);
258
259 /// Select a 64-bit MVE vector reduction with two vector operands
260 /// arm_mve_vmlldava_[predicated]
261 void SelectMVE_VMLLDAV(SDNode *N, bool Predicated, const uint16_t *OpcodesS,
262 const uint16_t *OpcodesU);
263 /// Select a 72-bit MVE vector rounding reduction with two vector operands
264 /// int_arm_mve_vrmlldavha[_predicated]
265 void SelectMVE_VRMLLDAVH(SDNode *N, bool Predicated, const uint16_t *OpcodesS,
266 const uint16_t *OpcodesU);
267
268 /// SelectMVE_VLD - Select MVE interleaving load intrinsics. NumVecs
269 /// should be 2 or 4. The opcode array specifies the instructions
270 /// used for 8, 16 and 32-bit lane sizes respectively, and each
271 /// pointer points to a set of NumVecs sub-opcodes used for the
272 /// different stages (e.g. VLD20 versus VLD21) of each load family.
273 void SelectMVE_VLD(SDNode *N, unsigned NumVecs,
274 const uint16_t *const *Opcodes, bool HasWriteback);
275
276 /// SelectMVE_VxDUP - Select MVE incrementing-dup instructions. Opcodes is an
277 /// array of 3 elements for the 8, 16 and 32-bit lane sizes.
278 void SelectMVE_VxDUP(SDNode *N, const uint16_t *Opcodes,
279 bool Wrapping, bool Predicated);
280
281 /// Select SelectCDE_CXxD - Select CDE dual-GPR instruction (one of CX1D,
282 /// CX1DA, CX2D, CX2DA, CX3, CX3DA).
283 /// \arg \c NumExtraOps number of extra operands besides the coprocossor,
284 /// the accumulator and the immediate operand, i.e. 0
285 /// for CX1*, 1 for CX2*, 2 for CX3*
286 /// \arg \c HasAccum whether the instruction has an accumulator operand
287 void SelectCDE_CXxD(SDNode *N, uint16_t Opcode, size_t NumExtraOps,
288 bool HasAccum);
289
290 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs
291 /// should be 1, 2, 3 or 4. The opcode array specifies the instructions used
292 /// for loading D registers.
293 void SelectVLDDup(SDNode *N, bool IsIntrinsic, bool isUpdating,
294 unsigned NumVecs, const uint16_t *DOpcodes,
295 const uint16_t *QOpcodes0 = nullptr,
296 const uint16_t *QOpcodes1 = nullptr);
297
298 /// Try to select SBFX/UBFX instructions for ARM.
299 bool tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
300
301 bool tryInsertVectorElt(SDNode *N);
302
303 bool tryShiftAmountMod(SDNode *N);
304
305 bool tryReadRegister(SDNode *N);
306 bool tryWriteRegister(SDNode *N);
307
308 bool tryInlineAsm(SDNode *N);
309
310 void SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI);
311
312 void SelectCMP_SWAP(SDNode *N);
313
314 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
315 /// inline asm expressions.
316 bool SelectInlineAsmMemoryOperand(const SDValue &Op,
317 InlineAsm::ConstraintCode ConstraintID,
318 std::vector<SDValue> &OutOps) override;
319
320 // Form pairs of consecutive R, S, D, or Q registers.
322 SDNode *createSRegPairNode(EVT VT, SDValue V0, SDValue V1);
323 SDNode *createDRegPairNode(EVT VT, SDValue V0, SDValue V1);
324 SDNode *createQRegPairNode(EVT VT, SDValue V0, SDValue V1);
325
326 // Form sequences of 4 consecutive S, D, or Q registers.
327 SDNode *createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
328 SDNode *createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
329 SDNode *createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
330
331 // Get the alignment operand for a NEON VLD or VST instruction.
332 SDValue GetVLDSTAlign(SDValue Align, const SDLoc &dl, unsigned NumVecs,
333 bool is64BitVector);
334
335 /// Checks if N is a multiplication by a constant where we can extract out a
336 /// power of two from the constant so that it can be used in a shift, but only
337 /// if it simplifies the materialization of the constant. Returns true if it
338 /// is, and assigns to PowerOfTwo the power of two that should be extracted
339 /// out and to NewMulConst the new constant to be multiplied by.
340 bool canExtractShiftFromMul(const SDValue &N, unsigned MaxShift,
341 unsigned &PowerOfTwo, SDValue &NewMulConst) const;
342
343 /// Replace N with M in CurDAG, in a way that also ensures that M gets
344 /// selected when N would have been selected.
345 void replaceDAGValue(const SDValue &N, SDValue M);
346};
347
348class ARMDAGToDAGISelLegacy : public SelectionDAGISelLegacy {
349public:
350 static char ID;
351 ARMDAGToDAGISelLegacy(ARMBaseTargetMachine &tm, CodeGenOptLevel OptLevel)
353 ID, std::make_unique<ARMDAGToDAGISel>(tm, OptLevel)) {}
354};
355}
356
357char ARMDAGToDAGISelLegacy::ID = 0;
358
359INITIALIZE_PASS(ARMDAGToDAGISelLegacy, DEBUG_TYPE, PASS_NAME, false, false)
360
361/// isInt32Immediate - This method tests to see if the node is a 32-bit constant
362/// operand. If so Imm will receive the 32-bit value.
363static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
364 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
365 Imm = N->getAsZExtVal();
366 return true;
367 }
368 return false;
369}
370
371// isInt32Immediate - This method tests to see if a constant operand.
372// If so Imm will receive the 32 bit value.
373static bool isInt32Immediate(SDValue N, unsigned &Imm) {
374 return isInt32Immediate(N.getNode(), Imm);
375}
376
377// isOpcWithIntImmediate - This method tests to see if the node is a specific
378// opcode and that it has a immediate integer right operand.
379// If so Imm will receive the 32 bit value.
380static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
381 return N->getOpcode() == Opc &&
382 isInt32Immediate(N->getOperand(1).getNode(), Imm);
383}
384
385/// Check whether a particular node is a constant value representable as
386/// (N * Scale) where (N in [\p RangeMin, \p RangeMax).
387///
388/// \param ScaledConstant [out] - On success, the pre-scaled constant value.
389static bool isScaledConstantInRange(SDValue Node, int Scale,
390 int RangeMin, int RangeMax,
391 int &ScaledConstant) {
392 assert(Scale > 0 && "Invalid scale!");
393
394 // Check that this is a constant.
396 if (!C)
397 return false;
398
399 ScaledConstant = (int) C->getZExtValue();
400 if ((ScaledConstant % Scale) != 0)
401 return false;
402
403 ScaledConstant /= Scale;
404 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
405}
406
407void ARMDAGToDAGISel::PreprocessISelDAG() {
408 if (!Subtarget->hasV6T2Ops())
409 return;
410
411 bool isThumb2 = Subtarget->isThumb();
412 // We use make_early_inc_range to avoid invalidation issues.
413 for (SDNode &N : llvm::make_early_inc_range(CurDAG->allnodes())) {
414 if (N.getOpcode() != ISD::ADD)
415 continue;
416
417 // Look for (add X1, (and (srl X2, c1), c2)) where c2 is constant with
418 // leading zeros, followed by consecutive set bits, followed by 1 or 2
419 // trailing zeros, e.g. 1020.
420 // Transform the expression to
421 // (add X1, (shl (and (srl X2, c1), (c2>>tz)), tz)) where tz is the number
422 // of trailing zeros of c2. The left shift would be folded as an shifter
423 // operand of 'add' and the 'and' and 'srl' would become a bits extraction
424 // node (UBFX).
425
426 SDValue N0 = N.getOperand(0);
427 SDValue N1 = N.getOperand(1);
428 unsigned And_imm = 0;
429 if (!isOpcWithIntImmediate(N1.getNode(), ISD::AND, And_imm)) {
430 if (isOpcWithIntImmediate(N0.getNode(), ISD::AND, And_imm))
431 std::swap(N0, N1);
432 }
433 if (!And_imm)
434 continue;
435
436 // Check if the AND mask is an immediate of the form: 000.....1111111100
437 unsigned TZ = llvm::countr_zero(And_imm);
438 if (TZ != 1 && TZ != 2)
439 // Be conservative here. Shifter operands aren't always free. e.g. On
440 // Swift, left shifter operand of 1 / 2 for free but others are not.
441 // e.g.
442 // ubfx r3, r1, #16, #8
443 // ldr.w r3, [r0, r3, lsl #2]
444 // vs.
445 // mov.w r9, #1020
446 // and.w r2, r9, r1, lsr #14
447 // ldr r2, [r0, r2]
448 continue;
449 And_imm >>= TZ;
450 if (And_imm & (And_imm + 1))
451 continue;
452
453 // Look for (and (srl X, c1), c2).
454 SDValue Srl = N1.getOperand(0);
455 unsigned Srl_imm = 0;
456 if (!isOpcWithIntImmediate(Srl.getNode(), ISD::SRL, Srl_imm) ||
457 (Srl_imm <= 2))
458 continue;
459
460 // Make sure first operand is not a shifter operand which would prevent
461 // folding of the left shift.
462 SDValue CPTmp0;
463 SDValue CPTmp1;
464 SDValue CPTmp2;
465 if (isThumb2) {
466 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1))
467 continue;
468 } else {
469 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1) ||
470 SelectRegShifterOperand(N0, CPTmp0, CPTmp1, CPTmp2))
471 continue;
472 }
473
474 // Now make the transformation.
475 Srl = CurDAG->getNode(ISD::SRL, SDLoc(Srl), MVT::i32,
476 Srl.getOperand(0),
477 CurDAG->getConstant(Srl_imm + TZ, SDLoc(Srl),
478 MVT::i32));
479 N1 = CurDAG->getNode(ISD::AND, SDLoc(N1), MVT::i32,
480 Srl,
481 CurDAG->getConstant(And_imm, SDLoc(Srl), MVT::i32));
482 N1 = CurDAG->getNode(ISD::SHL, SDLoc(N1), MVT::i32,
483 N1, CurDAG->getConstant(TZ, SDLoc(Srl), MVT::i32));
484 CurDAG->UpdateNodeOperands(&N, N0, N1);
485 }
486}
487
488/// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
489/// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
490/// least on current ARM implementations) which should be avoidded.
491bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
492 if (OptLevel == CodeGenOptLevel::None)
493 return true;
494
495 if (!Subtarget->hasVMLxHazards())
496 return true;
497
498 if (!N->hasOneUse())
499 return false;
500
501 SDNode *User = *N->user_begin();
502 if (User->getOpcode() == ISD::CopyToReg)
503 return true;
504 if (User->isMachineOpcode()) {
505 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
506 CurDAG->getSubtarget().getInstrInfo());
507
508 const MCInstrDesc &MCID = TII->get(User->getMachineOpcode());
509 if (MCID.mayStore())
510 return true;
511 unsigned Opcode = MCID.getOpcode();
512 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
513 return true;
514 // vmlx feeding into another vmlx. We actually want to unfold
515 // the use later in the MLxExpansion pass. e.g.
516 // vmla
517 // vmla (stall 8 cycles)
518 //
519 // vmul (5 cycles)
520 // vadd (5 cycles)
521 // vmla
522 // This adds up to about 18 - 19 cycles.
523 //
524 // vmla
525 // vmul (stall 4 cycles)
526 // vadd adds up to about 14 cycles.
527 return TII->isFpMLxInstruction(Opcode);
528 }
529
530 return false;
531}
532
533bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
534 ARM_AM::ShiftOpc ShOpcVal,
535 unsigned ShAmt) {
536 if (!Subtarget->isLikeA9() && !Subtarget->isSwift())
537 return true;
538 if (Shift.hasOneUse())
539 return true;
540 // R << 2 is free.
541 return ShOpcVal == ARM_AM::lsl &&
542 (ShAmt == 2 || (Subtarget->isSwift() && ShAmt == 1));
543}
544
545bool ARMDAGToDAGISel::canExtractShiftFromMul(const SDValue &N,
546 unsigned MaxShift,
547 unsigned &PowerOfTwo,
548 SDValue &NewMulConst) const {
549 assert(N.getOpcode() == ISD::MUL);
550 assert(MaxShift > 0);
551
552 // If the multiply is used in more than one place then changing the constant
553 // will make other uses incorrect, so don't.
554 if (!N.hasOneUse()) return false;
555 // Check if the multiply is by a constant
556 ConstantSDNode *MulConst = dyn_cast<ConstantSDNode>(N.getOperand(1));
557 if (!MulConst) return false;
558 // If the constant is used in more than one place then modifying it will mean
559 // we need to materialize two constants instead of one, which is a bad idea.
560 if (!MulConst->hasOneUse()) return false;
561 unsigned MulConstVal = MulConst->getZExtValue();
562 if (MulConstVal == 0) return false;
563
564 // Find the largest power of 2 that MulConstVal is a multiple of
565 PowerOfTwo = MaxShift;
566 while ((MulConstVal % (1 << PowerOfTwo)) != 0) {
567 --PowerOfTwo;
568 if (PowerOfTwo == 0) return false;
569 }
570
571 // Only optimise if the new cost is better
572 unsigned NewMulConstVal = MulConstVal / (1 << PowerOfTwo);
573 NewMulConst = CurDAG->getConstant(NewMulConstVal, SDLoc(N), MVT::i32);
574 unsigned OldCost = ConstantMaterializationCost(MulConstVal, Subtarget);
575 unsigned NewCost = ConstantMaterializationCost(NewMulConstVal, Subtarget);
576 return NewCost < OldCost;
577}
578
579void ARMDAGToDAGISel::replaceDAGValue(const SDValue &N, SDValue M) {
580 CurDAG->RepositionNode(N.getNode()->getIterator(), M.getNode());
581 ReplaceUses(N, M);
582}
583
584bool ARMDAGToDAGISel::SelectImmShifterOperand(SDValue N,
585 SDValue &BaseReg,
586 SDValue &Opc,
587 bool CheckProfitability) {
588 if (!Subtarget->getCLOpts().shifter_op)
589 return false;
590
591 // If N is a multiply-by-constant and it's profitable to extract a shift and
592 // use it in a shifted operand do so.
593 if (N.getOpcode() == ISD::MUL) {
594 unsigned PowerOfTwo = 0;
595 SDValue NewMulConst;
596 if (canExtractShiftFromMul(N, 31, PowerOfTwo, NewMulConst)) {
597 HandleSDNode Handle(N);
598 SDLoc Loc(N);
599 replaceDAGValue(N.getOperand(1), NewMulConst);
600 BaseReg = Handle.getValue();
601 Opc = CurDAG->getTargetConstant(
602 ARM_AM::getSORegOpc(ARM_AM::lsl, PowerOfTwo), Loc, MVT::i32);
603 return true;
604 }
605 }
606
607 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
608
609 // Don't match base register only case. That is matched to a separate
610 // lower complexity pattern with explicit register operand.
611 if (ShOpcVal == ARM_AM::no_shift) return false;
612
613 BaseReg = N.getOperand(0);
614 unsigned ShImmVal = 0;
615 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
616 if (!RHS) return false;
617 ShImmVal = RHS->getZExtValue() & 31;
618 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
619 SDLoc(N), MVT::i32);
620 return true;
621}
622
623bool ARMDAGToDAGISel::SelectRegShifterOperand(SDValue N,
624 SDValue &BaseReg,
625 SDValue &ShReg,
626 SDValue &Opc,
627 bool CheckProfitability) {
628 if (!Subtarget->getCLOpts().shifter_op)
629 return false;
630
631 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
632
633 // Don't match base register only case. That is matched to a separate
634 // lower complexity pattern with explicit register operand.
635 if (ShOpcVal == ARM_AM::no_shift) return false;
636
637 BaseReg = N.getOperand(0);
638 unsigned ShImmVal = 0;
639 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
640 if (RHS) return false;
641
642 ShReg = N.getOperand(1);
643 if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal))
644 return false;
645 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
646 SDLoc(N), MVT::i32);
647 return true;
648}
649
650// Determine whether an ISD::OR's operands are suitable to turn the operation
651// into an addition, which often has more compact encodings.
652bool ARMDAGToDAGISel::SelectAddLikeOr(SDNode *Parent, SDValue N, SDValue &Out) {
653 assert(Parent->getOpcode() == ISD::OR && "unexpected parent");
654 Out = N;
655 return CurDAG->haveNoCommonBitsSet(N, Parent->getOperand(1));
656}
657
658
659bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
660 SDValue &Base,
661 SDValue &OffImm) {
662 // Match simple R + imm12 operands.
663
664 // Base only.
665 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
666 !CurDAG->isBaseWithConstantOffset(N)) {
667 if (N.getOpcode() == ISD::FrameIndex) {
668 // Match frame index.
669 int FI = cast<FrameIndexSDNode>(N)->getIndex();
670 Base = CurDAG->getTargetFrameIndex(
671 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
672 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
673 return true;
674 }
675
676 if (N.getOpcode() == ARMISD::Wrapper &&
677 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
678 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
679 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
680 Base = N.getOperand(0);
681 } else
682 Base = N;
683 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
684 return true;
685 }
686
687 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
688 int RHSC = (int)RHS->getSExtValue();
689 if (N.getOpcode() == ISD::SUB)
690 RHSC = -RHSC;
691
692 if (RHSC > -0x1000 && RHSC < 0x1000) { // 12 bits
693 Base = N.getOperand(0);
694 if (Base.getOpcode() == ISD::FrameIndex) {
695 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
696 Base = CurDAG->getTargetFrameIndex(
697 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
698 }
699 OffImm = CurDAG->getSignedTargetConstant(RHSC, SDLoc(N), MVT::i32);
700 return true;
701 }
702 }
703
704 // Base only.
705 Base = N;
706 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
707 return true;
708}
709
710
711
712bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
713 SDValue &Opc) {
714 if (N.getOpcode() == ISD::MUL &&
715 ((!Subtarget->isLikeA9() && !Subtarget->isSwift()) || N.hasOneUse())) {
716 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
717 // X * [3,5,9] -> X + X * [2,4,8] etc.
718 int RHSC = (int)RHS->getZExtValue();
719 if (RHSC & 1) {
720 RHSC = RHSC & ~1;
722 if (RHSC < 0) {
724 RHSC = - RHSC;
725 }
726 if (isPowerOf2_32(RHSC)) {
727 unsigned ShAmt = Log2_32(RHSC);
728 Base = Offset = N.getOperand(0);
729 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
731 SDLoc(N), MVT::i32);
732 return true;
733 }
734 }
735 }
736 }
737
738 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
739 // ISD::OR that is equivalent to an ISD::ADD.
740 !CurDAG->isBaseWithConstantOffset(N))
741 return false;
742
743 // Leave simple R +/- imm12 operands for LDRi12
744 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
745 int RHSC;
746 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
747 -0x1000+1, 0x1000, RHSC)) // 12 bits.
748 return false;
749 }
750
751 // Otherwise this is R +/- [possibly shifted] R.
753 ARM_AM::ShiftOpc ShOpcVal =
754 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
755 unsigned ShAmt = 0;
756
757 Base = N.getOperand(0);
758 Offset = N.getOperand(1);
759
760 if (ShOpcVal != ARM_AM::no_shift) {
761 // Check to see if the RHS of the shift is a constant, if not, we can't fold
762 // it.
763 if (ConstantSDNode *Sh =
764 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
765 ShAmt = Sh->getZExtValue();
766 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
767 Offset = N.getOperand(1).getOperand(0);
768 else {
769 ShAmt = 0;
770 ShOpcVal = ARM_AM::no_shift;
771 }
772 } else {
773 ShOpcVal = ARM_AM::no_shift;
774 }
775 }
776
777 // Try matching (R shl C) + (R).
778 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
779 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
780 N.getOperand(0).hasOneUse())) {
781 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
782 if (ShOpcVal != ARM_AM::no_shift) {
783 // Check to see if the RHS of the shift is a constant, if not, we can't
784 // fold it.
785 if (ConstantSDNode *Sh =
786 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
787 ShAmt = Sh->getZExtValue();
788 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
789 Offset = N.getOperand(0).getOperand(0);
790 Base = N.getOperand(1);
791 } else {
792 ShAmt = 0;
793 ShOpcVal = ARM_AM::no_shift;
794 }
795 } else {
796 ShOpcVal = ARM_AM::no_shift;
797 }
798 }
799 }
800
801 // If Offset is a multiply-by-constant and it's profitable to extract a shift
802 // and use it in a shifted operand do so.
803 if (Offset.getOpcode() == ISD::MUL && N.hasOneUse()) {
804 unsigned PowerOfTwo = 0;
805 SDValue NewMulConst;
806 if (canExtractShiftFromMul(Offset, 31, PowerOfTwo, NewMulConst)) {
807 HandleSDNode Handle(Offset);
808 replaceDAGValue(Offset.getOperand(1), NewMulConst);
809 Offset = Handle.getValue();
810 ShAmt = PowerOfTwo;
811 ShOpcVal = ARM_AM::lsl;
812 }
813 }
814
815 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
816 SDLoc(N), MVT::i32);
817 return true;
818}
819
820bool ARMDAGToDAGISel::SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
822 unsigned Opcode = Op->getOpcode();
823 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
824 ? cast<LoadSDNode>(Op)->getAddressingMode()
825 : cast<StoreSDNode>(Op)->getAddressingMode();
827 ? ARM_AM::add : ARM_AM::sub;
828 int Val;
829 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val))
830 return false;
831
832 Offset = N;
833 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
834 unsigned ShAmt = 0;
835 if (ShOpcVal != ARM_AM::no_shift) {
836 // Check to see if the RHS of the shift is a constant, if not, we can't fold
837 // it.
838 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
839 ShAmt = Sh->getZExtValue();
840 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
841 Offset = N.getOperand(0);
842 else {
843 ShAmt = 0;
844 ShOpcVal = ARM_AM::no_shift;
845 }
846 } else {
847 ShOpcVal = ARM_AM::no_shift;
848 }
849 }
850
851 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
852 SDLoc(N), MVT::i32);
853 return true;
854}
855
856bool ARMDAGToDAGISel::SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
858 unsigned Opcode = Op->getOpcode();
859 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
860 ? cast<LoadSDNode>(Op)->getAddressingMode()
861 : cast<StoreSDNode>(Op)->getAddressingMode();
863 ? ARM_AM::add : ARM_AM::sub;
864 int Val;
865 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
866 if (AddSub == ARM_AM::sub) Val *= -1;
867 Offset = CurDAG->getRegister(0, MVT::i32);
868 Opc = CurDAG->getSignedTargetConstant(Val, SDLoc(Op), MVT::i32);
869 return true;
870 }
871
872 return false;
873}
874
875
876bool ARMDAGToDAGISel::SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
878 unsigned Opcode = Op->getOpcode();
879 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
880 ? cast<LoadSDNode>(Op)->getAddressingMode()
881 : cast<StoreSDNode>(Op)->getAddressingMode();
883 ? ARM_AM::add : ARM_AM::sub;
884 int Val;
885 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
886 Offset = CurDAG->getRegister(0, MVT::i32);
887 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
889 SDLoc(Op), MVT::i32);
890 return true;
891 }
892
893 return false;
894}
895
896bool ARMDAGToDAGISel::SelectAddrOffsetNone(SDValue N, SDValue &Base) {
897 Base = N;
898 return true;
899}
900
901bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
903 SDValue &Opc) {
904 if (N.getOpcode() == ISD::SUB) {
905 // X - C is canonicalize to X + -C, no need to handle it here.
906 Base = N.getOperand(0);
907 Offset = N.getOperand(1);
908 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0), SDLoc(N),
909 MVT::i32);
910 return true;
911 }
912
913 if (!CurDAG->isBaseWithConstantOffset(N)) {
914 Base = N;
915 if (N.getOpcode() == ISD::FrameIndex) {
916 int FI = cast<FrameIndexSDNode>(N)->getIndex();
917 Base = CurDAG->getTargetFrameIndex(
918 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
919 }
920 Offset = CurDAG->getRegister(0, MVT::i32);
921 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
922 MVT::i32);
923 return true;
924 }
925
926 // If the RHS is +/- imm8, fold into addr mode.
927 int RHSC;
928 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
929 -256 + 1, 256, RHSC)) { // 8 bits.
930 Base = N.getOperand(0);
931 if (Base.getOpcode() == ISD::FrameIndex) {
932 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
933 Base = CurDAG->getTargetFrameIndex(
934 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
935 }
936 Offset = CurDAG->getRegister(0, MVT::i32);
937
939 if (RHSC < 0) {
941 RHSC = -RHSC;
942 }
943 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC), SDLoc(N),
944 MVT::i32);
945 return true;
946 }
947
948 Base = N.getOperand(0);
949 Offset = N.getOperand(1);
950 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
951 MVT::i32);
952 return true;
953}
954
955bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
957 unsigned Opcode = Op->getOpcode();
958 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
959 ? cast<LoadSDNode>(Op)->getAddressingMode()
960 : cast<StoreSDNode>(Op)->getAddressingMode();
962 ? ARM_AM::add : ARM_AM::sub;
963 int Val;
964 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
965 Offset = CurDAG->getRegister(0, MVT::i32);
966 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), SDLoc(Op),
967 MVT::i32);
968 return true;
969 }
970
971 Offset = N;
972 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), SDLoc(Op),
973 MVT::i32);
974 return true;
975}
976
977bool ARMDAGToDAGISel::IsAddressingMode5(SDValue N, SDValue &Base, SDValue &Offset,
978 bool FP16) {
979 if (!CurDAG->isBaseWithConstantOffset(N)) {
980 Base = N;
981 if (N.getOpcode() == ISD::FrameIndex) {
982 int FI = cast<FrameIndexSDNode>(N)->getIndex();
983 Base = CurDAG->getTargetFrameIndex(
984 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
985 } else if (N.getOpcode() == ARMISD::Wrapper &&
986 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
987 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
988 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
989 Base = N.getOperand(0);
990 }
991 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
992 SDLoc(N), MVT::i32);
993 return true;
994 }
995
996 // If the RHS is +/- imm8, fold into addr mode.
997 int RHSC;
998 const int Scale = FP16 ? 2 : 4;
999
1000 if (isScaledConstantInRange(N.getOperand(1), Scale, -255, 256, RHSC)) {
1001 Base = N.getOperand(0);
1002 if (Base.getOpcode() == ISD::FrameIndex) {
1003 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1004 Base = CurDAG->getTargetFrameIndex(
1005 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1006 }
1007
1009 if (RHSC < 0) {
1011 RHSC = -RHSC;
1012 }
1013
1014 if (FP16)
1015 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5FP16Opc(AddSub, RHSC),
1016 SDLoc(N), MVT::i32);
1017 else
1018 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
1019 SDLoc(N), MVT::i32);
1020
1021 return true;
1022 }
1023
1024 Base = N;
1025
1026 if (FP16)
1027 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5FP16Opc(ARM_AM::add, 0),
1028 SDLoc(N), MVT::i32);
1029 else
1030 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
1031 SDLoc(N), MVT::i32);
1032
1033 return true;
1034}
1035
1036bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
1038 return IsAddressingMode5(N, Base, Offset, /*FP16=*/ false);
1039}
1040
1041bool ARMDAGToDAGISel::SelectAddrMode5FP16(SDValue N,
1043 return IsAddressingMode5(N, Base, Offset, /*FP16=*/ true);
1044}
1045
1046bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
1047 SDValue &Align) {
1048 Addr = N;
1049
1050 unsigned Alignment = 0;
1051
1052 MemSDNode *MemN = cast<MemSDNode>(Parent);
1053
1054 if (isa<LSBaseSDNode>(MemN) ||
1055 ((MemN->getOpcode() == ARMISD::VST1_UPD ||
1056 MemN->getOpcode() == ARMISD::VLD1_UPD) &&
1057 MemN->getConstantOperandVal(MemN->getNumOperands() - 1) == 1)) {
1058 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
1059 // The maximum alignment is equal to the memory size being referenced.
1060 llvm::Align MMOAlign = MemN->getAlign();
1061 unsigned MemSize = MemN->getMemoryVT().getSizeInBits() / 8;
1062 if (MMOAlign.value() >= MemSize && MemSize > 1)
1063 Alignment = MemSize;
1064 } else {
1065 // All other uses of addrmode6 are for intrinsics. For now just record
1066 // the raw alignment value; it will be refined later based on the legal
1067 // alignment operands for the intrinsic.
1068 Alignment = MemN->getAlign().value();
1069 }
1070
1071 Align = CurDAG->getTargetConstant(Alignment, SDLoc(N), MVT::i32);
1072 return true;
1073}
1074
1075bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N,
1076 SDValue &Offset) {
1077 LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op);
1079 if (AM != ISD::POST_INC)
1080 return false;
1081 Offset = N;
1082 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) {
1083 if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits())
1084 Offset = CurDAG->getRegister(0, MVT::i32);
1085 }
1086 return true;
1087}
1088
1089bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
1090 SDValue &Offset, SDValue &Label) {
1091 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
1092 Offset = N.getOperand(0);
1093 SDValue N1 = N.getOperand(1);
1094 Label = CurDAG->getTargetConstant(N1->getAsZExtVal(), SDLoc(N), MVT::i32);
1095 return true;
1096 }
1097
1098 return false;
1099}
1100
1101
1102//===----------------------------------------------------------------------===//
1103// Thumb Addressing Modes
1104//===----------------------------------------------------------------------===//
1105
1107 // Negative numbers are difficult to materialise in thumb1. If we are
1108 // selecting the add of a negative, instead try to select ri with a zero
1109 // offset, so create the add node directly which will become a sub.
1110 if (N.getOpcode() != ISD::ADD)
1111 return false;
1112
1113 // Look for an imm which is not legal for ld/st, but is legal for sub.
1114 if (auto C = dyn_cast<ConstantSDNode>(N.getOperand(1)))
1115 return C->getSExtValue() < 0 && C->getSExtValue() >= -255;
1116
1117 return false;
1118}
1119
1120bool ARMDAGToDAGISel::SelectThumbAddrModeRRSext(SDValue N, SDValue &Base,
1121 SDValue &Offset) {
1122 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) {
1123 if (!isNullConstant(N))
1124 return false;
1125
1126 Base = Offset = N;
1127 return true;
1128 }
1129
1130 Base = N.getOperand(0);
1131 Offset = N.getOperand(1);
1132 return true;
1133}
1134
1135bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N, SDValue &Base,
1136 SDValue &Offset) {
1138 return false; // Select ri instead
1139 return SelectThumbAddrModeRRSext(N, Base, Offset);
1140}
1141
1142bool
1143ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
1144 SDValue &Base, SDValue &OffImm) {
1146 Base = N;
1147 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1148 return true;
1149 }
1150
1151 if (!CurDAG->isBaseWithConstantOffset(N)) {
1152 if (N.getOpcode() == ISD::ADD) {
1153 return false; // We want to select register offset instead
1154 } else if (N.getOpcode() == ARMISD::Wrapper &&
1155 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1156 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1157 N.getOperand(0).getOpcode() != ISD::TargetConstantPool &&
1158 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1159 Base = N.getOperand(0);
1160 } else {
1161 Base = N;
1162 }
1163
1164 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1165 return true;
1166 }
1167
1168 // If the RHS is + imm5 * scale, fold into addr mode.
1169 int RHSC;
1170 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1171 Base = N.getOperand(0);
1172 OffImm = CurDAG->getSignedTargetConstant(RHSC, SDLoc(N), MVT::i32);
1173 return true;
1174 }
1175
1176 // Offset is too large, so use register offset instead.
1177 return false;
1178}
1179
1180bool
1181ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1182 SDValue &OffImm) {
1183 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
1184}
1185
1186bool
1187ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1188 SDValue &OffImm) {
1189 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
1190}
1191
1192bool
1193ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1194 SDValue &OffImm) {
1195 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
1196}
1197
1198bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1199 SDValue &Base, SDValue &OffImm) {
1200 if (N.getOpcode() == ISD::FrameIndex) {
1201 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1202 // Only multiples of 4 are allowed for the offset, so the frame object
1203 // alignment must be at least 4.
1204 MachineFrameInfo &MFI = MF->getFrameInfo();
1205 if (MFI.getObjectAlign(FI) < Align(4))
1206 MFI.setObjectAlignment(FI, Align(4));
1207 Base = CurDAG->getTargetFrameIndex(
1208 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1209 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1210 return true;
1211 }
1212
1213 if (!CurDAG->isBaseWithConstantOffset(N))
1214 return false;
1215
1216 if (N.getOperand(0).getOpcode() == ISD::FrameIndex) {
1217 // If the RHS is + imm8 * scale, fold into addr mode.
1218 int RHSC;
1219 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1220 Base = N.getOperand(0);
1221 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1222 // Make sure the offset is inside the object, or we might fail to
1223 // allocate an emergency spill slot. (An out-of-range access is UB, but
1224 // it could show up anyway.)
1225 MachineFrameInfo &MFI = MF->getFrameInfo();
1226 if (RHSC * 4 < MFI.getObjectSize(FI)) {
1227 // For LHS+RHS to result in an offset that's a multiple of 4 the object
1228 // indexed by the LHS must be 4-byte aligned.
1229 if (!MFI.isFixedObjectIndex(FI) && MFI.getObjectAlign(FI) < Align(4))
1230 MFI.setObjectAlignment(FI, Align(4));
1231 if (MFI.getObjectAlign(FI) >= Align(4)) {
1232 Base = CurDAG->getTargetFrameIndex(
1233 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1234 OffImm = CurDAG->getSignedTargetConstant(RHSC, SDLoc(N), MVT::i32);
1235 return true;
1236 }
1237 }
1238 }
1239 }
1240
1241 return false;
1242}
1243
1244template <unsigned Shift>
1245bool ARMDAGToDAGISel::SelectTAddrModeImm7(SDValue N, SDValue &Base,
1246 SDValue &OffImm) {
1247 if (N.getOpcode() == ISD::SUB || CurDAG->isBaseWithConstantOffset(N)) {
1248 int RHSC;
1249 if (isScaledConstantInRange(N.getOperand(1), 1 << Shift, -0x7f, 0x80,
1250 RHSC)) {
1251 Base = N.getOperand(0);
1252 if (N.getOpcode() == ISD::SUB)
1253 RHSC = -RHSC;
1254 OffImm = CurDAG->getSignedTargetConstant(RHSC * (1 << Shift), SDLoc(N),
1255 MVT::i32);
1256 return true;
1257 }
1258 }
1259
1260 // Base only.
1261 Base = N;
1262 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1263 return true;
1264}
1265
1266
1267//===----------------------------------------------------------------------===//
1268// Thumb 2 Addressing Modes
1269//===----------------------------------------------------------------------===//
1270
1271
1272bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
1273 SDValue &Base, SDValue &OffImm) {
1274 // Match simple R + imm12 operands.
1275
1276 // Base only.
1277 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1278 !CurDAG->isBaseWithConstantOffset(N)) {
1279 if (N.getOpcode() == ISD::FrameIndex) {
1280 // Match frame index.
1281 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1282 Base = CurDAG->getTargetFrameIndex(
1283 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1284 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1285 return true;
1286 }
1287
1288 if (N.getOpcode() == ARMISD::Wrapper &&
1289 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1290 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1291 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1292 Base = N.getOperand(0);
1293 if (Base.getOpcode() == ISD::TargetConstantPool)
1294 return false; // We want to select t2LDRpci instead.
1295 } else
1296 Base = N;
1297 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1298 return true;
1299 }
1300
1301 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1302 if (SelectT2AddrModeImm8(N, Base, OffImm))
1303 // Let t2LDRi8 handle (R - imm8).
1304 return false;
1305
1306 int RHSC = (int)RHS->getZExtValue();
1307 if (N.getOpcode() == ISD::SUB)
1308 RHSC = -RHSC;
1309
1310 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
1311 Base = N.getOperand(0);
1312 if (Base.getOpcode() == ISD::FrameIndex) {
1313 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1314 Base = CurDAG->getTargetFrameIndex(
1315 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1316 }
1317 OffImm = CurDAG->getSignedTargetConstant(RHSC, SDLoc(N), MVT::i32);
1318 return true;
1319 }
1320 }
1321
1322 // Base only.
1323 Base = N;
1324 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1325 return true;
1326}
1327
1328template <unsigned Shift>
1329bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N, SDValue &Base,
1330 SDValue &OffImm) {
1331 if (N.getOpcode() == ISD::SUB || CurDAG->isBaseWithConstantOffset(N)) {
1332 int RHSC;
1333 if (isScaledConstantInRange(N.getOperand(1), 1 << Shift, -255, 256, RHSC)) {
1334 Base = N.getOperand(0);
1335 if (Base.getOpcode() == ISD::FrameIndex) {
1336 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1337 Base = CurDAG->getTargetFrameIndex(
1338 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1339 }
1340
1341 if (N.getOpcode() == ISD::SUB)
1342 RHSC = -RHSC;
1343 OffImm = CurDAG->getSignedTargetConstant(RHSC * (1 << Shift), SDLoc(N),
1344 MVT::i32);
1345 return true;
1346 }
1347 }
1348
1349 // Base only.
1350 Base = N;
1351 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1352 return true;
1353}
1354
1355bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
1356 SDValue &Base, SDValue &OffImm) {
1357 // Match simple R - imm8 operands.
1358 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1359 !CurDAG->isBaseWithConstantOffset(N))
1360 return false;
1361
1362 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1363 int RHSC = (int)RHS->getSExtValue();
1364 if (N.getOpcode() == ISD::SUB)
1365 RHSC = -RHSC;
1366
1367 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1368 Base = N.getOperand(0);
1369 if (Base.getOpcode() == ISD::FrameIndex) {
1370 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1371 Base = CurDAG->getTargetFrameIndex(
1372 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1373 }
1374 OffImm = CurDAG->getSignedTargetConstant(RHSC, SDLoc(N), MVT::i32);
1375 return true;
1376 }
1377 }
1378
1379 return false;
1380}
1381
1382bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
1383 SDValue &OffImm){
1384 unsigned Opcode = Op->getOpcode();
1385 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1386 ? cast<LoadSDNode>(Op)->getAddressingMode()
1387 : cast<StoreSDNode>(Op)->getAddressingMode();
1388 int RHSC;
1389 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1390 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1391 ? CurDAG->getSignedTargetConstant(RHSC, SDLoc(N), MVT::i32)
1392 : CurDAG->getSignedTargetConstant(-RHSC, SDLoc(N), MVT::i32);
1393 return true;
1394 }
1395
1396 return false;
1397}
1398
1399template <unsigned Shift>
1400bool ARMDAGToDAGISel::SelectT2AddrModeImm7(SDValue N, SDValue &Base,
1401 SDValue &OffImm) {
1402 if (N.getOpcode() == ISD::SUB || CurDAG->isBaseWithConstantOffset(N)) {
1403 int RHSC;
1404 if (isScaledConstantInRange(N.getOperand(1), 1 << Shift, -0x7f, 0x80,
1405 RHSC)) {
1406 Base = N.getOperand(0);
1407 if (Base.getOpcode() == ISD::FrameIndex) {
1408 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1409 Base = CurDAG->getTargetFrameIndex(
1410 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1411 }
1412
1413 if (N.getOpcode() == ISD::SUB)
1414 RHSC = -RHSC;
1415 OffImm = CurDAG->getSignedTargetConstant(RHSC * (1 << Shift), SDLoc(N),
1416 MVT::i32);
1417 return true;
1418 }
1419 }
1420
1421 // Base only.
1422 Base = N;
1423 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1424 return true;
1425}
1426
1427template <unsigned Shift>
1428bool ARMDAGToDAGISel::SelectT2AddrModeImm7Offset(SDNode *Op, SDValue N,
1429 SDValue &OffImm) {
1430 return SelectT2AddrModeImm7Offset(Op, N, OffImm, Shift);
1431}
1432
1433bool ARMDAGToDAGISel::SelectT2AddrModeImm7Offset(SDNode *Op, SDValue N,
1434 SDValue &OffImm,
1435 unsigned Shift) {
1436 unsigned Opcode = Op->getOpcode();
1438 switch (Opcode) {
1439 case ISD::LOAD:
1440 AM = cast<LoadSDNode>(Op)->getAddressingMode();
1441 break;
1442 case ISD::STORE:
1443 AM = cast<StoreSDNode>(Op)->getAddressingMode();
1444 break;
1445 case ISD::MLOAD:
1446 AM = cast<MaskedLoadSDNode>(Op)->getAddressingMode();
1447 break;
1448 case ISD::MSTORE:
1449 AM = cast<MaskedStoreSDNode>(Op)->getAddressingMode();
1450 break;
1451 default:
1452 llvm_unreachable("Unexpected Opcode for Imm7Offset");
1453 }
1454
1455 int RHSC;
1456 // 7 bit constant, shifted by Shift.
1457 if (isScaledConstantInRange(N, 1 << Shift, 0, 0x80, RHSC)) {
1458 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1459 ? CurDAG->getSignedTargetConstant(RHSC * (1 << Shift),
1460 SDLoc(N), MVT::i32)
1461 : CurDAG->getSignedTargetConstant(-RHSC * (1 << Shift),
1462 SDLoc(N), MVT::i32);
1463 return true;
1464 }
1465 return false;
1466}
1467
1468template <int Min, int Max>
1469bool ARMDAGToDAGISel::SelectImmediateInRange(SDValue N, SDValue &OffImm) {
1470 int Val;
1471 if (isScaledConstantInRange(N, 1, Min, Max, Val)) {
1472 OffImm = CurDAG->getSignedTargetConstant(Val, SDLoc(N), MVT::i32);
1473 return true;
1474 }
1475 return false;
1476}
1477
1478bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
1479 SDValue &Base,
1480 SDValue &OffReg, SDValue &ShImm) {
1481 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
1482 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N))
1483 return false;
1484
1485 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1486 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1487 int RHSC = (int)RHS->getZExtValue();
1488 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1489 return false;
1490 else if (RHSC < 0 && RHSC >= -255) // 8 bits
1491 return false;
1492 }
1493
1494 // Look for (R + R) or (R + (R << [1,2,3])).
1495 unsigned ShAmt = 0;
1496 Base = N.getOperand(0);
1497 OffReg = N.getOperand(1);
1498
1499 // Swap if it is ((R << c) + R).
1501 if (ShOpcVal != ARM_AM::lsl) {
1502 ShOpcVal = ARM_AM::getShiftOpcForNode(Base.getOpcode());
1503 if (ShOpcVal == ARM_AM::lsl)
1504 std::swap(Base, OffReg);
1505 }
1506
1507 if (ShOpcVal == ARM_AM::lsl) {
1508 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1509 // it.
1510 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1511 ShAmt = Sh->getZExtValue();
1512 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1513 OffReg = OffReg.getOperand(0);
1514 else {
1515 ShAmt = 0;
1516 }
1517 }
1518 }
1519
1520 // If OffReg is a multiply-by-constant and it's profitable to extract a shift
1521 // and use it in a shifted operand do so.
1522 if (OffReg.getOpcode() == ISD::MUL && N.hasOneUse()) {
1523 unsigned PowerOfTwo = 0;
1524 SDValue NewMulConst;
1525 if (canExtractShiftFromMul(OffReg, 3, PowerOfTwo, NewMulConst)) {
1526 HandleSDNode Handle(OffReg);
1527 replaceDAGValue(OffReg.getOperand(1), NewMulConst);
1528 OffReg = Handle.getValue();
1529 ShAmt = PowerOfTwo;
1530 }
1531 }
1532
1533 ShImm = CurDAG->getTargetConstant(ShAmt, SDLoc(N), MVT::i32);
1534
1535 return true;
1536}
1537
1538bool ARMDAGToDAGISel::SelectT2AddrModeExclusive(SDValue N, SDValue &Base,
1539 SDValue &OffImm) {
1540 // This *must* succeed since it's used for the irreplaceable ldrex and strex
1541 // instructions.
1542 Base = N;
1543 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1544
1545 if (N.getOpcode() != ISD::ADD || !CurDAG->isBaseWithConstantOffset(N))
1546 return true;
1547
1548 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1549 if (!RHS)
1550 return true;
1551
1552 uint32_t RHSC = (int)RHS->getZExtValue();
1553 if (RHSC > 1020 || RHSC % 4 != 0)
1554 return true;
1555
1556 Base = N.getOperand(0);
1557 if (Base.getOpcode() == ISD::FrameIndex) {
1558 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1559 Base = CurDAG->getTargetFrameIndex(
1560 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1561 }
1562
1563 OffImm = CurDAG->getTargetConstant(RHSC/4, SDLoc(N), MVT::i32);
1564 return true;
1565}
1566
1567//===--------------------------------------------------------------------===//
1568
1569/// getAL - Returns a ARMCC::AL immediate node.
1570static inline SDValue getAL(SelectionDAG *CurDAG, const SDLoc &dl) {
1571 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, dl, MVT::i32);
1572}
1573
1574void ARMDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
1575 MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand();
1576 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Result), {MemOp});
1577}
1578
1579bool ARMDAGToDAGISel::tryARMIndexedLoad(SDNode *N) {
1580 LoadSDNode *LD = cast<LoadSDNode>(N);
1581 ISD::MemIndexedMode AM = LD->getAddressingMode();
1582 if (AM == ISD::UNINDEXED)
1583 return false;
1584
1585 EVT LoadedVT = LD->getMemoryVT();
1586 SDValue Offset, AMOpc;
1587 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1588 unsigned Opcode = 0;
1589 bool Match = false;
1590 if (LoadedVT == MVT::i32 && isPre &&
1591 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1592 Opcode = ARM::LDR_PRE_IMM;
1593 Match = true;
1594 } else if (LoadedVT == MVT::i32 && !isPre &&
1595 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1596 Opcode = ARM::LDR_POST_IMM;
1597 Match = true;
1598 } else if (LoadedVT == MVT::i32 &&
1599 SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1600 Opcode = isPre ? ARM::LDR_PRE_REG : ARM::LDR_POST_REG;
1601 Match = true;
1602
1603 } else if (LoadedVT == MVT::i16 &&
1604 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1605 Match = true;
1606 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1607 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1608 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
1609 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
1610 if (LD->getExtensionType() == ISD::SEXTLOAD) {
1611 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1612 Match = true;
1613 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1614 }
1615 } else {
1616 if (isPre &&
1617 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1618 Match = true;
1619 Opcode = ARM::LDRB_PRE_IMM;
1620 } else if (!isPre &&
1621 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1622 Match = true;
1623 Opcode = ARM::LDRB_POST_IMM;
1624 } else if (SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1625 Match = true;
1626 Opcode = isPre ? ARM::LDRB_PRE_REG : ARM::LDRB_POST_REG;
1627 }
1628 }
1629 }
1630
1631 if (Match) {
1632 if (Opcode == ARM::LDR_PRE_IMM || Opcode == ARM::LDRB_PRE_IMM) {
1633 SDValue Chain = LD->getChain();
1634 SDValue Base = LD->getBasePtr();
1635 SDValue Ops[]= { Base, AMOpc, getAL(CurDAG, SDLoc(N)),
1636 CurDAG->getRegister(0, MVT::i32), Chain };
1637 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1638 MVT::Other, Ops);
1639 transferMemOperands(N, New);
1640 ReplaceNode(N, New);
1641 return true;
1642 } else {
1643 SDValue Chain = LD->getChain();
1644 SDValue Base = LD->getBasePtr();
1645 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG, SDLoc(N)),
1646 CurDAG->getRegister(0, MVT::i32), Chain };
1647 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1648 MVT::Other, Ops);
1649 transferMemOperands(N, New);
1650 ReplaceNode(N, New);
1651 return true;
1652 }
1653 }
1654
1655 return false;
1656}
1657
1658bool ARMDAGToDAGISel::tryT1IndexedLoad(SDNode *N) {
1659 LoadSDNode *LD = cast<LoadSDNode>(N);
1660 EVT LoadedVT = LD->getMemoryVT();
1661 ISD::MemIndexedMode AM = LD->getAddressingMode();
1662 if (AM != ISD::POST_INC || LD->getExtensionType() != ISD::NON_EXTLOAD ||
1663 LoadedVT.getSimpleVT().SimpleTy != MVT::i32)
1664 return false;
1665
1666 auto *COffs = dyn_cast<ConstantSDNode>(LD->getOffset());
1667 if (!COffs || COffs->getZExtValue() != 4)
1668 return false;
1669
1670 // A T1 post-indexed load is just a single register LDM: LDM r0!, {r1}.
1671 // The encoding of LDM is not how the rest of ISel expects a post-inc load to
1672 // look however, so we use a pseudo here and switch it for a tLDMIA_UPD after
1673 // ISel.
1674 SDValue Chain = LD->getChain();
1675 SDValue Base = LD->getBasePtr();
1676 SDValue Ops[]= { Base, getAL(CurDAG, SDLoc(N)),
1677 CurDAG->getRegister(0, MVT::i32), Chain };
1678 SDNode *New = CurDAG->getMachineNode(ARM::tLDR_postidx, SDLoc(N), MVT::i32,
1679 MVT::i32, MVT::Other, Ops);
1680 transferMemOperands(N, New);
1681 ReplaceNode(N, New);
1682 return true;
1683}
1684
1685bool ARMDAGToDAGISel::tryT2IndexedLoad(SDNode *N) {
1686 LoadSDNode *LD = cast<LoadSDNode>(N);
1687 ISD::MemIndexedMode AM = LD->getAddressingMode();
1688 if (AM == ISD::UNINDEXED)
1689 return false;
1690
1691 EVT LoadedVT = LD->getMemoryVT();
1692 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
1694 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1695 unsigned Opcode = 0;
1696 bool Match = false;
1697 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
1698 switch (LoadedVT.getSimpleVT().SimpleTy) {
1699 case MVT::i32:
1700 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1701 break;
1702 case MVT::i16:
1703 if (isSExtLd)
1704 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1705 else
1706 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
1707 break;
1708 case MVT::i8:
1709 case MVT::i1:
1710 if (isSExtLd)
1711 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1712 else
1713 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
1714 break;
1715 default:
1716 return false;
1717 }
1718 Match = true;
1719 }
1720
1721 if (Match) {
1722 SDValue Chain = LD->getChain();
1723 SDValue Base = LD->getBasePtr();
1724 SDValue Ops[]= { Base, Offset, getAL(CurDAG, SDLoc(N)),
1725 CurDAG->getRegister(0, MVT::i32), Chain };
1726 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1727 MVT::Other, Ops);
1728 transferMemOperands(N, New);
1729 ReplaceNode(N, New);
1730 return true;
1731 }
1732
1733 return false;
1734}
1735
1736bool ARMDAGToDAGISel::tryMVEIndexedLoad(SDNode *N) {
1737 EVT LoadedVT;
1738 unsigned Opcode = 0;
1739 bool isSExtLd, isPre;
1742 SDValue PredReg;
1743 SDValue Chain, Base, Offset;
1744
1745 if (LoadSDNode *LD = dyn_cast<LoadSDNode>(N)) {
1746 ISD::MemIndexedMode AM = LD->getAddressingMode();
1747 if (AM == ISD::UNINDEXED)
1748 return false;
1749 LoadedVT = LD->getMemoryVT();
1750 if (!LoadedVT.isVector())
1751 return false;
1752
1753 Chain = LD->getChain();
1754 Base = LD->getBasePtr();
1755 Offset = LD->getOffset();
1756 Alignment = LD->getAlign();
1757 isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
1758 isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1760 PredReg = CurDAG->getRegister(0, MVT::i32);
1761 } else if (MaskedLoadSDNode *LD = dyn_cast<MaskedLoadSDNode>(N)) {
1762 ISD::MemIndexedMode AM = LD->getAddressingMode();
1763 if (AM == ISD::UNINDEXED)
1764 return false;
1765 LoadedVT = LD->getMemoryVT();
1766 if (!LoadedVT.isVector())
1767 return false;
1768
1769 Chain = LD->getChain();
1770 Base = LD->getBasePtr();
1771 Offset = LD->getOffset();
1772 Alignment = LD->getAlign();
1773 isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
1774 isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1776 PredReg = LD->getMask();
1777 } else
1778 llvm_unreachable("Expected a Load or a Masked Load!");
1779
1780 // We allow LE non-masked loads to change the type (for example use a vldrb.8
1781 // as opposed to a vldrw.32). This can allow extra addressing modes or
1782 // alignments for what is otherwise an equivalent instruction.
1783 bool CanChangeType = Subtarget->isLittle() && !isa<MaskedLoadSDNode>(N);
1784
1785 SDValue NewOffset;
1786 if (Alignment >= Align(2) && LoadedVT == MVT::v4i16 &&
1787 SelectT2AddrModeImm7Offset(N, Offset, NewOffset, 1)) {
1788 if (isSExtLd)
1789 Opcode = isPre ? ARM::MVE_VLDRHS32_pre : ARM::MVE_VLDRHS32_post;
1790 else
1791 Opcode = isPre ? ARM::MVE_VLDRHU32_pre : ARM::MVE_VLDRHU32_post;
1792 } else if (LoadedVT == MVT::v8i8 &&
1793 SelectT2AddrModeImm7Offset(N, Offset, NewOffset, 0)) {
1794 if (isSExtLd)
1795 Opcode = isPre ? ARM::MVE_VLDRBS16_pre : ARM::MVE_VLDRBS16_post;
1796 else
1797 Opcode = isPre ? ARM::MVE_VLDRBU16_pre : ARM::MVE_VLDRBU16_post;
1798 } else if (LoadedVT == MVT::v4i8 &&
1799 SelectT2AddrModeImm7Offset(N, Offset, NewOffset, 0)) {
1800 if (isSExtLd)
1801 Opcode = isPre ? ARM::MVE_VLDRBS32_pre : ARM::MVE_VLDRBS32_post;
1802 else
1803 Opcode = isPre ? ARM::MVE_VLDRBU32_pre : ARM::MVE_VLDRBU32_post;
1804 } else if (Alignment >= Align(4) &&
1805 (CanChangeType || LoadedVT == MVT::v4i32 ||
1806 LoadedVT == MVT::v4f32) &&
1807 SelectT2AddrModeImm7Offset(N, Offset, NewOffset, 2))
1808 Opcode = isPre ? ARM::MVE_VLDRWU32_pre : ARM::MVE_VLDRWU32_post;
1809 else if (Alignment >= Align(2) &&
1810 (CanChangeType || LoadedVT == MVT::v8i16 ||
1811 LoadedVT == MVT::v8f16) &&
1812 SelectT2AddrModeImm7Offset(N, Offset, NewOffset, 1))
1813 Opcode = isPre ? ARM::MVE_VLDRHU16_pre : ARM::MVE_VLDRHU16_post;
1814 else if ((CanChangeType || LoadedVT == MVT::v16i8) &&
1815 SelectT2AddrModeImm7Offset(N, Offset, NewOffset, 0))
1816 Opcode = isPre ? ARM::MVE_VLDRBU8_pre : ARM::MVE_VLDRBU8_post;
1817 else
1818 return false;
1819
1820 SDValue Ops[] = {Base,
1821 NewOffset,
1822 CurDAG->getTargetConstant(Pred, SDLoc(N), MVT::i32),
1823 PredReg,
1824 CurDAG->getRegister(0, MVT::i32), // tp_reg
1825 Chain};
1826 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32,
1827 N->getValueType(0), MVT::Other, Ops);
1828 transferMemOperands(N, New);
1829 ReplaceUses(SDValue(N, 0), SDValue(New, 1));
1830 ReplaceUses(SDValue(N, 1), SDValue(New, 0));
1831 ReplaceUses(SDValue(N, 2), SDValue(New, 2));
1832 CurDAG->RemoveDeadNode(N);
1833 return true;
1834}
1835
1836/// Form a GPRPair pseudo register from a pair of GPR regs.
1837SDNode *ARMDAGToDAGISel::createGPRPairNode(EVT VT, SDValue V0, SDValue V1) {
1838 SDLoc dl(V0.getNode());
1839 SDValue RegClass =
1840 CurDAG->getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
1841 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
1842 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
1843 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1844 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1845}
1846
1847/// Form a D register from a pair of S registers.
1848SDNode *ARMDAGToDAGISel::createSRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1849 SDLoc dl(V0.getNode());
1850 SDValue RegClass =
1851 CurDAG->getTargetConstant(ARM::DPR_VFP2RegClassID, dl, MVT::i32);
1852 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1853 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1854 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1855 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1856}
1857
1858/// Form a quad register from a pair of D registers.
1859SDNode *ARMDAGToDAGISel::createDRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1860 SDLoc dl(V0.getNode());
1861 SDValue RegClass = CurDAG->getTargetConstant(ARM::QPRRegClassID, dl,
1862 MVT::i32);
1863 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1864 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1865 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1866 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1867}
1868
1869/// Form 4 consecutive D registers from a pair of Q registers.
1870SDNode *ARMDAGToDAGISel::createQRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1871 SDLoc dl(V0.getNode());
1872 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1873 MVT::i32);
1874 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1875 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1876 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1877 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1878}
1879
1880/// Form 4 consecutive S registers.
1881SDNode *ARMDAGToDAGISel::createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1,
1882 SDValue V2, SDValue V3) {
1883 SDLoc dl(V0.getNode());
1884 SDValue RegClass =
1885 CurDAG->getTargetConstant(ARM::QPR_VFP2RegClassID, dl, MVT::i32);
1886 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1887 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1888 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, dl, MVT::i32);
1889 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, dl, MVT::i32);
1890 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1891 V2, SubReg2, V3, SubReg3 };
1892 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1893}
1894
1895/// Form 4 consecutive D registers.
1896SDNode *ARMDAGToDAGISel::createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1,
1897 SDValue V2, SDValue V3) {
1898 SDLoc dl(V0.getNode());
1899 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1900 MVT::i32);
1901 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1902 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1903 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, dl, MVT::i32);
1904 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, dl, MVT::i32);
1905 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1906 V2, SubReg2, V3, SubReg3 };
1907 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1908}
1909
1910/// Form 4 consecutive Q registers.
1911SDNode *ARMDAGToDAGISel::createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1,
1912 SDValue V2, SDValue V3) {
1913 SDLoc dl(V0.getNode());
1914 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQQQPRRegClassID, dl,
1915 MVT::i32);
1916 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1917 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1918 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, dl, MVT::i32);
1919 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, dl, MVT::i32);
1920 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1921 V2, SubReg2, V3, SubReg3 };
1922 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1923}
1924
1925/// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1926/// of a NEON VLD or VST instruction. The supported values depend on the
1927/// number of registers being loaded.
1928SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, const SDLoc &dl,
1929 unsigned NumVecs, bool is64BitVector) {
1930 unsigned NumRegs = NumVecs;
1931 if (!is64BitVector && NumVecs < 3)
1932 NumRegs *= 2;
1933
1934 unsigned Alignment = Align->getAsZExtVal();
1935 if (Alignment >= 32 && NumRegs == 4)
1936 Alignment = 32;
1937 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1938 Alignment = 16;
1939 else if (Alignment >= 8)
1940 Alignment = 8;
1941 else
1942 Alignment = 0;
1943
1944 return CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
1945}
1946
1947static bool isVLDfixed(unsigned Opc)
1948{
1949 switch (Opc) {
1950 default: return false;
1951 case ARM::VLD1d8wb_fixed : return true;
1952 case ARM::VLD1d16wb_fixed : return true;
1953 case ARM::VLD1d64Qwb_fixed : return true;
1954 case ARM::VLD1d32wb_fixed : return true;
1955 case ARM::VLD1d64wb_fixed : return true;
1956 case ARM::VLD1d8TPseudoWB_fixed : return true;
1957 case ARM::VLD1d16TPseudoWB_fixed : return true;
1958 case ARM::VLD1d32TPseudoWB_fixed : return true;
1959 case ARM::VLD1d64TPseudoWB_fixed : return true;
1960 case ARM::VLD1d8QPseudoWB_fixed : return true;
1961 case ARM::VLD1d16QPseudoWB_fixed : return true;
1962 case ARM::VLD1d32QPseudoWB_fixed : return true;
1963 case ARM::VLD1d64QPseudoWB_fixed : return true;
1964 case ARM::VLD1q8wb_fixed : return true;
1965 case ARM::VLD1q16wb_fixed : return true;
1966 case ARM::VLD1q32wb_fixed : return true;
1967 case ARM::VLD1q64wb_fixed : return true;
1968 case ARM::VLD1DUPd8wb_fixed : return true;
1969 case ARM::VLD1DUPd16wb_fixed : return true;
1970 case ARM::VLD1DUPd32wb_fixed : return true;
1971 case ARM::VLD1DUPq8wb_fixed : return true;
1972 case ARM::VLD1DUPq16wb_fixed : return true;
1973 case ARM::VLD1DUPq32wb_fixed : return true;
1974 case ARM::VLD2d8wb_fixed : return true;
1975 case ARM::VLD2d16wb_fixed : return true;
1976 case ARM::VLD2d32wb_fixed : return true;
1977 case ARM::VLD2q8PseudoWB_fixed : return true;
1978 case ARM::VLD2q16PseudoWB_fixed : return true;
1979 case ARM::VLD2q32PseudoWB_fixed : return true;
1980 case ARM::VLD2DUPd8wb_fixed : return true;
1981 case ARM::VLD2DUPd16wb_fixed : return true;
1982 case ARM::VLD2DUPd32wb_fixed : return true;
1983 case ARM::VLD2DUPq8OddPseudoWB_fixed: return true;
1984 case ARM::VLD2DUPq16OddPseudoWB_fixed: return true;
1985 case ARM::VLD2DUPq32OddPseudoWB_fixed: return true;
1986 }
1987}
1988
1989static bool isVSTfixed(unsigned Opc)
1990{
1991 switch (Opc) {
1992 default: return false;
1993 case ARM::VST1d8wb_fixed : return true;
1994 case ARM::VST1d16wb_fixed : return true;
1995 case ARM::VST1d32wb_fixed : return true;
1996 case ARM::VST1d64wb_fixed : return true;
1997 case ARM::VST1q8wb_fixed : return true;
1998 case ARM::VST1q16wb_fixed : return true;
1999 case ARM::VST1q32wb_fixed : return true;
2000 case ARM::VST1q64wb_fixed : return true;
2001 case ARM::VST1d8TPseudoWB_fixed : return true;
2002 case ARM::VST1d16TPseudoWB_fixed : return true;
2003 case ARM::VST1d32TPseudoWB_fixed : return true;
2004 case ARM::VST1d64TPseudoWB_fixed : return true;
2005 case ARM::VST1d8QPseudoWB_fixed : return true;
2006 case ARM::VST1d16QPseudoWB_fixed : return true;
2007 case ARM::VST1d32QPseudoWB_fixed : return true;
2008 case ARM::VST1d64QPseudoWB_fixed : return true;
2009 case ARM::VST2d8wb_fixed : return true;
2010 case ARM::VST2d16wb_fixed : return true;
2011 case ARM::VST2d32wb_fixed : return true;
2012 case ARM::VST2q8PseudoWB_fixed : return true;
2013 case ARM::VST2q16PseudoWB_fixed : return true;
2014 case ARM::VST2q32PseudoWB_fixed : return true;
2015 }
2016}
2017
2018// Get the register stride update opcode of a VLD/VST instruction that
2019// is otherwise equivalent to the given fixed stride updating instruction.
2020static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) {
2022 && "Incorrect fixed stride updating instruction.");
2023 switch (Opc) {
2024 default: break;
2025 case ARM::VLD1d8wb_fixed: return ARM::VLD1d8wb_register;
2026 case ARM::VLD1d16wb_fixed: return ARM::VLD1d16wb_register;
2027 case ARM::VLD1d32wb_fixed: return ARM::VLD1d32wb_register;
2028 case ARM::VLD1d64wb_fixed: return ARM::VLD1d64wb_register;
2029 case ARM::VLD1q8wb_fixed: return ARM::VLD1q8wb_register;
2030 case ARM::VLD1q16wb_fixed: return ARM::VLD1q16wb_register;
2031 case ARM::VLD1q32wb_fixed: return ARM::VLD1q32wb_register;
2032 case ARM::VLD1q64wb_fixed: return ARM::VLD1q64wb_register;
2033 case ARM::VLD1d64Twb_fixed: return ARM::VLD1d64Twb_register;
2034 case ARM::VLD1d64Qwb_fixed: return ARM::VLD1d64Qwb_register;
2035 case ARM::VLD1d8TPseudoWB_fixed: return ARM::VLD1d8TPseudoWB_register;
2036 case ARM::VLD1d16TPseudoWB_fixed: return ARM::VLD1d16TPseudoWB_register;
2037 case ARM::VLD1d32TPseudoWB_fixed: return ARM::VLD1d32TPseudoWB_register;
2038 case ARM::VLD1d64TPseudoWB_fixed: return ARM::VLD1d64TPseudoWB_register;
2039 case ARM::VLD1d8QPseudoWB_fixed: return ARM::VLD1d8QPseudoWB_register;
2040 case ARM::VLD1d16QPseudoWB_fixed: return ARM::VLD1d16QPseudoWB_register;
2041 case ARM::VLD1d32QPseudoWB_fixed: return ARM::VLD1d32QPseudoWB_register;
2042 case ARM::VLD1d64QPseudoWB_fixed: return ARM::VLD1d64QPseudoWB_register;
2043 case ARM::VLD1DUPd8wb_fixed : return ARM::VLD1DUPd8wb_register;
2044 case ARM::VLD1DUPd16wb_fixed : return ARM::VLD1DUPd16wb_register;
2045 case ARM::VLD1DUPd32wb_fixed : return ARM::VLD1DUPd32wb_register;
2046 case ARM::VLD1DUPq8wb_fixed : return ARM::VLD1DUPq8wb_register;
2047 case ARM::VLD1DUPq16wb_fixed : return ARM::VLD1DUPq16wb_register;
2048 case ARM::VLD1DUPq32wb_fixed : return ARM::VLD1DUPq32wb_register;
2049 case ARM::VLD2DUPq8OddPseudoWB_fixed: return ARM::VLD2DUPq8OddPseudoWB_register;
2050 case ARM::VLD2DUPq16OddPseudoWB_fixed: return ARM::VLD2DUPq16OddPseudoWB_register;
2051 case ARM::VLD2DUPq32OddPseudoWB_fixed: return ARM::VLD2DUPq32OddPseudoWB_register;
2052
2053 case ARM::VST1d8wb_fixed: return ARM::VST1d8wb_register;
2054 case ARM::VST1d16wb_fixed: return ARM::VST1d16wb_register;
2055 case ARM::VST1d32wb_fixed: return ARM::VST1d32wb_register;
2056 case ARM::VST1d64wb_fixed: return ARM::VST1d64wb_register;
2057 case ARM::VST1q8wb_fixed: return ARM::VST1q8wb_register;
2058 case ARM::VST1q16wb_fixed: return ARM::VST1q16wb_register;
2059 case ARM::VST1q32wb_fixed: return ARM::VST1q32wb_register;
2060 case ARM::VST1q64wb_fixed: return ARM::VST1q64wb_register;
2061 case ARM::VST1d8TPseudoWB_fixed: return ARM::VST1d8TPseudoWB_register;
2062 case ARM::VST1d16TPseudoWB_fixed: return ARM::VST1d16TPseudoWB_register;
2063 case ARM::VST1d32TPseudoWB_fixed: return ARM::VST1d32TPseudoWB_register;
2064 case ARM::VST1d64TPseudoWB_fixed: return ARM::VST1d64TPseudoWB_register;
2065 case ARM::VST1d8QPseudoWB_fixed: return ARM::VST1d8QPseudoWB_register;
2066 case ARM::VST1d16QPseudoWB_fixed: return ARM::VST1d16QPseudoWB_register;
2067 case ARM::VST1d32QPseudoWB_fixed: return ARM::VST1d32QPseudoWB_register;
2068 case ARM::VST1d64QPseudoWB_fixed: return ARM::VST1d64QPseudoWB_register;
2069
2070 case ARM::VLD2d8wb_fixed: return ARM::VLD2d8wb_register;
2071 case ARM::VLD2d16wb_fixed: return ARM::VLD2d16wb_register;
2072 case ARM::VLD2d32wb_fixed: return ARM::VLD2d32wb_register;
2073 case ARM::VLD2q8PseudoWB_fixed: return ARM::VLD2q8PseudoWB_register;
2074 case ARM::VLD2q16PseudoWB_fixed: return ARM::VLD2q16PseudoWB_register;
2075 case ARM::VLD2q32PseudoWB_fixed: return ARM::VLD2q32PseudoWB_register;
2076
2077 case ARM::VST2d8wb_fixed: return ARM::VST2d8wb_register;
2078 case ARM::VST2d16wb_fixed: return ARM::VST2d16wb_register;
2079 case ARM::VST2d32wb_fixed: return ARM::VST2d32wb_register;
2080 case ARM::VST2q8PseudoWB_fixed: return ARM::VST2q8PseudoWB_register;
2081 case ARM::VST2q16PseudoWB_fixed: return ARM::VST2q16PseudoWB_register;
2082 case ARM::VST2q32PseudoWB_fixed: return ARM::VST2q32PseudoWB_register;
2083
2084 case ARM::VLD2DUPd8wb_fixed: return ARM::VLD2DUPd8wb_register;
2085 case ARM::VLD2DUPd16wb_fixed: return ARM::VLD2DUPd16wb_register;
2086 case ARM::VLD2DUPd32wb_fixed: return ARM::VLD2DUPd32wb_register;
2087 }
2088 return Opc; // If not one we handle, return it unchanged.
2089}
2090
2091/// Returns true if the given increment is a Constant known to be equal to the
2092/// access size performed by a NEON load/store. This means the "[rN]!" form can
2093/// be used.
2094static bool isPerfectIncrement(SDValue Inc, EVT VecTy, unsigned NumVecs) {
2095 auto C = dyn_cast<ConstantSDNode>(Inc);
2096 return C && C->getZExtValue() == VecTy.getSizeInBits() / 8 * NumVecs;
2097}
2098
2099void ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
2100 const uint16_t *DOpcodes,
2101 const uint16_t *QOpcodes0,
2102 const uint16_t *QOpcodes1) {
2103 assert(Subtarget->hasNEON());
2104 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
2105 SDLoc dl(N);
2106
2107 SDValue MemAddr, Align;
2108 bool IsIntrinsic = !isUpdating; // By coincidence, all supported updating
2109 // nodes are not intrinsics.
2110 unsigned AddrOpIdx = IsIntrinsic ? 2 : 1;
2111 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2112 return;
2113
2114 SDValue Chain = N->getOperand(0);
2115 EVT VT = N->getValueType(0);
2116 bool is64BitVector = VT.is64BitVector();
2117 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
2118
2119 unsigned OpcodeIndex;
2120 switch (VT.getSimpleVT().SimpleTy) {
2121 default: llvm_unreachable("unhandled vld type");
2122 // Double-register operations:
2123 case MVT::v8i8: OpcodeIndex = 0; break;
2124 case MVT::v4f16:
2125 case MVT::v4bf16:
2126 case MVT::v4i16: OpcodeIndex = 1; break;
2127 case MVT::v2f32:
2128 case MVT::v2i32: OpcodeIndex = 2; break;
2129 case MVT::v1i64: OpcodeIndex = 3; break;
2130 // Quad-register operations:
2131 case MVT::v16i8: OpcodeIndex = 0; break;
2132 case MVT::v8f16:
2133 case MVT::v8bf16:
2134 case MVT::v8i16: OpcodeIndex = 1; break;
2135 case MVT::v4f32:
2136 case MVT::v4i32: OpcodeIndex = 2; break;
2137 case MVT::v2f64:
2138 case MVT::v2i64: OpcodeIndex = 3; break;
2139 }
2140
2141 EVT ResTy;
2142 if (NumVecs == 1)
2143 ResTy = VT;
2144 else {
2145 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2146 if (!is64BitVector)
2147 ResTyElts *= 2;
2148 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
2149 }
2150 std::vector<EVT> ResTys;
2151 ResTys.push_back(ResTy);
2152 if (isUpdating)
2153 ResTys.push_back(MVT::i32);
2154 ResTys.push_back(MVT::Other);
2155
2156 SDValue Pred = getAL(CurDAG, dl);
2157 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2158 SDNode *VLd;
2160
2161 // Double registers and VLD1/VLD2 quad registers are directly supported.
2162 if (is64BitVector || NumVecs <= 2) {
2163 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2164 QOpcodes0[OpcodeIndex]);
2165 Ops.push_back(MemAddr);
2166 Ops.push_back(Align);
2167 if (isUpdating) {
2168 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2169 bool IsImmUpdate = isPerfectIncrement(Inc, VT, NumVecs);
2170 if (!IsImmUpdate) {
2171 // We use a VLD1 for v1i64 even if the pseudo says vld2/3/4, so
2172 // check for the opcode rather than the number of vector elements.
2173 if (isVLDfixed(Opc))
2175 Ops.push_back(Inc);
2176 // VLD1/VLD2 fixed increment does not need Reg0 so only include it in
2177 // the operands if not such an opcode.
2178 } else if (!isVLDfixed(Opc))
2179 Ops.push_back(Reg0);
2180 }
2181 Ops.push_back(Pred);
2182 Ops.push_back(Reg0);
2183 Ops.push_back(Chain);
2184 VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2185
2186 } else {
2187 // Otherwise, quad registers are loaded with two separate instructions,
2188 // where one loads the even registers and the other loads the odd registers.
2189 EVT AddrTy = MemAddr.getValueType();
2190
2191 // Load the even subregs. This is always an updating load, so that it
2192 // provides the address to the second load for the odd subregs.
2193 SDValue ImplDef =
2194 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
2195 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
2196 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
2197 ResTy, AddrTy, MVT::Other, OpsA);
2198 Chain = SDValue(VLdA, 2);
2199
2200 // Load the odd subregs.
2201 Ops.push_back(SDValue(VLdA, 1));
2202 Ops.push_back(Align);
2203 if (isUpdating) {
2204 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2206 "only constant post-increment update allowed for VLD3/4");
2207 (void)Inc;
2208 Ops.push_back(Reg0);
2209 }
2210 Ops.push_back(SDValue(VLdA, 0));
2211 Ops.push_back(Pred);
2212 Ops.push_back(Reg0);
2213 Ops.push_back(Chain);
2214 VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, Ops);
2215 }
2216
2217 // Transfer memoperands.
2218 MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2219 CurDAG->setNodeMemRefs(cast<MachineSDNode>(VLd), {MemOp});
2220
2221 if (NumVecs == 1) {
2222 ReplaceNode(N, VLd);
2223 return;
2224 }
2225
2226 // Extract out the subregisters.
2227 SDValue SuperReg = SDValue(VLd, 0);
2228 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
2229 ARM::qsub_3 == ARM::qsub_0 + 3,
2230 "Unexpected subreg numbering");
2231 unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0);
2232 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2233 ReplaceUses(SDValue(N, Vec),
2234 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
2235 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
2236 if (isUpdating)
2237 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
2238 CurDAG->RemoveDeadNode(N);
2239}
2240
2241void ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
2242 const uint16_t *DOpcodes,
2243 const uint16_t *QOpcodes0,
2244 const uint16_t *QOpcodes1) {
2245 assert(Subtarget->hasNEON());
2246 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
2247 SDLoc dl(N);
2248
2249 SDValue MemAddr, Align;
2250 bool IsIntrinsic = !isUpdating; // By coincidence, all supported updating
2251 // nodes are not intrinsics.
2252 unsigned AddrOpIdx = IsIntrinsic ? 2 : 1;
2253 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2254 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2255 return;
2256
2257 MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2258
2259 SDValue Chain = N->getOperand(0);
2260 EVT VT = N->getOperand(Vec0Idx).getValueType();
2261 bool is64BitVector = VT.is64BitVector();
2262 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
2263
2264 unsigned OpcodeIndex;
2265 switch (VT.getSimpleVT().SimpleTy) {
2266 default: llvm_unreachable("unhandled vst type");
2267 // Double-register operations:
2268 case MVT::v8i8: OpcodeIndex = 0; break;
2269 case MVT::v4f16:
2270 case MVT::v4bf16:
2271 case MVT::v4i16: OpcodeIndex = 1; break;
2272 case MVT::v2f32:
2273 case MVT::v2i32: OpcodeIndex = 2; break;
2274 case MVT::v1i64: OpcodeIndex = 3; break;
2275 // Quad-register operations:
2276 case MVT::v16i8: OpcodeIndex = 0; break;
2277 case MVT::v8f16:
2278 case MVT::v8bf16:
2279 case MVT::v8i16: OpcodeIndex = 1; break;
2280 case MVT::v4f32:
2281 case MVT::v4i32: OpcodeIndex = 2; break;
2282 case MVT::v2f64:
2283 case MVT::v2i64: OpcodeIndex = 3; break;
2284 }
2285
2286 std::vector<EVT> ResTys;
2287 if (isUpdating)
2288 ResTys.push_back(MVT::i32);
2289 ResTys.push_back(MVT::Other);
2290
2291 SDValue Pred = getAL(CurDAG, dl);
2292 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2294
2295 // Double registers and VST1/VST2 quad registers are directly supported.
2296 if (is64BitVector || NumVecs <= 2) {
2297 SDValue SrcReg;
2298 if (NumVecs == 1) {
2299 SrcReg = N->getOperand(Vec0Idx);
2300 } else if (is64BitVector) {
2301 // Form a REG_SEQUENCE to force register allocation.
2302 SDValue V0 = N->getOperand(Vec0Idx + 0);
2303 SDValue V1 = N->getOperand(Vec0Idx + 1);
2304 if (NumVecs == 2)
2305 SrcReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
2306 else {
2307 SDValue V2 = N->getOperand(Vec0Idx + 2);
2308 // If it's a vst3, form a quad D-register and leave the last part as
2309 // an undef.
2310 SDValue V3 = (NumVecs == 3)
2311 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
2312 : N->getOperand(Vec0Idx + 3);
2313 SrcReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2314 }
2315 } else {
2316 // Form a QQ register.
2317 SDValue Q0 = N->getOperand(Vec0Idx);
2318 SDValue Q1 = N->getOperand(Vec0Idx + 1);
2319 SrcReg = SDValue(createQRegPairNode(MVT::v4i64, Q0, Q1), 0);
2320 }
2321
2322 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2323 QOpcodes0[OpcodeIndex]);
2324 Ops.push_back(MemAddr);
2325 Ops.push_back(Align);
2326 if (isUpdating) {
2327 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2328 bool IsImmUpdate = isPerfectIncrement(Inc, VT, NumVecs);
2329 if (!IsImmUpdate) {
2330 // We use a VST1 for v1i64 even if the pseudo says VST2/3/4, so
2331 // check for the opcode rather than the number of vector elements.
2332 if (isVSTfixed(Opc))
2334 Ops.push_back(Inc);
2335 }
2336 // VST1/VST2 fixed increment does not need Reg0 so only include it in
2337 // the operands if not such an opcode.
2338 else if (!isVSTfixed(Opc))
2339 Ops.push_back(Reg0);
2340 }
2341 Ops.push_back(SrcReg);
2342 Ops.push_back(Pred);
2343 Ops.push_back(Reg0);
2344 Ops.push_back(Chain);
2345 SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2346
2347 // Transfer memoperands.
2348 CurDAG->setNodeMemRefs(cast<MachineSDNode>(VSt), {MemOp});
2349
2350 ReplaceNode(N, VSt);
2351 return;
2352 }
2353
2354 // Otherwise, quad registers are stored with two separate instructions,
2355 // where one stores the even registers and the other stores the odd registers.
2356
2357 // Form the QQQQ REG_SEQUENCE.
2358 SDValue V0 = N->getOperand(Vec0Idx + 0);
2359 SDValue V1 = N->getOperand(Vec0Idx + 1);
2360 SDValue V2 = N->getOperand(Vec0Idx + 2);
2361 SDValue V3 = (NumVecs == 3)
2362 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2363 : N->getOperand(Vec0Idx + 3);
2364 SDValue RegSeq = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
2365
2366 // Store the even D registers. This is always an updating store, so that it
2367 // provides the address to the second store for the odd subregs.
2368 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
2369 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
2370 MemAddr.getValueType(),
2371 MVT::Other, OpsA);
2372 CurDAG->setNodeMemRefs(cast<MachineSDNode>(VStA), {MemOp});
2373 Chain = SDValue(VStA, 1);
2374
2375 // Store the odd D registers.
2376 Ops.push_back(SDValue(VStA, 0));
2377 Ops.push_back(Align);
2378 if (isUpdating) {
2379 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2381 "only constant post-increment update allowed for VST3/4");
2382 (void)Inc;
2383 Ops.push_back(Reg0);
2384 }
2385 Ops.push_back(RegSeq);
2386 Ops.push_back(Pred);
2387 Ops.push_back(Reg0);
2388 Ops.push_back(Chain);
2389 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
2390 Ops);
2391 CurDAG->setNodeMemRefs(cast<MachineSDNode>(VStB), {MemOp});
2392 ReplaceNode(N, VStB);
2393}
2394
2395void ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
2396 unsigned NumVecs,
2397 const uint16_t *DOpcodes,
2398 const uint16_t *QOpcodes) {
2399 assert(Subtarget->hasNEON());
2400 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
2401 SDLoc dl(N);
2402
2403 SDValue MemAddr, Align;
2404 bool IsIntrinsic = !isUpdating; // By coincidence, all supported updating
2405 // nodes are not intrinsics.
2406 unsigned AddrOpIdx = IsIntrinsic ? 2 : 1;
2407 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2408 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2409 return;
2410
2411 MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2412
2413 SDValue Chain = N->getOperand(0);
2414 unsigned Lane = N->getConstantOperandVal(Vec0Idx + NumVecs);
2415 EVT VT = N->getOperand(Vec0Idx).getValueType();
2416 bool is64BitVector = VT.is64BitVector();
2417
2418 unsigned Alignment = 0;
2419 if (NumVecs != 3) {
2420 Alignment = Align->getAsZExtVal();
2421 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8;
2422 if (Alignment > NumBytes)
2423 Alignment = NumBytes;
2424 if (Alignment < 8 && Alignment < NumBytes)
2425 Alignment = 0;
2426 // Alignment must be a power of two; make sure of that.
2428 if (Alignment == 1)
2429 Alignment = 0;
2430 }
2431 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
2432
2433 unsigned OpcodeIndex;
2434 switch (VT.getSimpleVT().SimpleTy) {
2435 default: llvm_unreachable("unhandled vld/vst lane type");
2436 // Double-register operations:
2437 case MVT::v8i8: OpcodeIndex = 0; break;
2438 case MVT::v4f16:
2439 case MVT::v4bf16:
2440 case MVT::v4i16: OpcodeIndex = 1; break;
2441 case MVT::v2f32:
2442 case MVT::v2i32: OpcodeIndex = 2; break;
2443 // Quad-register operations:
2444 case MVT::v8f16:
2445 case MVT::v8bf16:
2446 case MVT::v8i16: OpcodeIndex = 0; break;
2447 case MVT::v4f32:
2448 case MVT::v4i32: OpcodeIndex = 1; break;
2449 }
2450
2451 std::vector<EVT> ResTys;
2452 if (IsLoad) {
2453 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2454 if (!is64BitVector)
2455 ResTyElts *= 2;
2456 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(),
2457 MVT::i64, ResTyElts));
2458 }
2459 if (isUpdating)
2460 ResTys.push_back(MVT::i32);
2461 ResTys.push_back(MVT::Other);
2462
2463 SDValue Pred = getAL(CurDAG, dl);
2464 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2465
2467 Ops.push_back(MemAddr);
2468 Ops.push_back(Align);
2469 if (isUpdating) {
2470 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2471 bool IsImmUpdate =
2472 isPerfectIncrement(Inc, VT.getVectorElementType(), NumVecs);
2473 Ops.push_back(IsImmUpdate ? Reg0 : Inc);
2474 }
2475
2476 SDValue SuperReg;
2477 SDValue V0 = N->getOperand(Vec0Idx + 0);
2478 SDValue V1 = N->getOperand(Vec0Idx + 1);
2479 if (NumVecs == 2) {
2480 if (is64BitVector)
2481 SuperReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
2482 else
2483 SuperReg = SDValue(createQRegPairNode(MVT::v4i64, V0, V1), 0);
2484 } else {
2485 SDValue V2 = N->getOperand(Vec0Idx + 2);
2486 SDValue V3 = (NumVecs == 3)
2487 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2488 : N->getOperand(Vec0Idx + 3);
2489 if (is64BitVector)
2490 SuperReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2491 else
2492 SuperReg = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
2493 }
2494 Ops.push_back(SuperReg);
2495 Ops.push_back(getI32Imm(Lane, dl));
2496 Ops.push_back(Pred);
2497 Ops.push_back(Reg0);
2498 Ops.push_back(Chain);
2499
2500 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2501 QOpcodes[OpcodeIndex]);
2502 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2503 CurDAG->setNodeMemRefs(cast<MachineSDNode>(VLdLn), {MemOp});
2504 if (!IsLoad) {
2505 ReplaceNode(N, VLdLn);
2506 return;
2507 }
2508
2509 // Extract the subregisters.
2510 SuperReg = SDValue(VLdLn, 0);
2511 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
2512 ARM::qsub_3 == ARM::qsub_0 + 3,
2513 "Unexpected subreg numbering");
2514 unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
2515 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2516 ReplaceUses(SDValue(N, Vec),
2517 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
2518 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1));
2519 if (isUpdating)
2520 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2));
2521 CurDAG->RemoveDeadNode(N);
2522}
2523
2524template <typename SDValueVector>
2525void ARMDAGToDAGISel::AddMVEPredicateToOps(SDValueVector &Ops, SDLoc Loc,
2526 SDValue PredicateMask) {
2527 Ops.push_back(CurDAG->getTargetConstant(ARMVCC::Then, Loc, MVT::i32));
2528 Ops.push_back(PredicateMask);
2529 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // tp_reg
2530}
2531
2532template <typename SDValueVector>
2533void ARMDAGToDAGISel::AddMVEPredicateToOps(SDValueVector &Ops, SDLoc Loc,
2534 SDValue PredicateMask,
2535 SDValue Inactive) {
2536 Ops.push_back(CurDAG->getTargetConstant(ARMVCC::Then, Loc, MVT::i32));
2537 Ops.push_back(PredicateMask);
2538 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // tp_reg
2539 Ops.push_back(Inactive);
2540}
2541
2542template <typename SDValueVector>
2543void ARMDAGToDAGISel::AddEmptyMVEPredicateToOps(SDValueVector &Ops, SDLoc Loc) {
2544 Ops.push_back(CurDAG->getTargetConstant(ARMVCC::None, Loc, MVT::i32));
2545 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
2546 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // tp_reg
2547}
2548
2549template <typename SDValueVector>
2550void ARMDAGToDAGISel::AddEmptyMVEPredicateToOps(SDValueVector &Ops, SDLoc Loc,
2551 EVT InactiveTy) {
2552 Ops.push_back(CurDAG->getTargetConstant(ARMVCC::None, Loc, MVT::i32));
2553 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
2554 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // tp_reg
2555 Ops.push_back(SDValue(
2556 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, Loc, InactiveTy), 0));
2557}
2558
2559void ARMDAGToDAGISel::SelectMVE_WB(SDNode *N, const uint16_t *Opcodes,
2560 bool Predicated) {
2561 SDLoc Loc(N);
2563
2564 uint16_t Opcode;
2565 switch (N->getValueType(1).getVectorElementType().getSizeInBits()) {
2566 case 32:
2567 Opcode = Opcodes[0];
2568 break;
2569 case 64:
2570 Opcode = Opcodes[1];
2571 break;
2572 default:
2573 llvm_unreachable("bad vector element size in SelectMVE_WB");
2574 }
2575
2576 Ops.push_back(N->getOperand(2)); // vector of base addresses
2577
2578 int32_t ImmValue = N->getConstantOperandVal(3);
2579 Ops.push_back(getI32Imm(ImmValue, Loc)); // immediate offset
2580
2581 if (Predicated)
2582 AddMVEPredicateToOps(Ops, Loc, N->getOperand(4));
2583 else
2584 AddEmptyMVEPredicateToOps(Ops, Loc);
2585
2586 Ops.push_back(N->getOperand(0)); // chain
2587
2589 VTs.push_back(N->getValueType(1));
2590 VTs.push_back(N->getValueType(0));
2591 VTs.push_back(N->getValueType(2));
2592
2593 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), VTs, Ops);
2594 ReplaceUses(SDValue(N, 0), SDValue(New, 1));
2595 ReplaceUses(SDValue(N, 1), SDValue(New, 0));
2596 ReplaceUses(SDValue(N, 2), SDValue(New, 2));
2597 transferMemOperands(N, New);
2598 CurDAG->RemoveDeadNode(N);
2599}
2600
2601void ARMDAGToDAGISel::SelectMVE_LongShift(SDNode *N, uint16_t Opcode,
2602 bool Immediate,
2603 bool HasSaturationOperand) {
2604 SDLoc Loc(N);
2606
2607 // Two 32-bit halves of the value to be shifted
2608 Ops.push_back(N->getOperand(1));
2609 Ops.push_back(N->getOperand(2));
2610
2611 // The shift count
2612 if (Immediate) {
2613 int32_t ImmValue = N->getConstantOperandVal(3);
2614 Ops.push_back(getI32Imm(ImmValue, Loc)); // immediate shift count
2615 } else {
2616 Ops.push_back(N->getOperand(3));
2617 }
2618
2619 // The immediate saturation operand, if any
2620 if (HasSaturationOperand) {
2621 int32_t SatOp = N->getConstantOperandVal(4);
2622 int SatBit = (SatOp == 64 ? 0 : 1);
2623 Ops.push_back(getI32Imm(SatBit, Loc));
2624 }
2625
2626 // MVE scalar shifts are IT-predicable, so include the standard
2627 // predicate arguments.
2628 Ops.push_back(getAL(CurDAG, Loc));
2629 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
2630
2631 CurDAG->SelectNodeTo(N, Opcode, N->getVTList(), ArrayRef(Ops));
2632}
2633
2634void ARMDAGToDAGISel::SelectMVE_VADCSBC(SDNode *N, uint16_t OpcodeWithCarry,
2635 uint16_t OpcodeWithNoCarry,
2636 bool Add, bool Predicated) {
2637 SDLoc Loc(N);
2639 uint16_t Opcode;
2640
2641 unsigned FirstInputOp = Predicated ? 2 : 1;
2642
2643 // Two input vectors and the input carry flag
2644 Ops.push_back(N->getOperand(FirstInputOp));
2645 Ops.push_back(N->getOperand(FirstInputOp + 1));
2646 SDValue CarryIn = N->getOperand(FirstInputOp + 2);
2647 ConstantSDNode *CarryInConstant = dyn_cast<ConstantSDNode>(CarryIn);
2648 uint32_t CarryMask = 1 << 29;
2649 uint32_t CarryExpected = Add ? 0 : CarryMask;
2650 if (CarryInConstant &&
2651 (CarryInConstant->getZExtValue() & CarryMask) == CarryExpected) {
2652 Opcode = OpcodeWithNoCarry;
2653 } else {
2654 Ops.push_back(CarryIn);
2655 Opcode = OpcodeWithCarry;
2656 }
2657
2658 if (Predicated)
2659 AddMVEPredicateToOps(Ops, Loc,
2660 N->getOperand(FirstInputOp + 3), // predicate
2661 N->getOperand(FirstInputOp - 1)); // inactive
2662 else
2663 AddEmptyMVEPredicateToOps(Ops, Loc, N->getValueType(0));
2664
2665 CurDAG->SelectNodeTo(N, Opcode, N->getVTList(), ArrayRef(Ops));
2666}
2667
2668void ARMDAGToDAGISel::SelectMVE_VSHLC(SDNode *N, bool Predicated) {
2669 SDLoc Loc(N);
2671
2672 // One vector input, followed by a 32-bit word of bits to shift in
2673 // and then an immediate shift count
2674 Ops.push_back(N->getOperand(1));
2675 Ops.push_back(N->getOperand(2));
2676 int32_t ImmValue = N->getConstantOperandVal(3);
2677 Ops.push_back(getI32Imm(ImmValue, Loc)); // immediate shift count
2678
2679 if (Predicated)
2680 AddMVEPredicateToOps(Ops, Loc, N->getOperand(4));
2681 else
2682 AddEmptyMVEPredicateToOps(Ops, Loc);
2683
2684 CurDAG->SelectNodeTo(N, ARM::MVE_VSHLC, N->getVTList(), ArrayRef(Ops));
2685}
2686
2687static bool SDValueToConstBool(SDValue SDVal) {
2688 assert(isa<ConstantSDNode>(SDVal) && "expected a compile-time constant");
2689 ConstantSDNode *SDValConstant = dyn_cast<ConstantSDNode>(SDVal);
2690 uint64_t Value = SDValConstant->getZExtValue();
2691 assert((Value == 0 || Value == 1) && "expected value 0 or 1");
2692 return Value;
2693}
2694
2695void ARMDAGToDAGISel::SelectBaseMVE_VMLLDAV(SDNode *N, bool Predicated,
2696 const uint16_t *OpcodesS,
2697 const uint16_t *OpcodesU,
2698 size_t Stride, size_t TySize) {
2699 assert(TySize < Stride && "Invalid TySize");
2700 bool IsUnsigned = SDValueToConstBool(N->getOperand(1));
2701 bool IsSub = SDValueToConstBool(N->getOperand(2));
2702 bool IsExchange = SDValueToConstBool(N->getOperand(3));
2703 if (IsUnsigned) {
2704 assert(!IsSub &&
2705 "Unsigned versions of vmlsldav[a]/vrmlsldavh[a] do not exist");
2706 assert(!IsExchange &&
2707 "Unsigned versions of vmlaldav[a]x/vrmlaldavh[a]x do not exist");
2708 }
2709
2710 auto OpIsZero = [N](size_t OpNo) {
2711 return isNullConstant(N->getOperand(OpNo));
2712 };
2713
2714 // If the input accumulator value is not zero, select an instruction with
2715 // accumulator, otherwise select an instruction without accumulator
2716 bool IsAccum = !(OpIsZero(4) && OpIsZero(5));
2717
2718 const uint16_t *Opcodes = IsUnsigned ? OpcodesU : OpcodesS;
2719 if (IsSub)
2720 Opcodes += 4 * Stride;
2721 if (IsExchange)
2722 Opcodes += 2 * Stride;
2723 if (IsAccum)
2724 Opcodes += Stride;
2725 uint16_t Opcode = Opcodes[TySize];
2726
2727 SDLoc Loc(N);
2729 // Push the accumulator operands, if they are used
2730 if (IsAccum) {
2731 Ops.push_back(N->getOperand(4));
2732 Ops.push_back(N->getOperand(5));
2733 }
2734 // Push the two vector operands
2735 Ops.push_back(N->getOperand(6));
2736 Ops.push_back(N->getOperand(7));
2737
2738 if (Predicated)
2739 AddMVEPredicateToOps(Ops, Loc, N->getOperand(8));
2740 else
2741 AddEmptyMVEPredicateToOps(Ops, Loc);
2742
2743 CurDAG->SelectNodeTo(N, Opcode, N->getVTList(), ArrayRef(Ops));
2744}
2745
2746void ARMDAGToDAGISel::SelectMVE_VMLLDAV(SDNode *N, bool Predicated,
2747 const uint16_t *OpcodesS,
2748 const uint16_t *OpcodesU) {
2749 EVT VecTy = N->getOperand(6).getValueType();
2750 size_t SizeIndex;
2751 switch (VecTy.getVectorElementType().getSizeInBits()) {
2752 case 16:
2753 SizeIndex = 0;
2754 break;
2755 case 32:
2756 SizeIndex = 1;
2757 break;
2758 default:
2759 llvm_unreachable("bad vector element size");
2760 }
2761
2762 SelectBaseMVE_VMLLDAV(N, Predicated, OpcodesS, OpcodesU, 2, SizeIndex);
2763}
2764
2765void ARMDAGToDAGISel::SelectMVE_VRMLLDAVH(SDNode *N, bool Predicated,
2766 const uint16_t *OpcodesS,
2767 const uint16_t *OpcodesU) {
2768 assert(
2769 N->getOperand(6).getValueType().getVectorElementType().getSizeInBits() ==
2770 32 &&
2771 "bad vector element size");
2772 SelectBaseMVE_VMLLDAV(N, Predicated, OpcodesS, OpcodesU, 1, 0);
2773}
2774
2775void ARMDAGToDAGISel::SelectMVE_VLD(SDNode *N, unsigned NumVecs,
2776 const uint16_t *const *Opcodes,
2777 bool HasWriteback) {
2778 EVT VT = N->getValueType(0);
2779 SDLoc Loc(N);
2780
2781 const uint16_t *OurOpcodes;
2782 switch (VT.getVectorElementType().getSizeInBits()) {
2783 case 8:
2784 OurOpcodes = Opcodes[0];
2785 break;
2786 case 16:
2787 OurOpcodes = Opcodes[1];
2788 break;
2789 case 32:
2790 OurOpcodes = Opcodes[2];
2791 break;
2792 default:
2793 llvm_unreachable("bad vector element size in SelectMVE_VLD");
2794 }
2795
2796 EVT DataTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, NumVecs * 2);
2797 SmallVector<EVT, 4> ResultTys = {DataTy, MVT::Other};
2798 unsigned PtrOperand = HasWriteback ? 1 : 2;
2799
2800 auto Data = SDValue(
2801 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, Loc, DataTy), 0);
2802 SDValue Chain = N->getOperand(0);
2803 // Add a MVE_VLDn instruction for each Vec, except the last
2804 for (unsigned Stage = 0; Stage < NumVecs - 1; ++Stage) {
2805 SDValue Ops[] = {Data, N->getOperand(PtrOperand), Chain};
2806 auto LoadInst =
2807 CurDAG->getMachineNode(OurOpcodes[Stage], Loc, ResultTys, Ops);
2808 Data = SDValue(LoadInst, 0);
2809 Chain = SDValue(LoadInst, 1);
2810 transferMemOperands(N, LoadInst);
2811 }
2812 // The last may need a writeback on it
2813 if (HasWriteback)
2814 ResultTys = {DataTy, MVT::i32, MVT::Other};
2815 SDValue Ops[] = {Data, N->getOperand(PtrOperand), Chain};
2816 auto LoadInst =
2817 CurDAG->getMachineNode(OurOpcodes[NumVecs - 1], Loc, ResultTys, Ops);
2818 transferMemOperands(N, LoadInst);
2819
2820 unsigned i;
2821 for (i = 0; i < NumVecs; i++)
2822 ReplaceUses(SDValue(N, i),
2823 CurDAG->getTargetExtractSubreg(ARM::qsub_0 + i, Loc, VT,
2824 SDValue(LoadInst, 0)));
2825 if (HasWriteback)
2826 ReplaceUses(SDValue(N, i++), SDValue(LoadInst, 1));
2827 ReplaceUses(SDValue(N, i), SDValue(LoadInst, HasWriteback ? 2 : 1));
2828 CurDAG->RemoveDeadNode(N);
2829}
2830
2831void ARMDAGToDAGISel::SelectMVE_VxDUP(SDNode *N, const uint16_t *Opcodes,
2832 bool Wrapping, bool Predicated) {
2833 EVT VT = N->getValueType(0);
2834 SDLoc Loc(N);
2835
2836 uint16_t Opcode;
2837 switch (VT.getScalarSizeInBits()) {
2838 case 8:
2839 Opcode = Opcodes[0];
2840 break;
2841 case 16:
2842 Opcode = Opcodes[1];
2843 break;
2844 case 32:
2845 Opcode = Opcodes[2];
2846 break;
2847 default:
2848 llvm_unreachable("bad vector element size in SelectMVE_VxDUP");
2849 }
2850
2852 unsigned OpIdx = 1;
2853
2854 SDValue Inactive;
2855 if (Predicated)
2856 Inactive = N->getOperand(OpIdx++);
2857
2858 Ops.push_back(N->getOperand(OpIdx++)); // base
2859 if (Wrapping)
2860 Ops.push_back(N->getOperand(OpIdx++)); // limit
2861
2862 SDValue ImmOp = N->getOperand(OpIdx++); // step
2863 int ImmValue = ImmOp->getAsZExtVal();
2864 Ops.push_back(getI32Imm(ImmValue, Loc));
2865
2866 if (Predicated)
2867 AddMVEPredicateToOps(Ops, Loc, N->getOperand(OpIdx), Inactive);
2868 else
2869 AddEmptyMVEPredicateToOps(Ops, Loc, N->getValueType(0));
2870
2871 CurDAG->SelectNodeTo(N, Opcode, N->getVTList(), ArrayRef(Ops));
2872}
2873
2874void ARMDAGToDAGISel::SelectCDE_CXxD(SDNode *N, uint16_t Opcode,
2875 size_t NumExtraOps, bool HasAccum) {
2876 bool IsBigEndian = CurDAG->getDataLayout().isBigEndian();
2877 SDLoc Loc(N);
2879
2880 unsigned OpIdx = 1;
2881
2882 // Convert and append the immediate operand designating the coprocessor.
2883 SDValue ImmCorpoc = N->getOperand(OpIdx++);
2884 uint32_t ImmCoprocVal = ImmCorpoc->getAsZExtVal();
2885 Ops.push_back(getI32Imm(ImmCoprocVal, Loc));
2886
2887 // For accumulating variants copy the low and high order parts of the
2888 // accumulator into a register pair and add it to the operand vector.
2889 if (HasAccum) {
2890 SDValue AccLo = N->getOperand(OpIdx++);
2891 SDValue AccHi = N->getOperand(OpIdx++);
2892 if (IsBigEndian)
2893 std::swap(AccLo, AccHi);
2894 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, AccLo, AccHi), 0));
2895 }
2896
2897 // Copy extra operands as-is.
2898 for (size_t I = 0; I < NumExtraOps; I++)
2899 Ops.push_back(N->getOperand(OpIdx++));
2900
2901 // Convert and append the immediate operand
2902 SDValue Imm = N->getOperand(OpIdx);
2903 uint32_t ImmVal = Imm->getAsZExtVal();
2904 Ops.push_back(getI32Imm(ImmVal, Loc));
2905
2906 // Accumulating variants are IT-predicable, add predicate operands.
2907 if (HasAccum) {
2908 SDValue Pred = getAL(CurDAG, Loc);
2909 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2910 Ops.push_back(Pred);
2911 Ops.push_back(PredReg);
2912 }
2913
2914 // Create the CDE instruction
2915 SDNode *InstrNode = CurDAG->getMachineNode(Opcode, Loc, MVT::Untyped, Ops);
2916 SDValue ResultPair = SDValue(InstrNode, 0);
2917
2918 // The original intrinsic had two outputs, and the output of the dual-register
2919 // CDE instruction is a register pair. We need to extract the two subregisters
2920 // and replace all uses of the original outputs with the extracted
2921 // subregisters.
2922 uint16_t SubRegs[2] = {ARM::gsub_0, ARM::gsub_1};
2923 if (IsBigEndian)
2924 std::swap(SubRegs[0], SubRegs[1]);
2925
2926 for (size_t ResIdx = 0; ResIdx < 2; ResIdx++) {
2927 if (SDValue(N, ResIdx).use_empty())
2928 continue;
2929 SDValue SubReg = CurDAG->getTargetExtractSubreg(SubRegs[ResIdx], Loc,
2930 MVT::i32, ResultPair);
2931 ReplaceUses(SDValue(N, ResIdx), SubReg);
2932 }
2933
2934 CurDAG->RemoveDeadNode(N);
2935}
2936
2937void ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool IsIntrinsic,
2938 bool isUpdating, unsigned NumVecs,
2939 const uint16_t *DOpcodes,
2940 const uint16_t *QOpcodes0,
2941 const uint16_t *QOpcodes1) {
2942 assert(Subtarget->hasNEON());
2943 assert(NumVecs >= 1 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
2944 SDLoc dl(N);
2945
2946 SDValue MemAddr, Align;
2947 unsigned AddrOpIdx = IsIntrinsic ? 2 : 1;
2948 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2949 return;
2950
2951 SDValue Chain = N->getOperand(0);
2952 EVT VT = N->getValueType(0);
2953 bool is64BitVector = VT.is64BitVector();
2954
2955 unsigned Alignment = 0;
2956 if (NumVecs != 3) {
2957 Alignment = Align->getAsZExtVal();
2958 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8;
2959 if (Alignment > NumBytes)
2960 Alignment = NumBytes;
2961 if (Alignment < 8 && Alignment < NumBytes)
2962 Alignment = 0;
2963 // Alignment must be a power of two; make sure of that.
2965 if (Alignment == 1)
2966 Alignment = 0;
2967 }
2968 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
2969
2970 unsigned OpcodeIndex;
2971 switch (VT.getSimpleVT().SimpleTy) {
2972 default: llvm_unreachable("unhandled vld-dup type");
2973 case MVT::v8i8:
2974 case MVT::v16i8: OpcodeIndex = 0; break;
2975 case MVT::v4i16:
2976 case MVT::v8i16:
2977 case MVT::v4f16:
2978 case MVT::v8f16:
2979 case MVT::v4bf16:
2980 case MVT::v8bf16:
2981 OpcodeIndex = 1; break;
2982 case MVT::v2f32:
2983 case MVT::v2i32:
2984 case MVT::v4f32:
2985 case MVT::v4i32: OpcodeIndex = 2; break;
2986 case MVT::v1f64:
2987 case MVT::v1i64: OpcodeIndex = 3; break;
2988 }
2989
2990 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2991 if (!is64BitVector)
2992 ResTyElts *= 2;
2993 EVT ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
2994
2995 std::vector<EVT> ResTys;
2996 ResTys.push_back(ResTy);
2997 if (isUpdating)
2998 ResTys.push_back(MVT::i32);
2999 ResTys.push_back(MVT::Other);
3000
3001 SDValue Pred = getAL(CurDAG, dl);
3002 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
3003
3005 Ops.push_back(MemAddr);
3006 Ops.push_back(Align);
3007 unsigned Opc = is64BitVector ? DOpcodes[OpcodeIndex]
3008 : (NumVecs == 1) ? QOpcodes0[OpcodeIndex]
3009 : QOpcodes1[OpcodeIndex];
3010 if (isUpdating) {
3011 SDValue Inc = N->getOperand(2);
3012 bool IsImmUpdate =
3013 isPerfectIncrement(Inc, VT.getVectorElementType(), NumVecs);
3014 if (IsImmUpdate) {
3015 if (!isVLDfixed(Opc))
3016 Ops.push_back(Reg0);
3017 } else {
3018 if (isVLDfixed(Opc))
3020 Ops.push_back(Inc);
3021 }
3022 }
3023 if (is64BitVector || NumVecs == 1) {
3024 // Double registers and VLD1 quad registers are directly supported.
3025 } else {
3026 SDValue ImplDef = SDValue(
3027 CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
3028 const SDValue OpsA[] = {MemAddr, Align, ImplDef, Pred, Reg0, Chain};
3029 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl, ResTy,
3030 MVT::Other, OpsA);
3031 Ops.push_back(SDValue(VLdA, 0));
3032 Chain = SDValue(VLdA, 1);
3033 }
3034
3035 Ops.push_back(Pred);
3036 Ops.push_back(Reg0);
3037 Ops.push_back(Chain);
3038
3039 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
3040
3041 // Transfer memoperands.
3042 MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3043 CurDAG->setNodeMemRefs(cast<MachineSDNode>(VLdDup), {MemOp});
3044
3045 // Extract the subregisters.
3046 if (NumVecs == 1) {
3047 ReplaceUses(SDValue(N, 0), SDValue(VLdDup, 0));
3048 } else {
3049 SDValue SuperReg = SDValue(VLdDup, 0);
3050 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7, "Unexpected subreg numbering");
3051 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
3052 for (unsigned Vec = 0; Vec != NumVecs; ++Vec) {
3053 ReplaceUses(SDValue(N, Vec),
3054 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
3055 }
3056 }
3057 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1));
3058 if (isUpdating)
3059 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2));
3060 CurDAG->RemoveDeadNode(N);
3061}
3062
3063bool ARMDAGToDAGISel::tryInsertVectorElt(SDNode *N) {
3064 if (!Subtarget->hasMVEIntegerOps())
3065 return false;
3066
3067 SDLoc dl(N);
3068
3069 // We are trying to use VMOV/VMOVX/VINS to more efficiently lower insert and
3070 // extracts of v8f16 and v8i16 vectors. Check that we have two adjacent
3071 // inserts of the correct type:
3072 SDValue Ins1 = SDValue(N, 0);
3073 SDValue Ins2 = N->getOperand(0);
3074 EVT VT = Ins1.getValueType();
3075 if (Ins2.getOpcode() != ISD::INSERT_VECTOR_ELT || !Ins2.hasOneUse() ||
3076 !isa<ConstantSDNode>(Ins1.getOperand(2)) ||
3077 !isa<ConstantSDNode>(Ins2.getOperand(2)) ||
3078 (VT != MVT::v8f16 && VT != MVT::v8i16) || (Ins2.getValueType() != VT))
3079 return false;
3080
3081 unsigned Lane1 = Ins1.getConstantOperandVal(2);
3082 unsigned Lane2 = Ins2.getConstantOperandVal(2);
3083 if (Lane2 % 2 != 0 || Lane1 != Lane2 + 1)
3084 return false;
3085
3086 // If the inserted values will be able to use T/B already, leave it to the
3087 // existing tablegen patterns. For example VCVTT/VCVTB.
3088 SDValue Val1 = Ins1.getOperand(1);
3089 SDValue Val2 = Ins2.getOperand(1);
3090 if (Val1.getOpcode() == ISD::FP_ROUND || Val2.getOpcode() == ISD::FP_ROUND)
3091 return false;
3092
3093 // Check if the inserted values are both extracts.
3094 if ((Val1.getOpcode() == ISD::EXTRACT_VECTOR_ELT ||
3095 Val1.getOpcode() == ARMISD::VGETLANEu) &&
3097 Val2.getOpcode() == ARMISD::VGETLANEu) &&
3100 (Val1.getOperand(0).getValueType() == MVT::v8f16 ||
3101 Val1.getOperand(0).getValueType() == MVT::v8i16) &&
3102 (Val2.getOperand(0).getValueType() == MVT::v8f16 ||
3103 Val2.getOperand(0).getValueType() == MVT::v8i16)) {
3104 unsigned ExtractLane1 = Val1.getConstantOperandVal(1);
3105 unsigned ExtractLane2 = Val2.getConstantOperandVal(1);
3106
3107 // If the two extracted lanes are from the same place and adjacent, this
3108 // simplifies into a f32 lane move.
3109 if (Val1.getOperand(0) == Val2.getOperand(0) && ExtractLane2 % 2 == 0 &&
3110 ExtractLane1 == ExtractLane2 + 1) {
3111 SDValue NewExt = CurDAG->getTargetExtractSubreg(
3112 ARM::ssub_0 + ExtractLane2 / 2, dl, MVT::f32, Val1.getOperand(0));
3113 SDValue NewIns = CurDAG->getTargetInsertSubreg(
3114 ARM::ssub_0 + Lane2 / 2, dl, VT, Ins2.getOperand(0),
3115 NewExt);
3116 ReplaceUses(Ins1, NewIns);
3117 return true;
3118 }
3119
3120 // Else v8i16 pattern of an extract and an insert, with a optional vmovx for
3121 // extracting odd lanes.
3122 if (VT == MVT::v8i16 && Subtarget->hasFullFP16()) {
3123 SDValue Inp1 = CurDAG->getTargetExtractSubreg(
3124 ARM::ssub_0 + ExtractLane1 / 2, dl, MVT::f32, Val1.getOperand(0));
3125 SDValue Inp2 = CurDAG->getTargetExtractSubreg(
3126 ARM::ssub_0 + ExtractLane2 / 2, dl, MVT::f32, Val2.getOperand(0));
3127 if (ExtractLane1 % 2 != 0)
3128 Inp1 = SDValue(CurDAG->getMachineNode(ARM::VMOVH, dl, MVT::f32, Inp1), 0);
3129 if (ExtractLane2 % 2 != 0)
3130 Inp2 = SDValue(CurDAG->getMachineNode(ARM::VMOVH, dl, MVT::f32, Inp2), 0);
3131 SDNode *VINS = CurDAG->getMachineNode(ARM::VINSH, dl, MVT::f32, Inp2, Inp1);
3132 SDValue NewIns =
3133 CurDAG->getTargetInsertSubreg(ARM::ssub_0 + Lane2 / 2, dl, MVT::v4f32,
3134 Ins2.getOperand(0), SDValue(VINS, 0));
3135 ReplaceUses(Ins1, NewIns);
3136 return true;
3137 }
3138 }
3139
3140 // The inserted values are not extracted - if they are f16 then insert them
3141 // directly using a VINS.
3142 if (VT == MVT::v8f16 && Subtarget->hasFullFP16()) {
3143 SDNode *VINS = CurDAG->getMachineNode(ARM::VINSH, dl, MVT::f32, Val2, Val1);
3144 SDValue NewIns =
3145 CurDAG->getTargetInsertSubreg(ARM::ssub_0 + Lane2 / 2, dl, MVT::v4f32,
3146 Ins2.getOperand(0), SDValue(VINS, 0));
3147 ReplaceUses(Ins1, NewIns);
3148 return true;
3149 }
3150
3151 return false;
3152}
3153
3154/// tryShiftAmountMod - Take advantage of built-in mod of shift amount in
3155/// variable shift/rotate instructions.
3156bool ARMDAGToDAGISel::tryShiftAmountMod(SDNode *N) {
3157 EVT VT = N->getValueType(0);
3158 if (VT != MVT::i32)
3159 return false;
3160 // On ARM we intentionally do this only for ROTR. Unlike AArch64, variable
3161 // SHL/SRL/SRA do not all have the same modulo-shift semantics we can exploit.
3162 // Select ROR by register; in ARM state this is modeled as MOVsr with a ROR
3163 // shifter operand, while in Thumb we use tROR/t2RORrr directly.
3164
3165 SDValue ShiftAmt = N->getOperand(1);
3166 SDLoc DL(N);
3167 SDValue NewShiftAmt;
3168 auto emitUnary = [&](unsigned Opc, SDValue Src, bool IsRSB) {
3169 if (Subtarget->isThumb2() || !Subtarget->isThumb()) {
3170 SDValue Ops[] = {Src};
3171 if (IsRSB) {
3172 SDValue ZeroImm = CurDAG->getTargetConstant(0, DL, MVT::i32);
3173 SDValue FullOps[] = {Src, ZeroImm, getAL(CurDAG, DL),
3174 CurDAG->getRegister(0, MVT::i32),
3175 CurDAG->getRegister(0, MVT::i32)};
3176 MachineSDNode *Unary =
3177 CurDAG->getMachineNode(Opc, DL, MVT::i32, FullOps);
3178 return SDValue(Unary, 0);
3179 }
3180 SDValue FullOps[] = {Ops[0], getAL(CurDAG, DL),
3181 CurDAG->getRegister(0, MVT::i32),
3182 CurDAG->getRegister(0, MVT::i32)};
3183 MachineSDNode *Unary = CurDAG->getMachineNode(Opc, DL, MVT::i32, FullOps);
3184 return SDValue(Unary, 0);
3185 }
3186 SDValue Thumb1Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), Src,
3187 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32)};
3188 MachineSDNode *Unary = CurDAG->getMachineNode(Opc, DL, MVT::i32, Thumb1Ops);
3189 return SDValue(Unary, 0);
3190 };
3191
3192 if (ShiftAmt->getOpcode() == ISD::ADD || ShiftAmt->getOpcode() == ISD::SUB) {
3193 SDValue Add0 = ShiftAmt->getOperand(0);
3194 SDValue Add1 = ShiftAmt->getOperand(1);
3195 unsigned Add0Imm;
3196 unsigned Add1Imm;
3197 if (isInt32Immediate(Add1, Add1Imm) && ((Add1Imm & 31) == 0)) {
3198 NewShiftAmt = Add0;
3199 } else if (ShiftAmt->getOpcode() == ISD::SUB &&
3200 isInt32Immediate(Add0, Add0Imm) && Add0Imm != 0 &&
3201 ((Add0Imm & 31) == 0)) {
3202 unsigned NegOpc =
3203 Subtarget->isThumb()
3204 ? (Subtarget->hasThumb2() ? ARM::t2RSBri : ARM::tRSB)
3205 : ARM::RSBri;
3206 NewShiftAmt = emitUnary(NegOpc, Add1, /*IsRSB=*/true);
3207 } else if (ShiftAmt->getOpcode() == ISD::SUB &&
3208 isInt32Immediate(Add0, Add0Imm) && ((Add0Imm & 31) == 31)) {
3209 unsigned NotOpc = Subtarget->isThumb()
3210 ? (Subtarget->isThumb2() ? ARM::t2MVNr : ARM::tMVN)
3211 : ARM::MVNr;
3212 NewShiftAmt = emitUnary(NotOpc, Add1, /*IsRSB=*/false);
3213 } else {
3214 return false;
3215 }
3216 } else {
3217 return false;
3218 }
3219
3220 if (Subtarget->isThumb()) {
3221 if (Subtarget->isThumb1Only()) {
3222 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32),
3223 N->getOperand(0), NewShiftAmt, getAL(CurDAG, DL),
3224 CurDAG->getRegister(0, MVT::i32)};
3225 CurDAG->SelectNodeTo(N, ARM::tROR, VT, Ops);
3226 } else {
3227 SDValue Ops[] = {N->getOperand(0), NewShiftAmt, getAL(CurDAG, DL),
3228 CurDAG->getRegister(0, MVT::i32),
3229 CurDAG->getRegister(0, MVT::i32)};
3230 CurDAG->SelectNodeTo(N, ARM::t2RORrr, VT, Ops);
3231 }
3232 } else {
3233 SDValue BaseReg = N->getOperand(0);
3234 SDValue ShReg = NewShiftAmt;
3235 SDValue OpcEnc = CurDAG->getTargetConstant(
3236 ARM_AM::getSORegOpc(ARM_AM::ror, 0), DL, MVT::i32);
3237 SDValue Ops[] = {BaseReg,
3238 ShReg,
3239 OpcEnc,
3240 getAL(CurDAG, DL),
3241 CurDAG->getRegister(0, MVT::i32),
3242 CurDAG->getRegister(0, MVT::i32)};
3243 CurDAG->SelectNodeTo(N, ARM::MOVsr, VT, Ops);
3244 }
3245 return true;
3246}
3247
3248bool ARMDAGToDAGISel::transformFixedFloatingPointConversion(SDNode *N,
3249 SDNode *FMul,
3250 bool IsUnsigned,
3251 bool FixedToFloat) {
3252 auto Type = N->getValueType(0);
3253 unsigned ScalarBits = Type.getScalarSizeInBits();
3254 if (ScalarBits > 32)
3255 return false;
3256
3257 SDNodeFlags FMulFlags = FMul->getFlags();
3258 // The fixed-point vcvt and vcvt+vmul are not always equivalent if inf is
3259 // allowed in 16 bit unsigned floats
3260 if (ScalarBits == 16 && !FMulFlags.hasNoInfs() && IsUnsigned)
3261 return false;
3262
3263 SDValue ImmNode = FMul->getOperand(1);
3264 SDValue VecVal = FMul->getOperand(0);
3265 if (VecVal->getOpcode() == ISD::UINT_TO_FP ||
3266 VecVal->getOpcode() == ISD::SINT_TO_FP)
3267 VecVal = VecVal->getOperand(0);
3268
3269 if (VecVal.getValueType().getScalarSizeInBits() != ScalarBits)
3270 return false;
3271
3272 if (ImmNode.getOpcode() == ISD::BITCAST) {
3273 if (ImmNode.getValueType().getScalarSizeInBits() != ScalarBits)
3274 return false;
3275 ImmNode = ImmNode.getOperand(0);
3276 }
3277
3278 if (ImmNode.getValueType().getScalarSizeInBits() != ScalarBits)
3279 return false;
3280
3281 APFloat ImmAPF(0.0f);
3282 switch (ImmNode.getOpcode()) {
3283 case ARMISD::VMOVIMM:
3284 case ARMISD::VDUP: {
3285 if (!isa<ConstantSDNode>(ImmNode.getOperand(0)))
3286 return false;
3287 unsigned Imm = ImmNode.getConstantOperandVal(0);
3288 if (ImmNode.getOpcode() == ARMISD::VMOVIMM)
3289 Imm = ARM_AM::decodeVMOVModImm(Imm, ScalarBits);
3290 ImmAPF =
3291 APFloat(ScalarBits == 32 ? APFloat::IEEEsingle() : APFloat::IEEEhalf(),
3292 APInt(ScalarBits, Imm));
3293 break;
3294 }
3295 case ARMISD::VMOVFPIMM: {
3297 break;
3298 }
3299 default:
3300 return false;
3301 }
3302
3303 // Where n is the number of fractional bits, multiplying by 2^n will convert
3304 // from float to fixed and multiplying by 2^-n will convert from fixed to
3305 // float. Taking log2 of the factor (after taking the inverse in the case of
3306 // float to fixed) will give n.
3307 APFloat ToConvert = ImmAPF;
3308 if (FixedToFloat) {
3309 if (!ImmAPF.getExactInverse(&ToConvert))
3310 return false;
3311 }
3312 APSInt Converted(64, false);
3313 bool IsExact;
3315 &IsExact);
3316 if (!IsExact || !Converted.isPowerOf2())
3317 return false;
3318
3319 unsigned FracBits = Converted.logBase2();
3320 if (FracBits > ScalarBits)
3321 return false;
3322
3324 VecVal, CurDAG->getTargetConstant(FracBits, SDLoc(N), MVT::i32)};
3325 AddEmptyMVEPredicateToOps(Ops, SDLoc(N), Type);
3326
3327 unsigned int Opcode;
3328 switch (ScalarBits) {
3329 case 16:
3330 if (FixedToFloat)
3331 Opcode = IsUnsigned ? ARM::MVE_VCVTf16u16_fix : ARM::MVE_VCVTf16s16_fix;
3332 else
3333 Opcode = IsUnsigned ? ARM::MVE_VCVTu16f16_fix : ARM::MVE_VCVTs16f16_fix;
3334 break;
3335 case 32:
3336 if (FixedToFloat)
3337 Opcode = IsUnsigned ? ARM::MVE_VCVTf32u32_fix : ARM::MVE_VCVTf32s32_fix;
3338 else
3339 Opcode = IsUnsigned ? ARM::MVE_VCVTu32f32_fix : ARM::MVE_VCVTs32f32_fix;
3340 break;
3341 default:
3342 llvm_unreachable("unexpected number of scalar bits");
3343 break;
3344 }
3345
3346 ReplaceNode(N, CurDAG->getMachineNode(Opcode, SDLoc(N), Type, Ops));
3347 return true;
3348}
3349
3350bool ARMDAGToDAGISel::tryFP_TO_INT(SDNode *N, SDLoc dl) {
3351 // Transform a floating-point to fixed-point conversion to a VCVT
3352 if (!Subtarget->hasMVEFloatOps())
3353 return false;
3354 EVT Type = N->getValueType(0);
3355 if (!Type.isVector())
3356 return false;
3357 unsigned int ScalarBits = Type.getScalarSizeInBits();
3358
3359 bool IsUnsigned = N->getOpcode() == ISD::FP_TO_UINT ||
3360 N->getOpcode() == ISD::FP_TO_UINT_SAT;
3361 SDNode *Node = N->getOperand(0).getNode();
3362
3363 // floating-point to fixed-point with one fractional bit gets turned into an
3364 // FP_TO_[U|S]INT(FADD (x, x)) rather than an FP_TO_[U|S]INT(FMUL (x, y))
3365 if (Node->getOpcode() == ISD::FADD) {
3366 if (Node->getOperand(0) != Node->getOperand(1))
3367 return false;
3368 SDNodeFlags Flags = Node->getFlags();
3369 // The fixed-point vcvt and vcvt+vmul are not always equivalent if inf is
3370 // allowed in 16 bit unsigned floats
3371 if (ScalarBits == 16 && !Flags.hasNoInfs() && IsUnsigned)
3372 return false;
3373
3374 unsigned Opcode;
3375 switch (ScalarBits) {
3376 case 16:
3377 Opcode = IsUnsigned ? ARM::MVE_VCVTu16f16_fix : ARM::MVE_VCVTs16f16_fix;
3378 break;
3379 case 32:
3380 Opcode = IsUnsigned ? ARM::MVE_VCVTu32f32_fix : ARM::MVE_VCVTs32f32_fix;
3381 break;
3382 }
3383 SmallVector<SDValue, 3> Ops{Node->getOperand(0),
3384 CurDAG->getConstant(1, dl, MVT::i32)};
3385 AddEmptyMVEPredicateToOps(Ops, dl, Type);
3386
3387 ReplaceNode(N, CurDAG->getMachineNode(Opcode, dl, Type, Ops));
3388 return true;
3389 }
3390
3391 if (Node->getOpcode() != ISD::FMUL)
3392 return false;
3393
3394 return transformFixedFloatingPointConversion(N, Node, IsUnsigned, false);
3395}
3396
3397bool ARMDAGToDAGISel::tryFMULFixed(SDNode *N, SDLoc dl) {
3398 // Transform a fixed-point to floating-point conversion to a VCVT
3399 if (!Subtarget->hasMVEFloatOps())
3400 return false;
3401 auto Type = N->getValueType(0);
3402 if (!Type.isVector())
3403 return false;
3404
3405 auto LHS = N->getOperand(0);
3406 if (LHS.getOpcode() != ISD::SINT_TO_FP && LHS.getOpcode() != ISD::UINT_TO_FP)
3407 return false;
3408
3409 return transformFixedFloatingPointConversion(
3410 N, N, LHS.getOpcode() == ISD::UINT_TO_FP, true);
3411}
3412
3413bool ARMDAGToDAGISel::tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned) {
3414 if (!Subtarget->hasV6T2Ops())
3415 return false;
3416
3417 unsigned Opc = isSigned
3418 ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
3419 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
3420 SDLoc dl(N);
3421
3422 // For unsigned extracts, check for a shift right and mask
3423 unsigned And_imm = 0;
3424 if (N->getOpcode() == ISD::AND) {
3425 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
3426
3427 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
3428 if (And_imm & (And_imm + 1))
3429 return false;
3430
3431 unsigned Srl_imm = 0;
3432 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
3433 Srl_imm)) {
3434 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
3435
3436 // Mask off the unnecessary bits of the AND immediate; normally
3437 // DAGCombine will do this, but that might not happen if
3438 // targetShrinkDemandedConstant chooses a different immediate.
3439 And_imm &= -1U >> Srl_imm;
3440
3441 // Note: The width operand is encoded as width-1.
3442 unsigned Width = llvm::countr_one(And_imm) - 1;
3443 unsigned LSB = Srl_imm;
3444
3445 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
3446
3447 if ((LSB + Width + 1) == N->getValueType(0).getSizeInBits()) {
3448 // It's cheaper to use a right shift to extract the top bits.
3449 if (Subtarget->isThumb()) {
3450 Opc = isSigned ? ARM::t2ASRri : ARM::t2LSRri;
3451 SDValue Ops[] = { N->getOperand(0).getOperand(0),
3452 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
3453 getAL(CurDAG, dl), Reg0, Reg0 };
3454 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
3455 return true;
3456 }
3457
3458 // ARM models shift instructions as MOVsi with shifter operand.
3460 SDValue ShOpc =
3461 CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, LSB), dl,
3462 MVT::i32);
3463 SDValue Ops[] = { N->getOperand(0).getOperand(0), ShOpc,
3464 getAL(CurDAG, dl), Reg0, Reg0 };
3465 CurDAG->SelectNodeTo(N, ARM::MOVsi, MVT::i32, Ops);
3466 return true;
3467 }
3468
3469 assert(LSB + Width + 1 <= 32 && "Shouldn't create an invalid ubfx");
3470 SDValue Ops[] = { N->getOperand(0).getOperand(0),
3471 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
3472 CurDAG->getTargetConstant(Width, dl, MVT::i32),
3473 getAL(CurDAG, dl), Reg0 };
3474 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
3475 return true;
3476 }
3477 }
3478 return false;
3479 }
3480
3481 // Otherwise, we're looking for a shift of a shift
3482 unsigned Shl_imm = 0;
3483 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
3484 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
3485 unsigned Srl_imm = 0;
3486 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
3487 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
3488 // Note: The width operand is encoded as width-1.
3489 unsigned Width = 32 - Srl_imm - 1;
3490 int LSB = Srl_imm - Shl_imm;
3491 if (LSB < 0)
3492 return false;
3493 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
3494 assert(LSB + Width + 1 <= 32 && "Shouldn't create an invalid ubfx");
3495 SDValue Ops[] = { N->getOperand(0).getOperand(0),
3496 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
3497 CurDAG->getTargetConstant(Width, dl, MVT::i32),
3498 getAL(CurDAG, dl), Reg0 };
3499 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
3500 return true;
3501 }
3502 }
3503
3504 // Or we are looking for a shift of an and, with a mask operand
3505 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, And_imm) &&
3506 isShiftedMask_32(And_imm)) {
3507 unsigned Srl_imm = 0;
3508 unsigned LSB = llvm::countr_zero(And_imm);
3509 // Shift must be the same as the ands lsb
3510 if (isInt32Immediate(N->getOperand(1), Srl_imm) && Srl_imm == LSB) {
3511 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
3512 unsigned MSB = llvm::Log2_32(And_imm);
3513 // Note: The width operand is encoded as width-1.
3514 unsigned Width = MSB - LSB;
3515 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
3516 assert(Srl_imm + Width + 1 <= 32 && "Shouldn't create an invalid ubfx");
3517 SDValue Ops[] = { N->getOperand(0).getOperand(0),
3518 CurDAG->getTargetConstant(Srl_imm, dl, MVT::i32),
3519 CurDAG->getTargetConstant(Width, dl, MVT::i32),
3520 getAL(CurDAG, dl), Reg0 };
3521 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
3522 return true;
3523 }
3524 }
3525
3526 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG) {
3527 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
3528 unsigned LSB = 0;
3529 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, LSB) &&
3530 !isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRA, LSB))
3531 return false;
3532
3533 if (LSB + Width > 32)
3534 return false;
3535
3536 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
3537 assert(LSB + Width <= 32 && "Shouldn't create an invalid ubfx");
3538 SDValue Ops[] = { N->getOperand(0).getOperand(0),
3539 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
3540 CurDAG->getTargetConstant(Width - 1, dl, MVT::i32),
3541 getAL(CurDAG, dl), Reg0 };
3542 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
3543 return true;
3544 }
3545
3546 return false;
3547}
3548
3549/// We've got special pseudo-instructions for these
3550void ARMDAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
3551 unsigned Opcode;
3552 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
3553 if (MemTy == MVT::i8)
3554 Opcode = Subtarget->isThumb() ? ARM::tCMP_SWAP_8 : ARM::CMP_SWAP_8;
3555 else if (MemTy == MVT::i16)
3556 Opcode = Subtarget->isThumb() ? ARM::tCMP_SWAP_16 : ARM::CMP_SWAP_16;
3557 else if (MemTy == MVT::i32)
3558 Opcode = Subtarget->isThumb() ? ARM::tCMP_SWAP_32 : ARM::CMP_SWAP_32;
3559 else
3560 llvm_unreachable("Unknown AtomicCmpSwap type");
3561
3562 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
3563 N->getOperand(0)};
3564 SDNode *CmpSwap = CurDAG->getMachineNode(
3565 Opcode, SDLoc(N),
3566 CurDAG->getVTList(MVT::i32, MVT::i32, MVT::Other), Ops);
3567
3568 MachineMemOperand *MemOp = cast<MemSDNode>(N)->getMemOperand();
3569 CurDAG->setNodeMemRefs(cast<MachineSDNode>(CmpSwap), {MemOp});
3570
3571 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
3572 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
3573 CurDAG->RemoveDeadNode(N);
3574}
3575
3576static std::optional<std::pair<unsigned, unsigned>>
3578 unsigned FirstOne = A.getBitWidth() - A.countl_zero() - 1;
3579 unsigned LastOne = A.countr_zero();
3580 if (A.popcount() != (FirstOne - LastOne + 1))
3581 return std::nullopt;
3582 return std::make_pair(FirstOne, LastOne);
3583}
3584
3585void ARMDAGToDAGISel::SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI) {
3586 assert(N->getOpcode() == ARMISD::CMPZ);
3587 SwitchEQNEToPLMI = false;
3588
3589 if (!Subtarget->isThumb())
3590 // FIXME: Work out whether it is profitable to do this in A32 mode - LSL and
3591 // LSR don't exist as standalone instructions - they need the barrel shifter.
3592 return;
3593
3594 // select (cmpz (and X, C), #0) -> (LSLS X) or (LSRS X) or (LSRS (LSLS X))
3595 SDValue And = N->getOperand(0);
3596 if (!And->hasOneUse())
3597 return;
3598
3599 SDValue Zero = N->getOperand(1);
3600 if (!isNullConstant(Zero) || And->getOpcode() != ISD::AND)
3601 return;
3602 SDValue X = And.getOperand(0);
3603 auto C = dyn_cast<ConstantSDNode>(And.getOperand(1));
3604
3605 if (!C)
3606 return;
3607 auto Range = getContiguousRangeOfSetBits(C->getAPIntValue());
3608 if (!Range)
3609 return;
3610
3611 // There are several ways to lower this:
3612 SDNode *NewN;
3613 SDLoc dl(N);
3614
3615 auto EmitShift = [&](unsigned Opc, SDValue Src, unsigned Imm) -> SDNode* {
3616 if (Subtarget->isThumb2()) {
3617 Opc = (Opc == ARM::tLSLri) ? ARM::t2LSLri : ARM::t2LSRri;
3618 SDValue Ops[] = { Src, CurDAG->getTargetConstant(Imm, dl, MVT::i32),
3619 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
3620 CurDAG->getRegister(0, MVT::i32) };
3621 return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
3622 } else {
3623 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), Src,
3624 CurDAG->getTargetConstant(Imm, dl, MVT::i32),
3625 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
3626 return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
3627 }
3628 };
3629
3630 if (Range->second == 0) {
3631 // 1. Mask includes the LSB -> Simply shift the top N bits off
3632 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
3633 ReplaceNode(And.getNode(), NewN);
3634 } else if (Range->first == 31) {
3635 // 2. Mask includes the MSB -> Simply shift the bottom N bits off
3636 NewN = EmitShift(ARM::tLSRri, X, Range->second);
3637 ReplaceNode(And.getNode(), NewN);
3638 } else if (Range->first == Range->second) {
3639 // 3. Only one bit is set. We can shift this into the sign bit and use a
3640 // PL/MI comparison. This is not safe if CMPZ has multiple uses because
3641 // only one of them (the one currently being selected) will be switched
3642 // to use the new condition code.
3643 if (!N->hasOneUse())
3644 return;
3645 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
3646 ReplaceNode(And.getNode(), NewN);
3647
3648 SwitchEQNEToPLMI = true;
3649 } else if (!Subtarget->hasV6T2Ops()) {
3650 // 4. Do a double shift to clear bottom and top bits, but only in
3651 // thumb-1 mode as in thumb-2 we can use UBFX.
3652 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
3653 NewN = EmitShift(ARM::tLSRri, SDValue(NewN, 0),
3654 Range->second + (31 - Range->first));
3655 ReplaceNode(And.getNode(), NewN);
3656 }
3657}
3658
3659static unsigned getVectorShuffleOpcode(EVT VT, unsigned Opc64[3],
3660 unsigned Opc128[3]) {
3661 assert((VT.is64BitVector() || VT.is128BitVector()) &&
3662 "Unexpected vector shuffle length");
3663 switch (VT.getScalarSizeInBits()) {
3664 default:
3665 llvm_unreachable("Unexpected vector shuffle element size");
3666 case 8:
3667 return VT.is64BitVector() ? Opc64[0] : Opc128[0];
3668 case 16:
3669 return VT.is64BitVector() ? Opc64[1] : Opc128[1];
3670 case 32:
3671 return VT.is64BitVector() ? Opc64[2] : Opc128[2];
3672 }
3673}
3674
3675void ARMDAGToDAGISel::Select(SDNode *N) {
3676 SDLoc dl(N);
3677
3678 if (N->isMachineOpcode()) {
3679 N->setNodeId(-1);
3680 return; // Already selected.
3681 }
3682
3683 switch (N->getOpcode()) {
3684 default: break;
3685 case ISD::STORE: {
3686 // For Thumb1, match an sp-relative store in C++. This is a little
3687 // unfortunate, but I don't think I can make the chain check work
3688 // otherwise. (The chain of the store has to be the same as the chain
3689 // of the CopyFromReg, or else we can't replace the CopyFromReg with
3690 // a direct reference to "SP".)
3691 //
3692 // This is only necessary on Thumb1 because Thumb1 sp-relative stores use
3693 // a different addressing mode from other four-byte stores.
3694 //
3695 // This pattern usually comes up with call arguments.
3696 StoreSDNode *ST = cast<StoreSDNode>(N);
3697 SDValue Ptr = ST->getBasePtr();
3698 if (Subtarget->isThumb1Only() && ST->isUnindexed()) {
3699 int RHSC = 0;
3700 if (Ptr.getOpcode() == ISD::ADD &&
3701 isScaledConstantInRange(Ptr.getOperand(1), /*Scale=*/4, 0, 256, RHSC))
3702 Ptr = Ptr.getOperand(0);
3703
3704 if (Ptr.getOpcode() == ISD::CopyFromReg &&
3705 cast<RegisterSDNode>(Ptr.getOperand(1))->getReg() == ARM::SP &&
3706 Ptr.getOperand(0) == ST->getChain()) {
3707 SDValue Ops[] = {ST->getValue(),
3708 CurDAG->getRegister(ARM::SP, MVT::i32),
3709 CurDAG->getTargetConstant(RHSC, dl, MVT::i32),
3710 getAL(CurDAG, dl),
3711 CurDAG->getRegister(0, MVT::i32),
3712 ST->getChain()};
3713 MachineSDNode *ResNode =
3714 CurDAG->getMachineNode(ARM::tSTRspi, dl, MVT::Other, Ops);
3715 MachineMemOperand *MemOp = ST->getMemOperand();
3716 CurDAG->setNodeMemRefs(cast<MachineSDNode>(ResNode), {MemOp});
3717 ReplaceNode(N, ResNode);
3718 return;
3719 }
3720 }
3721 break;
3722 }
3724 if (tryWriteRegister(N))
3725 return;
3726 break;
3727 case ISD::READ_REGISTER:
3728 if (tryReadRegister(N))
3729 return;
3730 break;
3731 case ISD::INLINEASM:
3732 case ISD::INLINEASM_BR:
3733 if (tryInlineAsm(N))
3734 return;
3735 break;
3736 case ISD::Constant: {
3737 unsigned Val = N->getAsZExtVal();
3738 // If we can't materialize the constant we need to use a literal pool
3739 if (ConstantMaterializationCost(Val, Subtarget) > 2 &&
3740 !Subtarget->genExecuteOnly()) {
3741 SDValue CPIdx = CurDAG->getTargetConstantPool(
3742 ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), Val),
3743 TLI->getPointerTy(CurDAG->getDataLayout()));
3744
3745 SDNode *ResNode;
3746 if (Subtarget->isThumb()) {
3747 SDValue Ops[] = {
3748 CPIdx,
3749 getAL(CurDAG, dl),
3750 CurDAG->getRegister(0, MVT::i32),
3751 CurDAG->getEntryNode()
3752 };
3753 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
3754 Ops);
3755 } else {
3756 SDValue Ops[] = {
3757 CPIdx,
3758 CurDAG->getTargetConstant(0, dl, MVT::i32),
3759 getAL(CurDAG, dl),
3760 CurDAG->getRegister(0, MVT::i32),
3761 CurDAG->getEntryNode()
3762 };
3763 ResNode = CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
3764 Ops);
3765 }
3766 // Annotate the Node with memory operand information so that MachineInstr
3767 // queries work properly. This e.g. gives the register allocation the
3768 // required information for rematerialization.
3769 MachineFunction& MF = CurDAG->getMachineFunction();
3770 MachineMemOperand *MemOp =
3773
3774 CurDAG->setNodeMemRefs(cast<MachineSDNode>(ResNode), {MemOp});
3775
3776 ReplaceNode(N, ResNode);
3777 return;
3778 }
3779
3780 // Other cases are autogenerated.
3781 break;
3782 }
3783 case ISD::FrameIndex: {
3784 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
3785 int FI = cast<FrameIndexSDNode>(N)->getIndex();
3786 SDValue TFI = CurDAG->getTargetFrameIndex(
3787 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
3788 if (Subtarget->isThumb1Only()) {
3789 // Set the alignment of the frame object to 4, to avoid having to generate
3790 // more than one ADD
3791 MachineFrameInfo &MFI = MF->getFrameInfo();
3792 if (MFI.getObjectAlign(FI) < Align(4))
3793 MFI.setObjectAlignment(FI, Align(4));
3794 CurDAG->SelectNodeTo(N, ARM::tADDframe, MVT::i32, TFI,
3795 CurDAG->getTargetConstant(0, dl, MVT::i32));
3796 return;
3797 } else {
3798 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
3799 ARM::t2ADDri : ARM::ADDri);
3800 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, dl, MVT::i32),
3801 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
3802 CurDAG->getRegister(0, MVT::i32) };
3803 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
3804 return;
3805 }
3806 }
3808 if (tryInsertVectorElt(N))
3809 return;
3810 break;
3811 }
3812 case ISD::SRL:
3813 if (tryV6T2BitfieldExtractOp(N, false))
3814 return;
3815 break;
3817 case ISD::SRA:
3818 if (tryV6T2BitfieldExtractOp(N, true))
3819 return;
3820 break;
3821 case ISD::ROTR:
3822 if (tryShiftAmountMod(N))
3823 return;
3824 break;
3825 case ISD::FP_TO_UINT:
3826 case ISD::FP_TO_SINT:
3829 if (tryFP_TO_INT(N, dl))
3830 return;
3831 break;
3832 case ISD::FMUL:
3833 if (tryFMULFixed(N, dl))
3834 return;
3835 break;
3836 case ISD::MUL:
3837 if (Subtarget->isThumb1Only())
3838 break;
3839 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
3840 unsigned RHSV = C->getZExtValue();
3841 if (!RHSV) break;
3842 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
3843 unsigned ShImm = Log2_32(RHSV-1);
3844 if (ShImm >= 32)
3845 break;
3846 SDValue V = N->getOperand(0);
3847 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
3848 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
3849 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
3850 if (Subtarget->isThumb()) {
3851 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
3852 CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops);
3853 return;
3854 } else {
3855 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
3856 Reg0 };
3857 CurDAG->SelectNodeTo(N, ARM::ADDrsi, MVT::i32, Ops);
3858 return;
3859 }
3860 }
3861 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
3862 unsigned ShImm = Log2_32(RHSV+1);
3863 if (ShImm >= 32)
3864 break;
3865 SDValue V = N->getOperand(0);
3866 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
3867 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
3868 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
3869 if (Subtarget->isThumb()) {
3870 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
3871 CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops);
3872 return;
3873 } else {
3874 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
3875 Reg0 };
3876 CurDAG->SelectNodeTo(N, ARM::RSBrsi, MVT::i32, Ops);
3877 return;
3878 }
3879 }
3880 }
3881 break;
3882 case ISD::AND: {
3883 // Check for unsigned bitfield extract
3884 if (tryV6T2BitfieldExtractOp(N, false))
3885 return;
3886
3887 // If an immediate is used in an AND node, it is possible that the immediate
3888 // can be more optimally materialized when negated. If this is the case we
3889 // can negate the immediate and use a BIC instead.
3890 auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
3891 if (N1C && N1C->hasOneUse() && Subtarget->isThumb()) {
3892 uint32_t Imm = (uint32_t) N1C->getZExtValue();
3893
3894 // In Thumb2 mode, an AND can take a 12-bit immediate. If this
3895 // immediate can be negated and fit in the immediate operand of
3896 // a t2BIC, don't do any manual transform here as this can be
3897 // handled by the generic ISel machinery.
3898 bool PreferImmediateEncoding =
3899 Subtarget->hasThumb2() && (is_t2_so_imm(Imm) || is_t2_so_imm_not(Imm));
3900 if (!PreferImmediateEncoding &&
3903 // The current immediate costs more to materialize than a negated
3904 // immediate, so negate the immediate and use a BIC.
3905 SDValue NewImm = CurDAG->getConstant(~Imm, dl, MVT::i32);
3906 // If the new constant didn't exist before, reposition it in the topological
3907 // ordering so it is just before N. Otherwise, don't touch its location.
3908 if (NewImm->getNodeId() == -1)
3909 CurDAG->RepositionNode(N->getIterator(), NewImm.getNode());
3910
3911 if (!Subtarget->hasThumb2()) {
3912 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32),
3913 N->getOperand(0), NewImm, getAL(CurDAG, dl),
3914 CurDAG->getRegister(0, MVT::i32)};
3915 ReplaceNode(N, CurDAG->getMachineNode(ARM::tBIC, dl, MVT::i32, Ops));
3916 return;
3917 } else {
3918 SDValue Ops[] = {N->getOperand(0), NewImm, getAL(CurDAG, dl),
3919 CurDAG->getRegister(0, MVT::i32),
3920 CurDAG->getRegister(0, MVT::i32)};
3921 ReplaceNode(N,
3922 CurDAG->getMachineNode(ARM::t2BICrr, dl, MVT::i32, Ops));
3923 return;
3924 }
3925 }
3926 }
3927
3928 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
3929 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
3930 // are entirely contributed by c2 and lower 16-bits are entirely contributed
3931 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
3932 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
3933 EVT VT = N->getValueType(0);
3934 if (VT != MVT::i32)
3935 break;
3936 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
3937 ? ARM::t2MOVTi16
3938 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
3939 if (!Opc)
3940 break;
3941 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
3942 N1C = dyn_cast<ConstantSDNode>(N1);
3943 if (!N1C)
3944 break;
3945 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
3946 SDValue N2 = N0.getOperand(1);
3947 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
3948 if (!N2C)
3949 break;
3950 unsigned N1CVal = N1C->getZExtValue();
3951 unsigned N2CVal = N2C->getZExtValue();
3952 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
3953 (N1CVal & 0xffffU) == 0xffffU &&
3954 (N2CVal & 0xffffU) == 0x0U) {
3955 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
3956 dl, MVT::i32);
3957 SDValue Ops[] = { N0.getOperand(0), Imm16,
3958 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) };
3959 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
3960 return;
3961 }
3962 }
3963
3964 break;
3965 }
3966 case ARMISD::UMAAL: {
3967 unsigned Opc = Subtarget->isThumb() ? ARM::t2UMAAL : ARM::UMAAL;
3968 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
3969 N->getOperand(2), N->getOperand(3),
3970 getAL(CurDAG, dl),
3971 CurDAG->getRegister(0, MVT::i32) };
3972 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, MVT::i32, Ops));
3973 return;
3974 }
3975 case ARMISD::UMLAL:{
3976 if (Subtarget->isThumb()) {
3977 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
3978 N->getOperand(3), getAL(CurDAG, dl),
3979 CurDAG->getRegister(0, MVT::i32)};
3980 ReplaceNode(
3981 N, CurDAG->getMachineNode(ARM::t2UMLAL, dl, MVT::i32, MVT::i32, Ops));
3982 return;
3983 }else{
3984 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
3985 N->getOperand(3), getAL(CurDAG, dl),
3986 CurDAG->getRegister(0, MVT::i32),
3987 CurDAG->getRegister(0, MVT::i32) };
3988 ReplaceNode(N, CurDAG->getMachineNode(
3989 Subtarget->hasV6Ops() ? ARM::UMLAL : ARM::UMLALv5, dl,
3990 MVT::i32, MVT::i32, Ops));
3991 return;
3992 }
3993 }
3994 case ARMISD::SMLAL:{
3995 if (Subtarget->isThumb()) {
3996 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
3997 N->getOperand(3), getAL(CurDAG, dl),
3998 CurDAG->getRegister(0, MVT::i32)};
3999 ReplaceNode(
4000 N, CurDAG->getMachineNode(ARM::t2SMLAL, dl, MVT::i32, MVT::i32, Ops));
4001 return;
4002 }else{
4003 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
4004 N->getOperand(3), getAL(CurDAG, dl),
4005 CurDAG->getRegister(0, MVT::i32),
4006 CurDAG->getRegister(0, MVT::i32) };
4007 ReplaceNode(N, CurDAG->getMachineNode(
4008 Subtarget->hasV6Ops() ? ARM::SMLAL : ARM::SMLALv5, dl,
4009 MVT::i32, MVT::i32, Ops));
4010 return;
4011 }
4012 }
4013 case ARMISD::SUBE: {
4014 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP())
4015 break;
4016 // Look for a pattern to match SMMLS
4017 // (sube a, (smul_loHi a, b), (subc 0, (smul_LOhi(a, b))))
4018 if (N->getOperand(1).getOpcode() != ISD::SMUL_LOHI ||
4019 N->getOperand(2).getOpcode() != ARMISD::SUBC ||
4020 !SDValue(N, 1).use_empty())
4021 break;
4022
4023 if (Subtarget->isThumb())
4024 assert(Subtarget->hasThumb2() &&
4025 "This pattern should not be generated for Thumb");
4026
4027 SDValue SmulLoHi = N->getOperand(1);
4028 SDValue Subc = N->getOperand(2);
4029 SDValue Zero = Subc.getOperand(0);
4030
4031 if (!isNullConstant(Zero) || Subc.getOperand(1) != SmulLoHi.getValue(0) ||
4032 N->getOperand(1) != SmulLoHi.getValue(1) ||
4033 N->getOperand(2) != Subc.getValue(1))
4034 break;
4035
4036 unsigned Opc = Subtarget->isThumb2() ? ARM::t2SMMLS : ARM::SMMLS;
4037 SDValue Ops[] = { SmulLoHi.getOperand(0), SmulLoHi.getOperand(1),
4038 N->getOperand(0), getAL(CurDAG, dl),
4039 CurDAG->getRegister(0, MVT::i32) };
4040 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops));
4041 return;
4042 }
4043 case ISD::LOAD: {
4044 if (Subtarget->hasMVEIntegerOps() && tryMVEIndexedLoad(N))
4045 return;
4046 if (Subtarget->isThumb() && Subtarget->hasThumb2()) {
4047 if (tryT2IndexedLoad(N))
4048 return;
4049 } else if (Subtarget->isThumb()) {
4050 if (tryT1IndexedLoad(N))
4051 return;
4052 } else if (tryARMIndexedLoad(N))
4053 return;
4054 // Other cases are autogenerated.
4055 break;
4056 }
4057 case ISD::MLOAD:
4058 if (Subtarget->hasMVEIntegerOps() && tryMVEIndexedLoad(N))
4059 return;
4060 // Other cases are autogenerated.
4061 break;
4062 case ARMISD::LDRD: {
4063 if (Subtarget->isThumb2())
4064 break; // TableGen handles isel in this case.
4065 SDValue Base, RegOffset, ImmOffset;
4066 const SDValue &Chain = N->getOperand(0);
4067 const SDValue &Addr = N->getOperand(1);
4068 SelectAddrMode3(Addr, Base, RegOffset, ImmOffset);
4069 if (RegOffset != CurDAG->getRegister(0, MVT::i32)) {
4070 // The register-offset variant of LDRD mandates that the register
4071 // allocated to RegOffset is not reused in any of the remaining operands.
4072 // This restriction is currently not enforced. Therefore emitting this
4073 // variant is explicitly avoided.
4074 Base = Addr;
4075 RegOffset = CurDAG->getRegister(0, MVT::i32);
4076 }
4077 SDValue Ops[] = {Base, RegOffset, ImmOffset, Chain};
4078 SDNode *New = CurDAG->getMachineNode(ARM::LOADDUAL, dl,
4079 {MVT::Untyped, MVT::Other}, Ops);
4080 SDValue Lo = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32,
4081 SDValue(New, 0));
4082 SDValue Hi = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32,
4083 SDValue(New, 0));
4084 transferMemOperands(N, New);
4085 ReplaceUses(SDValue(N, 0), Lo);
4086 ReplaceUses(SDValue(N, 1), Hi);
4087 ReplaceUses(SDValue(N, 2), SDValue(New, 1));
4088 CurDAG->RemoveDeadNode(N);
4089 return;
4090 }
4091 case ARMISD::STRD: {
4092 if (Subtarget->isThumb2())
4093 break; // TableGen handles isel in this case.
4094 SDValue Base, RegOffset, ImmOffset;
4095 const SDValue &Chain = N->getOperand(0);
4096 const SDValue &Addr = N->getOperand(3);
4097 SelectAddrMode3(Addr, Base, RegOffset, ImmOffset);
4098 if (RegOffset != CurDAG->getRegister(0, MVT::i32)) {
4099 // The register-offset variant of STRD mandates that the register
4100 // allocated to RegOffset is not reused in any of the remaining operands.
4101 // This restriction is currently not enforced. Therefore emitting this
4102 // variant is explicitly avoided.
4103 Base = Addr;
4104 RegOffset = CurDAG->getRegister(0, MVT::i32);
4105 }
4106 SDNode *RegPair =
4107 createGPRPairNode(MVT::Untyped, N->getOperand(1), N->getOperand(2));
4108 SDValue Ops[] = {SDValue(RegPair, 0), Base, RegOffset, ImmOffset, Chain};
4109 SDNode *New = CurDAG->getMachineNode(ARM::STOREDUAL, dl, MVT::Other, Ops);
4110 transferMemOperands(N, New);
4111 ReplaceUses(SDValue(N, 0), SDValue(New, 0));
4112 CurDAG->RemoveDeadNode(N);
4113 return;
4114 }
4115 case ARMISD::BRCOND: {
4116 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
4117 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
4118 // Pattern complexity = 6 cost = 1 size = 0
4119
4120 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
4121 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
4122 // Pattern complexity = 6 cost = 1 size = 0
4123
4124 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
4125 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
4126 // Pattern complexity = 6 cost = 1 size = 0
4127
4128 unsigned Opc = Subtarget->isThumb() ?
4129 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
4130 SDValue Chain = N->getOperand(0);
4131 SDValue N1 = N->getOperand(1);
4132 SDValue N2 = N->getOperand(2);
4133 SDValue Flags = N->getOperand(3);
4136
4137 unsigned CC = (unsigned)N2->getAsZExtVal();
4138
4139 if (Flags.getOpcode() == ARMISD::CMPZ) {
4140 if (Flags.getOperand(0).getOpcode() == ISD::INTRINSIC_W_CHAIN) {
4141 SDValue Int = Flags.getOperand(0);
4142 uint64_t ID = Int->getConstantOperandVal(1);
4143
4144 // Handle low-overhead loops.
4145 if (ID == Intrinsic::loop_decrement_reg) {
4146 SDValue Elements = Int.getOperand(2);
4147 SDValue Size = CurDAG->getTargetConstant(Int.getConstantOperandVal(3),
4148 dl, MVT::i32);
4149
4150 SDValue Args[] = { Elements, Size, Int.getOperand(0) };
4151 SDNode *LoopDec =
4152 CurDAG->getMachineNode(ARM::t2LoopDec, dl,
4153 CurDAG->getVTList(MVT::i32, MVT::Other),
4154 Args);
4155 ReplaceUses(Int.getNode(), LoopDec);
4156
4157 SDValue EndArgs[] = { SDValue(LoopDec, 0), N1, Chain };
4158 SDNode *LoopEnd =
4159 CurDAG->getMachineNode(ARM::t2LoopEnd, dl, MVT::Other, EndArgs);
4160
4161 ReplaceUses(N, LoopEnd);
4162 CurDAG->RemoveDeadNode(N);
4163 CurDAG->RemoveDeadNode(Flags.getNode());
4164 CurDAG->RemoveDeadNode(Int.getNode());
4165 return;
4166 }
4167 }
4168
4169 bool SwitchEQNEToPLMI;
4170 SelectCMPZ(Flags.getNode(), SwitchEQNEToPLMI);
4171 Flags = N->getOperand(3);
4172
4173 if (SwitchEQNEToPLMI) {
4174 switch ((ARMCC::CondCodes)CC) {
4175 default: llvm_unreachable("CMPZ must be either NE or EQ!");
4176 case ARMCC::NE:
4177 CC = (unsigned)ARMCC::MI;
4178 break;
4179 case ARMCC::EQ:
4180 CC = (unsigned)ARMCC::PL;
4181 break;
4182 }
4183 }
4184 }
4185
4186 SDValue Tmp2 = CurDAG->getTargetConstant(CC, dl, MVT::i32);
4187 Chain = CurDAG->getCopyToReg(Chain, dl, ARM::CPSR, Flags, SDValue());
4188 SDValue Ops[] = {N1, Tmp2, CurDAG->getRegister(ARM::CPSR, MVT::i32), Chain,
4189 Chain.getValue(1)};
4190 CurDAG->SelectNodeTo(N, Opc, MVT::Other, Ops);
4191 return;
4192 }
4193
4194 case ARMISD::CMPZ: {
4195 // select (CMPZ X, #-C) -> (CMPZ (ADDS X, #C), #0)
4196 // This allows us to avoid materializing the expensive negative constant.
4197 // The CMPZ #0 is useless and will be peepholed away but we need to keep
4198 // it for its flags output.
4199 SDValue X = N->getOperand(0);
4200 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1).getNode());
4201 if (C && C->getSExtValue() < 0 && Subtarget->isThumb()) {
4202 int64_t Addend = -C->getSExtValue();
4203
4204 SDNode *Add = nullptr;
4205 // ADDS can be better than CMN if the immediate fits in a
4206 // 16-bit ADDS, which means either [0,256) for tADDi8 or [0,8) for tADDi3.
4207 // Outside that range we can just use a CMN which is 32-bit but has a
4208 // 12-bit immediate range.
4209 if (Addend < 1<<8) {
4210 if (Subtarget->isThumb2()) {
4211 SDValue Ops[] = { X, CurDAG->getTargetConstant(Addend, dl, MVT::i32),
4212 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
4213 CurDAG->getRegister(0, MVT::i32) };
4214 Add = CurDAG->getMachineNode(ARM::t2ADDri, dl, MVT::i32, Ops);
4215 } else {
4216 unsigned Opc = (Addend < 1<<3) ? ARM::tADDi3 : ARM::tADDi8;
4217 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), X,
4218 CurDAG->getTargetConstant(Addend, dl, MVT::i32),
4219 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
4220 Add = CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
4221 }
4222 }
4223 if (Add) {
4224 SDValue Ops2[] = {SDValue(Add, 0), CurDAG->getConstant(0, dl, MVT::i32)};
4225 CurDAG->MorphNodeTo(N, ARMISD::CMPZ, N->getVTList(), Ops2);
4226 }
4227 }
4228 // Other cases are autogenerated.
4229 break;
4230 }
4231
4232 case ARMISD::CMOV: {
4233 SDValue Flags = N->getOperand(3);
4234
4235 if (Flags.getOpcode() == ARMISD::CMPZ) {
4236 bool SwitchEQNEToPLMI;
4237 SelectCMPZ(Flags.getNode(), SwitchEQNEToPLMI);
4238
4239 if (SwitchEQNEToPLMI) {
4240 SDValue ARMcc = N->getOperand(2);
4242
4243 switch (CC) {
4244 default: llvm_unreachable("CMPZ must be either NE or EQ!");
4245 case ARMCC::NE:
4246 CC = ARMCC::MI;
4247 break;
4248 case ARMCC::EQ:
4249 CC = ARMCC::PL;
4250 break;
4251 }
4252 SDValue NewARMcc = CurDAG->getConstant((unsigned)CC, dl, MVT::i32);
4253 SDValue Ops[] = {N->getOperand(0), N->getOperand(1), NewARMcc,
4254 N->getOperand(3)};
4255 CurDAG->MorphNodeTo(N, ARMISD::CMOV, N->getVTList(), Ops);
4256 }
4257 }
4258 // Other cases are autogenerated.
4259 break;
4260 }
4261 case ARMISD::VZIP: {
4262 EVT VT = N->getValueType(0);
4263 // vzip.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
4264 unsigned Opc64[] = {ARM::VZIPd8, ARM::VZIPd16, ARM::VTRNd32};
4265 unsigned Opc128[] = {ARM::VZIPq8, ARM::VZIPq16, ARM::VZIPq32};
4266 unsigned Opc = getVectorShuffleOpcode(VT, Opc64, Opc128);
4267 SDValue Pred = getAL(CurDAG, dl);
4268 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
4269 SDValue Ops[] = {N->getOperand(0), N->getOperand(1), Pred, PredReg};
4270 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
4271 return;
4272 }
4273 case ARMISD::VUZP: {
4274 EVT VT = N->getValueType(0);
4275 // vuzp.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
4276 unsigned Opc64[] = {ARM::VUZPd8, ARM::VUZPd16, ARM::VTRNd32};
4277 unsigned Opc128[] = {ARM::VUZPq8, ARM::VUZPq16, ARM::VUZPq32};
4278 unsigned Opc = getVectorShuffleOpcode(VT, Opc64, Opc128);
4279 SDValue Pred = getAL(CurDAG, dl);
4280 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
4281 SDValue Ops[] = {N->getOperand(0), N->getOperand(1), Pred, PredReg};
4282 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
4283 return;
4284 }
4285 case ARMISD::VTRN: {
4286 EVT VT = N->getValueType(0);
4287 unsigned Opc64[] = {ARM::VTRNd8, ARM::VTRNd16, ARM::VTRNd32};
4288 unsigned Opc128[] = {ARM::VTRNq8, ARM::VTRNq16, ARM::VTRNq32};
4289 unsigned Opc = getVectorShuffleOpcode(VT, Opc64, Opc128);
4290 SDValue Pred = getAL(CurDAG, dl);
4291 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
4292 SDValue Ops[] = {N->getOperand(0), N->getOperand(1), Pred, PredReg};
4293 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
4294 return;
4295 }
4296 case ARMISD::BUILD_VECTOR: {
4297 EVT VecVT = N->getValueType(0);
4298 EVT EltVT = VecVT.getVectorElementType();
4299 unsigned NumElts = VecVT.getVectorNumElements();
4300 if (EltVT == MVT::f64) {
4301 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
4302 ReplaceNode(
4303 N, createDRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
4304 return;
4305 }
4306 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
4307 if (NumElts == 2) {
4308 ReplaceNode(
4309 N, createSRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
4310 return;
4311 }
4312 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
4313 ReplaceNode(N,
4314 createQuadSRegsNode(VecVT, N->getOperand(0), N->getOperand(1),
4315 N->getOperand(2), N->getOperand(3)));
4316 return;
4317 }
4318
4319 case ARMISD::VLD1DUP: {
4320 static const uint16_t DOpcodes[] = { ARM::VLD1DUPd8, ARM::VLD1DUPd16,
4321 ARM::VLD1DUPd32 };
4322 static const uint16_t QOpcodes[] = { ARM::VLD1DUPq8, ARM::VLD1DUPq16,
4323 ARM::VLD1DUPq32 };
4324 SelectVLDDup(N, /* IsIntrinsic= */ false, false, 1, DOpcodes, QOpcodes);
4325 return;
4326 }
4327
4328 case ARMISD::VLD2DUP: {
4329 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
4330 ARM::VLD2DUPd32 };
4331 SelectVLDDup(N, /* IsIntrinsic= */ false, false, 2, Opcodes);
4332 return;
4333 }
4334
4335 case ARMISD::VLD3DUP: {
4336 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo,
4337 ARM::VLD3DUPd16Pseudo,
4338 ARM::VLD3DUPd32Pseudo };
4339 SelectVLDDup(N, /* IsIntrinsic= */ false, false, 3, Opcodes);
4340 return;
4341 }
4342
4343 case ARMISD::VLD4DUP: {
4344 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo,
4345 ARM::VLD4DUPd16Pseudo,
4346 ARM::VLD4DUPd32Pseudo };
4347 SelectVLDDup(N, /* IsIntrinsic= */ false, false, 4, Opcodes);
4348 return;
4349 }
4350
4351 case ARMISD::VLD1DUP_UPD: {
4352 static const uint16_t DOpcodes[] = { ARM::VLD1DUPd8wb_fixed,
4353 ARM::VLD1DUPd16wb_fixed,
4354 ARM::VLD1DUPd32wb_fixed };
4355 static const uint16_t QOpcodes[] = { ARM::VLD1DUPq8wb_fixed,
4356 ARM::VLD1DUPq16wb_fixed,
4357 ARM::VLD1DUPq32wb_fixed };
4358 SelectVLDDup(N, /* IsIntrinsic= */ false, true, 1, DOpcodes, QOpcodes);
4359 return;
4360 }
4361
4362 case ARMISD::VLD2DUP_UPD: {
4363 static const uint16_t DOpcodes[] = { ARM::VLD2DUPd8wb_fixed,
4364 ARM::VLD2DUPd16wb_fixed,
4365 ARM::VLD2DUPd32wb_fixed,
4366 ARM::VLD1q64wb_fixed };
4367 static const uint16_t QOpcodes0[] = { ARM::VLD2DUPq8EvenPseudo,
4368 ARM::VLD2DUPq16EvenPseudo,
4369 ARM::VLD2DUPq32EvenPseudo };
4370 static const uint16_t QOpcodes1[] = { ARM::VLD2DUPq8OddPseudoWB_fixed,
4371 ARM::VLD2DUPq16OddPseudoWB_fixed,
4372 ARM::VLD2DUPq32OddPseudoWB_fixed };
4373 SelectVLDDup(N, /* IsIntrinsic= */ false, true, 2, DOpcodes, QOpcodes0, QOpcodes1);
4374 return;
4375 }
4376
4377 case ARMISD::VLD3DUP_UPD: {
4378 static const uint16_t DOpcodes[] = { ARM::VLD3DUPd8Pseudo_UPD,
4379 ARM::VLD3DUPd16Pseudo_UPD,
4380 ARM::VLD3DUPd32Pseudo_UPD,
4381 ARM::VLD1d64TPseudoWB_fixed };
4382 static const uint16_t QOpcodes0[] = { ARM::VLD3DUPq8EvenPseudo,
4383 ARM::VLD3DUPq16EvenPseudo,
4384 ARM::VLD3DUPq32EvenPseudo };
4385 static const uint16_t QOpcodes1[] = { ARM::VLD3DUPq8OddPseudo_UPD,
4386 ARM::VLD3DUPq16OddPseudo_UPD,
4387 ARM::VLD3DUPq32OddPseudo_UPD };
4388 SelectVLDDup(N, /* IsIntrinsic= */ false, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
4389 return;
4390 }
4391
4392 case ARMISD::VLD4DUP_UPD: {
4393 static const uint16_t DOpcodes[] = { ARM::VLD4DUPd8Pseudo_UPD,
4394 ARM::VLD4DUPd16Pseudo_UPD,
4395 ARM::VLD4DUPd32Pseudo_UPD,
4396 ARM::VLD1d64QPseudoWB_fixed };
4397 static const uint16_t QOpcodes0[] = { ARM::VLD4DUPq8EvenPseudo,
4398 ARM::VLD4DUPq16EvenPseudo,
4399 ARM::VLD4DUPq32EvenPseudo };
4400 static const uint16_t QOpcodes1[] = { ARM::VLD4DUPq8OddPseudo_UPD,
4401 ARM::VLD4DUPq16OddPseudo_UPD,
4402 ARM::VLD4DUPq32OddPseudo_UPD };
4403 SelectVLDDup(N, /* IsIntrinsic= */ false, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
4404 return;
4405 }
4406
4407 case ARMISD::VLD1_UPD: {
4408 static const uint16_t DOpcodes[] = { ARM::VLD1d8wb_fixed,
4409 ARM::VLD1d16wb_fixed,
4410 ARM::VLD1d32wb_fixed,
4411 ARM::VLD1d64wb_fixed };
4412 static const uint16_t QOpcodes[] = { ARM::VLD1q8wb_fixed,
4413 ARM::VLD1q16wb_fixed,
4414 ARM::VLD1q32wb_fixed,
4415 ARM::VLD1q64wb_fixed };
4416 SelectVLD(N, true, 1, DOpcodes, QOpcodes, nullptr);
4417 return;
4418 }
4419
4420 case ARMISD::VLD2_UPD: {
4421 if (Subtarget->hasNEON()) {
4422 static const uint16_t DOpcodes[] = {
4423 ARM::VLD2d8wb_fixed, ARM::VLD2d16wb_fixed, ARM::VLD2d32wb_fixed,
4424 ARM::VLD1q64wb_fixed};
4425 static const uint16_t QOpcodes[] = {ARM::VLD2q8PseudoWB_fixed,
4426 ARM::VLD2q16PseudoWB_fixed,
4427 ARM::VLD2q32PseudoWB_fixed};
4428 SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr);
4429 } else {
4430 static const uint16_t Opcodes8[] = {ARM::MVE_VLD20_8,
4431 ARM::MVE_VLD21_8_wb};
4432 static const uint16_t Opcodes16[] = {ARM::MVE_VLD20_16,
4433 ARM::MVE_VLD21_16_wb};
4434 static const uint16_t Opcodes32[] = {ARM::MVE_VLD20_32,
4435 ARM::MVE_VLD21_32_wb};
4436 static const uint16_t *const Opcodes[] = {Opcodes8, Opcodes16, Opcodes32};
4437 SelectMVE_VLD(N, 2, Opcodes, true);
4438 }
4439 return;
4440 }
4441
4442 case ARMISD::VLD3_UPD: {
4443 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo_UPD,
4444 ARM::VLD3d16Pseudo_UPD,
4445 ARM::VLD3d32Pseudo_UPD,
4446 ARM::VLD1d64TPseudoWB_fixed};
4447 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
4448 ARM::VLD3q16Pseudo_UPD,
4449 ARM::VLD3q32Pseudo_UPD };
4450 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
4451 ARM::VLD3q16oddPseudo_UPD,
4452 ARM::VLD3q32oddPseudo_UPD };
4453 SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
4454 return;
4455 }
4456
4457 case ARMISD::VLD4_UPD: {
4458 if (Subtarget->hasNEON()) {
4459 static const uint16_t DOpcodes[] = {
4460 ARM::VLD4d8Pseudo_UPD, ARM::VLD4d16Pseudo_UPD, ARM::VLD4d32Pseudo_UPD,
4461 ARM::VLD1d64QPseudoWB_fixed};
4462 static const uint16_t QOpcodes0[] = {ARM::VLD4q8Pseudo_UPD,
4463 ARM::VLD4q16Pseudo_UPD,
4464 ARM::VLD4q32Pseudo_UPD};
4465 static const uint16_t QOpcodes1[] = {ARM::VLD4q8oddPseudo_UPD,
4466 ARM::VLD4q16oddPseudo_UPD,
4467 ARM::VLD4q32oddPseudo_UPD};
4468 SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
4469 } else {
4470 static const uint16_t Opcodes8[] = {ARM::MVE_VLD40_8, ARM::MVE_VLD41_8,
4471 ARM::MVE_VLD42_8,
4472 ARM::MVE_VLD43_8_wb};
4473 static const uint16_t Opcodes16[] = {ARM::MVE_VLD40_16, ARM::MVE_VLD41_16,
4474 ARM::MVE_VLD42_16,
4475 ARM::MVE_VLD43_16_wb};
4476 static const uint16_t Opcodes32[] = {ARM::MVE_VLD40_32, ARM::MVE_VLD41_32,
4477 ARM::MVE_VLD42_32,
4478 ARM::MVE_VLD43_32_wb};
4479 static const uint16_t *const Opcodes[] = {Opcodes8, Opcodes16, Opcodes32};
4480 SelectMVE_VLD(N, 4, Opcodes, true);
4481 }
4482 return;
4483 }
4484
4485 case ARMISD::VLD1x2_UPD: {
4486 if (Subtarget->hasNEON()) {
4487 static const uint16_t DOpcodes[] = {
4488 ARM::VLD1q8wb_fixed, ARM::VLD1q16wb_fixed, ARM::VLD1q32wb_fixed,
4489 ARM::VLD1q64wb_fixed};
4490 static const uint16_t QOpcodes[] = {
4491 ARM::VLD1d8QPseudoWB_fixed, ARM::VLD1d16QPseudoWB_fixed,
4492 ARM::VLD1d32QPseudoWB_fixed, ARM::VLD1d64QPseudoWB_fixed};
4493 SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr);
4494 return;
4495 }
4496 break;
4497 }
4498
4499 case ARMISD::VLD1x3_UPD: {
4500 if (Subtarget->hasNEON()) {
4501 static const uint16_t DOpcodes[] = {
4502 ARM::VLD1d8TPseudoWB_fixed, ARM::VLD1d16TPseudoWB_fixed,
4503 ARM::VLD1d32TPseudoWB_fixed, ARM::VLD1d64TPseudoWB_fixed};
4504 static const uint16_t QOpcodes0[] = {
4505 ARM::VLD1q8LowTPseudo_UPD, ARM::VLD1q16LowTPseudo_UPD,
4506 ARM::VLD1q32LowTPseudo_UPD, ARM::VLD1q64LowTPseudo_UPD};
4507 static const uint16_t QOpcodes1[] = {
4508 ARM::VLD1q8HighTPseudo_UPD, ARM::VLD1q16HighTPseudo_UPD,
4509 ARM::VLD1q32HighTPseudo_UPD, ARM::VLD1q64HighTPseudo_UPD};
4510 SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
4511 return;
4512 }
4513 break;
4514 }
4515
4516 case ARMISD::VLD1x4_UPD: {
4517 if (Subtarget->hasNEON()) {
4518 static const uint16_t DOpcodes[] = {
4519 ARM::VLD1d8QPseudoWB_fixed, ARM::VLD1d16QPseudoWB_fixed,
4520 ARM::VLD1d32QPseudoWB_fixed, ARM::VLD1d64QPseudoWB_fixed};
4521 static const uint16_t QOpcodes0[] = {
4522 ARM::VLD1q8LowQPseudo_UPD, ARM::VLD1q16LowQPseudo_UPD,
4523 ARM::VLD1q32LowQPseudo_UPD, ARM::VLD1q64LowQPseudo_UPD};
4524 static const uint16_t QOpcodes1[] = {
4525 ARM::VLD1q8HighQPseudo_UPD, ARM::VLD1q16HighQPseudo_UPD,
4526 ARM::VLD1q32HighQPseudo_UPD, ARM::VLD1q64HighQPseudo_UPD};
4527 SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
4528 return;
4529 }
4530 break;
4531 }
4532
4533 case ARMISD::VLD2LN_UPD: {
4534 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD,
4535 ARM::VLD2LNd16Pseudo_UPD,
4536 ARM::VLD2LNd32Pseudo_UPD };
4537 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD,
4538 ARM::VLD2LNq32Pseudo_UPD };
4539 SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes);
4540 return;
4541 }
4542
4543 case ARMISD::VLD3LN_UPD: {
4544 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD,
4545 ARM::VLD3LNd16Pseudo_UPD,
4546 ARM::VLD3LNd32Pseudo_UPD };
4547 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD,
4548 ARM::VLD3LNq32Pseudo_UPD };
4549 SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes);
4550 return;
4551 }
4552
4553 case ARMISD::VLD4LN_UPD: {
4554 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD,
4555 ARM::VLD4LNd16Pseudo_UPD,
4556 ARM::VLD4LNd32Pseudo_UPD };
4557 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD,
4558 ARM::VLD4LNq32Pseudo_UPD };
4559 SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes);
4560 return;
4561 }
4562
4563 case ARMISD::VST1_UPD: {
4564 static const uint16_t DOpcodes[] = { ARM::VST1d8wb_fixed,
4565 ARM::VST1d16wb_fixed,
4566 ARM::VST1d32wb_fixed,
4567 ARM::VST1d64wb_fixed };
4568 static const uint16_t QOpcodes[] = { ARM::VST1q8wb_fixed,
4569 ARM::VST1q16wb_fixed,
4570 ARM::VST1q32wb_fixed,
4571 ARM::VST1q64wb_fixed };
4572 SelectVST(N, true, 1, DOpcodes, QOpcodes, nullptr);
4573 return;
4574 }
4575
4576 case ARMISD::VST2_UPD: {
4577 if (Subtarget->hasNEON()) {
4578 static const uint16_t DOpcodes[] = {
4579 ARM::VST2d8wb_fixed, ARM::VST2d16wb_fixed, ARM::VST2d32wb_fixed,
4580 ARM::VST1q64wb_fixed};
4581 static const uint16_t QOpcodes[] = {ARM::VST2q8PseudoWB_fixed,
4582 ARM::VST2q16PseudoWB_fixed,
4583 ARM::VST2q32PseudoWB_fixed};
4584 SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr);
4585 return;
4586 }
4587 break;
4588 }
4589
4590 case ARMISD::VST3_UPD: {
4591 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo_UPD,
4592 ARM::VST3d16Pseudo_UPD,
4593 ARM::VST3d32Pseudo_UPD,
4594 ARM::VST1d64TPseudoWB_fixed};
4595 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
4596 ARM::VST3q16Pseudo_UPD,
4597 ARM::VST3q32Pseudo_UPD };
4598 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
4599 ARM::VST3q16oddPseudo_UPD,
4600 ARM::VST3q32oddPseudo_UPD };
4601 SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
4602 return;
4603 }
4604
4605 case ARMISD::VST4_UPD: {
4606 if (Subtarget->hasNEON()) {
4607 static const uint16_t DOpcodes[] = {
4608 ARM::VST4d8Pseudo_UPD, ARM::VST4d16Pseudo_UPD, ARM::VST4d32Pseudo_UPD,
4609 ARM::VST1d64QPseudoWB_fixed};
4610 static const uint16_t QOpcodes0[] = {ARM::VST4q8Pseudo_UPD,
4611 ARM::VST4q16Pseudo_UPD,
4612 ARM::VST4q32Pseudo_UPD};
4613 static const uint16_t QOpcodes1[] = {ARM::VST4q8oddPseudo_UPD,
4614 ARM::VST4q16oddPseudo_UPD,
4615 ARM::VST4q32oddPseudo_UPD};
4616 SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
4617 return;
4618 }
4619 break;
4620 }
4621
4622 case ARMISD::VST1x2_UPD: {
4623 if (Subtarget->hasNEON()) {
4624 static const uint16_t DOpcodes[] = { ARM::VST1q8wb_fixed,
4625 ARM::VST1q16wb_fixed,
4626 ARM::VST1q32wb_fixed,
4627 ARM::VST1q64wb_fixed};
4628 static const uint16_t QOpcodes[] = { ARM::VST1d8QPseudoWB_fixed,
4629 ARM::VST1d16QPseudoWB_fixed,
4630 ARM::VST1d32QPseudoWB_fixed,
4631 ARM::VST1d64QPseudoWB_fixed };
4632 SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr);
4633 return;
4634 }
4635 break;
4636 }
4637
4638 case ARMISD::VST1x3_UPD: {
4639 if (Subtarget->hasNEON()) {
4640 static const uint16_t DOpcodes[] = { ARM::VST1d8TPseudoWB_fixed,
4641 ARM::VST1d16TPseudoWB_fixed,
4642 ARM::VST1d32TPseudoWB_fixed,
4643 ARM::VST1d64TPseudoWB_fixed };
4644 static const uint16_t QOpcodes0[] = { ARM::VST1q8LowTPseudo_UPD,
4645 ARM::VST1q16LowTPseudo_UPD,
4646 ARM::VST1q32LowTPseudo_UPD,
4647 ARM::VST1q64LowTPseudo_UPD };
4648 static const uint16_t QOpcodes1[] = { ARM::VST1q8HighTPseudo_UPD,
4649 ARM::VST1q16HighTPseudo_UPD,
4650 ARM::VST1q32HighTPseudo_UPD,
4651 ARM::VST1q64HighTPseudo_UPD };
4652 SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
4653 return;
4654 }
4655 break;
4656 }
4657
4658 case ARMISD::VST1x4_UPD: {
4659 if (Subtarget->hasNEON()) {
4660 static const uint16_t DOpcodes[] = { ARM::VST1d8QPseudoWB_fixed,
4661 ARM::VST1d16QPseudoWB_fixed,
4662 ARM::VST1d32QPseudoWB_fixed,
4663 ARM::VST1d64QPseudoWB_fixed };
4664 static const uint16_t QOpcodes0[] = { ARM::VST1q8LowQPseudo_UPD,
4665 ARM::VST1q16LowQPseudo_UPD,
4666 ARM::VST1q32LowQPseudo_UPD,
4667 ARM::VST1q64LowQPseudo_UPD };
4668 static const uint16_t QOpcodes1[] = { ARM::VST1q8HighQPseudo_UPD,
4669 ARM::VST1q16HighQPseudo_UPD,
4670 ARM::VST1q32HighQPseudo_UPD,
4671 ARM::VST1q64HighQPseudo_UPD };
4672 SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
4673 return;
4674 }
4675 break;
4676 }
4677 case ARMISD::VST2LN_UPD: {
4678 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD,
4679 ARM::VST2LNd16Pseudo_UPD,
4680 ARM::VST2LNd32Pseudo_UPD };
4681 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD,
4682 ARM::VST2LNq32Pseudo_UPD };
4683 SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes);
4684 return;
4685 }
4686
4687 case ARMISD::VST3LN_UPD: {
4688 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD,
4689 ARM::VST3LNd16Pseudo_UPD,
4690 ARM::VST3LNd32Pseudo_UPD };
4691 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD,
4692 ARM::VST3LNq32Pseudo_UPD };
4693 SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes);
4694 return;
4695 }
4696
4697 case ARMISD::VST4LN_UPD: {
4698 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD,
4699 ARM::VST4LNd16Pseudo_UPD,
4700 ARM::VST4LNd32Pseudo_UPD };
4701 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD,
4702 ARM::VST4LNq32Pseudo_UPD };
4703 SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes);
4704 return;
4705 }
4706
4709 unsigned IntNo = N->getConstantOperandVal(1);
4710 switch (IntNo) {
4711 default:
4712 break;
4713
4714 case Intrinsic::arm_mrrc:
4715 case Intrinsic::arm_mrrc2: {
4716 SDLoc dl(N);
4717 SDValue Chain = N->getOperand(0);
4718 unsigned Opc;
4719
4720 if (Subtarget->isThumb())
4721 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::t2MRRC : ARM::t2MRRC2);
4722 else
4723 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::MRRC : ARM::MRRC2);
4724
4726 Ops.push_back(getI32Imm(N->getConstantOperandVal(2), dl)); /* coproc */
4727 Ops.push_back(getI32Imm(N->getConstantOperandVal(3), dl)); /* opc */
4728 Ops.push_back(getI32Imm(N->getConstantOperandVal(4), dl)); /* CRm */
4729
4730 // The mrrc2 instruction in ARM doesn't allow predicates, the top 4 bits of the encoded
4731 // instruction will always be '1111' but it is possible in assembly language to specify
4732 // AL as a predicate to mrrc2 but it doesn't make any difference to the encoded instruction.
4733 if (Opc != ARM::MRRC2) {
4734 Ops.push_back(getAL(CurDAG, dl));
4735 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
4736 }
4737
4738 Ops.push_back(Chain);
4739
4740 // Writes to two registers.
4741 const EVT RetType[] = {MVT::i32, MVT::i32, MVT::Other};
4742
4743 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, RetType, Ops));
4744 return;
4745 }
4746 case Intrinsic::arm_ldaexd:
4747 case Intrinsic::arm_ldrexd: {
4748 SDLoc dl(N);
4749 SDValue Chain = N->getOperand(0);
4750 SDValue MemAddr = N->getOperand(2);
4751 bool isThumb = Subtarget->isThumb() && Subtarget->hasV8MBaselineOps();
4752
4753 bool IsAcquire = IntNo == Intrinsic::arm_ldaexd;
4754 unsigned NewOpc = isThumb ? (IsAcquire ? ARM::t2LDAEXD : ARM::t2LDREXD)
4755 : (IsAcquire ? ARM::LDAEXD : ARM::LDREXD);
4756
4757 // arm_ldrexd returns a i64 value in {i32, i32}
4758 std::vector<EVT> ResTys;
4759 if (isThumb) {
4760 ResTys.push_back(MVT::i32);
4761 ResTys.push_back(MVT::i32);
4762 } else
4763 ResTys.push_back(MVT::Untyped);
4764 ResTys.push_back(MVT::Other);
4765
4766 // Place arguments in the right order.
4767 SDValue Ops[] = {MemAddr, getAL(CurDAG, dl),
4768 CurDAG->getRegister(0, MVT::i32), Chain};
4769 SDNode *Ld = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
4770 // Transfer memoperands.
4771 MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
4772 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Ld), {MemOp});
4773
4774 // Remap uses.
4775 SDValue OutChain = isThumb ? SDValue(Ld, 2) : SDValue(Ld, 1);
4776 if (!SDValue(N, 0).use_empty()) {
4778 if (isThumb)
4779 Result = SDValue(Ld, 0);
4780 else {
4781 SDValue SubRegIdx =
4782 CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
4783 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
4784 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
4785 Result = SDValue(ResNode,0);
4786 }
4787 ReplaceUses(SDValue(N, 0), Result);
4788 }
4789 if (!SDValue(N, 1).use_empty()) {
4791 if (isThumb)
4792 Result = SDValue(Ld, 1);
4793 else {
4794 SDValue SubRegIdx =
4795 CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
4796 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
4797 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
4798 Result = SDValue(ResNode,0);
4799 }
4800 ReplaceUses(SDValue(N, 1), Result);
4801 }
4802 ReplaceUses(SDValue(N, 2), OutChain);
4803 CurDAG->RemoveDeadNode(N);
4804 return;
4805 }
4806 case Intrinsic::arm_stlexd:
4807 case Intrinsic::arm_strexd: {
4808 SDLoc dl(N);
4809 SDValue Chain = N->getOperand(0);
4810 SDValue Val0 = N->getOperand(2);
4811 SDValue Val1 = N->getOperand(3);
4812 SDValue MemAddr = N->getOperand(4);
4813
4814 // Store exclusive double return a i32 value which is the return status
4815 // of the issued store.
4816 const EVT ResTys[] = {MVT::i32, MVT::Other};
4817
4818 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
4819 // Place arguments in the right order.
4821 if (isThumb) {
4822 Ops.push_back(Val0);
4823 Ops.push_back(Val1);
4824 } else
4825 // arm_strexd uses GPRPair.
4826 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, Val0, Val1), 0));
4827 Ops.push_back(MemAddr);
4828 Ops.push_back(getAL(CurDAG, dl));
4829 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
4830 Ops.push_back(Chain);
4831
4832 bool IsRelease = IntNo == Intrinsic::arm_stlexd;
4833 unsigned NewOpc = isThumb ? (IsRelease ? ARM::t2STLEXD : ARM::t2STREXD)
4834 : (IsRelease ? ARM::STLEXD : ARM::STREXD);
4835
4836 SDNode *St = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
4837 // Transfer memoperands.
4838 MachineMemOperand *MemOp = cast<MemIntrinsicSDNode>(N)->getMemOperand();
4839 CurDAG->setNodeMemRefs(cast<MachineSDNode>(St), {MemOp});
4840
4841 ReplaceNode(N, St);
4842 return;
4843 }
4844
4845 case Intrinsic::arm_neon_vld1: {
4846 static const uint16_t DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
4847 ARM::VLD1d32, ARM::VLD1d64 };
4848 static const uint16_t QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
4849 ARM::VLD1q32, ARM::VLD1q64};
4850 SelectVLD(N, false, 1, DOpcodes, QOpcodes, nullptr);
4851 return;
4852 }
4853
4854 case Intrinsic::arm_neon_vld1x2: {
4855 static const uint16_t DOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
4856 ARM::VLD1q32, ARM::VLD1q64 };
4857 static const uint16_t QOpcodes[] = { ARM::VLD1d8QPseudo,
4858 ARM::VLD1d16QPseudo,
4859 ARM::VLD1d32QPseudo,
4860 ARM::VLD1d64QPseudo };
4861 SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
4862 return;
4863 }
4864
4865 case Intrinsic::arm_neon_vld1x3: {
4866 static const uint16_t DOpcodes[] = { ARM::VLD1d8TPseudo,
4867 ARM::VLD1d16TPseudo,
4868 ARM::VLD1d32TPseudo,
4869 ARM::VLD1d64TPseudo };
4870 static const uint16_t QOpcodes0[] = { ARM::VLD1q8LowTPseudo_UPD,
4871 ARM::VLD1q16LowTPseudo_UPD,
4872 ARM::VLD1q32LowTPseudo_UPD,
4873 ARM::VLD1q64LowTPseudo_UPD };
4874 static const uint16_t QOpcodes1[] = { ARM::VLD1q8HighTPseudo,
4875 ARM::VLD1q16HighTPseudo,
4876 ARM::VLD1q32HighTPseudo,
4877 ARM::VLD1q64HighTPseudo };
4878 SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
4879 return;
4880 }
4881
4882 case Intrinsic::arm_neon_vld1x4: {
4883 static const uint16_t DOpcodes[] = { ARM::VLD1d8QPseudo,
4884 ARM::VLD1d16QPseudo,
4885 ARM::VLD1d32QPseudo,
4886 ARM::VLD1d64QPseudo };
4887 static const uint16_t QOpcodes0[] = { ARM::VLD1q8LowQPseudo_UPD,
4888 ARM::VLD1q16LowQPseudo_UPD,
4889 ARM::VLD1q32LowQPseudo_UPD,
4890 ARM::VLD1q64LowQPseudo_UPD };
4891 static const uint16_t QOpcodes1[] = { ARM::VLD1q8HighQPseudo,
4892 ARM::VLD1q16HighQPseudo,
4893 ARM::VLD1q32HighQPseudo,
4894 ARM::VLD1q64HighQPseudo };
4895 SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
4896 return;
4897 }
4898
4899 case Intrinsic::arm_neon_vld2: {
4900 static const uint16_t DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
4901 ARM::VLD2d32, ARM::VLD1q64 };
4902 static const uint16_t QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
4903 ARM::VLD2q32Pseudo };
4904 SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
4905 return;
4906 }
4907
4908 case Intrinsic::arm_neon_vld3: {
4909 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo,
4910 ARM::VLD3d16Pseudo,
4911 ARM::VLD3d32Pseudo,
4912 ARM::VLD1d64TPseudo };
4913 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
4914 ARM::VLD3q16Pseudo_UPD,
4915 ARM::VLD3q32Pseudo_UPD };
4916 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo,
4917 ARM::VLD3q16oddPseudo,
4918 ARM::VLD3q32oddPseudo };
4919 SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
4920 return;
4921 }
4922
4923 case Intrinsic::arm_neon_vld4: {
4924 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo,
4925 ARM::VLD4d16Pseudo,
4926 ARM::VLD4d32Pseudo,
4927 ARM::VLD1d64QPseudo };
4928 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
4929 ARM::VLD4q16Pseudo_UPD,
4930 ARM::VLD4q32Pseudo_UPD };
4931 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo,
4932 ARM::VLD4q16oddPseudo,
4933 ARM::VLD4q32oddPseudo };
4934 SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
4935 return;
4936 }
4937
4938 case Intrinsic::arm_neon_vld2dup: {
4939 static const uint16_t DOpcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
4940 ARM::VLD2DUPd32, ARM::VLD1q64 };
4941 static const uint16_t QOpcodes0[] = { ARM::VLD2DUPq8EvenPseudo,
4942 ARM::VLD2DUPq16EvenPseudo,
4943 ARM::VLD2DUPq32EvenPseudo };
4944 static const uint16_t QOpcodes1[] = { ARM::VLD2DUPq8OddPseudo,
4945 ARM::VLD2DUPq16OddPseudo,
4946 ARM::VLD2DUPq32OddPseudo };
4947 SelectVLDDup(N, /* IsIntrinsic= */ true, false, 2,
4948 DOpcodes, QOpcodes0, QOpcodes1);
4949 return;
4950 }
4951
4952 case Intrinsic::arm_neon_vld3dup: {
4953 static const uint16_t DOpcodes[] = { ARM::VLD3DUPd8Pseudo,
4954 ARM::VLD3DUPd16Pseudo,
4955 ARM::VLD3DUPd32Pseudo,
4956 ARM::VLD1d64TPseudo };
4957 static const uint16_t QOpcodes0[] = { ARM::VLD3DUPq8EvenPseudo,
4958 ARM::VLD3DUPq16EvenPseudo,
4959 ARM::VLD3DUPq32EvenPseudo };
4960 static const uint16_t QOpcodes1[] = { ARM::VLD3DUPq8OddPseudo,
4961 ARM::VLD3DUPq16OddPseudo,
4962 ARM::VLD3DUPq32OddPseudo };
4963 SelectVLDDup(N, /* IsIntrinsic= */ true, false, 3,
4964 DOpcodes, QOpcodes0, QOpcodes1);
4965 return;
4966 }
4967
4968 case Intrinsic::arm_neon_vld4dup: {
4969 static const uint16_t DOpcodes[] = { ARM::VLD4DUPd8Pseudo,
4970 ARM::VLD4DUPd16Pseudo,
4971 ARM::VLD4DUPd32Pseudo,
4972 ARM::VLD1d64QPseudo };
4973 static const uint16_t QOpcodes0[] = { ARM::VLD4DUPq8EvenPseudo,
4974 ARM::VLD4DUPq16EvenPseudo,
4975 ARM::VLD4DUPq32EvenPseudo };
4976 static const uint16_t QOpcodes1[] = { ARM::VLD4DUPq8OddPseudo,
4977 ARM::VLD4DUPq16OddPseudo,
4978 ARM::VLD4DUPq32OddPseudo };
4979 SelectVLDDup(N, /* IsIntrinsic= */ true, false, 4,
4980 DOpcodes, QOpcodes0, QOpcodes1);
4981 return;
4982 }
4983
4984 case Intrinsic::arm_neon_vld2lane: {
4985 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo,
4986 ARM::VLD2LNd16Pseudo,
4987 ARM::VLD2LNd32Pseudo };
4988 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo,
4989 ARM::VLD2LNq32Pseudo };
4990 SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes);
4991 return;
4992 }
4993
4994 case Intrinsic::arm_neon_vld3lane: {
4995 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo,
4996 ARM::VLD3LNd16Pseudo,
4997 ARM::VLD3LNd32Pseudo };
4998 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo,
4999 ARM::VLD3LNq32Pseudo };
5000 SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes);
5001 return;
5002 }
5003
5004 case Intrinsic::arm_neon_vld4lane: {
5005 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo,
5006 ARM::VLD4LNd16Pseudo,
5007 ARM::VLD4LNd32Pseudo };
5008 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo,
5009 ARM::VLD4LNq32Pseudo };
5010 SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes);
5011 return;
5012 }
5013
5014 case Intrinsic::arm_neon_vst1: {
5015 static const uint16_t DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
5016 ARM::VST1d32, ARM::VST1d64 };
5017 static const uint16_t QOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
5018 ARM::VST1q32, ARM::VST1q64 };
5019 SelectVST(N, false, 1, DOpcodes, QOpcodes, nullptr);
5020 return;
5021 }
5022
5023 case Intrinsic::arm_neon_vst1x2: {
5024 static const uint16_t DOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
5025 ARM::VST1q32, ARM::VST1q64 };
5026 static const uint16_t QOpcodes[] = { ARM::VST1d8QPseudo,
5027 ARM::VST1d16QPseudo,
5028 ARM::VST1d32QPseudo,
5029 ARM::VST1d64QPseudo };
5030 SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
5031 return;
5032 }
5033
5034 case Intrinsic::arm_neon_vst1x3: {
5035 static const uint16_t DOpcodes[] = { ARM::VST1d8TPseudo,
5036 ARM::VST1d16TPseudo,
5037 ARM::VST1d32TPseudo,
5038 ARM::VST1d64TPseudo };
5039 static const uint16_t QOpcodes0[] = { ARM::VST1q8LowTPseudo_UPD,
5040 ARM::VST1q16LowTPseudo_UPD,
5041 ARM::VST1q32LowTPseudo_UPD,
5042 ARM::VST1q64LowTPseudo_UPD };
5043 static const uint16_t QOpcodes1[] = { ARM::VST1q8HighTPseudo,
5044 ARM::VST1q16HighTPseudo,
5045 ARM::VST1q32HighTPseudo,
5046 ARM::VST1q64HighTPseudo };
5047 SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
5048 return;
5049 }
5050
5051 case Intrinsic::arm_neon_vst1x4: {
5052 static const uint16_t DOpcodes[] = { ARM::VST1d8QPseudo,
5053 ARM::VST1d16QPseudo,
5054 ARM::VST1d32QPseudo,
5055 ARM::VST1d64QPseudo };
5056 static const uint16_t QOpcodes0[] = { ARM::VST1q8LowQPseudo_UPD,
5057 ARM::VST1q16LowQPseudo_UPD,
5058 ARM::VST1q32LowQPseudo_UPD,
5059 ARM::VST1q64LowQPseudo_UPD };
5060 static const uint16_t QOpcodes1[] = { ARM::VST1q8HighQPseudo,
5061 ARM::VST1q16HighQPseudo,
5062 ARM::VST1q32HighQPseudo,
5063 ARM::VST1q64HighQPseudo };
5064 SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
5065 return;
5066 }
5067
5068 case Intrinsic::arm_neon_vst2: {
5069 static const uint16_t DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
5070 ARM::VST2d32, ARM::VST1q64 };
5071 static const uint16_t QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
5072 ARM::VST2q32Pseudo };
5073 SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
5074 return;
5075 }
5076
5077 case Intrinsic::arm_neon_vst3: {
5078 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo,
5079 ARM::VST3d16Pseudo,
5080 ARM::VST3d32Pseudo,
5081 ARM::VST1d64TPseudo };
5082 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
5083 ARM::VST3q16Pseudo_UPD,
5084 ARM::VST3q32Pseudo_UPD };
5085 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo,
5086 ARM::VST3q16oddPseudo,
5087 ARM::VST3q32oddPseudo };
5088 SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
5089 return;
5090 }
5091
5092 case Intrinsic::arm_neon_vst4: {
5093 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo,
5094 ARM::VST4d16Pseudo,
5095 ARM::VST4d32Pseudo,
5096 ARM::VST1d64QPseudo };
5097 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
5098 ARM::VST4q16Pseudo_UPD,
5099 ARM::VST4q32Pseudo_UPD };
5100 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo,
5101 ARM::VST4q16oddPseudo,
5102 ARM::VST4q32oddPseudo };
5103 SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
5104 return;
5105 }
5106
5107 case Intrinsic::arm_neon_vst2lane: {
5108 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo,
5109 ARM::VST2LNd16Pseudo,
5110 ARM::VST2LNd32Pseudo };
5111 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo,
5112 ARM::VST2LNq32Pseudo };
5113 SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes);
5114 return;
5115 }
5116
5117 case Intrinsic::arm_neon_vst3lane: {
5118 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo,
5119 ARM::VST3LNd16Pseudo,
5120 ARM::VST3LNd32Pseudo };
5121 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo,
5122 ARM::VST3LNq32Pseudo };
5123 SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes);
5124 return;
5125 }
5126
5127 case Intrinsic::arm_neon_vst4lane: {
5128 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo,
5129 ARM::VST4LNd16Pseudo,
5130 ARM::VST4LNd32Pseudo };
5131 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo,
5132 ARM::VST4LNq32Pseudo };
5133 SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes);
5134 return;
5135 }
5136
5137 case Intrinsic::arm_mve_vldr_gather_base_wb:
5138 case Intrinsic::arm_mve_vldr_gather_base_wb_predicated: {
5139 static const uint16_t Opcodes[] = {ARM::MVE_VLDRWU32_qi_pre,
5140 ARM::MVE_VLDRDU64_qi_pre};
5141 SelectMVE_WB(N, Opcodes,
5142 IntNo == Intrinsic::arm_mve_vldr_gather_base_wb_predicated);
5143 return;
5144 }
5145
5146 case Intrinsic::arm_mve_vld2q: {
5147 static const uint16_t Opcodes8[] = {ARM::MVE_VLD20_8, ARM::MVE_VLD21_8};
5148 static const uint16_t Opcodes16[] = {ARM::MVE_VLD20_16,
5149 ARM::MVE_VLD21_16};
5150 static const uint16_t Opcodes32[] = {ARM::MVE_VLD20_32,
5151 ARM::MVE_VLD21_32};
5152 static const uint16_t *const Opcodes[] = {Opcodes8, Opcodes16, Opcodes32};
5153 SelectMVE_VLD(N, 2, Opcodes, false);
5154 return;
5155 }
5156
5157 case Intrinsic::arm_mve_vld4q: {
5158 static const uint16_t Opcodes8[] = {ARM::MVE_VLD40_8, ARM::MVE_VLD41_8,
5159 ARM::MVE_VLD42_8, ARM::MVE_VLD43_8};
5160 static const uint16_t Opcodes16[] = {ARM::MVE_VLD40_16, ARM::MVE_VLD41_16,
5161 ARM::MVE_VLD42_16,
5162 ARM::MVE_VLD43_16};
5163 static const uint16_t Opcodes32[] = {ARM::MVE_VLD40_32, ARM::MVE_VLD41_32,
5164 ARM::MVE_VLD42_32,
5165 ARM::MVE_VLD43_32};
5166 static const uint16_t *const Opcodes[] = {Opcodes8, Opcodes16, Opcodes32};
5167 SelectMVE_VLD(N, 4, Opcodes, false);
5168 return;
5169 }
5170 }
5171 break;
5172 }
5173
5175 unsigned IntNo = N->getConstantOperandVal(0);
5176 switch (IntNo) {
5177 default:
5178 break;
5179
5180 // Scalar f32 -> bf16
5181 case Intrinsic::arm_neon_vcvtbfp2bf: {
5182 SDLoc dl(N);
5183 const SDValue &Src = N->getOperand(1);
5184 llvm::EVT DestTy = N->getValueType(0);
5185 SDValue Pred = getAL(CurDAG, dl);
5186 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
5187 SDValue Ops[] = { Src, Src, Pred, Reg0 };
5188 CurDAG->SelectNodeTo(N, ARM::BF16_VCVTB, DestTy, Ops);
5189 return;
5190 }
5191
5192 // Vector v4f32 -> v4bf16
5193 case Intrinsic::arm_neon_vcvtfp2bf: {
5194 SDLoc dl(N);
5195 const SDValue &Src = N->getOperand(1);
5196 SDValue Pred = getAL(CurDAG, dl);
5197 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
5198 SDValue Ops[] = { Src, Pred, Reg0 };
5199 CurDAG->SelectNodeTo(N, ARM::BF16_VCVT, MVT::v4bf16, Ops);
5200 return;
5201 }
5202
5203 case Intrinsic::arm_mve_urshrl:
5204 SelectMVE_LongShift(N, ARM::MVE_URSHRL, true, false);
5205 return;
5206 case Intrinsic::arm_mve_uqshll:
5207 SelectMVE_LongShift(N, ARM::MVE_UQSHLL, true, false);
5208 return;
5209 case Intrinsic::arm_mve_srshrl:
5210 SelectMVE_LongShift(N, ARM::MVE_SRSHRL, true, false);
5211 return;
5212 case Intrinsic::arm_mve_sqshll:
5213 SelectMVE_LongShift(N, ARM::MVE_SQSHLL, true, false);
5214 return;
5215 case Intrinsic::arm_mve_uqrshll:
5216 SelectMVE_LongShift(N, ARM::MVE_UQRSHLL, false, true);
5217 return;
5218 case Intrinsic::arm_mve_sqrshrl:
5219 SelectMVE_LongShift(N, ARM::MVE_SQRSHRL, false, true);
5220 return;
5221
5222 case Intrinsic::arm_mve_vadc:
5223 case Intrinsic::arm_mve_vadc_predicated:
5224 SelectMVE_VADCSBC(N, ARM::MVE_VADC, ARM::MVE_VADCI, true,
5225 IntNo == Intrinsic::arm_mve_vadc_predicated);
5226 return;
5227 case Intrinsic::arm_mve_vsbc:
5228 case Intrinsic::arm_mve_vsbc_predicated:
5229 SelectMVE_VADCSBC(N, ARM::MVE_VSBC, ARM::MVE_VSBCI, false,
5230 IntNo == Intrinsic::arm_mve_vsbc_predicated);
5231 return;
5232 case Intrinsic::arm_mve_vshlc:
5233 case Intrinsic::arm_mve_vshlc_predicated:
5234 SelectMVE_VSHLC(N, IntNo == Intrinsic::arm_mve_vshlc_predicated);
5235 return;
5236
5237 case Intrinsic::arm_mve_vmlldava:
5238 case Intrinsic::arm_mve_vmlldava_predicated: {
5239 static const uint16_t OpcodesU[] = {
5240 ARM::MVE_VMLALDAVu16, ARM::MVE_VMLALDAVu32,
5241 ARM::MVE_VMLALDAVau16, ARM::MVE_VMLALDAVau32,
5242 };
5243 static const uint16_t OpcodesS[] = {
5244 ARM::MVE_VMLALDAVs16, ARM::MVE_VMLALDAVs32,
5245 ARM::MVE_VMLALDAVas16, ARM::MVE_VMLALDAVas32,
5246 ARM::MVE_VMLALDAVxs16, ARM::MVE_VMLALDAVxs32,
5247 ARM::MVE_VMLALDAVaxs16, ARM::MVE_VMLALDAVaxs32,
5248 ARM::MVE_VMLSLDAVs16, ARM::MVE_VMLSLDAVs32,
5249 ARM::MVE_VMLSLDAVas16, ARM::MVE_VMLSLDAVas32,
5250 ARM::MVE_VMLSLDAVxs16, ARM::MVE_VMLSLDAVxs32,
5251 ARM::MVE_VMLSLDAVaxs16, ARM::MVE_VMLSLDAVaxs32,
5252 };
5253 SelectMVE_VMLLDAV(N, IntNo == Intrinsic::arm_mve_vmlldava_predicated,
5254 OpcodesS, OpcodesU);
5255 return;
5256 }
5257
5258 case Intrinsic::arm_mve_vrmlldavha:
5259 case Intrinsic::arm_mve_vrmlldavha_predicated: {
5260 static const uint16_t OpcodesU[] = {
5261 ARM::MVE_VRMLALDAVHu32, ARM::MVE_VRMLALDAVHau32,
5262 };
5263 static const uint16_t OpcodesS[] = {
5264 ARM::MVE_VRMLALDAVHs32, ARM::MVE_VRMLALDAVHas32,
5265 ARM::MVE_VRMLALDAVHxs32, ARM::MVE_VRMLALDAVHaxs32,
5266 ARM::MVE_VRMLSLDAVHs32, ARM::MVE_VRMLSLDAVHas32,
5267 ARM::MVE_VRMLSLDAVHxs32, ARM::MVE_VRMLSLDAVHaxs32,
5268 };
5269 SelectMVE_VRMLLDAVH(N, IntNo == Intrinsic::arm_mve_vrmlldavha_predicated,
5270 OpcodesS, OpcodesU);
5271 return;
5272 }
5273
5274 case Intrinsic::arm_mve_vidup:
5275 case Intrinsic::arm_mve_vidup_predicated: {
5276 static const uint16_t Opcodes[] = {
5277 ARM::MVE_VIDUPu8, ARM::MVE_VIDUPu16, ARM::MVE_VIDUPu32,
5278 };
5279 SelectMVE_VxDUP(N, Opcodes, false,
5280 IntNo == Intrinsic::arm_mve_vidup_predicated);
5281 return;
5282 }
5283
5284 case Intrinsic::arm_mve_vddup:
5285 case Intrinsic::arm_mve_vddup_predicated: {
5286 static const uint16_t Opcodes[] = {
5287 ARM::MVE_VDDUPu8, ARM::MVE_VDDUPu16, ARM::MVE_VDDUPu32,
5288 };
5289 SelectMVE_VxDUP(N, Opcodes, false,
5290 IntNo == Intrinsic::arm_mve_vddup_predicated);
5291 return;
5292 }
5293
5294 case Intrinsic::arm_mve_viwdup:
5295 case Intrinsic::arm_mve_viwdup_predicated: {
5296 static const uint16_t Opcodes[] = {
5297 ARM::MVE_VIWDUPu8, ARM::MVE_VIWDUPu16, ARM::MVE_VIWDUPu32,
5298 };
5299 SelectMVE_VxDUP(N, Opcodes, true,
5300 IntNo == Intrinsic::arm_mve_viwdup_predicated);
5301 return;
5302 }
5303
5304 case Intrinsic::arm_mve_vdwdup:
5305 case Intrinsic::arm_mve_vdwdup_predicated: {
5306 static const uint16_t Opcodes[] = {
5307 ARM::MVE_VDWDUPu8, ARM::MVE_VDWDUPu16, ARM::MVE_VDWDUPu32,
5308 };
5309 SelectMVE_VxDUP(N, Opcodes, true,
5310 IntNo == Intrinsic::arm_mve_vdwdup_predicated);
5311 return;
5312 }
5313
5314 case Intrinsic::arm_cde_cx1d:
5315 case Intrinsic::arm_cde_cx1da:
5316 case Intrinsic::arm_cde_cx2d:
5317 case Intrinsic::arm_cde_cx2da:
5318 case Intrinsic::arm_cde_cx3d:
5319 case Intrinsic::arm_cde_cx3da: {
5320 bool HasAccum = IntNo == Intrinsic::arm_cde_cx1da ||
5321 IntNo == Intrinsic::arm_cde_cx2da ||
5322 IntNo == Intrinsic::arm_cde_cx3da;
5323 size_t NumExtraOps;
5324 uint16_t Opcode;
5325 switch (IntNo) {
5326 case Intrinsic::arm_cde_cx1d:
5327 case Intrinsic::arm_cde_cx1da:
5328 NumExtraOps = 0;
5329 Opcode = HasAccum ? ARM::CDE_CX1DA : ARM::CDE_CX1D;
5330 break;
5331 case Intrinsic::arm_cde_cx2d:
5332 case Intrinsic::arm_cde_cx2da:
5333 NumExtraOps = 1;
5334 Opcode = HasAccum ? ARM::CDE_CX2DA : ARM::CDE_CX2D;
5335 break;
5336 case Intrinsic::arm_cde_cx3d:
5337 case Intrinsic::arm_cde_cx3da:
5338 NumExtraOps = 2;
5339 Opcode = HasAccum ? ARM::CDE_CX3DA : ARM::CDE_CX3D;
5340 break;
5341 default:
5342 llvm_unreachable("Unexpected opcode");
5343 }
5344 SelectCDE_CXxD(N, Opcode, NumExtraOps, HasAccum);
5345 return;
5346 }
5347 }
5348 break;
5349 }
5350
5352 SelectCMP_SWAP(N);
5353 return;
5354 }
5355
5356 SelectCode(N);
5357}
5358
5359// Inspect a register string of the form
5360// cp<coprocessor>:<opc1>:c<CRn>:c<CRm>:<opc2> (32bit) or
5361// cp<coprocessor>:<opc1>:c<CRm> (64bit) inspect the fields of the string
5362// and obtain the integer operands from them, adding these operands to the
5363// provided vector.
5365 SelectionDAG *CurDAG,
5366 const SDLoc &DL,
5367 std::vector<SDValue> &Ops) {
5369 RegString.split(Fields, ':');
5370
5371 if (Fields.size() > 1) {
5372 bool AllIntFields = true;
5373
5374 for (StringRef Field : Fields) {
5375 // Need to trim out leading 'cp' characters and get the integer field.
5376 unsigned IntField;
5377 AllIntFields &= !Field.trim("CPcp").getAsInteger(10, IntField);
5378 Ops.push_back(CurDAG->getTargetConstant(IntField, DL, MVT::i32));
5379 }
5380
5381 assert(AllIntFields &&
5382 "Unexpected non-integer value in special register string.");
5383 (void)AllIntFields;
5384 }
5385}
5386
5387// Maps a Banked Register string to its mask value. The mask value returned is
5388// for use in the MRSbanked / MSRbanked instruction nodes as the Banked Register
5389// mask operand, which expresses which register is to be used, e.g. r8, and in
5390// which mode it is to be used, e.g. usr. Returns -1 to signify that the string
5391// was invalid.
5392static inline int getBankedRegisterMask(StringRef RegString) {
5393 auto TheReg = ARMBankedReg::lookupBankedRegByName(RegString.lower());
5394 if (!TheReg)
5395 return -1;
5396 return TheReg->Encoding;
5397}
5398
5399// The flags here are common to those allowed for apsr in the A class cores and
5400// those allowed for the special registers in the M class cores. Returns a
5401// value representing which flags were present, -1 if invalid.
5402static inline int getMClassFlagsMask(StringRef Flags) {
5403 return StringSwitch<int>(Flags)
5404 .Case("", 0x2) // no flags means nzcvq for psr registers, and 0x2 is
5405 // correct when flags are not permitted
5406 .Case("g", 0x1)
5407 .Case("nzcvq", 0x2)
5408 .Case("nzcvqg", 0x3)
5409 .Default(-1);
5410}
5411
5412// Maps MClass special registers string to its value for use in the
5413// t2MRS_M/t2MSR_M instruction nodes as the SYSm value operand.
5414// Returns -1 to signify that the string was invalid.
5416 auto TheReg = ARMSysReg::lookupMClassSysRegByName(Reg);
5417 const FeatureBitset &FeatureBits = Subtarget->getFeatureBits();
5418 if (!TheReg || !TheReg->hasRequiredFeatures(FeatureBits))
5419 return -1;
5420 return (int)(TheReg->Encoding & 0xFFF); // SYSm value
5421}
5422
5424 // The mask operand contains the special register (R Bit) in bit 4, whether
5425 // the register is spsr (R bit is 1) or one of cpsr/apsr (R bit is 0), and
5426 // bits 3-0 contains the fields to be accessed in the special register, set by
5427 // the flags provided with the register.
5428 int Mask = 0;
5429 if (Reg == "apsr") {
5430 // The flags permitted for apsr are the same flags that are allowed in
5431 // M class registers. We get the flag value and then shift the flags into
5432 // the correct place to combine with the mask.
5433 Mask = getMClassFlagsMask(Flags);
5434 if (Mask == -1)
5435 return -1;
5436 return Mask << 2;
5437 }
5438
5439 if (Reg != "cpsr" && Reg != "spsr") {
5440 return -1;
5441 }
5442
5443 // This is the same as if the flags were "fc"
5444 if (Flags.empty() || Flags == "all")
5445 return Mask | 0x9;
5446
5447 // Inspect the supplied flags string and set the bits in the mask for
5448 // the relevant and valid flags allowed for cpsr and spsr.
5449 for (char Flag : Flags) {
5450 int FlagVal;
5451 switch (Flag) {
5452 case 'c':
5453 FlagVal = 0x1;
5454 break;
5455 case 'x':
5456 FlagVal = 0x2;
5457 break;
5458 case 's':
5459 FlagVal = 0x4;
5460 break;
5461 case 'f':
5462 FlagVal = 0x8;
5463 break;
5464 default:
5465 FlagVal = 0;
5466 }
5467
5468 // This avoids allowing strings where the same flag bit appears twice.
5469 if (!FlagVal || (Mask & FlagVal))
5470 return -1;
5471 Mask |= FlagVal;
5472 }
5473
5474 // If the register is spsr then we need to set the R bit.
5475 if (Reg == "spsr")
5476 Mask |= 0x10;
5477
5478 return Mask;
5479}
5480
5481// Lower the read_register intrinsic to ARM specific DAG nodes
5482// using the supplied metadata string to select the instruction node to use
5483// and the registers/masks to construct as operands for the node.
5484bool ARMDAGToDAGISel::tryReadRegister(SDNode *N){
5485 const auto *MD = cast<MDNodeSDNode>(N->getOperand(1));
5486 const auto *RegString = cast<MDString>(MD->getMD()->getOperand(0));
5487 bool IsThumb2 = Subtarget->isThumb2();
5488 SDLoc DL(N);
5489
5490 std::vector<SDValue> Ops;
5491 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
5492
5493 if (!Ops.empty()) {
5494 // If the special register string was constructed of fields (as defined
5495 // in the ACLE) then need to lower to MRC node (32 bit) or
5496 // MRRC node(64 bit), we can make the distinction based on the number of
5497 // operands we have.
5498 unsigned Opcode;
5499 SmallVector<EVT, 3> ResTypes;
5500 if (Ops.size() == 5){
5501 Opcode = IsThumb2 ? ARM::t2MRC : ARM::MRC;
5502 ResTypes.append({ MVT::i32, MVT::Other });
5503 } else {
5504 assert(Ops.size() == 3 &&
5505 "Invalid number of fields in special register string.");
5506 Opcode = IsThumb2 ? ARM::t2MRRC : ARM::MRRC;
5507 ResTypes.append({ MVT::i32, MVT::i32, MVT::Other });
5508 }
5509
5510 Ops.push_back(getAL(CurDAG, DL));
5511 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
5512 Ops.push_back(N->getOperand(0));
5513 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, ResTypes, Ops));
5514 return true;
5515 }
5516
5517 std::string SpecialReg = RegString->getString().lower();
5518
5519 int BankedReg = getBankedRegisterMask(SpecialReg);
5520 if (BankedReg != -1) {
5521 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32),
5522 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
5523 N->getOperand(0) };
5524 ReplaceNode(
5525 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSbanked : ARM::MRSbanked,
5526 DL, MVT::i32, MVT::Other, Ops));
5527 return true;
5528 }
5529
5530 // The VFP registers are read by creating SelectionDAG nodes with opcodes
5531 // corresponding to the register that is being read from. So we switch on the
5532 // string to find which opcode we need to use.
5533 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
5534 .Case("fpscr", ARM::VMRS)
5535 .Case("fpexc", ARM::VMRS_FPEXC)
5536 .Case("fpsid", ARM::VMRS_FPSID)
5537 .Case("mvfr0", ARM::VMRS_MVFR0)
5538 .Case("mvfr1", ARM::VMRS_MVFR1)
5539 .Case("mvfr2", ARM::VMRS_MVFR2)
5540 .Case("fpinst", ARM::VMRS_FPINST)
5541 .Case("fpinst2", ARM::VMRS_FPINST2)
5542 .Default(0);
5543
5544 // If an opcode was found then we can lower the read to a VFP instruction.
5545 if (Opcode) {
5546 if (!Subtarget->hasVFP2Base())
5547 return false;
5548 if (Opcode == ARM::VMRS_MVFR2 && !Subtarget->hasFPARMv8Base())
5549 return false;
5550
5551 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
5552 N->getOperand(0) };
5553 ReplaceNode(N,
5554 CurDAG->getMachineNode(Opcode, DL, MVT::i32, MVT::Other, Ops));
5555 return true;
5556 }
5557
5558 // If the target is M Class then need to validate that the register string
5559 // is an acceptable value, so check that a mask can be constructed from the
5560 // string.
5561 if (Subtarget->isMClass()) {
5562 int SYSmValue = getMClassRegisterMask(SpecialReg, Subtarget);
5563 if (SYSmValue == -1)
5564 return false;
5565
5566 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
5567 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
5568 N->getOperand(0) };
5569 ReplaceNode(
5570 N, CurDAG->getMachineNode(ARM::t2MRS_M, DL, MVT::i32, MVT::Other, Ops));
5571 return true;
5572 }
5573
5574 // Here we know the target is not M Class so we need to check if it is one
5575 // of the remaining possible values which are apsr, cpsr or spsr.
5576 if (SpecialReg == "apsr" || SpecialReg == "cpsr") {
5577 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
5578 N->getOperand(0) };
5579 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRS_AR : ARM::MRS,
5580 DL, MVT::i32, MVT::Other, Ops));
5581 return true;
5582 }
5583
5584 if (SpecialReg == "spsr") {
5585 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
5586 N->getOperand(0) };
5587 ReplaceNode(
5588 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSsys_AR : ARM::MRSsys, DL,
5589 MVT::i32, MVT::Other, Ops));
5590 return true;
5591 }
5592
5593 return false;
5594}
5595
5596// Lower the write_register intrinsic to ARM specific DAG nodes
5597// using the supplied metadata string to select the instruction node to use
5598// and the registers/masks to use in the nodes
5599bool ARMDAGToDAGISel::tryWriteRegister(SDNode *N){
5600 const auto *MD = cast<MDNodeSDNode>(N->getOperand(1));
5601 const auto *RegString = cast<MDString>(MD->getMD()->getOperand(0));
5602 bool IsThumb2 = Subtarget->isThumb2();
5603 SDLoc DL(N);
5604
5605 std::vector<SDValue> Ops;
5606 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
5607
5608 if (!Ops.empty()) {
5609 // If the special register string was constructed of fields (as defined
5610 // in the ACLE) then need to lower to MCR node (32 bit) or
5611 // MCRR node(64 bit), we can make the distinction based on the number of
5612 // operands we have.
5613 unsigned Opcode;
5614 if (Ops.size() == 5) {
5615 Opcode = IsThumb2 ? ARM::t2MCR : ARM::MCR;
5616 Ops.insert(Ops.begin()+2, N->getOperand(2));
5617 } else {
5618 assert(Ops.size() == 3 &&
5619 "Invalid number of fields in special register string.");
5620 Opcode = IsThumb2 ? ARM::t2MCRR : ARM::MCRR;
5621 SDValue WriteValue[] = { N->getOperand(2), N->getOperand(3) };
5622 Ops.insert(Ops.begin()+2, WriteValue, WriteValue+2);
5623 }
5624
5625 Ops.push_back(getAL(CurDAG, DL));
5626 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
5627 Ops.push_back(N->getOperand(0));
5628
5629 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
5630 return true;
5631 }
5632
5633 std::string SpecialReg = RegString->getString().lower();
5634 int BankedReg = getBankedRegisterMask(SpecialReg);
5635 if (BankedReg != -1) {
5636 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32), N->getOperand(2),
5637 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
5638 N->getOperand(0) };
5639 ReplaceNode(
5640 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSRbanked : ARM::MSRbanked,
5641 DL, MVT::Other, Ops));
5642 return true;
5643 }
5644
5645 // The VFP registers are written to by creating SelectionDAG nodes with
5646 // opcodes corresponding to the register that is being written. So we switch
5647 // on the string to find which opcode we need to use.
5648 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
5649 .Case("fpscr", ARM::VMSR)
5650 .Case("fpexc", ARM::VMSR_FPEXC)
5651 .Case("fpsid", ARM::VMSR_FPSID)
5652 .Case("fpinst", ARM::VMSR_FPINST)
5653 .Case("fpinst2", ARM::VMSR_FPINST2)
5654 .Default(0);
5655
5656 if (Opcode) {
5657 if (!Subtarget->hasVFP2Base())
5658 return false;
5659 Ops = { N->getOperand(2), getAL(CurDAG, DL),
5660 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
5661 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
5662 return true;
5663 }
5664
5665 std::pair<StringRef, StringRef> Fields;
5666 Fields = StringRef(SpecialReg).rsplit('_');
5667 std::string Reg = Fields.first.str();
5668 StringRef Flags = Fields.second;
5669
5670 // If the target was M Class then need to validate the special register value
5671 // and retrieve the mask for use in the instruction node.
5672 if (Subtarget->isMClass()) {
5673 int SYSmValue = getMClassRegisterMask(SpecialReg, Subtarget);
5674 if (SYSmValue == -1)
5675 return false;
5676
5677 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
5678 N->getOperand(2), getAL(CurDAG, DL),
5679 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
5680 ReplaceNode(N, CurDAG->getMachineNode(ARM::t2MSR_M, DL, MVT::Other, Ops));
5681 return true;
5682 }
5683
5684 // We then check to see if a valid mask can be constructed for one of the
5685 // register string values permitted for the A and R class cores. These values
5686 // are apsr, spsr and cpsr; these are also valid on older cores.
5687 int Mask = getARClassRegisterMask(Reg, Flags);
5688 if (Mask != -1) {
5689 Ops = { CurDAG->getTargetConstant(Mask, DL, MVT::i32), N->getOperand(2),
5690 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
5691 N->getOperand(0) };
5692 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSR_AR : ARM::MSR,
5693 DL, MVT::Other, Ops));
5694 return true;
5695 }
5696
5697 return false;
5698}
5699
5700bool ARMDAGToDAGISel::tryInlineAsm(SDNode *N){
5701 std::vector<SDValue> AsmNodeOperands;
5702 InlineAsm::Flag Flag;
5703 bool Changed = false;
5704 unsigned NumOps = N->getNumOperands();
5705
5706 // Normally, i64 data is bounded to two arbitrary GRPs for "%r" constraint.
5707 // However, some instrstions (e.g. ldrexd/strexd in ARM mode) require
5708 // (even/even+1) GPRs and use %n and %Hn to refer to the individual regs
5709 // respectively. Since there is no constraint to explicitly specify a
5710 // reg pair, we use GPRPair reg class for "%r" for 64-bit data. For Thumb,
5711 // the 64-bit data may be referred by H, Q, R modifiers, so we still pack
5712 // them into a GPRPair.
5713
5714 SDLoc dl(N);
5715 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps - 1) : SDValue();
5716
5717 SmallVector<bool, 8> OpChanged;
5718 // Glue node will be appended late.
5719 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) {
5720 SDValue op = N->getOperand(i);
5721 AsmNodeOperands.push_back(op);
5722
5724 continue;
5725
5726 if (const auto *C = dyn_cast<ConstantSDNode>(N->getOperand(i)))
5727 Flag = InlineAsm::Flag(C->getZExtValue());
5728 else
5729 continue;
5730
5731 // Immediate operands to inline asm in the SelectionDAG are modeled with
5732 // two operands. The first is a constant of value InlineAsm::Kind::Imm, and
5733 // the second is a constant with the value of the immediate. If we get here
5734 // and we have a Kind::Imm, skip the next operand, and continue.
5735 if (Flag.isImmKind()) {
5736 SDValue op = N->getOperand(++i);
5737 AsmNodeOperands.push_back(op);
5738 continue;
5739 }
5740
5741 const unsigned NumRegs = Flag.getNumOperandRegisters();
5742 if (NumRegs)
5743 OpChanged.push_back(false);
5744
5745 unsigned DefIdx = 0;
5746 bool IsTiedToChangedOp = false;
5747 // If it's a use that is tied with a previous def, it has no
5748 // reg class constraint.
5749 if (Changed && Flag.isUseOperandTiedToDef(DefIdx))
5750 IsTiedToChangedOp = OpChanged[DefIdx];
5751
5752 // Memory operands to inline asm in the SelectionDAG are modeled with two
5753 // operands: a constant of value InlineAsm::Kind::Mem followed by the input
5754 // operand. If we get here and we have a Kind::Mem, skip the next operand
5755 // (so it doesn't get misinterpreted), and continue. We do this here because
5756 // it's important to update the OpChanged array correctly before moving on.
5757 if (Flag.isMemKind()) {
5758 SDValue op = N->getOperand(++i);
5759 AsmNodeOperands.push_back(op);
5760 continue;
5761 }
5762
5763 if (!Flag.isRegUseKind() && !Flag.isRegDefKind() &&
5764 !Flag.isRegDefEarlyClobberKind())
5765 continue;
5766
5767 unsigned RC;
5768 const bool HasRC = Flag.hasRegClassConstraint(RC);
5769 if ((!IsTiedToChangedOp && (!HasRC || RC != ARM::GPRRegClassID))
5770 || NumRegs != 2)
5771 continue;
5772
5773 assert((i+2 < NumOps) && "Invalid number of operands in inline asm");
5774 SDValue V0 = N->getOperand(i+1);
5775 SDValue V1 = N->getOperand(i+2);
5776 Register Reg0 = cast<RegisterSDNode>(V0)->getReg();
5777 Register Reg1 = cast<RegisterSDNode>(V1)->getReg();
5778 SDValue PairedReg;
5779 MachineRegisterInfo &MRI = MF->getRegInfo();
5780
5781 if (Flag.isRegDefKind() || Flag.isRegDefEarlyClobberKind()) {
5782 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to
5783 // the original GPRs.
5784
5785 Register GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5786 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
5787 SDValue Chain = SDValue(N,0);
5788
5789 SDNode *GU = N->getGluedUser();
5790 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::Untyped,
5791 Chain.getValue(1));
5792
5793 // Extract values from a GPRPair reg and copy to the original GPR reg.
5794 SDValue Sub0 = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32,
5795 RegCopy);
5796 SDValue Sub1 = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32,
5797 RegCopy);
5798 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0,
5799 RegCopy.getValue(1));
5800 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1));
5801
5802 // Update the original glue user.
5803 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1);
5804 Ops.push_back(T1.getValue(1));
5805 CurDAG->UpdateNodeOperands(GU, Ops);
5806 } else {
5807 // For Kind == InlineAsm::Kind::RegUse, we first copy two GPRs into a
5808 // GPRPair and then pass the GPRPair to the inline asm.
5809 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain];
5810
5811 // As REG_SEQ doesn't take RegisterSDNode, we copy them first.
5812 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32,
5813 Chain.getValue(1));
5814 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32,
5815 T0.getValue(1));
5816 SDValue Pair = SDValue(createGPRPairNode(MVT::Untyped, T0, T1), 0);
5817
5818 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two
5819 // i32 VRs of inline asm with it.
5820 Register GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
5821 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
5822 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1));
5823
5824 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
5825 Glue = Chain.getValue(1);
5826 }
5827
5828 Changed = true;
5829
5830 if(PairedReg.getNode()) {
5831 OpChanged[OpChanged.size() -1 ] = true;
5832 Flag = InlineAsm::Flag(Flag.getKind(), 1 /* RegNum*/);
5833 if (IsTiedToChangedOp)
5834 Flag.setMatchingOp(DefIdx);
5835 else
5836 Flag.setRegClass(ARM::GPRPairRegClassID);
5837 // Replace the current flag.
5838 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant(
5839 Flag, dl, MVT::i32);
5840 // Add the new register node and skip the original two GPRs.
5841 AsmNodeOperands.push_back(PairedReg);
5842 // Skip the next two GPRs.
5843 i += 2;
5844 }
5845 }
5846
5847 if (Glue.getNode())
5848 AsmNodeOperands.push_back(Glue);
5849 if (!Changed)
5850 return false;
5851
5852 SDValue New = CurDAG->getNode(N->getOpcode(), SDLoc(N),
5853 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
5854 New->setNodeId(-1);
5855 ReplaceNode(N, New.getNode());
5856 return true;
5857}
5858
5859bool ARMDAGToDAGISel::SelectInlineAsmMemoryOperand(
5860 const SDValue &Op, InlineAsm::ConstraintCode ConstraintID,
5861 std::vector<SDValue> &OutOps) {
5862 switch(ConstraintID) {
5863 default:
5864 llvm_unreachable("Unexpected asm memory constraint");
5865 case InlineAsm::ConstraintCode::m:
5866 case InlineAsm::ConstraintCode::o:
5867 case InlineAsm::ConstraintCode::Q:
5868 case InlineAsm::ConstraintCode::Um:
5869 case InlineAsm::ConstraintCode::Un:
5870 case InlineAsm::ConstraintCode::Uq:
5871 case InlineAsm::ConstraintCode::Us:
5872 case InlineAsm::ConstraintCode::Ut:
5873 case InlineAsm::ConstraintCode::Uv:
5874 case InlineAsm::ConstraintCode::Uy:
5875 // Require the address to be in a register. That is safe for all ARM
5876 // variants and it is hard to do anything much smarter without knowing
5877 // how the operand is used.
5878 OutOps.push_back(Op);
5879 return false;
5880 }
5881 return true;
5882}
5883
5884/// createARMISelDag - This pass converts a legalized DAG into a
5885/// ARM-specific DAG, ready for instruction scheduling.
5886///
5888 CodeGenOptLevel OptLevel) {
5889 return new ARMDAGToDAGISelLegacy(TM, OptLevel);
5890}
static bool isOpcWithIntImmediate(const SDNode *N, unsigned Opc, uint64_t &Imm)
auto * MemN
return SDValue()
const AArch64Subtarget & Subtarget
static SDValue createGPRPairNode(SelectionDAG &DAG, SDValue V)
const TargetLowering & TLI
SDValue Pred
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t uint64_t
uint64_t Imm
AMDGPU Register Bank Select
This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...
static bool isThumb(const MCSubtargetInfo &STI)
static unsigned getVectorShuffleOpcode(EVT VT, unsigned Opc64[3], unsigned Opc128[3])
static int getBankedRegisterMask(StringRef RegString)
static bool isPerfectIncrement(SDValue Inc, EVT VecTy, unsigned NumVecs)
Returns true if the given increment is a Constant known to be equal to the access size performed by a...
static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc)
static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned &Imm)
static bool isVSTfixed(unsigned Opc)
static bool isVLDfixed(unsigned Opc)
static bool isInt32Immediate(SDNode *N, unsigned &Imm)
isInt32Immediate - This method tests to see if the node is a 32-bit constant operand.
static std::optional< std::pair< unsigned, unsigned > > getContiguousRangeOfSetBits(const APInt &A)
static void getIntOperandsFromRegisterString(StringRef RegString, SelectionDAG *CurDAG, const SDLoc &DL, std::vector< SDValue > &Ops)
static int getARClassRegisterMask(StringRef Reg, StringRef Flags)
static int getMClassRegisterMask(StringRef Reg, const ARMSubtarget *Subtarget)
static SDValue getAL(SelectionDAG *CurDAG, const SDLoc &dl)
getAL - Returns a ARMCC::AL immediate node.
static bool shouldUseZeroOffsetLdSt(SDValue N)
static int getMClassFlagsMask(StringRef Flags)
static bool SDValueToConstBool(SDValue SDVal)
static bool isScaledConstantInRange(SDValue Node, int Scale, int RangeMin, int RangeMax, int &ScaledConstant)
Check whether a particular node is a constant value representable as (N * Scale) where (N in [RangeMi...
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define X(NUM, ENUM, NAME)
Definition ELF.h:665
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
static bool isSigned(unsigned Opcode)
#define DEBUG_TYPE
#define op(i)
const HexagonInstrInfo * TII
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
unsigned Opc
#define I(x, y, z)
Definition MD5.cpp:57
Register Reg
Promote Memory to Register
Definition Mem2Reg.cpp:110
#define T1
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
OptimizedStructLayoutField Field
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
RISCVCC::CondCode CC
MachineRegisterInfo & MRI
MachineInstr *& Lo
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
This file describes how to lower LLVM code to machine code.
#define PASS_NAME
Value * RHS
Value * LHS
const AArch64Options & getCLOpts() const
opStatus convertToInteger(MutableArrayRef< integerPart > Input, unsigned int Width, bool IsSigned, roundingMode RM, bool *IsExact) const
Definition APFloat.h:1469
Class for arbitrary precision integers.
Definition APInt.h:78
uint64_t getZExtValue() const
Container class for subtarget features.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
ISD::MemIndexedMode getAddressingMode() const
Return the addressing mode for this load or store: unindexed, pre-inc, pre-dec, post-inc,...
bool mayStore() const
Return true if this instruction could possibly modify memory.
unsigned getOpcode() const
Return the opcode number for this descriptor.
SimpleValueType SimpleTy
Align getObjectAlign(int ObjectIdx) const
Return the alignment of the specified stack object.
int64_t getObjectSize(int ObjectIdx) const
Return the size of the specified object.
bool isFixedObjectIndex(int ObjectIdx) const
Returns true if the specified index corresponds to a fixed stack object.
void setObjectAlignment(int ObjectIdx, Align Alignment)
setObjectAlignment - Change the alignment of the specified stack object.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFrameInfo & getFrameInfo()
getFrameInfo - Return the frame info object for the current function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
MachineMemOperand * getMachineMemOperand(MachinePointerInfo PtrInfo, MachineMemOperand::Flags F, LLT MemTy, Align BaseAlignment, const MMOMetadata &Metadata=MMOMetadata(), SyncScope::ID SSID=SyncScope::System, AtomicOrdering Ordering=AtomicOrdering::NotAtomic, AtomicOrdering FailureOrdering=AtomicOrdering::NotAtomic)
getMachineMemOperand - Allocate a new MachineMemOperand.
@ MOLoad
The memory access reads data.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
EVT getMemoryVT() const
Return the type of the in-memory value.
Wrapper class for IR location info (IR ordering and DebugLoc) to be passed into SDNode creation funct...
Represents one node in the SelectionDAG.
int getNodeId() const
Return the unique node id.
unsigned getOpcode() const
Return the SelectionDAG opcode value for this node.
bool hasOneUse() const
Return true if there is exactly one use of this node.
uint64_t getAsZExtVal() const
Helper method returns the zero-extended integer value of a ConstantSDNode.
const SDValue & getOperand(unsigned Num) const
uint64_t getConstantOperandVal(unsigned Num) const
Helper method returns the integer value of a ConstantSDNode operand.
op_iterator op_end() const
op_iterator op_begin() const
Unlike LLVM values, Selection DAG nodes may return multiple values as the result of a computation.
SDNode * getNode() const
get the SDNode which holds the desired result
bool hasOneUse() const
Return true if there is exactly one node using value ResNo of Node, in exactly one operand.
SDValue getValue(unsigned R) const
EVT getValueType() const
Return the ValueType of the referenced return value.
const SDValue & getOperand(unsigned i) const
uint64_t getConstantOperandVal(unsigned i) const
unsigned getOpcode() const
SelectionDAGISel - This is the common base class used for SelectionDAG-based pattern-matching instruc...
virtual bool runOnMachineFunction(MachineFunction &mf)
This is used to represent a portion of an LLVM function in a low-level Data Dependence DAG representa...
SDValue getTargetConstant(uint64_t Val, const SDLoc &DL, EVT VT, bool isOpaque=false)
void append(ItTy in_start, ItTy in_end)
Add the specified range to the end of the SmallVector.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:728
LLVM_ABI std::string lower() const
A switch()-like statement whose cases are string literals.
StringSwitch & Case(StringLiteral S, T Value)
virtual MVT getPointerTy(const DataLayout &DL, uint32_t AS=0) const
Return the pointer type for the given address space, defaults to the pointer type from the data layou...
LLVM Value Representation.
Definition Value.h:75
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
static ShiftOpc getShiftOpcForNode(unsigned Opcode)
int getSOImmVal(unsigned Arg)
getSOImmVal - Given a 32-bit immediate, if it is something that can fit into an shifter_operand immed...
uint64_t decodeVMOVModImm(unsigned ModImm, unsigned &EltBits)
decodeVMOVModImm - Decode a NEON/MVE modified immediate value into the element value and the element ...
float getFPImmFloat(unsigned Imm)
int getT2SOImmVal(unsigned Arg)
getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit into a Thumb-2 shifter_oper...
unsigned getAM2Opc(AddrOpc Opc, unsigned Imm12, ShiftOpc SO, unsigned IdxMode=0)
unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset)
getAM5Opc - This function encodes the addrmode5 opc field.
unsigned getAM5FP16Opc(AddrOpc Opc, unsigned char Offset)
getAM5FP16Opc - This function encodes the addrmode5fp16 opc field.
unsigned getAM3Opc(AddrOpc Opc, unsigned char Offset, unsigned IdxMode=0)
getAM3Opc - This function encodes the addrmode3 opc field.
unsigned getSORegOpc(ShiftOpc ShOp, unsigned Imm)
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ TargetConstantPool
Definition ISDOpcodes.h:191
@ MLOAD
Masked load and store - consecutive vector load and store operations with additional mask operand tha...
@ SMUL_LOHI
SMUL_LOHI/UMUL_LOHI - Multiply two integers of type iN, producing a signed/unsigned value of type i[2...
Definition ISDOpcodes.h:277
@ ADD
Simple integer binary arithmetic operators.
Definition ISDOpcodes.h:266
@ LOAD
LOAD and STORE have token chains as their first operand, then the same operands as an LLVM load/store...
@ INTRINSIC_VOID
OUTCHAIN = INTRINSIC_VOID(INCHAIN, INTRINSICID, arg1, arg2, ...) This node represents a target intrin...
Definition ISDOpcodes.h:222
@ SINT_TO_FP
[SU]INT_TO_FP - These operators convert integers (whose interpreted sign depends on the first letter)...
Definition ISDOpcodes.h:912
@ FADD
Simple binary floating point operators.
Definition ISDOpcodes.h:420
@ BITCAST
BITCAST - This operator converts between integer, vector and FP values, as if the value was stored to...
@ TargetExternalSymbol
Definition ISDOpcodes.h:192
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:83
@ CopyFromReg
CopyFromReg - This node indicates that the input value is a virtual or physical register that is defi...
Definition ISDOpcodes.h:232
@ TargetGlobalAddress
TargetGlobalAddress - Like GlobalAddress, but the DAG does no folding or anything else with this node...
Definition ISDOpcodes.h:187
@ SHL
Shift and rotation operations.
Definition ISDOpcodes.h:793
@ READ_REGISTER
READ_REGISTER, WRITE_REGISTER - This node represents llvm.register on the DAG, which implements the n...
Definition ISDOpcodes.h:141
@ EXTRACT_VECTOR_ELT
EXTRACT_VECTOR_ELT(VECTOR, IDX) - Returns a single element from VECTOR identified by the (potentially...
Definition ISDOpcodes.h:581
@ CopyToReg
CopyToReg - This node has three operands: a chain, a register number to set to this value,...
Definition ISDOpcodes.h:226
@ ATOMIC_CMP_SWAP
Val, OUTCHAIN = ATOMIC_CMP_SWAP(INCHAIN, ptr, cmp, swap) For double-word atomic operations: ValLo,...
@ SIGN_EXTEND_INREG
SIGN_EXTEND_INREG - This operator atomically performs a SHL/SRA pair to sign extend a small value in ...
Definition ISDOpcodes.h:920
@ INLINEASM_BR
INLINEASM_BR - Branching version of inline asm. Used by asm-goto.
@ FP_TO_SINT
FP_TO_[US]INT - Convert a floating point value to a signed or unsigned integer.
Definition ISDOpcodes.h:958
@ AND
Bitwise operators - logical and, logical or, logical xor.
Definition ISDOpcodes.h:763
@ INTRINSIC_WO_CHAIN
RESULT = INTRINSIC_WO_CHAIN(INTRINSICID, arg1, arg2, ...) This node represents a target intrinsic fun...
Definition ISDOpcodes.h:207
@ INSERT_VECTOR_ELT
INSERT_VECTOR_ELT(VECTOR, VAL, IDX) - Returns VECTOR with the element at IDX replaced with VAL.
Definition ISDOpcodes.h:570
@ FP_ROUND
X = FP_ROUND(Y, TRUNC) - Rounding 'Y' from a larger floating point type down to the precision of the ...
Definition ISDOpcodes.h:991
@ INLINEASM
INLINEASM - Represents an inline asm block.
@ FP_TO_SINT_SAT
FP_TO_[US]INT_SAT - Convert floating point value in operand 0 to a signed or unsigned scalar integer ...
Definition ISDOpcodes.h:977
@ INTRINSIC_W_CHAIN
RESULT,OUTCHAIN = INTRINSIC_W_CHAIN(INCHAIN, INTRINSICID, arg1, ...) This node represents a target in...
Definition ISDOpcodes.h:215
@ TargetGlobalTLSAddress
Definition ISDOpcodes.h:188
MemIndexedMode
MemIndexedMode enum - This enum defines the load / store indexed addressing modes.
Flag
These should be considered private to the implementation of the MCInstrDesc class.
@ ARM
Windows AXP64.
Definition MCAsmInfo.h:50
@ User
could "use" a pointer
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
BaseReg
Stack frame base register. Bit 0 of FREInfo.Info.
Definition SFrame.h:77
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
LLVM_ABI bool isNullConstant(SDValue V)
Returns true if V is a constant integer zero.
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
int countr_one(T Value)
Count the number of ones from the least significant bit to the first zero bit.
Definition bit.h:315
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:649
FunctionPass * createARMISelDag(ARMBaseTargetMachine &TM, CodeGenOptLevel OptLevel)
createARMISelDag - This pass converts a legalized DAG into a ARM-specific DAG, ready for instruction ...
constexpr bool isShiftedMask_32(uint32_t Value)
Return true if the argument contains a non-empty sequence of ones with the remainder zero (32 bit ver...
Definition MathExtras.h:268
int countr_zero(T Val)
Count number of 0's from the least significant bit to the most stopping at the first 1.
Definition bit.h:204
unsigned Log2_32(uint32_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
Definition MathExtras.h:326
constexpr bool isPowerOf2_32(uint32_t Value)
Return true if the argument is a power of two > 0.
Definition MathExtras.h:280
CodeGenOptLevel
Code generation optimization level.
Definition CodeGen.h:280
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
unsigned ConstantMaterializationCost(unsigned Val, const ARMSubtarget *Subtarget, bool ForCodesize=false)
Returns the number of instructions required to materialize the given constant in a register,...
@ FMul
Product of floats.
@ And
Bitwise or logical AND of integers.
@ Add
Sum of integers.
DWARFExpression::Operation Op
@ NearestTiesToEven
roundTiesToEven.
ArrayRef(const T &OneElt) -> ArrayRef< T >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.
Definition BitVector.h:881
#define N
#define NC
Definition regutils.h:42
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
constexpr uint64_t value() const
This is a hole in the type system and should not be abused.
Definition Alignment.h:82
Extended Value Type.
Definition ValueTypes.h:35
static EVT getVectorVT(LLVMContext &Context, EVT VT, unsigned NumElements, bool IsScalable=false)
Returns the EVT that represents a vector NumElements in length, where each element is of type VT.
Definition ValueTypes.h:70
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
Definition ValueTypes.h:411
uint64_t getScalarSizeInBits() const
Definition ValueTypes.h:423
MVT getSimpleVT() const
Return the SimpleValueType held in the specified simple EVT.
Definition ValueTypes.h:354
bool is128BitVector() const
Return true if this is a 128-bit vector type.
Definition ValueTypes.h:245
bool isVector() const
Return true if this is a vector value type.
Definition ValueTypes.h:191
EVT getVectorElementType() const
Given a vector type, return the type of each element.
Definition ValueTypes.h:366
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
Definition ValueTypes.h:374
bool is64BitVector() const
Return true if this is a 64-bit vector type.
Definition ValueTypes.h:240
static LLVM_ABI MachinePointerInfo getConstantPool(MachineFunction &MF)
Return a MachinePointerInfo record that refers to the constant pool.