1 //===-- ARMISelDAGToDAG.cpp - A dag to dag inst selector for ARM ----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines an instruction selector for the ARM target.
12 //===----------------------------------------------------------------------===//
15 #include "ARMBaseInstrInfo.h"
16 #include "ARMTargetMachine.h"
17 #include "MCTargetDesc/ARMAddressingModes.h"
18 #include "Utils/ARMBaseInfo.h"
19 #include "llvm/ADT/StringSwitch.h"
20 #include "llvm/CodeGen/MachineFrameInfo.h"
21 #include "llvm/CodeGen/MachineFunction.h"
22 #include "llvm/CodeGen/MachineInstrBuilder.h"
23 #include "llvm/CodeGen/MachineRegisterInfo.h"
24 #include "llvm/CodeGen/SelectionDAG.h"
25 #include "llvm/CodeGen/SelectionDAGISel.h"
26 #include "llvm/CodeGen/TargetLowering.h"
27 #include "llvm/IR/CallingConv.h"
28 #include "llvm/IR/Constants.h"
29 #include "llvm/IR/DerivedTypes.h"
30 #include "llvm/IR/Function.h"
31 #include "llvm/IR/Intrinsics.h"
32 #include "llvm/IR/LLVMContext.h"
33 #include "llvm/Support/CommandLine.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Target/TargetOptions.h"
40 #define DEBUG_TYPE "arm-isel"
43 DisableShifterOp("disable-shifter-op", cl::Hidden,
44 cl::desc("Disable isel of shifter-op"),
47 //===--------------------------------------------------------------------===//
48 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
49 /// instructions for SelectionDAG operations.
53 class ARMDAGToDAGISel : public SelectionDAGISel {
54 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
55 /// make the right decision when generating code for different targets.
56 const ARMSubtarget *Subtarget;
59 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm, CodeGenOpt::Level OptLevel)
60 : SelectionDAGISel(tm, OptLevel) {}
62 bool runOnMachineFunction(MachineFunction &MF) override {
63 // Reset the subtarget each time through.
64 Subtarget = &MF.getSubtarget<ARMSubtarget>();
65 SelectionDAGISel::runOnMachineFunction(MF);
69 StringRef getPassName() const override { return "ARM Instruction Selection"; }
71 void PreprocessISelDAG() override;
73 /// getI32Imm - Return a target constant of type i32 with the specified
75 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
76 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
79 void Select(SDNode *N) override;
81 bool hasNoVMLxHazardUse(SDNode *N) const;
82 bool isShifterOpProfitable(const SDValue &Shift,
83 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
84 bool SelectRegShifterOperand(SDValue N, SDValue &A,
85 SDValue &B, SDValue &C,
86 bool CheckProfitability = true);
87 bool SelectImmShifterOperand(SDValue N, SDValue &A,
88 SDValue &B, bool CheckProfitability = true);
89 bool SelectShiftRegShifterOperand(SDValue N, SDValue &A,
90 SDValue &B, SDValue &C) {
91 // Don't apply the profitability check
92 return SelectRegShifterOperand(N, A, B, C, false);
94 bool SelectShiftImmShifterOperand(SDValue N, SDValue &A,
96 // Don't apply the profitability check
97 return SelectImmShifterOperand(N, A, B, false);
100 bool SelectAddLikeOr(SDNode *Parent, SDValue N, SDValue &Out);
102 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
103 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
105 bool SelectCMOVPred(SDValue N, SDValue &Pred, SDValue &Reg) {
106 const ConstantSDNode *CN = cast<ConstantSDNode>(N);
107 Pred = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(N), MVT::i32);
108 Reg = CurDAG->getRegister(ARM::CPSR, MVT::i32);
112 bool SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
113 SDValue &Offset, SDValue &Opc);
114 bool SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
115 SDValue &Offset, SDValue &Opc);
116 bool SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
117 SDValue &Offset, SDValue &Opc);
118 bool SelectAddrOffsetNone(SDValue N, SDValue &Base);
119 bool SelectAddrMode3(SDValue N, SDValue &Base,
120 SDValue &Offset, SDValue &Opc);
121 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
122 SDValue &Offset, SDValue &Opc);
123 bool IsAddressingMode5(SDValue N, SDValue &Base, SDValue &Offset,
124 int Lwb, int Upb, bool FP16);
125 bool SelectAddrMode5(SDValue N, SDValue &Base, SDValue &Offset);
126 bool SelectAddrMode5FP16(SDValue N, SDValue &Base, SDValue &Offset);
127 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
128 bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset);
130 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
132 // Thumb Addressing Modes:
133 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
134 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
136 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
138 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
140 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
142 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
144 // Thumb 2 Addressing Modes:
145 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
146 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
148 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
150 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
151 SDValue &OffReg, SDValue &ShImm);
152 bool SelectT2AddrModeExclusive(SDValue N, SDValue &Base, SDValue &OffImm);
154 inline bool is_so_imm(unsigned Imm) const {
155 return ARM_AM::getSOImmVal(Imm) != -1;
158 inline bool is_so_imm_not(unsigned Imm) const {
159 return ARM_AM::getSOImmVal(~Imm) != -1;
162 inline bool is_t2_so_imm(unsigned Imm) const {
163 return ARM_AM::getT2SOImmVal(Imm) != -1;
166 inline bool is_t2_so_imm_not(unsigned Imm) const {
167 return ARM_AM::getT2SOImmVal(~Imm) != -1;
170 // Include the pieces autogenerated from the target description.
171 #include "ARMGenDAGISel.inc"
174 void transferMemOperands(SDNode *Src, SDNode *Dst);
176 /// Indexed (pre/post inc/dec) load matching code for ARM.
177 bool tryARMIndexedLoad(SDNode *N);
178 bool tryT1IndexedLoad(SDNode *N);
179 bool tryT2IndexedLoad(SDNode *N);
181 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
182 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
183 /// loads of D registers and even subregs and odd subregs of Q registers.
184 /// For NumVecs <= 2, QOpcodes1 is not used.
185 void SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
186 const uint16_t *DOpcodes, const uint16_t *QOpcodes0,
187 const uint16_t *QOpcodes1);
189 /// SelectVST - Select NEON store intrinsics. NumVecs should
190 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
191 /// stores of D registers and even subregs and odd subregs of Q registers.
192 /// For NumVecs <= 2, QOpcodes1 is not used.
193 void SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
194 const uint16_t *DOpcodes, const uint16_t *QOpcodes0,
195 const uint16_t *QOpcodes1);
197 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
198 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
199 /// load/store of D registers and Q registers.
200 void SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
201 unsigned NumVecs, const uint16_t *DOpcodes,
202 const uint16_t *QOpcodes);
204 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs
205 /// should be 1, 2, 3 or 4. The opcode array specifies the instructions used
206 /// for loading D registers.
207 void SelectVLDDup(SDNode *N, bool IsIntrinsic, bool isUpdating,
208 unsigned NumVecs, const uint16_t *DOpcodes,
209 const uint16_t *QOpcodes0 = nullptr,
210 const uint16_t *QOpcodes1 = nullptr);
212 /// Try to select SBFX/UBFX instructions for ARM.
213 bool tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
215 // Select special operations if node forms integer ABS pattern
216 bool tryABSOp(SDNode *N);
218 bool tryReadRegister(SDNode *N);
219 bool tryWriteRegister(SDNode *N);
221 bool tryInlineAsm(SDNode *N);
223 void SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI);
225 void SelectCMP_SWAP(SDNode *N);
227 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
228 /// inline asm expressions.
229 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
230 std::vector<SDValue> &OutOps) override;
232 // Form pairs of consecutive R, S, D, or Q registers.
233 SDNode *createGPRPairNode(EVT VT, SDValue V0, SDValue V1);
234 SDNode *createSRegPairNode(EVT VT, SDValue V0, SDValue V1);
235 SDNode *createDRegPairNode(EVT VT, SDValue V0, SDValue V1);
236 SDNode *createQRegPairNode(EVT VT, SDValue V0, SDValue V1);
238 // Form sequences of 4 consecutive S, D, or Q registers.
239 SDNode *createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
240 SDNode *createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
241 SDNode *createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
243 // Get the alignment operand for a NEON VLD or VST instruction.
244 SDValue GetVLDSTAlign(SDValue Align, const SDLoc &dl, unsigned NumVecs,
247 /// Returns the number of instructions required to materialize the given
248 /// constant in a register, or 3 if a literal pool load is needed.
249 unsigned ConstantMaterializationCost(unsigned Val) const;
251 /// Checks if N is a multiplication by a constant where we can extract out a
252 /// power of two from the constant so that it can be used in a shift, but only
253 /// if it simplifies the materialization of the constant. Returns true if it
254 /// is, and assigns to PowerOfTwo the power of two that should be extracted
255 /// out and to NewMulConst the new constant to be multiplied by.
256 bool canExtractShiftFromMul(const SDValue &N, unsigned MaxShift,
257 unsigned &PowerOfTwo, SDValue &NewMulConst) const;
259 /// Replace N with M in CurDAG, in a way that also ensures that M gets
260 /// selected when N would have been selected.
261 void replaceDAGValue(const SDValue &N, SDValue M);
265 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
266 /// operand. If so Imm will receive the 32-bit value.
267 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
268 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
269 Imm = cast<ConstantSDNode>(N)->getZExtValue();
275 // isInt32Immediate - This method tests to see if a constant operand.
276 // If so Imm will receive the 32 bit value.
277 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
278 return isInt32Immediate(N.getNode(), Imm);
281 // isOpcWithIntImmediate - This method tests to see if the node is a specific
282 // opcode and that it has a immediate integer right operand.
283 // If so Imm will receive the 32 bit value.
284 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
285 return N->getOpcode() == Opc &&
286 isInt32Immediate(N->getOperand(1).getNode(), Imm);
289 /// Check whether a particular node is a constant value representable as
290 /// (N * Scale) where (N in [\p RangeMin, \p RangeMax).
292 /// \param ScaledConstant [out] - On success, the pre-scaled constant value.
293 static bool isScaledConstantInRange(SDValue Node, int Scale,
294 int RangeMin, int RangeMax,
295 int &ScaledConstant) {
296 assert(Scale > 0 && "Invalid scale!");
298 // Check that this is a constant.
299 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node);
303 ScaledConstant = (int) C->getZExtValue();
304 if ((ScaledConstant % Scale) != 0)
307 ScaledConstant /= Scale;
308 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
311 void ARMDAGToDAGISel::PreprocessISelDAG() {
312 if (!Subtarget->hasV6T2Ops())
315 bool isThumb2 = Subtarget->isThumb();
316 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
317 E = CurDAG->allnodes_end(); I != E; ) {
318 SDNode *N = &*I++; // Preincrement iterator to avoid invalidation issues.
320 if (N->getOpcode() != ISD::ADD)
323 // Look for (add X1, (and (srl X2, c1), c2)) where c2 is constant with
324 // leading zeros, followed by consecutive set bits, followed by 1 or 2
325 // trailing zeros, e.g. 1020.
326 // Transform the expression to
327 // (add X1, (shl (and (srl X2, c1), (c2>>tz)), tz)) where tz is the number
328 // of trailing zeros of c2. The left shift would be folded as an shifter
329 // operand of 'add' and the 'and' and 'srl' would become a bits extraction
332 SDValue N0 = N->getOperand(0);
333 SDValue N1 = N->getOperand(1);
334 unsigned And_imm = 0;
335 if (!isOpcWithIntImmediate(N1.getNode(), ISD::AND, And_imm)) {
336 if (isOpcWithIntImmediate(N0.getNode(), ISD::AND, And_imm))
342 // Check if the AND mask is an immediate of the form: 000.....1111111100
343 unsigned TZ = countTrailingZeros(And_imm);
344 if (TZ != 1 && TZ != 2)
345 // Be conservative here. Shifter operands aren't always free. e.g. On
346 // Swift, left shifter operand of 1 / 2 for free but others are not.
348 // ubfx r3, r1, #16, #8
349 // ldr.w r3, [r0, r3, lsl #2]
352 // and.w r2, r9, r1, lsr #14
356 if (And_imm & (And_imm + 1))
359 // Look for (and (srl X, c1), c2).
360 SDValue Srl = N1.getOperand(0);
361 unsigned Srl_imm = 0;
362 if (!isOpcWithIntImmediate(Srl.getNode(), ISD::SRL, Srl_imm) ||
366 // Make sure first operand is not a shifter operand which would prevent
367 // folding of the left shift.
372 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1))
375 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1) ||
376 SelectRegShifterOperand(N0, CPTmp0, CPTmp1, CPTmp2))
380 // Now make the transformation.
381 Srl = CurDAG->getNode(ISD::SRL, SDLoc(Srl), MVT::i32,
383 CurDAG->getConstant(Srl_imm + TZ, SDLoc(Srl),
385 N1 = CurDAG->getNode(ISD::AND, SDLoc(N1), MVT::i32,
387 CurDAG->getConstant(And_imm, SDLoc(Srl), MVT::i32));
388 N1 = CurDAG->getNode(ISD::SHL, SDLoc(N1), MVT::i32,
389 N1, CurDAG->getConstant(TZ, SDLoc(Srl), MVT::i32));
390 CurDAG->UpdateNodeOperands(N, N0, N1);
394 /// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
395 /// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
396 /// least on current ARM implementations) which should be avoidded.
397 bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
398 if (OptLevel == CodeGenOpt::None)
401 if (!Subtarget->hasVMLxHazards())
407 SDNode *Use = *N->use_begin();
408 if (Use->getOpcode() == ISD::CopyToReg)
410 if (Use->isMachineOpcode()) {
411 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
412 CurDAG->getSubtarget().getInstrInfo());
414 const MCInstrDesc &MCID = TII->get(Use->getMachineOpcode());
417 unsigned Opcode = MCID.getOpcode();
418 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
420 // vmlx feeding into another vmlx. We actually want to unfold
421 // the use later in the MLxExpansion pass. e.g.
423 // vmla (stall 8 cycles)
428 // This adds up to about 18 - 19 cycles.
431 // vmul (stall 4 cycles)
432 // vadd adds up to about 14 cycles.
433 return TII->isFpMLxInstruction(Opcode);
439 bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
440 ARM_AM::ShiftOpc ShOpcVal,
442 if (!Subtarget->isLikeA9() && !Subtarget->isSwift())
444 if (Shift.hasOneUse())
447 return ShOpcVal == ARM_AM::lsl &&
448 (ShAmt == 2 || (Subtarget->isSwift() && ShAmt == 1));
451 unsigned ARMDAGToDAGISel::ConstantMaterializationCost(unsigned Val) const {
452 if (Subtarget->isThumb()) {
453 if (Val <= 255) return 1; // MOV
454 if (Subtarget->hasV6T2Ops() &&
455 (Val <= 0xffff || ARM_AM::getT2SOImmValSplatVal(Val) != -1))
457 if (Val <= 510) return 2; // MOV + ADDi8
458 if (~Val <= 255) return 2; // MOV + MVN
459 if (ARM_AM::isThumbImmShiftedVal(Val)) return 2; // MOV + LSL
461 if (ARM_AM::getSOImmVal(Val) != -1) return 1; // MOV
462 if (ARM_AM::getSOImmVal(~Val) != -1) return 1; // MVN
463 if (Subtarget->hasV6T2Ops() && Val <= 0xffff) return 1; // MOVW
464 if (ARM_AM::isSOImmTwoPartVal(Val)) return 2; // two instrs
466 if (Subtarget->useMovt(*MF)) return 2; // MOVW + MOVT
467 return 3; // Literal pool load
470 bool ARMDAGToDAGISel::canExtractShiftFromMul(const SDValue &N,
472 unsigned &PowerOfTwo,
473 SDValue &NewMulConst) const {
474 assert(N.getOpcode() == ISD::MUL);
475 assert(MaxShift > 0);
477 // If the multiply is used in more than one place then changing the constant
478 // will make other uses incorrect, so don't.
479 if (!N.hasOneUse()) return false;
480 // Check if the multiply is by a constant
481 ConstantSDNode *MulConst = dyn_cast<ConstantSDNode>(N.getOperand(1));
482 if (!MulConst) return false;
483 // If the constant is used in more than one place then modifying it will mean
484 // we need to materialize two constants instead of one, which is a bad idea.
485 if (!MulConst->hasOneUse()) return false;
486 unsigned MulConstVal = MulConst->getZExtValue();
487 if (MulConstVal == 0) return false;
489 // Find the largest power of 2 that MulConstVal is a multiple of
490 PowerOfTwo = MaxShift;
491 while ((MulConstVal % (1 << PowerOfTwo)) != 0) {
493 if (PowerOfTwo == 0) return false;
496 // Only optimise if the new cost is better
497 unsigned NewMulConstVal = MulConstVal / (1 << PowerOfTwo);
498 NewMulConst = CurDAG->getConstant(NewMulConstVal, SDLoc(N), MVT::i32);
499 unsigned OldCost = ConstantMaterializationCost(MulConstVal);
500 unsigned NewCost = ConstantMaterializationCost(NewMulConstVal);
501 return NewCost < OldCost;
504 void ARMDAGToDAGISel::replaceDAGValue(const SDValue &N, SDValue M) {
505 CurDAG->RepositionNode(N.getNode()->getIterator(), M.getNode());
509 bool ARMDAGToDAGISel::SelectImmShifterOperand(SDValue N,
512 bool CheckProfitability) {
513 if (DisableShifterOp)
516 // If N is a multiply-by-constant and it's profitable to extract a shift and
517 // use it in a shifted operand do so.
518 if (N.getOpcode() == ISD::MUL) {
519 unsigned PowerOfTwo = 0;
521 if (canExtractShiftFromMul(N, 31, PowerOfTwo, NewMulConst)) {
522 HandleSDNode Handle(N);
524 replaceDAGValue(N.getOperand(1), NewMulConst);
525 BaseReg = Handle.getValue();
526 Opc = CurDAG->getTargetConstant(
527 ARM_AM::getSORegOpc(ARM_AM::lsl, PowerOfTwo), Loc, MVT::i32);
532 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
534 // Don't match base register only case. That is matched to a separate
535 // lower complexity pattern with explicit register operand.
536 if (ShOpcVal == ARM_AM::no_shift) return false;
538 BaseReg = N.getOperand(0);
539 unsigned ShImmVal = 0;
540 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
541 if (!RHS) return false;
542 ShImmVal = RHS->getZExtValue() & 31;
543 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
548 bool ARMDAGToDAGISel::SelectRegShifterOperand(SDValue N,
552 bool CheckProfitability) {
553 if (DisableShifterOp)
556 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
558 // Don't match base register only case. That is matched to a separate
559 // lower complexity pattern with explicit register operand.
560 if (ShOpcVal == ARM_AM::no_shift) return false;
562 BaseReg = N.getOperand(0);
563 unsigned ShImmVal = 0;
564 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
565 if (RHS) return false;
567 ShReg = N.getOperand(1);
568 if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal))
570 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
575 // Determine whether an ISD::OR's operands are suitable to turn the operation
576 // into an addition, which often has more compact encodings.
577 bool ARMDAGToDAGISel::SelectAddLikeOr(SDNode *Parent, SDValue N, SDValue &Out) {
578 assert(Parent->getOpcode() == ISD::OR && "unexpected parent");
580 return CurDAG->haveNoCommonBitsSet(N, Parent->getOperand(1));
584 bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
587 // Match simple R + imm12 operands.
590 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
591 !CurDAG->isBaseWithConstantOffset(N)) {
592 if (N.getOpcode() == ISD::FrameIndex) {
593 // Match frame index.
594 int FI = cast<FrameIndexSDNode>(N)->getIndex();
595 Base = CurDAG->getTargetFrameIndex(
596 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
597 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
601 if (N.getOpcode() == ARMISD::Wrapper &&
602 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
603 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
604 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
605 Base = N.getOperand(0);
608 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
612 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
613 int RHSC = (int)RHS->getSExtValue();
614 if (N.getOpcode() == ISD::SUB)
617 if (RHSC > -0x1000 && RHSC < 0x1000) { // 12 bits
618 Base = N.getOperand(0);
619 if (Base.getOpcode() == ISD::FrameIndex) {
620 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
621 Base = CurDAG->getTargetFrameIndex(
622 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
624 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
631 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
637 bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
639 if (N.getOpcode() == ISD::MUL &&
640 ((!Subtarget->isLikeA9() && !Subtarget->isSwift()) || N.hasOneUse())) {
641 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
642 // X * [3,5,9] -> X + X * [2,4,8] etc.
643 int RHSC = (int)RHS->getZExtValue();
646 ARM_AM::AddrOpc AddSub = ARM_AM::add;
648 AddSub = ARM_AM::sub;
651 if (isPowerOf2_32(RHSC)) {
652 unsigned ShAmt = Log2_32(RHSC);
653 Base = Offset = N.getOperand(0);
654 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
663 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
664 // ISD::OR that is equivalent to an ISD::ADD.
665 !CurDAG->isBaseWithConstantOffset(N))
668 // Leave simple R +/- imm12 operands for LDRi12
669 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
671 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
672 -0x1000+1, 0x1000, RHSC)) // 12 bits.
676 // Otherwise this is R +/- [possibly shifted] R.
677 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::SUB ? ARM_AM::sub:ARM_AM::add;
678 ARM_AM::ShiftOpc ShOpcVal =
679 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
682 Base = N.getOperand(0);
683 Offset = N.getOperand(1);
685 if (ShOpcVal != ARM_AM::no_shift) {
686 // Check to see if the RHS of the shift is a constant, if not, we can't fold
688 if (ConstantSDNode *Sh =
689 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
690 ShAmt = Sh->getZExtValue();
691 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
692 Offset = N.getOperand(1).getOperand(0);
695 ShOpcVal = ARM_AM::no_shift;
698 ShOpcVal = ARM_AM::no_shift;
702 // Try matching (R shl C) + (R).
703 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
704 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
705 N.getOperand(0).hasOneUse())) {
706 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
707 if (ShOpcVal != ARM_AM::no_shift) {
708 // Check to see if the RHS of the shift is a constant, if not, we can't
710 if (ConstantSDNode *Sh =
711 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
712 ShAmt = Sh->getZExtValue();
713 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
714 Offset = N.getOperand(0).getOperand(0);
715 Base = N.getOperand(1);
718 ShOpcVal = ARM_AM::no_shift;
721 ShOpcVal = ARM_AM::no_shift;
726 // If Offset is a multiply-by-constant and it's profitable to extract a shift
727 // and use it in a shifted operand do so.
728 if (Offset.getOpcode() == ISD::MUL && N.hasOneUse()) {
729 unsigned PowerOfTwo = 0;
731 if (canExtractShiftFromMul(Offset, 31, PowerOfTwo, NewMulConst)) {
732 HandleSDNode Handle(Offset);
733 replaceDAGValue(Offset.getOperand(1), NewMulConst);
734 Offset = Handle.getValue();
736 ShOpcVal = ARM_AM::lsl;
740 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
745 bool ARMDAGToDAGISel::SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
746 SDValue &Offset, SDValue &Opc) {
747 unsigned Opcode = Op->getOpcode();
748 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
749 ? cast<LoadSDNode>(Op)->getAddressingMode()
750 : cast<StoreSDNode>(Op)->getAddressingMode();
751 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
752 ? ARM_AM::add : ARM_AM::sub;
754 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val))
758 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
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
763 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
764 ShAmt = Sh->getZExtValue();
765 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
766 Offset = N.getOperand(0);
769 ShOpcVal = ARM_AM::no_shift;
772 ShOpcVal = ARM_AM::no_shift;
776 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
781 bool ARMDAGToDAGISel::SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
782 SDValue &Offset, SDValue &Opc) {
783 unsigned Opcode = Op->getOpcode();
784 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
785 ? cast<LoadSDNode>(Op)->getAddressingMode()
786 : cast<StoreSDNode>(Op)->getAddressingMode();
787 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
788 ? ARM_AM::add : ARM_AM::sub;
790 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
791 if (AddSub == ARM_AM::sub) Val *= -1;
792 Offset = CurDAG->getRegister(0, MVT::i32);
793 Opc = CurDAG->getTargetConstant(Val, SDLoc(Op), MVT::i32);
801 bool ARMDAGToDAGISel::SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
802 SDValue &Offset, SDValue &Opc) {
803 unsigned Opcode = Op->getOpcode();
804 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
805 ? cast<LoadSDNode>(Op)->getAddressingMode()
806 : cast<StoreSDNode>(Op)->getAddressingMode();
807 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
808 ? ARM_AM::add : ARM_AM::sub;
810 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
811 Offset = CurDAG->getRegister(0, MVT::i32);
812 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
814 SDLoc(Op), MVT::i32);
821 bool ARMDAGToDAGISel::SelectAddrOffsetNone(SDValue N, SDValue &Base) {
826 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
827 SDValue &Base, SDValue &Offset,
829 if (N.getOpcode() == ISD::SUB) {
830 // X - C is canonicalize to X + -C, no need to handle it here.
831 Base = N.getOperand(0);
832 Offset = N.getOperand(1);
833 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0), SDLoc(N),
838 if (!CurDAG->isBaseWithConstantOffset(N)) {
840 if (N.getOpcode() == ISD::FrameIndex) {
841 int FI = cast<FrameIndexSDNode>(N)->getIndex();
842 Base = CurDAG->getTargetFrameIndex(
843 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
845 Offset = CurDAG->getRegister(0, MVT::i32);
846 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
851 // If the RHS is +/- imm8, fold into addr mode.
853 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
854 -256 + 1, 256, RHSC)) { // 8 bits.
855 Base = N.getOperand(0);
856 if (Base.getOpcode() == ISD::FrameIndex) {
857 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
858 Base = CurDAG->getTargetFrameIndex(
859 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
861 Offset = CurDAG->getRegister(0, MVT::i32);
863 ARM_AM::AddrOpc AddSub = ARM_AM::add;
865 AddSub = ARM_AM::sub;
868 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC), SDLoc(N),
873 Base = N.getOperand(0);
874 Offset = N.getOperand(1);
875 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
880 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
881 SDValue &Offset, SDValue &Opc) {
882 unsigned Opcode = Op->getOpcode();
883 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
884 ? cast<LoadSDNode>(Op)->getAddressingMode()
885 : cast<StoreSDNode>(Op)->getAddressingMode();
886 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
887 ? ARM_AM::add : ARM_AM::sub;
889 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
890 Offset = CurDAG->getRegister(0, MVT::i32);
891 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), SDLoc(Op),
897 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), SDLoc(Op),
902 bool ARMDAGToDAGISel::IsAddressingMode5(SDValue N, SDValue &Base, SDValue &Offset,
903 int Lwb, int Upb, bool FP16) {
904 if (!CurDAG->isBaseWithConstantOffset(N)) {
906 if (N.getOpcode() == ISD::FrameIndex) {
907 int FI = cast<FrameIndexSDNode>(N)->getIndex();
908 Base = CurDAG->getTargetFrameIndex(
909 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
910 } else if (N.getOpcode() == ARMISD::Wrapper &&
911 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
912 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
913 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
914 Base = N.getOperand(0);
916 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
921 // If the RHS is +/- imm8, fold into addr mode.
923 const int Scale = FP16 ? 2 : 4;
925 if (isScaledConstantInRange(N.getOperand(1), Scale, Lwb, Upb, RHSC)) {
926 Base = N.getOperand(0);
927 if (Base.getOpcode() == ISD::FrameIndex) {
928 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
929 Base = CurDAG->getTargetFrameIndex(
930 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
933 ARM_AM::AddrOpc AddSub = ARM_AM::add;
935 AddSub = ARM_AM::sub;
940 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5FP16Opc(AddSub, RHSC),
943 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
952 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5FP16Opc(ARM_AM::add, 0),
955 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
961 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
962 SDValue &Base, SDValue &Offset) {
965 return IsAddressingMode5(N, Base, Offset, Lwb, Upb, /*FP16=*/ false);
968 bool ARMDAGToDAGISel::SelectAddrMode5FP16(SDValue N,
969 SDValue &Base, SDValue &Offset) {
972 return IsAddressingMode5(N, Base, Offset, Lwb, Upb, /*FP16=*/ true);
975 bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
979 unsigned Alignment = 0;
981 MemSDNode *MemN = cast<MemSDNode>(Parent);
983 if (isa<LSBaseSDNode>(MemN) ||
984 ((MemN->getOpcode() == ARMISD::VST1_UPD ||
985 MemN->getOpcode() == ARMISD::VLD1_UPD) &&
986 MemN->getConstantOperandVal(MemN->getNumOperands() - 1) == 1)) {
987 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
988 // The maximum alignment is equal to the memory size being referenced.
989 unsigned MMOAlign = MemN->getAlignment();
990 unsigned MemSize = MemN->getMemoryVT().getSizeInBits() / 8;
991 if (MMOAlign >= MemSize && MemSize > 1)
994 // All other uses of addrmode6 are for intrinsics. For now just record
995 // the raw alignment value; it will be refined later based on the legal
996 // alignment operands for the intrinsic.
997 Alignment = MemN->getAlignment();
1000 Align = CurDAG->getTargetConstant(Alignment, SDLoc(N), MVT::i32);
1004 bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N,
1006 LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op);
1007 ISD::MemIndexedMode AM = LdSt->getAddressingMode();
1008 if (AM != ISD::POST_INC)
1011 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) {
1012 if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits())
1013 Offset = CurDAG->getRegister(0, MVT::i32);
1018 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
1019 SDValue &Offset, SDValue &Label) {
1020 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
1021 Offset = N.getOperand(0);
1022 SDValue N1 = N.getOperand(1);
1023 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
1024 SDLoc(N), MVT::i32);
1032 //===----------------------------------------------------------------------===//
1033 // Thumb Addressing Modes
1034 //===----------------------------------------------------------------------===//
1036 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
1037 SDValue &Base, SDValue &Offset){
1038 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) {
1039 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
1040 if (!NC || !NC->isNullValue())
1047 Base = N.getOperand(0);
1048 Offset = N.getOperand(1);
1053 ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
1054 SDValue &Base, SDValue &OffImm) {
1055 if (!CurDAG->isBaseWithConstantOffset(N)) {
1056 if (N.getOpcode() == ISD::ADD) {
1057 return false; // We want to select register offset instead
1058 } else if (N.getOpcode() == ARMISD::Wrapper &&
1059 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1060 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1061 N.getOperand(0).getOpcode() != ISD::TargetConstantPool &&
1062 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1063 Base = N.getOperand(0);
1068 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1072 // If the RHS is + imm5 * scale, fold into addr mode.
1074 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1075 Base = N.getOperand(0);
1076 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1080 // Offset is too large, so use register offset instead.
1085 ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1087 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
1091 ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1093 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
1097 ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1099 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
1102 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1103 SDValue &Base, SDValue &OffImm) {
1104 if (N.getOpcode() == ISD::FrameIndex) {
1105 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1106 // Only multiples of 4 are allowed for the offset, so the frame object
1107 // alignment must be at least 4.
1108 MachineFrameInfo &MFI = MF->getFrameInfo();
1109 if (MFI.getObjectAlignment(FI) < 4)
1110 MFI.setObjectAlignment(FI, 4);
1111 Base = CurDAG->getTargetFrameIndex(
1112 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1113 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1117 if (!CurDAG->isBaseWithConstantOffset(N))
1120 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1121 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
1122 (LHSR && LHSR->getReg() == ARM::SP)) {
1123 // If the RHS is + imm8 * scale, fold into addr mode.
1125 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1126 Base = N.getOperand(0);
1127 if (Base.getOpcode() == ISD::FrameIndex) {
1128 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1129 // For LHS+RHS to result in an offset that's a multiple of 4 the object
1130 // indexed by the LHS must be 4-byte aligned.
1131 MachineFrameInfo &MFI = MF->getFrameInfo();
1132 if (MFI.getObjectAlignment(FI) < 4)
1133 MFI.setObjectAlignment(FI, 4);
1134 Base = CurDAG->getTargetFrameIndex(
1135 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1137 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1146 //===----------------------------------------------------------------------===//
1147 // Thumb 2 Addressing Modes
1148 //===----------------------------------------------------------------------===//
1151 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
1152 SDValue &Base, SDValue &OffImm) {
1153 // Match simple R + imm12 operands.
1156 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1157 !CurDAG->isBaseWithConstantOffset(N)) {
1158 if (N.getOpcode() == ISD::FrameIndex) {
1159 // Match frame index.
1160 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1161 Base = CurDAG->getTargetFrameIndex(
1162 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1163 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1167 if (N.getOpcode() == ARMISD::Wrapper &&
1168 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1169 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1170 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1171 Base = N.getOperand(0);
1172 if (Base.getOpcode() == ISD::TargetConstantPool)
1173 return false; // We want to select t2LDRpci instead.
1176 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1180 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1181 if (SelectT2AddrModeImm8(N, Base, OffImm))
1182 // Let t2LDRi8 handle (R - imm8).
1185 int RHSC = (int)RHS->getZExtValue();
1186 if (N.getOpcode() == ISD::SUB)
1189 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
1190 Base = N.getOperand(0);
1191 if (Base.getOpcode() == ISD::FrameIndex) {
1192 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1193 Base = CurDAG->getTargetFrameIndex(
1194 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1196 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1203 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1207 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
1208 SDValue &Base, SDValue &OffImm) {
1209 // Match simple R - imm8 operands.
1210 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1211 !CurDAG->isBaseWithConstantOffset(N))
1214 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1215 int RHSC = (int)RHS->getSExtValue();
1216 if (N.getOpcode() == ISD::SUB)
1219 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1220 Base = N.getOperand(0);
1221 if (Base.getOpcode() == ISD::FrameIndex) {
1222 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1223 Base = CurDAG->getTargetFrameIndex(
1224 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1226 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1234 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
1236 unsigned Opcode = Op->getOpcode();
1237 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1238 ? cast<LoadSDNode>(Op)->getAddressingMode()
1239 : cast<StoreSDNode>(Op)->getAddressingMode();
1241 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1242 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1243 ? CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32)
1244 : CurDAG->getTargetConstant(-RHSC, SDLoc(N), MVT::i32);
1251 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
1253 SDValue &OffReg, SDValue &ShImm) {
1254 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
1255 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N))
1258 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1259 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1260 int RHSC = (int)RHS->getZExtValue();
1261 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1263 else if (RHSC < 0 && RHSC >= -255) // 8 bits
1267 // Look for (R + R) or (R + (R << [1,2,3])).
1269 Base = N.getOperand(0);
1270 OffReg = N.getOperand(1);
1272 // Swap if it is ((R << c) + R).
1273 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg.getOpcode());
1274 if (ShOpcVal != ARM_AM::lsl) {
1275 ShOpcVal = ARM_AM::getShiftOpcForNode(Base.getOpcode());
1276 if (ShOpcVal == ARM_AM::lsl)
1277 std::swap(Base, OffReg);
1280 if (ShOpcVal == ARM_AM::lsl) {
1281 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1283 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1284 ShAmt = Sh->getZExtValue();
1285 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1286 OffReg = OffReg.getOperand(0);
1293 // If OffReg is a multiply-by-constant and it's profitable to extract a shift
1294 // and use it in a shifted operand do so.
1295 if (OffReg.getOpcode() == ISD::MUL && N.hasOneUse()) {
1296 unsigned PowerOfTwo = 0;
1297 SDValue NewMulConst;
1298 if (canExtractShiftFromMul(OffReg, 3, PowerOfTwo, NewMulConst)) {
1299 HandleSDNode Handle(OffReg);
1300 replaceDAGValue(OffReg.getOperand(1), NewMulConst);
1301 OffReg = Handle.getValue();
1306 ShImm = CurDAG->getTargetConstant(ShAmt, SDLoc(N), MVT::i32);
1311 bool ARMDAGToDAGISel::SelectT2AddrModeExclusive(SDValue N, SDValue &Base,
1313 // This *must* succeed since it's used for the irreplaceable ldrex and strex
1316 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1318 if (N.getOpcode() != ISD::ADD || !CurDAG->isBaseWithConstantOffset(N))
1321 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1325 uint32_t RHSC = (int)RHS->getZExtValue();
1326 if (RHSC > 1020 || RHSC % 4 != 0)
1329 Base = N.getOperand(0);
1330 if (Base.getOpcode() == ISD::FrameIndex) {
1331 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1332 Base = CurDAG->getTargetFrameIndex(
1333 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1336 OffImm = CurDAG->getTargetConstant(RHSC/4, SDLoc(N), MVT::i32);
1340 //===--------------------------------------------------------------------===//
1342 /// getAL - Returns a ARMCC::AL immediate node.
1343 static inline SDValue getAL(SelectionDAG *CurDAG, const SDLoc &dl) {
1344 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, dl, MVT::i32);
1347 void ARMDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
1348 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1349 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
1350 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
1353 bool ARMDAGToDAGISel::tryARMIndexedLoad(SDNode *N) {
1354 LoadSDNode *LD = cast<LoadSDNode>(N);
1355 ISD::MemIndexedMode AM = LD->getAddressingMode();
1356 if (AM == ISD::UNINDEXED)
1359 EVT LoadedVT = LD->getMemoryVT();
1360 SDValue Offset, AMOpc;
1361 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1362 unsigned Opcode = 0;
1364 if (LoadedVT == MVT::i32 && isPre &&
1365 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1366 Opcode = ARM::LDR_PRE_IMM;
1368 } else if (LoadedVT == MVT::i32 && !isPre &&
1369 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1370 Opcode = ARM::LDR_POST_IMM;
1372 } else if (LoadedVT == MVT::i32 &&
1373 SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1374 Opcode = isPre ? ARM::LDR_PRE_REG : ARM::LDR_POST_REG;
1377 } else if (LoadedVT == MVT::i16 &&
1378 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1380 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1381 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1382 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
1383 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
1384 if (LD->getExtensionType() == ISD::SEXTLOAD) {
1385 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1387 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1391 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1393 Opcode = ARM::LDRB_PRE_IMM;
1394 } else if (!isPre &&
1395 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1397 Opcode = ARM::LDRB_POST_IMM;
1398 } else if (SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1400 Opcode = isPre ? ARM::LDRB_PRE_REG : ARM::LDRB_POST_REG;
1406 if (Opcode == ARM::LDR_PRE_IMM || Opcode == ARM::LDRB_PRE_IMM) {
1407 SDValue Chain = LD->getChain();
1408 SDValue Base = LD->getBasePtr();
1409 SDValue Ops[]= { Base, AMOpc, getAL(CurDAG, SDLoc(N)),
1410 CurDAG->getRegister(0, MVT::i32), Chain };
1411 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1413 transferMemOperands(N, New);
1414 ReplaceNode(N, New);
1417 SDValue Chain = LD->getChain();
1418 SDValue Base = LD->getBasePtr();
1419 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG, SDLoc(N)),
1420 CurDAG->getRegister(0, MVT::i32), Chain };
1421 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1423 transferMemOperands(N, New);
1424 ReplaceNode(N, New);
1432 bool ARMDAGToDAGISel::tryT1IndexedLoad(SDNode *N) {
1433 LoadSDNode *LD = cast<LoadSDNode>(N);
1434 EVT LoadedVT = LD->getMemoryVT();
1435 ISD::MemIndexedMode AM = LD->getAddressingMode();
1436 if (AM != ISD::POST_INC || LD->getExtensionType() != ISD::NON_EXTLOAD ||
1437 LoadedVT.getSimpleVT().SimpleTy != MVT::i32)
1440 auto *COffs = dyn_cast<ConstantSDNode>(LD->getOffset());
1441 if (!COffs || COffs->getZExtValue() != 4)
1444 // A T1 post-indexed load is just a single register LDM: LDM r0!, {r1}.
1445 // The encoding of LDM is not how the rest of ISel expects a post-inc load to
1446 // look however, so we use a pseudo here and switch it for a tLDMIA_UPD after
1448 SDValue Chain = LD->getChain();
1449 SDValue Base = LD->getBasePtr();
1450 SDValue Ops[]= { Base, getAL(CurDAG, SDLoc(N)),
1451 CurDAG->getRegister(0, MVT::i32), Chain };
1452 SDNode *New = CurDAG->getMachineNode(ARM::tLDR_postidx, SDLoc(N), MVT::i32,
1453 MVT::i32, MVT::Other, Ops);
1454 transferMemOperands(N, New);
1455 ReplaceNode(N, New);
1459 bool ARMDAGToDAGISel::tryT2IndexedLoad(SDNode *N) {
1460 LoadSDNode *LD = cast<LoadSDNode>(N);
1461 ISD::MemIndexedMode AM = LD->getAddressingMode();
1462 if (AM == ISD::UNINDEXED)
1465 EVT LoadedVT = LD->getMemoryVT();
1466 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
1468 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1469 unsigned Opcode = 0;
1471 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
1472 switch (LoadedVT.getSimpleVT().SimpleTy) {
1474 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1478 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1480 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
1485 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1487 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
1496 SDValue Chain = LD->getChain();
1497 SDValue Base = LD->getBasePtr();
1498 SDValue Ops[]= { Base, Offset, getAL(CurDAG, SDLoc(N)),
1499 CurDAG->getRegister(0, MVT::i32), Chain };
1500 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1502 transferMemOperands(N, New);
1503 ReplaceNode(N, New);
1510 /// Form a GPRPair pseudo register from a pair of GPR regs.
1511 SDNode *ARMDAGToDAGISel::createGPRPairNode(EVT VT, SDValue V0, SDValue V1) {
1512 SDLoc dl(V0.getNode());
1514 CurDAG->getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
1515 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
1516 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
1517 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1518 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1521 /// Form a D register from a pair of S registers.
1522 SDNode *ARMDAGToDAGISel::createSRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1523 SDLoc dl(V0.getNode());
1525 CurDAG->getTargetConstant(ARM::DPR_VFP2RegClassID, dl, MVT::i32);
1526 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1527 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1528 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1529 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1532 /// Form a quad register from a pair of D registers.
1533 SDNode *ARMDAGToDAGISel::createDRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1534 SDLoc dl(V0.getNode());
1535 SDValue RegClass = CurDAG->getTargetConstant(ARM::QPRRegClassID, dl,
1537 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1538 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1539 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1540 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1543 /// Form 4 consecutive D registers from a pair of Q registers.
1544 SDNode *ARMDAGToDAGISel::createQRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1545 SDLoc dl(V0.getNode());
1546 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1548 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1549 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1550 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1551 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1554 /// Form 4 consecutive S registers.
1555 SDNode *ARMDAGToDAGISel::createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1,
1556 SDValue V2, SDValue V3) {
1557 SDLoc dl(V0.getNode());
1559 CurDAG->getTargetConstant(ARM::QPR_VFP2RegClassID, dl, MVT::i32);
1560 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1561 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1562 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, dl, MVT::i32);
1563 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, dl, MVT::i32);
1564 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1565 V2, SubReg2, V3, SubReg3 };
1566 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1569 /// Form 4 consecutive D registers.
1570 SDNode *ARMDAGToDAGISel::createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1,
1571 SDValue V2, SDValue V3) {
1572 SDLoc dl(V0.getNode());
1573 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1575 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1576 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1577 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, dl, MVT::i32);
1578 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, dl, MVT::i32);
1579 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1580 V2, SubReg2, V3, SubReg3 };
1581 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1584 /// Form 4 consecutive Q registers.
1585 SDNode *ARMDAGToDAGISel::createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1,
1586 SDValue V2, SDValue V3) {
1587 SDLoc dl(V0.getNode());
1588 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQQQPRRegClassID, dl,
1590 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1591 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1592 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, dl, MVT::i32);
1593 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, dl, MVT::i32);
1594 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1595 V2, SubReg2, V3, SubReg3 };
1596 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1599 /// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1600 /// of a NEON VLD or VST instruction. The supported values depend on the
1601 /// number of registers being loaded.
1602 SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, const SDLoc &dl,
1603 unsigned NumVecs, bool is64BitVector) {
1604 unsigned NumRegs = NumVecs;
1605 if (!is64BitVector && NumVecs < 3)
1608 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1609 if (Alignment >= 32 && NumRegs == 4)
1611 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1613 else if (Alignment >= 8)
1618 return CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
1621 static bool isVLDfixed(unsigned Opc)
1624 default: return false;
1625 case ARM::VLD1d8wb_fixed : return true;
1626 case ARM::VLD1d16wb_fixed : return true;
1627 case ARM::VLD1d64Qwb_fixed : return true;
1628 case ARM::VLD1d32wb_fixed : return true;
1629 case ARM::VLD1d64wb_fixed : return true;
1630 case ARM::VLD1d64TPseudoWB_fixed : return true;
1631 case ARM::VLD1d64QPseudoWB_fixed : return true;
1632 case ARM::VLD1q8wb_fixed : return true;
1633 case ARM::VLD1q16wb_fixed : return true;
1634 case ARM::VLD1q32wb_fixed : return true;
1635 case ARM::VLD1q64wb_fixed : return true;
1636 case ARM::VLD1DUPd8wb_fixed : return true;
1637 case ARM::VLD1DUPd16wb_fixed : return true;
1638 case ARM::VLD1DUPd32wb_fixed : return true;
1639 case ARM::VLD1DUPq8wb_fixed : return true;
1640 case ARM::VLD1DUPq16wb_fixed : return true;
1641 case ARM::VLD1DUPq32wb_fixed : return true;
1642 case ARM::VLD2d8wb_fixed : return true;
1643 case ARM::VLD2d16wb_fixed : return true;
1644 case ARM::VLD2d32wb_fixed : return true;
1645 case ARM::VLD2q8PseudoWB_fixed : return true;
1646 case ARM::VLD2q16PseudoWB_fixed : return true;
1647 case ARM::VLD2q32PseudoWB_fixed : return true;
1648 case ARM::VLD2DUPd8wb_fixed : return true;
1649 case ARM::VLD2DUPd16wb_fixed : return true;
1650 case ARM::VLD2DUPd32wb_fixed : return true;
1654 static bool isVSTfixed(unsigned Opc)
1657 default: return false;
1658 case ARM::VST1d8wb_fixed : return true;
1659 case ARM::VST1d16wb_fixed : return true;
1660 case ARM::VST1d32wb_fixed : return true;
1661 case ARM::VST1d64wb_fixed : return true;
1662 case ARM::VST1q8wb_fixed : return true;
1663 case ARM::VST1q16wb_fixed : return true;
1664 case ARM::VST1q32wb_fixed : return true;
1665 case ARM::VST1q64wb_fixed : return true;
1666 case ARM::VST1d64TPseudoWB_fixed : return true;
1667 case ARM::VST1d64QPseudoWB_fixed : return true;
1668 case ARM::VST2d8wb_fixed : return true;
1669 case ARM::VST2d16wb_fixed : return true;
1670 case ARM::VST2d32wb_fixed : return true;
1671 case ARM::VST2q8PseudoWB_fixed : return true;
1672 case ARM::VST2q16PseudoWB_fixed : return true;
1673 case ARM::VST2q32PseudoWB_fixed : return true;
1677 // Get the register stride update opcode of a VLD/VST instruction that
1678 // is otherwise equivalent to the given fixed stride updating instruction.
1679 static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) {
1680 assert((isVLDfixed(Opc) || isVSTfixed(Opc))
1681 && "Incorrect fixed stride updating instruction.");
1684 case ARM::VLD1d8wb_fixed: return ARM::VLD1d8wb_register;
1685 case ARM::VLD1d16wb_fixed: return ARM::VLD1d16wb_register;
1686 case ARM::VLD1d32wb_fixed: return ARM::VLD1d32wb_register;
1687 case ARM::VLD1d64wb_fixed: return ARM::VLD1d64wb_register;
1688 case ARM::VLD1q8wb_fixed: return ARM::VLD1q8wb_register;
1689 case ARM::VLD1q16wb_fixed: return ARM::VLD1q16wb_register;
1690 case ARM::VLD1q32wb_fixed: return ARM::VLD1q32wb_register;
1691 case ARM::VLD1q64wb_fixed: return ARM::VLD1q64wb_register;
1692 case ARM::VLD1d64Twb_fixed: return ARM::VLD1d64Twb_register;
1693 case ARM::VLD1d64Qwb_fixed: return ARM::VLD1d64Qwb_register;
1694 case ARM::VLD1d64TPseudoWB_fixed: return ARM::VLD1d64TPseudoWB_register;
1695 case ARM::VLD1d64QPseudoWB_fixed: return ARM::VLD1d64QPseudoWB_register;
1696 case ARM::VLD1DUPd8wb_fixed : return ARM::VLD1DUPd8wb_register;
1697 case ARM::VLD1DUPd16wb_fixed : return ARM::VLD1DUPd16wb_register;
1698 case ARM::VLD1DUPd32wb_fixed : return ARM::VLD1DUPd32wb_register;
1699 case ARM::VLD1DUPq8wb_fixed : return ARM::VLD1DUPq8wb_register;
1700 case ARM::VLD1DUPq16wb_fixed : return ARM::VLD1DUPq16wb_register;
1701 case ARM::VLD1DUPq32wb_fixed : return ARM::VLD1DUPq32wb_register;
1703 case ARM::VST1d8wb_fixed: return ARM::VST1d8wb_register;
1704 case ARM::VST1d16wb_fixed: return ARM::VST1d16wb_register;
1705 case ARM::VST1d32wb_fixed: return ARM::VST1d32wb_register;
1706 case ARM::VST1d64wb_fixed: return ARM::VST1d64wb_register;
1707 case ARM::VST1q8wb_fixed: return ARM::VST1q8wb_register;
1708 case ARM::VST1q16wb_fixed: return ARM::VST1q16wb_register;
1709 case ARM::VST1q32wb_fixed: return ARM::VST1q32wb_register;
1710 case ARM::VST1q64wb_fixed: return ARM::VST1q64wb_register;
1711 case ARM::VST1d64TPseudoWB_fixed: return ARM::VST1d64TPseudoWB_register;
1712 case ARM::VST1d64QPseudoWB_fixed: return ARM::VST1d64QPseudoWB_register;
1714 case ARM::VLD2d8wb_fixed: return ARM::VLD2d8wb_register;
1715 case ARM::VLD2d16wb_fixed: return ARM::VLD2d16wb_register;
1716 case ARM::VLD2d32wb_fixed: return ARM::VLD2d32wb_register;
1717 case ARM::VLD2q8PseudoWB_fixed: return ARM::VLD2q8PseudoWB_register;
1718 case ARM::VLD2q16PseudoWB_fixed: return ARM::VLD2q16PseudoWB_register;
1719 case ARM::VLD2q32PseudoWB_fixed: return ARM::VLD2q32PseudoWB_register;
1721 case ARM::VST2d8wb_fixed: return ARM::VST2d8wb_register;
1722 case ARM::VST2d16wb_fixed: return ARM::VST2d16wb_register;
1723 case ARM::VST2d32wb_fixed: return ARM::VST2d32wb_register;
1724 case ARM::VST2q8PseudoWB_fixed: return ARM::VST2q8PseudoWB_register;
1725 case ARM::VST2q16PseudoWB_fixed: return ARM::VST2q16PseudoWB_register;
1726 case ARM::VST2q32PseudoWB_fixed: return ARM::VST2q32PseudoWB_register;
1728 case ARM::VLD2DUPd8wb_fixed: return ARM::VLD2DUPd8wb_register;
1729 case ARM::VLD2DUPd16wb_fixed: return ARM::VLD2DUPd16wb_register;
1730 case ARM::VLD2DUPd32wb_fixed: return ARM::VLD2DUPd32wb_register;
1732 return Opc; // If not one we handle, return it unchanged.
1735 /// Returns true if the given increment is a Constant known to be equal to the
1736 /// access size performed by a NEON load/store. This means the "[rN]!" form can
1738 static bool isPerfectIncrement(SDValue Inc, EVT VecTy, unsigned NumVecs) {
1739 auto C = dyn_cast<ConstantSDNode>(Inc);
1740 return C && C->getZExtValue() == VecTy.getSizeInBits() / 8 * NumVecs;
1743 void ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
1744 const uint16_t *DOpcodes,
1745 const uint16_t *QOpcodes0,
1746 const uint16_t *QOpcodes1) {
1747 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1750 SDValue MemAddr, Align;
1751 bool IsIntrinsic = !isUpdating; // By coincidence, all supported updating
1752 // nodes are not intrinsics.
1753 unsigned AddrOpIdx = IsIntrinsic ? 2 : 1;
1754 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
1757 SDValue Chain = N->getOperand(0);
1758 EVT VT = N->getValueType(0);
1759 bool is64BitVector = VT.is64BitVector();
1760 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
1762 unsigned OpcodeIndex;
1763 switch (VT.getSimpleVT().SimpleTy) {
1764 default: llvm_unreachable("unhandled vld type");
1765 // Double-register operations:
1766 case MVT::v8i8: OpcodeIndex = 0; break;
1767 case MVT::v4i16: OpcodeIndex = 1; break;
1769 case MVT::v2i32: OpcodeIndex = 2; break;
1770 case MVT::v1i64: OpcodeIndex = 3; break;
1771 // Quad-register operations:
1772 case MVT::v16i8: OpcodeIndex = 0; break;
1773 case MVT::v8i16: OpcodeIndex = 1; break;
1775 case MVT::v4i32: OpcodeIndex = 2; break;
1777 case MVT::v2i64: OpcodeIndex = 3; break;
1784 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1787 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1789 std::vector<EVT> ResTys;
1790 ResTys.push_back(ResTy);
1792 ResTys.push_back(MVT::i32);
1793 ResTys.push_back(MVT::Other);
1795 SDValue Pred = getAL(CurDAG, dl);
1796 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1798 SmallVector<SDValue, 7> Ops;
1800 // Double registers and VLD1/VLD2 quad registers are directly supported.
1801 if (is64BitVector || NumVecs <= 2) {
1802 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1803 QOpcodes0[OpcodeIndex]);
1804 Ops.push_back(MemAddr);
1805 Ops.push_back(Align);
1807 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1808 bool IsImmUpdate = isPerfectIncrement(Inc, VT, NumVecs);
1810 // We use a VLD1 for v1i64 even if the pseudo says vld2/3/4, so
1811 // check for the opcode rather than the number of vector elements.
1812 if (isVLDfixed(Opc))
1813 Opc = getVLDSTRegisterUpdateOpcode(Opc);
1815 // VLD1/VLD2 fixed increment does not need Reg0 so only include it in
1816 // the operands if not such an opcode.
1817 } else if (!isVLDfixed(Opc))
1818 Ops.push_back(Reg0);
1820 Ops.push_back(Pred);
1821 Ops.push_back(Reg0);
1822 Ops.push_back(Chain);
1823 VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1826 // Otherwise, quad registers are loaded with two separate instructions,
1827 // where one loads the even registers and the other loads the odd registers.
1828 EVT AddrTy = MemAddr.getValueType();
1830 // Load the even subregs. This is always an updating load, so that it
1831 // provides the address to the second load for the odd subregs.
1833 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1834 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1835 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1836 ResTy, AddrTy, MVT::Other, OpsA);
1837 Chain = SDValue(VLdA, 2);
1839 // Load the odd subregs.
1840 Ops.push_back(SDValue(VLdA, 1));
1841 Ops.push_back(Align);
1843 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1844 assert(isa<ConstantSDNode>(Inc.getNode()) &&
1845 "only constant post-increment update allowed for VLD3/4");
1847 Ops.push_back(Reg0);
1849 Ops.push_back(SDValue(VLdA, 0));
1850 Ops.push_back(Pred);
1851 Ops.push_back(Reg0);
1852 Ops.push_back(Chain);
1853 VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, Ops);
1856 // Transfer memoperands.
1857 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1858 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1859 cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1);
1862 ReplaceNode(N, VLd);
1866 // Extract out the subregisters.
1867 SDValue SuperReg = SDValue(VLd, 0);
1868 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
1869 ARM::qsub_3 == ARM::qsub_0 + 3,
1870 "Unexpected subreg numbering");
1871 unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0);
1872 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1873 ReplaceUses(SDValue(N, Vec),
1874 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
1875 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1877 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
1878 CurDAG->RemoveDeadNode(N);
1881 void ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
1882 const uint16_t *DOpcodes,
1883 const uint16_t *QOpcodes0,
1884 const uint16_t *QOpcodes1) {
1885 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
1888 SDValue MemAddr, Align;
1889 bool IsIntrinsic = !isUpdating; // By coincidence, all supported updating
1890 // nodes are not intrinsics.
1891 unsigned AddrOpIdx = IsIntrinsic ? 2 : 1;
1892 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
1893 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
1896 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1897 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1899 SDValue Chain = N->getOperand(0);
1900 EVT VT = N->getOperand(Vec0Idx).getValueType();
1901 bool is64BitVector = VT.is64BitVector();
1902 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
1904 unsigned OpcodeIndex;
1905 switch (VT.getSimpleVT().SimpleTy) {
1906 default: llvm_unreachable("unhandled vst type");
1907 // Double-register operations:
1908 case MVT::v8i8: OpcodeIndex = 0; break;
1910 case MVT::v4i16: OpcodeIndex = 1; break;
1912 case MVT::v2i32: OpcodeIndex = 2; break;
1913 case MVT::v1i64: OpcodeIndex = 3; break;
1914 // Quad-register operations:
1915 case MVT::v16i8: OpcodeIndex = 0; break;
1917 case MVT::v8i16: OpcodeIndex = 1; break;
1919 case MVT::v4i32: OpcodeIndex = 2; break;
1921 case MVT::v2i64: OpcodeIndex = 3; break;
1924 std::vector<EVT> ResTys;
1926 ResTys.push_back(MVT::i32);
1927 ResTys.push_back(MVT::Other);
1929 SDValue Pred = getAL(CurDAG, dl);
1930 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1931 SmallVector<SDValue, 7> Ops;
1933 // Double registers and VST1/VST2 quad registers are directly supported.
1934 if (is64BitVector || NumVecs <= 2) {
1937 SrcReg = N->getOperand(Vec0Idx);
1938 } else if (is64BitVector) {
1939 // Form a REG_SEQUENCE to force register allocation.
1940 SDValue V0 = N->getOperand(Vec0Idx + 0);
1941 SDValue V1 = N->getOperand(Vec0Idx + 1);
1943 SrcReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
1945 SDValue V2 = N->getOperand(Vec0Idx + 2);
1946 // If it's a vst3, form a quad D-register and leave the last part as
1948 SDValue V3 = (NumVecs == 3)
1949 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
1950 : N->getOperand(Vec0Idx + 3);
1951 SrcReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
1954 // Form a QQ register.
1955 SDValue Q0 = N->getOperand(Vec0Idx);
1956 SDValue Q1 = N->getOperand(Vec0Idx + 1);
1957 SrcReg = SDValue(createQRegPairNode(MVT::v4i64, Q0, Q1), 0);
1960 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1961 QOpcodes0[OpcodeIndex]);
1962 Ops.push_back(MemAddr);
1963 Ops.push_back(Align);
1965 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1966 bool IsImmUpdate = isPerfectIncrement(Inc, VT, NumVecs);
1968 // We use a VST1 for v1i64 even if the pseudo says VST2/3/4, so
1969 // check for the opcode rather than the number of vector elements.
1970 if (isVSTfixed(Opc))
1971 Opc = getVLDSTRegisterUpdateOpcode(Opc);
1974 // VST1/VST2 fixed increment does not need Reg0 so only include it in
1975 // the operands if not such an opcode.
1976 else if (!isVSTfixed(Opc))
1977 Ops.push_back(Reg0);
1979 Ops.push_back(SrcReg);
1980 Ops.push_back(Pred);
1981 Ops.push_back(Reg0);
1982 Ops.push_back(Chain);
1983 SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1985 // Transfer memoperands.
1986 cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1);
1988 ReplaceNode(N, VSt);
1992 // Otherwise, quad registers are stored with two separate instructions,
1993 // where one stores the even registers and the other stores the odd registers.
1995 // Form the QQQQ REG_SEQUENCE.
1996 SDValue V0 = N->getOperand(Vec0Idx + 0);
1997 SDValue V1 = N->getOperand(Vec0Idx + 1);
1998 SDValue V2 = N->getOperand(Vec0Idx + 2);
1999 SDValue V3 = (NumVecs == 3)
2000 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2001 : N->getOperand(Vec0Idx + 3);
2002 SDValue RegSeq = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
2004 // Store the even D registers. This is always an updating store, so that it
2005 // provides the address to the second store for the odd subregs.
2006 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
2007 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
2008 MemAddr.getValueType(),
2010 cast<MachineSDNode>(VStA)->setMemRefs(MemOp, MemOp + 1);
2011 Chain = SDValue(VStA, 1);
2013 // Store the odd D registers.
2014 Ops.push_back(SDValue(VStA, 0));
2015 Ops.push_back(Align);
2017 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2018 assert(isa<ConstantSDNode>(Inc.getNode()) &&
2019 "only constant post-increment update allowed for VST3/4");
2021 Ops.push_back(Reg0);
2023 Ops.push_back(RegSeq);
2024 Ops.push_back(Pred);
2025 Ops.push_back(Reg0);
2026 Ops.push_back(Chain);
2027 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
2029 cast<MachineSDNode>(VStB)->setMemRefs(MemOp, MemOp + 1);
2030 ReplaceNode(N, VStB);
2033 void ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
2035 const uint16_t *DOpcodes,
2036 const uint16_t *QOpcodes) {
2037 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
2040 SDValue MemAddr, Align;
2041 bool IsIntrinsic = !isUpdating; // By coincidence, all supported updating
2042 // nodes are not intrinsics.
2043 unsigned AddrOpIdx = IsIntrinsic ? 2 : 1;
2044 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2045 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2048 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2049 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2051 SDValue Chain = N->getOperand(0);
2053 cast<ConstantSDNode>(N->getOperand(Vec0Idx + NumVecs))->getZExtValue();
2054 EVT VT = N->getOperand(Vec0Idx).getValueType();
2055 bool is64BitVector = VT.is64BitVector();
2057 unsigned Alignment = 0;
2059 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
2060 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8;
2061 if (Alignment > NumBytes)
2062 Alignment = NumBytes;
2063 if (Alignment < 8 && Alignment < NumBytes)
2065 // Alignment must be a power of two; make sure of that.
2066 Alignment = (Alignment & -Alignment);
2070 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
2072 unsigned OpcodeIndex;
2073 switch (VT.getSimpleVT().SimpleTy) {
2074 default: llvm_unreachable("unhandled vld/vst lane type");
2075 // Double-register operations:
2076 case MVT::v8i8: OpcodeIndex = 0; break;
2077 case MVT::v4i16: OpcodeIndex = 1; break;
2079 case MVT::v2i32: OpcodeIndex = 2; break;
2080 // Quad-register operations:
2081 case MVT::v8i16: OpcodeIndex = 0; break;
2083 case MVT::v4i32: OpcodeIndex = 1; break;
2086 std::vector<EVT> ResTys;
2088 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2091 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(),
2092 MVT::i64, ResTyElts));
2095 ResTys.push_back(MVT::i32);
2096 ResTys.push_back(MVT::Other);
2098 SDValue Pred = getAL(CurDAG, dl);
2099 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2101 SmallVector<SDValue, 8> Ops;
2102 Ops.push_back(MemAddr);
2103 Ops.push_back(Align);
2105 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2107 isPerfectIncrement(Inc, VT.getVectorElementType(), NumVecs);
2108 Ops.push_back(IsImmUpdate ? Reg0 : Inc);
2112 SDValue V0 = N->getOperand(Vec0Idx + 0);
2113 SDValue V1 = N->getOperand(Vec0Idx + 1);
2116 SuperReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
2118 SuperReg = SDValue(createQRegPairNode(MVT::v4i64, V0, V1), 0);
2120 SDValue V2 = N->getOperand(Vec0Idx + 2);
2121 SDValue V3 = (NumVecs == 3)
2122 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2123 : N->getOperand(Vec0Idx + 3);
2125 SuperReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2127 SuperReg = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
2129 Ops.push_back(SuperReg);
2130 Ops.push_back(getI32Imm(Lane, dl));
2131 Ops.push_back(Pred);
2132 Ops.push_back(Reg0);
2133 Ops.push_back(Chain);
2135 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2136 QOpcodes[OpcodeIndex]);
2137 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2138 cast<MachineSDNode>(VLdLn)->setMemRefs(MemOp, MemOp + 1);
2140 ReplaceNode(N, VLdLn);
2144 // Extract the subregisters.
2145 SuperReg = SDValue(VLdLn, 0);
2146 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
2147 ARM::qsub_3 == ARM::qsub_0 + 3,
2148 "Unexpected subreg numbering");
2149 unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
2150 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2151 ReplaceUses(SDValue(N, Vec),
2152 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
2153 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1));
2155 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2));
2156 CurDAG->RemoveDeadNode(N);
2159 void ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool IsIntrinsic,
2160 bool isUpdating, unsigned NumVecs,
2161 const uint16_t *DOpcodes,
2162 const uint16_t *QOpcodes0,
2163 const uint16_t *QOpcodes1) {
2164 assert(NumVecs >= 1 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
2167 SDValue MemAddr, Align;
2168 unsigned AddrOpIdx = IsIntrinsic ? 2 : 1;
2169 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2172 SDValue Chain = N->getOperand(0);
2173 EVT VT = N->getValueType(0);
2174 bool is64BitVector = VT.is64BitVector();
2176 unsigned Alignment = 0;
2178 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
2179 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8;
2180 if (Alignment > NumBytes)
2181 Alignment = NumBytes;
2182 if (Alignment < 8 && Alignment < NumBytes)
2184 // Alignment must be a power of two; make sure of that.
2185 Alignment = (Alignment & -Alignment);
2189 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
2191 unsigned OpcodeIndex;
2192 switch (VT.getSimpleVT().SimpleTy) {
2193 default: llvm_unreachable("unhandled vld-dup type");
2195 case MVT::v16i8: OpcodeIndex = 0; break;
2197 case MVT::v8i16: OpcodeIndex = 1; break;
2201 case MVT::v4i32: OpcodeIndex = 2; break;
2203 case MVT::v1i64: OpcodeIndex = 3; break;
2206 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2209 EVT ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
2211 std::vector<EVT> ResTys;
2212 ResTys.push_back(ResTy);
2214 ResTys.push_back(MVT::i32);
2215 ResTys.push_back(MVT::Other);
2217 SDValue Pred = getAL(CurDAG, dl);
2218 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2221 if (is64BitVector || NumVecs == 1) {
2222 SmallVector<SDValue, 6> Ops;
2223 Ops.push_back(MemAddr);
2224 Ops.push_back(Align);
2225 unsigned Opc = is64BitVector ? DOpcodes[OpcodeIndex] :
2226 QOpcodes0[OpcodeIndex];
2228 // fixed-stride update instructions don't have an explicit writeback
2229 // operand. It's implicit in the opcode itself.
2230 SDValue Inc = N->getOperand(2);
2232 isPerfectIncrement(Inc, VT.getVectorElementType(), NumVecs);
2233 if (NumVecs <= 2 && !IsImmUpdate)
2234 Opc = getVLDSTRegisterUpdateOpcode(Opc);
2237 // FIXME: VLD3 and VLD4 haven't been updated to that form yet.
2238 else if (NumVecs > 2)
2239 Ops.push_back(Reg0);
2241 Ops.push_back(Pred);
2242 Ops.push_back(Reg0);
2243 Ops.push_back(Chain);
2244 VLdDup = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2245 } else if (NumVecs == 2) {
2246 const SDValue OpsA[] = { MemAddr, Align, Pred, Reg0, Chain };
2247 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex],
2250 Chain = SDValue(VLdA, 1);
2251 const SDValue OpsB[] = { MemAddr, Align, Pred, Reg0, Chain };
2252 VLdDup = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, OpsB);
2255 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
2256 const SDValue OpsA[] = { MemAddr, Align, ImplDef, Pred, Reg0, Chain };
2257 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex],
2260 SDValue SuperReg = SDValue(VLdA, 0);
2261 Chain = SDValue(VLdA, 1);
2262 const SDValue OpsB[] = { MemAddr, Align, SuperReg, Pred, Reg0, Chain };
2263 VLdDup = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, OpsB);
2266 // Transfer memoperands.
2267 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2268 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2269 cast<MachineSDNode>(VLdDup)->setMemRefs(MemOp, MemOp + 1);
2271 // Extract the subregisters.
2273 ReplaceUses(SDValue(N, 0), SDValue(VLdDup, 0));
2275 SDValue SuperReg = SDValue(VLdDup, 0);
2276 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7, "Unexpected subreg numbering");
2277 unsigned SubIdx = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
2278 for (unsigned Vec = 0; Vec != NumVecs; ++Vec) {
2279 ReplaceUses(SDValue(N, Vec),
2280 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
2283 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1));
2285 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2));
2286 CurDAG->RemoveDeadNode(N);
2289 bool ARMDAGToDAGISel::tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned) {
2290 if (!Subtarget->hasV6T2Ops())
2293 unsigned Opc = isSigned
2294 ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
2295 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
2298 // For unsigned extracts, check for a shift right and mask
2299 unsigned And_imm = 0;
2300 if (N->getOpcode() == ISD::AND) {
2301 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
2303 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
2304 if (And_imm & (And_imm + 1))
2307 unsigned Srl_imm = 0;
2308 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
2310 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2312 // Note: The width operand is encoded as width-1.
2313 unsigned Width = countTrailingOnes(And_imm) - 1;
2314 unsigned LSB = Srl_imm;
2316 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2318 if ((LSB + Width + 1) == N->getValueType(0).getSizeInBits()) {
2319 // It's cheaper to use a right shift to extract the top bits.
2320 if (Subtarget->isThumb()) {
2321 Opc = isSigned ? ARM::t2ASRri : ARM::t2LSRri;
2322 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2323 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2324 getAL(CurDAG, dl), Reg0, Reg0 };
2325 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2329 // ARM models shift instructions as MOVsi with shifter operand.
2330 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(ISD::SRL);
2332 CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, LSB), dl,
2334 SDValue Ops[] = { N->getOperand(0).getOperand(0), ShOpc,
2335 getAL(CurDAG, dl), Reg0, Reg0 };
2336 CurDAG->SelectNodeTo(N, ARM::MOVsi, MVT::i32, Ops);
2340 assert(LSB + Width + 1 <= 32 && "Shouldn't create an invalid ubfx");
2341 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2342 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2343 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2344 getAL(CurDAG, dl), Reg0 };
2345 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2352 // Otherwise, we're looking for a shift of a shift
2353 unsigned Shl_imm = 0;
2354 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
2355 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
2356 unsigned Srl_imm = 0;
2357 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
2358 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2359 // Note: The width operand is encoded as width-1.
2360 unsigned Width = 32 - Srl_imm - 1;
2361 int LSB = Srl_imm - Shl_imm;
2364 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2365 assert(LSB + Width + 1 <= 32 && "Shouldn't create an invalid ubfx");
2366 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2367 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2368 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2369 getAL(CurDAG, dl), Reg0 };
2370 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2375 // Or we are looking for a shift of an and, with a mask operand
2376 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, And_imm) &&
2377 isShiftedMask_32(And_imm)) {
2378 unsigned Srl_imm = 0;
2379 unsigned LSB = countTrailingZeros(And_imm);
2380 // Shift must be the same as the ands lsb
2381 if (isInt32Immediate(N->getOperand(1), Srl_imm) && Srl_imm == LSB) {
2382 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2383 unsigned MSB = 31 - countLeadingZeros(And_imm);
2384 // Note: The width operand is encoded as width-1.
2385 unsigned Width = MSB - LSB;
2386 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2387 assert(Srl_imm + Width + 1 <= 32 && "Shouldn't create an invalid ubfx");
2388 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2389 CurDAG->getTargetConstant(Srl_imm, dl, MVT::i32),
2390 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2391 getAL(CurDAG, dl), Reg0 };
2392 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2397 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG) {
2398 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
2400 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, LSB) &&
2401 !isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRA, LSB))
2404 if (LSB + Width > 32)
2407 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2408 assert(LSB + Width <= 32 && "Shouldn't create an invalid ubfx");
2409 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2410 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2411 CurDAG->getTargetConstant(Width - 1, dl, MVT::i32),
2412 getAL(CurDAG, dl), Reg0 };
2413 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2420 /// Target-specific DAG combining for ISD::XOR.
2421 /// Target-independent combining lowers SELECT_CC nodes of the form
2422 /// select_cc setg[ge] X, 0, X, -X
2423 /// select_cc setgt X, -1, X, -X
2424 /// select_cc setl[te] X, 0, -X, X
2425 /// select_cc setlt X, 1, -X, X
2426 /// which represent Integer ABS into:
2427 /// Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2428 /// ARM instruction selection detects the latter and matches it to
2429 /// ARM::ABS or ARM::t2ABS machine node.
2430 bool ARMDAGToDAGISel::tryABSOp(SDNode *N){
2431 SDValue XORSrc0 = N->getOperand(0);
2432 SDValue XORSrc1 = N->getOperand(1);
2433 EVT VT = N->getValueType(0);
2435 if (Subtarget->isThumb1Only())
2438 if (XORSrc0.getOpcode() != ISD::ADD || XORSrc1.getOpcode() != ISD::SRA)
2441 SDValue ADDSrc0 = XORSrc0.getOperand(0);
2442 SDValue ADDSrc1 = XORSrc0.getOperand(1);
2443 SDValue SRASrc0 = XORSrc1.getOperand(0);
2444 SDValue SRASrc1 = XORSrc1.getOperand(1);
2445 ConstantSDNode *SRAConstant = dyn_cast<ConstantSDNode>(SRASrc1);
2446 EVT XType = SRASrc0.getValueType();
2447 unsigned Size = XType.getSizeInBits() - 1;
2449 if (ADDSrc1 == XORSrc1 && ADDSrc0 == SRASrc0 &&
2450 XType.isInteger() && SRAConstant != nullptr &&
2451 Size == SRAConstant->getZExtValue()) {
2452 unsigned Opcode = Subtarget->isThumb2() ? ARM::t2ABS : ARM::ABS;
2453 CurDAG->SelectNodeTo(N, Opcode, VT, ADDSrc0);
2460 /// We've got special pseudo-instructions for these
2461 void ARMDAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
2463 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
2464 if (MemTy == MVT::i8)
2465 Opcode = ARM::CMP_SWAP_8;
2466 else if (MemTy == MVT::i16)
2467 Opcode = ARM::CMP_SWAP_16;
2468 else if (MemTy == MVT::i32)
2469 Opcode = ARM::CMP_SWAP_32;
2471 llvm_unreachable("Unknown AtomicCmpSwap type");
2473 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
2475 SDNode *CmpSwap = CurDAG->getMachineNode(
2477 CurDAG->getVTList(MVT::i32, MVT::i32, MVT::Other), Ops);
2479 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2480 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2481 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
2483 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
2484 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
2485 CurDAG->RemoveDeadNode(N);
2488 static Optional<std::pair<unsigned, unsigned>>
2489 getContiguousRangeOfSetBits(const APInt &A) {
2490 unsigned FirstOne = A.getBitWidth() - A.countLeadingZeros() - 1;
2491 unsigned LastOne = A.countTrailingZeros();
2492 if (A.countPopulation() != (FirstOne - LastOne + 1))
2493 return Optional<std::pair<unsigned,unsigned>>();
2494 return std::make_pair(FirstOne, LastOne);
2497 void ARMDAGToDAGISel::SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI) {
2498 assert(N->getOpcode() == ARMISD::CMPZ);
2499 SwitchEQNEToPLMI = false;
2501 if (!Subtarget->isThumb())
2502 // FIXME: Work out whether it is profitable to do this in A32 mode - LSL and
2503 // LSR don't exist as standalone instructions - they need the barrel shifter.
2506 // select (cmpz (and X, C), #0) -> (LSLS X) or (LSRS X) or (LSRS (LSLS X))
2507 SDValue And = N->getOperand(0);
2508 if (!And->hasOneUse())
2511 SDValue Zero = N->getOperand(1);
2512 if (!isa<ConstantSDNode>(Zero) || !cast<ConstantSDNode>(Zero)->isNullValue() ||
2513 And->getOpcode() != ISD::AND)
2515 SDValue X = And.getOperand(0);
2516 auto C = dyn_cast<ConstantSDNode>(And.getOperand(1));
2520 auto Range = getContiguousRangeOfSetBits(C->getAPIntValue());
2524 // There are several ways to lower this:
2528 auto EmitShift = [&](unsigned Opc, SDValue Src, unsigned Imm) -> SDNode* {
2529 if (Subtarget->isThumb2()) {
2530 Opc = (Opc == ARM::tLSLri) ? ARM::t2LSLri : ARM::t2LSRri;
2531 SDValue Ops[] = { Src, CurDAG->getTargetConstant(Imm, dl, MVT::i32),
2532 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2533 CurDAG->getRegister(0, MVT::i32) };
2534 return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2536 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), Src,
2537 CurDAG->getTargetConstant(Imm, dl, MVT::i32),
2538 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
2539 return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2543 if (Range->second == 0) {
2544 // 1. Mask includes the LSB -> Simply shift the top N bits off
2545 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2546 ReplaceNode(And.getNode(), NewN);
2547 } else if (Range->first == 31) {
2548 // 2. Mask includes the MSB -> Simply shift the bottom N bits off
2549 NewN = EmitShift(ARM::tLSRri, X, Range->second);
2550 ReplaceNode(And.getNode(), NewN);
2551 } else if (Range->first == Range->second) {
2552 // 3. Only one bit is set. We can shift this into the sign bit and use a
2553 // PL/MI comparison.
2554 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2555 ReplaceNode(And.getNode(), NewN);
2557 SwitchEQNEToPLMI = true;
2558 } else if (!Subtarget->hasV6T2Ops()) {
2559 // 4. Do a double shift to clear bottom and top bits, but only in
2560 // thumb-1 mode as in thumb-2 we can use UBFX.
2561 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2562 NewN = EmitShift(ARM::tLSRri, SDValue(NewN, 0),
2563 Range->second + (31 - Range->first));
2564 ReplaceNode(And.getNode(), NewN);
2569 void ARMDAGToDAGISel::Select(SDNode *N) {
2572 if (N->isMachineOpcode()) {
2574 return; // Already selected.
2577 switch (N->getOpcode()) {
2579 case ISD::WRITE_REGISTER:
2580 if (tryWriteRegister(N))
2583 case ISD::READ_REGISTER:
2584 if (tryReadRegister(N))
2587 case ISD::INLINEASM:
2588 if (tryInlineAsm(N))
2592 // Select special operations if XOR node forms integer ABS pattern
2595 // Other cases are autogenerated.
2597 case ISD::Constant: {
2598 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
2599 // If we can't materialize the constant we need to use a literal pool
2600 if (ConstantMaterializationCost(Val) > 2) {
2601 SDValue CPIdx = CurDAG->getTargetConstantPool(
2602 ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), Val),
2603 TLI->getPointerTy(CurDAG->getDataLayout()));
2606 if (Subtarget->isThumb()) {
2610 CurDAG->getRegister(0, MVT::i32),
2611 CurDAG->getEntryNode()
2613 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
2618 CurDAG->getTargetConstant(0, dl, MVT::i32),
2620 CurDAG->getRegister(0, MVT::i32),
2621 CurDAG->getEntryNode()
2623 ResNode = CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
2626 // Annotate the Node with memory operand information so that MachineInstr
2627 // queries work properly. This e.g. gives the register allocation the
2628 // required information for rematerialization.
2629 MachineFunction& MF = CurDAG->getMachineFunction();
2630 MachineSDNode::mmo_iterator MemOp = MF.allocateMemRefsArray(1);
2631 MemOp[0] = MF.getMachineMemOperand(
2632 MachinePointerInfo::getConstantPool(MF),
2633 MachineMemOperand::MOLoad, 4, 4);
2635 cast<MachineSDNode>(ResNode)->setMemRefs(MemOp, MemOp+1);
2637 ReplaceNode(N, ResNode);
2641 // Other cases are autogenerated.
2644 case ISD::FrameIndex: {
2645 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
2646 int FI = cast<FrameIndexSDNode>(N)->getIndex();
2647 SDValue TFI = CurDAG->getTargetFrameIndex(
2648 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
2649 if (Subtarget->isThumb1Only()) {
2650 // Set the alignment of the frame object to 4, to avoid having to generate
2651 // more than one ADD
2652 MachineFrameInfo &MFI = MF->getFrameInfo();
2653 if (MFI.getObjectAlignment(FI) < 4)
2654 MFI.setObjectAlignment(FI, 4);
2655 CurDAG->SelectNodeTo(N, ARM::tADDframe, MVT::i32, TFI,
2656 CurDAG->getTargetConstant(0, dl, MVT::i32));
2659 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2660 ARM::t2ADDri : ARM::ADDri);
2661 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, dl, MVT::i32),
2662 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2663 CurDAG->getRegister(0, MVT::i32) };
2664 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2669 if (tryV6T2BitfieldExtractOp(N, false))
2672 case ISD::SIGN_EXTEND_INREG:
2674 if (tryV6T2BitfieldExtractOp(N, true))
2678 if (Subtarget->isThumb1Only())
2680 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
2681 unsigned RHSV = C->getZExtValue();
2683 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
2684 unsigned ShImm = Log2_32(RHSV-1);
2687 SDValue V = N->getOperand(0);
2688 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2689 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
2690 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2691 if (Subtarget->isThumb()) {
2692 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
2693 CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops);
2696 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
2698 CurDAG->SelectNodeTo(N, ARM::ADDrsi, MVT::i32, Ops);
2702 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
2703 unsigned ShImm = Log2_32(RHSV+1);
2706 SDValue V = N->getOperand(0);
2707 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2708 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
2709 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2710 if (Subtarget->isThumb()) {
2711 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
2712 CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops);
2715 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
2717 CurDAG->SelectNodeTo(N, ARM::RSBrsi, MVT::i32, Ops);
2724 // Check for unsigned bitfield extract
2725 if (tryV6T2BitfieldExtractOp(N, false))
2728 // If an immediate is used in an AND node, it is possible that the immediate
2729 // can be more optimally materialized when negated. If this is the case we
2730 // can negate the immediate and use a BIC instead.
2731 auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
2732 if (N1C && N1C->hasOneUse() && Subtarget->isThumb()) {
2733 uint32_t Imm = (uint32_t) N1C->getZExtValue();
2735 // In Thumb2 mode, an AND can take a 12-bit immediate. If this
2736 // immediate can be negated and fit in the immediate operand of
2737 // a t2BIC, don't do any manual transform here as this can be
2738 // handled by the generic ISel machinery.
2739 bool PreferImmediateEncoding =
2740 Subtarget->hasThumb2() && (is_t2_so_imm(Imm) || is_t2_so_imm_not(Imm));
2741 if (!PreferImmediateEncoding &&
2742 ConstantMaterializationCost(Imm) >
2743 ConstantMaterializationCost(~Imm)) {
2744 // The current immediate costs more to materialize than a negated
2745 // immediate, so negate the immediate and use a BIC.
2747 CurDAG->getConstant(~N1C->getZExtValue(), dl, MVT::i32);
2748 // If the new constant didn't exist before, reposition it in the topological
2749 // ordering so it is just before N. Otherwise, don't touch its location.
2750 if (NewImm->getNodeId() == -1)
2751 CurDAG->RepositionNode(N->getIterator(), NewImm.getNode());
2753 if (!Subtarget->hasThumb2()) {
2754 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32),
2755 N->getOperand(0), NewImm, getAL(CurDAG, dl),
2756 CurDAG->getRegister(0, MVT::i32)};
2757 ReplaceNode(N, CurDAG->getMachineNode(ARM::tBIC, dl, MVT::i32, Ops));
2760 SDValue Ops[] = {N->getOperand(0), NewImm, getAL(CurDAG, dl),
2761 CurDAG->getRegister(0, MVT::i32),
2762 CurDAG->getRegister(0, MVT::i32)};
2764 CurDAG->getMachineNode(ARM::t2BICrr, dl, MVT::i32, Ops));
2770 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2771 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2772 // are entirely contributed by c2 and lower 16-bits are entirely contributed
2773 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2774 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
2775 EVT VT = N->getValueType(0);
2778 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2780 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2783 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
2784 N1C = dyn_cast<ConstantSDNode>(N1);
2787 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2788 SDValue N2 = N0.getOperand(1);
2789 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2792 unsigned N1CVal = N1C->getZExtValue();
2793 unsigned N2CVal = N2C->getZExtValue();
2794 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2795 (N1CVal & 0xffffU) == 0xffffU &&
2796 (N2CVal & 0xffffU) == 0x0U) {
2797 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2799 SDValue Ops[] = { N0.getOperand(0), Imm16,
2800 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) };
2801 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
2808 case ARMISD::UMAAL: {
2809 unsigned Opc = Subtarget->isThumb() ? ARM::t2UMAAL : ARM::UMAAL;
2810 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2811 N->getOperand(2), N->getOperand(3),
2813 CurDAG->getRegister(0, MVT::i32) };
2814 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, MVT::i32, Ops));
2817 case ARMISD::UMLAL:{
2818 if (Subtarget->isThumb()) {
2819 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2820 N->getOperand(3), getAL(CurDAG, dl),
2821 CurDAG->getRegister(0, MVT::i32)};
2823 N, CurDAG->getMachineNode(ARM::t2UMLAL, dl, MVT::i32, MVT::i32, Ops));
2826 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2827 N->getOperand(3), getAL(CurDAG, dl),
2828 CurDAG->getRegister(0, MVT::i32),
2829 CurDAG->getRegister(0, MVT::i32) };
2830 ReplaceNode(N, CurDAG->getMachineNode(
2831 Subtarget->hasV6Ops() ? ARM::UMLAL : ARM::UMLALv5, dl,
2832 MVT::i32, MVT::i32, Ops));
2836 case ARMISD::SMLAL:{
2837 if (Subtarget->isThumb()) {
2838 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2839 N->getOperand(3), getAL(CurDAG, dl),
2840 CurDAG->getRegister(0, MVT::i32)};
2842 N, CurDAG->getMachineNode(ARM::t2SMLAL, dl, MVT::i32, MVT::i32, Ops));
2845 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2846 N->getOperand(3), getAL(CurDAG, dl),
2847 CurDAG->getRegister(0, MVT::i32),
2848 CurDAG->getRegister(0, MVT::i32) };
2849 ReplaceNode(N, CurDAG->getMachineNode(
2850 Subtarget->hasV6Ops() ? ARM::SMLAL : ARM::SMLALv5, dl,
2851 MVT::i32, MVT::i32, Ops));
2855 case ARMISD::SUBE: {
2856 if (!Subtarget->hasV6Ops() || !Subtarget->hasDSP())
2858 // Look for a pattern to match SMMLS
2859 // (sube a, (smul_loHi a, b), (subc 0, (smul_LOhi(a, b))))
2860 if (N->getOperand(1).getOpcode() != ISD::SMUL_LOHI ||
2861 N->getOperand(2).getOpcode() != ARMISD::SUBC ||
2862 !SDValue(N, 1).use_empty())
2865 if (Subtarget->isThumb())
2866 assert(Subtarget->hasThumb2() &&
2867 "This pattern should not be generated for Thumb");
2869 SDValue SmulLoHi = N->getOperand(1);
2870 SDValue Subc = N->getOperand(2);
2871 auto *Zero = dyn_cast<ConstantSDNode>(Subc.getOperand(0));
2873 if (!Zero || Zero->getZExtValue() != 0 ||
2874 Subc.getOperand(1) != SmulLoHi.getValue(0) ||
2875 N->getOperand(1) != SmulLoHi.getValue(1) ||
2876 N->getOperand(2) != Subc.getValue(1))
2879 unsigned Opc = Subtarget->isThumb2() ? ARM::t2SMMLS : ARM::SMMLS;
2880 SDValue Ops[] = { SmulLoHi.getOperand(0), SmulLoHi.getOperand(1),
2881 N->getOperand(0), getAL(CurDAG, dl),
2882 CurDAG->getRegister(0, MVT::i32) };
2883 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops));
2887 if (Subtarget->isThumb() && Subtarget->hasThumb2()) {
2888 if (tryT2IndexedLoad(N))
2890 } else if (Subtarget->isThumb()) {
2891 if (tryT1IndexedLoad(N))
2893 } else if (tryARMIndexedLoad(N))
2895 // Other cases are autogenerated.
2898 case ARMISD::BRCOND: {
2899 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2900 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2901 // Pattern complexity = 6 cost = 1 size = 0
2903 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2904 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2905 // Pattern complexity = 6 cost = 1 size = 0
2907 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2908 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2909 // Pattern complexity = 6 cost = 1 size = 0
2911 unsigned Opc = Subtarget->isThumb() ?
2912 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
2913 SDValue Chain = N->getOperand(0);
2914 SDValue N1 = N->getOperand(1);
2915 SDValue N2 = N->getOperand(2);
2916 SDValue N3 = N->getOperand(3);
2917 SDValue InFlag = N->getOperand(4);
2918 assert(N1.getOpcode() == ISD::BasicBlock);
2919 assert(N2.getOpcode() == ISD::Constant);
2920 assert(N3.getOpcode() == ISD::Register);
2922 unsigned CC = (unsigned) cast<ConstantSDNode>(N2)->getZExtValue();
2924 if (InFlag.getOpcode() == ARMISD::CMPZ) {
2925 bool SwitchEQNEToPLMI;
2926 SelectCMPZ(InFlag.getNode(), SwitchEQNEToPLMI);
2927 InFlag = N->getOperand(4);
2929 if (SwitchEQNEToPLMI) {
2930 switch ((ARMCC::CondCodes)CC) {
2931 default: llvm_unreachable("CMPZ must be either NE or EQ!");
2933 CC = (unsigned)ARMCC::MI;
2936 CC = (unsigned)ARMCC::PL;
2942 SDValue Tmp2 = CurDAG->getTargetConstant(CC, dl, MVT::i32);
2943 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
2944 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
2946 Chain = SDValue(ResNode, 0);
2947 if (N->getNumValues() == 2) {
2948 InFlag = SDValue(ResNode, 1);
2949 ReplaceUses(SDValue(N, 1), InFlag);
2951 ReplaceUses(SDValue(N, 0),
2952 SDValue(Chain.getNode(), Chain.getResNo()));
2953 CurDAG->RemoveDeadNode(N);
2957 case ARMISD::CMPZ: {
2958 // select (CMPZ X, #-C) -> (CMPZ (ADDS X, #C), #0)
2959 // This allows us to avoid materializing the expensive negative constant.
2960 // The CMPZ #0 is useless and will be peepholed away but we need to keep it
2961 // for its glue output.
2962 SDValue X = N->getOperand(0);
2963 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1).getNode());
2964 if (C && C->getSExtValue() < 0 && Subtarget->isThumb()) {
2965 int64_t Addend = -C->getSExtValue();
2967 SDNode *Add = nullptr;
2968 // ADDS can be better than CMN if the immediate fits in a
2969 // 16-bit ADDS, which means either [0,256) for tADDi8 or [0,8) for tADDi3.
2970 // Outside that range we can just use a CMN which is 32-bit but has a
2971 // 12-bit immediate range.
2972 if (Addend < 1<<8) {
2973 if (Subtarget->isThumb2()) {
2974 SDValue Ops[] = { X, CurDAG->getTargetConstant(Addend, dl, MVT::i32),
2975 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2976 CurDAG->getRegister(0, MVT::i32) };
2977 Add = CurDAG->getMachineNode(ARM::t2ADDri, dl, MVT::i32, Ops);
2979 unsigned Opc = (Addend < 1<<3) ? ARM::tADDi3 : ARM::tADDi8;
2980 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), X,
2981 CurDAG->getTargetConstant(Addend, dl, MVT::i32),
2982 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
2983 Add = CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2987 SDValue Ops2[] = {SDValue(Add, 0), CurDAG->getConstant(0, dl, MVT::i32)};
2988 CurDAG->MorphNodeTo(N, ARMISD::CMPZ, CurDAG->getVTList(MVT::Glue), Ops2);
2991 // Other cases are autogenerated.
2995 case ARMISD::CMOV: {
2996 SDValue InFlag = N->getOperand(4);
2998 if (InFlag.getOpcode() == ARMISD::CMPZ) {
2999 bool SwitchEQNEToPLMI;
3000 SelectCMPZ(InFlag.getNode(), SwitchEQNEToPLMI);
3002 if (SwitchEQNEToPLMI) {
3003 SDValue ARMcc = N->getOperand(2);
3004 ARMCC::CondCodes CC =
3005 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
3008 default: llvm_unreachable("CMPZ must be either NE or EQ!");
3016 SDValue NewARMcc = CurDAG->getConstant((unsigned)CC, dl, MVT::i32);
3017 SDValue Ops[] = {N->getOperand(0), N->getOperand(1), NewARMcc,
3018 N->getOperand(3), N->getOperand(4)};
3019 CurDAG->MorphNodeTo(N, ARMISD::CMOV, N->getVTList(), Ops);
3023 // Other cases are autogenerated.
3027 case ARMISD::VZIP: {
3029 EVT VT = N->getValueType(0);
3030 switch (VT.getSimpleVT().SimpleTy) {
3032 case MVT::v8i8: Opc = ARM::VZIPd8; break;
3033 case MVT::v4i16: Opc = ARM::VZIPd16; break;
3035 // vzip.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
3036 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3037 case MVT::v16i8: Opc = ARM::VZIPq8; break;
3038 case MVT::v8i16: Opc = ARM::VZIPq16; break;
3040 case MVT::v4i32: Opc = ARM::VZIPq32; break;
3042 SDValue Pred = getAL(CurDAG, dl);
3043 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3044 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
3045 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3048 case ARMISD::VUZP: {
3050 EVT VT = N->getValueType(0);
3051 switch (VT.getSimpleVT().SimpleTy) {
3053 case MVT::v8i8: Opc = ARM::VUZPd8; break;
3054 case MVT::v4i16: Opc = ARM::VUZPd16; break;
3056 // vuzp.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
3057 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3058 case MVT::v16i8: Opc = ARM::VUZPq8; break;
3059 case MVT::v8i16: Opc = ARM::VUZPq16; break;
3061 case MVT::v4i32: Opc = ARM::VUZPq32; break;
3063 SDValue Pred = getAL(CurDAG, dl);
3064 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3065 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
3066 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3069 case ARMISD::VTRN: {
3071 EVT VT = N->getValueType(0);
3072 switch (VT.getSimpleVT().SimpleTy) {
3074 case MVT::v8i8: Opc = ARM::VTRNd8; break;
3075 case MVT::v4i16: Opc = ARM::VTRNd16; break;
3077 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3078 case MVT::v16i8: Opc = ARM::VTRNq8; break;
3079 case MVT::v8i16: Opc = ARM::VTRNq16; break;
3081 case MVT::v4i32: Opc = ARM::VTRNq32; break;
3083 SDValue Pred = getAL(CurDAG, dl);
3084 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3085 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
3086 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3089 case ARMISD::BUILD_VECTOR: {
3090 EVT VecVT = N->getValueType(0);
3091 EVT EltVT = VecVT.getVectorElementType();
3092 unsigned NumElts = VecVT.getVectorNumElements();
3093 if (EltVT == MVT::f64) {
3094 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
3096 N, createDRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
3099 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
3102 N, createSRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
3105 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
3107 createQuadSRegsNode(VecVT, N->getOperand(0), N->getOperand(1),
3108 N->getOperand(2), N->getOperand(3)));
3112 case ARMISD::VLD1DUP: {
3113 static const uint16_t DOpcodes[] = { ARM::VLD1DUPd8, ARM::VLD1DUPd16,
3115 static const uint16_t QOpcodes[] = { ARM::VLD1DUPq8, ARM::VLD1DUPq16,
3117 SelectVLDDup(N, /* IsIntrinsic= */ false, false, 1, DOpcodes, QOpcodes);
3121 case ARMISD::VLD2DUP: {
3122 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
3124 SelectVLDDup(N, /* IsIntrinsic= */ false, false, 2, Opcodes);
3128 case ARMISD::VLD3DUP: {
3129 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo,
3130 ARM::VLD3DUPd16Pseudo,
3131 ARM::VLD3DUPd32Pseudo };
3132 SelectVLDDup(N, /* IsIntrinsic= */ false, false, 3, Opcodes);
3136 case ARMISD::VLD4DUP: {
3137 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo,
3138 ARM::VLD4DUPd16Pseudo,
3139 ARM::VLD4DUPd32Pseudo };
3140 SelectVLDDup(N, /* IsIntrinsic= */ false, false, 4, Opcodes);
3144 case ARMISD::VLD1DUP_UPD: {
3145 static const uint16_t DOpcodes[] = { ARM::VLD1DUPd8wb_fixed,
3146 ARM::VLD1DUPd16wb_fixed,
3147 ARM::VLD1DUPd32wb_fixed };
3148 static const uint16_t QOpcodes[] = { ARM::VLD1DUPq8wb_fixed,
3149 ARM::VLD1DUPq16wb_fixed,
3150 ARM::VLD1DUPq32wb_fixed };
3151 SelectVLDDup(N, /* IsIntrinsic= */ false, true, 1, DOpcodes, QOpcodes);
3155 case ARMISD::VLD2DUP_UPD: {
3156 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8wb_fixed,
3157 ARM::VLD2DUPd16wb_fixed,
3158 ARM::VLD2DUPd32wb_fixed };
3159 SelectVLDDup(N, /* IsIntrinsic= */ false, true, 2, Opcodes);
3163 case ARMISD::VLD3DUP_UPD: {
3164 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo_UPD,
3165 ARM::VLD3DUPd16Pseudo_UPD,
3166 ARM::VLD3DUPd32Pseudo_UPD };
3167 SelectVLDDup(N, /* IsIntrinsic= */ false, true, 3, Opcodes);
3171 case ARMISD::VLD4DUP_UPD: {
3172 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo_UPD,
3173 ARM::VLD4DUPd16Pseudo_UPD,
3174 ARM::VLD4DUPd32Pseudo_UPD };
3175 SelectVLDDup(N, /* IsIntrinsic= */ false, true, 4, Opcodes);
3179 case ARMISD::VLD1_UPD: {
3180 static const uint16_t DOpcodes[] = { ARM::VLD1d8wb_fixed,
3181 ARM::VLD1d16wb_fixed,
3182 ARM::VLD1d32wb_fixed,
3183 ARM::VLD1d64wb_fixed };
3184 static const uint16_t QOpcodes[] = { ARM::VLD1q8wb_fixed,
3185 ARM::VLD1q16wb_fixed,
3186 ARM::VLD1q32wb_fixed,
3187 ARM::VLD1q64wb_fixed };
3188 SelectVLD(N, true, 1, DOpcodes, QOpcodes, nullptr);
3192 case ARMISD::VLD2_UPD: {
3193 static const uint16_t DOpcodes[] = { ARM::VLD2d8wb_fixed,
3194 ARM::VLD2d16wb_fixed,
3195 ARM::VLD2d32wb_fixed,
3196 ARM::VLD1q64wb_fixed};
3197 static const uint16_t QOpcodes[] = { ARM::VLD2q8PseudoWB_fixed,
3198 ARM::VLD2q16PseudoWB_fixed,
3199 ARM::VLD2q32PseudoWB_fixed };
3200 SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr);
3204 case ARMISD::VLD3_UPD: {
3205 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo_UPD,
3206 ARM::VLD3d16Pseudo_UPD,
3207 ARM::VLD3d32Pseudo_UPD,
3208 ARM::VLD1d64TPseudoWB_fixed};
3209 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3210 ARM::VLD3q16Pseudo_UPD,
3211 ARM::VLD3q32Pseudo_UPD };
3212 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
3213 ARM::VLD3q16oddPseudo_UPD,
3214 ARM::VLD3q32oddPseudo_UPD };
3215 SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
3219 case ARMISD::VLD4_UPD: {
3220 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo_UPD,
3221 ARM::VLD4d16Pseudo_UPD,
3222 ARM::VLD4d32Pseudo_UPD,
3223 ARM::VLD1d64QPseudoWB_fixed};
3224 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3225 ARM::VLD4q16Pseudo_UPD,
3226 ARM::VLD4q32Pseudo_UPD };
3227 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
3228 ARM::VLD4q16oddPseudo_UPD,
3229 ARM::VLD4q32oddPseudo_UPD };
3230 SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
3234 case ARMISD::VLD2LN_UPD: {
3235 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD,
3236 ARM::VLD2LNd16Pseudo_UPD,
3237 ARM::VLD2LNd32Pseudo_UPD };
3238 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD,
3239 ARM::VLD2LNq32Pseudo_UPD };
3240 SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes);
3244 case ARMISD::VLD3LN_UPD: {
3245 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD,
3246 ARM::VLD3LNd16Pseudo_UPD,
3247 ARM::VLD3LNd32Pseudo_UPD };
3248 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD,
3249 ARM::VLD3LNq32Pseudo_UPD };
3250 SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes);
3254 case ARMISD::VLD4LN_UPD: {
3255 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD,
3256 ARM::VLD4LNd16Pseudo_UPD,
3257 ARM::VLD4LNd32Pseudo_UPD };
3258 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD,
3259 ARM::VLD4LNq32Pseudo_UPD };
3260 SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes);
3264 case ARMISD::VST1_UPD: {
3265 static const uint16_t DOpcodes[] = { ARM::VST1d8wb_fixed,
3266 ARM::VST1d16wb_fixed,
3267 ARM::VST1d32wb_fixed,
3268 ARM::VST1d64wb_fixed };
3269 static const uint16_t QOpcodes[] = { ARM::VST1q8wb_fixed,
3270 ARM::VST1q16wb_fixed,
3271 ARM::VST1q32wb_fixed,
3272 ARM::VST1q64wb_fixed };
3273 SelectVST(N, true, 1, DOpcodes, QOpcodes, nullptr);
3277 case ARMISD::VST2_UPD: {
3278 static const uint16_t DOpcodes[] = { ARM::VST2d8wb_fixed,
3279 ARM::VST2d16wb_fixed,
3280 ARM::VST2d32wb_fixed,
3281 ARM::VST1q64wb_fixed};
3282 static const uint16_t QOpcodes[] = { ARM::VST2q8PseudoWB_fixed,
3283 ARM::VST2q16PseudoWB_fixed,
3284 ARM::VST2q32PseudoWB_fixed };
3285 SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr);
3289 case ARMISD::VST3_UPD: {
3290 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo_UPD,
3291 ARM::VST3d16Pseudo_UPD,
3292 ARM::VST3d32Pseudo_UPD,
3293 ARM::VST1d64TPseudoWB_fixed};
3294 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3295 ARM::VST3q16Pseudo_UPD,
3296 ARM::VST3q32Pseudo_UPD };
3297 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
3298 ARM::VST3q16oddPseudo_UPD,
3299 ARM::VST3q32oddPseudo_UPD };
3300 SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
3304 case ARMISD::VST4_UPD: {
3305 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo_UPD,
3306 ARM::VST4d16Pseudo_UPD,
3307 ARM::VST4d32Pseudo_UPD,
3308 ARM::VST1d64QPseudoWB_fixed};
3309 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3310 ARM::VST4q16Pseudo_UPD,
3311 ARM::VST4q32Pseudo_UPD };
3312 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
3313 ARM::VST4q16oddPseudo_UPD,
3314 ARM::VST4q32oddPseudo_UPD };
3315 SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
3319 case ARMISD::VST2LN_UPD: {
3320 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD,
3321 ARM::VST2LNd16Pseudo_UPD,
3322 ARM::VST2LNd32Pseudo_UPD };
3323 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD,
3324 ARM::VST2LNq32Pseudo_UPD };
3325 SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes);
3329 case ARMISD::VST3LN_UPD: {
3330 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD,
3331 ARM::VST3LNd16Pseudo_UPD,
3332 ARM::VST3LNd32Pseudo_UPD };
3333 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD,
3334 ARM::VST3LNq32Pseudo_UPD };
3335 SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes);
3339 case ARMISD::VST4LN_UPD: {
3340 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD,
3341 ARM::VST4LNd16Pseudo_UPD,
3342 ARM::VST4LNd32Pseudo_UPD };
3343 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD,
3344 ARM::VST4LNq32Pseudo_UPD };
3345 SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes);
3349 case ISD::INTRINSIC_VOID:
3350 case ISD::INTRINSIC_W_CHAIN: {
3351 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
3356 case Intrinsic::arm_mrrc:
3357 case Intrinsic::arm_mrrc2: {
3359 SDValue Chain = N->getOperand(0);
3362 if (Subtarget->isThumb())
3363 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::t2MRRC : ARM::t2MRRC2);
3365 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::MRRC : ARM::MRRC2);
3367 SmallVector<SDValue, 5> Ops;
3368 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(), dl)); /* coproc */
3369 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(3))->getZExtValue(), dl)); /* opc */
3370 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(4))->getZExtValue(), dl)); /* CRm */
3372 // The mrrc2 instruction in ARM doesn't allow predicates, the top 4 bits of the encoded
3373 // instruction will always be '1111' but it is possible in assembly language to specify
3374 // AL as a predicate to mrrc2 but it doesn't make any difference to the encoded instruction.
3375 if (Opc != ARM::MRRC2) {
3376 Ops.push_back(getAL(CurDAG, dl));
3377 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3380 Ops.push_back(Chain);
3382 // Writes to two registers.
3383 const EVT RetType[] = {MVT::i32, MVT::i32, MVT::Other};
3385 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, RetType, Ops));
3388 case Intrinsic::arm_ldaexd:
3389 case Intrinsic::arm_ldrexd: {
3391 SDValue Chain = N->getOperand(0);
3392 SDValue MemAddr = N->getOperand(2);
3393 bool isThumb = Subtarget->isThumb() && Subtarget->hasV8MBaselineOps();
3395 bool IsAcquire = IntNo == Intrinsic::arm_ldaexd;
3396 unsigned NewOpc = isThumb ? (IsAcquire ? ARM::t2LDAEXD : ARM::t2LDREXD)
3397 : (IsAcquire ? ARM::LDAEXD : ARM::LDREXD);
3399 // arm_ldrexd returns a i64 value in {i32, i32}
3400 std::vector<EVT> ResTys;
3402 ResTys.push_back(MVT::i32);
3403 ResTys.push_back(MVT::i32);
3405 ResTys.push_back(MVT::Untyped);
3406 ResTys.push_back(MVT::Other);
3408 // Place arguments in the right order.
3409 SDValue Ops[] = {MemAddr, getAL(CurDAG, dl),
3410 CurDAG->getRegister(0, MVT::i32), Chain};
3411 SDNode *Ld = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
3412 // Transfer memoperands.
3413 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3414 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3415 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
3418 SDValue OutChain = isThumb ? SDValue(Ld, 2) : SDValue(Ld, 1);
3419 if (!SDValue(N, 0).use_empty()) {
3422 Result = SDValue(Ld, 0);
3425 CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
3426 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
3427 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
3428 Result = SDValue(ResNode,0);
3430 ReplaceUses(SDValue(N, 0), Result);
3432 if (!SDValue(N, 1).use_empty()) {
3435 Result = SDValue(Ld, 1);
3438 CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
3439 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
3440 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
3441 Result = SDValue(ResNode,0);
3443 ReplaceUses(SDValue(N, 1), Result);
3445 ReplaceUses(SDValue(N, 2), OutChain);
3446 CurDAG->RemoveDeadNode(N);
3449 case Intrinsic::arm_stlexd:
3450 case Intrinsic::arm_strexd: {
3452 SDValue Chain = N->getOperand(0);
3453 SDValue Val0 = N->getOperand(2);
3454 SDValue Val1 = N->getOperand(3);
3455 SDValue MemAddr = N->getOperand(4);
3457 // Store exclusive double return a i32 value which is the return status
3458 // of the issued store.
3459 const EVT ResTys[] = {MVT::i32, MVT::Other};
3461 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
3462 // Place arguments in the right order.
3463 SmallVector<SDValue, 7> Ops;
3465 Ops.push_back(Val0);
3466 Ops.push_back(Val1);
3468 // arm_strexd uses GPRPair.
3469 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, Val0, Val1), 0));
3470 Ops.push_back(MemAddr);
3471 Ops.push_back(getAL(CurDAG, dl));
3472 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3473 Ops.push_back(Chain);
3475 bool IsRelease = IntNo == Intrinsic::arm_stlexd;
3476 unsigned NewOpc = isThumb ? (IsRelease ? ARM::t2STLEXD : ARM::t2STREXD)
3477 : (IsRelease ? ARM::STLEXD : ARM::STREXD);
3479 SDNode *St = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
3480 // Transfer memoperands.
3481 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3482 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3483 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
3489 case Intrinsic::arm_neon_vld1: {
3490 static const uint16_t DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
3491 ARM::VLD1d32, ARM::VLD1d64 };
3492 static const uint16_t QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
3493 ARM::VLD1q32, ARM::VLD1q64};
3494 SelectVLD(N, false, 1, DOpcodes, QOpcodes, nullptr);
3498 case Intrinsic::arm_neon_vld1x2: {
3499 static const uint16_t DOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
3500 ARM::VLD1q32, ARM::VLD1q64 };
3501 static const uint16_t QOpcodes[] = { ARM::VLD1d8QPseudo,
3502 ARM::VLD1d16QPseudo,
3503 ARM::VLD1d32QPseudo,
3504 ARM::VLD1d64QPseudo };
3505 SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
3509 case Intrinsic::arm_neon_vld1x3: {
3510 static const uint16_t DOpcodes[] = { ARM::VLD1d8TPseudo,
3511 ARM::VLD1d16TPseudo,
3512 ARM::VLD1d32TPseudo,
3513 ARM::VLD1d64TPseudo };
3514 static const uint16_t QOpcodes0[] = { ARM::VLD1q8LowTPseudo_UPD,
3515 ARM::VLD1q16LowTPseudo_UPD,
3516 ARM::VLD1q32LowTPseudo_UPD,
3517 ARM::VLD1q64LowTPseudo_UPD };
3518 static const uint16_t QOpcodes1[] = { ARM::VLD1q8HighTPseudo,
3519 ARM::VLD1q16HighTPseudo,
3520 ARM::VLD1q32HighTPseudo,
3521 ARM::VLD1q64HighTPseudo };
3522 SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3526 case Intrinsic::arm_neon_vld1x4: {
3527 static const uint16_t DOpcodes[] = { ARM::VLD1d8QPseudo,
3528 ARM::VLD1d16QPseudo,
3529 ARM::VLD1d32QPseudo,
3530 ARM::VLD1d64QPseudo };
3531 static const uint16_t QOpcodes0[] = { ARM::VLD1q8LowQPseudo_UPD,
3532 ARM::VLD1q16LowQPseudo_UPD,
3533 ARM::VLD1q32LowQPseudo_UPD,
3534 ARM::VLD1q64LowQPseudo_UPD };
3535 static const uint16_t QOpcodes1[] = { ARM::VLD1q8HighQPseudo,
3536 ARM::VLD1q16HighQPseudo,
3537 ARM::VLD1q32HighQPseudo,
3538 ARM::VLD1q64HighQPseudo };
3539 SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3543 case Intrinsic::arm_neon_vld2: {
3544 static const uint16_t DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
3545 ARM::VLD2d32, ARM::VLD1q64 };
3546 static const uint16_t QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
3547 ARM::VLD2q32Pseudo };
3548 SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
3552 case Intrinsic::arm_neon_vld3: {
3553 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo,
3556 ARM::VLD1d64TPseudo };
3557 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3558 ARM::VLD3q16Pseudo_UPD,
3559 ARM::VLD3q32Pseudo_UPD };
3560 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo,
3561 ARM::VLD3q16oddPseudo,
3562 ARM::VLD3q32oddPseudo };
3563 SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3567 case Intrinsic::arm_neon_vld4: {
3568 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo,
3571 ARM::VLD1d64QPseudo };
3572 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3573 ARM::VLD4q16Pseudo_UPD,
3574 ARM::VLD4q32Pseudo_UPD };
3575 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo,
3576 ARM::VLD4q16oddPseudo,
3577 ARM::VLD4q32oddPseudo };
3578 SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3582 case Intrinsic::arm_neon_vld2dup: {
3583 static const uint16_t DOpcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
3584 ARM::VLD2DUPd32, ARM::VLD1q64 };
3585 static const uint16_t QOpcodes0[] = { ARM::VLD2DUPq8EvenPseudo,
3586 ARM::VLD2DUPq16EvenPseudo,
3587 ARM::VLD2DUPq32EvenPseudo };
3588 static const uint16_t QOpcodes1[] = { ARM::VLD2DUPq8OddPseudo,
3589 ARM::VLD2DUPq16OddPseudo,
3590 ARM::VLD2DUPq32OddPseudo };
3591 SelectVLDDup(N, /* IsIntrinsic= */ true, false, 2,
3592 DOpcodes, QOpcodes0, QOpcodes1);
3596 case Intrinsic::arm_neon_vld3dup: {
3597 static const uint16_t DOpcodes[] = { ARM::VLD3DUPd8Pseudo,
3598 ARM::VLD3DUPd16Pseudo,
3599 ARM::VLD3DUPd32Pseudo,
3600 ARM::VLD1d64TPseudo };
3601 static const uint16_t QOpcodes0[] = { ARM::VLD3DUPq8EvenPseudo,
3602 ARM::VLD3DUPq16EvenPseudo,
3603 ARM::VLD3DUPq32EvenPseudo };
3604 static const uint16_t QOpcodes1[] = { ARM::VLD3DUPq8OddPseudo,
3605 ARM::VLD3DUPq16OddPseudo,
3606 ARM::VLD3DUPq32OddPseudo };
3607 SelectVLDDup(N, /* IsIntrinsic= */ true, false, 3,
3608 DOpcodes, QOpcodes0, QOpcodes1);
3612 case Intrinsic::arm_neon_vld4dup: {
3613 static const uint16_t DOpcodes[] = { ARM::VLD4DUPd8Pseudo,
3614 ARM::VLD4DUPd16Pseudo,
3615 ARM::VLD4DUPd32Pseudo,
3616 ARM::VLD1d64QPseudo };
3617 static const uint16_t QOpcodes0[] = { ARM::VLD4DUPq8EvenPseudo,
3618 ARM::VLD4DUPq16EvenPseudo,
3619 ARM::VLD4DUPq32EvenPseudo };
3620 static const uint16_t QOpcodes1[] = { ARM::VLD4DUPq8OddPseudo,
3621 ARM::VLD4DUPq16OddPseudo,
3622 ARM::VLD4DUPq32OddPseudo };
3623 SelectVLDDup(N, /* IsIntrinsic= */ true, false, 4,
3624 DOpcodes, QOpcodes0, QOpcodes1);
3628 case Intrinsic::arm_neon_vld2lane: {
3629 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo,
3630 ARM::VLD2LNd16Pseudo,
3631 ARM::VLD2LNd32Pseudo };
3632 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo,
3633 ARM::VLD2LNq32Pseudo };
3634 SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes);
3638 case Intrinsic::arm_neon_vld3lane: {
3639 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo,
3640 ARM::VLD3LNd16Pseudo,
3641 ARM::VLD3LNd32Pseudo };
3642 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo,
3643 ARM::VLD3LNq32Pseudo };
3644 SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes);
3648 case Intrinsic::arm_neon_vld4lane: {
3649 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo,
3650 ARM::VLD4LNd16Pseudo,
3651 ARM::VLD4LNd32Pseudo };
3652 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo,
3653 ARM::VLD4LNq32Pseudo };
3654 SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes);
3658 case Intrinsic::arm_neon_vst1: {
3659 static const uint16_t DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
3660 ARM::VST1d32, ARM::VST1d64 };
3661 static const uint16_t QOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
3662 ARM::VST1q32, ARM::VST1q64 };
3663 SelectVST(N, false, 1, DOpcodes, QOpcodes, nullptr);
3667 case Intrinsic::arm_neon_vst1x2: {
3668 static const uint16_t DOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
3669 ARM::VST1q32, ARM::VST1q64 };
3670 static const uint16_t QOpcodes[] = { ARM::VST1d8QPseudo,
3671 ARM::VST1d16QPseudo,
3672 ARM::VST1d32QPseudo,
3673 ARM::VST1d64QPseudo };
3674 SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
3678 case Intrinsic::arm_neon_vst1x3: {
3679 static const uint16_t DOpcodes[] = { ARM::VST1d8TPseudo,
3680 ARM::VST1d16TPseudo,
3681 ARM::VST1d32TPseudo,
3682 ARM::VST1d64TPseudo };
3683 static const uint16_t QOpcodes0[] = { ARM::VST1q8LowTPseudo_UPD,
3684 ARM::VST1q16LowTPseudo_UPD,
3685 ARM::VST1q32LowTPseudo_UPD,
3686 ARM::VST1q64LowTPseudo_UPD };
3687 static const uint16_t QOpcodes1[] = { ARM::VST1q8HighTPseudo,
3688 ARM::VST1q16HighTPseudo,
3689 ARM::VST1q32HighTPseudo,
3690 ARM::VST1q64HighTPseudo };
3691 SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3695 case Intrinsic::arm_neon_vst1x4: {
3696 static const uint16_t DOpcodes[] = { ARM::VST1d8QPseudo,
3697 ARM::VST1d16QPseudo,
3698 ARM::VST1d32QPseudo,
3699 ARM::VST1d64QPseudo };
3700 static const uint16_t QOpcodes0[] = { ARM::VST1q8LowQPseudo_UPD,
3701 ARM::VST1q16LowQPseudo_UPD,
3702 ARM::VST1q32LowQPseudo_UPD,
3703 ARM::VST1q64LowQPseudo_UPD };
3704 static const uint16_t QOpcodes1[] = { ARM::VST1q8HighQPseudo,
3705 ARM::VST1q16HighQPseudo,
3706 ARM::VST1q32HighQPseudo,
3707 ARM::VST1q64HighQPseudo };
3708 SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3712 case Intrinsic::arm_neon_vst2: {
3713 static const uint16_t DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
3714 ARM::VST2d32, ARM::VST1q64 };
3715 static const uint16_t QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
3716 ARM::VST2q32Pseudo };
3717 SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
3721 case Intrinsic::arm_neon_vst3: {
3722 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo,
3725 ARM::VST1d64TPseudo };
3726 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3727 ARM::VST3q16Pseudo_UPD,
3728 ARM::VST3q32Pseudo_UPD };
3729 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo,
3730 ARM::VST3q16oddPseudo,
3731 ARM::VST3q32oddPseudo };
3732 SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3736 case Intrinsic::arm_neon_vst4: {
3737 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo,
3740 ARM::VST1d64QPseudo };
3741 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3742 ARM::VST4q16Pseudo_UPD,
3743 ARM::VST4q32Pseudo_UPD };
3744 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo,
3745 ARM::VST4q16oddPseudo,
3746 ARM::VST4q32oddPseudo };
3747 SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3751 case Intrinsic::arm_neon_vst2lane: {
3752 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo,
3753 ARM::VST2LNd16Pseudo,
3754 ARM::VST2LNd32Pseudo };
3755 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo,
3756 ARM::VST2LNq32Pseudo };
3757 SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes);
3761 case Intrinsic::arm_neon_vst3lane: {
3762 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo,
3763 ARM::VST3LNd16Pseudo,
3764 ARM::VST3LNd32Pseudo };
3765 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo,
3766 ARM::VST3LNq32Pseudo };
3767 SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes);
3771 case Intrinsic::arm_neon_vst4lane: {
3772 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo,
3773 ARM::VST4LNd16Pseudo,
3774 ARM::VST4LNd32Pseudo };
3775 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo,
3776 ARM::VST4LNq32Pseudo };
3777 SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes);
3784 case ISD::ATOMIC_CMP_SWAP:
3792 // Inspect a register string of the form
3793 // cp<coprocessor>:<opc1>:c<CRn>:c<CRm>:<opc2> (32bit) or
3794 // cp<coprocessor>:<opc1>:c<CRm> (64bit) inspect the fields of the string
3795 // and obtain the integer operands from them, adding these operands to the
3797 static void getIntOperandsFromRegisterString(StringRef RegString,
3798 SelectionDAG *CurDAG,
3800 std::vector<SDValue> &Ops) {
3801 SmallVector<StringRef, 5> Fields;
3802 RegString.split(Fields, ':');
3804 if (Fields.size() > 1) {
3805 bool AllIntFields = true;
3807 for (StringRef Field : Fields) {
3808 // Need to trim out leading 'cp' characters and get the integer field.
3810 AllIntFields &= !Field.trim("CPcp").getAsInteger(10, IntField);
3811 Ops.push_back(CurDAG->getTargetConstant(IntField, DL, MVT::i32));
3814 assert(AllIntFields &&
3815 "Unexpected non-integer value in special register string.");
3819 // Maps a Banked Register string to its mask value. The mask value returned is
3820 // for use in the MRSbanked / MSRbanked instruction nodes as the Banked Register
3821 // mask operand, which expresses which register is to be used, e.g. r8, and in
3822 // which mode it is to be used, e.g. usr. Returns -1 to signify that the string
3824 static inline int getBankedRegisterMask(StringRef RegString) {
3825 auto TheReg = ARMBankedReg::lookupBankedRegByName(RegString.lower());
3828 return TheReg->Encoding;
3831 // The flags here are common to those allowed for apsr in the A class cores and
3832 // those allowed for the special registers in the M class cores. Returns a
3833 // value representing which flags were present, -1 if invalid.
3834 static inline int getMClassFlagsMask(StringRef Flags) {
3835 return StringSwitch<int>(Flags)
3836 .Case("", 0x2) // no flags means nzcvq for psr registers, and 0x2 is
3837 // correct when flags are not permitted
3840 .Case("nzcvqg", 0x3)
3844 // Maps MClass special registers string to its value for use in the
3845 // t2MRS_M/t2MSR_M instruction nodes as the SYSm value operand.
3846 // Returns -1 to signify that the string was invalid.
3847 static int getMClassRegisterMask(StringRef Reg, const ARMSubtarget *Subtarget) {
3848 auto TheReg = ARMSysReg::lookupMClassSysRegByName(Reg);
3849 const FeatureBitset &FeatureBits = Subtarget->getFeatureBits();
3850 if (!TheReg || !TheReg->hasRequiredFeatures(FeatureBits))
3852 return (int)(TheReg->Encoding & 0xFFF); // SYSm value
3855 static int getARClassRegisterMask(StringRef Reg, StringRef Flags) {
3856 // The mask operand contains the special register (R Bit) in bit 4, whether
3857 // the register is spsr (R bit is 1) or one of cpsr/apsr (R bit is 0), and
3858 // bits 3-0 contains the fields to be accessed in the special register, set by
3859 // the flags provided with the register.
3861 if (Reg == "apsr") {
3862 // The flags permitted for apsr are the same flags that are allowed in
3863 // M class registers. We get the flag value and then shift the flags into
3864 // the correct place to combine with the mask.
3865 Mask = getMClassFlagsMask(Flags);
3871 if (Reg != "cpsr" && Reg != "spsr") {
3875 // This is the same as if the flags were "fc"
3876 if (Flags.empty() || Flags == "all")
3879 // Inspect the supplied flags string and set the bits in the mask for
3880 // the relevant and valid flags allowed for cpsr and spsr.
3881 for (char Flag : Flags) {
3900 // This avoids allowing strings where the same flag bit appears twice.
3901 if (!FlagVal || (Mask & FlagVal))
3906 // If the register is spsr then we need to set the R bit.
3913 // Lower the read_register intrinsic to ARM specific DAG nodes
3914 // using the supplied metadata string to select the instruction node to use
3915 // and the registers/masks to construct as operands for the node.
3916 bool ARMDAGToDAGISel::tryReadRegister(SDNode *N){
3917 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
3918 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
3919 bool IsThumb2 = Subtarget->isThumb2();
3922 std::vector<SDValue> Ops;
3923 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
3926 // If the special register string was constructed of fields (as defined
3927 // in the ACLE) then need to lower to MRC node (32 bit) or
3928 // MRRC node(64 bit), we can make the distinction based on the number of
3929 // operands we have.
3931 SmallVector<EVT, 3> ResTypes;
3932 if (Ops.size() == 5){
3933 Opcode = IsThumb2 ? ARM::t2MRC : ARM::MRC;
3934 ResTypes.append({ MVT::i32, MVT::Other });
3936 assert(Ops.size() == 3 &&
3937 "Invalid number of fields in special register string.");
3938 Opcode = IsThumb2 ? ARM::t2MRRC : ARM::MRRC;
3939 ResTypes.append({ MVT::i32, MVT::i32, MVT::Other });
3942 Ops.push_back(getAL(CurDAG, DL));
3943 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3944 Ops.push_back(N->getOperand(0));
3945 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, ResTypes, Ops));
3949 std::string SpecialReg = RegString->getString().lower();
3951 int BankedReg = getBankedRegisterMask(SpecialReg);
3952 if (BankedReg != -1) {
3953 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32),
3954 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3957 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSbanked : ARM::MRSbanked,
3958 DL, MVT::i32, MVT::Other, Ops));
3962 // The VFP registers are read by creating SelectionDAG nodes with opcodes
3963 // corresponding to the register that is being read from. So we switch on the
3964 // string to find which opcode we need to use.
3965 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
3966 .Case("fpscr", ARM::VMRS)
3967 .Case("fpexc", ARM::VMRS_FPEXC)
3968 .Case("fpsid", ARM::VMRS_FPSID)
3969 .Case("mvfr0", ARM::VMRS_MVFR0)
3970 .Case("mvfr1", ARM::VMRS_MVFR1)
3971 .Case("mvfr2", ARM::VMRS_MVFR2)
3972 .Case("fpinst", ARM::VMRS_FPINST)
3973 .Case("fpinst2", ARM::VMRS_FPINST2)
3976 // If an opcode was found then we can lower the read to a VFP instruction.
3978 if (!Subtarget->hasVFP2())
3980 if (Opcode == ARM::VMRS_MVFR2 && !Subtarget->hasFPARMv8())
3983 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
3986 CurDAG->getMachineNode(Opcode, DL, MVT::i32, MVT::Other, Ops));
3990 // If the target is M Class then need to validate that the register string
3991 // is an acceptable value, so check that a mask can be constructed from the
3993 if (Subtarget->isMClass()) {
3994 int SYSmValue = getMClassRegisterMask(SpecialReg, Subtarget);
3995 if (SYSmValue == -1)
3998 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
3999 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4002 N, CurDAG->getMachineNode(ARM::t2MRS_M, DL, MVT::i32, MVT::Other, Ops));
4006 // Here we know the target is not M Class so we need to check if it is one
4007 // of the remaining possible values which are apsr, cpsr or spsr.
4008 if (SpecialReg == "apsr" || SpecialReg == "cpsr") {
4009 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4011 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRS_AR : ARM::MRS,
4012 DL, MVT::i32, MVT::Other, Ops));
4016 if (SpecialReg == "spsr") {
4017 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4020 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSsys_AR : ARM::MRSsys, DL,
4021 MVT::i32, MVT::Other, Ops));
4028 // Lower the write_register intrinsic to ARM specific DAG nodes
4029 // using the supplied metadata string to select the instruction node to use
4030 // and the registers/masks to use in the nodes
4031 bool ARMDAGToDAGISel::tryWriteRegister(SDNode *N){
4032 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
4033 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
4034 bool IsThumb2 = Subtarget->isThumb2();
4037 std::vector<SDValue> Ops;
4038 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
4041 // If the special register string was constructed of fields (as defined
4042 // in the ACLE) then need to lower to MCR node (32 bit) or
4043 // MCRR node(64 bit), we can make the distinction based on the number of
4044 // operands we have.
4046 if (Ops.size() == 5) {
4047 Opcode = IsThumb2 ? ARM::t2MCR : ARM::MCR;
4048 Ops.insert(Ops.begin()+2, N->getOperand(2));
4050 assert(Ops.size() == 3 &&
4051 "Invalid number of fields in special register string.");
4052 Opcode = IsThumb2 ? ARM::t2MCRR : ARM::MCRR;
4053 SDValue WriteValue[] = { N->getOperand(2), N->getOperand(3) };
4054 Ops.insert(Ops.begin()+2, WriteValue, WriteValue+2);
4057 Ops.push_back(getAL(CurDAG, DL));
4058 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
4059 Ops.push_back(N->getOperand(0));
4061 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
4065 std::string SpecialReg = RegString->getString().lower();
4066 int BankedReg = getBankedRegisterMask(SpecialReg);
4067 if (BankedReg != -1) {
4068 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32), N->getOperand(2),
4069 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4072 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSRbanked : ARM::MSRbanked,
4073 DL, MVT::Other, Ops));
4077 // The VFP registers are written to by creating SelectionDAG nodes with
4078 // opcodes corresponding to the register that is being written. So we switch
4079 // on the string to find which opcode we need to use.
4080 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
4081 .Case("fpscr", ARM::VMSR)
4082 .Case("fpexc", ARM::VMSR_FPEXC)
4083 .Case("fpsid", ARM::VMSR_FPSID)
4084 .Case("fpinst", ARM::VMSR_FPINST)
4085 .Case("fpinst2", ARM::VMSR_FPINST2)
4089 if (!Subtarget->hasVFP2())
4091 Ops = { N->getOperand(2), getAL(CurDAG, DL),
4092 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
4093 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
4097 std::pair<StringRef, StringRef> Fields;
4098 Fields = StringRef(SpecialReg).rsplit('_');
4099 std::string Reg = Fields.first.str();
4100 StringRef Flags = Fields.second;
4102 // If the target was M Class then need to validate the special register value
4103 // and retrieve the mask for use in the instruction node.
4104 if (Subtarget->isMClass()) {
4105 int SYSmValue = getMClassRegisterMask(SpecialReg, Subtarget);
4106 if (SYSmValue == -1)
4109 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
4110 N->getOperand(2), getAL(CurDAG, DL),
4111 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
4112 ReplaceNode(N, CurDAG->getMachineNode(ARM::t2MSR_M, DL, MVT::Other, Ops));
4116 // We then check to see if a valid mask can be constructed for one of the
4117 // register string values permitted for the A and R class cores. These values
4118 // are apsr, spsr and cpsr; these are also valid on older cores.
4119 int Mask = getARClassRegisterMask(Reg, Flags);
4121 Ops = { CurDAG->getTargetConstant(Mask, DL, MVT::i32), N->getOperand(2),
4122 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4124 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSR_AR : ARM::MSR,
4125 DL, MVT::Other, Ops));
4132 bool ARMDAGToDAGISel::tryInlineAsm(SDNode *N){
4133 std::vector<SDValue> AsmNodeOperands;
4134 unsigned Flag, Kind;
4135 bool Changed = false;
4136 unsigned NumOps = N->getNumOperands();
4138 // Normally, i64 data is bounded to two arbitrary GRPs for "%r" constraint.
4139 // However, some instrstions (e.g. ldrexd/strexd in ARM mode) require
4140 // (even/even+1) GPRs and use %n and %Hn to refer to the individual regs
4141 // respectively. Since there is no constraint to explicitly specify a
4142 // reg pair, we use GPRPair reg class for "%r" for 64-bit data. For Thumb,
4143 // the 64-bit data may be referred by H, Q, R modifiers, so we still pack
4144 // them into a GPRPair.
4147 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps-1)
4148 : SDValue(nullptr,0);
4150 SmallVector<bool, 8> OpChanged;
4151 // Glue node will be appended late.
4152 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) {
4153 SDValue op = N->getOperand(i);
4154 AsmNodeOperands.push_back(op);
4156 if (i < InlineAsm::Op_FirstOperand)
4159 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(i))) {
4160 Flag = C->getZExtValue();
4161 Kind = InlineAsm::getKind(Flag);
4166 // Immediate operands to inline asm in the SelectionDAG are modeled with
4167 // two operands. The first is a constant of value InlineAsm::Kind_Imm, and
4168 // the second is a constant with the value of the immediate. If we get here
4169 // and we have a Kind_Imm, skip the next operand, and continue.
4170 if (Kind == InlineAsm::Kind_Imm) {
4171 SDValue op = N->getOperand(++i);
4172 AsmNodeOperands.push_back(op);
4176 unsigned NumRegs = InlineAsm::getNumOperandRegisters(Flag);
4178 OpChanged.push_back(false);
4180 unsigned DefIdx = 0;
4181 bool IsTiedToChangedOp = false;
4182 // If it's a use that is tied with a previous def, it has no
4183 // reg class constraint.
4184 if (Changed && InlineAsm::isUseOperandTiedToDef(Flag, DefIdx))
4185 IsTiedToChangedOp = OpChanged[DefIdx];
4187 // Memory operands to inline asm in the SelectionDAG are modeled with two
4188 // operands: a constant of value InlineAsm::Kind_Mem followed by the input
4189 // operand. If we get here and we have a Kind_Mem, skip the next operand (so
4190 // it doesn't get misinterpreted), and continue. We do this here because
4191 // it's important to update the OpChanged array correctly before moving on.
4192 if (Kind == InlineAsm::Kind_Mem) {
4193 SDValue op = N->getOperand(++i);
4194 AsmNodeOperands.push_back(op);
4198 if (Kind != InlineAsm::Kind_RegUse && Kind != InlineAsm::Kind_RegDef
4199 && Kind != InlineAsm::Kind_RegDefEarlyClobber)
4203 bool HasRC = InlineAsm::hasRegClassConstraint(Flag, RC);
4204 if ((!IsTiedToChangedOp && (!HasRC || RC != ARM::GPRRegClassID))
4208 assert((i+2 < NumOps) && "Invalid number of operands in inline asm");
4209 SDValue V0 = N->getOperand(i+1);
4210 SDValue V1 = N->getOperand(i+2);
4211 unsigned Reg0 = cast<RegisterSDNode>(V0)->getReg();
4212 unsigned Reg1 = cast<RegisterSDNode>(V1)->getReg();
4214 MachineRegisterInfo &MRI = MF->getRegInfo();
4216 if (Kind == InlineAsm::Kind_RegDef ||
4217 Kind == InlineAsm::Kind_RegDefEarlyClobber) {
4218 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to
4219 // the original GPRs.
4221 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
4222 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
4223 SDValue Chain = SDValue(N,0);
4225 SDNode *GU = N->getGluedUser();
4226 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::Untyped,
4229 // Extract values from a GPRPair reg and copy to the original GPR reg.
4230 SDValue Sub0 = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32,
4232 SDValue Sub1 = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32,
4234 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0,
4235 RegCopy.getValue(1));
4236 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1));
4238 // Update the original glue user.
4239 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1);
4240 Ops.push_back(T1.getValue(1));
4241 CurDAG->UpdateNodeOperands(GU, Ops);
4244 // For Kind == InlineAsm::Kind_RegUse, we first copy two GPRs into a
4245 // GPRPair and then pass the GPRPair to the inline asm.
4246 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain];
4248 // As REG_SEQ doesn't take RegisterSDNode, we copy them first.
4249 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32,
4251 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32,
4253 SDValue Pair = SDValue(createGPRPairNode(MVT::Untyped, T0, T1), 0);
4255 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two
4256 // i32 VRs of inline asm with it.
4257 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
4258 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
4259 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1));
4261 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
4262 Glue = Chain.getValue(1);
4267 if(PairedReg.getNode()) {
4268 OpChanged[OpChanged.size() -1 ] = true;
4269 Flag = InlineAsm::getFlagWord(Kind, 1 /* RegNum*/);
4270 if (IsTiedToChangedOp)
4271 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, DefIdx);
4273 Flag = InlineAsm::getFlagWordForRegClass(Flag, ARM::GPRPairRegClassID);
4274 // Replace the current flag.
4275 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant(
4276 Flag, dl, MVT::i32);
4277 // Add the new register node and skip the original two GPRs.
4278 AsmNodeOperands.push_back(PairedReg);
4279 // Skip the next two GPRs.
4285 AsmNodeOperands.push_back(Glue);
4289 SDValue New = CurDAG->getNode(ISD::INLINEASM, SDLoc(N),
4290 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
4292 ReplaceNode(N, New.getNode());
4297 bool ARMDAGToDAGISel::
4298 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
4299 std::vector<SDValue> &OutOps) {
4300 switch(ConstraintID) {
4302 llvm_unreachable("Unexpected asm memory constraint");
4303 case InlineAsm::Constraint_i:
4304 // FIXME: It seems strange that 'i' is needed here since it's supposed to
4305 // be an immediate and not a memory constraint.
4307 case InlineAsm::Constraint_m:
4308 case InlineAsm::Constraint_o:
4309 case InlineAsm::Constraint_Q:
4310 case InlineAsm::Constraint_Um:
4311 case InlineAsm::Constraint_Un:
4312 case InlineAsm::Constraint_Uq:
4313 case InlineAsm::Constraint_Us:
4314 case InlineAsm::Constraint_Ut:
4315 case InlineAsm::Constraint_Uv:
4316 case InlineAsm::Constraint_Uy:
4317 // Require the address to be in a register. That is safe for all ARM
4318 // variants and it is hard to do anything much smarter without knowing
4319 // how the operand is used.
4320 OutOps.push_back(Op);
4326 /// createARMISelDag - This pass converts a legalized DAG into a
4327 /// ARM-specific DAG, ready for instruction scheduling.
4329 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
4330 CodeGenOpt::Level OptLevel) {
4331 return new ARMDAGToDAGISel(TM, OptLevel);