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 "llvm/ADT/StringSwitch.h"
19 #include "llvm/CodeGen/MachineFrameInfo.h"
20 #include "llvm/CodeGen/MachineFunction.h"
21 #include "llvm/CodeGen/MachineInstrBuilder.h"
22 #include "llvm/CodeGen/MachineRegisterInfo.h"
23 #include "llvm/CodeGen/SelectionDAG.h"
24 #include "llvm/CodeGen/SelectionDAGISel.h"
25 #include "llvm/IR/CallingConv.h"
26 #include "llvm/IR/Constants.h"
27 #include "llvm/IR/DerivedTypes.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Intrinsics.h"
30 #include "llvm/IR/LLVMContext.h"
31 #include "llvm/Support/CommandLine.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ErrorHandling.h"
34 #include "llvm/Target/TargetLowering.h"
35 #include "llvm/Target/TargetOptions.h"
39 #define DEBUG_TYPE "arm-isel"
42 DisableShifterOp("disable-shifter-op", cl::Hidden,
43 cl::desc("Disable isel of shifter-op"),
46 //===--------------------------------------------------------------------===//
47 /// ARMDAGToDAGISel - ARM specific code to select ARM machine
48 /// instructions for SelectionDAG operations.
53 AM2_BASE, // Simple AM2 (+-imm12)
54 AM2_SHOP // Shifter-op AM2
57 class ARMDAGToDAGISel : public SelectionDAGISel {
58 /// Subtarget - Keep a pointer to the ARMSubtarget around so that we can
59 /// make the right decision when generating code for different targets.
60 const ARMSubtarget *Subtarget;
63 explicit ARMDAGToDAGISel(ARMBaseTargetMachine &tm, CodeGenOpt::Level OptLevel)
64 : SelectionDAGISel(tm, OptLevel) {}
66 bool runOnMachineFunction(MachineFunction &MF) override {
67 // Reset the subtarget each time through.
68 Subtarget = &MF.getSubtarget<ARMSubtarget>();
69 SelectionDAGISel::runOnMachineFunction(MF);
73 StringRef getPassName() const override { return "ARM Instruction Selection"; }
75 void PreprocessISelDAG() override;
77 /// getI32Imm - Return a target constant of type i32 with the specified
79 inline SDValue getI32Imm(unsigned Imm, const SDLoc &dl) {
80 return CurDAG->getTargetConstant(Imm, dl, MVT::i32);
83 void Select(SDNode *N) override;
85 bool hasNoVMLxHazardUse(SDNode *N) const;
86 bool isShifterOpProfitable(const SDValue &Shift,
87 ARM_AM::ShiftOpc ShOpcVal, unsigned ShAmt);
88 bool SelectRegShifterOperand(SDValue N, SDValue &A,
89 SDValue &B, SDValue &C,
90 bool CheckProfitability = true);
91 bool SelectImmShifterOperand(SDValue N, SDValue &A,
92 SDValue &B, bool CheckProfitability = true);
93 bool SelectShiftRegShifterOperand(SDValue N, SDValue &A,
94 SDValue &B, SDValue &C) {
95 // Don't apply the profitability check
96 return SelectRegShifterOperand(N, A, B, C, false);
98 bool SelectShiftImmShifterOperand(SDValue N, SDValue &A,
100 // Don't apply the profitability check
101 return SelectImmShifterOperand(N, A, B, false);
104 bool SelectAddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
105 bool SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset, SDValue &Opc);
107 AddrMode2Type SelectAddrMode2Worker(SDValue N, SDValue &Base,
108 SDValue &Offset, SDValue &Opc);
109 bool SelectAddrMode2Base(SDValue N, SDValue &Base, SDValue &Offset,
111 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_BASE;
114 bool SelectAddrMode2ShOp(SDValue N, SDValue &Base, SDValue &Offset,
116 return SelectAddrMode2Worker(N, Base, Offset, Opc) == AM2_SHOP;
119 bool SelectAddrMode2(SDValue N, SDValue &Base, SDValue &Offset,
121 SelectAddrMode2Worker(N, Base, Offset, Opc);
122 // return SelectAddrMode2ShOp(N, Base, Offset, Opc);
123 // This always matches one way or another.
127 bool SelectCMOVPred(SDValue N, SDValue &Pred, SDValue &Reg) {
128 const ConstantSDNode *CN = cast<ConstantSDNode>(N);
129 Pred = CurDAG->getTargetConstant(CN->getZExtValue(), SDLoc(N), MVT::i32);
130 Reg = CurDAG->getRegister(ARM::CPSR, MVT::i32);
134 bool SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
135 SDValue &Offset, SDValue &Opc);
136 bool SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
137 SDValue &Offset, SDValue &Opc);
138 bool SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
139 SDValue &Offset, SDValue &Opc);
140 bool SelectAddrOffsetNone(SDValue N, SDValue &Base);
141 bool SelectAddrMode3(SDValue N, SDValue &Base,
142 SDValue &Offset, SDValue &Opc);
143 bool SelectAddrMode3Offset(SDNode *Op, SDValue N,
144 SDValue &Offset, SDValue &Opc);
145 bool SelectAddrMode5(SDValue N, SDValue &Base,
147 bool SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,SDValue &Align);
148 bool SelectAddrMode6Offset(SDNode *Op, SDValue N, SDValue &Offset);
150 bool SelectAddrModePC(SDValue N, SDValue &Offset, SDValue &Label);
152 // Thumb Addressing Modes:
153 bool SelectThumbAddrModeRR(SDValue N, SDValue &Base, SDValue &Offset);
154 bool SelectThumbAddrModeImm5S(SDValue N, unsigned Scale, SDValue &Base,
156 bool SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
158 bool SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
160 bool SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
162 bool SelectThumbAddrModeSP(SDValue N, SDValue &Base, SDValue &OffImm);
164 // Thumb 2 Addressing Modes:
165 bool SelectT2AddrModeImm12(SDValue N, SDValue &Base, SDValue &OffImm);
166 bool SelectT2AddrModeImm8(SDValue N, SDValue &Base,
168 bool SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
170 bool SelectT2AddrModeSoReg(SDValue N, SDValue &Base,
171 SDValue &OffReg, SDValue &ShImm);
172 bool SelectT2AddrModeExclusive(SDValue N, SDValue &Base, SDValue &OffImm);
174 inline bool is_so_imm(unsigned Imm) const {
175 return ARM_AM::getSOImmVal(Imm) != -1;
178 inline bool is_so_imm_not(unsigned Imm) const {
179 return ARM_AM::getSOImmVal(~Imm) != -1;
182 inline bool is_t2_so_imm(unsigned Imm) const {
183 return ARM_AM::getT2SOImmVal(Imm) != -1;
186 inline bool is_t2_so_imm_not(unsigned Imm) const {
187 return ARM_AM::getT2SOImmVal(~Imm) != -1;
190 // Include the pieces autogenerated from the target description.
191 #include "ARMGenDAGISel.inc"
194 void transferMemOperands(SDNode *Src, SDNode *Dst);
196 /// Indexed (pre/post inc/dec) load matching code for ARM.
197 bool tryARMIndexedLoad(SDNode *N);
198 bool tryT1IndexedLoad(SDNode *N);
199 bool tryT2IndexedLoad(SDNode *N);
201 /// SelectVLD - Select NEON load intrinsics. NumVecs should be
202 /// 1, 2, 3 or 4. The opcode arrays specify the instructions used for
203 /// loads of D registers and even subregs and odd subregs of Q registers.
204 /// For NumVecs <= 2, QOpcodes1 is not used.
205 void SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
206 const uint16_t *DOpcodes, const uint16_t *QOpcodes0,
207 const uint16_t *QOpcodes1);
209 /// SelectVST - Select NEON store intrinsics. NumVecs should
210 /// be 1, 2, 3 or 4. The opcode arrays specify the instructions used for
211 /// stores of D registers and even subregs and odd subregs of Q registers.
212 /// For NumVecs <= 2, QOpcodes1 is not used.
213 void SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
214 const uint16_t *DOpcodes, const uint16_t *QOpcodes0,
215 const uint16_t *QOpcodes1);
217 /// SelectVLDSTLane - Select NEON load/store lane intrinsics. NumVecs should
218 /// be 2, 3 or 4. The opcode arrays specify the instructions used for
219 /// load/store of D registers and Q registers.
220 void SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
221 unsigned NumVecs, const uint16_t *DOpcodes,
222 const uint16_t *QOpcodes);
224 /// SelectVLDDup - Select NEON load-duplicate intrinsics. NumVecs
225 /// should be 1, 2, 3 or 4. The opcode array specifies the instructions used
226 /// for loading D registers. (Q registers are not supported.)
227 void SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs,
228 const uint16_t *DOpcodes,
229 const uint16_t *QOpcodes = nullptr);
231 /// SelectVTBL - Select NEON VTBL and VTBX intrinsics. NumVecs should be 2,
232 /// 3 or 4. These are custom-selected so that a REG_SEQUENCE can be
233 /// generated to force the table registers to be consecutive.
234 void SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs, unsigned Opc);
236 /// Try to select SBFX/UBFX instructions for ARM.
237 bool tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned);
239 // Select special operations if node forms integer ABS pattern
240 bool tryABSOp(SDNode *N);
242 bool tryReadRegister(SDNode *N);
243 bool tryWriteRegister(SDNode *N);
245 bool tryInlineAsm(SDNode *N);
247 void SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI);
249 void SelectCMP_SWAP(SDNode *N);
251 /// SelectInlineAsmMemoryOperand - Implement addressing mode selection for
252 /// inline asm expressions.
253 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
254 std::vector<SDValue> &OutOps) override;
256 // Form pairs of consecutive R, S, D, or Q registers.
257 SDNode *createGPRPairNode(EVT VT, SDValue V0, SDValue V1);
258 SDNode *createSRegPairNode(EVT VT, SDValue V0, SDValue V1);
259 SDNode *createDRegPairNode(EVT VT, SDValue V0, SDValue V1);
260 SDNode *createQRegPairNode(EVT VT, SDValue V0, SDValue V1);
262 // Form sequences of 4 consecutive S, D, or Q registers.
263 SDNode *createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
264 SDNode *createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
265 SDNode *createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1, SDValue V2, SDValue V3);
267 // Get the alignment operand for a NEON VLD or VST instruction.
268 SDValue GetVLDSTAlign(SDValue Align, const SDLoc &dl, unsigned NumVecs,
271 /// Returns the number of instructions required to materialize the given
272 /// constant in a register, or 3 if a literal pool load is needed.
273 unsigned ConstantMaterializationCost(unsigned Val) const;
275 /// Checks if N is a multiplication by a constant where we can extract out a
276 /// power of two from the constant so that it can be used in a shift, but only
277 /// if it simplifies the materialization of the constant. Returns true if it
278 /// is, and assigns to PowerOfTwo the power of two that should be extracted
279 /// out and to NewMulConst the new constant to be multiplied by.
280 bool canExtractShiftFromMul(const SDValue &N, unsigned MaxShift,
281 unsigned &PowerOfTwo, SDValue &NewMulConst) const;
283 /// Replace N with M in CurDAG, in a way that also ensures that M gets
284 /// selected when N would have been selected.
285 void replaceDAGValue(const SDValue &N, SDValue M);
289 /// isInt32Immediate - This method tests to see if the node is a 32-bit constant
290 /// operand. If so Imm will receive the 32-bit value.
291 static bool isInt32Immediate(SDNode *N, unsigned &Imm) {
292 if (N->getOpcode() == ISD::Constant && N->getValueType(0) == MVT::i32) {
293 Imm = cast<ConstantSDNode>(N)->getZExtValue();
299 // isInt32Immediate - This method tests to see if a constant operand.
300 // If so Imm will receive the 32 bit value.
301 static bool isInt32Immediate(SDValue N, unsigned &Imm) {
302 return isInt32Immediate(N.getNode(), Imm);
305 // isOpcWithIntImmediate - This method tests to see if the node is a specific
306 // opcode and that it has a immediate integer right operand.
307 // If so Imm will receive the 32 bit value.
308 static bool isOpcWithIntImmediate(SDNode *N, unsigned Opc, unsigned& Imm) {
309 return N->getOpcode() == Opc &&
310 isInt32Immediate(N->getOperand(1).getNode(), Imm);
313 /// \brief Check whether a particular node is a constant value representable as
314 /// (N * Scale) where (N in [\p RangeMin, \p RangeMax).
316 /// \param ScaledConstant [out] - On success, the pre-scaled constant value.
317 static bool isScaledConstantInRange(SDValue Node, int Scale,
318 int RangeMin, int RangeMax,
319 int &ScaledConstant) {
320 assert(Scale > 0 && "Invalid scale!");
322 // Check that this is a constant.
323 const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Node);
327 ScaledConstant = (int) C->getZExtValue();
328 if ((ScaledConstant % Scale) != 0)
331 ScaledConstant /= Scale;
332 return ScaledConstant >= RangeMin && ScaledConstant < RangeMax;
335 void ARMDAGToDAGISel::PreprocessISelDAG() {
336 if (!Subtarget->hasV6T2Ops())
339 bool isThumb2 = Subtarget->isThumb();
340 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(),
341 E = CurDAG->allnodes_end(); I != E; ) {
342 SDNode *N = &*I++; // Preincrement iterator to avoid invalidation issues.
344 if (N->getOpcode() != ISD::ADD)
347 // Look for (add X1, (and (srl X2, c1), c2)) where c2 is constant with
348 // leading zeros, followed by consecutive set bits, followed by 1 or 2
349 // trailing zeros, e.g. 1020.
350 // Transform the expression to
351 // (add X1, (shl (and (srl X2, c1), (c2>>tz)), tz)) where tz is the number
352 // of trailing zeros of c2. The left shift would be folded as an shifter
353 // operand of 'add' and the 'and' and 'srl' would become a bits extraction
356 SDValue N0 = N->getOperand(0);
357 SDValue N1 = N->getOperand(1);
358 unsigned And_imm = 0;
359 if (!isOpcWithIntImmediate(N1.getNode(), ISD::AND, And_imm)) {
360 if (isOpcWithIntImmediate(N0.getNode(), ISD::AND, And_imm))
366 // Check if the AND mask is an immediate of the form: 000.....1111111100
367 unsigned TZ = countTrailingZeros(And_imm);
368 if (TZ != 1 && TZ != 2)
369 // Be conservative here. Shifter operands aren't always free. e.g. On
370 // Swift, left shifter operand of 1 / 2 for free but others are not.
372 // ubfx r3, r1, #16, #8
373 // ldr.w r3, [r0, r3, lsl #2]
376 // and.w r2, r9, r1, lsr #14
380 if (And_imm & (And_imm + 1))
383 // Look for (and (srl X, c1), c2).
384 SDValue Srl = N1.getOperand(0);
385 unsigned Srl_imm = 0;
386 if (!isOpcWithIntImmediate(Srl.getNode(), ISD::SRL, Srl_imm) ||
390 // Make sure first operand is not a shifter operand which would prevent
391 // folding of the left shift.
396 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1))
399 if (SelectImmShifterOperand(N0, CPTmp0, CPTmp1) ||
400 SelectRegShifterOperand(N0, CPTmp0, CPTmp1, CPTmp2))
404 // Now make the transformation.
405 Srl = CurDAG->getNode(ISD::SRL, SDLoc(Srl), MVT::i32,
407 CurDAG->getConstant(Srl_imm + TZ, SDLoc(Srl),
409 N1 = CurDAG->getNode(ISD::AND, SDLoc(N1), MVT::i32,
411 CurDAG->getConstant(And_imm, SDLoc(Srl), MVT::i32));
412 N1 = CurDAG->getNode(ISD::SHL, SDLoc(N1), MVT::i32,
413 N1, CurDAG->getConstant(TZ, SDLoc(Srl), MVT::i32));
414 CurDAG->UpdateNodeOperands(N, N0, N1);
418 /// hasNoVMLxHazardUse - Return true if it's desirable to select a FP MLA / MLS
419 /// node. VFP / NEON fp VMLA / VMLS instructions have special RAW hazards (at
420 /// least on current ARM implementations) which should be avoidded.
421 bool ARMDAGToDAGISel::hasNoVMLxHazardUse(SDNode *N) const {
422 if (OptLevel == CodeGenOpt::None)
425 if (!Subtarget->hasVMLxHazards())
431 SDNode *Use = *N->use_begin();
432 if (Use->getOpcode() == ISD::CopyToReg)
434 if (Use->isMachineOpcode()) {
435 const ARMBaseInstrInfo *TII = static_cast<const ARMBaseInstrInfo *>(
436 CurDAG->getSubtarget().getInstrInfo());
438 const MCInstrDesc &MCID = TII->get(Use->getMachineOpcode());
441 unsigned Opcode = MCID.getOpcode();
442 if (Opcode == ARM::VMOVRS || Opcode == ARM::VMOVRRD)
444 // vmlx feeding into another vmlx. We actually want to unfold
445 // the use later in the MLxExpansion pass. e.g.
447 // vmla (stall 8 cycles)
452 // This adds up to about 18 - 19 cycles.
455 // vmul (stall 4 cycles)
456 // vadd adds up to about 14 cycles.
457 return TII->isFpMLxInstruction(Opcode);
463 bool ARMDAGToDAGISel::isShifterOpProfitable(const SDValue &Shift,
464 ARM_AM::ShiftOpc ShOpcVal,
466 if (!Subtarget->isLikeA9() && !Subtarget->isSwift())
468 if (Shift.hasOneUse())
471 return ShOpcVal == ARM_AM::lsl &&
472 (ShAmt == 2 || (Subtarget->isSwift() && ShAmt == 1));
475 unsigned ARMDAGToDAGISel::ConstantMaterializationCost(unsigned Val) const {
476 if (Subtarget->isThumb()) {
477 if (Val <= 255) return 1; // MOV
478 if (Subtarget->hasV6T2Ops() &&
479 (Val <= 0xffff || ARM_AM::getT2SOImmValSplatVal(Val) != -1))
481 if (Val <= 510) return 2; // MOV + ADDi8
482 if (~Val <= 255) return 2; // MOV + MVN
483 if (ARM_AM::isThumbImmShiftedVal(Val)) return 2; // MOV + LSL
485 if (ARM_AM::getSOImmVal(Val) != -1) return 1; // MOV
486 if (ARM_AM::getSOImmVal(~Val) != -1) return 1; // MVN
487 if (Subtarget->hasV6T2Ops() && Val <= 0xffff) return 1; // MOVW
488 if (ARM_AM::isSOImmTwoPartVal(Val)) return 2; // two instrs
490 if (Subtarget->useMovt(*MF)) return 2; // MOVW + MOVT
491 return 3; // Literal pool load
494 bool ARMDAGToDAGISel::canExtractShiftFromMul(const SDValue &N,
496 unsigned &PowerOfTwo,
497 SDValue &NewMulConst) const {
498 assert(N.getOpcode() == ISD::MUL);
499 assert(MaxShift > 0);
501 // If the multiply is used in more than one place then changing the constant
502 // will make other uses incorrect, so don't.
503 if (!N.hasOneUse()) return false;
504 // Check if the multiply is by a constant
505 ConstantSDNode *MulConst = dyn_cast<ConstantSDNode>(N.getOperand(1));
506 if (!MulConst) return false;
507 // If the constant is used in more than one place then modifying it will mean
508 // we need to materialize two constants instead of one, which is a bad idea.
509 if (!MulConst->hasOneUse()) return false;
510 unsigned MulConstVal = MulConst->getZExtValue();
511 if (MulConstVal == 0) return false;
513 // Find the largest power of 2 that MulConstVal is a multiple of
514 PowerOfTwo = MaxShift;
515 while ((MulConstVal % (1 << PowerOfTwo)) != 0) {
517 if (PowerOfTwo == 0) return false;
520 // Only optimise if the new cost is better
521 unsigned NewMulConstVal = MulConstVal / (1 << PowerOfTwo);
522 NewMulConst = CurDAG->getConstant(NewMulConstVal, SDLoc(N), MVT::i32);
523 unsigned OldCost = ConstantMaterializationCost(MulConstVal);
524 unsigned NewCost = ConstantMaterializationCost(NewMulConstVal);
525 return NewCost < OldCost;
528 void ARMDAGToDAGISel::replaceDAGValue(const SDValue &N, SDValue M) {
529 CurDAG->RepositionNode(N.getNode()->getIterator(), M.getNode());
530 CurDAG->ReplaceAllUsesWith(N, M);
533 bool ARMDAGToDAGISel::SelectImmShifterOperand(SDValue N,
536 bool CheckProfitability) {
537 if (DisableShifterOp)
540 // If N is a multiply-by-constant and it's profitable to extract a shift and
541 // use it in a shifted operand do so.
542 if (N.getOpcode() == ISD::MUL) {
543 unsigned PowerOfTwo = 0;
545 if (canExtractShiftFromMul(N, 31, PowerOfTwo, NewMulConst)) {
546 HandleSDNode Handle(N);
547 replaceDAGValue(N.getOperand(1), NewMulConst);
548 BaseReg = Handle.getValue();
549 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ARM_AM::lsl,
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;
566 ShImmVal = RHS->getZExtValue() & 31;
567 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
572 bool ARMDAGToDAGISel::SelectRegShifterOperand(SDValue N,
576 bool CheckProfitability) {
577 if (DisableShifterOp)
580 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
582 // Don't match base register only case. That is matched to a separate
583 // lower complexity pattern with explicit register operand.
584 if (ShOpcVal == ARM_AM::no_shift) return false;
586 BaseReg = N.getOperand(0);
587 unsigned ShImmVal = 0;
588 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
589 if (RHS) return false;
591 ShReg = N.getOperand(1);
592 if (CheckProfitability && !isShifterOpProfitable(N, ShOpcVal, ShImmVal))
594 Opc = CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, ShImmVal),
600 bool ARMDAGToDAGISel::SelectAddrModeImm12(SDValue N,
603 // Match simple R + imm12 operands.
606 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
607 !CurDAG->isBaseWithConstantOffset(N)) {
608 if (N.getOpcode() == ISD::FrameIndex) {
609 // Match frame index.
610 int FI = cast<FrameIndexSDNode>(N)->getIndex();
611 Base = CurDAG->getTargetFrameIndex(
612 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
613 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
617 if (N.getOpcode() == ARMISD::Wrapper &&
618 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
619 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
620 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
621 Base = N.getOperand(0);
624 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
628 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
629 int RHSC = (int)RHS->getSExtValue();
630 if (N.getOpcode() == ISD::SUB)
633 if (RHSC > -0x1000 && RHSC < 0x1000) { // 12 bits
634 Base = N.getOperand(0);
635 if (Base.getOpcode() == ISD::FrameIndex) {
636 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
637 Base = CurDAG->getTargetFrameIndex(
638 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
640 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
647 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
653 bool ARMDAGToDAGISel::SelectLdStSOReg(SDValue N, SDValue &Base, SDValue &Offset,
655 if (N.getOpcode() == ISD::MUL &&
656 ((!Subtarget->isLikeA9() && !Subtarget->isSwift()) || N.hasOneUse())) {
657 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
658 // X * [3,5,9] -> X + X * [2,4,8] etc.
659 int RHSC = (int)RHS->getZExtValue();
662 ARM_AM::AddrOpc AddSub = ARM_AM::add;
664 AddSub = ARM_AM::sub;
667 if (isPowerOf2_32(RHSC)) {
668 unsigned ShAmt = Log2_32(RHSC);
669 Base = Offset = N.getOperand(0);
670 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
679 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
680 // ISD::OR that is equivalent to an ISD::ADD.
681 !CurDAG->isBaseWithConstantOffset(N))
684 // Leave simple R +/- imm12 operands for LDRi12
685 if (N.getOpcode() == ISD::ADD || N.getOpcode() == ISD::OR) {
687 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
688 -0x1000+1, 0x1000, RHSC)) // 12 bits.
692 // Otherwise this is R +/- [possibly shifted] R.
693 ARM_AM::AddrOpc AddSub = N.getOpcode() == ISD::SUB ? ARM_AM::sub:ARM_AM::add;
694 ARM_AM::ShiftOpc ShOpcVal =
695 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
698 Base = N.getOperand(0);
699 Offset = N.getOperand(1);
701 if (ShOpcVal != ARM_AM::no_shift) {
702 // Check to see if the RHS of the shift is a constant, if not, we can't fold
704 if (ConstantSDNode *Sh =
705 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
706 ShAmt = Sh->getZExtValue();
707 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
708 Offset = N.getOperand(1).getOperand(0);
711 ShOpcVal = ARM_AM::no_shift;
714 ShOpcVal = ARM_AM::no_shift;
718 // Try matching (R shl C) + (R).
719 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
720 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
721 N.getOperand(0).hasOneUse())) {
722 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
723 if (ShOpcVal != ARM_AM::no_shift) {
724 // Check to see if the RHS of the shift is a constant, if not, we can't
726 if (ConstantSDNode *Sh =
727 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
728 ShAmt = Sh->getZExtValue();
729 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
730 Offset = N.getOperand(0).getOperand(0);
731 Base = N.getOperand(1);
734 ShOpcVal = ARM_AM::no_shift;
737 ShOpcVal = ARM_AM::no_shift;
742 // If Offset is a multiply-by-constant and it's profitable to extract a shift
743 // and use it in a shifted operand do so.
744 if (Offset.getOpcode() == ISD::MUL && N.hasOneUse()) {
745 unsigned PowerOfTwo = 0;
747 if (canExtractShiftFromMul(Offset, 31, PowerOfTwo, NewMulConst)) {
748 replaceDAGValue(Offset.getOperand(1), NewMulConst);
750 ShOpcVal = ARM_AM::lsl;
754 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
762 AddrMode2Type ARMDAGToDAGISel::SelectAddrMode2Worker(SDValue N,
766 if (N.getOpcode() == ISD::MUL &&
767 (!(Subtarget->isLikeA9() || Subtarget->isSwift()) || N.hasOneUse())) {
768 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
769 // X * [3,5,9] -> X + X * [2,4,8] etc.
770 int RHSC = (int)RHS->getZExtValue();
773 ARM_AM::AddrOpc AddSub = ARM_AM::add;
775 AddSub = ARM_AM::sub;
778 if (isPowerOf2_32(RHSC)) {
779 unsigned ShAmt = Log2_32(RHSC);
780 Base = Offset = N.getOperand(0);
781 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt,
790 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
791 // ISD::OR that is equivalent to an ADD.
792 !CurDAG->isBaseWithConstantOffset(N)) {
794 if (N.getOpcode() == ISD::FrameIndex) {
795 int FI = cast<FrameIndexSDNode>(N)->getIndex();
796 Base = CurDAG->getTargetFrameIndex(
797 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
798 } else if (N.getOpcode() == ARMISD::Wrapper &&
799 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
800 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
801 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
802 Base = N.getOperand(0);
804 Offset = CurDAG->getRegister(0, MVT::i32);
805 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
811 // Match simple R +/- imm12 operands.
812 if (N.getOpcode() != ISD::SUB) {
814 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
815 -0x1000+1, 0x1000, RHSC)) { // 12 bits.
816 Base = N.getOperand(0);
817 if (Base.getOpcode() == ISD::FrameIndex) {
818 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
819 Base = CurDAG->getTargetFrameIndex(
820 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
822 Offset = CurDAG->getRegister(0, MVT::i32);
824 ARM_AM::AddrOpc AddSub = ARM_AM::add;
826 AddSub = ARM_AM::sub;
829 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, RHSC,
836 if ((Subtarget->isLikeA9() || Subtarget->isSwift()) && !N.hasOneUse()) {
837 // Compute R +/- (R << N) and reuse it.
839 Offset = CurDAG->getRegister(0, MVT::i32);
840 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(ARM_AM::add, 0,
846 // Otherwise this is R +/- [possibly shifted] R.
847 ARM_AM::AddrOpc AddSub = N.getOpcode() != ISD::SUB ? ARM_AM::add:ARM_AM::sub;
848 ARM_AM::ShiftOpc ShOpcVal =
849 ARM_AM::getShiftOpcForNode(N.getOperand(1).getOpcode());
852 Base = N.getOperand(0);
853 Offset = N.getOperand(1);
855 if (ShOpcVal != ARM_AM::no_shift) {
856 // Check to see if the RHS of the shift is a constant, if not, we can't fold
858 if (ConstantSDNode *Sh =
859 dyn_cast<ConstantSDNode>(N.getOperand(1).getOperand(1))) {
860 ShAmt = Sh->getZExtValue();
861 if (isShifterOpProfitable(Offset, ShOpcVal, ShAmt))
862 Offset = N.getOperand(1).getOperand(0);
865 ShOpcVal = ARM_AM::no_shift;
868 ShOpcVal = ARM_AM::no_shift;
872 // Try matching (R shl C) + (R).
873 if (N.getOpcode() != ISD::SUB && ShOpcVal == ARM_AM::no_shift &&
874 !(Subtarget->isLikeA9() || Subtarget->isSwift() ||
875 N.getOperand(0).hasOneUse())) {
876 ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOperand(0).getOpcode());
877 if (ShOpcVal != ARM_AM::no_shift) {
878 // Check to see if the RHS of the shift is a constant, if not, we can't
880 if (ConstantSDNode *Sh =
881 dyn_cast<ConstantSDNode>(N.getOperand(0).getOperand(1))) {
882 ShAmt = Sh->getZExtValue();
883 if (isShifterOpProfitable(N.getOperand(0), ShOpcVal, ShAmt)) {
884 Offset = N.getOperand(0).getOperand(0);
885 Base = N.getOperand(1);
888 ShOpcVal = ARM_AM::no_shift;
891 ShOpcVal = ARM_AM::no_shift;
896 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
901 bool ARMDAGToDAGISel::SelectAddrMode2OffsetReg(SDNode *Op, SDValue N,
902 SDValue &Offset, SDValue &Opc) {
903 unsigned Opcode = Op->getOpcode();
904 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
905 ? cast<LoadSDNode>(Op)->getAddressingMode()
906 : cast<StoreSDNode>(Op)->getAddressingMode();
907 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
908 ? ARM_AM::add : ARM_AM::sub;
910 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val))
914 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(N.getOpcode());
916 if (ShOpcVal != ARM_AM::no_shift) {
917 // Check to see if the RHS of the shift is a constant, if not, we can't fold
919 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
920 ShAmt = Sh->getZExtValue();
921 if (isShifterOpProfitable(N, ShOpcVal, ShAmt))
922 Offset = N.getOperand(0);
925 ShOpcVal = ARM_AM::no_shift;
928 ShOpcVal = ARM_AM::no_shift;
932 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, ShAmt, ShOpcVal),
937 bool ARMDAGToDAGISel::SelectAddrMode2OffsetImmPre(SDNode *Op, SDValue N,
938 SDValue &Offset, SDValue &Opc) {
939 unsigned Opcode = Op->getOpcode();
940 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
941 ? cast<LoadSDNode>(Op)->getAddressingMode()
942 : cast<StoreSDNode>(Op)->getAddressingMode();
943 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
944 ? ARM_AM::add : ARM_AM::sub;
946 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
947 if (AddSub == ARM_AM::sub) Val *= -1;
948 Offset = CurDAG->getRegister(0, MVT::i32);
949 Opc = CurDAG->getTargetConstant(Val, SDLoc(Op), MVT::i32);
957 bool ARMDAGToDAGISel::SelectAddrMode2OffsetImm(SDNode *Op, SDValue N,
958 SDValue &Offset, SDValue &Opc) {
959 unsigned Opcode = Op->getOpcode();
960 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
961 ? cast<LoadSDNode>(Op)->getAddressingMode()
962 : cast<StoreSDNode>(Op)->getAddressingMode();
963 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
964 ? ARM_AM::add : ARM_AM::sub;
966 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x1000, Val)) { // 12 bits.
967 Offset = CurDAG->getRegister(0, MVT::i32);
968 Opc = CurDAG->getTargetConstant(ARM_AM::getAM2Opc(AddSub, Val,
970 SDLoc(Op), MVT::i32);
977 bool ARMDAGToDAGISel::SelectAddrOffsetNone(SDValue N, SDValue &Base) {
982 bool ARMDAGToDAGISel::SelectAddrMode3(SDValue N,
983 SDValue &Base, SDValue &Offset,
985 if (N.getOpcode() == ISD::SUB) {
986 // X - C is canonicalize to X + -C, no need to handle it here.
987 Base = N.getOperand(0);
988 Offset = N.getOperand(1);
989 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::sub, 0), SDLoc(N),
994 if (!CurDAG->isBaseWithConstantOffset(N)) {
996 if (N.getOpcode() == ISD::FrameIndex) {
997 int FI = cast<FrameIndexSDNode>(N)->getIndex();
998 Base = CurDAG->getTargetFrameIndex(
999 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1001 Offset = CurDAG->getRegister(0, MVT::i32);
1002 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
1007 // If the RHS is +/- imm8, fold into addr mode.
1009 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/1,
1010 -256 + 1, 256, RHSC)) { // 8 bits.
1011 Base = N.getOperand(0);
1012 if (Base.getOpcode() == ISD::FrameIndex) {
1013 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1014 Base = CurDAG->getTargetFrameIndex(
1015 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1017 Offset = CurDAG->getRegister(0, MVT::i32);
1019 ARM_AM::AddrOpc AddSub = ARM_AM::add;
1021 AddSub = ARM_AM::sub;
1024 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, RHSC), SDLoc(N),
1029 Base = N.getOperand(0);
1030 Offset = N.getOperand(1);
1031 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(ARM_AM::add, 0), SDLoc(N),
1036 bool ARMDAGToDAGISel::SelectAddrMode3Offset(SDNode *Op, SDValue N,
1037 SDValue &Offset, SDValue &Opc) {
1038 unsigned Opcode = Op->getOpcode();
1039 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1040 ? cast<LoadSDNode>(Op)->getAddressingMode()
1041 : cast<StoreSDNode>(Op)->getAddressingMode();
1042 ARM_AM::AddrOpc AddSub = (AM == ISD::PRE_INC || AM == ISD::POST_INC)
1043 ? ARM_AM::add : ARM_AM::sub;
1045 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 256, Val)) { // 12 bits.
1046 Offset = CurDAG->getRegister(0, MVT::i32);
1047 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, Val), SDLoc(Op),
1053 Opc = CurDAG->getTargetConstant(ARM_AM::getAM3Opc(AddSub, 0), SDLoc(Op),
1058 bool ARMDAGToDAGISel::SelectAddrMode5(SDValue N,
1059 SDValue &Base, SDValue &Offset) {
1060 if (!CurDAG->isBaseWithConstantOffset(N)) {
1062 if (N.getOpcode() == ISD::FrameIndex) {
1063 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1064 Base = CurDAG->getTargetFrameIndex(
1065 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1066 } else if (N.getOpcode() == ARMISD::Wrapper &&
1067 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1068 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1069 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1070 Base = N.getOperand(0);
1072 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
1073 SDLoc(N), MVT::i32);
1077 // If the RHS is +/- imm8, fold into addr mode.
1079 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4,
1080 -256 + 1, 256, RHSC)) {
1081 Base = N.getOperand(0);
1082 if (Base.getOpcode() == ISD::FrameIndex) {
1083 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1084 Base = CurDAG->getTargetFrameIndex(
1085 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1088 ARM_AM::AddrOpc AddSub = ARM_AM::add;
1090 AddSub = ARM_AM::sub;
1093 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(AddSub, RHSC),
1094 SDLoc(N), MVT::i32);
1099 Offset = CurDAG->getTargetConstant(ARM_AM::getAM5Opc(ARM_AM::add, 0),
1100 SDLoc(N), MVT::i32);
1104 bool ARMDAGToDAGISel::SelectAddrMode6(SDNode *Parent, SDValue N, SDValue &Addr,
1108 unsigned Alignment = 0;
1110 MemSDNode *MemN = cast<MemSDNode>(Parent);
1112 if (isa<LSBaseSDNode>(MemN) ||
1113 ((MemN->getOpcode() == ARMISD::VST1_UPD ||
1114 MemN->getOpcode() == ARMISD::VLD1_UPD) &&
1115 MemN->getConstantOperandVal(MemN->getNumOperands() - 1) == 1)) {
1116 // This case occurs only for VLD1-lane/dup and VST1-lane instructions.
1117 // The maximum alignment is equal to the memory size being referenced.
1118 unsigned MMOAlign = MemN->getAlignment();
1119 unsigned MemSize = MemN->getMemoryVT().getSizeInBits() / 8;
1120 if (MMOAlign >= MemSize && MemSize > 1)
1121 Alignment = MemSize;
1123 // All other uses of addrmode6 are for intrinsics. For now just record
1124 // the raw alignment value; it will be refined later based on the legal
1125 // alignment operands for the intrinsic.
1126 Alignment = MemN->getAlignment();
1129 Align = CurDAG->getTargetConstant(Alignment, SDLoc(N), MVT::i32);
1133 bool ARMDAGToDAGISel::SelectAddrMode6Offset(SDNode *Op, SDValue N,
1135 LSBaseSDNode *LdSt = cast<LSBaseSDNode>(Op);
1136 ISD::MemIndexedMode AM = LdSt->getAddressingMode();
1137 if (AM != ISD::POST_INC)
1140 if (ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N)) {
1141 if (NC->getZExtValue() * 8 == LdSt->getMemoryVT().getSizeInBits())
1142 Offset = CurDAG->getRegister(0, MVT::i32);
1147 bool ARMDAGToDAGISel::SelectAddrModePC(SDValue N,
1148 SDValue &Offset, SDValue &Label) {
1149 if (N.getOpcode() == ARMISD::PIC_ADD && N.hasOneUse()) {
1150 Offset = N.getOperand(0);
1151 SDValue N1 = N.getOperand(1);
1152 Label = CurDAG->getTargetConstant(cast<ConstantSDNode>(N1)->getZExtValue(),
1153 SDLoc(N), MVT::i32);
1161 //===----------------------------------------------------------------------===//
1162 // Thumb Addressing Modes
1163 //===----------------------------------------------------------------------===//
1165 bool ARMDAGToDAGISel::SelectThumbAddrModeRR(SDValue N,
1166 SDValue &Base, SDValue &Offset){
1167 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N)) {
1168 ConstantSDNode *NC = dyn_cast<ConstantSDNode>(N);
1169 if (!NC || !NC->isNullValue())
1176 Base = N.getOperand(0);
1177 Offset = N.getOperand(1);
1182 ARMDAGToDAGISel::SelectThumbAddrModeImm5S(SDValue N, unsigned Scale,
1183 SDValue &Base, SDValue &OffImm) {
1184 if (!CurDAG->isBaseWithConstantOffset(N)) {
1185 if (N.getOpcode() == ISD::ADD) {
1186 return false; // We want to select register offset instead
1187 } else if (N.getOpcode() == ARMISD::Wrapper &&
1188 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1189 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1190 N.getOperand(0).getOpcode() != ISD::TargetConstantPool &&
1191 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1192 Base = N.getOperand(0);
1197 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1201 // If the RHS is + imm5 * scale, fold into addr mode.
1203 if (isScaledConstantInRange(N.getOperand(1), Scale, 0, 32, RHSC)) {
1204 Base = N.getOperand(0);
1205 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1209 // Offset is too large, so use register offset instead.
1214 ARMDAGToDAGISel::SelectThumbAddrModeImm5S4(SDValue N, SDValue &Base,
1216 return SelectThumbAddrModeImm5S(N, 4, Base, OffImm);
1220 ARMDAGToDAGISel::SelectThumbAddrModeImm5S2(SDValue N, SDValue &Base,
1222 return SelectThumbAddrModeImm5S(N, 2, Base, OffImm);
1226 ARMDAGToDAGISel::SelectThumbAddrModeImm5S1(SDValue N, SDValue &Base,
1228 return SelectThumbAddrModeImm5S(N, 1, Base, OffImm);
1231 bool ARMDAGToDAGISel::SelectThumbAddrModeSP(SDValue N,
1232 SDValue &Base, SDValue &OffImm) {
1233 if (N.getOpcode() == ISD::FrameIndex) {
1234 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1235 // Only multiples of 4 are allowed for the offset, so the frame object
1236 // alignment must be at least 4.
1237 MachineFrameInfo &MFI = MF->getFrameInfo();
1238 if (MFI.getObjectAlignment(FI) < 4)
1239 MFI.setObjectAlignment(FI, 4);
1240 Base = CurDAG->getTargetFrameIndex(
1241 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1242 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1246 if (!CurDAG->isBaseWithConstantOffset(N))
1249 RegisterSDNode *LHSR = dyn_cast<RegisterSDNode>(N.getOperand(0));
1250 if (N.getOperand(0).getOpcode() == ISD::FrameIndex ||
1251 (LHSR && LHSR->getReg() == ARM::SP)) {
1252 // If the RHS is + imm8 * scale, fold into addr mode.
1254 if (isScaledConstantInRange(N.getOperand(1), /*Scale=*/4, 0, 256, RHSC)) {
1255 Base = N.getOperand(0);
1256 if (Base.getOpcode() == ISD::FrameIndex) {
1257 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1258 // For LHS+RHS to result in an offset that's a multiple of 4 the object
1259 // indexed by the LHS must be 4-byte aligned.
1260 MachineFrameInfo &MFI = MF->getFrameInfo();
1261 if (MFI.getObjectAlignment(FI) < 4)
1262 MFI.setObjectAlignment(FI, 4);
1263 Base = CurDAG->getTargetFrameIndex(
1264 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1266 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1275 //===----------------------------------------------------------------------===//
1276 // Thumb 2 Addressing Modes
1277 //===----------------------------------------------------------------------===//
1280 bool ARMDAGToDAGISel::SelectT2AddrModeImm12(SDValue N,
1281 SDValue &Base, SDValue &OffImm) {
1282 // Match simple R + imm12 operands.
1285 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1286 !CurDAG->isBaseWithConstantOffset(N)) {
1287 if (N.getOpcode() == ISD::FrameIndex) {
1288 // Match frame index.
1289 int FI = cast<FrameIndexSDNode>(N)->getIndex();
1290 Base = CurDAG->getTargetFrameIndex(
1291 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1292 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1296 if (N.getOpcode() == ARMISD::Wrapper &&
1297 N.getOperand(0).getOpcode() != ISD::TargetGlobalAddress &&
1298 N.getOperand(0).getOpcode() != ISD::TargetExternalSymbol &&
1299 N.getOperand(0).getOpcode() != ISD::TargetGlobalTLSAddress) {
1300 Base = N.getOperand(0);
1301 if (Base.getOpcode() == ISD::TargetConstantPool)
1302 return false; // We want to select t2LDRpci instead.
1305 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1309 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1310 if (SelectT2AddrModeImm8(N, Base, OffImm))
1311 // Let t2LDRi8 handle (R - imm8).
1314 int RHSC = (int)RHS->getZExtValue();
1315 if (N.getOpcode() == ISD::SUB)
1318 if (RHSC >= 0 && RHSC < 0x1000) { // 12 bits (unsigned)
1319 Base = N.getOperand(0);
1320 if (Base.getOpcode() == ISD::FrameIndex) {
1321 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1322 Base = CurDAG->getTargetFrameIndex(
1323 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1325 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1332 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1336 bool ARMDAGToDAGISel::SelectT2AddrModeImm8(SDValue N,
1337 SDValue &Base, SDValue &OffImm) {
1338 // Match simple R - imm8 operands.
1339 if (N.getOpcode() != ISD::ADD && N.getOpcode() != ISD::SUB &&
1340 !CurDAG->isBaseWithConstantOffset(N))
1343 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1344 int RHSC = (int)RHS->getSExtValue();
1345 if (N.getOpcode() == ISD::SUB)
1348 if ((RHSC >= -255) && (RHSC < 0)) { // 8 bits (always negative)
1349 Base = N.getOperand(0);
1350 if (Base.getOpcode() == ISD::FrameIndex) {
1351 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1352 Base = CurDAG->getTargetFrameIndex(
1353 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1355 OffImm = CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32);
1363 bool ARMDAGToDAGISel::SelectT2AddrModeImm8Offset(SDNode *Op, SDValue N,
1365 unsigned Opcode = Op->getOpcode();
1366 ISD::MemIndexedMode AM = (Opcode == ISD::LOAD)
1367 ? cast<LoadSDNode>(Op)->getAddressingMode()
1368 : cast<StoreSDNode>(Op)->getAddressingMode();
1370 if (isScaledConstantInRange(N, /*Scale=*/1, 0, 0x100, RHSC)) { // 8 bits.
1371 OffImm = ((AM == ISD::PRE_INC) || (AM == ISD::POST_INC))
1372 ? CurDAG->getTargetConstant(RHSC, SDLoc(N), MVT::i32)
1373 : CurDAG->getTargetConstant(-RHSC, SDLoc(N), MVT::i32);
1380 bool ARMDAGToDAGISel::SelectT2AddrModeSoReg(SDValue N,
1382 SDValue &OffReg, SDValue &ShImm) {
1383 // (R - imm8) should be handled by t2LDRi8. The rest are handled by t2LDRi12.
1384 if (N.getOpcode() != ISD::ADD && !CurDAG->isBaseWithConstantOffset(N))
1387 // Leave (R + imm12) for t2LDRi12, (R - imm8) for t2LDRi8.
1388 if (ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1))) {
1389 int RHSC = (int)RHS->getZExtValue();
1390 if (RHSC >= 0 && RHSC < 0x1000) // 12 bits (unsigned)
1392 else if (RHSC < 0 && RHSC >= -255) // 8 bits
1396 // Look for (R + R) or (R + (R << [1,2,3])).
1398 Base = N.getOperand(0);
1399 OffReg = N.getOperand(1);
1401 // Swap if it is ((R << c) + R).
1402 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(OffReg.getOpcode());
1403 if (ShOpcVal != ARM_AM::lsl) {
1404 ShOpcVal = ARM_AM::getShiftOpcForNode(Base.getOpcode());
1405 if (ShOpcVal == ARM_AM::lsl)
1406 std::swap(Base, OffReg);
1409 if (ShOpcVal == ARM_AM::lsl) {
1410 // Check to see if the RHS of the shift is a constant, if not, we can't fold
1412 if (ConstantSDNode *Sh = dyn_cast<ConstantSDNode>(OffReg.getOperand(1))) {
1413 ShAmt = Sh->getZExtValue();
1414 if (ShAmt < 4 && isShifterOpProfitable(OffReg, ShOpcVal, ShAmt))
1415 OffReg = OffReg.getOperand(0);
1422 // If OffReg is a multiply-by-constant and it's profitable to extract a shift
1423 // and use it in a shifted operand do so.
1424 if (OffReg.getOpcode() == ISD::MUL && N.hasOneUse()) {
1425 unsigned PowerOfTwo = 0;
1426 SDValue NewMulConst;
1427 if (canExtractShiftFromMul(OffReg, 3, PowerOfTwo, NewMulConst)) {
1428 replaceDAGValue(OffReg.getOperand(1), NewMulConst);
1433 ShImm = CurDAG->getTargetConstant(ShAmt, SDLoc(N), MVT::i32);
1438 bool ARMDAGToDAGISel::SelectT2AddrModeExclusive(SDValue N, SDValue &Base,
1440 // This *must* succeed since it's used for the irreplaceable ldrex and strex
1443 OffImm = CurDAG->getTargetConstant(0, SDLoc(N), MVT::i32);
1445 if (N.getOpcode() != ISD::ADD || !CurDAG->isBaseWithConstantOffset(N))
1448 ConstantSDNode *RHS = dyn_cast<ConstantSDNode>(N.getOperand(1));
1452 uint32_t RHSC = (int)RHS->getZExtValue();
1453 if (RHSC > 1020 || RHSC % 4 != 0)
1456 Base = N.getOperand(0);
1457 if (Base.getOpcode() == ISD::FrameIndex) {
1458 int FI = cast<FrameIndexSDNode>(Base)->getIndex();
1459 Base = CurDAG->getTargetFrameIndex(
1460 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
1463 OffImm = CurDAG->getTargetConstant(RHSC/4, SDLoc(N), MVT::i32);
1467 //===--------------------------------------------------------------------===//
1469 /// getAL - Returns a ARMCC::AL immediate node.
1470 static inline SDValue getAL(SelectionDAG *CurDAG, const SDLoc &dl) {
1471 return CurDAG->getTargetConstant((uint64_t)ARMCC::AL, dl, MVT::i32);
1474 void ARMDAGToDAGISel::transferMemOperands(SDNode *N, SDNode *Result) {
1475 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1476 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
1477 cast<MachineSDNode>(Result)->setMemRefs(MemOp, MemOp + 1);
1480 bool ARMDAGToDAGISel::tryARMIndexedLoad(SDNode *N) {
1481 LoadSDNode *LD = cast<LoadSDNode>(N);
1482 ISD::MemIndexedMode AM = LD->getAddressingMode();
1483 if (AM == ISD::UNINDEXED)
1486 EVT LoadedVT = LD->getMemoryVT();
1487 SDValue Offset, AMOpc;
1488 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1489 unsigned Opcode = 0;
1491 if (LoadedVT == MVT::i32 && isPre &&
1492 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1493 Opcode = ARM::LDR_PRE_IMM;
1495 } else if (LoadedVT == MVT::i32 && !isPre &&
1496 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1497 Opcode = ARM::LDR_POST_IMM;
1499 } else if (LoadedVT == MVT::i32 &&
1500 SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1501 Opcode = isPre ? ARM::LDR_PRE_REG : ARM::LDR_POST_REG;
1504 } else if (LoadedVT == MVT::i16 &&
1505 SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1507 Opcode = (LD->getExtensionType() == ISD::SEXTLOAD)
1508 ? (isPre ? ARM::LDRSH_PRE : ARM::LDRSH_POST)
1509 : (isPre ? ARM::LDRH_PRE : ARM::LDRH_POST);
1510 } else if (LoadedVT == MVT::i8 || LoadedVT == MVT::i1) {
1511 if (LD->getExtensionType() == ISD::SEXTLOAD) {
1512 if (SelectAddrMode3Offset(N, LD->getOffset(), Offset, AMOpc)) {
1514 Opcode = isPre ? ARM::LDRSB_PRE : ARM::LDRSB_POST;
1518 SelectAddrMode2OffsetImmPre(N, LD->getOffset(), Offset, AMOpc)) {
1520 Opcode = ARM::LDRB_PRE_IMM;
1521 } else if (!isPre &&
1522 SelectAddrMode2OffsetImm(N, LD->getOffset(), Offset, AMOpc)) {
1524 Opcode = ARM::LDRB_POST_IMM;
1525 } else if (SelectAddrMode2OffsetReg(N, LD->getOffset(), Offset, AMOpc)) {
1527 Opcode = isPre ? ARM::LDRB_PRE_REG : ARM::LDRB_POST_REG;
1533 if (Opcode == ARM::LDR_PRE_IMM || Opcode == ARM::LDRB_PRE_IMM) {
1534 SDValue Chain = LD->getChain();
1535 SDValue Base = LD->getBasePtr();
1536 SDValue Ops[]= { Base, AMOpc, getAL(CurDAG, SDLoc(N)),
1537 CurDAG->getRegister(0, MVT::i32), Chain };
1538 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1540 transferMemOperands(N, New);
1541 ReplaceNode(N, New);
1544 SDValue Chain = LD->getChain();
1545 SDValue Base = LD->getBasePtr();
1546 SDValue Ops[]= { Base, Offset, AMOpc, getAL(CurDAG, SDLoc(N)),
1547 CurDAG->getRegister(0, MVT::i32), Chain };
1548 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1550 transferMemOperands(N, New);
1551 ReplaceNode(N, New);
1559 bool ARMDAGToDAGISel::tryT1IndexedLoad(SDNode *N) {
1560 LoadSDNode *LD = cast<LoadSDNode>(N);
1561 EVT LoadedVT = LD->getMemoryVT();
1562 ISD::MemIndexedMode AM = LD->getAddressingMode();
1563 if (AM != ISD::POST_INC || LD->getExtensionType() != ISD::NON_EXTLOAD ||
1564 LoadedVT.getSimpleVT().SimpleTy != MVT::i32)
1567 auto *COffs = dyn_cast<ConstantSDNode>(LD->getOffset());
1568 if (!COffs || COffs->getZExtValue() != 4)
1571 // A T1 post-indexed load is just a single register LDM: LDM r0!, {r1}.
1572 // The encoding of LDM is not how the rest of ISel expects a post-inc load to
1573 // look however, so we use a pseudo here and switch it for a tLDMIA_UPD after
1575 SDValue Chain = LD->getChain();
1576 SDValue Base = LD->getBasePtr();
1577 SDValue Ops[]= { Base, getAL(CurDAG, SDLoc(N)),
1578 CurDAG->getRegister(0, MVT::i32), Chain };
1579 SDNode *New = CurDAG->getMachineNode(ARM::tLDR_postidx, SDLoc(N), MVT::i32,
1580 MVT::i32, MVT::Other, Ops);
1581 transferMemOperands(N, New);
1582 ReplaceNode(N, New);
1586 bool ARMDAGToDAGISel::tryT2IndexedLoad(SDNode *N) {
1587 LoadSDNode *LD = cast<LoadSDNode>(N);
1588 ISD::MemIndexedMode AM = LD->getAddressingMode();
1589 if (AM == ISD::UNINDEXED)
1592 EVT LoadedVT = LD->getMemoryVT();
1593 bool isSExtLd = LD->getExtensionType() == ISD::SEXTLOAD;
1595 bool isPre = (AM == ISD::PRE_INC) || (AM == ISD::PRE_DEC);
1596 unsigned Opcode = 0;
1598 if (SelectT2AddrModeImm8Offset(N, LD->getOffset(), Offset)) {
1599 switch (LoadedVT.getSimpleVT().SimpleTy) {
1601 Opcode = isPre ? ARM::t2LDR_PRE : ARM::t2LDR_POST;
1605 Opcode = isPre ? ARM::t2LDRSH_PRE : ARM::t2LDRSH_POST;
1607 Opcode = isPre ? ARM::t2LDRH_PRE : ARM::t2LDRH_POST;
1612 Opcode = isPre ? ARM::t2LDRSB_PRE : ARM::t2LDRSB_POST;
1614 Opcode = isPre ? ARM::t2LDRB_PRE : ARM::t2LDRB_POST;
1623 SDValue Chain = LD->getChain();
1624 SDValue Base = LD->getBasePtr();
1625 SDValue Ops[]= { Base, Offset, getAL(CurDAG, SDLoc(N)),
1626 CurDAG->getRegister(0, MVT::i32), Chain };
1627 SDNode *New = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i32, MVT::i32,
1629 transferMemOperands(N, New);
1630 ReplaceNode(N, New);
1637 /// \brief Form a GPRPair pseudo register from a pair of GPR regs.
1638 SDNode *ARMDAGToDAGISel::createGPRPairNode(EVT VT, SDValue V0, SDValue V1) {
1639 SDLoc dl(V0.getNode());
1641 CurDAG->getTargetConstant(ARM::GPRPairRegClassID, dl, MVT::i32);
1642 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
1643 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
1644 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1645 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1648 /// \brief Form a D register from a pair of S registers.
1649 SDNode *ARMDAGToDAGISel::createSRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1650 SDLoc dl(V0.getNode());
1652 CurDAG->getTargetConstant(ARM::DPR_VFP2RegClassID, dl, MVT::i32);
1653 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1654 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1655 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1656 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1659 /// \brief Form a quad register from a pair of D registers.
1660 SDNode *ARMDAGToDAGISel::createDRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1661 SDLoc dl(V0.getNode());
1662 SDValue RegClass = CurDAG->getTargetConstant(ARM::QPRRegClassID, dl,
1664 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1665 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1666 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1667 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1670 /// \brief Form 4 consecutive D registers from a pair of Q registers.
1671 SDNode *ARMDAGToDAGISel::createQRegPairNode(EVT VT, SDValue V0, SDValue V1) {
1672 SDLoc dl(V0.getNode());
1673 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1675 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1676 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1677 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1 };
1678 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1681 /// \brief Form 4 consecutive S registers.
1682 SDNode *ARMDAGToDAGISel::createQuadSRegsNode(EVT VT, SDValue V0, SDValue V1,
1683 SDValue V2, SDValue V3) {
1684 SDLoc dl(V0.getNode());
1686 CurDAG->getTargetConstant(ARM::QPR_VFP2RegClassID, dl, MVT::i32);
1687 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::ssub_0, dl, MVT::i32);
1688 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::ssub_1, dl, MVT::i32);
1689 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::ssub_2, dl, MVT::i32);
1690 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::ssub_3, dl, MVT::i32);
1691 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1692 V2, SubReg2, V3, SubReg3 };
1693 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1696 /// \brief Form 4 consecutive D registers.
1697 SDNode *ARMDAGToDAGISel::createQuadDRegsNode(EVT VT, SDValue V0, SDValue V1,
1698 SDValue V2, SDValue V3) {
1699 SDLoc dl(V0.getNode());
1700 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQPRRegClassID, dl,
1702 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::dsub_0, dl, MVT::i32);
1703 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::dsub_1, dl, MVT::i32);
1704 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::dsub_2, dl, MVT::i32);
1705 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::dsub_3, dl, MVT::i32);
1706 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1707 V2, SubReg2, V3, SubReg3 };
1708 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1711 /// \brief Form 4 consecutive Q registers.
1712 SDNode *ARMDAGToDAGISel::createQuadQRegsNode(EVT VT, SDValue V0, SDValue V1,
1713 SDValue V2, SDValue V3) {
1714 SDLoc dl(V0.getNode());
1715 SDValue RegClass = CurDAG->getTargetConstant(ARM::QQQQPRRegClassID, dl,
1717 SDValue SubReg0 = CurDAG->getTargetConstant(ARM::qsub_0, dl, MVT::i32);
1718 SDValue SubReg1 = CurDAG->getTargetConstant(ARM::qsub_1, dl, MVT::i32);
1719 SDValue SubReg2 = CurDAG->getTargetConstant(ARM::qsub_2, dl, MVT::i32);
1720 SDValue SubReg3 = CurDAG->getTargetConstant(ARM::qsub_3, dl, MVT::i32);
1721 const SDValue Ops[] = { RegClass, V0, SubReg0, V1, SubReg1,
1722 V2, SubReg2, V3, SubReg3 };
1723 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, dl, VT, Ops);
1726 /// GetVLDSTAlign - Get the alignment (in bytes) for the alignment operand
1727 /// of a NEON VLD or VST instruction. The supported values depend on the
1728 /// number of registers being loaded.
1729 SDValue ARMDAGToDAGISel::GetVLDSTAlign(SDValue Align, const SDLoc &dl,
1730 unsigned NumVecs, bool is64BitVector) {
1731 unsigned NumRegs = NumVecs;
1732 if (!is64BitVector && NumVecs < 3)
1735 unsigned Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
1736 if (Alignment >= 32 && NumRegs == 4)
1738 else if (Alignment >= 16 && (NumRegs == 2 || NumRegs == 4))
1740 else if (Alignment >= 8)
1745 return CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
1748 static bool isVLDfixed(unsigned Opc)
1751 default: return false;
1752 case ARM::VLD1d8wb_fixed : return true;
1753 case ARM::VLD1d16wb_fixed : return true;
1754 case ARM::VLD1d64Qwb_fixed : return true;
1755 case ARM::VLD1d32wb_fixed : return true;
1756 case ARM::VLD1d64wb_fixed : return true;
1757 case ARM::VLD1d64TPseudoWB_fixed : return true;
1758 case ARM::VLD1d64QPseudoWB_fixed : return true;
1759 case ARM::VLD1q8wb_fixed : return true;
1760 case ARM::VLD1q16wb_fixed : return true;
1761 case ARM::VLD1q32wb_fixed : return true;
1762 case ARM::VLD1q64wb_fixed : return true;
1763 case ARM::VLD1DUPd8wb_fixed : return true;
1764 case ARM::VLD1DUPd16wb_fixed : return true;
1765 case ARM::VLD1DUPd32wb_fixed : return true;
1766 case ARM::VLD1DUPq8wb_fixed : return true;
1767 case ARM::VLD1DUPq16wb_fixed : return true;
1768 case ARM::VLD1DUPq32wb_fixed : return true;
1769 case ARM::VLD2d8wb_fixed : return true;
1770 case ARM::VLD2d16wb_fixed : return true;
1771 case ARM::VLD2d32wb_fixed : return true;
1772 case ARM::VLD2q8PseudoWB_fixed : return true;
1773 case ARM::VLD2q16PseudoWB_fixed : return true;
1774 case ARM::VLD2q32PseudoWB_fixed : return true;
1775 case ARM::VLD2DUPd8wb_fixed : return true;
1776 case ARM::VLD2DUPd16wb_fixed : return true;
1777 case ARM::VLD2DUPd32wb_fixed : return true;
1781 static bool isVSTfixed(unsigned Opc)
1784 default: return false;
1785 case ARM::VST1d8wb_fixed : return true;
1786 case ARM::VST1d16wb_fixed : return true;
1787 case ARM::VST1d32wb_fixed : return true;
1788 case ARM::VST1d64wb_fixed : return true;
1789 case ARM::VST1q8wb_fixed : return true;
1790 case ARM::VST1q16wb_fixed : return true;
1791 case ARM::VST1q32wb_fixed : return true;
1792 case ARM::VST1q64wb_fixed : return true;
1793 case ARM::VST1d64TPseudoWB_fixed : return true;
1794 case ARM::VST1d64QPseudoWB_fixed : return true;
1795 case ARM::VST2d8wb_fixed : return true;
1796 case ARM::VST2d16wb_fixed : return true;
1797 case ARM::VST2d32wb_fixed : return true;
1798 case ARM::VST2q8PseudoWB_fixed : return true;
1799 case ARM::VST2q16PseudoWB_fixed : return true;
1800 case ARM::VST2q32PseudoWB_fixed : return true;
1804 // Get the register stride update opcode of a VLD/VST instruction that
1805 // is otherwise equivalent to the given fixed stride updating instruction.
1806 static unsigned getVLDSTRegisterUpdateOpcode(unsigned Opc) {
1807 assert((isVLDfixed(Opc) || isVSTfixed(Opc))
1808 && "Incorrect fixed stride updating instruction.");
1811 case ARM::VLD1d8wb_fixed: return ARM::VLD1d8wb_register;
1812 case ARM::VLD1d16wb_fixed: return ARM::VLD1d16wb_register;
1813 case ARM::VLD1d32wb_fixed: return ARM::VLD1d32wb_register;
1814 case ARM::VLD1d64wb_fixed: return ARM::VLD1d64wb_register;
1815 case ARM::VLD1q8wb_fixed: return ARM::VLD1q8wb_register;
1816 case ARM::VLD1q16wb_fixed: return ARM::VLD1q16wb_register;
1817 case ARM::VLD1q32wb_fixed: return ARM::VLD1q32wb_register;
1818 case ARM::VLD1q64wb_fixed: return ARM::VLD1q64wb_register;
1819 case ARM::VLD1d64Twb_fixed: return ARM::VLD1d64Twb_register;
1820 case ARM::VLD1d64Qwb_fixed: return ARM::VLD1d64Qwb_register;
1821 case ARM::VLD1d64TPseudoWB_fixed: return ARM::VLD1d64TPseudoWB_register;
1822 case ARM::VLD1d64QPseudoWB_fixed: return ARM::VLD1d64QPseudoWB_register;
1823 case ARM::VLD1DUPd8wb_fixed : return ARM::VLD1DUPd8wb_register;
1824 case ARM::VLD1DUPd16wb_fixed : return ARM::VLD1DUPd16wb_register;
1825 case ARM::VLD1DUPd32wb_fixed : return ARM::VLD1DUPd32wb_register;
1826 case ARM::VLD1DUPq8wb_fixed : return ARM::VLD1DUPq8wb_register;
1827 case ARM::VLD1DUPq16wb_fixed : return ARM::VLD1DUPq16wb_register;
1828 case ARM::VLD1DUPq32wb_fixed : return ARM::VLD1DUPq32wb_register;
1830 case ARM::VST1d8wb_fixed: return ARM::VST1d8wb_register;
1831 case ARM::VST1d16wb_fixed: return ARM::VST1d16wb_register;
1832 case ARM::VST1d32wb_fixed: return ARM::VST1d32wb_register;
1833 case ARM::VST1d64wb_fixed: return ARM::VST1d64wb_register;
1834 case ARM::VST1q8wb_fixed: return ARM::VST1q8wb_register;
1835 case ARM::VST1q16wb_fixed: return ARM::VST1q16wb_register;
1836 case ARM::VST1q32wb_fixed: return ARM::VST1q32wb_register;
1837 case ARM::VST1q64wb_fixed: return ARM::VST1q64wb_register;
1838 case ARM::VST1d64TPseudoWB_fixed: return ARM::VST1d64TPseudoWB_register;
1839 case ARM::VST1d64QPseudoWB_fixed: return ARM::VST1d64QPseudoWB_register;
1841 case ARM::VLD2d8wb_fixed: return ARM::VLD2d8wb_register;
1842 case ARM::VLD2d16wb_fixed: return ARM::VLD2d16wb_register;
1843 case ARM::VLD2d32wb_fixed: return ARM::VLD2d32wb_register;
1844 case ARM::VLD2q8PseudoWB_fixed: return ARM::VLD2q8PseudoWB_register;
1845 case ARM::VLD2q16PseudoWB_fixed: return ARM::VLD2q16PseudoWB_register;
1846 case ARM::VLD2q32PseudoWB_fixed: return ARM::VLD2q32PseudoWB_register;
1848 case ARM::VST2d8wb_fixed: return ARM::VST2d8wb_register;
1849 case ARM::VST2d16wb_fixed: return ARM::VST2d16wb_register;
1850 case ARM::VST2d32wb_fixed: return ARM::VST2d32wb_register;
1851 case ARM::VST2q8PseudoWB_fixed: return ARM::VST2q8PseudoWB_register;
1852 case ARM::VST2q16PseudoWB_fixed: return ARM::VST2q16PseudoWB_register;
1853 case ARM::VST2q32PseudoWB_fixed: return ARM::VST2q32PseudoWB_register;
1855 case ARM::VLD2DUPd8wb_fixed: return ARM::VLD2DUPd8wb_register;
1856 case ARM::VLD2DUPd16wb_fixed: return ARM::VLD2DUPd16wb_register;
1857 case ARM::VLD2DUPd32wb_fixed: return ARM::VLD2DUPd32wb_register;
1859 return Opc; // If not one we handle, return it unchanged.
1862 void ARMDAGToDAGISel::SelectVLD(SDNode *N, bool isUpdating, unsigned NumVecs,
1863 const uint16_t *DOpcodes,
1864 const uint16_t *QOpcodes0,
1865 const uint16_t *QOpcodes1) {
1866 assert(NumVecs >= 1 && NumVecs <= 4 && "VLD NumVecs out-of-range");
1869 SDValue MemAddr, Align;
1870 unsigned AddrOpIdx = isUpdating ? 1 : 2;
1871 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
1874 SDValue Chain = N->getOperand(0);
1875 EVT VT = N->getValueType(0);
1876 bool is64BitVector = VT.is64BitVector();
1877 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
1879 unsigned OpcodeIndex;
1880 switch (VT.getSimpleVT().SimpleTy) {
1881 default: llvm_unreachable("unhandled vld type");
1882 // Double-register operations:
1883 case MVT::v8i8: OpcodeIndex = 0; break;
1884 case MVT::v4i16: OpcodeIndex = 1; break;
1886 case MVT::v2i32: OpcodeIndex = 2; break;
1887 case MVT::v1i64: OpcodeIndex = 3; break;
1888 // Quad-register operations:
1889 case MVT::v16i8: OpcodeIndex = 0; break;
1890 case MVT::v8i16: OpcodeIndex = 1; break;
1892 case MVT::v4i32: OpcodeIndex = 2; break;
1894 case MVT::v2i64: OpcodeIndex = 3;
1895 assert(NumVecs == 1 && "v2i64 type only supported for VLD1");
1903 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
1906 ResTy = EVT::getVectorVT(*CurDAG->getContext(), MVT::i64, ResTyElts);
1908 std::vector<EVT> ResTys;
1909 ResTys.push_back(ResTy);
1911 ResTys.push_back(MVT::i32);
1912 ResTys.push_back(MVT::Other);
1914 SDValue Pred = getAL(CurDAG, dl);
1915 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
1917 SmallVector<SDValue, 7> Ops;
1919 // Double registers and VLD1/VLD2 quad registers are directly supported.
1920 if (is64BitVector || NumVecs <= 2) {
1921 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
1922 QOpcodes0[OpcodeIndex]);
1923 Ops.push_back(MemAddr);
1924 Ops.push_back(Align);
1926 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1927 // FIXME: VLD1/VLD2 fixed increment doesn't need Reg0. Remove the reg0
1928 // case entirely when the rest are updated to that form, too.
1929 if ((NumVecs <= 2) && !isa<ConstantSDNode>(Inc.getNode()))
1930 Opc = getVLDSTRegisterUpdateOpcode(Opc);
1931 // FIXME: We use a VLD1 for v1i64 even if the pseudo says vld2/3/4, so
1932 // check for that explicitly too. Horribly hacky, but temporary.
1933 if ((NumVecs > 2 && !isVLDfixed(Opc)) ||
1934 !isa<ConstantSDNode>(Inc.getNode()))
1935 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
1937 Ops.push_back(Pred);
1938 Ops.push_back(Reg0);
1939 Ops.push_back(Chain);
1940 VLd = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
1943 // Otherwise, quad registers are loaded with two separate instructions,
1944 // where one loads the even registers and the other loads the odd registers.
1945 EVT AddrTy = MemAddr.getValueType();
1947 // Load the even subregs. This is always an updating load, so that it
1948 // provides the address to the second load for the odd subregs.
1950 SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, ResTy), 0);
1951 const SDValue OpsA[] = { MemAddr, Align, Reg0, ImplDef, Pred, Reg0, Chain };
1952 SDNode *VLdA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
1953 ResTy, AddrTy, MVT::Other, OpsA);
1954 Chain = SDValue(VLdA, 2);
1956 // Load the odd subregs.
1957 Ops.push_back(SDValue(VLdA, 1));
1958 Ops.push_back(Align);
1960 SDValue Inc = N->getOperand(AddrOpIdx + 1);
1961 assert(isa<ConstantSDNode>(Inc.getNode()) &&
1962 "only constant post-increment update allowed for VLD3/4");
1964 Ops.push_back(Reg0);
1966 Ops.push_back(SDValue(VLdA, 0));
1967 Ops.push_back(Pred);
1968 Ops.push_back(Reg0);
1969 Ops.push_back(Chain);
1970 VLd = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys, Ops);
1973 // Transfer memoperands.
1974 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
1975 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
1976 cast<MachineSDNode>(VLd)->setMemRefs(MemOp, MemOp + 1);
1979 ReplaceNode(N, VLd);
1983 // Extract out the subregisters.
1984 SDValue SuperReg = SDValue(VLd, 0);
1985 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
1986 ARM::qsub_3 == ARM::qsub_0 + 3,
1987 "Unexpected subreg numbering");
1988 unsigned Sub0 = (is64BitVector ? ARM::dsub_0 : ARM::qsub_0);
1989 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
1990 ReplaceUses(SDValue(N, Vec),
1991 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
1992 ReplaceUses(SDValue(N, NumVecs), SDValue(VLd, 1));
1994 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLd, 2));
1995 CurDAG->RemoveDeadNode(N);
1998 void ARMDAGToDAGISel::SelectVST(SDNode *N, bool isUpdating, unsigned NumVecs,
1999 const uint16_t *DOpcodes,
2000 const uint16_t *QOpcodes0,
2001 const uint16_t *QOpcodes1) {
2002 assert(NumVecs >= 1 && NumVecs <= 4 && "VST NumVecs out-of-range");
2005 SDValue MemAddr, Align;
2006 unsigned AddrOpIdx = isUpdating ? 1 : 2;
2007 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2008 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2011 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2012 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2014 SDValue Chain = N->getOperand(0);
2015 EVT VT = N->getOperand(Vec0Idx).getValueType();
2016 bool is64BitVector = VT.is64BitVector();
2017 Align = GetVLDSTAlign(Align, dl, NumVecs, is64BitVector);
2019 unsigned OpcodeIndex;
2020 switch (VT.getSimpleVT().SimpleTy) {
2021 default: llvm_unreachable("unhandled vst type");
2022 // Double-register operations:
2023 case MVT::v8i8: OpcodeIndex = 0; break;
2024 case MVT::v4i16: OpcodeIndex = 1; break;
2026 case MVT::v2i32: OpcodeIndex = 2; break;
2027 case MVT::v1i64: OpcodeIndex = 3; break;
2028 // Quad-register operations:
2029 case MVT::v16i8: OpcodeIndex = 0; break;
2030 case MVT::v8i16: OpcodeIndex = 1; break;
2032 case MVT::v4i32: OpcodeIndex = 2; break;
2034 case MVT::v2i64: OpcodeIndex = 3;
2035 assert(NumVecs == 1 && "v2i64 type only supported for VST1");
2039 std::vector<EVT> ResTys;
2041 ResTys.push_back(MVT::i32);
2042 ResTys.push_back(MVT::Other);
2044 SDValue Pred = getAL(CurDAG, dl);
2045 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2046 SmallVector<SDValue, 7> Ops;
2048 // Double registers and VST1/VST2 quad registers are directly supported.
2049 if (is64BitVector || NumVecs <= 2) {
2052 SrcReg = N->getOperand(Vec0Idx);
2053 } else if (is64BitVector) {
2054 // Form a REG_SEQUENCE to force register allocation.
2055 SDValue V0 = N->getOperand(Vec0Idx + 0);
2056 SDValue V1 = N->getOperand(Vec0Idx + 1);
2058 SrcReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
2060 SDValue V2 = N->getOperand(Vec0Idx + 2);
2061 // If it's a vst3, form a quad D-register and leave the last part as
2063 SDValue V3 = (NumVecs == 3)
2064 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF,dl,VT), 0)
2065 : N->getOperand(Vec0Idx + 3);
2066 SrcReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2069 // Form a QQ register.
2070 SDValue Q0 = N->getOperand(Vec0Idx);
2071 SDValue Q1 = N->getOperand(Vec0Idx + 1);
2072 SrcReg = SDValue(createQRegPairNode(MVT::v4i64, Q0, Q1), 0);
2075 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2076 QOpcodes0[OpcodeIndex]);
2077 Ops.push_back(MemAddr);
2078 Ops.push_back(Align);
2080 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2081 // FIXME: VST1/VST2 fixed increment doesn't need Reg0. Remove the reg0
2082 // case entirely when the rest are updated to that form, too.
2083 if (NumVecs <= 2 && !isa<ConstantSDNode>(Inc.getNode()))
2084 Opc = getVLDSTRegisterUpdateOpcode(Opc);
2085 // FIXME: We use a VST1 for v1i64 even if the pseudo says vld2/3/4, so
2086 // check for that explicitly too. Horribly hacky, but temporary.
2087 if (!isa<ConstantSDNode>(Inc.getNode()))
2089 else if (NumVecs > 2 && !isVSTfixed(Opc))
2090 Ops.push_back(Reg0);
2092 Ops.push_back(SrcReg);
2093 Ops.push_back(Pred);
2094 Ops.push_back(Reg0);
2095 Ops.push_back(Chain);
2096 SDNode *VSt = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2098 // Transfer memoperands.
2099 cast<MachineSDNode>(VSt)->setMemRefs(MemOp, MemOp + 1);
2101 ReplaceNode(N, VSt);
2105 // Otherwise, quad registers are stored with two separate instructions,
2106 // where one stores the even registers and the other stores the odd registers.
2108 // Form the QQQQ REG_SEQUENCE.
2109 SDValue V0 = N->getOperand(Vec0Idx + 0);
2110 SDValue V1 = N->getOperand(Vec0Idx + 1);
2111 SDValue V2 = N->getOperand(Vec0Idx + 2);
2112 SDValue V3 = (NumVecs == 3)
2113 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2114 : N->getOperand(Vec0Idx + 3);
2115 SDValue RegSeq = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
2117 // Store the even D registers. This is always an updating store, so that it
2118 // provides the address to the second store for the odd subregs.
2119 const SDValue OpsA[] = { MemAddr, Align, Reg0, RegSeq, Pred, Reg0, Chain };
2120 SDNode *VStA = CurDAG->getMachineNode(QOpcodes0[OpcodeIndex], dl,
2121 MemAddr.getValueType(),
2123 cast<MachineSDNode>(VStA)->setMemRefs(MemOp, MemOp + 1);
2124 Chain = SDValue(VStA, 1);
2126 // Store the odd D registers.
2127 Ops.push_back(SDValue(VStA, 0));
2128 Ops.push_back(Align);
2130 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2131 assert(isa<ConstantSDNode>(Inc.getNode()) &&
2132 "only constant post-increment update allowed for VST3/4");
2134 Ops.push_back(Reg0);
2136 Ops.push_back(RegSeq);
2137 Ops.push_back(Pred);
2138 Ops.push_back(Reg0);
2139 Ops.push_back(Chain);
2140 SDNode *VStB = CurDAG->getMachineNode(QOpcodes1[OpcodeIndex], dl, ResTys,
2142 cast<MachineSDNode>(VStB)->setMemRefs(MemOp, MemOp + 1);
2143 ReplaceNode(N, VStB);
2146 void ARMDAGToDAGISel::SelectVLDSTLane(SDNode *N, bool IsLoad, bool isUpdating,
2148 const uint16_t *DOpcodes,
2149 const uint16_t *QOpcodes) {
2150 assert(NumVecs >=2 && NumVecs <= 4 && "VLDSTLane NumVecs out-of-range");
2153 SDValue MemAddr, Align;
2154 unsigned AddrOpIdx = isUpdating ? 1 : 2;
2155 unsigned Vec0Idx = 3; // AddrOpIdx + (isUpdating ? 2 : 1)
2156 if (!SelectAddrMode6(N, N->getOperand(AddrOpIdx), MemAddr, Align))
2159 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2160 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2162 SDValue Chain = N->getOperand(0);
2164 cast<ConstantSDNode>(N->getOperand(Vec0Idx + NumVecs))->getZExtValue();
2165 EVT VT = N->getOperand(Vec0Idx).getValueType();
2166 bool is64BitVector = VT.is64BitVector();
2168 unsigned Alignment = 0;
2170 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
2171 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8;
2172 if (Alignment > NumBytes)
2173 Alignment = NumBytes;
2174 if (Alignment < 8 && Alignment < NumBytes)
2176 // Alignment must be a power of two; make sure of that.
2177 Alignment = (Alignment & -Alignment);
2181 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
2183 unsigned OpcodeIndex;
2184 switch (VT.getSimpleVT().SimpleTy) {
2185 default: llvm_unreachable("unhandled vld/vst lane type");
2186 // Double-register operations:
2187 case MVT::v8i8: OpcodeIndex = 0; break;
2188 case MVT::v4i16: OpcodeIndex = 1; break;
2190 case MVT::v2i32: OpcodeIndex = 2; break;
2191 // Quad-register operations:
2192 case MVT::v8i16: OpcodeIndex = 0; break;
2194 case MVT::v4i32: OpcodeIndex = 1; break;
2197 std::vector<EVT> ResTys;
2199 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2202 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(),
2203 MVT::i64, ResTyElts));
2206 ResTys.push_back(MVT::i32);
2207 ResTys.push_back(MVT::Other);
2209 SDValue Pred = getAL(CurDAG, dl);
2210 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2212 SmallVector<SDValue, 8> Ops;
2213 Ops.push_back(MemAddr);
2214 Ops.push_back(Align);
2216 SDValue Inc = N->getOperand(AddrOpIdx + 1);
2217 Ops.push_back(isa<ConstantSDNode>(Inc.getNode()) ? Reg0 : Inc);
2221 SDValue V0 = N->getOperand(Vec0Idx + 0);
2222 SDValue V1 = N->getOperand(Vec0Idx + 1);
2225 SuperReg = SDValue(createDRegPairNode(MVT::v2i64, V0, V1), 0);
2227 SuperReg = SDValue(createQRegPairNode(MVT::v4i64, V0, V1), 0);
2229 SDValue V2 = N->getOperand(Vec0Idx + 2);
2230 SDValue V3 = (NumVecs == 3)
2231 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2232 : N->getOperand(Vec0Idx + 3);
2234 SuperReg = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2236 SuperReg = SDValue(createQuadQRegsNode(MVT::v8i64, V0, V1, V2, V3), 0);
2238 Ops.push_back(SuperReg);
2239 Ops.push_back(getI32Imm(Lane, dl));
2240 Ops.push_back(Pred);
2241 Ops.push_back(Reg0);
2242 Ops.push_back(Chain);
2244 unsigned Opc = (is64BitVector ? DOpcodes[OpcodeIndex] :
2245 QOpcodes[OpcodeIndex]);
2246 SDNode *VLdLn = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2247 cast<MachineSDNode>(VLdLn)->setMemRefs(MemOp, MemOp + 1);
2249 ReplaceNode(N, VLdLn);
2253 // Extract the subregisters.
2254 SuperReg = SDValue(VLdLn, 0);
2255 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7 &&
2256 ARM::qsub_3 == ARM::qsub_0 + 3,
2257 "Unexpected subreg numbering");
2258 unsigned Sub0 = is64BitVector ? ARM::dsub_0 : ARM::qsub_0;
2259 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2260 ReplaceUses(SDValue(N, Vec),
2261 CurDAG->getTargetExtractSubreg(Sub0 + Vec, dl, VT, SuperReg));
2262 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdLn, 1));
2264 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdLn, 2));
2265 CurDAG->RemoveDeadNode(N);
2268 void ARMDAGToDAGISel::SelectVLDDup(SDNode *N, bool isUpdating, unsigned NumVecs,
2269 const uint16_t *DOpcodes,
2270 const uint16_t *QOpcodes) {
2271 assert(NumVecs >= 1 && NumVecs <= 4 && "VLDDup NumVecs out-of-range");
2274 SDValue MemAddr, Align;
2275 if (!SelectAddrMode6(N, N->getOperand(1), MemAddr, Align))
2278 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2279 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
2281 SDValue Chain = N->getOperand(0);
2282 EVT VT = N->getValueType(0);
2284 unsigned Alignment = 0;
2286 Alignment = cast<ConstantSDNode>(Align)->getZExtValue();
2287 unsigned NumBytes = NumVecs * VT.getScalarSizeInBits() / 8;
2288 if (Alignment > NumBytes)
2289 Alignment = NumBytes;
2290 if (Alignment < 8 && Alignment < NumBytes)
2292 // Alignment must be a power of two; make sure of that.
2293 Alignment = (Alignment & -Alignment);
2297 Align = CurDAG->getTargetConstant(Alignment, dl, MVT::i32);
2300 switch (VT.getSimpleVT().SimpleTy) {
2301 default: llvm_unreachable("unhandled vld-dup type");
2302 case MVT::v8i8: Opc = DOpcodes[0]; break;
2303 case MVT::v16i8: Opc = QOpcodes[0]; break;
2304 case MVT::v4i16: Opc = DOpcodes[1]; break;
2305 case MVT::v8i16: Opc = QOpcodes[1]; break;
2307 case MVT::v2i32: Opc = DOpcodes[2]; break;
2309 case MVT::v4i32: Opc = QOpcodes[2]; break;
2312 SDValue Pred = getAL(CurDAG, dl);
2313 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2314 SmallVector<SDValue, 6> Ops;
2315 Ops.push_back(MemAddr);
2316 Ops.push_back(Align);
2318 // fixed-stride update instructions don't have an explicit writeback
2319 // operand. It's implicit in the opcode itself.
2320 SDValue Inc = N->getOperand(2);
2321 if (NumVecs <= 2 && !isa<ConstantSDNode>(Inc.getNode()))
2322 Opc = getVLDSTRegisterUpdateOpcode(Opc);
2323 if (!isa<ConstantSDNode>(Inc.getNode()))
2325 // FIXME: VLD3 and VLD4 haven't been updated to that form yet.
2326 else if (NumVecs > 2)
2327 Ops.push_back(Reg0);
2329 Ops.push_back(Pred);
2330 Ops.push_back(Reg0);
2331 Ops.push_back(Chain);
2333 unsigned ResTyElts = (NumVecs == 3) ? 4 : NumVecs;
2334 std::vector<EVT> ResTys;
2335 ResTys.push_back(EVT::getVectorVT(*CurDAG->getContext(), MVT::i64,ResTyElts));
2337 ResTys.push_back(MVT::i32);
2338 ResTys.push_back(MVT::Other);
2339 SDNode *VLdDup = CurDAG->getMachineNode(Opc, dl, ResTys, Ops);
2340 cast<MachineSDNode>(VLdDup)->setMemRefs(MemOp, MemOp + 1);
2342 // Extract the subregisters.
2344 ReplaceUses(SDValue(N, 0), SDValue(VLdDup, 0));
2346 SDValue SuperReg = SDValue(VLdDup, 0);
2347 static_assert(ARM::dsub_7 == ARM::dsub_0 + 7, "Unexpected subreg numbering");
2348 unsigned SubIdx = ARM::dsub_0;
2349 for (unsigned Vec = 0; Vec < NumVecs; ++Vec)
2350 ReplaceUses(SDValue(N, Vec),
2351 CurDAG->getTargetExtractSubreg(SubIdx+Vec, dl, VT, SuperReg));
2353 ReplaceUses(SDValue(N, NumVecs), SDValue(VLdDup, 1));
2355 ReplaceUses(SDValue(N, NumVecs + 1), SDValue(VLdDup, 2));
2356 CurDAG->RemoveDeadNode(N);
2359 void ARMDAGToDAGISel::SelectVTBL(SDNode *N, bool IsExt, unsigned NumVecs,
2361 assert(NumVecs >= 2 && NumVecs <= 4 && "VTBL NumVecs out-of-range");
2363 EVT VT = N->getValueType(0);
2364 unsigned FirstTblReg = IsExt ? 2 : 1;
2366 // Form a REG_SEQUENCE to force register allocation.
2368 SDValue V0 = N->getOperand(FirstTblReg + 0);
2369 SDValue V1 = N->getOperand(FirstTblReg + 1);
2371 RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0);
2373 SDValue V2 = N->getOperand(FirstTblReg + 2);
2374 // If it's a vtbl3, form a quad D-register and leave the last part as
2376 SDValue V3 = (NumVecs == 3)
2377 ? SDValue(CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, dl, VT), 0)
2378 : N->getOperand(FirstTblReg + 3);
2379 RegSeq = SDValue(createQuadDRegsNode(MVT::v4i64, V0, V1, V2, V3), 0);
2382 SmallVector<SDValue, 6> Ops;
2384 Ops.push_back(N->getOperand(1));
2385 Ops.push_back(RegSeq);
2386 Ops.push_back(N->getOperand(FirstTblReg + NumVecs));
2387 Ops.push_back(getAL(CurDAG, dl)); // predicate
2388 Ops.push_back(CurDAG->getRegister(0, MVT::i32)); // predicate register
2389 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
2392 bool ARMDAGToDAGISel::tryV6T2BitfieldExtractOp(SDNode *N, bool isSigned) {
2393 if (!Subtarget->hasV6T2Ops())
2396 unsigned Opc = isSigned
2397 ? (Subtarget->isThumb() ? ARM::t2SBFX : ARM::SBFX)
2398 : (Subtarget->isThumb() ? ARM::t2UBFX : ARM::UBFX);
2401 // For unsigned extracts, check for a shift right and mask
2402 unsigned And_imm = 0;
2403 if (N->getOpcode() == ISD::AND) {
2404 if (isOpcWithIntImmediate(N, ISD::AND, And_imm)) {
2406 // The immediate is a mask of the low bits iff imm & (imm+1) == 0
2407 if (And_imm & (And_imm + 1))
2410 unsigned Srl_imm = 0;
2411 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL,
2413 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2415 // Note: The width operand is encoded as width-1.
2416 unsigned Width = countTrailingOnes(And_imm) - 1;
2417 unsigned LSB = Srl_imm;
2419 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2421 if ((LSB + Width + 1) == N->getValueType(0).getSizeInBits()) {
2422 // It's cheaper to use a right shift to extract the top bits.
2423 if (Subtarget->isThumb()) {
2424 Opc = isSigned ? ARM::t2ASRri : ARM::t2LSRri;
2425 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2426 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2427 getAL(CurDAG, dl), Reg0, Reg0 };
2428 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2432 // ARM models shift instructions as MOVsi with shifter operand.
2433 ARM_AM::ShiftOpc ShOpcVal = ARM_AM::getShiftOpcForNode(ISD::SRL);
2435 CurDAG->getTargetConstant(ARM_AM::getSORegOpc(ShOpcVal, LSB), dl,
2437 SDValue Ops[] = { N->getOperand(0).getOperand(0), ShOpc,
2438 getAL(CurDAG, dl), Reg0, Reg0 };
2439 CurDAG->SelectNodeTo(N, ARM::MOVsi, MVT::i32, Ops);
2443 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2444 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2445 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2446 getAL(CurDAG, dl), Reg0 };
2447 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2454 // Otherwise, we're looking for a shift of a shift
2455 unsigned Shl_imm = 0;
2456 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SHL, Shl_imm)) {
2457 assert(Shl_imm > 0 && Shl_imm < 32 && "bad amount in shift node!");
2458 unsigned Srl_imm = 0;
2459 if (isInt32Immediate(N->getOperand(1), Srl_imm)) {
2460 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2461 // Note: The width operand is encoded as width-1.
2462 unsigned Width = 32 - Srl_imm - 1;
2463 int LSB = Srl_imm - Shl_imm;
2466 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2467 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2468 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2469 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2470 getAL(CurDAG, dl), Reg0 };
2471 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2476 // Or we are looking for a shift of an and, with a mask operand
2477 if (isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::AND, And_imm) &&
2478 isShiftedMask_32(And_imm)) {
2479 unsigned Srl_imm = 0;
2480 unsigned LSB = countTrailingZeros(And_imm);
2481 // Shift must be the same as the ands lsb
2482 if (isInt32Immediate(N->getOperand(1), Srl_imm) && Srl_imm == LSB) {
2483 assert(Srl_imm > 0 && Srl_imm < 32 && "bad amount in shift node!");
2484 unsigned MSB = 31 - countLeadingZeros(And_imm);
2485 // Note: The width operand is encoded as width-1.
2486 unsigned Width = MSB - LSB;
2487 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2488 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2489 CurDAG->getTargetConstant(Srl_imm, dl, MVT::i32),
2490 CurDAG->getTargetConstant(Width, dl, MVT::i32),
2491 getAL(CurDAG, dl), Reg0 };
2492 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2497 if (N->getOpcode() == ISD::SIGN_EXTEND_INREG) {
2498 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits();
2500 if (!isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRL, LSB) &&
2501 !isOpcWithIntImmediate(N->getOperand(0).getNode(), ISD::SRA, LSB))
2504 if (LSB + Width > 32)
2507 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2508 SDValue Ops[] = { N->getOperand(0).getOperand(0),
2509 CurDAG->getTargetConstant(LSB, dl, MVT::i32),
2510 CurDAG->getTargetConstant(Width - 1, dl, MVT::i32),
2511 getAL(CurDAG, dl), Reg0 };
2512 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2519 /// Target-specific DAG combining for ISD::XOR.
2520 /// Target-independent combining lowers SELECT_CC nodes of the form
2521 /// select_cc setg[ge] X, 0, X, -X
2522 /// select_cc setgt X, -1, X, -X
2523 /// select_cc setl[te] X, 0, -X, X
2524 /// select_cc setlt X, 1, -X, X
2525 /// which represent Integer ABS into:
2526 /// Y = sra (X, size(X)-1); xor (add (X, Y), Y)
2527 /// ARM instruction selection detects the latter and matches it to
2528 /// ARM::ABS or ARM::t2ABS machine node.
2529 bool ARMDAGToDAGISel::tryABSOp(SDNode *N){
2530 SDValue XORSrc0 = N->getOperand(0);
2531 SDValue XORSrc1 = N->getOperand(1);
2532 EVT VT = N->getValueType(0);
2534 if (Subtarget->isThumb1Only())
2537 if (XORSrc0.getOpcode() != ISD::ADD || XORSrc1.getOpcode() != ISD::SRA)
2540 SDValue ADDSrc0 = XORSrc0.getOperand(0);
2541 SDValue ADDSrc1 = XORSrc0.getOperand(1);
2542 SDValue SRASrc0 = XORSrc1.getOperand(0);
2543 SDValue SRASrc1 = XORSrc1.getOperand(1);
2544 ConstantSDNode *SRAConstant = dyn_cast<ConstantSDNode>(SRASrc1);
2545 EVT XType = SRASrc0.getValueType();
2546 unsigned Size = XType.getSizeInBits() - 1;
2548 if (ADDSrc1 == XORSrc1 && ADDSrc0 == SRASrc0 &&
2549 XType.isInteger() && SRAConstant != nullptr &&
2550 Size == SRAConstant->getZExtValue()) {
2551 unsigned Opcode = Subtarget->isThumb2() ? ARM::t2ABS : ARM::ABS;
2552 CurDAG->SelectNodeTo(N, Opcode, VT, ADDSrc0);
2559 /// We've got special pseudo-instructions for these
2560 void ARMDAGToDAGISel::SelectCMP_SWAP(SDNode *N) {
2562 EVT MemTy = cast<MemSDNode>(N)->getMemoryVT();
2563 if (MemTy == MVT::i8)
2564 Opcode = ARM::CMP_SWAP_8;
2565 else if (MemTy == MVT::i16)
2566 Opcode = ARM::CMP_SWAP_16;
2567 else if (MemTy == MVT::i32)
2568 Opcode = ARM::CMP_SWAP_32;
2570 llvm_unreachable("Unknown AtomicCmpSwap type");
2572 SDValue Ops[] = {N->getOperand(1), N->getOperand(2), N->getOperand(3),
2574 SDNode *CmpSwap = CurDAG->getMachineNode(
2576 CurDAG->getVTList(MVT::i32, MVT::i32, MVT::Other), Ops);
2578 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
2579 MemOp[0] = cast<MemSDNode>(N)->getMemOperand();
2580 cast<MachineSDNode>(CmpSwap)->setMemRefs(MemOp, MemOp + 1);
2582 ReplaceUses(SDValue(N, 0), SDValue(CmpSwap, 0));
2583 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 2));
2584 CurDAG->RemoveDeadNode(N);
2587 static Optional<std::pair<unsigned, unsigned>>
2588 getContiguousRangeOfSetBits(const APInt &A) {
2589 unsigned FirstOne = A.getBitWidth() - A.countLeadingZeros() - 1;
2590 unsigned LastOne = A.countTrailingZeros();
2591 if (A.countPopulation() != (FirstOne - LastOne + 1))
2592 return Optional<std::pair<unsigned,unsigned>>();
2593 return std::make_pair(FirstOne, LastOne);
2596 void ARMDAGToDAGISel::SelectCMPZ(SDNode *N, bool &SwitchEQNEToPLMI) {
2597 assert(N->getOpcode() == ARMISD::CMPZ);
2598 SwitchEQNEToPLMI = false;
2600 if (!Subtarget->isThumb())
2601 // FIXME: Work out whether it is profitable to do this in A32 mode - LSL and
2602 // LSR don't exist as standalone instructions - they need the barrel shifter.
2605 // select (cmpz (and X, C), #0) -> (LSLS X) or (LSRS X) or (LSRS (LSLS X))
2606 SDValue And = N->getOperand(0);
2607 if (!And->hasOneUse())
2610 SDValue Zero = N->getOperand(1);
2611 if (!isa<ConstantSDNode>(Zero) || !cast<ConstantSDNode>(Zero)->isNullValue() ||
2612 And->getOpcode() != ISD::AND)
2614 SDValue X = And.getOperand(0);
2615 auto C = dyn_cast<ConstantSDNode>(And.getOperand(1));
2617 if (!C || !X->hasOneUse())
2619 auto Range = getContiguousRangeOfSetBits(C->getAPIntValue());
2623 // There are several ways to lower this:
2627 auto EmitShift = [&](unsigned Opc, SDValue Src, unsigned Imm) -> SDNode* {
2628 if (Subtarget->isThumb2()) {
2629 Opc = (Opc == ARM::tLSLri) ? ARM::t2LSLri : ARM::t2LSRri;
2630 SDValue Ops[] = { Src, CurDAG->getTargetConstant(Imm, dl, MVT::i32),
2631 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2632 CurDAG->getRegister(0, MVT::i32) };
2633 return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2635 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), Src,
2636 CurDAG->getTargetConstant(Imm, dl, MVT::i32),
2637 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
2638 return CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
2642 if (Range->second == 0) {
2643 // 1. Mask includes the LSB -> Simply shift the top N bits off
2644 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2645 ReplaceNode(And.getNode(), NewN);
2646 } else if (Range->first == 31) {
2647 // 2. Mask includes the MSB -> Simply shift the bottom N bits off
2648 NewN = EmitShift(ARM::tLSRri, X, Range->second);
2649 ReplaceNode(And.getNode(), NewN);
2650 } else if (Range->first == Range->second) {
2651 // 3. Only one bit is set. We can shift this into the sign bit and use a
2652 // PL/MI comparison.
2653 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2654 ReplaceNode(And.getNode(), NewN);
2656 SwitchEQNEToPLMI = true;
2657 } else if (!Subtarget->hasV6T2Ops()) {
2658 // 4. Do a double shift to clear bottom and top bits, but only in
2659 // thumb-1 mode as in thumb-2 we can use UBFX.
2660 NewN = EmitShift(ARM::tLSLri, X, 31 - Range->first);
2661 NewN = EmitShift(ARM::tLSRri, SDValue(NewN, 0),
2662 Range->second + (31 - Range->first));
2663 ReplaceNode(And.getNode(), NewN);
2668 void ARMDAGToDAGISel::Select(SDNode *N) {
2671 if (N->isMachineOpcode()) {
2673 return; // Already selected.
2676 switch (N->getOpcode()) {
2678 case ISD::WRITE_REGISTER:
2679 if (tryWriteRegister(N))
2682 case ISD::READ_REGISTER:
2683 if (tryReadRegister(N))
2686 case ISD::INLINEASM:
2687 if (tryInlineAsm(N))
2691 // Select special operations if XOR node forms integer ABS pattern
2694 // Other cases are autogenerated.
2696 case ISD::Constant: {
2697 unsigned Val = cast<ConstantSDNode>(N)->getZExtValue();
2698 // If we can't materialize the constant we need to use a literal pool
2699 if (ConstantMaterializationCost(Val) > 2) {
2700 SDValue CPIdx = CurDAG->getTargetConstantPool(
2701 ConstantInt::get(Type::getInt32Ty(*CurDAG->getContext()), Val),
2702 TLI->getPointerTy(CurDAG->getDataLayout()));
2705 if (Subtarget->isThumb()) {
2706 SDValue Pred = getAL(CurDAG, dl);
2707 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
2708 SDValue Ops[] = { CPIdx, Pred, PredReg, CurDAG->getEntryNode() };
2709 ResNode = CurDAG->getMachineNode(ARM::tLDRpci, dl, MVT::i32, MVT::Other,
2714 CurDAG->getTargetConstant(0, dl, MVT::i32),
2716 CurDAG->getRegister(0, MVT::i32),
2717 CurDAG->getEntryNode()
2719 ResNode = CurDAG->getMachineNode(ARM::LDRcp, dl, MVT::i32, MVT::Other,
2722 ReplaceNode(N, ResNode);
2726 // Other cases are autogenerated.
2729 case ISD::FrameIndex: {
2730 // Selects to ADDri FI, 0 which in turn will become ADDri SP, imm.
2731 int FI = cast<FrameIndexSDNode>(N)->getIndex();
2732 SDValue TFI = CurDAG->getTargetFrameIndex(
2733 FI, TLI->getPointerTy(CurDAG->getDataLayout()));
2734 if (Subtarget->isThumb1Only()) {
2735 // Set the alignment of the frame object to 4, to avoid having to generate
2736 // more than one ADD
2737 MachineFrameInfo &MFI = MF->getFrameInfo();
2738 if (MFI.getObjectAlignment(FI) < 4)
2739 MFI.setObjectAlignment(FI, 4);
2740 CurDAG->SelectNodeTo(N, ARM::tADDframe, MVT::i32, TFI,
2741 CurDAG->getTargetConstant(0, dl, MVT::i32));
2744 unsigned Opc = ((Subtarget->isThumb() && Subtarget->hasThumb2()) ?
2745 ARM::t2ADDri : ARM::ADDri);
2746 SDValue Ops[] = { TFI, CurDAG->getTargetConstant(0, dl, MVT::i32),
2747 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
2748 CurDAG->getRegister(0, MVT::i32) };
2749 CurDAG->SelectNodeTo(N, Opc, MVT::i32, Ops);
2754 if (tryV6T2BitfieldExtractOp(N, false))
2757 case ISD::SIGN_EXTEND_INREG:
2759 if (tryV6T2BitfieldExtractOp(N, true))
2763 if (Subtarget->isThumb1Only())
2765 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1))) {
2766 unsigned RHSV = C->getZExtValue();
2768 if (isPowerOf2_32(RHSV-1)) { // 2^n+1?
2769 unsigned ShImm = Log2_32(RHSV-1);
2772 SDValue V = N->getOperand(0);
2773 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2774 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
2775 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2776 if (Subtarget->isThumb()) {
2777 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
2778 CurDAG->SelectNodeTo(N, ARM::t2ADDrs, MVT::i32, Ops);
2781 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
2783 CurDAG->SelectNodeTo(N, ARM::ADDrsi, MVT::i32, Ops);
2787 if (isPowerOf2_32(RHSV+1)) { // 2^n-1?
2788 unsigned ShImm = Log2_32(RHSV+1);
2791 SDValue V = N->getOperand(0);
2792 ShImm = ARM_AM::getSORegOpc(ARM_AM::lsl, ShImm);
2793 SDValue ShImmOp = CurDAG->getTargetConstant(ShImm, dl, MVT::i32);
2794 SDValue Reg0 = CurDAG->getRegister(0, MVT::i32);
2795 if (Subtarget->isThumb()) {
2796 SDValue Ops[] = { V, V, ShImmOp, getAL(CurDAG, dl), Reg0, Reg0 };
2797 CurDAG->SelectNodeTo(N, ARM::t2RSBrs, MVT::i32, Ops);
2800 SDValue Ops[] = { V, V, Reg0, ShImmOp, getAL(CurDAG, dl), Reg0,
2802 CurDAG->SelectNodeTo(N, ARM::RSBrsi, MVT::i32, Ops);
2809 // Check for unsigned bitfield extract
2810 if (tryV6T2BitfieldExtractOp(N, false))
2813 // If an immediate is used in an AND node, it is possible that the immediate
2814 // can be more optimally materialized when negated. If this is the case we
2815 // can negate the immediate and use a BIC instead.
2816 auto *N1C = dyn_cast<ConstantSDNode>(N->getOperand(1));
2817 if (N1C && N1C->hasOneUse() && Subtarget->isThumb()) {
2818 uint32_t Imm = (uint32_t) N1C->getZExtValue();
2820 // In Thumb2 mode, an AND can take a 12-bit immediate. If this
2821 // immediate can be negated and fit in the immediate operand of
2822 // a t2BIC, don't do any manual transform here as this can be
2823 // handled by the generic ISel machinery.
2824 bool PreferImmediateEncoding =
2825 Subtarget->hasThumb2() && (is_t2_so_imm(Imm) || is_t2_so_imm_not(Imm));
2826 if (!PreferImmediateEncoding &&
2827 ConstantMaterializationCost(Imm) >
2828 ConstantMaterializationCost(~Imm)) {
2829 // The current immediate costs more to materialize than a negated
2830 // immediate, so negate the immediate and use a BIC.
2832 CurDAG->getConstant(~N1C->getZExtValue(), dl, MVT::i32);
2833 // If the new constant didn't exist before, reposition it in the topological
2834 // ordering so it is just before N. Otherwise, don't touch its location.
2835 if (NewImm->getNodeId() == -1)
2836 CurDAG->RepositionNode(N->getIterator(), NewImm.getNode());
2838 if (!Subtarget->hasThumb2()) {
2839 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32),
2840 N->getOperand(0), NewImm, getAL(CurDAG, dl),
2841 CurDAG->getRegister(0, MVT::i32)};
2842 ReplaceNode(N, CurDAG->getMachineNode(ARM::tBIC, dl, MVT::i32, Ops));
2845 SDValue Ops[] = {N->getOperand(0), NewImm, getAL(CurDAG, dl),
2846 CurDAG->getRegister(0, MVT::i32),
2847 CurDAG->getRegister(0, MVT::i32)};
2849 CurDAG->getMachineNode(ARM::t2BICrr, dl, MVT::i32, Ops));
2855 // (and (or x, c2), c1) and top 16-bits of c1 and c2 match, lower 16-bits
2856 // of c1 are 0xffff, and lower 16-bit of c2 are 0. That is, the top 16-bits
2857 // are entirely contributed by c2 and lower 16-bits are entirely contributed
2858 // by x. That's equal to (or (and x, 0xffff), (and c1, 0xffff0000)).
2859 // Select it to: "movt x, ((c1 & 0xffff) >> 16)
2860 EVT VT = N->getValueType(0);
2863 unsigned Opc = (Subtarget->isThumb() && Subtarget->hasThumb2())
2865 : (Subtarget->hasV6T2Ops() ? ARM::MOVTi16 : 0);
2868 SDValue N0 = N->getOperand(0), N1 = N->getOperand(1);
2869 N1C = dyn_cast<ConstantSDNode>(N1);
2872 if (N0.getOpcode() == ISD::OR && N0.getNode()->hasOneUse()) {
2873 SDValue N2 = N0.getOperand(1);
2874 ConstantSDNode *N2C = dyn_cast<ConstantSDNode>(N2);
2877 unsigned N1CVal = N1C->getZExtValue();
2878 unsigned N2CVal = N2C->getZExtValue();
2879 if ((N1CVal & 0xffff0000U) == (N2CVal & 0xffff0000U) &&
2880 (N1CVal & 0xffffU) == 0xffffU &&
2881 (N2CVal & 0xffffU) == 0x0U) {
2882 SDValue Imm16 = CurDAG->getTargetConstant((N2CVal & 0xFFFF0000U) >> 16,
2884 SDValue Ops[] = { N0.getOperand(0), Imm16,
2885 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32) };
2886 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, Ops));
2893 case ARMISD::UMAAL: {
2894 unsigned Opc = Subtarget->isThumb() ? ARM::t2UMAAL : ARM::UMAAL;
2895 SDValue Ops[] = { N->getOperand(0), N->getOperand(1),
2896 N->getOperand(2), N->getOperand(3),
2898 CurDAG->getRegister(0, MVT::i32) };
2899 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, MVT::i32, Ops));
2902 case ARMISD::UMLAL:{
2903 if (Subtarget->isThumb()) {
2904 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2905 N->getOperand(3), getAL(CurDAG, dl),
2906 CurDAG->getRegister(0, MVT::i32)};
2908 N, CurDAG->getMachineNode(ARM::t2UMLAL, dl, MVT::i32, MVT::i32, Ops));
2911 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2912 N->getOperand(3), getAL(CurDAG, dl),
2913 CurDAG->getRegister(0, MVT::i32),
2914 CurDAG->getRegister(0, MVT::i32) };
2915 ReplaceNode(N, CurDAG->getMachineNode(
2916 Subtarget->hasV6Ops() ? ARM::UMLAL : ARM::UMLALv5, dl,
2917 MVT::i32, MVT::i32, Ops));
2921 case ARMISD::SMLAL:{
2922 if (Subtarget->isThumb()) {
2923 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2924 N->getOperand(3), getAL(CurDAG, dl),
2925 CurDAG->getRegister(0, MVT::i32)};
2927 N, CurDAG->getMachineNode(ARM::t2SMLAL, dl, MVT::i32, MVT::i32, Ops));
2930 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2),
2931 N->getOperand(3), getAL(CurDAG, dl),
2932 CurDAG->getRegister(0, MVT::i32),
2933 CurDAG->getRegister(0, MVT::i32) };
2934 ReplaceNode(N, CurDAG->getMachineNode(
2935 Subtarget->hasV6Ops() ? ARM::SMLAL : ARM::SMLALv5, dl,
2936 MVT::i32, MVT::i32, Ops));
2940 case ARMISD::SUBE: {
2941 if (!Subtarget->hasV6Ops())
2943 // Look for a pattern to match SMMLS
2944 // (sube a, (smul_loHi a, b), (subc 0, (smul_LOhi(a, b))))
2945 if (N->getOperand(1).getOpcode() != ISD::SMUL_LOHI ||
2946 N->getOperand(2).getOpcode() != ARMISD::SUBC ||
2947 !SDValue(N, 1).use_empty())
2950 if (Subtarget->isThumb())
2951 assert(Subtarget->hasThumb2() &&
2952 "This pattern should not be generated for Thumb");
2954 SDValue SmulLoHi = N->getOperand(1);
2955 SDValue Subc = N->getOperand(2);
2956 auto *Zero = dyn_cast<ConstantSDNode>(Subc.getOperand(0));
2958 if (!Zero || Zero->getZExtValue() != 0 ||
2959 Subc.getOperand(1) != SmulLoHi.getValue(0) ||
2960 N->getOperand(1) != SmulLoHi.getValue(1) ||
2961 N->getOperand(2) != Subc.getValue(1))
2964 unsigned Opc = Subtarget->isThumb2() ? ARM::t2SMMLS : ARM::SMMLS;
2965 SDValue Ops[] = { SmulLoHi.getOperand(0), SmulLoHi.getOperand(1),
2966 N->getOperand(0), getAL(CurDAG, dl),
2967 CurDAG->getRegister(0, MVT::i32) };
2968 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops));
2972 if (Subtarget->isThumb() && Subtarget->hasThumb2()) {
2973 if (tryT2IndexedLoad(N))
2975 } else if (Subtarget->isThumb()) {
2976 if (tryT1IndexedLoad(N))
2978 } else if (tryARMIndexedLoad(N))
2980 // Other cases are autogenerated.
2983 case ARMISD::BRCOND: {
2984 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2985 // Emits: (Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2986 // Pattern complexity = 6 cost = 1 size = 0
2988 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2989 // Emits: (tBcc:void (bb:Other):$dst, (imm:i32):$cc)
2990 // Pattern complexity = 6 cost = 1 size = 0
2992 // Pattern: (ARMbrcond:void (bb:Other):$dst, (imm:i32):$cc)
2993 // Emits: (t2Bcc:void (bb:Other):$dst, (imm:i32):$cc)
2994 // Pattern complexity = 6 cost = 1 size = 0
2996 unsigned Opc = Subtarget->isThumb() ?
2997 ((Subtarget->hasThumb2()) ? ARM::t2Bcc : ARM::tBcc) : ARM::Bcc;
2998 SDValue Chain = N->getOperand(0);
2999 SDValue N1 = N->getOperand(1);
3000 SDValue N2 = N->getOperand(2);
3001 SDValue N3 = N->getOperand(3);
3002 SDValue InFlag = N->getOperand(4);
3003 assert(N1.getOpcode() == ISD::BasicBlock);
3004 assert(N2.getOpcode() == ISD::Constant);
3005 assert(N3.getOpcode() == ISD::Register);
3007 unsigned CC = (unsigned) cast<ConstantSDNode>(N2)->getZExtValue();
3009 if (InFlag.getOpcode() == ARMISD::CMPZ) {
3010 bool SwitchEQNEToPLMI;
3011 SelectCMPZ(InFlag.getNode(), SwitchEQNEToPLMI);
3012 InFlag = N->getOperand(4);
3014 if (SwitchEQNEToPLMI) {
3015 switch ((ARMCC::CondCodes)CC) {
3016 default: llvm_unreachable("CMPZ must be either NE or EQ!");
3018 CC = (unsigned)ARMCC::MI;
3021 CC = (unsigned)ARMCC::PL;
3027 SDValue Tmp2 = CurDAG->getTargetConstant(CC, dl, MVT::i32);
3028 SDValue Ops[] = { N1, Tmp2, N3, Chain, InFlag };
3029 SDNode *ResNode = CurDAG->getMachineNode(Opc, dl, MVT::Other,
3031 Chain = SDValue(ResNode, 0);
3032 if (N->getNumValues() == 2) {
3033 InFlag = SDValue(ResNode, 1);
3034 ReplaceUses(SDValue(N, 1), InFlag);
3036 ReplaceUses(SDValue(N, 0),
3037 SDValue(Chain.getNode(), Chain.getResNo()));
3038 CurDAG->RemoveDeadNode(N);
3042 case ARMISD::CMPZ: {
3043 // select (CMPZ X, #-C) -> (CMPZ (ADDS X, #C), #0)
3044 // This allows us to avoid materializing the expensive negative constant.
3045 // The CMPZ #0 is useless and will be peepholed away but we need to keep it
3046 // for its glue output.
3047 SDValue X = N->getOperand(0);
3048 auto *C = dyn_cast<ConstantSDNode>(N->getOperand(1).getNode());
3049 if (C && C->getSExtValue() < 0 && Subtarget->isThumb()) {
3050 int64_t Addend = -C->getSExtValue();
3052 SDNode *Add = nullptr;
3053 // ADDS can be better than CMN if the immediate fits in a
3054 // 16-bit ADDS, which means either [0,256) for tADDi8 or [0,8) for tADDi3.
3055 // Outside that range we can just use a CMN which is 32-bit but has a
3056 // 12-bit immediate range.
3057 if (Addend < 1<<8) {
3058 if (Subtarget->isThumb2()) {
3059 SDValue Ops[] = { X, CurDAG->getTargetConstant(Addend, dl, MVT::i32),
3060 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32),
3061 CurDAG->getRegister(0, MVT::i32) };
3062 Add = CurDAG->getMachineNode(ARM::t2ADDri, dl, MVT::i32, Ops);
3064 unsigned Opc = (Addend < 1<<3) ? ARM::tADDi3 : ARM::tADDi8;
3065 SDValue Ops[] = {CurDAG->getRegister(ARM::CPSR, MVT::i32), X,
3066 CurDAG->getTargetConstant(Addend, dl, MVT::i32),
3067 getAL(CurDAG, dl), CurDAG->getRegister(0, MVT::i32)};
3068 Add = CurDAG->getMachineNode(Opc, dl, MVT::i32, Ops);
3072 SDValue Ops2[] = {SDValue(Add, 0), CurDAG->getConstant(0, dl, MVT::i32)};
3073 CurDAG->MorphNodeTo(N, ARMISD::CMPZ, CurDAG->getVTList(MVT::Glue), Ops2);
3076 // Other cases are autogenerated.
3080 case ARMISD::CMOV: {
3081 SDValue InFlag = N->getOperand(4);
3083 if (InFlag.getOpcode() == ARMISD::CMPZ) {
3084 bool SwitchEQNEToPLMI;
3085 SelectCMPZ(InFlag.getNode(), SwitchEQNEToPLMI);
3087 if (SwitchEQNEToPLMI) {
3088 SDValue ARMcc = N->getOperand(2);
3089 ARMCC::CondCodes CC =
3090 (ARMCC::CondCodes)cast<ConstantSDNode>(ARMcc)->getZExtValue();
3093 default: llvm_unreachable("CMPZ must be either NE or EQ!");
3101 SDValue NewARMcc = CurDAG->getConstant((unsigned)CC, dl, MVT::i32);
3102 SDValue Ops[] = {N->getOperand(0), N->getOperand(1), NewARMcc,
3103 N->getOperand(3), N->getOperand(4)};
3104 CurDAG->MorphNodeTo(N, ARMISD::CMOV, N->getVTList(), Ops);
3108 // Other cases are autogenerated.
3112 case ARMISD::VZIP: {
3114 EVT VT = N->getValueType(0);
3115 switch (VT.getSimpleVT().SimpleTy) {
3117 case MVT::v8i8: Opc = ARM::VZIPd8; break;
3118 case MVT::v4i16: Opc = ARM::VZIPd16; break;
3120 // vzip.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
3121 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3122 case MVT::v16i8: Opc = ARM::VZIPq8; break;
3123 case MVT::v8i16: Opc = ARM::VZIPq16; break;
3125 case MVT::v4i32: Opc = ARM::VZIPq32; break;
3127 SDValue Pred = getAL(CurDAG, dl);
3128 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3129 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
3130 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3133 case ARMISD::VUZP: {
3135 EVT VT = N->getValueType(0);
3136 switch (VT.getSimpleVT().SimpleTy) {
3138 case MVT::v8i8: Opc = ARM::VUZPd8; break;
3139 case MVT::v4i16: Opc = ARM::VUZPd16; break;
3141 // vuzp.32 Dd, Dm is a pseudo-instruction expanded to vtrn.32 Dd, Dm.
3142 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3143 case MVT::v16i8: Opc = ARM::VUZPq8; break;
3144 case MVT::v8i16: Opc = ARM::VUZPq16; break;
3146 case MVT::v4i32: Opc = ARM::VUZPq32; break;
3148 SDValue Pred = getAL(CurDAG, dl);
3149 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3150 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
3151 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3154 case ARMISD::VTRN: {
3156 EVT VT = N->getValueType(0);
3157 switch (VT.getSimpleVT().SimpleTy) {
3159 case MVT::v8i8: Opc = ARM::VTRNd8; break;
3160 case MVT::v4i16: Opc = ARM::VTRNd16; break;
3162 case MVT::v2i32: Opc = ARM::VTRNd32; break;
3163 case MVT::v16i8: Opc = ARM::VTRNq8; break;
3164 case MVT::v8i16: Opc = ARM::VTRNq16; break;
3166 case MVT::v4i32: Opc = ARM::VTRNq32; break;
3168 SDValue Pred = getAL(CurDAG, dl);
3169 SDValue PredReg = CurDAG->getRegister(0, MVT::i32);
3170 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), Pred, PredReg };
3171 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, VT, VT, Ops));
3174 case ARMISD::BUILD_VECTOR: {
3175 EVT VecVT = N->getValueType(0);
3176 EVT EltVT = VecVT.getVectorElementType();
3177 unsigned NumElts = VecVT.getVectorNumElements();
3178 if (EltVT == MVT::f64) {
3179 assert(NumElts == 2 && "unexpected type for BUILD_VECTOR");
3181 N, createDRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
3184 assert(EltVT == MVT::f32 && "unexpected type for BUILD_VECTOR");
3187 N, createSRegPairNode(VecVT, N->getOperand(0), N->getOperand(1)));
3190 assert(NumElts == 4 && "unexpected type for BUILD_VECTOR");
3192 createQuadSRegsNode(VecVT, N->getOperand(0), N->getOperand(1),
3193 N->getOperand(2), N->getOperand(3)));
3197 case ARMISD::VLD1DUP: {
3198 static const uint16_t DOpcodes[] = { ARM::VLD1DUPd8, ARM::VLD1DUPd16,
3200 static const uint16_t QOpcodes[] = { ARM::VLD1DUPq8, ARM::VLD1DUPq16,
3202 SelectVLDDup(N, false, 1, DOpcodes, QOpcodes);
3206 case ARMISD::VLD2DUP: {
3207 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8, ARM::VLD2DUPd16,
3209 SelectVLDDup(N, false, 2, Opcodes);
3213 case ARMISD::VLD3DUP: {
3214 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo,
3215 ARM::VLD3DUPd16Pseudo,
3216 ARM::VLD3DUPd32Pseudo };
3217 SelectVLDDup(N, false, 3, Opcodes);
3221 case ARMISD::VLD4DUP: {
3222 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo,
3223 ARM::VLD4DUPd16Pseudo,
3224 ARM::VLD4DUPd32Pseudo };
3225 SelectVLDDup(N, false, 4, Opcodes);
3229 case ARMISD::VLD1DUP_UPD: {
3230 static const uint16_t DOpcodes[] = { ARM::VLD1DUPd8wb_fixed,
3231 ARM::VLD1DUPd16wb_fixed,
3232 ARM::VLD1DUPd32wb_fixed };
3233 static const uint16_t QOpcodes[] = { ARM::VLD1DUPq8wb_fixed,
3234 ARM::VLD1DUPq16wb_fixed,
3235 ARM::VLD1DUPq32wb_fixed };
3236 SelectVLDDup(N, true, 1, DOpcodes, QOpcodes);
3240 case ARMISD::VLD2DUP_UPD: {
3241 static const uint16_t Opcodes[] = { ARM::VLD2DUPd8wb_fixed,
3242 ARM::VLD2DUPd16wb_fixed,
3243 ARM::VLD2DUPd32wb_fixed };
3244 SelectVLDDup(N, true, 2, Opcodes);
3248 case ARMISD::VLD3DUP_UPD: {
3249 static const uint16_t Opcodes[] = { ARM::VLD3DUPd8Pseudo_UPD,
3250 ARM::VLD3DUPd16Pseudo_UPD,
3251 ARM::VLD3DUPd32Pseudo_UPD };
3252 SelectVLDDup(N, true, 3, Opcodes);
3256 case ARMISD::VLD4DUP_UPD: {
3257 static const uint16_t Opcodes[] = { ARM::VLD4DUPd8Pseudo_UPD,
3258 ARM::VLD4DUPd16Pseudo_UPD,
3259 ARM::VLD4DUPd32Pseudo_UPD };
3260 SelectVLDDup(N, true, 4, Opcodes);
3264 case ARMISD::VLD1_UPD: {
3265 static const uint16_t DOpcodes[] = { ARM::VLD1d8wb_fixed,
3266 ARM::VLD1d16wb_fixed,
3267 ARM::VLD1d32wb_fixed,
3268 ARM::VLD1d64wb_fixed };
3269 static const uint16_t QOpcodes[] = { ARM::VLD1q8wb_fixed,
3270 ARM::VLD1q16wb_fixed,
3271 ARM::VLD1q32wb_fixed,
3272 ARM::VLD1q64wb_fixed };
3273 SelectVLD(N, true, 1, DOpcodes, QOpcodes, nullptr);
3277 case ARMISD::VLD2_UPD: {
3278 static const uint16_t DOpcodes[] = { ARM::VLD2d8wb_fixed,
3279 ARM::VLD2d16wb_fixed,
3280 ARM::VLD2d32wb_fixed,
3281 ARM::VLD1q64wb_fixed};
3282 static const uint16_t QOpcodes[] = { ARM::VLD2q8PseudoWB_fixed,
3283 ARM::VLD2q16PseudoWB_fixed,
3284 ARM::VLD2q32PseudoWB_fixed };
3285 SelectVLD(N, true, 2, DOpcodes, QOpcodes, nullptr);
3289 case ARMISD::VLD3_UPD: {
3290 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo_UPD,
3291 ARM::VLD3d16Pseudo_UPD,
3292 ARM::VLD3d32Pseudo_UPD,
3293 ARM::VLD1d64TPseudoWB_fixed};
3294 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3295 ARM::VLD3q16Pseudo_UPD,
3296 ARM::VLD3q32Pseudo_UPD };
3297 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo_UPD,
3298 ARM::VLD3q16oddPseudo_UPD,
3299 ARM::VLD3q32oddPseudo_UPD };
3300 SelectVLD(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
3304 case ARMISD::VLD4_UPD: {
3305 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo_UPD,
3306 ARM::VLD4d16Pseudo_UPD,
3307 ARM::VLD4d32Pseudo_UPD,
3308 ARM::VLD1d64QPseudoWB_fixed};
3309 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3310 ARM::VLD4q16Pseudo_UPD,
3311 ARM::VLD4q32Pseudo_UPD };
3312 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo_UPD,
3313 ARM::VLD4q16oddPseudo_UPD,
3314 ARM::VLD4q32oddPseudo_UPD };
3315 SelectVLD(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
3319 case ARMISD::VLD2LN_UPD: {
3320 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo_UPD,
3321 ARM::VLD2LNd16Pseudo_UPD,
3322 ARM::VLD2LNd32Pseudo_UPD };
3323 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo_UPD,
3324 ARM::VLD2LNq32Pseudo_UPD };
3325 SelectVLDSTLane(N, true, true, 2, DOpcodes, QOpcodes);
3329 case ARMISD::VLD3LN_UPD: {
3330 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo_UPD,
3331 ARM::VLD3LNd16Pseudo_UPD,
3332 ARM::VLD3LNd32Pseudo_UPD };
3333 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo_UPD,
3334 ARM::VLD3LNq32Pseudo_UPD };
3335 SelectVLDSTLane(N, true, true, 3, DOpcodes, QOpcodes);
3339 case ARMISD::VLD4LN_UPD: {
3340 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo_UPD,
3341 ARM::VLD4LNd16Pseudo_UPD,
3342 ARM::VLD4LNd32Pseudo_UPD };
3343 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo_UPD,
3344 ARM::VLD4LNq32Pseudo_UPD };
3345 SelectVLDSTLane(N, true, true, 4, DOpcodes, QOpcodes);
3349 case ARMISD::VST1_UPD: {
3350 static const uint16_t DOpcodes[] = { ARM::VST1d8wb_fixed,
3351 ARM::VST1d16wb_fixed,
3352 ARM::VST1d32wb_fixed,
3353 ARM::VST1d64wb_fixed };
3354 static const uint16_t QOpcodes[] = { ARM::VST1q8wb_fixed,
3355 ARM::VST1q16wb_fixed,
3356 ARM::VST1q32wb_fixed,
3357 ARM::VST1q64wb_fixed };
3358 SelectVST(N, true, 1, DOpcodes, QOpcodes, nullptr);
3362 case ARMISD::VST2_UPD: {
3363 static const uint16_t DOpcodes[] = { ARM::VST2d8wb_fixed,
3364 ARM::VST2d16wb_fixed,
3365 ARM::VST2d32wb_fixed,
3366 ARM::VST1q64wb_fixed};
3367 static const uint16_t QOpcodes[] = { ARM::VST2q8PseudoWB_fixed,
3368 ARM::VST2q16PseudoWB_fixed,
3369 ARM::VST2q32PseudoWB_fixed };
3370 SelectVST(N, true, 2, DOpcodes, QOpcodes, nullptr);
3374 case ARMISD::VST3_UPD: {
3375 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo_UPD,
3376 ARM::VST3d16Pseudo_UPD,
3377 ARM::VST3d32Pseudo_UPD,
3378 ARM::VST1d64TPseudoWB_fixed};
3379 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3380 ARM::VST3q16Pseudo_UPD,
3381 ARM::VST3q32Pseudo_UPD };
3382 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo_UPD,
3383 ARM::VST3q16oddPseudo_UPD,
3384 ARM::VST3q32oddPseudo_UPD };
3385 SelectVST(N, true, 3, DOpcodes, QOpcodes0, QOpcodes1);
3389 case ARMISD::VST4_UPD: {
3390 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo_UPD,
3391 ARM::VST4d16Pseudo_UPD,
3392 ARM::VST4d32Pseudo_UPD,
3393 ARM::VST1d64QPseudoWB_fixed};
3394 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3395 ARM::VST4q16Pseudo_UPD,
3396 ARM::VST4q32Pseudo_UPD };
3397 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo_UPD,
3398 ARM::VST4q16oddPseudo_UPD,
3399 ARM::VST4q32oddPseudo_UPD };
3400 SelectVST(N, true, 4, DOpcodes, QOpcodes0, QOpcodes1);
3404 case ARMISD::VST2LN_UPD: {
3405 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo_UPD,
3406 ARM::VST2LNd16Pseudo_UPD,
3407 ARM::VST2LNd32Pseudo_UPD };
3408 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo_UPD,
3409 ARM::VST2LNq32Pseudo_UPD };
3410 SelectVLDSTLane(N, false, true, 2, DOpcodes, QOpcodes);
3414 case ARMISD::VST3LN_UPD: {
3415 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo_UPD,
3416 ARM::VST3LNd16Pseudo_UPD,
3417 ARM::VST3LNd32Pseudo_UPD };
3418 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo_UPD,
3419 ARM::VST3LNq32Pseudo_UPD };
3420 SelectVLDSTLane(N, false, true, 3, DOpcodes, QOpcodes);
3424 case ARMISD::VST4LN_UPD: {
3425 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo_UPD,
3426 ARM::VST4LNd16Pseudo_UPD,
3427 ARM::VST4LNd32Pseudo_UPD };
3428 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo_UPD,
3429 ARM::VST4LNq32Pseudo_UPD };
3430 SelectVLDSTLane(N, false, true, 4, DOpcodes, QOpcodes);
3434 case ISD::INTRINSIC_VOID:
3435 case ISD::INTRINSIC_W_CHAIN: {
3436 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue();
3441 case Intrinsic::arm_mrrc:
3442 case Intrinsic::arm_mrrc2: {
3444 SDValue Chain = N->getOperand(0);
3447 if (Subtarget->isThumb())
3448 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::t2MRRC : ARM::t2MRRC2);
3450 Opc = (IntNo == Intrinsic::arm_mrrc ? ARM::MRRC : ARM::MRRC2);
3452 SmallVector<SDValue, 5> Ops;
3453 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(2))->getZExtValue(), dl)); /* coproc */
3454 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(3))->getZExtValue(), dl)); /* opc */
3455 Ops.push_back(getI32Imm(cast<ConstantSDNode>(N->getOperand(4))->getZExtValue(), dl)); /* CRm */
3457 // The mrrc2 instruction in ARM doesn't allow predicates, the top 4 bits of the encoded
3458 // instruction will always be '1111' but it is possible in assembly language to specify
3459 // AL as a predicate to mrrc2 but it doesn't make any difference to the encoded instruction.
3460 if (Opc != ARM::MRRC2) {
3461 Ops.push_back(getAL(CurDAG, dl));
3462 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3465 Ops.push_back(Chain);
3467 // Writes to two registers.
3468 const EVT RetType[] = {MVT::i32, MVT::i32, MVT::Other};
3470 ReplaceNode(N, CurDAG->getMachineNode(Opc, dl, RetType, Ops));
3473 case Intrinsic::arm_ldaexd:
3474 case Intrinsic::arm_ldrexd: {
3476 SDValue Chain = N->getOperand(0);
3477 SDValue MemAddr = N->getOperand(2);
3478 bool isThumb = Subtarget->isThumb() && Subtarget->hasV8MBaselineOps();
3480 bool IsAcquire = IntNo == Intrinsic::arm_ldaexd;
3481 unsigned NewOpc = isThumb ? (IsAcquire ? ARM::t2LDAEXD : ARM::t2LDREXD)
3482 : (IsAcquire ? ARM::LDAEXD : ARM::LDREXD);
3484 // arm_ldrexd returns a i64 value in {i32, i32}
3485 std::vector<EVT> ResTys;
3487 ResTys.push_back(MVT::i32);
3488 ResTys.push_back(MVT::i32);
3490 ResTys.push_back(MVT::Untyped);
3491 ResTys.push_back(MVT::Other);
3493 // Place arguments in the right order.
3494 SDValue Ops[] = {MemAddr, getAL(CurDAG, dl),
3495 CurDAG->getRegister(0, MVT::i32), Chain};
3496 SDNode *Ld = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
3497 // Transfer memoperands.
3498 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3499 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3500 cast<MachineSDNode>(Ld)->setMemRefs(MemOp, MemOp + 1);
3503 SDValue OutChain = isThumb ? SDValue(Ld, 2) : SDValue(Ld, 1);
3504 if (!SDValue(N, 0).use_empty()) {
3507 Result = SDValue(Ld, 0);
3510 CurDAG->getTargetConstant(ARM::gsub_0, dl, MVT::i32);
3511 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
3512 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
3513 Result = SDValue(ResNode,0);
3515 ReplaceUses(SDValue(N, 0), Result);
3517 if (!SDValue(N, 1).use_empty()) {
3520 Result = SDValue(Ld, 1);
3523 CurDAG->getTargetConstant(ARM::gsub_1, dl, MVT::i32);
3524 SDNode *ResNode = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG,
3525 dl, MVT::i32, SDValue(Ld, 0), SubRegIdx);
3526 Result = SDValue(ResNode,0);
3528 ReplaceUses(SDValue(N, 1), Result);
3530 ReplaceUses(SDValue(N, 2), OutChain);
3531 CurDAG->RemoveDeadNode(N);
3534 case Intrinsic::arm_stlexd:
3535 case Intrinsic::arm_strexd: {
3537 SDValue Chain = N->getOperand(0);
3538 SDValue Val0 = N->getOperand(2);
3539 SDValue Val1 = N->getOperand(3);
3540 SDValue MemAddr = N->getOperand(4);
3542 // Store exclusive double return a i32 value which is the return status
3543 // of the issued store.
3544 const EVT ResTys[] = {MVT::i32, MVT::Other};
3546 bool isThumb = Subtarget->isThumb() && Subtarget->hasThumb2();
3547 // Place arguments in the right order.
3548 SmallVector<SDValue, 7> Ops;
3550 Ops.push_back(Val0);
3551 Ops.push_back(Val1);
3553 // arm_strexd uses GPRPair.
3554 Ops.push_back(SDValue(createGPRPairNode(MVT::Untyped, Val0, Val1), 0));
3555 Ops.push_back(MemAddr);
3556 Ops.push_back(getAL(CurDAG, dl));
3557 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
3558 Ops.push_back(Chain);
3560 bool IsRelease = IntNo == Intrinsic::arm_stlexd;
3561 unsigned NewOpc = isThumb ? (IsRelease ? ARM::t2STLEXD : ARM::t2STREXD)
3562 : (IsRelease ? ARM::STLEXD : ARM::STREXD);
3564 SDNode *St = CurDAG->getMachineNode(NewOpc, dl, ResTys, Ops);
3565 // Transfer memoperands.
3566 MachineSDNode::mmo_iterator MemOp = MF->allocateMemRefsArray(1);
3567 MemOp[0] = cast<MemIntrinsicSDNode>(N)->getMemOperand();
3568 cast<MachineSDNode>(St)->setMemRefs(MemOp, MemOp + 1);
3574 case Intrinsic::arm_neon_vld1: {
3575 static const uint16_t DOpcodes[] = { ARM::VLD1d8, ARM::VLD1d16,
3576 ARM::VLD1d32, ARM::VLD1d64 };
3577 static const uint16_t QOpcodes[] = { ARM::VLD1q8, ARM::VLD1q16,
3578 ARM::VLD1q32, ARM::VLD1q64};
3579 SelectVLD(N, false, 1, DOpcodes, QOpcodes, nullptr);
3583 case Intrinsic::arm_neon_vld2: {
3584 static const uint16_t DOpcodes[] = { ARM::VLD2d8, ARM::VLD2d16,
3585 ARM::VLD2d32, ARM::VLD1q64 };
3586 static const uint16_t QOpcodes[] = { ARM::VLD2q8Pseudo, ARM::VLD2q16Pseudo,
3587 ARM::VLD2q32Pseudo };
3588 SelectVLD(N, false, 2, DOpcodes, QOpcodes, nullptr);
3592 case Intrinsic::arm_neon_vld3: {
3593 static const uint16_t DOpcodes[] = { ARM::VLD3d8Pseudo,
3596 ARM::VLD1d64TPseudo };
3597 static const uint16_t QOpcodes0[] = { ARM::VLD3q8Pseudo_UPD,
3598 ARM::VLD3q16Pseudo_UPD,
3599 ARM::VLD3q32Pseudo_UPD };
3600 static const uint16_t QOpcodes1[] = { ARM::VLD3q8oddPseudo,
3601 ARM::VLD3q16oddPseudo,
3602 ARM::VLD3q32oddPseudo };
3603 SelectVLD(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3607 case Intrinsic::arm_neon_vld4: {
3608 static const uint16_t DOpcodes[] = { ARM::VLD4d8Pseudo,
3611 ARM::VLD1d64QPseudo };
3612 static const uint16_t QOpcodes0[] = { ARM::VLD4q8Pseudo_UPD,
3613 ARM::VLD4q16Pseudo_UPD,
3614 ARM::VLD4q32Pseudo_UPD };
3615 static const uint16_t QOpcodes1[] = { ARM::VLD4q8oddPseudo,
3616 ARM::VLD4q16oddPseudo,
3617 ARM::VLD4q32oddPseudo };
3618 SelectVLD(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3622 case Intrinsic::arm_neon_vld2lane: {
3623 static const uint16_t DOpcodes[] = { ARM::VLD2LNd8Pseudo,
3624 ARM::VLD2LNd16Pseudo,
3625 ARM::VLD2LNd32Pseudo };
3626 static const uint16_t QOpcodes[] = { ARM::VLD2LNq16Pseudo,
3627 ARM::VLD2LNq32Pseudo };
3628 SelectVLDSTLane(N, true, false, 2, DOpcodes, QOpcodes);
3632 case Intrinsic::arm_neon_vld3lane: {
3633 static const uint16_t DOpcodes[] = { ARM::VLD3LNd8Pseudo,
3634 ARM::VLD3LNd16Pseudo,
3635 ARM::VLD3LNd32Pseudo };
3636 static const uint16_t QOpcodes[] = { ARM::VLD3LNq16Pseudo,
3637 ARM::VLD3LNq32Pseudo };
3638 SelectVLDSTLane(N, true, false, 3, DOpcodes, QOpcodes);
3642 case Intrinsic::arm_neon_vld4lane: {
3643 static const uint16_t DOpcodes[] = { ARM::VLD4LNd8Pseudo,
3644 ARM::VLD4LNd16Pseudo,
3645 ARM::VLD4LNd32Pseudo };
3646 static const uint16_t QOpcodes[] = { ARM::VLD4LNq16Pseudo,
3647 ARM::VLD4LNq32Pseudo };
3648 SelectVLDSTLane(N, true, false, 4, DOpcodes, QOpcodes);
3652 case Intrinsic::arm_neon_vst1: {
3653 static const uint16_t DOpcodes[] = { ARM::VST1d8, ARM::VST1d16,
3654 ARM::VST1d32, ARM::VST1d64 };
3655 static const uint16_t QOpcodes[] = { ARM::VST1q8, ARM::VST1q16,
3656 ARM::VST1q32, ARM::VST1q64 };
3657 SelectVST(N, false, 1, DOpcodes, QOpcodes, nullptr);
3661 case Intrinsic::arm_neon_vst2: {
3662 static const uint16_t DOpcodes[] = { ARM::VST2d8, ARM::VST2d16,
3663 ARM::VST2d32, ARM::VST1q64 };
3664 static const uint16_t QOpcodes[] = { ARM::VST2q8Pseudo, ARM::VST2q16Pseudo,
3665 ARM::VST2q32Pseudo };
3666 SelectVST(N, false, 2, DOpcodes, QOpcodes, nullptr);
3670 case Intrinsic::arm_neon_vst3: {
3671 static const uint16_t DOpcodes[] = { ARM::VST3d8Pseudo,
3674 ARM::VST1d64TPseudo };
3675 static const uint16_t QOpcodes0[] = { ARM::VST3q8Pseudo_UPD,
3676 ARM::VST3q16Pseudo_UPD,
3677 ARM::VST3q32Pseudo_UPD };
3678 static const uint16_t QOpcodes1[] = { ARM::VST3q8oddPseudo,
3679 ARM::VST3q16oddPseudo,
3680 ARM::VST3q32oddPseudo };
3681 SelectVST(N, false, 3, DOpcodes, QOpcodes0, QOpcodes1);
3685 case Intrinsic::arm_neon_vst4: {
3686 static const uint16_t DOpcodes[] = { ARM::VST4d8Pseudo,
3689 ARM::VST1d64QPseudo };
3690 static const uint16_t QOpcodes0[] = { ARM::VST4q8Pseudo_UPD,
3691 ARM::VST4q16Pseudo_UPD,
3692 ARM::VST4q32Pseudo_UPD };
3693 static const uint16_t QOpcodes1[] = { ARM::VST4q8oddPseudo,
3694 ARM::VST4q16oddPseudo,
3695 ARM::VST4q32oddPseudo };
3696 SelectVST(N, false, 4, DOpcodes, QOpcodes0, QOpcodes1);
3700 case Intrinsic::arm_neon_vst2lane: {
3701 static const uint16_t DOpcodes[] = { ARM::VST2LNd8Pseudo,
3702 ARM::VST2LNd16Pseudo,
3703 ARM::VST2LNd32Pseudo };
3704 static const uint16_t QOpcodes[] = { ARM::VST2LNq16Pseudo,
3705 ARM::VST2LNq32Pseudo };
3706 SelectVLDSTLane(N, false, false, 2, DOpcodes, QOpcodes);
3710 case Intrinsic::arm_neon_vst3lane: {
3711 static const uint16_t DOpcodes[] = { ARM::VST3LNd8Pseudo,
3712 ARM::VST3LNd16Pseudo,
3713 ARM::VST3LNd32Pseudo };
3714 static const uint16_t QOpcodes[] = { ARM::VST3LNq16Pseudo,
3715 ARM::VST3LNq32Pseudo };
3716 SelectVLDSTLane(N, false, false, 3, DOpcodes, QOpcodes);
3720 case Intrinsic::arm_neon_vst4lane: {
3721 static const uint16_t DOpcodes[] = { ARM::VST4LNd8Pseudo,
3722 ARM::VST4LNd16Pseudo,
3723 ARM::VST4LNd32Pseudo };
3724 static const uint16_t QOpcodes[] = { ARM::VST4LNq16Pseudo,
3725 ARM::VST4LNq32Pseudo };
3726 SelectVLDSTLane(N, false, false, 4, DOpcodes, QOpcodes);
3733 case ISD::INTRINSIC_WO_CHAIN: {
3734 unsigned IntNo = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue();
3739 case Intrinsic::arm_neon_vtbl2:
3740 SelectVTBL(N, false, 2, ARM::VTBL2);
3742 case Intrinsic::arm_neon_vtbl3:
3743 SelectVTBL(N, false, 3, ARM::VTBL3Pseudo);
3745 case Intrinsic::arm_neon_vtbl4:
3746 SelectVTBL(N, false, 4, ARM::VTBL4Pseudo);
3749 case Intrinsic::arm_neon_vtbx2:
3750 SelectVTBL(N, true, 2, ARM::VTBX2);
3752 case Intrinsic::arm_neon_vtbx3:
3753 SelectVTBL(N, true, 3, ARM::VTBX3Pseudo);
3755 case Intrinsic::arm_neon_vtbx4:
3756 SelectVTBL(N, true, 4, ARM::VTBX4Pseudo);
3762 case ARMISD::VTBL1: {
3764 EVT VT = N->getValueType(0);
3765 SDValue Ops[] = {N->getOperand(0), N->getOperand(1),
3766 getAL(CurDAG, dl), // Predicate
3767 CurDAG->getRegister(0, MVT::i32)}; // Predicate Register
3768 ReplaceNode(N, CurDAG->getMachineNode(ARM::VTBL1, dl, VT, Ops));
3771 case ARMISD::VTBL2: {
3773 EVT VT = N->getValueType(0);
3775 // Form a REG_SEQUENCE to force register allocation.
3776 SDValue V0 = N->getOperand(0);
3777 SDValue V1 = N->getOperand(1);
3778 SDValue RegSeq = SDValue(createDRegPairNode(MVT::v16i8, V0, V1), 0);
3780 SDValue Ops[] = {RegSeq, N->getOperand(2), getAL(CurDAG, dl), // Predicate
3781 CurDAG->getRegister(0, MVT::i32)}; // Predicate Register
3782 ReplaceNode(N, CurDAG->getMachineNode(ARM::VTBL2, dl, VT, Ops));
3786 case ISD::ATOMIC_CMP_SWAP:
3794 // Inspect a register string of the form
3795 // cp<coprocessor>:<opc1>:c<CRn>:c<CRm>:<opc2> (32bit) or
3796 // cp<coprocessor>:<opc1>:c<CRm> (64bit) inspect the fields of the string
3797 // and obtain the integer operands from them, adding these operands to the
3799 static void getIntOperandsFromRegisterString(StringRef RegString,
3800 SelectionDAG *CurDAG,
3802 std::vector<SDValue> &Ops) {
3803 SmallVector<StringRef, 5> Fields;
3804 RegString.split(Fields, ':');
3806 if (Fields.size() > 1) {
3807 bool AllIntFields = true;
3809 for (StringRef Field : Fields) {
3810 // Need to trim out leading 'cp' characters and get the integer field.
3812 AllIntFields &= !Field.trim("CPcp").getAsInteger(10, IntField);
3813 Ops.push_back(CurDAG->getTargetConstant(IntField, DL, MVT::i32));
3816 assert(AllIntFields &&
3817 "Unexpected non-integer value in special register string.");
3821 // Maps a Banked Register string to its mask value. The mask value returned is
3822 // for use in the MRSbanked / MSRbanked instruction nodes as the Banked Register
3823 // mask operand, which expresses which register is to be used, e.g. r8, and in
3824 // which mode it is to be used, e.g. usr. Returns -1 to signify that the string
3826 static inline int getBankedRegisterMask(StringRef RegString) {
3827 return StringSwitch<int>(RegString.lower())
3828 .Case("r8_usr", 0x00)
3829 .Case("r9_usr", 0x01)
3830 .Case("r10_usr", 0x02)
3831 .Case("r11_usr", 0x03)
3832 .Case("r12_usr", 0x04)
3833 .Case("sp_usr", 0x05)
3834 .Case("lr_usr", 0x06)
3835 .Case("r8_fiq", 0x08)
3836 .Case("r9_fiq", 0x09)
3837 .Case("r10_fiq", 0x0a)
3838 .Case("r11_fiq", 0x0b)
3839 .Case("r12_fiq", 0x0c)
3840 .Case("sp_fiq", 0x0d)
3841 .Case("lr_fiq", 0x0e)
3842 .Case("lr_irq", 0x10)
3843 .Case("sp_irq", 0x11)
3844 .Case("lr_svc", 0x12)
3845 .Case("sp_svc", 0x13)
3846 .Case("lr_abt", 0x14)
3847 .Case("sp_abt", 0x15)
3848 .Case("lr_und", 0x16)
3849 .Case("sp_und", 0x17)
3850 .Case("lr_mon", 0x1c)
3851 .Case("sp_mon", 0x1d)
3852 .Case("elr_hyp", 0x1e)
3853 .Case("sp_hyp", 0x1f)
3854 .Case("spsr_fiq", 0x2e)
3855 .Case("spsr_irq", 0x30)
3856 .Case("spsr_svc", 0x32)
3857 .Case("spsr_abt", 0x34)
3858 .Case("spsr_und", 0x36)
3859 .Case("spsr_mon", 0x3c)
3860 .Case("spsr_hyp", 0x3e)
3864 // Maps a MClass special register string to its value for use in the
3865 // t2MRS_M / t2MSR_M instruction nodes as the SYSm value operand.
3866 // Returns -1 to signify that the string was invalid.
3867 static inline int getMClassRegisterSYSmValueMask(StringRef RegString) {
3868 return StringSwitch<int>(RegString.lower())
3878 .Case("primask", 0x10)
3879 .Case("basepri", 0x11)
3880 .Case("basepri_max", 0x12)
3881 .Case("faultmask", 0x13)
3882 .Case("control", 0x14)
3883 .Case("msplim", 0x0a)
3884 .Case("psplim", 0x0b)
3889 // The flags here are common to those allowed for apsr in the A class cores and
3890 // those allowed for the special registers in the M class cores. Returns a
3891 // value representing which flags were present, -1 if invalid.
3892 static inline int getMClassFlagsMask(StringRef Flags) {
3893 return StringSwitch<int>(Flags)
3894 .Case("", 0x2) // no flags means nzcvq for psr registers, and 0x2 is
3895 // correct when flags are not permitted
3898 .Case("nzcvqg", 0x3)
3902 static int getMClassRegisterMask(StringRef Reg, StringRef Flags, bool IsRead,
3903 const ARMSubtarget *Subtarget) {
3904 // Ensure that the register (without flags) was a valid M Class special
3906 int SYSmvalue = getMClassRegisterSYSmValueMask(Reg);
3907 if (SYSmvalue == -1)
3910 // basepri, basepri_max and faultmask are only valid for V7m.
3911 if (!Subtarget->hasV7Ops() && SYSmvalue >= 0x11 && SYSmvalue <= 0x13)
3914 if (Subtarget->has8MSecExt() && Flags.lower() == "ns") {
3919 if (!Subtarget->has8MSecExt() &&
3920 (SYSmvalue == 0xa || SYSmvalue == 0xb || SYSmvalue > 0x14))
3923 if (!Subtarget->hasV8MMainlineOps() &&
3924 (SYSmvalue == 0x8a || SYSmvalue == 0x8b || SYSmvalue == 0x91 ||
3928 // If it was a read then we won't be expecting flags and so at this point
3929 // we can return the mask.
3937 // We know we are now handling a write so need to get the mask for the flags.
3938 int Mask = getMClassFlagsMask(Flags);
3940 // Only apsr, iapsr, eapsr, xpsr can have flags. The other register values
3941 // shouldn't have flags present.
3942 if ((SYSmvalue < 0x4 && Mask == -1) || (SYSmvalue > 0x4 && !Flags.empty()))
3945 // The _g and _nzcvqg versions are only valid if the DSP extension is
3947 if (!Subtarget->hasDSP() && (Mask & 0x1))
3950 // The register was valid so need to put the mask in the correct place
3951 // (the flags need to be in bits 11-10) and combine with the SYSmvalue to
3952 // construct the operand for the instruction node.
3953 return SYSmvalue | Mask << 10;
3956 static int getARClassRegisterMask(StringRef Reg, StringRef Flags) {
3957 // The mask operand contains the special register (R Bit) in bit 4, whether
3958 // the register is spsr (R bit is 1) or one of cpsr/apsr (R bit is 0), and
3959 // bits 3-0 contains the fields to be accessed in the special register, set by
3960 // the flags provided with the register.
3962 if (Reg == "apsr") {
3963 // The flags permitted for apsr are the same flags that are allowed in
3964 // M class registers. We get the flag value and then shift the flags into
3965 // the correct place to combine with the mask.
3966 Mask = getMClassFlagsMask(Flags);
3972 if (Reg != "cpsr" && Reg != "spsr") {
3976 // This is the same as if the flags were "fc"
3977 if (Flags.empty() || Flags == "all")
3980 // Inspect the supplied flags string and set the bits in the mask for
3981 // the relevant and valid flags allowed for cpsr and spsr.
3982 for (char Flag : Flags) {
4001 // This avoids allowing strings where the same flag bit appears twice.
4002 if (!FlagVal || (Mask & FlagVal))
4007 // If the register is spsr then we need to set the R bit.
4014 // Lower the read_register intrinsic to ARM specific DAG nodes
4015 // using the supplied metadata string to select the instruction node to use
4016 // and the registers/masks to construct as operands for the node.
4017 bool ARMDAGToDAGISel::tryReadRegister(SDNode *N){
4018 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
4019 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
4020 bool IsThumb2 = Subtarget->isThumb2();
4023 std::vector<SDValue> Ops;
4024 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
4027 // If the special register string was constructed of fields (as defined
4028 // in the ACLE) then need to lower to MRC node (32 bit) or
4029 // MRRC node(64 bit), we can make the distinction based on the number of
4030 // operands we have.
4032 SmallVector<EVT, 3> ResTypes;
4033 if (Ops.size() == 5){
4034 Opcode = IsThumb2 ? ARM::t2MRC : ARM::MRC;
4035 ResTypes.append({ MVT::i32, MVT::Other });
4037 assert(Ops.size() == 3 &&
4038 "Invalid number of fields in special register string.");
4039 Opcode = IsThumb2 ? ARM::t2MRRC : ARM::MRRC;
4040 ResTypes.append({ MVT::i32, MVT::i32, MVT::Other });
4043 Ops.push_back(getAL(CurDAG, DL));
4044 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
4045 Ops.push_back(N->getOperand(0));
4046 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, ResTypes, Ops));
4050 std::string SpecialReg = RegString->getString().lower();
4052 int BankedReg = getBankedRegisterMask(SpecialReg);
4053 if (BankedReg != -1) {
4054 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32),
4055 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4058 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSbanked : ARM::MRSbanked,
4059 DL, MVT::i32, MVT::Other, Ops));
4063 // The VFP registers are read by creating SelectionDAG nodes with opcodes
4064 // corresponding to the register that is being read from. So we switch on the
4065 // string to find which opcode we need to use.
4066 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
4067 .Case("fpscr", ARM::VMRS)
4068 .Case("fpexc", ARM::VMRS_FPEXC)
4069 .Case("fpsid", ARM::VMRS_FPSID)
4070 .Case("mvfr0", ARM::VMRS_MVFR0)
4071 .Case("mvfr1", ARM::VMRS_MVFR1)
4072 .Case("mvfr2", ARM::VMRS_MVFR2)
4073 .Case("fpinst", ARM::VMRS_FPINST)
4074 .Case("fpinst2", ARM::VMRS_FPINST2)
4077 // If an opcode was found then we can lower the read to a VFP instruction.
4079 if (!Subtarget->hasVFP2())
4081 if (Opcode == ARM::VMRS_MVFR2 && !Subtarget->hasFPARMv8())
4084 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4087 CurDAG->getMachineNode(Opcode, DL, MVT::i32, MVT::Other, Ops));
4091 // If the target is M Class then need to validate that the register string
4092 // is an acceptable value, so check that a mask can be constructed from the
4094 if (Subtarget->isMClass()) {
4095 StringRef Flags = "", Reg = SpecialReg;
4096 if (Reg.endswith("_ns")) {
4098 Reg = Reg.drop_back(3);
4101 int SYSmValue = getMClassRegisterMask(Reg, Flags, true, Subtarget);
4102 if (SYSmValue == -1)
4105 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
4106 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4109 N, CurDAG->getMachineNode(ARM::t2MRS_M, DL, MVT::i32, MVT::Other, Ops));
4113 // Here we know the target is not M Class so we need to check if it is one
4114 // of the remaining possible values which are apsr, cpsr or spsr.
4115 if (SpecialReg == "apsr" || SpecialReg == "cpsr") {
4116 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4118 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRS_AR : ARM::MRS,
4119 DL, MVT::i32, MVT::Other, Ops));
4123 if (SpecialReg == "spsr") {
4124 Ops = { getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4127 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MRSsys_AR : ARM::MRSsys, DL,
4128 MVT::i32, MVT::Other, Ops));
4135 // Lower the write_register intrinsic to ARM specific DAG nodes
4136 // using the supplied metadata string to select the instruction node to use
4137 // and the registers/masks to use in the nodes
4138 bool ARMDAGToDAGISel::tryWriteRegister(SDNode *N){
4139 const MDNodeSDNode *MD = dyn_cast<MDNodeSDNode>(N->getOperand(1));
4140 const MDString *RegString = dyn_cast<MDString>(MD->getMD()->getOperand(0));
4141 bool IsThumb2 = Subtarget->isThumb2();
4144 std::vector<SDValue> Ops;
4145 getIntOperandsFromRegisterString(RegString->getString(), CurDAG, DL, Ops);
4148 // If the special register string was constructed of fields (as defined
4149 // in the ACLE) then need to lower to MCR node (32 bit) or
4150 // MCRR node(64 bit), we can make the distinction based on the number of
4151 // operands we have.
4153 if (Ops.size() == 5) {
4154 Opcode = IsThumb2 ? ARM::t2MCR : ARM::MCR;
4155 Ops.insert(Ops.begin()+2, N->getOperand(2));
4157 assert(Ops.size() == 3 &&
4158 "Invalid number of fields in special register string.");
4159 Opcode = IsThumb2 ? ARM::t2MCRR : ARM::MCRR;
4160 SDValue WriteValue[] = { N->getOperand(2), N->getOperand(3) };
4161 Ops.insert(Ops.begin()+2, WriteValue, WriteValue+2);
4164 Ops.push_back(getAL(CurDAG, DL));
4165 Ops.push_back(CurDAG->getRegister(0, MVT::i32));
4166 Ops.push_back(N->getOperand(0));
4168 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
4172 std::string SpecialReg = RegString->getString().lower();
4173 int BankedReg = getBankedRegisterMask(SpecialReg);
4174 if (BankedReg != -1) {
4175 Ops = { CurDAG->getTargetConstant(BankedReg, DL, MVT::i32), N->getOperand(2),
4176 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4179 N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSRbanked : ARM::MSRbanked,
4180 DL, MVT::Other, Ops));
4184 // The VFP registers are written to by creating SelectionDAG nodes with
4185 // opcodes corresponding to the register that is being written. So we switch
4186 // on the string to find which opcode we need to use.
4187 unsigned Opcode = StringSwitch<unsigned>(SpecialReg)
4188 .Case("fpscr", ARM::VMSR)
4189 .Case("fpexc", ARM::VMSR_FPEXC)
4190 .Case("fpsid", ARM::VMSR_FPSID)
4191 .Case("fpinst", ARM::VMSR_FPINST)
4192 .Case("fpinst2", ARM::VMSR_FPINST2)
4196 if (!Subtarget->hasVFP2())
4198 Ops = { N->getOperand(2), getAL(CurDAG, DL),
4199 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
4200 ReplaceNode(N, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops));
4204 std::pair<StringRef, StringRef> Fields;
4205 Fields = StringRef(SpecialReg).rsplit('_');
4206 std::string Reg = Fields.first.str();
4207 StringRef Flags = Fields.second;
4209 // If the target was M Class then need to validate the special register value
4210 // and retrieve the mask for use in the instruction node.
4211 if (Subtarget->isMClass()) {
4212 // basepri_max gets split so need to correct Reg and Flags.
4213 if (SpecialReg == "basepri_max") {
4217 int SYSmValue = getMClassRegisterMask(Reg, Flags, false, Subtarget);
4218 if (SYSmValue == -1)
4221 SDValue Ops[] = { CurDAG->getTargetConstant(SYSmValue, DL, MVT::i32),
4222 N->getOperand(2), getAL(CurDAG, DL),
4223 CurDAG->getRegister(0, MVT::i32), N->getOperand(0) };
4224 ReplaceNode(N, CurDAG->getMachineNode(ARM::t2MSR_M, DL, MVT::Other, Ops));
4228 // We then check to see if a valid mask can be constructed for one of the
4229 // register string values permitted for the A and R class cores. These values
4230 // are apsr, spsr and cpsr; these are also valid on older cores.
4231 int Mask = getARClassRegisterMask(Reg, Flags);
4233 Ops = { CurDAG->getTargetConstant(Mask, DL, MVT::i32), N->getOperand(2),
4234 getAL(CurDAG, DL), CurDAG->getRegister(0, MVT::i32),
4236 ReplaceNode(N, CurDAG->getMachineNode(IsThumb2 ? ARM::t2MSR_AR : ARM::MSR,
4237 DL, MVT::Other, Ops));
4244 bool ARMDAGToDAGISel::tryInlineAsm(SDNode *N){
4245 std::vector<SDValue> AsmNodeOperands;
4246 unsigned Flag, Kind;
4247 bool Changed = false;
4248 unsigned NumOps = N->getNumOperands();
4250 // Normally, i64 data is bounded to two arbitrary GRPs for "%r" constraint.
4251 // However, some instrstions (e.g. ldrexd/strexd in ARM mode) require
4252 // (even/even+1) GPRs and use %n and %Hn to refer to the individual regs
4253 // respectively. Since there is no constraint to explicitly specify a
4254 // reg pair, we use GPRPair reg class for "%r" for 64-bit data. For Thumb,
4255 // the 64-bit data may be referred by H, Q, R modifiers, so we still pack
4256 // them into a GPRPair.
4259 SDValue Glue = N->getGluedNode() ? N->getOperand(NumOps-1)
4260 : SDValue(nullptr,0);
4262 SmallVector<bool, 8> OpChanged;
4263 // Glue node will be appended late.
4264 for(unsigned i = 0, e = N->getGluedNode() ? NumOps - 1 : NumOps; i < e; ++i) {
4265 SDValue op = N->getOperand(i);
4266 AsmNodeOperands.push_back(op);
4268 if (i < InlineAsm::Op_FirstOperand)
4271 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(i))) {
4272 Flag = C->getZExtValue();
4273 Kind = InlineAsm::getKind(Flag);
4278 // Immediate operands to inline asm in the SelectionDAG are modeled with
4279 // two operands. The first is a constant of value InlineAsm::Kind_Imm, and
4280 // the second is a constant with the value of the immediate. If we get here
4281 // and we have a Kind_Imm, skip the next operand, and continue.
4282 if (Kind == InlineAsm::Kind_Imm) {
4283 SDValue op = N->getOperand(++i);
4284 AsmNodeOperands.push_back(op);
4288 unsigned NumRegs = InlineAsm::getNumOperandRegisters(Flag);
4290 OpChanged.push_back(false);
4292 unsigned DefIdx = 0;
4293 bool IsTiedToChangedOp = false;
4294 // If it's a use that is tied with a previous def, it has no
4295 // reg class constraint.
4296 if (Changed && InlineAsm::isUseOperandTiedToDef(Flag, DefIdx))
4297 IsTiedToChangedOp = OpChanged[DefIdx];
4299 // Memory operands to inline asm in the SelectionDAG are modeled with two
4300 // operands: a constant of value InlineAsm::Kind_Mem followed by the input
4301 // operand. If we get here and we have a Kind_Mem, skip the next operand (so
4302 // it doesn't get misinterpreted), and continue. We do this here because
4303 // it's important to update the OpChanged array correctly before moving on.
4304 if (Kind == InlineAsm::Kind_Mem) {
4305 SDValue op = N->getOperand(++i);
4306 AsmNodeOperands.push_back(op);
4310 if (Kind != InlineAsm::Kind_RegUse && Kind != InlineAsm::Kind_RegDef
4311 && Kind != InlineAsm::Kind_RegDefEarlyClobber)
4315 bool HasRC = InlineAsm::hasRegClassConstraint(Flag, RC);
4316 if ((!IsTiedToChangedOp && (!HasRC || RC != ARM::GPRRegClassID))
4320 assert((i+2 < NumOps) && "Invalid number of operands in inline asm");
4321 SDValue V0 = N->getOperand(i+1);
4322 SDValue V1 = N->getOperand(i+2);
4323 unsigned Reg0 = cast<RegisterSDNode>(V0)->getReg();
4324 unsigned Reg1 = cast<RegisterSDNode>(V1)->getReg();
4326 MachineRegisterInfo &MRI = MF->getRegInfo();
4328 if (Kind == InlineAsm::Kind_RegDef ||
4329 Kind == InlineAsm::Kind_RegDefEarlyClobber) {
4330 // Replace the two GPRs with 1 GPRPair and copy values from GPRPair to
4331 // the original GPRs.
4333 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
4334 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
4335 SDValue Chain = SDValue(N,0);
4337 SDNode *GU = N->getGluedUser();
4338 SDValue RegCopy = CurDAG->getCopyFromReg(Chain, dl, GPVR, MVT::Untyped,
4341 // Extract values from a GPRPair reg and copy to the original GPR reg.
4342 SDValue Sub0 = CurDAG->getTargetExtractSubreg(ARM::gsub_0, dl, MVT::i32,
4344 SDValue Sub1 = CurDAG->getTargetExtractSubreg(ARM::gsub_1, dl, MVT::i32,
4346 SDValue T0 = CurDAG->getCopyToReg(Sub0, dl, Reg0, Sub0,
4347 RegCopy.getValue(1));
4348 SDValue T1 = CurDAG->getCopyToReg(Sub1, dl, Reg1, Sub1, T0.getValue(1));
4350 // Update the original glue user.
4351 std::vector<SDValue> Ops(GU->op_begin(), GU->op_end()-1);
4352 Ops.push_back(T1.getValue(1));
4353 CurDAG->UpdateNodeOperands(GU, Ops);
4356 // For Kind == InlineAsm::Kind_RegUse, we first copy two GPRs into a
4357 // GPRPair and then pass the GPRPair to the inline asm.
4358 SDValue Chain = AsmNodeOperands[InlineAsm::Op_InputChain];
4360 // As REG_SEQ doesn't take RegisterSDNode, we copy them first.
4361 SDValue T0 = CurDAG->getCopyFromReg(Chain, dl, Reg0, MVT::i32,
4363 SDValue T1 = CurDAG->getCopyFromReg(Chain, dl, Reg1, MVT::i32,
4365 SDValue Pair = SDValue(createGPRPairNode(MVT::Untyped, T0, T1), 0);
4367 // Copy REG_SEQ into a GPRPair-typed VR and replace the original two
4368 // i32 VRs of inline asm with it.
4369 unsigned GPVR = MRI.createVirtualRegister(&ARM::GPRPairRegClass);
4370 PairedReg = CurDAG->getRegister(GPVR, MVT::Untyped);
4371 Chain = CurDAG->getCopyToReg(T1, dl, GPVR, Pair, T1.getValue(1));
4373 AsmNodeOperands[InlineAsm::Op_InputChain] = Chain;
4374 Glue = Chain.getValue(1);
4379 if(PairedReg.getNode()) {
4380 OpChanged[OpChanged.size() -1 ] = true;
4381 Flag = InlineAsm::getFlagWord(Kind, 1 /* RegNum*/);
4382 if (IsTiedToChangedOp)
4383 Flag = InlineAsm::getFlagWordForMatchingOp(Flag, DefIdx);
4385 Flag = InlineAsm::getFlagWordForRegClass(Flag, ARM::GPRPairRegClassID);
4386 // Replace the current flag.
4387 AsmNodeOperands[AsmNodeOperands.size() -1] = CurDAG->getTargetConstant(
4388 Flag, dl, MVT::i32);
4389 // Add the new register node and skip the original two GPRs.
4390 AsmNodeOperands.push_back(PairedReg);
4391 // Skip the next two GPRs.
4397 AsmNodeOperands.push_back(Glue);
4401 SDValue New = CurDAG->getNode(ISD::INLINEASM, SDLoc(N),
4402 CurDAG->getVTList(MVT::Other, MVT::Glue), AsmNodeOperands);
4404 ReplaceNode(N, New.getNode());
4409 bool ARMDAGToDAGISel::
4410 SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID,
4411 std::vector<SDValue> &OutOps) {
4412 switch(ConstraintID) {
4414 llvm_unreachable("Unexpected asm memory constraint");
4415 case InlineAsm::Constraint_i:
4416 // FIXME: It seems strange that 'i' is needed here since it's supposed to
4417 // be an immediate and not a memory constraint.
4419 case InlineAsm::Constraint_m:
4420 case InlineAsm::Constraint_o:
4421 case InlineAsm::Constraint_Q:
4422 case InlineAsm::Constraint_Um:
4423 case InlineAsm::Constraint_Un:
4424 case InlineAsm::Constraint_Uq:
4425 case InlineAsm::Constraint_Us:
4426 case InlineAsm::Constraint_Ut:
4427 case InlineAsm::Constraint_Uv:
4428 case InlineAsm::Constraint_Uy:
4429 // Require the address to be in a register. That is safe for all ARM
4430 // variants and it is hard to do anything much smarter without knowing
4431 // how the operand is used.
4432 OutOps.push_back(Op);
4438 /// createARMISelDag - This pass converts a legalized DAG into a
4439 /// ARM-specific DAG, ready for instruction scheduling.
4441 FunctionPass *llvm::createARMISelDag(ARMBaseTargetMachine &TM,
4442 CodeGenOpt::Level OptLevel) {
4443 return new ARMDAGToDAGISel(TM, OptLevel);